]> code.delx.au - gnu-emacs/blob - src/xdisp.c
(display_mode_element): Call display_string with correct
[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 around tool bar buttons in pixels. */
273
274 Lisp_Object Vtool_bar_button_margin;
275
276 /* Thickness of shadow to draw around tool bar buttons. */
277
278 EMACS_INT tool_bar_button_relief;
279
280 /* Non-zero means automatically resize tool-bars so that all tool-bar
281 items are visible, and no blank lines remain. */
282
283 int auto_resize_tool_bars_p;
284
285 /* Non-zero means draw block and hollow cursor as wide as the glyph
286 under it. For example, if a block cursor is over a tab, it will be
287 drawn as wide as that tab on the display. */
288
289 int x_stretch_cursor_p;
290
291 /* Non-nil means don't actually do any redisplay. */
292
293 Lisp_Object Vinhibit_redisplay, Qinhibit_redisplay;
294
295 /* Non-zero means Lisp evaluation during redisplay is inhibited. */
296
297 int inhibit_eval_during_redisplay;
298
299 /* Names of text properties relevant for redisplay. */
300
301 Lisp_Object Qdisplay;
302 extern Lisp_Object Qface, Qinvisible, Qwidth;
303
304 /* Symbols used in text property values. */
305
306 Lisp_Object Vdisplay_pixels_per_inch;
307 Lisp_Object Qspace, QCalign_to, QCrelative_width, QCrelative_height;
308 Lisp_Object Qleft_margin, Qright_margin, Qspace_width, Qraise;
309 Lisp_Object Qslice;
310 Lisp_Object Qcenter;
311 Lisp_Object Qmargin, Qpointer;
312 Lisp_Object Qline_height;
313 extern Lisp_Object Qheight;
314 extern Lisp_Object QCwidth, QCheight, QCascent;
315 extern Lisp_Object Qscroll_bar;
316 extern Lisp_Object Qcursor;
317
318 /* Non-nil means highlight trailing whitespace. */
319
320 Lisp_Object Vshow_trailing_whitespace;
321
322 /* Non-nil means escape non-break space and hyphens. */
323
324 Lisp_Object Vnobreak_char_display;
325
326 #ifdef HAVE_WINDOW_SYSTEM
327 extern Lisp_Object Voverflow_newline_into_fringe;
328
329 /* Test if overflow newline into fringe. Called with iterator IT
330 at or past right window margin, and with IT->current_x set. */
331
332 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(it) \
333 (!NILP (Voverflow_newline_into_fringe) \
334 && FRAME_WINDOW_P (it->f) \
335 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) > 0 \
336 && it->current_x == it->last_visible_x)
337
338 #endif /* HAVE_WINDOW_SYSTEM */
339
340 /* Non-nil means show the text cursor in void text areas
341 i.e. in blank areas after eol and eob. This used to be
342 the default in 21.3. */
343
344 Lisp_Object Vvoid_text_area_pointer;
345
346 /* Name of the face used to highlight trailing whitespace. */
347
348 Lisp_Object Qtrailing_whitespace;
349
350 /* Name and number of the face used to highlight escape glyphs. */
351
352 Lisp_Object Qescape_glyph;
353
354 /* Name and number of the face used to highlight non-breaking spaces. */
355
356 Lisp_Object Qnobreak_space;
357
358 /* The symbol `image' which is the car of the lists used to represent
359 images in Lisp. */
360
361 Lisp_Object Qimage;
362
363 /* The image map types. */
364 Lisp_Object QCmap, QCpointer;
365 Lisp_Object Qrect, Qcircle, Qpoly;
366
367 /* Non-zero means print newline to stdout before next mini-buffer
368 message. */
369
370 int noninteractive_need_newline;
371
372 /* Non-zero means print newline to message log before next message. */
373
374 static int message_log_need_newline;
375
376 /* Three markers that message_dolog uses.
377 It could allocate them itself, but that causes trouble
378 in handling memory-full errors. */
379 static Lisp_Object message_dolog_marker1;
380 static Lisp_Object message_dolog_marker2;
381 static Lisp_Object message_dolog_marker3;
382 \f
383 /* The buffer position of the first character appearing entirely or
384 partially on the line of the selected window which contains the
385 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
386 redisplay optimization in redisplay_internal. */
387
388 static struct text_pos this_line_start_pos;
389
390 /* Number of characters past the end of the line above, including the
391 terminating newline. */
392
393 static struct text_pos this_line_end_pos;
394
395 /* The vertical positions and the height of this line. */
396
397 static int this_line_vpos;
398 static int this_line_y;
399 static int this_line_pixel_height;
400
401 /* X position at which this display line starts. Usually zero;
402 negative if first character is partially visible. */
403
404 static int this_line_start_x;
405
406 /* Buffer that this_line_.* variables are referring to. */
407
408 static struct buffer *this_line_buffer;
409
410 /* Nonzero means truncate lines in all windows less wide than the
411 frame. */
412
413 int truncate_partial_width_windows;
414
415 /* A flag to control how to display unibyte 8-bit character. */
416
417 int unibyte_display_via_language_environment;
418
419 /* Nonzero means we have more than one non-mini-buffer-only frame.
420 Not guaranteed to be accurate except while parsing
421 frame-title-format. */
422
423 int multiple_frames;
424
425 Lisp_Object Vglobal_mode_string;
426
427
428 /* List of variables (symbols) which hold markers for overlay arrows.
429 The symbols on this list are examined during redisplay to determine
430 where to display overlay arrows. */
431
432 Lisp_Object Voverlay_arrow_variable_list;
433
434 /* Marker for where to display an arrow on top of the buffer text. */
435
436 Lisp_Object Voverlay_arrow_position;
437
438 /* String to display for the arrow. Only used on terminal frames. */
439
440 Lisp_Object Voverlay_arrow_string;
441
442 /* Values of those variables at last redisplay are stored as
443 properties on `overlay-arrow-position' symbol. However, if
444 Voverlay_arrow_position is a marker, last-arrow-position is its
445 numerical position. */
446
447 Lisp_Object Qlast_arrow_position, Qlast_arrow_string;
448
449 /* Alternative overlay-arrow-string and overlay-arrow-bitmap
450 properties on a symbol in overlay-arrow-variable-list. */
451
452 Lisp_Object Qoverlay_arrow_string, Qoverlay_arrow_bitmap;
453
454 /* Like mode-line-format, but for the title bar on a visible frame. */
455
456 Lisp_Object Vframe_title_format;
457
458 /* Like mode-line-format, but for the title bar on an iconified frame. */
459
460 Lisp_Object Vicon_title_format;
461
462 /* List of functions to call when a window's size changes. These
463 functions get one arg, a frame on which one or more windows' sizes
464 have changed. */
465
466 static Lisp_Object Vwindow_size_change_functions;
467
468 Lisp_Object Qmenu_bar_update_hook, Vmenu_bar_update_hook;
469
470 /* Nonzero if an overlay arrow has been displayed in this window. */
471
472 static int overlay_arrow_seen;
473
474 /* Nonzero means highlight the region even in nonselected windows. */
475
476 int highlight_nonselected_windows;
477
478 /* If cursor motion alone moves point off frame, try scrolling this
479 many lines up or down if that will bring it back. */
480
481 static EMACS_INT scroll_step;
482
483 /* Nonzero means scroll just far enough to bring point back on the
484 screen, when appropriate. */
485
486 static EMACS_INT scroll_conservatively;
487
488 /* Recenter the window whenever point gets within this many lines of
489 the top or bottom of the window. This value is translated into a
490 pixel value by multiplying it with FRAME_LINE_HEIGHT, which means
491 that there is really a fixed pixel height scroll margin. */
492
493 EMACS_INT scroll_margin;
494
495 /* Number of windows showing the buffer of the selected window (or
496 another buffer with the same base buffer). keyboard.c refers to
497 this. */
498
499 int buffer_shared;
500
501 /* Vector containing glyphs for an ellipsis `...'. */
502
503 static Lisp_Object default_invis_vector[3];
504
505 /* Zero means display the mode-line/header-line/menu-bar in the default face
506 (this slightly odd definition is for compatibility with previous versions
507 of emacs), non-zero means display them using their respective faces.
508
509 This variable is deprecated. */
510
511 int mode_line_inverse_video;
512
513 /* Prompt to display in front of the mini-buffer contents. */
514
515 Lisp_Object minibuf_prompt;
516
517 /* Width of current mini-buffer prompt. Only set after display_line
518 of the line that contains the prompt. */
519
520 int minibuf_prompt_width;
521
522 /* This is the window where the echo area message was displayed. It
523 is always a mini-buffer window, but it may not be the same window
524 currently active as a mini-buffer. */
525
526 Lisp_Object echo_area_window;
527
528 /* List of pairs (MESSAGE . MULTIBYTE). The function save_message
529 pushes the current message and the value of
530 message_enable_multibyte on the stack, the function restore_message
531 pops the stack and displays MESSAGE again. */
532
533 Lisp_Object Vmessage_stack;
534
535 /* Nonzero means multibyte characters were enabled when the echo area
536 message was specified. */
537
538 int message_enable_multibyte;
539
540 /* Nonzero if we should redraw the mode lines on the next redisplay. */
541
542 int update_mode_lines;
543
544 /* Nonzero if window sizes or contents have changed since last
545 redisplay that finished. */
546
547 int windows_or_buffers_changed;
548
549 /* Nonzero means a frame's cursor type has been changed. */
550
551 int cursor_type_changed;
552
553 /* Nonzero after display_mode_line if %l was used and it displayed a
554 line number. */
555
556 int line_number_displayed;
557
558 /* Maximum buffer size for which to display line numbers. */
559
560 Lisp_Object Vline_number_display_limit;
561
562 /* Line width to consider when repositioning for line number display. */
563
564 static EMACS_INT line_number_display_limit_width;
565
566 /* Number of lines to keep in the message log buffer. t means
567 infinite. nil means don't log at all. */
568
569 Lisp_Object Vmessage_log_max;
570
571 /* The name of the *Messages* buffer, a string. */
572
573 static Lisp_Object Vmessages_buffer_name;
574
575 /* Index 0 is the buffer that holds the current (desired) echo area message,
576 or nil if none is desired right now.
577
578 Index 1 is the buffer that holds the previously displayed echo area message,
579 or nil to indicate no message. This is normally what's on the screen now.
580
581 These two can point to the same buffer. That happens when the last
582 message output by the user (or made by echoing) has been displayed. */
583
584 Lisp_Object echo_area_buffer[2];
585
586 /* Permanent pointers to the two buffers that are used for echo area
587 purposes. Once the two buffers are made, and their pointers are
588 placed here, these two slots remain unchanged unless those buffers
589 need to be created afresh. */
590
591 static Lisp_Object echo_buffer[2];
592
593 /* A vector saved used in with_area_buffer to reduce consing. */
594
595 static Lisp_Object Vwith_echo_area_save_vector;
596
597 /* Non-zero means display_echo_area should display the last echo area
598 message again. Set by redisplay_preserve_echo_area. */
599
600 static int display_last_displayed_message_p;
601
602 /* Nonzero if echo area is being used by print; zero if being used by
603 message. */
604
605 int message_buf_print;
606
607 /* The symbol `inhibit-menubar-update' and its DEFVAR_BOOL variable. */
608
609 Lisp_Object Qinhibit_menubar_update;
610 int inhibit_menubar_update;
611
612 /* Maximum height for resizing mini-windows. Either a float
613 specifying a fraction of the available height, or an integer
614 specifying a number of lines. */
615
616 Lisp_Object Vmax_mini_window_height;
617
618 /* Non-zero means messages should be displayed with truncated
619 lines instead of being continued. */
620
621 int message_truncate_lines;
622 Lisp_Object Qmessage_truncate_lines;
623
624 /* Set to 1 in clear_message to make redisplay_internal aware
625 of an emptied echo area. */
626
627 static int message_cleared_p;
628
629 /* How to blink the default frame cursor off. */
630 Lisp_Object Vblink_cursor_alist;
631
632 /* A scratch glyph row with contents used for generating truncation
633 glyphs. Also used in direct_output_for_insert. */
634
635 #define MAX_SCRATCH_GLYPHS 100
636 struct glyph_row scratch_glyph_row;
637 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
638
639 /* Ascent and height of the last line processed by move_it_to. */
640
641 static int last_max_ascent, last_height;
642
643 /* Non-zero if there's a help-echo in the echo area. */
644
645 int help_echo_showing_p;
646
647 /* If >= 0, computed, exact values of mode-line and header-line height
648 to use in the macros CURRENT_MODE_LINE_HEIGHT and
649 CURRENT_HEADER_LINE_HEIGHT. */
650
651 int current_mode_line_height, current_header_line_height;
652
653 /* The maximum distance to look ahead for text properties. Values
654 that are too small let us call compute_char_face and similar
655 functions too often which is expensive. Values that are too large
656 let us call compute_char_face and alike too often because we
657 might not be interested in text properties that far away. */
658
659 #define TEXT_PROP_DISTANCE_LIMIT 100
660
661 #if GLYPH_DEBUG
662
663 /* Variables to turn off display optimizations from Lisp. */
664
665 int inhibit_try_window_id, inhibit_try_window_reusing;
666 int inhibit_try_cursor_movement;
667
668 /* Non-zero means print traces of redisplay if compiled with
669 GLYPH_DEBUG != 0. */
670
671 int trace_redisplay_p;
672
673 #endif /* GLYPH_DEBUG */
674
675 #ifdef DEBUG_TRACE_MOVE
676 /* Non-zero means trace with TRACE_MOVE to stderr. */
677 int trace_move;
678
679 #define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
680 #else
681 #define TRACE_MOVE(x) (void) 0
682 #endif
683
684 /* Non-zero means automatically scroll windows horizontally to make
685 point visible. */
686
687 int automatic_hscrolling_p;
688
689 /* How close to the margin can point get before the window is scrolled
690 horizontally. */
691 EMACS_INT hscroll_margin;
692
693 /* How much to scroll horizontally when point is inside the above margin. */
694 Lisp_Object Vhscroll_step;
695
696 /* The variable `resize-mini-windows'. If nil, don't resize
697 mini-windows. If t, always resize them to fit the text they
698 display. If `grow-only', let mini-windows grow only until they
699 become empty. */
700
701 Lisp_Object Vresize_mini_windows;
702
703 /* Buffer being redisplayed -- for redisplay_window_error. */
704
705 struct buffer *displayed_buffer;
706
707 /* Value returned from text property handlers (see below). */
708
709 enum prop_handled
710 {
711 HANDLED_NORMALLY,
712 HANDLED_RECOMPUTE_PROPS,
713 HANDLED_OVERLAY_STRING_CONSUMED,
714 HANDLED_RETURN
715 };
716
717 /* A description of text properties that redisplay is interested
718 in. */
719
720 struct props
721 {
722 /* The name of the property. */
723 Lisp_Object *name;
724
725 /* A unique index for the property. */
726 enum prop_idx idx;
727
728 /* A handler function called to set up iterator IT from the property
729 at IT's current position. Value is used to steer handle_stop. */
730 enum prop_handled (*handler) P_ ((struct it *it));
731 };
732
733 static enum prop_handled handle_face_prop P_ ((struct it *));
734 static enum prop_handled handle_invisible_prop P_ ((struct it *));
735 static enum prop_handled handle_display_prop P_ ((struct it *));
736 static enum prop_handled handle_composition_prop P_ ((struct it *));
737 static enum prop_handled handle_overlay_change P_ ((struct it *));
738 static enum prop_handled handle_fontified_prop P_ ((struct it *));
739
740 /* Properties handled by iterators. */
741
742 static struct props it_props[] =
743 {
744 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
745 /* Handle `face' before `display' because some sub-properties of
746 `display' need to know the face. */
747 {&Qface, FACE_PROP_IDX, handle_face_prop},
748 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
749 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
750 {&Qcomposition, COMPOSITION_PROP_IDX, handle_composition_prop},
751 {NULL, 0, NULL}
752 };
753
754 /* Value is the position described by X. If X is a marker, value is
755 the marker_position of X. Otherwise, value is X. */
756
757 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
758
759 /* Enumeration returned by some move_it_.* functions internally. */
760
761 enum move_it_result
762 {
763 /* Not used. Undefined value. */
764 MOVE_UNDEFINED,
765
766 /* Move ended at the requested buffer position or ZV. */
767 MOVE_POS_MATCH_OR_ZV,
768
769 /* Move ended at the requested X pixel position. */
770 MOVE_X_REACHED,
771
772 /* Move within a line ended at the end of a line that must be
773 continued. */
774 MOVE_LINE_CONTINUED,
775
776 /* Move within a line ended at the end of a line that would
777 be displayed truncated. */
778 MOVE_LINE_TRUNCATED,
779
780 /* Move within a line ended at a line end. */
781 MOVE_NEWLINE_OR_CR
782 };
783
784 /* This counter is used to clear the face cache every once in a while
785 in redisplay_internal. It is incremented for each redisplay.
786 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
787 cleared. */
788
789 #define CLEAR_FACE_CACHE_COUNT 500
790 static int clear_face_cache_count;
791
792 /* Similarly for the image cache. */
793
794 #ifdef HAVE_WINDOW_SYSTEM
795 #define CLEAR_IMAGE_CACHE_COUNT 101
796 static int clear_image_cache_count;
797 #endif
798
799 /* Record the previous terminal frame we displayed. */
800
801 static struct frame *previous_terminal_frame;
802
803 /* Non-zero while redisplay_internal is in progress. */
804
805 int redisplaying_p;
806
807 /* Non-zero means don't free realized faces. Bound while freeing
808 realized faces is dangerous because glyph matrices might still
809 reference them. */
810
811 int inhibit_free_realized_faces;
812 Lisp_Object Qinhibit_free_realized_faces;
813
814 /* If a string, XTread_socket generates an event to display that string.
815 (The display is done in read_char.) */
816
817 Lisp_Object help_echo_string;
818 Lisp_Object help_echo_window;
819 Lisp_Object help_echo_object;
820 int help_echo_pos;
821
822 /* Temporary variable for XTread_socket. */
823
824 Lisp_Object previous_help_echo_string;
825
826 /* Null glyph slice */
827
828 static struct glyph_slice null_glyph_slice = { 0, 0, 0, 0 };
829
830 \f
831 /* Function prototypes. */
832
833 static void setup_for_ellipsis P_ ((struct it *, int));
834 static void mark_window_display_accurate_1 P_ ((struct window *, int));
835 static int single_display_spec_string_p P_ ((Lisp_Object, Lisp_Object));
836 static int display_prop_string_p P_ ((Lisp_Object, Lisp_Object));
837 static int cursor_row_p P_ ((struct window *, struct glyph_row *));
838 static int redisplay_mode_lines P_ ((Lisp_Object, int));
839 static char *decode_mode_spec_coding P_ ((Lisp_Object, char *, int));
840
841 #if 0
842 static int invisible_text_between_p P_ ((struct it *, int, int));
843 #endif
844
845 static void pint2str P_ ((char *, int, int));
846 static void pint2hrstr P_ ((char *, int, int));
847 static struct text_pos run_window_scroll_functions P_ ((Lisp_Object,
848 struct text_pos));
849 static void reconsider_clip_changes P_ ((struct window *, struct buffer *));
850 static int text_outside_line_unchanged_p P_ ((struct window *, int, int));
851 static void store_mode_line_noprop_char P_ ((char));
852 static int store_mode_line_noprop P_ ((const unsigned char *, int, int));
853 static void x_consider_frame_title P_ ((Lisp_Object));
854 static void handle_stop P_ ((struct it *));
855 static int tool_bar_lines_needed P_ ((struct frame *));
856 static int single_display_spec_intangible_p P_ ((Lisp_Object));
857 static void ensure_echo_area_buffers P_ ((void));
858 static Lisp_Object unwind_with_echo_area_buffer P_ ((Lisp_Object));
859 static Lisp_Object with_echo_area_buffer_unwind_data P_ ((struct window *));
860 static int with_echo_area_buffer P_ ((struct window *, int,
861 int (*) (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT),
862 EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
863 static void clear_garbaged_frames P_ ((void));
864 static int current_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
865 static int truncate_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
866 static int set_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
867 static int display_echo_area P_ ((struct window *));
868 static int display_echo_area_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
869 static int resize_mini_window_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
870 static Lisp_Object unwind_redisplay P_ ((Lisp_Object));
871 static int string_char_and_length P_ ((const unsigned char *, int, int *));
872 static struct text_pos display_prop_end P_ ((struct it *, Lisp_Object,
873 struct text_pos));
874 static int compute_window_start_on_continuation_line P_ ((struct window *));
875 static Lisp_Object safe_eval_handler P_ ((Lisp_Object));
876 static void insert_left_trunc_glyphs P_ ((struct it *));
877 static struct glyph_row *get_overlay_arrow_glyph_row P_ ((struct window *,
878 Lisp_Object));
879 static void extend_face_to_end_of_line P_ ((struct it *));
880 static int append_space_for_newline P_ ((struct it *, int));
881 static int cursor_row_fully_visible_p P_ ((struct window *, int, int));
882 static int try_scrolling P_ ((Lisp_Object, int, EMACS_INT, EMACS_INT, int, int));
883 static int try_cursor_movement P_ ((Lisp_Object, struct text_pos, int *));
884 static int trailing_whitespace_p P_ ((int));
885 static int message_log_check_duplicate P_ ((int, int, int, int));
886 static void push_it P_ ((struct it *));
887 static void pop_it P_ ((struct it *));
888 static void sync_frame_with_window_matrix_rows P_ ((struct window *));
889 static void select_frame_for_redisplay P_ ((Lisp_Object));
890 static void redisplay_internal P_ ((int));
891 static int echo_area_display P_ ((int));
892 static void redisplay_windows P_ ((Lisp_Object));
893 static void redisplay_window P_ ((Lisp_Object, int));
894 static Lisp_Object redisplay_window_error ();
895 static Lisp_Object redisplay_window_0 P_ ((Lisp_Object));
896 static Lisp_Object redisplay_window_1 P_ ((Lisp_Object));
897 static void update_menu_bar P_ ((struct frame *, int));
898 static int try_window_reusing_current_matrix P_ ((struct window *));
899 static int try_window_id P_ ((struct window *));
900 static int display_line P_ ((struct it *));
901 static int display_mode_lines P_ ((struct window *));
902 static int display_mode_line P_ ((struct window *, enum face_id, Lisp_Object));
903 static int display_mode_element P_ ((struct it *, int, int, int, Lisp_Object, Lisp_Object, int));
904 static int store_mode_line_string P_ ((char *, Lisp_Object, int, int, int, Lisp_Object));
905 static char *decode_mode_spec P_ ((struct window *, int, int, int, int *));
906 static void display_menu_bar P_ ((struct window *));
907 static int display_count_lines P_ ((int, int, int, int, int *));
908 static int display_string P_ ((unsigned char *, Lisp_Object, Lisp_Object,
909 int, int, struct it *, int, int, int, int));
910 static void compute_line_metrics P_ ((struct it *));
911 static void run_redisplay_end_trigger_hook P_ ((struct it *));
912 static int get_overlay_strings P_ ((struct it *, int));
913 static void next_overlay_string P_ ((struct it *));
914 static void reseat P_ ((struct it *, struct text_pos, int));
915 static void reseat_1 P_ ((struct it *, struct text_pos, int));
916 static void back_to_previous_visible_line_start P_ ((struct it *));
917 void reseat_at_previous_visible_line_start P_ ((struct it *));
918 static void reseat_at_next_visible_line_start P_ ((struct it *, int));
919 static int next_element_from_ellipsis P_ ((struct it *));
920 static int next_element_from_display_vector P_ ((struct it *));
921 static int next_element_from_string P_ ((struct it *));
922 static int next_element_from_c_string P_ ((struct it *));
923 static int next_element_from_buffer P_ ((struct it *));
924 static int next_element_from_composition P_ ((struct it *));
925 static int next_element_from_image P_ ((struct it *));
926 static int next_element_from_stretch P_ ((struct it *));
927 static void load_overlay_strings P_ ((struct it *, int));
928 static int init_from_display_pos P_ ((struct it *, struct window *,
929 struct display_pos *));
930 static void reseat_to_string P_ ((struct it *, unsigned char *,
931 Lisp_Object, int, int, int, int));
932 static enum move_it_result move_it_in_display_line_to P_ ((struct it *,
933 int, int, int));
934 void move_it_vertically_backward P_ ((struct it *, int));
935 static void init_to_row_start P_ ((struct it *, struct window *,
936 struct glyph_row *));
937 static int init_to_row_end P_ ((struct it *, struct window *,
938 struct glyph_row *));
939 static void back_to_previous_line_start P_ ((struct it *));
940 static int forward_to_next_line_start P_ ((struct it *, int *));
941 static struct text_pos string_pos_nchars_ahead P_ ((struct text_pos,
942 Lisp_Object, int));
943 static struct text_pos string_pos P_ ((int, Lisp_Object));
944 static struct text_pos c_string_pos P_ ((int, unsigned char *, int));
945 static int number_of_chars P_ ((unsigned char *, int));
946 static void compute_stop_pos P_ ((struct it *));
947 static void compute_string_pos P_ ((struct text_pos *, struct text_pos,
948 Lisp_Object));
949 static int face_before_or_after_it_pos P_ ((struct it *, int));
950 static int next_overlay_change P_ ((int));
951 static int handle_single_display_spec P_ ((struct it *, Lisp_Object,
952 Lisp_Object, struct text_pos *,
953 int));
954 static int underlying_face_id P_ ((struct it *));
955 static int in_ellipses_for_invisible_text_p P_ ((struct display_pos *,
956 struct window *));
957
958 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
959 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
960
961 #ifdef HAVE_WINDOW_SYSTEM
962
963 static void update_tool_bar P_ ((struct frame *, int));
964 static void build_desired_tool_bar_string P_ ((struct frame *f));
965 static int redisplay_tool_bar P_ ((struct frame *));
966 static void display_tool_bar_line P_ ((struct it *));
967 static void notice_overwritten_cursor P_ ((struct window *,
968 enum glyph_row_area,
969 int, int, int, int));
970
971
972
973 #endif /* HAVE_WINDOW_SYSTEM */
974
975 \f
976 /***********************************************************************
977 Window display dimensions
978 ***********************************************************************/
979
980 /* Return the bottom boundary y-position for text lines in window W.
981 This is the first y position at which a line cannot start.
982 It is relative to the top of the window.
983
984 This is the height of W minus the height of a mode line, if any. */
985
986 INLINE int
987 window_text_bottom_y (w)
988 struct window *w;
989 {
990 int height = WINDOW_TOTAL_HEIGHT (w);
991
992 if (WINDOW_WANTS_MODELINE_P (w))
993 height -= CURRENT_MODE_LINE_HEIGHT (w);
994 return height;
995 }
996
997 /* Return the pixel width of display area AREA of window W. AREA < 0
998 means return the total width of W, not including fringes to
999 the left and right of the window. */
1000
1001 INLINE int
1002 window_box_width (w, area)
1003 struct window *w;
1004 int area;
1005 {
1006 int cols = XFASTINT (w->total_cols);
1007 int pixels = 0;
1008
1009 if (!w->pseudo_window_p)
1010 {
1011 cols -= WINDOW_SCROLL_BAR_COLS (w);
1012
1013 if (area == TEXT_AREA)
1014 {
1015 if (INTEGERP (w->left_margin_cols))
1016 cols -= XFASTINT (w->left_margin_cols);
1017 if (INTEGERP (w->right_margin_cols))
1018 cols -= XFASTINT (w->right_margin_cols);
1019 pixels = -WINDOW_TOTAL_FRINGE_WIDTH (w);
1020 }
1021 else if (area == LEFT_MARGIN_AREA)
1022 {
1023 cols = (INTEGERP (w->left_margin_cols)
1024 ? XFASTINT (w->left_margin_cols) : 0);
1025 pixels = 0;
1026 }
1027 else if (area == RIGHT_MARGIN_AREA)
1028 {
1029 cols = (INTEGERP (w->right_margin_cols)
1030 ? XFASTINT (w->right_margin_cols) : 0);
1031 pixels = 0;
1032 }
1033 }
1034
1035 return cols * WINDOW_FRAME_COLUMN_WIDTH (w) + pixels;
1036 }
1037
1038
1039 /* Return the pixel height of the display area of window W, not
1040 including mode lines of W, if any. */
1041
1042 INLINE int
1043 window_box_height (w)
1044 struct window *w;
1045 {
1046 struct frame *f = XFRAME (w->frame);
1047 int height = WINDOW_TOTAL_HEIGHT (w);
1048
1049 xassert (height >= 0);
1050
1051 /* Note: the code below that determines the mode-line/header-line
1052 height is essentially the same as that contained in the macro
1053 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
1054 the appropriate glyph row has its `mode_line_p' flag set,
1055 and if it doesn't, uses estimate_mode_line_height instead. */
1056
1057 if (WINDOW_WANTS_MODELINE_P (w))
1058 {
1059 struct glyph_row *ml_row
1060 = (w->current_matrix && w->current_matrix->rows
1061 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
1062 : 0);
1063 if (ml_row && ml_row->mode_line_p)
1064 height -= ml_row->height;
1065 else
1066 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
1067 }
1068
1069 if (WINDOW_WANTS_HEADER_LINE_P (w))
1070 {
1071 struct glyph_row *hl_row
1072 = (w->current_matrix && w->current_matrix->rows
1073 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
1074 : 0);
1075 if (hl_row && hl_row->mode_line_p)
1076 height -= hl_row->height;
1077 else
1078 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
1079 }
1080
1081 /* With a very small font and a mode-line that's taller than
1082 default, we might end up with a negative height. */
1083 return max (0, height);
1084 }
1085
1086 /* Return the window-relative coordinate of the left edge of display
1087 area AREA of window W. AREA < 0 means return the left edge of the
1088 whole window, to the right of the left fringe of W. */
1089
1090 INLINE int
1091 window_box_left_offset (w, area)
1092 struct window *w;
1093 int area;
1094 {
1095 int x;
1096
1097 if (w->pseudo_window_p)
1098 return 0;
1099
1100 x = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
1101
1102 if (area == TEXT_AREA)
1103 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1104 + window_box_width (w, LEFT_MARGIN_AREA));
1105 else if (area == RIGHT_MARGIN_AREA)
1106 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1107 + window_box_width (w, LEFT_MARGIN_AREA)
1108 + window_box_width (w, TEXT_AREA)
1109 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1110 ? 0
1111 : WINDOW_RIGHT_FRINGE_WIDTH (w)));
1112 else if (area == LEFT_MARGIN_AREA
1113 && WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1114 x += WINDOW_LEFT_FRINGE_WIDTH (w);
1115
1116 return x;
1117 }
1118
1119
1120 /* Return the window-relative coordinate of the right edge of display
1121 area AREA of window W. AREA < 0 means return the left edge of the
1122 whole window, to the left of the right fringe of W. */
1123
1124 INLINE int
1125 window_box_right_offset (w, area)
1126 struct window *w;
1127 int area;
1128 {
1129 return window_box_left_offset (w, area) + window_box_width (w, area);
1130 }
1131
1132 /* Return the frame-relative coordinate of the left edge of display
1133 area AREA of window W. AREA < 0 means return the left edge of the
1134 whole window, to the right of the left fringe of W. */
1135
1136 INLINE int
1137 window_box_left (w, area)
1138 struct window *w;
1139 int area;
1140 {
1141 struct frame *f = XFRAME (w->frame);
1142 int x;
1143
1144 if (w->pseudo_window_p)
1145 return FRAME_INTERNAL_BORDER_WIDTH (f);
1146
1147 x = (WINDOW_LEFT_EDGE_X (w)
1148 + window_box_left_offset (w, area));
1149
1150 return x;
1151 }
1152
1153
1154 /* Return the frame-relative coordinate of the right edge of display
1155 area AREA of window W. AREA < 0 means return the left edge of the
1156 whole window, to the left of the right fringe of W. */
1157
1158 INLINE int
1159 window_box_right (w, area)
1160 struct window *w;
1161 int area;
1162 {
1163 return window_box_left (w, area) + window_box_width (w, area);
1164 }
1165
1166 /* Get the bounding box of the display area AREA of window W, without
1167 mode lines, in frame-relative coordinates. AREA < 0 means the
1168 whole window, not including the left and right fringes of
1169 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1170 coordinates of the upper-left corner of the box. Return in
1171 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1172
1173 INLINE void
1174 window_box (w, area, box_x, box_y, box_width, box_height)
1175 struct window *w;
1176 int area;
1177 int *box_x, *box_y, *box_width, *box_height;
1178 {
1179 if (box_width)
1180 *box_width = window_box_width (w, area);
1181 if (box_height)
1182 *box_height = window_box_height (w);
1183 if (box_x)
1184 *box_x = window_box_left (w, area);
1185 if (box_y)
1186 {
1187 *box_y = WINDOW_TOP_EDGE_Y (w);
1188 if (WINDOW_WANTS_HEADER_LINE_P (w))
1189 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1190 }
1191 }
1192
1193
1194 /* Get the bounding box of the display area AREA of window W, without
1195 mode lines. AREA < 0 means the whole window, not including the
1196 left and right fringe of the window. Return in *TOP_LEFT_X
1197 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1198 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1199 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1200 box. */
1201
1202 INLINE void
1203 window_box_edges (w, area, top_left_x, top_left_y,
1204 bottom_right_x, bottom_right_y)
1205 struct window *w;
1206 int area;
1207 int *top_left_x, *top_left_y, *bottom_right_x, *bottom_right_y;
1208 {
1209 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
1210 bottom_right_y);
1211 *bottom_right_x += *top_left_x;
1212 *bottom_right_y += *top_left_y;
1213 }
1214
1215
1216 \f
1217 /***********************************************************************
1218 Utilities
1219 ***********************************************************************/
1220
1221 /* Return the bottom y-position of the line the iterator IT is in.
1222 This can modify IT's settings. */
1223
1224 int
1225 line_bottom_y (it)
1226 struct it *it;
1227 {
1228 int line_height = it->max_ascent + it->max_descent;
1229 int line_top_y = it->current_y;
1230
1231 if (line_height == 0)
1232 {
1233 if (last_height)
1234 line_height = last_height;
1235 else if (IT_CHARPOS (*it) < ZV)
1236 {
1237 move_it_by_lines (it, 1, 1);
1238 line_height = (it->max_ascent || it->max_descent
1239 ? it->max_ascent + it->max_descent
1240 : last_height);
1241 }
1242 else
1243 {
1244 struct glyph_row *row = it->glyph_row;
1245
1246 /* Use the default character height. */
1247 it->glyph_row = NULL;
1248 it->what = IT_CHARACTER;
1249 it->c = ' ';
1250 it->len = 1;
1251 PRODUCE_GLYPHS (it);
1252 line_height = it->ascent + it->descent;
1253 it->glyph_row = row;
1254 }
1255 }
1256
1257 return line_top_y + line_height;
1258 }
1259
1260
1261 /* Return 1 if position CHARPOS is visible in window W.
1262 If visible, set *X and *Y to pixel coordinates of top left corner.
1263 Set *RTOP and *RBOT to pixel height of an invisible area of glyph at POS.
1264 EXACT_MODE_LINE_HEIGHTS_P non-zero means compute exact mode-line
1265 and header-lines heights. */
1266
1267 int
1268 pos_visible_p (w, charpos, x, y, rtop, rbot, exact_mode_line_heights_p)
1269 struct window *w;
1270 int charpos, *x, *y, *rtop, *rbot, exact_mode_line_heights_p;
1271 {
1272 struct it it;
1273 struct text_pos top;
1274 int visible_p = 0;
1275 struct buffer *old_buffer = NULL;
1276
1277 if (noninteractive)
1278 return visible_p;
1279
1280 if (XBUFFER (w->buffer) != current_buffer)
1281 {
1282 old_buffer = current_buffer;
1283 set_buffer_internal_1 (XBUFFER (w->buffer));
1284 }
1285
1286 SET_TEXT_POS_FROM_MARKER (top, w->start);
1287
1288 /* Compute exact mode line heights, if requested. */
1289 if (exact_mode_line_heights_p)
1290 {
1291 if (WINDOW_WANTS_MODELINE_P (w))
1292 current_mode_line_height
1293 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1294 current_buffer->mode_line_format);
1295
1296 if (WINDOW_WANTS_HEADER_LINE_P (w))
1297 current_header_line_height
1298 = display_mode_line (w, HEADER_LINE_FACE_ID,
1299 current_buffer->header_line_format);
1300 }
1301
1302 start_display (&it, w, top);
1303 move_it_to (&it, charpos, -1, it.last_visible_y, -1,
1304 MOVE_TO_POS | MOVE_TO_Y);
1305
1306 /* Note that we may overshoot because of invisible text. */
1307 if (IT_CHARPOS (it) >= charpos)
1308 {
1309 int top_x = it.current_x;
1310 int top_y = it.current_y;
1311 int bottom_y = (last_height = 0, line_bottom_y (&it));
1312 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
1313
1314 if (top_y < window_top_y)
1315 visible_p = bottom_y > window_top_y;
1316 else if (top_y < it.last_visible_y)
1317 visible_p = 1;
1318 if (visible_p)
1319 {
1320 *x = top_x;
1321 *y = max (top_y + max (0, it.max_ascent - it.ascent), window_top_y);
1322 *rtop = max (0, window_top_y - top_y);
1323 *rbot = max (0, bottom_y - it.last_visible_y);
1324 }
1325 }
1326 else
1327 {
1328 struct it it2;
1329
1330 it2 = it;
1331 if (IT_CHARPOS (it) < ZV && FETCH_BYTE (IT_BYTEPOS (it)) != '\n')
1332 move_it_by_lines (&it, 1, 0);
1333 if (charpos < IT_CHARPOS (it))
1334 {
1335 visible_p = 1;
1336 move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS);
1337 *x = it2.current_x;
1338 *y = it2.current_y + it2.max_ascent - it2.ascent;
1339 *rtop = max (0, -it2.current_y);
1340 *rbot = max (0, ((it2.current_y + it2.max_ascent + it2.max_descent)
1341 - it.last_visible_y));
1342 }
1343 }
1344
1345 if (old_buffer)
1346 set_buffer_internal_1 (old_buffer);
1347
1348 current_header_line_height = current_mode_line_height = -1;
1349
1350 if (visible_p && XFASTINT (w->hscroll) > 0)
1351 *x -= XFASTINT (w->hscroll) * WINDOW_FRAME_COLUMN_WIDTH (w);
1352
1353 return visible_p;
1354 }
1355
1356
1357 /* Return the next character from STR which is MAXLEN bytes long.
1358 Return in *LEN the length of the character. This is like
1359 STRING_CHAR_AND_LENGTH but never returns an invalid character. If
1360 we find one, we return a `?', but with the length of the invalid
1361 character. */
1362
1363 static INLINE int
1364 string_char_and_length (str, maxlen, len)
1365 const unsigned char *str;
1366 int maxlen, *len;
1367 {
1368 int c;
1369
1370 c = STRING_CHAR_AND_LENGTH (str, maxlen, *len);
1371 if (!CHAR_VALID_P (c, 1))
1372 /* We may not change the length here because other places in Emacs
1373 don't use this function, i.e. they silently accept invalid
1374 characters. */
1375 c = '?';
1376
1377 return c;
1378 }
1379
1380
1381
1382 /* Given a position POS containing a valid character and byte position
1383 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1384
1385 static struct text_pos
1386 string_pos_nchars_ahead (pos, string, nchars)
1387 struct text_pos pos;
1388 Lisp_Object string;
1389 int nchars;
1390 {
1391 xassert (STRINGP (string) && nchars >= 0);
1392
1393 if (STRING_MULTIBYTE (string))
1394 {
1395 int rest = SBYTES (string) - BYTEPOS (pos);
1396 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1397 int len;
1398
1399 while (nchars--)
1400 {
1401 string_char_and_length (p, rest, &len);
1402 p += len, rest -= len;
1403 xassert (rest >= 0);
1404 CHARPOS (pos) += 1;
1405 BYTEPOS (pos) += len;
1406 }
1407 }
1408 else
1409 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1410
1411 return pos;
1412 }
1413
1414
1415 /* Value is the text position, i.e. character and byte position,
1416 for character position CHARPOS in STRING. */
1417
1418 static INLINE struct text_pos
1419 string_pos (charpos, string)
1420 int charpos;
1421 Lisp_Object string;
1422 {
1423 struct text_pos pos;
1424 xassert (STRINGP (string));
1425 xassert (charpos >= 0);
1426 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1427 return pos;
1428 }
1429
1430
1431 /* Value is a text position, i.e. character and byte position, for
1432 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1433 means recognize multibyte characters. */
1434
1435 static struct text_pos
1436 c_string_pos (charpos, s, multibyte_p)
1437 int charpos;
1438 unsigned char *s;
1439 int multibyte_p;
1440 {
1441 struct text_pos pos;
1442
1443 xassert (s != NULL);
1444 xassert (charpos >= 0);
1445
1446 if (multibyte_p)
1447 {
1448 int rest = strlen (s), len;
1449
1450 SET_TEXT_POS (pos, 0, 0);
1451 while (charpos--)
1452 {
1453 string_char_and_length (s, rest, &len);
1454 s += len, rest -= len;
1455 xassert (rest >= 0);
1456 CHARPOS (pos) += 1;
1457 BYTEPOS (pos) += len;
1458 }
1459 }
1460 else
1461 SET_TEXT_POS (pos, charpos, charpos);
1462
1463 return pos;
1464 }
1465
1466
1467 /* Value is the number of characters in C string S. MULTIBYTE_P
1468 non-zero means recognize multibyte characters. */
1469
1470 static int
1471 number_of_chars (s, multibyte_p)
1472 unsigned char *s;
1473 int multibyte_p;
1474 {
1475 int nchars;
1476
1477 if (multibyte_p)
1478 {
1479 int rest = strlen (s), len;
1480 unsigned char *p = (unsigned char *) s;
1481
1482 for (nchars = 0; rest > 0; ++nchars)
1483 {
1484 string_char_and_length (p, rest, &len);
1485 rest -= len, p += len;
1486 }
1487 }
1488 else
1489 nchars = strlen (s);
1490
1491 return nchars;
1492 }
1493
1494
1495 /* Compute byte position NEWPOS->bytepos corresponding to
1496 NEWPOS->charpos. POS is a known position in string STRING.
1497 NEWPOS->charpos must be >= POS.charpos. */
1498
1499 static void
1500 compute_string_pos (newpos, pos, string)
1501 struct text_pos *newpos, pos;
1502 Lisp_Object string;
1503 {
1504 xassert (STRINGP (string));
1505 xassert (CHARPOS (*newpos) >= CHARPOS (pos));
1506
1507 if (STRING_MULTIBYTE (string))
1508 *newpos = string_pos_nchars_ahead (pos, string,
1509 CHARPOS (*newpos) - CHARPOS (pos));
1510 else
1511 BYTEPOS (*newpos) = CHARPOS (*newpos);
1512 }
1513
1514 /* EXPORT:
1515 Return an estimation of the pixel height of mode or top lines on
1516 frame F. FACE_ID specifies what line's height to estimate. */
1517
1518 int
1519 estimate_mode_line_height (f, face_id)
1520 struct frame *f;
1521 enum face_id face_id;
1522 {
1523 #ifdef HAVE_WINDOW_SYSTEM
1524 if (FRAME_WINDOW_P (f))
1525 {
1526 int height = FONT_HEIGHT (FRAME_FONT (f));
1527
1528 /* This function is called so early when Emacs starts that the face
1529 cache and mode line face are not yet initialized. */
1530 if (FRAME_FACE_CACHE (f))
1531 {
1532 struct face *face = FACE_FROM_ID (f, face_id);
1533 if (face)
1534 {
1535 if (face->font)
1536 height = FONT_HEIGHT (face->font);
1537 if (face->box_line_width > 0)
1538 height += 2 * face->box_line_width;
1539 }
1540 }
1541
1542 return height;
1543 }
1544 #endif
1545
1546 return 1;
1547 }
1548
1549 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1550 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1551 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
1552 not force the value into range. */
1553
1554 void
1555 pixel_to_glyph_coords (f, pix_x, pix_y, x, y, bounds, noclip)
1556 FRAME_PTR f;
1557 register int pix_x, pix_y;
1558 int *x, *y;
1559 NativeRectangle *bounds;
1560 int noclip;
1561 {
1562
1563 #ifdef HAVE_WINDOW_SYSTEM
1564 if (FRAME_WINDOW_P (f))
1565 {
1566 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1567 even for negative values. */
1568 if (pix_x < 0)
1569 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1570 if (pix_y < 0)
1571 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1572
1573 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1574 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1575
1576 if (bounds)
1577 STORE_NATIVE_RECT (*bounds,
1578 FRAME_COL_TO_PIXEL_X (f, pix_x),
1579 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1580 FRAME_COLUMN_WIDTH (f) - 1,
1581 FRAME_LINE_HEIGHT (f) - 1);
1582
1583 if (!noclip)
1584 {
1585 if (pix_x < 0)
1586 pix_x = 0;
1587 else if (pix_x > FRAME_TOTAL_COLS (f))
1588 pix_x = FRAME_TOTAL_COLS (f);
1589
1590 if (pix_y < 0)
1591 pix_y = 0;
1592 else if (pix_y > FRAME_LINES (f))
1593 pix_y = FRAME_LINES (f);
1594 }
1595 }
1596 #endif
1597
1598 *x = pix_x;
1599 *y = pix_y;
1600 }
1601
1602
1603 /* Given HPOS/VPOS in the current matrix of W, return corresponding
1604 frame-relative pixel positions in *FRAME_X and *FRAME_Y. If we
1605 can't tell the positions because W's display is not up to date,
1606 return 0. */
1607
1608 int
1609 glyph_to_pixel_coords (w, hpos, vpos, frame_x, frame_y)
1610 struct window *w;
1611 int hpos, vpos;
1612 int *frame_x, *frame_y;
1613 {
1614 #ifdef HAVE_WINDOW_SYSTEM
1615 if (FRAME_WINDOW_P (XFRAME (WINDOW_FRAME (w))))
1616 {
1617 int success_p;
1618
1619 xassert (hpos >= 0 && hpos < w->current_matrix->matrix_w);
1620 xassert (vpos >= 0 && vpos < w->current_matrix->matrix_h);
1621
1622 if (display_completed)
1623 {
1624 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
1625 struct glyph *glyph = row->glyphs[TEXT_AREA];
1626 struct glyph *end = glyph + min (hpos, row->used[TEXT_AREA]);
1627
1628 hpos = row->x;
1629 vpos = row->y;
1630 while (glyph < end)
1631 {
1632 hpos += glyph->pixel_width;
1633 ++glyph;
1634 }
1635
1636 /* If first glyph is partially visible, its first visible position is still 0. */
1637 if (hpos < 0)
1638 hpos = 0;
1639
1640 success_p = 1;
1641 }
1642 else
1643 {
1644 hpos = vpos = 0;
1645 success_p = 0;
1646 }
1647
1648 *frame_x = WINDOW_TO_FRAME_PIXEL_X (w, hpos);
1649 *frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, vpos);
1650 return success_p;
1651 }
1652 #endif
1653
1654 *frame_x = hpos;
1655 *frame_y = vpos;
1656 return 1;
1657 }
1658
1659
1660 #ifdef HAVE_WINDOW_SYSTEM
1661
1662 /* Find the glyph under window-relative coordinates X/Y in window W.
1663 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1664 strings. Return in *HPOS and *VPOS the row and column number of
1665 the glyph found. Return in *AREA the glyph area containing X.
1666 Value is a pointer to the glyph found or null if X/Y is not on
1667 text, or we can't tell because W's current matrix is not up to
1668 date. */
1669
1670 static struct glyph *
1671 x_y_to_hpos_vpos (w, x, y, hpos, vpos, dx, dy, area)
1672 struct window *w;
1673 int x, y;
1674 int *hpos, *vpos, *dx, *dy, *area;
1675 {
1676 struct glyph *glyph, *end;
1677 struct glyph_row *row = NULL;
1678 int x0, i;
1679
1680 /* Find row containing Y. Give up if some row is not enabled. */
1681 for (i = 0; i < w->current_matrix->nrows; ++i)
1682 {
1683 row = MATRIX_ROW (w->current_matrix, i);
1684 if (!row->enabled_p)
1685 return NULL;
1686 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1687 break;
1688 }
1689
1690 *vpos = i;
1691 *hpos = 0;
1692
1693 /* Give up if Y is not in the window. */
1694 if (i == w->current_matrix->nrows)
1695 return NULL;
1696
1697 /* Get the glyph area containing X. */
1698 if (w->pseudo_window_p)
1699 {
1700 *area = TEXT_AREA;
1701 x0 = 0;
1702 }
1703 else
1704 {
1705 if (x < window_box_left_offset (w, TEXT_AREA))
1706 {
1707 *area = LEFT_MARGIN_AREA;
1708 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1709 }
1710 else if (x < window_box_right_offset (w, TEXT_AREA))
1711 {
1712 *area = TEXT_AREA;
1713 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
1714 }
1715 else
1716 {
1717 *area = RIGHT_MARGIN_AREA;
1718 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1719 }
1720 }
1721
1722 /* Find glyph containing X. */
1723 glyph = row->glyphs[*area];
1724 end = glyph + row->used[*area];
1725 x -= x0;
1726 while (glyph < end && x >= glyph->pixel_width)
1727 {
1728 x -= glyph->pixel_width;
1729 ++glyph;
1730 }
1731
1732 if (glyph == end)
1733 return NULL;
1734
1735 if (dx)
1736 {
1737 *dx = x;
1738 *dy = y - (row->y + row->ascent - glyph->ascent);
1739 }
1740
1741 *hpos = glyph - row->glyphs[*area];
1742 return glyph;
1743 }
1744
1745
1746 /* EXPORT:
1747 Convert frame-relative x/y to coordinates relative to window W.
1748 Takes pseudo-windows into account. */
1749
1750 void
1751 frame_to_window_pixel_xy (w, x, y)
1752 struct window *w;
1753 int *x, *y;
1754 {
1755 if (w->pseudo_window_p)
1756 {
1757 /* A pseudo-window is always full-width, and starts at the
1758 left edge of the frame, plus a frame border. */
1759 struct frame *f = XFRAME (w->frame);
1760 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
1761 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1762 }
1763 else
1764 {
1765 *x -= WINDOW_LEFT_EDGE_X (w);
1766 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1767 }
1768 }
1769
1770 /* EXPORT:
1771 Return in RECTS[] at most N clipping rectangles for glyph string S.
1772 Return the number of stored rectangles. */
1773
1774 int
1775 get_glyph_string_clip_rects (s, rects, n)
1776 struct glyph_string *s;
1777 NativeRectangle *rects;
1778 int n;
1779 {
1780 XRectangle r;
1781
1782 if (n <= 0)
1783 return 0;
1784
1785 if (s->row->full_width_p)
1786 {
1787 /* Draw full-width. X coordinates are relative to S->w->left_col. */
1788 r.x = WINDOW_LEFT_EDGE_X (s->w);
1789 r.width = WINDOW_TOTAL_WIDTH (s->w);
1790
1791 /* Unless displaying a mode or menu bar line, which are always
1792 fully visible, clip to the visible part of the row. */
1793 if (s->w->pseudo_window_p)
1794 r.height = s->row->visible_height;
1795 else
1796 r.height = s->height;
1797 }
1798 else
1799 {
1800 /* This is a text line that may be partially visible. */
1801 r.x = window_box_left (s->w, s->area);
1802 r.width = window_box_width (s->w, s->area);
1803 r.height = s->row->visible_height;
1804 }
1805
1806 if (s->clip_head)
1807 if (r.x < s->clip_head->x)
1808 {
1809 if (r.width >= s->clip_head->x - r.x)
1810 r.width -= s->clip_head->x - r.x;
1811 else
1812 r.width = 0;
1813 r.x = s->clip_head->x;
1814 }
1815 if (s->clip_tail)
1816 if (r.x + r.width > s->clip_tail->x + s->clip_tail->background_width)
1817 {
1818 if (s->clip_tail->x + s->clip_tail->background_width >= r.x)
1819 r.width = s->clip_tail->x + s->clip_tail->background_width - r.x;
1820 else
1821 r.width = 0;
1822 }
1823
1824 /* If S draws overlapping rows, it's sufficient to use the top and
1825 bottom of the window for clipping because this glyph string
1826 intentionally draws over other lines. */
1827 if (s->for_overlaps)
1828 {
1829 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1830 r.height = window_text_bottom_y (s->w) - r.y;
1831
1832 /* Alas, the above simple strategy does not work for the
1833 environments with anti-aliased text: if the same text is
1834 drawn onto the same place multiple times, it gets thicker.
1835 If the overlap we are processing is for the erased cursor, we
1836 take the intersection with the rectagle of the cursor. */
1837 if (s->for_overlaps & OVERLAPS_ERASED_CURSOR)
1838 {
1839 XRectangle rc, r_save = r;
1840
1841 rc.x = WINDOW_TEXT_TO_FRAME_PIXEL_X (s->w, s->w->phys_cursor.x);
1842 rc.y = s->w->phys_cursor.y;
1843 rc.width = s->w->phys_cursor_width;
1844 rc.height = s->w->phys_cursor_height;
1845
1846 x_intersect_rectangles (&r_save, &rc, &r);
1847 }
1848 }
1849 else
1850 {
1851 /* Don't use S->y for clipping because it doesn't take partially
1852 visible lines into account. For example, it can be negative for
1853 partially visible lines at the top of a window. */
1854 if (!s->row->full_width_p
1855 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
1856 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1857 else
1858 r.y = max (0, s->row->y);
1859
1860 /* If drawing a tool-bar window, draw it over the internal border
1861 at the top of the window. */
1862 if (WINDOWP (s->f->tool_bar_window)
1863 && s->w == XWINDOW (s->f->tool_bar_window))
1864 r.y -= FRAME_INTERNAL_BORDER_WIDTH (s->f);
1865 }
1866
1867 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
1868
1869 /* If drawing the cursor, don't let glyph draw outside its
1870 advertised boundaries. Cleartype does this under some circumstances. */
1871 if (s->hl == DRAW_CURSOR)
1872 {
1873 struct glyph *glyph = s->first_glyph;
1874 int height, max_y;
1875
1876 if (s->x > r.x)
1877 {
1878 r.width -= s->x - r.x;
1879 r.x = s->x;
1880 }
1881 r.width = min (r.width, glyph->pixel_width);
1882
1883 /* If r.y is below window bottom, ensure that we still see a cursor. */
1884 height = min (glyph->ascent + glyph->descent,
1885 min (FRAME_LINE_HEIGHT (s->f), s->row->visible_height));
1886 max_y = window_text_bottom_y (s->w) - height;
1887 max_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, max_y);
1888 if (s->ybase - glyph->ascent > max_y)
1889 {
1890 r.y = max_y;
1891 r.height = height;
1892 }
1893 else
1894 {
1895 /* Don't draw cursor glyph taller than our actual glyph. */
1896 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
1897 if (height < r.height)
1898 {
1899 max_y = r.y + r.height;
1900 r.y = min (max_y, max (r.y, s->ybase + glyph->descent - height));
1901 r.height = min (max_y - r.y, height);
1902 }
1903 }
1904 }
1905
1906 if ((s->for_overlaps & OVERLAPS_BOTH) == 0
1907 || (s->for_overlaps & OVERLAPS_BOTH) == OVERLAPS_BOTH && n == 1)
1908 {
1909 #ifdef CONVERT_FROM_XRECT
1910 CONVERT_FROM_XRECT (r, *rects);
1911 #else
1912 *rects = r;
1913 #endif
1914 return 1;
1915 }
1916 else
1917 {
1918 /* If we are processing overlapping and allowed to return
1919 multiple clipping rectangles, we exclude the row of the glyph
1920 string from the clipping rectangle. This is to avoid drawing
1921 the same text on the environment with anti-aliasing. */
1922 #ifdef CONVERT_FROM_XRECT
1923 XRectangle rs[2];
1924 #else
1925 XRectangle *rs = rects;
1926 #endif
1927 int i = 0, row_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, s->row->y);
1928
1929 if (s->for_overlaps & OVERLAPS_PRED)
1930 {
1931 rs[i] = r;
1932 if (r.y + r.height > row_y)
1933 if (r.y < row_y)
1934 rs[i].height = row_y - r.y;
1935 else
1936 rs[i].height = 0;
1937 i++;
1938 }
1939 if (s->for_overlaps & OVERLAPS_SUCC)
1940 {
1941 rs[i] = r;
1942 if (r.y < row_y + s->row->visible_height)
1943 if (r.y + r.height > row_y + s->row->visible_height)
1944 {
1945 rs[i].y = row_y + s->row->visible_height;
1946 rs[i].height = r.y + r.height - rs[i].y;
1947 }
1948 else
1949 rs[i].height = 0;
1950 i++;
1951 }
1952
1953 n = i;
1954 #ifdef CONVERT_FROM_XRECT
1955 for (i = 0; i < n; i++)
1956 CONVERT_FROM_XRECT (rs[i], rects[i]);
1957 #endif
1958 return n;
1959 }
1960 }
1961
1962 /* EXPORT:
1963 Return in *NR the clipping rectangle for glyph string S. */
1964
1965 void
1966 get_glyph_string_clip_rect (s, nr)
1967 struct glyph_string *s;
1968 NativeRectangle *nr;
1969 {
1970 get_glyph_string_clip_rects (s, nr, 1);
1971 }
1972
1973
1974 /* EXPORT:
1975 Return the position and height of the phys cursor in window W.
1976 Set w->phys_cursor_width to width of phys cursor.
1977 */
1978
1979 int
1980 get_phys_cursor_geometry (w, row, glyph, heightp)
1981 struct window *w;
1982 struct glyph_row *row;
1983 struct glyph *glyph;
1984 int *heightp;
1985 {
1986 struct frame *f = XFRAME (WINDOW_FRAME (w));
1987 int y, wd, h, h0, y0;
1988
1989 /* Compute the width of the rectangle to draw. If on a stretch
1990 glyph, and `x-stretch-block-cursor' is nil, don't draw a
1991 rectangle as wide as the glyph, but use a canonical character
1992 width instead. */
1993 wd = glyph->pixel_width - 1;
1994 #ifdef HAVE_NTGUI
1995 wd++; /* Why? */
1996 #endif
1997 if (glyph->type == STRETCH_GLYPH
1998 && !x_stretch_cursor_p)
1999 wd = min (FRAME_COLUMN_WIDTH (f), wd);
2000 w->phys_cursor_width = wd;
2001
2002 y = w->phys_cursor.y + row->ascent - glyph->ascent;
2003
2004 /* If y is below window bottom, ensure that we still see a cursor. */
2005 h0 = min (FRAME_LINE_HEIGHT (f), row->visible_height);
2006
2007 h = max (h0, glyph->ascent + glyph->descent);
2008 h0 = min (h0, glyph->ascent + glyph->descent);
2009
2010 y0 = WINDOW_HEADER_LINE_HEIGHT (w);
2011 if (y < y0)
2012 {
2013 h = max (h - (y0 - y) + 1, h0);
2014 y = y0 - 1;
2015 }
2016 else
2017 {
2018 y0 = window_text_bottom_y (w) - h0;
2019 if (y > y0)
2020 {
2021 h += y - y0;
2022 y = y0;
2023 }
2024 }
2025
2026 *heightp = h - 1;
2027 return WINDOW_TO_FRAME_PIXEL_Y (w, y);
2028 }
2029
2030 /*
2031 * Remember which glyph the mouse is over.
2032 */
2033
2034 void
2035 remember_mouse_glyph (f, gx, gy, rect)
2036 struct frame *f;
2037 int gx, gy;
2038 NativeRectangle *rect;
2039 {
2040 Lisp_Object window;
2041 struct window *w;
2042 struct glyph_row *r, *gr, *end_row;
2043 enum window_part part;
2044 enum glyph_row_area area;
2045 int x, y, width, height;
2046
2047 /* Try to determine frame pixel position and size of the glyph under
2048 frame pixel coordinates X/Y on frame F. */
2049
2050 window = window_from_coordinates (f, gx, gy, &part, &x, &y, 0);
2051 if (NILP (window))
2052 {
2053 width = FRAME_SMALLEST_CHAR_WIDTH (f);
2054 height = FRAME_SMALLEST_FONT_HEIGHT (f);
2055 goto virtual_glyph;
2056 }
2057
2058 w = XWINDOW (window);
2059 width = WINDOW_FRAME_COLUMN_WIDTH (w);
2060 height = WINDOW_FRAME_LINE_HEIGHT (w);
2061
2062 r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
2063 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
2064
2065 if (w->pseudo_window_p)
2066 {
2067 area = TEXT_AREA;
2068 part = ON_MODE_LINE; /* Don't adjust margin. */
2069 goto text_glyph;
2070 }
2071
2072 switch (part)
2073 {
2074 case ON_LEFT_MARGIN:
2075 area = LEFT_MARGIN_AREA;
2076 goto text_glyph;
2077
2078 case ON_RIGHT_MARGIN:
2079 area = RIGHT_MARGIN_AREA;
2080 goto text_glyph;
2081
2082 case ON_HEADER_LINE:
2083 case ON_MODE_LINE:
2084 gr = (part == ON_HEADER_LINE
2085 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
2086 : MATRIX_MODE_LINE_ROW (w->current_matrix));
2087 gy = gr->y;
2088 area = TEXT_AREA;
2089 goto text_glyph_row_found;
2090
2091 case ON_TEXT:
2092 area = TEXT_AREA;
2093
2094 text_glyph:
2095 gr = 0; gy = 0;
2096 for (; r <= end_row && r->enabled_p; ++r)
2097 if (r->y + r->height > y)
2098 {
2099 gr = r; gy = r->y;
2100 break;
2101 }
2102
2103 text_glyph_row_found:
2104 if (gr && gy <= y)
2105 {
2106 struct glyph *g = gr->glyphs[area];
2107 struct glyph *end = g + gr->used[area];
2108
2109 height = gr->height;
2110 for (gx = gr->x; g < end; gx += g->pixel_width, ++g)
2111 if (gx + g->pixel_width > x)
2112 break;
2113
2114 if (g < end)
2115 {
2116 if (g->type == IMAGE_GLYPH)
2117 {
2118 /* Don't remember when mouse is over image, as
2119 image may have hot-spots. */
2120 STORE_NATIVE_RECT (*rect, 0, 0, 0, 0);
2121 return;
2122 }
2123 width = g->pixel_width;
2124 }
2125 else
2126 {
2127 /* Use nominal char spacing at end of line. */
2128 x -= gx;
2129 gx += (x / width) * width;
2130 }
2131
2132 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2133 gx += window_box_left_offset (w, area);
2134 }
2135 else
2136 {
2137 /* Use nominal line height at end of window. */
2138 gx = (x / width) * width;
2139 y -= gy;
2140 gy += (y / height) * height;
2141 }
2142 break;
2143
2144 case ON_LEFT_FRINGE:
2145 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2146 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w)
2147 : window_box_right_offset (w, LEFT_MARGIN_AREA));
2148 width = WINDOW_LEFT_FRINGE_WIDTH (w);
2149 goto row_glyph;
2150
2151 case ON_RIGHT_FRINGE:
2152 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2153 ? window_box_right_offset (w, RIGHT_MARGIN_AREA)
2154 : window_box_right_offset (w, TEXT_AREA));
2155 width = WINDOW_RIGHT_FRINGE_WIDTH (w);
2156 goto row_glyph;
2157
2158 case ON_SCROLL_BAR:
2159 gx = (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
2160 ? 0
2161 : (window_box_right_offset (w, RIGHT_MARGIN_AREA)
2162 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2163 ? WINDOW_RIGHT_FRINGE_WIDTH (w)
2164 : 0)));
2165 width = WINDOW_SCROLL_BAR_AREA_WIDTH (w);
2166
2167 row_glyph:
2168 gr = 0, gy = 0;
2169 for (; r <= end_row && r->enabled_p; ++r)
2170 if (r->y + r->height > y)
2171 {
2172 gr = r; gy = r->y;
2173 break;
2174 }
2175
2176 if (gr && gy <= y)
2177 height = gr->height;
2178 else
2179 {
2180 /* Use nominal line height at end of window. */
2181 y -= gy;
2182 gy += (y / height) * height;
2183 }
2184 break;
2185
2186 default:
2187 ;
2188 virtual_glyph:
2189 /* If there is no glyph under the mouse, then we divide the screen
2190 into a grid of the smallest glyph in the frame, and use that
2191 as our "glyph". */
2192
2193 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to
2194 round down even for negative values. */
2195 if (gx < 0)
2196 gx -= width - 1;
2197 if (gy < 0)
2198 gy -= height - 1;
2199
2200 gx = (gx / width) * width;
2201 gy = (gy / height) * height;
2202
2203 goto store_rect;
2204 }
2205
2206 gx += WINDOW_LEFT_EDGE_X (w);
2207 gy += WINDOW_TOP_EDGE_Y (w);
2208
2209 store_rect:
2210 STORE_NATIVE_RECT (*rect, gx, gy, width, height);
2211
2212 /* Visible feedback for debugging. */
2213 #if 0
2214 #if HAVE_X_WINDOWS
2215 XDrawRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2216 f->output_data.x->normal_gc,
2217 gx, gy, width, height);
2218 #endif
2219 #endif
2220 }
2221
2222
2223 #endif /* HAVE_WINDOW_SYSTEM */
2224
2225 \f
2226 /***********************************************************************
2227 Lisp form evaluation
2228 ***********************************************************************/
2229
2230 /* Error handler for safe_eval and safe_call. */
2231
2232 static Lisp_Object
2233 safe_eval_handler (arg)
2234 Lisp_Object arg;
2235 {
2236 add_to_log ("Error during redisplay: %s", arg, Qnil);
2237 return Qnil;
2238 }
2239
2240
2241 /* Evaluate SEXPR and return the result, or nil if something went
2242 wrong. Prevent redisplay during the evaluation. */
2243
2244 Lisp_Object
2245 safe_eval (sexpr)
2246 Lisp_Object sexpr;
2247 {
2248 Lisp_Object val;
2249
2250 if (inhibit_eval_during_redisplay)
2251 val = Qnil;
2252 else
2253 {
2254 int count = SPECPDL_INDEX ();
2255 struct gcpro gcpro1;
2256
2257 GCPRO1 (sexpr);
2258 specbind (Qinhibit_redisplay, Qt);
2259 /* Use Qt to ensure debugger does not run,
2260 so there is no possibility of wanting to redisplay. */
2261 val = internal_condition_case_1 (Feval, sexpr, Qt,
2262 safe_eval_handler);
2263 UNGCPRO;
2264 val = unbind_to (count, val);
2265 }
2266
2267 return val;
2268 }
2269
2270
2271 /* Call function ARGS[0] with arguments ARGS[1] to ARGS[NARGS - 1].
2272 Return the result, or nil if something went wrong. Prevent
2273 redisplay during the evaluation. */
2274
2275 Lisp_Object
2276 safe_call (nargs, args)
2277 int nargs;
2278 Lisp_Object *args;
2279 {
2280 Lisp_Object val;
2281
2282 if (inhibit_eval_during_redisplay)
2283 val = Qnil;
2284 else
2285 {
2286 int count = SPECPDL_INDEX ();
2287 struct gcpro gcpro1;
2288
2289 GCPRO1 (args[0]);
2290 gcpro1.nvars = nargs;
2291 specbind (Qinhibit_redisplay, Qt);
2292 /* Use Qt to ensure debugger does not run,
2293 so there is no possibility of wanting to redisplay. */
2294 val = internal_condition_case_2 (Ffuncall, nargs, args, Qt,
2295 safe_eval_handler);
2296 UNGCPRO;
2297 val = unbind_to (count, val);
2298 }
2299
2300 return val;
2301 }
2302
2303
2304 /* Call function FN with one argument ARG.
2305 Return the result, or nil if something went wrong. */
2306
2307 Lisp_Object
2308 safe_call1 (fn, arg)
2309 Lisp_Object fn, arg;
2310 {
2311 Lisp_Object args[2];
2312 args[0] = fn;
2313 args[1] = arg;
2314 return safe_call (2, args);
2315 }
2316
2317
2318 \f
2319 /***********************************************************************
2320 Debugging
2321 ***********************************************************************/
2322
2323 #if 0
2324
2325 /* Define CHECK_IT to perform sanity checks on iterators.
2326 This is for debugging. It is too slow to do unconditionally. */
2327
2328 static void
2329 check_it (it)
2330 struct it *it;
2331 {
2332 if (it->method == GET_FROM_STRING)
2333 {
2334 xassert (STRINGP (it->string));
2335 xassert (IT_STRING_CHARPOS (*it) >= 0);
2336 }
2337 else
2338 {
2339 xassert (IT_STRING_CHARPOS (*it) < 0);
2340 if (it->method == GET_FROM_BUFFER)
2341 {
2342 /* Check that character and byte positions agree. */
2343 xassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
2344 }
2345 }
2346
2347 if (it->dpvec)
2348 xassert (it->current.dpvec_index >= 0);
2349 else
2350 xassert (it->current.dpvec_index < 0);
2351 }
2352
2353 #define CHECK_IT(IT) check_it ((IT))
2354
2355 #else /* not 0 */
2356
2357 #define CHECK_IT(IT) (void) 0
2358
2359 #endif /* not 0 */
2360
2361
2362 #if GLYPH_DEBUG
2363
2364 /* Check that the window end of window W is what we expect it
2365 to be---the last row in the current matrix displaying text. */
2366
2367 static void
2368 check_window_end (w)
2369 struct window *w;
2370 {
2371 if (!MINI_WINDOW_P (w)
2372 && !NILP (w->window_end_valid))
2373 {
2374 struct glyph_row *row;
2375 xassert ((row = MATRIX_ROW (w->current_matrix,
2376 XFASTINT (w->window_end_vpos)),
2377 !row->enabled_p
2378 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
2379 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
2380 }
2381 }
2382
2383 #define CHECK_WINDOW_END(W) check_window_end ((W))
2384
2385 #else /* not GLYPH_DEBUG */
2386
2387 #define CHECK_WINDOW_END(W) (void) 0
2388
2389 #endif /* not GLYPH_DEBUG */
2390
2391
2392 \f
2393 /***********************************************************************
2394 Iterator initialization
2395 ***********************************************************************/
2396
2397 /* Initialize IT for displaying current_buffer in window W, starting
2398 at character position CHARPOS. CHARPOS < 0 means that no buffer
2399 position is specified which is useful when the iterator is assigned
2400 a position later. BYTEPOS is the byte position corresponding to
2401 CHARPOS. BYTEPOS < 0 means compute it from CHARPOS.
2402
2403 If ROW is not null, calls to produce_glyphs with IT as parameter
2404 will produce glyphs in that row.
2405
2406 BASE_FACE_ID is the id of a base face to use. It must be one of
2407 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2408 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2409 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2410
2411 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2412 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2413 will be initialized to use the corresponding mode line glyph row of
2414 the desired matrix of W. */
2415
2416 void
2417 init_iterator (it, w, charpos, bytepos, row, base_face_id)
2418 struct it *it;
2419 struct window *w;
2420 int charpos, bytepos;
2421 struct glyph_row *row;
2422 enum face_id base_face_id;
2423 {
2424 int highlight_region_p;
2425
2426 /* Some precondition checks. */
2427 xassert (w != NULL && it != NULL);
2428 xassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2429 && charpos <= ZV));
2430
2431 /* If face attributes have been changed since the last redisplay,
2432 free realized faces now because they depend on face definitions
2433 that might have changed. Don't free faces while there might be
2434 desired matrices pending which reference these faces. */
2435 if (face_change_count && !inhibit_free_realized_faces)
2436 {
2437 face_change_count = 0;
2438 free_all_realized_faces (Qnil);
2439 }
2440
2441 /* Use one of the mode line rows of W's desired matrix if
2442 appropriate. */
2443 if (row == NULL)
2444 {
2445 if (base_face_id == MODE_LINE_FACE_ID
2446 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2447 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2448 else if (base_face_id == HEADER_LINE_FACE_ID)
2449 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2450 }
2451
2452 /* Clear IT. */
2453 bzero (it, sizeof *it);
2454 it->current.overlay_string_index = -1;
2455 it->current.dpvec_index = -1;
2456 it->base_face_id = base_face_id;
2457 it->string = Qnil;
2458 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2459
2460 /* The window in which we iterate over current_buffer: */
2461 XSETWINDOW (it->window, w);
2462 it->w = w;
2463 it->f = XFRAME (w->frame);
2464
2465 /* Extra space between lines (on window systems only). */
2466 if (base_face_id == DEFAULT_FACE_ID
2467 && FRAME_WINDOW_P (it->f))
2468 {
2469 if (NATNUMP (current_buffer->extra_line_spacing))
2470 it->extra_line_spacing = XFASTINT (current_buffer->extra_line_spacing);
2471 else if (FLOATP (current_buffer->extra_line_spacing))
2472 it->extra_line_spacing = (XFLOAT_DATA (current_buffer->extra_line_spacing)
2473 * FRAME_LINE_HEIGHT (it->f));
2474 else if (it->f->extra_line_spacing > 0)
2475 it->extra_line_spacing = it->f->extra_line_spacing;
2476 it->max_extra_line_spacing = 0;
2477 }
2478
2479 /* If realized faces have been removed, e.g. because of face
2480 attribute changes of named faces, recompute them. When running
2481 in batch mode, the face cache of Vterminal_frame is null. If
2482 we happen to get called, make a dummy face cache. */
2483 if (noninteractive && FRAME_FACE_CACHE (it->f) == NULL)
2484 init_frame_faces (it->f);
2485 if (FRAME_FACE_CACHE (it->f)->used == 0)
2486 recompute_basic_faces (it->f);
2487
2488 /* Current value of the `slice', `space-width', and 'height' properties. */
2489 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
2490 it->space_width = Qnil;
2491 it->font_height = Qnil;
2492 it->override_ascent = -1;
2493
2494 /* Are control characters displayed as `^C'? */
2495 it->ctl_arrow_p = !NILP (current_buffer->ctl_arrow);
2496
2497 /* -1 means everything between a CR and the following line end
2498 is invisible. >0 means lines indented more than this value are
2499 invisible. */
2500 it->selective = (INTEGERP (current_buffer->selective_display)
2501 ? XFASTINT (current_buffer->selective_display)
2502 : (!NILP (current_buffer->selective_display)
2503 ? -1 : 0));
2504 it->selective_display_ellipsis_p
2505 = !NILP (current_buffer->selective_display_ellipses);
2506
2507 /* Display table to use. */
2508 it->dp = window_display_table (w);
2509
2510 /* Are multibyte characters enabled in current_buffer? */
2511 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
2512
2513 /* Non-zero if we should highlight the region. */
2514 highlight_region_p
2515 = (!NILP (Vtransient_mark_mode)
2516 && !NILP (current_buffer->mark_active)
2517 && XMARKER (current_buffer->mark)->buffer != 0);
2518
2519 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
2520 start and end of a visible region in window IT->w. Set both to
2521 -1 to indicate no region. */
2522 if (highlight_region_p
2523 /* Maybe highlight only in selected window. */
2524 && (/* Either show region everywhere. */
2525 highlight_nonselected_windows
2526 /* Or show region in the selected window. */
2527 || w == XWINDOW (selected_window)
2528 /* Or show the region if we are in the mini-buffer and W is
2529 the window the mini-buffer refers to. */
2530 || (MINI_WINDOW_P (XWINDOW (selected_window))
2531 && WINDOWP (minibuf_selected_window)
2532 && w == XWINDOW (minibuf_selected_window))))
2533 {
2534 int charpos = marker_position (current_buffer->mark);
2535 it->region_beg_charpos = min (PT, charpos);
2536 it->region_end_charpos = max (PT, charpos);
2537 }
2538 else
2539 it->region_beg_charpos = it->region_end_charpos = -1;
2540
2541 /* Get the position at which the redisplay_end_trigger hook should
2542 be run, if it is to be run at all. */
2543 if (MARKERP (w->redisplay_end_trigger)
2544 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2545 it->redisplay_end_trigger_charpos
2546 = marker_position (w->redisplay_end_trigger);
2547 else if (INTEGERP (w->redisplay_end_trigger))
2548 it->redisplay_end_trigger_charpos = XINT (w->redisplay_end_trigger);
2549
2550 /* Correct bogus values of tab_width. */
2551 it->tab_width = XINT (current_buffer->tab_width);
2552 if (it->tab_width <= 0 || it->tab_width > 1000)
2553 it->tab_width = 8;
2554
2555 /* Are lines in the display truncated? */
2556 it->truncate_lines_p
2557 = (base_face_id != DEFAULT_FACE_ID
2558 || XINT (it->w->hscroll)
2559 || (truncate_partial_width_windows
2560 && !WINDOW_FULL_WIDTH_P (it->w))
2561 || !NILP (current_buffer->truncate_lines));
2562
2563 /* Get dimensions of truncation and continuation glyphs. These are
2564 displayed as fringe bitmaps under X, so we don't need them for such
2565 frames. */
2566 if (!FRAME_WINDOW_P (it->f))
2567 {
2568 if (it->truncate_lines_p)
2569 {
2570 /* We will need the truncation glyph. */
2571 xassert (it->glyph_row == NULL);
2572 produce_special_glyphs (it, IT_TRUNCATION);
2573 it->truncation_pixel_width = it->pixel_width;
2574 }
2575 else
2576 {
2577 /* We will need the continuation glyph. */
2578 xassert (it->glyph_row == NULL);
2579 produce_special_glyphs (it, IT_CONTINUATION);
2580 it->continuation_pixel_width = it->pixel_width;
2581 }
2582
2583 /* Reset these values to zero because the produce_special_glyphs
2584 above has changed them. */
2585 it->pixel_width = it->ascent = it->descent = 0;
2586 it->phys_ascent = it->phys_descent = 0;
2587 }
2588
2589 /* Set this after getting the dimensions of truncation and
2590 continuation glyphs, so that we don't produce glyphs when calling
2591 produce_special_glyphs, above. */
2592 it->glyph_row = row;
2593 it->area = TEXT_AREA;
2594
2595 /* Get the dimensions of the display area. The display area
2596 consists of the visible window area plus a horizontally scrolled
2597 part to the left of the window. All x-values are relative to the
2598 start of this total display area. */
2599 if (base_face_id != DEFAULT_FACE_ID)
2600 {
2601 /* Mode lines, menu bar in terminal frames. */
2602 it->first_visible_x = 0;
2603 it->last_visible_x = WINDOW_TOTAL_WIDTH (w);
2604 }
2605 else
2606 {
2607 it->first_visible_x
2608 = XFASTINT (it->w->hscroll) * FRAME_COLUMN_WIDTH (it->f);
2609 it->last_visible_x = (it->first_visible_x
2610 + window_box_width (w, TEXT_AREA));
2611
2612 /* If we truncate lines, leave room for the truncator glyph(s) at
2613 the right margin. Otherwise, leave room for the continuation
2614 glyph(s). Truncation and continuation glyphs are not inserted
2615 for window-based redisplay. */
2616 if (!FRAME_WINDOW_P (it->f))
2617 {
2618 if (it->truncate_lines_p)
2619 it->last_visible_x -= it->truncation_pixel_width;
2620 else
2621 it->last_visible_x -= it->continuation_pixel_width;
2622 }
2623
2624 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2625 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2626 }
2627
2628 /* Leave room for a border glyph. */
2629 if (!FRAME_WINDOW_P (it->f)
2630 && !WINDOW_RIGHTMOST_P (it->w))
2631 it->last_visible_x -= 1;
2632
2633 it->last_visible_y = window_text_bottom_y (w);
2634
2635 /* For mode lines and alike, arrange for the first glyph having a
2636 left box line if the face specifies a box. */
2637 if (base_face_id != DEFAULT_FACE_ID)
2638 {
2639 struct face *face;
2640
2641 it->face_id = base_face_id;
2642
2643 /* If we have a boxed mode line, make the first character appear
2644 with a left box line. */
2645 face = FACE_FROM_ID (it->f, base_face_id);
2646 if (face->box != FACE_NO_BOX)
2647 it->start_of_box_run_p = 1;
2648 }
2649
2650 /* If a buffer position was specified, set the iterator there,
2651 getting overlays and face properties from that position. */
2652 if (charpos >= BUF_BEG (current_buffer))
2653 {
2654 it->end_charpos = ZV;
2655 it->face_id = -1;
2656 IT_CHARPOS (*it) = charpos;
2657
2658 /* Compute byte position if not specified. */
2659 if (bytepos < charpos)
2660 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
2661 else
2662 IT_BYTEPOS (*it) = bytepos;
2663
2664 it->start = it->current;
2665
2666 /* Compute faces etc. */
2667 reseat (it, it->current.pos, 1);
2668 }
2669
2670 CHECK_IT (it);
2671 }
2672
2673
2674 /* Initialize IT for the display of window W with window start POS. */
2675
2676 void
2677 start_display (it, w, pos)
2678 struct it *it;
2679 struct window *w;
2680 struct text_pos pos;
2681 {
2682 struct glyph_row *row;
2683 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
2684
2685 row = w->desired_matrix->rows + first_vpos;
2686 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
2687 it->first_vpos = first_vpos;
2688
2689 /* Don't reseat to previous visible line start if current start
2690 position is in a string or image. */
2691 if (it->method == GET_FROM_BUFFER && !it->truncate_lines_p)
2692 {
2693 int start_at_line_beg_p;
2694 int first_y = it->current_y;
2695
2696 /* If window start is not at a line start, skip forward to POS to
2697 get the correct continuation lines width. */
2698 start_at_line_beg_p = (CHARPOS (pos) == BEGV
2699 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
2700 if (!start_at_line_beg_p)
2701 {
2702 int new_x;
2703
2704 reseat_at_previous_visible_line_start (it);
2705 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
2706
2707 new_x = it->current_x + it->pixel_width;
2708
2709 /* If lines are continued, this line may end in the middle
2710 of a multi-glyph character (e.g. a control character
2711 displayed as \003, or in the middle of an overlay
2712 string). In this case move_it_to above will not have
2713 taken us to the start of the continuation line but to the
2714 end of the continued line. */
2715 if (it->current_x > 0
2716 && !it->truncate_lines_p /* Lines are continued. */
2717 && (/* And glyph doesn't fit on the line. */
2718 new_x > it->last_visible_x
2719 /* Or it fits exactly and we're on a window
2720 system frame. */
2721 || (new_x == it->last_visible_x
2722 && FRAME_WINDOW_P (it->f))))
2723 {
2724 if (it->current.dpvec_index >= 0
2725 || it->current.overlay_string_index >= 0)
2726 {
2727 set_iterator_to_next (it, 1);
2728 move_it_in_display_line_to (it, -1, -1, 0);
2729 }
2730
2731 it->continuation_lines_width += it->current_x;
2732 }
2733
2734 /* We're starting a new display line, not affected by the
2735 height of the continued line, so clear the appropriate
2736 fields in the iterator structure. */
2737 it->max_ascent = it->max_descent = 0;
2738 it->max_phys_ascent = it->max_phys_descent = 0;
2739
2740 it->current_y = first_y;
2741 it->vpos = 0;
2742 it->current_x = it->hpos = 0;
2743 }
2744 }
2745
2746 #if 0 /* Don't assert the following because start_display is sometimes
2747 called intentionally with a window start that is not at a
2748 line start. Please leave this code in as a comment. */
2749
2750 /* Window start should be on a line start, now. */
2751 xassert (it->continuation_lines_width
2752 || IT_CHARPOS (it) == BEGV
2753 || FETCH_BYTE (IT_BYTEPOS (it) - 1) == '\n');
2754 #endif /* 0 */
2755 }
2756
2757
2758 /* Return 1 if POS is a position in ellipses displayed for invisible
2759 text. W is the window we display, for text property lookup. */
2760
2761 static int
2762 in_ellipses_for_invisible_text_p (pos, w)
2763 struct display_pos *pos;
2764 struct window *w;
2765 {
2766 Lisp_Object prop, window;
2767 int ellipses_p = 0;
2768 int charpos = CHARPOS (pos->pos);
2769
2770 /* If POS specifies a position in a display vector, this might
2771 be for an ellipsis displayed for invisible text. We won't
2772 get the iterator set up for delivering that ellipsis unless
2773 we make sure that it gets aware of the invisible text. */
2774 if (pos->dpvec_index >= 0
2775 && pos->overlay_string_index < 0
2776 && CHARPOS (pos->string_pos) < 0
2777 && charpos > BEGV
2778 && (XSETWINDOW (window, w),
2779 prop = Fget_char_property (make_number (charpos),
2780 Qinvisible, window),
2781 !TEXT_PROP_MEANS_INVISIBLE (prop)))
2782 {
2783 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
2784 window);
2785 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
2786 }
2787
2788 return ellipses_p;
2789 }
2790
2791
2792 /* Initialize IT for stepping through current_buffer in window W,
2793 starting at position POS that includes overlay string and display
2794 vector/ control character translation position information. Value
2795 is zero if there are overlay strings with newlines at POS. */
2796
2797 static int
2798 init_from_display_pos (it, w, pos)
2799 struct it *it;
2800 struct window *w;
2801 struct display_pos *pos;
2802 {
2803 int charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
2804 int i, overlay_strings_with_newlines = 0;
2805
2806 /* If POS specifies a position in a display vector, this might
2807 be for an ellipsis displayed for invisible text. We won't
2808 get the iterator set up for delivering that ellipsis unless
2809 we make sure that it gets aware of the invisible text. */
2810 if (in_ellipses_for_invisible_text_p (pos, w))
2811 {
2812 --charpos;
2813 bytepos = 0;
2814 }
2815
2816 /* Keep in mind: the call to reseat in init_iterator skips invisible
2817 text, so we might end up at a position different from POS. This
2818 is only a problem when POS is a row start after a newline and an
2819 overlay starts there with an after-string, and the overlay has an
2820 invisible property. Since we don't skip invisible text in
2821 display_line and elsewhere immediately after consuming the
2822 newline before the row start, such a POS will not be in a string,
2823 but the call to init_iterator below will move us to the
2824 after-string. */
2825 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
2826
2827 /* This only scans the current chunk -- it should scan all chunks.
2828 However, OVERLAY_STRING_CHUNK_SIZE has been increased from 3 in 21.1
2829 to 16 in 22.1 to make this a lesser problem. */
2830 for (i = 0; i < it->n_overlay_strings && i < OVERLAY_STRING_CHUNK_SIZE; ++i)
2831 {
2832 const char *s = SDATA (it->overlay_strings[i]);
2833 const char *e = s + SBYTES (it->overlay_strings[i]);
2834
2835 while (s < e && *s != '\n')
2836 ++s;
2837
2838 if (s < e)
2839 {
2840 overlay_strings_with_newlines = 1;
2841 break;
2842 }
2843 }
2844
2845 /* If position is within an overlay string, set up IT to the right
2846 overlay string. */
2847 if (pos->overlay_string_index >= 0)
2848 {
2849 int relative_index;
2850
2851 /* If the first overlay string happens to have a `display'
2852 property for an image, the iterator will be set up for that
2853 image, and we have to undo that setup first before we can
2854 correct the overlay string index. */
2855 if (it->method == GET_FROM_IMAGE)
2856 pop_it (it);
2857
2858 /* We already have the first chunk of overlay strings in
2859 IT->overlay_strings. Load more until the one for
2860 pos->overlay_string_index is in IT->overlay_strings. */
2861 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
2862 {
2863 int n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
2864 it->current.overlay_string_index = 0;
2865 while (n--)
2866 {
2867 load_overlay_strings (it, 0);
2868 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
2869 }
2870 }
2871
2872 it->current.overlay_string_index = pos->overlay_string_index;
2873 relative_index = (it->current.overlay_string_index
2874 % OVERLAY_STRING_CHUNK_SIZE);
2875 it->string = it->overlay_strings[relative_index];
2876 xassert (STRINGP (it->string));
2877 it->current.string_pos = pos->string_pos;
2878 it->method = GET_FROM_STRING;
2879 }
2880
2881 #if 0 /* This is bogus because POS not having an overlay string
2882 position does not mean it's after the string. Example: A
2883 line starting with a before-string and initialization of IT
2884 to the previous row's end position. */
2885 else if (it->current.overlay_string_index >= 0)
2886 {
2887 /* If POS says we're already after an overlay string ending at
2888 POS, make sure to pop the iterator because it will be in
2889 front of that overlay string. When POS is ZV, we've thereby
2890 also ``processed'' overlay strings at ZV. */
2891 while (it->sp)
2892 pop_it (it);
2893 it->current.overlay_string_index = -1;
2894 it->method = GET_FROM_BUFFER;
2895 if (CHARPOS (pos->pos) == ZV)
2896 it->overlay_strings_at_end_processed_p = 1;
2897 }
2898 #endif /* 0 */
2899
2900 if (CHARPOS (pos->string_pos) >= 0)
2901 {
2902 /* Recorded position is not in an overlay string, but in another
2903 string. This can only be a string from a `display' property.
2904 IT should already be filled with that string. */
2905 it->current.string_pos = pos->string_pos;
2906 xassert (STRINGP (it->string));
2907 }
2908
2909 /* Restore position in display vector translations, control
2910 character translations or ellipses. */
2911 if (pos->dpvec_index >= 0)
2912 {
2913 if (it->dpvec == NULL)
2914 get_next_display_element (it);
2915 xassert (it->dpvec && it->current.dpvec_index == 0);
2916 it->current.dpvec_index = pos->dpvec_index;
2917 }
2918
2919 CHECK_IT (it);
2920 return !overlay_strings_with_newlines;
2921 }
2922
2923
2924 /* Initialize IT for stepping through current_buffer in window W
2925 starting at ROW->start. */
2926
2927 static void
2928 init_to_row_start (it, w, row)
2929 struct it *it;
2930 struct window *w;
2931 struct glyph_row *row;
2932 {
2933 init_from_display_pos (it, w, &row->start);
2934 it->start = row->start;
2935 it->continuation_lines_width = row->continuation_lines_width;
2936 CHECK_IT (it);
2937 }
2938
2939
2940 /* Initialize IT for stepping through current_buffer in window W
2941 starting in the line following ROW, i.e. starting at ROW->end.
2942 Value is zero if there are overlay strings with newlines at ROW's
2943 end position. */
2944
2945 static int
2946 init_to_row_end (it, w, row)
2947 struct it *it;
2948 struct window *w;
2949 struct glyph_row *row;
2950 {
2951 int success = 0;
2952
2953 if (init_from_display_pos (it, w, &row->end))
2954 {
2955 if (row->continued_p)
2956 it->continuation_lines_width
2957 = row->continuation_lines_width + row->pixel_width;
2958 CHECK_IT (it);
2959 success = 1;
2960 }
2961
2962 return success;
2963 }
2964
2965
2966
2967 \f
2968 /***********************************************************************
2969 Text properties
2970 ***********************************************************************/
2971
2972 /* Called when IT reaches IT->stop_charpos. Handle text property and
2973 overlay changes. Set IT->stop_charpos to the next position where
2974 to stop. */
2975
2976 static void
2977 handle_stop (it)
2978 struct it *it;
2979 {
2980 enum prop_handled handled;
2981 int handle_overlay_change_p;
2982 struct props *p;
2983
2984 it->dpvec = NULL;
2985 it->current.dpvec_index = -1;
2986 handle_overlay_change_p = !it->ignore_overlay_strings_at_pos_p;
2987 it->ignore_overlay_strings_at_pos_p = 0;
2988
2989 /* Use face of preceding text for ellipsis (if invisible) */
2990 if (it->selective_display_ellipsis_p)
2991 it->saved_face_id = it->face_id;
2992
2993 do
2994 {
2995 handled = HANDLED_NORMALLY;
2996
2997 /* Call text property handlers. */
2998 for (p = it_props; p->handler; ++p)
2999 {
3000 handled = p->handler (it);
3001
3002 if (handled == HANDLED_RECOMPUTE_PROPS)
3003 break;
3004 else if (handled == HANDLED_RETURN)
3005 return;
3006 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
3007 handle_overlay_change_p = 0;
3008 }
3009
3010 if (handled != HANDLED_RECOMPUTE_PROPS)
3011 {
3012 /* Don't check for overlay strings below when set to deliver
3013 characters from a display vector. */
3014 if (it->method == GET_FROM_DISPLAY_VECTOR)
3015 handle_overlay_change_p = 0;
3016
3017 /* Handle overlay changes. */
3018 if (handle_overlay_change_p)
3019 handled = handle_overlay_change (it);
3020
3021 /* Determine where to stop next. */
3022 if (handled == HANDLED_NORMALLY)
3023 compute_stop_pos (it);
3024 }
3025 }
3026 while (handled == HANDLED_RECOMPUTE_PROPS);
3027 }
3028
3029
3030 /* Compute IT->stop_charpos from text property and overlay change
3031 information for IT's current position. */
3032
3033 static void
3034 compute_stop_pos (it)
3035 struct it *it;
3036 {
3037 register INTERVAL iv, next_iv;
3038 Lisp_Object object, limit, position;
3039
3040 /* If nowhere else, stop at the end. */
3041 it->stop_charpos = it->end_charpos;
3042
3043 if (STRINGP (it->string))
3044 {
3045 /* Strings are usually short, so don't limit the search for
3046 properties. */
3047 object = it->string;
3048 limit = Qnil;
3049 position = make_number (IT_STRING_CHARPOS (*it));
3050 }
3051 else
3052 {
3053 int charpos;
3054
3055 /* If next overlay change is in front of the current stop pos
3056 (which is IT->end_charpos), stop there. Note: value of
3057 next_overlay_change is point-max if no overlay change
3058 follows. */
3059 charpos = next_overlay_change (IT_CHARPOS (*it));
3060 if (charpos < it->stop_charpos)
3061 it->stop_charpos = charpos;
3062
3063 /* If showing the region, we have to stop at the region
3064 start or end because the face might change there. */
3065 if (it->region_beg_charpos > 0)
3066 {
3067 if (IT_CHARPOS (*it) < it->region_beg_charpos)
3068 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
3069 else if (IT_CHARPOS (*it) < it->region_end_charpos)
3070 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
3071 }
3072
3073 /* Set up variables for computing the stop position from text
3074 property changes. */
3075 XSETBUFFER (object, current_buffer);
3076 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
3077 position = make_number (IT_CHARPOS (*it));
3078
3079 }
3080
3081 /* Get the interval containing IT's position. Value is a null
3082 interval if there isn't such an interval. */
3083 iv = validate_interval_range (object, &position, &position, 0);
3084 if (!NULL_INTERVAL_P (iv))
3085 {
3086 Lisp_Object values_here[LAST_PROP_IDX];
3087 struct props *p;
3088
3089 /* Get properties here. */
3090 for (p = it_props; p->handler; ++p)
3091 values_here[p->idx] = textget (iv->plist, *p->name);
3092
3093 /* Look for an interval following iv that has different
3094 properties. */
3095 for (next_iv = next_interval (iv);
3096 (!NULL_INTERVAL_P (next_iv)
3097 && (NILP (limit)
3098 || XFASTINT (limit) > next_iv->position));
3099 next_iv = next_interval (next_iv))
3100 {
3101 for (p = it_props; p->handler; ++p)
3102 {
3103 Lisp_Object new_value;
3104
3105 new_value = textget (next_iv->plist, *p->name);
3106 if (!EQ (values_here[p->idx], new_value))
3107 break;
3108 }
3109
3110 if (p->handler)
3111 break;
3112 }
3113
3114 if (!NULL_INTERVAL_P (next_iv))
3115 {
3116 if (INTEGERP (limit)
3117 && next_iv->position >= XFASTINT (limit))
3118 /* No text property change up to limit. */
3119 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
3120 else
3121 /* Text properties change in next_iv. */
3122 it->stop_charpos = min (it->stop_charpos, next_iv->position);
3123 }
3124 }
3125
3126 xassert (STRINGP (it->string)
3127 || (it->stop_charpos >= BEGV
3128 && it->stop_charpos >= IT_CHARPOS (*it)));
3129 }
3130
3131
3132 /* Return the position of the next overlay change after POS in
3133 current_buffer. Value is point-max if no overlay change
3134 follows. This is like `next-overlay-change' but doesn't use
3135 xmalloc. */
3136
3137 static int
3138 next_overlay_change (pos)
3139 int pos;
3140 {
3141 int noverlays;
3142 int endpos;
3143 Lisp_Object *overlays;
3144 int i;
3145
3146 /* Get all overlays at the given position. */
3147 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, 1);
3148
3149 /* If any of these overlays ends before endpos,
3150 use its ending point instead. */
3151 for (i = 0; i < noverlays; ++i)
3152 {
3153 Lisp_Object oend;
3154 int oendpos;
3155
3156 oend = OVERLAY_END (overlays[i]);
3157 oendpos = OVERLAY_POSITION (oend);
3158 endpos = min (endpos, oendpos);
3159 }
3160
3161 return endpos;
3162 }
3163
3164
3165 \f
3166 /***********************************************************************
3167 Fontification
3168 ***********************************************************************/
3169
3170 /* Handle changes in the `fontified' property of the current buffer by
3171 calling hook functions from Qfontification_functions to fontify
3172 regions of text. */
3173
3174 static enum prop_handled
3175 handle_fontified_prop (it)
3176 struct it *it;
3177 {
3178 Lisp_Object prop, pos;
3179 enum prop_handled handled = HANDLED_NORMALLY;
3180
3181 if (!NILP (Vmemory_full))
3182 return handled;
3183
3184 /* Get the value of the `fontified' property at IT's current buffer
3185 position. (The `fontified' property doesn't have a special
3186 meaning in strings.) If the value is nil, call functions from
3187 Qfontification_functions. */
3188 if (!STRINGP (it->string)
3189 && it->s == NULL
3190 && !NILP (Vfontification_functions)
3191 && !NILP (Vrun_hooks)
3192 && (pos = make_number (IT_CHARPOS (*it)),
3193 prop = Fget_char_property (pos, Qfontified, Qnil),
3194 NILP (prop)))
3195 {
3196 int count = SPECPDL_INDEX ();
3197 Lisp_Object val;
3198
3199 val = Vfontification_functions;
3200 specbind (Qfontification_functions, Qnil);
3201
3202 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
3203 safe_call1 (val, pos);
3204 else
3205 {
3206 Lisp_Object globals, fn;
3207 struct gcpro gcpro1, gcpro2;
3208
3209 globals = Qnil;
3210 GCPRO2 (val, globals);
3211
3212 for (; CONSP (val); val = XCDR (val))
3213 {
3214 fn = XCAR (val);
3215
3216 if (EQ (fn, Qt))
3217 {
3218 /* A value of t indicates this hook has a local
3219 binding; it means to run the global binding too.
3220 In a global value, t should not occur. If it
3221 does, we must ignore it to avoid an endless
3222 loop. */
3223 for (globals = Fdefault_value (Qfontification_functions);
3224 CONSP (globals);
3225 globals = XCDR (globals))
3226 {
3227 fn = XCAR (globals);
3228 if (!EQ (fn, Qt))
3229 safe_call1 (fn, pos);
3230 }
3231 }
3232 else
3233 safe_call1 (fn, pos);
3234 }
3235
3236 UNGCPRO;
3237 }
3238
3239 unbind_to (count, Qnil);
3240
3241 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
3242 something. This avoids an endless loop if they failed to
3243 fontify the text for which reason ever. */
3244 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
3245 handled = HANDLED_RECOMPUTE_PROPS;
3246 }
3247
3248 return handled;
3249 }
3250
3251
3252 \f
3253 /***********************************************************************
3254 Faces
3255 ***********************************************************************/
3256
3257 /* Set up iterator IT from face properties at its current position.
3258 Called from handle_stop. */
3259
3260 static enum prop_handled
3261 handle_face_prop (it)
3262 struct it *it;
3263 {
3264 int new_face_id, next_stop;
3265
3266 if (!STRINGP (it->string))
3267 {
3268 new_face_id
3269 = face_at_buffer_position (it->w,
3270 IT_CHARPOS (*it),
3271 it->region_beg_charpos,
3272 it->region_end_charpos,
3273 &next_stop,
3274 (IT_CHARPOS (*it)
3275 + TEXT_PROP_DISTANCE_LIMIT),
3276 0);
3277
3278 /* Is this a start of a run of characters with box face?
3279 Caveat: this can be called for a freshly initialized
3280 iterator; face_id is -1 in this case. We know that the new
3281 face will not change until limit, i.e. if the new face has a
3282 box, all characters up to limit will have one. But, as
3283 usual, we don't know whether limit is really the end. */
3284 if (new_face_id != it->face_id)
3285 {
3286 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3287
3288 /* If new face has a box but old face has not, this is
3289 the start of a run of characters with box, i.e. it has
3290 a shadow on the left side. The value of face_id of the
3291 iterator will be -1 if this is the initial call that gets
3292 the face. In this case, we have to look in front of IT's
3293 position and see whether there is a face != new_face_id. */
3294 it->start_of_box_run_p
3295 = (new_face->box != FACE_NO_BOX
3296 && (it->face_id >= 0
3297 || IT_CHARPOS (*it) == BEG
3298 || new_face_id != face_before_it_pos (it)));
3299 it->face_box_p = new_face->box != FACE_NO_BOX;
3300 }
3301 }
3302 else
3303 {
3304 int base_face_id, bufpos;
3305
3306 if (it->current.overlay_string_index >= 0)
3307 bufpos = IT_CHARPOS (*it);
3308 else
3309 bufpos = 0;
3310
3311 /* For strings from a buffer, i.e. overlay strings or strings
3312 from a `display' property, use the face at IT's current
3313 buffer position as the base face to merge with, so that
3314 overlay strings appear in the same face as surrounding
3315 text, unless they specify their own faces. */
3316 base_face_id = underlying_face_id (it);
3317
3318 new_face_id = face_at_string_position (it->w,
3319 it->string,
3320 IT_STRING_CHARPOS (*it),
3321 bufpos,
3322 it->region_beg_charpos,
3323 it->region_end_charpos,
3324 &next_stop,
3325 base_face_id, 0);
3326
3327 #if 0 /* This shouldn't be neccessary. Let's check it. */
3328 /* If IT is used to display a mode line we would really like to
3329 use the mode line face instead of the frame's default face. */
3330 if (it->glyph_row == MATRIX_MODE_LINE_ROW (it->w->desired_matrix)
3331 && new_face_id == DEFAULT_FACE_ID)
3332 new_face_id = CURRENT_MODE_LINE_FACE_ID (it->w);
3333 #endif
3334
3335 /* Is this a start of a run of characters with box? Caveat:
3336 this can be called for a freshly allocated iterator; face_id
3337 is -1 is this case. We know that the new face will not
3338 change until the next check pos, i.e. if the new face has a
3339 box, all characters up to that position will have a
3340 box. But, as usual, we don't know whether that position
3341 is really the end. */
3342 if (new_face_id != it->face_id)
3343 {
3344 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3345 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3346
3347 /* If new face has a box but old face hasn't, this is the
3348 start of a run of characters with box, i.e. it has a
3349 shadow on the left side. */
3350 it->start_of_box_run_p
3351 = new_face->box && (old_face == NULL || !old_face->box);
3352 it->face_box_p = new_face->box != FACE_NO_BOX;
3353 }
3354 }
3355
3356 it->face_id = new_face_id;
3357 return HANDLED_NORMALLY;
3358 }
3359
3360
3361 /* Return the ID of the face ``underlying'' IT's current position,
3362 which is in a string. If the iterator is associated with a
3363 buffer, return the face at IT's current buffer position.
3364 Otherwise, use the iterator's base_face_id. */
3365
3366 static int
3367 underlying_face_id (it)
3368 struct it *it;
3369 {
3370 int face_id = it->base_face_id, i;
3371
3372 xassert (STRINGP (it->string));
3373
3374 for (i = it->sp - 1; i >= 0; --i)
3375 if (NILP (it->stack[i].string))
3376 face_id = it->stack[i].face_id;
3377
3378 return face_id;
3379 }
3380
3381
3382 /* Compute the face one character before or after the current position
3383 of IT. BEFORE_P non-zero means get the face in front of IT's
3384 position. Value is the id of the face. */
3385
3386 static int
3387 face_before_or_after_it_pos (it, before_p)
3388 struct it *it;
3389 int before_p;
3390 {
3391 int face_id, limit;
3392 int next_check_charpos;
3393 struct text_pos pos;
3394
3395 xassert (it->s == NULL);
3396
3397 if (STRINGP (it->string))
3398 {
3399 int bufpos, base_face_id;
3400
3401 /* No face change past the end of the string (for the case
3402 we are padding with spaces). No face change before the
3403 string start. */
3404 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
3405 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
3406 return it->face_id;
3407
3408 /* Set pos to the position before or after IT's current position. */
3409 if (before_p)
3410 pos = string_pos (IT_STRING_CHARPOS (*it) - 1, it->string);
3411 else
3412 /* For composition, we must check the character after the
3413 composition. */
3414 pos = (it->what == IT_COMPOSITION
3415 ? string_pos (IT_STRING_CHARPOS (*it) + it->cmp_len, it->string)
3416 : string_pos (IT_STRING_CHARPOS (*it) + 1, it->string));
3417
3418 if (it->current.overlay_string_index >= 0)
3419 bufpos = IT_CHARPOS (*it);
3420 else
3421 bufpos = 0;
3422
3423 base_face_id = underlying_face_id (it);
3424
3425 /* Get the face for ASCII, or unibyte. */
3426 face_id = face_at_string_position (it->w,
3427 it->string,
3428 CHARPOS (pos),
3429 bufpos,
3430 it->region_beg_charpos,
3431 it->region_end_charpos,
3432 &next_check_charpos,
3433 base_face_id, 0);
3434
3435 /* Correct the face for charsets different from ASCII. Do it
3436 for the multibyte case only. The face returned above is
3437 suitable for unibyte text if IT->string is unibyte. */
3438 if (STRING_MULTIBYTE (it->string))
3439 {
3440 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos);
3441 int rest = SBYTES (it->string) - BYTEPOS (pos);
3442 int c, len;
3443 struct face *face = FACE_FROM_ID (it->f, face_id);
3444
3445 c = string_char_and_length (p, rest, &len);
3446 face_id = FACE_FOR_CHAR (it->f, face, c);
3447 }
3448 }
3449 else
3450 {
3451 if ((IT_CHARPOS (*it) >= ZV && !before_p)
3452 || (IT_CHARPOS (*it) <= BEGV && before_p))
3453 return it->face_id;
3454
3455 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
3456 pos = it->current.pos;
3457
3458 if (before_p)
3459 DEC_TEXT_POS (pos, it->multibyte_p);
3460 else
3461 {
3462 if (it->what == IT_COMPOSITION)
3463 /* For composition, we must check the position after the
3464 composition. */
3465 pos.charpos += it->cmp_len, pos.bytepos += it->len;
3466 else
3467 INC_TEXT_POS (pos, it->multibyte_p);
3468 }
3469
3470 /* Determine face for CHARSET_ASCII, or unibyte. */
3471 face_id = face_at_buffer_position (it->w,
3472 CHARPOS (pos),
3473 it->region_beg_charpos,
3474 it->region_end_charpos,
3475 &next_check_charpos,
3476 limit, 0);
3477
3478 /* Correct the face for charsets different from ASCII. Do it
3479 for the multibyte case only. The face returned above is
3480 suitable for unibyte text if current_buffer is unibyte. */
3481 if (it->multibyte_p)
3482 {
3483 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
3484 struct face *face = FACE_FROM_ID (it->f, face_id);
3485 face_id = FACE_FOR_CHAR (it->f, face, c);
3486 }
3487 }
3488
3489 return face_id;
3490 }
3491
3492
3493 \f
3494 /***********************************************************************
3495 Invisible text
3496 ***********************************************************************/
3497
3498 /* Set up iterator IT from invisible properties at its current
3499 position. Called from handle_stop. */
3500
3501 static enum prop_handled
3502 handle_invisible_prop (it)
3503 struct it *it;
3504 {
3505 enum prop_handled handled = HANDLED_NORMALLY;
3506
3507 if (STRINGP (it->string))
3508 {
3509 extern Lisp_Object Qinvisible;
3510 Lisp_Object prop, end_charpos, limit, charpos;
3511
3512 /* Get the value of the invisible text property at the
3513 current position. Value will be nil if there is no such
3514 property. */
3515 charpos = make_number (IT_STRING_CHARPOS (*it));
3516 prop = Fget_text_property (charpos, Qinvisible, it->string);
3517
3518 if (!NILP (prop)
3519 && IT_STRING_CHARPOS (*it) < it->end_charpos)
3520 {
3521 handled = HANDLED_RECOMPUTE_PROPS;
3522
3523 /* Get the position at which the next change of the
3524 invisible text property can be found in IT->string.
3525 Value will be nil if the property value is the same for
3526 all the rest of IT->string. */
3527 XSETINT (limit, SCHARS (it->string));
3528 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
3529 it->string, limit);
3530
3531 /* Text at current position is invisible. The next
3532 change in the property is at position end_charpos.
3533 Move IT's current position to that position. */
3534 if (INTEGERP (end_charpos)
3535 && XFASTINT (end_charpos) < XFASTINT (limit))
3536 {
3537 struct text_pos old;
3538 old = it->current.string_pos;
3539 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
3540 compute_string_pos (&it->current.string_pos, old, it->string);
3541 }
3542 else
3543 {
3544 /* The rest of the string is invisible. If this is an
3545 overlay string, proceed with the next overlay string
3546 or whatever comes and return a character from there. */
3547 if (it->current.overlay_string_index >= 0)
3548 {
3549 next_overlay_string (it);
3550 /* Don't check for overlay strings when we just
3551 finished processing them. */
3552 handled = HANDLED_OVERLAY_STRING_CONSUMED;
3553 }
3554 else
3555 {
3556 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
3557 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
3558 }
3559 }
3560 }
3561 }
3562 else
3563 {
3564 int invis_p, newpos, next_stop, start_charpos;
3565 Lisp_Object pos, prop, overlay;
3566
3567 /* First of all, is there invisible text at this position? */
3568 start_charpos = IT_CHARPOS (*it);
3569 pos = make_number (IT_CHARPOS (*it));
3570 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
3571 &overlay);
3572 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3573
3574 /* If we are on invisible text, skip over it. */
3575 if (invis_p && IT_CHARPOS (*it) < it->end_charpos)
3576 {
3577 /* Record whether we have to display an ellipsis for the
3578 invisible text. */
3579 int display_ellipsis_p = invis_p == 2;
3580
3581 handled = HANDLED_RECOMPUTE_PROPS;
3582
3583 /* Loop skipping over invisible text. The loop is left at
3584 ZV or with IT on the first char being visible again. */
3585 do
3586 {
3587 /* Try to skip some invisible text. Return value is the
3588 position reached which can be equal to IT's position
3589 if there is nothing invisible here. This skips both
3590 over invisible text properties and overlays with
3591 invisible property. */
3592 newpos = skip_invisible (IT_CHARPOS (*it),
3593 &next_stop, ZV, it->window);
3594
3595 /* If we skipped nothing at all we weren't at invisible
3596 text in the first place. If everything to the end of
3597 the buffer was skipped, end the loop. */
3598 if (newpos == IT_CHARPOS (*it) || newpos >= ZV)
3599 invis_p = 0;
3600 else
3601 {
3602 /* We skipped some characters but not necessarily
3603 all there are. Check if we ended up on visible
3604 text. Fget_char_property returns the property of
3605 the char before the given position, i.e. if we
3606 get invis_p = 0, this means that the char at
3607 newpos is visible. */
3608 pos = make_number (newpos);
3609 prop = Fget_char_property (pos, Qinvisible, it->window);
3610 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3611 }
3612
3613 /* If we ended up on invisible text, proceed to
3614 skip starting with next_stop. */
3615 if (invis_p)
3616 IT_CHARPOS (*it) = next_stop;
3617
3618 /* If there are adjacent invisible texts, don't lose the
3619 second one's ellipsis. */
3620 if (invis_p == 2)
3621 display_ellipsis_p = 1;
3622 }
3623 while (invis_p);
3624
3625 /* The position newpos is now either ZV or on visible text. */
3626 IT_CHARPOS (*it) = newpos;
3627 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
3628
3629 /* If there are before-strings at the start of invisible
3630 text, and the text is invisible because of a text
3631 property, arrange to show before-strings because 20.x did
3632 it that way. (If the text is invisible because of an
3633 overlay property instead of a text property, this is
3634 already handled in the overlay code.) */
3635 if (NILP (overlay)
3636 && get_overlay_strings (it, start_charpos))
3637 {
3638 handled = HANDLED_RECOMPUTE_PROPS;
3639 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
3640 }
3641 else if (display_ellipsis_p)
3642 {
3643 /* Make sure that the glyphs of the ellipsis will get
3644 correct `charpos' values. If we would not update
3645 it->position here, the glyphs would belong to the
3646 last visible character _before_ the invisible
3647 text, which confuses `set_cursor_from_row'.
3648
3649 We use the last invisible position instead of the
3650 first because this way the cursor is always drawn on
3651 the first "." of the ellipsis, whenever PT is inside
3652 the invisible text. Otherwise the cursor would be
3653 placed _after_ the ellipsis when the point is after the
3654 first invisible character. */
3655 it->position.charpos = IT_CHARPOS (*it) - 1;
3656 it->position.bytepos = CHAR_TO_BYTE (it->position.charpos);
3657 setup_for_ellipsis (it, 0);
3658 }
3659 }
3660 }
3661
3662 return handled;
3663 }
3664
3665
3666 /* Make iterator IT return `...' next.
3667 Replaces LEN characters from buffer. */
3668
3669 static void
3670 setup_for_ellipsis (it, len)
3671 struct it *it;
3672 int len;
3673 {
3674 /* Use the display table definition for `...'. Invalid glyphs
3675 will be handled by the method returning elements from dpvec. */
3676 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
3677 {
3678 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
3679 it->dpvec = v->contents;
3680 it->dpend = v->contents + v->size;
3681 }
3682 else
3683 {
3684 /* Default `...'. */
3685 it->dpvec = default_invis_vector;
3686 it->dpend = default_invis_vector + 3;
3687 }
3688
3689 it->dpvec_char_len = len;
3690 it->current.dpvec_index = 0;
3691 it->dpvec_face_id = -1;
3692
3693 /* Remember the current face id in case glyphs specify faces.
3694 IT's face is restored in set_iterator_to_next.
3695 saved_face_id was set to preceding char's face in handle_stop. */
3696 if (it->saved_face_id < 0 || it->saved_face_id != it->face_id)
3697 it->saved_face_id = it->face_id = DEFAULT_FACE_ID;
3698
3699 it->method = GET_FROM_DISPLAY_VECTOR;
3700 it->ellipsis_p = 1;
3701 }
3702
3703
3704 \f
3705 /***********************************************************************
3706 'display' property
3707 ***********************************************************************/
3708
3709 /* Set up iterator IT from `display' property at its current position.
3710 Called from handle_stop.
3711 We return HANDLED_RETURN if some part of the display property
3712 overrides the display of the buffer text itself.
3713 Otherwise we return HANDLED_NORMALLY. */
3714
3715 static enum prop_handled
3716 handle_display_prop (it)
3717 struct it *it;
3718 {
3719 Lisp_Object prop, object;
3720 struct text_pos *position;
3721 /* Nonzero if some property replaces the display of the text itself. */
3722 int display_replaced_p = 0;
3723
3724 if (STRINGP (it->string))
3725 {
3726 object = it->string;
3727 position = &it->current.string_pos;
3728 }
3729 else
3730 {
3731 XSETWINDOW (object, it->w);
3732 position = &it->current.pos;
3733 }
3734
3735 /* Reset those iterator values set from display property values. */
3736 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
3737 it->space_width = Qnil;
3738 it->font_height = Qnil;
3739 it->voffset = 0;
3740
3741 /* We don't support recursive `display' properties, i.e. string
3742 values that have a string `display' property, that have a string
3743 `display' property etc. */
3744 if (!it->string_from_display_prop_p)
3745 it->area = TEXT_AREA;
3746
3747 prop = Fget_char_property (make_number (position->charpos),
3748 Qdisplay, object);
3749 if (NILP (prop))
3750 return HANDLED_NORMALLY;
3751
3752 if (!STRINGP (it->string))
3753 object = it->w->buffer;
3754
3755 if (CONSP (prop)
3756 /* Simple properties. */
3757 && !EQ (XCAR (prop), Qimage)
3758 && !EQ (XCAR (prop), Qspace)
3759 && !EQ (XCAR (prop), Qwhen)
3760 && !EQ (XCAR (prop), Qslice)
3761 && !EQ (XCAR (prop), Qspace_width)
3762 && !EQ (XCAR (prop), Qheight)
3763 && !EQ (XCAR (prop), Qraise)
3764 /* Marginal area specifications. */
3765 && !(CONSP (XCAR (prop)) && EQ (XCAR (XCAR (prop)), Qmargin))
3766 && !EQ (XCAR (prop), Qleft_fringe)
3767 && !EQ (XCAR (prop), Qright_fringe)
3768 && !NILP (XCAR (prop)))
3769 {
3770 for (; CONSP (prop); prop = XCDR (prop))
3771 {
3772 if (handle_single_display_spec (it, XCAR (prop), object,
3773 position, display_replaced_p))
3774 display_replaced_p = 1;
3775 }
3776 }
3777 else if (VECTORP (prop))
3778 {
3779 int i;
3780 for (i = 0; i < ASIZE (prop); ++i)
3781 if (handle_single_display_spec (it, AREF (prop, i), object,
3782 position, display_replaced_p))
3783 display_replaced_p = 1;
3784 }
3785 else
3786 {
3787 int ret = handle_single_display_spec (it, prop, object, position, 0);
3788 if (ret < 0) /* Replaced by "", i.e. nothing. */
3789 return HANDLED_RECOMPUTE_PROPS;
3790 if (ret)
3791 display_replaced_p = 1;
3792 }
3793
3794 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
3795 }
3796
3797
3798 /* Value is the position of the end of the `display' property starting
3799 at START_POS in OBJECT. */
3800
3801 static struct text_pos
3802 display_prop_end (it, object, start_pos)
3803 struct it *it;
3804 Lisp_Object object;
3805 struct text_pos start_pos;
3806 {
3807 Lisp_Object end;
3808 struct text_pos end_pos;
3809
3810 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
3811 Qdisplay, object, Qnil);
3812 CHARPOS (end_pos) = XFASTINT (end);
3813 if (STRINGP (object))
3814 compute_string_pos (&end_pos, start_pos, it->string);
3815 else
3816 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
3817
3818 return end_pos;
3819 }
3820
3821
3822 /* Set up IT from a single `display' specification PROP. OBJECT
3823 is the object in which the `display' property was found. *POSITION
3824 is the position at which it was found. DISPLAY_REPLACED_P non-zero
3825 means that we previously saw a display specification which already
3826 replaced text display with something else, for example an image;
3827 we ignore such properties after the first one has been processed.
3828
3829 If PROP is a `space' or `image' specification, and in some other
3830 cases too, set *POSITION to the position where the `display'
3831 property ends.
3832
3833 Value is non-zero if something was found which replaces the display
3834 of buffer or string text. Specifically, the value is -1 if that
3835 "something" is "nothing". */
3836
3837 static int
3838 handle_single_display_spec (it, spec, object, position,
3839 display_replaced_before_p)
3840 struct it *it;
3841 Lisp_Object spec;
3842 Lisp_Object object;
3843 struct text_pos *position;
3844 int display_replaced_before_p;
3845 {
3846 Lisp_Object form;
3847 Lisp_Object location, value;
3848 struct text_pos start_pos;
3849 int valid_p;
3850
3851 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
3852 If the result is non-nil, use VALUE instead of SPEC. */
3853 form = Qt;
3854 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
3855 {
3856 spec = XCDR (spec);
3857 if (!CONSP (spec))
3858 return 0;
3859 form = XCAR (spec);
3860 spec = XCDR (spec);
3861 }
3862
3863 if (!NILP (form) && !EQ (form, Qt))
3864 {
3865 int count = SPECPDL_INDEX ();
3866 struct gcpro gcpro1;
3867
3868 /* Bind `object' to the object having the `display' property, a
3869 buffer or string. Bind `position' to the position in the
3870 object where the property was found, and `buffer-position'
3871 to the current position in the buffer. */
3872 specbind (Qobject, object);
3873 specbind (Qposition, make_number (CHARPOS (*position)));
3874 specbind (Qbuffer_position,
3875 make_number (STRINGP (object)
3876 ? IT_CHARPOS (*it) : CHARPOS (*position)));
3877 GCPRO1 (form);
3878 form = safe_eval (form);
3879 UNGCPRO;
3880 unbind_to (count, Qnil);
3881 }
3882
3883 if (NILP (form))
3884 return 0;
3885
3886 /* Handle `(height HEIGHT)' specifications. */
3887 if (CONSP (spec)
3888 && EQ (XCAR (spec), Qheight)
3889 && CONSP (XCDR (spec)))
3890 {
3891 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3892 return 0;
3893
3894 it->font_height = XCAR (XCDR (spec));
3895 if (!NILP (it->font_height))
3896 {
3897 struct face *face = FACE_FROM_ID (it->f, it->face_id);
3898 int new_height = -1;
3899
3900 if (CONSP (it->font_height)
3901 && (EQ (XCAR (it->font_height), Qplus)
3902 || EQ (XCAR (it->font_height), Qminus))
3903 && CONSP (XCDR (it->font_height))
3904 && INTEGERP (XCAR (XCDR (it->font_height))))
3905 {
3906 /* `(+ N)' or `(- N)' where N is an integer. */
3907 int steps = XINT (XCAR (XCDR (it->font_height)));
3908 if (EQ (XCAR (it->font_height), Qplus))
3909 steps = - steps;
3910 it->face_id = smaller_face (it->f, it->face_id, steps);
3911 }
3912 else if (FUNCTIONP (it->font_height))
3913 {
3914 /* Call function with current height as argument.
3915 Value is the new height. */
3916 Lisp_Object height;
3917 height = safe_call1 (it->font_height,
3918 face->lface[LFACE_HEIGHT_INDEX]);
3919 if (NUMBERP (height))
3920 new_height = XFLOATINT (height);
3921 }
3922 else if (NUMBERP (it->font_height))
3923 {
3924 /* Value is a multiple of the canonical char height. */
3925 struct face *face;
3926
3927 face = FACE_FROM_ID (it->f, DEFAULT_FACE_ID);
3928 new_height = (XFLOATINT (it->font_height)
3929 * XINT (face->lface[LFACE_HEIGHT_INDEX]));
3930 }
3931 else
3932 {
3933 /* Evaluate IT->font_height with `height' bound to the
3934 current specified height to get the new height. */
3935 int count = SPECPDL_INDEX ();
3936
3937 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
3938 value = safe_eval (it->font_height);
3939 unbind_to (count, Qnil);
3940
3941 if (NUMBERP (value))
3942 new_height = XFLOATINT (value);
3943 }
3944
3945 if (new_height > 0)
3946 it->face_id = face_with_height (it->f, it->face_id, new_height);
3947 }
3948
3949 return 0;
3950 }
3951
3952 /* Handle `(space_width WIDTH)'. */
3953 if (CONSP (spec)
3954 && EQ (XCAR (spec), Qspace_width)
3955 && CONSP (XCDR (spec)))
3956 {
3957 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3958 return 0;
3959
3960 value = XCAR (XCDR (spec));
3961 if (NUMBERP (value) && XFLOATINT (value) > 0)
3962 it->space_width = value;
3963
3964 return 0;
3965 }
3966
3967 /* Handle `(slice X Y WIDTH HEIGHT)'. */
3968 if (CONSP (spec)
3969 && EQ (XCAR (spec), Qslice))
3970 {
3971 Lisp_Object tem;
3972
3973 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3974 return 0;
3975
3976 if (tem = XCDR (spec), CONSP (tem))
3977 {
3978 it->slice.x = XCAR (tem);
3979 if (tem = XCDR (tem), CONSP (tem))
3980 {
3981 it->slice.y = XCAR (tem);
3982 if (tem = XCDR (tem), CONSP (tem))
3983 {
3984 it->slice.width = XCAR (tem);
3985 if (tem = XCDR (tem), CONSP (tem))
3986 it->slice.height = XCAR (tem);
3987 }
3988 }
3989 }
3990
3991 return 0;
3992 }
3993
3994 /* Handle `(raise FACTOR)'. */
3995 if (CONSP (spec)
3996 && EQ (XCAR (spec), Qraise)
3997 && CONSP (XCDR (spec)))
3998 {
3999 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
4000 return 0;
4001
4002 #ifdef HAVE_WINDOW_SYSTEM
4003 value = XCAR (XCDR (spec));
4004 if (NUMBERP (value))
4005 {
4006 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4007 it->voffset = - (XFLOATINT (value)
4008 * (FONT_HEIGHT (face->font)));
4009 }
4010 #endif /* HAVE_WINDOW_SYSTEM */
4011
4012 return 0;
4013 }
4014
4015 /* Don't handle the other kinds of display specifications
4016 inside a string that we got from a `display' property. */
4017 if (it->string_from_display_prop_p)
4018 return 0;
4019
4020 /* Characters having this form of property are not displayed, so
4021 we have to find the end of the property. */
4022 start_pos = *position;
4023 *position = display_prop_end (it, object, start_pos);
4024 value = Qnil;
4025
4026 /* Stop the scan at that end position--we assume that all
4027 text properties change there. */
4028 it->stop_charpos = position->charpos;
4029
4030 /* Handle `(left-fringe BITMAP [FACE])'
4031 and `(right-fringe BITMAP [FACE])'. */
4032 if (CONSP (spec)
4033 && (EQ (XCAR (spec), Qleft_fringe)
4034 || EQ (XCAR (spec), Qright_fringe))
4035 && CONSP (XCDR (spec)))
4036 {
4037 int face_id = DEFAULT_FACE_ID;
4038 int fringe_bitmap;
4039
4040 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
4041 /* If we return here, POSITION has been advanced
4042 across the text with this property. */
4043 return 0;
4044
4045 #ifdef HAVE_WINDOW_SYSTEM
4046 value = XCAR (XCDR (spec));
4047 if (!SYMBOLP (value)
4048 || !(fringe_bitmap = lookup_fringe_bitmap (value)))
4049 /* If we return here, POSITION has been advanced
4050 across the text with this property. */
4051 return 0;
4052
4053 if (CONSP (XCDR (XCDR (spec))))
4054 {
4055 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
4056 int face_id2 = lookup_derived_face (it->f, face_name,
4057 'A', FRINGE_FACE_ID, 0);
4058 if (face_id2 >= 0)
4059 face_id = face_id2;
4060 }
4061
4062 /* Save current settings of IT so that we can restore them
4063 when we are finished with the glyph property value. */
4064
4065 push_it (it);
4066
4067 it->area = TEXT_AREA;
4068 it->what = IT_IMAGE;
4069 it->image_id = -1; /* no image */
4070 it->position = start_pos;
4071 it->object = NILP (object) ? it->w->buffer : object;
4072 it->method = GET_FROM_IMAGE;
4073 it->face_id = face_id;
4074
4075 /* Say that we haven't consumed the characters with
4076 `display' property yet. The call to pop_it in
4077 set_iterator_to_next will clean this up. */
4078 *position = start_pos;
4079
4080 if (EQ (XCAR (spec), Qleft_fringe))
4081 {
4082 it->left_user_fringe_bitmap = fringe_bitmap;
4083 it->left_user_fringe_face_id = face_id;
4084 }
4085 else
4086 {
4087 it->right_user_fringe_bitmap = fringe_bitmap;
4088 it->right_user_fringe_face_id = face_id;
4089 }
4090 #endif /* HAVE_WINDOW_SYSTEM */
4091 return 1;
4092 }
4093
4094 /* Prepare to handle `((margin left-margin) ...)',
4095 `((margin right-margin) ...)' and `((margin nil) ...)'
4096 prefixes for display specifications. */
4097 location = Qunbound;
4098 if (CONSP (spec) && CONSP (XCAR (spec)))
4099 {
4100 Lisp_Object tem;
4101
4102 value = XCDR (spec);
4103 if (CONSP (value))
4104 value = XCAR (value);
4105
4106 tem = XCAR (spec);
4107 if (EQ (XCAR (tem), Qmargin)
4108 && (tem = XCDR (tem),
4109 tem = CONSP (tem) ? XCAR (tem) : Qnil,
4110 (NILP (tem)
4111 || EQ (tem, Qleft_margin)
4112 || EQ (tem, Qright_margin))))
4113 location = tem;
4114 }
4115
4116 if (EQ (location, Qunbound))
4117 {
4118 location = Qnil;
4119 value = spec;
4120 }
4121
4122 /* After this point, VALUE is the property after any
4123 margin prefix has been stripped. It must be a string,
4124 an image specification, or `(space ...)'.
4125
4126 LOCATION specifies where to display: `left-margin',
4127 `right-margin' or nil. */
4128
4129 valid_p = (STRINGP (value)
4130 #ifdef HAVE_WINDOW_SYSTEM
4131 || (!FRAME_TERMCAP_P (it->f) && valid_image_p (value))
4132 #endif /* not HAVE_WINDOW_SYSTEM */
4133 || (CONSP (value) && EQ (XCAR (value), Qspace)));
4134
4135 if (valid_p && !display_replaced_before_p)
4136 {
4137 /* Save current settings of IT so that we can restore them
4138 when we are finished with the glyph property value. */
4139 push_it (it);
4140
4141 if (NILP (location))
4142 it->area = TEXT_AREA;
4143 else if (EQ (location, Qleft_margin))
4144 it->area = LEFT_MARGIN_AREA;
4145 else
4146 it->area = RIGHT_MARGIN_AREA;
4147
4148 if (STRINGP (value))
4149 {
4150 if (SCHARS (value) == 0)
4151 {
4152 pop_it (it);
4153 return -1; /* Replaced by "", i.e. nothing. */
4154 }
4155 it->string = value;
4156 it->multibyte_p = STRING_MULTIBYTE (it->string);
4157 it->current.overlay_string_index = -1;
4158 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
4159 it->end_charpos = it->string_nchars = SCHARS (it->string);
4160 it->method = GET_FROM_STRING;
4161 it->stop_charpos = 0;
4162 it->string_from_display_prop_p = 1;
4163 /* Say that we haven't consumed the characters with
4164 `display' property yet. The call to pop_it in
4165 set_iterator_to_next will clean this up. */
4166 *position = start_pos;
4167 }
4168 else if (CONSP (value) && EQ (XCAR (value), Qspace))
4169 {
4170 it->method = GET_FROM_STRETCH;
4171 it->object = value;
4172 *position = it->position = start_pos;
4173 }
4174 #ifdef HAVE_WINDOW_SYSTEM
4175 else
4176 {
4177 it->what = IT_IMAGE;
4178 it->image_id = lookup_image (it->f, value);
4179 it->position = start_pos;
4180 it->object = NILP (object) ? it->w->buffer : object;
4181 it->method = GET_FROM_IMAGE;
4182
4183 /* Say that we haven't consumed the characters with
4184 `display' property yet. The call to pop_it in
4185 set_iterator_to_next will clean this up. */
4186 *position = start_pos;
4187 }
4188 #endif /* HAVE_WINDOW_SYSTEM */
4189
4190 return 1;
4191 }
4192
4193 /* Invalid property or property not supported. Restore
4194 POSITION to what it was before. */
4195 *position = start_pos;
4196 return 0;
4197 }
4198
4199
4200 /* Check if SPEC is a display specification value whose text should be
4201 treated as intangible. */
4202
4203 static int
4204 single_display_spec_intangible_p (prop)
4205 Lisp_Object prop;
4206 {
4207 /* Skip over `when FORM'. */
4208 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
4209 {
4210 prop = XCDR (prop);
4211 if (!CONSP (prop))
4212 return 0;
4213 prop = XCDR (prop);
4214 }
4215
4216 if (STRINGP (prop))
4217 return 1;
4218
4219 if (!CONSP (prop))
4220 return 0;
4221
4222 /* Skip over `margin LOCATION'. If LOCATION is in the margins,
4223 we don't need to treat text as intangible. */
4224 if (EQ (XCAR (prop), Qmargin))
4225 {
4226 prop = XCDR (prop);
4227 if (!CONSP (prop))
4228 return 0;
4229
4230 prop = XCDR (prop);
4231 if (!CONSP (prop)
4232 || EQ (XCAR (prop), Qleft_margin)
4233 || EQ (XCAR (prop), Qright_margin))
4234 return 0;
4235 }
4236
4237 return (CONSP (prop)
4238 && (EQ (XCAR (prop), Qimage)
4239 || EQ (XCAR (prop), Qspace)));
4240 }
4241
4242
4243 /* Check if PROP is a display property value whose text should be
4244 treated as intangible. */
4245
4246 int
4247 display_prop_intangible_p (prop)
4248 Lisp_Object prop;
4249 {
4250 if (CONSP (prop)
4251 && CONSP (XCAR (prop))
4252 && !EQ (Qmargin, XCAR (XCAR (prop))))
4253 {
4254 /* A list of sub-properties. */
4255 while (CONSP (prop))
4256 {
4257 if (single_display_spec_intangible_p (XCAR (prop)))
4258 return 1;
4259 prop = XCDR (prop);
4260 }
4261 }
4262 else if (VECTORP (prop))
4263 {
4264 /* A vector of sub-properties. */
4265 int i;
4266 for (i = 0; i < ASIZE (prop); ++i)
4267 if (single_display_spec_intangible_p (AREF (prop, i)))
4268 return 1;
4269 }
4270 else
4271 return single_display_spec_intangible_p (prop);
4272
4273 return 0;
4274 }
4275
4276
4277 /* Return 1 if PROP is a display sub-property value containing STRING. */
4278
4279 static int
4280 single_display_spec_string_p (prop, string)
4281 Lisp_Object prop, string;
4282 {
4283 if (EQ (string, prop))
4284 return 1;
4285
4286 /* Skip over `when FORM'. */
4287 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
4288 {
4289 prop = XCDR (prop);
4290 if (!CONSP (prop))
4291 return 0;
4292 prop = XCDR (prop);
4293 }
4294
4295 if (CONSP (prop))
4296 /* Skip over `margin LOCATION'. */
4297 if (EQ (XCAR (prop), Qmargin))
4298 {
4299 prop = XCDR (prop);
4300 if (!CONSP (prop))
4301 return 0;
4302
4303 prop = XCDR (prop);
4304 if (!CONSP (prop))
4305 return 0;
4306 }
4307
4308 return CONSP (prop) && EQ (XCAR (prop), string);
4309 }
4310
4311
4312 /* Return 1 if STRING appears in the `display' property PROP. */
4313
4314 static int
4315 display_prop_string_p (prop, string)
4316 Lisp_Object prop, string;
4317 {
4318 if (CONSP (prop)
4319 && CONSP (XCAR (prop))
4320 && !EQ (Qmargin, XCAR (XCAR (prop))))
4321 {
4322 /* A list of sub-properties. */
4323 while (CONSP (prop))
4324 {
4325 if (single_display_spec_string_p (XCAR (prop), string))
4326 return 1;
4327 prop = XCDR (prop);
4328 }
4329 }
4330 else if (VECTORP (prop))
4331 {
4332 /* A vector of sub-properties. */
4333 int i;
4334 for (i = 0; i < ASIZE (prop); ++i)
4335 if (single_display_spec_string_p (AREF (prop, i), string))
4336 return 1;
4337 }
4338 else
4339 return single_display_spec_string_p (prop, string);
4340
4341 return 0;
4342 }
4343
4344
4345 /* Determine from which buffer position in W's buffer STRING comes
4346 from. AROUND_CHARPOS is an approximate position where it could
4347 be from. Value is the buffer position or 0 if it couldn't be
4348 determined.
4349
4350 W's buffer must be current.
4351
4352 This function is necessary because we don't record buffer positions
4353 in glyphs generated from strings (to keep struct glyph small).
4354 This function may only use code that doesn't eval because it is
4355 called asynchronously from note_mouse_highlight. */
4356
4357 int
4358 string_buffer_position (w, string, around_charpos)
4359 struct window *w;
4360 Lisp_Object string;
4361 int around_charpos;
4362 {
4363 Lisp_Object limit, prop, pos;
4364 const int MAX_DISTANCE = 1000;
4365 int found = 0;
4366
4367 pos = make_number (around_charpos);
4368 limit = make_number (min (XINT (pos) + MAX_DISTANCE, ZV));
4369 while (!found && !EQ (pos, limit))
4370 {
4371 prop = Fget_char_property (pos, Qdisplay, Qnil);
4372 if (!NILP (prop) && display_prop_string_p (prop, string))
4373 found = 1;
4374 else
4375 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil, limit);
4376 }
4377
4378 if (!found)
4379 {
4380 pos = make_number (around_charpos);
4381 limit = make_number (max (XINT (pos) - MAX_DISTANCE, BEGV));
4382 while (!found && !EQ (pos, limit))
4383 {
4384 prop = Fget_char_property (pos, Qdisplay, Qnil);
4385 if (!NILP (prop) && display_prop_string_p (prop, string))
4386 found = 1;
4387 else
4388 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
4389 limit);
4390 }
4391 }
4392
4393 return found ? XINT (pos) : 0;
4394 }
4395
4396
4397 \f
4398 /***********************************************************************
4399 `composition' property
4400 ***********************************************************************/
4401
4402 /* Set up iterator IT from `composition' property at its current
4403 position. Called from handle_stop. */
4404
4405 static enum prop_handled
4406 handle_composition_prop (it)
4407 struct it *it;
4408 {
4409 Lisp_Object prop, string;
4410 int pos, pos_byte, end;
4411 enum prop_handled handled = HANDLED_NORMALLY;
4412
4413 if (STRINGP (it->string))
4414 {
4415 pos = IT_STRING_CHARPOS (*it);
4416 pos_byte = IT_STRING_BYTEPOS (*it);
4417 string = it->string;
4418 }
4419 else
4420 {
4421 pos = IT_CHARPOS (*it);
4422 pos_byte = IT_BYTEPOS (*it);
4423 string = Qnil;
4424 }
4425
4426 /* If there's a valid composition and point is not inside of the
4427 composition (in the case that the composition is from the current
4428 buffer), draw a glyph composed from the composition components. */
4429 if (find_composition (pos, -1, &pos, &end, &prop, string)
4430 && COMPOSITION_VALID_P (pos, end, prop)
4431 && (STRINGP (it->string) || (PT <= pos || PT >= end)))
4432 {
4433 int id = get_composition_id (pos, pos_byte, end - pos, prop, string);
4434
4435 if (id >= 0)
4436 {
4437 it->method = GET_FROM_COMPOSITION;
4438 it->cmp_id = id;
4439 it->cmp_len = COMPOSITION_LENGTH (prop);
4440 /* For a terminal, draw only the first character of the
4441 components. */
4442 it->c = COMPOSITION_GLYPH (composition_table[id], 0);
4443 it->len = (STRINGP (it->string)
4444 ? string_char_to_byte (it->string, end)
4445 : CHAR_TO_BYTE (end)) - pos_byte;
4446 it->stop_charpos = end;
4447 handled = HANDLED_RETURN;
4448 }
4449 }
4450
4451 return handled;
4452 }
4453
4454
4455 \f
4456 /***********************************************************************
4457 Overlay strings
4458 ***********************************************************************/
4459
4460 /* The following structure is used to record overlay strings for
4461 later sorting in load_overlay_strings. */
4462
4463 struct overlay_entry
4464 {
4465 Lisp_Object overlay;
4466 Lisp_Object string;
4467 int priority;
4468 int after_string_p;
4469 };
4470
4471
4472 /* Set up iterator IT from overlay strings at its current position.
4473 Called from handle_stop. */
4474
4475 static enum prop_handled
4476 handle_overlay_change (it)
4477 struct it *it;
4478 {
4479 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
4480 return HANDLED_RECOMPUTE_PROPS;
4481 else
4482 return HANDLED_NORMALLY;
4483 }
4484
4485
4486 /* Set up the next overlay string for delivery by IT, if there is an
4487 overlay string to deliver. Called by set_iterator_to_next when the
4488 end of the current overlay string is reached. If there are more
4489 overlay strings to display, IT->string and
4490 IT->current.overlay_string_index are set appropriately here.
4491 Otherwise IT->string is set to nil. */
4492
4493 static void
4494 next_overlay_string (it)
4495 struct it *it;
4496 {
4497 ++it->current.overlay_string_index;
4498 if (it->current.overlay_string_index == it->n_overlay_strings)
4499 {
4500 /* No more overlay strings. Restore IT's settings to what
4501 they were before overlay strings were processed, and
4502 continue to deliver from current_buffer. */
4503 int display_ellipsis_p = it->stack[it->sp - 1].display_ellipsis_p;
4504
4505 pop_it (it);
4506 xassert (it->stop_charpos >= BEGV
4507 && it->stop_charpos <= it->end_charpos);
4508 it->string = Qnil;
4509 it->current.overlay_string_index = -1;
4510 SET_TEXT_POS (it->current.string_pos, -1, -1);
4511 it->n_overlay_strings = 0;
4512 it->method = GET_FROM_BUFFER;
4513
4514 /* If we're at the end of the buffer, record that we have
4515 processed the overlay strings there already, so that
4516 next_element_from_buffer doesn't try it again. */
4517 if (IT_CHARPOS (*it) >= it->end_charpos)
4518 it->overlay_strings_at_end_processed_p = 1;
4519
4520 /* If we have to display `...' for invisible text, set
4521 the iterator up for that. */
4522 if (display_ellipsis_p)
4523 setup_for_ellipsis (it, 0);
4524 }
4525 else
4526 {
4527 /* There are more overlay strings to process. If
4528 IT->current.overlay_string_index has advanced to a position
4529 where we must load IT->overlay_strings with more strings, do
4530 it. */
4531 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
4532
4533 if (it->current.overlay_string_index && i == 0)
4534 load_overlay_strings (it, 0);
4535
4536 /* Initialize IT to deliver display elements from the overlay
4537 string. */
4538 it->string = it->overlay_strings[i];
4539 it->multibyte_p = STRING_MULTIBYTE (it->string);
4540 SET_TEXT_POS (it->current.string_pos, 0, 0);
4541 it->method = GET_FROM_STRING;
4542 it->stop_charpos = 0;
4543 }
4544
4545 CHECK_IT (it);
4546 }
4547
4548
4549 /* Compare two overlay_entry structures E1 and E2. Used as a
4550 comparison function for qsort in load_overlay_strings. Overlay
4551 strings for the same position are sorted so that
4552
4553 1. All after-strings come in front of before-strings, except
4554 when they come from the same overlay.
4555
4556 2. Within after-strings, strings are sorted so that overlay strings
4557 from overlays with higher priorities come first.
4558
4559 2. Within before-strings, strings are sorted so that overlay
4560 strings from overlays with higher priorities come last.
4561
4562 Value is analogous to strcmp. */
4563
4564
4565 static int
4566 compare_overlay_entries (e1, e2)
4567 void *e1, *e2;
4568 {
4569 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
4570 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
4571 int result;
4572
4573 if (entry1->after_string_p != entry2->after_string_p)
4574 {
4575 /* Let after-strings appear in front of before-strings if
4576 they come from different overlays. */
4577 if (EQ (entry1->overlay, entry2->overlay))
4578 result = entry1->after_string_p ? 1 : -1;
4579 else
4580 result = entry1->after_string_p ? -1 : 1;
4581 }
4582 else if (entry1->after_string_p)
4583 /* After-strings sorted in order of decreasing priority. */
4584 result = entry2->priority - entry1->priority;
4585 else
4586 /* Before-strings sorted in order of increasing priority. */
4587 result = entry1->priority - entry2->priority;
4588
4589 return result;
4590 }
4591
4592
4593 /* Load the vector IT->overlay_strings with overlay strings from IT's
4594 current buffer position, or from CHARPOS if that is > 0. Set
4595 IT->n_overlays to the total number of overlay strings found.
4596
4597 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
4598 a time. On entry into load_overlay_strings,
4599 IT->current.overlay_string_index gives the number of overlay
4600 strings that have already been loaded by previous calls to this
4601 function.
4602
4603 IT->add_overlay_start contains an additional overlay start
4604 position to consider for taking overlay strings from, if non-zero.
4605 This position comes into play when the overlay has an `invisible'
4606 property, and both before and after-strings. When we've skipped to
4607 the end of the overlay, because of its `invisible' property, we
4608 nevertheless want its before-string to appear.
4609 IT->add_overlay_start will contain the overlay start position
4610 in this case.
4611
4612 Overlay strings are sorted so that after-string strings come in
4613 front of before-string strings. Within before and after-strings,
4614 strings are sorted by overlay priority. See also function
4615 compare_overlay_entries. */
4616
4617 static void
4618 load_overlay_strings (it, charpos)
4619 struct it *it;
4620 int charpos;
4621 {
4622 extern Lisp_Object Qafter_string, Qbefore_string, Qwindow, Qpriority;
4623 Lisp_Object overlay, window, str, invisible;
4624 struct Lisp_Overlay *ov;
4625 int start, end;
4626 int size = 20;
4627 int n = 0, i, j, invis_p;
4628 struct overlay_entry *entries
4629 = (struct overlay_entry *) alloca (size * sizeof *entries);
4630
4631 if (charpos <= 0)
4632 charpos = IT_CHARPOS (*it);
4633
4634 /* Append the overlay string STRING of overlay OVERLAY to vector
4635 `entries' which has size `size' and currently contains `n'
4636 elements. AFTER_P non-zero means STRING is an after-string of
4637 OVERLAY. */
4638 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
4639 do \
4640 { \
4641 Lisp_Object priority; \
4642 \
4643 if (n == size) \
4644 { \
4645 int new_size = 2 * size; \
4646 struct overlay_entry *old = entries; \
4647 entries = \
4648 (struct overlay_entry *) alloca (new_size \
4649 * sizeof *entries); \
4650 bcopy (old, entries, size * sizeof *entries); \
4651 size = new_size; \
4652 } \
4653 \
4654 entries[n].string = (STRING); \
4655 entries[n].overlay = (OVERLAY); \
4656 priority = Foverlay_get ((OVERLAY), Qpriority); \
4657 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
4658 entries[n].after_string_p = (AFTER_P); \
4659 ++n; \
4660 } \
4661 while (0)
4662
4663 /* Process overlay before the overlay center. */
4664 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
4665 {
4666 XSETMISC (overlay, ov);
4667 xassert (OVERLAYP (overlay));
4668 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4669 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4670
4671 if (end < charpos)
4672 break;
4673
4674 /* Skip this overlay if it doesn't start or end at IT's current
4675 position. */
4676 if (end != charpos && start != charpos)
4677 continue;
4678
4679 /* Skip this overlay if it doesn't apply to IT->w. */
4680 window = Foverlay_get (overlay, Qwindow);
4681 if (WINDOWP (window) && XWINDOW (window) != it->w)
4682 continue;
4683
4684 /* If the text ``under'' the overlay is invisible, both before-
4685 and after-strings from this overlay are visible; start and
4686 end position are indistinguishable. */
4687 invisible = Foverlay_get (overlay, Qinvisible);
4688 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4689
4690 /* If overlay has a non-empty before-string, record it. */
4691 if ((start == charpos || (end == charpos && invis_p))
4692 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
4693 && SCHARS (str))
4694 RECORD_OVERLAY_STRING (overlay, str, 0);
4695
4696 /* If overlay has a non-empty after-string, record it. */
4697 if ((end == charpos || (start == charpos && invis_p))
4698 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
4699 && SCHARS (str))
4700 RECORD_OVERLAY_STRING (overlay, str, 1);
4701 }
4702
4703 /* Process overlays after the overlay center. */
4704 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
4705 {
4706 XSETMISC (overlay, ov);
4707 xassert (OVERLAYP (overlay));
4708 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4709 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4710
4711 if (start > charpos)
4712 break;
4713
4714 /* Skip this overlay if it doesn't start or end at IT's current
4715 position. */
4716 if (end != charpos && start != charpos)
4717 continue;
4718
4719 /* Skip this overlay if it doesn't apply to IT->w. */
4720 window = Foverlay_get (overlay, Qwindow);
4721 if (WINDOWP (window) && XWINDOW (window) != it->w)
4722 continue;
4723
4724 /* If the text ``under'' the overlay is invisible, it has a zero
4725 dimension, and both before- and after-strings apply. */
4726 invisible = Foverlay_get (overlay, Qinvisible);
4727 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4728
4729 /* If overlay has a non-empty before-string, record it. */
4730 if ((start == charpos || (end == charpos && invis_p))
4731 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
4732 && SCHARS (str))
4733 RECORD_OVERLAY_STRING (overlay, str, 0);
4734
4735 /* If overlay has a non-empty after-string, record it. */
4736 if ((end == charpos || (start == charpos && invis_p))
4737 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
4738 && SCHARS (str))
4739 RECORD_OVERLAY_STRING (overlay, str, 1);
4740 }
4741
4742 #undef RECORD_OVERLAY_STRING
4743
4744 /* Sort entries. */
4745 if (n > 1)
4746 qsort (entries, n, sizeof *entries, compare_overlay_entries);
4747
4748 /* Record the total number of strings to process. */
4749 it->n_overlay_strings = n;
4750
4751 /* IT->current.overlay_string_index is the number of overlay strings
4752 that have already been consumed by IT. Copy some of the
4753 remaining overlay strings to IT->overlay_strings. */
4754 i = 0;
4755 j = it->current.overlay_string_index;
4756 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
4757 it->overlay_strings[i++] = entries[j++].string;
4758
4759 CHECK_IT (it);
4760 }
4761
4762
4763 /* Get the first chunk of overlay strings at IT's current buffer
4764 position, or at CHARPOS if that is > 0. Value is non-zero if at
4765 least one overlay string was found. */
4766
4767 static int
4768 get_overlay_strings (it, charpos)
4769 struct it *it;
4770 int charpos;
4771 {
4772 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
4773 process. This fills IT->overlay_strings with strings, and sets
4774 IT->n_overlay_strings to the total number of strings to process.
4775 IT->pos.overlay_string_index has to be set temporarily to zero
4776 because load_overlay_strings needs this; it must be set to -1
4777 when no overlay strings are found because a zero value would
4778 indicate a position in the first overlay string. */
4779 it->current.overlay_string_index = 0;
4780 load_overlay_strings (it, charpos);
4781
4782 /* If we found overlay strings, set up IT to deliver display
4783 elements from the first one. Otherwise set up IT to deliver
4784 from current_buffer. */
4785 if (it->n_overlay_strings)
4786 {
4787 /* Make sure we know settings in current_buffer, so that we can
4788 restore meaningful values when we're done with the overlay
4789 strings. */
4790 compute_stop_pos (it);
4791 xassert (it->face_id >= 0);
4792
4793 /* Save IT's settings. They are restored after all overlay
4794 strings have been processed. */
4795 xassert (it->sp == 0);
4796 push_it (it);
4797
4798 /* Set up IT to deliver display elements from the first overlay
4799 string. */
4800 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
4801 it->string = it->overlay_strings[0];
4802 it->stop_charpos = 0;
4803 xassert (STRINGP (it->string));
4804 it->end_charpos = SCHARS (it->string);
4805 it->multibyte_p = STRING_MULTIBYTE (it->string);
4806 it->method = GET_FROM_STRING;
4807 }
4808 else
4809 {
4810 it->string = Qnil;
4811 it->current.overlay_string_index = -1;
4812 it->method = GET_FROM_BUFFER;
4813 }
4814
4815 CHECK_IT (it);
4816
4817 /* Value is non-zero if we found at least one overlay string. */
4818 return STRINGP (it->string);
4819 }
4820
4821
4822 \f
4823 /***********************************************************************
4824 Saving and restoring state
4825 ***********************************************************************/
4826
4827 /* Save current settings of IT on IT->stack. Called, for example,
4828 before setting up IT for an overlay string, to be able to restore
4829 IT's settings to what they were after the overlay string has been
4830 processed. */
4831
4832 static void
4833 push_it (it)
4834 struct it *it;
4835 {
4836 struct iterator_stack_entry *p;
4837
4838 xassert (it->sp < 2);
4839 p = it->stack + it->sp;
4840
4841 p->stop_charpos = it->stop_charpos;
4842 xassert (it->face_id >= 0);
4843 p->face_id = it->face_id;
4844 p->string = it->string;
4845 p->pos = it->current;
4846 p->end_charpos = it->end_charpos;
4847 p->string_nchars = it->string_nchars;
4848 p->area = it->area;
4849 p->multibyte_p = it->multibyte_p;
4850 p->slice = it->slice;
4851 p->space_width = it->space_width;
4852 p->font_height = it->font_height;
4853 p->voffset = it->voffset;
4854 p->string_from_display_prop_p = it->string_from_display_prop_p;
4855 p->display_ellipsis_p = 0;
4856 ++it->sp;
4857 }
4858
4859
4860 /* Restore IT's settings from IT->stack. Called, for example, when no
4861 more overlay strings must be processed, and we return to delivering
4862 display elements from a buffer, or when the end of a string from a
4863 `display' property is reached and we return to delivering display
4864 elements from an overlay string, or from a buffer. */
4865
4866 static void
4867 pop_it (it)
4868 struct it *it;
4869 {
4870 struct iterator_stack_entry *p;
4871
4872 xassert (it->sp > 0);
4873 --it->sp;
4874 p = it->stack + it->sp;
4875 it->stop_charpos = p->stop_charpos;
4876 it->face_id = p->face_id;
4877 it->string = p->string;
4878 it->current = p->pos;
4879 it->end_charpos = p->end_charpos;
4880 it->string_nchars = p->string_nchars;
4881 it->area = p->area;
4882 it->multibyte_p = p->multibyte_p;
4883 it->slice = p->slice;
4884 it->space_width = p->space_width;
4885 it->font_height = p->font_height;
4886 it->voffset = p->voffset;
4887 it->string_from_display_prop_p = p->string_from_display_prop_p;
4888 }
4889
4890
4891 \f
4892 /***********************************************************************
4893 Moving over lines
4894 ***********************************************************************/
4895
4896 /* Set IT's current position to the previous line start. */
4897
4898 static void
4899 back_to_previous_line_start (it)
4900 struct it *it;
4901 {
4902 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
4903 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
4904 }
4905
4906
4907 /* Move IT to the next line start.
4908
4909 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
4910 we skipped over part of the text (as opposed to moving the iterator
4911 continuously over the text). Otherwise, don't change the value
4912 of *SKIPPED_P.
4913
4914 Newlines may come from buffer text, overlay strings, or strings
4915 displayed via the `display' property. That's the reason we can't
4916 simply use find_next_newline_no_quit.
4917
4918 Note that this function may not skip over invisible text that is so
4919 because of text properties and immediately follows a newline. If
4920 it would, function reseat_at_next_visible_line_start, when called
4921 from set_iterator_to_next, would effectively make invisible
4922 characters following a newline part of the wrong glyph row, which
4923 leads to wrong cursor motion. */
4924
4925 static int
4926 forward_to_next_line_start (it, skipped_p)
4927 struct it *it;
4928 int *skipped_p;
4929 {
4930 int old_selective, newline_found_p, n;
4931 const int MAX_NEWLINE_DISTANCE = 500;
4932
4933 /* If already on a newline, just consume it to avoid unintended
4934 skipping over invisible text below. */
4935 if (it->what == IT_CHARACTER
4936 && it->c == '\n'
4937 && CHARPOS (it->position) == IT_CHARPOS (*it))
4938 {
4939 set_iterator_to_next (it, 0);
4940 it->c = 0;
4941 return 1;
4942 }
4943
4944 /* Don't handle selective display in the following. It's (a)
4945 unnecessary because it's done by the caller, and (b) leads to an
4946 infinite recursion because next_element_from_ellipsis indirectly
4947 calls this function. */
4948 old_selective = it->selective;
4949 it->selective = 0;
4950
4951 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
4952 from buffer text. */
4953 for (n = newline_found_p = 0;
4954 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
4955 n += STRINGP (it->string) ? 0 : 1)
4956 {
4957 if (!get_next_display_element (it))
4958 return 0;
4959 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
4960 set_iterator_to_next (it, 0);
4961 }
4962
4963 /* If we didn't find a newline near enough, see if we can use a
4964 short-cut. */
4965 if (!newline_found_p)
4966 {
4967 int start = IT_CHARPOS (*it);
4968 int limit = find_next_newline_no_quit (start, 1);
4969 Lisp_Object pos;
4970
4971 xassert (!STRINGP (it->string));
4972
4973 /* If there isn't any `display' property in sight, and no
4974 overlays, we can just use the position of the newline in
4975 buffer text. */
4976 if (it->stop_charpos >= limit
4977 || ((pos = Fnext_single_property_change (make_number (start),
4978 Qdisplay,
4979 Qnil, make_number (limit)),
4980 NILP (pos))
4981 && next_overlay_change (start) == ZV))
4982 {
4983 IT_CHARPOS (*it) = limit;
4984 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
4985 *skipped_p = newline_found_p = 1;
4986 }
4987 else
4988 {
4989 while (get_next_display_element (it)
4990 && !newline_found_p)
4991 {
4992 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
4993 set_iterator_to_next (it, 0);
4994 }
4995 }
4996 }
4997
4998 it->selective = old_selective;
4999 return newline_found_p;
5000 }
5001
5002
5003 /* Set IT's current position to the previous visible line start. Skip
5004 invisible text that is so either due to text properties or due to
5005 selective display. Caution: this does not change IT->current_x and
5006 IT->hpos. */
5007
5008 static void
5009 back_to_previous_visible_line_start (it)
5010 struct it *it;
5011 {
5012 while (IT_CHARPOS (*it) > BEGV)
5013 {
5014 back_to_previous_line_start (it);
5015 if (IT_CHARPOS (*it) <= BEGV)
5016 break;
5017
5018 /* If selective > 0, then lines indented more than that values
5019 are invisible. */
5020 if (it->selective > 0
5021 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
5022 (double) it->selective)) /* iftc */
5023 continue;
5024
5025 /* Check the newline before point for invisibility. */
5026 {
5027 Lisp_Object prop;
5028 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
5029 Qinvisible, it->window);
5030 if (TEXT_PROP_MEANS_INVISIBLE (prop))
5031 continue;
5032 }
5033
5034 /* If newline has a display property that replaces the newline with something
5035 else (image or text), find start of overlay or interval and continue search
5036 from that point. */
5037 if (IT_CHARPOS (*it) > BEGV)
5038 {
5039 struct it it2 = *it;
5040 int pos;
5041 int beg, end;
5042 Lisp_Object val, overlay;
5043
5044 pos = --IT_CHARPOS (it2);
5045 --IT_BYTEPOS (it2);
5046 it2.sp = 0;
5047 if (handle_display_prop (&it2) == HANDLED_RETURN
5048 && !NILP (val = get_char_property_and_overlay
5049 (make_number (pos), Qdisplay, Qnil, &overlay))
5050 && (OVERLAYP (overlay)
5051 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
5052 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
5053 {
5054 if (beg < BEGV)
5055 beg = BEGV;
5056 IT_CHARPOS (*it) = beg;
5057 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
5058 continue;
5059 }
5060 }
5061
5062 break;
5063 }
5064
5065 xassert (IT_CHARPOS (*it) >= BEGV);
5066 xassert (IT_CHARPOS (*it) == BEGV
5067 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
5068 CHECK_IT (it);
5069 }
5070
5071
5072 /* Reseat iterator IT at the previous visible line start. Skip
5073 invisible text that is so either due to text properties or due to
5074 selective display. At the end, update IT's overlay information,
5075 face information etc. */
5076
5077 void
5078 reseat_at_previous_visible_line_start (it)
5079 struct it *it;
5080 {
5081 back_to_previous_visible_line_start (it);
5082 reseat (it, it->current.pos, 1);
5083 CHECK_IT (it);
5084 }
5085
5086
5087 /* Reseat iterator IT on the next visible line start in the current
5088 buffer. ON_NEWLINE_P non-zero means position IT on the newline
5089 preceding the line start. Skip over invisible text that is so
5090 because of selective display. Compute faces, overlays etc at the
5091 new position. Note that this function does not skip over text that
5092 is invisible because of text properties. */
5093
5094 static void
5095 reseat_at_next_visible_line_start (it, on_newline_p)
5096 struct it *it;
5097 int on_newline_p;
5098 {
5099 int newline_found_p, skipped_p = 0;
5100
5101 newline_found_p = forward_to_next_line_start (it, &skipped_p);
5102
5103 /* Skip over lines that are invisible because they are indented
5104 more than the value of IT->selective. */
5105 if (it->selective > 0)
5106 while (IT_CHARPOS (*it) < ZV
5107 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
5108 (double) it->selective)) /* iftc */
5109 {
5110 xassert (IT_BYTEPOS (*it) == BEGV
5111 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
5112 newline_found_p = forward_to_next_line_start (it, &skipped_p);
5113 }
5114
5115 /* Position on the newline if that's what's requested. */
5116 if (on_newline_p && newline_found_p)
5117 {
5118 if (STRINGP (it->string))
5119 {
5120 if (IT_STRING_CHARPOS (*it) > 0)
5121 {
5122 --IT_STRING_CHARPOS (*it);
5123 --IT_STRING_BYTEPOS (*it);
5124 }
5125 }
5126 else if (IT_CHARPOS (*it) > BEGV)
5127 {
5128 --IT_CHARPOS (*it);
5129 --IT_BYTEPOS (*it);
5130 reseat (it, it->current.pos, 0);
5131 }
5132 }
5133 else if (skipped_p)
5134 reseat (it, it->current.pos, 0);
5135
5136 CHECK_IT (it);
5137 }
5138
5139
5140 \f
5141 /***********************************************************************
5142 Changing an iterator's position
5143 ***********************************************************************/
5144
5145 /* Change IT's current position to POS in current_buffer. If FORCE_P
5146 is non-zero, always check for text properties at the new position.
5147 Otherwise, text properties are only looked up if POS >=
5148 IT->check_charpos of a property. */
5149
5150 static void
5151 reseat (it, pos, force_p)
5152 struct it *it;
5153 struct text_pos pos;
5154 int force_p;
5155 {
5156 int original_pos = IT_CHARPOS (*it);
5157
5158 reseat_1 (it, pos, 0);
5159
5160 /* Determine where to check text properties. Avoid doing it
5161 where possible because text property lookup is very expensive. */
5162 if (force_p
5163 || CHARPOS (pos) > it->stop_charpos
5164 || CHARPOS (pos) < original_pos)
5165 handle_stop (it);
5166
5167 CHECK_IT (it);
5168 }
5169
5170
5171 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
5172 IT->stop_pos to POS, also. */
5173
5174 static void
5175 reseat_1 (it, pos, set_stop_p)
5176 struct it *it;
5177 struct text_pos pos;
5178 int set_stop_p;
5179 {
5180 /* Don't call this function when scanning a C string. */
5181 xassert (it->s == NULL);
5182
5183 /* POS must be a reasonable value. */
5184 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
5185
5186 it->current.pos = it->position = pos;
5187 XSETBUFFER (it->object, current_buffer);
5188 it->end_charpos = ZV;
5189 it->dpvec = NULL;
5190 it->current.dpvec_index = -1;
5191 it->current.overlay_string_index = -1;
5192 IT_STRING_CHARPOS (*it) = -1;
5193 IT_STRING_BYTEPOS (*it) = -1;
5194 it->string = Qnil;
5195 it->method = GET_FROM_BUFFER;
5196 /* RMS: I added this to fix a bug in move_it_vertically_backward
5197 where it->area continued to relate to the starting point
5198 for the backward motion. Bug report from
5199 Nick Roberts <nick@nick.uklinux.net> on 19 May 2003.
5200 However, I am not sure whether reseat still does the right thing
5201 in general after this change. */
5202 it->area = TEXT_AREA;
5203 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
5204 it->sp = 0;
5205 it->face_before_selective_p = 0;
5206
5207 if (set_stop_p)
5208 it->stop_charpos = CHARPOS (pos);
5209 }
5210
5211
5212 /* Set up IT for displaying a string, starting at CHARPOS in window W.
5213 If S is non-null, it is a C string to iterate over. Otherwise,
5214 STRING gives a Lisp string to iterate over.
5215
5216 If PRECISION > 0, don't return more then PRECISION number of
5217 characters from the string.
5218
5219 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
5220 characters have been returned. FIELD_WIDTH < 0 means an infinite
5221 field width.
5222
5223 MULTIBYTE = 0 means disable processing of multibyte characters,
5224 MULTIBYTE > 0 means enable it,
5225 MULTIBYTE < 0 means use IT->multibyte_p.
5226
5227 IT must be initialized via a prior call to init_iterator before
5228 calling this function. */
5229
5230 static void
5231 reseat_to_string (it, s, string, charpos, precision, field_width, multibyte)
5232 struct it *it;
5233 unsigned char *s;
5234 Lisp_Object string;
5235 int charpos;
5236 int precision, field_width, multibyte;
5237 {
5238 /* No region in strings. */
5239 it->region_beg_charpos = it->region_end_charpos = -1;
5240
5241 /* No text property checks performed by default, but see below. */
5242 it->stop_charpos = -1;
5243
5244 /* Set iterator position and end position. */
5245 bzero (&it->current, sizeof it->current);
5246 it->current.overlay_string_index = -1;
5247 it->current.dpvec_index = -1;
5248 xassert (charpos >= 0);
5249
5250 /* If STRING is specified, use its multibyteness, otherwise use the
5251 setting of MULTIBYTE, if specified. */
5252 if (multibyte >= 0)
5253 it->multibyte_p = multibyte > 0;
5254
5255 if (s == NULL)
5256 {
5257 xassert (STRINGP (string));
5258 it->string = string;
5259 it->s = NULL;
5260 it->end_charpos = it->string_nchars = SCHARS (string);
5261 it->method = GET_FROM_STRING;
5262 it->current.string_pos = string_pos (charpos, string);
5263 }
5264 else
5265 {
5266 it->s = s;
5267 it->string = Qnil;
5268
5269 /* Note that we use IT->current.pos, not it->current.string_pos,
5270 for displaying C strings. */
5271 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
5272 if (it->multibyte_p)
5273 {
5274 it->current.pos = c_string_pos (charpos, s, 1);
5275 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
5276 }
5277 else
5278 {
5279 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
5280 it->end_charpos = it->string_nchars = strlen (s);
5281 }
5282
5283 it->method = GET_FROM_C_STRING;
5284 }
5285
5286 /* PRECISION > 0 means don't return more than PRECISION characters
5287 from the string. */
5288 if (precision > 0 && it->end_charpos - charpos > precision)
5289 it->end_charpos = it->string_nchars = charpos + precision;
5290
5291 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
5292 characters have been returned. FIELD_WIDTH == 0 means don't pad,
5293 FIELD_WIDTH < 0 means infinite field width. This is useful for
5294 padding with `-' at the end of a mode line. */
5295 if (field_width < 0)
5296 field_width = INFINITY;
5297 if (field_width > it->end_charpos - charpos)
5298 it->end_charpos = charpos + field_width;
5299
5300 /* Use the standard display table for displaying strings. */
5301 if (DISP_TABLE_P (Vstandard_display_table))
5302 it->dp = XCHAR_TABLE (Vstandard_display_table);
5303
5304 it->stop_charpos = charpos;
5305 CHECK_IT (it);
5306 }
5307
5308
5309 \f
5310 /***********************************************************************
5311 Iteration
5312 ***********************************************************************/
5313
5314 /* Map enum it_method value to corresponding next_element_from_* function. */
5315
5316 static int (* get_next_element[NUM_IT_METHODS]) P_ ((struct it *it)) =
5317 {
5318 next_element_from_buffer,
5319 next_element_from_display_vector,
5320 next_element_from_composition,
5321 next_element_from_string,
5322 next_element_from_c_string,
5323 next_element_from_image,
5324 next_element_from_stretch
5325 };
5326
5327
5328 /* Load IT's display element fields with information about the next
5329 display element from the current position of IT. Value is zero if
5330 end of buffer (or C string) is reached. */
5331
5332 static struct frame *last_escape_glyph_frame = NULL;
5333 static unsigned last_escape_glyph_face_id = (1 << FACE_ID_BITS);
5334 static int last_escape_glyph_merged_face_id = 0;
5335
5336 int
5337 get_next_display_element (it)
5338 struct it *it;
5339 {
5340 /* Non-zero means that we found a display element. Zero means that
5341 we hit the end of what we iterate over. Performance note: the
5342 function pointer `method' used here turns out to be faster than
5343 using a sequence of if-statements. */
5344 int success_p;
5345
5346 get_next:
5347 success_p = (*get_next_element[it->method]) (it);
5348
5349 if (it->what == IT_CHARACTER)
5350 {
5351 /* Map via display table or translate control characters.
5352 IT->c, IT->len etc. have been set to the next character by
5353 the function call above. If we have a display table, and it
5354 contains an entry for IT->c, translate it. Don't do this if
5355 IT->c itself comes from a display table, otherwise we could
5356 end up in an infinite recursion. (An alternative could be to
5357 count the recursion depth of this function and signal an
5358 error when a certain maximum depth is reached.) Is it worth
5359 it? */
5360 if (success_p && it->dpvec == NULL)
5361 {
5362 Lisp_Object dv;
5363
5364 if (it->dp
5365 && (dv = DISP_CHAR_VECTOR (it->dp, it->c),
5366 VECTORP (dv)))
5367 {
5368 struct Lisp_Vector *v = XVECTOR (dv);
5369
5370 /* Return the first character from the display table
5371 entry, if not empty. If empty, don't display the
5372 current character. */
5373 if (v->size)
5374 {
5375 it->dpvec_char_len = it->len;
5376 it->dpvec = v->contents;
5377 it->dpend = v->contents + v->size;
5378 it->current.dpvec_index = 0;
5379 it->dpvec_face_id = -1;
5380 it->saved_face_id = it->face_id;
5381 it->method = GET_FROM_DISPLAY_VECTOR;
5382 it->ellipsis_p = 0;
5383 }
5384 else
5385 {
5386 set_iterator_to_next (it, 0);
5387 }
5388 goto get_next;
5389 }
5390
5391 /* Translate control characters into `\003' or `^C' form.
5392 Control characters coming from a display table entry are
5393 currently not translated because we use IT->dpvec to hold
5394 the translation. This could easily be changed but I
5395 don't believe that it is worth doing.
5396
5397 If it->multibyte_p is nonzero, eight-bit characters and
5398 non-printable multibyte characters are also translated to
5399 octal form.
5400
5401 If it->multibyte_p is zero, eight-bit characters that
5402 don't have corresponding multibyte char code are also
5403 translated to octal form. */
5404 else if ((it->c < ' '
5405 && (it->area != TEXT_AREA
5406 /* In mode line, treat \n like other crl chars. */
5407 || (it->c != '\t'
5408 && it->glyph_row && it->glyph_row->mode_line_p)
5409 || (it->c != '\n' && it->c != '\t')))
5410 || (it->multibyte_p
5411 ? ((it->c >= 127
5412 && it->len == 1)
5413 || !CHAR_PRINTABLE_P (it->c)
5414 || (!NILP (Vnobreak_char_display)
5415 && (it->c == 0x8a0 || it->c == 0x8ad
5416 || it->c == 0x920 || it->c == 0x92d
5417 || it->c == 0xe20 || it->c == 0xe2d
5418 || it->c == 0xf20 || it->c == 0xf2d)))
5419 : (it->c >= 127
5420 && (!unibyte_display_via_language_environment
5421 || it->c == unibyte_char_to_multibyte (it->c)))))
5422 {
5423 /* IT->c is a control character which must be displayed
5424 either as '\003' or as `^C' where the '\\' and '^'
5425 can be defined in the display table. Fill
5426 IT->ctl_chars with glyphs for what we have to
5427 display. Then, set IT->dpvec to these glyphs. */
5428 GLYPH g;
5429 int ctl_len;
5430 int face_id, lface_id = 0 ;
5431 GLYPH escape_glyph;
5432
5433 /* Handle control characters with ^. */
5434
5435 if (it->c < 128 && it->ctl_arrow_p)
5436 {
5437 g = '^'; /* default glyph for Control */
5438 /* Set IT->ctl_chars[0] to the glyph for `^'. */
5439 if (it->dp
5440 && INTEGERP (DISP_CTRL_GLYPH (it->dp))
5441 && GLYPH_CHAR_VALID_P (XINT (DISP_CTRL_GLYPH (it->dp))))
5442 {
5443 g = XINT (DISP_CTRL_GLYPH (it->dp));
5444 lface_id = FAST_GLYPH_FACE (g);
5445 }
5446 if (lface_id)
5447 {
5448 g = FAST_GLYPH_CHAR (g);
5449 face_id = merge_faces (it->f, Qt, lface_id,
5450 it->face_id);
5451 }
5452 else if (it->f == last_escape_glyph_frame
5453 && it->face_id == last_escape_glyph_face_id)
5454 {
5455 face_id = last_escape_glyph_merged_face_id;
5456 }
5457 else
5458 {
5459 /* Merge the escape-glyph face into the current face. */
5460 face_id = merge_faces (it->f, Qescape_glyph, 0,
5461 it->face_id);
5462 last_escape_glyph_frame = it->f;
5463 last_escape_glyph_face_id = it->face_id;
5464 last_escape_glyph_merged_face_id = face_id;
5465 }
5466
5467 XSETINT (it->ctl_chars[0], g);
5468 g = it->c ^ 0100;
5469 XSETINT (it->ctl_chars[1], g);
5470 ctl_len = 2;
5471 goto display_control;
5472 }
5473
5474 /* Handle non-break space in the mode where it only gets
5475 highlighting. */
5476
5477 if (EQ (Vnobreak_char_display, Qt)
5478 && (it->c == 0x8a0 || it->c == 0x920
5479 || it->c == 0xe20 || it->c == 0xf20))
5480 {
5481 /* Merge the no-break-space face into the current face. */
5482 face_id = merge_faces (it->f, Qnobreak_space, 0,
5483 it->face_id);
5484
5485 g = it->c = ' ';
5486 XSETINT (it->ctl_chars[0], g);
5487 ctl_len = 1;
5488 goto display_control;
5489 }
5490
5491 /* Handle sequences that start with the "escape glyph". */
5492
5493 /* the default escape glyph is \. */
5494 escape_glyph = '\\';
5495
5496 if (it->dp
5497 && INTEGERP (DISP_ESCAPE_GLYPH (it->dp))
5498 && GLYPH_CHAR_VALID_P (XFASTINT (DISP_ESCAPE_GLYPH (it->dp))))
5499 {
5500 escape_glyph = XFASTINT (DISP_ESCAPE_GLYPH (it->dp));
5501 lface_id = FAST_GLYPH_FACE (escape_glyph);
5502 }
5503 if (lface_id)
5504 {
5505 /* The display table specified a face.
5506 Merge it into face_id and also into escape_glyph. */
5507 escape_glyph = FAST_GLYPH_CHAR (escape_glyph);
5508 face_id = merge_faces (it->f, Qt, lface_id,
5509 it->face_id);
5510 }
5511 else if (it->f == last_escape_glyph_frame
5512 && it->face_id == last_escape_glyph_face_id)
5513 {
5514 face_id = last_escape_glyph_merged_face_id;
5515 }
5516 else
5517 {
5518 /* Merge the escape-glyph face into the current face. */
5519 face_id = merge_faces (it->f, Qescape_glyph, 0,
5520 it->face_id);
5521 last_escape_glyph_frame = it->f;
5522 last_escape_glyph_face_id = it->face_id;
5523 last_escape_glyph_merged_face_id = face_id;
5524 }
5525
5526 /* Handle soft hyphens in the mode where they only get
5527 highlighting. */
5528
5529 if (EQ (Vnobreak_char_display, Qt)
5530 && (it->c == 0x8ad || it->c == 0x92d
5531 || it->c == 0xe2d || it->c == 0xf2d))
5532 {
5533 g = it->c = '-';
5534 XSETINT (it->ctl_chars[0], g);
5535 ctl_len = 1;
5536 goto display_control;
5537 }
5538
5539 /* Handle non-break space and soft hyphen
5540 with the escape glyph. */
5541
5542 if (it->c == 0x8a0 || it->c == 0x8ad
5543 || it->c == 0x920 || it->c == 0x92d
5544 || it->c == 0xe20 || it->c == 0xe2d
5545 || it->c == 0xf20 || it->c == 0xf2d)
5546 {
5547 XSETINT (it->ctl_chars[0], escape_glyph);
5548 g = it->c = ((it->c & 0xf) == 0 ? ' ' : '-');
5549 XSETINT (it->ctl_chars[1], g);
5550 ctl_len = 2;
5551 goto display_control;
5552 }
5553
5554 {
5555 unsigned char str[MAX_MULTIBYTE_LENGTH];
5556 int len;
5557 int i;
5558
5559 /* Set IT->ctl_chars[0] to the glyph for `\\'. */
5560 if (SINGLE_BYTE_CHAR_P (it->c))
5561 str[0] = it->c, len = 1;
5562 else
5563 {
5564 len = CHAR_STRING_NO_SIGNAL (it->c, str);
5565 if (len < 0)
5566 {
5567 /* It's an invalid character, which shouldn't
5568 happen actually, but due to bugs it may
5569 happen. Let's print the char as is, there's
5570 not much meaningful we can do with it. */
5571 str[0] = it->c;
5572 str[1] = it->c >> 8;
5573 str[2] = it->c >> 16;
5574 str[3] = it->c >> 24;
5575 len = 4;
5576 }
5577 }
5578
5579 for (i = 0; i < len; i++)
5580 {
5581 XSETINT (it->ctl_chars[i * 4], escape_glyph);
5582 /* Insert three more glyphs into IT->ctl_chars for
5583 the octal display of the character. */
5584 g = ((str[i] >> 6) & 7) + '0';
5585 XSETINT (it->ctl_chars[i * 4 + 1], g);
5586 g = ((str[i] >> 3) & 7) + '0';
5587 XSETINT (it->ctl_chars[i * 4 + 2], g);
5588 g = (str[i] & 7) + '0';
5589 XSETINT (it->ctl_chars[i * 4 + 3], g);
5590 }
5591 ctl_len = len * 4;
5592 }
5593
5594 display_control:
5595 /* Set up IT->dpvec and return first character from it. */
5596 it->dpvec_char_len = it->len;
5597 it->dpvec = it->ctl_chars;
5598 it->dpend = it->dpvec + ctl_len;
5599 it->current.dpvec_index = 0;
5600 it->dpvec_face_id = face_id;
5601 it->saved_face_id = it->face_id;
5602 it->method = GET_FROM_DISPLAY_VECTOR;
5603 it->ellipsis_p = 0;
5604 goto get_next;
5605 }
5606 }
5607
5608 /* Adjust face id for a multibyte character. There are no
5609 multibyte character in unibyte text. */
5610 if (it->multibyte_p
5611 && success_p
5612 && FRAME_WINDOW_P (it->f))
5613 {
5614 struct face *face = FACE_FROM_ID (it->f, it->face_id);
5615 it->face_id = FACE_FOR_CHAR (it->f, face, it->c);
5616 }
5617 }
5618
5619 /* Is this character the last one of a run of characters with
5620 box? If yes, set IT->end_of_box_run_p to 1. */
5621 if (it->face_box_p
5622 && it->s == NULL)
5623 {
5624 int face_id;
5625 struct face *face;
5626
5627 it->end_of_box_run_p
5628 = ((face_id = face_after_it_pos (it),
5629 face_id != it->face_id)
5630 && (face = FACE_FROM_ID (it->f, face_id),
5631 face->box == FACE_NO_BOX));
5632 }
5633
5634 /* Value is 0 if end of buffer or string reached. */
5635 return success_p;
5636 }
5637
5638
5639 /* Move IT to the next display element.
5640
5641 RESEAT_P non-zero means if called on a newline in buffer text,
5642 skip to the next visible line start.
5643
5644 Functions get_next_display_element and set_iterator_to_next are
5645 separate because I find this arrangement easier to handle than a
5646 get_next_display_element function that also increments IT's
5647 position. The way it is we can first look at an iterator's current
5648 display element, decide whether it fits on a line, and if it does,
5649 increment the iterator position. The other way around we probably
5650 would either need a flag indicating whether the iterator has to be
5651 incremented the next time, or we would have to implement a
5652 decrement position function which would not be easy to write. */
5653
5654 void
5655 set_iterator_to_next (it, reseat_p)
5656 struct it *it;
5657 int reseat_p;
5658 {
5659 /* Reset flags indicating start and end of a sequence of characters
5660 with box. Reset them at the start of this function because
5661 moving the iterator to a new position might set them. */
5662 it->start_of_box_run_p = it->end_of_box_run_p = 0;
5663
5664 switch (it->method)
5665 {
5666 case GET_FROM_BUFFER:
5667 /* The current display element of IT is a character from
5668 current_buffer. Advance in the buffer, and maybe skip over
5669 invisible lines that are so because of selective display. */
5670 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
5671 reseat_at_next_visible_line_start (it, 0);
5672 else
5673 {
5674 xassert (it->len != 0);
5675 IT_BYTEPOS (*it) += it->len;
5676 IT_CHARPOS (*it) += 1;
5677 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
5678 }
5679 break;
5680
5681 case GET_FROM_COMPOSITION:
5682 xassert (it->cmp_id >= 0 && it->cmp_id < n_compositions);
5683 if (STRINGP (it->string))
5684 {
5685 IT_STRING_BYTEPOS (*it) += it->len;
5686 IT_STRING_CHARPOS (*it) += it->cmp_len;
5687 it->method = GET_FROM_STRING;
5688 goto consider_string_end;
5689 }
5690 else
5691 {
5692 IT_BYTEPOS (*it) += it->len;
5693 IT_CHARPOS (*it) += it->cmp_len;
5694 it->method = GET_FROM_BUFFER;
5695 }
5696 break;
5697
5698 case GET_FROM_C_STRING:
5699 /* Current display element of IT is from a C string. */
5700 IT_BYTEPOS (*it) += it->len;
5701 IT_CHARPOS (*it) += 1;
5702 break;
5703
5704 case GET_FROM_DISPLAY_VECTOR:
5705 /* Current display element of IT is from a display table entry.
5706 Advance in the display table definition. Reset it to null if
5707 end reached, and continue with characters from buffers/
5708 strings. */
5709 ++it->current.dpvec_index;
5710
5711 /* Restore face of the iterator to what they were before the
5712 display vector entry (these entries may contain faces). */
5713 it->face_id = it->saved_face_id;
5714
5715 if (it->dpvec + it->current.dpvec_index == it->dpend)
5716 {
5717 int recheck_faces = it->ellipsis_p;
5718
5719 if (it->s)
5720 it->method = GET_FROM_C_STRING;
5721 else if (STRINGP (it->string))
5722 it->method = GET_FROM_STRING;
5723 else
5724 it->method = GET_FROM_BUFFER;
5725
5726 it->dpvec = NULL;
5727 it->current.dpvec_index = -1;
5728
5729 /* Skip over characters which were displayed via IT->dpvec. */
5730 if (it->dpvec_char_len < 0)
5731 reseat_at_next_visible_line_start (it, 1);
5732 else if (it->dpvec_char_len > 0)
5733 {
5734 if (it->method == GET_FROM_STRING
5735 && it->n_overlay_strings > 0)
5736 it->ignore_overlay_strings_at_pos_p = 1;
5737 it->len = it->dpvec_char_len;
5738 set_iterator_to_next (it, reseat_p);
5739 }
5740
5741 /* Maybe recheck faces after display vector */
5742 if (recheck_faces)
5743 it->stop_charpos = IT_CHARPOS (*it);
5744 }
5745 break;
5746
5747 case GET_FROM_STRING:
5748 /* Current display element is a character from a Lisp string. */
5749 xassert (it->s == NULL && STRINGP (it->string));
5750 IT_STRING_BYTEPOS (*it) += it->len;
5751 IT_STRING_CHARPOS (*it) += 1;
5752
5753 consider_string_end:
5754
5755 if (it->current.overlay_string_index >= 0)
5756 {
5757 /* IT->string is an overlay string. Advance to the
5758 next, if there is one. */
5759 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
5760 next_overlay_string (it);
5761 }
5762 else
5763 {
5764 /* IT->string is not an overlay string. If we reached
5765 its end, and there is something on IT->stack, proceed
5766 with what is on the stack. This can be either another
5767 string, this time an overlay string, or a buffer. */
5768 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
5769 && it->sp > 0)
5770 {
5771 pop_it (it);
5772 if (STRINGP (it->string))
5773 goto consider_string_end;
5774 it->method = GET_FROM_BUFFER;
5775 }
5776 }
5777 break;
5778
5779 case GET_FROM_IMAGE:
5780 case GET_FROM_STRETCH:
5781 /* The position etc with which we have to proceed are on
5782 the stack. The position may be at the end of a string,
5783 if the `display' property takes up the whole string. */
5784 xassert (it->sp > 0);
5785 pop_it (it);
5786 it->image_id = 0;
5787 if (STRINGP (it->string))
5788 {
5789 it->method = GET_FROM_STRING;
5790 goto consider_string_end;
5791 }
5792 it->method = GET_FROM_BUFFER;
5793 break;
5794
5795 default:
5796 /* There are no other methods defined, so this should be a bug. */
5797 abort ();
5798 }
5799
5800 xassert (it->method != GET_FROM_STRING
5801 || (STRINGP (it->string)
5802 && IT_STRING_CHARPOS (*it) >= 0));
5803 }
5804
5805 /* Load IT's display element fields with information about the next
5806 display element which comes from a display table entry or from the
5807 result of translating a control character to one of the forms `^C'
5808 or `\003'.
5809
5810 IT->dpvec holds the glyphs to return as characters.
5811 IT->saved_face_id holds the face id before the display vector--
5812 it is restored into IT->face_idin set_iterator_to_next. */
5813
5814 static int
5815 next_element_from_display_vector (it)
5816 struct it *it;
5817 {
5818 /* Precondition. */
5819 xassert (it->dpvec && it->current.dpvec_index >= 0);
5820
5821 it->face_id = it->saved_face_id;
5822
5823 if (INTEGERP (*it->dpvec)
5824 && GLYPH_CHAR_VALID_P (XFASTINT (*it->dpvec)))
5825 {
5826 GLYPH g;
5827
5828 g = XFASTINT (it->dpvec[it->current.dpvec_index]);
5829 it->c = FAST_GLYPH_CHAR (g);
5830 it->len = CHAR_BYTES (it->c);
5831
5832 /* The entry may contain a face id to use. Such a face id is
5833 the id of a Lisp face, not a realized face. A face id of
5834 zero means no face is specified. */
5835 if (it->dpvec_face_id >= 0)
5836 it->face_id = it->dpvec_face_id;
5837 else
5838 {
5839 int lface_id = FAST_GLYPH_FACE (g);
5840 if (lface_id > 0)
5841 it->face_id = merge_faces (it->f, Qt, lface_id,
5842 it->saved_face_id);
5843 }
5844 }
5845 else
5846 /* Display table entry is invalid. Return a space. */
5847 it->c = ' ', it->len = 1;
5848
5849 /* Don't change position and object of the iterator here. They are
5850 still the values of the character that had this display table
5851 entry or was translated, and that's what we want. */
5852 it->what = IT_CHARACTER;
5853 return 1;
5854 }
5855
5856
5857 /* Load IT with the next display element from Lisp string IT->string.
5858 IT->current.string_pos is the current position within the string.
5859 If IT->current.overlay_string_index >= 0, the Lisp string is an
5860 overlay string. */
5861
5862 static int
5863 next_element_from_string (it)
5864 struct it *it;
5865 {
5866 struct text_pos position;
5867
5868 xassert (STRINGP (it->string));
5869 xassert (IT_STRING_CHARPOS (*it) >= 0);
5870 position = it->current.string_pos;
5871
5872 /* Time to check for invisible text? */
5873 if (IT_STRING_CHARPOS (*it) < it->end_charpos
5874 && IT_STRING_CHARPOS (*it) == it->stop_charpos)
5875 {
5876 handle_stop (it);
5877
5878 /* Since a handler may have changed IT->method, we must
5879 recurse here. */
5880 return get_next_display_element (it);
5881 }
5882
5883 if (it->current.overlay_string_index >= 0)
5884 {
5885 /* Get the next character from an overlay string. In overlay
5886 strings, There is no field width or padding with spaces to
5887 do. */
5888 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
5889 {
5890 it->what = IT_EOB;
5891 return 0;
5892 }
5893 else if (STRING_MULTIBYTE (it->string))
5894 {
5895 int remaining = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
5896 const unsigned char *s = (SDATA (it->string)
5897 + IT_STRING_BYTEPOS (*it));
5898 it->c = string_char_and_length (s, remaining, &it->len);
5899 }
5900 else
5901 {
5902 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
5903 it->len = 1;
5904 }
5905 }
5906 else
5907 {
5908 /* Get the next character from a Lisp string that is not an
5909 overlay string. Such strings come from the mode line, for
5910 example. We may have to pad with spaces, or truncate the
5911 string. See also next_element_from_c_string. */
5912 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
5913 {
5914 it->what = IT_EOB;
5915 return 0;
5916 }
5917 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
5918 {
5919 /* Pad with spaces. */
5920 it->c = ' ', it->len = 1;
5921 CHARPOS (position) = BYTEPOS (position) = -1;
5922 }
5923 else if (STRING_MULTIBYTE (it->string))
5924 {
5925 int maxlen = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
5926 const unsigned char *s = (SDATA (it->string)
5927 + IT_STRING_BYTEPOS (*it));
5928 it->c = string_char_and_length (s, maxlen, &it->len);
5929 }
5930 else
5931 {
5932 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
5933 it->len = 1;
5934 }
5935 }
5936
5937 /* Record what we have and where it came from. Note that we store a
5938 buffer position in IT->position although it could arguably be a
5939 string position. */
5940 it->what = IT_CHARACTER;
5941 it->object = it->string;
5942 it->position = position;
5943 return 1;
5944 }
5945
5946
5947 /* Load IT with next display element from C string IT->s.
5948 IT->string_nchars is the maximum number of characters to return
5949 from the string. IT->end_charpos may be greater than
5950 IT->string_nchars when this function is called, in which case we
5951 may have to return padding spaces. Value is zero if end of string
5952 reached, including padding spaces. */
5953
5954 static int
5955 next_element_from_c_string (it)
5956 struct it *it;
5957 {
5958 int success_p = 1;
5959
5960 xassert (it->s);
5961 it->what = IT_CHARACTER;
5962 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
5963 it->object = Qnil;
5964
5965 /* IT's position can be greater IT->string_nchars in case a field
5966 width or precision has been specified when the iterator was
5967 initialized. */
5968 if (IT_CHARPOS (*it) >= it->end_charpos)
5969 {
5970 /* End of the game. */
5971 it->what = IT_EOB;
5972 success_p = 0;
5973 }
5974 else if (IT_CHARPOS (*it) >= it->string_nchars)
5975 {
5976 /* Pad with spaces. */
5977 it->c = ' ', it->len = 1;
5978 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
5979 }
5980 else if (it->multibyte_p)
5981 {
5982 /* Implementation note: The calls to strlen apparently aren't a
5983 performance problem because there is no noticeable performance
5984 difference between Emacs running in unibyte or multibyte mode. */
5985 int maxlen = strlen (it->s) - IT_BYTEPOS (*it);
5986 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it),
5987 maxlen, &it->len);
5988 }
5989 else
5990 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
5991
5992 return success_p;
5993 }
5994
5995
5996 /* Set up IT to return characters from an ellipsis, if appropriate.
5997 The definition of the ellipsis glyphs may come from a display table
5998 entry. This function Fills IT with the first glyph from the
5999 ellipsis if an ellipsis is to be displayed. */
6000
6001 static int
6002 next_element_from_ellipsis (it)
6003 struct it *it;
6004 {
6005 if (it->selective_display_ellipsis_p)
6006 setup_for_ellipsis (it, it->len);
6007 else
6008 {
6009 /* The face at the current position may be different from the
6010 face we find after the invisible text. Remember what it
6011 was in IT->saved_face_id, and signal that it's there by
6012 setting face_before_selective_p. */
6013 it->saved_face_id = it->face_id;
6014 it->method = GET_FROM_BUFFER;
6015 reseat_at_next_visible_line_start (it, 1);
6016 it->face_before_selective_p = 1;
6017 }
6018
6019 return get_next_display_element (it);
6020 }
6021
6022
6023 /* Deliver an image display element. The iterator IT is already
6024 filled with image information (done in handle_display_prop). Value
6025 is always 1. */
6026
6027
6028 static int
6029 next_element_from_image (it)
6030 struct it *it;
6031 {
6032 it->what = IT_IMAGE;
6033 return 1;
6034 }
6035
6036
6037 /* Fill iterator IT with next display element from a stretch glyph
6038 property. IT->object is the value of the text property. Value is
6039 always 1. */
6040
6041 static int
6042 next_element_from_stretch (it)
6043 struct it *it;
6044 {
6045 it->what = IT_STRETCH;
6046 return 1;
6047 }
6048
6049
6050 /* Load IT with the next display element from current_buffer. Value
6051 is zero if end of buffer reached. IT->stop_charpos is the next
6052 position at which to stop and check for text properties or buffer
6053 end. */
6054
6055 static int
6056 next_element_from_buffer (it)
6057 struct it *it;
6058 {
6059 int success_p = 1;
6060
6061 /* Check this assumption, otherwise, we would never enter the
6062 if-statement, below. */
6063 xassert (IT_CHARPOS (*it) >= BEGV
6064 && IT_CHARPOS (*it) <= it->stop_charpos);
6065
6066 if (IT_CHARPOS (*it) >= it->stop_charpos)
6067 {
6068 if (IT_CHARPOS (*it) >= it->end_charpos)
6069 {
6070 int overlay_strings_follow_p;
6071
6072 /* End of the game, except when overlay strings follow that
6073 haven't been returned yet. */
6074 if (it->overlay_strings_at_end_processed_p)
6075 overlay_strings_follow_p = 0;
6076 else
6077 {
6078 it->overlay_strings_at_end_processed_p = 1;
6079 overlay_strings_follow_p = get_overlay_strings (it, 0);
6080 }
6081
6082 if (overlay_strings_follow_p)
6083 success_p = get_next_display_element (it);
6084 else
6085 {
6086 it->what = IT_EOB;
6087 it->position = it->current.pos;
6088 success_p = 0;
6089 }
6090 }
6091 else
6092 {
6093 handle_stop (it);
6094 return get_next_display_element (it);
6095 }
6096 }
6097 else
6098 {
6099 /* No face changes, overlays etc. in sight, so just return a
6100 character from current_buffer. */
6101 unsigned char *p;
6102
6103 /* Maybe run the redisplay end trigger hook. Performance note:
6104 This doesn't seem to cost measurable time. */
6105 if (it->redisplay_end_trigger_charpos
6106 && it->glyph_row
6107 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
6108 run_redisplay_end_trigger_hook (it);
6109
6110 /* Get the next character, maybe multibyte. */
6111 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
6112 if (it->multibyte_p && !ASCII_BYTE_P (*p))
6113 {
6114 int maxlen = ((IT_BYTEPOS (*it) >= GPT_BYTE ? ZV_BYTE : GPT_BYTE)
6115 - IT_BYTEPOS (*it));
6116 it->c = string_char_and_length (p, maxlen, &it->len);
6117 }
6118 else
6119 it->c = *p, it->len = 1;
6120
6121 /* Record what we have and where it came from. */
6122 it->what = IT_CHARACTER;;
6123 it->object = it->w->buffer;
6124 it->position = it->current.pos;
6125
6126 /* Normally we return the character found above, except when we
6127 really want to return an ellipsis for selective display. */
6128 if (it->selective)
6129 {
6130 if (it->c == '\n')
6131 {
6132 /* A value of selective > 0 means hide lines indented more
6133 than that number of columns. */
6134 if (it->selective > 0
6135 && IT_CHARPOS (*it) + 1 < ZV
6136 && indented_beyond_p (IT_CHARPOS (*it) + 1,
6137 IT_BYTEPOS (*it) + 1,
6138 (double) it->selective)) /* iftc */
6139 {
6140 success_p = next_element_from_ellipsis (it);
6141 it->dpvec_char_len = -1;
6142 }
6143 }
6144 else if (it->c == '\r' && it->selective == -1)
6145 {
6146 /* A value of selective == -1 means that everything from the
6147 CR to the end of the line is invisible, with maybe an
6148 ellipsis displayed for it. */
6149 success_p = next_element_from_ellipsis (it);
6150 it->dpvec_char_len = -1;
6151 }
6152 }
6153 }
6154
6155 /* Value is zero if end of buffer reached. */
6156 xassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
6157 return success_p;
6158 }
6159
6160
6161 /* Run the redisplay end trigger hook for IT. */
6162
6163 static void
6164 run_redisplay_end_trigger_hook (it)
6165 struct it *it;
6166 {
6167 Lisp_Object args[3];
6168
6169 /* IT->glyph_row should be non-null, i.e. we should be actually
6170 displaying something, or otherwise we should not run the hook. */
6171 xassert (it->glyph_row);
6172
6173 /* Set up hook arguments. */
6174 args[0] = Qredisplay_end_trigger_functions;
6175 args[1] = it->window;
6176 XSETINT (args[2], it->redisplay_end_trigger_charpos);
6177 it->redisplay_end_trigger_charpos = 0;
6178
6179 /* Since we are *trying* to run these functions, don't try to run
6180 them again, even if they get an error. */
6181 it->w->redisplay_end_trigger = Qnil;
6182 Frun_hook_with_args (3, args);
6183
6184 /* Notice if it changed the face of the character we are on. */
6185 handle_face_prop (it);
6186 }
6187
6188
6189 /* Deliver a composition display element. The iterator IT is already
6190 filled with composition information (done in
6191 handle_composition_prop). Value is always 1. */
6192
6193 static int
6194 next_element_from_composition (it)
6195 struct it *it;
6196 {
6197 it->what = IT_COMPOSITION;
6198 it->position = (STRINGP (it->string)
6199 ? it->current.string_pos
6200 : it->current.pos);
6201 return 1;
6202 }
6203
6204
6205 \f
6206 /***********************************************************************
6207 Moving an iterator without producing glyphs
6208 ***********************************************************************/
6209
6210 /* Check if iterator is at a position corresponding to a valid buffer
6211 position after some move_it_ call. */
6212
6213 #define IT_POS_VALID_AFTER_MOVE_P(it) \
6214 ((it)->method == GET_FROM_STRING \
6215 ? IT_STRING_CHARPOS (*it) == 0 \
6216 : 1)
6217
6218
6219 /* Move iterator IT to a specified buffer or X position within one
6220 line on the display without producing glyphs.
6221
6222 OP should be a bit mask including some or all of these bits:
6223 MOVE_TO_X: Stop on reaching x-position TO_X.
6224 MOVE_TO_POS: Stop on reaching buffer or string position TO_CHARPOS.
6225 Regardless of OP's value, stop in reaching the end of the display line.
6226
6227 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
6228 This means, in particular, that TO_X includes window's horizontal
6229 scroll amount.
6230
6231 The return value has several possible values that
6232 say what condition caused the scan to stop:
6233
6234 MOVE_POS_MATCH_OR_ZV
6235 - when TO_POS or ZV was reached.
6236
6237 MOVE_X_REACHED
6238 -when TO_X was reached before TO_POS or ZV were reached.
6239
6240 MOVE_LINE_CONTINUED
6241 - when we reached the end of the display area and the line must
6242 be continued.
6243
6244 MOVE_LINE_TRUNCATED
6245 - when we reached the end of the display area and the line is
6246 truncated.
6247
6248 MOVE_NEWLINE_OR_CR
6249 - when we stopped at a line end, i.e. a newline or a CR and selective
6250 display is on. */
6251
6252 static enum move_it_result
6253 move_it_in_display_line_to (it, to_charpos, to_x, op)
6254 struct it *it;
6255 int to_charpos, to_x, op;
6256 {
6257 enum move_it_result result = MOVE_UNDEFINED;
6258 struct glyph_row *saved_glyph_row;
6259
6260 /* Don't produce glyphs in produce_glyphs. */
6261 saved_glyph_row = it->glyph_row;
6262 it->glyph_row = NULL;
6263
6264 #define BUFFER_POS_REACHED_P() \
6265 ((op & MOVE_TO_POS) != 0 \
6266 && BUFFERP (it->object) \
6267 && IT_CHARPOS (*it) >= to_charpos \
6268 && (it->method == GET_FROM_BUFFER \
6269 || (it->method == GET_FROM_DISPLAY_VECTOR \
6270 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
6271
6272
6273 while (1)
6274 {
6275 int x, i, ascent = 0, descent = 0;
6276
6277 /* Stop if we move beyond TO_CHARPOS (after an image or stretch glyph). */
6278 if ((op & MOVE_TO_POS) != 0
6279 && BUFFERP (it->object)
6280 && it->method == GET_FROM_BUFFER
6281 && IT_CHARPOS (*it) > to_charpos)
6282 {
6283 result = MOVE_POS_MATCH_OR_ZV;
6284 break;
6285 }
6286
6287 /* Stop when ZV reached.
6288 We used to stop here when TO_CHARPOS reached as well, but that is
6289 too soon if this glyph does not fit on this line. So we handle it
6290 explicitly below. */
6291 if (!get_next_display_element (it)
6292 || (it->truncate_lines_p
6293 && BUFFER_POS_REACHED_P ()))
6294 {
6295 result = MOVE_POS_MATCH_OR_ZV;
6296 break;
6297 }
6298
6299 /* The call to produce_glyphs will get the metrics of the
6300 display element IT is loaded with. We record in x the
6301 x-position before this display element in case it does not
6302 fit on the line. */
6303 x = it->current_x;
6304
6305 /* Remember the line height so far in case the next element doesn't
6306 fit on the line. */
6307 if (!it->truncate_lines_p)
6308 {
6309 ascent = it->max_ascent;
6310 descent = it->max_descent;
6311 }
6312
6313 PRODUCE_GLYPHS (it);
6314
6315 if (it->area != TEXT_AREA)
6316 {
6317 set_iterator_to_next (it, 1);
6318 continue;
6319 }
6320
6321 /* The number of glyphs we get back in IT->nglyphs will normally
6322 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
6323 character on a terminal frame, or (iii) a line end. For the
6324 second case, IT->nglyphs - 1 padding glyphs will be present
6325 (on X frames, there is only one glyph produced for a
6326 composite character.
6327
6328 The behavior implemented below means, for continuation lines,
6329 that as many spaces of a TAB as fit on the current line are
6330 displayed there. For terminal frames, as many glyphs of a
6331 multi-glyph character are displayed in the current line, too.
6332 This is what the old redisplay code did, and we keep it that
6333 way. Under X, the whole shape of a complex character must
6334 fit on the line or it will be completely displayed in the
6335 next line.
6336
6337 Note that both for tabs and padding glyphs, all glyphs have
6338 the same width. */
6339 if (it->nglyphs)
6340 {
6341 /* More than one glyph or glyph doesn't fit on line. All
6342 glyphs have the same width. */
6343 int single_glyph_width = it->pixel_width / it->nglyphs;
6344 int new_x;
6345 int x_before_this_char = x;
6346 int hpos_before_this_char = it->hpos;
6347
6348 for (i = 0; i < it->nglyphs; ++i, x = new_x)
6349 {
6350 new_x = x + single_glyph_width;
6351
6352 /* We want to leave anything reaching TO_X to the caller. */
6353 if ((op & MOVE_TO_X) && new_x > to_x)
6354 {
6355 if (BUFFER_POS_REACHED_P ())
6356 goto buffer_pos_reached;
6357 it->current_x = x;
6358 result = MOVE_X_REACHED;
6359 break;
6360 }
6361 else if (/* Lines are continued. */
6362 !it->truncate_lines_p
6363 && (/* And glyph doesn't fit on the line. */
6364 new_x > it->last_visible_x
6365 /* Or it fits exactly and we're on a window
6366 system frame. */
6367 || (new_x == it->last_visible_x
6368 && FRAME_WINDOW_P (it->f))))
6369 {
6370 if (/* IT->hpos == 0 means the very first glyph
6371 doesn't fit on the line, e.g. a wide image. */
6372 it->hpos == 0
6373 || (new_x == it->last_visible_x
6374 && FRAME_WINDOW_P (it->f)))
6375 {
6376 ++it->hpos;
6377 it->current_x = new_x;
6378
6379 /* The character's last glyph just barely fits
6380 in this row. */
6381 if (i == it->nglyphs - 1)
6382 {
6383 /* If this is the destination position,
6384 return a position *before* it in this row,
6385 now that we know it fits in this row. */
6386 if (BUFFER_POS_REACHED_P ())
6387 {
6388 it->hpos = hpos_before_this_char;
6389 it->current_x = x_before_this_char;
6390 result = MOVE_POS_MATCH_OR_ZV;
6391 break;
6392 }
6393
6394 set_iterator_to_next (it, 1);
6395 #ifdef HAVE_WINDOW_SYSTEM
6396 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
6397 {
6398 if (!get_next_display_element (it))
6399 {
6400 result = MOVE_POS_MATCH_OR_ZV;
6401 break;
6402 }
6403 if (BUFFER_POS_REACHED_P ())
6404 {
6405 if (ITERATOR_AT_END_OF_LINE_P (it))
6406 result = MOVE_POS_MATCH_OR_ZV;
6407 else
6408 result = MOVE_LINE_CONTINUED;
6409 break;
6410 }
6411 if (ITERATOR_AT_END_OF_LINE_P (it))
6412 {
6413 result = MOVE_NEWLINE_OR_CR;
6414 break;
6415 }
6416 }
6417 #endif /* HAVE_WINDOW_SYSTEM */
6418 }
6419 }
6420 else
6421 {
6422 it->current_x = x;
6423 it->max_ascent = ascent;
6424 it->max_descent = descent;
6425 }
6426
6427 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
6428 IT_CHARPOS (*it)));
6429 result = MOVE_LINE_CONTINUED;
6430 break;
6431 }
6432 else if (BUFFER_POS_REACHED_P ())
6433 goto buffer_pos_reached;
6434 else if (new_x > it->first_visible_x)
6435 {
6436 /* Glyph is visible. Increment number of glyphs that
6437 would be displayed. */
6438 ++it->hpos;
6439 }
6440 else
6441 {
6442 /* Glyph is completely off the left margin of the display
6443 area. Nothing to do. */
6444 }
6445 }
6446
6447 if (result != MOVE_UNDEFINED)
6448 break;
6449 }
6450 else if (BUFFER_POS_REACHED_P ())
6451 {
6452 buffer_pos_reached:
6453 it->current_x = x;
6454 it->max_ascent = ascent;
6455 it->max_descent = descent;
6456 result = MOVE_POS_MATCH_OR_ZV;
6457 break;
6458 }
6459 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
6460 {
6461 /* Stop when TO_X specified and reached. This check is
6462 necessary here because of lines consisting of a line end,
6463 only. The line end will not produce any glyphs and we
6464 would never get MOVE_X_REACHED. */
6465 xassert (it->nglyphs == 0);
6466 result = MOVE_X_REACHED;
6467 break;
6468 }
6469
6470 /* Is this a line end? If yes, we're done. */
6471 if (ITERATOR_AT_END_OF_LINE_P (it))
6472 {
6473 result = MOVE_NEWLINE_OR_CR;
6474 break;
6475 }
6476
6477 /* The current display element has been consumed. Advance
6478 to the next. */
6479 set_iterator_to_next (it, 1);
6480
6481 /* Stop if lines are truncated and IT's current x-position is
6482 past the right edge of the window now. */
6483 if (it->truncate_lines_p
6484 && it->current_x >= it->last_visible_x)
6485 {
6486 #ifdef HAVE_WINDOW_SYSTEM
6487 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
6488 {
6489 if (!get_next_display_element (it)
6490 || BUFFER_POS_REACHED_P ())
6491 {
6492 result = MOVE_POS_MATCH_OR_ZV;
6493 break;
6494 }
6495 if (ITERATOR_AT_END_OF_LINE_P (it))
6496 {
6497 result = MOVE_NEWLINE_OR_CR;
6498 break;
6499 }
6500 }
6501 #endif /* HAVE_WINDOW_SYSTEM */
6502 result = MOVE_LINE_TRUNCATED;
6503 break;
6504 }
6505 }
6506
6507 #undef BUFFER_POS_REACHED_P
6508
6509 /* Restore the iterator settings altered at the beginning of this
6510 function. */
6511 it->glyph_row = saved_glyph_row;
6512 return result;
6513 }
6514
6515
6516 /* Move IT forward until it satisfies one or more of the criteria in
6517 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
6518
6519 OP is a bit-mask that specifies where to stop, and in particular,
6520 which of those four position arguments makes a difference. See the
6521 description of enum move_operation_enum.
6522
6523 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
6524 screen line, this function will set IT to the next position >
6525 TO_CHARPOS. */
6526
6527 void
6528 move_it_to (it, to_charpos, to_x, to_y, to_vpos, op)
6529 struct it *it;
6530 int to_charpos, to_x, to_y, to_vpos;
6531 int op;
6532 {
6533 enum move_it_result skip, skip2 = MOVE_X_REACHED;
6534 int line_height;
6535 int reached = 0;
6536
6537 for (;;)
6538 {
6539 if (op & MOVE_TO_VPOS)
6540 {
6541 /* If no TO_CHARPOS and no TO_X specified, stop at the
6542 start of the line TO_VPOS. */
6543 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
6544 {
6545 if (it->vpos == to_vpos)
6546 {
6547 reached = 1;
6548 break;
6549 }
6550 else
6551 skip = move_it_in_display_line_to (it, -1, -1, 0);
6552 }
6553 else
6554 {
6555 /* TO_VPOS >= 0 means stop at TO_X in the line at
6556 TO_VPOS, or at TO_POS, whichever comes first. */
6557 if (it->vpos == to_vpos)
6558 {
6559 reached = 2;
6560 break;
6561 }
6562
6563 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
6564
6565 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
6566 {
6567 reached = 3;
6568 break;
6569 }
6570 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
6571 {
6572 /* We have reached TO_X but not in the line we want. */
6573 skip = move_it_in_display_line_to (it, to_charpos,
6574 -1, MOVE_TO_POS);
6575 if (skip == MOVE_POS_MATCH_OR_ZV)
6576 {
6577 reached = 4;
6578 break;
6579 }
6580 }
6581 }
6582 }
6583 else if (op & MOVE_TO_Y)
6584 {
6585 struct it it_backup;
6586
6587 /* TO_Y specified means stop at TO_X in the line containing
6588 TO_Y---or at TO_CHARPOS if this is reached first. The
6589 problem is that we can't really tell whether the line
6590 contains TO_Y before we have completely scanned it, and
6591 this may skip past TO_X. What we do is to first scan to
6592 TO_X.
6593
6594 If TO_X is not specified, use a TO_X of zero. The reason
6595 is to make the outcome of this function more predictable.
6596 If we didn't use TO_X == 0, we would stop at the end of
6597 the line which is probably not what a caller would expect
6598 to happen. */
6599 skip = move_it_in_display_line_to (it, to_charpos,
6600 ((op & MOVE_TO_X)
6601 ? to_x : 0),
6602 (MOVE_TO_X
6603 | (op & MOVE_TO_POS)));
6604
6605 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
6606 if (skip == MOVE_POS_MATCH_OR_ZV)
6607 {
6608 reached = 5;
6609 break;
6610 }
6611
6612 /* If TO_X was reached, we would like to know whether TO_Y
6613 is in the line. This can only be said if we know the
6614 total line height which requires us to scan the rest of
6615 the line. */
6616 if (skip == MOVE_X_REACHED)
6617 {
6618 it_backup = *it;
6619 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
6620 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
6621 op & MOVE_TO_POS);
6622 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
6623 }
6624
6625 /* Now, decide whether TO_Y is in this line. */
6626 line_height = it->max_ascent + it->max_descent;
6627 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
6628
6629 if (to_y >= it->current_y
6630 && to_y < it->current_y + line_height)
6631 {
6632 if (skip == MOVE_X_REACHED)
6633 /* If TO_Y is in this line and TO_X was reached above,
6634 we scanned too far. We have to restore IT's settings
6635 to the ones before skipping. */
6636 *it = it_backup;
6637 reached = 6;
6638 }
6639 else if (skip == MOVE_X_REACHED)
6640 {
6641 skip = skip2;
6642 if (skip == MOVE_POS_MATCH_OR_ZV)
6643 reached = 7;
6644 }
6645
6646 if (reached)
6647 break;
6648 }
6649 else
6650 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
6651
6652 switch (skip)
6653 {
6654 case MOVE_POS_MATCH_OR_ZV:
6655 reached = 8;
6656 goto out;
6657
6658 case MOVE_NEWLINE_OR_CR:
6659 set_iterator_to_next (it, 1);
6660 it->continuation_lines_width = 0;
6661 break;
6662
6663 case MOVE_LINE_TRUNCATED:
6664 it->continuation_lines_width = 0;
6665 reseat_at_next_visible_line_start (it, 0);
6666 if ((op & MOVE_TO_POS) != 0
6667 && IT_CHARPOS (*it) > to_charpos)
6668 {
6669 reached = 9;
6670 goto out;
6671 }
6672 break;
6673
6674 case MOVE_LINE_CONTINUED:
6675 it->continuation_lines_width += it->current_x;
6676 break;
6677
6678 default:
6679 abort ();
6680 }
6681
6682 /* Reset/increment for the next run. */
6683 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
6684 it->current_x = it->hpos = 0;
6685 it->current_y += it->max_ascent + it->max_descent;
6686 ++it->vpos;
6687 last_height = it->max_ascent + it->max_descent;
6688 last_max_ascent = it->max_ascent;
6689 it->max_ascent = it->max_descent = 0;
6690 }
6691
6692 out:
6693
6694 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
6695 }
6696
6697
6698 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
6699
6700 If DY > 0, move IT backward at least that many pixels. DY = 0
6701 means move IT backward to the preceding line start or BEGV. This
6702 function may move over more than DY pixels if IT->current_y - DY
6703 ends up in the middle of a line; in this case IT->current_y will be
6704 set to the top of the line moved to. */
6705
6706 void
6707 move_it_vertically_backward (it, dy)
6708 struct it *it;
6709 int dy;
6710 {
6711 int nlines, h;
6712 struct it it2, it3;
6713 int start_pos;
6714
6715 move_further_back:
6716 xassert (dy >= 0);
6717
6718 start_pos = IT_CHARPOS (*it);
6719
6720 /* Estimate how many newlines we must move back. */
6721 nlines = max (1, dy / FRAME_LINE_HEIGHT (it->f));
6722
6723 /* Set the iterator's position that many lines back. */
6724 while (nlines-- && IT_CHARPOS (*it) > BEGV)
6725 back_to_previous_visible_line_start (it);
6726
6727 /* Reseat the iterator here. When moving backward, we don't want
6728 reseat to skip forward over invisible text, set up the iterator
6729 to deliver from overlay strings at the new position etc. So,
6730 use reseat_1 here. */
6731 reseat_1 (it, it->current.pos, 1);
6732
6733 /* We are now surely at a line start. */
6734 it->current_x = it->hpos = 0;
6735 it->continuation_lines_width = 0;
6736
6737 /* Move forward and see what y-distance we moved. First move to the
6738 start of the next line so that we get its height. We need this
6739 height to be able to tell whether we reached the specified
6740 y-distance. */
6741 it2 = *it;
6742 it2.max_ascent = it2.max_descent = 0;
6743 do
6744 {
6745 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
6746 MOVE_TO_POS | MOVE_TO_VPOS);
6747 }
6748 while (!IT_POS_VALID_AFTER_MOVE_P (&it2));
6749 xassert (IT_CHARPOS (*it) >= BEGV);
6750 it3 = it2;
6751
6752 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
6753 xassert (IT_CHARPOS (*it) >= BEGV);
6754 /* H is the actual vertical distance from the position in *IT
6755 and the starting position. */
6756 h = it2.current_y - it->current_y;
6757 /* NLINES is the distance in number of lines. */
6758 nlines = it2.vpos - it->vpos;
6759
6760 /* Correct IT's y and vpos position
6761 so that they are relative to the starting point. */
6762 it->vpos -= nlines;
6763 it->current_y -= h;
6764
6765 if (dy == 0)
6766 {
6767 /* DY == 0 means move to the start of the screen line. The
6768 value of nlines is > 0 if continuation lines were involved. */
6769 if (nlines > 0)
6770 move_it_by_lines (it, nlines, 1);
6771 #if 0
6772 /* I think this assert is bogus if buffer contains
6773 invisible text or images. KFS. */
6774 xassert (IT_CHARPOS (*it) <= start_pos);
6775 #endif
6776 }
6777 else
6778 {
6779 /* The y-position we try to reach, relative to *IT.
6780 Note that H has been subtracted in front of the if-statement. */
6781 int target_y = it->current_y + h - dy;
6782 int y0 = it3.current_y;
6783 int y1 = line_bottom_y (&it3);
6784 int line_height = y1 - y0;
6785
6786 /* If we did not reach target_y, try to move further backward if
6787 we can. If we moved too far backward, try to move forward. */
6788 if (target_y < it->current_y
6789 /* This is heuristic. In a window that's 3 lines high, with
6790 a line height of 13 pixels each, recentering with point
6791 on the bottom line will try to move -39/2 = 19 pixels
6792 backward. Try to avoid moving into the first line. */
6793 && (it->current_y - target_y
6794 > min (window_box_height (it->w), line_height * 2 / 3))
6795 && IT_CHARPOS (*it) > BEGV)
6796 {
6797 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
6798 target_y - it->current_y));
6799 dy = it->current_y - target_y;
6800 goto move_further_back;
6801 }
6802 else if (target_y >= it->current_y + line_height
6803 && IT_CHARPOS (*it) < ZV)
6804 {
6805 /* Should move forward by at least one line, maybe more.
6806
6807 Note: Calling move_it_by_lines can be expensive on
6808 terminal frames, where compute_motion is used (via
6809 vmotion) to do the job, when there are very long lines
6810 and truncate-lines is nil. That's the reason for
6811 treating terminal frames specially here. */
6812
6813 if (!FRAME_WINDOW_P (it->f))
6814 move_it_vertically (it, target_y - (it->current_y + line_height));
6815 else
6816 {
6817 do
6818 {
6819 move_it_by_lines (it, 1, 1);
6820 }
6821 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
6822 }
6823
6824 #if 0
6825 /* I think this assert is bogus if buffer contains
6826 invisible text or images. KFS. */
6827 xassert (IT_CHARPOS (*it) >= BEGV);
6828 #endif
6829 }
6830 }
6831 }
6832
6833
6834 /* Move IT by a specified amount of pixel lines DY. DY negative means
6835 move backwards. DY = 0 means move to start of screen line. At the
6836 end, IT will be on the start of a screen line. */
6837
6838 void
6839 move_it_vertically (it, dy)
6840 struct it *it;
6841 int dy;
6842 {
6843 if (dy <= 0)
6844 move_it_vertically_backward (it, -dy);
6845 else
6846 {
6847 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
6848 move_it_to (it, ZV, -1, it->current_y + dy, -1,
6849 MOVE_TO_POS | MOVE_TO_Y);
6850 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
6851
6852 /* If buffer ends in ZV without a newline, move to the start of
6853 the line to satisfy the post-condition. */
6854 if (IT_CHARPOS (*it) == ZV
6855 && ZV > BEGV
6856 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
6857 move_it_by_lines (it, 0, 0);
6858 }
6859 }
6860
6861
6862 /* Move iterator IT past the end of the text line it is in. */
6863
6864 void
6865 move_it_past_eol (it)
6866 struct it *it;
6867 {
6868 enum move_it_result rc;
6869
6870 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
6871 if (rc == MOVE_NEWLINE_OR_CR)
6872 set_iterator_to_next (it, 0);
6873 }
6874
6875
6876 #if 0 /* Currently not used. */
6877
6878 /* Return non-zero if some text between buffer positions START_CHARPOS
6879 and END_CHARPOS is invisible. IT->window is the window for text
6880 property lookup. */
6881
6882 static int
6883 invisible_text_between_p (it, start_charpos, end_charpos)
6884 struct it *it;
6885 int start_charpos, end_charpos;
6886 {
6887 Lisp_Object prop, limit;
6888 int invisible_found_p;
6889
6890 xassert (it != NULL && start_charpos <= end_charpos);
6891
6892 /* Is text at START invisible? */
6893 prop = Fget_char_property (make_number (start_charpos), Qinvisible,
6894 it->window);
6895 if (TEXT_PROP_MEANS_INVISIBLE (prop))
6896 invisible_found_p = 1;
6897 else
6898 {
6899 limit = Fnext_single_char_property_change (make_number (start_charpos),
6900 Qinvisible, Qnil,
6901 make_number (end_charpos));
6902 invisible_found_p = XFASTINT (limit) < end_charpos;
6903 }
6904
6905 return invisible_found_p;
6906 }
6907
6908 #endif /* 0 */
6909
6910
6911 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
6912 negative means move up. DVPOS == 0 means move to the start of the
6913 screen line. NEED_Y_P non-zero means calculate IT->current_y. If
6914 NEED_Y_P is zero, IT->current_y will be left unchanged.
6915
6916 Further optimization ideas: If we would know that IT->f doesn't use
6917 a face with proportional font, we could be faster for
6918 truncate-lines nil. */
6919
6920 void
6921 move_it_by_lines (it, dvpos, need_y_p)
6922 struct it *it;
6923 int dvpos, need_y_p;
6924 {
6925 struct position pos;
6926
6927 if (!FRAME_WINDOW_P (it->f))
6928 {
6929 struct text_pos textpos;
6930
6931 /* We can use vmotion on frames without proportional fonts. */
6932 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
6933 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
6934 reseat (it, textpos, 1);
6935 it->vpos += pos.vpos;
6936 it->current_y += pos.vpos;
6937 }
6938 else if (dvpos == 0)
6939 {
6940 /* DVPOS == 0 means move to the start of the screen line. */
6941 move_it_vertically_backward (it, 0);
6942 xassert (it->current_x == 0 && it->hpos == 0);
6943 /* Let next call to line_bottom_y calculate real line height */
6944 last_height = 0;
6945 }
6946 else if (dvpos > 0)
6947 {
6948 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
6949 if (!IT_POS_VALID_AFTER_MOVE_P (it))
6950 move_it_to (it, IT_CHARPOS (*it) + 1, -1, -1, -1, MOVE_TO_POS);
6951 }
6952 else
6953 {
6954 struct it it2;
6955 int start_charpos, i;
6956
6957 /* Start at the beginning of the screen line containing IT's
6958 position. This may actually move vertically backwards,
6959 in case of overlays, so adjust dvpos accordingly. */
6960 dvpos += it->vpos;
6961 move_it_vertically_backward (it, 0);
6962 dvpos -= it->vpos;
6963
6964 /* Go back -DVPOS visible lines and reseat the iterator there. */
6965 start_charpos = IT_CHARPOS (*it);
6966 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > BEGV; --i)
6967 back_to_previous_visible_line_start (it);
6968 reseat (it, it->current.pos, 1);
6969
6970 /* Move further back if we end up in a string or an image. */
6971 while (!IT_POS_VALID_AFTER_MOVE_P (it))
6972 {
6973 /* First try to move to start of display line. */
6974 dvpos += it->vpos;
6975 move_it_vertically_backward (it, 0);
6976 dvpos -= it->vpos;
6977 if (IT_POS_VALID_AFTER_MOVE_P (it))
6978 break;
6979 /* If start of line is still in string or image,
6980 move further back. */
6981 back_to_previous_visible_line_start (it);
6982 reseat (it, it->current.pos, 1);
6983 dvpos--;
6984 }
6985
6986 it->current_x = it->hpos = 0;
6987
6988 /* Above call may have moved too far if continuation lines
6989 are involved. Scan forward and see if it did. */
6990 it2 = *it;
6991 it2.vpos = it2.current_y = 0;
6992 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
6993 it->vpos -= it2.vpos;
6994 it->current_y -= it2.current_y;
6995 it->current_x = it->hpos = 0;
6996
6997 /* If we moved too far back, move IT some lines forward. */
6998 if (it2.vpos > -dvpos)
6999 {
7000 int delta = it2.vpos + dvpos;
7001 it2 = *it;
7002 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
7003 /* Move back again if we got too far ahead. */
7004 if (IT_CHARPOS (*it) >= start_charpos)
7005 *it = it2;
7006 }
7007 }
7008 }
7009
7010 /* Return 1 if IT points into the middle of a display vector. */
7011
7012 int
7013 in_display_vector_p (it)
7014 struct it *it;
7015 {
7016 return (it->method == GET_FROM_DISPLAY_VECTOR
7017 && it->current.dpvec_index > 0
7018 && it->dpvec + it->current.dpvec_index != it->dpend);
7019 }
7020
7021 \f
7022 /***********************************************************************
7023 Messages
7024 ***********************************************************************/
7025
7026
7027 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
7028 to *Messages*. */
7029
7030 void
7031 add_to_log (format, arg1, arg2)
7032 char *format;
7033 Lisp_Object arg1, arg2;
7034 {
7035 Lisp_Object args[3];
7036 Lisp_Object msg, fmt;
7037 char *buffer;
7038 int len;
7039 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
7040 USE_SAFE_ALLOCA;
7041
7042 /* Do nothing if called asynchronously. Inserting text into
7043 a buffer may call after-change-functions and alike and
7044 that would means running Lisp asynchronously. */
7045 if (handling_signal)
7046 return;
7047
7048 fmt = msg = Qnil;
7049 GCPRO4 (fmt, msg, arg1, arg2);
7050
7051 args[0] = fmt = build_string (format);
7052 args[1] = arg1;
7053 args[2] = arg2;
7054 msg = Fformat (3, args);
7055
7056 len = SBYTES (msg) + 1;
7057 SAFE_ALLOCA (buffer, char *, len);
7058 bcopy (SDATA (msg), buffer, len);
7059
7060 message_dolog (buffer, len - 1, 1, 0);
7061 SAFE_FREE ();
7062
7063 UNGCPRO;
7064 }
7065
7066
7067 /* Output a newline in the *Messages* buffer if "needs" one. */
7068
7069 void
7070 message_log_maybe_newline ()
7071 {
7072 if (message_log_need_newline)
7073 message_dolog ("", 0, 1, 0);
7074 }
7075
7076
7077 /* Add a string M of length NBYTES to the message log, optionally
7078 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
7079 nonzero, means interpret the contents of M as multibyte. This
7080 function calls low-level routines in order to bypass text property
7081 hooks, etc. which might not be safe to run.
7082
7083 This may GC (insert may run before/after change hooks),
7084 so the buffer M must NOT point to a Lisp string. */
7085
7086 void
7087 message_dolog (m, nbytes, nlflag, multibyte)
7088 const char *m;
7089 int nbytes, nlflag, multibyte;
7090 {
7091 if (!NILP (Vmemory_full))
7092 return;
7093
7094 if (!NILP (Vmessage_log_max))
7095 {
7096 struct buffer *oldbuf;
7097 Lisp_Object oldpoint, oldbegv, oldzv;
7098 int old_windows_or_buffers_changed = windows_or_buffers_changed;
7099 int point_at_end = 0;
7100 int zv_at_end = 0;
7101 Lisp_Object old_deactivate_mark, tem;
7102 struct gcpro gcpro1;
7103
7104 old_deactivate_mark = Vdeactivate_mark;
7105 oldbuf = current_buffer;
7106 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
7107 current_buffer->undo_list = Qt;
7108
7109 oldpoint = message_dolog_marker1;
7110 set_marker_restricted (oldpoint, make_number (PT), Qnil);
7111 oldbegv = message_dolog_marker2;
7112 set_marker_restricted (oldbegv, make_number (BEGV), Qnil);
7113 oldzv = message_dolog_marker3;
7114 set_marker_restricted (oldzv, make_number (ZV), Qnil);
7115 GCPRO1 (old_deactivate_mark);
7116
7117 if (PT == Z)
7118 point_at_end = 1;
7119 if (ZV == Z)
7120 zv_at_end = 1;
7121
7122 BEGV = BEG;
7123 BEGV_BYTE = BEG_BYTE;
7124 ZV = Z;
7125 ZV_BYTE = Z_BYTE;
7126 TEMP_SET_PT_BOTH (Z, Z_BYTE);
7127
7128 /* Insert the string--maybe converting multibyte to single byte
7129 or vice versa, so that all the text fits the buffer. */
7130 if (multibyte
7131 && NILP (current_buffer->enable_multibyte_characters))
7132 {
7133 int i, c, char_bytes;
7134 unsigned char work[1];
7135
7136 /* Convert a multibyte string to single-byte
7137 for the *Message* buffer. */
7138 for (i = 0; i < nbytes; i += char_bytes)
7139 {
7140 c = string_char_and_length (m + i, nbytes - i, &char_bytes);
7141 work[0] = (SINGLE_BYTE_CHAR_P (c)
7142 ? c
7143 : multibyte_char_to_unibyte (c, Qnil));
7144 insert_1_both (work, 1, 1, 1, 0, 0);
7145 }
7146 }
7147 else if (! multibyte
7148 && ! NILP (current_buffer->enable_multibyte_characters))
7149 {
7150 int i, c, char_bytes;
7151 unsigned char *msg = (unsigned char *) m;
7152 unsigned char str[MAX_MULTIBYTE_LENGTH];
7153 /* Convert a single-byte string to multibyte
7154 for the *Message* buffer. */
7155 for (i = 0; i < nbytes; i++)
7156 {
7157 c = unibyte_char_to_multibyte (msg[i]);
7158 char_bytes = CHAR_STRING (c, str);
7159 insert_1_both (str, 1, char_bytes, 1, 0, 0);
7160 }
7161 }
7162 else if (nbytes)
7163 insert_1 (m, nbytes, 1, 0, 0);
7164
7165 if (nlflag)
7166 {
7167 int this_bol, this_bol_byte, prev_bol, prev_bol_byte, dup;
7168 insert_1 ("\n", 1, 1, 0, 0);
7169
7170 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
7171 this_bol = PT;
7172 this_bol_byte = PT_BYTE;
7173
7174 /* See if this line duplicates the previous one.
7175 If so, combine duplicates. */
7176 if (this_bol > BEG)
7177 {
7178 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
7179 prev_bol = PT;
7180 prev_bol_byte = PT_BYTE;
7181
7182 dup = message_log_check_duplicate (prev_bol, prev_bol_byte,
7183 this_bol, this_bol_byte);
7184 if (dup)
7185 {
7186 del_range_both (prev_bol, prev_bol_byte,
7187 this_bol, this_bol_byte, 0);
7188 if (dup > 1)
7189 {
7190 char dupstr[40];
7191 int duplen;
7192
7193 /* If you change this format, don't forget to also
7194 change message_log_check_duplicate. */
7195 sprintf (dupstr, " [%d times]", dup);
7196 duplen = strlen (dupstr);
7197 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
7198 insert_1 (dupstr, duplen, 1, 0, 1);
7199 }
7200 }
7201 }
7202
7203 /* If we have more than the desired maximum number of lines
7204 in the *Messages* buffer now, delete the oldest ones.
7205 This is safe because we don't have undo in this buffer. */
7206
7207 if (NATNUMP (Vmessage_log_max))
7208 {
7209 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
7210 -XFASTINT (Vmessage_log_max) - 1, 0);
7211 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
7212 }
7213 }
7214 BEGV = XMARKER (oldbegv)->charpos;
7215 BEGV_BYTE = marker_byte_position (oldbegv);
7216
7217 if (zv_at_end)
7218 {
7219 ZV = Z;
7220 ZV_BYTE = Z_BYTE;
7221 }
7222 else
7223 {
7224 ZV = XMARKER (oldzv)->charpos;
7225 ZV_BYTE = marker_byte_position (oldzv);
7226 }
7227
7228 if (point_at_end)
7229 TEMP_SET_PT_BOTH (Z, Z_BYTE);
7230 else
7231 /* We can't do Fgoto_char (oldpoint) because it will run some
7232 Lisp code. */
7233 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
7234 XMARKER (oldpoint)->bytepos);
7235
7236 UNGCPRO;
7237 unchain_marker (XMARKER (oldpoint));
7238 unchain_marker (XMARKER (oldbegv));
7239 unchain_marker (XMARKER (oldzv));
7240
7241 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
7242 set_buffer_internal (oldbuf);
7243 if (NILP (tem))
7244 windows_or_buffers_changed = old_windows_or_buffers_changed;
7245 message_log_need_newline = !nlflag;
7246 Vdeactivate_mark = old_deactivate_mark;
7247 }
7248 }
7249
7250
7251 /* We are at the end of the buffer after just having inserted a newline.
7252 (Note: We depend on the fact we won't be crossing the gap.)
7253 Check to see if the most recent message looks a lot like the previous one.
7254 Return 0 if different, 1 if the new one should just replace it, or a
7255 value N > 1 if we should also append " [N times]". */
7256
7257 static int
7258 message_log_check_duplicate (prev_bol, prev_bol_byte, this_bol, this_bol_byte)
7259 int prev_bol, this_bol;
7260 int prev_bol_byte, this_bol_byte;
7261 {
7262 int i;
7263 int len = Z_BYTE - 1 - this_bol_byte;
7264 int seen_dots = 0;
7265 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
7266 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
7267
7268 for (i = 0; i < len; i++)
7269 {
7270 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
7271 seen_dots = 1;
7272 if (p1[i] != p2[i])
7273 return seen_dots;
7274 }
7275 p1 += len;
7276 if (*p1 == '\n')
7277 return 2;
7278 if (*p1++ == ' ' && *p1++ == '[')
7279 {
7280 int n = 0;
7281 while (*p1 >= '0' && *p1 <= '9')
7282 n = n * 10 + *p1++ - '0';
7283 if (strncmp (p1, " times]\n", 8) == 0)
7284 return n+1;
7285 }
7286 return 0;
7287 }
7288 \f
7289
7290 /* Display an echo area message M with a specified length of NBYTES
7291 bytes. The string may include null characters. If M is 0, clear
7292 out any existing message, and let the mini-buffer text show
7293 through.
7294
7295 This may GC, so the buffer M must NOT point to a Lisp string. */
7296
7297 void
7298 message2 (m, nbytes, multibyte)
7299 const char *m;
7300 int nbytes;
7301 int multibyte;
7302 {
7303 /* First flush out any partial line written with print. */
7304 message_log_maybe_newline ();
7305 if (m)
7306 message_dolog (m, nbytes, 1, multibyte);
7307 message2_nolog (m, nbytes, multibyte);
7308 }
7309
7310
7311 /* The non-logging counterpart of message2. */
7312
7313 void
7314 message2_nolog (m, nbytes, multibyte)
7315 const char *m;
7316 int nbytes, multibyte;
7317 {
7318 struct frame *sf = SELECTED_FRAME ();
7319 message_enable_multibyte = multibyte;
7320
7321 if (noninteractive)
7322 {
7323 if (noninteractive_need_newline)
7324 putc ('\n', stderr);
7325 noninteractive_need_newline = 0;
7326 if (m)
7327 fwrite (m, nbytes, 1, stderr);
7328 if (cursor_in_echo_area == 0)
7329 fprintf (stderr, "\n");
7330 fflush (stderr);
7331 }
7332 /* A null message buffer means that the frame hasn't really been
7333 initialized yet. Error messages get reported properly by
7334 cmd_error, so this must be just an informative message; toss it. */
7335 else if (INTERACTIVE
7336 && sf->glyphs_initialized_p
7337 && FRAME_MESSAGE_BUF (sf))
7338 {
7339 Lisp_Object mini_window;
7340 struct frame *f;
7341
7342 /* Get the frame containing the mini-buffer
7343 that the selected frame is using. */
7344 mini_window = FRAME_MINIBUF_WINDOW (sf);
7345 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
7346
7347 FRAME_SAMPLE_VISIBILITY (f);
7348 if (FRAME_VISIBLE_P (sf)
7349 && ! FRAME_VISIBLE_P (f))
7350 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
7351
7352 if (m)
7353 {
7354 set_message (m, Qnil, nbytes, multibyte);
7355 if (minibuffer_auto_raise)
7356 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
7357 }
7358 else
7359 clear_message (1, 1);
7360
7361 do_pending_window_change (0);
7362 echo_area_display (1);
7363 do_pending_window_change (0);
7364 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
7365 (*frame_up_to_date_hook) (f);
7366 }
7367 }
7368
7369
7370 /* Display an echo area message M with a specified length of NBYTES
7371 bytes. The string may include null characters. If M is not a
7372 string, clear out any existing message, and let the mini-buffer
7373 text show through.
7374
7375 This function cancels echoing. */
7376
7377 void
7378 message3 (m, nbytes, multibyte)
7379 Lisp_Object m;
7380 int nbytes;
7381 int multibyte;
7382 {
7383 struct gcpro gcpro1;
7384
7385 GCPRO1 (m);
7386 clear_message (1,1);
7387 cancel_echoing ();
7388
7389 /* First flush out any partial line written with print. */
7390 message_log_maybe_newline ();
7391 if (STRINGP (m))
7392 {
7393 char *buffer;
7394 USE_SAFE_ALLOCA;
7395
7396 SAFE_ALLOCA (buffer, char *, nbytes);
7397 bcopy (SDATA (m), buffer, nbytes);
7398 message_dolog (buffer, nbytes, 1, multibyte);
7399 SAFE_FREE ();
7400 }
7401 message3_nolog (m, nbytes, multibyte);
7402
7403 UNGCPRO;
7404 }
7405
7406
7407 /* The non-logging version of message3.
7408 This does not cancel echoing, because it is used for echoing.
7409 Perhaps we need to make a separate function for echoing
7410 and make this cancel echoing. */
7411
7412 void
7413 message3_nolog (m, nbytes, multibyte)
7414 Lisp_Object m;
7415 int nbytes, multibyte;
7416 {
7417 struct frame *sf = SELECTED_FRAME ();
7418 message_enable_multibyte = multibyte;
7419
7420 if (noninteractive)
7421 {
7422 if (noninteractive_need_newline)
7423 putc ('\n', stderr);
7424 noninteractive_need_newline = 0;
7425 if (STRINGP (m))
7426 fwrite (SDATA (m), nbytes, 1, stderr);
7427 if (cursor_in_echo_area == 0)
7428 fprintf (stderr, "\n");
7429 fflush (stderr);
7430 }
7431 /* A null message buffer means that the frame hasn't really been
7432 initialized yet. Error messages get reported properly by
7433 cmd_error, so this must be just an informative message; toss it. */
7434 else if (INTERACTIVE
7435 && sf->glyphs_initialized_p
7436 && FRAME_MESSAGE_BUF (sf))
7437 {
7438 Lisp_Object mini_window;
7439 Lisp_Object frame;
7440 struct frame *f;
7441
7442 /* Get the frame containing the mini-buffer
7443 that the selected frame is using. */
7444 mini_window = FRAME_MINIBUF_WINDOW (sf);
7445 frame = XWINDOW (mini_window)->frame;
7446 f = XFRAME (frame);
7447
7448 FRAME_SAMPLE_VISIBILITY (f);
7449 if (FRAME_VISIBLE_P (sf)
7450 && !FRAME_VISIBLE_P (f))
7451 Fmake_frame_visible (frame);
7452
7453 if (STRINGP (m) && SCHARS (m) > 0)
7454 {
7455 set_message (NULL, m, nbytes, multibyte);
7456 if (minibuffer_auto_raise)
7457 Fraise_frame (frame);
7458 /* Assume we are not echoing.
7459 (If we are, echo_now will override this.) */
7460 echo_message_buffer = Qnil;
7461 }
7462 else
7463 clear_message (1, 1);
7464
7465 do_pending_window_change (0);
7466 echo_area_display (1);
7467 do_pending_window_change (0);
7468 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
7469 (*frame_up_to_date_hook) (f);
7470 }
7471 }
7472
7473
7474 /* Display a null-terminated echo area message M. If M is 0, clear
7475 out any existing message, and let the mini-buffer text show through.
7476
7477 The buffer M must continue to exist until after the echo area gets
7478 cleared or some other message gets displayed there. Do not pass
7479 text that is stored in a Lisp string. Do not pass text in a buffer
7480 that was alloca'd. */
7481
7482 void
7483 message1 (m)
7484 char *m;
7485 {
7486 message2 (m, (m ? strlen (m) : 0), 0);
7487 }
7488
7489
7490 /* The non-logging counterpart of message1. */
7491
7492 void
7493 message1_nolog (m)
7494 char *m;
7495 {
7496 message2_nolog (m, (m ? strlen (m) : 0), 0);
7497 }
7498
7499 /* Display a message M which contains a single %s
7500 which gets replaced with STRING. */
7501
7502 void
7503 message_with_string (m, string, log)
7504 char *m;
7505 Lisp_Object string;
7506 int log;
7507 {
7508 CHECK_STRING (string);
7509
7510 if (noninteractive)
7511 {
7512 if (m)
7513 {
7514 if (noninteractive_need_newline)
7515 putc ('\n', stderr);
7516 noninteractive_need_newline = 0;
7517 fprintf (stderr, m, SDATA (string));
7518 if (cursor_in_echo_area == 0)
7519 fprintf (stderr, "\n");
7520 fflush (stderr);
7521 }
7522 }
7523 else if (INTERACTIVE)
7524 {
7525 /* The frame whose minibuffer we're going to display the message on.
7526 It may be larger than the selected frame, so we need
7527 to use its buffer, not the selected frame's buffer. */
7528 Lisp_Object mini_window;
7529 struct frame *f, *sf = SELECTED_FRAME ();
7530
7531 /* Get the frame containing the minibuffer
7532 that the selected frame is using. */
7533 mini_window = FRAME_MINIBUF_WINDOW (sf);
7534 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
7535
7536 /* A null message buffer means that the frame hasn't really been
7537 initialized yet. Error messages get reported properly by
7538 cmd_error, so this must be just an informative message; toss it. */
7539 if (FRAME_MESSAGE_BUF (f))
7540 {
7541 Lisp_Object args[2], message;
7542 struct gcpro gcpro1, gcpro2;
7543
7544 args[0] = build_string (m);
7545 args[1] = message = string;
7546 GCPRO2 (args[0], message);
7547 gcpro1.nvars = 2;
7548
7549 message = Fformat (2, args);
7550
7551 if (log)
7552 message3 (message, SBYTES (message), STRING_MULTIBYTE (message));
7553 else
7554 message3_nolog (message, SBYTES (message), STRING_MULTIBYTE (message));
7555
7556 UNGCPRO;
7557
7558 /* Print should start at the beginning of the message
7559 buffer next time. */
7560 message_buf_print = 0;
7561 }
7562 }
7563 }
7564
7565
7566 /* Dump an informative message to the minibuf. If M is 0, clear out
7567 any existing message, and let the mini-buffer text show through. */
7568
7569 /* VARARGS 1 */
7570 void
7571 message (m, a1, a2, a3)
7572 char *m;
7573 EMACS_INT a1, a2, a3;
7574 {
7575 if (noninteractive)
7576 {
7577 if (m)
7578 {
7579 if (noninteractive_need_newline)
7580 putc ('\n', stderr);
7581 noninteractive_need_newline = 0;
7582 fprintf (stderr, m, a1, a2, a3);
7583 if (cursor_in_echo_area == 0)
7584 fprintf (stderr, "\n");
7585 fflush (stderr);
7586 }
7587 }
7588 else if (INTERACTIVE)
7589 {
7590 /* The frame whose mini-buffer we're going to display the message
7591 on. It may be larger than the selected frame, so we need to
7592 use its buffer, not the selected frame's buffer. */
7593 Lisp_Object mini_window;
7594 struct frame *f, *sf = SELECTED_FRAME ();
7595
7596 /* Get the frame containing the mini-buffer
7597 that the selected frame is using. */
7598 mini_window = FRAME_MINIBUF_WINDOW (sf);
7599 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
7600
7601 /* A null message buffer means that the frame hasn't really been
7602 initialized yet. Error messages get reported properly by
7603 cmd_error, so this must be just an informative message; toss
7604 it. */
7605 if (FRAME_MESSAGE_BUF (f))
7606 {
7607 if (m)
7608 {
7609 int len;
7610 #ifdef NO_ARG_ARRAY
7611 char *a[3];
7612 a[0] = (char *) a1;
7613 a[1] = (char *) a2;
7614 a[2] = (char *) a3;
7615
7616 len = doprnt (FRAME_MESSAGE_BUF (f),
7617 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
7618 #else
7619 len = doprnt (FRAME_MESSAGE_BUF (f),
7620 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3,
7621 (char **) &a1);
7622 #endif /* NO_ARG_ARRAY */
7623
7624 message2 (FRAME_MESSAGE_BUF (f), len, 0);
7625 }
7626 else
7627 message1 (0);
7628
7629 /* Print should start at the beginning of the message
7630 buffer next time. */
7631 message_buf_print = 0;
7632 }
7633 }
7634 }
7635
7636
7637 /* The non-logging version of message. */
7638
7639 void
7640 message_nolog (m, a1, a2, a3)
7641 char *m;
7642 EMACS_INT a1, a2, a3;
7643 {
7644 Lisp_Object old_log_max;
7645 old_log_max = Vmessage_log_max;
7646 Vmessage_log_max = Qnil;
7647 message (m, a1, a2, a3);
7648 Vmessage_log_max = old_log_max;
7649 }
7650
7651
7652 /* Display the current message in the current mini-buffer. This is
7653 only called from error handlers in process.c, and is not time
7654 critical. */
7655
7656 void
7657 update_echo_area ()
7658 {
7659 if (!NILP (echo_area_buffer[0]))
7660 {
7661 Lisp_Object string;
7662 string = Fcurrent_message ();
7663 message3 (string, SBYTES (string),
7664 !NILP (current_buffer->enable_multibyte_characters));
7665 }
7666 }
7667
7668
7669 /* Make sure echo area buffers in `echo_buffers' are live.
7670 If they aren't, make new ones. */
7671
7672 static void
7673 ensure_echo_area_buffers ()
7674 {
7675 int i;
7676
7677 for (i = 0; i < 2; ++i)
7678 if (!BUFFERP (echo_buffer[i])
7679 || NILP (XBUFFER (echo_buffer[i])->name))
7680 {
7681 char name[30];
7682 Lisp_Object old_buffer;
7683 int j;
7684
7685 old_buffer = echo_buffer[i];
7686 sprintf (name, " *Echo Area %d*", i);
7687 echo_buffer[i] = Fget_buffer_create (build_string (name));
7688 XBUFFER (echo_buffer[i])->truncate_lines = Qnil;
7689
7690 for (j = 0; j < 2; ++j)
7691 if (EQ (old_buffer, echo_area_buffer[j]))
7692 echo_area_buffer[j] = echo_buffer[i];
7693 }
7694 }
7695
7696
7697 /* Call FN with args A1..A4 with either the current or last displayed
7698 echo_area_buffer as current buffer.
7699
7700 WHICH zero means use the current message buffer
7701 echo_area_buffer[0]. If that is nil, choose a suitable buffer
7702 from echo_buffer[] and clear it.
7703
7704 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
7705 suitable buffer from echo_buffer[] and clear it.
7706
7707 Value is what FN returns. */
7708
7709 static int
7710 with_echo_area_buffer (w, which, fn, a1, a2, a3, a4)
7711 struct window *w;
7712 int which;
7713 int (*fn) P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
7714 EMACS_INT a1;
7715 Lisp_Object a2;
7716 EMACS_INT a3, a4;
7717 {
7718 Lisp_Object buffer;
7719 int this_one, the_other, clear_buffer_p, rc;
7720 int count = SPECPDL_INDEX ();
7721
7722 /* If buffers aren't live, make new ones. */
7723 ensure_echo_area_buffers ();
7724
7725 clear_buffer_p = 0;
7726
7727 if (which == 0)
7728 this_one = 0, the_other = 1;
7729 else if (which > 0)
7730 this_one = 1, the_other = 0;
7731
7732 /* Choose a suitable buffer from echo_buffer[] is we don't
7733 have one. */
7734 if (NILP (echo_area_buffer[this_one]))
7735 {
7736 echo_area_buffer[this_one]
7737 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
7738 ? echo_buffer[the_other]
7739 : echo_buffer[this_one]);
7740 clear_buffer_p = 1;
7741 }
7742
7743 buffer = echo_area_buffer[this_one];
7744
7745 /* Don't get confused by reusing the buffer used for echoing
7746 for a different purpose. */
7747 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
7748 cancel_echoing ();
7749
7750 record_unwind_protect (unwind_with_echo_area_buffer,
7751 with_echo_area_buffer_unwind_data (w));
7752
7753 /* Make the echo area buffer current. Note that for display
7754 purposes, it is not necessary that the displayed window's buffer
7755 == current_buffer, except for text property lookup. So, let's
7756 only set that buffer temporarily here without doing a full
7757 Fset_window_buffer. We must also change w->pointm, though,
7758 because otherwise an assertions in unshow_buffer fails, and Emacs
7759 aborts. */
7760 set_buffer_internal_1 (XBUFFER (buffer));
7761 if (w)
7762 {
7763 w->buffer = buffer;
7764 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
7765 }
7766
7767 current_buffer->undo_list = Qt;
7768 current_buffer->read_only = Qnil;
7769 specbind (Qinhibit_read_only, Qt);
7770 specbind (Qinhibit_modification_hooks, Qt);
7771
7772 if (clear_buffer_p && Z > BEG)
7773 del_range (BEG, Z);
7774
7775 xassert (BEGV >= BEG);
7776 xassert (ZV <= Z && ZV >= BEGV);
7777
7778 rc = fn (a1, a2, a3, a4);
7779
7780 xassert (BEGV >= BEG);
7781 xassert (ZV <= Z && ZV >= BEGV);
7782
7783 unbind_to (count, Qnil);
7784 return rc;
7785 }
7786
7787
7788 /* Save state that should be preserved around the call to the function
7789 FN called in with_echo_area_buffer. */
7790
7791 static Lisp_Object
7792 with_echo_area_buffer_unwind_data (w)
7793 struct window *w;
7794 {
7795 int i = 0;
7796 Lisp_Object vector;
7797
7798 /* Reduce consing by keeping one vector in
7799 Vwith_echo_area_save_vector. */
7800 vector = Vwith_echo_area_save_vector;
7801 Vwith_echo_area_save_vector = Qnil;
7802
7803 if (NILP (vector))
7804 vector = Fmake_vector (make_number (7), Qnil);
7805
7806 XSETBUFFER (AREF (vector, i), current_buffer); ++i;
7807 AREF (vector, i) = Vdeactivate_mark, ++i;
7808 AREF (vector, i) = make_number (windows_or_buffers_changed), ++i;
7809
7810 if (w)
7811 {
7812 XSETWINDOW (AREF (vector, i), w); ++i;
7813 AREF (vector, i) = w->buffer; ++i;
7814 AREF (vector, i) = make_number (XMARKER (w->pointm)->charpos); ++i;
7815 AREF (vector, i) = make_number (XMARKER (w->pointm)->bytepos); ++i;
7816 }
7817 else
7818 {
7819 int end = i + 4;
7820 for (; i < end; ++i)
7821 AREF (vector, i) = Qnil;
7822 }
7823
7824 xassert (i == ASIZE (vector));
7825 return vector;
7826 }
7827
7828
7829 /* Restore global state from VECTOR which was created by
7830 with_echo_area_buffer_unwind_data. */
7831
7832 static Lisp_Object
7833 unwind_with_echo_area_buffer (vector)
7834 Lisp_Object vector;
7835 {
7836 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
7837 Vdeactivate_mark = AREF (vector, 1);
7838 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
7839
7840 if (WINDOWP (AREF (vector, 3)))
7841 {
7842 struct window *w;
7843 Lisp_Object buffer, charpos, bytepos;
7844
7845 w = XWINDOW (AREF (vector, 3));
7846 buffer = AREF (vector, 4);
7847 charpos = AREF (vector, 5);
7848 bytepos = AREF (vector, 6);
7849
7850 w->buffer = buffer;
7851 set_marker_both (w->pointm, buffer,
7852 XFASTINT (charpos), XFASTINT (bytepos));
7853 }
7854
7855 Vwith_echo_area_save_vector = vector;
7856 return Qnil;
7857 }
7858
7859
7860 /* Set up the echo area for use by print functions. MULTIBYTE_P
7861 non-zero means we will print multibyte. */
7862
7863 void
7864 setup_echo_area_for_printing (multibyte_p)
7865 int multibyte_p;
7866 {
7867 /* If we can't find an echo area any more, exit. */
7868 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
7869 Fkill_emacs (Qnil);
7870
7871 ensure_echo_area_buffers ();
7872
7873 if (!message_buf_print)
7874 {
7875 /* A message has been output since the last time we printed.
7876 Choose a fresh echo area buffer. */
7877 if (EQ (echo_area_buffer[1], echo_buffer[0]))
7878 echo_area_buffer[0] = echo_buffer[1];
7879 else
7880 echo_area_buffer[0] = echo_buffer[0];
7881
7882 /* Switch to that buffer and clear it. */
7883 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
7884 current_buffer->truncate_lines = Qnil;
7885
7886 if (Z > BEG)
7887 {
7888 int count = SPECPDL_INDEX ();
7889 specbind (Qinhibit_read_only, Qt);
7890 /* Note that undo recording is always disabled. */
7891 del_range (BEG, Z);
7892 unbind_to (count, Qnil);
7893 }
7894 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
7895
7896 /* Set up the buffer for the multibyteness we need. */
7897 if (multibyte_p
7898 != !NILP (current_buffer->enable_multibyte_characters))
7899 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
7900
7901 /* Raise the frame containing the echo area. */
7902 if (minibuffer_auto_raise)
7903 {
7904 struct frame *sf = SELECTED_FRAME ();
7905 Lisp_Object mini_window;
7906 mini_window = FRAME_MINIBUF_WINDOW (sf);
7907 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
7908 }
7909
7910 message_log_maybe_newline ();
7911 message_buf_print = 1;
7912 }
7913 else
7914 {
7915 if (NILP (echo_area_buffer[0]))
7916 {
7917 if (EQ (echo_area_buffer[1], echo_buffer[0]))
7918 echo_area_buffer[0] = echo_buffer[1];
7919 else
7920 echo_area_buffer[0] = echo_buffer[0];
7921 }
7922
7923 if (current_buffer != XBUFFER (echo_area_buffer[0]))
7924 {
7925 /* Someone switched buffers between print requests. */
7926 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
7927 current_buffer->truncate_lines = Qnil;
7928 }
7929 }
7930 }
7931
7932
7933 /* Display an echo area message in window W. Value is non-zero if W's
7934 height is changed. If display_last_displayed_message_p is
7935 non-zero, display the message that was last displayed, otherwise
7936 display the current message. */
7937
7938 static int
7939 display_echo_area (w)
7940 struct window *w;
7941 {
7942 int i, no_message_p, window_height_changed_p, count;
7943
7944 /* Temporarily disable garbage collections while displaying the echo
7945 area. This is done because a GC can print a message itself.
7946 That message would modify the echo area buffer's contents while a
7947 redisplay of the buffer is going on, and seriously confuse
7948 redisplay. */
7949 count = inhibit_garbage_collection ();
7950
7951 /* If there is no message, we must call display_echo_area_1
7952 nevertheless because it resizes the window. But we will have to
7953 reset the echo_area_buffer in question to nil at the end because
7954 with_echo_area_buffer will sets it to an empty buffer. */
7955 i = display_last_displayed_message_p ? 1 : 0;
7956 no_message_p = NILP (echo_area_buffer[i]);
7957
7958 window_height_changed_p
7959 = with_echo_area_buffer (w, display_last_displayed_message_p,
7960 display_echo_area_1,
7961 (EMACS_INT) w, Qnil, 0, 0);
7962
7963 if (no_message_p)
7964 echo_area_buffer[i] = Qnil;
7965
7966 unbind_to (count, Qnil);
7967 return window_height_changed_p;
7968 }
7969
7970
7971 /* Helper for display_echo_area. Display the current buffer which
7972 contains the current echo area message in window W, a mini-window,
7973 a pointer to which is passed in A1. A2..A4 are currently not used.
7974 Change the height of W so that all of the message is displayed.
7975 Value is non-zero if height of W was changed. */
7976
7977 static int
7978 display_echo_area_1 (a1, a2, a3, a4)
7979 EMACS_INT a1;
7980 Lisp_Object a2;
7981 EMACS_INT a3, a4;
7982 {
7983 struct window *w = (struct window *) a1;
7984 Lisp_Object window;
7985 struct text_pos start;
7986 int window_height_changed_p = 0;
7987
7988 /* Do this before displaying, so that we have a large enough glyph
7989 matrix for the display. If we can't get enough space for the
7990 whole text, display the last N lines. That works by setting w->start. */
7991 window_height_changed_p = resize_mini_window (w, 0);
7992
7993 /* Use the starting position chosen by resize_mini_window. */
7994 SET_TEXT_POS_FROM_MARKER (start, w->start);
7995
7996 /* Display. */
7997 clear_glyph_matrix (w->desired_matrix);
7998 XSETWINDOW (window, w);
7999 try_window (window, start, 0);
8000
8001 return window_height_changed_p;
8002 }
8003
8004
8005 /* Resize the echo area window to exactly the size needed for the
8006 currently displayed message, if there is one. If a mini-buffer
8007 is active, don't shrink it. */
8008
8009 void
8010 resize_echo_area_exactly ()
8011 {
8012 if (BUFFERP (echo_area_buffer[0])
8013 && WINDOWP (echo_area_window))
8014 {
8015 struct window *w = XWINDOW (echo_area_window);
8016 int resized_p;
8017 Lisp_Object resize_exactly;
8018
8019 if (minibuf_level == 0)
8020 resize_exactly = Qt;
8021 else
8022 resize_exactly = Qnil;
8023
8024 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
8025 (EMACS_INT) w, resize_exactly, 0, 0);
8026 if (resized_p)
8027 {
8028 ++windows_or_buffers_changed;
8029 ++update_mode_lines;
8030 redisplay_internal (0);
8031 }
8032 }
8033 }
8034
8035
8036 /* Callback function for with_echo_area_buffer, when used from
8037 resize_echo_area_exactly. A1 contains a pointer to the window to
8038 resize, EXACTLY non-nil means resize the mini-window exactly to the
8039 size of the text displayed. A3 and A4 are not used. Value is what
8040 resize_mini_window returns. */
8041
8042 static int
8043 resize_mini_window_1 (a1, exactly, a3, a4)
8044 EMACS_INT a1;
8045 Lisp_Object exactly;
8046 EMACS_INT a3, a4;
8047 {
8048 return resize_mini_window ((struct window *) a1, !NILP (exactly));
8049 }
8050
8051
8052 /* Resize mini-window W to fit the size of its contents. EXACT:P
8053 means size the window exactly to the size needed. Otherwise, it's
8054 only enlarged until W's buffer is empty.
8055
8056 Set W->start to the right place to begin display. If the whole
8057 contents fit, start at the beginning. Otherwise, start so as
8058 to make the end of the contents appear. This is particularly
8059 important for y-or-n-p, but seems desirable generally.
8060
8061 Value is non-zero if the window height has been changed. */
8062
8063 int
8064 resize_mini_window (w, exact_p)
8065 struct window *w;
8066 int exact_p;
8067 {
8068 struct frame *f = XFRAME (w->frame);
8069 int window_height_changed_p = 0;
8070
8071 xassert (MINI_WINDOW_P (w));
8072
8073 /* By default, start display at the beginning. */
8074 set_marker_both (w->start, w->buffer,
8075 BUF_BEGV (XBUFFER (w->buffer)),
8076 BUF_BEGV_BYTE (XBUFFER (w->buffer)));
8077
8078 /* Don't resize windows while redisplaying a window; it would
8079 confuse redisplay functions when the size of the window they are
8080 displaying changes from under them. Such a resizing can happen,
8081 for instance, when which-func prints a long message while
8082 we are running fontification-functions. We're running these
8083 functions with safe_call which binds inhibit-redisplay to t. */
8084 if (!NILP (Vinhibit_redisplay))
8085 return 0;
8086
8087 /* Nil means don't try to resize. */
8088 if (NILP (Vresize_mini_windows)
8089 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
8090 return 0;
8091
8092 if (!FRAME_MINIBUF_ONLY_P (f))
8093 {
8094 struct it it;
8095 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
8096 int total_height = WINDOW_TOTAL_LINES (root) + WINDOW_TOTAL_LINES (w);
8097 int height, max_height;
8098 int unit = FRAME_LINE_HEIGHT (f);
8099 struct text_pos start;
8100 struct buffer *old_current_buffer = NULL;
8101
8102 if (current_buffer != XBUFFER (w->buffer))
8103 {
8104 old_current_buffer = current_buffer;
8105 set_buffer_internal (XBUFFER (w->buffer));
8106 }
8107
8108 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
8109
8110 /* Compute the max. number of lines specified by the user. */
8111 if (FLOATP (Vmax_mini_window_height))
8112 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_LINES (f);
8113 else if (INTEGERP (Vmax_mini_window_height))
8114 max_height = XINT (Vmax_mini_window_height);
8115 else
8116 max_height = total_height / 4;
8117
8118 /* Correct that max. height if it's bogus. */
8119 max_height = max (1, max_height);
8120 max_height = min (total_height, max_height);
8121
8122 /* Find out the height of the text in the window. */
8123 if (it.truncate_lines_p)
8124 height = 1;
8125 else
8126 {
8127 last_height = 0;
8128 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
8129 if (it.max_ascent == 0 && it.max_descent == 0)
8130 height = it.current_y + last_height;
8131 else
8132 height = it.current_y + it.max_ascent + it.max_descent;
8133 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
8134 height = (height + unit - 1) / unit;
8135 }
8136
8137 /* Compute a suitable window start. */
8138 if (height > max_height)
8139 {
8140 height = max_height;
8141 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
8142 move_it_vertically_backward (&it, (height - 1) * unit);
8143 start = it.current.pos;
8144 }
8145 else
8146 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
8147 SET_MARKER_FROM_TEXT_POS (w->start, start);
8148
8149 if (EQ (Vresize_mini_windows, Qgrow_only))
8150 {
8151 /* Let it grow only, until we display an empty message, in which
8152 case the window shrinks again. */
8153 if (height > WINDOW_TOTAL_LINES (w))
8154 {
8155 int old_height = WINDOW_TOTAL_LINES (w);
8156 freeze_window_starts (f, 1);
8157 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
8158 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8159 }
8160 else if (height < WINDOW_TOTAL_LINES (w)
8161 && (exact_p || BEGV == ZV))
8162 {
8163 int old_height = WINDOW_TOTAL_LINES (w);
8164 freeze_window_starts (f, 0);
8165 shrink_mini_window (w);
8166 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8167 }
8168 }
8169 else
8170 {
8171 /* Always resize to exact size needed. */
8172 if (height > WINDOW_TOTAL_LINES (w))
8173 {
8174 int old_height = WINDOW_TOTAL_LINES (w);
8175 freeze_window_starts (f, 1);
8176 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
8177 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8178 }
8179 else if (height < WINDOW_TOTAL_LINES (w))
8180 {
8181 int old_height = WINDOW_TOTAL_LINES (w);
8182 freeze_window_starts (f, 0);
8183 shrink_mini_window (w);
8184
8185 if (height)
8186 {
8187 freeze_window_starts (f, 1);
8188 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
8189 }
8190
8191 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8192 }
8193 }
8194
8195 if (old_current_buffer)
8196 set_buffer_internal (old_current_buffer);
8197 }
8198
8199 return window_height_changed_p;
8200 }
8201
8202
8203 /* Value is the current message, a string, or nil if there is no
8204 current message. */
8205
8206 Lisp_Object
8207 current_message ()
8208 {
8209 Lisp_Object msg;
8210
8211 if (NILP (echo_area_buffer[0]))
8212 msg = Qnil;
8213 else
8214 {
8215 with_echo_area_buffer (0, 0, current_message_1,
8216 (EMACS_INT) &msg, Qnil, 0, 0);
8217 if (NILP (msg))
8218 echo_area_buffer[0] = Qnil;
8219 }
8220
8221 return msg;
8222 }
8223
8224
8225 static int
8226 current_message_1 (a1, a2, a3, a4)
8227 EMACS_INT a1;
8228 Lisp_Object a2;
8229 EMACS_INT a3, a4;
8230 {
8231 Lisp_Object *msg = (Lisp_Object *) a1;
8232
8233 if (Z > BEG)
8234 *msg = make_buffer_string (BEG, Z, 1);
8235 else
8236 *msg = Qnil;
8237 return 0;
8238 }
8239
8240
8241 /* Push the current message on Vmessage_stack for later restauration
8242 by restore_message. Value is non-zero if the current message isn't
8243 empty. This is a relatively infrequent operation, so it's not
8244 worth optimizing. */
8245
8246 int
8247 push_message ()
8248 {
8249 Lisp_Object msg;
8250 msg = current_message ();
8251 Vmessage_stack = Fcons (msg, Vmessage_stack);
8252 return STRINGP (msg);
8253 }
8254
8255
8256 /* Restore message display from the top of Vmessage_stack. */
8257
8258 void
8259 restore_message ()
8260 {
8261 Lisp_Object msg;
8262
8263 xassert (CONSP (Vmessage_stack));
8264 msg = XCAR (Vmessage_stack);
8265 if (STRINGP (msg))
8266 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
8267 else
8268 message3_nolog (msg, 0, 0);
8269 }
8270
8271
8272 /* Handler for record_unwind_protect calling pop_message. */
8273
8274 Lisp_Object
8275 pop_message_unwind (dummy)
8276 Lisp_Object dummy;
8277 {
8278 pop_message ();
8279 return Qnil;
8280 }
8281
8282 /* Pop the top-most entry off Vmessage_stack. */
8283
8284 void
8285 pop_message ()
8286 {
8287 xassert (CONSP (Vmessage_stack));
8288 Vmessage_stack = XCDR (Vmessage_stack);
8289 }
8290
8291
8292 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
8293 exits. If the stack is not empty, we have a missing pop_message
8294 somewhere. */
8295
8296 void
8297 check_message_stack ()
8298 {
8299 if (!NILP (Vmessage_stack))
8300 abort ();
8301 }
8302
8303
8304 /* Truncate to NCHARS what will be displayed in the echo area the next
8305 time we display it---but don't redisplay it now. */
8306
8307 void
8308 truncate_echo_area (nchars)
8309 int nchars;
8310 {
8311 if (nchars == 0)
8312 echo_area_buffer[0] = Qnil;
8313 /* A null message buffer means that the frame hasn't really been
8314 initialized yet. Error messages get reported properly by
8315 cmd_error, so this must be just an informative message; toss it. */
8316 else if (!noninteractive
8317 && INTERACTIVE
8318 && !NILP (echo_area_buffer[0]))
8319 {
8320 struct frame *sf = SELECTED_FRAME ();
8321 if (FRAME_MESSAGE_BUF (sf))
8322 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
8323 }
8324 }
8325
8326
8327 /* Helper function for truncate_echo_area. Truncate the current
8328 message to at most NCHARS characters. */
8329
8330 static int
8331 truncate_message_1 (nchars, a2, a3, a4)
8332 EMACS_INT nchars;
8333 Lisp_Object a2;
8334 EMACS_INT a3, a4;
8335 {
8336 if (BEG + nchars < Z)
8337 del_range (BEG + nchars, Z);
8338 if (Z == BEG)
8339 echo_area_buffer[0] = Qnil;
8340 return 0;
8341 }
8342
8343
8344 /* Set the current message to a substring of S or STRING.
8345
8346 If STRING is a Lisp string, set the message to the first NBYTES
8347 bytes from STRING. NBYTES zero means use the whole string. If
8348 STRING is multibyte, the message will be displayed multibyte.
8349
8350 If S is not null, set the message to the first LEN bytes of S. LEN
8351 zero means use the whole string. MULTIBYTE_P non-zero means S is
8352 multibyte. Display the message multibyte in that case.
8353
8354 Doesn't GC, as with_echo_area_buffer binds Qinhibit_modification_hooks
8355 to t before calling set_message_1 (which calls insert).
8356 */
8357
8358 void
8359 set_message (s, string, nbytes, multibyte_p)
8360 const char *s;
8361 Lisp_Object string;
8362 int nbytes, multibyte_p;
8363 {
8364 message_enable_multibyte
8365 = ((s && multibyte_p)
8366 || (STRINGP (string) && STRING_MULTIBYTE (string)));
8367
8368 with_echo_area_buffer (0, 0, set_message_1,
8369 (EMACS_INT) s, string, nbytes, multibyte_p);
8370 message_buf_print = 0;
8371 help_echo_showing_p = 0;
8372 }
8373
8374
8375 /* Helper function for set_message. Arguments have the same meaning
8376 as there, with A1 corresponding to S and A2 corresponding to STRING
8377 This function is called with the echo area buffer being
8378 current. */
8379
8380 static int
8381 set_message_1 (a1, a2, nbytes, multibyte_p)
8382 EMACS_INT a1;
8383 Lisp_Object a2;
8384 EMACS_INT nbytes, multibyte_p;
8385 {
8386 const char *s = (const char *) a1;
8387 Lisp_Object string = a2;
8388
8389 /* Change multibyteness of the echo buffer appropriately. */
8390 if (message_enable_multibyte
8391 != !NILP (current_buffer->enable_multibyte_characters))
8392 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
8393
8394 current_buffer->truncate_lines = message_truncate_lines ? Qt : Qnil;
8395
8396 /* Insert new message at BEG. */
8397 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
8398 Ferase_buffer ();
8399
8400 if (STRINGP (string))
8401 {
8402 int nchars;
8403
8404 if (nbytes == 0)
8405 nbytes = SBYTES (string);
8406 nchars = string_byte_to_char (string, nbytes);
8407
8408 /* This function takes care of single/multibyte conversion. We
8409 just have to ensure that the echo area buffer has the right
8410 setting of enable_multibyte_characters. */
8411 insert_from_string (string, 0, 0, nchars, nbytes, 1);
8412 }
8413 else if (s)
8414 {
8415 if (nbytes == 0)
8416 nbytes = strlen (s);
8417
8418 if (multibyte_p && NILP (current_buffer->enable_multibyte_characters))
8419 {
8420 /* Convert from multi-byte to single-byte. */
8421 int i, c, n;
8422 unsigned char work[1];
8423
8424 /* Convert a multibyte string to single-byte. */
8425 for (i = 0; i < nbytes; i += n)
8426 {
8427 c = string_char_and_length (s + i, nbytes - i, &n);
8428 work[0] = (SINGLE_BYTE_CHAR_P (c)
8429 ? c
8430 : multibyte_char_to_unibyte (c, Qnil));
8431 insert_1_both (work, 1, 1, 1, 0, 0);
8432 }
8433 }
8434 else if (!multibyte_p
8435 && !NILP (current_buffer->enable_multibyte_characters))
8436 {
8437 /* Convert from single-byte to multi-byte. */
8438 int i, c, n;
8439 const unsigned char *msg = (const unsigned char *) s;
8440 unsigned char str[MAX_MULTIBYTE_LENGTH];
8441
8442 /* Convert a single-byte string to multibyte. */
8443 for (i = 0; i < nbytes; i++)
8444 {
8445 c = unibyte_char_to_multibyte (msg[i]);
8446 n = CHAR_STRING (c, str);
8447 insert_1_both (str, 1, n, 1, 0, 0);
8448 }
8449 }
8450 else
8451 insert_1 (s, nbytes, 1, 0, 0);
8452 }
8453
8454 return 0;
8455 }
8456
8457
8458 /* Clear messages. CURRENT_P non-zero means clear the current
8459 message. LAST_DISPLAYED_P non-zero means clear the message
8460 last displayed. */
8461
8462 void
8463 clear_message (current_p, last_displayed_p)
8464 int current_p, last_displayed_p;
8465 {
8466 if (current_p)
8467 {
8468 echo_area_buffer[0] = Qnil;
8469 message_cleared_p = 1;
8470 }
8471
8472 if (last_displayed_p)
8473 echo_area_buffer[1] = Qnil;
8474
8475 message_buf_print = 0;
8476 }
8477
8478 /* Clear garbaged frames.
8479
8480 This function is used where the old redisplay called
8481 redraw_garbaged_frames which in turn called redraw_frame which in
8482 turn called clear_frame. The call to clear_frame was a source of
8483 flickering. I believe a clear_frame is not necessary. It should
8484 suffice in the new redisplay to invalidate all current matrices,
8485 and ensure a complete redisplay of all windows. */
8486
8487 static void
8488 clear_garbaged_frames ()
8489 {
8490 if (frame_garbaged)
8491 {
8492 Lisp_Object tail, frame;
8493 int changed_count = 0;
8494
8495 FOR_EACH_FRAME (tail, frame)
8496 {
8497 struct frame *f = XFRAME (frame);
8498
8499 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
8500 {
8501 if (f->resized_p)
8502 {
8503 Fredraw_frame (frame);
8504 f->force_flush_display_p = 1;
8505 }
8506 clear_current_matrices (f);
8507 changed_count++;
8508 f->garbaged = 0;
8509 f->resized_p = 0;
8510 }
8511 }
8512
8513 frame_garbaged = 0;
8514 if (changed_count)
8515 ++windows_or_buffers_changed;
8516 }
8517 }
8518
8519
8520 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
8521 is non-zero update selected_frame. Value is non-zero if the
8522 mini-windows height has been changed. */
8523
8524 static int
8525 echo_area_display (update_frame_p)
8526 int update_frame_p;
8527 {
8528 Lisp_Object mini_window;
8529 struct window *w;
8530 struct frame *f;
8531 int window_height_changed_p = 0;
8532 struct frame *sf = SELECTED_FRAME ();
8533
8534 mini_window = FRAME_MINIBUF_WINDOW (sf);
8535 w = XWINDOW (mini_window);
8536 f = XFRAME (WINDOW_FRAME (w));
8537
8538 /* Don't display if frame is invisible or not yet initialized. */
8539 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
8540 return 0;
8541
8542 /* The terminal frame is used as the first Emacs frame on the Mac OS. */
8543 #ifndef MAC_OS8
8544 #ifdef HAVE_WINDOW_SYSTEM
8545 /* When Emacs starts, selected_frame may be a visible terminal
8546 frame, even if we run under a window system. If we let this
8547 through, a message would be displayed on the terminal. */
8548 if (EQ (selected_frame, Vterminal_frame)
8549 && !NILP (Vwindow_system))
8550 return 0;
8551 #endif /* HAVE_WINDOW_SYSTEM */
8552 #endif
8553
8554 /* Redraw garbaged frames. */
8555 if (frame_garbaged)
8556 clear_garbaged_frames ();
8557
8558 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
8559 {
8560 echo_area_window = mini_window;
8561 window_height_changed_p = display_echo_area (w);
8562 w->must_be_updated_p = 1;
8563
8564 /* Update the display, unless called from redisplay_internal.
8565 Also don't update the screen during redisplay itself. The
8566 update will happen at the end of redisplay, and an update
8567 here could cause confusion. */
8568 if (update_frame_p && !redisplaying_p)
8569 {
8570 int n = 0;
8571
8572 /* If the display update has been interrupted by pending
8573 input, update mode lines in the frame. Due to the
8574 pending input, it might have been that redisplay hasn't
8575 been called, so that mode lines above the echo area are
8576 garbaged. This looks odd, so we prevent it here. */
8577 if (!display_completed)
8578 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
8579
8580 if (window_height_changed_p
8581 /* Don't do this if Emacs is shutting down. Redisplay
8582 needs to run hooks. */
8583 && !NILP (Vrun_hooks))
8584 {
8585 /* Must update other windows. Likewise as in other
8586 cases, don't let this update be interrupted by
8587 pending input. */
8588 int count = SPECPDL_INDEX ();
8589 specbind (Qredisplay_dont_pause, Qt);
8590 windows_or_buffers_changed = 1;
8591 redisplay_internal (0);
8592 unbind_to (count, Qnil);
8593 }
8594 else if (FRAME_WINDOW_P (f) && n == 0)
8595 {
8596 /* Window configuration is the same as before.
8597 Can do with a display update of the echo area,
8598 unless we displayed some mode lines. */
8599 update_single_window (w, 1);
8600 rif->flush_display (f);
8601 }
8602 else
8603 update_frame (f, 1, 1);
8604
8605 /* If cursor is in the echo area, make sure that the next
8606 redisplay displays the minibuffer, so that the cursor will
8607 be replaced with what the minibuffer wants. */
8608 if (cursor_in_echo_area)
8609 ++windows_or_buffers_changed;
8610 }
8611 }
8612 else if (!EQ (mini_window, selected_window))
8613 windows_or_buffers_changed++;
8614
8615 /* The current message is now also the last one displayed. */
8616 echo_area_buffer[1] = echo_area_buffer[0];
8617
8618 /* Prevent redisplay optimization in redisplay_internal by resetting
8619 this_line_start_pos. This is done because the mini-buffer now
8620 displays the message instead of its buffer text. */
8621 if (EQ (mini_window, selected_window))
8622 CHARPOS (this_line_start_pos) = 0;
8623
8624 return window_height_changed_p;
8625 }
8626
8627
8628 \f
8629 /***********************************************************************
8630 Mode Lines and Frame Titles
8631 ***********************************************************************/
8632
8633 /* A buffer for constructing non-propertized mode-line strings and
8634 frame titles in it; allocated from the heap in init_xdisp and
8635 resized as needed in store_mode_line_noprop_char. */
8636
8637 static char *mode_line_noprop_buf;
8638
8639 /* The buffer's end, and a current output position in it. */
8640
8641 static char *mode_line_noprop_buf_end;
8642 static char *mode_line_noprop_ptr;
8643
8644 #define MODE_LINE_NOPROP_LEN(start) \
8645 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
8646
8647 static enum {
8648 MODE_LINE_DISPLAY = 0,
8649 MODE_LINE_TITLE,
8650 MODE_LINE_NOPROP,
8651 MODE_LINE_STRING
8652 } mode_line_target;
8653
8654 /* Alist that caches the results of :propertize.
8655 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
8656 static Lisp_Object mode_line_proptrans_alist;
8657
8658 /* List of strings making up the mode-line. */
8659 static Lisp_Object mode_line_string_list;
8660
8661 /* Base face property when building propertized mode line string. */
8662 static Lisp_Object mode_line_string_face;
8663 static Lisp_Object mode_line_string_face_prop;
8664
8665
8666 /* Unwind data for mode line strings */
8667
8668 static Lisp_Object Vmode_line_unwind_vector;
8669
8670 static Lisp_Object
8671 format_mode_line_unwind_data (obuf, save_proptrans)
8672 struct buffer *obuf;
8673 {
8674 Lisp_Object vector;
8675
8676 /* Reduce consing by keeping one vector in
8677 Vwith_echo_area_save_vector. */
8678 vector = Vmode_line_unwind_vector;
8679 Vmode_line_unwind_vector = Qnil;
8680
8681 if (NILP (vector))
8682 vector = Fmake_vector (make_number (7), Qnil);
8683
8684 AREF (vector, 0) = make_number (mode_line_target);
8685 AREF (vector, 1) = make_number (MODE_LINE_NOPROP_LEN (0));
8686 AREF (vector, 2) = mode_line_string_list;
8687 AREF (vector, 3) = (save_proptrans ? mode_line_proptrans_alist : Qt);
8688 AREF (vector, 4) = mode_line_string_face;
8689 AREF (vector, 5) = mode_line_string_face_prop;
8690
8691 if (obuf)
8692 XSETBUFFER (AREF (vector, 6), obuf);
8693 else
8694 AREF (vector, 6) = Qnil;
8695
8696 return vector;
8697 }
8698
8699 static Lisp_Object
8700 unwind_format_mode_line (vector)
8701 Lisp_Object vector;
8702 {
8703 mode_line_target = XINT (AREF (vector, 0));
8704 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
8705 mode_line_string_list = AREF (vector, 2);
8706 if (! EQ (AREF (vector, 3), Qt))
8707 mode_line_proptrans_alist = AREF (vector, 3);
8708 mode_line_string_face = AREF (vector, 4);
8709 mode_line_string_face_prop = AREF (vector, 5);
8710
8711 if (!NILP (AREF (vector, 6)))
8712 {
8713 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
8714 AREF (vector, 6) = Qnil;
8715 }
8716
8717 Vmode_line_unwind_vector = vector;
8718 return Qnil;
8719 }
8720
8721
8722 /* Store a single character C for the frame title in mode_line_noprop_buf.
8723 Re-allocate mode_line_noprop_buf if necessary. */
8724
8725 static void
8726 #ifdef PROTOTYPES
8727 store_mode_line_noprop_char (char c)
8728 #else
8729 store_mode_line_noprop_char (c)
8730 char c;
8731 #endif
8732 {
8733 /* If output position has reached the end of the allocated buffer,
8734 double the buffer's size. */
8735 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
8736 {
8737 int len = MODE_LINE_NOPROP_LEN (0);
8738 int new_size = 2 * len * sizeof *mode_line_noprop_buf;
8739 mode_line_noprop_buf = (char *) xrealloc (mode_line_noprop_buf, new_size);
8740 mode_line_noprop_buf_end = mode_line_noprop_buf + new_size;
8741 mode_line_noprop_ptr = mode_line_noprop_buf + len;
8742 }
8743
8744 *mode_line_noprop_ptr++ = c;
8745 }
8746
8747
8748 /* Store part of a frame title in mode_line_noprop_buf, beginning at
8749 mode_line_noprop_ptr. STR is the string to store. Do not copy
8750 characters that yield more columns than PRECISION; PRECISION <= 0
8751 means copy the whole string. Pad with spaces until FIELD_WIDTH
8752 number of characters have been copied; FIELD_WIDTH <= 0 means don't
8753 pad. Called from display_mode_element when it is used to build a
8754 frame title. */
8755
8756 static int
8757 store_mode_line_noprop (str, field_width, precision)
8758 const unsigned char *str;
8759 int field_width, precision;
8760 {
8761 int n = 0;
8762 int dummy, nbytes;
8763
8764 /* Copy at most PRECISION chars from STR. */
8765 nbytes = strlen (str);
8766 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
8767 while (nbytes--)
8768 store_mode_line_noprop_char (*str++);
8769
8770 /* Fill up with spaces until FIELD_WIDTH reached. */
8771 while (field_width > 0
8772 && n < field_width)
8773 {
8774 store_mode_line_noprop_char (' ');
8775 ++n;
8776 }
8777
8778 return n;
8779 }
8780
8781 /***********************************************************************
8782 Frame Titles
8783 ***********************************************************************/
8784
8785 #ifdef HAVE_WINDOW_SYSTEM
8786
8787 /* Set the title of FRAME, if it has changed. The title format is
8788 Vicon_title_format if FRAME is iconified, otherwise it is
8789 frame_title_format. */
8790
8791 static void
8792 x_consider_frame_title (frame)
8793 Lisp_Object frame;
8794 {
8795 struct frame *f = XFRAME (frame);
8796
8797 if (FRAME_WINDOW_P (f)
8798 || FRAME_MINIBUF_ONLY_P (f)
8799 || f->explicit_name)
8800 {
8801 /* Do we have more than one visible frame on this X display? */
8802 Lisp_Object tail;
8803 Lisp_Object fmt;
8804 int title_start;
8805 char *title;
8806 int len;
8807 struct it it;
8808 int count = SPECPDL_INDEX ();
8809
8810 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
8811 {
8812 Lisp_Object other_frame = XCAR (tail);
8813 struct frame *tf = XFRAME (other_frame);
8814
8815 if (tf != f
8816 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
8817 && !FRAME_MINIBUF_ONLY_P (tf)
8818 && !EQ (other_frame, tip_frame)
8819 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
8820 break;
8821 }
8822
8823 /* Set global variable indicating that multiple frames exist. */
8824 multiple_frames = CONSP (tail);
8825
8826 /* Switch to the buffer of selected window of the frame. Set up
8827 mode_line_target so that display_mode_element will output into
8828 mode_line_noprop_buf; then display the title. */
8829 record_unwind_protect (unwind_format_mode_line,
8830 format_mode_line_unwind_data (current_buffer, 0));
8831
8832 set_buffer_internal_1 (XBUFFER (XWINDOW (f->selected_window)->buffer));
8833 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
8834
8835 mode_line_target = MODE_LINE_TITLE;
8836 title_start = MODE_LINE_NOPROP_LEN (0);
8837 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
8838 NULL, DEFAULT_FACE_ID);
8839 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
8840 len = MODE_LINE_NOPROP_LEN (title_start);
8841 title = mode_line_noprop_buf + title_start;
8842 unbind_to (count, Qnil);
8843
8844 /* Set the title only if it's changed. This avoids consing in
8845 the common case where it hasn't. (If it turns out that we've
8846 already wasted too much time by walking through the list with
8847 display_mode_element, then we might need to optimize at a
8848 higher level than this.) */
8849 if (! STRINGP (f->name)
8850 || SBYTES (f->name) != len
8851 || bcmp (title, SDATA (f->name), len) != 0)
8852 x_implicitly_set_name (f, make_string (title, len), Qnil);
8853 }
8854 }
8855
8856 #endif /* not HAVE_WINDOW_SYSTEM */
8857
8858
8859
8860 \f
8861 /***********************************************************************
8862 Menu Bars
8863 ***********************************************************************/
8864
8865
8866 /* Prepare for redisplay by updating menu-bar item lists when
8867 appropriate. This can call eval. */
8868
8869 void
8870 prepare_menu_bars ()
8871 {
8872 int all_windows;
8873 struct gcpro gcpro1, gcpro2;
8874 struct frame *f;
8875 Lisp_Object tooltip_frame;
8876
8877 #ifdef HAVE_WINDOW_SYSTEM
8878 tooltip_frame = tip_frame;
8879 #else
8880 tooltip_frame = Qnil;
8881 #endif
8882
8883 /* Update all frame titles based on their buffer names, etc. We do
8884 this before the menu bars so that the buffer-menu will show the
8885 up-to-date frame titles. */
8886 #ifdef HAVE_WINDOW_SYSTEM
8887 if (windows_or_buffers_changed || update_mode_lines)
8888 {
8889 Lisp_Object tail, frame;
8890
8891 FOR_EACH_FRAME (tail, frame)
8892 {
8893 f = XFRAME (frame);
8894 if (!EQ (frame, tooltip_frame)
8895 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
8896 x_consider_frame_title (frame);
8897 }
8898 }
8899 #endif /* HAVE_WINDOW_SYSTEM */
8900
8901 /* Update the menu bar item lists, if appropriate. This has to be
8902 done before any actual redisplay or generation of display lines. */
8903 all_windows = (update_mode_lines
8904 || buffer_shared > 1
8905 || windows_or_buffers_changed);
8906 if (all_windows)
8907 {
8908 Lisp_Object tail, frame;
8909 int count = SPECPDL_INDEX ();
8910
8911 record_unwind_save_match_data ();
8912
8913 FOR_EACH_FRAME (tail, frame)
8914 {
8915 f = XFRAME (frame);
8916
8917 /* Ignore tooltip frame. */
8918 if (EQ (frame, tooltip_frame))
8919 continue;
8920
8921 /* If a window on this frame changed size, report that to
8922 the user and clear the size-change flag. */
8923 if (FRAME_WINDOW_SIZES_CHANGED (f))
8924 {
8925 Lisp_Object functions;
8926
8927 /* Clear flag first in case we get an error below. */
8928 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
8929 functions = Vwindow_size_change_functions;
8930 GCPRO2 (tail, functions);
8931
8932 while (CONSP (functions))
8933 {
8934 call1 (XCAR (functions), frame);
8935 functions = XCDR (functions);
8936 }
8937 UNGCPRO;
8938 }
8939
8940 GCPRO1 (tail);
8941 update_menu_bar (f, 0);
8942 #ifdef HAVE_WINDOW_SYSTEM
8943 update_tool_bar (f, 0);
8944 #endif
8945 UNGCPRO;
8946 }
8947
8948 unbind_to (count, Qnil);
8949 }
8950 else
8951 {
8952 struct frame *sf = SELECTED_FRAME ();
8953 update_menu_bar (sf, 1);
8954 #ifdef HAVE_WINDOW_SYSTEM
8955 update_tool_bar (sf, 1);
8956 #endif
8957 }
8958
8959 /* Motif needs this. See comment in xmenu.c. Turn it off when
8960 pending_menu_activation is not defined. */
8961 #ifdef USE_X_TOOLKIT
8962 pending_menu_activation = 0;
8963 #endif
8964 }
8965
8966
8967 /* Update the menu bar item list for frame F. This has to be done
8968 before we start to fill in any display lines, because it can call
8969 eval.
8970
8971 If SAVE_MATCH_DATA is non-zero, we must save and restore it here. */
8972
8973 static void
8974 update_menu_bar (f, save_match_data)
8975 struct frame *f;
8976 int save_match_data;
8977 {
8978 Lisp_Object window;
8979 register struct window *w;
8980
8981 /* If called recursively during a menu update, do nothing. This can
8982 happen when, for instance, an activate-menubar-hook causes a
8983 redisplay. */
8984 if (inhibit_menubar_update)
8985 return;
8986
8987 window = FRAME_SELECTED_WINDOW (f);
8988 w = XWINDOW (window);
8989
8990 #if 0 /* The if statement below this if statement used to include the
8991 condition !NILP (w->update_mode_line), rather than using
8992 update_mode_lines directly, and this if statement may have
8993 been added to make that condition work. Now the if
8994 statement below matches its comment, this isn't needed. */
8995 if (update_mode_lines)
8996 w->update_mode_line = Qt;
8997 #endif
8998
8999 if (FRAME_WINDOW_P (f)
9000 ?
9001 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
9002 || defined (USE_GTK)
9003 FRAME_EXTERNAL_MENU_BAR (f)
9004 #else
9005 FRAME_MENU_BAR_LINES (f) > 0
9006 #endif
9007 : FRAME_MENU_BAR_LINES (f) > 0)
9008 {
9009 /* If the user has switched buffers or windows, we need to
9010 recompute to reflect the new bindings. But we'll
9011 recompute when update_mode_lines is set too; that means
9012 that people can use force-mode-line-update to request
9013 that the menu bar be recomputed. The adverse effect on
9014 the rest of the redisplay algorithm is about the same as
9015 windows_or_buffers_changed anyway. */
9016 if (windows_or_buffers_changed
9017 /* This used to test w->update_mode_line, but we believe
9018 there is no need to recompute the menu in that case. */
9019 || update_mode_lines
9020 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
9021 < BUF_MODIFF (XBUFFER (w->buffer)))
9022 != !NILP (w->last_had_star))
9023 || ((!NILP (Vtransient_mark_mode)
9024 && !NILP (XBUFFER (w->buffer)->mark_active))
9025 != !NILP (w->region_showing)))
9026 {
9027 struct buffer *prev = current_buffer;
9028 int count = SPECPDL_INDEX ();
9029
9030 specbind (Qinhibit_menubar_update, Qt);
9031
9032 set_buffer_internal_1 (XBUFFER (w->buffer));
9033 if (save_match_data)
9034 record_unwind_save_match_data ();
9035 if (NILP (Voverriding_local_map_menu_flag))
9036 {
9037 specbind (Qoverriding_terminal_local_map, Qnil);
9038 specbind (Qoverriding_local_map, Qnil);
9039 }
9040
9041 /* Run the Lucid hook. */
9042 safe_run_hooks (Qactivate_menubar_hook);
9043
9044 /* If it has changed current-menubar from previous value,
9045 really recompute the menu-bar from the value. */
9046 if (! NILP (Vlucid_menu_bar_dirty_flag))
9047 call0 (Qrecompute_lucid_menubar);
9048
9049 safe_run_hooks (Qmenu_bar_update_hook);
9050 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
9051
9052 /* Redisplay the menu bar in case we changed it. */
9053 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
9054 || defined (USE_GTK)
9055 if (FRAME_WINDOW_P (f)
9056 #if defined (MAC_OS)
9057 /* All frames on Mac OS share the same menubar. So only the
9058 selected frame should be allowed to set it. */
9059 && f == SELECTED_FRAME ()
9060 #endif
9061 )
9062 set_frame_menubar (f, 0, 0);
9063 else
9064 /* On a terminal screen, the menu bar is an ordinary screen
9065 line, and this makes it get updated. */
9066 w->update_mode_line = Qt;
9067 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || MAC_OS || USE_GTK) */
9068 /* In the non-toolkit version, the menu bar is an ordinary screen
9069 line, and this makes it get updated. */
9070 w->update_mode_line = Qt;
9071 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || MAC_OS || USE_GTK) */
9072
9073 unbind_to (count, Qnil);
9074 set_buffer_internal_1 (prev);
9075 }
9076 }
9077 }
9078
9079
9080 \f
9081 /***********************************************************************
9082 Output Cursor
9083 ***********************************************************************/
9084
9085 #ifdef HAVE_WINDOW_SYSTEM
9086
9087 /* EXPORT:
9088 Nominal cursor position -- where to draw output.
9089 HPOS and VPOS are window relative glyph matrix coordinates.
9090 X and Y are window relative pixel coordinates. */
9091
9092 struct cursor_pos output_cursor;
9093
9094
9095 /* EXPORT:
9096 Set the global variable output_cursor to CURSOR. All cursor
9097 positions are relative to updated_window. */
9098
9099 void
9100 set_output_cursor (cursor)
9101 struct cursor_pos *cursor;
9102 {
9103 output_cursor.hpos = cursor->hpos;
9104 output_cursor.vpos = cursor->vpos;
9105 output_cursor.x = cursor->x;
9106 output_cursor.y = cursor->y;
9107 }
9108
9109
9110 /* EXPORT for RIF:
9111 Set a nominal cursor position.
9112
9113 HPOS and VPOS are column/row positions in a window glyph matrix. X
9114 and Y are window text area relative pixel positions.
9115
9116 If this is done during an update, updated_window will contain the
9117 window that is being updated and the position is the future output
9118 cursor position for that window. If updated_window is null, use
9119 selected_window and display the cursor at the given position. */
9120
9121 void
9122 x_cursor_to (vpos, hpos, y, x)
9123 int vpos, hpos, y, x;
9124 {
9125 struct window *w;
9126
9127 /* If updated_window is not set, work on selected_window. */
9128 if (updated_window)
9129 w = updated_window;
9130 else
9131 w = XWINDOW (selected_window);
9132
9133 /* Set the output cursor. */
9134 output_cursor.hpos = hpos;
9135 output_cursor.vpos = vpos;
9136 output_cursor.x = x;
9137 output_cursor.y = y;
9138
9139 /* If not called as part of an update, really display the cursor.
9140 This will also set the cursor position of W. */
9141 if (updated_window == NULL)
9142 {
9143 BLOCK_INPUT;
9144 display_and_set_cursor (w, 1, hpos, vpos, x, y);
9145 if (rif->flush_display_optional)
9146 rif->flush_display_optional (SELECTED_FRAME ());
9147 UNBLOCK_INPUT;
9148 }
9149 }
9150
9151 #endif /* HAVE_WINDOW_SYSTEM */
9152
9153 \f
9154 /***********************************************************************
9155 Tool-bars
9156 ***********************************************************************/
9157
9158 #ifdef HAVE_WINDOW_SYSTEM
9159
9160 /* Where the mouse was last time we reported a mouse event. */
9161
9162 FRAME_PTR last_mouse_frame;
9163
9164 /* Tool-bar item index of the item on which a mouse button was pressed
9165 or -1. */
9166
9167 int last_tool_bar_item;
9168
9169
9170 /* Update the tool-bar item list for frame F. This has to be done
9171 before we start to fill in any display lines. Called from
9172 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
9173 and restore it here. */
9174
9175 static void
9176 update_tool_bar (f, save_match_data)
9177 struct frame *f;
9178 int save_match_data;
9179 {
9180 #ifdef USE_GTK
9181 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
9182 #else
9183 int do_update = WINDOWP (f->tool_bar_window)
9184 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0;
9185 #endif
9186
9187 if (do_update)
9188 {
9189 Lisp_Object window;
9190 struct window *w;
9191
9192 window = FRAME_SELECTED_WINDOW (f);
9193 w = XWINDOW (window);
9194
9195 /* If the user has switched buffers or windows, we need to
9196 recompute to reflect the new bindings. But we'll
9197 recompute when update_mode_lines is set too; that means
9198 that people can use force-mode-line-update to request
9199 that the menu bar be recomputed. The adverse effect on
9200 the rest of the redisplay algorithm is about the same as
9201 windows_or_buffers_changed anyway. */
9202 if (windows_or_buffers_changed
9203 || !NILP (w->update_mode_line)
9204 || update_mode_lines
9205 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
9206 < BUF_MODIFF (XBUFFER (w->buffer)))
9207 != !NILP (w->last_had_star))
9208 || ((!NILP (Vtransient_mark_mode)
9209 && !NILP (XBUFFER (w->buffer)->mark_active))
9210 != !NILP (w->region_showing)))
9211 {
9212 struct buffer *prev = current_buffer;
9213 int count = SPECPDL_INDEX ();
9214 Lisp_Object new_tool_bar;
9215 int new_n_tool_bar;
9216 struct gcpro gcpro1;
9217
9218 /* Set current_buffer to the buffer of the selected
9219 window of the frame, so that we get the right local
9220 keymaps. */
9221 set_buffer_internal_1 (XBUFFER (w->buffer));
9222
9223 /* Save match data, if we must. */
9224 if (save_match_data)
9225 record_unwind_save_match_data ();
9226
9227 /* Make sure that we don't accidentally use bogus keymaps. */
9228 if (NILP (Voverriding_local_map_menu_flag))
9229 {
9230 specbind (Qoverriding_terminal_local_map, Qnil);
9231 specbind (Qoverriding_local_map, Qnil);
9232 }
9233
9234 GCPRO1 (new_tool_bar);
9235
9236 /* Build desired tool-bar items from keymaps. */
9237 new_tool_bar = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
9238 &new_n_tool_bar);
9239
9240 /* Redisplay the tool-bar if we changed it. */
9241 if (NILP (Fequal (new_tool_bar, f->tool_bar_items)))
9242 {
9243 /* Redisplay that happens asynchronously due to an expose event
9244 may access f->tool_bar_items. Make sure we update both
9245 variables within BLOCK_INPUT so no such event interrupts. */
9246 BLOCK_INPUT;
9247 f->tool_bar_items = new_tool_bar;
9248 f->n_tool_bar_items = new_n_tool_bar;
9249 w->update_mode_line = Qt;
9250 UNBLOCK_INPUT;
9251 }
9252
9253 UNGCPRO;
9254
9255 unbind_to (count, Qnil);
9256 set_buffer_internal_1 (prev);
9257 }
9258 }
9259 }
9260
9261
9262 /* Set F->desired_tool_bar_string to a Lisp string representing frame
9263 F's desired tool-bar contents. F->tool_bar_items must have
9264 been set up previously by calling prepare_menu_bars. */
9265
9266 static void
9267 build_desired_tool_bar_string (f)
9268 struct frame *f;
9269 {
9270 int i, size, size_needed;
9271 struct gcpro gcpro1, gcpro2, gcpro3;
9272 Lisp_Object image, plist, props;
9273
9274 image = plist = props = Qnil;
9275 GCPRO3 (image, plist, props);
9276
9277 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
9278 Otherwise, make a new string. */
9279
9280 /* The size of the string we might be able to reuse. */
9281 size = (STRINGP (f->desired_tool_bar_string)
9282 ? SCHARS (f->desired_tool_bar_string)
9283 : 0);
9284
9285 /* We need one space in the string for each image. */
9286 size_needed = f->n_tool_bar_items;
9287
9288 /* Reuse f->desired_tool_bar_string, if possible. */
9289 if (size < size_needed || NILP (f->desired_tool_bar_string))
9290 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
9291 make_number (' '));
9292 else
9293 {
9294 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
9295 Fremove_text_properties (make_number (0), make_number (size),
9296 props, f->desired_tool_bar_string);
9297 }
9298
9299 /* Put a `display' property on the string for the images to display,
9300 put a `menu_item' property on tool-bar items with a value that
9301 is the index of the item in F's tool-bar item vector. */
9302 for (i = 0; i < f->n_tool_bar_items; ++i)
9303 {
9304 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
9305
9306 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
9307 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
9308 int hmargin, vmargin, relief, idx, end;
9309 extern Lisp_Object QCrelief, QCmargin, QCconversion;
9310
9311 /* If image is a vector, choose the image according to the
9312 button state. */
9313 image = PROP (TOOL_BAR_ITEM_IMAGES);
9314 if (VECTORP (image))
9315 {
9316 if (enabled_p)
9317 idx = (selected_p
9318 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
9319 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
9320 else
9321 idx = (selected_p
9322 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
9323 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
9324
9325 xassert (ASIZE (image) >= idx);
9326 image = AREF (image, idx);
9327 }
9328 else
9329 idx = -1;
9330
9331 /* Ignore invalid image specifications. */
9332 if (!valid_image_p (image))
9333 continue;
9334
9335 /* Display the tool-bar button pressed, or depressed. */
9336 plist = Fcopy_sequence (XCDR (image));
9337
9338 /* Compute margin and relief to draw. */
9339 relief = (tool_bar_button_relief >= 0
9340 ? tool_bar_button_relief
9341 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
9342 hmargin = vmargin = relief;
9343
9344 if (INTEGERP (Vtool_bar_button_margin)
9345 && XINT (Vtool_bar_button_margin) > 0)
9346 {
9347 hmargin += XFASTINT (Vtool_bar_button_margin);
9348 vmargin += XFASTINT (Vtool_bar_button_margin);
9349 }
9350 else if (CONSP (Vtool_bar_button_margin))
9351 {
9352 if (INTEGERP (XCAR (Vtool_bar_button_margin))
9353 && XINT (XCAR (Vtool_bar_button_margin)) > 0)
9354 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
9355
9356 if (INTEGERP (XCDR (Vtool_bar_button_margin))
9357 && XINT (XCDR (Vtool_bar_button_margin)) > 0)
9358 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
9359 }
9360
9361 if (auto_raise_tool_bar_buttons_p)
9362 {
9363 /* Add a `:relief' property to the image spec if the item is
9364 selected. */
9365 if (selected_p)
9366 {
9367 plist = Fplist_put (plist, QCrelief, make_number (-relief));
9368 hmargin -= relief;
9369 vmargin -= relief;
9370 }
9371 }
9372 else
9373 {
9374 /* If image is selected, display it pressed, i.e. with a
9375 negative relief. If it's not selected, display it with a
9376 raised relief. */
9377 plist = Fplist_put (plist, QCrelief,
9378 (selected_p
9379 ? make_number (-relief)
9380 : make_number (relief)));
9381 hmargin -= relief;
9382 vmargin -= relief;
9383 }
9384
9385 /* Put a margin around the image. */
9386 if (hmargin || vmargin)
9387 {
9388 if (hmargin == vmargin)
9389 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
9390 else
9391 plist = Fplist_put (plist, QCmargin,
9392 Fcons (make_number (hmargin),
9393 make_number (vmargin)));
9394 }
9395
9396 /* If button is not enabled, and we don't have special images
9397 for the disabled state, make the image appear disabled by
9398 applying an appropriate algorithm to it. */
9399 if (!enabled_p && idx < 0)
9400 plist = Fplist_put (plist, QCconversion, Qdisabled);
9401
9402 /* Put a `display' text property on the string for the image to
9403 display. Put a `menu-item' property on the string that gives
9404 the start of this item's properties in the tool-bar items
9405 vector. */
9406 image = Fcons (Qimage, plist);
9407 props = list4 (Qdisplay, image,
9408 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
9409
9410 /* Let the last image hide all remaining spaces in the tool bar
9411 string. The string can be longer than needed when we reuse a
9412 previous string. */
9413 if (i + 1 == f->n_tool_bar_items)
9414 end = SCHARS (f->desired_tool_bar_string);
9415 else
9416 end = i + 1;
9417 Fadd_text_properties (make_number (i), make_number (end),
9418 props, f->desired_tool_bar_string);
9419 #undef PROP
9420 }
9421
9422 UNGCPRO;
9423 }
9424
9425
9426 /* Display one line of the tool-bar of frame IT->f. */
9427
9428 static void
9429 display_tool_bar_line (it)
9430 struct it *it;
9431 {
9432 struct glyph_row *row = it->glyph_row;
9433 int max_x = it->last_visible_x;
9434 struct glyph *last;
9435
9436 prepare_desired_row (row);
9437 row->y = it->current_y;
9438
9439 /* Note that this isn't made use of if the face hasn't a box,
9440 so there's no need to check the face here. */
9441 it->start_of_box_run_p = 1;
9442
9443 while (it->current_x < max_x)
9444 {
9445 int x_before, x, n_glyphs_before, i, nglyphs;
9446
9447 /* Get the next display element. */
9448 if (!get_next_display_element (it))
9449 break;
9450
9451 /* Produce glyphs. */
9452 x_before = it->current_x;
9453 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
9454 PRODUCE_GLYPHS (it);
9455
9456 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
9457 i = 0;
9458 x = x_before;
9459 while (i < nglyphs)
9460 {
9461 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
9462
9463 if (x + glyph->pixel_width > max_x)
9464 {
9465 /* Glyph doesn't fit on line. */
9466 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
9467 it->current_x = x;
9468 goto out;
9469 }
9470
9471 ++it->hpos;
9472 x += glyph->pixel_width;
9473 ++i;
9474 }
9475
9476 /* Stop at line ends. */
9477 if (ITERATOR_AT_END_OF_LINE_P (it))
9478 break;
9479
9480 set_iterator_to_next (it, 1);
9481 }
9482
9483 out:;
9484
9485 row->displays_text_p = row->used[TEXT_AREA] != 0;
9486 extend_face_to_end_of_line (it);
9487 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
9488 last->right_box_line_p = 1;
9489 if (last == row->glyphs[TEXT_AREA])
9490 last->left_box_line_p = 1;
9491 compute_line_metrics (it);
9492
9493 /* If line is empty, make it occupy the rest of the tool-bar. */
9494 if (!row->displays_text_p)
9495 {
9496 row->height = row->phys_height = it->last_visible_y - row->y;
9497 row->ascent = row->phys_ascent = 0;
9498 row->extra_line_spacing = 0;
9499 }
9500
9501 row->full_width_p = 1;
9502 row->continued_p = 0;
9503 row->truncated_on_left_p = 0;
9504 row->truncated_on_right_p = 0;
9505
9506 it->current_x = it->hpos = 0;
9507 it->current_y += row->height;
9508 ++it->vpos;
9509 ++it->glyph_row;
9510 }
9511
9512
9513 /* Value is the number of screen lines needed to make all tool-bar
9514 items of frame F visible. */
9515
9516 static int
9517 tool_bar_lines_needed (f)
9518 struct frame *f;
9519 {
9520 struct window *w = XWINDOW (f->tool_bar_window);
9521 struct it it;
9522
9523 /* Initialize an iterator for iteration over
9524 F->desired_tool_bar_string in the tool-bar window of frame F. */
9525 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
9526 it.first_visible_x = 0;
9527 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
9528 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
9529
9530 while (!ITERATOR_AT_END_P (&it))
9531 {
9532 it.glyph_row = w->desired_matrix->rows;
9533 clear_glyph_row (it.glyph_row);
9534 display_tool_bar_line (&it);
9535 }
9536
9537 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
9538 }
9539
9540
9541 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
9542 0, 1, 0,
9543 doc: /* Return the number of lines occupied by the tool bar of FRAME. */)
9544 (frame)
9545 Lisp_Object frame;
9546 {
9547 struct frame *f;
9548 struct window *w;
9549 int nlines = 0;
9550
9551 if (NILP (frame))
9552 frame = selected_frame;
9553 else
9554 CHECK_FRAME (frame);
9555 f = XFRAME (frame);
9556
9557 if (WINDOWP (f->tool_bar_window)
9558 || (w = XWINDOW (f->tool_bar_window),
9559 WINDOW_TOTAL_LINES (w) > 0))
9560 {
9561 update_tool_bar (f, 1);
9562 if (f->n_tool_bar_items)
9563 {
9564 build_desired_tool_bar_string (f);
9565 nlines = tool_bar_lines_needed (f);
9566 }
9567 }
9568
9569 return make_number (nlines);
9570 }
9571
9572
9573 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
9574 height should be changed. */
9575
9576 static int
9577 redisplay_tool_bar (f)
9578 struct frame *f;
9579 {
9580 struct window *w;
9581 struct it it;
9582 struct glyph_row *row;
9583 int change_height_p = 0;
9584
9585 #ifdef USE_GTK
9586 if (FRAME_EXTERNAL_TOOL_BAR (f))
9587 update_frame_tool_bar (f);
9588 return 0;
9589 #endif
9590
9591 /* If frame hasn't a tool-bar window or if it is zero-height, don't
9592 do anything. This means you must start with tool-bar-lines
9593 non-zero to get the auto-sizing effect. Or in other words, you
9594 can turn off tool-bars by specifying tool-bar-lines zero. */
9595 if (!WINDOWP (f->tool_bar_window)
9596 || (w = XWINDOW (f->tool_bar_window),
9597 WINDOW_TOTAL_LINES (w) == 0))
9598 return 0;
9599
9600 /* Set up an iterator for the tool-bar window. */
9601 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
9602 it.first_visible_x = 0;
9603 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
9604 row = it.glyph_row;
9605
9606 /* Build a string that represents the contents of the tool-bar. */
9607 build_desired_tool_bar_string (f);
9608 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
9609
9610 /* Display as many lines as needed to display all tool-bar items. */
9611 while (it.current_y < it.last_visible_y)
9612 display_tool_bar_line (&it);
9613
9614 /* It doesn't make much sense to try scrolling in the tool-bar
9615 window, so don't do it. */
9616 w->desired_matrix->no_scrolling_p = 1;
9617 w->must_be_updated_p = 1;
9618
9619 if (auto_resize_tool_bars_p)
9620 {
9621 int nlines;
9622
9623 /* If we couldn't display everything, change the tool-bar's
9624 height. */
9625 if (IT_STRING_CHARPOS (it) < it.end_charpos)
9626 change_height_p = 1;
9627
9628 /* If there are blank lines at the end, except for a partially
9629 visible blank line at the end that is smaller than
9630 FRAME_LINE_HEIGHT, change the tool-bar's height. */
9631 row = it.glyph_row - 1;
9632 if (!row->displays_text_p
9633 && row->height >= FRAME_LINE_HEIGHT (f))
9634 change_height_p = 1;
9635
9636 /* If row displays tool-bar items, but is partially visible,
9637 change the tool-bar's height. */
9638 if (row->displays_text_p
9639 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y)
9640 change_height_p = 1;
9641
9642 /* Resize windows as needed by changing the `tool-bar-lines'
9643 frame parameter. */
9644 if (change_height_p
9645 && (nlines = tool_bar_lines_needed (f),
9646 nlines != WINDOW_TOTAL_LINES (w)))
9647 {
9648 extern Lisp_Object Qtool_bar_lines;
9649 Lisp_Object frame;
9650 int old_height = WINDOW_TOTAL_LINES (w);
9651
9652 XSETFRAME (frame, f);
9653 clear_glyph_matrix (w->desired_matrix);
9654 Fmodify_frame_parameters (frame,
9655 Fcons (Fcons (Qtool_bar_lines,
9656 make_number (nlines)),
9657 Qnil));
9658 if (WINDOW_TOTAL_LINES (w) != old_height)
9659 fonts_changed_p = 1;
9660 }
9661 }
9662
9663 return change_height_p;
9664 }
9665
9666
9667 /* Get information about the tool-bar item which is displayed in GLYPH
9668 on frame F. Return in *PROP_IDX the index where tool-bar item
9669 properties start in F->tool_bar_items. Value is zero if
9670 GLYPH doesn't display a tool-bar item. */
9671
9672 static int
9673 tool_bar_item_info (f, glyph, prop_idx)
9674 struct frame *f;
9675 struct glyph *glyph;
9676 int *prop_idx;
9677 {
9678 Lisp_Object prop;
9679 int success_p;
9680 int charpos;
9681
9682 /* This function can be called asynchronously, which means we must
9683 exclude any possibility that Fget_text_property signals an
9684 error. */
9685 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
9686 charpos = max (0, charpos);
9687
9688 /* Get the text property `menu-item' at pos. The value of that
9689 property is the start index of this item's properties in
9690 F->tool_bar_items. */
9691 prop = Fget_text_property (make_number (charpos),
9692 Qmenu_item, f->current_tool_bar_string);
9693 if (INTEGERP (prop))
9694 {
9695 *prop_idx = XINT (prop);
9696 success_p = 1;
9697 }
9698 else
9699 success_p = 0;
9700
9701 return success_p;
9702 }
9703
9704 \f
9705 /* Get information about the tool-bar item at position X/Y on frame F.
9706 Return in *GLYPH a pointer to the glyph of the tool-bar item in
9707 the current matrix of the tool-bar window of F, or NULL if not
9708 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
9709 item in F->tool_bar_items. Value is
9710
9711 -1 if X/Y is not on a tool-bar item
9712 0 if X/Y is on the same item that was highlighted before.
9713 1 otherwise. */
9714
9715 static int
9716 get_tool_bar_item (f, x, y, glyph, hpos, vpos, prop_idx)
9717 struct frame *f;
9718 int x, y;
9719 struct glyph **glyph;
9720 int *hpos, *vpos, *prop_idx;
9721 {
9722 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9723 struct window *w = XWINDOW (f->tool_bar_window);
9724 int area;
9725
9726 /* Find the glyph under X/Y. */
9727 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
9728 if (*glyph == NULL)
9729 return -1;
9730
9731 /* Get the start of this tool-bar item's properties in
9732 f->tool_bar_items. */
9733 if (!tool_bar_item_info (f, *glyph, prop_idx))
9734 return -1;
9735
9736 /* Is mouse on the highlighted item? */
9737 if (EQ (f->tool_bar_window, dpyinfo->mouse_face_window)
9738 && *vpos >= dpyinfo->mouse_face_beg_row
9739 && *vpos <= dpyinfo->mouse_face_end_row
9740 && (*vpos > dpyinfo->mouse_face_beg_row
9741 || *hpos >= dpyinfo->mouse_face_beg_col)
9742 && (*vpos < dpyinfo->mouse_face_end_row
9743 || *hpos < dpyinfo->mouse_face_end_col
9744 || dpyinfo->mouse_face_past_end))
9745 return 0;
9746
9747 return 1;
9748 }
9749
9750
9751 /* EXPORT:
9752 Handle mouse button event on the tool-bar of frame F, at
9753 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
9754 0 for button release. MODIFIERS is event modifiers for button
9755 release. */
9756
9757 void
9758 handle_tool_bar_click (f, x, y, down_p, modifiers)
9759 struct frame *f;
9760 int x, y, down_p;
9761 unsigned int modifiers;
9762 {
9763 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9764 struct window *w = XWINDOW (f->tool_bar_window);
9765 int hpos, vpos, prop_idx;
9766 struct glyph *glyph;
9767 Lisp_Object enabled_p;
9768
9769 /* If not on the highlighted tool-bar item, return. */
9770 frame_to_window_pixel_xy (w, &x, &y);
9771 if (get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
9772 return;
9773
9774 /* If item is disabled, do nothing. */
9775 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
9776 if (NILP (enabled_p))
9777 return;
9778
9779 if (down_p)
9780 {
9781 /* Show item in pressed state. */
9782 show_mouse_face (dpyinfo, DRAW_IMAGE_SUNKEN);
9783 dpyinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
9784 last_tool_bar_item = prop_idx;
9785 }
9786 else
9787 {
9788 Lisp_Object key, frame;
9789 struct input_event event;
9790 EVENT_INIT (event);
9791
9792 /* Show item in released state. */
9793 show_mouse_face (dpyinfo, DRAW_IMAGE_RAISED);
9794 dpyinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
9795
9796 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
9797
9798 XSETFRAME (frame, f);
9799 event.kind = TOOL_BAR_EVENT;
9800 event.frame_or_window = frame;
9801 event.arg = frame;
9802 kbd_buffer_store_event (&event);
9803
9804 event.kind = TOOL_BAR_EVENT;
9805 event.frame_or_window = frame;
9806 event.arg = key;
9807 event.modifiers = modifiers;
9808 kbd_buffer_store_event (&event);
9809 last_tool_bar_item = -1;
9810 }
9811 }
9812
9813
9814 /* Possibly highlight a tool-bar item on frame F when mouse moves to
9815 tool-bar window-relative coordinates X/Y. Called from
9816 note_mouse_highlight. */
9817
9818 static void
9819 note_tool_bar_highlight (f, x, y)
9820 struct frame *f;
9821 int x, y;
9822 {
9823 Lisp_Object window = f->tool_bar_window;
9824 struct window *w = XWINDOW (window);
9825 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9826 int hpos, vpos;
9827 struct glyph *glyph;
9828 struct glyph_row *row;
9829 int i;
9830 Lisp_Object enabled_p;
9831 int prop_idx;
9832 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
9833 int mouse_down_p, rc;
9834
9835 /* Function note_mouse_highlight is called with negative x(y
9836 values when mouse moves outside of the frame. */
9837 if (x <= 0 || y <= 0)
9838 {
9839 clear_mouse_face (dpyinfo);
9840 return;
9841 }
9842
9843 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
9844 if (rc < 0)
9845 {
9846 /* Not on tool-bar item. */
9847 clear_mouse_face (dpyinfo);
9848 return;
9849 }
9850 else if (rc == 0)
9851 /* On same tool-bar item as before. */
9852 goto set_help_echo;
9853
9854 clear_mouse_face (dpyinfo);
9855
9856 /* Mouse is down, but on different tool-bar item? */
9857 mouse_down_p = (dpyinfo->grabbed
9858 && f == last_mouse_frame
9859 && FRAME_LIVE_P (f));
9860 if (mouse_down_p
9861 && last_tool_bar_item != prop_idx)
9862 return;
9863
9864 dpyinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
9865 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
9866
9867 /* If tool-bar item is not enabled, don't highlight it. */
9868 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
9869 if (!NILP (enabled_p))
9870 {
9871 /* Compute the x-position of the glyph. In front and past the
9872 image is a space. We include this in the highlighted area. */
9873 row = MATRIX_ROW (w->current_matrix, vpos);
9874 for (i = x = 0; i < hpos; ++i)
9875 x += row->glyphs[TEXT_AREA][i].pixel_width;
9876
9877 /* Record this as the current active region. */
9878 dpyinfo->mouse_face_beg_col = hpos;
9879 dpyinfo->mouse_face_beg_row = vpos;
9880 dpyinfo->mouse_face_beg_x = x;
9881 dpyinfo->mouse_face_beg_y = row->y;
9882 dpyinfo->mouse_face_past_end = 0;
9883
9884 dpyinfo->mouse_face_end_col = hpos + 1;
9885 dpyinfo->mouse_face_end_row = vpos;
9886 dpyinfo->mouse_face_end_x = x + glyph->pixel_width;
9887 dpyinfo->mouse_face_end_y = row->y;
9888 dpyinfo->mouse_face_window = window;
9889 dpyinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
9890
9891 /* Display it as active. */
9892 show_mouse_face (dpyinfo, draw);
9893 dpyinfo->mouse_face_image_state = draw;
9894 }
9895
9896 set_help_echo:
9897
9898 /* Set help_echo_string to a help string to display for this tool-bar item.
9899 XTread_socket does the rest. */
9900 help_echo_object = help_echo_window = Qnil;
9901 help_echo_pos = -1;
9902 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
9903 if (NILP (help_echo_string))
9904 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
9905 }
9906
9907 #endif /* HAVE_WINDOW_SYSTEM */
9908
9909
9910 \f
9911 /************************************************************************
9912 Horizontal scrolling
9913 ************************************************************************/
9914
9915 static int hscroll_window_tree P_ ((Lisp_Object));
9916 static int hscroll_windows P_ ((Lisp_Object));
9917
9918 /* For all leaf windows in the window tree rooted at WINDOW, set their
9919 hscroll value so that PT is (i) visible in the window, and (ii) so
9920 that it is not within a certain margin at the window's left and
9921 right border. Value is non-zero if any window's hscroll has been
9922 changed. */
9923
9924 static int
9925 hscroll_window_tree (window)
9926 Lisp_Object window;
9927 {
9928 int hscrolled_p = 0;
9929 int hscroll_relative_p = FLOATP (Vhscroll_step);
9930 int hscroll_step_abs = 0;
9931 double hscroll_step_rel = 0;
9932
9933 if (hscroll_relative_p)
9934 {
9935 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
9936 if (hscroll_step_rel < 0)
9937 {
9938 hscroll_relative_p = 0;
9939 hscroll_step_abs = 0;
9940 }
9941 }
9942 else if (INTEGERP (Vhscroll_step))
9943 {
9944 hscroll_step_abs = XINT (Vhscroll_step);
9945 if (hscroll_step_abs < 0)
9946 hscroll_step_abs = 0;
9947 }
9948 else
9949 hscroll_step_abs = 0;
9950
9951 while (WINDOWP (window))
9952 {
9953 struct window *w = XWINDOW (window);
9954
9955 if (WINDOWP (w->hchild))
9956 hscrolled_p |= hscroll_window_tree (w->hchild);
9957 else if (WINDOWP (w->vchild))
9958 hscrolled_p |= hscroll_window_tree (w->vchild);
9959 else if (w->cursor.vpos >= 0)
9960 {
9961 int h_margin;
9962 int text_area_width;
9963 struct glyph_row *current_cursor_row
9964 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
9965 struct glyph_row *desired_cursor_row
9966 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
9967 struct glyph_row *cursor_row
9968 = (desired_cursor_row->enabled_p
9969 ? desired_cursor_row
9970 : current_cursor_row);
9971
9972 text_area_width = window_box_width (w, TEXT_AREA);
9973
9974 /* Scroll when cursor is inside this scroll margin. */
9975 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
9976
9977 if ((XFASTINT (w->hscroll)
9978 && w->cursor.x <= h_margin)
9979 || (cursor_row->enabled_p
9980 && cursor_row->truncated_on_right_p
9981 && (w->cursor.x >= text_area_width - h_margin)))
9982 {
9983 struct it it;
9984 int hscroll;
9985 struct buffer *saved_current_buffer;
9986 int pt;
9987 int wanted_x;
9988
9989 /* Find point in a display of infinite width. */
9990 saved_current_buffer = current_buffer;
9991 current_buffer = XBUFFER (w->buffer);
9992
9993 if (w == XWINDOW (selected_window))
9994 pt = BUF_PT (current_buffer);
9995 else
9996 {
9997 pt = marker_position (w->pointm);
9998 pt = max (BEGV, pt);
9999 pt = min (ZV, pt);
10000 }
10001
10002 /* Move iterator to pt starting at cursor_row->start in
10003 a line with infinite width. */
10004 init_to_row_start (&it, w, cursor_row);
10005 it.last_visible_x = INFINITY;
10006 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
10007 current_buffer = saved_current_buffer;
10008
10009 /* Position cursor in window. */
10010 if (!hscroll_relative_p && hscroll_step_abs == 0)
10011 hscroll = max (0, (it.current_x
10012 - (ITERATOR_AT_END_OF_LINE_P (&it)
10013 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
10014 : (text_area_width / 2))))
10015 / FRAME_COLUMN_WIDTH (it.f);
10016 else if (w->cursor.x >= text_area_width - h_margin)
10017 {
10018 if (hscroll_relative_p)
10019 wanted_x = text_area_width * (1 - hscroll_step_rel)
10020 - h_margin;
10021 else
10022 wanted_x = text_area_width
10023 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
10024 - h_margin;
10025 hscroll
10026 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
10027 }
10028 else
10029 {
10030 if (hscroll_relative_p)
10031 wanted_x = text_area_width * hscroll_step_rel
10032 + h_margin;
10033 else
10034 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
10035 + h_margin;
10036 hscroll
10037 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
10038 }
10039 hscroll = max (hscroll, XFASTINT (w->min_hscroll));
10040
10041 /* Don't call Fset_window_hscroll if value hasn't
10042 changed because it will prevent redisplay
10043 optimizations. */
10044 if (XFASTINT (w->hscroll) != hscroll)
10045 {
10046 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
10047 w->hscroll = make_number (hscroll);
10048 hscrolled_p = 1;
10049 }
10050 }
10051 }
10052
10053 window = w->next;
10054 }
10055
10056 /* Value is non-zero if hscroll of any leaf window has been changed. */
10057 return hscrolled_p;
10058 }
10059
10060
10061 /* Set hscroll so that cursor is visible and not inside horizontal
10062 scroll margins for all windows in the tree rooted at WINDOW. See
10063 also hscroll_window_tree above. Value is non-zero if any window's
10064 hscroll has been changed. If it has, desired matrices on the frame
10065 of WINDOW are cleared. */
10066
10067 static int
10068 hscroll_windows (window)
10069 Lisp_Object window;
10070 {
10071 int hscrolled_p;
10072
10073 if (automatic_hscrolling_p)
10074 {
10075 hscrolled_p = hscroll_window_tree (window);
10076 if (hscrolled_p)
10077 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
10078 }
10079 else
10080 hscrolled_p = 0;
10081 return hscrolled_p;
10082 }
10083
10084
10085 \f
10086 /************************************************************************
10087 Redisplay
10088 ************************************************************************/
10089
10090 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
10091 to a non-zero value. This is sometimes handy to have in a debugger
10092 session. */
10093
10094 #if GLYPH_DEBUG
10095
10096 /* First and last unchanged row for try_window_id. */
10097
10098 int debug_first_unchanged_at_end_vpos;
10099 int debug_last_unchanged_at_beg_vpos;
10100
10101 /* Delta vpos and y. */
10102
10103 int debug_dvpos, debug_dy;
10104
10105 /* Delta in characters and bytes for try_window_id. */
10106
10107 int debug_delta, debug_delta_bytes;
10108
10109 /* Values of window_end_pos and window_end_vpos at the end of
10110 try_window_id. */
10111
10112 EMACS_INT debug_end_pos, debug_end_vpos;
10113
10114 /* Append a string to W->desired_matrix->method. FMT is a printf
10115 format string. A1...A9 are a supplement for a variable-length
10116 argument list. If trace_redisplay_p is non-zero also printf the
10117 resulting string to stderr. */
10118
10119 static void
10120 debug_method_add (w, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9)
10121 struct window *w;
10122 char *fmt;
10123 int a1, a2, a3, a4, a5, a6, a7, a8, a9;
10124 {
10125 char buffer[512];
10126 char *method = w->desired_matrix->method;
10127 int len = strlen (method);
10128 int size = sizeof w->desired_matrix->method;
10129 int remaining = size - len - 1;
10130
10131 sprintf (buffer, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9);
10132 if (len && remaining)
10133 {
10134 method[len] = '|';
10135 --remaining, ++len;
10136 }
10137
10138 strncpy (method + len, buffer, remaining);
10139
10140 if (trace_redisplay_p)
10141 fprintf (stderr, "%p (%s): %s\n",
10142 w,
10143 ((BUFFERP (w->buffer)
10144 && STRINGP (XBUFFER (w->buffer)->name))
10145 ? (char *) SDATA (XBUFFER (w->buffer)->name)
10146 : "no buffer"),
10147 buffer);
10148 }
10149
10150 #endif /* GLYPH_DEBUG */
10151
10152
10153 /* Value is non-zero if all changes in window W, which displays
10154 current_buffer, are in the text between START and END. START is a
10155 buffer position, END is given as a distance from Z. Used in
10156 redisplay_internal for display optimization. */
10157
10158 static INLINE int
10159 text_outside_line_unchanged_p (w, start, end)
10160 struct window *w;
10161 int start, end;
10162 {
10163 int unchanged_p = 1;
10164
10165 /* If text or overlays have changed, see where. */
10166 if (XFASTINT (w->last_modified) < MODIFF
10167 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
10168 {
10169 /* Gap in the line? */
10170 if (GPT < start || Z - GPT < end)
10171 unchanged_p = 0;
10172
10173 /* Changes start in front of the line, or end after it? */
10174 if (unchanged_p
10175 && (BEG_UNCHANGED < start - 1
10176 || END_UNCHANGED < end))
10177 unchanged_p = 0;
10178
10179 /* If selective display, can't optimize if changes start at the
10180 beginning of the line. */
10181 if (unchanged_p
10182 && INTEGERP (current_buffer->selective_display)
10183 && XINT (current_buffer->selective_display) > 0
10184 && (BEG_UNCHANGED < start || GPT <= start))
10185 unchanged_p = 0;
10186
10187 /* If there are overlays at the start or end of the line, these
10188 may have overlay strings with newlines in them. A change at
10189 START, for instance, may actually concern the display of such
10190 overlay strings as well, and they are displayed on different
10191 lines. So, quickly rule out this case. (For the future, it
10192 might be desirable to implement something more telling than
10193 just BEG/END_UNCHANGED.) */
10194 if (unchanged_p)
10195 {
10196 if (BEG + BEG_UNCHANGED == start
10197 && overlay_touches_p (start))
10198 unchanged_p = 0;
10199 if (END_UNCHANGED == end
10200 && overlay_touches_p (Z - end))
10201 unchanged_p = 0;
10202 }
10203 }
10204
10205 return unchanged_p;
10206 }
10207
10208
10209 /* Do a frame update, taking possible shortcuts into account. This is
10210 the main external entry point for redisplay.
10211
10212 If the last redisplay displayed an echo area message and that message
10213 is no longer requested, we clear the echo area or bring back the
10214 mini-buffer if that is in use. */
10215
10216 void
10217 redisplay ()
10218 {
10219 redisplay_internal (0);
10220 }
10221
10222
10223 static Lisp_Object
10224 overlay_arrow_string_or_property (var)
10225 Lisp_Object var;
10226 {
10227 Lisp_Object val;
10228
10229 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
10230 return val;
10231
10232 return Voverlay_arrow_string;
10233 }
10234
10235 /* Return 1 if there are any overlay-arrows in current_buffer. */
10236 static int
10237 overlay_arrow_in_current_buffer_p ()
10238 {
10239 Lisp_Object vlist;
10240
10241 for (vlist = Voverlay_arrow_variable_list;
10242 CONSP (vlist);
10243 vlist = XCDR (vlist))
10244 {
10245 Lisp_Object var = XCAR (vlist);
10246 Lisp_Object val;
10247
10248 if (!SYMBOLP (var))
10249 continue;
10250 val = find_symbol_value (var);
10251 if (MARKERP (val)
10252 && current_buffer == XMARKER (val)->buffer)
10253 return 1;
10254 }
10255 return 0;
10256 }
10257
10258
10259 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
10260 has changed. */
10261
10262 static int
10263 overlay_arrows_changed_p ()
10264 {
10265 Lisp_Object vlist;
10266
10267 for (vlist = Voverlay_arrow_variable_list;
10268 CONSP (vlist);
10269 vlist = XCDR (vlist))
10270 {
10271 Lisp_Object var = XCAR (vlist);
10272 Lisp_Object val, pstr;
10273
10274 if (!SYMBOLP (var))
10275 continue;
10276 val = find_symbol_value (var);
10277 if (!MARKERP (val))
10278 continue;
10279 if (! EQ (COERCE_MARKER (val),
10280 Fget (var, Qlast_arrow_position))
10281 || ! (pstr = overlay_arrow_string_or_property (var),
10282 EQ (pstr, Fget (var, Qlast_arrow_string))))
10283 return 1;
10284 }
10285 return 0;
10286 }
10287
10288 /* Mark overlay arrows to be updated on next redisplay. */
10289
10290 static void
10291 update_overlay_arrows (up_to_date)
10292 int up_to_date;
10293 {
10294 Lisp_Object vlist;
10295
10296 for (vlist = Voverlay_arrow_variable_list;
10297 CONSP (vlist);
10298 vlist = XCDR (vlist))
10299 {
10300 Lisp_Object var = XCAR (vlist);
10301
10302 if (!SYMBOLP (var))
10303 continue;
10304
10305 if (up_to_date > 0)
10306 {
10307 Lisp_Object val = find_symbol_value (var);
10308 Fput (var, Qlast_arrow_position,
10309 COERCE_MARKER (val));
10310 Fput (var, Qlast_arrow_string,
10311 overlay_arrow_string_or_property (var));
10312 }
10313 else if (up_to_date < 0
10314 || !NILP (Fget (var, Qlast_arrow_position)))
10315 {
10316 Fput (var, Qlast_arrow_position, Qt);
10317 Fput (var, Qlast_arrow_string, Qt);
10318 }
10319 }
10320 }
10321
10322
10323 /* Return overlay arrow string to display at row.
10324 Return integer (bitmap number) for arrow bitmap in left fringe.
10325 Return nil if no overlay arrow. */
10326
10327 static Lisp_Object
10328 overlay_arrow_at_row (it, row)
10329 struct it *it;
10330 struct glyph_row *row;
10331 {
10332 Lisp_Object vlist;
10333
10334 for (vlist = Voverlay_arrow_variable_list;
10335 CONSP (vlist);
10336 vlist = XCDR (vlist))
10337 {
10338 Lisp_Object var = XCAR (vlist);
10339 Lisp_Object val;
10340
10341 if (!SYMBOLP (var))
10342 continue;
10343
10344 val = find_symbol_value (var);
10345
10346 if (MARKERP (val)
10347 && current_buffer == XMARKER (val)->buffer
10348 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
10349 {
10350 if (FRAME_WINDOW_P (it->f)
10351 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
10352 {
10353 #ifdef HAVE_WINDOW_SYSTEM
10354 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
10355 {
10356 int fringe_bitmap;
10357 if ((fringe_bitmap = lookup_fringe_bitmap (val)) != 0)
10358 return make_number (fringe_bitmap);
10359 }
10360 #endif
10361 return make_number (-1); /* Use default arrow bitmap */
10362 }
10363 return overlay_arrow_string_or_property (var);
10364 }
10365 }
10366
10367 return Qnil;
10368 }
10369
10370 /* Return 1 if point moved out of or into a composition. Otherwise
10371 return 0. PREV_BUF and PREV_PT are the last point buffer and
10372 position. BUF and PT are the current point buffer and position. */
10373
10374 int
10375 check_point_in_composition (prev_buf, prev_pt, buf, pt)
10376 struct buffer *prev_buf, *buf;
10377 int prev_pt, pt;
10378 {
10379 int start, end;
10380 Lisp_Object prop;
10381 Lisp_Object buffer;
10382
10383 XSETBUFFER (buffer, buf);
10384 /* Check a composition at the last point if point moved within the
10385 same buffer. */
10386 if (prev_buf == buf)
10387 {
10388 if (prev_pt == pt)
10389 /* Point didn't move. */
10390 return 0;
10391
10392 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
10393 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
10394 && COMPOSITION_VALID_P (start, end, prop)
10395 && start < prev_pt && end > prev_pt)
10396 /* The last point was within the composition. Return 1 iff
10397 point moved out of the composition. */
10398 return (pt <= start || pt >= end);
10399 }
10400
10401 /* Check a composition at the current point. */
10402 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
10403 && find_composition (pt, -1, &start, &end, &prop, buffer)
10404 && COMPOSITION_VALID_P (start, end, prop)
10405 && start < pt && end > pt);
10406 }
10407
10408
10409 /* Reconsider the setting of B->clip_changed which is displayed
10410 in window W. */
10411
10412 static INLINE void
10413 reconsider_clip_changes (w, b)
10414 struct window *w;
10415 struct buffer *b;
10416 {
10417 if (b->clip_changed
10418 && !NILP (w->window_end_valid)
10419 && w->current_matrix->buffer == b
10420 && w->current_matrix->zv == BUF_ZV (b)
10421 && w->current_matrix->begv == BUF_BEGV (b))
10422 b->clip_changed = 0;
10423
10424 /* If display wasn't paused, and W is not a tool bar window, see if
10425 point has been moved into or out of a composition. In that case,
10426 we set b->clip_changed to 1 to force updating the screen. If
10427 b->clip_changed has already been set to 1, we can skip this
10428 check. */
10429 if (!b->clip_changed
10430 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
10431 {
10432 int pt;
10433
10434 if (w == XWINDOW (selected_window))
10435 pt = BUF_PT (current_buffer);
10436 else
10437 pt = marker_position (w->pointm);
10438
10439 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
10440 || pt != XINT (w->last_point))
10441 && check_point_in_composition (w->current_matrix->buffer,
10442 XINT (w->last_point),
10443 XBUFFER (w->buffer), pt))
10444 b->clip_changed = 1;
10445 }
10446 }
10447 \f
10448
10449 /* Select FRAME to forward the values of frame-local variables into C
10450 variables so that the redisplay routines can access those values
10451 directly. */
10452
10453 static void
10454 select_frame_for_redisplay (frame)
10455 Lisp_Object frame;
10456 {
10457 Lisp_Object tail, sym, val;
10458 Lisp_Object old = selected_frame;
10459
10460 selected_frame = frame;
10461
10462 for (tail = XFRAME (frame)->param_alist; CONSP (tail); tail = XCDR (tail))
10463 if (CONSP (XCAR (tail))
10464 && (sym = XCAR (XCAR (tail)),
10465 SYMBOLP (sym))
10466 && (sym = indirect_variable (sym),
10467 val = SYMBOL_VALUE (sym),
10468 (BUFFER_LOCAL_VALUEP (val)
10469 || SOME_BUFFER_LOCAL_VALUEP (val)))
10470 && XBUFFER_LOCAL_VALUE (val)->check_frame)
10471 /* Use find_symbol_value rather than Fsymbol_value
10472 to avoid an error if it is void. */
10473 find_symbol_value (sym);
10474
10475 for (tail = XFRAME (old)->param_alist; CONSP (tail); tail = XCDR (tail))
10476 if (CONSP (XCAR (tail))
10477 && (sym = XCAR (XCAR (tail)),
10478 SYMBOLP (sym))
10479 && (sym = indirect_variable (sym),
10480 val = SYMBOL_VALUE (sym),
10481 (BUFFER_LOCAL_VALUEP (val)
10482 || SOME_BUFFER_LOCAL_VALUEP (val)))
10483 && XBUFFER_LOCAL_VALUE (val)->check_frame)
10484 find_symbol_value (sym);
10485 }
10486
10487
10488 #define STOP_POLLING \
10489 do { if (! polling_stopped_here) stop_polling (); \
10490 polling_stopped_here = 1; } while (0)
10491
10492 #define RESUME_POLLING \
10493 do { if (polling_stopped_here) start_polling (); \
10494 polling_stopped_here = 0; } while (0)
10495
10496
10497 /* If PRESERVE_ECHO_AREA is nonzero, it means this redisplay is not in
10498 response to any user action; therefore, we should preserve the echo
10499 area. (Actually, our caller does that job.) Perhaps in the future
10500 avoid recentering windows if it is not necessary; currently that
10501 causes some problems. */
10502
10503 static void
10504 redisplay_internal (preserve_echo_area)
10505 int preserve_echo_area;
10506 {
10507 struct window *w = XWINDOW (selected_window);
10508 struct frame *f = XFRAME (w->frame);
10509 int pause;
10510 int must_finish = 0;
10511 struct text_pos tlbufpos, tlendpos;
10512 int number_of_visible_frames;
10513 int count;
10514 struct frame *sf = SELECTED_FRAME ();
10515 int polling_stopped_here = 0;
10516
10517 /* Non-zero means redisplay has to consider all windows on all
10518 frames. Zero means, only selected_window is considered. */
10519 int consider_all_windows_p;
10520
10521 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
10522
10523 /* No redisplay if running in batch mode or frame is not yet fully
10524 initialized, or redisplay is explicitly turned off by setting
10525 Vinhibit_redisplay. */
10526 if (noninteractive
10527 || !NILP (Vinhibit_redisplay)
10528 || !f->glyphs_initialized_p)
10529 return;
10530
10531 /* The flag redisplay_performed_directly_p is set by
10532 direct_output_for_insert when it already did the whole screen
10533 update necessary. */
10534 if (redisplay_performed_directly_p)
10535 {
10536 redisplay_performed_directly_p = 0;
10537 if (!hscroll_windows (selected_window))
10538 return;
10539 }
10540
10541 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
10542 if (popup_activated ())
10543 return;
10544 #endif
10545
10546 /* I don't think this happens but let's be paranoid. */
10547 if (redisplaying_p)
10548 return;
10549
10550 /* Record a function that resets redisplaying_p to its old value
10551 when we leave this function. */
10552 count = SPECPDL_INDEX ();
10553 record_unwind_protect (unwind_redisplay,
10554 Fcons (make_number (redisplaying_p), selected_frame));
10555 ++redisplaying_p;
10556 specbind (Qinhibit_free_realized_faces, Qnil);
10557
10558 {
10559 Lisp_Object tail, frame;
10560
10561 FOR_EACH_FRAME (tail, frame)
10562 {
10563 struct frame *f = XFRAME (frame);
10564 f->already_hscrolled_p = 0;
10565 }
10566 }
10567
10568 retry:
10569 pause = 0;
10570 reconsider_clip_changes (w, current_buffer);
10571 last_escape_glyph_frame = NULL;
10572 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
10573
10574 /* If new fonts have been loaded that make a glyph matrix adjustment
10575 necessary, do it. */
10576 if (fonts_changed_p)
10577 {
10578 adjust_glyphs (NULL);
10579 ++windows_or_buffers_changed;
10580 fonts_changed_p = 0;
10581 }
10582
10583 /* If face_change_count is non-zero, init_iterator will free all
10584 realized faces, which includes the faces referenced from current
10585 matrices. So, we can't reuse current matrices in this case. */
10586 if (face_change_count)
10587 ++windows_or_buffers_changed;
10588
10589 if (! FRAME_WINDOW_P (sf)
10590 && previous_terminal_frame != sf)
10591 {
10592 /* Since frames on an ASCII terminal share the same display
10593 area, displaying a different frame means redisplay the whole
10594 thing. */
10595 windows_or_buffers_changed++;
10596 SET_FRAME_GARBAGED (sf);
10597 XSETFRAME (Vterminal_frame, sf);
10598 }
10599 previous_terminal_frame = sf;
10600
10601 /* Set the visible flags for all frames. Do this before checking
10602 for resized or garbaged frames; they want to know if their frames
10603 are visible. See the comment in frame.h for
10604 FRAME_SAMPLE_VISIBILITY. */
10605 {
10606 Lisp_Object tail, frame;
10607
10608 number_of_visible_frames = 0;
10609
10610 FOR_EACH_FRAME (tail, frame)
10611 {
10612 struct frame *f = XFRAME (frame);
10613
10614 FRAME_SAMPLE_VISIBILITY (f);
10615 if (FRAME_VISIBLE_P (f))
10616 ++number_of_visible_frames;
10617 clear_desired_matrices (f);
10618 }
10619 }
10620
10621 /* Notice any pending interrupt request to change frame size. */
10622 do_pending_window_change (1);
10623
10624 /* Clear frames marked as garbaged. */
10625 if (frame_garbaged)
10626 clear_garbaged_frames ();
10627
10628 /* Build menubar and tool-bar items. */
10629 if (NILP (Vmemory_full))
10630 prepare_menu_bars ();
10631
10632 if (windows_or_buffers_changed)
10633 update_mode_lines++;
10634
10635 /* Detect case that we need to write or remove a star in the mode line. */
10636 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
10637 {
10638 w->update_mode_line = Qt;
10639 if (buffer_shared > 1)
10640 update_mode_lines++;
10641 }
10642
10643 /* If %c is in the mode line, update it if needed. */
10644 if (!NILP (w->column_number_displayed)
10645 /* This alternative quickly identifies a common case
10646 where no change is needed. */
10647 && !(PT == XFASTINT (w->last_point)
10648 && XFASTINT (w->last_modified) >= MODIFF
10649 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
10650 && (XFASTINT (w->column_number_displayed)
10651 != (int) current_column ())) /* iftc */
10652 w->update_mode_line = Qt;
10653
10654 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
10655
10656 /* The variable buffer_shared is set in redisplay_window and
10657 indicates that we redisplay a buffer in different windows. See
10658 there. */
10659 consider_all_windows_p = (update_mode_lines || buffer_shared > 1
10660 || cursor_type_changed);
10661
10662 /* If specs for an arrow have changed, do thorough redisplay
10663 to ensure we remove any arrow that should no longer exist. */
10664 if (overlay_arrows_changed_p ())
10665 consider_all_windows_p = windows_or_buffers_changed = 1;
10666
10667 /* Normally the message* functions will have already displayed and
10668 updated the echo area, but the frame may have been trashed, or
10669 the update may have been preempted, so display the echo area
10670 again here. Checking message_cleared_p captures the case that
10671 the echo area should be cleared. */
10672 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
10673 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
10674 || (message_cleared_p
10675 && minibuf_level == 0
10676 /* If the mini-window is currently selected, this means the
10677 echo-area doesn't show through. */
10678 && !MINI_WINDOW_P (XWINDOW (selected_window))))
10679 {
10680 int window_height_changed_p = echo_area_display (0);
10681 must_finish = 1;
10682
10683 /* If we don't display the current message, don't clear the
10684 message_cleared_p flag, because, if we did, we wouldn't clear
10685 the echo area in the next redisplay which doesn't preserve
10686 the echo area. */
10687 if (!display_last_displayed_message_p)
10688 message_cleared_p = 0;
10689
10690 if (fonts_changed_p)
10691 goto retry;
10692 else if (window_height_changed_p)
10693 {
10694 consider_all_windows_p = 1;
10695 ++update_mode_lines;
10696 ++windows_or_buffers_changed;
10697
10698 /* If window configuration was changed, frames may have been
10699 marked garbaged. Clear them or we will experience
10700 surprises wrt scrolling. */
10701 if (frame_garbaged)
10702 clear_garbaged_frames ();
10703 }
10704 }
10705 else if (EQ (selected_window, minibuf_window)
10706 && (current_buffer->clip_changed
10707 || XFASTINT (w->last_modified) < MODIFF
10708 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
10709 && resize_mini_window (w, 0))
10710 {
10711 /* Resized active mini-window to fit the size of what it is
10712 showing if its contents might have changed. */
10713 must_finish = 1;
10714 consider_all_windows_p = 1;
10715 ++windows_or_buffers_changed;
10716 ++update_mode_lines;
10717
10718 /* If window configuration was changed, frames may have been
10719 marked garbaged. Clear them or we will experience
10720 surprises wrt scrolling. */
10721 if (frame_garbaged)
10722 clear_garbaged_frames ();
10723 }
10724
10725
10726 /* If showing the region, and mark has changed, we must redisplay
10727 the whole window. The assignment to this_line_start_pos prevents
10728 the optimization directly below this if-statement. */
10729 if (((!NILP (Vtransient_mark_mode)
10730 && !NILP (XBUFFER (w->buffer)->mark_active))
10731 != !NILP (w->region_showing))
10732 || (!NILP (w->region_showing)
10733 && !EQ (w->region_showing,
10734 Fmarker_position (XBUFFER (w->buffer)->mark))))
10735 CHARPOS (this_line_start_pos) = 0;
10736
10737 /* Optimize the case that only the line containing the cursor in the
10738 selected window has changed. Variables starting with this_ are
10739 set in display_line and record information about the line
10740 containing the cursor. */
10741 tlbufpos = this_line_start_pos;
10742 tlendpos = this_line_end_pos;
10743 if (!consider_all_windows_p
10744 && CHARPOS (tlbufpos) > 0
10745 && NILP (w->update_mode_line)
10746 && !current_buffer->clip_changed
10747 && !current_buffer->prevent_redisplay_optimizations_p
10748 && FRAME_VISIBLE_P (XFRAME (w->frame))
10749 && !FRAME_OBSCURED_P (XFRAME (w->frame))
10750 /* Make sure recorded data applies to current buffer, etc. */
10751 && this_line_buffer == current_buffer
10752 && current_buffer == XBUFFER (w->buffer)
10753 && NILP (w->force_start)
10754 && NILP (w->optional_new_start)
10755 /* Point must be on the line that we have info recorded about. */
10756 && PT >= CHARPOS (tlbufpos)
10757 && PT <= Z - CHARPOS (tlendpos)
10758 /* All text outside that line, including its final newline,
10759 must be unchanged */
10760 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
10761 CHARPOS (tlendpos)))
10762 {
10763 if (CHARPOS (tlbufpos) > BEGV
10764 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
10765 && (CHARPOS (tlbufpos) == ZV
10766 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
10767 /* Former continuation line has disappeared by becoming empty */
10768 goto cancel;
10769 else if (XFASTINT (w->last_modified) < MODIFF
10770 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
10771 || MINI_WINDOW_P (w))
10772 {
10773 /* We have to handle the case of continuation around a
10774 wide-column character (See the comment in indent.c around
10775 line 885).
10776
10777 For instance, in the following case:
10778
10779 -------- Insert --------
10780 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
10781 J_I_ ==> J_I_ `^^' are cursors.
10782 ^^ ^^
10783 -------- --------
10784
10785 As we have to redraw the line above, we should goto cancel. */
10786
10787 struct it it;
10788 int line_height_before = this_line_pixel_height;
10789
10790 /* Note that start_display will handle the case that the
10791 line starting at tlbufpos is a continuation lines. */
10792 start_display (&it, w, tlbufpos);
10793
10794 /* Implementation note: It this still necessary? */
10795 if (it.current_x != this_line_start_x)
10796 goto cancel;
10797
10798 TRACE ((stderr, "trying display optimization 1\n"));
10799 w->cursor.vpos = -1;
10800 overlay_arrow_seen = 0;
10801 it.vpos = this_line_vpos;
10802 it.current_y = this_line_y;
10803 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
10804 display_line (&it);
10805
10806 /* If line contains point, is not continued,
10807 and ends at same distance from eob as before, we win */
10808 if (w->cursor.vpos >= 0
10809 /* Line is not continued, otherwise this_line_start_pos
10810 would have been set to 0 in display_line. */
10811 && CHARPOS (this_line_start_pos)
10812 /* Line ends as before. */
10813 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
10814 /* Line has same height as before. Otherwise other lines
10815 would have to be shifted up or down. */
10816 && this_line_pixel_height == line_height_before)
10817 {
10818 /* If this is not the window's last line, we must adjust
10819 the charstarts of the lines below. */
10820 if (it.current_y < it.last_visible_y)
10821 {
10822 struct glyph_row *row
10823 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
10824 int delta, delta_bytes;
10825
10826 if (Z - CHARPOS (tlendpos) == ZV)
10827 {
10828 /* This line ends at end of (accessible part of)
10829 buffer. There is no newline to count. */
10830 delta = (Z
10831 - CHARPOS (tlendpos)
10832 - MATRIX_ROW_START_CHARPOS (row));
10833 delta_bytes = (Z_BYTE
10834 - BYTEPOS (tlendpos)
10835 - MATRIX_ROW_START_BYTEPOS (row));
10836 }
10837 else
10838 {
10839 /* This line ends in a newline. Must take
10840 account of the newline and the rest of the
10841 text that follows. */
10842 delta = (Z
10843 - CHARPOS (tlendpos)
10844 - MATRIX_ROW_START_CHARPOS (row));
10845 delta_bytes = (Z_BYTE
10846 - BYTEPOS (tlendpos)
10847 - MATRIX_ROW_START_BYTEPOS (row));
10848 }
10849
10850 increment_matrix_positions (w->current_matrix,
10851 this_line_vpos + 1,
10852 w->current_matrix->nrows,
10853 delta, delta_bytes);
10854 }
10855
10856 /* If this row displays text now but previously didn't,
10857 or vice versa, w->window_end_vpos may have to be
10858 adjusted. */
10859 if ((it.glyph_row - 1)->displays_text_p)
10860 {
10861 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
10862 XSETINT (w->window_end_vpos, this_line_vpos);
10863 }
10864 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
10865 && this_line_vpos > 0)
10866 XSETINT (w->window_end_vpos, this_line_vpos - 1);
10867 w->window_end_valid = Qnil;
10868
10869 /* Update hint: No need to try to scroll in update_window. */
10870 w->desired_matrix->no_scrolling_p = 1;
10871
10872 #if GLYPH_DEBUG
10873 *w->desired_matrix->method = 0;
10874 debug_method_add (w, "optimization 1");
10875 #endif
10876 #ifdef HAVE_WINDOW_SYSTEM
10877 update_window_fringes (w, 0);
10878 #endif
10879 goto update;
10880 }
10881 else
10882 goto cancel;
10883 }
10884 else if (/* Cursor position hasn't changed. */
10885 PT == XFASTINT (w->last_point)
10886 /* Make sure the cursor was last displayed
10887 in this window. Otherwise we have to reposition it. */
10888 && 0 <= w->cursor.vpos
10889 && WINDOW_TOTAL_LINES (w) > w->cursor.vpos)
10890 {
10891 if (!must_finish)
10892 {
10893 do_pending_window_change (1);
10894
10895 /* We used to always goto end_of_redisplay here, but this
10896 isn't enough if we have a blinking cursor. */
10897 if (w->cursor_off_p == w->last_cursor_off_p)
10898 goto end_of_redisplay;
10899 }
10900 goto update;
10901 }
10902 /* If highlighting the region, or if the cursor is in the echo area,
10903 then we can't just move the cursor. */
10904 else if (! (!NILP (Vtransient_mark_mode)
10905 && !NILP (current_buffer->mark_active))
10906 && (EQ (selected_window, current_buffer->last_selected_window)
10907 || highlight_nonselected_windows)
10908 && NILP (w->region_showing)
10909 && NILP (Vshow_trailing_whitespace)
10910 && !cursor_in_echo_area)
10911 {
10912 struct it it;
10913 struct glyph_row *row;
10914
10915 /* Skip from tlbufpos to PT and see where it is. Note that
10916 PT may be in invisible text. If so, we will end at the
10917 next visible position. */
10918 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
10919 NULL, DEFAULT_FACE_ID);
10920 it.current_x = this_line_start_x;
10921 it.current_y = this_line_y;
10922 it.vpos = this_line_vpos;
10923
10924 /* The call to move_it_to stops in front of PT, but
10925 moves over before-strings. */
10926 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
10927
10928 if (it.vpos == this_line_vpos
10929 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
10930 row->enabled_p))
10931 {
10932 xassert (this_line_vpos == it.vpos);
10933 xassert (this_line_y == it.current_y);
10934 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
10935 #if GLYPH_DEBUG
10936 *w->desired_matrix->method = 0;
10937 debug_method_add (w, "optimization 3");
10938 #endif
10939 goto update;
10940 }
10941 else
10942 goto cancel;
10943 }
10944
10945 cancel:
10946 /* Text changed drastically or point moved off of line. */
10947 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
10948 }
10949
10950 CHARPOS (this_line_start_pos) = 0;
10951 consider_all_windows_p |= buffer_shared > 1;
10952 ++clear_face_cache_count;
10953 #ifdef HAVE_WINDOW_SYSTEM
10954 ++clear_image_cache_count;
10955 #endif
10956
10957 /* Build desired matrices, and update the display. If
10958 consider_all_windows_p is non-zero, do it for all windows on all
10959 frames. Otherwise do it for selected_window, only. */
10960
10961 if (consider_all_windows_p)
10962 {
10963 Lisp_Object tail, frame;
10964
10965 FOR_EACH_FRAME (tail, frame)
10966 XFRAME (frame)->updated_p = 0;
10967
10968 /* Recompute # windows showing selected buffer. This will be
10969 incremented each time such a window is displayed. */
10970 buffer_shared = 0;
10971
10972 FOR_EACH_FRAME (tail, frame)
10973 {
10974 struct frame *f = XFRAME (frame);
10975
10976 if (FRAME_WINDOW_P (f) || f == sf)
10977 {
10978 if (! EQ (frame, selected_frame))
10979 /* Select the frame, for the sake of frame-local
10980 variables. */
10981 select_frame_for_redisplay (frame);
10982
10983 /* Mark all the scroll bars to be removed; we'll redeem
10984 the ones we want when we redisplay their windows. */
10985 if (condemn_scroll_bars_hook)
10986 condemn_scroll_bars_hook (f);
10987
10988 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
10989 redisplay_windows (FRAME_ROOT_WINDOW (f));
10990
10991 /* Any scroll bars which redisplay_windows should have
10992 nuked should now go away. */
10993 if (judge_scroll_bars_hook)
10994 judge_scroll_bars_hook (f);
10995
10996 /* If fonts changed, display again. */
10997 /* ??? rms: I suspect it is a mistake to jump all the way
10998 back to retry here. It should just retry this frame. */
10999 if (fonts_changed_p)
11000 goto retry;
11001
11002 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
11003 {
11004 /* See if we have to hscroll. */
11005 if (!f->already_hscrolled_p)
11006 {
11007 f->already_hscrolled_p = 1;
11008 if (hscroll_windows (f->root_window))
11009 goto retry;
11010 }
11011
11012 /* Prevent various kinds of signals during display
11013 update. stdio is not robust about handling
11014 signals, which can cause an apparent I/O
11015 error. */
11016 if (interrupt_input)
11017 unrequest_sigio ();
11018 STOP_POLLING;
11019
11020 /* Update the display. */
11021 set_window_update_flags (XWINDOW (f->root_window), 1);
11022 pause |= update_frame (f, 0, 0);
11023 #if 0 /* Exiting the loop can leave the wrong value for buffer_shared. */
11024 if (pause)
11025 break;
11026 #endif
11027
11028 f->updated_p = 1;
11029 }
11030 }
11031 }
11032
11033 if (!pause)
11034 {
11035 /* Do the mark_window_display_accurate after all windows have
11036 been redisplayed because this call resets flags in buffers
11037 which are needed for proper redisplay. */
11038 FOR_EACH_FRAME (tail, frame)
11039 {
11040 struct frame *f = XFRAME (frame);
11041 if (f->updated_p)
11042 {
11043 mark_window_display_accurate (f->root_window, 1);
11044 if (frame_up_to_date_hook)
11045 frame_up_to_date_hook (f);
11046 }
11047 }
11048 }
11049 }
11050 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
11051 {
11052 Lisp_Object mini_window;
11053 struct frame *mini_frame;
11054
11055 displayed_buffer = XBUFFER (XWINDOW (selected_window)->buffer);
11056 /* Use list_of_error, not Qerror, so that
11057 we catch only errors and don't run the debugger. */
11058 internal_condition_case_1 (redisplay_window_1, selected_window,
11059 list_of_error,
11060 redisplay_window_error);
11061
11062 /* Compare desired and current matrices, perform output. */
11063
11064 update:
11065 /* If fonts changed, display again. */
11066 if (fonts_changed_p)
11067 goto retry;
11068
11069 /* Prevent various kinds of signals during display update.
11070 stdio is not robust about handling signals,
11071 which can cause an apparent I/O error. */
11072 if (interrupt_input)
11073 unrequest_sigio ();
11074 STOP_POLLING;
11075
11076 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
11077 {
11078 if (hscroll_windows (selected_window))
11079 goto retry;
11080
11081 XWINDOW (selected_window)->must_be_updated_p = 1;
11082 pause = update_frame (sf, 0, 0);
11083 }
11084
11085 /* We may have called echo_area_display at the top of this
11086 function. If the echo area is on another frame, that may
11087 have put text on a frame other than the selected one, so the
11088 above call to update_frame would not have caught it. Catch
11089 it here. */
11090 mini_window = FRAME_MINIBUF_WINDOW (sf);
11091 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
11092
11093 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
11094 {
11095 XWINDOW (mini_window)->must_be_updated_p = 1;
11096 pause |= update_frame (mini_frame, 0, 0);
11097 if (!pause && hscroll_windows (mini_window))
11098 goto retry;
11099 }
11100 }
11101
11102 /* If display was paused because of pending input, make sure we do a
11103 thorough update the next time. */
11104 if (pause)
11105 {
11106 /* Prevent the optimization at the beginning of
11107 redisplay_internal that tries a single-line update of the
11108 line containing the cursor in the selected window. */
11109 CHARPOS (this_line_start_pos) = 0;
11110
11111 /* Let the overlay arrow be updated the next time. */
11112 update_overlay_arrows (0);
11113
11114 /* If we pause after scrolling, some rows in the current
11115 matrices of some windows are not valid. */
11116 if (!WINDOW_FULL_WIDTH_P (w)
11117 && !FRAME_WINDOW_P (XFRAME (w->frame)))
11118 update_mode_lines = 1;
11119 }
11120 else
11121 {
11122 if (!consider_all_windows_p)
11123 {
11124 /* This has already been done above if
11125 consider_all_windows_p is set. */
11126 mark_window_display_accurate_1 (w, 1);
11127
11128 /* Say overlay arrows are up to date. */
11129 update_overlay_arrows (1);
11130
11131 if (frame_up_to_date_hook != 0)
11132 frame_up_to_date_hook (sf);
11133 }
11134
11135 update_mode_lines = 0;
11136 windows_or_buffers_changed = 0;
11137 cursor_type_changed = 0;
11138 }
11139
11140 /* Start SIGIO interrupts coming again. Having them off during the
11141 code above makes it less likely one will discard output, but not
11142 impossible, since there might be stuff in the system buffer here.
11143 But it is much hairier to try to do anything about that. */
11144 if (interrupt_input)
11145 request_sigio ();
11146 RESUME_POLLING;
11147
11148 /* If a frame has become visible which was not before, redisplay
11149 again, so that we display it. Expose events for such a frame
11150 (which it gets when becoming visible) don't call the parts of
11151 redisplay constructing glyphs, so simply exposing a frame won't
11152 display anything in this case. So, we have to display these
11153 frames here explicitly. */
11154 if (!pause)
11155 {
11156 Lisp_Object tail, frame;
11157 int new_count = 0;
11158
11159 FOR_EACH_FRAME (tail, frame)
11160 {
11161 int this_is_visible = 0;
11162
11163 if (XFRAME (frame)->visible)
11164 this_is_visible = 1;
11165 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
11166 if (XFRAME (frame)->visible)
11167 this_is_visible = 1;
11168
11169 if (this_is_visible)
11170 new_count++;
11171 }
11172
11173 if (new_count != number_of_visible_frames)
11174 windows_or_buffers_changed++;
11175 }
11176
11177 /* Change frame size now if a change is pending. */
11178 do_pending_window_change (1);
11179
11180 /* If we just did a pending size change, or have additional
11181 visible frames, redisplay again. */
11182 if (windows_or_buffers_changed && !pause)
11183 goto retry;
11184
11185 /* Clear the face cache eventually. */
11186 if (consider_all_windows_p)
11187 {
11188 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
11189 {
11190 clear_face_cache (0);
11191 clear_face_cache_count = 0;
11192 }
11193 #ifdef HAVE_WINDOW_SYSTEM
11194 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
11195 {
11196 Lisp_Object tail, frame;
11197 FOR_EACH_FRAME (tail, frame)
11198 {
11199 struct frame *f = XFRAME (frame);
11200 if (FRAME_WINDOW_P (f))
11201 clear_image_cache (f, 0);
11202 }
11203 clear_image_cache_count = 0;
11204 }
11205 #endif /* HAVE_WINDOW_SYSTEM */
11206 }
11207
11208 end_of_redisplay:
11209 unbind_to (count, Qnil);
11210 RESUME_POLLING;
11211 }
11212
11213
11214 /* Redisplay, but leave alone any recent echo area message unless
11215 another message has been requested in its place.
11216
11217 This is useful in situations where you need to redisplay but no
11218 user action has occurred, making it inappropriate for the message
11219 area to be cleared. See tracking_off and
11220 wait_reading_process_output for examples of these situations.
11221
11222 FROM_WHERE is an integer saying from where this function was
11223 called. This is useful for debugging. */
11224
11225 void
11226 redisplay_preserve_echo_area (from_where)
11227 int from_where;
11228 {
11229 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
11230
11231 if (!NILP (echo_area_buffer[1]))
11232 {
11233 /* We have a previously displayed message, but no current
11234 message. Redisplay the previous message. */
11235 display_last_displayed_message_p = 1;
11236 redisplay_internal (1);
11237 display_last_displayed_message_p = 0;
11238 }
11239 else
11240 redisplay_internal (1);
11241
11242 if (rif != NULL && rif->flush_display_optional)
11243 rif->flush_display_optional (NULL);
11244 }
11245
11246
11247 /* Function registered with record_unwind_protect in
11248 redisplay_internal. Reset redisplaying_p to the value it had
11249 before redisplay_internal was called, and clear
11250 prevent_freeing_realized_faces_p. It also selects the previously
11251 selected frame. */
11252
11253 static Lisp_Object
11254 unwind_redisplay (val)
11255 Lisp_Object val;
11256 {
11257 Lisp_Object old_redisplaying_p, old_frame;
11258
11259 old_redisplaying_p = XCAR (val);
11260 redisplaying_p = XFASTINT (old_redisplaying_p);
11261 old_frame = XCDR (val);
11262 if (! EQ (old_frame, selected_frame))
11263 select_frame_for_redisplay (old_frame);
11264 return Qnil;
11265 }
11266
11267
11268 /* Mark the display of window W as accurate or inaccurate. If
11269 ACCURATE_P is non-zero mark display of W as accurate. If
11270 ACCURATE_P is zero, arrange for W to be redisplayed the next time
11271 redisplay_internal is called. */
11272
11273 static void
11274 mark_window_display_accurate_1 (w, accurate_p)
11275 struct window *w;
11276 int accurate_p;
11277 {
11278 if (BUFFERP (w->buffer))
11279 {
11280 struct buffer *b = XBUFFER (w->buffer);
11281
11282 w->last_modified
11283 = make_number (accurate_p ? BUF_MODIFF (b) : 0);
11284 w->last_overlay_modified
11285 = make_number (accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
11286 w->last_had_star
11287 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b) ? Qt : Qnil;
11288
11289 if (accurate_p)
11290 {
11291 b->clip_changed = 0;
11292 b->prevent_redisplay_optimizations_p = 0;
11293
11294 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
11295 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
11296 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
11297 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
11298
11299 w->current_matrix->buffer = b;
11300 w->current_matrix->begv = BUF_BEGV (b);
11301 w->current_matrix->zv = BUF_ZV (b);
11302
11303 w->last_cursor = w->cursor;
11304 w->last_cursor_off_p = w->cursor_off_p;
11305
11306 if (w == XWINDOW (selected_window))
11307 w->last_point = make_number (BUF_PT (b));
11308 else
11309 w->last_point = make_number (XMARKER (w->pointm)->charpos);
11310 }
11311 }
11312
11313 if (accurate_p)
11314 {
11315 w->window_end_valid = w->buffer;
11316 #if 0 /* This is incorrect with variable-height lines. */
11317 xassert (XINT (w->window_end_vpos)
11318 < (WINDOW_TOTAL_LINES (w)
11319 - (WINDOW_WANTS_MODELINE_P (w) ? 1 : 0)));
11320 #endif
11321 w->update_mode_line = Qnil;
11322 }
11323 }
11324
11325
11326 /* Mark the display of windows in the window tree rooted at WINDOW as
11327 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
11328 windows as accurate. If ACCURATE_P is zero, arrange for windows to
11329 be redisplayed the next time redisplay_internal is called. */
11330
11331 void
11332 mark_window_display_accurate (window, accurate_p)
11333 Lisp_Object window;
11334 int accurate_p;
11335 {
11336 struct window *w;
11337
11338 for (; !NILP (window); window = w->next)
11339 {
11340 w = XWINDOW (window);
11341 mark_window_display_accurate_1 (w, accurate_p);
11342
11343 if (!NILP (w->vchild))
11344 mark_window_display_accurate (w->vchild, accurate_p);
11345 if (!NILP (w->hchild))
11346 mark_window_display_accurate (w->hchild, accurate_p);
11347 }
11348
11349 if (accurate_p)
11350 {
11351 update_overlay_arrows (1);
11352 }
11353 else
11354 {
11355 /* Force a thorough redisplay the next time by setting
11356 last_arrow_position and last_arrow_string to t, which is
11357 unequal to any useful value of Voverlay_arrow_... */
11358 update_overlay_arrows (-1);
11359 }
11360 }
11361
11362
11363 /* Return value in display table DP (Lisp_Char_Table *) for character
11364 C. Since a display table doesn't have any parent, we don't have to
11365 follow parent. Do not call this function directly but use the
11366 macro DISP_CHAR_VECTOR. */
11367
11368 Lisp_Object
11369 disp_char_vector (dp, c)
11370 struct Lisp_Char_Table *dp;
11371 int c;
11372 {
11373 int code[4], i;
11374 Lisp_Object val;
11375
11376 if (SINGLE_BYTE_CHAR_P (c))
11377 return (dp->contents[c]);
11378
11379 SPLIT_CHAR (c, code[0], code[1], code[2]);
11380 if (code[1] < 32)
11381 code[1] = -1;
11382 else if (code[2] < 32)
11383 code[2] = -1;
11384
11385 /* Here, the possible range of code[0] (== charset ID) is
11386 128..max_charset. Since the top level char table contains data
11387 for multibyte characters after 256th element, we must increment
11388 code[0] by 128 to get a correct index. */
11389 code[0] += 128;
11390 code[3] = -1; /* anchor */
11391
11392 for (i = 0; code[i] >= 0; i++, dp = XCHAR_TABLE (val))
11393 {
11394 val = dp->contents[code[i]];
11395 if (!SUB_CHAR_TABLE_P (val))
11396 return (NILP (val) ? dp->defalt : val);
11397 }
11398
11399 /* Here, val is a sub char table. We return the default value of
11400 it. */
11401 return (dp->defalt);
11402 }
11403
11404
11405 \f
11406 /***********************************************************************
11407 Window Redisplay
11408 ***********************************************************************/
11409
11410 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
11411
11412 static void
11413 redisplay_windows (window)
11414 Lisp_Object window;
11415 {
11416 while (!NILP (window))
11417 {
11418 struct window *w = XWINDOW (window);
11419
11420 if (!NILP (w->hchild))
11421 redisplay_windows (w->hchild);
11422 else if (!NILP (w->vchild))
11423 redisplay_windows (w->vchild);
11424 else
11425 {
11426 displayed_buffer = XBUFFER (w->buffer);
11427 /* Use list_of_error, not Qerror, so that
11428 we catch only errors and don't run the debugger. */
11429 internal_condition_case_1 (redisplay_window_0, window,
11430 list_of_error,
11431 redisplay_window_error);
11432 }
11433
11434 window = w->next;
11435 }
11436 }
11437
11438 static Lisp_Object
11439 redisplay_window_error ()
11440 {
11441 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
11442 return Qnil;
11443 }
11444
11445 static Lisp_Object
11446 redisplay_window_0 (window)
11447 Lisp_Object window;
11448 {
11449 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
11450 redisplay_window (window, 0);
11451 return Qnil;
11452 }
11453
11454 static Lisp_Object
11455 redisplay_window_1 (window)
11456 Lisp_Object window;
11457 {
11458 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
11459 redisplay_window (window, 1);
11460 return Qnil;
11461 }
11462 \f
11463
11464 /* Increment GLYPH until it reaches END or CONDITION fails while
11465 adding (GLYPH)->pixel_width to X. */
11466
11467 #define SKIP_GLYPHS(glyph, end, x, condition) \
11468 do \
11469 { \
11470 (x) += (glyph)->pixel_width; \
11471 ++(glyph); \
11472 } \
11473 while ((glyph) < (end) && (condition))
11474
11475
11476 /* Set cursor position of W. PT is assumed to be displayed in ROW.
11477 DELTA is the number of bytes by which positions recorded in ROW
11478 differ from current buffer positions. */
11479
11480 void
11481 set_cursor_from_row (w, row, matrix, delta, delta_bytes, dy, dvpos)
11482 struct window *w;
11483 struct glyph_row *row;
11484 struct glyph_matrix *matrix;
11485 int delta, delta_bytes, dy, dvpos;
11486 {
11487 struct glyph *glyph = row->glyphs[TEXT_AREA];
11488 struct glyph *end = glyph + row->used[TEXT_AREA];
11489 struct glyph *cursor = NULL;
11490 /* The first glyph that starts a sequence of glyphs from string. */
11491 struct glyph *string_start;
11492 /* The X coordinate of string_start. */
11493 int string_start_x;
11494 /* The last known character position. */
11495 int last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
11496 /* The last known character position before string_start. */
11497 int string_before_pos;
11498 int x = row->x;
11499 int cursor_x = x;
11500 int cursor_from_overlay_pos = 0;
11501 int pt_old = PT - delta;
11502
11503 /* Skip over glyphs not having an object at the start of the row.
11504 These are special glyphs like truncation marks on terminal
11505 frames. */
11506 if (row->displays_text_p)
11507 while (glyph < end
11508 && INTEGERP (glyph->object)
11509 && glyph->charpos < 0)
11510 {
11511 x += glyph->pixel_width;
11512 ++glyph;
11513 }
11514
11515 string_start = NULL;
11516 while (glyph < end
11517 && !INTEGERP (glyph->object)
11518 && (!BUFFERP (glyph->object)
11519 || (last_pos = glyph->charpos) < pt_old))
11520 {
11521 if (! STRINGP (glyph->object))
11522 {
11523 string_start = NULL;
11524 x += glyph->pixel_width;
11525 ++glyph;
11526 if (cursor_from_overlay_pos
11527 && last_pos > cursor_from_overlay_pos)
11528 {
11529 cursor_from_overlay_pos = 0;
11530 cursor = 0;
11531 }
11532 }
11533 else
11534 {
11535 string_before_pos = last_pos;
11536 string_start = glyph;
11537 string_start_x = x;
11538 /* Skip all glyphs from string. */
11539 do
11540 {
11541 int pos;
11542 if ((cursor == NULL || glyph > cursor)
11543 && !NILP (Fget_char_property (make_number ((glyph)->charpos),
11544 Qcursor, (glyph)->object))
11545 && (pos = string_buffer_position (w, glyph->object,
11546 string_before_pos),
11547 (pos == 0 /* From overlay */
11548 || pos == pt_old)))
11549 {
11550 /* Estimate overlay buffer position from the buffer
11551 positions of the glyphs before and after the overlay.
11552 Add 1 to last_pos so that if point corresponds to the
11553 glyph right after the overlay, we still use a 'cursor'
11554 property found in that overlay. */
11555 cursor_from_overlay_pos = pos == 0 ? last_pos+1 : 0;
11556 cursor = glyph;
11557 cursor_x = x;
11558 }
11559 x += glyph->pixel_width;
11560 ++glyph;
11561 }
11562 while (glyph < end && STRINGP (glyph->object));
11563 }
11564 }
11565
11566 if (cursor != NULL)
11567 {
11568 glyph = cursor;
11569 x = cursor_x;
11570 }
11571 else if (row->ends_in_ellipsis_p && glyph == end)
11572 {
11573 /* Scan back over the ellipsis glyphs, decrementing positions. */
11574 while (glyph > row->glyphs[TEXT_AREA]
11575 && (glyph - 1)->charpos == last_pos)
11576 glyph--, x -= glyph->pixel_width;
11577 /* That loop always goes one position too far,
11578 including the glyph before the ellipsis.
11579 So scan forward over that one. */
11580 x += glyph->pixel_width;
11581 glyph++;
11582 }
11583 else if (string_start
11584 && (glyph == end || !BUFFERP (glyph->object) || last_pos > pt_old))
11585 {
11586 /* We may have skipped over point because the previous glyphs
11587 are from string. As there's no easy way to know the
11588 character position of the current glyph, find the correct
11589 glyph on point by scanning from string_start again. */
11590 Lisp_Object limit;
11591 Lisp_Object string;
11592 int pos;
11593
11594 limit = make_number (pt_old + 1);
11595 end = glyph;
11596 glyph = string_start;
11597 x = string_start_x;
11598 string = glyph->object;
11599 pos = string_buffer_position (w, string, string_before_pos);
11600 /* If STRING is from overlay, LAST_POS == 0. We skip such glyphs
11601 because we always put cursor after overlay strings. */
11602 while (pos == 0 && glyph < end)
11603 {
11604 string = glyph->object;
11605 SKIP_GLYPHS (glyph, end, x, EQ (glyph->object, string));
11606 if (glyph < end)
11607 pos = string_buffer_position (w, glyph->object, string_before_pos);
11608 }
11609
11610 while (glyph < end)
11611 {
11612 pos = XINT (Fnext_single_char_property_change
11613 (make_number (pos), Qdisplay, Qnil, limit));
11614 if (pos > pt_old)
11615 break;
11616 /* Skip glyphs from the same string. */
11617 string = glyph->object;
11618 SKIP_GLYPHS (glyph, end, x, EQ (glyph->object, string));
11619 /* Skip glyphs from an overlay. */
11620 while (glyph < end
11621 && ! string_buffer_position (w, glyph->object, pos))
11622 {
11623 string = glyph->object;
11624 SKIP_GLYPHS (glyph, end, x, EQ (glyph->object, string));
11625 }
11626 }
11627 }
11628
11629 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
11630 w->cursor.x = x;
11631 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
11632 w->cursor.y = row->y + dy;
11633
11634 if (w == XWINDOW (selected_window))
11635 {
11636 if (!row->continued_p
11637 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
11638 && row->x == 0)
11639 {
11640 this_line_buffer = XBUFFER (w->buffer);
11641
11642 CHARPOS (this_line_start_pos)
11643 = MATRIX_ROW_START_CHARPOS (row) + delta;
11644 BYTEPOS (this_line_start_pos)
11645 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
11646
11647 CHARPOS (this_line_end_pos)
11648 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
11649 BYTEPOS (this_line_end_pos)
11650 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
11651
11652 this_line_y = w->cursor.y;
11653 this_line_pixel_height = row->height;
11654 this_line_vpos = w->cursor.vpos;
11655 this_line_start_x = row->x;
11656 }
11657 else
11658 CHARPOS (this_line_start_pos) = 0;
11659 }
11660 }
11661
11662
11663 /* Run window scroll functions, if any, for WINDOW with new window
11664 start STARTP. Sets the window start of WINDOW to that position.
11665
11666 We assume that the window's buffer is really current. */
11667
11668 static INLINE struct text_pos
11669 run_window_scroll_functions (window, startp)
11670 Lisp_Object window;
11671 struct text_pos startp;
11672 {
11673 struct window *w = XWINDOW (window);
11674 SET_MARKER_FROM_TEXT_POS (w->start, startp);
11675
11676 if (current_buffer != XBUFFER (w->buffer))
11677 abort ();
11678
11679 if (!NILP (Vwindow_scroll_functions))
11680 {
11681 run_hook_with_args_2 (Qwindow_scroll_functions, window,
11682 make_number (CHARPOS (startp)));
11683 SET_TEXT_POS_FROM_MARKER (startp, w->start);
11684 /* In case the hook functions switch buffers. */
11685 if (current_buffer != XBUFFER (w->buffer))
11686 set_buffer_internal_1 (XBUFFER (w->buffer));
11687 }
11688
11689 return startp;
11690 }
11691
11692
11693 /* Make sure the line containing the cursor is fully visible.
11694 A value of 1 means there is nothing to be done.
11695 (Either the line is fully visible, or it cannot be made so,
11696 or we cannot tell.)
11697
11698 If FORCE_P is non-zero, return 0 even if partial visible cursor row
11699 is higher than window.
11700
11701 A value of 0 means the caller should do scrolling
11702 as if point had gone off the screen. */
11703
11704 static int
11705 cursor_row_fully_visible_p (w, force_p, current_matrix_p)
11706 struct window *w;
11707 int force_p;
11708 int current_matrix_p;
11709 {
11710 struct glyph_matrix *matrix;
11711 struct glyph_row *row;
11712 int window_height;
11713
11714 if (!make_cursor_line_fully_visible_p)
11715 return 1;
11716
11717 /* It's not always possible to find the cursor, e.g, when a window
11718 is full of overlay strings. Don't do anything in that case. */
11719 if (w->cursor.vpos < 0)
11720 return 1;
11721
11722 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
11723 row = MATRIX_ROW (matrix, w->cursor.vpos);
11724
11725 /* If the cursor row is not partially visible, there's nothing to do. */
11726 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
11727 return 1;
11728
11729 /* If the row the cursor is in is taller than the window's height,
11730 it's not clear what to do, so do nothing. */
11731 window_height = window_box_height (w);
11732 if (row->height >= window_height)
11733 {
11734 if (!force_p || MINI_WINDOW_P (w) || w->vscroll)
11735 return 1;
11736 }
11737 return 0;
11738
11739 #if 0
11740 /* This code used to try to scroll the window just enough to make
11741 the line visible. It returned 0 to say that the caller should
11742 allocate larger glyph matrices. */
11743
11744 if (MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (w, row))
11745 {
11746 int dy = row->height - row->visible_height;
11747 w->vscroll = 0;
11748 w->cursor.y += dy;
11749 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
11750 }
11751 else /* MATRIX_ROW_PARTIALLY_VISIBLE_AT_BOTTOM_P (w, row)) */
11752 {
11753 int dy = - (row->height - row->visible_height);
11754 w->vscroll = dy;
11755 w->cursor.y += dy;
11756 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
11757 }
11758
11759 /* When we change the cursor y-position of the selected window,
11760 change this_line_y as well so that the display optimization for
11761 the cursor line of the selected window in redisplay_internal uses
11762 the correct y-position. */
11763 if (w == XWINDOW (selected_window))
11764 this_line_y = w->cursor.y;
11765
11766 /* If vscrolling requires a larger glyph matrix, arrange for a fresh
11767 redisplay with larger matrices. */
11768 if (matrix->nrows < required_matrix_height (w))
11769 {
11770 fonts_changed_p = 1;
11771 return 0;
11772 }
11773
11774 return 1;
11775 #endif /* 0 */
11776 }
11777
11778
11779 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
11780 non-zero means only WINDOW is redisplayed in redisplay_internal.
11781 TEMP_SCROLL_STEP has the same meaning as scroll_step, and is used
11782 in redisplay_window to bring a partially visible line into view in
11783 the case that only the cursor has moved.
11784
11785 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
11786 last screen line's vertical height extends past the end of the screen.
11787
11788 Value is
11789
11790 1 if scrolling succeeded
11791
11792 0 if scrolling didn't find point.
11793
11794 -1 if new fonts have been loaded so that we must interrupt
11795 redisplay, adjust glyph matrices, and try again. */
11796
11797 enum
11798 {
11799 SCROLLING_SUCCESS,
11800 SCROLLING_FAILED,
11801 SCROLLING_NEED_LARGER_MATRICES
11802 };
11803
11804 static int
11805 try_scrolling (window, just_this_one_p, scroll_conservatively,
11806 scroll_step, temp_scroll_step, last_line_misfit)
11807 Lisp_Object window;
11808 int just_this_one_p;
11809 EMACS_INT scroll_conservatively, scroll_step;
11810 int temp_scroll_step;
11811 int last_line_misfit;
11812 {
11813 struct window *w = XWINDOW (window);
11814 struct frame *f = XFRAME (w->frame);
11815 struct text_pos scroll_margin_pos;
11816 struct text_pos pos;
11817 struct text_pos startp;
11818 struct it it;
11819 Lisp_Object window_end;
11820 int this_scroll_margin;
11821 int dy = 0;
11822 int scroll_max;
11823 int rc;
11824 int amount_to_scroll = 0;
11825 Lisp_Object aggressive;
11826 int height;
11827 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
11828
11829 #if GLYPH_DEBUG
11830 debug_method_add (w, "try_scrolling");
11831 #endif
11832
11833 SET_TEXT_POS_FROM_MARKER (startp, w->start);
11834
11835 /* Compute scroll margin height in pixels. We scroll when point is
11836 within this distance from the top or bottom of the window. */
11837 if (scroll_margin > 0)
11838 {
11839 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
11840 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
11841 }
11842 else
11843 this_scroll_margin = 0;
11844
11845 /* Force scroll_conservatively to have a reasonable value so it doesn't
11846 cause an overflow while computing how much to scroll. */
11847 if (scroll_conservatively)
11848 scroll_conservatively = min (scroll_conservatively,
11849 MOST_POSITIVE_FIXNUM / FRAME_LINE_HEIGHT (f));
11850
11851 /* Compute how much we should try to scroll maximally to bring point
11852 into view. */
11853 if (scroll_step || scroll_conservatively || temp_scroll_step)
11854 scroll_max = max (scroll_step,
11855 max (scroll_conservatively, temp_scroll_step));
11856 else if (NUMBERP (current_buffer->scroll_down_aggressively)
11857 || NUMBERP (current_buffer->scroll_up_aggressively))
11858 /* We're trying to scroll because of aggressive scrolling
11859 but no scroll_step is set. Choose an arbitrary one. Maybe
11860 there should be a variable for this. */
11861 scroll_max = 10;
11862 else
11863 scroll_max = 0;
11864 scroll_max *= FRAME_LINE_HEIGHT (f);
11865
11866 /* Decide whether we have to scroll down. Start at the window end
11867 and move this_scroll_margin up to find the position of the scroll
11868 margin. */
11869 window_end = Fwindow_end (window, Qt);
11870
11871 too_near_end:
11872
11873 CHARPOS (scroll_margin_pos) = XINT (window_end);
11874 BYTEPOS (scroll_margin_pos) = CHAR_TO_BYTE (CHARPOS (scroll_margin_pos));
11875
11876 if (this_scroll_margin || extra_scroll_margin_lines)
11877 {
11878 start_display (&it, w, scroll_margin_pos);
11879 if (this_scroll_margin)
11880 move_it_vertically_backward (&it, this_scroll_margin);
11881 if (extra_scroll_margin_lines)
11882 move_it_by_lines (&it, - extra_scroll_margin_lines, 0);
11883 scroll_margin_pos = it.current.pos;
11884 }
11885
11886 if (PT >= CHARPOS (scroll_margin_pos))
11887 {
11888 int y0;
11889
11890 /* Point is in the scroll margin at the bottom of the window, or
11891 below. Compute a new window start that makes point visible. */
11892
11893 /* Compute the distance from the scroll margin to PT.
11894 Give up if the distance is greater than scroll_max. */
11895 start_display (&it, w, scroll_margin_pos);
11896 y0 = it.current_y;
11897 move_it_to (&it, PT, 0, it.last_visible_y, -1,
11898 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
11899
11900 /* To make point visible, we have to move the window start
11901 down so that the line the cursor is in is visible, which
11902 means we have to add in the height of the cursor line. */
11903 dy = line_bottom_y (&it) - y0;
11904
11905 if (dy > scroll_max)
11906 return SCROLLING_FAILED;
11907
11908 /* Move the window start down. If scrolling conservatively,
11909 move it just enough down to make point visible. If
11910 scroll_step is set, move it down by scroll_step. */
11911 start_display (&it, w, startp);
11912
11913 if (scroll_conservatively)
11914 /* Set AMOUNT_TO_SCROLL to at least one line,
11915 and at most scroll_conservatively lines. */
11916 amount_to_scroll
11917 = min (max (dy, FRAME_LINE_HEIGHT (f)),
11918 FRAME_LINE_HEIGHT (f) * scroll_conservatively);
11919 else if (scroll_step || temp_scroll_step)
11920 amount_to_scroll = scroll_max;
11921 else
11922 {
11923 aggressive = current_buffer->scroll_up_aggressively;
11924 height = WINDOW_BOX_TEXT_HEIGHT (w);
11925 if (NUMBERP (aggressive))
11926 {
11927 double float_amount = XFLOATINT (aggressive) * height;
11928 amount_to_scroll = float_amount;
11929 if (amount_to_scroll == 0 && float_amount > 0)
11930 amount_to_scroll = 1;
11931 }
11932 }
11933
11934 if (amount_to_scroll <= 0)
11935 return SCROLLING_FAILED;
11936
11937 /* If moving by amount_to_scroll leaves STARTP unchanged,
11938 move it down one screen line. */
11939
11940 move_it_vertically (&it, amount_to_scroll);
11941 if (CHARPOS (it.current.pos) == CHARPOS (startp))
11942 move_it_by_lines (&it, 1, 1);
11943 startp = it.current.pos;
11944 }
11945 else
11946 {
11947 /* See if point is inside the scroll margin at the top of the
11948 window. */
11949 scroll_margin_pos = startp;
11950 if (this_scroll_margin)
11951 {
11952 start_display (&it, w, startp);
11953 move_it_vertically (&it, this_scroll_margin);
11954 scroll_margin_pos = it.current.pos;
11955 }
11956
11957 if (PT < CHARPOS (scroll_margin_pos))
11958 {
11959 /* Point is in the scroll margin at the top of the window or
11960 above what is displayed in the window. */
11961 int y0;
11962
11963 /* Compute the vertical distance from PT to the scroll
11964 margin position. Give up if distance is greater than
11965 scroll_max. */
11966 SET_TEXT_POS (pos, PT, PT_BYTE);
11967 start_display (&it, w, pos);
11968 y0 = it.current_y;
11969 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
11970 it.last_visible_y, -1,
11971 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
11972 dy = it.current_y - y0;
11973 if (dy > scroll_max)
11974 return SCROLLING_FAILED;
11975
11976 /* Compute new window start. */
11977 start_display (&it, w, startp);
11978
11979 if (scroll_conservatively)
11980 amount_to_scroll
11981 = max (dy, FRAME_LINE_HEIGHT (f) * max (scroll_step, temp_scroll_step));
11982 else if (scroll_step || temp_scroll_step)
11983 amount_to_scroll = scroll_max;
11984 else
11985 {
11986 aggressive = current_buffer->scroll_down_aggressively;
11987 height = WINDOW_BOX_TEXT_HEIGHT (w);
11988 if (NUMBERP (aggressive))
11989 {
11990 double float_amount = XFLOATINT (aggressive) * height;
11991 amount_to_scroll = float_amount;
11992 if (amount_to_scroll == 0 && float_amount > 0)
11993 amount_to_scroll = 1;
11994 }
11995 }
11996
11997 if (amount_to_scroll <= 0)
11998 return SCROLLING_FAILED;
11999
12000 move_it_vertically_backward (&it, amount_to_scroll);
12001 startp = it.current.pos;
12002 }
12003 }
12004
12005 /* Run window scroll functions. */
12006 startp = run_window_scroll_functions (window, startp);
12007
12008 /* Display the window. Give up if new fonts are loaded, or if point
12009 doesn't appear. */
12010 if (!try_window (window, startp, 0))
12011 rc = SCROLLING_NEED_LARGER_MATRICES;
12012 else if (w->cursor.vpos < 0)
12013 {
12014 clear_glyph_matrix (w->desired_matrix);
12015 rc = SCROLLING_FAILED;
12016 }
12017 else
12018 {
12019 /* Maybe forget recorded base line for line number display. */
12020 if (!just_this_one_p
12021 || current_buffer->clip_changed
12022 || BEG_UNCHANGED < CHARPOS (startp))
12023 w->base_line_number = Qnil;
12024
12025 /* If cursor ends up on a partially visible line,
12026 treat that as being off the bottom of the screen. */
12027 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1, 0))
12028 {
12029 clear_glyph_matrix (w->desired_matrix);
12030 ++extra_scroll_margin_lines;
12031 goto too_near_end;
12032 }
12033 rc = SCROLLING_SUCCESS;
12034 }
12035
12036 return rc;
12037 }
12038
12039
12040 /* Compute a suitable window start for window W if display of W starts
12041 on a continuation line. Value is non-zero if a new window start
12042 was computed.
12043
12044 The new window start will be computed, based on W's width, starting
12045 from the start of the continued line. It is the start of the
12046 screen line with the minimum distance from the old start W->start. */
12047
12048 static int
12049 compute_window_start_on_continuation_line (w)
12050 struct window *w;
12051 {
12052 struct text_pos pos, start_pos;
12053 int window_start_changed_p = 0;
12054
12055 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
12056
12057 /* If window start is on a continuation line... Window start may be
12058 < BEGV in case there's invisible text at the start of the
12059 buffer (M-x rmail, for example). */
12060 if (CHARPOS (start_pos) > BEGV
12061 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
12062 {
12063 struct it it;
12064 struct glyph_row *row;
12065
12066 /* Handle the case that the window start is out of range. */
12067 if (CHARPOS (start_pos) < BEGV)
12068 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
12069 else if (CHARPOS (start_pos) > ZV)
12070 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
12071
12072 /* Find the start of the continued line. This should be fast
12073 because scan_buffer is fast (newline cache). */
12074 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
12075 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
12076 row, DEFAULT_FACE_ID);
12077 reseat_at_previous_visible_line_start (&it);
12078
12079 /* If the line start is "too far" away from the window start,
12080 say it takes too much time to compute a new window start. */
12081 if (CHARPOS (start_pos) - IT_CHARPOS (it)
12082 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
12083 {
12084 int min_distance, distance;
12085
12086 /* Move forward by display lines to find the new window
12087 start. If window width was enlarged, the new start can
12088 be expected to be > the old start. If window width was
12089 decreased, the new window start will be < the old start.
12090 So, we're looking for the display line start with the
12091 minimum distance from the old window start. */
12092 pos = it.current.pos;
12093 min_distance = INFINITY;
12094 while ((distance = abs (CHARPOS (start_pos) - IT_CHARPOS (it))),
12095 distance < min_distance)
12096 {
12097 min_distance = distance;
12098 pos = it.current.pos;
12099 move_it_by_lines (&it, 1, 0);
12100 }
12101
12102 /* Set the window start there. */
12103 SET_MARKER_FROM_TEXT_POS (w->start, pos);
12104 window_start_changed_p = 1;
12105 }
12106 }
12107
12108 return window_start_changed_p;
12109 }
12110
12111
12112 /* Try cursor movement in case text has not changed in window WINDOW,
12113 with window start STARTP. Value is
12114
12115 CURSOR_MOVEMENT_SUCCESS if successful
12116
12117 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
12118
12119 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
12120 display. *SCROLL_STEP is set to 1, under certain circumstances, if
12121 we want to scroll as if scroll-step were set to 1. See the code.
12122
12123 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
12124 which case we have to abort this redisplay, and adjust matrices
12125 first. */
12126
12127 enum
12128 {
12129 CURSOR_MOVEMENT_SUCCESS,
12130 CURSOR_MOVEMENT_CANNOT_BE_USED,
12131 CURSOR_MOVEMENT_MUST_SCROLL,
12132 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
12133 };
12134
12135 static int
12136 try_cursor_movement (window, startp, scroll_step)
12137 Lisp_Object window;
12138 struct text_pos startp;
12139 int *scroll_step;
12140 {
12141 struct window *w = XWINDOW (window);
12142 struct frame *f = XFRAME (w->frame);
12143 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
12144
12145 #if GLYPH_DEBUG
12146 if (inhibit_try_cursor_movement)
12147 return rc;
12148 #endif
12149
12150 /* Handle case where text has not changed, only point, and it has
12151 not moved off the frame. */
12152 if (/* Point may be in this window. */
12153 PT >= CHARPOS (startp)
12154 /* Selective display hasn't changed. */
12155 && !current_buffer->clip_changed
12156 /* Function force-mode-line-update is used to force a thorough
12157 redisplay. It sets either windows_or_buffers_changed or
12158 update_mode_lines. So don't take a shortcut here for these
12159 cases. */
12160 && !update_mode_lines
12161 && !windows_or_buffers_changed
12162 && !cursor_type_changed
12163 /* Can't use this case if highlighting a region. When a
12164 region exists, cursor movement has to do more than just
12165 set the cursor. */
12166 && !(!NILP (Vtransient_mark_mode)
12167 && !NILP (current_buffer->mark_active))
12168 && NILP (w->region_showing)
12169 && NILP (Vshow_trailing_whitespace)
12170 /* Right after splitting windows, last_point may be nil. */
12171 && INTEGERP (w->last_point)
12172 /* This code is not used for mini-buffer for the sake of the case
12173 of redisplaying to replace an echo area message; since in
12174 that case the mini-buffer contents per se are usually
12175 unchanged. This code is of no real use in the mini-buffer
12176 since the handling of this_line_start_pos, etc., in redisplay
12177 handles the same cases. */
12178 && !EQ (window, minibuf_window)
12179 /* When splitting windows or for new windows, it happens that
12180 redisplay is called with a nil window_end_vpos or one being
12181 larger than the window. This should really be fixed in
12182 window.c. I don't have this on my list, now, so we do
12183 approximately the same as the old redisplay code. --gerd. */
12184 && INTEGERP (w->window_end_vpos)
12185 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
12186 && (FRAME_WINDOW_P (f)
12187 || !overlay_arrow_in_current_buffer_p ()))
12188 {
12189 int this_scroll_margin, top_scroll_margin;
12190 struct glyph_row *row = NULL;
12191
12192 #if GLYPH_DEBUG
12193 debug_method_add (w, "cursor movement");
12194 #endif
12195
12196 /* Scroll if point within this distance from the top or bottom
12197 of the window. This is a pixel value. */
12198 this_scroll_margin = max (0, scroll_margin);
12199 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
12200 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
12201
12202 top_scroll_margin = this_scroll_margin;
12203 if (WINDOW_WANTS_HEADER_LINE_P (w))
12204 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
12205
12206 /* Start with the row the cursor was displayed during the last
12207 not paused redisplay. Give up if that row is not valid. */
12208 if (w->last_cursor.vpos < 0
12209 || w->last_cursor.vpos >= w->current_matrix->nrows)
12210 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12211 else
12212 {
12213 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
12214 if (row->mode_line_p)
12215 ++row;
12216 if (!row->enabled_p)
12217 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12218 }
12219
12220 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
12221 {
12222 int scroll_p = 0;
12223 int last_y = window_text_bottom_y (w) - this_scroll_margin;
12224
12225 if (PT > XFASTINT (w->last_point))
12226 {
12227 /* Point has moved forward. */
12228 while (MATRIX_ROW_END_CHARPOS (row) < PT
12229 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
12230 {
12231 xassert (row->enabled_p);
12232 ++row;
12233 }
12234
12235 /* The end position of a row equals the start position
12236 of the next row. If PT is there, we would rather
12237 display it in the next line. */
12238 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
12239 && MATRIX_ROW_END_CHARPOS (row) == PT
12240 && !cursor_row_p (w, row))
12241 ++row;
12242
12243 /* If within the scroll margin, scroll. Note that
12244 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
12245 the next line would be drawn, and that
12246 this_scroll_margin can be zero. */
12247 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
12248 || PT > MATRIX_ROW_END_CHARPOS (row)
12249 /* Line is completely visible last line in window
12250 and PT is to be set in the next line. */
12251 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
12252 && PT == MATRIX_ROW_END_CHARPOS (row)
12253 && !row->ends_at_zv_p
12254 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
12255 scroll_p = 1;
12256 }
12257 else if (PT < XFASTINT (w->last_point))
12258 {
12259 /* Cursor has to be moved backward. Note that PT >=
12260 CHARPOS (startp) because of the outer if-statement. */
12261 while (!row->mode_line_p
12262 && (MATRIX_ROW_START_CHARPOS (row) > PT
12263 || (MATRIX_ROW_START_CHARPOS (row) == PT
12264 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
12265 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
12266 row > w->current_matrix->rows
12267 && (row-1)->ends_in_newline_from_string_p))))
12268 && (row->y > top_scroll_margin
12269 || CHARPOS (startp) == BEGV))
12270 {
12271 xassert (row->enabled_p);
12272 --row;
12273 }
12274
12275 /* Consider the following case: Window starts at BEGV,
12276 there is invisible, intangible text at BEGV, so that
12277 display starts at some point START > BEGV. It can
12278 happen that we are called with PT somewhere between
12279 BEGV and START. Try to handle that case. */
12280 if (row < w->current_matrix->rows
12281 || row->mode_line_p)
12282 {
12283 row = w->current_matrix->rows;
12284 if (row->mode_line_p)
12285 ++row;
12286 }
12287
12288 /* Due to newlines in overlay strings, we may have to
12289 skip forward over overlay strings. */
12290 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
12291 && MATRIX_ROW_END_CHARPOS (row) == PT
12292 && !cursor_row_p (w, row))
12293 ++row;
12294
12295 /* If within the scroll margin, scroll. */
12296 if (row->y < top_scroll_margin
12297 && CHARPOS (startp) != BEGV)
12298 scroll_p = 1;
12299 }
12300 else
12301 {
12302 /* Cursor did not move. So don't scroll even if cursor line
12303 is partially visible, as it was so before. */
12304 rc = CURSOR_MOVEMENT_SUCCESS;
12305 }
12306
12307 if (PT < MATRIX_ROW_START_CHARPOS (row)
12308 || PT > MATRIX_ROW_END_CHARPOS (row))
12309 {
12310 /* if PT is not in the glyph row, give up. */
12311 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12312 }
12313 else if (rc != CURSOR_MOVEMENT_SUCCESS
12314 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
12315 && make_cursor_line_fully_visible_p)
12316 {
12317 if (PT == MATRIX_ROW_END_CHARPOS (row)
12318 && !row->ends_at_zv_p
12319 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
12320 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12321 else if (row->height > window_box_height (w))
12322 {
12323 /* If we end up in a partially visible line, let's
12324 make it fully visible, except when it's taller
12325 than the window, in which case we can't do much
12326 about it. */
12327 *scroll_step = 1;
12328 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12329 }
12330 else
12331 {
12332 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
12333 if (!cursor_row_fully_visible_p (w, 0, 1))
12334 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12335 else
12336 rc = CURSOR_MOVEMENT_SUCCESS;
12337 }
12338 }
12339 else if (scroll_p)
12340 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12341 else
12342 {
12343 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
12344 rc = CURSOR_MOVEMENT_SUCCESS;
12345 }
12346 }
12347 }
12348
12349 return rc;
12350 }
12351
12352 void
12353 set_vertical_scroll_bar (w)
12354 struct window *w;
12355 {
12356 int start, end, whole;
12357
12358 /* Calculate the start and end positions for the current window.
12359 At some point, it would be nice to choose between scrollbars
12360 which reflect the whole buffer size, with special markers
12361 indicating narrowing, and scrollbars which reflect only the
12362 visible region.
12363
12364 Note that mini-buffers sometimes aren't displaying any text. */
12365 if (!MINI_WINDOW_P (w)
12366 || (w == XWINDOW (minibuf_window)
12367 && NILP (echo_area_buffer[0])))
12368 {
12369 struct buffer *buf = XBUFFER (w->buffer);
12370 whole = BUF_ZV (buf) - BUF_BEGV (buf);
12371 start = marker_position (w->start) - BUF_BEGV (buf);
12372 /* I don't think this is guaranteed to be right. For the
12373 moment, we'll pretend it is. */
12374 end = BUF_Z (buf) - XFASTINT (w->window_end_pos) - BUF_BEGV (buf);
12375
12376 if (end < start)
12377 end = start;
12378 if (whole < (end - start))
12379 whole = end - start;
12380 }
12381 else
12382 start = end = whole = 0;
12383
12384 /* Indicate what this scroll bar ought to be displaying now. */
12385 set_vertical_scroll_bar_hook (w, end - start, whole, start);
12386 }
12387
12388
12389 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
12390 selected_window is redisplayed.
12391
12392 We can return without actually redisplaying the window if
12393 fonts_changed_p is nonzero. In that case, redisplay_internal will
12394 retry. */
12395
12396 static void
12397 redisplay_window (window, just_this_one_p)
12398 Lisp_Object window;
12399 int just_this_one_p;
12400 {
12401 struct window *w = XWINDOW (window);
12402 struct frame *f = XFRAME (w->frame);
12403 struct buffer *buffer = XBUFFER (w->buffer);
12404 struct buffer *old = current_buffer;
12405 struct text_pos lpoint, opoint, startp;
12406 int update_mode_line;
12407 int tem;
12408 struct it it;
12409 /* Record it now because it's overwritten. */
12410 int current_matrix_up_to_date_p = 0;
12411 int used_current_matrix_p = 0;
12412 /* This is less strict than current_matrix_up_to_date_p.
12413 It indictes that the buffer contents and narrowing are unchanged. */
12414 int buffer_unchanged_p = 0;
12415 int temp_scroll_step = 0;
12416 int count = SPECPDL_INDEX ();
12417 int rc;
12418 int centering_position = -1;
12419 int last_line_misfit = 0;
12420
12421 SET_TEXT_POS (lpoint, PT, PT_BYTE);
12422 opoint = lpoint;
12423
12424 /* W must be a leaf window here. */
12425 xassert (!NILP (w->buffer));
12426 #if GLYPH_DEBUG
12427 *w->desired_matrix->method = 0;
12428 #endif
12429
12430 specbind (Qinhibit_point_motion_hooks, Qt);
12431
12432 reconsider_clip_changes (w, buffer);
12433
12434 /* Has the mode line to be updated? */
12435 update_mode_line = (!NILP (w->update_mode_line)
12436 || update_mode_lines
12437 || buffer->clip_changed
12438 || buffer->prevent_redisplay_optimizations_p);
12439
12440 if (MINI_WINDOW_P (w))
12441 {
12442 if (w == XWINDOW (echo_area_window)
12443 && !NILP (echo_area_buffer[0]))
12444 {
12445 if (update_mode_line)
12446 /* We may have to update a tty frame's menu bar or a
12447 tool-bar. Example `M-x C-h C-h C-g'. */
12448 goto finish_menu_bars;
12449 else
12450 /* We've already displayed the echo area glyphs in this window. */
12451 goto finish_scroll_bars;
12452 }
12453 else if ((w != XWINDOW (minibuf_window)
12454 || minibuf_level == 0)
12455 /* When buffer is nonempty, redisplay window normally. */
12456 && BUF_Z (XBUFFER (w->buffer)) == BUF_BEG (XBUFFER (w->buffer))
12457 /* Quail displays non-mini buffers in minibuffer window.
12458 In that case, redisplay the window normally. */
12459 && !NILP (Fmemq (w->buffer, Vminibuffer_list)))
12460 {
12461 /* W is a mini-buffer window, but it's not active, so clear
12462 it. */
12463 int yb = window_text_bottom_y (w);
12464 struct glyph_row *row;
12465 int y;
12466
12467 for (y = 0, row = w->desired_matrix->rows;
12468 y < yb;
12469 y += row->height, ++row)
12470 blank_row (w, row, y);
12471 goto finish_scroll_bars;
12472 }
12473
12474 clear_glyph_matrix (w->desired_matrix);
12475 }
12476
12477 /* Otherwise set up data on this window; select its buffer and point
12478 value. */
12479 /* Really select the buffer, for the sake of buffer-local
12480 variables. */
12481 set_buffer_internal_1 (XBUFFER (w->buffer));
12482 SET_TEXT_POS (opoint, PT, PT_BYTE);
12483
12484 current_matrix_up_to_date_p
12485 = (!NILP (w->window_end_valid)
12486 && !current_buffer->clip_changed
12487 && !current_buffer->prevent_redisplay_optimizations_p
12488 && XFASTINT (w->last_modified) >= MODIFF
12489 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
12490
12491 buffer_unchanged_p
12492 = (!NILP (w->window_end_valid)
12493 && !current_buffer->clip_changed
12494 && XFASTINT (w->last_modified) >= MODIFF
12495 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
12496
12497 /* When windows_or_buffers_changed is non-zero, we can't rely on
12498 the window end being valid, so set it to nil there. */
12499 if (windows_or_buffers_changed)
12500 {
12501 /* If window starts on a continuation line, maybe adjust the
12502 window start in case the window's width changed. */
12503 if (XMARKER (w->start)->buffer == current_buffer)
12504 compute_window_start_on_continuation_line (w);
12505
12506 w->window_end_valid = Qnil;
12507 }
12508
12509 /* Some sanity checks. */
12510 CHECK_WINDOW_END (w);
12511 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
12512 abort ();
12513 if (BYTEPOS (opoint) < CHARPOS (opoint))
12514 abort ();
12515
12516 /* If %c is in mode line, update it if needed. */
12517 if (!NILP (w->column_number_displayed)
12518 /* This alternative quickly identifies a common case
12519 where no change is needed. */
12520 && !(PT == XFASTINT (w->last_point)
12521 && XFASTINT (w->last_modified) >= MODIFF
12522 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
12523 && (XFASTINT (w->column_number_displayed)
12524 != (int) current_column ())) /* iftc */
12525 update_mode_line = 1;
12526
12527 /* Count number of windows showing the selected buffer. An indirect
12528 buffer counts as its base buffer. */
12529 if (!just_this_one_p)
12530 {
12531 struct buffer *current_base, *window_base;
12532 current_base = current_buffer;
12533 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
12534 if (current_base->base_buffer)
12535 current_base = current_base->base_buffer;
12536 if (window_base->base_buffer)
12537 window_base = window_base->base_buffer;
12538 if (current_base == window_base)
12539 buffer_shared++;
12540 }
12541
12542 /* Point refers normally to the selected window. For any other
12543 window, set up appropriate value. */
12544 if (!EQ (window, selected_window))
12545 {
12546 int new_pt = XMARKER (w->pointm)->charpos;
12547 int new_pt_byte = marker_byte_position (w->pointm);
12548 if (new_pt < BEGV)
12549 {
12550 new_pt = BEGV;
12551 new_pt_byte = BEGV_BYTE;
12552 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
12553 }
12554 else if (new_pt > (ZV - 1))
12555 {
12556 new_pt = ZV;
12557 new_pt_byte = ZV_BYTE;
12558 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
12559 }
12560
12561 /* We don't use SET_PT so that the point-motion hooks don't run. */
12562 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
12563 }
12564
12565 /* If any of the character widths specified in the display table
12566 have changed, invalidate the width run cache. It's true that
12567 this may be a bit late to catch such changes, but the rest of
12568 redisplay goes (non-fatally) haywire when the display table is
12569 changed, so why should we worry about doing any better? */
12570 if (current_buffer->width_run_cache)
12571 {
12572 struct Lisp_Char_Table *disptab = buffer_display_table ();
12573
12574 if (! disptab_matches_widthtab (disptab,
12575 XVECTOR (current_buffer->width_table)))
12576 {
12577 invalidate_region_cache (current_buffer,
12578 current_buffer->width_run_cache,
12579 BEG, Z);
12580 recompute_width_table (current_buffer, disptab);
12581 }
12582 }
12583
12584 /* If window-start is screwed up, choose a new one. */
12585 if (XMARKER (w->start)->buffer != current_buffer)
12586 goto recenter;
12587
12588 SET_TEXT_POS_FROM_MARKER (startp, w->start);
12589
12590 /* If someone specified a new starting point but did not insist,
12591 check whether it can be used. */
12592 if (!NILP (w->optional_new_start)
12593 && CHARPOS (startp) >= BEGV
12594 && CHARPOS (startp) <= ZV)
12595 {
12596 w->optional_new_start = Qnil;
12597 start_display (&it, w, startp);
12598 move_it_to (&it, PT, 0, it.last_visible_y, -1,
12599 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
12600 if (IT_CHARPOS (it) == PT)
12601 w->force_start = Qt;
12602 /* IT may overshoot PT if text at PT is invisible. */
12603 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
12604 w->force_start = Qt;
12605
12606
12607 }
12608
12609 /* Handle case where place to start displaying has been specified,
12610 unless the specified location is outside the accessible range. */
12611 if (!NILP (w->force_start)
12612 || w->frozen_window_start_p)
12613 {
12614 /* We set this later on if we have to adjust point. */
12615 int new_vpos = -1;
12616 int val;
12617
12618 w->force_start = Qnil;
12619 w->vscroll = 0;
12620 w->window_end_valid = Qnil;
12621
12622 /* Forget any recorded base line for line number display. */
12623 if (!buffer_unchanged_p)
12624 w->base_line_number = Qnil;
12625
12626 /* Redisplay the mode line. Select the buffer properly for that.
12627 Also, run the hook window-scroll-functions
12628 because we have scrolled. */
12629 /* Note, we do this after clearing force_start because
12630 if there's an error, it is better to forget about force_start
12631 than to get into an infinite loop calling the hook functions
12632 and having them get more errors. */
12633 if (!update_mode_line
12634 || ! NILP (Vwindow_scroll_functions))
12635 {
12636 update_mode_line = 1;
12637 w->update_mode_line = Qt;
12638 startp = run_window_scroll_functions (window, startp);
12639 }
12640
12641 w->last_modified = make_number (0);
12642 w->last_overlay_modified = make_number (0);
12643 if (CHARPOS (startp) < BEGV)
12644 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
12645 else if (CHARPOS (startp) > ZV)
12646 SET_TEXT_POS (startp, ZV, ZV_BYTE);
12647
12648 /* Redisplay, then check if cursor has been set during the
12649 redisplay. Give up if new fonts were loaded. */
12650 val = try_window (window, startp, 1);
12651 if (!val)
12652 {
12653 w->force_start = Qt;
12654 clear_glyph_matrix (w->desired_matrix);
12655 goto need_larger_matrices;
12656 }
12657 /* Point was outside the scroll margins. */
12658 if (val < 0)
12659 new_vpos = window_box_height (w) / 2;
12660
12661 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
12662 {
12663 /* If point does not appear, try to move point so it does
12664 appear. The desired matrix has been built above, so we
12665 can use it here. */
12666 new_vpos = window_box_height (w) / 2;
12667 }
12668
12669 if (!cursor_row_fully_visible_p (w, 0, 0))
12670 {
12671 /* Point does appear, but on a line partly visible at end of window.
12672 Move it back to a fully-visible line. */
12673 new_vpos = window_box_height (w);
12674 }
12675
12676 /* If we need to move point for either of the above reasons,
12677 now actually do it. */
12678 if (new_vpos >= 0)
12679 {
12680 struct glyph_row *row;
12681
12682 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
12683 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
12684 ++row;
12685
12686 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
12687 MATRIX_ROW_START_BYTEPOS (row));
12688
12689 if (w != XWINDOW (selected_window))
12690 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
12691 else if (current_buffer == old)
12692 SET_TEXT_POS (lpoint, PT, PT_BYTE);
12693
12694 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
12695
12696 /* If we are highlighting the region, then we just changed
12697 the region, so redisplay to show it. */
12698 if (!NILP (Vtransient_mark_mode)
12699 && !NILP (current_buffer->mark_active))
12700 {
12701 clear_glyph_matrix (w->desired_matrix);
12702 if (!try_window (window, startp, 0))
12703 goto need_larger_matrices;
12704 }
12705 }
12706
12707 #if GLYPH_DEBUG
12708 debug_method_add (w, "forced window start");
12709 #endif
12710 goto done;
12711 }
12712
12713 /* Handle case where text has not changed, only point, and it has
12714 not moved off the frame, and we are not retrying after hscroll.
12715 (current_matrix_up_to_date_p is nonzero when retrying.) */
12716 if (current_matrix_up_to_date_p
12717 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
12718 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
12719 {
12720 switch (rc)
12721 {
12722 case CURSOR_MOVEMENT_SUCCESS:
12723 used_current_matrix_p = 1;
12724 goto done;
12725
12726 #if 0 /* try_cursor_movement never returns this value. */
12727 case CURSOR_MOVEMENT_NEED_LARGER_MATRICES:
12728 goto need_larger_matrices;
12729 #endif
12730
12731 case CURSOR_MOVEMENT_MUST_SCROLL:
12732 goto try_to_scroll;
12733
12734 default:
12735 abort ();
12736 }
12737 }
12738 /* If current starting point was originally the beginning of a line
12739 but no longer is, find a new starting point. */
12740 else if (!NILP (w->start_at_line_beg)
12741 && !(CHARPOS (startp) <= BEGV
12742 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
12743 {
12744 #if GLYPH_DEBUG
12745 debug_method_add (w, "recenter 1");
12746 #endif
12747 goto recenter;
12748 }
12749
12750 /* Try scrolling with try_window_id. Value is > 0 if update has
12751 been done, it is -1 if we know that the same window start will
12752 not work. It is 0 if unsuccessful for some other reason. */
12753 else if ((tem = try_window_id (w)) != 0)
12754 {
12755 #if GLYPH_DEBUG
12756 debug_method_add (w, "try_window_id %d", tem);
12757 #endif
12758
12759 if (fonts_changed_p)
12760 goto need_larger_matrices;
12761 if (tem > 0)
12762 goto done;
12763
12764 /* Otherwise try_window_id has returned -1 which means that we
12765 don't want the alternative below this comment to execute. */
12766 }
12767 else if (CHARPOS (startp) >= BEGV
12768 && CHARPOS (startp) <= ZV
12769 && PT >= CHARPOS (startp)
12770 && (CHARPOS (startp) < ZV
12771 /* Avoid starting at end of buffer. */
12772 || CHARPOS (startp) == BEGV
12773 || (XFASTINT (w->last_modified) >= MODIFF
12774 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
12775 {
12776 #if GLYPH_DEBUG
12777 debug_method_add (w, "same window start");
12778 #endif
12779
12780 /* Try to redisplay starting at same place as before.
12781 If point has not moved off frame, accept the results. */
12782 if (!current_matrix_up_to_date_p
12783 /* Don't use try_window_reusing_current_matrix in this case
12784 because a window scroll function can have changed the
12785 buffer. */
12786 || !NILP (Vwindow_scroll_functions)
12787 || MINI_WINDOW_P (w)
12788 || !(used_current_matrix_p
12789 = try_window_reusing_current_matrix (w)))
12790 {
12791 IF_DEBUG (debug_method_add (w, "1"));
12792 if (try_window (window, startp, 1) < 0)
12793 /* -1 means we need to scroll.
12794 0 means we need new matrices, but fonts_changed_p
12795 is set in that case, so we will detect it below. */
12796 goto try_to_scroll;
12797 }
12798
12799 if (fonts_changed_p)
12800 goto need_larger_matrices;
12801
12802 if (w->cursor.vpos >= 0)
12803 {
12804 if (!just_this_one_p
12805 || current_buffer->clip_changed
12806 || BEG_UNCHANGED < CHARPOS (startp))
12807 /* Forget any recorded base line for line number display. */
12808 w->base_line_number = Qnil;
12809
12810 if (!cursor_row_fully_visible_p (w, 1, 0))
12811 {
12812 clear_glyph_matrix (w->desired_matrix);
12813 last_line_misfit = 1;
12814 }
12815 /* Drop through and scroll. */
12816 else
12817 goto done;
12818 }
12819 else
12820 clear_glyph_matrix (w->desired_matrix);
12821 }
12822
12823 try_to_scroll:
12824
12825 w->last_modified = make_number (0);
12826 w->last_overlay_modified = make_number (0);
12827
12828 /* Redisplay the mode line. Select the buffer properly for that. */
12829 if (!update_mode_line)
12830 {
12831 update_mode_line = 1;
12832 w->update_mode_line = Qt;
12833 }
12834
12835 /* Try to scroll by specified few lines. */
12836 if ((scroll_conservatively
12837 || scroll_step
12838 || temp_scroll_step
12839 || NUMBERP (current_buffer->scroll_up_aggressively)
12840 || NUMBERP (current_buffer->scroll_down_aggressively))
12841 && !current_buffer->clip_changed
12842 && CHARPOS (startp) >= BEGV
12843 && CHARPOS (startp) <= ZV)
12844 {
12845 /* The function returns -1 if new fonts were loaded, 1 if
12846 successful, 0 if not successful. */
12847 int rc = try_scrolling (window, just_this_one_p,
12848 scroll_conservatively,
12849 scroll_step,
12850 temp_scroll_step, last_line_misfit);
12851 switch (rc)
12852 {
12853 case SCROLLING_SUCCESS:
12854 goto done;
12855
12856 case SCROLLING_NEED_LARGER_MATRICES:
12857 goto need_larger_matrices;
12858
12859 case SCROLLING_FAILED:
12860 break;
12861
12862 default:
12863 abort ();
12864 }
12865 }
12866
12867 /* Finally, just choose place to start which centers point */
12868
12869 recenter:
12870 if (centering_position < 0)
12871 centering_position = window_box_height (w) / 2;
12872
12873 #if GLYPH_DEBUG
12874 debug_method_add (w, "recenter");
12875 #endif
12876
12877 /* w->vscroll = 0; */
12878
12879 /* Forget any previously recorded base line for line number display. */
12880 if (!buffer_unchanged_p)
12881 w->base_line_number = Qnil;
12882
12883 /* Move backward half the height of the window. */
12884 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
12885 it.current_y = it.last_visible_y;
12886 move_it_vertically_backward (&it, centering_position);
12887 xassert (IT_CHARPOS (it) >= BEGV);
12888
12889 /* The function move_it_vertically_backward may move over more
12890 than the specified y-distance. If it->w is small, e.g. a
12891 mini-buffer window, we may end up in front of the window's
12892 display area. Start displaying at the start of the line
12893 containing PT in this case. */
12894 if (it.current_y <= 0)
12895 {
12896 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
12897 move_it_vertically_backward (&it, 0);
12898 #if 0
12899 /* I think this assert is bogus if buffer contains
12900 invisible text or images. KFS. */
12901 xassert (IT_CHARPOS (it) <= PT);
12902 #endif
12903 it.current_y = 0;
12904 }
12905
12906 it.current_x = it.hpos = 0;
12907
12908 /* Set startp here explicitly in case that helps avoid an infinite loop
12909 in case the window-scroll-functions functions get errors. */
12910 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
12911
12912 /* Run scroll hooks. */
12913 startp = run_window_scroll_functions (window, it.current.pos);
12914
12915 /* Redisplay the window. */
12916 if (!current_matrix_up_to_date_p
12917 || windows_or_buffers_changed
12918 || cursor_type_changed
12919 /* Don't use try_window_reusing_current_matrix in this case
12920 because it can have changed the buffer. */
12921 || !NILP (Vwindow_scroll_functions)
12922 || !just_this_one_p
12923 || MINI_WINDOW_P (w)
12924 || !(used_current_matrix_p
12925 = try_window_reusing_current_matrix (w)))
12926 try_window (window, startp, 0);
12927
12928 /* If new fonts have been loaded (due to fontsets), give up. We
12929 have to start a new redisplay since we need to re-adjust glyph
12930 matrices. */
12931 if (fonts_changed_p)
12932 goto need_larger_matrices;
12933
12934 /* If cursor did not appear assume that the middle of the window is
12935 in the first line of the window. Do it again with the next line.
12936 (Imagine a window of height 100, displaying two lines of height
12937 60. Moving back 50 from it->last_visible_y will end in the first
12938 line.) */
12939 if (w->cursor.vpos < 0)
12940 {
12941 if (!NILP (w->window_end_valid)
12942 && PT >= Z - XFASTINT (w->window_end_pos))
12943 {
12944 clear_glyph_matrix (w->desired_matrix);
12945 move_it_by_lines (&it, 1, 0);
12946 try_window (window, it.current.pos, 0);
12947 }
12948 else if (PT < IT_CHARPOS (it))
12949 {
12950 clear_glyph_matrix (w->desired_matrix);
12951 move_it_by_lines (&it, -1, 0);
12952 try_window (window, it.current.pos, 0);
12953 }
12954 else
12955 {
12956 /* Not much we can do about it. */
12957 }
12958 }
12959
12960 /* Consider the following case: Window starts at BEGV, there is
12961 invisible, intangible text at BEGV, so that display starts at
12962 some point START > BEGV. It can happen that we are called with
12963 PT somewhere between BEGV and START. Try to handle that case. */
12964 if (w->cursor.vpos < 0)
12965 {
12966 struct glyph_row *row = w->current_matrix->rows;
12967 if (row->mode_line_p)
12968 ++row;
12969 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
12970 }
12971
12972 if (!cursor_row_fully_visible_p (w, 0, 0))
12973 {
12974 /* If vscroll is enabled, disable it and try again. */
12975 if (w->vscroll)
12976 {
12977 w->vscroll = 0;
12978 clear_glyph_matrix (w->desired_matrix);
12979 goto recenter;
12980 }
12981
12982 /* If centering point failed to make the whole line visible,
12983 put point at the top instead. That has to make the whole line
12984 visible, if it can be done. */
12985 if (centering_position == 0)
12986 goto done;
12987
12988 clear_glyph_matrix (w->desired_matrix);
12989 centering_position = 0;
12990 goto recenter;
12991 }
12992
12993 done:
12994
12995 SET_TEXT_POS_FROM_MARKER (startp, w->start);
12996 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
12997 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
12998 ? Qt : Qnil);
12999
13000 /* Display the mode line, if we must. */
13001 if ((update_mode_line
13002 /* If window not full width, must redo its mode line
13003 if (a) the window to its side is being redone and
13004 (b) we do a frame-based redisplay. This is a consequence
13005 of how inverted lines are drawn in frame-based redisplay. */
13006 || (!just_this_one_p
13007 && !FRAME_WINDOW_P (f)
13008 && !WINDOW_FULL_WIDTH_P (w))
13009 /* Line number to display. */
13010 || INTEGERP (w->base_line_pos)
13011 /* Column number is displayed and different from the one displayed. */
13012 || (!NILP (w->column_number_displayed)
13013 && (XFASTINT (w->column_number_displayed)
13014 != (int) current_column ()))) /* iftc */
13015 /* This means that the window has a mode line. */
13016 && (WINDOW_WANTS_MODELINE_P (w)
13017 || WINDOW_WANTS_HEADER_LINE_P (w)))
13018 {
13019 display_mode_lines (w);
13020
13021 /* If mode line height has changed, arrange for a thorough
13022 immediate redisplay using the correct mode line height. */
13023 if (WINDOW_WANTS_MODELINE_P (w)
13024 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
13025 {
13026 fonts_changed_p = 1;
13027 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
13028 = DESIRED_MODE_LINE_HEIGHT (w);
13029 }
13030
13031 /* If top line height has changed, arrange for a thorough
13032 immediate redisplay using the correct mode line height. */
13033 if (WINDOW_WANTS_HEADER_LINE_P (w)
13034 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
13035 {
13036 fonts_changed_p = 1;
13037 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
13038 = DESIRED_HEADER_LINE_HEIGHT (w);
13039 }
13040
13041 if (fonts_changed_p)
13042 goto need_larger_matrices;
13043 }
13044
13045 if (!line_number_displayed
13046 && !BUFFERP (w->base_line_pos))
13047 {
13048 w->base_line_pos = Qnil;
13049 w->base_line_number = Qnil;
13050 }
13051
13052 finish_menu_bars:
13053
13054 /* When we reach a frame's selected window, redo the frame's menu bar. */
13055 if (update_mode_line
13056 && EQ (FRAME_SELECTED_WINDOW (f), window))
13057 {
13058 int redisplay_menu_p = 0;
13059 int redisplay_tool_bar_p = 0;
13060
13061 if (FRAME_WINDOW_P (f))
13062 {
13063 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
13064 || defined (USE_GTK)
13065 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
13066 #else
13067 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
13068 #endif
13069 }
13070 else
13071 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
13072
13073 if (redisplay_menu_p)
13074 display_menu_bar (w);
13075
13076 #ifdef HAVE_WINDOW_SYSTEM
13077 #ifdef USE_GTK
13078 redisplay_tool_bar_p = FRAME_EXTERNAL_TOOL_BAR (f);
13079 #else
13080 redisplay_tool_bar_p = WINDOWP (f->tool_bar_window)
13081 && (FRAME_TOOL_BAR_LINES (f) > 0
13082 || auto_resize_tool_bars_p);
13083
13084 #endif
13085
13086 if (redisplay_tool_bar_p)
13087 redisplay_tool_bar (f);
13088 #endif
13089 }
13090
13091 #ifdef HAVE_WINDOW_SYSTEM
13092 if (FRAME_WINDOW_P (f)
13093 && update_window_fringes (w, (just_this_one_p
13094 || (!used_current_matrix_p && !overlay_arrow_seen)
13095 || w->pseudo_window_p)))
13096 {
13097 update_begin (f);
13098 BLOCK_INPUT;
13099 if (draw_window_fringes (w, 1))
13100 x_draw_vertical_border (w);
13101 UNBLOCK_INPUT;
13102 update_end (f);
13103 }
13104 #endif /* HAVE_WINDOW_SYSTEM */
13105
13106 /* We go to this label, with fonts_changed_p nonzero,
13107 if it is necessary to try again using larger glyph matrices.
13108 We have to redeem the scroll bar even in this case,
13109 because the loop in redisplay_internal expects that. */
13110 need_larger_matrices:
13111 ;
13112 finish_scroll_bars:
13113
13114 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
13115 {
13116 /* Set the thumb's position and size. */
13117 set_vertical_scroll_bar (w);
13118
13119 /* Note that we actually used the scroll bar attached to this
13120 window, so it shouldn't be deleted at the end of redisplay. */
13121 redeem_scroll_bar_hook (w);
13122 }
13123
13124 /* Restore current_buffer and value of point in it. */
13125 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
13126 set_buffer_internal_1 (old);
13127 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
13128
13129 unbind_to (count, Qnil);
13130 }
13131
13132
13133 /* Build the complete desired matrix of WINDOW with a window start
13134 buffer position POS.
13135
13136 Value is 1 if successful. It is zero if fonts were loaded during
13137 redisplay which makes re-adjusting glyph matrices necessary, and -1
13138 if point would appear in the scroll margins.
13139 (We check that only if CHECK_MARGINS is nonzero. */
13140
13141 int
13142 try_window (window, pos, check_margins)
13143 Lisp_Object window;
13144 struct text_pos pos;
13145 int check_margins;
13146 {
13147 struct window *w = XWINDOW (window);
13148 struct it it;
13149 struct glyph_row *last_text_row = NULL;
13150
13151 /* Make POS the new window start. */
13152 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
13153
13154 /* Mark cursor position as unknown. No overlay arrow seen. */
13155 w->cursor.vpos = -1;
13156 overlay_arrow_seen = 0;
13157
13158 /* Initialize iterator and info to start at POS. */
13159 start_display (&it, w, pos);
13160
13161 /* Display all lines of W. */
13162 while (it.current_y < it.last_visible_y)
13163 {
13164 if (display_line (&it))
13165 last_text_row = it.glyph_row - 1;
13166 if (fonts_changed_p)
13167 return 0;
13168 }
13169
13170 /* Don't let the cursor end in the scroll margins. */
13171 if (check_margins
13172 && !MINI_WINDOW_P (w))
13173 {
13174 int this_scroll_margin;
13175
13176 this_scroll_margin = max (0, scroll_margin);
13177 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
13178 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
13179
13180 if ((w->cursor.y < this_scroll_margin
13181 && CHARPOS (pos) > BEGV
13182 && IT_CHARPOS (it) < ZV)
13183 /* rms: considering make_cursor_line_fully_visible_p here
13184 seems to give wrong results. We don't want to recenter
13185 when the last line is partly visible, we want to allow
13186 that case to be handled in the usual way. */
13187 || (w->cursor.y + 1) > it.last_visible_y)
13188 {
13189 w->cursor.vpos = -1;
13190 clear_glyph_matrix (w->desired_matrix);
13191 return -1;
13192 }
13193 }
13194
13195 /* If bottom moved off end of frame, change mode line percentage. */
13196 if (XFASTINT (w->window_end_pos) <= 0
13197 && Z != IT_CHARPOS (it))
13198 w->update_mode_line = Qt;
13199
13200 /* Set window_end_pos to the offset of the last character displayed
13201 on the window from the end of current_buffer. Set
13202 window_end_vpos to its row number. */
13203 if (last_text_row)
13204 {
13205 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
13206 w->window_end_bytepos
13207 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
13208 w->window_end_pos
13209 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
13210 w->window_end_vpos
13211 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
13212 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
13213 ->displays_text_p);
13214 }
13215 else
13216 {
13217 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
13218 w->window_end_pos = make_number (Z - ZV);
13219 w->window_end_vpos = make_number (0);
13220 }
13221
13222 /* But that is not valid info until redisplay finishes. */
13223 w->window_end_valid = Qnil;
13224 return 1;
13225 }
13226
13227
13228 \f
13229 /************************************************************************
13230 Window redisplay reusing current matrix when buffer has not changed
13231 ************************************************************************/
13232
13233 /* Try redisplay of window W showing an unchanged buffer with a
13234 different window start than the last time it was displayed by
13235 reusing its current matrix. Value is non-zero if successful.
13236 W->start is the new window start. */
13237
13238 static int
13239 try_window_reusing_current_matrix (w)
13240 struct window *w;
13241 {
13242 struct frame *f = XFRAME (w->frame);
13243 struct glyph_row *row, *bottom_row;
13244 struct it it;
13245 struct run run;
13246 struct text_pos start, new_start;
13247 int nrows_scrolled, i;
13248 struct glyph_row *last_text_row;
13249 struct glyph_row *last_reused_text_row;
13250 struct glyph_row *start_row;
13251 int start_vpos, min_y, max_y;
13252
13253 #if GLYPH_DEBUG
13254 if (inhibit_try_window_reusing)
13255 return 0;
13256 #endif
13257
13258 if (/* This function doesn't handle terminal frames. */
13259 !FRAME_WINDOW_P (f)
13260 /* Don't try to reuse the display if windows have been split
13261 or such. */
13262 || windows_or_buffers_changed
13263 || cursor_type_changed)
13264 return 0;
13265
13266 /* Can't do this if region may have changed. */
13267 if ((!NILP (Vtransient_mark_mode)
13268 && !NILP (current_buffer->mark_active))
13269 || !NILP (w->region_showing)
13270 || !NILP (Vshow_trailing_whitespace))
13271 return 0;
13272
13273 /* If top-line visibility has changed, give up. */
13274 if (WINDOW_WANTS_HEADER_LINE_P (w)
13275 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
13276 return 0;
13277
13278 /* Give up if old or new display is scrolled vertically. We could
13279 make this function handle this, but right now it doesn't. */
13280 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
13281 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
13282 return 0;
13283
13284 /* The variable new_start now holds the new window start. The old
13285 start `start' can be determined from the current matrix. */
13286 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
13287 start = start_row->start.pos;
13288 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
13289
13290 /* Clear the desired matrix for the display below. */
13291 clear_glyph_matrix (w->desired_matrix);
13292
13293 if (CHARPOS (new_start) <= CHARPOS (start))
13294 {
13295 int first_row_y;
13296
13297 /* Don't use this method if the display starts with an ellipsis
13298 displayed for invisible text. It's not easy to handle that case
13299 below, and it's certainly not worth the effort since this is
13300 not a frequent case. */
13301 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
13302 return 0;
13303
13304 IF_DEBUG (debug_method_add (w, "twu1"));
13305
13306 /* Display up to a row that can be reused. The variable
13307 last_text_row is set to the last row displayed that displays
13308 text. Note that it.vpos == 0 if or if not there is a
13309 header-line; it's not the same as the MATRIX_ROW_VPOS! */
13310 start_display (&it, w, new_start);
13311 first_row_y = it.current_y;
13312 w->cursor.vpos = -1;
13313 last_text_row = last_reused_text_row = NULL;
13314
13315 while (it.current_y < it.last_visible_y
13316 && !fonts_changed_p)
13317 {
13318 /* If we have reached into the characters in the START row,
13319 that means the line boundaries have changed. So we
13320 can't start copying with the row START. Maybe it will
13321 work to start copying with the following row. */
13322 while (IT_CHARPOS (it) > CHARPOS (start))
13323 {
13324 /* Advance to the next row as the "start". */
13325 start_row++;
13326 start = start_row->start.pos;
13327 /* If there are no more rows to try, or just one, give up. */
13328 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
13329 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
13330 || CHARPOS (start) == ZV)
13331 {
13332 clear_glyph_matrix (w->desired_matrix);
13333 return 0;
13334 }
13335
13336 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
13337 }
13338 /* If we have reached alignment,
13339 we can copy the rest of the rows. */
13340 if (IT_CHARPOS (it) == CHARPOS (start))
13341 break;
13342
13343 if (display_line (&it))
13344 last_text_row = it.glyph_row - 1;
13345 }
13346
13347 /* A value of current_y < last_visible_y means that we stopped
13348 at the previous window start, which in turn means that we
13349 have at least one reusable row. */
13350 if (it.current_y < it.last_visible_y)
13351 {
13352 /* IT.vpos always starts from 0; it counts text lines. */
13353 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
13354
13355 /* Find PT if not already found in the lines displayed. */
13356 if (w->cursor.vpos < 0)
13357 {
13358 int dy = it.current_y - start_row->y;
13359
13360 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
13361 row = row_containing_pos (w, PT, row, NULL, dy);
13362 if (row)
13363 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
13364 dy, nrows_scrolled);
13365 else
13366 {
13367 clear_glyph_matrix (w->desired_matrix);
13368 return 0;
13369 }
13370 }
13371
13372 /* Scroll the display. Do it before the current matrix is
13373 changed. The problem here is that update has not yet
13374 run, i.e. part of the current matrix is not up to date.
13375 scroll_run_hook will clear the cursor, and use the
13376 current matrix to get the height of the row the cursor is
13377 in. */
13378 run.current_y = start_row->y;
13379 run.desired_y = it.current_y;
13380 run.height = it.last_visible_y - it.current_y;
13381
13382 if (run.height > 0 && run.current_y != run.desired_y)
13383 {
13384 update_begin (f);
13385 rif->update_window_begin_hook (w);
13386 rif->clear_window_mouse_face (w);
13387 rif->scroll_run_hook (w, &run);
13388 rif->update_window_end_hook (w, 0, 0);
13389 update_end (f);
13390 }
13391
13392 /* Shift current matrix down by nrows_scrolled lines. */
13393 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
13394 rotate_matrix (w->current_matrix,
13395 start_vpos,
13396 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
13397 nrows_scrolled);
13398
13399 /* Disable lines that must be updated. */
13400 for (i = 0; i < it.vpos; ++i)
13401 (start_row + i)->enabled_p = 0;
13402
13403 /* Re-compute Y positions. */
13404 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
13405 max_y = it.last_visible_y;
13406 for (row = start_row + nrows_scrolled;
13407 row < bottom_row;
13408 ++row)
13409 {
13410 row->y = it.current_y;
13411 row->visible_height = row->height;
13412
13413 if (row->y < min_y)
13414 row->visible_height -= min_y - row->y;
13415 if (row->y + row->height > max_y)
13416 row->visible_height -= row->y + row->height - max_y;
13417 row->redraw_fringe_bitmaps_p = 1;
13418
13419 it.current_y += row->height;
13420
13421 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
13422 last_reused_text_row = row;
13423 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
13424 break;
13425 }
13426
13427 /* Disable lines in the current matrix which are now
13428 below the window. */
13429 for (++row; row < bottom_row; ++row)
13430 row->enabled_p = row->mode_line_p = 0;
13431 }
13432
13433 /* Update window_end_pos etc.; last_reused_text_row is the last
13434 reused row from the current matrix containing text, if any.
13435 The value of last_text_row is the last displayed line
13436 containing text. */
13437 if (last_reused_text_row)
13438 {
13439 w->window_end_bytepos
13440 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
13441 w->window_end_pos
13442 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
13443 w->window_end_vpos
13444 = make_number (MATRIX_ROW_VPOS (last_reused_text_row,
13445 w->current_matrix));
13446 }
13447 else if (last_text_row)
13448 {
13449 w->window_end_bytepos
13450 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
13451 w->window_end_pos
13452 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
13453 w->window_end_vpos
13454 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
13455 }
13456 else
13457 {
13458 /* This window must be completely empty. */
13459 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
13460 w->window_end_pos = make_number (Z - ZV);
13461 w->window_end_vpos = make_number (0);
13462 }
13463 w->window_end_valid = Qnil;
13464
13465 /* Update hint: don't try scrolling again in update_window. */
13466 w->desired_matrix->no_scrolling_p = 1;
13467
13468 #if GLYPH_DEBUG
13469 debug_method_add (w, "try_window_reusing_current_matrix 1");
13470 #endif
13471 return 1;
13472 }
13473 else if (CHARPOS (new_start) > CHARPOS (start))
13474 {
13475 struct glyph_row *pt_row, *row;
13476 struct glyph_row *first_reusable_row;
13477 struct glyph_row *first_row_to_display;
13478 int dy;
13479 int yb = window_text_bottom_y (w);
13480
13481 /* Find the row starting at new_start, if there is one. Don't
13482 reuse a partially visible line at the end. */
13483 first_reusable_row = start_row;
13484 while (first_reusable_row->enabled_p
13485 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
13486 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
13487 < CHARPOS (new_start)))
13488 ++first_reusable_row;
13489
13490 /* Give up if there is no row to reuse. */
13491 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
13492 || !first_reusable_row->enabled_p
13493 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
13494 != CHARPOS (new_start)))
13495 return 0;
13496
13497 /* We can reuse fully visible rows beginning with
13498 first_reusable_row to the end of the window. Set
13499 first_row_to_display to the first row that cannot be reused.
13500 Set pt_row to the row containing point, if there is any. */
13501 pt_row = NULL;
13502 for (first_row_to_display = first_reusable_row;
13503 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
13504 ++first_row_to_display)
13505 {
13506 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
13507 && PT < MATRIX_ROW_END_CHARPOS (first_row_to_display))
13508 pt_row = first_row_to_display;
13509 }
13510
13511 /* Start displaying at the start of first_row_to_display. */
13512 xassert (first_row_to_display->y < yb);
13513 init_to_row_start (&it, w, first_row_to_display);
13514
13515 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
13516 - start_vpos);
13517 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
13518 - nrows_scrolled);
13519 it.current_y = (first_row_to_display->y - first_reusable_row->y
13520 + WINDOW_HEADER_LINE_HEIGHT (w));
13521
13522 /* Display lines beginning with first_row_to_display in the
13523 desired matrix. Set last_text_row to the last row displayed
13524 that displays text. */
13525 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
13526 if (pt_row == NULL)
13527 w->cursor.vpos = -1;
13528 last_text_row = NULL;
13529 while (it.current_y < it.last_visible_y && !fonts_changed_p)
13530 if (display_line (&it))
13531 last_text_row = it.glyph_row - 1;
13532
13533 /* Give up If point isn't in a row displayed or reused. */
13534 if (w->cursor.vpos < 0)
13535 {
13536 clear_glyph_matrix (w->desired_matrix);
13537 return 0;
13538 }
13539
13540 /* If point is in a reused row, adjust y and vpos of the cursor
13541 position. */
13542 if (pt_row)
13543 {
13544 w->cursor.vpos -= nrows_scrolled;
13545 w->cursor.y -= first_reusable_row->y - start_row->y;
13546 }
13547
13548 /* Scroll the display. */
13549 run.current_y = first_reusable_row->y;
13550 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
13551 run.height = it.last_visible_y - run.current_y;
13552 dy = run.current_y - run.desired_y;
13553
13554 if (run.height)
13555 {
13556 update_begin (f);
13557 rif->update_window_begin_hook (w);
13558 rif->clear_window_mouse_face (w);
13559 rif->scroll_run_hook (w, &run);
13560 rif->update_window_end_hook (w, 0, 0);
13561 update_end (f);
13562 }
13563
13564 /* Adjust Y positions of reused rows. */
13565 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
13566 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
13567 max_y = it.last_visible_y;
13568 for (row = first_reusable_row; row < first_row_to_display; ++row)
13569 {
13570 row->y -= dy;
13571 row->visible_height = row->height;
13572 if (row->y < min_y)
13573 row->visible_height -= min_y - row->y;
13574 if (row->y + row->height > max_y)
13575 row->visible_height -= row->y + row->height - max_y;
13576 row->redraw_fringe_bitmaps_p = 1;
13577 }
13578
13579 /* Scroll the current matrix. */
13580 xassert (nrows_scrolled > 0);
13581 rotate_matrix (w->current_matrix,
13582 start_vpos,
13583 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
13584 -nrows_scrolled);
13585
13586 /* Disable rows not reused. */
13587 for (row -= nrows_scrolled; row < bottom_row; ++row)
13588 row->enabled_p = 0;
13589
13590 /* Point may have moved to a different line, so we cannot assume that
13591 the previous cursor position is valid; locate the correct row. */
13592 if (pt_row)
13593 {
13594 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
13595 row < bottom_row && PT >= MATRIX_ROW_END_CHARPOS (row);
13596 row++)
13597 {
13598 w->cursor.vpos++;
13599 w->cursor.y = row->y;
13600 }
13601 if (row < bottom_row)
13602 {
13603 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
13604 while (glyph->charpos < PT)
13605 {
13606 w->cursor.hpos++;
13607 w->cursor.x += glyph->pixel_width;
13608 glyph++;
13609 }
13610 }
13611 }
13612
13613 /* Adjust window end. A null value of last_text_row means that
13614 the window end is in reused rows which in turn means that
13615 only its vpos can have changed. */
13616 if (last_text_row)
13617 {
13618 w->window_end_bytepos
13619 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
13620 w->window_end_pos
13621 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
13622 w->window_end_vpos
13623 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
13624 }
13625 else
13626 {
13627 w->window_end_vpos
13628 = make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled);
13629 }
13630
13631 w->window_end_valid = Qnil;
13632 w->desired_matrix->no_scrolling_p = 1;
13633
13634 #if GLYPH_DEBUG
13635 debug_method_add (w, "try_window_reusing_current_matrix 2");
13636 #endif
13637 return 1;
13638 }
13639
13640 return 0;
13641 }
13642
13643
13644 \f
13645 /************************************************************************
13646 Window redisplay reusing current matrix when buffer has changed
13647 ************************************************************************/
13648
13649 static struct glyph_row *find_last_unchanged_at_beg_row P_ ((struct window *));
13650 static struct glyph_row *find_first_unchanged_at_end_row P_ ((struct window *,
13651 int *, int *));
13652 static struct glyph_row *
13653 find_last_row_displaying_text P_ ((struct glyph_matrix *, struct it *,
13654 struct glyph_row *));
13655
13656
13657 /* Return the last row in MATRIX displaying text. If row START is
13658 non-null, start searching with that row. IT gives the dimensions
13659 of the display. Value is null if matrix is empty; otherwise it is
13660 a pointer to the row found. */
13661
13662 static struct glyph_row *
13663 find_last_row_displaying_text (matrix, it, start)
13664 struct glyph_matrix *matrix;
13665 struct it *it;
13666 struct glyph_row *start;
13667 {
13668 struct glyph_row *row, *row_found;
13669
13670 /* Set row_found to the last row in IT->w's current matrix
13671 displaying text. The loop looks funny but think of partially
13672 visible lines. */
13673 row_found = NULL;
13674 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
13675 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
13676 {
13677 xassert (row->enabled_p);
13678 row_found = row;
13679 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
13680 break;
13681 ++row;
13682 }
13683
13684 return row_found;
13685 }
13686
13687
13688 /* Return the last row in the current matrix of W that is not affected
13689 by changes at the start of current_buffer that occurred since W's
13690 current matrix was built. Value is null if no such row exists.
13691
13692 BEG_UNCHANGED us the number of characters unchanged at the start of
13693 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
13694 first changed character in current_buffer. Characters at positions <
13695 BEG + BEG_UNCHANGED are at the same buffer positions as they were
13696 when the current matrix was built. */
13697
13698 static struct glyph_row *
13699 find_last_unchanged_at_beg_row (w)
13700 struct window *w;
13701 {
13702 int first_changed_pos = BEG + BEG_UNCHANGED;
13703 struct glyph_row *row;
13704 struct glyph_row *row_found = NULL;
13705 int yb = window_text_bottom_y (w);
13706
13707 /* Find the last row displaying unchanged text. */
13708 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
13709 while (MATRIX_ROW_DISPLAYS_TEXT_P (row)
13710 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos)
13711 {
13712 if (/* If row ends before first_changed_pos, it is unchanged,
13713 except in some case. */
13714 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
13715 /* When row ends in ZV and we write at ZV it is not
13716 unchanged. */
13717 && !row->ends_at_zv_p
13718 /* When first_changed_pos is the end of a continued line,
13719 row is not unchanged because it may be no longer
13720 continued. */
13721 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
13722 && (row->continued_p
13723 || row->exact_window_width_line_p)))
13724 row_found = row;
13725
13726 /* Stop if last visible row. */
13727 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
13728 break;
13729
13730 ++row;
13731 }
13732
13733 return row_found;
13734 }
13735
13736
13737 /* Find the first glyph row in the current matrix of W that is not
13738 affected by changes at the end of current_buffer since the
13739 time W's current matrix was built.
13740
13741 Return in *DELTA the number of chars by which buffer positions in
13742 unchanged text at the end of current_buffer must be adjusted.
13743
13744 Return in *DELTA_BYTES the corresponding number of bytes.
13745
13746 Value is null if no such row exists, i.e. all rows are affected by
13747 changes. */
13748
13749 static struct glyph_row *
13750 find_first_unchanged_at_end_row (w, delta, delta_bytes)
13751 struct window *w;
13752 int *delta, *delta_bytes;
13753 {
13754 struct glyph_row *row;
13755 struct glyph_row *row_found = NULL;
13756
13757 *delta = *delta_bytes = 0;
13758
13759 /* Display must not have been paused, otherwise the current matrix
13760 is not up to date. */
13761 if (NILP (w->window_end_valid))
13762 abort ();
13763
13764 /* A value of window_end_pos >= END_UNCHANGED means that the window
13765 end is in the range of changed text. If so, there is no
13766 unchanged row at the end of W's current matrix. */
13767 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
13768 return NULL;
13769
13770 /* Set row to the last row in W's current matrix displaying text. */
13771 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
13772
13773 /* If matrix is entirely empty, no unchanged row exists. */
13774 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
13775 {
13776 /* The value of row is the last glyph row in the matrix having a
13777 meaningful buffer position in it. The end position of row
13778 corresponds to window_end_pos. This allows us to translate
13779 buffer positions in the current matrix to current buffer
13780 positions for characters not in changed text. */
13781 int Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
13782 int Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
13783 int last_unchanged_pos, last_unchanged_pos_old;
13784 struct glyph_row *first_text_row
13785 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
13786
13787 *delta = Z - Z_old;
13788 *delta_bytes = Z_BYTE - Z_BYTE_old;
13789
13790 /* Set last_unchanged_pos to the buffer position of the last
13791 character in the buffer that has not been changed. Z is the
13792 index + 1 of the last character in current_buffer, i.e. by
13793 subtracting END_UNCHANGED we get the index of the last
13794 unchanged character, and we have to add BEG to get its buffer
13795 position. */
13796 last_unchanged_pos = Z - END_UNCHANGED + BEG;
13797 last_unchanged_pos_old = last_unchanged_pos - *delta;
13798
13799 /* Search backward from ROW for a row displaying a line that
13800 starts at a minimum position >= last_unchanged_pos_old. */
13801 for (; row > first_text_row; --row)
13802 {
13803 /* This used to abort, but it can happen.
13804 It is ok to just stop the search instead here. KFS. */
13805 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
13806 break;
13807
13808 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
13809 row_found = row;
13810 }
13811 }
13812
13813 if (row_found && !MATRIX_ROW_DISPLAYS_TEXT_P (row_found))
13814 abort ();
13815
13816 return row_found;
13817 }
13818
13819
13820 /* Make sure that glyph rows in the current matrix of window W
13821 reference the same glyph memory as corresponding rows in the
13822 frame's frame matrix. This function is called after scrolling W's
13823 current matrix on a terminal frame in try_window_id and
13824 try_window_reusing_current_matrix. */
13825
13826 static void
13827 sync_frame_with_window_matrix_rows (w)
13828 struct window *w;
13829 {
13830 struct frame *f = XFRAME (w->frame);
13831 struct glyph_row *window_row, *window_row_end, *frame_row;
13832
13833 /* Preconditions: W must be a leaf window and full-width. Its frame
13834 must have a frame matrix. */
13835 xassert (NILP (w->hchild) && NILP (w->vchild));
13836 xassert (WINDOW_FULL_WIDTH_P (w));
13837 xassert (!FRAME_WINDOW_P (f));
13838
13839 /* If W is a full-width window, glyph pointers in W's current matrix
13840 have, by definition, to be the same as glyph pointers in the
13841 corresponding frame matrix. Note that frame matrices have no
13842 marginal areas (see build_frame_matrix). */
13843 window_row = w->current_matrix->rows;
13844 window_row_end = window_row + w->current_matrix->nrows;
13845 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
13846 while (window_row < window_row_end)
13847 {
13848 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
13849 struct glyph *end = window_row->glyphs[LAST_AREA];
13850
13851 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
13852 frame_row->glyphs[TEXT_AREA] = start;
13853 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
13854 frame_row->glyphs[LAST_AREA] = end;
13855
13856 /* Disable frame rows whose corresponding window rows have
13857 been disabled in try_window_id. */
13858 if (!window_row->enabled_p)
13859 frame_row->enabled_p = 0;
13860
13861 ++window_row, ++frame_row;
13862 }
13863 }
13864
13865
13866 /* Find the glyph row in window W containing CHARPOS. Consider all
13867 rows between START and END (not inclusive). END null means search
13868 all rows to the end of the display area of W. Value is the row
13869 containing CHARPOS or null. */
13870
13871 struct glyph_row *
13872 row_containing_pos (w, charpos, start, end, dy)
13873 struct window *w;
13874 int charpos;
13875 struct glyph_row *start, *end;
13876 int dy;
13877 {
13878 struct glyph_row *row = start;
13879 int last_y;
13880
13881 /* If we happen to start on a header-line, skip that. */
13882 if (row->mode_line_p)
13883 ++row;
13884
13885 if ((end && row >= end) || !row->enabled_p)
13886 return NULL;
13887
13888 last_y = window_text_bottom_y (w) - dy;
13889
13890 while (1)
13891 {
13892 /* Give up if we have gone too far. */
13893 if (end && row >= end)
13894 return NULL;
13895 /* This formerly returned if they were equal.
13896 I think that both quantities are of a "last plus one" type;
13897 if so, when they are equal, the row is within the screen. -- rms. */
13898 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
13899 return NULL;
13900
13901 /* If it is in this row, return this row. */
13902 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
13903 || (MATRIX_ROW_END_CHARPOS (row) == charpos
13904 /* The end position of a row equals the start
13905 position of the next row. If CHARPOS is there, we
13906 would rather display it in the next line, except
13907 when this line ends in ZV. */
13908 && !row->ends_at_zv_p
13909 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
13910 && charpos >= MATRIX_ROW_START_CHARPOS (row))
13911 return row;
13912 ++row;
13913 }
13914 }
13915
13916
13917 /* Try to redisplay window W by reusing its existing display. W's
13918 current matrix must be up to date when this function is called,
13919 i.e. window_end_valid must not be nil.
13920
13921 Value is
13922
13923 1 if display has been updated
13924 0 if otherwise unsuccessful
13925 -1 if redisplay with same window start is known not to succeed
13926
13927 The following steps are performed:
13928
13929 1. Find the last row in the current matrix of W that is not
13930 affected by changes at the start of current_buffer. If no such row
13931 is found, give up.
13932
13933 2. Find the first row in W's current matrix that is not affected by
13934 changes at the end of current_buffer. Maybe there is no such row.
13935
13936 3. Display lines beginning with the row + 1 found in step 1 to the
13937 row found in step 2 or, if step 2 didn't find a row, to the end of
13938 the window.
13939
13940 4. If cursor is not known to appear on the window, give up.
13941
13942 5. If display stopped at the row found in step 2, scroll the
13943 display and current matrix as needed.
13944
13945 6. Maybe display some lines at the end of W, if we must. This can
13946 happen under various circumstances, like a partially visible line
13947 becoming fully visible, or because newly displayed lines are displayed
13948 in smaller font sizes.
13949
13950 7. Update W's window end information. */
13951
13952 static int
13953 try_window_id (w)
13954 struct window *w;
13955 {
13956 struct frame *f = XFRAME (w->frame);
13957 struct glyph_matrix *current_matrix = w->current_matrix;
13958 struct glyph_matrix *desired_matrix = w->desired_matrix;
13959 struct glyph_row *last_unchanged_at_beg_row;
13960 struct glyph_row *first_unchanged_at_end_row;
13961 struct glyph_row *row;
13962 struct glyph_row *bottom_row;
13963 int bottom_vpos;
13964 struct it it;
13965 int delta = 0, delta_bytes = 0, stop_pos, dvpos, dy;
13966 struct text_pos start_pos;
13967 struct run run;
13968 int first_unchanged_at_end_vpos = 0;
13969 struct glyph_row *last_text_row, *last_text_row_at_end;
13970 struct text_pos start;
13971 int first_changed_charpos, last_changed_charpos;
13972
13973 #if GLYPH_DEBUG
13974 if (inhibit_try_window_id)
13975 return 0;
13976 #endif
13977
13978 /* This is handy for debugging. */
13979 #if 0
13980 #define GIVE_UP(X) \
13981 do { \
13982 fprintf (stderr, "try_window_id give up %d\n", (X)); \
13983 return 0; \
13984 } while (0)
13985 #else
13986 #define GIVE_UP(X) return 0
13987 #endif
13988
13989 SET_TEXT_POS_FROM_MARKER (start, w->start);
13990
13991 /* Don't use this for mini-windows because these can show
13992 messages and mini-buffers, and we don't handle that here. */
13993 if (MINI_WINDOW_P (w))
13994 GIVE_UP (1);
13995
13996 /* This flag is used to prevent redisplay optimizations. */
13997 if (windows_or_buffers_changed || cursor_type_changed)
13998 GIVE_UP (2);
13999
14000 /* Verify that narrowing has not changed.
14001 Also verify that we were not told to prevent redisplay optimizations.
14002 It would be nice to further
14003 reduce the number of cases where this prevents try_window_id. */
14004 if (current_buffer->clip_changed
14005 || current_buffer->prevent_redisplay_optimizations_p)
14006 GIVE_UP (3);
14007
14008 /* Window must either use window-based redisplay or be full width. */
14009 if (!FRAME_WINDOW_P (f)
14010 && (!line_ins_del_ok
14011 || !WINDOW_FULL_WIDTH_P (w)))
14012 GIVE_UP (4);
14013
14014 /* Give up if point is not known NOT to appear in W. */
14015 if (PT < CHARPOS (start))
14016 GIVE_UP (5);
14017
14018 /* Another way to prevent redisplay optimizations. */
14019 if (XFASTINT (w->last_modified) == 0)
14020 GIVE_UP (6);
14021
14022 /* Verify that window is not hscrolled. */
14023 if (XFASTINT (w->hscroll) != 0)
14024 GIVE_UP (7);
14025
14026 /* Verify that display wasn't paused. */
14027 if (NILP (w->window_end_valid))
14028 GIVE_UP (8);
14029
14030 /* Can't use this if highlighting a region because a cursor movement
14031 will do more than just set the cursor. */
14032 if (!NILP (Vtransient_mark_mode)
14033 && !NILP (current_buffer->mark_active))
14034 GIVE_UP (9);
14035
14036 /* Likewise if highlighting trailing whitespace. */
14037 if (!NILP (Vshow_trailing_whitespace))
14038 GIVE_UP (11);
14039
14040 /* Likewise if showing a region. */
14041 if (!NILP (w->region_showing))
14042 GIVE_UP (10);
14043
14044 /* Can use this if overlay arrow position and or string have changed. */
14045 if (overlay_arrows_changed_p ())
14046 GIVE_UP (12);
14047
14048
14049 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
14050 only if buffer has really changed. The reason is that the gap is
14051 initially at Z for freshly visited files. The code below would
14052 set end_unchanged to 0 in that case. */
14053 if (MODIFF > SAVE_MODIFF
14054 /* This seems to happen sometimes after saving a buffer. */
14055 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
14056 {
14057 if (GPT - BEG < BEG_UNCHANGED)
14058 BEG_UNCHANGED = GPT - BEG;
14059 if (Z - GPT < END_UNCHANGED)
14060 END_UNCHANGED = Z - GPT;
14061 }
14062
14063 /* The position of the first and last character that has been changed. */
14064 first_changed_charpos = BEG + BEG_UNCHANGED;
14065 last_changed_charpos = Z - END_UNCHANGED;
14066
14067 /* If window starts after a line end, and the last change is in
14068 front of that newline, then changes don't affect the display.
14069 This case happens with stealth-fontification. Note that although
14070 the display is unchanged, glyph positions in the matrix have to
14071 be adjusted, of course. */
14072 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
14073 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
14074 && ((last_changed_charpos < CHARPOS (start)
14075 && CHARPOS (start) == BEGV)
14076 || (last_changed_charpos < CHARPOS (start) - 1
14077 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
14078 {
14079 int Z_old, delta, Z_BYTE_old, delta_bytes;
14080 struct glyph_row *r0;
14081
14082 /* Compute how many chars/bytes have been added to or removed
14083 from the buffer. */
14084 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
14085 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
14086 delta = Z - Z_old;
14087 delta_bytes = Z_BYTE - Z_BYTE_old;
14088
14089 /* Give up if PT is not in the window. Note that it already has
14090 been checked at the start of try_window_id that PT is not in
14091 front of the window start. */
14092 if (PT >= MATRIX_ROW_END_CHARPOS (row) + delta)
14093 GIVE_UP (13);
14094
14095 /* If window start is unchanged, we can reuse the whole matrix
14096 as is, after adjusting glyph positions. No need to compute
14097 the window end again, since its offset from Z hasn't changed. */
14098 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
14099 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + delta
14100 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + delta_bytes
14101 /* PT must not be in a partially visible line. */
14102 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + delta
14103 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
14104 {
14105 /* Adjust positions in the glyph matrix. */
14106 if (delta || delta_bytes)
14107 {
14108 struct glyph_row *r1
14109 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
14110 increment_matrix_positions (w->current_matrix,
14111 MATRIX_ROW_VPOS (r0, current_matrix),
14112 MATRIX_ROW_VPOS (r1, current_matrix),
14113 delta, delta_bytes);
14114 }
14115
14116 /* Set the cursor. */
14117 row = row_containing_pos (w, PT, r0, NULL, 0);
14118 if (row)
14119 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
14120 else
14121 abort ();
14122 return 1;
14123 }
14124 }
14125
14126 /* Handle the case that changes are all below what is displayed in
14127 the window, and that PT is in the window. This shortcut cannot
14128 be taken if ZV is visible in the window, and text has been added
14129 there that is visible in the window. */
14130 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
14131 /* ZV is not visible in the window, or there are no
14132 changes at ZV, actually. */
14133 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
14134 || first_changed_charpos == last_changed_charpos))
14135 {
14136 struct glyph_row *r0;
14137
14138 /* Give up if PT is not in the window. Note that it already has
14139 been checked at the start of try_window_id that PT is not in
14140 front of the window start. */
14141 if (PT >= MATRIX_ROW_END_CHARPOS (row))
14142 GIVE_UP (14);
14143
14144 /* If window start is unchanged, we can reuse the whole matrix
14145 as is, without changing glyph positions since no text has
14146 been added/removed in front of the window end. */
14147 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
14148 if (TEXT_POS_EQUAL_P (start, r0->start.pos)
14149 /* PT must not be in a partially visible line. */
14150 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
14151 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
14152 {
14153 /* We have to compute the window end anew since text
14154 can have been added/removed after it. */
14155 w->window_end_pos
14156 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
14157 w->window_end_bytepos
14158 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
14159
14160 /* Set the cursor. */
14161 row = row_containing_pos (w, PT, r0, NULL, 0);
14162 if (row)
14163 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
14164 else
14165 abort ();
14166 return 2;
14167 }
14168 }
14169
14170 /* Give up if window start is in the changed area.
14171
14172 The condition used to read
14173
14174 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
14175
14176 but why that was tested escapes me at the moment. */
14177 if (CHARPOS (start) >= first_changed_charpos
14178 && CHARPOS (start) <= last_changed_charpos)
14179 GIVE_UP (15);
14180
14181 /* Check that window start agrees with the start of the first glyph
14182 row in its current matrix. Check this after we know the window
14183 start is not in changed text, otherwise positions would not be
14184 comparable. */
14185 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
14186 if (!TEXT_POS_EQUAL_P (start, row->start.pos))
14187 GIVE_UP (16);
14188
14189 /* Give up if the window ends in strings. Overlay strings
14190 at the end are difficult to handle, so don't try. */
14191 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
14192 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
14193 GIVE_UP (20);
14194
14195 /* Compute the position at which we have to start displaying new
14196 lines. Some of the lines at the top of the window might be
14197 reusable because they are not displaying changed text. Find the
14198 last row in W's current matrix not affected by changes at the
14199 start of current_buffer. Value is null if changes start in the
14200 first line of window. */
14201 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
14202 if (last_unchanged_at_beg_row)
14203 {
14204 /* Avoid starting to display in the moddle of a character, a TAB
14205 for instance. This is easier than to set up the iterator
14206 exactly, and it's not a frequent case, so the additional
14207 effort wouldn't really pay off. */
14208 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
14209 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
14210 && last_unchanged_at_beg_row > w->current_matrix->rows)
14211 --last_unchanged_at_beg_row;
14212
14213 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
14214 GIVE_UP (17);
14215
14216 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
14217 GIVE_UP (18);
14218 start_pos = it.current.pos;
14219
14220 /* Start displaying new lines in the desired matrix at the same
14221 vpos we would use in the current matrix, i.e. below
14222 last_unchanged_at_beg_row. */
14223 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
14224 current_matrix);
14225 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
14226 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
14227
14228 xassert (it.hpos == 0 && it.current_x == 0);
14229 }
14230 else
14231 {
14232 /* There are no reusable lines at the start of the window.
14233 Start displaying in the first text line. */
14234 start_display (&it, w, start);
14235 it.vpos = it.first_vpos;
14236 start_pos = it.current.pos;
14237 }
14238
14239 /* Find the first row that is not affected by changes at the end of
14240 the buffer. Value will be null if there is no unchanged row, in
14241 which case we must redisplay to the end of the window. delta
14242 will be set to the value by which buffer positions beginning with
14243 first_unchanged_at_end_row have to be adjusted due to text
14244 changes. */
14245 first_unchanged_at_end_row
14246 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
14247 IF_DEBUG (debug_delta = delta);
14248 IF_DEBUG (debug_delta_bytes = delta_bytes);
14249
14250 /* Set stop_pos to the buffer position up to which we will have to
14251 display new lines. If first_unchanged_at_end_row != NULL, this
14252 is the buffer position of the start of the line displayed in that
14253 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
14254 that we don't stop at a buffer position. */
14255 stop_pos = 0;
14256 if (first_unchanged_at_end_row)
14257 {
14258 xassert (last_unchanged_at_beg_row == NULL
14259 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
14260
14261 /* If this is a continuation line, move forward to the next one
14262 that isn't. Changes in lines above affect this line.
14263 Caution: this may move first_unchanged_at_end_row to a row
14264 not displaying text. */
14265 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
14266 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
14267 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
14268 < it.last_visible_y))
14269 ++first_unchanged_at_end_row;
14270
14271 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
14272 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
14273 >= it.last_visible_y))
14274 first_unchanged_at_end_row = NULL;
14275 else
14276 {
14277 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
14278 + delta);
14279 first_unchanged_at_end_vpos
14280 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
14281 xassert (stop_pos >= Z - END_UNCHANGED);
14282 }
14283 }
14284 else if (last_unchanged_at_beg_row == NULL)
14285 GIVE_UP (19);
14286
14287
14288 #if GLYPH_DEBUG
14289
14290 /* Either there is no unchanged row at the end, or the one we have
14291 now displays text. This is a necessary condition for the window
14292 end pos calculation at the end of this function. */
14293 xassert (first_unchanged_at_end_row == NULL
14294 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
14295
14296 debug_last_unchanged_at_beg_vpos
14297 = (last_unchanged_at_beg_row
14298 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
14299 : -1);
14300 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
14301
14302 #endif /* GLYPH_DEBUG != 0 */
14303
14304
14305 /* Display new lines. Set last_text_row to the last new line
14306 displayed which has text on it, i.e. might end up as being the
14307 line where the window_end_vpos is. */
14308 w->cursor.vpos = -1;
14309 last_text_row = NULL;
14310 overlay_arrow_seen = 0;
14311 while (it.current_y < it.last_visible_y
14312 && !fonts_changed_p
14313 && (first_unchanged_at_end_row == NULL
14314 || IT_CHARPOS (it) < stop_pos))
14315 {
14316 if (display_line (&it))
14317 last_text_row = it.glyph_row - 1;
14318 }
14319
14320 if (fonts_changed_p)
14321 return -1;
14322
14323
14324 /* Compute differences in buffer positions, y-positions etc. for
14325 lines reused at the bottom of the window. Compute what we can
14326 scroll. */
14327 if (first_unchanged_at_end_row
14328 /* No lines reused because we displayed everything up to the
14329 bottom of the window. */
14330 && it.current_y < it.last_visible_y)
14331 {
14332 dvpos = (it.vpos
14333 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
14334 current_matrix));
14335 dy = it.current_y - first_unchanged_at_end_row->y;
14336 run.current_y = first_unchanged_at_end_row->y;
14337 run.desired_y = run.current_y + dy;
14338 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
14339 }
14340 else
14341 {
14342 delta = dvpos = dy = run.current_y = run.desired_y = run.height = 0;
14343 first_unchanged_at_end_row = NULL;
14344 }
14345 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
14346
14347
14348 /* Find the cursor if not already found. We have to decide whether
14349 PT will appear on this window (it sometimes doesn't, but this is
14350 not a very frequent case.) This decision has to be made before
14351 the current matrix is altered. A value of cursor.vpos < 0 means
14352 that PT is either in one of the lines beginning at
14353 first_unchanged_at_end_row or below the window. Don't care for
14354 lines that might be displayed later at the window end; as
14355 mentioned, this is not a frequent case. */
14356 if (w->cursor.vpos < 0)
14357 {
14358 /* Cursor in unchanged rows at the top? */
14359 if (PT < CHARPOS (start_pos)
14360 && last_unchanged_at_beg_row)
14361 {
14362 row = row_containing_pos (w, PT,
14363 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
14364 last_unchanged_at_beg_row + 1, 0);
14365 if (row)
14366 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
14367 }
14368
14369 /* Start from first_unchanged_at_end_row looking for PT. */
14370 else if (first_unchanged_at_end_row)
14371 {
14372 row = row_containing_pos (w, PT - delta,
14373 first_unchanged_at_end_row, NULL, 0);
14374 if (row)
14375 set_cursor_from_row (w, row, w->current_matrix, delta,
14376 delta_bytes, dy, dvpos);
14377 }
14378
14379 /* Give up if cursor was not found. */
14380 if (w->cursor.vpos < 0)
14381 {
14382 clear_glyph_matrix (w->desired_matrix);
14383 return -1;
14384 }
14385 }
14386
14387 /* Don't let the cursor end in the scroll margins. */
14388 {
14389 int this_scroll_margin, cursor_height;
14390
14391 this_scroll_margin = max (0, scroll_margin);
14392 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
14393 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
14394 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
14395
14396 if ((w->cursor.y < this_scroll_margin
14397 && CHARPOS (start) > BEGV)
14398 /* Old redisplay didn't take scroll margin into account at the bottom,
14399 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
14400 || (w->cursor.y + (make_cursor_line_fully_visible_p
14401 ? cursor_height + this_scroll_margin
14402 : 1)) > it.last_visible_y)
14403 {
14404 w->cursor.vpos = -1;
14405 clear_glyph_matrix (w->desired_matrix);
14406 return -1;
14407 }
14408 }
14409
14410 /* Scroll the display. Do it before changing the current matrix so
14411 that xterm.c doesn't get confused about where the cursor glyph is
14412 found. */
14413 if (dy && run.height)
14414 {
14415 update_begin (f);
14416
14417 if (FRAME_WINDOW_P (f))
14418 {
14419 rif->update_window_begin_hook (w);
14420 rif->clear_window_mouse_face (w);
14421 rif->scroll_run_hook (w, &run);
14422 rif->update_window_end_hook (w, 0, 0);
14423 }
14424 else
14425 {
14426 /* Terminal frame. In this case, dvpos gives the number of
14427 lines to scroll by; dvpos < 0 means scroll up. */
14428 int first_unchanged_at_end_vpos
14429 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
14430 int from = WINDOW_TOP_EDGE_LINE (w) + first_unchanged_at_end_vpos;
14431 int end = (WINDOW_TOP_EDGE_LINE (w)
14432 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
14433 + window_internal_height (w));
14434
14435 /* Perform the operation on the screen. */
14436 if (dvpos > 0)
14437 {
14438 /* Scroll last_unchanged_at_beg_row to the end of the
14439 window down dvpos lines. */
14440 set_terminal_window (end);
14441
14442 /* On dumb terminals delete dvpos lines at the end
14443 before inserting dvpos empty lines. */
14444 if (!scroll_region_ok)
14445 ins_del_lines (end - dvpos, -dvpos);
14446
14447 /* Insert dvpos empty lines in front of
14448 last_unchanged_at_beg_row. */
14449 ins_del_lines (from, dvpos);
14450 }
14451 else if (dvpos < 0)
14452 {
14453 /* Scroll up last_unchanged_at_beg_vpos to the end of
14454 the window to last_unchanged_at_beg_vpos - |dvpos|. */
14455 set_terminal_window (end);
14456
14457 /* Delete dvpos lines in front of
14458 last_unchanged_at_beg_vpos. ins_del_lines will set
14459 the cursor to the given vpos and emit |dvpos| delete
14460 line sequences. */
14461 ins_del_lines (from + dvpos, dvpos);
14462
14463 /* On a dumb terminal insert dvpos empty lines at the
14464 end. */
14465 if (!scroll_region_ok)
14466 ins_del_lines (end + dvpos, -dvpos);
14467 }
14468
14469 set_terminal_window (0);
14470 }
14471
14472 update_end (f);
14473 }
14474
14475 /* Shift reused rows of the current matrix to the right position.
14476 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
14477 text. */
14478 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
14479 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
14480 if (dvpos < 0)
14481 {
14482 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
14483 bottom_vpos, dvpos);
14484 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
14485 bottom_vpos, 0);
14486 }
14487 else if (dvpos > 0)
14488 {
14489 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
14490 bottom_vpos, dvpos);
14491 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
14492 first_unchanged_at_end_vpos + dvpos, 0);
14493 }
14494
14495 /* For frame-based redisplay, make sure that current frame and window
14496 matrix are in sync with respect to glyph memory. */
14497 if (!FRAME_WINDOW_P (f))
14498 sync_frame_with_window_matrix_rows (w);
14499
14500 /* Adjust buffer positions in reused rows. */
14501 if (delta)
14502 increment_matrix_positions (current_matrix,
14503 first_unchanged_at_end_vpos + dvpos,
14504 bottom_vpos, delta, delta_bytes);
14505
14506 /* Adjust Y positions. */
14507 if (dy)
14508 shift_glyph_matrix (w, current_matrix,
14509 first_unchanged_at_end_vpos + dvpos,
14510 bottom_vpos, dy);
14511
14512 if (first_unchanged_at_end_row)
14513 {
14514 first_unchanged_at_end_row += dvpos;
14515 if (first_unchanged_at_end_row->y >= it.last_visible_y
14516 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
14517 first_unchanged_at_end_row = NULL;
14518 }
14519
14520 /* If scrolling up, there may be some lines to display at the end of
14521 the window. */
14522 last_text_row_at_end = NULL;
14523 if (dy < 0)
14524 {
14525 /* Scrolling up can leave for example a partially visible line
14526 at the end of the window to be redisplayed. */
14527 /* Set last_row to the glyph row in the current matrix where the
14528 window end line is found. It has been moved up or down in
14529 the matrix by dvpos. */
14530 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
14531 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
14532
14533 /* If last_row is the window end line, it should display text. */
14534 xassert (last_row->displays_text_p);
14535
14536 /* If window end line was partially visible before, begin
14537 displaying at that line. Otherwise begin displaying with the
14538 line following it. */
14539 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
14540 {
14541 init_to_row_start (&it, w, last_row);
14542 it.vpos = last_vpos;
14543 it.current_y = last_row->y;
14544 }
14545 else
14546 {
14547 init_to_row_end (&it, w, last_row);
14548 it.vpos = 1 + last_vpos;
14549 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
14550 ++last_row;
14551 }
14552
14553 /* We may start in a continuation line. If so, we have to
14554 get the right continuation_lines_width and current_x. */
14555 it.continuation_lines_width = last_row->continuation_lines_width;
14556 it.hpos = it.current_x = 0;
14557
14558 /* Display the rest of the lines at the window end. */
14559 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
14560 while (it.current_y < it.last_visible_y
14561 && !fonts_changed_p)
14562 {
14563 /* Is it always sure that the display agrees with lines in
14564 the current matrix? I don't think so, so we mark rows
14565 displayed invalid in the current matrix by setting their
14566 enabled_p flag to zero. */
14567 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
14568 if (display_line (&it))
14569 last_text_row_at_end = it.glyph_row - 1;
14570 }
14571 }
14572
14573 /* Update window_end_pos and window_end_vpos. */
14574 if (first_unchanged_at_end_row
14575 && !last_text_row_at_end)
14576 {
14577 /* Window end line if one of the preserved rows from the current
14578 matrix. Set row to the last row displaying text in current
14579 matrix starting at first_unchanged_at_end_row, after
14580 scrolling. */
14581 xassert (first_unchanged_at_end_row->displays_text_p);
14582 row = find_last_row_displaying_text (w->current_matrix, &it,
14583 first_unchanged_at_end_row);
14584 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
14585
14586 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
14587 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
14588 w->window_end_vpos
14589 = make_number (MATRIX_ROW_VPOS (row, w->current_matrix));
14590 xassert (w->window_end_bytepos >= 0);
14591 IF_DEBUG (debug_method_add (w, "A"));
14592 }
14593 else if (last_text_row_at_end)
14594 {
14595 w->window_end_pos
14596 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
14597 w->window_end_bytepos
14598 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
14599 w->window_end_vpos
14600 = make_number (MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
14601 xassert (w->window_end_bytepos >= 0);
14602 IF_DEBUG (debug_method_add (w, "B"));
14603 }
14604 else if (last_text_row)
14605 {
14606 /* We have displayed either to the end of the window or at the
14607 end of the window, i.e. the last row with text is to be found
14608 in the desired matrix. */
14609 w->window_end_pos
14610 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
14611 w->window_end_bytepos
14612 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
14613 w->window_end_vpos
14614 = make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix));
14615 xassert (w->window_end_bytepos >= 0);
14616 }
14617 else if (first_unchanged_at_end_row == NULL
14618 && last_text_row == NULL
14619 && last_text_row_at_end == NULL)
14620 {
14621 /* Displayed to end of window, but no line containing text was
14622 displayed. Lines were deleted at the end of the window. */
14623 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
14624 int vpos = XFASTINT (w->window_end_vpos);
14625 struct glyph_row *current_row = current_matrix->rows + vpos;
14626 struct glyph_row *desired_row = desired_matrix->rows + vpos;
14627
14628 for (row = NULL;
14629 row == NULL && vpos >= first_vpos;
14630 --vpos, --current_row, --desired_row)
14631 {
14632 if (desired_row->enabled_p)
14633 {
14634 if (desired_row->displays_text_p)
14635 row = desired_row;
14636 }
14637 else if (current_row->displays_text_p)
14638 row = current_row;
14639 }
14640
14641 xassert (row != NULL);
14642 w->window_end_vpos = make_number (vpos + 1);
14643 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
14644 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
14645 xassert (w->window_end_bytepos >= 0);
14646 IF_DEBUG (debug_method_add (w, "C"));
14647 }
14648 else
14649 abort ();
14650
14651 #if 0 /* This leads to problems, for instance when the cursor is
14652 at ZV, and the cursor line displays no text. */
14653 /* Disable rows below what's displayed in the window. This makes
14654 debugging easier. */
14655 enable_glyph_matrix_rows (current_matrix,
14656 XFASTINT (w->window_end_vpos) + 1,
14657 bottom_vpos, 0);
14658 #endif
14659
14660 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
14661 debug_end_vpos = XFASTINT (w->window_end_vpos));
14662
14663 /* Record that display has not been completed. */
14664 w->window_end_valid = Qnil;
14665 w->desired_matrix->no_scrolling_p = 1;
14666 return 3;
14667
14668 #undef GIVE_UP
14669 }
14670
14671
14672 \f
14673 /***********************************************************************
14674 More debugging support
14675 ***********************************************************************/
14676
14677 #if GLYPH_DEBUG
14678
14679 void dump_glyph_row P_ ((struct glyph_row *, int, int));
14680 void dump_glyph_matrix P_ ((struct glyph_matrix *, int));
14681 void dump_glyph P_ ((struct glyph_row *, struct glyph *, int));
14682
14683
14684 /* Dump the contents of glyph matrix MATRIX on stderr.
14685
14686 GLYPHS 0 means don't show glyph contents.
14687 GLYPHS 1 means show glyphs in short form
14688 GLYPHS > 1 means show glyphs in long form. */
14689
14690 void
14691 dump_glyph_matrix (matrix, glyphs)
14692 struct glyph_matrix *matrix;
14693 int glyphs;
14694 {
14695 int i;
14696 for (i = 0; i < matrix->nrows; ++i)
14697 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
14698 }
14699
14700
14701 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
14702 the glyph row and area where the glyph comes from. */
14703
14704 void
14705 dump_glyph (row, glyph, area)
14706 struct glyph_row *row;
14707 struct glyph *glyph;
14708 int area;
14709 {
14710 if (glyph->type == CHAR_GLYPH)
14711 {
14712 fprintf (stderr,
14713 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
14714 glyph - row->glyphs[TEXT_AREA],
14715 'C',
14716 glyph->charpos,
14717 (BUFFERP (glyph->object)
14718 ? 'B'
14719 : (STRINGP (glyph->object)
14720 ? 'S'
14721 : '-')),
14722 glyph->pixel_width,
14723 glyph->u.ch,
14724 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
14725 ? glyph->u.ch
14726 : '.'),
14727 glyph->face_id,
14728 glyph->left_box_line_p,
14729 glyph->right_box_line_p);
14730 }
14731 else if (glyph->type == STRETCH_GLYPH)
14732 {
14733 fprintf (stderr,
14734 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
14735 glyph - row->glyphs[TEXT_AREA],
14736 'S',
14737 glyph->charpos,
14738 (BUFFERP (glyph->object)
14739 ? 'B'
14740 : (STRINGP (glyph->object)
14741 ? 'S'
14742 : '-')),
14743 glyph->pixel_width,
14744 0,
14745 '.',
14746 glyph->face_id,
14747 glyph->left_box_line_p,
14748 glyph->right_box_line_p);
14749 }
14750 else if (glyph->type == IMAGE_GLYPH)
14751 {
14752 fprintf (stderr,
14753 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
14754 glyph - row->glyphs[TEXT_AREA],
14755 'I',
14756 glyph->charpos,
14757 (BUFFERP (glyph->object)
14758 ? 'B'
14759 : (STRINGP (glyph->object)
14760 ? 'S'
14761 : '-')),
14762 glyph->pixel_width,
14763 glyph->u.img_id,
14764 '.',
14765 glyph->face_id,
14766 glyph->left_box_line_p,
14767 glyph->right_box_line_p);
14768 }
14769 }
14770
14771
14772 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
14773 GLYPHS 0 means don't show glyph contents.
14774 GLYPHS 1 means show glyphs in short form
14775 GLYPHS > 1 means show glyphs in long form. */
14776
14777 void
14778 dump_glyph_row (row, vpos, glyphs)
14779 struct glyph_row *row;
14780 int vpos, glyphs;
14781 {
14782 if (glyphs != 1)
14783 {
14784 fprintf (stderr, "Row Start End Used oEI><\\CTZFesm X Y W H V A P\n");
14785 fprintf (stderr, "======================================================================\n");
14786
14787 fprintf (stderr, "%3d %5d %5d %4d %1.1d%1.1d%1.1d%1.1d\
14788 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
14789 vpos,
14790 MATRIX_ROW_START_CHARPOS (row),
14791 MATRIX_ROW_END_CHARPOS (row),
14792 row->used[TEXT_AREA],
14793 row->contains_overlapping_glyphs_p,
14794 row->enabled_p,
14795 row->truncated_on_left_p,
14796 row->truncated_on_right_p,
14797 row->continued_p,
14798 MATRIX_ROW_CONTINUATION_LINE_P (row),
14799 row->displays_text_p,
14800 row->ends_at_zv_p,
14801 row->fill_line_p,
14802 row->ends_in_middle_of_char_p,
14803 row->starts_in_middle_of_char_p,
14804 row->mouse_face_p,
14805 row->x,
14806 row->y,
14807 row->pixel_width,
14808 row->height,
14809 row->visible_height,
14810 row->ascent,
14811 row->phys_ascent);
14812 fprintf (stderr, "%9d %5d\t%5d\n", row->start.overlay_string_index,
14813 row->end.overlay_string_index,
14814 row->continuation_lines_width);
14815 fprintf (stderr, "%9d %5d\n",
14816 CHARPOS (row->start.string_pos),
14817 CHARPOS (row->end.string_pos));
14818 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
14819 row->end.dpvec_index);
14820 }
14821
14822 if (glyphs > 1)
14823 {
14824 int area;
14825
14826 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
14827 {
14828 struct glyph *glyph = row->glyphs[area];
14829 struct glyph *glyph_end = glyph + row->used[area];
14830
14831 /* Glyph for a line end in text. */
14832 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
14833 ++glyph_end;
14834
14835 if (glyph < glyph_end)
14836 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
14837
14838 for (; glyph < glyph_end; ++glyph)
14839 dump_glyph (row, glyph, area);
14840 }
14841 }
14842 else if (glyphs == 1)
14843 {
14844 int area;
14845
14846 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
14847 {
14848 char *s = (char *) alloca (row->used[area] + 1);
14849 int i;
14850
14851 for (i = 0; i < row->used[area]; ++i)
14852 {
14853 struct glyph *glyph = row->glyphs[area] + i;
14854 if (glyph->type == CHAR_GLYPH
14855 && glyph->u.ch < 0x80
14856 && glyph->u.ch >= ' ')
14857 s[i] = glyph->u.ch;
14858 else
14859 s[i] = '.';
14860 }
14861
14862 s[i] = '\0';
14863 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
14864 }
14865 }
14866 }
14867
14868
14869 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
14870 Sdump_glyph_matrix, 0, 1, "p",
14871 doc: /* Dump the current matrix of the selected window to stderr.
14872 Shows contents of glyph row structures. With non-nil
14873 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
14874 glyphs in short form, otherwise show glyphs in long form. */)
14875 (glyphs)
14876 Lisp_Object glyphs;
14877 {
14878 struct window *w = XWINDOW (selected_window);
14879 struct buffer *buffer = XBUFFER (w->buffer);
14880
14881 fprintf (stderr, "PT = %d, BEGV = %d. ZV = %d\n",
14882 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
14883 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
14884 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
14885 fprintf (stderr, "=============================================\n");
14886 dump_glyph_matrix (w->current_matrix,
14887 NILP (glyphs) ? 0 : XINT (glyphs));
14888 return Qnil;
14889 }
14890
14891
14892 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
14893 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
14894 ()
14895 {
14896 struct frame *f = XFRAME (selected_frame);
14897 dump_glyph_matrix (f->current_matrix, 1);
14898 return Qnil;
14899 }
14900
14901
14902 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
14903 doc: /* Dump glyph row ROW to stderr.
14904 GLYPH 0 means don't dump glyphs.
14905 GLYPH 1 means dump glyphs in short form.
14906 GLYPH > 1 or omitted means dump glyphs in long form. */)
14907 (row, glyphs)
14908 Lisp_Object row, glyphs;
14909 {
14910 struct glyph_matrix *matrix;
14911 int vpos;
14912
14913 CHECK_NUMBER (row);
14914 matrix = XWINDOW (selected_window)->current_matrix;
14915 vpos = XINT (row);
14916 if (vpos >= 0 && vpos < matrix->nrows)
14917 dump_glyph_row (MATRIX_ROW (matrix, vpos),
14918 vpos,
14919 INTEGERP (glyphs) ? XINT (glyphs) : 2);
14920 return Qnil;
14921 }
14922
14923
14924 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
14925 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
14926 GLYPH 0 means don't dump glyphs.
14927 GLYPH 1 means dump glyphs in short form.
14928 GLYPH > 1 or omitted means dump glyphs in long form. */)
14929 (row, glyphs)
14930 Lisp_Object row, glyphs;
14931 {
14932 struct frame *sf = SELECTED_FRAME ();
14933 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
14934 int vpos;
14935
14936 CHECK_NUMBER (row);
14937 vpos = XINT (row);
14938 if (vpos >= 0 && vpos < m->nrows)
14939 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
14940 INTEGERP (glyphs) ? XINT (glyphs) : 2);
14941 return Qnil;
14942 }
14943
14944
14945 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
14946 doc: /* Toggle tracing of redisplay.
14947 With ARG, turn tracing on if and only if ARG is positive. */)
14948 (arg)
14949 Lisp_Object arg;
14950 {
14951 if (NILP (arg))
14952 trace_redisplay_p = !trace_redisplay_p;
14953 else
14954 {
14955 arg = Fprefix_numeric_value (arg);
14956 trace_redisplay_p = XINT (arg) > 0;
14957 }
14958
14959 return Qnil;
14960 }
14961
14962
14963 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
14964 doc: /* Like `format', but print result to stderr.
14965 usage: (trace-to-stderr STRING &rest OBJECTS) */)
14966 (nargs, args)
14967 int nargs;
14968 Lisp_Object *args;
14969 {
14970 Lisp_Object s = Fformat (nargs, args);
14971 fprintf (stderr, "%s", SDATA (s));
14972 return Qnil;
14973 }
14974
14975 #endif /* GLYPH_DEBUG */
14976
14977
14978 \f
14979 /***********************************************************************
14980 Building Desired Matrix Rows
14981 ***********************************************************************/
14982
14983 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
14984 Used for non-window-redisplay windows, and for windows w/o left fringe. */
14985
14986 static struct glyph_row *
14987 get_overlay_arrow_glyph_row (w, overlay_arrow_string)
14988 struct window *w;
14989 Lisp_Object overlay_arrow_string;
14990 {
14991 struct frame *f = XFRAME (WINDOW_FRAME (w));
14992 struct buffer *buffer = XBUFFER (w->buffer);
14993 struct buffer *old = current_buffer;
14994 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
14995 int arrow_len = SCHARS (overlay_arrow_string);
14996 const unsigned char *arrow_end = arrow_string + arrow_len;
14997 const unsigned char *p;
14998 struct it it;
14999 int multibyte_p;
15000 int n_glyphs_before;
15001
15002 set_buffer_temp (buffer);
15003 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
15004 it.glyph_row->used[TEXT_AREA] = 0;
15005 SET_TEXT_POS (it.position, 0, 0);
15006
15007 multibyte_p = !NILP (buffer->enable_multibyte_characters);
15008 p = arrow_string;
15009 while (p < arrow_end)
15010 {
15011 Lisp_Object face, ilisp;
15012
15013 /* Get the next character. */
15014 if (multibyte_p)
15015 it.c = string_char_and_length (p, arrow_len, &it.len);
15016 else
15017 it.c = *p, it.len = 1;
15018 p += it.len;
15019
15020 /* Get its face. */
15021 ilisp = make_number (p - arrow_string);
15022 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
15023 it.face_id = compute_char_face (f, it.c, face);
15024
15025 /* Compute its width, get its glyphs. */
15026 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
15027 SET_TEXT_POS (it.position, -1, -1);
15028 PRODUCE_GLYPHS (&it);
15029
15030 /* If this character doesn't fit any more in the line, we have
15031 to remove some glyphs. */
15032 if (it.current_x > it.last_visible_x)
15033 {
15034 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
15035 break;
15036 }
15037 }
15038
15039 set_buffer_temp (old);
15040 return it.glyph_row;
15041 }
15042
15043
15044 /* Insert truncation glyphs at the start of IT->glyph_row. Truncation
15045 glyphs are only inserted for terminal frames since we can't really
15046 win with truncation glyphs when partially visible glyphs are
15047 involved. Which glyphs to insert is determined by
15048 produce_special_glyphs. */
15049
15050 static void
15051 insert_left_trunc_glyphs (it)
15052 struct it *it;
15053 {
15054 struct it truncate_it;
15055 struct glyph *from, *end, *to, *toend;
15056
15057 xassert (!FRAME_WINDOW_P (it->f));
15058
15059 /* Get the truncation glyphs. */
15060 truncate_it = *it;
15061 truncate_it.current_x = 0;
15062 truncate_it.face_id = DEFAULT_FACE_ID;
15063 truncate_it.glyph_row = &scratch_glyph_row;
15064 truncate_it.glyph_row->used[TEXT_AREA] = 0;
15065 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
15066 truncate_it.object = make_number (0);
15067 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
15068
15069 /* Overwrite glyphs from IT with truncation glyphs. */
15070 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
15071 end = from + truncate_it.glyph_row->used[TEXT_AREA];
15072 to = it->glyph_row->glyphs[TEXT_AREA];
15073 toend = to + it->glyph_row->used[TEXT_AREA];
15074
15075 while (from < end)
15076 *to++ = *from++;
15077
15078 /* There may be padding glyphs left over. Overwrite them too. */
15079 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
15080 {
15081 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
15082 while (from < end)
15083 *to++ = *from++;
15084 }
15085
15086 if (to > toend)
15087 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
15088 }
15089
15090
15091 /* Compute the pixel height and width of IT->glyph_row.
15092
15093 Most of the time, ascent and height of a display line will be equal
15094 to the max_ascent and max_height values of the display iterator
15095 structure. This is not the case if
15096
15097 1. We hit ZV without displaying anything. In this case, max_ascent
15098 and max_height will be zero.
15099
15100 2. We have some glyphs that don't contribute to the line height.
15101 (The glyph row flag contributes_to_line_height_p is for future
15102 pixmap extensions).
15103
15104 The first case is easily covered by using default values because in
15105 these cases, the line height does not really matter, except that it
15106 must not be zero. */
15107
15108 static void
15109 compute_line_metrics (it)
15110 struct it *it;
15111 {
15112 struct glyph_row *row = it->glyph_row;
15113 int area, i;
15114
15115 if (FRAME_WINDOW_P (it->f))
15116 {
15117 int i, min_y, max_y;
15118
15119 /* The line may consist of one space only, that was added to
15120 place the cursor on it. If so, the row's height hasn't been
15121 computed yet. */
15122 if (row->height == 0)
15123 {
15124 if (it->max_ascent + it->max_descent == 0)
15125 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
15126 row->ascent = it->max_ascent;
15127 row->height = it->max_ascent + it->max_descent;
15128 row->phys_ascent = it->max_phys_ascent;
15129 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
15130 row->extra_line_spacing = it->max_extra_line_spacing;
15131 }
15132
15133 /* Compute the width of this line. */
15134 row->pixel_width = row->x;
15135 for (i = 0; i < row->used[TEXT_AREA]; ++i)
15136 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
15137
15138 xassert (row->pixel_width >= 0);
15139 xassert (row->ascent >= 0 && row->height > 0);
15140
15141 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
15142 || MATRIX_ROW_OVERLAPS_PRED_P (row));
15143
15144 /* If first line's physical ascent is larger than its logical
15145 ascent, use the physical ascent, and make the row taller.
15146 This makes accented characters fully visible. */
15147 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
15148 && row->phys_ascent > row->ascent)
15149 {
15150 row->height += row->phys_ascent - row->ascent;
15151 row->ascent = row->phys_ascent;
15152 }
15153
15154 /* Compute how much of the line is visible. */
15155 row->visible_height = row->height;
15156
15157 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
15158 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
15159
15160 if (row->y < min_y)
15161 row->visible_height -= min_y - row->y;
15162 if (row->y + row->height > max_y)
15163 row->visible_height -= row->y + row->height - max_y;
15164 }
15165 else
15166 {
15167 row->pixel_width = row->used[TEXT_AREA];
15168 if (row->continued_p)
15169 row->pixel_width -= it->continuation_pixel_width;
15170 else if (row->truncated_on_right_p)
15171 row->pixel_width -= it->truncation_pixel_width;
15172 row->ascent = row->phys_ascent = 0;
15173 row->height = row->phys_height = row->visible_height = 1;
15174 row->extra_line_spacing = 0;
15175 }
15176
15177 /* Compute a hash code for this row. */
15178 row->hash = 0;
15179 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
15180 for (i = 0; i < row->used[area]; ++i)
15181 row->hash = ((((row->hash << 4) + (row->hash >> 24)) & 0x0fffffff)
15182 + row->glyphs[area][i].u.val
15183 + row->glyphs[area][i].face_id
15184 + row->glyphs[area][i].padding_p
15185 + (row->glyphs[area][i].type << 2));
15186
15187 it->max_ascent = it->max_descent = 0;
15188 it->max_phys_ascent = it->max_phys_descent = 0;
15189 }
15190
15191
15192 /* Append one space to the glyph row of iterator IT if doing a
15193 window-based redisplay. The space has the same face as
15194 IT->face_id. Value is non-zero if a space was added.
15195
15196 This function is called to make sure that there is always one glyph
15197 at the end of a glyph row that the cursor can be set on under
15198 window-systems. (If there weren't such a glyph we would not know
15199 how wide and tall a box cursor should be displayed).
15200
15201 At the same time this space let's a nicely handle clearing to the
15202 end of the line if the row ends in italic text. */
15203
15204 static int
15205 append_space_for_newline (it, default_face_p)
15206 struct it *it;
15207 int default_face_p;
15208 {
15209 if (FRAME_WINDOW_P (it->f))
15210 {
15211 int n = it->glyph_row->used[TEXT_AREA];
15212
15213 if (it->glyph_row->glyphs[TEXT_AREA] + n
15214 < it->glyph_row->glyphs[1 + TEXT_AREA])
15215 {
15216 /* Save some values that must not be changed.
15217 Must save IT->c and IT->len because otherwise
15218 ITERATOR_AT_END_P wouldn't work anymore after
15219 append_space_for_newline has been called. */
15220 enum display_element_type saved_what = it->what;
15221 int saved_c = it->c, saved_len = it->len;
15222 int saved_x = it->current_x;
15223 int saved_face_id = it->face_id;
15224 struct text_pos saved_pos;
15225 Lisp_Object saved_object;
15226 struct face *face;
15227
15228 saved_object = it->object;
15229 saved_pos = it->position;
15230
15231 it->what = IT_CHARACTER;
15232 bzero (&it->position, sizeof it->position);
15233 it->object = make_number (0);
15234 it->c = ' ';
15235 it->len = 1;
15236
15237 if (default_face_p)
15238 it->face_id = DEFAULT_FACE_ID;
15239 else if (it->face_before_selective_p)
15240 it->face_id = it->saved_face_id;
15241 face = FACE_FROM_ID (it->f, it->face_id);
15242 it->face_id = FACE_FOR_CHAR (it->f, face, 0);
15243
15244 PRODUCE_GLYPHS (it);
15245
15246 it->override_ascent = -1;
15247 it->constrain_row_ascent_descent_p = 0;
15248 it->current_x = saved_x;
15249 it->object = saved_object;
15250 it->position = saved_pos;
15251 it->what = saved_what;
15252 it->face_id = saved_face_id;
15253 it->len = saved_len;
15254 it->c = saved_c;
15255 return 1;
15256 }
15257 }
15258
15259 return 0;
15260 }
15261
15262
15263 /* Extend the face of the last glyph in the text area of IT->glyph_row
15264 to the end of the display line. Called from display_line.
15265 If the glyph row is empty, add a space glyph to it so that we
15266 know the face to draw. Set the glyph row flag fill_line_p. */
15267
15268 static void
15269 extend_face_to_end_of_line (it)
15270 struct it *it;
15271 {
15272 struct face *face;
15273 struct frame *f = it->f;
15274
15275 /* If line is already filled, do nothing. */
15276 if (it->current_x >= it->last_visible_x)
15277 return;
15278
15279 /* Face extension extends the background and box of IT->face_id
15280 to the end of the line. If the background equals the background
15281 of the frame, we don't have to do anything. */
15282 if (it->face_before_selective_p)
15283 face = FACE_FROM_ID (it->f, it->saved_face_id);
15284 else
15285 face = FACE_FROM_ID (f, it->face_id);
15286
15287 if (FRAME_WINDOW_P (f)
15288 && face->box == FACE_NO_BOX
15289 && face->background == FRAME_BACKGROUND_PIXEL (f)
15290 && !face->stipple)
15291 return;
15292
15293 /* Set the glyph row flag indicating that the face of the last glyph
15294 in the text area has to be drawn to the end of the text area. */
15295 it->glyph_row->fill_line_p = 1;
15296
15297 /* If current character of IT is not ASCII, make sure we have the
15298 ASCII face. This will be automatically undone the next time
15299 get_next_display_element returns a multibyte character. Note
15300 that the character will always be single byte in unibyte text. */
15301 if (!SINGLE_BYTE_CHAR_P (it->c))
15302 {
15303 it->face_id = FACE_FOR_CHAR (f, face, 0);
15304 }
15305
15306 if (FRAME_WINDOW_P (f))
15307 {
15308 /* If the row is empty, add a space with the current face of IT,
15309 so that we know which face to draw. */
15310 if (it->glyph_row->used[TEXT_AREA] == 0)
15311 {
15312 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
15313 it->glyph_row->glyphs[TEXT_AREA][0].face_id = it->face_id;
15314 it->glyph_row->used[TEXT_AREA] = 1;
15315 }
15316 }
15317 else
15318 {
15319 /* Save some values that must not be changed. */
15320 int saved_x = it->current_x;
15321 struct text_pos saved_pos;
15322 Lisp_Object saved_object;
15323 enum display_element_type saved_what = it->what;
15324 int saved_face_id = it->face_id;
15325
15326 saved_object = it->object;
15327 saved_pos = it->position;
15328
15329 it->what = IT_CHARACTER;
15330 bzero (&it->position, sizeof it->position);
15331 it->object = make_number (0);
15332 it->c = ' ';
15333 it->len = 1;
15334 it->face_id = face->id;
15335
15336 PRODUCE_GLYPHS (it);
15337
15338 while (it->current_x <= it->last_visible_x)
15339 PRODUCE_GLYPHS (it);
15340
15341 /* Don't count these blanks really. It would let us insert a left
15342 truncation glyph below and make us set the cursor on them, maybe. */
15343 it->current_x = saved_x;
15344 it->object = saved_object;
15345 it->position = saved_pos;
15346 it->what = saved_what;
15347 it->face_id = saved_face_id;
15348 }
15349 }
15350
15351
15352 /* Value is non-zero if text starting at CHARPOS in current_buffer is
15353 trailing whitespace. */
15354
15355 static int
15356 trailing_whitespace_p (charpos)
15357 int charpos;
15358 {
15359 int bytepos = CHAR_TO_BYTE (charpos);
15360 int c = 0;
15361
15362 while (bytepos < ZV_BYTE
15363 && (c = FETCH_CHAR (bytepos),
15364 c == ' ' || c == '\t'))
15365 ++bytepos;
15366
15367 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
15368 {
15369 if (bytepos != PT_BYTE)
15370 return 1;
15371 }
15372 return 0;
15373 }
15374
15375
15376 /* Highlight trailing whitespace, if any, in ROW. */
15377
15378 void
15379 highlight_trailing_whitespace (f, row)
15380 struct frame *f;
15381 struct glyph_row *row;
15382 {
15383 int used = row->used[TEXT_AREA];
15384
15385 if (used)
15386 {
15387 struct glyph *start = row->glyphs[TEXT_AREA];
15388 struct glyph *glyph = start + used - 1;
15389
15390 /* Skip over glyphs inserted to display the cursor at the
15391 end of a line, for extending the face of the last glyph
15392 to the end of the line on terminals, and for truncation
15393 and continuation glyphs. */
15394 while (glyph >= start
15395 && glyph->type == CHAR_GLYPH
15396 && INTEGERP (glyph->object))
15397 --glyph;
15398
15399 /* If last glyph is a space or stretch, and it's trailing
15400 whitespace, set the face of all trailing whitespace glyphs in
15401 IT->glyph_row to `trailing-whitespace'. */
15402 if (glyph >= start
15403 && BUFFERP (glyph->object)
15404 && (glyph->type == STRETCH_GLYPH
15405 || (glyph->type == CHAR_GLYPH
15406 && glyph->u.ch == ' '))
15407 && trailing_whitespace_p (glyph->charpos))
15408 {
15409 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0, 0);
15410 if (face_id < 0)
15411 return;
15412
15413 while (glyph >= start
15414 && BUFFERP (glyph->object)
15415 && (glyph->type == STRETCH_GLYPH
15416 || (glyph->type == CHAR_GLYPH
15417 && glyph->u.ch == ' ')))
15418 (glyph--)->face_id = face_id;
15419 }
15420 }
15421 }
15422
15423
15424 /* Value is non-zero if glyph row ROW in window W should be
15425 used to hold the cursor. */
15426
15427 static int
15428 cursor_row_p (w, row)
15429 struct window *w;
15430 struct glyph_row *row;
15431 {
15432 int cursor_row_p = 1;
15433
15434 if (PT == MATRIX_ROW_END_CHARPOS (row))
15435 {
15436 /* If the row ends with a newline from a string, we don't want
15437 the cursor there, but we still want it at the start of the
15438 string if the string starts in this row.
15439 If the row is continued it doesn't end in a newline. */
15440 if (CHARPOS (row->end.string_pos) >= 0)
15441 cursor_row_p = (row->continued_p
15442 || PT >= MATRIX_ROW_START_CHARPOS (row));
15443 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
15444 {
15445 /* If the row ends in middle of a real character,
15446 and the line is continued, we want the cursor here.
15447 That's because MATRIX_ROW_END_CHARPOS would equal
15448 PT if PT is before the character. */
15449 if (!row->ends_in_ellipsis_p)
15450 cursor_row_p = row->continued_p;
15451 else
15452 /* If the row ends in an ellipsis, then
15453 MATRIX_ROW_END_CHARPOS will equal point after the invisible text.
15454 We want that position to be displayed after the ellipsis. */
15455 cursor_row_p = 0;
15456 }
15457 /* If the row ends at ZV, display the cursor at the end of that
15458 row instead of at the start of the row below. */
15459 else if (row->ends_at_zv_p)
15460 cursor_row_p = 1;
15461 else
15462 cursor_row_p = 0;
15463 }
15464
15465 return cursor_row_p;
15466 }
15467
15468
15469 /* Construct the glyph row IT->glyph_row in the desired matrix of
15470 IT->w from text at the current position of IT. See dispextern.h
15471 for an overview of struct it. Value is non-zero if
15472 IT->glyph_row displays text, as opposed to a line displaying ZV
15473 only. */
15474
15475 static int
15476 display_line (it)
15477 struct it *it;
15478 {
15479 struct glyph_row *row = it->glyph_row;
15480 Lisp_Object overlay_arrow_string;
15481
15482 /* We always start displaying at hpos zero even if hscrolled. */
15483 xassert (it->hpos == 0 && it->current_x == 0);
15484
15485 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
15486 >= it->w->desired_matrix->nrows)
15487 {
15488 it->w->nrows_scale_factor++;
15489 fonts_changed_p = 1;
15490 return 0;
15491 }
15492
15493 /* Is IT->w showing the region? */
15494 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
15495
15496 /* Clear the result glyph row and enable it. */
15497 prepare_desired_row (row);
15498
15499 row->y = it->current_y;
15500 row->start = it->start;
15501 row->continuation_lines_width = it->continuation_lines_width;
15502 row->displays_text_p = 1;
15503 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
15504 it->starts_in_middle_of_char_p = 0;
15505
15506 /* Arrange the overlays nicely for our purposes. Usually, we call
15507 display_line on only one line at a time, in which case this
15508 can't really hurt too much, or we call it on lines which appear
15509 one after another in the buffer, in which case all calls to
15510 recenter_overlay_lists but the first will be pretty cheap. */
15511 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
15512
15513 /* Move over display elements that are not visible because we are
15514 hscrolled. This may stop at an x-position < IT->first_visible_x
15515 if the first glyph is partially visible or if we hit a line end. */
15516 if (it->current_x < it->first_visible_x)
15517 {
15518 move_it_in_display_line_to (it, ZV, it->first_visible_x,
15519 MOVE_TO_POS | MOVE_TO_X);
15520 }
15521
15522 /* Get the initial row height. This is either the height of the
15523 text hscrolled, if there is any, or zero. */
15524 row->ascent = it->max_ascent;
15525 row->height = it->max_ascent + it->max_descent;
15526 row->phys_ascent = it->max_phys_ascent;
15527 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
15528 row->extra_line_spacing = it->max_extra_line_spacing;
15529
15530 /* Loop generating characters. The loop is left with IT on the next
15531 character to display. */
15532 while (1)
15533 {
15534 int n_glyphs_before, hpos_before, x_before;
15535 int x, i, nglyphs;
15536 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
15537
15538 /* Retrieve the next thing to display. Value is zero if end of
15539 buffer reached. */
15540 if (!get_next_display_element (it))
15541 {
15542 /* Maybe add a space at the end of this line that is used to
15543 display the cursor there under X. Set the charpos of the
15544 first glyph of blank lines not corresponding to any text
15545 to -1. */
15546 #ifdef HAVE_WINDOW_SYSTEM
15547 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
15548 row->exact_window_width_line_p = 1;
15549 else
15550 #endif /* HAVE_WINDOW_SYSTEM */
15551 if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
15552 || row->used[TEXT_AREA] == 0)
15553 {
15554 row->glyphs[TEXT_AREA]->charpos = -1;
15555 row->displays_text_p = 0;
15556
15557 if (!NILP (XBUFFER (it->w->buffer)->indicate_empty_lines)
15558 && (!MINI_WINDOW_P (it->w)
15559 || (minibuf_level && EQ (it->window, minibuf_window))))
15560 row->indicate_empty_line_p = 1;
15561 }
15562
15563 it->continuation_lines_width = 0;
15564 row->ends_at_zv_p = 1;
15565 break;
15566 }
15567
15568 /* Now, get the metrics of what we want to display. This also
15569 generates glyphs in `row' (which is IT->glyph_row). */
15570 n_glyphs_before = row->used[TEXT_AREA];
15571 x = it->current_x;
15572
15573 /* Remember the line height so far in case the next element doesn't
15574 fit on the line. */
15575 if (!it->truncate_lines_p)
15576 {
15577 ascent = it->max_ascent;
15578 descent = it->max_descent;
15579 phys_ascent = it->max_phys_ascent;
15580 phys_descent = it->max_phys_descent;
15581 }
15582
15583 PRODUCE_GLYPHS (it);
15584
15585 /* If this display element was in marginal areas, continue with
15586 the next one. */
15587 if (it->area != TEXT_AREA)
15588 {
15589 row->ascent = max (row->ascent, it->max_ascent);
15590 row->height = max (row->height, it->max_ascent + it->max_descent);
15591 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
15592 row->phys_height = max (row->phys_height,
15593 it->max_phys_ascent + it->max_phys_descent);
15594 row->extra_line_spacing = max (row->extra_line_spacing,
15595 it->max_extra_line_spacing);
15596 set_iterator_to_next (it, 1);
15597 continue;
15598 }
15599
15600 /* Does the display element fit on the line? If we truncate
15601 lines, we should draw past the right edge of the window. If
15602 we don't truncate, we want to stop so that we can display the
15603 continuation glyph before the right margin. If lines are
15604 continued, there are two possible strategies for characters
15605 resulting in more than 1 glyph (e.g. tabs): Display as many
15606 glyphs as possible in this line and leave the rest for the
15607 continuation line, or display the whole element in the next
15608 line. Original redisplay did the former, so we do it also. */
15609 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
15610 hpos_before = it->hpos;
15611 x_before = x;
15612
15613 if (/* Not a newline. */
15614 nglyphs > 0
15615 /* Glyphs produced fit entirely in the line. */
15616 && it->current_x < it->last_visible_x)
15617 {
15618 it->hpos += nglyphs;
15619 row->ascent = max (row->ascent, it->max_ascent);
15620 row->height = max (row->height, it->max_ascent + it->max_descent);
15621 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
15622 row->phys_height = max (row->phys_height,
15623 it->max_phys_ascent + it->max_phys_descent);
15624 row->extra_line_spacing = max (row->extra_line_spacing,
15625 it->max_extra_line_spacing);
15626 if (it->current_x - it->pixel_width < it->first_visible_x)
15627 row->x = x - it->first_visible_x;
15628 }
15629 else
15630 {
15631 int new_x;
15632 struct glyph *glyph;
15633
15634 for (i = 0; i < nglyphs; ++i, x = new_x)
15635 {
15636 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
15637 new_x = x + glyph->pixel_width;
15638
15639 if (/* Lines are continued. */
15640 !it->truncate_lines_p
15641 && (/* Glyph doesn't fit on the line. */
15642 new_x > it->last_visible_x
15643 /* Or it fits exactly on a window system frame. */
15644 || (new_x == it->last_visible_x
15645 && FRAME_WINDOW_P (it->f))))
15646 {
15647 /* End of a continued line. */
15648
15649 if (it->hpos == 0
15650 || (new_x == it->last_visible_x
15651 && FRAME_WINDOW_P (it->f)))
15652 {
15653 /* Current glyph is the only one on the line or
15654 fits exactly on the line. We must continue
15655 the line because we can't draw the cursor
15656 after the glyph. */
15657 row->continued_p = 1;
15658 it->current_x = new_x;
15659 it->continuation_lines_width += new_x;
15660 ++it->hpos;
15661 if (i == nglyphs - 1)
15662 {
15663 set_iterator_to_next (it, 1);
15664 #ifdef HAVE_WINDOW_SYSTEM
15665 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
15666 {
15667 if (!get_next_display_element (it))
15668 {
15669 row->exact_window_width_line_p = 1;
15670 it->continuation_lines_width = 0;
15671 row->continued_p = 0;
15672 row->ends_at_zv_p = 1;
15673 }
15674 else if (ITERATOR_AT_END_OF_LINE_P (it))
15675 {
15676 row->continued_p = 0;
15677 row->exact_window_width_line_p = 1;
15678 }
15679 }
15680 #endif /* HAVE_WINDOW_SYSTEM */
15681 }
15682 }
15683 else if (CHAR_GLYPH_PADDING_P (*glyph)
15684 && !FRAME_WINDOW_P (it->f))
15685 {
15686 /* A padding glyph that doesn't fit on this line.
15687 This means the whole character doesn't fit
15688 on the line. */
15689 row->used[TEXT_AREA] = n_glyphs_before;
15690
15691 /* Fill the rest of the row with continuation
15692 glyphs like in 20.x. */
15693 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
15694 < row->glyphs[1 + TEXT_AREA])
15695 produce_special_glyphs (it, IT_CONTINUATION);
15696
15697 row->continued_p = 1;
15698 it->current_x = x_before;
15699 it->continuation_lines_width += x_before;
15700
15701 /* Restore the height to what it was before the
15702 element not fitting on the line. */
15703 it->max_ascent = ascent;
15704 it->max_descent = descent;
15705 it->max_phys_ascent = phys_ascent;
15706 it->max_phys_descent = phys_descent;
15707 }
15708 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
15709 {
15710 /* A TAB that extends past the right edge of the
15711 window. This produces a single glyph on
15712 window system frames. We leave the glyph in
15713 this row and let it fill the row, but don't
15714 consume the TAB. */
15715 it->continuation_lines_width += it->last_visible_x;
15716 row->ends_in_middle_of_char_p = 1;
15717 row->continued_p = 1;
15718 glyph->pixel_width = it->last_visible_x - x;
15719 it->starts_in_middle_of_char_p = 1;
15720 }
15721 else
15722 {
15723 /* Something other than a TAB that draws past
15724 the right edge of the window. Restore
15725 positions to values before the element. */
15726 row->used[TEXT_AREA] = n_glyphs_before + i;
15727
15728 /* Display continuation glyphs. */
15729 if (!FRAME_WINDOW_P (it->f))
15730 produce_special_glyphs (it, IT_CONTINUATION);
15731 row->continued_p = 1;
15732
15733 it->current_x = x_before;
15734 it->continuation_lines_width += x;
15735 extend_face_to_end_of_line (it);
15736
15737 if (nglyphs > 1 && i > 0)
15738 {
15739 row->ends_in_middle_of_char_p = 1;
15740 it->starts_in_middle_of_char_p = 1;
15741 }
15742
15743 /* Restore the height to what it was before the
15744 element not fitting on the line. */
15745 it->max_ascent = ascent;
15746 it->max_descent = descent;
15747 it->max_phys_ascent = phys_ascent;
15748 it->max_phys_descent = phys_descent;
15749 }
15750
15751 break;
15752 }
15753 else if (new_x > it->first_visible_x)
15754 {
15755 /* Increment number of glyphs actually displayed. */
15756 ++it->hpos;
15757
15758 if (x < it->first_visible_x)
15759 /* Glyph is partially visible, i.e. row starts at
15760 negative X position. */
15761 row->x = x - it->first_visible_x;
15762 }
15763 else
15764 {
15765 /* Glyph is completely off the left margin of the
15766 window. This should not happen because of the
15767 move_it_in_display_line at the start of this
15768 function, unless the text display area of the
15769 window is empty. */
15770 xassert (it->first_visible_x <= it->last_visible_x);
15771 }
15772 }
15773
15774 row->ascent = max (row->ascent, it->max_ascent);
15775 row->height = max (row->height, it->max_ascent + it->max_descent);
15776 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
15777 row->phys_height = max (row->phys_height,
15778 it->max_phys_ascent + it->max_phys_descent);
15779 row->extra_line_spacing = max (row->extra_line_spacing,
15780 it->max_extra_line_spacing);
15781
15782 /* End of this display line if row is continued. */
15783 if (row->continued_p || row->ends_at_zv_p)
15784 break;
15785 }
15786
15787 at_end_of_line:
15788 /* Is this a line end? If yes, we're also done, after making
15789 sure that a non-default face is extended up to the right
15790 margin of the window. */
15791 if (ITERATOR_AT_END_OF_LINE_P (it))
15792 {
15793 int used_before = row->used[TEXT_AREA];
15794
15795 row->ends_in_newline_from_string_p = STRINGP (it->object);
15796
15797 #ifdef HAVE_WINDOW_SYSTEM
15798 /* Add a space at the end of the line that is used to
15799 display the cursor there. */
15800 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
15801 append_space_for_newline (it, 0);
15802 #endif /* HAVE_WINDOW_SYSTEM */
15803
15804 /* Extend the face to the end of the line. */
15805 extend_face_to_end_of_line (it);
15806
15807 /* Make sure we have the position. */
15808 if (used_before == 0)
15809 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
15810
15811 /* Consume the line end. This skips over invisible lines. */
15812 set_iterator_to_next (it, 1);
15813 it->continuation_lines_width = 0;
15814 break;
15815 }
15816
15817 /* Proceed with next display element. Note that this skips
15818 over lines invisible because of selective display. */
15819 set_iterator_to_next (it, 1);
15820
15821 /* If we truncate lines, we are done when the last displayed
15822 glyphs reach past the right margin of the window. */
15823 if (it->truncate_lines_p
15824 && (FRAME_WINDOW_P (it->f)
15825 ? (it->current_x >= it->last_visible_x)
15826 : (it->current_x > it->last_visible_x)))
15827 {
15828 /* Maybe add truncation glyphs. */
15829 if (!FRAME_WINDOW_P (it->f))
15830 {
15831 int i, n;
15832
15833 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
15834 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
15835 break;
15836
15837 for (n = row->used[TEXT_AREA]; i < n; ++i)
15838 {
15839 row->used[TEXT_AREA] = i;
15840 produce_special_glyphs (it, IT_TRUNCATION);
15841 }
15842 }
15843 #ifdef HAVE_WINDOW_SYSTEM
15844 else
15845 {
15846 /* Don't truncate if we can overflow newline into fringe. */
15847 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
15848 {
15849 if (!get_next_display_element (it))
15850 {
15851 it->continuation_lines_width = 0;
15852 row->ends_at_zv_p = 1;
15853 row->exact_window_width_line_p = 1;
15854 break;
15855 }
15856 if (ITERATOR_AT_END_OF_LINE_P (it))
15857 {
15858 row->exact_window_width_line_p = 1;
15859 goto at_end_of_line;
15860 }
15861 }
15862 }
15863 #endif /* HAVE_WINDOW_SYSTEM */
15864
15865 row->truncated_on_right_p = 1;
15866 it->continuation_lines_width = 0;
15867 reseat_at_next_visible_line_start (it, 0);
15868 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
15869 it->hpos = hpos_before;
15870 it->current_x = x_before;
15871 break;
15872 }
15873 }
15874
15875 /* If line is not empty and hscrolled, maybe insert truncation glyphs
15876 at the left window margin. */
15877 if (it->first_visible_x
15878 && IT_CHARPOS (*it) != MATRIX_ROW_START_CHARPOS (row))
15879 {
15880 if (!FRAME_WINDOW_P (it->f))
15881 insert_left_trunc_glyphs (it);
15882 row->truncated_on_left_p = 1;
15883 }
15884
15885 /* If the start of this line is the overlay arrow-position, then
15886 mark this glyph row as the one containing the overlay arrow.
15887 This is clearly a mess with variable size fonts. It would be
15888 better to let it be displayed like cursors under X. */
15889 if ((row->displays_text_p || !overlay_arrow_seen)
15890 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
15891 !NILP (overlay_arrow_string)))
15892 {
15893 /* Overlay arrow in window redisplay is a fringe bitmap. */
15894 if (STRINGP (overlay_arrow_string))
15895 {
15896 struct glyph_row *arrow_row
15897 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
15898 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
15899 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
15900 struct glyph *p = row->glyphs[TEXT_AREA];
15901 struct glyph *p2, *end;
15902
15903 /* Copy the arrow glyphs. */
15904 while (glyph < arrow_end)
15905 *p++ = *glyph++;
15906
15907 /* Throw away padding glyphs. */
15908 p2 = p;
15909 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
15910 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
15911 ++p2;
15912 if (p2 > p)
15913 {
15914 while (p2 < end)
15915 *p++ = *p2++;
15916 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
15917 }
15918 }
15919 else
15920 {
15921 xassert (INTEGERP (overlay_arrow_string));
15922 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
15923 }
15924 overlay_arrow_seen = 1;
15925 }
15926
15927 /* Compute pixel dimensions of this line. */
15928 compute_line_metrics (it);
15929
15930 /* Remember the position at which this line ends. */
15931 row->end = it->current;
15932
15933 /* Record whether this row ends inside an ellipsis. */
15934 row->ends_in_ellipsis_p
15935 = (it->method == GET_FROM_DISPLAY_VECTOR
15936 && it->ellipsis_p);
15937
15938 /* Save fringe bitmaps in this row. */
15939 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
15940 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
15941 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
15942 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
15943
15944 it->left_user_fringe_bitmap = 0;
15945 it->left_user_fringe_face_id = 0;
15946 it->right_user_fringe_bitmap = 0;
15947 it->right_user_fringe_face_id = 0;
15948
15949 /* Maybe set the cursor. */
15950 if (it->w->cursor.vpos < 0
15951 && PT >= MATRIX_ROW_START_CHARPOS (row)
15952 && PT <= MATRIX_ROW_END_CHARPOS (row)
15953 && cursor_row_p (it->w, row))
15954 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
15955
15956 /* Highlight trailing whitespace. */
15957 if (!NILP (Vshow_trailing_whitespace))
15958 highlight_trailing_whitespace (it->f, it->glyph_row);
15959
15960 /* Prepare for the next line. This line starts horizontally at (X
15961 HPOS) = (0 0). Vertical positions are incremented. As a
15962 convenience for the caller, IT->glyph_row is set to the next
15963 row to be used. */
15964 it->current_x = it->hpos = 0;
15965 it->current_y += row->height;
15966 ++it->vpos;
15967 ++it->glyph_row;
15968 it->start = it->current;
15969 return row->displays_text_p;
15970 }
15971
15972
15973 \f
15974 /***********************************************************************
15975 Menu Bar
15976 ***********************************************************************/
15977
15978 /* Redisplay the menu bar in the frame for window W.
15979
15980 The menu bar of X frames that don't have X toolkit support is
15981 displayed in a special window W->frame->menu_bar_window.
15982
15983 The menu bar of terminal frames is treated specially as far as
15984 glyph matrices are concerned. Menu bar lines are not part of
15985 windows, so the update is done directly on the frame matrix rows
15986 for the menu bar. */
15987
15988 static void
15989 display_menu_bar (w)
15990 struct window *w;
15991 {
15992 struct frame *f = XFRAME (WINDOW_FRAME (w));
15993 struct it it;
15994 Lisp_Object items;
15995 int i;
15996
15997 /* Don't do all this for graphical frames. */
15998 #ifdef HAVE_NTGUI
15999 if (!NILP (Vwindow_system))
16000 return;
16001 #endif
16002 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
16003 if (FRAME_X_P (f))
16004 return;
16005 #endif
16006 #ifdef MAC_OS
16007 if (FRAME_MAC_P (f))
16008 return;
16009 #endif
16010
16011 #ifdef USE_X_TOOLKIT
16012 xassert (!FRAME_WINDOW_P (f));
16013 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
16014 it.first_visible_x = 0;
16015 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
16016 #else /* not USE_X_TOOLKIT */
16017 if (FRAME_WINDOW_P (f))
16018 {
16019 /* Menu bar lines are displayed in the desired matrix of the
16020 dummy window menu_bar_window. */
16021 struct window *menu_w;
16022 xassert (WINDOWP (f->menu_bar_window));
16023 menu_w = XWINDOW (f->menu_bar_window);
16024 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
16025 MENU_FACE_ID);
16026 it.first_visible_x = 0;
16027 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
16028 }
16029 else
16030 {
16031 /* This is a TTY frame, i.e. character hpos/vpos are used as
16032 pixel x/y. */
16033 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
16034 MENU_FACE_ID);
16035 it.first_visible_x = 0;
16036 it.last_visible_x = FRAME_COLS (f);
16037 }
16038 #endif /* not USE_X_TOOLKIT */
16039
16040 if (! mode_line_inverse_video)
16041 /* Force the menu-bar to be displayed in the default face. */
16042 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
16043
16044 /* Clear all rows of the menu bar. */
16045 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
16046 {
16047 struct glyph_row *row = it.glyph_row + i;
16048 clear_glyph_row (row);
16049 row->enabled_p = 1;
16050 row->full_width_p = 1;
16051 }
16052
16053 /* Display all items of the menu bar. */
16054 items = FRAME_MENU_BAR_ITEMS (it.f);
16055 for (i = 0; i < XVECTOR (items)->size; i += 4)
16056 {
16057 Lisp_Object string;
16058
16059 /* Stop at nil string. */
16060 string = AREF (items, i + 1);
16061 if (NILP (string))
16062 break;
16063
16064 /* Remember where item was displayed. */
16065 AREF (items, i + 3) = make_number (it.hpos);
16066
16067 /* Display the item, pad with one space. */
16068 if (it.current_x < it.last_visible_x)
16069 display_string (NULL, string, Qnil, 0, 0, &it,
16070 SCHARS (string) + 1, 0, 0, -1);
16071 }
16072
16073 /* Fill out the line with spaces. */
16074 if (it.current_x < it.last_visible_x)
16075 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
16076
16077 /* Compute the total height of the lines. */
16078 compute_line_metrics (&it);
16079 }
16080
16081
16082 \f
16083 /***********************************************************************
16084 Mode Line
16085 ***********************************************************************/
16086
16087 /* Redisplay mode lines in the window tree whose root is WINDOW. If
16088 FORCE is non-zero, redisplay mode lines unconditionally.
16089 Otherwise, redisplay only mode lines that are garbaged. Value is
16090 the number of windows whose mode lines were redisplayed. */
16091
16092 static int
16093 redisplay_mode_lines (window, force)
16094 Lisp_Object window;
16095 int force;
16096 {
16097 int nwindows = 0;
16098
16099 while (!NILP (window))
16100 {
16101 struct window *w = XWINDOW (window);
16102
16103 if (WINDOWP (w->hchild))
16104 nwindows += redisplay_mode_lines (w->hchild, force);
16105 else if (WINDOWP (w->vchild))
16106 nwindows += redisplay_mode_lines (w->vchild, force);
16107 else if (force
16108 || FRAME_GARBAGED_P (XFRAME (w->frame))
16109 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
16110 {
16111 struct text_pos lpoint;
16112 struct buffer *old = current_buffer;
16113
16114 /* Set the window's buffer for the mode line display. */
16115 SET_TEXT_POS (lpoint, PT, PT_BYTE);
16116 set_buffer_internal_1 (XBUFFER (w->buffer));
16117
16118 /* Point refers normally to the selected window. For any
16119 other window, set up appropriate value. */
16120 if (!EQ (window, selected_window))
16121 {
16122 struct text_pos pt;
16123
16124 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
16125 if (CHARPOS (pt) < BEGV)
16126 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
16127 else if (CHARPOS (pt) > (ZV - 1))
16128 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
16129 else
16130 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
16131 }
16132
16133 /* Display mode lines. */
16134 clear_glyph_matrix (w->desired_matrix);
16135 if (display_mode_lines (w))
16136 {
16137 ++nwindows;
16138 w->must_be_updated_p = 1;
16139 }
16140
16141 /* Restore old settings. */
16142 set_buffer_internal_1 (old);
16143 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
16144 }
16145
16146 window = w->next;
16147 }
16148
16149 return nwindows;
16150 }
16151
16152
16153 /* Display the mode and/or top line of window W. Value is the number
16154 of mode lines displayed. */
16155
16156 static int
16157 display_mode_lines (w)
16158 struct window *w;
16159 {
16160 Lisp_Object old_selected_window, old_selected_frame;
16161 int n = 0;
16162
16163 old_selected_frame = selected_frame;
16164 selected_frame = w->frame;
16165 old_selected_window = selected_window;
16166 XSETWINDOW (selected_window, w);
16167
16168 /* These will be set while the mode line specs are processed. */
16169 line_number_displayed = 0;
16170 w->column_number_displayed = Qnil;
16171
16172 if (WINDOW_WANTS_MODELINE_P (w))
16173 {
16174 struct window *sel_w = XWINDOW (old_selected_window);
16175
16176 /* Select mode line face based on the real selected window. */
16177 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
16178 current_buffer->mode_line_format);
16179 ++n;
16180 }
16181
16182 if (WINDOW_WANTS_HEADER_LINE_P (w))
16183 {
16184 display_mode_line (w, HEADER_LINE_FACE_ID,
16185 current_buffer->header_line_format);
16186 ++n;
16187 }
16188
16189 selected_frame = old_selected_frame;
16190 selected_window = old_selected_window;
16191 return n;
16192 }
16193
16194
16195 /* Display mode or top line of window W. FACE_ID specifies which line
16196 to display; it is either MODE_LINE_FACE_ID or HEADER_LINE_FACE_ID.
16197 FORMAT is the mode line format to display. Value is the pixel
16198 height of the mode line displayed. */
16199
16200 static int
16201 display_mode_line (w, face_id, format)
16202 struct window *w;
16203 enum face_id face_id;
16204 Lisp_Object format;
16205 {
16206 struct it it;
16207 struct face *face;
16208 int count = SPECPDL_INDEX ();
16209
16210 init_iterator (&it, w, -1, -1, NULL, face_id);
16211 prepare_desired_row (it.glyph_row);
16212
16213 it.glyph_row->mode_line_p = 1;
16214
16215 if (! mode_line_inverse_video)
16216 /* Force the mode-line to be displayed in the default face. */
16217 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
16218
16219 record_unwind_protect (unwind_format_mode_line,
16220 format_mode_line_unwind_data (NULL, 0));
16221
16222 mode_line_target = MODE_LINE_DISPLAY;
16223
16224 /* Temporarily make frame's keyboard the current kboard so that
16225 kboard-local variables in the mode_line_format will get the right
16226 values. */
16227 push_frame_kboard (it.f);
16228 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
16229 pop_frame_kboard ();
16230
16231 unbind_to (count, Qnil);
16232
16233 /* Fill up with spaces. */
16234 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
16235
16236 compute_line_metrics (&it);
16237 it.glyph_row->full_width_p = 1;
16238 it.glyph_row->continued_p = 0;
16239 it.glyph_row->truncated_on_left_p = 0;
16240 it.glyph_row->truncated_on_right_p = 0;
16241
16242 /* Make a 3D mode-line have a shadow at its right end. */
16243 face = FACE_FROM_ID (it.f, face_id);
16244 extend_face_to_end_of_line (&it);
16245 if (face->box != FACE_NO_BOX)
16246 {
16247 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
16248 + it.glyph_row->used[TEXT_AREA] - 1);
16249 last->right_box_line_p = 1;
16250 }
16251
16252 return it.glyph_row->height;
16253 }
16254
16255 /* Move element ELT in LIST to the front of LIST.
16256 Return the updated list. */
16257
16258 static Lisp_Object
16259 move_elt_to_front (elt, list)
16260 Lisp_Object elt, list;
16261 {
16262 register Lisp_Object tail, prev;
16263 register Lisp_Object tem;
16264
16265 tail = list;
16266 prev = Qnil;
16267 while (CONSP (tail))
16268 {
16269 tem = XCAR (tail);
16270
16271 if (EQ (elt, tem))
16272 {
16273 /* Splice out the link TAIL. */
16274 if (NILP (prev))
16275 list = XCDR (tail);
16276 else
16277 Fsetcdr (prev, XCDR (tail));
16278
16279 /* Now make it the first. */
16280 Fsetcdr (tail, list);
16281 return tail;
16282 }
16283 else
16284 prev = tail;
16285 tail = XCDR (tail);
16286 QUIT;
16287 }
16288
16289 /* Not found--return unchanged LIST. */
16290 return list;
16291 }
16292
16293 /* Contribute ELT to the mode line for window IT->w. How it
16294 translates into text depends on its data type.
16295
16296 IT describes the display environment in which we display, as usual.
16297
16298 DEPTH is the depth in recursion. It is used to prevent
16299 infinite recursion here.
16300
16301 FIELD_WIDTH is the number of characters the display of ELT should
16302 occupy in the mode line, and PRECISION is the maximum number of
16303 characters to display from ELT's representation. See
16304 display_string for details.
16305
16306 Returns the hpos of the end of the text generated by ELT.
16307
16308 PROPS is a property list to add to any string we encounter.
16309
16310 If RISKY is nonzero, remove (disregard) any properties in any string
16311 we encounter, and ignore :eval and :propertize.
16312
16313 The global variable `mode_line_target' determines whether the
16314 output is passed to `store_mode_line_noprop',
16315 `store_mode_line_string', or `display_string'. */
16316
16317 static int
16318 display_mode_element (it, depth, field_width, precision, elt, props, risky)
16319 struct it *it;
16320 int depth;
16321 int field_width, precision;
16322 Lisp_Object elt, props;
16323 int risky;
16324 {
16325 int n = 0, field, prec;
16326 int literal = 0;
16327
16328 tail_recurse:
16329 if (depth > 100)
16330 elt = build_string ("*too-deep*");
16331
16332 depth++;
16333
16334 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
16335 {
16336 case Lisp_String:
16337 {
16338 /* A string: output it and check for %-constructs within it. */
16339 unsigned char c;
16340 int offset = 0;
16341
16342 if (SCHARS (elt) > 0
16343 && (!NILP (props) || risky))
16344 {
16345 Lisp_Object oprops, aelt;
16346 oprops = Ftext_properties_at (make_number (0), elt);
16347
16348 /* If the starting string's properties are not what
16349 we want, translate the string. Also, if the string
16350 is risky, do that anyway. */
16351
16352 if (NILP (Fequal (props, oprops)) || risky)
16353 {
16354 /* If the starting string has properties,
16355 merge the specified ones onto the existing ones. */
16356 if (! NILP (oprops) && !risky)
16357 {
16358 Lisp_Object tem;
16359
16360 oprops = Fcopy_sequence (oprops);
16361 tem = props;
16362 while (CONSP (tem))
16363 {
16364 oprops = Fplist_put (oprops, XCAR (tem),
16365 XCAR (XCDR (tem)));
16366 tem = XCDR (XCDR (tem));
16367 }
16368 props = oprops;
16369 }
16370
16371 aelt = Fassoc (elt, mode_line_proptrans_alist);
16372 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
16373 {
16374 /* AELT is what we want. Move it to the front
16375 without consing. */
16376 elt = XCAR (aelt);
16377 mode_line_proptrans_alist
16378 = move_elt_to_front (aelt, mode_line_proptrans_alist);
16379 }
16380 else
16381 {
16382 Lisp_Object tem;
16383
16384 /* If AELT has the wrong props, it is useless.
16385 so get rid of it. */
16386 if (! NILP (aelt))
16387 mode_line_proptrans_alist
16388 = Fdelq (aelt, mode_line_proptrans_alist);
16389
16390 elt = Fcopy_sequence (elt);
16391 Fset_text_properties (make_number (0), Flength (elt),
16392 props, elt);
16393 /* Add this item to mode_line_proptrans_alist. */
16394 mode_line_proptrans_alist
16395 = Fcons (Fcons (elt, props),
16396 mode_line_proptrans_alist);
16397 /* Truncate mode_line_proptrans_alist
16398 to at most 50 elements. */
16399 tem = Fnthcdr (make_number (50),
16400 mode_line_proptrans_alist);
16401 if (! NILP (tem))
16402 XSETCDR (tem, Qnil);
16403 }
16404 }
16405 }
16406
16407 offset = 0;
16408
16409 if (literal)
16410 {
16411 prec = precision - n;
16412 switch (mode_line_target)
16413 {
16414 case MODE_LINE_NOPROP:
16415 case MODE_LINE_TITLE:
16416 n += store_mode_line_noprop (SDATA (elt), -1, prec);
16417 break;
16418 case MODE_LINE_STRING:
16419 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
16420 break;
16421 case MODE_LINE_DISPLAY:
16422 n += display_string (NULL, elt, Qnil, 0, 0, it,
16423 0, prec, 0, STRING_MULTIBYTE (elt));
16424 break;
16425 }
16426
16427 break;
16428 }
16429
16430 /* Handle the non-literal case. */
16431
16432 while ((precision <= 0 || n < precision)
16433 && SREF (elt, offset) != 0
16434 && (mode_line_target != MODE_LINE_DISPLAY
16435 || it->current_x < it->last_visible_x))
16436 {
16437 int last_offset = offset;
16438
16439 /* Advance to end of string or next format specifier. */
16440 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
16441 ;
16442
16443 if (offset - 1 != last_offset)
16444 {
16445 int nchars, nbytes;
16446
16447 /* Output to end of string or up to '%'. Field width
16448 is length of string. Don't output more than
16449 PRECISION allows us. */
16450 offset--;
16451
16452 prec = c_string_width (SDATA (elt) + last_offset,
16453 offset - last_offset, precision - n,
16454 &nchars, &nbytes);
16455
16456 switch (mode_line_target)
16457 {
16458 case MODE_LINE_NOPROP:
16459 case MODE_LINE_TITLE:
16460 n += store_mode_line_noprop (SDATA (elt) + last_offset, 0, prec);
16461 break;
16462 case MODE_LINE_STRING:
16463 {
16464 int bytepos = last_offset;
16465 int charpos = string_byte_to_char (elt, bytepos);
16466 int endpos = (precision <= 0
16467 ? string_byte_to_char (elt, offset)
16468 : charpos + nchars);
16469
16470 n += store_mode_line_string (NULL,
16471 Fsubstring (elt, make_number (charpos),
16472 make_number (endpos)),
16473 0, 0, 0, Qnil);
16474 }
16475 break;
16476 case MODE_LINE_DISPLAY:
16477 {
16478 int bytepos = last_offset;
16479 int charpos = string_byte_to_char (elt, bytepos);
16480
16481 if (precision <= 0)
16482 nchars = string_byte_to_char (elt, offset) - charpos;
16483 n += display_string (NULL, elt, Qnil, 0, charpos,
16484 it, 0, nchars, 0,
16485 STRING_MULTIBYTE (elt));
16486 }
16487 break;
16488 }
16489 }
16490 else /* c == '%' */
16491 {
16492 int percent_position = offset;
16493
16494 /* Get the specified minimum width. Zero means
16495 don't pad. */
16496 field = 0;
16497 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
16498 field = field * 10 + c - '0';
16499
16500 /* Don't pad beyond the total padding allowed. */
16501 if (field_width - n > 0 && field > field_width - n)
16502 field = field_width - n;
16503
16504 /* Note that either PRECISION <= 0 or N < PRECISION. */
16505 prec = precision - n;
16506
16507 if (c == 'M')
16508 n += display_mode_element (it, depth, field, prec,
16509 Vglobal_mode_string, props,
16510 risky);
16511 else if (c != 0)
16512 {
16513 int multibyte;
16514 int bytepos, charpos;
16515 unsigned char *spec;
16516
16517 bytepos = percent_position;
16518 charpos = (STRING_MULTIBYTE (elt)
16519 ? string_byte_to_char (elt, bytepos)
16520 : bytepos);
16521
16522 spec
16523 = decode_mode_spec (it->w, c, field, prec, &multibyte);
16524
16525 switch (mode_line_target)
16526 {
16527 case MODE_LINE_NOPROP:
16528 case MODE_LINE_TITLE:
16529 n += store_mode_line_noprop (spec, field, prec);
16530 break;
16531 case MODE_LINE_STRING:
16532 {
16533 int len = strlen (spec);
16534 Lisp_Object tem = make_string (spec, len);
16535 props = Ftext_properties_at (make_number (charpos), elt);
16536 /* Should only keep face property in props */
16537 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
16538 }
16539 break;
16540 case MODE_LINE_DISPLAY:
16541 {
16542 int nglyphs_before, nwritten;
16543
16544 nglyphs_before = it->glyph_row->used[TEXT_AREA];
16545 nwritten = display_string (spec, Qnil, elt,
16546 charpos, 0, it,
16547 field, prec, 0,
16548 multibyte);
16549
16550 /* Assign to the glyphs written above the
16551 string where the `%x' came from, position
16552 of the `%'. */
16553 if (nwritten > 0)
16554 {
16555 struct glyph *glyph
16556 = (it->glyph_row->glyphs[TEXT_AREA]
16557 + nglyphs_before);
16558 int i;
16559
16560 for (i = 0; i < nwritten; ++i)
16561 {
16562 glyph[i].object = elt;
16563 glyph[i].charpos = charpos;
16564 }
16565
16566 n += nwritten;
16567 }
16568 }
16569 break;
16570 }
16571 }
16572 else /* c == 0 */
16573 break;
16574 }
16575 }
16576 }
16577 break;
16578
16579 case Lisp_Symbol:
16580 /* A symbol: process the value of the symbol recursively
16581 as if it appeared here directly. Avoid error if symbol void.
16582 Special case: if value of symbol is a string, output the string
16583 literally. */
16584 {
16585 register Lisp_Object tem;
16586
16587 /* If the variable is not marked as risky to set
16588 then its contents are risky to use. */
16589 if (NILP (Fget (elt, Qrisky_local_variable)))
16590 risky = 1;
16591
16592 tem = Fboundp (elt);
16593 if (!NILP (tem))
16594 {
16595 tem = Fsymbol_value (elt);
16596 /* If value is a string, output that string literally:
16597 don't check for % within it. */
16598 if (STRINGP (tem))
16599 literal = 1;
16600
16601 if (!EQ (tem, elt))
16602 {
16603 /* Give up right away for nil or t. */
16604 elt = tem;
16605 goto tail_recurse;
16606 }
16607 }
16608 }
16609 break;
16610
16611 case Lisp_Cons:
16612 {
16613 register Lisp_Object car, tem;
16614
16615 /* A cons cell: five distinct cases.
16616 If first element is :eval or :propertize, do something special.
16617 If first element is a string or a cons, process all the elements
16618 and effectively concatenate them.
16619 If first element is a negative number, truncate displaying cdr to
16620 at most that many characters. If positive, pad (with spaces)
16621 to at least that many characters.
16622 If first element is a symbol, process the cadr or caddr recursively
16623 according to whether the symbol's value is non-nil or nil. */
16624 car = XCAR (elt);
16625 if (EQ (car, QCeval))
16626 {
16627 /* An element of the form (:eval FORM) means evaluate FORM
16628 and use the result as mode line elements. */
16629
16630 if (risky)
16631 break;
16632
16633 if (CONSP (XCDR (elt)))
16634 {
16635 Lisp_Object spec;
16636 spec = safe_eval (XCAR (XCDR (elt)));
16637 n += display_mode_element (it, depth, field_width - n,
16638 precision - n, spec, props,
16639 risky);
16640 }
16641 }
16642 else if (EQ (car, QCpropertize))
16643 {
16644 /* An element of the form (:propertize ELT PROPS...)
16645 means display ELT but applying properties PROPS. */
16646
16647 if (risky)
16648 break;
16649
16650 if (CONSP (XCDR (elt)))
16651 n += display_mode_element (it, depth, field_width - n,
16652 precision - n, XCAR (XCDR (elt)),
16653 XCDR (XCDR (elt)), risky);
16654 }
16655 else if (SYMBOLP (car))
16656 {
16657 tem = Fboundp (car);
16658 elt = XCDR (elt);
16659 if (!CONSP (elt))
16660 goto invalid;
16661 /* elt is now the cdr, and we know it is a cons cell.
16662 Use its car if CAR has a non-nil value. */
16663 if (!NILP (tem))
16664 {
16665 tem = Fsymbol_value (car);
16666 if (!NILP (tem))
16667 {
16668 elt = XCAR (elt);
16669 goto tail_recurse;
16670 }
16671 }
16672 /* Symbol's value is nil (or symbol is unbound)
16673 Get the cddr of the original list
16674 and if possible find the caddr and use that. */
16675 elt = XCDR (elt);
16676 if (NILP (elt))
16677 break;
16678 else if (!CONSP (elt))
16679 goto invalid;
16680 elt = XCAR (elt);
16681 goto tail_recurse;
16682 }
16683 else if (INTEGERP (car))
16684 {
16685 register int lim = XINT (car);
16686 elt = XCDR (elt);
16687 if (lim < 0)
16688 {
16689 /* Negative int means reduce maximum width. */
16690 if (precision <= 0)
16691 precision = -lim;
16692 else
16693 precision = min (precision, -lim);
16694 }
16695 else if (lim > 0)
16696 {
16697 /* Padding specified. Don't let it be more than
16698 current maximum. */
16699 if (precision > 0)
16700 lim = min (precision, lim);
16701
16702 /* If that's more padding than already wanted, queue it.
16703 But don't reduce padding already specified even if
16704 that is beyond the current truncation point. */
16705 field_width = max (lim, field_width);
16706 }
16707 goto tail_recurse;
16708 }
16709 else if (STRINGP (car) || CONSP (car))
16710 {
16711 register int limit = 50;
16712 /* Limit is to protect against circular lists. */
16713 while (CONSP (elt)
16714 && --limit > 0
16715 && (precision <= 0 || n < precision))
16716 {
16717 n += display_mode_element (it, depth,
16718 /* Do padding only after the last
16719 element in the list. */
16720 (! CONSP (XCDR (elt))
16721 ? field_width - n
16722 : 0),
16723 precision - n, XCAR (elt),
16724 props, risky);
16725 elt = XCDR (elt);
16726 }
16727 }
16728 }
16729 break;
16730
16731 default:
16732 invalid:
16733 elt = build_string ("*invalid*");
16734 goto tail_recurse;
16735 }
16736
16737 /* Pad to FIELD_WIDTH. */
16738 if (field_width > 0 && n < field_width)
16739 {
16740 switch (mode_line_target)
16741 {
16742 case MODE_LINE_NOPROP:
16743 case MODE_LINE_TITLE:
16744 n += store_mode_line_noprop ("", field_width - n, 0);
16745 break;
16746 case MODE_LINE_STRING:
16747 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
16748 break;
16749 case MODE_LINE_DISPLAY:
16750 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
16751 0, 0, 0);
16752 break;
16753 }
16754 }
16755
16756 return n;
16757 }
16758
16759 /* Store a mode-line string element in mode_line_string_list.
16760
16761 If STRING is non-null, display that C string. Otherwise, the Lisp
16762 string LISP_STRING is displayed.
16763
16764 FIELD_WIDTH is the minimum number of output glyphs to produce.
16765 If STRING has fewer characters than FIELD_WIDTH, pad to the right
16766 with spaces. FIELD_WIDTH <= 0 means don't pad.
16767
16768 PRECISION is the maximum number of characters to output from
16769 STRING. PRECISION <= 0 means don't truncate the string.
16770
16771 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
16772 properties to the string.
16773
16774 PROPS are the properties to add to the string.
16775 The mode_line_string_face face property is always added to the string.
16776 */
16777
16778 static int
16779 store_mode_line_string (string, lisp_string, copy_string, field_width, precision, props)
16780 char *string;
16781 Lisp_Object lisp_string;
16782 int copy_string;
16783 int field_width;
16784 int precision;
16785 Lisp_Object props;
16786 {
16787 int len;
16788 int n = 0;
16789
16790 if (string != NULL)
16791 {
16792 len = strlen (string);
16793 if (precision > 0 && len > precision)
16794 len = precision;
16795 lisp_string = make_string (string, len);
16796 if (NILP (props))
16797 props = mode_line_string_face_prop;
16798 else if (!NILP (mode_line_string_face))
16799 {
16800 Lisp_Object face = Fplist_get (props, Qface);
16801 props = Fcopy_sequence (props);
16802 if (NILP (face))
16803 face = mode_line_string_face;
16804 else
16805 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
16806 props = Fplist_put (props, Qface, face);
16807 }
16808 Fadd_text_properties (make_number (0), make_number (len),
16809 props, lisp_string);
16810 }
16811 else
16812 {
16813 len = XFASTINT (Flength (lisp_string));
16814 if (precision > 0 && len > precision)
16815 {
16816 len = precision;
16817 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
16818 precision = -1;
16819 }
16820 if (!NILP (mode_line_string_face))
16821 {
16822 Lisp_Object face;
16823 if (NILP (props))
16824 props = Ftext_properties_at (make_number (0), lisp_string);
16825 face = Fplist_get (props, Qface);
16826 if (NILP (face))
16827 face = mode_line_string_face;
16828 else
16829 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
16830 props = Fcons (Qface, Fcons (face, Qnil));
16831 if (copy_string)
16832 lisp_string = Fcopy_sequence (lisp_string);
16833 }
16834 if (!NILP (props))
16835 Fadd_text_properties (make_number (0), make_number (len),
16836 props, lisp_string);
16837 }
16838
16839 if (len > 0)
16840 {
16841 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
16842 n += len;
16843 }
16844
16845 if (field_width > len)
16846 {
16847 field_width -= len;
16848 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
16849 if (!NILP (props))
16850 Fadd_text_properties (make_number (0), make_number (field_width),
16851 props, lisp_string);
16852 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
16853 n += field_width;
16854 }
16855
16856 return n;
16857 }
16858
16859
16860 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
16861 1, 4, 0,
16862 doc: /* Format a string out of a mode line format specification.
16863 First arg FORMAT specifies the mode line format (see `mode-line-format'
16864 for details) to use.
16865
16866 Optional second arg FACE specifies the face property to put
16867 on all characters for which no face is specified.
16868 t means whatever face the window's mode line currently uses
16869 \(either `mode-line' or `mode-line-inactive', depending).
16870 nil means the default is no face property.
16871 If FACE is an integer, the value string has no text properties.
16872
16873 Optional third and fourth args WINDOW and BUFFER specify the window
16874 and buffer to use as the context for the formatting (defaults
16875 are the selected window and the window's buffer). */)
16876 (format, face, window, buffer)
16877 Lisp_Object format, face, window, buffer;
16878 {
16879 struct it it;
16880 int len;
16881 struct window *w;
16882 struct buffer *old_buffer = NULL;
16883 int face_id = -1;
16884 int no_props = INTEGERP (face);
16885 int count = SPECPDL_INDEX ();
16886 Lisp_Object str;
16887 int string_start = 0;
16888
16889 if (NILP (window))
16890 window = selected_window;
16891 CHECK_WINDOW (window);
16892 w = XWINDOW (window);
16893
16894 if (NILP (buffer))
16895 buffer = w->buffer;
16896 CHECK_BUFFER (buffer);
16897
16898 if (NILP (format))
16899 return build_string ("");
16900
16901 if (no_props)
16902 face = Qnil;
16903
16904 if (!NILP (face))
16905 {
16906 if (EQ (face, Qt))
16907 face = (EQ (window, selected_window) ? Qmode_line : Qmode_line_inactive);
16908 face_id = lookup_named_face (XFRAME (WINDOW_FRAME (w)), face, 0, 0);
16909 }
16910
16911 if (face_id < 0)
16912 face_id = DEFAULT_FACE_ID;
16913
16914 if (XBUFFER (buffer) != current_buffer)
16915 old_buffer = current_buffer;
16916
16917 /* Save things including mode_line_proptrans_alist,
16918 and set that to nil so that we don't alter the outer value. */
16919 record_unwind_protect (unwind_format_mode_line,
16920 format_mode_line_unwind_data (old_buffer, 1));
16921 mode_line_proptrans_alist = Qnil;
16922
16923 if (old_buffer)
16924 set_buffer_internal_1 (XBUFFER (buffer));
16925
16926 init_iterator (&it, w, -1, -1, NULL, face_id);
16927
16928 if (no_props)
16929 {
16930 mode_line_target = MODE_LINE_NOPROP;
16931 mode_line_string_face_prop = Qnil;
16932 mode_line_string_list = Qnil;
16933 string_start = MODE_LINE_NOPROP_LEN (0);
16934 }
16935 else
16936 {
16937 mode_line_target = MODE_LINE_STRING;
16938 mode_line_string_list = Qnil;
16939 mode_line_string_face = face;
16940 mode_line_string_face_prop
16941 = (NILP (face) ? Qnil : Fcons (Qface, Fcons (face, Qnil)));
16942 }
16943
16944 push_frame_kboard (it.f);
16945 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
16946 pop_frame_kboard ();
16947
16948 if (no_props)
16949 {
16950 len = MODE_LINE_NOPROP_LEN (string_start);
16951 str = make_string (mode_line_noprop_buf + string_start, len);
16952 }
16953 else
16954 {
16955 mode_line_string_list = Fnreverse (mode_line_string_list);
16956 str = Fmapconcat (intern ("identity"), mode_line_string_list,
16957 make_string ("", 0));
16958 }
16959
16960 unbind_to (count, Qnil);
16961 return str;
16962 }
16963
16964 /* Write a null-terminated, right justified decimal representation of
16965 the positive integer D to BUF using a minimal field width WIDTH. */
16966
16967 static void
16968 pint2str (buf, width, d)
16969 register char *buf;
16970 register int width;
16971 register int d;
16972 {
16973 register char *p = buf;
16974
16975 if (d <= 0)
16976 *p++ = '0';
16977 else
16978 {
16979 while (d > 0)
16980 {
16981 *p++ = d % 10 + '0';
16982 d /= 10;
16983 }
16984 }
16985
16986 for (width -= (int) (p - buf); width > 0; --width)
16987 *p++ = ' ';
16988 *p-- = '\0';
16989 while (p > buf)
16990 {
16991 d = *buf;
16992 *buf++ = *p;
16993 *p-- = d;
16994 }
16995 }
16996
16997 /* Write a null-terminated, right justified decimal and "human
16998 readable" representation of the nonnegative integer D to BUF using
16999 a minimal field width WIDTH. D should be smaller than 999.5e24. */
17000
17001 static const char power_letter[] =
17002 {
17003 0, /* not used */
17004 'k', /* kilo */
17005 'M', /* mega */
17006 'G', /* giga */
17007 'T', /* tera */
17008 'P', /* peta */
17009 'E', /* exa */
17010 'Z', /* zetta */
17011 'Y' /* yotta */
17012 };
17013
17014 static void
17015 pint2hrstr (buf, width, d)
17016 char *buf;
17017 int width;
17018 int d;
17019 {
17020 /* We aim to represent the nonnegative integer D as
17021 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
17022 int quotient = d;
17023 int remainder = 0;
17024 /* -1 means: do not use TENTHS. */
17025 int tenths = -1;
17026 int exponent = 0;
17027
17028 /* Length of QUOTIENT.TENTHS as a string. */
17029 int length;
17030
17031 char * psuffix;
17032 char * p;
17033
17034 if (1000 <= quotient)
17035 {
17036 /* Scale to the appropriate EXPONENT. */
17037 do
17038 {
17039 remainder = quotient % 1000;
17040 quotient /= 1000;
17041 exponent++;
17042 }
17043 while (1000 <= quotient);
17044
17045 /* Round to nearest and decide whether to use TENTHS or not. */
17046 if (quotient <= 9)
17047 {
17048 tenths = remainder / 100;
17049 if (50 <= remainder % 100)
17050 {
17051 if (tenths < 9)
17052 tenths++;
17053 else
17054 {
17055 quotient++;
17056 if (quotient == 10)
17057 tenths = -1;
17058 else
17059 tenths = 0;
17060 }
17061 }
17062 }
17063 else
17064 if (500 <= remainder)
17065 {
17066 if (quotient < 999)
17067 quotient++;
17068 else
17069 {
17070 quotient = 1;
17071 exponent++;
17072 tenths = 0;
17073 }
17074 }
17075 }
17076
17077 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
17078 if (tenths == -1 && quotient <= 99)
17079 if (quotient <= 9)
17080 length = 1;
17081 else
17082 length = 2;
17083 else
17084 length = 3;
17085 p = psuffix = buf + max (width, length);
17086
17087 /* Print EXPONENT. */
17088 if (exponent)
17089 *psuffix++ = power_letter[exponent];
17090 *psuffix = '\0';
17091
17092 /* Print TENTHS. */
17093 if (tenths >= 0)
17094 {
17095 *--p = '0' + tenths;
17096 *--p = '.';
17097 }
17098
17099 /* Print QUOTIENT. */
17100 do
17101 {
17102 int digit = quotient % 10;
17103 *--p = '0' + digit;
17104 }
17105 while ((quotient /= 10) != 0);
17106
17107 /* Print leading spaces. */
17108 while (buf < p)
17109 *--p = ' ';
17110 }
17111
17112 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
17113 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
17114 type of CODING_SYSTEM. Return updated pointer into BUF. */
17115
17116 static unsigned char invalid_eol_type[] = "(*invalid*)";
17117
17118 static char *
17119 decode_mode_spec_coding (coding_system, buf, eol_flag)
17120 Lisp_Object coding_system;
17121 register char *buf;
17122 int eol_flag;
17123 {
17124 Lisp_Object val;
17125 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
17126 const unsigned char *eol_str;
17127 int eol_str_len;
17128 /* The EOL conversion we are using. */
17129 Lisp_Object eoltype;
17130
17131 val = Fget (coding_system, Qcoding_system);
17132 eoltype = Qnil;
17133
17134 if (!VECTORP (val)) /* Not yet decided. */
17135 {
17136 if (multibyte)
17137 *buf++ = '-';
17138 if (eol_flag)
17139 eoltype = eol_mnemonic_undecided;
17140 /* Don't mention EOL conversion if it isn't decided. */
17141 }
17142 else
17143 {
17144 Lisp_Object eolvalue;
17145
17146 eolvalue = Fget (coding_system, Qeol_type);
17147
17148 if (multibyte)
17149 *buf++ = XFASTINT (AREF (val, 1));
17150
17151 if (eol_flag)
17152 {
17153 /* The EOL conversion that is normal on this system. */
17154
17155 if (NILP (eolvalue)) /* Not yet decided. */
17156 eoltype = eol_mnemonic_undecided;
17157 else if (VECTORP (eolvalue)) /* Not yet decided. */
17158 eoltype = eol_mnemonic_undecided;
17159 else /* INTEGERP (eolvalue) -- 0:LF, 1:CRLF, 2:CR */
17160 eoltype = (XFASTINT (eolvalue) == 0
17161 ? eol_mnemonic_unix
17162 : (XFASTINT (eolvalue) == 1
17163 ? eol_mnemonic_dos : eol_mnemonic_mac));
17164 }
17165 }
17166
17167 if (eol_flag)
17168 {
17169 /* Mention the EOL conversion if it is not the usual one. */
17170 if (STRINGP (eoltype))
17171 {
17172 eol_str = SDATA (eoltype);
17173 eol_str_len = SBYTES (eoltype);
17174 }
17175 else if (INTEGERP (eoltype)
17176 && CHAR_VALID_P (XINT (eoltype), 0))
17177 {
17178 unsigned char *tmp = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
17179 eol_str_len = CHAR_STRING (XINT (eoltype), tmp);
17180 eol_str = tmp;
17181 }
17182 else
17183 {
17184 eol_str = invalid_eol_type;
17185 eol_str_len = sizeof (invalid_eol_type) - 1;
17186 }
17187 bcopy (eol_str, buf, eol_str_len);
17188 buf += eol_str_len;
17189 }
17190
17191 return buf;
17192 }
17193
17194 /* Return a string for the output of a mode line %-spec for window W,
17195 generated by character C. PRECISION >= 0 means don't return a
17196 string longer than that value. FIELD_WIDTH > 0 means pad the
17197 string returned with spaces to that value. Return 1 in *MULTIBYTE
17198 if the result is multibyte text.
17199
17200 Note we operate on the current buffer for most purposes,
17201 the exception being w->base_line_pos. */
17202
17203 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
17204
17205 static char *
17206 decode_mode_spec (w, c, field_width, precision, multibyte)
17207 struct window *w;
17208 register int c;
17209 int field_width, precision;
17210 int *multibyte;
17211 {
17212 Lisp_Object obj;
17213 struct frame *f = XFRAME (WINDOW_FRAME (w));
17214 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
17215 struct buffer *b = current_buffer;
17216
17217 obj = Qnil;
17218 *multibyte = 0;
17219
17220 switch (c)
17221 {
17222 case '*':
17223 if (!NILP (b->read_only))
17224 return "%";
17225 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
17226 return "*";
17227 return "-";
17228
17229 case '+':
17230 /* This differs from %* only for a modified read-only buffer. */
17231 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
17232 return "*";
17233 if (!NILP (b->read_only))
17234 return "%";
17235 return "-";
17236
17237 case '&':
17238 /* This differs from %* in ignoring read-only-ness. */
17239 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
17240 return "*";
17241 return "-";
17242
17243 case '%':
17244 return "%";
17245
17246 case '[':
17247 {
17248 int i;
17249 char *p;
17250
17251 if (command_loop_level > 5)
17252 return "[[[... ";
17253 p = decode_mode_spec_buf;
17254 for (i = 0; i < command_loop_level; i++)
17255 *p++ = '[';
17256 *p = 0;
17257 return decode_mode_spec_buf;
17258 }
17259
17260 case ']':
17261 {
17262 int i;
17263 char *p;
17264
17265 if (command_loop_level > 5)
17266 return " ...]]]";
17267 p = decode_mode_spec_buf;
17268 for (i = 0; i < command_loop_level; i++)
17269 *p++ = ']';
17270 *p = 0;
17271 return decode_mode_spec_buf;
17272 }
17273
17274 case '-':
17275 {
17276 register int i;
17277
17278 /* Let lots_of_dashes be a string of infinite length. */
17279 if (mode_line_target == MODE_LINE_NOPROP ||
17280 mode_line_target == MODE_LINE_STRING)
17281 return "--";
17282 if (field_width <= 0
17283 || field_width > sizeof (lots_of_dashes))
17284 {
17285 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
17286 decode_mode_spec_buf[i] = '-';
17287 decode_mode_spec_buf[i] = '\0';
17288 return decode_mode_spec_buf;
17289 }
17290 else
17291 return lots_of_dashes;
17292 }
17293
17294 case 'b':
17295 obj = b->name;
17296 break;
17297
17298 case 'c':
17299 {
17300 int col = (int) current_column (); /* iftc */
17301 w->column_number_displayed = make_number (col);
17302 pint2str (decode_mode_spec_buf, field_width, col);
17303 return decode_mode_spec_buf;
17304 }
17305
17306 case 'e':
17307 #ifndef SYSTEM_MALLOC
17308 {
17309 if (NILP (Vmemory_full))
17310 return "";
17311 else
17312 return "!MEM FULL! ";
17313 }
17314 #else
17315 return "";
17316 #endif
17317
17318 case 'F':
17319 /* %F displays the frame name. */
17320 if (!NILP (f->title))
17321 return (char *) SDATA (f->title);
17322 if (f->explicit_name || ! FRAME_WINDOW_P (f))
17323 return (char *) SDATA (f->name);
17324 return "Emacs";
17325
17326 case 'f':
17327 obj = b->filename;
17328 break;
17329
17330 case 'i':
17331 {
17332 int size = ZV - BEGV;
17333 pint2str (decode_mode_spec_buf, field_width, size);
17334 return decode_mode_spec_buf;
17335 }
17336
17337 case 'I':
17338 {
17339 int size = ZV - BEGV;
17340 pint2hrstr (decode_mode_spec_buf, field_width, size);
17341 return decode_mode_spec_buf;
17342 }
17343
17344 case 'l':
17345 {
17346 int startpos = XMARKER (w->start)->charpos;
17347 int startpos_byte = marker_byte_position (w->start);
17348 int line, linepos, linepos_byte, topline;
17349 int nlines, junk;
17350 int height = WINDOW_TOTAL_LINES (w);
17351
17352 /* If we decided that this buffer isn't suitable for line numbers,
17353 don't forget that too fast. */
17354 if (EQ (w->base_line_pos, w->buffer))
17355 goto no_value;
17356 /* But do forget it, if the window shows a different buffer now. */
17357 else if (BUFFERP (w->base_line_pos))
17358 w->base_line_pos = Qnil;
17359
17360 /* If the buffer is very big, don't waste time. */
17361 if (INTEGERP (Vline_number_display_limit)
17362 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
17363 {
17364 w->base_line_pos = Qnil;
17365 w->base_line_number = Qnil;
17366 goto no_value;
17367 }
17368
17369 if (!NILP (w->base_line_number)
17370 && !NILP (w->base_line_pos)
17371 && XFASTINT (w->base_line_pos) <= startpos)
17372 {
17373 line = XFASTINT (w->base_line_number);
17374 linepos = XFASTINT (w->base_line_pos);
17375 linepos_byte = buf_charpos_to_bytepos (b, linepos);
17376 }
17377 else
17378 {
17379 line = 1;
17380 linepos = BUF_BEGV (b);
17381 linepos_byte = BUF_BEGV_BYTE (b);
17382 }
17383
17384 /* Count lines from base line to window start position. */
17385 nlines = display_count_lines (linepos, linepos_byte,
17386 startpos_byte,
17387 startpos, &junk);
17388
17389 topline = nlines + line;
17390
17391 /* Determine a new base line, if the old one is too close
17392 or too far away, or if we did not have one.
17393 "Too close" means it's plausible a scroll-down would
17394 go back past it. */
17395 if (startpos == BUF_BEGV (b))
17396 {
17397 w->base_line_number = make_number (topline);
17398 w->base_line_pos = make_number (BUF_BEGV (b));
17399 }
17400 else if (nlines < height + 25 || nlines > height * 3 + 50
17401 || linepos == BUF_BEGV (b))
17402 {
17403 int limit = BUF_BEGV (b);
17404 int limit_byte = BUF_BEGV_BYTE (b);
17405 int position;
17406 int distance = (height * 2 + 30) * line_number_display_limit_width;
17407
17408 if (startpos - distance > limit)
17409 {
17410 limit = startpos - distance;
17411 limit_byte = CHAR_TO_BYTE (limit);
17412 }
17413
17414 nlines = display_count_lines (startpos, startpos_byte,
17415 limit_byte,
17416 - (height * 2 + 30),
17417 &position);
17418 /* If we couldn't find the lines we wanted within
17419 line_number_display_limit_width chars per line,
17420 give up on line numbers for this window. */
17421 if (position == limit_byte && limit == startpos - distance)
17422 {
17423 w->base_line_pos = w->buffer;
17424 w->base_line_number = Qnil;
17425 goto no_value;
17426 }
17427
17428 w->base_line_number = make_number (topline - nlines);
17429 w->base_line_pos = make_number (BYTE_TO_CHAR (position));
17430 }
17431
17432 /* Now count lines from the start pos to point. */
17433 nlines = display_count_lines (startpos, startpos_byte,
17434 PT_BYTE, PT, &junk);
17435
17436 /* Record that we did display the line number. */
17437 line_number_displayed = 1;
17438
17439 /* Make the string to show. */
17440 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
17441 return decode_mode_spec_buf;
17442 no_value:
17443 {
17444 char* p = decode_mode_spec_buf;
17445 int pad = field_width - 2;
17446 while (pad-- > 0)
17447 *p++ = ' ';
17448 *p++ = '?';
17449 *p++ = '?';
17450 *p = '\0';
17451 return decode_mode_spec_buf;
17452 }
17453 }
17454 break;
17455
17456 case 'm':
17457 obj = b->mode_name;
17458 break;
17459
17460 case 'n':
17461 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
17462 return " Narrow";
17463 break;
17464
17465 case 'p':
17466 {
17467 int pos = marker_position (w->start);
17468 int total = BUF_ZV (b) - BUF_BEGV (b);
17469
17470 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
17471 {
17472 if (pos <= BUF_BEGV (b))
17473 return "All";
17474 else
17475 return "Bottom";
17476 }
17477 else if (pos <= BUF_BEGV (b))
17478 return "Top";
17479 else
17480 {
17481 if (total > 1000000)
17482 /* Do it differently for a large value, to avoid overflow. */
17483 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
17484 else
17485 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
17486 /* We can't normally display a 3-digit number,
17487 so get us a 2-digit number that is close. */
17488 if (total == 100)
17489 total = 99;
17490 sprintf (decode_mode_spec_buf, "%2d%%", total);
17491 return decode_mode_spec_buf;
17492 }
17493 }
17494
17495 /* Display percentage of size above the bottom of the screen. */
17496 case 'P':
17497 {
17498 int toppos = marker_position (w->start);
17499 int botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
17500 int total = BUF_ZV (b) - BUF_BEGV (b);
17501
17502 if (botpos >= BUF_ZV (b))
17503 {
17504 if (toppos <= BUF_BEGV (b))
17505 return "All";
17506 else
17507 return "Bottom";
17508 }
17509 else
17510 {
17511 if (total > 1000000)
17512 /* Do it differently for a large value, to avoid overflow. */
17513 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
17514 else
17515 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
17516 /* We can't normally display a 3-digit number,
17517 so get us a 2-digit number that is close. */
17518 if (total == 100)
17519 total = 99;
17520 if (toppos <= BUF_BEGV (b))
17521 sprintf (decode_mode_spec_buf, "Top%2d%%", total);
17522 else
17523 sprintf (decode_mode_spec_buf, "%2d%%", total);
17524 return decode_mode_spec_buf;
17525 }
17526 }
17527
17528 case 's':
17529 /* status of process */
17530 obj = Fget_buffer_process (Fcurrent_buffer ());
17531 if (NILP (obj))
17532 return "no process";
17533 #ifdef subprocesses
17534 obj = Fsymbol_name (Fprocess_status (obj));
17535 #endif
17536 break;
17537
17538 case 't': /* indicate TEXT or BINARY */
17539 #ifdef MODE_LINE_BINARY_TEXT
17540 return MODE_LINE_BINARY_TEXT (b);
17541 #else
17542 return "T";
17543 #endif
17544
17545 case 'z':
17546 /* coding-system (not including end-of-line format) */
17547 case 'Z':
17548 /* coding-system (including end-of-line type) */
17549 {
17550 int eol_flag = (c == 'Z');
17551 char *p = decode_mode_spec_buf;
17552
17553 if (! FRAME_WINDOW_P (f))
17554 {
17555 /* No need to mention EOL here--the terminal never needs
17556 to do EOL conversion. */
17557 p = decode_mode_spec_coding (keyboard_coding.symbol, p, 0);
17558 p = decode_mode_spec_coding (terminal_coding.symbol, p, 0);
17559 }
17560 p = decode_mode_spec_coding (b->buffer_file_coding_system,
17561 p, eol_flag);
17562
17563 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
17564 #ifdef subprocesses
17565 obj = Fget_buffer_process (Fcurrent_buffer ());
17566 if (PROCESSP (obj))
17567 {
17568 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
17569 p, eol_flag);
17570 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
17571 p, eol_flag);
17572 }
17573 #endif /* subprocesses */
17574 #endif /* 0 */
17575 *p = 0;
17576 return decode_mode_spec_buf;
17577 }
17578 }
17579
17580 if (STRINGP (obj))
17581 {
17582 *multibyte = STRING_MULTIBYTE (obj);
17583 return (char *) SDATA (obj);
17584 }
17585 else
17586 return "";
17587 }
17588
17589
17590 /* Count up to COUNT lines starting from START / START_BYTE.
17591 But don't go beyond LIMIT_BYTE.
17592 Return the number of lines thus found (always nonnegative).
17593
17594 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
17595
17596 static int
17597 display_count_lines (start, start_byte, limit_byte, count, byte_pos_ptr)
17598 int start, start_byte, limit_byte, count;
17599 int *byte_pos_ptr;
17600 {
17601 register unsigned char *cursor;
17602 unsigned char *base;
17603
17604 register int ceiling;
17605 register unsigned char *ceiling_addr;
17606 int orig_count = count;
17607
17608 /* If we are not in selective display mode,
17609 check only for newlines. */
17610 int selective_display = (!NILP (current_buffer->selective_display)
17611 && !INTEGERP (current_buffer->selective_display));
17612
17613 if (count > 0)
17614 {
17615 while (start_byte < limit_byte)
17616 {
17617 ceiling = BUFFER_CEILING_OF (start_byte);
17618 ceiling = min (limit_byte - 1, ceiling);
17619 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
17620 base = (cursor = BYTE_POS_ADDR (start_byte));
17621 while (1)
17622 {
17623 if (selective_display)
17624 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
17625 ;
17626 else
17627 while (*cursor != '\n' && ++cursor != ceiling_addr)
17628 ;
17629
17630 if (cursor != ceiling_addr)
17631 {
17632 if (--count == 0)
17633 {
17634 start_byte += cursor - base + 1;
17635 *byte_pos_ptr = start_byte;
17636 return orig_count;
17637 }
17638 else
17639 if (++cursor == ceiling_addr)
17640 break;
17641 }
17642 else
17643 break;
17644 }
17645 start_byte += cursor - base;
17646 }
17647 }
17648 else
17649 {
17650 while (start_byte > limit_byte)
17651 {
17652 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
17653 ceiling = max (limit_byte, ceiling);
17654 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
17655 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
17656 while (1)
17657 {
17658 if (selective_display)
17659 while (--cursor != ceiling_addr
17660 && *cursor != '\n' && *cursor != 015)
17661 ;
17662 else
17663 while (--cursor != ceiling_addr && *cursor != '\n')
17664 ;
17665
17666 if (cursor != ceiling_addr)
17667 {
17668 if (++count == 0)
17669 {
17670 start_byte += cursor - base + 1;
17671 *byte_pos_ptr = start_byte;
17672 /* When scanning backwards, we should
17673 not count the newline posterior to which we stop. */
17674 return - orig_count - 1;
17675 }
17676 }
17677 else
17678 break;
17679 }
17680 /* Here we add 1 to compensate for the last decrement
17681 of CURSOR, which took it past the valid range. */
17682 start_byte += cursor - base + 1;
17683 }
17684 }
17685
17686 *byte_pos_ptr = limit_byte;
17687
17688 if (count < 0)
17689 return - orig_count + count;
17690 return orig_count - count;
17691
17692 }
17693
17694
17695 \f
17696 /***********************************************************************
17697 Displaying strings
17698 ***********************************************************************/
17699
17700 /* Display a NUL-terminated string, starting with index START.
17701
17702 If STRING is non-null, display that C string. Otherwise, the Lisp
17703 string LISP_STRING is displayed.
17704
17705 If FACE_STRING is not nil, FACE_STRING_POS is a position in
17706 FACE_STRING. Display STRING or LISP_STRING with the face at
17707 FACE_STRING_POS in FACE_STRING:
17708
17709 Display the string in the environment given by IT, but use the
17710 standard display table, temporarily.
17711
17712 FIELD_WIDTH is the minimum number of output glyphs to produce.
17713 If STRING has fewer characters than FIELD_WIDTH, pad to the right
17714 with spaces. If STRING has more characters, more than FIELD_WIDTH
17715 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
17716
17717 PRECISION is the maximum number of characters to output from
17718 STRING. PRECISION < 0 means don't truncate the string.
17719
17720 This is roughly equivalent to printf format specifiers:
17721
17722 FIELD_WIDTH PRECISION PRINTF
17723 ----------------------------------------
17724 -1 -1 %s
17725 -1 10 %.10s
17726 10 -1 %10s
17727 20 10 %20.10s
17728
17729 MULTIBYTE zero means do not display multibyte chars, > 0 means do
17730 display them, and < 0 means obey the current buffer's value of
17731 enable_multibyte_characters.
17732
17733 Value is the number of columns displayed. */
17734
17735 static int
17736 display_string (string, lisp_string, face_string, face_string_pos,
17737 start, it, field_width, precision, max_x, multibyte)
17738 unsigned char *string;
17739 Lisp_Object lisp_string;
17740 Lisp_Object face_string;
17741 int face_string_pos;
17742 int start;
17743 struct it *it;
17744 int field_width, precision, max_x;
17745 int multibyte;
17746 {
17747 int hpos_at_start = it->hpos;
17748 int saved_face_id = it->face_id;
17749 struct glyph_row *row = it->glyph_row;
17750
17751 /* Initialize the iterator IT for iteration over STRING beginning
17752 with index START. */
17753 reseat_to_string (it, string, lisp_string, start,
17754 precision, field_width, multibyte);
17755
17756 /* If displaying STRING, set up the face of the iterator
17757 from LISP_STRING, if that's given. */
17758 if (STRINGP (face_string))
17759 {
17760 int endptr;
17761 struct face *face;
17762
17763 it->face_id
17764 = face_at_string_position (it->w, face_string, face_string_pos,
17765 0, it->region_beg_charpos,
17766 it->region_end_charpos,
17767 &endptr, it->base_face_id, 0);
17768 face = FACE_FROM_ID (it->f, it->face_id);
17769 it->face_box_p = face->box != FACE_NO_BOX;
17770 }
17771
17772 /* Set max_x to the maximum allowed X position. Don't let it go
17773 beyond the right edge of the window. */
17774 if (max_x <= 0)
17775 max_x = it->last_visible_x;
17776 else
17777 max_x = min (max_x, it->last_visible_x);
17778
17779 /* Skip over display elements that are not visible. because IT->w is
17780 hscrolled. */
17781 if (it->current_x < it->first_visible_x)
17782 move_it_in_display_line_to (it, 100000, it->first_visible_x,
17783 MOVE_TO_POS | MOVE_TO_X);
17784
17785 row->ascent = it->max_ascent;
17786 row->height = it->max_ascent + it->max_descent;
17787 row->phys_ascent = it->max_phys_ascent;
17788 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
17789 row->extra_line_spacing = it->max_extra_line_spacing;
17790
17791 /* This condition is for the case that we are called with current_x
17792 past last_visible_x. */
17793 while (it->current_x < max_x)
17794 {
17795 int x_before, x, n_glyphs_before, i, nglyphs;
17796
17797 /* Get the next display element. */
17798 if (!get_next_display_element (it))
17799 break;
17800
17801 /* Produce glyphs. */
17802 x_before = it->current_x;
17803 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
17804 PRODUCE_GLYPHS (it);
17805
17806 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
17807 i = 0;
17808 x = x_before;
17809 while (i < nglyphs)
17810 {
17811 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
17812
17813 if (!it->truncate_lines_p
17814 && x + glyph->pixel_width > max_x)
17815 {
17816 /* End of continued line or max_x reached. */
17817 if (CHAR_GLYPH_PADDING_P (*glyph))
17818 {
17819 /* A wide character is unbreakable. */
17820 it->glyph_row->used[TEXT_AREA] = n_glyphs_before;
17821 it->current_x = x_before;
17822 }
17823 else
17824 {
17825 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
17826 it->current_x = x;
17827 }
17828 break;
17829 }
17830 else if (x + glyph->pixel_width > it->first_visible_x)
17831 {
17832 /* Glyph is at least partially visible. */
17833 ++it->hpos;
17834 if (x < it->first_visible_x)
17835 it->glyph_row->x = x - it->first_visible_x;
17836 }
17837 else
17838 {
17839 /* Glyph is off the left margin of the display area.
17840 Should not happen. */
17841 abort ();
17842 }
17843
17844 row->ascent = max (row->ascent, it->max_ascent);
17845 row->height = max (row->height, it->max_ascent + it->max_descent);
17846 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
17847 row->phys_height = max (row->phys_height,
17848 it->max_phys_ascent + it->max_phys_descent);
17849 row->extra_line_spacing = max (row->extra_line_spacing,
17850 it->max_extra_line_spacing);
17851 x += glyph->pixel_width;
17852 ++i;
17853 }
17854
17855 /* Stop if max_x reached. */
17856 if (i < nglyphs)
17857 break;
17858
17859 /* Stop at line ends. */
17860 if (ITERATOR_AT_END_OF_LINE_P (it))
17861 {
17862 it->continuation_lines_width = 0;
17863 break;
17864 }
17865
17866 set_iterator_to_next (it, 1);
17867
17868 /* Stop if truncating at the right edge. */
17869 if (it->truncate_lines_p
17870 && it->current_x >= it->last_visible_x)
17871 {
17872 /* Add truncation mark, but don't do it if the line is
17873 truncated at a padding space. */
17874 if (IT_CHARPOS (*it) < it->string_nchars)
17875 {
17876 if (!FRAME_WINDOW_P (it->f))
17877 {
17878 int i, n;
17879
17880 if (it->current_x > it->last_visible_x)
17881 {
17882 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
17883 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
17884 break;
17885 for (n = row->used[TEXT_AREA]; i < n; ++i)
17886 {
17887 row->used[TEXT_AREA] = i;
17888 produce_special_glyphs (it, IT_TRUNCATION);
17889 }
17890 }
17891 produce_special_glyphs (it, IT_TRUNCATION);
17892 }
17893 it->glyph_row->truncated_on_right_p = 1;
17894 }
17895 break;
17896 }
17897 }
17898
17899 /* Maybe insert a truncation at the left. */
17900 if (it->first_visible_x
17901 && IT_CHARPOS (*it) > 0)
17902 {
17903 if (!FRAME_WINDOW_P (it->f))
17904 insert_left_trunc_glyphs (it);
17905 it->glyph_row->truncated_on_left_p = 1;
17906 }
17907
17908 it->face_id = saved_face_id;
17909
17910 /* Value is number of columns displayed. */
17911 return it->hpos - hpos_at_start;
17912 }
17913
17914
17915 \f
17916 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
17917 appears as an element of LIST or as the car of an element of LIST.
17918 If PROPVAL is a list, compare each element against LIST in that
17919 way, and return 1/2 if any element of PROPVAL is found in LIST.
17920 Otherwise return 0. This function cannot quit.
17921 The return value is 2 if the text is invisible but with an ellipsis
17922 and 1 if it's invisible and without an ellipsis. */
17923
17924 int
17925 invisible_p (propval, list)
17926 register Lisp_Object propval;
17927 Lisp_Object list;
17928 {
17929 register Lisp_Object tail, proptail;
17930
17931 for (tail = list; CONSP (tail); tail = XCDR (tail))
17932 {
17933 register Lisp_Object tem;
17934 tem = XCAR (tail);
17935 if (EQ (propval, tem))
17936 return 1;
17937 if (CONSP (tem) && EQ (propval, XCAR (tem)))
17938 return NILP (XCDR (tem)) ? 1 : 2;
17939 }
17940
17941 if (CONSP (propval))
17942 {
17943 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
17944 {
17945 Lisp_Object propelt;
17946 propelt = XCAR (proptail);
17947 for (tail = list; CONSP (tail); tail = XCDR (tail))
17948 {
17949 register Lisp_Object tem;
17950 tem = XCAR (tail);
17951 if (EQ (propelt, tem))
17952 return 1;
17953 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
17954 return NILP (XCDR (tem)) ? 1 : 2;
17955 }
17956 }
17957 }
17958
17959 return 0;
17960 }
17961
17962 /* Calculate a width or height in pixels from a specification using
17963 the following elements:
17964
17965 SPEC ::=
17966 NUM - a (fractional) multiple of the default font width/height
17967 (NUM) - specifies exactly NUM pixels
17968 UNIT - a fixed number of pixels, see below.
17969 ELEMENT - size of a display element in pixels, see below.
17970 (NUM . SPEC) - equals NUM * SPEC
17971 (+ SPEC SPEC ...) - add pixel values
17972 (- SPEC SPEC ...) - subtract pixel values
17973 (- SPEC) - negate pixel value
17974
17975 NUM ::=
17976 INT or FLOAT - a number constant
17977 SYMBOL - use symbol's (buffer local) variable binding.
17978
17979 UNIT ::=
17980 in - pixels per inch *)
17981 mm - pixels per 1/1000 meter *)
17982 cm - pixels per 1/100 meter *)
17983 width - width of current font in pixels.
17984 height - height of current font in pixels.
17985
17986 *) using the ratio(s) defined in display-pixels-per-inch.
17987
17988 ELEMENT ::=
17989
17990 left-fringe - left fringe width in pixels
17991 right-fringe - right fringe width in pixels
17992
17993 left-margin - left margin width in pixels
17994 right-margin - right margin width in pixels
17995
17996 scroll-bar - scroll-bar area width in pixels
17997
17998 Examples:
17999
18000 Pixels corresponding to 5 inches:
18001 (5 . in)
18002
18003 Total width of non-text areas on left side of window (if scroll-bar is on left):
18004 '(space :width (+ left-fringe left-margin scroll-bar))
18005
18006 Align to first text column (in header line):
18007 '(space :align-to 0)
18008
18009 Align to middle of text area minus half the width of variable `my-image'
18010 containing a loaded image:
18011 '(space :align-to (0.5 . (- text my-image)))
18012
18013 Width of left margin minus width of 1 character in the default font:
18014 '(space :width (- left-margin 1))
18015
18016 Width of left margin minus width of 2 characters in the current font:
18017 '(space :width (- left-margin (2 . width)))
18018
18019 Center 1 character over left-margin (in header line):
18020 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
18021
18022 Different ways to express width of left fringe plus left margin minus one pixel:
18023 '(space :width (- (+ left-fringe left-margin) (1)))
18024 '(space :width (+ left-fringe left-margin (- (1))))
18025 '(space :width (+ left-fringe left-margin (-1)))
18026
18027 */
18028
18029 #define NUMVAL(X) \
18030 ((INTEGERP (X) || FLOATP (X)) \
18031 ? XFLOATINT (X) \
18032 : - 1)
18033
18034 int
18035 calc_pixel_width_or_height (res, it, prop, font, width_p, align_to)
18036 double *res;
18037 struct it *it;
18038 Lisp_Object prop;
18039 void *font;
18040 int width_p, *align_to;
18041 {
18042 double pixels;
18043
18044 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
18045 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
18046
18047 if (NILP (prop))
18048 return OK_PIXELS (0);
18049
18050 if (SYMBOLP (prop))
18051 {
18052 if (SCHARS (SYMBOL_NAME (prop)) == 2)
18053 {
18054 char *unit = SDATA (SYMBOL_NAME (prop));
18055
18056 if (unit[0] == 'i' && unit[1] == 'n')
18057 pixels = 1.0;
18058 else if (unit[0] == 'm' && unit[1] == 'm')
18059 pixels = 25.4;
18060 else if (unit[0] == 'c' && unit[1] == 'm')
18061 pixels = 2.54;
18062 else
18063 pixels = 0;
18064 if (pixels > 0)
18065 {
18066 double ppi;
18067 #ifdef HAVE_WINDOW_SYSTEM
18068 if (FRAME_WINDOW_P (it->f)
18069 && (ppi = (width_p
18070 ? FRAME_X_DISPLAY_INFO (it->f)->resx
18071 : FRAME_X_DISPLAY_INFO (it->f)->resy),
18072 ppi > 0))
18073 return OK_PIXELS (ppi / pixels);
18074 #endif
18075
18076 if ((ppi = NUMVAL (Vdisplay_pixels_per_inch), ppi > 0)
18077 || (CONSP (Vdisplay_pixels_per_inch)
18078 && (ppi = (width_p
18079 ? NUMVAL (XCAR (Vdisplay_pixels_per_inch))
18080 : NUMVAL (XCDR (Vdisplay_pixels_per_inch))),
18081 ppi > 0)))
18082 return OK_PIXELS (ppi / pixels);
18083
18084 return 0;
18085 }
18086 }
18087
18088 #ifdef HAVE_WINDOW_SYSTEM
18089 if (EQ (prop, Qheight))
18090 return OK_PIXELS (font ? FONT_HEIGHT ((XFontStruct *)font) : FRAME_LINE_HEIGHT (it->f));
18091 if (EQ (prop, Qwidth))
18092 return OK_PIXELS (font ? FONT_WIDTH ((XFontStruct *)font) : FRAME_COLUMN_WIDTH (it->f));
18093 #else
18094 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
18095 return OK_PIXELS (1);
18096 #endif
18097
18098 if (EQ (prop, Qtext))
18099 return OK_PIXELS (width_p
18100 ? window_box_width (it->w, TEXT_AREA)
18101 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
18102
18103 if (align_to && *align_to < 0)
18104 {
18105 *res = 0;
18106 if (EQ (prop, Qleft))
18107 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
18108 if (EQ (prop, Qright))
18109 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
18110 if (EQ (prop, Qcenter))
18111 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
18112 + window_box_width (it->w, TEXT_AREA) / 2);
18113 if (EQ (prop, Qleft_fringe))
18114 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
18115 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
18116 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
18117 if (EQ (prop, Qright_fringe))
18118 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
18119 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
18120 : window_box_right_offset (it->w, TEXT_AREA));
18121 if (EQ (prop, Qleft_margin))
18122 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
18123 if (EQ (prop, Qright_margin))
18124 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
18125 if (EQ (prop, Qscroll_bar))
18126 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
18127 ? 0
18128 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
18129 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
18130 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
18131 : 0)));
18132 }
18133 else
18134 {
18135 if (EQ (prop, Qleft_fringe))
18136 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
18137 if (EQ (prop, Qright_fringe))
18138 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
18139 if (EQ (prop, Qleft_margin))
18140 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
18141 if (EQ (prop, Qright_margin))
18142 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
18143 if (EQ (prop, Qscroll_bar))
18144 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
18145 }
18146
18147 prop = Fbuffer_local_value (prop, it->w->buffer);
18148 }
18149
18150 if (INTEGERP (prop) || FLOATP (prop))
18151 {
18152 int base_unit = (width_p
18153 ? FRAME_COLUMN_WIDTH (it->f)
18154 : FRAME_LINE_HEIGHT (it->f));
18155 return OK_PIXELS (XFLOATINT (prop) * base_unit);
18156 }
18157
18158 if (CONSP (prop))
18159 {
18160 Lisp_Object car = XCAR (prop);
18161 Lisp_Object cdr = XCDR (prop);
18162
18163 if (SYMBOLP (car))
18164 {
18165 #ifdef HAVE_WINDOW_SYSTEM
18166 if (valid_image_p (prop))
18167 {
18168 int id = lookup_image (it->f, prop);
18169 struct image *img = IMAGE_FROM_ID (it->f, id);
18170
18171 return OK_PIXELS (width_p ? img->width : img->height);
18172 }
18173 #endif
18174 if (EQ (car, Qplus) || EQ (car, Qminus))
18175 {
18176 int first = 1;
18177 double px;
18178
18179 pixels = 0;
18180 while (CONSP (cdr))
18181 {
18182 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
18183 font, width_p, align_to))
18184 return 0;
18185 if (first)
18186 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
18187 else
18188 pixels += px;
18189 cdr = XCDR (cdr);
18190 }
18191 if (EQ (car, Qminus))
18192 pixels = -pixels;
18193 return OK_PIXELS (pixels);
18194 }
18195
18196 car = Fbuffer_local_value (car, it->w->buffer);
18197 }
18198
18199 if (INTEGERP (car) || FLOATP (car))
18200 {
18201 double fact;
18202 pixels = XFLOATINT (car);
18203 if (NILP (cdr))
18204 return OK_PIXELS (pixels);
18205 if (calc_pixel_width_or_height (&fact, it, cdr,
18206 font, width_p, align_to))
18207 return OK_PIXELS (pixels * fact);
18208 return 0;
18209 }
18210
18211 return 0;
18212 }
18213
18214 return 0;
18215 }
18216
18217 \f
18218 /***********************************************************************
18219 Glyph Display
18220 ***********************************************************************/
18221
18222 #ifdef HAVE_WINDOW_SYSTEM
18223
18224 #if GLYPH_DEBUG
18225
18226 void
18227 dump_glyph_string (s)
18228 struct glyph_string *s;
18229 {
18230 fprintf (stderr, "glyph string\n");
18231 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
18232 s->x, s->y, s->width, s->height);
18233 fprintf (stderr, " ybase = %d\n", s->ybase);
18234 fprintf (stderr, " hl = %d\n", s->hl);
18235 fprintf (stderr, " left overhang = %d, right = %d\n",
18236 s->left_overhang, s->right_overhang);
18237 fprintf (stderr, " nchars = %d\n", s->nchars);
18238 fprintf (stderr, " extends to end of line = %d\n",
18239 s->extends_to_end_of_line_p);
18240 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
18241 fprintf (stderr, " bg width = %d\n", s->background_width);
18242 }
18243
18244 #endif /* GLYPH_DEBUG */
18245
18246 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
18247 of XChar2b structures for S; it can't be allocated in
18248 init_glyph_string because it must be allocated via `alloca'. W
18249 is the window on which S is drawn. ROW and AREA are the glyph row
18250 and area within the row from which S is constructed. START is the
18251 index of the first glyph structure covered by S. HL is a
18252 face-override for drawing S. */
18253
18254 #ifdef HAVE_NTGUI
18255 #define OPTIONAL_HDC(hdc) hdc,
18256 #define DECLARE_HDC(hdc) HDC hdc;
18257 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
18258 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
18259 #endif
18260
18261 #ifndef OPTIONAL_HDC
18262 #define OPTIONAL_HDC(hdc)
18263 #define DECLARE_HDC(hdc)
18264 #define ALLOCATE_HDC(hdc, f)
18265 #define RELEASE_HDC(hdc, f)
18266 #endif
18267
18268 static void
18269 init_glyph_string (s, OPTIONAL_HDC (hdc) char2b, w, row, area, start, hl)
18270 struct glyph_string *s;
18271 DECLARE_HDC (hdc)
18272 XChar2b *char2b;
18273 struct window *w;
18274 struct glyph_row *row;
18275 enum glyph_row_area area;
18276 int start;
18277 enum draw_glyphs_face hl;
18278 {
18279 bzero (s, sizeof *s);
18280 s->w = w;
18281 s->f = XFRAME (w->frame);
18282 #ifdef HAVE_NTGUI
18283 s->hdc = hdc;
18284 #endif
18285 s->display = FRAME_X_DISPLAY (s->f);
18286 s->window = FRAME_X_WINDOW (s->f);
18287 s->char2b = char2b;
18288 s->hl = hl;
18289 s->row = row;
18290 s->area = area;
18291 s->first_glyph = row->glyphs[area] + start;
18292 s->height = row->height;
18293 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
18294
18295 /* Display the internal border below the tool-bar window. */
18296 if (WINDOWP (s->f->tool_bar_window)
18297 && s->w == XWINDOW (s->f->tool_bar_window))
18298 s->y -= FRAME_INTERNAL_BORDER_WIDTH (s->f);
18299
18300 s->ybase = s->y + row->ascent;
18301 }
18302
18303
18304 /* Append the list of glyph strings with head H and tail T to the list
18305 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
18306
18307 static INLINE void
18308 append_glyph_string_lists (head, tail, h, t)
18309 struct glyph_string **head, **tail;
18310 struct glyph_string *h, *t;
18311 {
18312 if (h)
18313 {
18314 if (*head)
18315 (*tail)->next = h;
18316 else
18317 *head = h;
18318 h->prev = *tail;
18319 *tail = t;
18320 }
18321 }
18322
18323
18324 /* Prepend the list of glyph strings with head H and tail T to the
18325 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
18326 result. */
18327
18328 static INLINE void
18329 prepend_glyph_string_lists (head, tail, h, t)
18330 struct glyph_string **head, **tail;
18331 struct glyph_string *h, *t;
18332 {
18333 if (h)
18334 {
18335 if (*head)
18336 (*head)->prev = t;
18337 else
18338 *tail = t;
18339 t->next = *head;
18340 *head = h;
18341 }
18342 }
18343
18344
18345 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
18346 Set *HEAD and *TAIL to the resulting list. */
18347
18348 static INLINE void
18349 append_glyph_string (head, tail, s)
18350 struct glyph_string **head, **tail;
18351 struct glyph_string *s;
18352 {
18353 s->next = s->prev = NULL;
18354 append_glyph_string_lists (head, tail, s, s);
18355 }
18356
18357
18358 /* Get face and two-byte form of character glyph GLYPH on frame F.
18359 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
18360 a pointer to a realized face that is ready for display. */
18361
18362 static INLINE struct face *
18363 get_glyph_face_and_encoding (f, glyph, char2b, two_byte_p)
18364 struct frame *f;
18365 struct glyph *glyph;
18366 XChar2b *char2b;
18367 int *two_byte_p;
18368 {
18369 struct face *face;
18370
18371 xassert (glyph->type == CHAR_GLYPH);
18372 face = FACE_FROM_ID (f, glyph->face_id);
18373
18374 if (two_byte_p)
18375 *two_byte_p = 0;
18376
18377 if (!glyph->multibyte_p)
18378 {
18379 /* Unibyte case. We don't have to encode, but we have to make
18380 sure to use a face suitable for unibyte. */
18381 STORE_XCHAR2B (char2b, 0, glyph->u.ch);
18382 }
18383 else if (glyph->u.ch < 128
18384 && glyph->face_id < BASIC_FACE_ID_SENTINEL)
18385 {
18386 /* Case of ASCII in a face known to fit ASCII. */
18387 STORE_XCHAR2B (char2b, 0, glyph->u.ch);
18388 }
18389 else
18390 {
18391 int c1, c2, charset;
18392
18393 /* Split characters into bytes. If c2 is -1 afterwards, C is
18394 really a one-byte character so that byte1 is zero. */
18395 SPLIT_CHAR (glyph->u.ch, charset, c1, c2);
18396 if (c2 > 0)
18397 STORE_XCHAR2B (char2b, c1, c2);
18398 else
18399 STORE_XCHAR2B (char2b, 0, c1);
18400
18401 /* Maybe encode the character in *CHAR2B. */
18402 if (charset != CHARSET_ASCII)
18403 {
18404 struct font_info *font_info
18405 = FONT_INFO_FROM_ID (f, face->font_info_id);
18406 if (font_info)
18407 glyph->font_type
18408 = rif->encode_char (glyph->u.ch, char2b, font_info, two_byte_p);
18409 }
18410 }
18411
18412 /* Make sure X resources of the face are allocated. */
18413 xassert (face != NULL);
18414 PREPARE_FACE_FOR_DISPLAY (f, face);
18415 return face;
18416 }
18417
18418
18419 /* Fill glyph string S with composition components specified by S->cmp.
18420
18421 FACES is an array of faces for all components of this composition.
18422 S->gidx is the index of the first component for S.
18423
18424 OVERLAPS non-zero means S should draw the foreground only, and use
18425 its physical height for clipping. See also draw_glyphs.
18426
18427 Value is the index of a component not in S. */
18428
18429 static int
18430 fill_composite_glyph_string (s, faces, overlaps)
18431 struct glyph_string *s;
18432 struct face **faces;
18433 int overlaps;
18434 {
18435 int i;
18436
18437 xassert (s);
18438
18439 s->for_overlaps = overlaps;
18440
18441 s->face = faces[s->gidx];
18442 s->font = s->face->font;
18443 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
18444
18445 /* For all glyphs of this composition, starting at the offset
18446 S->gidx, until we reach the end of the definition or encounter a
18447 glyph that requires the different face, add it to S. */
18448 ++s->nchars;
18449 for (i = s->gidx + 1; i < s->cmp->glyph_len && faces[i] == s->face; ++i)
18450 ++s->nchars;
18451
18452 /* All glyph strings for the same composition has the same width,
18453 i.e. the width set for the first component of the composition. */
18454
18455 s->width = s->first_glyph->pixel_width;
18456
18457 /* If the specified font could not be loaded, use the frame's
18458 default font, but record the fact that we couldn't load it in
18459 the glyph string so that we can draw rectangles for the
18460 characters of the glyph string. */
18461 if (s->font == NULL)
18462 {
18463 s->font_not_found_p = 1;
18464 s->font = FRAME_FONT (s->f);
18465 }
18466
18467 /* Adjust base line for subscript/superscript text. */
18468 s->ybase += s->first_glyph->voffset;
18469
18470 xassert (s->face && s->face->gc);
18471
18472 /* This glyph string must always be drawn with 16-bit functions. */
18473 s->two_byte_p = 1;
18474
18475 return s->gidx + s->nchars;
18476 }
18477
18478
18479 /* Fill glyph string S from a sequence of character glyphs.
18480
18481 FACE_ID is the face id of the string. START is the index of the
18482 first glyph to consider, END is the index of the last + 1.
18483 OVERLAPS non-zero means S should draw the foreground only, and use
18484 its physical height for clipping. See also draw_glyphs.
18485
18486 Value is the index of the first glyph not in S. */
18487
18488 static int
18489 fill_glyph_string (s, face_id, start, end, overlaps)
18490 struct glyph_string *s;
18491 int face_id;
18492 int start, end, overlaps;
18493 {
18494 struct glyph *glyph, *last;
18495 int voffset;
18496 int glyph_not_available_p;
18497
18498 xassert (s->f == XFRAME (s->w->frame));
18499 xassert (s->nchars == 0);
18500 xassert (start >= 0 && end > start);
18501
18502 s->for_overlaps = overlaps,
18503 glyph = s->row->glyphs[s->area] + start;
18504 last = s->row->glyphs[s->area] + end;
18505 voffset = glyph->voffset;
18506
18507 glyph_not_available_p = glyph->glyph_not_available_p;
18508
18509 while (glyph < last
18510 && glyph->type == CHAR_GLYPH
18511 && glyph->voffset == voffset
18512 /* Same face id implies same font, nowadays. */
18513 && glyph->face_id == face_id
18514 && glyph->glyph_not_available_p == glyph_not_available_p)
18515 {
18516 int two_byte_p;
18517
18518 s->face = get_glyph_face_and_encoding (s->f, glyph,
18519 s->char2b + s->nchars,
18520 &two_byte_p);
18521 s->two_byte_p = two_byte_p;
18522 ++s->nchars;
18523 xassert (s->nchars <= end - start);
18524 s->width += glyph->pixel_width;
18525 ++glyph;
18526 }
18527
18528 s->font = s->face->font;
18529 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
18530
18531 /* If the specified font could not be loaded, use the frame's font,
18532 but record the fact that we couldn't load it in
18533 S->font_not_found_p so that we can draw rectangles for the
18534 characters of the glyph string. */
18535 if (s->font == NULL || glyph_not_available_p)
18536 {
18537 s->font_not_found_p = 1;
18538 s->font = FRAME_FONT (s->f);
18539 }
18540
18541 /* Adjust base line for subscript/superscript text. */
18542 s->ybase += voffset;
18543
18544 xassert (s->face && s->face->gc);
18545 return glyph - s->row->glyphs[s->area];
18546 }
18547
18548
18549 /* Fill glyph string S from image glyph S->first_glyph. */
18550
18551 static void
18552 fill_image_glyph_string (s)
18553 struct glyph_string *s;
18554 {
18555 xassert (s->first_glyph->type == IMAGE_GLYPH);
18556 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
18557 xassert (s->img);
18558 s->slice = s->first_glyph->slice;
18559 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
18560 s->font = s->face->font;
18561 s->width = s->first_glyph->pixel_width;
18562
18563 /* Adjust base line for subscript/superscript text. */
18564 s->ybase += s->first_glyph->voffset;
18565 }
18566
18567
18568 /* Fill glyph string S from a sequence of stretch glyphs.
18569
18570 ROW is the glyph row in which the glyphs are found, AREA is the
18571 area within the row. START is the index of the first glyph to
18572 consider, END is the index of the last + 1.
18573
18574 Value is the index of the first glyph not in S. */
18575
18576 static int
18577 fill_stretch_glyph_string (s, row, area, start, end)
18578 struct glyph_string *s;
18579 struct glyph_row *row;
18580 enum glyph_row_area area;
18581 int start, end;
18582 {
18583 struct glyph *glyph, *last;
18584 int voffset, face_id;
18585
18586 xassert (s->first_glyph->type == STRETCH_GLYPH);
18587
18588 glyph = s->row->glyphs[s->area] + start;
18589 last = s->row->glyphs[s->area] + end;
18590 face_id = glyph->face_id;
18591 s->face = FACE_FROM_ID (s->f, face_id);
18592 s->font = s->face->font;
18593 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
18594 s->width = glyph->pixel_width;
18595 voffset = glyph->voffset;
18596
18597 for (++glyph;
18598 (glyph < last
18599 && glyph->type == STRETCH_GLYPH
18600 && glyph->voffset == voffset
18601 && glyph->face_id == face_id);
18602 ++glyph)
18603 s->width += glyph->pixel_width;
18604
18605 /* Adjust base line for subscript/superscript text. */
18606 s->ybase += voffset;
18607
18608 /* The case that face->gc == 0 is handled when drawing the glyph
18609 string by calling PREPARE_FACE_FOR_DISPLAY. */
18610 xassert (s->face);
18611 return glyph - s->row->glyphs[s->area];
18612 }
18613
18614
18615 /* EXPORT for RIF:
18616 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
18617 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
18618 assumed to be zero. */
18619
18620 void
18621 x_get_glyph_overhangs (glyph, f, left, right)
18622 struct glyph *glyph;
18623 struct frame *f;
18624 int *left, *right;
18625 {
18626 *left = *right = 0;
18627
18628 if (glyph->type == CHAR_GLYPH)
18629 {
18630 XFontStruct *font;
18631 struct face *face;
18632 struct font_info *font_info;
18633 XChar2b char2b;
18634 XCharStruct *pcm;
18635
18636 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
18637 font = face->font;
18638 font_info = FONT_INFO_FROM_ID (f, face->font_info_id);
18639 if (font /* ++KFS: Should this be font_info ? */
18640 && (pcm = rif->per_char_metric (font, &char2b, glyph->font_type)))
18641 {
18642 if (pcm->rbearing > pcm->width)
18643 *right = pcm->rbearing - pcm->width;
18644 if (pcm->lbearing < 0)
18645 *left = -pcm->lbearing;
18646 }
18647 }
18648 }
18649
18650
18651 /* Return the index of the first glyph preceding glyph string S that
18652 is overwritten by S because of S's left overhang. Value is -1
18653 if no glyphs are overwritten. */
18654
18655 static int
18656 left_overwritten (s)
18657 struct glyph_string *s;
18658 {
18659 int k;
18660
18661 if (s->left_overhang)
18662 {
18663 int x = 0, i;
18664 struct glyph *glyphs = s->row->glyphs[s->area];
18665 int first = s->first_glyph - glyphs;
18666
18667 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
18668 x -= glyphs[i].pixel_width;
18669
18670 k = i + 1;
18671 }
18672 else
18673 k = -1;
18674
18675 return k;
18676 }
18677
18678
18679 /* Return the index of the first glyph preceding glyph string S that
18680 is overwriting S because of its right overhang. Value is -1 if no
18681 glyph in front of S overwrites S. */
18682
18683 static int
18684 left_overwriting (s)
18685 struct glyph_string *s;
18686 {
18687 int i, k, x;
18688 struct glyph *glyphs = s->row->glyphs[s->area];
18689 int first = s->first_glyph - glyphs;
18690
18691 k = -1;
18692 x = 0;
18693 for (i = first - 1; i >= 0; --i)
18694 {
18695 int left, right;
18696 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
18697 if (x + right > 0)
18698 k = i;
18699 x -= glyphs[i].pixel_width;
18700 }
18701
18702 return k;
18703 }
18704
18705
18706 /* Return the index of the last glyph following glyph string S that is
18707 not overwritten by S because of S's right overhang. Value is -1 if
18708 no such glyph is found. */
18709
18710 static int
18711 right_overwritten (s)
18712 struct glyph_string *s;
18713 {
18714 int k = -1;
18715
18716 if (s->right_overhang)
18717 {
18718 int x = 0, i;
18719 struct glyph *glyphs = s->row->glyphs[s->area];
18720 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
18721 int end = s->row->used[s->area];
18722
18723 for (i = first; i < end && s->right_overhang > x; ++i)
18724 x += glyphs[i].pixel_width;
18725
18726 k = i;
18727 }
18728
18729 return k;
18730 }
18731
18732
18733 /* Return the index of the last glyph following glyph string S that
18734 overwrites S because of its left overhang. Value is negative
18735 if no such glyph is found. */
18736
18737 static int
18738 right_overwriting (s)
18739 struct glyph_string *s;
18740 {
18741 int i, k, x;
18742 int end = s->row->used[s->area];
18743 struct glyph *glyphs = s->row->glyphs[s->area];
18744 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
18745
18746 k = -1;
18747 x = 0;
18748 for (i = first; i < end; ++i)
18749 {
18750 int left, right;
18751 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
18752 if (x - left < 0)
18753 k = i;
18754 x += glyphs[i].pixel_width;
18755 }
18756
18757 return k;
18758 }
18759
18760
18761 /* Get face and two-byte form of character C in face FACE_ID on frame
18762 F. The encoding of C is returned in *CHAR2B. MULTIBYTE_P non-zero
18763 means we want to display multibyte text. DISPLAY_P non-zero means
18764 make sure that X resources for the face returned are allocated.
18765 Value is a pointer to a realized face that is ready for display if
18766 DISPLAY_P is non-zero. */
18767
18768 static INLINE struct face *
18769 get_char_face_and_encoding (f, c, face_id, char2b, multibyte_p, display_p)
18770 struct frame *f;
18771 int c, face_id;
18772 XChar2b *char2b;
18773 int multibyte_p, display_p;
18774 {
18775 struct face *face = FACE_FROM_ID (f, face_id);
18776
18777 if (!multibyte_p)
18778 {
18779 /* Unibyte case. We don't have to encode, but we have to make
18780 sure to use a face suitable for unibyte. */
18781 STORE_XCHAR2B (char2b, 0, c);
18782 face_id = FACE_FOR_CHAR (f, face, c);
18783 face = FACE_FROM_ID (f, face_id);
18784 }
18785 else if (c < 128 && face_id < BASIC_FACE_ID_SENTINEL)
18786 {
18787 /* Case of ASCII in a face known to fit ASCII. */
18788 STORE_XCHAR2B (char2b, 0, c);
18789 }
18790 else
18791 {
18792 int c1, c2, charset;
18793
18794 /* Split characters into bytes. If c2 is -1 afterwards, C is
18795 really a one-byte character so that byte1 is zero. */
18796 SPLIT_CHAR (c, charset, c1, c2);
18797 if (c2 > 0)
18798 STORE_XCHAR2B (char2b, c1, c2);
18799 else
18800 STORE_XCHAR2B (char2b, 0, c1);
18801
18802 /* Maybe encode the character in *CHAR2B. */
18803 if (face->font != NULL)
18804 {
18805 struct font_info *font_info
18806 = FONT_INFO_FROM_ID (f, face->font_info_id);
18807 if (font_info)
18808 rif->encode_char (c, char2b, font_info, 0);
18809 }
18810 }
18811
18812 /* Make sure X resources of the face are allocated. */
18813 #ifdef HAVE_X_WINDOWS
18814 if (display_p)
18815 #endif
18816 {
18817 xassert (face != NULL);
18818 PREPARE_FACE_FOR_DISPLAY (f, face);
18819 }
18820
18821 return face;
18822 }
18823
18824
18825 /* Set background width of glyph string S. START is the index of the
18826 first glyph following S. LAST_X is the right-most x-position + 1
18827 in the drawing area. */
18828
18829 static INLINE void
18830 set_glyph_string_background_width (s, start, last_x)
18831 struct glyph_string *s;
18832 int start;
18833 int last_x;
18834 {
18835 /* If the face of this glyph string has to be drawn to the end of
18836 the drawing area, set S->extends_to_end_of_line_p. */
18837
18838 if (start == s->row->used[s->area]
18839 && s->area == TEXT_AREA
18840 && ((s->row->fill_line_p
18841 && (s->hl == DRAW_NORMAL_TEXT
18842 || s->hl == DRAW_IMAGE_RAISED
18843 || s->hl == DRAW_IMAGE_SUNKEN))
18844 || s->hl == DRAW_MOUSE_FACE))
18845 s->extends_to_end_of_line_p = 1;
18846
18847 /* If S extends its face to the end of the line, set its
18848 background_width to the distance to the right edge of the drawing
18849 area. */
18850 if (s->extends_to_end_of_line_p)
18851 s->background_width = last_x - s->x + 1;
18852 else
18853 s->background_width = s->width;
18854 }
18855
18856
18857 /* Compute overhangs and x-positions for glyph string S and its
18858 predecessors, or successors. X is the starting x-position for S.
18859 BACKWARD_P non-zero means process predecessors. */
18860
18861 static void
18862 compute_overhangs_and_x (s, x, backward_p)
18863 struct glyph_string *s;
18864 int x;
18865 int backward_p;
18866 {
18867 if (backward_p)
18868 {
18869 while (s)
18870 {
18871 if (rif->compute_glyph_string_overhangs)
18872 rif->compute_glyph_string_overhangs (s);
18873 x -= s->width;
18874 s->x = x;
18875 s = s->prev;
18876 }
18877 }
18878 else
18879 {
18880 while (s)
18881 {
18882 if (rif->compute_glyph_string_overhangs)
18883 rif->compute_glyph_string_overhangs (s);
18884 s->x = x;
18885 x += s->width;
18886 s = s->next;
18887 }
18888 }
18889 }
18890
18891
18892
18893 /* The following macros are only called from draw_glyphs below.
18894 They reference the following parameters of that function directly:
18895 `w', `row', `area', and `overlap_p'
18896 as well as the following local variables:
18897 `s', `f', and `hdc' (in W32) */
18898
18899 #ifdef HAVE_NTGUI
18900 /* On W32, silently add local `hdc' variable to argument list of
18901 init_glyph_string. */
18902 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
18903 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
18904 #else
18905 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
18906 init_glyph_string (s, char2b, w, row, area, start, hl)
18907 #endif
18908
18909 /* Add a glyph string for a stretch glyph to the list of strings
18910 between HEAD and TAIL. START is the index of the stretch glyph in
18911 row area AREA of glyph row ROW. END is the index of the last glyph
18912 in that glyph row area. X is the current output position assigned
18913 to the new glyph string constructed. HL overrides that face of the
18914 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
18915 is the right-most x-position of the drawing area. */
18916
18917 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
18918 and below -- keep them on one line. */
18919 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
18920 do \
18921 { \
18922 s = (struct glyph_string *) alloca (sizeof *s); \
18923 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
18924 START = fill_stretch_glyph_string (s, row, area, START, END); \
18925 append_glyph_string (&HEAD, &TAIL, s); \
18926 s->x = (X); \
18927 } \
18928 while (0)
18929
18930
18931 /* Add a glyph string for an image glyph to the list of strings
18932 between HEAD and TAIL. START is the index of the image glyph in
18933 row area AREA of glyph row ROW. END is the index of the last glyph
18934 in that glyph row area. X is the current output position assigned
18935 to the new glyph string constructed. HL overrides that face of the
18936 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
18937 is the right-most x-position of the drawing area. */
18938
18939 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
18940 do \
18941 { \
18942 s = (struct glyph_string *) alloca (sizeof *s); \
18943 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
18944 fill_image_glyph_string (s); \
18945 append_glyph_string (&HEAD, &TAIL, s); \
18946 ++START; \
18947 s->x = (X); \
18948 } \
18949 while (0)
18950
18951
18952 /* Add a glyph string for a sequence of character glyphs to the list
18953 of strings between HEAD and TAIL. START is the index of the first
18954 glyph in row area AREA of glyph row ROW that is part of the new
18955 glyph string. END is the index of the last glyph in that glyph row
18956 area. X is the current output position assigned to the new glyph
18957 string constructed. HL overrides that face of the glyph; e.g. it
18958 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
18959 right-most x-position of the drawing area. */
18960
18961 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
18962 do \
18963 { \
18964 int c, face_id; \
18965 XChar2b *char2b; \
18966 \
18967 c = (row)->glyphs[area][START].u.ch; \
18968 face_id = (row)->glyphs[area][START].face_id; \
18969 \
18970 s = (struct glyph_string *) alloca (sizeof *s); \
18971 char2b = (XChar2b *) alloca ((END - START) * sizeof *char2b); \
18972 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
18973 append_glyph_string (&HEAD, &TAIL, s); \
18974 s->x = (X); \
18975 START = fill_glyph_string (s, face_id, START, END, overlaps); \
18976 } \
18977 while (0)
18978
18979
18980 /* Add a glyph string for a composite sequence to the list of strings
18981 between HEAD and TAIL. START is the index of the first glyph in
18982 row area AREA of glyph row ROW that is part of the new glyph
18983 string. END is the index of the last glyph in that glyph row area.
18984 X is the current output position assigned to the new glyph string
18985 constructed. HL overrides that face of the glyph; e.g. it is
18986 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
18987 x-position of the drawing area. */
18988
18989 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
18990 do { \
18991 int cmp_id = (row)->glyphs[area][START].u.cmp_id; \
18992 int face_id = (row)->glyphs[area][START].face_id; \
18993 struct face *base_face = FACE_FROM_ID (f, face_id); \
18994 struct composition *cmp = composition_table[cmp_id]; \
18995 int glyph_len = cmp->glyph_len; \
18996 XChar2b *char2b; \
18997 struct face **faces; \
18998 struct glyph_string *first_s = NULL; \
18999 int n; \
19000 \
19001 base_face = base_face->ascii_face; \
19002 char2b = (XChar2b *) alloca ((sizeof *char2b) * glyph_len); \
19003 faces = (struct face **) alloca ((sizeof *faces) * glyph_len); \
19004 /* At first, fill in `char2b' and `faces'. */ \
19005 for (n = 0; n < glyph_len; n++) \
19006 { \
19007 int c = COMPOSITION_GLYPH (cmp, n); \
19008 int this_face_id = FACE_FOR_CHAR (f, base_face, c); \
19009 faces[n] = FACE_FROM_ID (f, this_face_id); \
19010 get_char_face_and_encoding (f, c, this_face_id, \
19011 char2b + n, 1, 1); \
19012 } \
19013 \
19014 /* Make glyph_strings for each glyph sequence that is drawable by \
19015 the same face, and append them to HEAD/TAIL. */ \
19016 for (n = 0; n < cmp->glyph_len;) \
19017 { \
19018 s = (struct glyph_string *) alloca (sizeof *s); \
19019 INIT_GLYPH_STRING (s, char2b + n, w, row, area, START, HL); \
19020 append_glyph_string (&(HEAD), &(TAIL), s); \
19021 s->cmp = cmp; \
19022 s->gidx = n; \
19023 s->x = (X); \
19024 \
19025 if (n == 0) \
19026 first_s = s; \
19027 \
19028 n = fill_composite_glyph_string (s, faces, overlaps); \
19029 } \
19030 \
19031 ++START; \
19032 s = first_s; \
19033 } while (0)
19034
19035
19036 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
19037 of AREA of glyph row ROW on window W between indices START and END.
19038 HL overrides the face for drawing glyph strings, e.g. it is
19039 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
19040 x-positions of the drawing area.
19041
19042 This is an ugly monster macro construct because we must use alloca
19043 to allocate glyph strings (because draw_glyphs can be called
19044 asynchronously). */
19045
19046 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
19047 do \
19048 { \
19049 HEAD = TAIL = NULL; \
19050 while (START < END) \
19051 { \
19052 struct glyph *first_glyph = (row)->glyphs[area] + START; \
19053 switch (first_glyph->type) \
19054 { \
19055 case CHAR_GLYPH: \
19056 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
19057 HL, X, LAST_X); \
19058 break; \
19059 \
19060 case COMPOSITE_GLYPH: \
19061 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
19062 HL, X, LAST_X); \
19063 break; \
19064 \
19065 case STRETCH_GLYPH: \
19066 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
19067 HL, X, LAST_X); \
19068 break; \
19069 \
19070 case IMAGE_GLYPH: \
19071 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
19072 HL, X, LAST_X); \
19073 break; \
19074 \
19075 default: \
19076 abort (); \
19077 } \
19078 \
19079 set_glyph_string_background_width (s, START, LAST_X); \
19080 (X) += s->width; \
19081 } \
19082 } \
19083 while (0)
19084
19085
19086 /* Draw glyphs between START and END in AREA of ROW on window W,
19087 starting at x-position X. X is relative to AREA in W. HL is a
19088 face-override with the following meaning:
19089
19090 DRAW_NORMAL_TEXT draw normally
19091 DRAW_CURSOR draw in cursor face
19092 DRAW_MOUSE_FACE draw in mouse face.
19093 DRAW_INVERSE_VIDEO draw in mode line face
19094 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
19095 DRAW_IMAGE_RAISED draw an image with a raised relief around it
19096
19097 If OVERLAPS is non-zero, draw only the foreground of characters and
19098 clip to the physical height of ROW. Non-zero value also defines
19099 the overlapping part to be drawn:
19100
19101 OVERLAPS_PRED overlap with preceding rows
19102 OVERLAPS_SUCC overlap with succeeding rows
19103 OVERLAPS_BOTH overlap with both preceding/succeeding rows
19104 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
19105
19106 Value is the x-position reached, relative to AREA of W. */
19107
19108 static int
19109 draw_glyphs (w, x, row, area, start, end, hl, overlaps)
19110 struct window *w;
19111 int x;
19112 struct glyph_row *row;
19113 enum glyph_row_area area;
19114 int start, end;
19115 enum draw_glyphs_face hl;
19116 int overlaps;
19117 {
19118 struct glyph_string *head, *tail;
19119 struct glyph_string *s;
19120 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
19121 int last_x, area_width;
19122 int x_reached;
19123 int i, j;
19124 struct frame *f = XFRAME (WINDOW_FRAME (w));
19125 DECLARE_HDC (hdc);
19126
19127 ALLOCATE_HDC (hdc, f);
19128
19129 /* Let's rather be paranoid than getting a SEGV. */
19130 end = min (end, row->used[area]);
19131 start = max (0, start);
19132 start = min (end, start);
19133
19134 /* Translate X to frame coordinates. Set last_x to the right
19135 end of the drawing area. */
19136 if (row->full_width_p)
19137 {
19138 /* X is relative to the left edge of W, without scroll bars
19139 or fringes. */
19140 x += WINDOW_LEFT_EDGE_X (w);
19141 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
19142 }
19143 else
19144 {
19145 int area_left = window_box_left (w, area);
19146 x += area_left;
19147 area_width = window_box_width (w, area);
19148 last_x = area_left + area_width;
19149 }
19150
19151 /* Build a doubly-linked list of glyph_string structures between
19152 head and tail from what we have to draw. Note that the macro
19153 BUILD_GLYPH_STRINGS will modify its start parameter. That's
19154 the reason we use a separate variable `i'. */
19155 i = start;
19156 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
19157 if (tail)
19158 x_reached = tail->x + tail->background_width;
19159 else
19160 x_reached = x;
19161
19162 /* If there are any glyphs with lbearing < 0 or rbearing > width in
19163 the row, redraw some glyphs in front or following the glyph
19164 strings built above. */
19165 if (head && !overlaps && row->contains_overlapping_glyphs_p)
19166 {
19167 int dummy_x = 0;
19168 struct glyph_string *h, *t;
19169
19170 /* Compute overhangs for all glyph strings. */
19171 if (rif->compute_glyph_string_overhangs)
19172 for (s = head; s; s = s->next)
19173 rif->compute_glyph_string_overhangs (s);
19174
19175 /* Prepend glyph strings for glyphs in front of the first glyph
19176 string that are overwritten because of the first glyph
19177 string's left overhang. The background of all strings
19178 prepended must be drawn because the first glyph string
19179 draws over it. */
19180 i = left_overwritten (head);
19181 if (i >= 0)
19182 {
19183 j = i;
19184 BUILD_GLYPH_STRINGS (j, start, h, t,
19185 DRAW_NORMAL_TEXT, dummy_x, last_x);
19186 start = i;
19187 compute_overhangs_and_x (t, head->x, 1);
19188 prepend_glyph_string_lists (&head, &tail, h, t);
19189 clip_head = head;
19190 }
19191
19192 /* Prepend glyph strings for glyphs in front of the first glyph
19193 string that overwrite that glyph string because of their
19194 right overhang. For these strings, only the foreground must
19195 be drawn, because it draws over the glyph string at `head'.
19196 The background must not be drawn because this would overwrite
19197 right overhangs of preceding glyphs for which no glyph
19198 strings exist. */
19199 i = left_overwriting (head);
19200 if (i >= 0)
19201 {
19202 clip_head = head;
19203 BUILD_GLYPH_STRINGS (i, start, h, t,
19204 DRAW_NORMAL_TEXT, dummy_x, last_x);
19205 for (s = h; s; s = s->next)
19206 s->background_filled_p = 1;
19207 compute_overhangs_and_x (t, head->x, 1);
19208 prepend_glyph_string_lists (&head, &tail, h, t);
19209 }
19210
19211 /* Append glyphs strings for glyphs following the last glyph
19212 string tail that are overwritten by tail. The background of
19213 these strings has to be drawn because tail's foreground draws
19214 over it. */
19215 i = right_overwritten (tail);
19216 if (i >= 0)
19217 {
19218 BUILD_GLYPH_STRINGS (end, i, h, t,
19219 DRAW_NORMAL_TEXT, x, last_x);
19220 compute_overhangs_and_x (h, tail->x + tail->width, 0);
19221 append_glyph_string_lists (&head, &tail, h, t);
19222 clip_tail = tail;
19223 }
19224
19225 /* Append glyph strings for glyphs following the last glyph
19226 string tail that overwrite tail. The foreground of such
19227 glyphs has to be drawn because it writes into the background
19228 of tail. The background must not be drawn because it could
19229 paint over the foreground of following glyphs. */
19230 i = right_overwriting (tail);
19231 if (i >= 0)
19232 {
19233 clip_tail = tail;
19234 BUILD_GLYPH_STRINGS (end, i, h, t,
19235 DRAW_NORMAL_TEXT, x, last_x);
19236 for (s = h; s; s = s->next)
19237 s->background_filled_p = 1;
19238 compute_overhangs_and_x (h, tail->x + tail->width, 0);
19239 append_glyph_string_lists (&head, &tail, h, t);
19240 }
19241 if (clip_head || clip_tail)
19242 for (s = head; s; s = s->next)
19243 {
19244 s->clip_head = clip_head;
19245 s->clip_tail = clip_tail;
19246 }
19247 }
19248
19249 /* Draw all strings. */
19250 for (s = head; s; s = s->next)
19251 rif->draw_glyph_string (s);
19252
19253 if (area == TEXT_AREA
19254 && !row->full_width_p
19255 /* When drawing overlapping rows, only the glyph strings'
19256 foreground is drawn, which doesn't erase a cursor
19257 completely. */
19258 && !overlaps)
19259 {
19260 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
19261 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
19262 : (tail ? tail->x + tail->background_width : x));
19263
19264 int text_left = window_box_left (w, TEXT_AREA);
19265 x0 -= text_left;
19266 x1 -= text_left;
19267
19268 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
19269 row->y, MATRIX_ROW_BOTTOM_Y (row));
19270 }
19271
19272 /* Value is the x-position up to which drawn, relative to AREA of W.
19273 This doesn't include parts drawn because of overhangs. */
19274 if (row->full_width_p)
19275 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
19276 else
19277 x_reached -= window_box_left (w, area);
19278
19279 RELEASE_HDC (hdc, f);
19280
19281 return x_reached;
19282 }
19283
19284 /* Expand row matrix if too narrow. Don't expand if area
19285 is not present. */
19286
19287 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
19288 { \
19289 if (!fonts_changed_p \
19290 && (it->glyph_row->glyphs[area] \
19291 < it->glyph_row->glyphs[area + 1])) \
19292 { \
19293 it->w->ncols_scale_factor++; \
19294 fonts_changed_p = 1; \
19295 } \
19296 }
19297
19298 /* Store one glyph for IT->char_to_display in IT->glyph_row.
19299 Called from x_produce_glyphs when IT->glyph_row is non-null. */
19300
19301 static INLINE void
19302 append_glyph (it)
19303 struct it *it;
19304 {
19305 struct glyph *glyph;
19306 enum glyph_row_area area = it->area;
19307
19308 xassert (it->glyph_row);
19309 xassert (it->char_to_display != '\n' && it->char_to_display != '\t');
19310
19311 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
19312 if (glyph < it->glyph_row->glyphs[area + 1])
19313 {
19314 glyph->charpos = CHARPOS (it->position);
19315 glyph->object = it->object;
19316 glyph->pixel_width = it->pixel_width;
19317 glyph->ascent = it->ascent;
19318 glyph->descent = it->descent;
19319 glyph->voffset = it->voffset;
19320 glyph->type = CHAR_GLYPH;
19321 glyph->multibyte_p = it->multibyte_p;
19322 glyph->left_box_line_p = it->start_of_box_run_p;
19323 glyph->right_box_line_p = it->end_of_box_run_p;
19324 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
19325 || it->phys_descent > it->descent);
19326 glyph->padding_p = 0;
19327 glyph->glyph_not_available_p = it->glyph_not_available_p;
19328 glyph->face_id = it->face_id;
19329 glyph->u.ch = it->char_to_display;
19330 glyph->slice = null_glyph_slice;
19331 glyph->font_type = FONT_TYPE_UNKNOWN;
19332 ++it->glyph_row->used[area];
19333 }
19334 else
19335 IT_EXPAND_MATRIX_WIDTH (it, area);
19336 }
19337
19338 /* Store one glyph for the composition IT->cmp_id in IT->glyph_row.
19339 Called from x_produce_glyphs when IT->glyph_row is non-null. */
19340
19341 static INLINE void
19342 append_composite_glyph (it)
19343 struct it *it;
19344 {
19345 struct glyph *glyph;
19346 enum glyph_row_area area = it->area;
19347
19348 xassert (it->glyph_row);
19349
19350 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
19351 if (glyph < it->glyph_row->glyphs[area + 1])
19352 {
19353 glyph->charpos = CHARPOS (it->position);
19354 glyph->object = it->object;
19355 glyph->pixel_width = it->pixel_width;
19356 glyph->ascent = it->ascent;
19357 glyph->descent = it->descent;
19358 glyph->voffset = it->voffset;
19359 glyph->type = COMPOSITE_GLYPH;
19360 glyph->multibyte_p = it->multibyte_p;
19361 glyph->left_box_line_p = it->start_of_box_run_p;
19362 glyph->right_box_line_p = it->end_of_box_run_p;
19363 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
19364 || it->phys_descent > it->descent);
19365 glyph->padding_p = 0;
19366 glyph->glyph_not_available_p = 0;
19367 glyph->face_id = it->face_id;
19368 glyph->u.cmp_id = it->cmp_id;
19369 glyph->slice = null_glyph_slice;
19370 glyph->font_type = FONT_TYPE_UNKNOWN;
19371 ++it->glyph_row->used[area];
19372 }
19373 else
19374 IT_EXPAND_MATRIX_WIDTH (it, area);
19375 }
19376
19377
19378 /* Change IT->ascent and IT->height according to the setting of
19379 IT->voffset. */
19380
19381 static INLINE void
19382 take_vertical_position_into_account (it)
19383 struct it *it;
19384 {
19385 if (it->voffset)
19386 {
19387 if (it->voffset < 0)
19388 /* Increase the ascent so that we can display the text higher
19389 in the line. */
19390 it->ascent -= it->voffset;
19391 else
19392 /* Increase the descent so that we can display the text lower
19393 in the line. */
19394 it->descent += it->voffset;
19395 }
19396 }
19397
19398
19399 /* Produce glyphs/get display metrics for the image IT is loaded with.
19400 See the description of struct display_iterator in dispextern.h for
19401 an overview of struct display_iterator. */
19402
19403 static void
19404 produce_image_glyph (it)
19405 struct it *it;
19406 {
19407 struct image *img;
19408 struct face *face;
19409 int glyph_ascent;
19410 struct glyph_slice slice;
19411
19412 xassert (it->what == IT_IMAGE);
19413
19414 face = FACE_FROM_ID (it->f, it->face_id);
19415 xassert (face);
19416 /* Make sure X resources of the face is loaded. */
19417 PREPARE_FACE_FOR_DISPLAY (it->f, face);
19418
19419 if (it->image_id < 0)
19420 {
19421 /* Fringe bitmap. */
19422 it->ascent = it->phys_ascent = 0;
19423 it->descent = it->phys_descent = 0;
19424 it->pixel_width = 0;
19425 it->nglyphs = 0;
19426 return;
19427 }
19428
19429 img = IMAGE_FROM_ID (it->f, it->image_id);
19430 xassert (img);
19431 /* Make sure X resources of the image is loaded. */
19432 prepare_image_for_display (it->f, img);
19433
19434 slice.x = slice.y = 0;
19435 slice.width = img->width;
19436 slice.height = img->height;
19437
19438 if (INTEGERP (it->slice.x))
19439 slice.x = XINT (it->slice.x);
19440 else if (FLOATP (it->slice.x))
19441 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
19442
19443 if (INTEGERP (it->slice.y))
19444 slice.y = XINT (it->slice.y);
19445 else if (FLOATP (it->slice.y))
19446 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
19447
19448 if (INTEGERP (it->slice.width))
19449 slice.width = XINT (it->slice.width);
19450 else if (FLOATP (it->slice.width))
19451 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
19452
19453 if (INTEGERP (it->slice.height))
19454 slice.height = XINT (it->slice.height);
19455 else if (FLOATP (it->slice.height))
19456 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
19457
19458 if (slice.x >= img->width)
19459 slice.x = img->width;
19460 if (slice.y >= img->height)
19461 slice.y = img->height;
19462 if (slice.x + slice.width >= img->width)
19463 slice.width = img->width - slice.x;
19464 if (slice.y + slice.height > img->height)
19465 slice.height = img->height - slice.y;
19466
19467 if (slice.width == 0 || slice.height == 0)
19468 return;
19469
19470 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
19471
19472 it->descent = slice.height - glyph_ascent;
19473 if (slice.y == 0)
19474 it->descent += img->vmargin;
19475 if (slice.y + slice.height == img->height)
19476 it->descent += img->vmargin;
19477 it->phys_descent = it->descent;
19478
19479 it->pixel_width = slice.width;
19480 if (slice.x == 0)
19481 it->pixel_width += img->hmargin;
19482 if (slice.x + slice.width == img->width)
19483 it->pixel_width += img->hmargin;
19484
19485 /* It's quite possible for images to have an ascent greater than
19486 their height, so don't get confused in that case. */
19487 if (it->descent < 0)
19488 it->descent = 0;
19489
19490 #if 0 /* this breaks image tiling */
19491 /* If this glyph is alone on the last line, adjust it.ascent to minimum row ascent. */
19492 int face_ascent = face->font ? FONT_BASE (face->font) : FRAME_BASELINE_OFFSET (it->f);
19493 if (face_ascent > it->ascent)
19494 it->ascent = it->phys_ascent = face_ascent;
19495 #endif
19496
19497 it->nglyphs = 1;
19498
19499 if (face->box != FACE_NO_BOX)
19500 {
19501 if (face->box_line_width > 0)
19502 {
19503 if (slice.y == 0)
19504 it->ascent += face->box_line_width;
19505 if (slice.y + slice.height == img->height)
19506 it->descent += face->box_line_width;
19507 }
19508
19509 if (it->start_of_box_run_p && slice.x == 0)
19510 it->pixel_width += abs (face->box_line_width);
19511 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
19512 it->pixel_width += abs (face->box_line_width);
19513 }
19514
19515 take_vertical_position_into_account (it);
19516
19517 if (it->glyph_row)
19518 {
19519 struct glyph *glyph;
19520 enum glyph_row_area area = it->area;
19521
19522 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
19523 if (glyph < it->glyph_row->glyphs[area + 1])
19524 {
19525 glyph->charpos = CHARPOS (it->position);
19526 glyph->object = it->object;
19527 glyph->pixel_width = it->pixel_width;
19528 glyph->ascent = glyph_ascent;
19529 glyph->descent = it->descent;
19530 glyph->voffset = it->voffset;
19531 glyph->type = IMAGE_GLYPH;
19532 glyph->multibyte_p = it->multibyte_p;
19533 glyph->left_box_line_p = it->start_of_box_run_p;
19534 glyph->right_box_line_p = it->end_of_box_run_p;
19535 glyph->overlaps_vertically_p = 0;
19536 glyph->padding_p = 0;
19537 glyph->glyph_not_available_p = 0;
19538 glyph->face_id = it->face_id;
19539 glyph->u.img_id = img->id;
19540 glyph->slice = slice;
19541 glyph->font_type = FONT_TYPE_UNKNOWN;
19542 ++it->glyph_row->used[area];
19543 }
19544 else
19545 IT_EXPAND_MATRIX_WIDTH (it, area);
19546 }
19547 }
19548
19549
19550 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
19551 of the glyph, WIDTH and HEIGHT are the width and height of the
19552 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
19553
19554 static void
19555 append_stretch_glyph (it, object, width, height, ascent)
19556 struct it *it;
19557 Lisp_Object object;
19558 int width, height;
19559 int ascent;
19560 {
19561 struct glyph *glyph;
19562 enum glyph_row_area area = it->area;
19563
19564 xassert (ascent >= 0 && ascent <= height);
19565
19566 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
19567 if (glyph < it->glyph_row->glyphs[area + 1])
19568 {
19569 glyph->charpos = CHARPOS (it->position);
19570 glyph->object = object;
19571 glyph->pixel_width = width;
19572 glyph->ascent = ascent;
19573 glyph->descent = height - ascent;
19574 glyph->voffset = it->voffset;
19575 glyph->type = STRETCH_GLYPH;
19576 glyph->multibyte_p = it->multibyte_p;
19577 glyph->left_box_line_p = it->start_of_box_run_p;
19578 glyph->right_box_line_p = it->end_of_box_run_p;
19579 glyph->overlaps_vertically_p = 0;
19580 glyph->padding_p = 0;
19581 glyph->glyph_not_available_p = 0;
19582 glyph->face_id = it->face_id;
19583 glyph->u.stretch.ascent = ascent;
19584 glyph->u.stretch.height = height;
19585 glyph->slice = null_glyph_slice;
19586 glyph->font_type = FONT_TYPE_UNKNOWN;
19587 ++it->glyph_row->used[area];
19588 }
19589 else
19590 IT_EXPAND_MATRIX_WIDTH (it, area);
19591 }
19592
19593
19594 /* Produce a stretch glyph for iterator IT. IT->object is the value
19595 of the glyph property displayed. The value must be a list
19596 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
19597 being recognized:
19598
19599 1. `:width WIDTH' specifies that the space should be WIDTH *
19600 canonical char width wide. WIDTH may be an integer or floating
19601 point number.
19602
19603 2. `:relative-width FACTOR' specifies that the width of the stretch
19604 should be computed from the width of the first character having the
19605 `glyph' property, and should be FACTOR times that width.
19606
19607 3. `:align-to HPOS' specifies that the space should be wide enough
19608 to reach HPOS, a value in canonical character units.
19609
19610 Exactly one of the above pairs must be present.
19611
19612 4. `:height HEIGHT' specifies that the height of the stretch produced
19613 should be HEIGHT, measured in canonical character units.
19614
19615 5. `:relative-height FACTOR' specifies that the height of the
19616 stretch should be FACTOR times the height of the characters having
19617 the glyph property.
19618
19619 Either none or exactly one of 4 or 5 must be present.
19620
19621 6. `:ascent ASCENT' specifies that ASCENT percent of the height
19622 of the stretch should be used for the ascent of the stretch.
19623 ASCENT must be in the range 0 <= ASCENT <= 100. */
19624
19625 static void
19626 produce_stretch_glyph (it)
19627 struct it *it;
19628 {
19629 /* (space :width WIDTH :height HEIGHT ...) */
19630 Lisp_Object prop, plist;
19631 int width = 0, height = 0, align_to = -1;
19632 int zero_width_ok_p = 0, zero_height_ok_p = 0;
19633 int ascent = 0;
19634 double tem;
19635 struct face *face = FACE_FROM_ID (it->f, it->face_id);
19636 XFontStruct *font = face->font ? face->font : FRAME_FONT (it->f);
19637
19638 PREPARE_FACE_FOR_DISPLAY (it->f, face);
19639
19640 /* List should start with `space'. */
19641 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
19642 plist = XCDR (it->object);
19643
19644 /* Compute the width of the stretch. */
19645 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
19646 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
19647 {
19648 /* Absolute width `:width WIDTH' specified and valid. */
19649 zero_width_ok_p = 1;
19650 width = (int)tem;
19651 }
19652 else if (prop = Fplist_get (plist, QCrelative_width),
19653 NUMVAL (prop) > 0)
19654 {
19655 /* Relative width `:relative-width FACTOR' specified and valid.
19656 Compute the width of the characters having the `glyph'
19657 property. */
19658 struct it it2;
19659 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
19660
19661 it2 = *it;
19662 if (it->multibyte_p)
19663 {
19664 int maxlen = ((IT_BYTEPOS (*it) >= GPT ? ZV : GPT)
19665 - IT_BYTEPOS (*it));
19666 it2.c = STRING_CHAR_AND_LENGTH (p, maxlen, it2.len);
19667 }
19668 else
19669 it2.c = *p, it2.len = 1;
19670
19671 it2.glyph_row = NULL;
19672 it2.what = IT_CHARACTER;
19673 x_produce_glyphs (&it2);
19674 width = NUMVAL (prop) * it2.pixel_width;
19675 }
19676 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
19677 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
19678 {
19679 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
19680 align_to = (align_to < 0
19681 ? 0
19682 : align_to - window_box_left_offset (it->w, TEXT_AREA));
19683 else if (align_to < 0)
19684 align_to = window_box_left_offset (it->w, TEXT_AREA);
19685 width = max (0, (int)tem + align_to - it->current_x);
19686 zero_width_ok_p = 1;
19687 }
19688 else
19689 /* Nothing specified -> width defaults to canonical char width. */
19690 width = FRAME_COLUMN_WIDTH (it->f);
19691
19692 if (width <= 0 && (width < 0 || !zero_width_ok_p))
19693 width = 1;
19694
19695 /* Compute height. */
19696 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
19697 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
19698 {
19699 height = (int)tem;
19700 zero_height_ok_p = 1;
19701 }
19702 else if (prop = Fplist_get (plist, QCrelative_height),
19703 NUMVAL (prop) > 0)
19704 height = FONT_HEIGHT (font) * NUMVAL (prop);
19705 else
19706 height = FONT_HEIGHT (font);
19707
19708 if (height <= 0 && (height < 0 || !zero_height_ok_p))
19709 height = 1;
19710
19711 /* Compute percentage of height used for ascent. If
19712 `:ascent ASCENT' is present and valid, use that. Otherwise,
19713 derive the ascent from the font in use. */
19714 if (prop = Fplist_get (plist, QCascent),
19715 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
19716 ascent = height * NUMVAL (prop) / 100.0;
19717 else if (!NILP (prop)
19718 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
19719 ascent = min (max (0, (int)tem), height);
19720 else
19721 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
19722
19723 if (width > 0 && !it->truncate_lines_p
19724 && it->current_x + width > it->last_visible_x)
19725 width = it->last_visible_x - it->current_x - 1;
19726
19727 if (width > 0 && height > 0 && it->glyph_row)
19728 {
19729 Lisp_Object object = it->stack[it->sp - 1].string;
19730 if (!STRINGP (object))
19731 object = it->w->buffer;
19732 append_stretch_glyph (it, object, width, height, ascent);
19733 }
19734
19735 it->pixel_width = width;
19736 it->ascent = it->phys_ascent = ascent;
19737 it->descent = it->phys_descent = height - it->ascent;
19738 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
19739
19740 if (width > 0 && height > 0 && face->box != FACE_NO_BOX)
19741 {
19742 if (face->box_line_width > 0)
19743 {
19744 it->ascent += face->box_line_width;
19745 it->descent += face->box_line_width;
19746 }
19747
19748 if (it->start_of_box_run_p)
19749 it->pixel_width += abs (face->box_line_width);
19750 if (it->end_of_box_run_p)
19751 it->pixel_width += abs (face->box_line_width);
19752 }
19753
19754 take_vertical_position_into_account (it);
19755 }
19756
19757 /* Get line-height and line-spacing property at point.
19758 If line-height has format (HEIGHT TOTAL), return TOTAL
19759 in TOTAL_HEIGHT. */
19760
19761 static Lisp_Object
19762 get_line_height_property (it, prop)
19763 struct it *it;
19764 Lisp_Object prop;
19765 {
19766 Lisp_Object position;
19767
19768 if (STRINGP (it->object))
19769 position = make_number (IT_STRING_CHARPOS (*it));
19770 else if (BUFFERP (it->object))
19771 position = make_number (IT_CHARPOS (*it));
19772 else
19773 return Qnil;
19774
19775 return Fget_char_property (position, prop, it->object);
19776 }
19777
19778 /* Calculate line-height and line-spacing properties.
19779 An integer value specifies explicit pixel value.
19780 A float value specifies relative value to current face height.
19781 A cons (float . face-name) specifies relative value to
19782 height of specified face font.
19783
19784 Returns height in pixels, or nil. */
19785
19786
19787 static Lisp_Object
19788 calc_line_height_property (it, val, font, boff, override)
19789 struct it *it;
19790 Lisp_Object val;
19791 XFontStruct *font;
19792 int boff, override;
19793 {
19794 Lisp_Object face_name = Qnil;
19795 int ascent, descent, height;
19796
19797 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
19798 return val;
19799
19800 if (CONSP (val))
19801 {
19802 face_name = XCAR (val);
19803 val = XCDR (val);
19804 if (!NUMBERP (val))
19805 val = make_number (1);
19806 if (NILP (face_name))
19807 {
19808 height = it->ascent + it->descent;
19809 goto scale;
19810 }
19811 }
19812
19813 if (NILP (face_name))
19814 {
19815 font = FRAME_FONT (it->f);
19816 boff = FRAME_BASELINE_OFFSET (it->f);
19817 }
19818 else if (EQ (face_name, Qt))
19819 {
19820 override = 0;
19821 }
19822 else
19823 {
19824 int face_id;
19825 struct face *face;
19826 struct font_info *font_info;
19827
19828 face_id = lookup_named_face (it->f, face_name, ' ', 0);
19829 if (face_id < 0)
19830 return make_number (-1);
19831
19832 face = FACE_FROM_ID (it->f, face_id);
19833 font = face->font;
19834 if (font == NULL)
19835 return make_number (-1);
19836
19837 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
19838 boff = font_info->baseline_offset;
19839 if (font_info->vertical_centering)
19840 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
19841 }
19842
19843 ascent = FONT_BASE (font) + boff;
19844 descent = FONT_DESCENT (font) - boff;
19845
19846 if (override)
19847 {
19848 it->override_ascent = ascent;
19849 it->override_descent = descent;
19850 it->override_boff = boff;
19851 }
19852
19853 height = ascent + descent;
19854
19855 scale:
19856 if (FLOATP (val))
19857 height = (int)(XFLOAT_DATA (val) * height);
19858 else if (INTEGERP (val))
19859 height *= XINT (val);
19860
19861 return make_number (height);
19862 }
19863
19864
19865 /* RIF:
19866 Produce glyphs/get display metrics for the display element IT is
19867 loaded with. See the description of struct it in dispextern.h
19868 for an overview of struct it. */
19869
19870 void
19871 x_produce_glyphs (it)
19872 struct it *it;
19873 {
19874 int extra_line_spacing = it->extra_line_spacing;
19875
19876 it->glyph_not_available_p = 0;
19877
19878 if (it->what == IT_CHARACTER)
19879 {
19880 XChar2b char2b;
19881 XFontStruct *font;
19882 struct face *face = FACE_FROM_ID (it->f, it->face_id);
19883 XCharStruct *pcm;
19884 int font_not_found_p;
19885 struct font_info *font_info;
19886 int boff; /* baseline offset */
19887 /* We may change it->multibyte_p upon unibyte<->multibyte
19888 conversion. So, save the current value now and restore it
19889 later.
19890
19891 Note: It seems that we don't have to record multibyte_p in
19892 struct glyph because the character code itself tells if or
19893 not the character is multibyte. Thus, in the future, we must
19894 consider eliminating the field `multibyte_p' in the struct
19895 glyph. */
19896 int saved_multibyte_p = it->multibyte_p;
19897
19898 /* Maybe translate single-byte characters to multibyte, or the
19899 other way. */
19900 it->char_to_display = it->c;
19901 if (!ASCII_BYTE_P (it->c))
19902 {
19903 if (unibyte_display_via_language_environment
19904 && SINGLE_BYTE_CHAR_P (it->c)
19905 && (it->c >= 0240
19906 || !NILP (Vnonascii_translation_table)))
19907 {
19908 it->char_to_display = unibyte_char_to_multibyte (it->c);
19909 it->multibyte_p = 1;
19910 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
19911 face = FACE_FROM_ID (it->f, it->face_id);
19912 }
19913 else if (!SINGLE_BYTE_CHAR_P (it->c)
19914 && !it->multibyte_p)
19915 {
19916 it->multibyte_p = 1;
19917 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
19918 face = FACE_FROM_ID (it->f, it->face_id);
19919 }
19920 }
19921
19922 /* Get font to use. Encode IT->char_to_display. */
19923 get_char_face_and_encoding (it->f, it->char_to_display, it->face_id,
19924 &char2b, it->multibyte_p, 0);
19925 font = face->font;
19926
19927 /* When no suitable font found, use the default font. */
19928 font_not_found_p = font == NULL;
19929 if (font_not_found_p)
19930 {
19931 font = FRAME_FONT (it->f);
19932 boff = FRAME_BASELINE_OFFSET (it->f);
19933 font_info = NULL;
19934 }
19935 else
19936 {
19937 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
19938 boff = font_info->baseline_offset;
19939 if (font_info->vertical_centering)
19940 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
19941 }
19942
19943 if (it->char_to_display >= ' '
19944 && (!it->multibyte_p || it->char_to_display < 128))
19945 {
19946 /* Either unibyte or ASCII. */
19947 int stretched_p;
19948
19949 it->nglyphs = 1;
19950
19951 pcm = rif->per_char_metric (font, &char2b,
19952 FONT_TYPE_FOR_UNIBYTE (font, it->char_to_display));
19953
19954 if (it->override_ascent >= 0)
19955 {
19956 it->ascent = it->override_ascent;
19957 it->descent = it->override_descent;
19958 boff = it->override_boff;
19959 }
19960 else
19961 {
19962 it->ascent = FONT_BASE (font) + boff;
19963 it->descent = FONT_DESCENT (font) - boff;
19964 }
19965
19966 if (pcm)
19967 {
19968 it->phys_ascent = pcm->ascent + boff;
19969 it->phys_descent = pcm->descent - boff;
19970 it->pixel_width = pcm->width;
19971 }
19972 else
19973 {
19974 it->glyph_not_available_p = 1;
19975 it->phys_ascent = it->ascent;
19976 it->phys_descent = it->descent;
19977 it->pixel_width = FONT_WIDTH (font);
19978 }
19979
19980 if (it->constrain_row_ascent_descent_p)
19981 {
19982 if (it->descent > it->max_descent)
19983 {
19984 it->ascent += it->descent - it->max_descent;
19985 it->descent = it->max_descent;
19986 }
19987 if (it->ascent > it->max_ascent)
19988 {
19989 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
19990 it->ascent = it->max_ascent;
19991 }
19992 it->phys_ascent = min (it->phys_ascent, it->ascent);
19993 it->phys_descent = min (it->phys_descent, it->descent);
19994 extra_line_spacing = 0;
19995 }
19996
19997 /* If this is a space inside a region of text with
19998 `space-width' property, change its width. */
19999 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
20000 if (stretched_p)
20001 it->pixel_width *= XFLOATINT (it->space_width);
20002
20003 /* If face has a box, add the box thickness to the character
20004 height. If character has a box line to the left and/or
20005 right, add the box line width to the character's width. */
20006 if (face->box != FACE_NO_BOX)
20007 {
20008 int thick = face->box_line_width;
20009
20010 if (thick > 0)
20011 {
20012 it->ascent += thick;
20013 it->descent += thick;
20014 }
20015 else
20016 thick = -thick;
20017
20018 if (it->start_of_box_run_p)
20019 it->pixel_width += thick;
20020 if (it->end_of_box_run_p)
20021 it->pixel_width += thick;
20022 }
20023
20024 /* If face has an overline, add the height of the overline
20025 (1 pixel) and a 1 pixel margin to the character height. */
20026 if (face->overline_p)
20027 it->ascent += 2;
20028
20029 if (it->constrain_row_ascent_descent_p)
20030 {
20031 if (it->ascent > it->max_ascent)
20032 it->ascent = it->max_ascent;
20033 if (it->descent > it->max_descent)
20034 it->descent = it->max_descent;
20035 }
20036
20037 take_vertical_position_into_account (it);
20038
20039 /* If we have to actually produce glyphs, do it. */
20040 if (it->glyph_row)
20041 {
20042 if (stretched_p)
20043 {
20044 /* Translate a space with a `space-width' property
20045 into a stretch glyph. */
20046 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
20047 / FONT_HEIGHT (font));
20048 append_stretch_glyph (it, it->object, it->pixel_width,
20049 it->ascent + it->descent, ascent);
20050 }
20051 else
20052 append_glyph (it);
20053
20054 /* If characters with lbearing or rbearing are displayed
20055 in this line, record that fact in a flag of the
20056 glyph row. This is used to optimize X output code. */
20057 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
20058 it->glyph_row->contains_overlapping_glyphs_p = 1;
20059 }
20060 }
20061 else if (it->char_to_display == '\n')
20062 {
20063 /* A newline has no width but we need the height of the line.
20064 But if previous part of the line set a height, don't
20065 increase that height */
20066
20067 Lisp_Object height;
20068 Lisp_Object total_height = Qnil;
20069
20070 it->override_ascent = -1;
20071 it->pixel_width = 0;
20072 it->nglyphs = 0;
20073
20074 height = get_line_height_property(it, Qline_height);
20075 /* Split (line-height total-height) list */
20076 if (CONSP (height)
20077 && CONSP (XCDR (height))
20078 && NILP (XCDR (XCDR (height))))
20079 {
20080 total_height = XCAR (XCDR (height));
20081 height = XCAR (height);
20082 }
20083 height = calc_line_height_property(it, height, font, boff, 1);
20084
20085 if (it->override_ascent >= 0)
20086 {
20087 it->ascent = it->override_ascent;
20088 it->descent = it->override_descent;
20089 boff = it->override_boff;
20090 }
20091 else
20092 {
20093 it->ascent = FONT_BASE (font) + boff;
20094 it->descent = FONT_DESCENT (font) - boff;
20095 }
20096
20097 if (EQ (height, Qt))
20098 {
20099 if (it->descent > it->max_descent)
20100 {
20101 it->ascent += it->descent - it->max_descent;
20102 it->descent = it->max_descent;
20103 }
20104 if (it->ascent > it->max_ascent)
20105 {
20106 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
20107 it->ascent = it->max_ascent;
20108 }
20109 it->phys_ascent = min (it->phys_ascent, it->ascent);
20110 it->phys_descent = min (it->phys_descent, it->descent);
20111 it->constrain_row_ascent_descent_p = 1;
20112 extra_line_spacing = 0;
20113 }
20114 else
20115 {
20116 Lisp_Object spacing;
20117
20118 it->phys_ascent = it->ascent;
20119 it->phys_descent = it->descent;
20120
20121 if ((it->max_ascent > 0 || it->max_descent > 0)
20122 && face->box != FACE_NO_BOX
20123 && face->box_line_width > 0)
20124 {
20125 it->ascent += face->box_line_width;
20126 it->descent += face->box_line_width;
20127 }
20128 if (!NILP (height)
20129 && XINT (height) > it->ascent + it->descent)
20130 it->ascent = XINT (height) - it->descent;
20131
20132 if (!NILP (total_height))
20133 spacing = calc_line_height_property(it, total_height, font, boff, 0);
20134 else
20135 {
20136 spacing = get_line_height_property(it, Qline_spacing);
20137 spacing = calc_line_height_property(it, spacing, font, boff, 0);
20138 }
20139 if (INTEGERP (spacing))
20140 {
20141 extra_line_spacing = XINT (spacing);
20142 if (!NILP (total_height))
20143 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
20144 }
20145 }
20146 }
20147 else if (it->char_to_display == '\t')
20148 {
20149 int tab_width = it->tab_width * FRAME_SPACE_WIDTH (it->f);
20150 int x = it->current_x + it->continuation_lines_width;
20151 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
20152
20153 /* If the distance from the current position to the next tab
20154 stop is less than a space character width, use the
20155 tab stop after that. */
20156 if (next_tab_x - x < FRAME_SPACE_WIDTH (it->f))
20157 next_tab_x += tab_width;
20158
20159 it->pixel_width = next_tab_x - x;
20160 it->nglyphs = 1;
20161 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
20162 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
20163
20164 if (it->glyph_row)
20165 {
20166 append_stretch_glyph (it, it->object, it->pixel_width,
20167 it->ascent + it->descent, it->ascent);
20168 }
20169 }
20170 else
20171 {
20172 /* A multi-byte character. Assume that the display width of the
20173 character is the width of the character multiplied by the
20174 width of the font. */
20175
20176 /* If we found a font, this font should give us the right
20177 metrics. If we didn't find a font, use the frame's
20178 default font and calculate the width of the character
20179 from the charset width; this is what old redisplay code
20180 did. */
20181
20182 pcm = rif->per_char_metric (font, &char2b,
20183 FONT_TYPE_FOR_MULTIBYTE (font, it->c));
20184
20185 if (font_not_found_p || !pcm)
20186 {
20187 int charset = CHAR_CHARSET (it->char_to_display);
20188
20189 it->glyph_not_available_p = 1;
20190 it->pixel_width = (FRAME_COLUMN_WIDTH (it->f)
20191 * CHARSET_WIDTH (charset));
20192 it->phys_ascent = FONT_BASE (font) + boff;
20193 it->phys_descent = FONT_DESCENT (font) - boff;
20194 }
20195 else
20196 {
20197 it->pixel_width = pcm->width;
20198 it->phys_ascent = pcm->ascent + boff;
20199 it->phys_descent = pcm->descent - boff;
20200 if (it->glyph_row
20201 && (pcm->lbearing < 0
20202 || pcm->rbearing > pcm->width))
20203 it->glyph_row->contains_overlapping_glyphs_p = 1;
20204 }
20205 it->nglyphs = 1;
20206 it->ascent = FONT_BASE (font) + boff;
20207 it->descent = FONT_DESCENT (font) - boff;
20208 if (face->box != FACE_NO_BOX)
20209 {
20210 int thick = face->box_line_width;
20211
20212 if (thick > 0)
20213 {
20214 it->ascent += thick;
20215 it->descent += thick;
20216 }
20217 else
20218 thick = - thick;
20219
20220 if (it->start_of_box_run_p)
20221 it->pixel_width += thick;
20222 if (it->end_of_box_run_p)
20223 it->pixel_width += thick;
20224 }
20225
20226 /* If face has an overline, add the height of the overline
20227 (1 pixel) and a 1 pixel margin to the character height. */
20228 if (face->overline_p)
20229 it->ascent += 2;
20230
20231 take_vertical_position_into_account (it);
20232
20233 if (it->glyph_row)
20234 append_glyph (it);
20235 }
20236 it->multibyte_p = saved_multibyte_p;
20237 }
20238 else if (it->what == IT_COMPOSITION)
20239 {
20240 /* Note: A composition is represented as one glyph in the
20241 glyph matrix. There are no padding glyphs. */
20242 XChar2b char2b;
20243 XFontStruct *font;
20244 struct face *face = FACE_FROM_ID (it->f, it->face_id);
20245 XCharStruct *pcm;
20246 int font_not_found_p;
20247 struct font_info *font_info;
20248 int boff; /* baseline offset */
20249 struct composition *cmp = composition_table[it->cmp_id];
20250
20251 /* Maybe translate single-byte characters to multibyte. */
20252 it->char_to_display = it->c;
20253 if (unibyte_display_via_language_environment
20254 && SINGLE_BYTE_CHAR_P (it->c)
20255 && (it->c >= 0240
20256 || (it->c >= 0200
20257 && !NILP (Vnonascii_translation_table))))
20258 {
20259 it->char_to_display = unibyte_char_to_multibyte (it->c);
20260 }
20261
20262 /* Get face and font to use. Encode IT->char_to_display. */
20263 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
20264 face = FACE_FROM_ID (it->f, it->face_id);
20265 get_char_face_and_encoding (it->f, it->char_to_display, it->face_id,
20266 &char2b, it->multibyte_p, 0);
20267 font = face->font;
20268
20269 /* When no suitable font found, use the default font. */
20270 font_not_found_p = font == NULL;
20271 if (font_not_found_p)
20272 {
20273 font = FRAME_FONT (it->f);
20274 boff = FRAME_BASELINE_OFFSET (it->f);
20275 font_info = NULL;
20276 }
20277 else
20278 {
20279 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
20280 boff = font_info->baseline_offset;
20281 if (font_info->vertical_centering)
20282 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
20283 }
20284
20285 /* There are no padding glyphs, so there is only one glyph to
20286 produce for the composition. Important is that pixel_width,
20287 ascent and descent are the values of what is drawn by
20288 draw_glyphs (i.e. the values of the overall glyphs composed). */
20289 it->nglyphs = 1;
20290
20291 /* If we have not yet calculated pixel size data of glyphs of
20292 the composition for the current face font, calculate them
20293 now. Theoretically, we have to check all fonts for the
20294 glyphs, but that requires much time and memory space. So,
20295 here we check only the font of the first glyph. This leads
20296 to incorrect display very rarely, and C-l (recenter) can
20297 correct the display anyway. */
20298 if (cmp->font != (void *) font)
20299 {
20300 /* Ascent and descent of the font of the first character of
20301 this composition (adjusted by baseline offset). Ascent
20302 and descent of overall glyphs should not be less than
20303 them respectively. */
20304 int font_ascent = FONT_BASE (font) + boff;
20305 int font_descent = FONT_DESCENT (font) - boff;
20306 /* Bounding box of the overall glyphs. */
20307 int leftmost, rightmost, lowest, highest;
20308 int i, width, ascent, descent;
20309
20310 cmp->font = (void *) font;
20311
20312 /* Initialize the bounding box. */
20313 if (font_info
20314 && (pcm = rif->per_char_metric (font, &char2b,
20315 FONT_TYPE_FOR_MULTIBYTE (font, it->c))))
20316 {
20317 width = pcm->width;
20318 ascent = pcm->ascent;
20319 descent = pcm->descent;
20320 }
20321 else
20322 {
20323 width = FONT_WIDTH (font);
20324 ascent = FONT_BASE (font);
20325 descent = FONT_DESCENT (font);
20326 }
20327
20328 rightmost = width;
20329 lowest = - descent + boff;
20330 highest = ascent + boff;
20331 leftmost = 0;
20332
20333 if (font_info
20334 && font_info->default_ascent
20335 && CHAR_TABLE_P (Vuse_default_ascent)
20336 && !NILP (Faref (Vuse_default_ascent,
20337 make_number (it->char_to_display))))
20338 highest = font_info->default_ascent + boff;
20339
20340 /* Draw the first glyph at the normal position. It may be
20341 shifted to right later if some other glyphs are drawn at
20342 the left. */
20343 cmp->offsets[0] = 0;
20344 cmp->offsets[1] = boff;
20345
20346 /* Set cmp->offsets for the remaining glyphs. */
20347 for (i = 1; i < cmp->glyph_len; i++)
20348 {
20349 int left, right, btm, top;
20350 int ch = COMPOSITION_GLYPH (cmp, i);
20351 int face_id = FACE_FOR_CHAR (it->f, face, ch);
20352
20353 face = FACE_FROM_ID (it->f, face_id);
20354 get_char_face_and_encoding (it->f, ch, face->id,
20355 &char2b, it->multibyte_p, 0);
20356 font = face->font;
20357 if (font == NULL)
20358 {
20359 font = FRAME_FONT (it->f);
20360 boff = FRAME_BASELINE_OFFSET (it->f);
20361 font_info = NULL;
20362 }
20363 else
20364 {
20365 font_info
20366 = FONT_INFO_FROM_ID (it->f, face->font_info_id);
20367 boff = font_info->baseline_offset;
20368 if (font_info->vertical_centering)
20369 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
20370 }
20371
20372 if (font_info
20373 && (pcm = rif->per_char_metric (font, &char2b,
20374 FONT_TYPE_FOR_MULTIBYTE (font, ch))))
20375 {
20376 width = pcm->width;
20377 ascent = pcm->ascent;
20378 descent = pcm->descent;
20379 }
20380 else
20381 {
20382 width = FONT_WIDTH (font);
20383 ascent = 1;
20384 descent = 0;
20385 }
20386
20387 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
20388 {
20389 /* Relative composition with or without
20390 alternate chars. */
20391 left = (leftmost + rightmost - width) / 2;
20392 btm = - descent + boff;
20393 if (font_info && font_info->relative_compose
20394 && (! CHAR_TABLE_P (Vignore_relative_composition)
20395 || NILP (Faref (Vignore_relative_composition,
20396 make_number (ch)))))
20397 {
20398
20399 if (- descent >= font_info->relative_compose)
20400 /* One extra pixel between two glyphs. */
20401 btm = highest + 1;
20402 else if (ascent <= 0)
20403 /* One extra pixel between two glyphs. */
20404 btm = lowest - 1 - ascent - descent;
20405 }
20406 }
20407 else
20408 {
20409 /* A composition rule is specified by an integer
20410 value that encodes global and new reference
20411 points (GREF and NREF). GREF and NREF are
20412 specified by numbers as below:
20413
20414 0---1---2 -- ascent
20415 | |
20416 | |
20417 | |
20418 9--10--11 -- center
20419 | |
20420 ---3---4---5--- baseline
20421 | |
20422 6---7---8 -- descent
20423 */
20424 int rule = COMPOSITION_RULE (cmp, i);
20425 int gref, nref, grefx, grefy, nrefx, nrefy;
20426
20427 COMPOSITION_DECODE_RULE (rule, gref, nref);
20428 grefx = gref % 3, nrefx = nref % 3;
20429 grefy = gref / 3, nrefy = nref / 3;
20430
20431 left = (leftmost
20432 + grefx * (rightmost - leftmost) / 2
20433 - nrefx * width / 2);
20434 btm = ((grefy == 0 ? highest
20435 : grefy == 1 ? 0
20436 : grefy == 2 ? lowest
20437 : (highest + lowest) / 2)
20438 - (nrefy == 0 ? ascent + descent
20439 : nrefy == 1 ? descent - boff
20440 : nrefy == 2 ? 0
20441 : (ascent + descent) / 2));
20442 }
20443
20444 cmp->offsets[i * 2] = left;
20445 cmp->offsets[i * 2 + 1] = btm + descent;
20446
20447 /* Update the bounding box of the overall glyphs. */
20448 right = left + width;
20449 top = btm + descent + ascent;
20450 if (left < leftmost)
20451 leftmost = left;
20452 if (right > rightmost)
20453 rightmost = right;
20454 if (top > highest)
20455 highest = top;
20456 if (btm < lowest)
20457 lowest = btm;
20458 }
20459
20460 /* If there are glyphs whose x-offsets are negative,
20461 shift all glyphs to the right and make all x-offsets
20462 non-negative. */
20463 if (leftmost < 0)
20464 {
20465 for (i = 0; i < cmp->glyph_len; i++)
20466 cmp->offsets[i * 2] -= leftmost;
20467 rightmost -= leftmost;
20468 }
20469
20470 cmp->pixel_width = rightmost;
20471 cmp->ascent = highest;
20472 cmp->descent = - lowest;
20473 if (cmp->ascent < font_ascent)
20474 cmp->ascent = font_ascent;
20475 if (cmp->descent < font_descent)
20476 cmp->descent = font_descent;
20477 }
20478
20479 it->pixel_width = cmp->pixel_width;
20480 it->ascent = it->phys_ascent = cmp->ascent;
20481 it->descent = it->phys_descent = cmp->descent;
20482
20483 if (face->box != FACE_NO_BOX)
20484 {
20485 int thick = face->box_line_width;
20486
20487 if (thick > 0)
20488 {
20489 it->ascent += thick;
20490 it->descent += thick;
20491 }
20492 else
20493 thick = - thick;
20494
20495 if (it->start_of_box_run_p)
20496 it->pixel_width += thick;
20497 if (it->end_of_box_run_p)
20498 it->pixel_width += thick;
20499 }
20500
20501 /* If face has an overline, add the height of the overline
20502 (1 pixel) and a 1 pixel margin to the character height. */
20503 if (face->overline_p)
20504 it->ascent += 2;
20505
20506 take_vertical_position_into_account (it);
20507
20508 if (it->glyph_row)
20509 append_composite_glyph (it);
20510 }
20511 else if (it->what == IT_IMAGE)
20512 produce_image_glyph (it);
20513 else if (it->what == IT_STRETCH)
20514 produce_stretch_glyph (it);
20515
20516 /* Accumulate dimensions. Note: can't assume that it->descent > 0
20517 because this isn't true for images with `:ascent 100'. */
20518 xassert (it->ascent >= 0 && it->descent >= 0);
20519 if (it->area == TEXT_AREA)
20520 it->current_x += it->pixel_width;
20521
20522 if (extra_line_spacing > 0)
20523 {
20524 it->descent += extra_line_spacing;
20525 if (extra_line_spacing > it->max_extra_line_spacing)
20526 it->max_extra_line_spacing = extra_line_spacing;
20527 }
20528
20529 it->max_ascent = max (it->max_ascent, it->ascent);
20530 it->max_descent = max (it->max_descent, it->descent);
20531 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
20532 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
20533 }
20534
20535 /* EXPORT for RIF:
20536 Output LEN glyphs starting at START at the nominal cursor position.
20537 Advance the nominal cursor over the text. The global variable
20538 updated_window contains the window being updated, updated_row is
20539 the glyph row being updated, and updated_area is the area of that
20540 row being updated. */
20541
20542 void
20543 x_write_glyphs (start, len)
20544 struct glyph *start;
20545 int len;
20546 {
20547 int x, hpos;
20548
20549 xassert (updated_window && updated_row);
20550 BLOCK_INPUT;
20551
20552 /* Write glyphs. */
20553
20554 hpos = start - updated_row->glyphs[updated_area];
20555 x = draw_glyphs (updated_window, output_cursor.x,
20556 updated_row, updated_area,
20557 hpos, hpos + len,
20558 DRAW_NORMAL_TEXT, 0);
20559
20560 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
20561 if (updated_area == TEXT_AREA
20562 && updated_window->phys_cursor_on_p
20563 && updated_window->phys_cursor.vpos == output_cursor.vpos
20564 && updated_window->phys_cursor.hpos >= hpos
20565 && updated_window->phys_cursor.hpos < hpos + len)
20566 updated_window->phys_cursor_on_p = 0;
20567
20568 UNBLOCK_INPUT;
20569
20570 /* Advance the output cursor. */
20571 output_cursor.hpos += len;
20572 output_cursor.x = x;
20573 }
20574
20575
20576 /* EXPORT for RIF:
20577 Insert LEN glyphs from START at the nominal cursor position. */
20578
20579 void
20580 x_insert_glyphs (start, len)
20581 struct glyph *start;
20582 int len;
20583 {
20584 struct frame *f;
20585 struct window *w;
20586 int line_height, shift_by_width, shifted_region_width;
20587 struct glyph_row *row;
20588 struct glyph *glyph;
20589 int frame_x, frame_y, hpos;
20590
20591 xassert (updated_window && updated_row);
20592 BLOCK_INPUT;
20593 w = updated_window;
20594 f = XFRAME (WINDOW_FRAME (w));
20595
20596 /* Get the height of the line we are in. */
20597 row = updated_row;
20598 line_height = row->height;
20599
20600 /* Get the width of the glyphs to insert. */
20601 shift_by_width = 0;
20602 for (glyph = start; glyph < start + len; ++glyph)
20603 shift_by_width += glyph->pixel_width;
20604
20605 /* Get the width of the region to shift right. */
20606 shifted_region_width = (window_box_width (w, updated_area)
20607 - output_cursor.x
20608 - shift_by_width);
20609
20610 /* Shift right. */
20611 frame_x = window_box_left (w, updated_area) + output_cursor.x;
20612 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
20613
20614 rif->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
20615 line_height, shift_by_width);
20616
20617 /* Write the glyphs. */
20618 hpos = start - row->glyphs[updated_area];
20619 draw_glyphs (w, output_cursor.x, row, updated_area,
20620 hpos, hpos + len,
20621 DRAW_NORMAL_TEXT, 0);
20622
20623 /* Advance the output cursor. */
20624 output_cursor.hpos += len;
20625 output_cursor.x += shift_by_width;
20626 UNBLOCK_INPUT;
20627 }
20628
20629
20630 /* EXPORT for RIF:
20631 Erase the current text line from the nominal cursor position
20632 (inclusive) to pixel column TO_X (exclusive). The idea is that
20633 everything from TO_X onward is already erased.
20634
20635 TO_X is a pixel position relative to updated_area of
20636 updated_window. TO_X == -1 means clear to the end of this area. */
20637
20638 void
20639 x_clear_end_of_line (to_x)
20640 int to_x;
20641 {
20642 struct frame *f;
20643 struct window *w = updated_window;
20644 int max_x, min_y, max_y;
20645 int from_x, from_y, to_y;
20646
20647 xassert (updated_window && updated_row);
20648 f = XFRAME (w->frame);
20649
20650 if (updated_row->full_width_p)
20651 max_x = WINDOW_TOTAL_WIDTH (w);
20652 else
20653 max_x = window_box_width (w, updated_area);
20654 max_y = window_text_bottom_y (w);
20655
20656 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
20657 of window. For TO_X > 0, truncate to end of drawing area. */
20658 if (to_x == 0)
20659 return;
20660 else if (to_x < 0)
20661 to_x = max_x;
20662 else
20663 to_x = min (to_x, max_x);
20664
20665 to_y = min (max_y, output_cursor.y + updated_row->height);
20666
20667 /* Notice if the cursor will be cleared by this operation. */
20668 if (!updated_row->full_width_p)
20669 notice_overwritten_cursor (w, updated_area,
20670 output_cursor.x, -1,
20671 updated_row->y,
20672 MATRIX_ROW_BOTTOM_Y (updated_row));
20673
20674 from_x = output_cursor.x;
20675
20676 /* Translate to frame coordinates. */
20677 if (updated_row->full_width_p)
20678 {
20679 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
20680 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
20681 }
20682 else
20683 {
20684 int area_left = window_box_left (w, updated_area);
20685 from_x += area_left;
20686 to_x += area_left;
20687 }
20688
20689 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
20690 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
20691 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
20692
20693 /* Prevent inadvertently clearing to end of the X window. */
20694 if (to_x > from_x && to_y > from_y)
20695 {
20696 BLOCK_INPUT;
20697 rif->clear_frame_area (f, from_x, from_y,
20698 to_x - from_x, to_y - from_y);
20699 UNBLOCK_INPUT;
20700 }
20701 }
20702
20703 #endif /* HAVE_WINDOW_SYSTEM */
20704
20705
20706 \f
20707 /***********************************************************************
20708 Cursor types
20709 ***********************************************************************/
20710
20711 /* Value is the internal representation of the specified cursor type
20712 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
20713 of the bar cursor. */
20714
20715 static enum text_cursor_kinds
20716 get_specified_cursor_type (arg, width)
20717 Lisp_Object arg;
20718 int *width;
20719 {
20720 enum text_cursor_kinds type;
20721
20722 if (NILP (arg))
20723 return NO_CURSOR;
20724
20725 if (EQ (arg, Qbox))
20726 return FILLED_BOX_CURSOR;
20727
20728 if (EQ (arg, Qhollow))
20729 return HOLLOW_BOX_CURSOR;
20730
20731 if (EQ (arg, Qbar))
20732 {
20733 *width = 2;
20734 return BAR_CURSOR;
20735 }
20736
20737 if (CONSP (arg)
20738 && EQ (XCAR (arg), Qbar)
20739 && INTEGERP (XCDR (arg))
20740 && XINT (XCDR (arg)) >= 0)
20741 {
20742 *width = XINT (XCDR (arg));
20743 return BAR_CURSOR;
20744 }
20745
20746 if (EQ (arg, Qhbar))
20747 {
20748 *width = 2;
20749 return HBAR_CURSOR;
20750 }
20751
20752 if (CONSP (arg)
20753 && EQ (XCAR (arg), Qhbar)
20754 && INTEGERP (XCDR (arg))
20755 && XINT (XCDR (arg)) >= 0)
20756 {
20757 *width = XINT (XCDR (arg));
20758 return HBAR_CURSOR;
20759 }
20760
20761 /* Treat anything unknown as "hollow box cursor".
20762 It was bad to signal an error; people have trouble fixing
20763 .Xdefaults with Emacs, when it has something bad in it. */
20764 type = HOLLOW_BOX_CURSOR;
20765
20766 return type;
20767 }
20768
20769 /* Set the default cursor types for specified frame. */
20770 void
20771 set_frame_cursor_types (f, arg)
20772 struct frame *f;
20773 Lisp_Object arg;
20774 {
20775 int width;
20776 Lisp_Object tem;
20777
20778 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
20779 FRAME_CURSOR_WIDTH (f) = width;
20780
20781 /* By default, set up the blink-off state depending on the on-state. */
20782
20783 tem = Fassoc (arg, Vblink_cursor_alist);
20784 if (!NILP (tem))
20785 {
20786 FRAME_BLINK_OFF_CURSOR (f)
20787 = get_specified_cursor_type (XCDR (tem), &width);
20788 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
20789 }
20790 else
20791 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
20792 }
20793
20794
20795 /* Return the cursor we want to be displayed in window W. Return
20796 width of bar/hbar cursor through WIDTH arg. Return with
20797 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
20798 (i.e. if the `system caret' should track this cursor).
20799
20800 In a mini-buffer window, we want the cursor only to appear if we
20801 are reading input from this window. For the selected window, we
20802 want the cursor type given by the frame parameter or buffer local
20803 setting of cursor-type. If explicitly marked off, draw no cursor.
20804 In all other cases, we want a hollow box cursor. */
20805
20806 static enum text_cursor_kinds
20807 get_window_cursor_type (w, glyph, width, active_cursor)
20808 struct window *w;
20809 struct glyph *glyph;
20810 int *width;
20811 int *active_cursor;
20812 {
20813 struct frame *f = XFRAME (w->frame);
20814 struct buffer *b = XBUFFER (w->buffer);
20815 int cursor_type = DEFAULT_CURSOR;
20816 Lisp_Object alt_cursor;
20817 int non_selected = 0;
20818
20819 *active_cursor = 1;
20820
20821 /* Echo area */
20822 if (cursor_in_echo_area
20823 && FRAME_HAS_MINIBUF_P (f)
20824 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
20825 {
20826 if (w == XWINDOW (echo_area_window))
20827 {
20828 if (EQ (b->cursor_type, Qt) || NILP (b->cursor_type))
20829 {
20830 *width = FRAME_CURSOR_WIDTH (f);
20831 return FRAME_DESIRED_CURSOR (f);
20832 }
20833 else
20834 return get_specified_cursor_type (b->cursor_type, width);
20835 }
20836
20837 *active_cursor = 0;
20838 non_selected = 1;
20839 }
20840
20841 /* Nonselected window or nonselected frame. */
20842 else if (w != XWINDOW (f->selected_window)
20843 #ifdef HAVE_WINDOW_SYSTEM
20844 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame
20845 #endif
20846 )
20847 {
20848 *active_cursor = 0;
20849
20850 if (MINI_WINDOW_P (w) && minibuf_level == 0)
20851 return NO_CURSOR;
20852
20853 non_selected = 1;
20854 }
20855
20856 /* Never display a cursor in a window in which cursor-type is nil. */
20857 if (NILP (b->cursor_type))
20858 return NO_CURSOR;
20859
20860 /* Use cursor-in-non-selected-windows for non-selected window or frame. */
20861 if (non_selected)
20862 {
20863 alt_cursor = b->cursor_in_non_selected_windows;
20864 return get_specified_cursor_type (alt_cursor, width);
20865 }
20866
20867 /* Get the normal cursor type for this window. */
20868 if (EQ (b->cursor_type, Qt))
20869 {
20870 cursor_type = FRAME_DESIRED_CURSOR (f);
20871 *width = FRAME_CURSOR_WIDTH (f);
20872 }
20873 else
20874 cursor_type = get_specified_cursor_type (b->cursor_type, width);
20875
20876 /* Use normal cursor if not blinked off. */
20877 if (!w->cursor_off_p)
20878 {
20879 if (glyph != NULL && glyph->type == IMAGE_GLYPH) {
20880 if (cursor_type == FILLED_BOX_CURSOR)
20881 cursor_type = HOLLOW_BOX_CURSOR;
20882 }
20883 return cursor_type;
20884 }
20885
20886 /* Cursor is blinked off, so determine how to "toggle" it. */
20887
20888 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
20889 if ((alt_cursor = Fassoc (b->cursor_type, Vblink_cursor_alist), !NILP (alt_cursor)))
20890 return get_specified_cursor_type (XCDR (alt_cursor), width);
20891
20892 /* Then see if frame has specified a specific blink off cursor type. */
20893 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
20894 {
20895 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
20896 return FRAME_BLINK_OFF_CURSOR (f);
20897 }
20898
20899 #if 0
20900 /* Some people liked having a permanently visible blinking cursor,
20901 while others had very strong opinions against it. So it was
20902 decided to remove it. KFS 2003-09-03 */
20903
20904 /* Finally perform built-in cursor blinking:
20905 filled box <-> hollow box
20906 wide [h]bar <-> narrow [h]bar
20907 narrow [h]bar <-> no cursor
20908 other type <-> no cursor */
20909
20910 if (cursor_type == FILLED_BOX_CURSOR)
20911 return HOLLOW_BOX_CURSOR;
20912
20913 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
20914 {
20915 *width = 1;
20916 return cursor_type;
20917 }
20918 #endif
20919
20920 return NO_CURSOR;
20921 }
20922
20923
20924 #ifdef HAVE_WINDOW_SYSTEM
20925
20926 /* Notice when the text cursor of window W has been completely
20927 overwritten by a drawing operation that outputs glyphs in AREA
20928 starting at X0 and ending at X1 in the line starting at Y0 and
20929 ending at Y1. X coordinates are area-relative. X1 < 0 means all
20930 the rest of the line after X0 has been written. Y coordinates
20931 are window-relative. */
20932
20933 static void
20934 notice_overwritten_cursor (w, area, x0, x1, y0, y1)
20935 struct window *w;
20936 enum glyph_row_area area;
20937 int x0, y0, x1, y1;
20938 {
20939 int cx0, cx1, cy0, cy1;
20940 struct glyph_row *row;
20941
20942 if (!w->phys_cursor_on_p)
20943 return;
20944 if (area != TEXT_AREA)
20945 return;
20946
20947 if (w->phys_cursor.vpos < 0
20948 || w->phys_cursor.vpos >= w->current_matrix->nrows
20949 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
20950 !(row->enabled_p && row->displays_text_p)))
20951 return;
20952
20953 if (row->cursor_in_fringe_p)
20954 {
20955 row->cursor_in_fringe_p = 0;
20956 draw_fringe_bitmap (w, row, 0);
20957 w->phys_cursor_on_p = 0;
20958 return;
20959 }
20960
20961 cx0 = w->phys_cursor.x;
20962 cx1 = cx0 + w->phys_cursor_width;
20963 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
20964 return;
20965
20966 /* The cursor image will be completely removed from the
20967 screen if the output area intersects the cursor area in
20968 y-direction. When we draw in [y0 y1[, and some part of
20969 the cursor is at y < y0, that part must have been drawn
20970 before. When scrolling, the cursor is erased before
20971 actually scrolling, so we don't come here. When not
20972 scrolling, the rows above the old cursor row must have
20973 changed, and in this case these rows must have written
20974 over the cursor image.
20975
20976 Likewise if part of the cursor is below y1, with the
20977 exception of the cursor being in the first blank row at
20978 the buffer and window end because update_text_area
20979 doesn't draw that row. (Except when it does, but
20980 that's handled in update_text_area.) */
20981
20982 cy0 = w->phys_cursor.y;
20983 cy1 = cy0 + w->phys_cursor_height;
20984 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
20985 return;
20986
20987 w->phys_cursor_on_p = 0;
20988 }
20989
20990 #endif /* HAVE_WINDOW_SYSTEM */
20991
20992 \f
20993 /************************************************************************
20994 Mouse Face
20995 ************************************************************************/
20996
20997 #ifdef HAVE_WINDOW_SYSTEM
20998
20999 /* EXPORT for RIF:
21000 Fix the display of area AREA of overlapping row ROW in window W
21001 with respect to the overlapping part OVERLAPS. */
21002
21003 void
21004 x_fix_overlapping_area (w, row, area, overlaps)
21005 struct window *w;
21006 struct glyph_row *row;
21007 enum glyph_row_area area;
21008 int overlaps;
21009 {
21010 int i, x;
21011
21012 BLOCK_INPUT;
21013
21014 x = 0;
21015 for (i = 0; i < row->used[area];)
21016 {
21017 if (row->glyphs[area][i].overlaps_vertically_p)
21018 {
21019 int start = i, start_x = x;
21020
21021 do
21022 {
21023 x += row->glyphs[area][i].pixel_width;
21024 ++i;
21025 }
21026 while (i < row->used[area]
21027 && row->glyphs[area][i].overlaps_vertically_p);
21028
21029 draw_glyphs (w, start_x, row, area,
21030 start, i,
21031 DRAW_NORMAL_TEXT, overlaps);
21032 }
21033 else
21034 {
21035 x += row->glyphs[area][i].pixel_width;
21036 ++i;
21037 }
21038 }
21039
21040 UNBLOCK_INPUT;
21041 }
21042
21043
21044 /* EXPORT:
21045 Draw the cursor glyph of window W in glyph row ROW. See the
21046 comment of draw_glyphs for the meaning of HL. */
21047
21048 void
21049 draw_phys_cursor_glyph (w, row, hl)
21050 struct window *w;
21051 struct glyph_row *row;
21052 enum draw_glyphs_face hl;
21053 {
21054 /* If cursor hpos is out of bounds, don't draw garbage. This can
21055 happen in mini-buffer windows when switching between echo area
21056 glyphs and mini-buffer. */
21057 if (w->phys_cursor.hpos < row->used[TEXT_AREA])
21058 {
21059 int on_p = w->phys_cursor_on_p;
21060 int x1;
21061 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA,
21062 w->phys_cursor.hpos, w->phys_cursor.hpos + 1,
21063 hl, 0);
21064 w->phys_cursor_on_p = on_p;
21065
21066 if (hl == DRAW_CURSOR)
21067 w->phys_cursor_width = x1 - w->phys_cursor.x;
21068 /* When we erase the cursor, and ROW is overlapped by other
21069 rows, make sure that these overlapping parts of other rows
21070 are redrawn. */
21071 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
21072 {
21073 w->phys_cursor_width = x1 - w->phys_cursor.x;
21074
21075 if (row > w->current_matrix->rows
21076 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
21077 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
21078 OVERLAPS_ERASED_CURSOR);
21079
21080 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
21081 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
21082 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
21083 OVERLAPS_ERASED_CURSOR);
21084 }
21085 }
21086 }
21087
21088
21089 /* EXPORT:
21090 Erase the image of a cursor of window W from the screen. */
21091
21092 void
21093 erase_phys_cursor (w)
21094 struct window *w;
21095 {
21096 struct frame *f = XFRAME (w->frame);
21097 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
21098 int hpos = w->phys_cursor.hpos;
21099 int vpos = w->phys_cursor.vpos;
21100 int mouse_face_here_p = 0;
21101 struct glyph_matrix *active_glyphs = w->current_matrix;
21102 struct glyph_row *cursor_row;
21103 struct glyph *cursor_glyph;
21104 enum draw_glyphs_face hl;
21105
21106 /* No cursor displayed or row invalidated => nothing to do on the
21107 screen. */
21108 if (w->phys_cursor_type == NO_CURSOR)
21109 goto mark_cursor_off;
21110
21111 /* VPOS >= active_glyphs->nrows means that window has been resized.
21112 Don't bother to erase the cursor. */
21113 if (vpos >= active_glyphs->nrows)
21114 goto mark_cursor_off;
21115
21116 /* If row containing cursor is marked invalid, there is nothing we
21117 can do. */
21118 cursor_row = MATRIX_ROW (active_glyphs, vpos);
21119 if (!cursor_row->enabled_p)
21120 goto mark_cursor_off;
21121
21122 /* If line spacing is > 0, old cursor may only be partially visible in
21123 window after split-window. So adjust visible height. */
21124 cursor_row->visible_height = min (cursor_row->visible_height,
21125 window_text_bottom_y (w) - cursor_row->y);
21126
21127 /* If row is completely invisible, don't attempt to delete a cursor which
21128 isn't there. This can happen if cursor is at top of a window, and
21129 we switch to a buffer with a header line in that window. */
21130 if (cursor_row->visible_height <= 0)
21131 goto mark_cursor_off;
21132
21133 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
21134 if (cursor_row->cursor_in_fringe_p)
21135 {
21136 cursor_row->cursor_in_fringe_p = 0;
21137 draw_fringe_bitmap (w, cursor_row, 0);
21138 goto mark_cursor_off;
21139 }
21140
21141 /* This can happen when the new row is shorter than the old one.
21142 In this case, either draw_glyphs or clear_end_of_line
21143 should have cleared the cursor. Note that we wouldn't be
21144 able to erase the cursor in this case because we don't have a
21145 cursor glyph at hand. */
21146 if (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])
21147 goto mark_cursor_off;
21148
21149 /* If the cursor is in the mouse face area, redisplay that when
21150 we clear the cursor. */
21151 if (! NILP (dpyinfo->mouse_face_window)
21152 && w == XWINDOW (dpyinfo->mouse_face_window)
21153 && (vpos > dpyinfo->mouse_face_beg_row
21154 || (vpos == dpyinfo->mouse_face_beg_row
21155 && hpos >= dpyinfo->mouse_face_beg_col))
21156 && (vpos < dpyinfo->mouse_face_end_row
21157 || (vpos == dpyinfo->mouse_face_end_row
21158 && hpos < dpyinfo->mouse_face_end_col))
21159 /* Don't redraw the cursor's spot in mouse face if it is at the
21160 end of a line (on a newline). The cursor appears there, but
21161 mouse highlighting does not. */
21162 && cursor_row->used[TEXT_AREA] > hpos)
21163 mouse_face_here_p = 1;
21164
21165 /* Maybe clear the display under the cursor. */
21166 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
21167 {
21168 int x, y;
21169 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
21170 int width;
21171
21172 cursor_glyph = get_phys_cursor_glyph (w);
21173 if (cursor_glyph == NULL)
21174 goto mark_cursor_off;
21175
21176 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
21177 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
21178 width = min (cursor_glyph->pixel_width,
21179 window_box_width (w, TEXT_AREA) - w->phys_cursor.x);
21180
21181 rif->clear_frame_area (f, x, y, width, cursor_row->visible_height);
21182 }
21183
21184 /* Erase the cursor by redrawing the character underneath it. */
21185 if (mouse_face_here_p)
21186 hl = DRAW_MOUSE_FACE;
21187 else
21188 hl = DRAW_NORMAL_TEXT;
21189 draw_phys_cursor_glyph (w, cursor_row, hl);
21190
21191 mark_cursor_off:
21192 w->phys_cursor_on_p = 0;
21193 w->phys_cursor_type = NO_CURSOR;
21194 }
21195
21196
21197 /* EXPORT:
21198 Display or clear cursor of window W. If ON is zero, clear the
21199 cursor. If it is non-zero, display the cursor. If ON is nonzero,
21200 where to put the cursor is specified by HPOS, VPOS, X and Y. */
21201
21202 void
21203 display_and_set_cursor (w, on, hpos, vpos, x, y)
21204 struct window *w;
21205 int on, hpos, vpos, x, y;
21206 {
21207 struct frame *f = XFRAME (w->frame);
21208 int new_cursor_type;
21209 int new_cursor_width;
21210 int active_cursor;
21211 struct glyph_row *glyph_row;
21212 struct glyph *glyph;
21213
21214 /* This is pointless on invisible frames, and dangerous on garbaged
21215 windows and frames; in the latter case, the frame or window may
21216 be in the midst of changing its size, and x and y may be off the
21217 window. */
21218 if (! FRAME_VISIBLE_P (f)
21219 || FRAME_GARBAGED_P (f)
21220 || vpos >= w->current_matrix->nrows
21221 || hpos >= w->current_matrix->matrix_w)
21222 return;
21223
21224 /* If cursor is off and we want it off, return quickly. */
21225 if (!on && !w->phys_cursor_on_p)
21226 return;
21227
21228 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
21229 /* If cursor row is not enabled, we don't really know where to
21230 display the cursor. */
21231 if (!glyph_row->enabled_p)
21232 {
21233 w->phys_cursor_on_p = 0;
21234 return;
21235 }
21236
21237 glyph = NULL;
21238 if (!glyph_row->exact_window_width_line_p
21239 || hpos < glyph_row->used[TEXT_AREA])
21240 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
21241
21242 xassert (interrupt_input_blocked);
21243
21244 /* Set new_cursor_type to the cursor we want to be displayed. */
21245 new_cursor_type = get_window_cursor_type (w, glyph,
21246 &new_cursor_width, &active_cursor);
21247
21248 /* If cursor is currently being shown and we don't want it to be or
21249 it is in the wrong place, or the cursor type is not what we want,
21250 erase it. */
21251 if (w->phys_cursor_on_p
21252 && (!on
21253 || w->phys_cursor.x != x
21254 || w->phys_cursor.y != y
21255 || new_cursor_type != w->phys_cursor_type
21256 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
21257 && new_cursor_width != w->phys_cursor_width)))
21258 erase_phys_cursor (w);
21259
21260 /* Don't check phys_cursor_on_p here because that flag is only set
21261 to zero in some cases where we know that the cursor has been
21262 completely erased, to avoid the extra work of erasing the cursor
21263 twice. In other words, phys_cursor_on_p can be 1 and the cursor
21264 still not be visible, or it has only been partly erased. */
21265 if (on)
21266 {
21267 w->phys_cursor_ascent = glyph_row->ascent;
21268 w->phys_cursor_height = glyph_row->height;
21269
21270 /* Set phys_cursor_.* before x_draw_.* is called because some
21271 of them may need the information. */
21272 w->phys_cursor.x = x;
21273 w->phys_cursor.y = glyph_row->y;
21274 w->phys_cursor.hpos = hpos;
21275 w->phys_cursor.vpos = vpos;
21276 }
21277
21278 rif->draw_window_cursor (w, glyph_row, x, y,
21279 new_cursor_type, new_cursor_width,
21280 on, active_cursor);
21281 }
21282
21283
21284 /* Switch the display of W's cursor on or off, according to the value
21285 of ON. */
21286
21287 static void
21288 update_window_cursor (w, on)
21289 struct window *w;
21290 int on;
21291 {
21292 /* Don't update cursor in windows whose frame is in the process
21293 of being deleted. */
21294 if (w->current_matrix)
21295 {
21296 BLOCK_INPUT;
21297 display_and_set_cursor (w, on, w->phys_cursor.hpos, w->phys_cursor.vpos,
21298 w->phys_cursor.x, w->phys_cursor.y);
21299 UNBLOCK_INPUT;
21300 }
21301 }
21302
21303
21304 /* Call update_window_cursor with parameter ON_P on all leaf windows
21305 in the window tree rooted at W. */
21306
21307 static void
21308 update_cursor_in_window_tree (w, on_p)
21309 struct window *w;
21310 int on_p;
21311 {
21312 while (w)
21313 {
21314 if (!NILP (w->hchild))
21315 update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
21316 else if (!NILP (w->vchild))
21317 update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
21318 else
21319 update_window_cursor (w, on_p);
21320
21321 w = NILP (w->next) ? 0 : XWINDOW (w->next);
21322 }
21323 }
21324
21325
21326 /* EXPORT:
21327 Display the cursor on window W, or clear it, according to ON_P.
21328 Don't change the cursor's position. */
21329
21330 void
21331 x_update_cursor (f, on_p)
21332 struct frame *f;
21333 int on_p;
21334 {
21335 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
21336 }
21337
21338
21339 /* EXPORT:
21340 Clear the cursor of window W to background color, and mark the
21341 cursor as not shown. This is used when the text where the cursor
21342 is is about to be rewritten. */
21343
21344 void
21345 x_clear_cursor (w)
21346 struct window *w;
21347 {
21348 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
21349 update_window_cursor (w, 0);
21350 }
21351
21352
21353 /* EXPORT:
21354 Display the active region described by mouse_face_* according to DRAW. */
21355
21356 void
21357 show_mouse_face (dpyinfo, draw)
21358 Display_Info *dpyinfo;
21359 enum draw_glyphs_face draw;
21360 {
21361 struct window *w = XWINDOW (dpyinfo->mouse_face_window);
21362 struct frame *f = XFRAME (WINDOW_FRAME (w));
21363
21364 if (/* If window is in the process of being destroyed, don't bother
21365 to do anything. */
21366 w->current_matrix != NULL
21367 /* Don't update mouse highlight if hidden */
21368 && (draw != DRAW_MOUSE_FACE || !dpyinfo->mouse_face_hidden)
21369 /* Recognize when we are called to operate on rows that don't exist
21370 anymore. This can happen when a window is split. */
21371 && dpyinfo->mouse_face_end_row < w->current_matrix->nrows)
21372 {
21373 int phys_cursor_on_p = w->phys_cursor_on_p;
21374 struct glyph_row *row, *first, *last;
21375
21376 first = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_beg_row);
21377 last = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_end_row);
21378
21379 for (row = first; row <= last && row->enabled_p; ++row)
21380 {
21381 int start_hpos, end_hpos, start_x;
21382
21383 /* For all but the first row, the highlight starts at column 0. */
21384 if (row == first)
21385 {
21386 start_hpos = dpyinfo->mouse_face_beg_col;
21387 start_x = dpyinfo->mouse_face_beg_x;
21388 }
21389 else
21390 {
21391 start_hpos = 0;
21392 start_x = 0;
21393 }
21394
21395 if (row == last)
21396 end_hpos = dpyinfo->mouse_face_end_col;
21397 else
21398 {
21399 end_hpos = row->used[TEXT_AREA];
21400 if (draw == DRAW_NORMAL_TEXT)
21401 row->fill_line_p = 1; /* Clear to end of line */
21402 }
21403
21404 if (end_hpos > start_hpos)
21405 {
21406 draw_glyphs (w, start_x, row, TEXT_AREA,
21407 start_hpos, end_hpos,
21408 draw, 0);
21409
21410 row->mouse_face_p
21411 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
21412 }
21413 }
21414
21415 /* When we've written over the cursor, arrange for it to
21416 be displayed again. */
21417 if (phys_cursor_on_p && !w->phys_cursor_on_p)
21418 {
21419 BLOCK_INPUT;
21420 display_and_set_cursor (w, 1,
21421 w->phys_cursor.hpos, w->phys_cursor.vpos,
21422 w->phys_cursor.x, w->phys_cursor.y);
21423 UNBLOCK_INPUT;
21424 }
21425 }
21426
21427 /* Change the mouse cursor. */
21428 if (draw == DRAW_NORMAL_TEXT)
21429 rif->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
21430 else if (draw == DRAW_MOUSE_FACE)
21431 rif->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
21432 else
21433 rif->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
21434 }
21435
21436 /* EXPORT:
21437 Clear out the mouse-highlighted active region.
21438 Redraw it un-highlighted first. Value is non-zero if mouse
21439 face was actually drawn unhighlighted. */
21440
21441 int
21442 clear_mouse_face (dpyinfo)
21443 Display_Info *dpyinfo;
21444 {
21445 int cleared = 0;
21446
21447 if (!dpyinfo->mouse_face_hidden && !NILP (dpyinfo->mouse_face_window))
21448 {
21449 show_mouse_face (dpyinfo, DRAW_NORMAL_TEXT);
21450 cleared = 1;
21451 }
21452
21453 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
21454 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
21455 dpyinfo->mouse_face_window = Qnil;
21456 dpyinfo->mouse_face_overlay = Qnil;
21457 return cleared;
21458 }
21459
21460
21461 /* EXPORT:
21462 Non-zero if physical cursor of window W is within mouse face. */
21463
21464 int
21465 cursor_in_mouse_face_p (w)
21466 struct window *w;
21467 {
21468 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
21469 int in_mouse_face = 0;
21470
21471 if (WINDOWP (dpyinfo->mouse_face_window)
21472 && XWINDOW (dpyinfo->mouse_face_window) == w)
21473 {
21474 int hpos = w->phys_cursor.hpos;
21475 int vpos = w->phys_cursor.vpos;
21476
21477 if (vpos >= dpyinfo->mouse_face_beg_row
21478 && vpos <= dpyinfo->mouse_face_end_row
21479 && (vpos > dpyinfo->mouse_face_beg_row
21480 || hpos >= dpyinfo->mouse_face_beg_col)
21481 && (vpos < dpyinfo->mouse_face_end_row
21482 || hpos < dpyinfo->mouse_face_end_col
21483 || dpyinfo->mouse_face_past_end))
21484 in_mouse_face = 1;
21485 }
21486
21487 return in_mouse_face;
21488 }
21489
21490
21491
21492 \f
21493 /* Find the glyph matrix position of buffer position CHARPOS in window
21494 *W. HPOS, *VPOS, *X, and *Y are set to the positions found. W's
21495 current glyphs must be up to date. If CHARPOS is above window
21496 start return (0, 0, 0, 0). If CHARPOS is after end of W, return end
21497 of last line in W. In the row containing CHARPOS, stop before glyphs
21498 having STOP as object. */
21499
21500 #if 1 /* This is a version of fast_find_position that's more correct
21501 in the presence of hscrolling, for example. I didn't install
21502 it right away because the problem fixed is minor, it failed
21503 in 20.x as well, and I think it's too risky to install
21504 so near the release of 21.1. 2001-09-25 gerd. */
21505
21506 static int
21507 fast_find_position (w, charpos, hpos, vpos, x, y, stop)
21508 struct window *w;
21509 int charpos;
21510 int *hpos, *vpos, *x, *y;
21511 Lisp_Object stop;
21512 {
21513 struct glyph_row *row, *first;
21514 struct glyph *glyph, *end;
21515 int past_end = 0;
21516
21517 first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
21518 if (charpos < MATRIX_ROW_START_CHARPOS (first))
21519 {
21520 *x = first->x;
21521 *y = first->y;
21522 *hpos = 0;
21523 *vpos = MATRIX_ROW_VPOS (first, w->current_matrix);
21524 return 1;
21525 }
21526
21527 row = row_containing_pos (w, charpos, first, NULL, 0);
21528 if (row == NULL)
21529 {
21530 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
21531 past_end = 1;
21532 }
21533
21534 /* If whole rows or last part of a row came from a display overlay,
21535 row_containing_pos will skip over such rows because their end pos
21536 equals the start pos of the overlay or interval.
21537
21538 Move back if we have a STOP object and previous row's
21539 end glyph came from STOP. */
21540 if (!NILP (stop))
21541 {
21542 struct glyph_row *prev;
21543 while ((prev = row - 1, prev >= first)
21544 && MATRIX_ROW_END_CHARPOS (prev) == charpos
21545 && prev->used[TEXT_AREA] > 0)
21546 {
21547 struct glyph *beg = prev->glyphs[TEXT_AREA];
21548 glyph = beg + prev->used[TEXT_AREA];
21549 while (--glyph >= beg
21550 && INTEGERP (glyph->object));
21551 if (glyph < beg
21552 || !EQ (stop, glyph->object))
21553 break;
21554 row = prev;
21555 }
21556 }
21557
21558 *x = row->x;
21559 *y = row->y;
21560 *vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
21561
21562 glyph = row->glyphs[TEXT_AREA];
21563 end = glyph + row->used[TEXT_AREA];
21564
21565 /* Skip over glyphs not having an object at the start of the row.
21566 These are special glyphs like truncation marks on terminal
21567 frames. */
21568 if (row->displays_text_p)
21569 while (glyph < end
21570 && INTEGERP (glyph->object)
21571 && !EQ (stop, glyph->object)
21572 && glyph->charpos < 0)
21573 {
21574 *x += glyph->pixel_width;
21575 ++glyph;
21576 }
21577
21578 while (glyph < end
21579 && !INTEGERP (glyph->object)
21580 && !EQ (stop, glyph->object)
21581 && (!BUFFERP (glyph->object)
21582 || glyph->charpos < charpos))
21583 {
21584 *x += glyph->pixel_width;
21585 ++glyph;
21586 }
21587
21588 *hpos = glyph - row->glyphs[TEXT_AREA];
21589 return !past_end;
21590 }
21591
21592 #else /* not 1 */
21593
21594 static int
21595 fast_find_position (w, pos, hpos, vpos, x, y, stop)
21596 struct window *w;
21597 int pos;
21598 int *hpos, *vpos, *x, *y;
21599 Lisp_Object stop;
21600 {
21601 int i;
21602 int lastcol;
21603 int maybe_next_line_p = 0;
21604 int line_start_position;
21605 int yb = window_text_bottom_y (w);
21606 struct glyph_row *row, *best_row;
21607 int row_vpos, best_row_vpos;
21608 int current_x;
21609
21610 row = best_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
21611 row_vpos = best_row_vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
21612
21613 while (row->y < yb)
21614 {
21615 if (row->used[TEXT_AREA])
21616 line_start_position = row->glyphs[TEXT_AREA]->charpos;
21617 else
21618 line_start_position = 0;
21619
21620 if (line_start_position > pos)
21621 break;
21622 /* If the position sought is the end of the buffer,
21623 don't include the blank lines at the bottom of the window. */
21624 else if (line_start_position == pos
21625 && pos == BUF_ZV (XBUFFER (w->buffer)))
21626 {
21627 maybe_next_line_p = 1;
21628 break;
21629 }
21630 else if (line_start_position > 0)
21631 {
21632 best_row = row;
21633 best_row_vpos = row_vpos;
21634 }
21635
21636 if (row->y + row->height >= yb)
21637 break;
21638
21639 ++row;
21640 ++row_vpos;
21641 }
21642
21643 /* Find the right column within BEST_ROW. */
21644 lastcol = 0;
21645 current_x = best_row->x;
21646 for (i = 0; i < best_row->used[TEXT_AREA]; i++)
21647 {
21648 struct glyph *glyph = best_row->glyphs[TEXT_AREA] + i;
21649 int charpos = glyph->charpos;
21650
21651 if (BUFFERP (glyph->object))
21652 {
21653 if (charpos == pos)
21654 {
21655 *hpos = i;
21656 *vpos = best_row_vpos;
21657 *x = current_x;
21658 *y = best_row->y;
21659 return 1;
21660 }
21661 else if (charpos > pos)
21662 break;
21663 }
21664 else if (EQ (glyph->object, stop))
21665 break;
21666
21667 if (charpos > 0)
21668 lastcol = i;
21669 current_x += glyph->pixel_width;
21670 }
21671
21672 /* If we're looking for the end of the buffer,
21673 and we didn't find it in the line we scanned,
21674 use the start of the following line. */
21675 if (maybe_next_line_p)
21676 {
21677 ++best_row;
21678 ++best_row_vpos;
21679 lastcol = 0;
21680 current_x = best_row->x;
21681 }
21682
21683 *vpos = best_row_vpos;
21684 *hpos = lastcol + 1;
21685 *x = current_x;
21686 *y = best_row->y;
21687 return 0;
21688 }
21689
21690 #endif /* not 1 */
21691
21692
21693 /* Find the position of the glyph for position POS in OBJECT in
21694 window W's current matrix, and return in *X, *Y the pixel
21695 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
21696
21697 RIGHT_P non-zero means return the position of the right edge of the
21698 glyph, RIGHT_P zero means return the left edge position.
21699
21700 If no glyph for POS exists in the matrix, return the position of
21701 the glyph with the next smaller position that is in the matrix, if
21702 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
21703 exists in the matrix, return the position of the glyph with the
21704 next larger position in OBJECT.
21705
21706 Value is non-zero if a glyph was found. */
21707
21708 static int
21709 fast_find_string_pos (w, pos, object, hpos, vpos, x, y, right_p)
21710 struct window *w;
21711 int pos;
21712 Lisp_Object object;
21713 int *hpos, *vpos, *x, *y;
21714 int right_p;
21715 {
21716 int yb = window_text_bottom_y (w);
21717 struct glyph_row *r;
21718 struct glyph *best_glyph = NULL;
21719 struct glyph_row *best_row = NULL;
21720 int best_x = 0;
21721
21722 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
21723 r->enabled_p && r->y < yb;
21724 ++r)
21725 {
21726 struct glyph *g = r->glyphs[TEXT_AREA];
21727 struct glyph *e = g + r->used[TEXT_AREA];
21728 int gx;
21729
21730 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
21731 if (EQ (g->object, object))
21732 {
21733 if (g->charpos == pos)
21734 {
21735 best_glyph = g;
21736 best_x = gx;
21737 best_row = r;
21738 goto found;
21739 }
21740 else if (best_glyph == NULL
21741 || ((abs (g->charpos - pos)
21742 < abs (best_glyph->charpos - pos))
21743 && (right_p
21744 ? g->charpos < pos
21745 : g->charpos > pos)))
21746 {
21747 best_glyph = g;
21748 best_x = gx;
21749 best_row = r;
21750 }
21751 }
21752 }
21753
21754 found:
21755
21756 if (best_glyph)
21757 {
21758 *x = best_x;
21759 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
21760
21761 if (right_p)
21762 {
21763 *x += best_glyph->pixel_width;
21764 ++*hpos;
21765 }
21766
21767 *y = best_row->y;
21768 *vpos = best_row - w->current_matrix->rows;
21769 }
21770
21771 return best_glyph != NULL;
21772 }
21773
21774
21775 /* See if position X, Y is within a hot-spot of an image. */
21776
21777 static int
21778 on_hot_spot_p (hot_spot, x, y)
21779 Lisp_Object hot_spot;
21780 int x, y;
21781 {
21782 if (!CONSP (hot_spot))
21783 return 0;
21784
21785 if (EQ (XCAR (hot_spot), Qrect))
21786 {
21787 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
21788 Lisp_Object rect = XCDR (hot_spot);
21789 Lisp_Object tem;
21790 if (!CONSP (rect))
21791 return 0;
21792 if (!CONSP (XCAR (rect)))
21793 return 0;
21794 if (!CONSP (XCDR (rect)))
21795 return 0;
21796 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
21797 return 0;
21798 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
21799 return 0;
21800 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
21801 return 0;
21802 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
21803 return 0;
21804 return 1;
21805 }
21806 else if (EQ (XCAR (hot_spot), Qcircle))
21807 {
21808 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
21809 Lisp_Object circ = XCDR (hot_spot);
21810 Lisp_Object lr, lx0, ly0;
21811 if (CONSP (circ)
21812 && CONSP (XCAR (circ))
21813 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
21814 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
21815 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
21816 {
21817 double r = XFLOATINT (lr);
21818 double dx = XINT (lx0) - x;
21819 double dy = XINT (ly0) - y;
21820 return (dx * dx + dy * dy <= r * r);
21821 }
21822 }
21823 else if (EQ (XCAR (hot_spot), Qpoly))
21824 {
21825 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
21826 if (VECTORP (XCDR (hot_spot)))
21827 {
21828 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
21829 Lisp_Object *poly = v->contents;
21830 int n = v->size;
21831 int i;
21832 int inside = 0;
21833 Lisp_Object lx, ly;
21834 int x0, y0;
21835
21836 /* Need an even number of coordinates, and at least 3 edges. */
21837 if (n < 6 || n & 1)
21838 return 0;
21839
21840 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
21841 If count is odd, we are inside polygon. Pixels on edges
21842 may or may not be included depending on actual geometry of the
21843 polygon. */
21844 if ((lx = poly[n-2], !INTEGERP (lx))
21845 || (ly = poly[n-1], !INTEGERP (lx)))
21846 return 0;
21847 x0 = XINT (lx), y0 = XINT (ly);
21848 for (i = 0; i < n; i += 2)
21849 {
21850 int x1 = x0, y1 = y0;
21851 if ((lx = poly[i], !INTEGERP (lx))
21852 || (ly = poly[i+1], !INTEGERP (ly)))
21853 return 0;
21854 x0 = XINT (lx), y0 = XINT (ly);
21855
21856 /* Does this segment cross the X line? */
21857 if (x0 >= x)
21858 {
21859 if (x1 >= x)
21860 continue;
21861 }
21862 else if (x1 < x)
21863 continue;
21864 if (y > y0 && y > y1)
21865 continue;
21866 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
21867 inside = !inside;
21868 }
21869 return inside;
21870 }
21871 }
21872 /* If we don't understand the format, pretend we're not in the hot-spot. */
21873 return 0;
21874 }
21875
21876 Lisp_Object
21877 find_hot_spot (map, x, y)
21878 Lisp_Object map;
21879 int x, y;
21880 {
21881 while (CONSP (map))
21882 {
21883 if (CONSP (XCAR (map))
21884 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
21885 return XCAR (map);
21886 map = XCDR (map);
21887 }
21888
21889 return Qnil;
21890 }
21891
21892 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
21893 3, 3, 0,
21894 doc: /* Lookup in image map MAP coordinates X and Y.
21895 An image map is an alist where each element has the format (AREA ID PLIST).
21896 An AREA is specified as either a rectangle, a circle, or a polygon:
21897 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
21898 pixel coordinates of the upper left and bottom right corners.
21899 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
21900 and the radius of the circle; r may be a float or integer.
21901 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
21902 vector describes one corner in the polygon.
21903 Returns the alist element for the first matching AREA in MAP. */)
21904 (map, x, y)
21905 Lisp_Object map;
21906 Lisp_Object x, y;
21907 {
21908 if (NILP (map))
21909 return Qnil;
21910
21911 CHECK_NUMBER (x);
21912 CHECK_NUMBER (y);
21913
21914 return find_hot_spot (map, XINT (x), XINT (y));
21915 }
21916
21917
21918 /* Display frame CURSOR, optionally using shape defined by POINTER. */
21919 static void
21920 define_frame_cursor1 (f, cursor, pointer)
21921 struct frame *f;
21922 Cursor cursor;
21923 Lisp_Object pointer;
21924 {
21925 /* Do not change cursor shape while dragging mouse. */
21926 if (!NILP (do_mouse_tracking))
21927 return;
21928
21929 if (!NILP (pointer))
21930 {
21931 if (EQ (pointer, Qarrow))
21932 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
21933 else if (EQ (pointer, Qhand))
21934 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
21935 else if (EQ (pointer, Qtext))
21936 cursor = FRAME_X_OUTPUT (f)->text_cursor;
21937 else if (EQ (pointer, intern ("hdrag")))
21938 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
21939 #ifdef HAVE_X_WINDOWS
21940 else if (EQ (pointer, intern ("vdrag")))
21941 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
21942 #endif
21943 else if (EQ (pointer, intern ("hourglass")))
21944 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
21945 else if (EQ (pointer, Qmodeline))
21946 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
21947 else
21948 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
21949 }
21950
21951 if (cursor != No_Cursor)
21952 rif->define_frame_cursor (f, cursor);
21953 }
21954
21955 /* Take proper action when mouse has moved to the mode or header line
21956 or marginal area AREA of window W, x-position X and y-position Y.
21957 X is relative to the start of the text display area of W, so the
21958 width of bitmap areas and scroll bars must be subtracted to get a
21959 position relative to the start of the mode line. */
21960
21961 static void
21962 note_mode_line_or_margin_highlight (window, x, y, area)
21963 Lisp_Object window;
21964 int x, y;
21965 enum window_part area;
21966 {
21967 struct window *w = XWINDOW (window);
21968 struct frame *f = XFRAME (w->frame);
21969 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
21970 Cursor cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
21971 Lisp_Object pointer = Qnil;
21972 int charpos, dx, dy, width, height;
21973 Lisp_Object string, object = Qnil;
21974 Lisp_Object pos, help;
21975
21976 Lisp_Object mouse_face;
21977 int original_x_pixel = x;
21978 struct glyph * glyph = NULL;
21979 struct glyph_row *row;
21980
21981 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
21982 {
21983 int x0;
21984 struct glyph *end;
21985
21986 string = mode_line_string (w, area, &x, &y, &charpos,
21987 &object, &dx, &dy, &width, &height);
21988
21989 row = (area == ON_MODE_LINE
21990 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
21991 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
21992
21993 /* Find glyph */
21994 if (row->mode_line_p && row->enabled_p)
21995 {
21996 glyph = row->glyphs[TEXT_AREA];
21997 end = glyph + row->used[TEXT_AREA];
21998
21999 for (x0 = original_x_pixel;
22000 glyph < end && x0 >= glyph->pixel_width;
22001 ++glyph)
22002 x0 -= glyph->pixel_width;
22003
22004 if (glyph >= end)
22005 glyph = NULL;
22006 }
22007 }
22008 else
22009 {
22010 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
22011 string = marginal_area_string (w, area, &x, &y, &charpos,
22012 &object, &dx, &dy, &width, &height);
22013 }
22014
22015 help = Qnil;
22016
22017 if (IMAGEP (object))
22018 {
22019 Lisp_Object image_map, hotspot;
22020 if ((image_map = Fplist_get (XCDR (object), QCmap),
22021 !NILP (image_map))
22022 && (hotspot = find_hot_spot (image_map, dx, dy),
22023 CONSP (hotspot))
22024 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
22025 {
22026 Lisp_Object area_id, plist;
22027
22028 area_id = XCAR (hotspot);
22029 /* Could check AREA_ID to see if we enter/leave this hot-spot.
22030 If so, we could look for mouse-enter, mouse-leave
22031 properties in PLIST (and do something...). */
22032 hotspot = XCDR (hotspot);
22033 if (CONSP (hotspot)
22034 && (plist = XCAR (hotspot), CONSP (plist)))
22035 {
22036 pointer = Fplist_get (plist, Qpointer);
22037 if (NILP (pointer))
22038 pointer = Qhand;
22039 help = Fplist_get (plist, Qhelp_echo);
22040 if (!NILP (help))
22041 {
22042 help_echo_string = help;
22043 /* Is this correct? ++kfs */
22044 XSETWINDOW (help_echo_window, w);
22045 help_echo_object = w->buffer;
22046 help_echo_pos = charpos;
22047 }
22048 }
22049 }
22050 if (NILP (pointer))
22051 pointer = Fplist_get (XCDR (object), QCpointer);
22052 }
22053
22054 if (STRINGP (string))
22055 {
22056 pos = make_number (charpos);
22057 /* If we're on a string with `help-echo' text property, arrange
22058 for the help to be displayed. This is done by setting the
22059 global variable help_echo_string to the help string. */
22060 if (NILP (help))
22061 {
22062 help = Fget_text_property (pos, Qhelp_echo, string);
22063 if (!NILP (help))
22064 {
22065 help_echo_string = help;
22066 XSETWINDOW (help_echo_window, w);
22067 help_echo_object = string;
22068 help_echo_pos = charpos;
22069 }
22070 }
22071
22072 if (NILP (pointer))
22073 pointer = Fget_text_property (pos, Qpointer, string);
22074
22075 /* Change the mouse pointer according to what is under X/Y. */
22076 if (NILP (pointer) && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
22077 {
22078 Lisp_Object map;
22079 map = Fget_text_property (pos, Qlocal_map, string);
22080 if (!KEYMAPP (map))
22081 map = Fget_text_property (pos, Qkeymap, string);
22082 if (!KEYMAPP (map))
22083 cursor = dpyinfo->vertical_scroll_bar_cursor;
22084 }
22085
22086 /* Change the mouse face according to what is under X/Y. */
22087 mouse_face = Fget_text_property (pos, Qmouse_face, string);
22088 if (!NILP (mouse_face)
22089 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
22090 && glyph)
22091 {
22092 Lisp_Object b, e;
22093
22094 struct glyph * tmp_glyph;
22095
22096 int gpos;
22097 int gseq_length;
22098 int total_pixel_width;
22099 int ignore;
22100
22101 int vpos, hpos;
22102
22103 b = Fprevious_single_property_change (make_number (charpos + 1),
22104 Qmouse_face, string, Qnil);
22105 if (NILP (b))
22106 b = make_number (0);
22107
22108 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
22109 if (NILP (e))
22110 e = make_number (SCHARS (string));
22111
22112 /* Calculate the position(glyph position: GPOS) of GLYPH in
22113 displayed string. GPOS is different from CHARPOS.
22114
22115 CHARPOS is the position of glyph in internal string
22116 object. A mode line string format has structures which
22117 is converted to a flatten by emacs lisp interpreter.
22118 The internal string is an element of the structures.
22119 The displayed string is the flatten string. */
22120 for (tmp_glyph = glyph - 1, gpos = 0;
22121 tmp_glyph->charpos >= XINT (b);
22122 tmp_glyph--, gpos++)
22123 {
22124 if (!EQ (tmp_glyph->object, glyph->object))
22125 break;
22126 }
22127
22128 /* Calculate the lenght(glyph sequence length: GSEQ_LENGTH) of
22129 displayed string holding GLYPH.
22130
22131 GSEQ_LENGTH is different from SCHARS (STRING).
22132 SCHARS (STRING) returns the length of the internal string. */
22133 for (tmp_glyph = glyph, gseq_length = gpos;
22134 tmp_glyph->charpos < XINT (e);
22135 tmp_glyph++, gseq_length++)
22136 {
22137 if (!EQ (tmp_glyph->object, glyph->object))
22138 break;
22139 }
22140
22141 total_pixel_width = 0;
22142 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
22143 total_pixel_width += tmp_glyph->pixel_width;
22144
22145 /* Pre calculation of re-rendering position */
22146 vpos = (x - gpos);
22147 hpos = (area == ON_MODE_LINE
22148 ? (w->current_matrix)->nrows - 1
22149 : 0);
22150
22151 /* If the re-rendering position is included in the last
22152 re-rendering area, we should do nothing. */
22153 if ( EQ (window, dpyinfo->mouse_face_window)
22154 && dpyinfo->mouse_face_beg_col <= vpos
22155 && vpos < dpyinfo->mouse_face_end_col
22156 && dpyinfo->mouse_face_beg_row == hpos )
22157 return;
22158
22159 if (clear_mouse_face (dpyinfo))
22160 cursor = No_Cursor;
22161
22162 dpyinfo->mouse_face_beg_col = vpos;
22163 dpyinfo->mouse_face_beg_row = hpos;
22164
22165 dpyinfo->mouse_face_beg_x = original_x_pixel - (total_pixel_width + dx);
22166 dpyinfo->mouse_face_beg_y = 0;
22167
22168 dpyinfo->mouse_face_end_col = vpos + gseq_length;
22169 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_beg_row;
22170
22171 dpyinfo->mouse_face_end_x = 0;
22172 dpyinfo->mouse_face_end_y = 0;
22173
22174 dpyinfo->mouse_face_past_end = 0;
22175 dpyinfo->mouse_face_window = window;
22176
22177 dpyinfo->mouse_face_face_id = face_at_string_position (w, string,
22178 charpos,
22179 0, 0, 0, &ignore,
22180 glyph->face_id, 1);
22181 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
22182
22183 if (NILP (pointer))
22184 pointer = Qhand;
22185 }
22186 else if ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
22187 clear_mouse_face (dpyinfo);
22188 }
22189 define_frame_cursor1 (f, cursor, pointer);
22190 }
22191
22192
22193 /* EXPORT:
22194 Take proper action when the mouse has moved to position X, Y on
22195 frame F as regards highlighting characters that have mouse-face
22196 properties. Also de-highlighting chars where the mouse was before.
22197 X and Y can be negative or out of range. */
22198
22199 void
22200 note_mouse_highlight (f, x, y)
22201 struct frame *f;
22202 int x, y;
22203 {
22204 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
22205 enum window_part part;
22206 Lisp_Object window;
22207 struct window *w;
22208 Cursor cursor = No_Cursor;
22209 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
22210 struct buffer *b;
22211
22212 /* When a menu is active, don't highlight because this looks odd. */
22213 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NTGUI)
22214 if (popup_activated ())
22215 return;
22216 #endif
22217
22218 if (NILP (Vmouse_highlight)
22219 || !f->glyphs_initialized_p)
22220 return;
22221
22222 dpyinfo->mouse_face_mouse_x = x;
22223 dpyinfo->mouse_face_mouse_y = y;
22224 dpyinfo->mouse_face_mouse_frame = f;
22225
22226 if (dpyinfo->mouse_face_defer)
22227 return;
22228
22229 if (gc_in_progress)
22230 {
22231 dpyinfo->mouse_face_deferred_gc = 1;
22232 return;
22233 }
22234
22235 /* Which window is that in? */
22236 window = window_from_coordinates (f, x, y, &part, 0, 0, 1);
22237
22238 /* If we were displaying active text in another window, clear that.
22239 Also clear if we move out of text area in same window. */
22240 if (! EQ (window, dpyinfo->mouse_face_window)
22241 || (part != ON_TEXT && part != ON_MODE_LINE && part != ON_HEADER_LINE
22242 && !NILP (dpyinfo->mouse_face_window)))
22243 clear_mouse_face (dpyinfo);
22244
22245 /* Not on a window -> return. */
22246 if (!WINDOWP (window))
22247 return;
22248
22249 /* Reset help_echo_string. It will get recomputed below. */
22250 help_echo_string = Qnil;
22251
22252 /* Convert to window-relative pixel coordinates. */
22253 w = XWINDOW (window);
22254 frame_to_window_pixel_xy (w, &x, &y);
22255
22256 /* Handle tool-bar window differently since it doesn't display a
22257 buffer. */
22258 if (EQ (window, f->tool_bar_window))
22259 {
22260 note_tool_bar_highlight (f, x, y);
22261 return;
22262 }
22263
22264 /* Mouse is on the mode, header line or margin? */
22265 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
22266 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
22267 {
22268 note_mode_line_or_margin_highlight (window, x, y, part);
22269 return;
22270 }
22271
22272 if (part == ON_VERTICAL_BORDER)
22273 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
22274 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
22275 || part == ON_SCROLL_BAR)
22276 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
22277 else
22278 cursor = FRAME_X_OUTPUT (f)->text_cursor;
22279
22280 /* Are we in a window whose display is up to date?
22281 And verify the buffer's text has not changed. */
22282 b = XBUFFER (w->buffer);
22283 if (part == ON_TEXT
22284 && EQ (w->window_end_valid, w->buffer)
22285 && XFASTINT (w->last_modified) == BUF_MODIFF (b)
22286 && XFASTINT (w->last_overlay_modified) == BUF_OVERLAY_MODIFF (b))
22287 {
22288 int hpos, vpos, pos, i, dx, dy, area;
22289 struct glyph *glyph;
22290 Lisp_Object object;
22291 Lisp_Object mouse_face = Qnil, overlay = Qnil, position;
22292 Lisp_Object *overlay_vec = NULL;
22293 int noverlays;
22294 struct buffer *obuf;
22295 int obegv, ozv, same_region;
22296
22297 /* Find the glyph under X/Y. */
22298 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
22299
22300 /* Look for :pointer property on image. */
22301 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
22302 {
22303 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
22304 if (img != NULL && IMAGEP (img->spec))
22305 {
22306 Lisp_Object image_map, hotspot;
22307 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
22308 !NILP (image_map))
22309 && (hotspot = find_hot_spot (image_map,
22310 glyph->slice.x + dx,
22311 glyph->slice.y + dy),
22312 CONSP (hotspot))
22313 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
22314 {
22315 Lisp_Object area_id, plist;
22316
22317 area_id = XCAR (hotspot);
22318 /* Could check AREA_ID to see if we enter/leave this hot-spot.
22319 If so, we could look for mouse-enter, mouse-leave
22320 properties in PLIST (and do something...). */
22321 hotspot = XCDR (hotspot);
22322 if (CONSP (hotspot)
22323 && (plist = XCAR (hotspot), CONSP (plist)))
22324 {
22325 pointer = Fplist_get (plist, Qpointer);
22326 if (NILP (pointer))
22327 pointer = Qhand;
22328 help_echo_string = Fplist_get (plist, Qhelp_echo);
22329 if (!NILP (help_echo_string))
22330 {
22331 help_echo_window = window;
22332 help_echo_object = glyph->object;
22333 help_echo_pos = glyph->charpos;
22334 }
22335 }
22336 }
22337 if (NILP (pointer))
22338 pointer = Fplist_get (XCDR (img->spec), QCpointer);
22339 }
22340 }
22341
22342 /* Clear mouse face if X/Y not over text. */
22343 if (glyph == NULL
22344 || area != TEXT_AREA
22345 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p)
22346 {
22347 if (clear_mouse_face (dpyinfo))
22348 cursor = No_Cursor;
22349 if (NILP (pointer))
22350 {
22351 if (area != TEXT_AREA)
22352 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
22353 else
22354 pointer = Vvoid_text_area_pointer;
22355 }
22356 goto set_cursor;
22357 }
22358
22359 pos = glyph->charpos;
22360 object = glyph->object;
22361 if (!STRINGP (object) && !BUFFERP (object))
22362 goto set_cursor;
22363
22364 /* If we get an out-of-range value, return now; avoid an error. */
22365 if (BUFFERP (object) && pos > BUF_Z (b))
22366 goto set_cursor;
22367
22368 /* Make the window's buffer temporarily current for
22369 overlays_at and compute_char_face. */
22370 obuf = current_buffer;
22371 current_buffer = b;
22372 obegv = BEGV;
22373 ozv = ZV;
22374 BEGV = BEG;
22375 ZV = Z;
22376
22377 /* Is this char mouse-active or does it have help-echo? */
22378 position = make_number (pos);
22379
22380 if (BUFFERP (object))
22381 {
22382 /* Put all the overlays we want in a vector in overlay_vec. */
22383 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
22384 /* Sort overlays into increasing priority order. */
22385 noverlays = sort_overlays (overlay_vec, noverlays, w);
22386 }
22387 else
22388 noverlays = 0;
22389
22390 same_region = (EQ (window, dpyinfo->mouse_face_window)
22391 && vpos >= dpyinfo->mouse_face_beg_row
22392 && vpos <= dpyinfo->mouse_face_end_row
22393 && (vpos > dpyinfo->mouse_face_beg_row
22394 || hpos >= dpyinfo->mouse_face_beg_col)
22395 && (vpos < dpyinfo->mouse_face_end_row
22396 || hpos < dpyinfo->mouse_face_end_col
22397 || dpyinfo->mouse_face_past_end));
22398
22399 if (same_region)
22400 cursor = No_Cursor;
22401
22402 /* Check mouse-face highlighting. */
22403 if (! same_region
22404 /* If there exists an overlay with mouse-face overlapping
22405 the one we are currently highlighting, we have to
22406 check if we enter the overlapping overlay, and then
22407 highlight only that. */
22408 || (OVERLAYP (dpyinfo->mouse_face_overlay)
22409 && mouse_face_overlay_overlaps (dpyinfo->mouse_face_overlay)))
22410 {
22411 /* Find the highest priority overlay that has a mouse-face
22412 property. */
22413 overlay = Qnil;
22414 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
22415 {
22416 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
22417 if (!NILP (mouse_face))
22418 overlay = overlay_vec[i];
22419 }
22420
22421 /* If we're actually highlighting the same overlay as
22422 before, there's no need to do that again. */
22423 if (!NILP (overlay)
22424 && EQ (overlay, dpyinfo->mouse_face_overlay))
22425 goto check_help_echo;
22426
22427 dpyinfo->mouse_face_overlay = overlay;
22428
22429 /* Clear the display of the old active region, if any. */
22430 if (clear_mouse_face (dpyinfo))
22431 cursor = No_Cursor;
22432
22433 /* If no overlay applies, get a text property. */
22434 if (NILP (overlay))
22435 mouse_face = Fget_text_property (position, Qmouse_face, object);
22436
22437 /* Handle the overlay case. */
22438 if (!NILP (overlay))
22439 {
22440 /* Find the range of text around this char that
22441 should be active. */
22442 Lisp_Object before, after;
22443 int ignore;
22444
22445 before = Foverlay_start (overlay);
22446 after = Foverlay_end (overlay);
22447 /* Record this as the current active region. */
22448 fast_find_position (w, XFASTINT (before),
22449 &dpyinfo->mouse_face_beg_col,
22450 &dpyinfo->mouse_face_beg_row,
22451 &dpyinfo->mouse_face_beg_x,
22452 &dpyinfo->mouse_face_beg_y, Qnil);
22453
22454 dpyinfo->mouse_face_past_end
22455 = !fast_find_position (w, XFASTINT (after),
22456 &dpyinfo->mouse_face_end_col,
22457 &dpyinfo->mouse_face_end_row,
22458 &dpyinfo->mouse_face_end_x,
22459 &dpyinfo->mouse_face_end_y, Qnil);
22460 dpyinfo->mouse_face_window = window;
22461
22462 dpyinfo->mouse_face_face_id
22463 = face_at_buffer_position (w, pos, 0, 0,
22464 &ignore, pos + 1,
22465 !dpyinfo->mouse_face_hidden);
22466
22467 /* Display it as active. */
22468 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
22469 cursor = No_Cursor;
22470 }
22471 /* Handle the text property case. */
22472 else if (!NILP (mouse_face) && BUFFERP (object))
22473 {
22474 /* Find the range of text around this char that
22475 should be active. */
22476 Lisp_Object before, after, beginning, end;
22477 int ignore;
22478
22479 beginning = Fmarker_position (w->start);
22480 end = make_number (BUF_Z (XBUFFER (object))
22481 - XFASTINT (w->window_end_pos));
22482 before
22483 = Fprevious_single_property_change (make_number (pos + 1),
22484 Qmouse_face,
22485 object, beginning);
22486 after
22487 = Fnext_single_property_change (position, Qmouse_face,
22488 object, end);
22489
22490 /* Record this as the current active region. */
22491 fast_find_position (w, XFASTINT (before),
22492 &dpyinfo->mouse_face_beg_col,
22493 &dpyinfo->mouse_face_beg_row,
22494 &dpyinfo->mouse_face_beg_x,
22495 &dpyinfo->mouse_face_beg_y, Qnil);
22496 dpyinfo->mouse_face_past_end
22497 = !fast_find_position (w, XFASTINT (after),
22498 &dpyinfo->mouse_face_end_col,
22499 &dpyinfo->mouse_face_end_row,
22500 &dpyinfo->mouse_face_end_x,
22501 &dpyinfo->mouse_face_end_y, Qnil);
22502 dpyinfo->mouse_face_window = window;
22503
22504 if (BUFFERP (object))
22505 dpyinfo->mouse_face_face_id
22506 = face_at_buffer_position (w, pos, 0, 0,
22507 &ignore, pos + 1,
22508 !dpyinfo->mouse_face_hidden);
22509
22510 /* Display it as active. */
22511 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
22512 cursor = No_Cursor;
22513 }
22514 else if (!NILP (mouse_face) && STRINGP (object))
22515 {
22516 Lisp_Object b, e;
22517 int ignore;
22518
22519 b = Fprevious_single_property_change (make_number (pos + 1),
22520 Qmouse_face,
22521 object, Qnil);
22522 e = Fnext_single_property_change (position, Qmouse_face,
22523 object, Qnil);
22524 if (NILP (b))
22525 b = make_number (0);
22526 if (NILP (e))
22527 e = make_number (SCHARS (object) - 1);
22528
22529 fast_find_string_pos (w, XINT (b), object,
22530 &dpyinfo->mouse_face_beg_col,
22531 &dpyinfo->mouse_face_beg_row,
22532 &dpyinfo->mouse_face_beg_x,
22533 &dpyinfo->mouse_face_beg_y, 0);
22534 fast_find_string_pos (w, XINT (e), object,
22535 &dpyinfo->mouse_face_end_col,
22536 &dpyinfo->mouse_face_end_row,
22537 &dpyinfo->mouse_face_end_x,
22538 &dpyinfo->mouse_face_end_y, 1);
22539 dpyinfo->mouse_face_past_end = 0;
22540 dpyinfo->mouse_face_window = window;
22541 dpyinfo->mouse_face_face_id
22542 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
22543 glyph->face_id, 1);
22544 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
22545 cursor = No_Cursor;
22546 }
22547 else if (STRINGP (object) && NILP (mouse_face))
22548 {
22549 /* A string which doesn't have mouse-face, but
22550 the text ``under'' it might have. */
22551 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
22552 int start = MATRIX_ROW_START_CHARPOS (r);
22553
22554 pos = string_buffer_position (w, object, start);
22555 if (pos > 0)
22556 mouse_face = get_char_property_and_overlay (make_number (pos),
22557 Qmouse_face,
22558 w->buffer,
22559 &overlay);
22560 if (!NILP (mouse_face) && !NILP (overlay))
22561 {
22562 Lisp_Object before = Foverlay_start (overlay);
22563 Lisp_Object after = Foverlay_end (overlay);
22564 int ignore;
22565
22566 /* Note that we might not be able to find position
22567 BEFORE in the glyph matrix if the overlay is
22568 entirely covered by a `display' property. In
22569 this case, we overshoot. So let's stop in
22570 the glyph matrix before glyphs for OBJECT. */
22571 fast_find_position (w, XFASTINT (before),
22572 &dpyinfo->mouse_face_beg_col,
22573 &dpyinfo->mouse_face_beg_row,
22574 &dpyinfo->mouse_face_beg_x,
22575 &dpyinfo->mouse_face_beg_y,
22576 object);
22577
22578 dpyinfo->mouse_face_past_end
22579 = !fast_find_position (w, XFASTINT (after),
22580 &dpyinfo->mouse_face_end_col,
22581 &dpyinfo->mouse_face_end_row,
22582 &dpyinfo->mouse_face_end_x,
22583 &dpyinfo->mouse_face_end_y,
22584 Qnil);
22585 dpyinfo->mouse_face_window = window;
22586 dpyinfo->mouse_face_face_id
22587 = face_at_buffer_position (w, pos, 0, 0,
22588 &ignore, pos + 1,
22589 !dpyinfo->mouse_face_hidden);
22590
22591 /* Display it as active. */
22592 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
22593 cursor = No_Cursor;
22594 }
22595 }
22596 }
22597
22598 check_help_echo:
22599
22600 /* Look for a `help-echo' property. */
22601 if (NILP (help_echo_string)) {
22602 Lisp_Object help, overlay;
22603
22604 /* Check overlays first. */
22605 help = overlay = Qnil;
22606 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
22607 {
22608 overlay = overlay_vec[i];
22609 help = Foverlay_get (overlay, Qhelp_echo);
22610 }
22611
22612 if (!NILP (help))
22613 {
22614 help_echo_string = help;
22615 help_echo_window = window;
22616 help_echo_object = overlay;
22617 help_echo_pos = pos;
22618 }
22619 else
22620 {
22621 Lisp_Object object = glyph->object;
22622 int charpos = glyph->charpos;
22623
22624 /* Try text properties. */
22625 if (STRINGP (object)
22626 && charpos >= 0
22627 && charpos < SCHARS (object))
22628 {
22629 help = Fget_text_property (make_number (charpos),
22630 Qhelp_echo, object);
22631 if (NILP (help))
22632 {
22633 /* If the string itself doesn't specify a help-echo,
22634 see if the buffer text ``under'' it does. */
22635 struct glyph_row *r
22636 = MATRIX_ROW (w->current_matrix, vpos);
22637 int start = MATRIX_ROW_START_CHARPOS (r);
22638 int pos = string_buffer_position (w, object, start);
22639 if (pos > 0)
22640 {
22641 help = Fget_char_property (make_number (pos),
22642 Qhelp_echo, w->buffer);
22643 if (!NILP (help))
22644 {
22645 charpos = pos;
22646 object = w->buffer;
22647 }
22648 }
22649 }
22650 }
22651 else if (BUFFERP (object)
22652 && charpos >= BEGV
22653 && charpos < ZV)
22654 help = Fget_text_property (make_number (charpos), Qhelp_echo,
22655 object);
22656
22657 if (!NILP (help))
22658 {
22659 help_echo_string = help;
22660 help_echo_window = window;
22661 help_echo_object = object;
22662 help_echo_pos = charpos;
22663 }
22664 }
22665 }
22666
22667 /* Look for a `pointer' property. */
22668 if (NILP (pointer))
22669 {
22670 /* Check overlays first. */
22671 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
22672 pointer = Foverlay_get (overlay_vec[i], Qpointer);
22673
22674 if (NILP (pointer))
22675 {
22676 Lisp_Object object = glyph->object;
22677 int charpos = glyph->charpos;
22678
22679 /* Try text properties. */
22680 if (STRINGP (object)
22681 && charpos >= 0
22682 && charpos < SCHARS (object))
22683 {
22684 pointer = Fget_text_property (make_number (charpos),
22685 Qpointer, object);
22686 if (NILP (pointer))
22687 {
22688 /* If the string itself doesn't specify a pointer,
22689 see if the buffer text ``under'' it does. */
22690 struct glyph_row *r
22691 = MATRIX_ROW (w->current_matrix, vpos);
22692 int start = MATRIX_ROW_START_CHARPOS (r);
22693 int pos = string_buffer_position (w, object, start);
22694 if (pos > 0)
22695 pointer = Fget_char_property (make_number (pos),
22696 Qpointer, w->buffer);
22697 }
22698 }
22699 else if (BUFFERP (object)
22700 && charpos >= BEGV
22701 && charpos < ZV)
22702 pointer = Fget_text_property (make_number (charpos),
22703 Qpointer, object);
22704 }
22705 }
22706
22707 BEGV = obegv;
22708 ZV = ozv;
22709 current_buffer = obuf;
22710 }
22711
22712 set_cursor:
22713
22714 define_frame_cursor1 (f, cursor, pointer);
22715 }
22716
22717
22718 /* EXPORT for RIF:
22719 Clear any mouse-face on window W. This function is part of the
22720 redisplay interface, and is called from try_window_id and similar
22721 functions to ensure the mouse-highlight is off. */
22722
22723 void
22724 x_clear_window_mouse_face (w)
22725 struct window *w;
22726 {
22727 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
22728 Lisp_Object window;
22729
22730 BLOCK_INPUT;
22731 XSETWINDOW (window, w);
22732 if (EQ (window, dpyinfo->mouse_face_window))
22733 clear_mouse_face (dpyinfo);
22734 UNBLOCK_INPUT;
22735 }
22736
22737
22738 /* EXPORT:
22739 Just discard the mouse face information for frame F, if any.
22740 This is used when the size of F is changed. */
22741
22742 void
22743 cancel_mouse_face (f)
22744 struct frame *f;
22745 {
22746 Lisp_Object window;
22747 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
22748
22749 window = dpyinfo->mouse_face_window;
22750 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
22751 {
22752 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
22753 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
22754 dpyinfo->mouse_face_window = Qnil;
22755 }
22756 }
22757
22758
22759 #endif /* HAVE_WINDOW_SYSTEM */
22760
22761 \f
22762 /***********************************************************************
22763 Exposure Events
22764 ***********************************************************************/
22765
22766 #ifdef HAVE_WINDOW_SYSTEM
22767
22768 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
22769 which intersects rectangle R. R is in window-relative coordinates. */
22770
22771 static void
22772 expose_area (w, row, r, area)
22773 struct window *w;
22774 struct glyph_row *row;
22775 XRectangle *r;
22776 enum glyph_row_area area;
22777 {
22778 struct glyph *first = row->glyphs[area];
22779 struct glyph *end = row->glyphs[area] + row->used[area];
22780 struct glyph *last;
22781 int first_x, start_x, x;
22782
22783 if (area == TEXT_AREA && row->fill_line_p)
22784 /* If row extends face to end of line write the whole line. */
22785 draw_glyphs (w, 0, row, area,
22786 0, row->used[area],
22787 DRAW_NORMAL_TEXT, 0);
22788 else
22789 {
22790 /* Set START_X to the window-relative start position for drawing glyphs of
22791 AREA. The first glyph of the text area can be partially visible.
22792 The first glyphs of other areas cannot. */
22793 start_x = window_box_left_offset (w, area);
22794 x = start_x;
22795 if (area == TEXT_AREA)
22796 x += row->x;
22797
22798 /* Find the first glyph that must be redrawn. */
22799 while (first < end
22800 && x + first->pixel_width < r->x)
22801 {
22802 x += first->pixel_width;
22803 ++first;
22804 }
22805
22806 /* Find the last one. */
22807 last = first;
22808 first_x = x;
22809 while (last < end
22810 && x < r->x + r->width)
22811 {
22812 x += last->pixel_width;
22813 ++last;
22814 }
22815
22816 /* Repaint. */
22817 if (last > first)
22818 draw_glyphs (w, first_x - start_x, row, area,
22819 first - row->glyphs[area], last - row->glyphs[area],
22820 DRAW_NORMAL_TEXT, 0);
22821 }
22822 }
22823
22824
22825 /* Redraw the parts of the glyph row ROW on window W intersecting
22826 rectangle R. R is in window-relative coordinates. Value is
22827 non-zero if mouse-face was overwritten. */
22828
22829 static int
22830 expose_line (w, row, r)
22831 struct window *w;
22832 struct glyph_row *row;
22833 XRectangle *r;
22834 {
22835 xassert (row->enabled_p);
22836
22837 if (row->mode_line_p || w->pseudo_window_p)
22838 draw_glyphs (w, 0, row, TEXT_AREA,
22839 0, row->used[TEXT_AREA],
22840 DRAW_NORMAL_TEXT, 0);
22841 else
22842 {
22843 if (row->used[LEFT_MARGIN_AREA])
22844 expose_area (w, row, r, LEFT_MARGIN_AREA);
22845 if (row->used[TEXT_AREA])
22846 expose_area (w, row, r, TEXT_AREA);
22847 if (row->used[RIGHT_MARGIN_AREA])
22848 expose_area (w, row, r, RIGHT_MARGIN_AREA);
22849 draw_row_fringe_bitmaps (w, row);
22850 }
22851
22852 return row->mouse_face_p;
22853 }
22854
22855
22856 /* Redraw those parts of glyphs rows during expose event handling that
22857 overlap other rows. Redrawing of an exposed line writes over parts
22858 of lines overlapping that exposed line; this function fixes that.
22859
22860 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
22861 row in W's current matrix that is exposed and overlaps other rows.
22862 LAST_OVERLAPPING_ROW is the last such row. */
22863
22864 static void
22865 expose_overlaps (w, first_overlapping_row, last_overlapping_row)
22866 struct window *w;
22867 struct glyph_row *first_overlapping_row;
22868 struct glyph_row *last_overlapping_row;
22869 {
22870 struct glyph_row *row;
22871
22872 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
22873 if (row->overlapping_p)
22874 {
22875 xassert (row->enabled_p && !row->mode_line_p);
22876
22877 if (row->used[LEFT_MARGIN_AREA])
22878 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
22879
22880 if (row->used[TEXT_AREA])
22881 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
22882
22883 if (row->used[RIGHT_MARGIN_AREA])
22884 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
22885 }
22886 }
22887
22888
22889 /* Return non-zero if W's cursor intersects rectangle R. */
22890
22891 static int
22892 phys_cursor_in_rect_p (w, r)
22893 struct window *w;
22894 XRectangle *r;
22895 {
22896 XRectangle cr, result;
22897 struct glyph *cursor_glyph;
22898
22899 cursor_glyph = get_phys_cursor_glyph (w);
22900 if (cursor_glyph)
22901 {
22902 /* r is relative to W's box, but w->phys_cursor.x is relative
22903 to left edge of W's TEXT area. Adjust it. */
22904 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
22905 cr.y = w->phys_cursor.y;
22906 cr.width = cursor_glyph->pixel_width;
22907 cr.height = w->phys_cursor_height;
22908 /* ++KFS: W32 version used W32-specific IntersectRect here, but
22909 I assume the effect is the same -- and this is portable. */
22910 return x_intersect_rectangles (&cr, r, &result);
22911 }
22912 else
22913 return 0;
22914 }
22915
22916
22917 /* EXPORT:
22918 Draw a vertical window border to the right of window W if W doesn't
22919 have vertical scroll bars. */
22920
22921 void
22922 x_draw_vertical_border (w)
22923 struct window *w;
22924 {
22925 /* We could do better, if we knew what type of scroll-bar the adjacent
22926 windows (on either side) have... But we don't :-(
22927 However, I think this works ok. ++KFS 2003-04-25 */
22928
22929 /* Redraw borders between horizontally adjacent windows. Don't
22930 do it for frames with vertical scroll bars because either the
22931 right scroll bar of a window, or the left scroll bar of its
22932 neighbor will suffice as a border. */
22933 if (FRAME_HAS_VERTICAL_SCROLL_BARS (XFRAME (w->frame)))
22934 return;
22935
22936 if (!WINDOW_RIGHTMOST_P (w)
22937 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
22938 {
22939 int x0, x1, y0, y1;
22940
22941 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
22942 y1 -= 1;
22943
22944 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
22945 x1 -= 1;
22946
22947 rif->draw_vertical_window_border (w, x1, y0, y1);
22948 }
22949 else if (!WINDOW_LEFTMOST_P (w)
22950 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
22951 {
22952 int x0, x1, y0, y1;
22953
22954 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
22955 y1 -= 1;
22956
22957 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
22958 x0 -= 1;
22959
22960 rif->draw_vertical_window_border (w, x0, y0, y1);
22961 }
22962 }
22963
22964
22965 /* Redraw the part of window W intersection rectangle FR. Pixel
22966 coordinates in FR are frame-relative. Call this function with
22967 input blocked. Value is non-zero if the exposure overwrites
22968 mouse-face. */
22969
22970 static int
22971 expose_window (w, fr)
22972 struct window *w;
22973 XRectangle *fr;
22974 {
22975 struct frame *f = XFRAME (w->frame);
22976 XRectangle wr, r;
22977 int mouse_face_overwritten_p = 0;
22978
22979 /* If window is not yet fully initialized, do nothing. This can
22980 happen when toolkit scroll bars are used and a window is split.
22981 Reconfiguring the scroll bar will generate an expose for a newly
22982 created window. */
22983 if (w->current_matrix == NULL)
22984 return 0;
22985
22986 /* When we're currently updating the window, display and current
22987 matrix usually don't agree. Arrange for a thorough display
22988 later. */
22989 if (w == updated_window)
22990 {
22991 SET_FRAME_GARBAGED (f);
22992 return 0;
22993 }
22994
22995 /* Frame-relative pixel rectangle of W. */
22996 wr.x = WINDOW_LEFT_EDGE_X (w);
22997 wr.y = WINDOW_TOP_EDGE_Y (w);
22998 wr.width = WINDOW_TOTAL_WIDTH (w);
22999 wr.height = WINDOW_TOTAL_HEIGHT (w);
23000
23001 if (x_intersect_rectangles (fr, &wr, &r))
23002 {
23003 int yb = window_text_bottom_y (w);
23004 struct glyph_row *row;
23005 int cursor_cleared_p;
23006 struct glyph_row *first_overlapping_row, *last_overlapping_row;
23007
23008 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
23009 r.x, r.y, r.width, r.height));
23010
23011 /* Convert to window coordinates. */
23012 r.x -= WINDOW_LEFT_EDGE_X (w);
23013 r.y -= WINDOW_TOP_EDGE_Y (w);
23014
23015 /* Turn off the cursor. */
23016 if (!w->pseudo_window_p
23017 && phys_cursor_in_rect_p (w, &r))
23018 {
23019 x_clear_cursor (w);
23020 cursor_cleared_p = 1;
23021 }
23022 else
23023 cursor_cleared_p = 0;
23024
23025 /* Update lines intersecting rectangle R. */
23026 first_overlapping_row = last_overlapping_row = NULL;
23027 for (row = w->current_matrix->rows;
23028 row->enabled_p;
23029 ++row)
23030 {
23031 int y0 = row->y;
23032 int y1 = MATRIX_ROW_BOTTOM_Y (row);
23033
23034 if ((y0 >= r.y && y0 < r.y + r.height)
23035 || (y1 > r.y && y1 < r.y + r.height)
23036 || (r.y >= y0 && r.y < y1)
23037 || (r.y + r.height > y0 && r.y + r.height < y1))
23038 {
23039 /* A header line may be overlapping, but there is no need
23040 to fix overlapping areas for them. KFS 2005-02-12 */
23041 if (row->overlapping_p && !row->mode_line_p)
23042 {
23043 if (first_overlapping_row == NULL)
23044 first_overlapping_row = row;
23045 last_overlapping_row = row;
23046 }
23047
23048 if (expose_line (w, row, &r))
23049 mouse_face_overwritten_p = 1;
23050 }
23051
23052 if (y1 >= yb)
23053 break;
23054 }
23055
23056 /* Display the mode line if there is one. */
23057 if (WINDOW_WANTS_MODELINE_P (w)
23058 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
23059 row->enabled_p)
23060 && row->y < r.y + r.height)
23061 {
23062 if (expose_line (w, row, &r))
23063 mouse_face_overwritten_p = 1;
23064 }
23065
23066 if (!w->pseudo_window_p)
23067 {
23068 /* Fix the display of overlapping rows. */
23069 if (first_overlapping_row)
23070 expose_overlaps (w, first_overlapping_row, last_overlapping_row);
23071
23072 /* Draw border between windows. */
23073 x_draw_vertical_border (w);
23074
23075 /* Turn the cursor on again. */
23076 if (cursor_cleared_p)
23077 update_window_cursor (w, 1);
23078 }
23079 }
23080
23081 return mouse_face_overwritten_p;
23082 }
23083
23084
23085
23086 /* Redraw (parts) of all windows in the window tree rooted at W that
23087 intersect R. R contains frame pixel coordinates. Value is
23088 non-zero if the exposure overwrites mouse-face. */
23089
23090 static int
23091 expose_window_tree (w, r)
23092 struct window *w;
23093 XRectangle *r;
23094 {
23095 struct frame *f = XFRAME (w->frame);
23096 int mouse_face_overwritten_p = 0;
23097
23098 while (w && !FRAME_GARBAGED_P (f))
23099 {
23100 if (!NILP (w->hchild))
23101 mouse_face_overwritten_p
23102 |= expose_window_tree (XWINDOW (w->hchild), r);
23103 else if (!NILP (w->vchild))
23104 mouse_face_overwritten_p
23105 |= expose_window_tree (XWINDOW (w->vchild), r);
23106 else
23107 mouse_face_overwritten_p |= expose_window (w, r);
23108
23109 w = NILP (w->next) ? NULL : XWINDOW (w->next);
23110 }
23111
23112 return mouse_face_overwritten_p;
23113 }
23114
23115
23116 /* EXPORT:
23117 Redisplay an exposed area of frame F. X and Y are the upper-left
23118 corner of the exposed rectangle. W and H are width and height of
23119 the exposed area. All are pixel values. W or H zero means redraw
23120 the entire frame. */
23121
23122 void
23123 expose_frame (f, x, y, w, h)
23124 struct frame *f;
23125 int x, y, w, h;
23126 {
23127 XRectangle r;
23128 int mouse_face_overwritten_p = 0;
23129
23130 TRACE ((stderr, "expose_frame "));
23131
23132 /* No need to redraw if frame will be redrawn soon. */
23133 if (FRAME_GARBAGED_P (f))
23134 {
23135 TRACE ((stderr, " garbaged\n"));
23136 return;
23137 }
23138
23139 /* If basic faces haven't been realized yet, there is no point in
23140 trying to redraw anything. This can happen when we get an expose
23141 event while Emacs is starting, e.g. by moving another window. */
23142 if (FRAME_FACE_CACHE (f) == NULL
23143 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
23144 {
23145 TRACE ((stderr, " no faces\n"));
23146 return;
23147 }
23148
23149 if (w == 0 || h == 0)
23150 {
23151 r.x = r.y = 0;
23152 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
23153 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
23154 }
23155 else
23156 {
23157 r.x = x;
23158 r.y = y;
23159 r.width = w;
23160 r.height = h;
23161 }
23162
23163 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
23164 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
23165
23166 if (WINDOWP (f->tool_bar_window))
23167 mouse_face_overwritten_p
23168 |= expose_window (XWINDOW (f->tool_bar_window), &r);
23169
23170 #ifdef HAVE_X_WINDOWS
23171 #ifndef MSDOS
23172 #ifndef USE_X_TOOLKIT
23173 if (WINDOWP (f->menu_bar_window))
23174 mouse_face_overwritten_p
23175 |= expose_window (XWINDOW (f->menu_bar_window), &r);
23176 #endif /* not USE_X_TOOLKIT */
23177 #endif
23178 #endif
23179
23180 /* Some window managers support a focus-follows-mouse style with
23181 delayed raising of frames. Imagine a partially obscured frame,
23182 and moving the mouse into partially obscured mouse-face on that
23183 frame. The visible part of the mouse-face will be highlighted,
23184 then the WM raises the obscured frame. With at least one WM, KDE
23185 2.1, Emacs is not getting any event for the raising of the frame
23186 (even tried with SubstructureRedirectMask), only Expose events.
23187 These expose events will draw text normally, i.e. not
23188 highlighted. Which means we must redo the highlight here.
23189 Subsume it under ``we love X''. --gerd 2001-08-15 */
23190 /* Included in Windows version because Windows most likely does not
23191 do the right thing if any third party tool offers
23192 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
23193 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
23194 {
23195 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
23196 if (f == dpyinfo->mouse_face_mouse_frame)
23197 {
23198 int x = dpyinfo->mouse_face_mouse_x;
23199 int y = dpyinfo->mouse_face_mouse_y;
23200 clear_mouse_face (dpyinfo);
23201 note_mouse_highlight (f, x, y);
23202 }
23203 }
23204 }
23205
23206
23207 /* EXPORT:
23208 Determine the intersection of two rectangles R1 and R2. Return
23209 the intersection in *RESULT. Value is non-zero if RESULT is not
23210 empty. */
23211
23212 int
23213 x_intersect_rectangles (r1, r2, result)
23214 XRectangle *r1, *r2, *result;
23215 {
23216 XRectangle *left, *right;
23217 XRectangle *upper, *lower;
23218 int intersection_p = 0;
23219
23220 /* Rearrange so that R1 is the left-most rectangle. */
23221 if (r1->x < r2->x)
23222 left = r1, right = r2;
23223 else
23224 left = r2, right = r1;
23225
23226 /* X0 of the intersection is right.x0, if this is inside R1,
23227 otherwise there is no intersection. */
23228 if (right->x <= left->x + left->width)
23229 {
23230 result->x = right->x;
23231
23232 /* The right end of the intersection is the minimum of the
23233 the right ends of left and right. */
23234 result->width = (min (left->x + left->width, right->x + right->width)
23235 - result->x);
23236
23237 /* Same game for Y. */
23238 if (r1->y < r2->y)
23239 upper = r1, lower = r2;
23240 else
23241 upper = r2, lower = r1;
23242
23243 /* The upper end of the intersection is lower.y0, if this is inside
23244 of upper. Otherwise, there is no intersection. */
23245 if (lower->y <= upper->y + upper->height)
23246 {
23247 result->y = lower->y;
23248
23249 /* The lower end of the intersection is the minimum of the lower
23250 ends of upper and lower. */
23251 result->height = (min (lower->y + lower->height,
23252 upper->y + upper->height)
23253 - result->y);
23254 intersection_p = 1;
23255 }
23256 }
23257
23258 return intersection_p;
23259 }
23260
23261 #endif /* HAVE_WINDOW_SYSTEM */
23262
23263 \f
23264 /***********************************************************************
23265 Initialization
23266 ***********************************************************************/
23267
23268 void
23269 syms_of_xdisp ()
23270 {
23271 Vwith_echo_area_save_vector = Qnil;
23272 staticpro (&Vwith_echo_area_save_vector);
23273
23274 Vmessage_stack = Qnil;
23275 staticpro (&Vmessage_stack);
23276
23277 Qinhibit_redisplay = intern ("inhibit-redisplay");
23278 staticpro (&Qinhibit_redisplay);
23279
23280 message_dolog_marker1 = Fmake_marker ();
23281 staticpro (&message_dolog_marker1);
23282 message_dolog_marker2 = Fmake_marker ();
23283 staticpro (&message_dolog_marker2);
23284 message_dolog_marker3 = Fmake_marker ();
23285 staticpro (&message_dolog_marker3);
23286
23287 #if GLYPH_DEBUG
23288 defsubr (&Sdump_frame_glyph_matrix);
23289 defsubr (&Sdump_glyph_matrix);
23290 defsubr (&Sdump_glyph_row);
23291 defsubr (&Sdump_tool_bar_row);
23292 defsubr (&Strace_redisplay);
23293 defsubr (&Strace_to_stderr);
23294 #endif
23295 #ifdef HAVE_WINDOW_SYSTEM
23296 defsubr (&Stool_bar_lines_needed);
23297 defsubr (&Slookup_image_map);
23298 #endif
23299 defsubr (&Sformat_mode_line);
23300
23301 staticpro (&Qmenu_bar_update_hook);
23302 Qmenu_bar_update_hook = intern ("menu-bar-update-hook");
23303
23304 staticpro (&Qoverriding_terminal_local_map);
23305 Qoverriding_terminal_local_map = intern ("overriding-terminal-local-map");
23306
23307 staticpro (&Qoverriding_local_map);
23308 Qoverriding_local_map = intern ("overriding-local-map");
23309
23310 staticpro (&Qwindow_scroll_functions);
23311 Qwindow_scroll_functions = intern ("window-scroll-functions");
23312
23313 staticpro (&Qredisplay_end_trigger_functions);
23314 Qredisplay_end_trigger_functions = intern ("redisplay-end-trigger-functions");
23315
23316 staticpro (&Qinhibit_point_motion_hooks);
23317 Qinhibit_point_motion_hooks = intern ("inhibit-point-motion-hooks");
23318
23319 QCdata = intern (":data");
23320 staticpro (&QCdata);
23321 Qdisplay = intern ("display");
23322 staticpro (&Qdisplay);
23323 Qspace_width = intern ("space-width");
23324 staticpro (&Qspace_width);
23325 Qraise = intern ("raise");
23326 staticpro (&Qraise);
23327 Qslice = intern ("slice");
23328 staticpro (&Qslice);
23329 Qspace = intern ("space");
23330 staticpro (&Qspace);
23331 Qmargin = intern ("margin");
23332 staticpro (&Qmargin);
23333 Qpointer = intern ("pointer");
23334 staticpro (&Qpointer);
23335 Qleft_margin = intern ("left-margin");
23336 staticpro (&Qleft_margin);
23337 Qright_margin = intern ("right-margin");
23338 staticpro (&Qright_margin);
23339 Qcenter = intern ("center");
23340 staticpro (&Qcenter);
23341 Qline_height = intern ("line-height");
23342 staticpro (&Qline_height);
23343 QCalign_to = intern (":align-to");
23344 staticpro (&QCalign_to);
23345 QCrelative_width = intern (":relative-width");
23346 staticpro (&QCrelative_width);
23347 QCrelative_height = intern (":relative-height");
23348 staticpro (&QCrelative_height);
23349 QCeval = intern (":eval");
23350 staticpro (&QCeval);
23351 QCpropertize = intern (":propertize");
23352 staticpro (&QCpropertize);
23353 QCfile = intern (":file");
23354 staticpro (&QCfile);
23355 Qfontified = intern ("fontified");
23356 staticpro (&Qfontified);
23357 Qfontification_functions = intern ("fontification-functions");
23358 staticpro (&Qfontification_functions);
23359 Qtrailing_whitespace = intern ("trailing-whitespace");
23360 staticpro (&Qtrailing_whitespace);
23361 Qescape_glyph = intern ("escape-glyph");
23362 staticpro (&Qescape_glyph);
23363 Qnobreak_space = intern ("nobreak-space");
23364 staticpro (&Qnobreak_space);
23365 Qimage = intern ("image");
23366 staticpro (&Qimage);
23367 QCmap = intern (":map");
23368 staticpro (&QCmap);
23369 QCpointer = intern (":pointer");
23370 staticpro (&QCpointer);
23371 Qrect = intern ("rect");
23372 staticpro (&Qrect);
23373 Qcircle = intern ("circle");
23374 staticpro (&Qcircle);
23375 Qpoly = intern ("poly");
23376 staticpro (&Qpoly);
23377 Qmessage_truncate_lines = intern ("message-truncate-lines");
23378 staticpro (&Qmessage_truncate_lines);
23379 Qgrow_only = intern ("grow-only");
23380 staticpro (&Qgrow_only);
23381 Qinhibit_menubar_update = intern ("inhibit-menubar-update");
23382 staticpro (&Qinhibit_menubar_update);
23383 Qinhibit_eval_during_redisplay = intern ("inhibit-eval-during-redisplay");
23384 staticpro (&Qinhibit_eval_during_redisplay);
23385 Qposition = intern ("position");
23386 staticpro (&Qposition);
23387 Qbuffer_position = intern ("buffer-position");
23388 staticpro (&Qbuffer_position);
23389 Qobject = intern ("object");
23390 staticpro (&Qobject);
23391 Qbar = intern ("bar");
23392 staticpro (&Qbar);
23393 Qhbar = intern ("hbar");
23394 staticpro (&Qhbar);
23395 Qbox = intern ("box");
23396 staticpro (&Qbox);
23397 Qhollow = intern ("hollow");
23398 staticpro (&Qhollow);
23399 Qhand = intern ("hand");
23400 staticpro (&Qhand);
23401 Qarrow = intern ("arrow");
23402 staticpro (&Qarrow);
23403 Qtext = intern ("text");
23404 staticpro (&Qtext);
23405 Qrisky_local_variable = intern ("risky-local-variable");
23406 staticpro (&Qrisky_local_variable);
23407 Qinhibit_free_realized_faces = intern ("inhibit-free-realized-faces");
23408 staticpro (&Qinhibit_free_realized_faces);
23409
23410 list_of_error = Fcons (Fcons (intern ("error"),
23411 Fcons (intern ("void-variable"), Qnil)),
23412 Qnil);
23413 staticpro (&list_of_error);
23414
23415 Qlast_arrow_position = intern ("last-arrow-position");
23416 staticpro (&Qlast_arrow_position);
23417 Qlast_arrow_string = intern ("last-arrow-string");
23418 staticpro (&Qlast_arrow_string);
23419
23420 Qoverlay_arrow_string = intern ("overlay-arrow-string");
23421 staticpro (&Qoverlay_arrow_string);
23422 Qoverlay_arrow_bitmap = intern ("overlay-arrow-bitmap");
23423 staticpro (&Qoverlay_arrow_bitmap);
23424
23425 echo_buffer[0] = echo_buffer[1] = Qnil;
23426 staticpro (&echo_buffer[0]);
23427 staticpro (&echo_buffer[1]);
23428
23429 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
23430 staticpro (&echo_area_buffer[0]);
23431 staticpro (&echo_area_buffer[1]);
23432
23433 Vmessages_buffer_name = build_string ("*Messages*");
23434 staticpro (&Vmessages_buffer_name);
23435
23436 mode_line_proptrans_alist = Qnil;
23437 staticpro (&mode_line_proptrans_alist);
23438 mode_line_string_list = Qnil;
23439 staticpro (&mode_line_string_list);
23440 mode_line_string_face = Qnil;
23441 staticpro (&mode_line_string_face);
23442 mode_line_string_face_prop = Qnil;
23443 staticpro (&mode_line_string_face_prop);
23444 Vmode_line_unwind_vector = Qnil;
23445 staticpro (&Vmode_line_unwind_vector);
23446
23447 help_echo_string = Qnil;
23448 staticpro (&help_echo_string);
23449 help_echo_object = Qnil;
23450 staticpro (&help_echo_object);
23451 help_echo_window = Qnil;
23452 staticpro (&help_echo_window);
23453 previous_help_echo_string = Qnil;
23454 staticpro (&previous_help_echo_string);
23455 help_echo_pos = -1;
23456
23457 #ifdef HAVE_WINDOW_SYSTEM
23458 DEFVAR_BOOL ("x-stretch-cursor", &x_stretch_cursor_p,
23459 doc: /* *Non-nil means draw block cursor as wide as the glyph under it.
23460 For example, if a block cursor is over a tab, it will be drawn as
23461 wide as that tab on the display. */);
23462 x_stretch_cursor_p = 0;
23463 #endif
23464
23465 DEFVAR_LISP ("show-trailing-whitespace", &Vshow_trailing_whitespace,
23466 doc: /* *Non-nil means highlight trailing whitespace.
23467 The face used for trailing whitespace is `trailing-whitespace'. */);
23468 Vshow_trailing_whitespace = Qnil;
23469
23470 DEFVAR_LISP ("nobreak-char-display", &Vnobreak_char_display,
23471 doc: /* *Control highlighting of nobreak space and soft hyphen.
23472 A value of t means highlight the character itself (for nobreak space,
23473 use face `nobreak-space').
23474 A value of nil means no highlighting.
23475 Other values mean display the escape glyph followed by an ordinary
23476 space or ordinary hyphen. */);
23477 Vnobreak_char_display = Qt;
23478
23479 DEFVAR_LISP ("void-text-area-pointer", &Vvoid_text_area_pointer,
23480 doc: /* *The pointer shape to show in void text areas.
23481 A value of nil means to show the text pointer. Other options are `arrow',
23482 `text', `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
23483 Vvoid_text_area_pointer = Qarrow;
23484
23485 DEFVAR_LISP ("inhibit-redisplay", &Vinhibit_redisplay,
23486 doc: /* Non-nil means don't actually do any redisplay.
23487 This is used for internal purposes. */);
23488 Vinhibit_redisplay = Qnil;
23489
23490 DEFVAR_LISP ("global-mode-string", &Vglobal_mode_string,
23491 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
23492 Vglobal_mode_string = Qnil;
23493
23494 DEFVAR_LISP ("overlay-arrow-position", &Voverlay_arrow_position,
23495 doc: /* Marker for where to display an arrow on top of the buffer text.
23496 This must be the beginning of a line in order to work.
23497 See also `overlay-arrow-string'. */);
23498 Voverlay_arrow_position = Qnil;
23499
23500 DEFVAR_LISP ("overlay-arrow-string", &Voverlay_arrow_string,
23501 doc: /* String to display as an arrow in non-window frames.
23502 See also `overlay-arrow-position'. */);
23503 Voverlay_arrow_string = build_string ("=>");
23504
23505 DEFVAR_LISP ("overlay-arrow-variable-list", &Voverlay_arrow_variable_list,
23506 doc: /* List of variables (symbols) which hold markers for overlay arrows.
23507 The symbols on this list are examined during redisplay to determine
23508 where to display overlay arrows. */);
23509 Voverlay_arrow_variable_list
23510 = Fcons (intern ("overlay-arrow-position"), Qnil);
23511
23512 DEFVAR_INT ("scroll-step", &scroll_step,
23513 doc: /* *The number of lines to try scrolling a window by when point moves out.
23514 If that fails to bring point back on frame, point is centered instead.
23515 If this is zero, point is always centered after it moves off frame.
23516 If you want scrolling to always be a line at a time, you should set
23517 `scroll-conservatively' to a large value rather than set this to 1. */);
23518
23519 DEFVAR_INT ("scroll-conservatively", &scroll_conservatively,
23520 doc: /* *Scroll up to this many lines, to bring point back on screen.
23521 A value of zero means to scroll the text to center point vertically
23522 in the window. */);
23523 scroll_conservatively = 0;
23524
23525 DEFVAR_INT ("scroll-margin", &scroll_margin,
23526 doc: /* *Number of lines of margin at the top and bottom of a window.
23527 Recenter the window whenever point gets within this many lines
23528 of the top or bottom of the window. */);
23529 scroll_margin = 0;
23530
23531 DEFVAR_LISP ("display-pixels-per-inch", &Vdisplay_pixels_per_inch,
23532 doc: /* Pixels per inch value for non-window system displays.
23533 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
23534 Vdisplay_pixels_per_inch = make_float (72.0);
23535
23536 #if GLYPH_DEBUG
23537 DEFVAR_INT ("debug-end-pos", &debug_end_pos, doc: /* Don't ask. */);
23538 #endif
23539
23540 DEFVAR_BOOL ("truncate-partial-width-windows",
23541 &truncate_partial_width_windows,
23542 doc: /* *Non-nil means truncate lines in all windows less than full frame wide. */);
23543 truncate_partial_width_windows = 1;
23544
23545 DEFVAR_BOOL ("mode-line-inverse-video", &mode_line_inverse_video,
23546 doc: /* nil means display the mode-line/header-line/menu-bar in the default face.
23547 Any other value means to use the appropriate face, `mode-line',
23548 `header-line', or `menu' respectively. */);
23549 mode_line_inverse_video = 1;
23550
23551 DEFVAR_LISP ("line-number-display-limit", &Vline_number_display_limit,
23552 doc: /* *Maximum buffer size for which line number should be displayed.
23553 If the buffer is bigger than this, the line number does not appear
23554 in the mode line. A value of nil means no limit. */);
23555 Vline_number_display_limit = Qnil;
23556
23557 DEFVAR_INT ("line-number-display-limit-width",
23558 &line_number_display_limit_width,
23559 doc: /* *Maximum line width (in characters) for line number display.
23560 If the average length of the lines near point is bigger than this, then the
23561 line number may be omitted from the mode line. */);
23562 line_number_display_limit_width = 200;
23563
23564 DEFVAR_BOOL ("highlight-nonselected-windows", &highlight_nonselected_windows,
23565 doc: /* *Non-nil means highlight region even in nonselected windows. */);
23566 highlight_nonselected_windows = 0;
23567
23568 DEFVAR_BOOL ("multiple-frames", &multiple_frames,
23569 doc: /* Non-nil if more than one frame is visible on this display.
23570 Minibuffer-only frames don't count, but iconified frames do.
23571 This variable is not guaranteed to be accurate except while processing
23572 `frame-title-format' and `icon-title-format'. */);
23573
23574 DEFVAR_LISP ("frame-title-format", &Vframe_title_format,
23575 doc: /* Template for displaying the title bar of visible frames.
23576 \(Assuming the window manager supports this feature.)
23577 This variable has the same structure as `mode-line-format' (which see),
23578 and is used only on frames for which no explicit name has been set
23579 \(see `modify-frame-parameters'). */);
23580
23581 DEFVAR_LISP ("icon-title-format", &Vicon_title_format,
23582 doc: /* Template for displaying the title bar of an iconified frame.
23583 \(Assuming the window manager supports this feature.)
23584 This variable has the same structure as `mode-line-format' (which see),
23585 and is used only on frames for which no explicit name has been set
23586 \(see `modify-frame-parameters'). */);
23587 Vicon_title_format
23588 = Vframe_title_format
23589 = Fcons (intern ("multiple-frames"),
23590 Fcons (build_string ("%b"),
23591 Fcons (Fcons (empty_string,
23592 Fcons (intern ("invocation-name"),
23593 Fcons (build_string ("@"),
23594 Fcons (intern ("system-name"),
23595 Qnil)))),
23596 Qnil)));
23597
23598 DEFVAR_LISP ("message-log-max", &Vmessage_log_max,
23599 doc: /* Maximum number of lines to keep in the message log buffer.
23600 If nil, disable message logging. If t, log messages but don't truncate
23601 the buffer when it becomes large. */);
23602 Vmessage_log_max = make_number (50);
23603
23604 DEFVAR_LISP ("window-size-change-functions", &Vwindow_size_change_functions,
23605 doc: /* Functions called before redisplay, if window sizes have changed.
23606 The value should be a list of functions that take one argument.
23607 Just before redisplay, for each frame, if any of its windows have changed
23608 size since the last redisplay, or have been split or deleted,
23609 all the functions in the list are called, with the frame as argument. */);
23610 Vwindow_size_change_functions = Qnil;
23611
23612 DEFVAR_LISP ("window-scroll-functions", &Vwindow_scroll_functions,
23613 doc: /* List of functions to call before redisplaying a window with scrolling.
23614 Each function is called with two arguments, the window
23615 and its new display-start position. Note that the value of `window-end'
23616 is not valid when these functions are called. */);
23617 Vwindow_scroll_functions = Qnil;
23618
23619 DEFVAR_LISP ("redisplay-end-trigger-functions", &Vredisplay_end_trigger_functions,
23620 doc: /* Functions called when redisplay of a window reaches the end trigger.
23621 Each function is called with two arguments, the window and the end trigger value.
23622 See `set-window-redisplay-end-trigger'. */);
23623 Vredisplay_end_trigger_functions = Qnil;
23624
23625 DEFVAR_BOOL ("mouse-autoselect-window", &mouse_autoselect_window,
23626 doc: /* *Non-nil means autoselect window with mouse pointer. */);
23627 mouse_autoselect_window = 0;
23628
23629 DEFVAR_BOOL ("auto-resize-tool-bars", &auto_resize_tool_bars_p,
23630 doc: /* *Non-nil means automatically resize tool-bars.
23631 This increases a tool-bar's height if not all tool-bar items are visible.
23632 It decreases a tool-bar's height when it would display blank lines
23633 otherwise. */);
23634 auto_resize_tool_bars_p = 1;
23635
23636 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", &auto_raise_tool_bar_buttons_p,
23637 doc: /* *Non-nil means raise tool-bar buttons when the mouse moves over them. */);
23638 auto_raise_tool_bar_buttons_p = 1;
23639
23640 DEFVAR_BOOL ("make-cursor-line-fully-visible", &make_cursor_line_fully_visible_p,
23641 doc: /* *Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
23642 make_cursor_line_fully_visible_p = 1;
23643
23644 DEFVAR_LISP ("tool-bar-button-margin", &Vtool_bar_button_margin,
23645 doc: /* *Margin around tool-bar buttons in pixels.
23646 If an integer, use that for both horizontal and vertical margins.
23647 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
23648 HORZ specifying the horizontal margin, and VERT specifying the
23649 vertical margin. */);
23650 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
23651
23652 DEFVAR_INT ("tool-bar-button-relief", &tool_bar_button_relief,
23653 doc: /* *Relief thickness of tool-bar buttons. */);
23654 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
23655
23656 DEFVAR_LISP ("fontification-functions", &Vfontification_functions,
23657 doc: /* List of functions to call to fontify regions of text.
23658 Each function is called with one argument POS. Functions must
23659 fontify a region starting at POS in the current buffer, and give
23660 fontified regions the property `fontified'. */);
23661 Vfontification_functions = Qnil;
23662 Fmake_variable_buffer_local (Qfontification_functions);
23663
23664 DEFVAR_BOOL ("unibyte-display-via-language-environment",
23665 &unibyte_display_via_language_environment,
23666 doc: /* *Non-nil means display unibyte text according to language environment.
23667 Specifically this means that unibyte non-ASCII characters
23668 are displayed by converting them to the equivalent multibyte characters
23669 according to the current language environment. As a result, they are
23670 displayed according to the current fontset. */);
23671 unibyte_display_via_language_environment = 0;
23672
23673 DEFVAR_LISP ("max-mini-window-height", &Vmax_mini_window_height,
23674 doc: /* *Maximum height for resizing mini-windows.
23675 If a float, it specifies a fraction of the mini-window frame's height.
23676 If an integer, it specifies a number of lines. */);
23677 Vmax_mini_window_height = make_float (0.25);
23678
23679 DEFVAR_LISP ("resize-mini-windows", &Vresize_mini_windows,
23680 doc: /* *How to resize mini-windows.
23681 A value of nil means don't automatically resize mini-windows.
23682 A value of t means resize them to fit the text displayed in them.
23683 A value of `grow-only', the default, means let mini-windows grow
23684 only, until their display becomes empty, at which point the windows
23685 go back to their normal size. */);
23686 Vresize_mini_windows = Qgrow_only;
23687
23688 DEFVAR_LISP ("blink-cursor-alist", &Vblink_cursor_alist,
23689 doc: /* Alist specifying how to blink the cursor off.
23690 Each element has the form (ON-STATE . OFF-STATE). Whenever the
23691 `cursor-type' frame-parameter or variable equals ON-STATE,
23692 comparing using `equal', Emacs uses OFF-STATE to specify
23693 how to blink it off. ON-STATE and OFF-STATE are values for
23694 the `cursor-type' frame parameter.
23695
23696 If a frame's ON-STATE has no entry in this list,
23697 the frame's other specifications determine how to blink the cursor off. */);
23698 Vblink_cursor_alist = Qnil;
23699
23700 DEFVAR_BOOL ("auto-hscroll-mode", &automatic_hscrolling_p,
23701 doc: /* *Non-nil means scroll the display automatically to make point visible. */);
23702 automatic_hscrolling_p = 1;
23703
23704 DEFVAR_INT ("hscroll-margin", &hscroll_margin,
23705 doc: /* *How many columns away from the window edge point is allowed to get
23706 before automatic hscrolling will horizontally scroll the window. */);
23707 hscroll_margin = 5;
23708
23709 DEFVAR_LISP ("hscroll-step", &Vhscroll_step,
23710 doc: /* *How many columns to scroll the window when point gets too close to the edge.
23711 When point is less than `automatic-hscroll-margin' columns from the window
23712 edge, automatic hscrolling will scroll the window by the amount of columns
23713 determined by this variable. If its value is a positive integer, scroll that
23714 many columns. If it's a positive floating-point number, it specifies the
23715 fraction of the window's width to scroll. If it's nil or zero, point will be
23716 centered horizontally after the scroll. Any other value, including negative
23717 numbers, are treated as if the value were zero.
23718
23719 Automatic hscrolling always moves point outside the scroll margin, so if
23720 point was more than scroll step columns inside the margin, the window will
23721 scroll more than the value given by the scroll step.
23722
23723 Note that the lower bound for automatic hscrolling specified by `scroll-left'
23724 and `scroll-right' overrides this variable's effect. */);
23725 Vhscroll_step = make_number (0);
23726
23727 DEFVAR_BOOL ("message-truncate-lines", &message_truncate_lines,
23728 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
23729 Bind this around calls to `message' to let it take effect. */);
23730 message_truncate_lines = 0;
23731
23732 DEFVAR_LISP ("menu-bar-update-hook", &Vmenu_bar_update_hook,
23733 doc: /* Normal hook run to update the menu bar definitions.
23734 Redisplay runs this hook before it redisplays the menu bar.
23735 This is used to update submenus such as Buffers,
23736 whose contents depend on various data. */);
23737 Vmenu_bar_update_hook = Qnil;
23738
23739 DEFVAR_BOOL ("inhibit-menubar-update", &inhibit_menubar_update,
23740 doc: /* Non-nil means don't update menu bars. Internal use only. */);
23741 inhibit_menubar_update = 0;
23742
23743 DEFVAR_BOOL ("inhibit-eval-during-redisplay", &inhibit_eval_during_redisplay,
23744 doc: /* Non-nil means don't eval Lisp during redisplay. */);
23745 inhibit_eval_during_redisplay = 0;
23746
23747 DEFVAR_BOOL ("inhibit-free-realized-faces", &inhibit_free_realized_faces,
23748 doc: /* Non-nil means don't free realized faces. Internal use only. */);
23749 inhibit_free_realized_faces = 0;
23750
23751 #if GLYPH_DEBUG
23752 DEFVAR_BOOL ("inhibit-try-window-id", &inhibit_try_window_id,
23753 doc: /* Inhibit try_window_id display optimization. */);
23754 inhibit_try_window_id = 0;
23755
23756 DEFVAR_BOOL ("inhibit-try-window-reusing", &inhibit_try_window_reusing,
23757 doc: /* Inhibit try_window_reusing display optimization. */);
23758 inhibit_try_window_reusing = 0;
23759
23760 DEFVAR_BOOL ("inhibit-try-cursor-movement", &inhibit_try_cursor_movement,
23761 doc: /* Inhibit try_cursor_movement display optimization. */);
23762 inhibit_try_cursor_movement = 0;
23763 #endif /* GLYPH_DEBUG */
23764 }
23765
23766
23767 /* Initialize this module when Emacs starts. */
23768
23769 void
23770 init_xdisp ()
23771 {
23772 Lisp_Object root_window;
23773 struct window *mini_w;
23774
23775 current_header_line_height = current_mode_line_height = -1;
23776
23777 CHARPOS (this_line_start_pos) = 0;
23778
23779 mini_w = XWINDOW (minibuf_window);
23780 root_window = FRAME_ROOT_WINDOW (XFRAME (WINDOW_FRAME (mini_w)));
23781
23782 if (!noninteractive)
23783 {
23784 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (root_window)));
23785 int i;
23786
23787 XWINDOW (root_window)->top_line = make_number (FRAME_TOP_MARGIN (f));
23788 set_window_height (root_window,
23789 FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f),
23790 0);
23791 mini_w->top_line = make_number (FRAME_LINES (f) - 1);
23792 set_window_height (minibuf_window, 1, 0);
23793
23794 XWINDOW (root_window)->total_cols = make_number (FRAME_COLS (f));
23795 mini_w->total_cols = make_number (FRAME_COLS (f));
23796
23797 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
23798 scratch_glyph_row.glyphs[TEXT_AREA + 1]
23799 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
23800
23801 /* The default ellipsis glyphs `...'. */
23802 for (i = 0; i < 3; ++i)
23803 default_invis_vector[i] = make_number ('.');
23804 }
23805
23806 {
23807 /* Allocate the buffer for frame titles.
23808 Also used for `format-mode-line'. */
23809 int size = 100;
23810 mode_line_noprop_buf = (char *) xmalloc (size);
23811 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
23812 mode_line_noprop_ptr = mode_line_noprop_buf;
23813 mode_line_target = MODE_LINE_DISPLAY;
23814 }
23815
23816 help_echo_showing_p = 0;
23817 }
23818
23819
23820 /* arch-tag: eacc864d-bb6a-4b74-894a-1a4399a1358b
23821 (do not change this comment) */