]> code.delx.au - gnu-emacs/blob - src/xdisp.c
(cp1125): Set :ascii-compatible-p property to t.
[gnu-emacs] / src / xdisp.c
1 /* Display generation from window structure and buffer text.
2 Copyright (C) 1985, 1986, 1987, 1988, 1993, 1994, 1995, 1997, 1998, 1999,
3 2000, 2001, 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
4
5 This file is part of GNU Emacs.
6
7 GNU Emacs is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
11
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs; see the file COPYING. If not, write to
19 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 Boston, MA 02110-1301, USA. */
21
22 /* New redisplay written by Gerd Moellmann <gerd@gnu.org>.
23
24 Redisplay.
25
26 Emacs separates the task of updating the display from code
27 modifying global state, e.g. buffer text. This way functions
28 operating on buffers don't also have to be concerned with updating
29 the display.
30
31 Updating the display is triggered by the Lisp interpreter when it
32 decides it's time to do it. This is done either automatically for
33 you as part of the interpreter's command loop or as the result of
34 calling Lisp functions like `sit-for'. The C function `redisplay'
35 in xdisp.c is the only entry into the inner redisplay code. (Or,
36 let's say almost---see the description of direct update
37 operations, below.)
38
39 The following diagram shows how redisplay code is invoked. As you
40 can see, Lisp calls redisplay and vice versa. Under window systems
41 like X, some portions of the redisplay code are also called
42 asynchronously during mouse movement or expose events. It is very
43 important that these code parts do NOT use the C library (malloc,
44 free) because many C libraries under Unix are not reentrant. They
45 may also NOT call functions of the Lisp interpreter which could
46 change the interpreter's state. If you don't follow these rules,
47 you will encounter bugs which are very hard to explain.
48
49 (Direct functions, see below)
50 direct_output_for_insert,
51 direct_forward_char (dispnew.c)
52 +---------------------------------+
53 | |
54 | V
55 +--------------+ redisplay +----------------+
56 | Lisp machine |---------------->| Redisplay code |<--+
57 +--------------+ (xdisp.c) +----------------+ |
58 ^ | |
59 +----------------------------------+ |
60 Don't use this path when called |
61 asynchronously! |
62 |
63 expose_window (asynchronous) |
64 |
65 X expose events -----+
66
67 What does redisplay do? Obviously, it has to figure out somehow what
68 has been changed since the last time the display has been updated,
69 and to make these changes visible. Preferably it would do that in
70 a moderately intelligent way, i.e. fast.
71
72 Changes in buffer text can be deduced from window and buffer
73 structures, and from some global variables like `beg_unchanged' and
74 `end_unchanged'. The contents of the display are additionally
75 recorded in a `glyph matrix', a two-dimensional matrix of glyph
76 structures. Each row in such a matrix corresponds to a line on the
77 display, and each glyph in a row corresponds to a column displaying
78 a character, an image, or what else. This matrix is called the
79 `current glyph matrix' or `current matrix' in redisplay
80 terminology.
81
82 For buffer parts that have been changed since the last update, a
83 second glyph matrix is constructed, the so called `desired glyph
84 matrix' or short `desired matrix'. Current and desired matrix are
85 then compared to find a cheap way to update the display, e.g. by
86 reusing part of the display by scrolling lines.
87
88
89 Direct operations.
90
91 You will find a lot of redisplay optimizations when you start
92 looking at the innards of redisplay. The overall goal of all these
93 optimizations is to make redisplay fast because it is done
94 frequently.
95
96 Two optimizations are not found in xdisp.c. These are the direct
97 operations mentioned above. As the name suggests they follow a
98 different principle than the rest of redisplay. Instead of
99 building a desired matrix and then comparing it with the current
100 display, they perform their actions directly on the display and on
101 the current matrix.
102
103 One direct operation updates the display after one character has
104 been entered. The other one moves the cursor by one position
105 forward or backward. You find these functions under the names
106 `direct_output_for_insert' and `direct_output_forward_char' in
107 dispnew.c.
108
109
110 Desired matrices.
111
112 Desired matrices are always built per Emacs window. The function
113 `display_line' is the central function to look at if you are
114 interested. It constructs one row in a desired matrix given an
115 iterator structure containing both a buffer position and a
116 description of the environment in which the text is to be
117 displayed. But this is too early, read on.
118
119 Characters and pixmaps displayed for a range of buffer text depend
120 on various settings of buffers and windows, on overlays and text
121 properties, on display tables, on selective display. The good news
122 is that all this hairy stuff is hidden behind a small set of
123 interface functions taking an iterator structure (struct it)
124 argument.
125
126 Iteration over things to be displayed is then simple. It is
127 started by initializing an iterator with a call to init_iterator.
128 Calls to get_next_display_element fill the iterator structure with
129 relevant information about the next thing to display. Calls to
130 set_iterator_to_next move the iterator to the next thing.
131
132 Besides this, an iterator also contains information about the
133 display environment in which glyphs for display elements are to be
134 produced. It has fields for the width and height of the display,
135 the information whether long lines are truncated or continued, a
136 current X and Y position, and lots of other stuff you can better
137 see in dispextern.h.
138
139 Glyphs in a desired matrix are normally constructed in a loop
140 calling get_next_display_element and then produce_glyphs. The call
141 to produce_glyphs will fill the iterator structure with pixel
142 information about the element being displayed and at the same time
143 produce glyphs for it. If the display element fits on the line
144 being displayed, set_iterator_to_next is called next, otherwise the
145 glyphs produced are discarded.
146
147
148 Frame matrices.
149
150 That just couldn't be all, could it? What about terminal types not
151 supporting operations on sub-windows of the screen? To update the
152 display on such a terminal, window-based glyph matrices are not
153 well suited. To be able to reuse part of the display (scrolling
154 lines up and down), we must instead have a view of the whole
155 screen. This is what `frame matrices' are for. They are a trick.
156
157 Frames on terminals like above have a glyph pool. Windows on such
158 a frame sub-allocate their glyph memory from their frame's glyph
159 pool. The frame itself is given its own glyph matrices. By
160 coincidence---or maybe something else---rows in window glyph
161 matrices are slices of corresponding rows in frame matrices. Thus
162 writing to window matrices implicitly updates a frame matrix which
163 provides us with the view of the whole screen that we originally
164 wanted to have without having to move many bytes around. To be
165 honest, there is a little bit more done, but not much more. If you
166 plan to extend that code, take a look at dispnew.c. The function
167 build_frame_matrix is a good starting point. */
168
169 #include <config.h>
170 #include <stdio.h>
171
172 #include "lisp.h"
173 #include "keyboard.h"
174 #include "frame.h"
175 #include "window.h"
176 #include "termchar.h"
177 #include "dispextern.h"
178 #include "buffer.h"
179 #include "character.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;
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 static enum prop_handled handle_auto_composed_prop P_ ((struct it *));
740
741 /* Properties handled by iterators. */
742
743 static struct props it_props[] =
744 {
745 {&Qauto_composed, AUTO_COMPOSED_PROP_IDX, handle_auto_composed_prop},
746 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
747 /* Handle `face' before `display' because some sub-properties of
748 `display' need to know the face. */
749 {&Qface, FACE_PROP_IDX, handle_face_prop},
750 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
751 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
752 {&Qcomposition, COMPOSITION_PROP_IDX, handle_composition_prop},
753 {NULL, 0, NULL}
754 };
755
756 /* Value is the position described by X. If X is a marker, value is
757 the marker_position of X. Otherwise, value is X. */
758
759 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
760
761 /* Enumeration returned by some move_it_.* functions internally. */
762
763 enum move_it_result
764 {
765 /* Not used. Undefined value. */
766 MOVE_UNDEFINED,
767
768 /* Move ended at the requested buffer position or ZV. */
769 MOVE_POS_MATCH_OR_ZV,
770
771 /* Move ended at the requested X pixel position. */
772 MOVE_X_REACHED,
773
774 /* Move within a line ended at the end of a line that must be
775 continued. */
776 MOVE_LINE_CONTINUED,
777
778 /* Move within a line ended at the end of a line that would
779 be displayed truncated. */
780 MOVE_LINE_TRUNCATED,
781
782 /* Move within a line ended at a line end. */
783 MOVE_NEWLINE_OR_CR
784 };
785
786 /* This counter is used to clear the face cache every once in a while
787 in redisplay_internal. It is incremented for each redisplay.
788 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
789 cleared. */
790
791 #define CLEAR_FACE_CACHE_COUNT 500
792 static int clear_face_cache_count;
793
794 /* Similarly for the image cache. */
795
796 #ifdef HAVE_WINDOW_SYSTEM
797 #define CLEAR_IMAGE_CACHE_COUNT 101
798 static int clear_image_cache_count;
799 #endif
800
801 /* Record the previous terminal frame we displayed. */
802
803 static struct frame *previous_terminal_frame;
804
805 /* Non-zero while redisplay_internal is in progress. */
806
807 int redisplaying_p;
808
809 /* Non-zero means don't free realized faces. Bound while freeing
810 realized faces is dangerous because glyph matrices might still
811 reference them. */
812
813 int inhibit_free_realized_faces;
814 Lisp_Object Qinhibit_free_realized_faces;
815
816 /* If a string, XTread_socket generates an event to display that string.
817 (The display is done in read_char.) */
818
819 Lisp_Object help_echo_string;
820 Lisp_Object help_echo_window;
821 Lisp_Object help_echo_object;
822 int help_echo_pos;
823
824 /* Temporary variable for XTread_socket. */
825
826 Lisp_Object previous_help_echo_string;
827
828 /* Null glyph slice */
829
830 static struct glyph_slice null_glyph_slice = { 0, 0, 0, 0 };
831
832 \f
833 /* Function prototypes. */
834
835 static void setup_for_ellipsis P_ ((struct it *, int));
836 static void mark_window_display_accurate_1 P_ ((struct window *, int));
837 static int single_display_spec_string_p P_ ((Lisp_Object, Lisp_Object));
838 static int display_prop_string_p P_ ((Lisp_Object, Lisp_Object));
839 static int cursor_row_p P_ ((struct window *, struct glyph_row *));
840 static int redisplay_mode_lines P_ ((Lisp_Object, int));
841 static char *decode_mode_spec_coding P_ ((Lisp_Object, char *, int));
842
843 #if 0
844 static int invisible_text_between_p P_ ((struct it *, int, int));
845 #endif
846
847 static void pint2str P_ ((char *, int, int));
848 static void pint2hrstr P_ ((char *, int, int));
849 static struct text_pos run_window_scroll_functions P_ ((Lisp_Object,
850 struct text_pos));
851 static void reconsider_clip_changes P_ ((struct window *, struct buffer *));
852 static int text_outside_line_unchanged_p P_ ((struct window *, int, int));
853 static void store_mode_line_noprop_char P_ ((char));
854 static int store_mode_line_noprop P_ ((const unsigned char *, int, int));
855 static void x_consider_frame_title P_ ((Lisp_Object));
856 static void handle_stop P_ ((struct it *));
857 static int tool_bar_lines_needed P_ ((struct frame *));
858 static int single_display_spec_intangible_p P_ ((Lisp_Object));
859 static void ensure_echo_area_buffers P_ ((void));
860 static Lisp_Object unwind_with_echo_area_buffer P_ ((Lisp_Object));
861 static Lisp_Object with_echo_area_buffer_unwind_data P_ ((struct window *));
862 static int with_echo_area_buffer P_ ((struct window *, int,
863 int (*) (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT),
864 EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
865 static void clear_garbaged_frames P_ ((void));
866 static int current_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
867 static int truncate_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
868 static int set_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
869 static int display_echo_area P_ ((struct window *));
870 static int display_echo_area_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
871 static int resize_mini_window_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
872 static Lisp_Object unwind_redisplay P_ ((Lisp_Object));
873 static int string_char_and_length P_ ((const unsigned char *, int, int *));
874 static struct text_pos display_prop_end P_ ((struct it *, Lisp_Object,
875 struct text_pos));
876 static int compute_window_start_on_continuation_line P_ ((struct window *));
877 static Lisp_Object safe_eval_handler P_ ((Lisp_Object));
878 static void insert_left_trunc_glyphs P_ ((struct it *));
879 static struct glyph_row *get_overlay_arrow_glyph_row P_ ((struct window *,
880 Lisp_Object));
881 static void extend_face_to_end_of_line P_ ((struct it *));
882 static int append_space_for_newline P_ ((struct it *, int));
883 static int cursor_row_fully_visible_p P_ ((struct window *, int, int));
884 static int try_scrolling P_ ((Lisp_Object, int, EMACS_INT, EMACS_INT, int, int));
885 static int try_cursor_movement P_ ((Lisp_Object, struct text_pos, int *));
886 static int trailing_whitespace_p P_ ((int));
887 static int message_log_check_duplicate P_ ((int, int, int, int));
888 static void push_it P_ ((struct it *));
889 static void pop_it P_ ((struct it *));
890 static void sync_frame_with_window_matrix_rows P_ ((struct window *));
891 static void select_frame_for_redisplay P_ ((Lisp_Object));
892 static void redisplay_internal P_ ((int));
893 static int echo_area_display P_ ((int));
894 static void redisplay_windows P_ ((Lisp_Object));
895 static void redisplay_window P_ ((Lisp_Object, int));
896 static Lisp_Object redisplay_window_error ();
897 static Lisp_Object redisplay_window_0 P_ ((Lisp_Object));
898 static Lisp_Object redisplay_window_1 P_ ((Lisp_Object));
899 static void update_menu_bar P_ ((struct frame *, int));
900 static int try_window_reusing_current_matrix P_ ((struct window *));
901 static int try_window_id P_ ((struct window *));
902 static int display_line P_ ((struct it *));
903 static int display_mode_lines P_ ((struct window *));
904 static int display_mode_line P_ ((struct window *, enum face_id, Lisp_Object));
905 static int display_mode_element P_ ((struct it *, int, int, int, Lisp_Object, Lisp_Object, int));
906 static int store_mode_line_string P_ ((char *, Lisp_Object, int, int, int, Lisp_Object));
907 static char *decode_mode_spec P_ ((struct window *, int, int, int, int *));
908 static void display_menu_bar P_ ((struct window *));
909 static int display_count_lines P_ ((int, int, int, int, int *));
910 static int display_string P_ ((unsigned char *, Lisp_Object, Lisp_Object,
911 int, int, struct it *, int, int, int, int));
912 static void compute_line_metrics P_ ((struct it *));
913 static void run_redisplay_end_trigger_hook P_ ((struct it *));
914 static int get_overlay_strings P_ ((struct it *, int));
915 static void next_overlay_string P_ ((struct it *));
916 static void reseat P_ ((struct it *, struct text_pos, int));
917 static void reseat_1 P_ ((struct it *, struct text_pos, int));
918 static void back_to_previous_visible_line_start P_ ((struct it *));
919 void reseat_at_previous_visible_line_start P_ ((struct it *));
920 static void reseat_at_next_visible_line_start P_ ((struct it *, int));
921 static int next_element_from_ellipsis P_ ((struct it *));
922 static int next_element_from_display_vector P_ ((struct it *));
923 static int next_element_from_string P_ ((struct it *));
924 static int next_element_from_c_string P_ ((struct it *));
925 static int next_element_from_buffer P_ ((struct it *));
926 static int next_element_from_composition P_ ((struct it *));
927 static int next_element_from_image P_ ((struct it *));
928 static int next_element_from_stretch P_ ((struct it *));
929 static void load_overlay_strings P_ ((struct it *, int));
930 static int init_from_display_pos P_ ((struct it *, struct window *,
931 struct display_pos *));
932 static void reseat_to_string P_ ((struct it *, unsigned char *,
933 Lisp_Object, int, int, int, int));
934 static enum move_it_result move_it_in_display_line_to P_ ((struct it *,
935 int, int, int));
936 void move_it_vertically_backward P_ ((struct it *, int));
937 static void init_to_row_start P_ ((struct it *, struct window *,
938 struct glyph_row *));
939 static int init_to_row_end P_ ((struct it *, struct window *,
940 struct glyph_row *));
941 static void back_to_previous_line_start P_ ((struct it *));
942 static int forward_to_next_line_start P_ ((struct it *, int *));
943 static struct text_pos string_pos_nchars_ahead P_ ((struct text_pos,
944 Lisp_Object, int));
945 static struct text_pos string_pos P_ ((int, Lisp_Object));
946 static struct text_pos c_string_pos P_ ((int, unsigned char *, int));
947 static int number_of_chars P_ ((unsigned char *, int));
948 static void compute_stop_pos P_ ((struct it *));
949 static void compute_string_pos P_ ((struct text_pos *, struct text_pos,
950 Lisp_Object));
951 static int face_before_or_after_it_pos P_ ((struct it *, int));
952 static int next_overlay_change P_ ((int));
953 static int handle_single_display_spec P_ ((struct it *, Lisp_Object,
954 Lisp_Object, struct text_pos *,
955 int));
956 static int underlying_face_id P_ ((struct it *));
957 static int in_ellipses_for_invisible_text_p P_ ((struct display_pos *,
958 struct window *));
959
960 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
961 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
962
963 #ifdef HAVE_WINDOW_SYSTEM
964
965 static void update_tool_bar P_ ((struct frame *, int));
966 static void build_desired_tool_bar_string P_ ((struct frame *f));
967 static int redisplay_tool_bar P_ ((struct frame *));
968 static void display_tool_bar_line P_ ((struct it *));
969 static void notice_overwritten_cursor P_ ((struct window *,
970 enum glyph_row_area,
971 int, int, int, int));
972
973
974
975 #endif /* HAVE_WINDOW_SYSTEM */
976
977 \f
978 /***********************************************************************
979 Window display dimensions
980 ***********************************************************************/
981
982 /* Return the bottom boundary y-position for text lines in window W.
983 This is the first y position at which a line cannot start.
984 It is relative to the top of the window.
985
986 This is the height of W minus the height of a mode line, if any. */
987
988 INLINE int
989 window_text_bottom_y (w)
990 struct window *w;
991 {
992 int height = WINDOW_TOTAL_HEIGHT (w);
993
994 if (WINDOW_WANTS_MODELINE_P (w))
995 height -= CURRENT_MODE_LINE_HEIGHT (w);
996 return height;
997 }
998
999 /* Return the pixel width of display area AREA of window W. AREA < 0
1000 means return the total width of W, not including fringes to
1001 the left and right of the window. */
1002
1003 INLINE int
1004 window_box_width (w, area)
1005 struct window *w;
1006 int area;
1007 {
1008 int cols = XFASTINT (w->total_cols);
1009 int pixels = 0;
1010
1011 if (!w->pseudo_window_p)
1012 {
1013 cols -= WINDOW_SCROLL_BAR_COLS (w);
1014
1015 if (area == TEXT_AREA)
1016 {
1017 if (INTEGERP (w->left_margin_cols))
1018 cols -= XFASTINT (w->left_margin_cols);
1019 if (INTEGERP (w->right_margin_cols))
1020 cols -= XFASTINT (w->right_margin_cols);
1021 pixels = -WINDOW_TOTAL_FRINGE_WIDTH (w);
1022 }
1023 else if (area == LEFT_MARGIN_AREA)
1024 {
1025 cols = (INTEGERP (w->left_margin_cols)
1026 ? XFASTINT (w->left_margin_cols) : 0);
1027 pixels = 0;
1028 }
1029 else if (area == RIGHT_MARGIN_AREA)
1030 {
1031 cols = (INTEGERP (w->right_margin_cols)
1032 ? XFASTINT (w->right_margin_cols) : 0);
1033 pixels = 0;
1034 }
1035 }
1036
1037 return cols * WINDOW_FRAME_COLUMN_WIDTH (w) + pixels;
1038 }
1039
1040
1041 /* Return the pixel height of the display area of window W, not
1042 including mode lines of W, if any. */
1043
1044 INLINE int
1045 window_box_height (w)
1046 struct window *w;
1047 {
1048 struct frame *f = XFRAME (w->frame);
1049 int height = WINDOW_TOTAL_HEIGHT (w);
1050
1051 xassert (height >= 0);
1052
1053 /* Note: the code below that determines the mode-line/header-line
1054 height is essentially the same as that contained in the macro
1055 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
1056 the appropriate glyph row has its `mode_line_p' flag set,
1057 and if it doesn't, uses estimate_mode_line_height instead. */
1058
1059 if (WINDOW_WANTS_MODELINE_P (w))
1060 {
1061 struct glyph_row *ml_row
1062 = (w->current_matrix && w->current_matrix->rows
1063 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
1064 : 0);
1065 if (ml_row && ml_row->mode_line_p)
1066 height -= ml_row->height;
1067 else
1068 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
1069 }
1070
1071 if (WINDOW_WANTS_HEADER_LINE_P (w))
1072 {
1073 struct glyph_row *hl_row
1074 = (w->current_matrix && w->current_matrix->rows
1075 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
1076 : 0);
1077 if (hl_row && hl_row->mode_line_p)
1078 height -= hl_row->height;
1079 else
1080 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
1081 }
1082
1083 /* With a very small font and a mode-line that's taller than
1084 default, we might end up with a negative height. */
1085 return max (0, height);
1086 }
1087
1088 /* Return the window-relative coordinate of the left edge of display
1089 area AREA of window W. AREA < 0 means return the left edge of the
1090 whole window, to the right of the left fringe of W. */
1091
1092 INLINE int
1093 window_box_left_offset (w, area)
1094 struct window *w;
1095 int area;
1096 {
1097 int x;
1098
1099 if (w->pseudo_window_p)
1100 return 0;
1101
1102 x = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
1103
1104 if (area == TEXT_AREA)
1105 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1106 + window_box_width (w, LEFT_MARGIN_AREA));
1107 else if (area == RIGHT_MARGIN_AREA)
1108 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1109 + window_box_width (w, LEFT_MARGIN_AREA)
1110 + window_box_width (w, TEXT_AREA)
1111 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1112 ? 0
1113 : WINDOW_RIGHT_FRINGE_WIDTH (w)));
1114 else if (area == LEFT_MARGIN_AREA
1115 && WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1116 x += WINDOW_LEFT_FRINGE_WIDTH (w);
1117
1118 return x;
1119 }
1120
1121
1122 /* Return the window-relative coordinate of the right edge of display
1123 area AREA of window W. AREA < 0 means return the left edge of the
1124 whole window, to the left of the right fringe of W. */
1125
1126 INLINE int
1127 window_box_right_offset (w, area)
1128 struct window *w;
1129 int area;
1130 {
1131 return window_box_left_offset (w, area) + window_box_width (w, area);
1132 }
1133
1134 /* Return the frame-relative coordinate of the left edge of display
1135 area AREA of window W. AREA < 0 means return the left edge of the
1136 whole window, to the right of the left fringe of W. */
1137
1138 INLINE int
1139 window_box_left (w, area)
1140 struct window *w;
1141 int area;
1142 {
1143 struct frame *f = XFRAME (w->frame);
1144 int x;
1145
1146 if (w->pseudo_window_p)
1147 return FRAME_INTERNAL_BORDER_WIDTH (f);
1148
1149 x = (WINDOW_LEFT_EDGE_X (w)
1150 + window_box_left_offset (w, area));
1151
1152 return x;
1153 }
1154
1155
1156 /* Return the frame-relative coordinate of the right edge of display
1157 area AREA of window W. AREA < 0 means return the left edge of the
1158 whole window, to the left of the right fringe of W. */
1159
1160 INLINE int
1161 window_box_right (w, area)
1162 struct window *w;
1163 int area;
1164 {
1165 return window_box_left (w, area) + window_box_width (w, area);
1166 }
1167
1168 /* Get the bounding box of the display area AREA of window W, without
1169 mode lines, in frame-relative coordinates. AREA < 0 means the
1170 whole window, not including the left and right fringes of
1171 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1172 coordinates of the upper-left corner of the box. Return in
1173 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1174
1175 INLINE void
1176 window_box (w, area, box_x, box_y, box_width, box_height)
1177 struct window *w;
1178 int area;
1179 int *box_x, *box_y, *box_width, *box_height;
1180 {
1181 if (box_width)
1182 *box_width = window_box_width (w, area);
1183 if (box_height)
1184 *box_height = window_box_height (w);
1185 if (box_x)
1186 *box_x = window_box_left (w, area);
1187 if (box_y)
1188 {
1189 *box_y = WINDOW_TOP_EDGE_Y (w);
1190 if (WINDOW_WANTS_HEADER_LINE_P (w))
1191 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1192 }
1193 }
1194
1195
1196 /* Get the bounding box of the display area AREA of window W, without
1197 mode lines. AREA < 0 means the whole window, not including the
1198 left and right fringe of the window. Return in *TOP_LEFT_X
1199 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1200 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1201 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1202 box. */
1203
1204 INLINE void
1205 window_box_edges (w, area, top_left_x, top_left_y,
1206 bottom_right_x, bottom_right_y)
1207 struct window *w;
1208 int area;
1209 int *top_left_x, *top_left_y, *bottom_right_x, *bottom_right_y;
1210 {
1211 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
1212 bottom_right_y);
1213 *bottom_right_x += *top_left_x;
1214 *bottom_right_y += *top_left_y;
1215 }
1216
1217
1218 \f
1219 /***********************************************************************
1220 Utilities
1221 ***********************************************************************/
1222
1223 /* Return the bottom y-position of the line the iterator IT is in.
1224 This can modify IT's settings. */
1225
1226 int
1227 line_bottom_y (it)
1228 struct it *it;
1229 {
1230 int line_height = it->max_ascent + it->max_descent;
1231 int line_top_y = it->current_y;
1232
1233 if (line_height == 0)
1234 {
1235 if (last_height)
1236 line_height = last_height;
1237 else if (IT_CHARPOS (*it) < ZV)
1238 {
1239 move_it_by_lines (it, 1, 1);
1240 line_height = (it->max_ascent || it->max_descent
1241 ? it->max_ascent + it->max_descent
1242 : last_height);
1243 }
1244 else
1245 {
1246 struct glyph_row *row = it->glyph_row;
1247
1248 /* Use the default character height. */
1249 it->glyph_row = NULL;
1250 it->what = IT_CHARACTER;
1251 it->c = ' ';
1252 it->len = 1;
1253 PRODUCE_GLYPHS (it);
1254 line_height = it->ascent + it->descent;
1255 it->glyph_row = row;
1256 }
1257 }
1258
1259 return line_top_y + line_height;
1260 }
1261
1262
1263 /* Return 1 if position CHARPOS is visible in window W.
1264 If visible, set *X and *Y to pixel coordinates of top left corner.
1265 Set *RTOP and *RBOT to pixel height of an invisible area of glyph at POS.
1266 EXACT_MODE_LINE_HEIGHTS_P non-zero means compute exact mode-line
1267 and header-lines heights. */
1268
1269 int
1270 pos_visible_p (w, charpos, x, y, rtop, rbot, exact_mode_line_heights_p)
1271 struct window *w;
1272 int charpos, *x, *y, *rtop, *rbot, exact_mode_line_heights_p;
1273 {
1274 struct it it;
1275 struct text_pos top;
1276 int visible_p = 0;
1277 struct buffer *old_buffer = NULL;
1278
1279 if (noninteractive)
1280 return visible_p;
1281
1282 if (XBUFFER (w->buffer) != current_buffer)
1283 {
1284 old_buffer = current_buffer;
1285 set_buffer_internal_1 (XBUFFER (w->buffer));
1286 }
1287
1288 SET_TEXT_POS_FROM_MARKER (top, w->start);
1289
1290 /* Compute exact mode line heights, if requested. */
1291 if (exact_mode_line_heights_p)
1292 {
1293 if (WINDOW_WANTS_MODELINE_P (w))
1294 current_mode_line_height
1295 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1296 current_buffer->mode_line_format);
1297
1298 if (WINDOW_WANTS_HEADER_LINE_P (w))
1299 current_header_line_height
1300 = display_mode_line (w, HEADER_LINE_FACE_ID,
1301 current_buffer->header_line_format);
1302 }
1303
1304 start_display (&it, w, top);
1305 move_it_to (&it, charpos, -1, it.last_visible_y, -1,
1306 MOVE_TO_POS | MOVE_TO_Y);
1307
1308 /* Note that we may overshoot because of invisible text. */
1309 if (IT_CHARPOS (it) >= charpos)
1310 {
1311 int top_x = it.current_x;
1312 int top_y = it.current_y;
1313 int bottom_y = (last_height = 0, line_bottom_y (&it));
1314 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
1315
1316 if (top_y < window_top_y)
1317 visible_p = bottom_y > window_top_y;
1318 else if (top_y < it.last_visible_y)
1319 visible_p = 1;
1320 if (visible_p)
1321 {
1322 *x = top_x;
1323 *y = max (top_y + max (0, it.max_ascent - it.ascent), window_top_y);
1324 *rtop = max (0, window_top_y - top_y);
1325 *rbot = max (0, bottom_y - it.last_visible_y);
1326 }
1327 }
1328 else
1329 {
1330 struct it it2;
1331
1332 it2 = it;
1333 if (IT_CHARPOS (it) < ZV && FETCH_BYTE (IT_BYTEPOS (it)) != '\n')
1334 move_it_by_lines (&it, 1, 0);
1335 if (charpos < IT_CHARPOS (it))
1336 {
1337 visible_p = 1;
1338 move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS);
1339 *x = it2.current_x;
1340 *y = it2.current_y + it2.max_ascent - it2.ascent;
1341 *rtop = max (0, -it2.current_y);
1342 *rbot = max (0, ((it2.current_y + it2.max_ascent + it2.max_descent)
1343 - it.last_visible_y));
1344 }
1345 }
1346
1347 if (old_buffer)
1348 set_buffer_internal_1 (old_buffer);
1349
1350 current_header_line_height = current_mode_line_height = -1;
1351
1352 return visible_p;
1353 }
1354
1355
1356 /* Return the next character from STR which is MAXLEN bytes long.
1357 Return in *LEN the length of the character. This is like
1358 STRING_CHAR_AND_LENGTH but never returns an invalid character. If
1359 we find one, we return a `?', but with the length of the invalid
1360 character. */
1361
1362 static INLINE int
1363 string_char_and_length (str, maxlen, len)
1364 const unsigned char *str;
1365 int maxlen, *len;
1366 {
1367 int c;
1368
1369 c = STRING_CHAR_AND_LENGTH (str, maxlen, *len);
1370 if (!CHAR_VALID_P (c, 1))
1371 /* We may not change the length here because other places in Emacs
1372 don't use this function, i.e. they silently accept invalid
1373 characters. */
1374 c = '?';
1375
1376 return c;
1377 }
1378
1379
1380
1381 /* Given a position POS containing a valid character and byte position
1382 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1383
1384 static struct text_pos
1385 string_pos_nchars_ahead (pos, string, nchars)
1386 struct text_pos pos;
1387 Lisp_Object string;
1388 int nchars;
1389 {
1390 xassert (STRINGP (string) && nchars >= 0);
1391
1392 if (STRING_MULTIBYTE (string))
1393 {
1394 int rest = SBYTES (string) - BYTEPOS (pos);
1395 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1396 int len;
1397
1398 while (nchars--)
1399 {
1400 string_char_and_length (p, rest, &len);
1401 p += len, rest -= len;
1402 xassert (rest >= 0);
1403 CHARPOS (pos) += 1;
1404 BYTEPOS (pos) += len;
1405 }
1406 }
1407 else
1408 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1409
1410 return pos;
1411 }
1412
1413
1414 /* Value is the text position, i.e. character and byte position,
1415 for character position CHARPOS in STRING. */
1416
1417 static INLINE struct text_pos
1418 string_pos (charpos, string)
1419 int charpos;
1420 Lisp_Object string;
1421 {
1422 struct text_pos pos;
1423 xassert (STRINGP (string));
1424 xassert (charpos >= 0);
1425 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1426 return pos;
1427 }
1428
1429
1430 /* Value is a text position, i.e. character and byte position, for
1431 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1432 means recognize multibyte characters. */
1433
1434 static struct text_pos
1435 c_string_pos (charpos, s, multibyte_p)
1436 int charpos;
1437 unsigned char *s;
1438 int multibyte_p;
1439 {
1440 struct text_pos pos;
1441
1442 xassert (s != NULL);
1443 xassert (charpos >= 0);
1444
1445 if (multibyte_p)
1446 {
1447 int rest = strlen (s), len;
1448
1449 SET_TEXT_POS (pos, 0, 0);
1450 while (charpos--)
1451 {
1452 string_char_and_length (s, rest, &len);
1453 s += len, rest -= len;
1454 xassert (rest >= 0);
1455 CHARPOS (pos) += 1;
1456 BYTEPOS (pos) += len;
1457 }
1458 }
1459 else
1460 SET_TEXT_POS (pos, charpos, charpos);
1461
1462 return pos;
1463 }
1464
1465
1466 /* Value is the number of characters in C string S. MULTIBYTE_P
1467 non-zero means recognize multibyte characters. */
1468
1469 static int
1470 number_of_chars (s, multibyte_p)
1471 unsigned char *s;
1472 int multibyte_p;
1473 {
1474 int nchars;
1475
1476 if (multibyte_p)
1477 {
1478 int rest = strlen (s), len;
1479 unsigned char *p = (unsigned char *) s;
1480
1481 for (nchars = 0; rest > 0; ++nchars)
1482 {
1483 string_char_and_length (p, rest, &len);
1484 rest -= len, p += len;
1485 }
1486 }
1487 else
1488 nchars = strlen (s);
1489
1490 return nchars;
1491 }
1492
1493
1494 /* Compute byte position NEWPOS->bytepos corresponding to
1495 NEWPOS->charpos. POS is a known position in string STRING.
1496 NEWPOS->charpos must be >= POS.charpos. */
1497
1498 static void
1499 compute_string_pos (newpos, pos, string)
1500 struct text_pos *newpos, pos;
1501 Lisp_Object string;
1502 {
1503 xassert (STRINGP (string));
1504 xassert (CHARPOS (*newpos) >= CHARPOS (pos));
1505
1506 if (STRING_MULTIBYTE (string))
1507 *newpos = string_pos_nchars_ahead (pos, string,
1508 CHARPOS (*newpos) - CHARPOS (pos));
1509 else
1510 BYTEPOS (*newpos) = CHARPOS (*newpos);
1511 }
1512
1513 /* EXPORT:
1514 Return an estimation of the pixel height of mode or top lines on
1515 frame F. FACE_ID specifies what line's height to estimate. */
1516
1517 int
1518 estimate_mode_line_height (f, face_id)
1519 struct frame *f;
1520 enum face_id face_id;
1521 {
1522 #ifdef HAVE_WINDOW_SYSTEM
1523 if (FRAME_WINDOW_P (f))
1524 {
1525 int height = FONT_HEIGHT (FRAME_FONT (f));
1526
1527 /* This function is called so early when Emacs starts that the face
1528 cache and mode line face are not yet initialized. */
1529 if (FRAME_FACE_CACHE (f))
1530 {
1531 struct face *face = FACE_FROM_ID (f, face_id);
1532 if (face)
1533 {
1534 if (face->font)
1535 height = FONT_HEIGHT (face->font);
1536 if (face->box_line_width > 0)
1537 height += 2 * face->box_line_width;
1538 }
1539 }
1540
1541 return height;
1542 }
1543 #endif
1544
1545 return 1;
1546 }
1547
1548 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1549 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1550 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
1551 not force the value into range. */
1552
1553 void
1554 pixel_to_glyph_coords (f, pix_x, pix_y, x, y, bounds, noclip)
1555 FRAME_PTR f;
1556 register int pix_x, pix_y;
1557 int *x, *y;
1558 NativeRectangle *bounds;
1559 int noclip;
1560 {
1561
1562 #ifdef HAVE_WINDOW_SYSTEM
1563 if (FRAME_WINDOW_P (f))
1564 {
1565 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1566 even for negative values. */
1567 if (pix_x < 0)
1568 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1569 if (pix_y < 0)
1570 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1571
1572 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1573 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1574
1575 if (bounds)
1576 STORE_NATIVE_RECT (*bounds,
1577 FRAME_COL_TO_PIXEL_X (f, pix_x),
1578 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1579 FRAME_COLUMN_WIDTH (f) - 1,
1580 FRAME_LINE_HEIGHT (f) - 1);
1581
1582 if (!noclip)
1583 {
1584 if (pix_x < 0)
1585 pix_x = 0;
1586 else if (pix_x > FRAME_TOTAL_COLS (f))
1587 pix_x = FRAME_TOTAL_COLS (f);
1588
1589 if (pix_y < 0)
1590 pix_y = 0;
1591 else if (pix_y > FRAME_LINES (f))
1592 pix_y = FRAME_LINES (f);
1593 }
1594 }
1595 #endif
1596
1597 *x = pix_x;
1598 *y = pix_y;
1599 }
1600
1601
1602 /* Given HPOS/VPOS in the current matrix of W, return corresponding
1603 frame-relative pixel positions in *FRAME_X and *FRAME_Y. If we
1604 can't tell the positions because W's display is not up to date,
1605 return 0. */
1606
1607 int
1608 glyph_to_pixel_coords (w, hpos, vpos, frame_x, frame_y)
1609 struct window *w;
1610 int hpos, vpos;
1611 int *frame_x, *frame_y;
1612 {
1613 #ifdef HAVE_WINDOW_SYSTEM
1614 if (FRAME_WINDOW_P (XFRAME (WINDOW_FRAME (w))))
1615 {
1616 int success_p;
1617
1618 xassert (hpos >= 0 && hpos < w->current_matrix->matrix_w);
1619 xassert (vpos >= 0 && vpos < w->current_matrix->matrix_h);
1620
1621 if (display_completed)
1622 {
1623 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
1624 struct glyph *glyph = row->glyphs[TEXT_AREA];
1625 struct glyph *end = glyph + min (hpos, row->used[TEXT_AREA]);
1626
1627 hpos = row->x;
1628 vpos = row->y;
1629 while (glyph < end)
1630 {
1631 hpos += glyph->pixel_width;
1632 ++glyph;
1633 }
1634
1635 /* If first glyph is partially visible, its first visible position is still 0. */
1636 if (hpos < 0)
1637 hpos = 0;
1638
1639 success_p = 1;
1640 }
1641 else
1642 {
1643 hpos = vpos = 0;
1644 success_p = 0;
1645 }
1646
1647 *frame_x = WINDOW_TO_FRAME_PIXEL_X (w, hpos);
1648 *frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, vpos);
1649 return success_p;
1650 }
1651 #endif
1652
1653 *frame_x = hpos;
1654 *frame_y = vpos;
1655 return 1;
1656 }
1657
1658
1659 #ifdef HAVE_WINDOW_SYSTEM
1660
1661 /* Find the glyph under window-relative coordinates X/Y in window W.
1662 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1663 strings. Return in *HPOS and *VPOS the row and column number of
1664 the glyph found. Return in *AREA the glyph area containing X.
1665 Value is a pointer to the glyph found or null if X/Y is not on
1666 text, or we can't tell because W's current matrix is not up to
1667 date. */
1668
1669 static struct glyph *
1670 x_y_to_hpos_vpos (w, x, y, hpos, vpos, dx, dy, area)
1671 struct window *w;
1672 int x, y;
1673 int *hpos, *vpos, *dx, *dy, *area;
1674 {
1675 struct glyph *glyph, *end;
1676 struct glyph_row *row = NULL;
1677 int x0, i;
1678
1679 /* Find row containing Y. Give up if some row is not enabled. */
1680 for (i = 0; i < w->current_matrix->nrows; ++i)
1681 {
1682 row = MATRIX_ROW (w->current_matrix, i);
1683 if (!row->enabled_p)
1684 return NULL;
1685 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1686 break;
1687 }
1688
1689 *vpos = i;
1690 *hpos = 0;
1691
1692 /* Give up if Y is not in the window. */
1693 if (i == w->current_matrix->nrows)
1694 return NULL;
1695
1696 /* Get the glyph area containing X. */
1697 if (w->pseudo_window_p)
1698 {
1699 *area = TEXT_AREA;
1700 x0 = 0;
1701 }
1702 else
1703 {
1704 if (x < window_box_left_offset (w, TEXT_AREA))
1705 {
1706 *area = LEFT_MARGIN_AREA;
1707 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1708 }
1709 else if (x < window_box_right_offset (w, TEXT_AREA))
1710 {
1711 *area = TEXT_AREA;
1712 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
1713 }
1714 else
1715 {
1716 *area = RIGHT_MARGIN_AREA;
1717 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1718 }
1719 }
1720
1721 /* Find glyph containing X. */
1722 glyph = row->glyphs[*area];
1723 end = glyph + row->used[*area];
1724 x -= x0;
1725 while (glyph < end && x >= glyph->pixel_width)
1726 {
1727 x -= glyph->pixel_width;
1728 ++glyph;
1729 }
1730
1731 if (glyph == end)
1732 return NULL;
1733
1734 if (dx)
1735 {
1736 *dx = x;
1737 *dy = y - (row->y + row->ascent - glyph->ascent);
1738 }
1739
1740 *hpos = glyph - row->glyphs[*area];
1741 return glyph;
1742 }
1743
1744
1745 /* EXPORT:
1746 Convert frame-relative x/y to coordinates relative to window W.
1747 Takes pseudo-windows into account. */
1748
1749 void
1750 frame_to_window_pixel_xy (w, x, y)
1751 struct window *w;
1752 int *x, *y;
1753 {
1754 if (w->pseudo_window_p)
1755 {
1756 /* A pseudo-window is always full-width, and starts at the
1757 left edge of the frame, plus a frame border. */
1758 struct frame *f = XFRAME (w->frame);
1759 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
1760 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1761 }
1762 else
1763 {
1764 *x -= WINDOW_LEFT_EDGE_X (w);
1765 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1766 }
1767 }
1768
1769 /* EXPORT:
1770 Return in *R the clipping rectangle for glyph string S. */
1771
1772 void
1773 get_glyph_string_clip_rect (s, nr)
1774 struct glyph_string *s;
1775 NativeRectangle *nr;
1776 {
1777 XRectangle r;
1778
1779 if (s->row->full_width_p)
1780 {
1781 /* Draw full-width. X coordinates are relative to S->w->left_col. */
1782 r.x = WINDOW_LEFT_EDGE_X (s->w);
1783 r.width = WINDOW_TOTAL_WIDTH (s->w);
1784
1785 /* Unless displaying a mode or menu bar line, which are always
1786 fully visible, clip to the visible part of the row. */
1787 if (s->w->pseudo_window_p)
1788 r.height = s->row->visible_height;
1789 else
1790 r.height = s->height;
1791 }
1792 else
1793 {
1794 /* This is a text line that may be partially visible. */
1795 r.x = window_box_left (s->w, s->area);
1796 r.width = window_box_width (s->w, s->area);
1797 r.height = s->row->visible_height;
1798 }
1799
1800 if (s->clip_head)
1801 if (r.x < s->clip_head->x)
1802 {
1803 if (r.width >= s->clip_head->x - r.x)
1804 r.width -= s->clip_head->x - r.x;
1805 else
1806 r.width = 0;
1807 r.x = s->clip_head->x;
1808 }
1809 if (s->clip_tail)
1810 if (r.x + r.width > s->clip_tail->x + s->clip_tail->background_width)
1811 {
1812 if (s->clip_tail->x + s->clip_tail->background_width >= r.x)
1813 r.width = s->clip_tail->x + s->clip_tail->background_width - r.x;
1814 else
1815 r.width = 0;
1816 }
1817
1818 /* If S draws overlapping rows, it's sufficient to use the top and
1819 bottom of the window for clipping because this glyph string
1820 intentionally draws over other lines. */
1821 if (s->for_overlaps_p)
1822 {
1823 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1824 r.height = window_text_bottom_y (s->w) - r.y;
1825 }
1826 else
1827 {
1828 /* Don't use S->y for clipping because it doesn't take partially
1829 visible lines into account. For example, it can be negative for
1830 partially visible lines at the top of a window. */
1831 if (!s->row->full_width_p
1832 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
1833 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1834 else
1835 r.y = max (0, s->row->y);
1836
1837 /* If drawing a tool-bar window, draw it over the internal border
1838 at the top of the window. */
1839 if (WINDOWP (s->f->tool_bar_window)
1840 && s->w == XWINDOW (s->f->tool_bar_window))
1841 r.y -= FRAME_INTERNAL_BORDER_WIDTH (s->f);
1842 }
1843
1844 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
1845
1846 /* If drawing the cursor, don't let glyph draw outside its
1847 advertised boundaries. Cleartype does this under some circumstances. */
1848 if (s->hl == DRAW_CURSOR)
1849 {
1850 struct glyph *glyph = s->first_glyph;
1851 int height, max_y;
1852
1853 if (s->x > r.x)
1854 {
1855 r.width -= s->x - r.x;
1856 r.x = s->x;
1857 }
1858 r.width = min (r.width, glyph->pixel_width);
1859
1860 /* If r.y is below window bottom, ensure that we still see a cursor. */
1861 height = min (glyph->ascent + glyph->descent,
1862 min (FRAME_LINE_HEIGHT (s->f), s->row->visible_height));
1863 max_y = window_text_bottom_y (s->w) - height;
1864 max_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, max_y);
1865 if (s->ybase - glyph->ascent > max_y)
1866 {
1867 r.y = max_y;
1868 r.height = height;
1869 }
1870 else
1871 {
1872 /* Don't draw cursor glyph taller than our actual glyph. */
1873 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
1874 if (height < r.height)
1875 {
1876 max_y = r.y + r.height;
1877 r.y = min (max_y, max (r.y, s->ybase + glyph->descent - height));
1878 r.height = min (max_y - r.y, height);
1879 }
1880 }
1881 }
1882
1883 #ifdef CONVERT_FROM_XRECT
1884 CONVERT_FROM_XRECT (r, *nr);
1885 #else
1886 *nr = r;
1887 #endif
1888 }
1889
1890
1891 /* EXPORT:
1892 Return the position and height of the phys cursor in window W.
1893 Set w->phys_cursor_width to width of phys cursor.
1894 */
1895
1896 int
1897 get_phys_cursor_geometry (w, row, glyph, heightp)
1898 struct window *w;
1899 struct glyph_row *row;
1900 struct glyph *glyph;
1901 int *heightp;
1902 {
1903 struct frame *f = XFRAME (WINDOW_FRAME (w));
1904 int y, wd, h, h0, y0;
1905
1906 /* Compute the width of the rectangle to draw. If on a stretch
1907 glyph, and `x-stretch-block-cursor' is nil, don't draw a
1908 rectangle as wide as the glyph, but use a canonical character
1909 width instead. */
1910 wd = glyph->pixel_width - 1;
1911 #ifdef HAVE_NTGUI
1912 wd++; /* Why? */
1913 #endif
1914 if (glyph->type == STRETCH_GLYPH
1915 && !x_stretch_cursor_p)
1916 wd = min (FRAME_COLUMN_WIDTH (f), wd);
1917 w->phys_cursor_width = wd;
1918
1919 y = w->phys_cursor.y + row->ascent - glyph->ascent;
1920
1921 /* If y is below window bottom, ensure that we still see a cursor. */
1922 h0 = min (FRAME_LINE_HEIGHT (f), row->visible_height);
1923
1924 h = max (h0, glyph->ascent + glyph->descent);
1925 h0 = min (h0, glyph->ascent + glyph->descent);
1926
1927 y0 = WINDOW_HEADER_LINE_HEIGHT (w);
1928 if (y < y0)
1929 {
1930 h = max (h - (y0 - y) + 1, h0);
1931 y = y0 - 1;
1932 }
1933 else
1934 {
1935 y0 = window_text_bottom_y (w) - h0;
1936 if (y > y0)
1937 {
1938 h += y - y0;
1939 y = y0;
1940 }
1941 }
1942
1943 *heightp = h - 1;
1944 return WINDOW_TO_FRAME_PIXEL_Y (w, y);
1945 }
1946
1947
1948 #endif /* HAVE_WINDOW_SYSTEM */
1949
1950 \f
1951 /***********************************************************************
1952 Lisp form evaluation
1953 ***********************************************************************/
1954
1955 /* Error handler for safe_eval and safe_call. */
1956
1957 static Lisp_Object
1958 safe_eval_handler (arg)
1959 Lisp_Object arg;
1960 {
1961 add_to_log ("Error during redisplay: %s", arg, Qnil);
1962 return Qnil;
1963 }
1964
1965
1966 /* Evaluate SEXPR and return the result, or nil if something went
1967 wrong. Prevent redisplay during the evaluation. */
1968
1969 Lisp_Object
1970 safe_eval (sexpr)
1971 Lisp_Object sexpr;
1972 {
1973 Lisp_Object val;
1974
1975 if (inhibit_eval_during_redisplay)
1976 val = Qnil;
1977 else
1978 {
1979 int count = SPECPDL_INDEX ();
1980 struct gcpro gcpro1;
1981
1982 GCPRO1 (sexpr);
1983 specbind (Qinhibit_redisplay, Qt);
1984 /* Use Qt to ensure debugger does not run,
1985 so there is no possibility of wanting to redisplay. */
1986 val = internal_condition_case_1 (Feval, sexpr, Qt,
1987 safe_eval_handler);
1988 UNGCPRO;
1989 val = unbind_to (count, val);
1990 }
1991
1992 return val;
1993 }
1994
1995
1996 /* Call function ARGS[0] with arguments ARGS[1] to ARGS[NARGS - 1].
1997 Return the result, or nil if something went wrong. Prevent
1998 redisplay during the evaluation. */
1999
2000 Lisp_Object
2001 safe_call (nargs, args)
2002 int nargs;
2003 Lisp_Object *args;
2004 {
2005 Lisp_Object val;
2006
2007 if (inhibit_eval_during_redisplay)
2008 val = Qnil;
2009 else
2010 {
2011 int count = SPECPDL_INDEX ();
2012 struct gcpro gcpro1;
2013
2014 GCPRO1 (args[0]);
2015 gcpro1.nvars = nargs;
2016 specbind (Qinhibit_redisplay, Qt);
2017 /* Use Qt to ensure debugger does not run,
2018 so there is no possibility of wanting to redisplay. */
2019 val = internal_condition_case_2 (Ffuncall, nargs, args, Qt,
2020 safe_eval_handler);
2021 UNGCPRO;
2022 val = unbind_to (count, val);
2023 }
2024
2025 return val;
2026 }
2027
2028
2029 /* Call function FN with one argument ARG.
2030 Return the result, or nil if something went wrong. */
2031
2032 Lisp_Object
2033 safe_call1 (fn, arg)
2034 Lisp_Object fn, arg;
2035 {
2036 Lisp_Object args[2];
2037 args[0] = fn;
2038 args[1] = arg;
2039 return safe_call (2, args);
2040 }
2041
2042
2043 \f
2044 /***********************************************************************
2045 Debugging
2046 ***********************************************************************/
2047
2048 #if 0
2049
2050 /* Define CHECK_IT to perform sanity checks on iterators.
2051 This is for debugging. It is too slow to do unconditionally. */
2052
2053 static void
2054 check_it (it)
2055 struct it *it;
2056 {
2057 if (it->method == GET_FROM_STRING)
2058 {
2059 xassert (STRINGP (it->string));
2060 xassert (IT_STRING_CHARPOS (*it) >= 0);
2061 }
2062 else
2063 {
2064 xassert (IT_STRING_CHARPOS (*it) < 0);
2065 if (it->method == GET_FROM_BUFFER)
2066 {
2067 /* Check that character and byte positions agree. */
2068 xassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
2069 }
2070 }
2071
2072 if (it->dpvec)
2073 xassert (it->current.dpvec_index >= 0);
2074 else
2075 xassert (it->current.dpvec_index < 0);
2076 }
2077
2078 #define CHECK_IT(IT) check_it ((IT))
2079
2080 #else /* not 0 */
2081
2082 #define CHECK_IT(IT) (void) 0
2083
2084 #endif /* not 0 */
2085
2086
2087 #if GLYPH_DEBUG
2088
2089 /* Check that the window end of window W is what we expect it
2090 to be---the last row in the current matrix displaying text. */
2091
2092 static void
2093 check_window_end (w)
2094 struct window *w;
2095 {
2096 if (!MINI_WINDOW_P (w)
2097 && !NILP (w->window_end_valid))
2098 {
2099 struct glyph_row *row;
2100 xassert ((row = MATRIX_ROW (w->current_matrix,
2101 XFASTINT (w->window_end_vpos)),
2102 !row->enabled_p
2103 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
2104 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
2105 }
2106 }
2107
2108 #define CHECK_WINDOW_END(W) check_window_end ((W))
2109
2110 #else /* not GLYPH_DEBUG */
2111
2112 #define CHECK_WINDOW_END(W) (void) 0
2113
2114 #endif /* not GLYPH_DEBUG */
2115
2116
2117 \f
2118 /***********************************************************************
2119 Iterator initialization
2120 ***********************************************************************/
2121
2122 /* Initialize IT for displaying current_buffer in window W, starting
2123 at character position CHARPOS. CHARPOS < 0 means that no buffer
2124 position is specified which is useful when the iterator is assigned
2125 a position later. BYTEPOS is the byte position corresponding to
2126 CHARPOS. BYTEPOS < 0 means compute it from CHARPOS.
2127
2128 If ROW is not null, calls to produce_glyphs with IT as parameter
2129 will produce glyphs in that row.
2130
2131 BASE_FACE_ID is the id of a base face to use. It must be one of
2132 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2133 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2134 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2135
2136 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2137 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2138 will be initialized to use the corresponding mode line glyph row of
2139 the desired matrix of W. */
2140
2141 void
2142 init_iterator (it, w, charpos, bytepos, row, base_face_id)
2143 struct it *it;
2144 struct window *w;
2145 int charpos, bytepos;
2146 struct glyph_row *row;
2147 enum face_id base_face_id;
2148 {
2149 int highlight_region_p;
2150
2151 /* Some precondition checks. */
2152 xassert (w != NULL && it != NULL);
2153 xassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2154 && charpos <= ZV));
2155
2156 /* If face attributes have been changed since the last redisplay,
2157 free realized faces now because they depend on face definitions
2158 that might have changed. Don't free faces while there might be
2159 desired matrices pending which reference these faces. */
2160 if (face_change_count && !inhibit_free_realized_faces)
2161 {
2162 face_change_count = 0;
2163 free_all_realized_faces (Qnil);
2164 }
2165
2166 /* Use one of the mode line rows of W's desired matrix if
2167 appropriate. */
2168 if (row == NULL)
2169 {
2170 if (base_face_id == MODE_LINE_FACE_ID
2171 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2172 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2173 else if (base_face_id == HEADER_LINE_FACE_ID)
2174 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2175 }
2176
2177 /* Clear IT. */
2178 bzero (it, sizeof *it);
2179 it->current.overlay_string_index = -1;
2180 it->current.dpvec_index = -1;
2181 it->base_face_id = base_face_id;
2182 it->string = Qnil;
2183 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2184
2185 /* The window in which we iterate over current_buffer: */
2186 XSETWINDOW (it->window, w);
2187 it->w = w;
2188 it->f = XFRAME (w->frame);
2189
2190 /* Extra space between lines (on window systems only). */
2191 if (base_face_id == DEFAULT_FACE_ID
2192 && FRAME_WINDOW_P (it->f))
2193 {
2194 if (NATNUMP (current_buffer->extra_line_spacing))
2195 it->extra_line_spacing = XFASTINT (current_buffer->extra_line_spacing);
2196 else if (FLOATP (current_buffer->extra_line_spacing))
2197 it->extra_line_spacing = (XFLOAT_DATA (current_buffer->extra_line_spacing)
2198 * FRAME_LINE_HEIGHT (it->f));
2199 else if (it->f->extra_line_spacing > 0)
2200 it->extra_line_spacing = it->f->extra_line_spacing;
2201 it->max_extra_line_spacing = 0;
2202 }
2203
2204 /* If realized faces have been removed, e.g. because of face
2205 attribute changes of named faces, recompute them. When running
2206 in batch mode, the face cache of Vterminal_frame is null. If
2207 we happen to get called, make a dummy face cache. */
2208 if (noninteractive && FRAME_FACE_CACHE (it->f) == NULL)
2209 init_frame_faces (it->f);
2210 if (FRAME_FACE_CACHE (it->f)->used == 0)
2211 recompute_basic_faces (it->f);
2212
2213 /* Current value of the `slice', `space-width', and 'height' properties. */
2214 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
2215 it->space_width = Qnil;
2216 it->font_height = Qnil;
2217 it->override_ascent = -1;
2218
2219 /* Are control characters displayed as `^C'? */
2220 it->ctl_arrow_p = !NILP (current_buffer->ctl_arrow);
2221
2222 /* -1 means everything between a CR and the following line end
2223 is invisible. >0 means lines indented more than this value are
2224 invisible. */
2225 it->selective = (INTEGERP (current_buffer->selective_display)
2226 ? XFASTINT (current_buffer->selective_display)
2227 : (!NILP (current_buffer->selective_display)
2228 ? -1 : 0));
2229 it->selective_display_ellipsis_p
2230 = !NILP (current_buffer->selective_display_ellipses);
2231
2232 /* Display table to use. */
2233 it->dp = window_display_table (w);
2234
2235 /* Are multibyte characters enabled in current_buffer? */
2236 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
2237
2238 /* Non-zero if we should highlight the region. */
2239 highlight_region_p
2240 = (!NILP (Vtransient_mark_mode)
2241 && !NILP (current_buffer->mark_active)
2242 && XMARKER (current_buffer->mark)->buffer != 0);
2243
2244 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
2245 start and end of a visible region in window IT->w. Set both to
2246 -1 to indicate no region. */
2247 if (highlight_region_p
2248 /* Maybe highlight only in selected window. */
2249 && (/* Either show region everywhere. */
2250 highlight_nonselected_windows
2251 /* Or show region in the selected window. */
2252 || w == XWINDOW (selected_window)
2253 /* Or show the region if we are in the mini-buffer and W is
2254 the window the mini-buffer refers to. */
2255 || (MINI_WINDOW_P (XWINDOW (selected_window))
2256 && WINDOWP (minibuf_selected_window)
2257 && w == XWINDOW (minibuf_selected_window))))
2258 {
2259 int charpos = marker_position (current_buffer->mark);
2260 it->region_beg_charpos = min (PT, charpos);
2261 it->region_end_charpos = max (PT, charpos);
2262 }
2263 else
2264 it->region_beg_charpos = it->region_end_charpos = -1;
2265
2266 /* Get the position at which the redisplay_end_trigger hook should
2267 be run, if it is to be run at all. */
2268 if (MARKERP (w->redisplay_end_trigger)
2269 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2270 it->redisplay_end_trigger_charpos
2271 = marker_position (w->redisplay_end_trigger);
2272 else if (INTEGERP (w->redisplay_end_trigger))
2273 it->redisplay_end_trigger_charpos = XINT (w->redisplay_end_trigger);
2274
2275 /* Correct bogus values of tab_width. */
2276 it->tab_width = XINT (current_buffer->tab_width);
2277 if (it->tab_width <= 0 || it->tab_width > 1000)
2278 it->tab_width = 8;
2279
2280 /* Are lines in the display truncated? */
2281 it->truncate_lines_p
2282 = (base_face_id != DEFAULT_FACE_ID
2283 || XINT (it->w->hscroll)
2284 || (truncate_partial_width_windows
2285 && !WINDOW_FULL_WIDTH_P (it->w))
2286 || !NILP (current_buffer->truncate_lines));
2287
2288 /* Get dimensions of truncation and continuation glyphs. These are
2289 displayed as fringe bitmaps under X, so we don't need them for such
2290 frames. */
2291 if (!FRAME_WINDOW_P (it->f))
2292 {
2293 if (it->truncate_lines_p)
2294 {
2295 /* We will need the truncation glyph. */
2296 xassert (it->glyph_row == NULL);
2297 produce_special_glyphs (it, IT_TRUNCATION);
2298 it->truncation_pixel_width = it->pixel_width;
2299 }
2300 else
2301 {
2302 /* We will need the continuation glyph. */
2303 xassert (it->glyph_row == NULL);
2304 produce_special_glyphs (it, IT_CONTINUATION);
2305 it->continuation_pixel_width = it->pixel_width;
2306 }
2307
2308 /* Reset these values to zero because the produce_special_glyphs
2309 above has changed them. */
2310 it->pixel_width = it->ascent = it->descent = 0;
2311 it->phys_ascent = it->phys_descent = 0;
2312 }
2313
2314 /* Set this after getting the dimensions of truncation and
2315 continuation glyphs, so that we don't produce glyphs when calling
2316 produce_special_glyphs, above. */
2317 it->glyph_row = row;
2318 it->area = TEXT_AREA;
2319
2320 /* Get the dimensions of the display area. The display area
2321 consists of the visible window area plus a horizontally scrolled
2322 part to the left of the window. All x-values are relative to the
2323 start of this total display area. */
2324 if (base_face_id != DEFAULT_FACE_ID)
2325 {
2326 /* Mode lines, menu bar in terminal frames. */
2327 it->first_visible_x = 0;
2328 it->last_visible_x = WINDOW_TOTAL_WIDTH (w);
2329 }
2330 else
2331 {
2332 it->first_visible_x
2333 = XFASTINT (it->w->hscroll) * FRAME_COLUMN_WIDTH (it->f);
2334 it->last_visible_x = (it->first_visible_x
2335 + window_box_width (w, TEXT_AREA));
2336
2337 /* If we truncate lines, leave room for the truncator glyph(s) at
2338 the right margin. Otherwise, leave room for the continuation
2339 glyph(s). Truncation and continuation glyphs are not inserted
2340 for window-based redisplay. */
2341 if (!FRAME_WINDOW_P (it->f))
2342 {
2343 if (it->truncate_lines_p)
2344 it->last_visible_x -= it->truncation_pixel_width;
2345 else
2346 it->last_visible_x -= it->continuation_pixel_width;
2347 }
2348
2349 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2350 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2351 }
2352
2353 /* Leave room for a border glyph. */
2354 if (!FRAME_WINDOW_P (it->f)
2355 && !WINDOW_RIGHTMOST_P (it->w))
2356 it->last_visible_x -= 1;
2357
2358 it->last_visible_y = window_text_bottom_y (w);
2359
2360 /* For mode lines and alike, arrange for the first glyph having a
2361 left box line if the face specifies a box. */
2362 if (base_face_id != DEFAULT_FACE_ID)
2363 {
2364 struct face *face;
2365
2366 it->face_id = base_face_id;
2367
2368 /* If we have a boxed mode line, make the first character appear
2369 with a left box line. */
2370 face = FACE_FROM_ID (it->f, base_face_id);
2371 if (face->box != FACE_NO_BOX)
2372 it->start_of_box_run_p = 1;
2373 }
2374
2375 /* If a buffer position was specified, set the iterator there,
2376 getting overlays and face properties from that position. */
2377 if (charpos >= BUF_BEG (current_buffer))
2378 {
2379 it->end_charpos = ZV;
2380 it->face_id = -1;
2381 IT_CHARPOS (*it) = charpos;
2382
2383 /* Compute byte position if not specified. */
2384 if (bytepos < charpos)
2385 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
2386 else
2387 IT_BYTEPOS (*it) = bytepos;
2388
2389 it->start = it->current;
2390
2391 /* Compute faces etc. */
2392 reseat (it, it->current.pos, 1);
2393 }
2394
2395 CHECK_IT (it);
2396 }
2397
2398
2399 /* Initialize IT for the display of window W with window start POS. */
2400
2401 void
2402 start_display (it, w, pos)
2403 struct it *it;
2404 struct window *w;
2405 struct text_pos pos;
2406 {
2407 struct glyph_row *row;
2408 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
2409
2410 row = w->desired_matrix->rows + first_vpos;
2411 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
2412 it->first_vpos = first_vpos;
2413
2414 /* Don't reseat to previous visible line start if current start
2415 position is in a string or image. */
2416 if (it->method == GET_FROM_BUFFER && !it->truncate_lines_p)
2417 {
2418 int start_at_line_beg_p;
2419 int first_y = it->current_y;
2420
2421 /* If window start is not at a line start, skip forward to POS to
2422 get the correct continuation lines width. */
2423 start_at_line_beg_p = (CHARPOS (pos) == BEGV
2424 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
2425 if (!start_at_line_beg_p)
2426 {
2427 int new_x;
2428
2429 reseat_at_previous_visible_line_start (it);
2430 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
2431
2432 new_x = it->current_x + it->pixel_width;
2433
2434 /* If lines are continued, this line may end in the middle
2435 of a multi-glyph character (e.g. a control character
2436 displayed as \003, or in the middle of an overlay
2437 string). In this case move_it_to above will not have
2438 taken us to the start of the continuation line but to the
2439 end of the continued line. */
2440 if (it->current_x > 0
2441 && !it->truncate_lines_p /* Lines are continued. */
2442 && (/* And glyph doesn't fit on the line. */
2443 new_x > it->last_visible_x
2444 /* Or it fits exactly and we're on a window
2445 system frame. */
2446 || (new_x == it->last_visible_x
2447 && FRAME_WINDOW_P (it->f))))
2448 {
2449 if (it->current.dpvec_index >= 0
2450 || it->current.overlay_string_index >= 0)
2451 {
2452 set_iterator_to_next (it, 1);
2453 move_it_in_display_line_to (it, -1, -1, 0);
2454 }
2455
2456 it->continuation_lines_width += it->current_x;
2457 }
2458
2459 /* We're starting a new display line, not affected by the
2460 height of the continued line, so clear the appropriate
2461 fields in the iterator structure. */
2462 it->max_ascent = it->max_descent = 0;
2463 it->max_phys_ascent = it->max_phys_descent = 0;
2464
2465 it->current_y = first_y;
2466 it->vpos = 0;
2467 it->current_x = it->hpos = 0;
2468 }
2469 }
2470
2471 #if 0 /* Don't assert the following because start_display is sometimes
2472 called intentionally with a window start that is not at a
2473 line start. Please leave this code in as a comment. */
2474
2475 /* Window start should be on a line start, now. */
2476 xassert (it->continuation_lines_width
2477 || IT_CHARPOS (it) == BEGV
2478 || FETCH_BYTE (IT_BYTEPOS (it) - 1) == '\n');
2479 #endif /* 0 */
2480 }
2481
2482
2483 /* Return 1 if POS is a position in ellipses displayed for invisible
2484 text. W is the window we display, for text property lookup. */
2485
2486 static int
2487 in_ellipses_for_invisible_text_p (pos, w)
2488 struct display_pos *pos;
2489 struct window *w;
2490 {
2491 Lisp_Object prop, window;
2492 int ellipses_p = 0;
2493 int charpos = CHARPOS (pos->pos);
2494
2495 /* If POS specifies a position in a display vector, this might
2496 be for an ellipsis displayed for invisible text. We won't
2497 get the iterator set up for delivering that ellipsis unless
2498 we make sure that it gets aware of the invisible text. */
2499 if (pos->dpvec_index >= 0
2500 && pos->overlay_string_index < 0
2501 && CHARPOS (pos->string_pos) < 0
2502 && charpos > BEGV
2503 && (XSETWINDOW (window, w),
2504 prop = Fget_char_property (make_number (charpos),
2505 Qinvisible, window),
2506 !TEXT_PROP_MEANS_INVISIBLE (prop)))
2507 {
2508 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
2509 window);
2510 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
2511 }
2512
2513 return ellipses_p;
2514 }
2515
2516
2517 /* Initialize IT for stepping through current_buffer in window W,
2518 starting at position POS that includes overlay string and display
2519 vector/ control character translation position information. Value
2520 is zero if there are overlay strings with newlines at POS. */
2521
2522 static int
2523 init_from_display_pos (it, w, pos)
2524 struct it *it;
2525 struct window *w;
2526 struct display_pos *pos;
2527 {
2528 int charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
2529 int i, overlay_strings_with_newlines = 0;
2530
2531 /* If POS specifies a position in a display vector, this might
2532 be for an ellipsis displayed for invisible text. We won't
2533 get the iterator set up for delivering that ellipsis unless
2534 we make sure that it gets aware of the invisible text. */
2535 if (in_ellipses_for_invisible_text_p (pos, w))
2536 {
2537 --charpos;
2538 bytepos = 0;
2539 }
2540
2541 /* Keep in mind: the call to reseat in init_iterator skips invisible
2542 text, so we might end up at a position different from POS. This
2543 is only a problem when POS is a row start after a newline and an
2544 overlay starts there with an after-string, and the overlay has an
2545 invisible property. Since we don't skip invisible text in
2546 display_line and elsewhere immediately after consuming the
2547 newline before the row start, such a POS will not be in a string,
2548 but the call to init_iterator below will move us to the
2549 after-string. */
2550 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
2551
2552 /* This only scans the current chunk -- it should scan all chunks.
2553 However, OVERLAY_STRING_CHUNK_SIZE has been increased from 3 in 21.1
2554 to 16 in 22.1 to make this a lesser problem. */
2555 for (i = 0; i < it->n_overlay_strings && i < OVERLAY_STRING_CHUNK_SIZE; ++i)
2556 {
2557 const char *s = SDATA (it->overlay_strings[i]);
2558 const char *e = s + SBYTES (it->overlay_strings[i]);
2559
2560 while (s < e && *s != '\n')
2561 ++s;
2562
2563 if (s < e)
2564 {
2565 overlay_strings_with_newlines = 1;
2566 break;
2567 }
2568 }
2569
2570 /* If position is within an overlay string, set up IT to the right
2571 overlay string. */
2572 if (pos->overlay_string_index >= 0)
2573 {
2574 int relative_index;
2575
2576 /* If the first overlay string happens to have a `display'
2577 property for an image, the iterator will be set up for that
2578 image, and we have to undo that setup first before we can
2579 correct the overlay string index. */
2580 if (it->method == GET_FROM_IMAGE)
2581 pop_it (it);
2582
2583 /* We already have the first chunk of overlay strings in
2584 IT->overlay_strings. Load more until the one for
2585 pos->overlay_string_index is in IT->overlay_strings. */
2586 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
2587 {
2588 int n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
2589 it->current.overlay_string_index = 0;
2590 while (n--)
2591 {
2592 load_overlay_strings (it, 0);
2593 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
2594 }
2595 }
2596
2597 it->current.overlay_string_index = pos->overlay_string_index;
2598 relative_index = (it->current.overlay_string_index
2599 % OVERLAY_STRING_CHUNK_SIZE);
2600 it->string = it->overlay_strings[relative_index];
2601 xassert (STRINGP (it->string));
2602 it->current.string_pos = pos->string_pos;
2603 it->method = GET_FROM_STRING;
2604 }
2605
2606 #if 0 /* This is bogus because POS not having an overlay string
2607 position does not mean it's after the string. Example: A
2608 line starting with a before-string and initialization of IT
2609 to the previous row's end position. */
2610 else if (it->current.overlay_string_index >= 0)
2611 {
2612 /* If POS says we're already after an overlay string ending at
2613 POS, make sure to pop the iterator because it will be in
2614 front of that overlay string. When POS is ZV, we've thereby
2615 also ``processed'' overlay strings at ZV. */
2616 while (it->sp)
2617 pop_it (it);
2618 it->current.overlay_string_index = -1;
2619 it->method = GET_FROM_BUFFER;
2620 if (CHARPOS (pos->pos) == ZV)
2621 it->overlay_strings_at_end_processed_p = 1;
2622 }
2623 #endif /* 0 */
2624
2625 if (CHARPOS (pos->string_pos) >= 0)
2626 {
2627 /* Recorded position is not in an overlay string, but in another
2628 string. This can only be a string from a `display' property.
2629 IT should already be filled with that string. */
2630 it->current.string_pos = pos->string_pos;
2631 xassert (STRINGP (it->string));
2632 }
2633
2634 /* Restore position in display vector translations, control
2635 character translations or ellipses. */
2636 if (pos->dpvec_index >= 0)
2637 {
2638 if (it->dpvec == NULL)
2639 get_next_display_element (it);
2640 xassert (it->dpvec && it->current.dpvec_index == 0);
2641 it->current.dpvec_index = pos->dpvec_index;
2642 }
2643
2644 CHECK_IT (it);
2645 return !overlay_strings_with_newlines;
2646 }
2647
2648
2649 /* Initialize IT for stepping through current_buffer in window W
2650 starting at ROW->start. */
2651
2652 static void
2653 init_to_row_start (it, w, row)
2654 struct it *it;
2655 struct window *w;
2656 struct glyph_row *row;
2657 {
2658 init_from_display_pos (it, w, &row->start);
2659 it->start = row->start;
2660 it->continuation_lines_width = row->continuation_lines_width;
2661 CHECK_IT (it);
2662 }
2663
2664
2665 /* Initialize IT for stepping through current_buffer in window W
2666 starting in the line following ROW, i.e. starting at ROW->end.
2667 Value is zero if there are overlay strings with newlines at ROW's
2668 end position. */
2669
2670 static int
2671 init_to_row_end (it, w, row)
2672 struct it *it;
2673 struct window *w;
2674 struct glyph_row *row;
2675 {
2676 int success = 0;
2677
2678 if (init_from_display_pos (it, w, &row->end))
2679 {
2680 if (row->continued_p)
2681 it->continuation_lines_width
2682 = row->continuation_lines_width + row->pixel_width;
2683 CHECK_IT (it);
2684 success = 1;
2685 }
2686
2687 return success;
2688 }
2689
2690
2691
2692 \f
2693 /***********************************************************************
2694 Text properties
2695 ***********************************************************************/
2696
2697 /* Called when IT reaches IT->stop_charpos. Handle text property and
2698 overlay changes. Set IT->stop_charpos to the next position where
2699 to stop. */
2700
2701 static void
2702 handle_stop (it)
2703 struct it *it;
2704 {
2705 enum prop_handled handled;
2706 int handle_overlay_change_p = 1;
2707 struct props *p;
2708
2709 it->dpvec = NULL;
2710 it->current.dpvec_index = -1;
2711
2712 /* Use face of preceding text for ellipsis (if invisible) */
2713 if (it->selective_display_ellipsis_p)
2714 it->saved_face_id = it->face_id;
2715
2716 do
2717 {
2718 handled = HANDLED_NORMALLY;
2719
2720 /* Call text property handlers. */
2721 for (p = it_props; p->handler; ++p)
2722 {
2723 handled = p->handler (it);
2724
2725 if (handled == HANDLED_RECOMPUTE_PROPS)
2726 break;
2727 else if (handled == HANDLED_RETURN)
2728 return;
2729 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
2730 handle_overlay_change_p = 0;
2731 }
2732
2733 if (handled != HANDLED_RECOMPUTE_PROPS)
2734 {
2735 /* Don't check for overlay strings below when set to deliver
2736 characters from a display vector. */
2737 if (it->method == GET_FROM_DISPLAY_VECTOR)
2738 handle_overlay_change_p = 0;
2739
2740 /* Handle overlay changes. */
2741 if (handle_overlay_change_p)
2742 handled = handle_overlay_change (it);
2743
2744 /* Determine where to stop next. */
2745 if (handled == HANDLED_NORMALLY)
2746 compute_stop_pos (it);
2747 }
2748 }
2749 while (handled == HANDLED_RECOMPUTE_PROPS);
2750 }
2751
2752
2753 /* Compute IT->stop_charpos from text property and overlay change
2754 information for IT's current position. */
2755
2756 static void
2757 compute_stop_pos (it)
2758 struct it *it;
2759 {
2760 register INTERVAL iv, next_iv;
2761 Lisp_Object object, limit, position;
2762
2763 /* If nowhere else, stop at the end. */
2764 it->stop_charpos = it->end_charpos;
2765
2766 if (STRINGP (it->string))
2767 {
2768 /* Strings are usually short, so don't limit the search for
2769 properties. */
2770 object = it->string;
2771 limit = Qnil;
2772 position = make_number (IT_STRING_CHARPOS (*it));
2773 }
2774 else
2775 {
2776 int charpos;
2777
2778 /* If next overlay change is in front of the current stop pos
2779 (which is IT->end_charpos), stop there. Note: value of
2780 next_overlay_change is point-max if no overlay change
2781 follows. */
2782 charpos = next_overlay_change (IT_CHARPOS (*it));
2783 if (charpos < it->stop_charpos)
2784 it->stop_charpos = charpos;
2785
2786 /* If showing the region, we have to stop at the region
2787 start or end because the face might change there. */
2788 if (it->region_beg_charpos > 0)
2789 {
2790 if (IT_CHARPOS (*it) < it->region_beg_charpos)
2791 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
2792 else if (IT_CHARPOS (*it) < it->region_end_charpos)
2793 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
2794 }
2795
2796 /* Set up variables for computing the stop position from text
2797 property changes. */
2798 XSETBUFFER (object, current_buffer);
2799 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
2800 position = make_number (IT_CHARPOS (*it));
2801
2802 }
2803
2804 /* Get the interval containing IT's position. Value is a null
2805 interval if there isn't such an interval. */
2806 iv = validate_interval_range (object, &position, &position, 0);
2807 if (!NULL_INTERVAL_P (iv))
2808 {
2809 Lisp_Object values_here[LAST_PROP_IDX];
2810 struct props *p;
2811
2812 /* Get properties here. */
2813 for (p = it_props; p->handler; ++p)
2814 values_here[p->idx] = textget (iv->plist, *p->name);
2815
2816 /* Look for an interval following iv that has different
2817 properties. */
2818 for (next_iv = next_interval (iv);
2819 (!NULL_INTERVAL_P (next_iv)
2820 && (NILP (limit)
2821 || XFASTINT (limit) > next_iv->position));
2822 next_iv = next_interval (next_iv))
2823 {
2824 for (p = it_props; p->handler; ++p)
2825 {
2826 Lisp_Object new_value;
2827
2828 new_value = textget (next_iv->plist, *p->name);
2829 if (!EQ (values_here[p->idx], new_value))
2830 break;
2831 }
2832
2833 if (p->handler)
2834 break;
2835 }
2836
2837 if (!NULL_INTERVAL_P (next_iv))
2838 {
2839 if (INTEGERP (limit)
2840 && next_iv->position >= XFASTINT (limit))
2841 /* No text property change up to limit. */
2842 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
2843 else
2844 /* Text properties change in next_iv. */
2845 it->stop_charpos = min (it->stop_charpos, next_iv->position);
2846 }
2847 }
2848
2849 xassert (STRINGP (it->string)
2850 || (it->stop_charpos >= BEGV
2851 && it->stop_charpos >= IT_CHARPOS (*it)));
2852 }
2853
2854
2855 /* Return the position of the next overlay change after POS in
2856 current_buffer. Value is point-max if no overlay change
2857 follows. This is like `next-overlay-change' but doesn't use
2858 xmalloc. */
2859
2860 static int
2861 next_overlay_change (pos)
2862 int pos;
2863 {
2864 int noverlays;
2865 int endpos;
2866 Lisp_Object *overlays;
2867 int i;
2868
2869 /* Get all overlays at the given position. */
2870 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, 1);
2871
2872 /* If any of these overlays ends before endpos,
2873 use its ending point instead. */
2874 for (i = 0; i < noverlays; ++i)
2875 {
2876 Lisp_Object oend;
2877 int oendpos;
2878
2879 oend = OVERLAY_END (overlays[i]);
2880 oendpos = OVERLAY_POSITION (oend);
2881 endpos = min (endpos, oendpos);
2882 }
2883
2884 return endpos;
2885 }
2886
2887
2888 \f
2889 /***********************************************************************
2890 Fontification
2891 ***********************************************************************/
2892
2893 /* Handle changes in the `fontified' property of the current buffer by
2894 calling hook functions from Qfontification_functions to fontify
2895 regions of text. */
2896
2897 static enum prop_handled
2898 handle_fontified_prop (it)
2899 struct it *it;
2900 {
2901 Lisp_Object prop, pos;
2902 enum prop_handled handled = HANDLED_NORMALLY;
2903
2904 /* Get the value of the `fontified' property at IT's current buffer
2905 position. (The `fontified' property doesn't have a special
2906 meaning in strings.) If the value is nil, call functions from
2907 Qfontification_functions. */
2908 if (!STRINGP (it->string)
2909 && it->s == NULL
2910 && !NILP (Vfontification_functions)
2911 && !NILP (Vrun_hooks)
2912 && (pos = make_number (IT_CHARPOS (*it)),
2913 prop = Fget_char_property (pos, Qfontified, Qnil),
2914 NILP (prop)))
2915 {
2916 int count = SPECPDL_INDEX ();
2917 Lisp_Object val;
2918
2919 val = Vfontification_functions;
2920 specbind (Qfontification_functions, Qnil);
2921
2922 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
2923 safe_call1 (val, pos);
2924 else
2925 {
2926 Lisp_Object globals, fn;
2927 struct gcpro gcpro1, gcpro2;
2928
2929 globals = Qnil;
2930 GCPRO2 (val, globals);
2931
2932 for (; CONSP (val); val = XCDR (val))
2933 {
2934 fn = XCAR (val);
2935
2936 if (EQ (fn, Qt))
2937 {
2938 /* A value of t indicates this hook has a local
2939 binding; it means to run the global binding too.
2940 In a global value, t should not occur. If it
2941 does, we must ignore it to avoid an endless
2942 loop. */
2943 for (globals = Fdefault_value (Qfontification_functions);
2944 CONSP (globals);
2945 globals = XCDR (globals))
2946 {
2947 fn = XCAR (globals);
2948 if (!EQ (fn, Qt))
2949 safe_call1 (fn, pos);
2950 }
2951 }
2952 else
2953 safe_call1 (fn, pos);
2954 }
2955
2956 UNGCPRO;
2957 }
2958
2959 unbind_to (count, Qnil);
2960
2961 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
2962 something. This avoids an endless loop if they failed to
2963 fontify the text for which reason ever. */
2964 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
2965 handled = HANDLED_RECOMPUTE_PROPS;
2966 }
2967
2968 return handled;
2969 }
2970
2971
2972 \f
2973 /***********************************************************************
2974 Faces
2975 ***********************************************************************/
2976
2977 /* Set up iterator IT from face properties at its current position.
2978 Called from handle_stop. */
2979
2980 static enum prop_handled
2981 handle_face_prop (it)
2982 struct it *it;
2983 {
2984 int new_face_id, next_stop;
2985
2986 if (!STRINGP (it->string))
2987 {
2988 new_face_id
2989 = face_at_buffer_position (it->w,
2990 IT_CHARPOS (*it),
2991 it->region_beg_charpos,
2992 it->region_end_charpos,
2993 &next_stop,
2994 (IT_CHARPOS (*it)
2995 + TEXT_PROP_DISTANCE_LIMIT),
2996 0);
2997
2998 /* Is this a start of a run of characters with box face?
2999 Caveat: this can be called for a freshly initialized
3000 iterator; face_id is -1 in this case. We know that the new
3001 face will not change until limit, i.e. if the new face has a
3002 box, all characters up to limit will have one. But, as
3003 usual, we don't know whether limit is really the end. */
3004 if (new_face_id != it->face_id)
3005 {
3006 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3007
3008 /* If new face has a box but old face has not, this is
3009 the start of a run of characters with box, i.e. it has
3010 a shadow on the left side. The value of face_id of the
3011 iterator will be -1 if this is the initial call that gets
3012 the face. In this case, we have to look in front of IT's
3013 position and see whether there is a face != new_face_id. */
3014 it->start_of_box_run_p
3015 = (new_face->box != FACE_NO_BOX
3016 && (it->face_id >= 0
3017 || IT_CHARPOS (*it) == BEG
3018 || new_face_id != face_before_it_pos (it)));
3019 it->face_box_p = new_face->box != FACE_NO_BOX;
3020 }
3021 }
3022 else
3023 {
3024 int base_face_id, bufpos;
3025
3026 if (it->current.overlay_string_index >= 0)
3027 bufpos = IT_CHARPOS (*it);
3028 else
3029 bufpos = 0;
3030
3031 /* For strings from a buffer, i.e. overlay strings or strings
3032 from a `display' property, use the face at IT's current
3033 buffer position as the base face to merge with, so that
3034 overlay strings appear in the same face as surrounding
3035 text, unless they specify their own faces. */
3036 base_face_id = underlying_face_id (it);
3037
3038 new_face_id = face_at_string_position (it->w,
3039 it->string,
3040 IT_STRING_CHARPOS (*it),
3041 bufpos,
3042 it->region_beg_charpos,
3043 it->region_end_charpos,
3044 &next_stop,
3045 base_face_id, 0);
3046
3047 #if 0 /* This shouldn't be neccessary. Let's check it. */
3048 /* If IT is used to display a mode line we would really like to
3049 use the mode line face instead of the frame's default face. */
3050 if (it->glyph_row == MATRIX_MODE_LINE_ROW (it->w->desired_matrix)
3051 && new_face_id == DEFAULT_FACE_ID)
3052 new_face_id = CURRENT_MODE_LINE_FACE_ID (it->w);
3053 #endif
3054
3055 /* Is this a start of a run of characters with box? Caveat:
3056 this can be called for a freshly allocated iterator; face_id
3057 is -1 is this case. We know that the new face will not
3058 change until the next check pos, i.e. if the new face has a
3059 box, all characters up to that position will have a
3060 box. But, as usual, we don't know whether that position
3061 is really the end. */
3062 if (new_face_id != it->face_id)
3063 {
3064 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3065 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3066
3067 /* If new face has a box but old face hasn't, this is the
3068 start of a run of characters with box, i.e. it has a
3069 shadow on the left side. */
3070 it->start_of_box_run_p
3071 = new_face->box && (old_face == NULL || !old_face->box);
3072 it->face_box_p = new_face->box != FACE_NO_BOX;
3073 }
3074 }
3075
3076 it->face_id = new_face_id;
3077 return HANDLED_NORMALLY;
3078 }
3079
3080
3081 /* Return the ID of the face ``underlying'' IT's current position,
3082 which is in a string. If the iterator is associated with a
3083 buffer, return the face at IT's current buffer position.
3084 Otherwise, use the iterator's base_face_id. */
3085
3086 static int
3087 underlying_face_id (it)
3088 struct it *it;
3089 {
3090 int face_id = it->base_face_id, i;
3091
3092 xassert (STRINGP (it->string));
3093
3094 for (i = it->sp - 1; i >= 0; --i)
3095 if (NILP (it->stack[i].string))
3096 face_id = it->stack[i].face_id;
3097
3098 return face_id;
3099 }
3100
3101
3102 /* Compute the face one character before or after the current position
3103 of IT. BEFORE_P non-zero means get the face in front of IT's
3104 position. Value is the id of the face. */
3105
3106 static int
3107 face_before_or_after_it_pos (it, before_p)
3108 struct it *it;
3109 int before_p;
3110 {
3111 int face_id, limit;
3112 int next_check_charpos;
3113 struct text_pos pos;
3114
3115 xassert (it->s == NULL);
3116
3117 if (STRINGP (it->string))
3118 {
3119 int bufpos, base_face_id;
3120
3121 /* No face change past the end of the string (for the case
3122 we are padding with spaces). No face change before the
3123 string start. */
3124 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
3125 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
3126 return it->face_id;
3127
3128 /* Set pos to the position before or after IT's current position. */
3129 if (before_p)
3130 pos = string_pos (IT_STRING_CHARPOS (*it) - 1, it->string);
3131 else
3132 /* For composition, we must check the character after the
3133 composition. */
3134 pos = (it->what == IT_COMPOSITION
3135 ? string_pos (IT_STRING_CHARPOS (*it) + it->cmp_len, it->string)
3136 : string_pos (IT_STRING_CHARPOS (*it) + 1, it->string));
3137
3138 if (it->current.overlay_string_index >= 0)
3139 bufpos = IT_CHARPOS (*it);
3140 else
3141 bufpos = 0;
3142
3143 base_face_id = underlying_face_id (it);
3144
3145 /* Get the face for ASCII, or unibyte. */
3146 face_id = face_at_string_position (it->w,
3147 it->string,
3148 CHARPOS (pos),
3149 bufpos,
3150 it->region_beg_charpos,
3151 it->region_end_charpos,
3152 &next_check_charpos,
3153 base_face_id, 0);
3154
3155 /* Correct the face for charsets different from ASCII. Do it
3156 for the multibyte case only. The face returned above is
3157 suitable for unibyte text if IT->string is unibyte. */
3158 if (STRING_MULTIBYTE (it->string))
3159 {
3160 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos);
3161 int rest = SBYTES (it->string) - BYTEPOS (pos);
3162 int c, len;
3163 struct face *face = FACE_FROM_ID (it->f, face_id);
3164
3165 c = string_char_and_length (p, rest, &len);
3166 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), it->string);
3167 }
3168 }
3169 else
3170 {
3171 if ((IT_CHARPOS (*it) >= ZV && !before_p)
3172 || (IT_CHARPOS (*it) <= BEGV && before_p))
3173 return it->face_id;
3174
3175 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
3176 pos = it->current.pos;
3177
3178 if (before_p)
3179 DEC_TEXT_POS (pos, it->multibyte_p);
3180 else
3181 {
3182 if (it->what == IT_COMPOSITION)
3183 /* For composition, we must check the position after the
3184 composition. */
3185 pos.charpos += it->cmp_len, pos.bytepos += it->len;
3186 else
3187 INC_TEXT_POS (pos, it->multibyte_p);
3188 }
3189
3190 /* Determine face for CHARSET_ASCII, or unibyte. */
3191 face_id = face_at_buffer_position (it->w,
3192 CHARPOS (pos),
3193 it->region_beg_charpos,
3194 it->region_end_charpos,
3195 &next_check_charpos,
3196 limit, 0);
3197
3198 /* Correct the face for charsets different from ASCII. Do it
3199 for the multibyte case only. The face returned above is
3200 suitable for unibyte text if current_buffer is unibyte. */
3201 if (it->multibyte_p)
3202 {
3203 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
3204 struct face *face = FACE_FROM_ID (it->f, face_id);
3205 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), Qnil);
3206 }
3207 }
3208
3209 return face_id;
3210 }
3211
3212
3213 \f
3214 /***********************************************************************
3215 Invisible text
3216 ***********************************************************************/
3217
3218 /* Set up iterator IT from invisible properties at its current
3219 position. Called from handle_stop. */
3220
3221 static enum prop_handled
3222 handle_invisible_prop (it)
3223 struct it *it;
3224 {
3225 enum prop_handled handled = HANDLED_NORMALLY;
3226
3227 if (STRINGP (it->string))
3228 {
3229 extern Lisp_Object Qinvisible;
3230 Lisp_Object prop, end_charpos, limit, charpos;
3231
3232 /* Get the value of the invisible text property at the
3233 current position. Value will be nil if there is no such
3234 property. */
3235 charpos = make_number (IT_STRING_CHARPOS (*it));
3236 prop = Fget_text_property (charpos, Qinvisible, it->string);
3237
3238 if (!NILP (prop)
3239 && IT_STRING_CHARPOS (*it) < it->end_charpos)
3240 {
3241 handled = HANDLED_RECOMPUTE_PROPS;
3242
3243 /* Get the position at which the next change of the
3244 invisible text property can be found in IT->string.
3245 Value will be nil if the property value is the same for
3246 all the rest of IT->string. */
3247 XSETINT (limit, SCHARS (it->string));
3248 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
3249 it->string, limit);
3250
3251 /* Text at current position is invisible. The next
3252 change in the property is at position end_charpos.
3253 Move IT's current position to that position. */
3254 if (INTEGERP (end_charpos)
3255 && XFASTINT (end_charpos) < XFASTINT (limit))
3256 {
3257 struct text_pos old;
3258 old = it->current.string_pos;
3259 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
3260 compute_string_pos (&it->current.string_pos, old, it->string);
3261 }
3262 else
3263 {
3264 /* The rest of the string is invisible. If this is an
3265 overlay string, proceed with the next overlay string
3266 or whatever comes and return a character from there. */
3267 if (it->current.overlay_string_index >= 0)
3268 {
3269 next_overlay_string (it);
3270 /* Don't check for overlay strings when we just
3271 finished processing them. */
3272 handled = HANDLED_OVERLAY_STRING_CONSUMED;
3273 }
3274 else
3275 {
3276 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
3277 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
3278 }
3279 }
3280 }
3281 }
3282 else
3283 {
3284 int invis_p, newpos, next_stop, start_charpos;
3285 Lisp_Object pos, prop, overlay;
3286
3287 /* First of all, is there invisible text at this position? */
3288 start_charpos = IT_CHARPOS (*it);
3289 pos = make_number (IT_CHARPOS (*it));
3290 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
3291 &overlay);
3292 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3293
3294 /* If we are on invisible text, skip over it. */
3295 if (invis_p && IT_CHARPOS (*it) < it->end_charpos)
3296 {
3297 /* Record whether we have to display an ellipsis for the
3298 invisible text. */
3299 int display_ellipsis_p = invis_p == 2;
3300
3301 handled = HANDLED_RECOMPUTE_PROPS;
3302
3303 /* Loop skipping over invisible text. The loop is left at
3304 ZV or with IT on the first char being visible again. */
3305 do
3306 {
3307 /* Try to skip some invisible text. Return value is the
3308 position reached which can be equal to IT's position
3309 if there is nothing invisible here. This skips both
3310 over invisible text properties and overlays with
3311 invisible property. */
3312 newpos = skip_invisible (IT_CHARPOS (*it),
3313 &next_stop, ZV, it->window);
3314
3315 /* If we skipped nothing at all we weren't at invisible
3316 text in the first place. If everything to the end of
3317 the buffer was skipped, end the loop. */
3318 if (newpos == IT_CHARPOS (*it) || newpos >= ZV)
3319 invis_p = 0;
3320 else
3321 {
3322 /* We skipped some characters but not necessarily
3323 all there are. Check if we ended up on visible
3324 text. Fget_char_property returns the property of
3325 the char before the given position, i.e. if we
3326 get invis_p = 0, this means that the char at
3327 newpos is visible. */
3328 pos = make_number (newpos);
3329 prop = Fget_char_property (pos, Qinvisible, it->window);
3330 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3331 }
3332
3333 /* If we ended up on invisible text, proceed to
3334 skip starting with next_stop. */
3335 if (invis_p)
3336 IT_CHARPOS (*it) = next_stop;
3337 }
3338 while (invis_p);
3339
3340 /* The position newpos is now either ZV or on visible text. */
3341 IT_CHARPOS (*it) = newpos;
3342 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
3343
3344 /* If there are before-strings at the start of invisible
3345 text, and the text is invisible because of a text
3346 property, arrange to show before-strings because 20.x did
3347 it that way. (If the text is invisible because of an
3348 overlay property instead of a text property, this is
3349 already handled in the overlay code.) */
3350 if (NILP (overlay)
3351 && get_overlay_strings (it, start_charpos))
3352 {
3353 handled = HANDLED_RECOMPUTE_PROPS;
3354 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
3355 }
3356 else if (display_ellipsis_p)
3357 setup_for_ellipsis (it, 0);
3358 }
3359 }
3360
3361 return handled;
3362 }
3363
3364
3365 /* Make iterator IT return `...' next.
3366 Replaces LEN characters from buffer. */
3367
3368 static void
3369 setup_for_ellipsis (it, len)
3370 struct it *it;
3371 int len;
3372 {
3373 /* Use the display table definition for `...'. Invalid glyphs
3374 will be handled by the method returning elements from dpvec. */
3375 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
3376 {
3377 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
3378 it->dpvec = v->contents;
3379 it->dpend = v->contents + v->size;
3380 }
3381 else
3382 {
3383 /* Default `...'. */
3384 it->dpvec = default_invis_vector;
3385 it->dpend = default_invis_vector + 3;
3386 }
3387
3388 it->dpvec_char_len = len;
3389 it->current.dpvec_index = 0;
3390 it->dpvec_face_id = -1;
3391
3392 /* Remember the current face id in case glyphs specify faces.
3393 IT's face is restored in set_iterator_to_next.
3394 saved_face_id was set to preceding char's face in handle_stop. */
3395 if (it->saved_face_id < 0 || it->saved_face_id != it->face_id)
3396 it->saved_face_id = it->face_id = DEFAULT_FACE_ID;
3397
3398 it->method = GET_FROM_DISPLAY_VECTOR;
3399 it->ellipsis_p = 1;
3400 }
3401
3402
3403 \f
3404 /***********************************************************************
3405 'display' property
3406 ***********************************************************************/
3407
3408 /* Set up iterator IT from `display' property at its current position.
3409 Called from handle_stop.
3410 We return HANDLED_RETURN if some part of the display property
3411 overrides the display of the buffer text itself.
3412 Otherwise we return HANDLED_NORMALLY. */
3413
3414 static enum prop_handled
3415 handle_display_prop (it)
3416 struct it *it;
3417 {
3418 Lisp_Object prop, object;
3419 struct text_pos *position;
3420 /* Nonzero if some property replaces the display of the text itself. */
3421 int display_replaced_p = 0;
3422
3423 if (STRINGP (it->string))
3424 {
3425 object = it->string;
3426 position = &it->current.string_pos;
3427 }
3428 else
3429 {
3430 object = it->w->buffer;
3431 position = &it->current.pos;
3432 }
3433
3434 /* Reset those iterator values set from display property values. */
3435 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
3436 it->space_width = Qnil;
3437 it->font_height = Qnil;
3438 it->voffset = 0;
3439
3440 /* We don't support recursive `display' properties, i.e. string
3441 values that have a string `display' property, that have a string
3442 `display' property etc. */
3443 if (!it->string_from_display_prop_p)
3444 it->area = TEXT_AREA;
3445
3446 prop = Fget_char_property (make_number (position->charpos),
3447 Qdisplay, object);
3448 if (NILP (prop))
3449 return HANDLED_NORMALLY;
3450
3451 if (CONSP (prop)
3452 /* Simple properties. */
3453 && !EQ (XCAR (prop), Qimage)
3454 && !EQ (XCAR (prop), Qspace)
3455 && !EQ (XCAR (prop), Qwhen)
3456 && !EQ (XCAR (prop), Qslice)
3457 && !EQ (XCAR (prop), Qspace_width)
3458 && !EQ (XCAR (prop), Qheight)
3459 && !EQ (XCAR (prop), Qraise)
3460 /* Marginal area specifications. */
3461 && !(CONSP (XCAR (prop)) && EQ (XCAR (XCAR (prop)), Qmargin))
3462 && !EQ (XCAR (prop), Qleft_fringe)
3463 && !EQ (XCAR (prop), Qright_fringe)
3464 && !NILP (XCAR (prop)))
3465 {
3466 for (; CONSP (prop); prop = XCDR (prop))
3467 {
3468 if (handle_single_display_spec (it, XCAR (prop), object,
3469 position, display_replaced_p))
3470 display_replaced_p = 1;
3471 }
3472 }
3473 else if (VECTORP (prop))
3474 {
3475 int i;
3476 for (i = 0; i < ASIZE (prop); ++i)
3477 if (handle_single_display_spec (it, AREF (prop, i), object,
3478 position, display_replaced_p))
3479 display_replaced_p = 1;
3480 }
3481 else
3482 {
3483 int ret = handle_single_display_spec (it, prop, object, position, 0);
3484 if (ret < 0) /* Replaced by "", i.e. nothing. */
3485 return HANDLED_RECOMPUTE_PROPS;
3486 if (ret)
3487 display_replaced_p = 1;
3488 }
3489
3490 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
3491 }
3492
3493
3494 /* Value is the position of the end of the `display' property starting
3495 at START_POS in OBJECT. */
3496
3497 static struct text_pos
3498 display_prop_end (it, object, start_pos)
3499 struct it *it;
3500 Lisp_Object object;
3501 struct text_pos start_pos;
3502 {
3503 Lisp_Object end;
3504 struct text_pos end_pos;
3505
3506 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
3507 Qdisplay, object, Qnil);
3508 CHARPOS (end_pos) = XFASTINT (end);
3509 if (STRINGP (object))
3510 compute_string_pos (&end_pos, start_pos, it->string);
3511 else
3512 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
3513
3514 return end_pos;
3515 }
3516
3517
3518 /* Set up IT from a single `display' specification PROP. OBJECT
3519 is the object in which the `display' property was found. *POSITION
3520 is the position at which it was found. DISPLAY_REPLACED_P non-zero
3521 means that we previously saw a display specification which already
3522 replaced text display with something else, for example an image;
3523 we ignore such properties after the first one has been processed.
3524
3525 If PROP is a `space' or `image' specification, and in some other
3526 cases too, set *POSITION to the position where the `display'
3527 property ends.
3528
3529 Value is non-zero if something was found which replaces the display
3530 of buffer or string text. Specifically, the value is -1 if that
3531 "something" is "nothing". */
3532
3533 static int
3534 handle_single_display_spec (it, spec, object, position,
3535 display_replaced_before_p)
3536 struct it *it;
3537 Lisp_Object spec;
3538 Lisp_Object object;
3539 struct text_pos *position;
3540 int display_replaced_before_p;
3541 {
3542 Lisp_Object form;
3543 Lisp_Object location, value;
3544 struct text_pos start_pos;
3545 int valid_p;
3546
3547 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
3548 If the result is non-nil, use VALUE instead of SPEC. */
3549 form = Qt;
3550 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
3551 {
3552 spec = XCDR (spec);
3553 if (!CONSP (spec))
3554 return 0;
3555 form = XCAR (spec);
3556 spec = XCDR (spec);
3557 }
3558
3559 if (!NILP (form) && !EQ (form, Qt))
3560 {
3561 int count = SPECPDL_INDEX ();
3562 struct gcpro gcpro1;
3563
3564 /* Bind `object' to the object having the `display' property, a
3565 buffer or string. Bind `position' to the position in the
3566 object where the property was found, and `buffer-position'
3567 to the current position in the buffer. */
3568 specbind (Qobject, object);
3569 specbind (Qposition, make_number (CHARPOS (*position)));
3570 specbind (Qbuffer_position,
3571 make_number (STRINGP (object)
3572 ? IT_CHARPOS (*it) : CHARPOS (*position)));
3573 GCPRO1 (form);
3574 form = safe_eval (form);
3575 UNGCPRO;
3576 unbind_to (count, Qnil);
3577 }
3578
3579 if (NILP (form))
3580 return 0;
3581
3582 /* Handle `(height HEIGHT)' specifications. */
3583 if (CONSP (spec)
3584 && EQ (XCAR (spec), Qheight)
3585 && CONSP (XCDR (spec)))
3586 {
3587 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3588 return 0;
3589
3590 it->font_height = XCAR (XCDR (spec));
3591 if (!NILP (it->font_height))
3592 {
3593 struct face *face = FACE_FROM_ID (it->f, it->face_id);
3594 int new_height = -1;
3595
3596 if (CONSP (it->font_height)
3597 && (EQ (XCAR (it->font_height), Qplus)
3598 || EQ (XCAR (it->font_height), Qminus))
3599 && CONSP (XCDR (it->font_height))
3600 && INTEGERP (XCAR (XCDR (it->font_height))))
3601 {
3602 /* `(+ N)' or `(- N)' where N is an integer. */
3603 int steps = XINT (XCAR (XCDR (it->font_height)));
3604 if (EQ (XCAR (it->font_height), Qplus))
3605 steps = - steps;
3606 it->face_id = smaller_face (it->f, it->face_id, steps);
3607 }
3608 else if (FUNCTIONP (it->font_height))
3609 {
3610 /* Call function with current height as argument.
3611 Value is the new height. */
3612 Lisp_Object height;
3613 height = safe_call1 (it->font_height,
3614 face->lface[LFACE_HEIGHT_INDEX]);
3615 if (NUMBERP (height))
3616 new_height = XFLOATINT (height);
3617 }
3618 else if (NUMBERP (it->font_height))
3619 {
3620 /* Value is a multiple of the canonical char height. */
3621 struct face *face;
3622
3623 face = FACE_FROM_ID (it->f, DEFAULT_FACE_ID);
3624 new_height = (XFLOATINT (it->font_height)
3625 * XINT (face->lface[LFACE_HEIGHT_INDEX]));
3626 }
3627 else
3628 {
3629 /* Evaluate IT->font_height with `height' bound to the
3630 current specified height to get the new height. */
3631 int count = SPECPDL_INDEX ();
3632
3633 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
3634 value = safe_eval (it->font_height);
3635 unbind_to (count, Qnil);
3636
3637 if (NUMBERP (value))
3638 new_height = XFLOATINT (value);
3639 }
3640
3641 if (new_height > 0)
3642 it->face_id = face_with_height (it->f, it->face_id, new_height);
3643 }
3644
3645 return 0;
3646 }
3647
3648 /* Handle `(space_width WIDTH)'. */
3649 if (CONSP (spec)
3650 && EQ (XCAR (spec), Qspace_width)
3651 && CONSP (XCDR (spec)))
3652 {
3653 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3654 return 0;
3655
3656 value = XCAR (XCDR (spec));
3657 if (NUMBERP (value) && XFLOATINT (value) > 0)
3658 it->space_width = value;
3659
3660 return 0;
3661 }
3662
3663 /* Handle `(slice X Y WIDTH HEIGHT)'. */
3664 if (CONSP (spec)
3665 && EQ (XCAR (spec), Qslice))
3666 {
3667 Lisp_Object tem;
3668
3669 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3670 return 0;
3671
3672 if (tem = XCDR (spec), CONSP (tem))
3673 {
3674 it->slice.x = XCAR (tem);
3675 if (tem = XCDR (tem), CONSP (tem))
3676 {
3677 it->slice.y = XCAR (tem);
3678 if (tem = XCDR (tem), CONSP (tem))
3679 {
3680 it->slice.width = XCAR (tem);
3681 if (tem = XCDR (tem), CONSP (tem))
3682 it->slice.height = XCAR (tem);
3683 }
3684 }
3685 }
3686
3687 return 0;
3688 }
3689
3690 /* Handle `(raise FACTOR)'. */
3691 if (CONSP (spec)
3692 && EQ (XCAR (spec), Qraise)
3693 && CONSP (XCDR (spec)))
3694 {
3695 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3696 return 0;
3697
3698 #ifdef HAVE_WINDOW_SYSTEM
3699 value = XCAR (XCDR (spec));
3700 if (NUMBERP (value))
3701 {
3702 struct face *face = FACE_FROM_ID (it->f, it->face_id);
3703 it->voffset = - (XFLOATINT (value)
3704 * (FONT_HEIGHT (face->font)));
3705 }
3706 #endif /* HAVE_WINDOW_SYSTEM */
3707
3708 return 0;
3709 }
3710
3711 /* Don't handle the other kinds of display specifications
3712 inside a string that we got from a `display' property. */
3713 if (it->string_from_display_prop_p)
3714 return 0;
3715
3716 /* Characters having this form of property are not displayed, so
3717 we have to find the end of the property. */
3718 start_pos = *position;
3719 *position = display_prop_end (it, object, start_pos);
3720 value = Qnil;
3721
3722 /* Stop the scan at that end position--we assume that all
3723 text properties change there. */
3724 it->stop_charpos = position->charpos;
3725
3726 /* Handle `(left-fringe BITMAP [FACE])'
3727 and `(right-fringe BITMAP [FACE])'. */
3728 if (CONSP (spec)
3729 && (EQ (XCAR (spec), Qleft_fringe)
3730 || EQ (XCAR (spec), Qright_fringe))
3731 && CONSP (XCDR (spec)))
3732 {
3733 int face_id = DEFAULT_FACE_ID;
3734 int fringe_bitmap;
3735
3736 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3737 /* If we return here, POSITION has been advanced
3738 across the text with this property. */
3739 return 0;
3740
3741 #ifdef HAVE_WINDOW_SYSTEM
3742 value = XCAR (XCDR (spec));
3743 if (!SYMBOLP (value)
3744 || !(fringe_bitmap = lookup_fringe_bitmap (value)))
3745 /* If we return here, POSITION has been advanced
3746 across the text with this property. */
3747 return 0;
3748
3749 if (CONSP (XCDR (XCDR (spec))))
3750 {
3751 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
3752 int face_id2 = lookup_derived_face (it->f, face_name,
3753 FRINGE_FACE_ID, 0);
3754 if (face_id2 >= 0)
3755 face_id = face_id2;
3756 }
3757
3758 /* Save current settings of IT so that we can restore them
3759 when we are finished with the glyph property value. */
3760
3761 push_it (it);
3762
3763 it->area = TEXT_AREA;
3764 it->what = IT_IMAGE;
3765 it->image_id = -1; /* no image */
3766 it->position = start_pos;
3767 it->object = NILP (object) ? it->w->buffer : object;
3768 it->method = GET_FROM_IMAGE;
3769 it->face_id = face_id;
3770
3771 /* Say that we haven't consumed the characters with
3772 `display' property yet. The call to pop_it in
3773 set_iterator_to_next will clean this up. */
3774 *position = start_pos;
3775
3776 if (EQ (XCAR (spec), Qleft_fringe))
3777 {
3778 it->left_user_fringe_bitmap = fringe_bitmap;
3779 it->left_user_fringe_face_id = face_id;
3780 }
3781 else
3782 {
3783 it->right_user_fringe_bitmap = fringe_bitmap;
3784 it->right_user_fringe_face_id = face_id;
3785 }
3786 #endif /* HAVE_WINDOW_SYSTEM */
3787 return 1;
3788 }
3789
3790 /* Prepare to handle `((margin left-margin) ...)',
3791 `((margin right-margin) ...)' and `((margin nil) ...)'
3792 prefixes for display specifications. */
3793 location = Qunbound;
3794 if (CONSP (spec) && CONSP (XCAR (spec)))
3795 {
3796 Lisp_Object tem;
3797
3798 value = XCDR (spec);
3799 if (CONSP (value))
3800 value = XCAR (value);
3801
3802 tem = XCAR (spec);
3803 if (EQ (XCAR (tem), Qmargin)
3804 && (tem = XCDR (tem),
3805 tem = CONSP (tem) ? XCAR (tem) : Qnil,
3806 (NILP (tem)
3807 || EQ (tem, Qleft_margin)
3808 || EQ (tem, Qright_margin))))
3809 location = tem;
3810 }
3811
3812 if (EQ (location, Qunbound))
3813 {
3814 location = Qnil;
3815 value = spec;
3816 }
3817
3818 /* After this point, VALUE is the property after any
3819 margin prefix has been stripped. It must be a string,
3820 an image specification, or `(space ...)'.
3821
3822 LOCATION specifies where to display: `left-margin',
3823 `right-margin' or nil. */
3824
3825 valid_p = (STRINGP (value)
3826 #ifdef HAVE_WINDOW_SYSTEM
3827 || (!FRAME_TERMCAP_P (it->f) && valid_image_p (value))
3828 #endif /* not HAVE_WINDOW_SYSTEM */
3829 || (CONSP (value) && EQ (XCAR (value), Qspace)));
3830
3831 if (valid_p && !display_replaced_before_p)
3832 {
3833 /* Save current settings of IT so that we can restore them
3834 when we are finished with the glyph property value. */
3835 push_it (it);
3836
3837 if (NILP (location))
3838 it->area = TEXT_AREA;
3839 else if (EQ (location, Qleft_margin))
3840 it->area = LEFT_MARGIN_AREA;
3841 else
3842 it->area = RIGHT_MARGIN_AREA;
3843
3844 if (STRINGP (value))
3845 {
3846 if (SCHARS (value) == 0)
3847 {
3848 pop_it (it);
3849 return -1; /* Replaced by "", i.e. nothing. */
3850 }
3851 it->string = value;
3852 it->multibyte_p = STRING_MULTIBYTE (it->string);
3853 it->current.overlay_string_index = -1;
3854 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
3855 it->end_charpos = it->string_nchars = SCHARS (it->string);
3856 it->method = GET_FROM_STRING;
3857 it->stop_charpos = 0;
3858 it->string_from_display_prop_p = 1;
3859 /* Say that we haven't consumed the characters with
3860 `display' property yet. The call to pop_it in
3861 set_iterator_to_next will clean this up. */
3862 *position = start_pos;
3863 }
3864 else if (CONSP (value) && EQ (XCAR (value), Qspace))
3865 {
3866 it->method = GET_FROM_STRETCH;
3867 it->object = value;
3868 it->current.pos = it->position = start_pos;
3869 }
3870 #ifdef HAVE_WINDOW_SYSTEM
3871 else
3872 {
3873 it->what = IT_IMAGE;
3874 it->image_id = lookup_image (it->f, value);
3875 it->position = start_pos;
3876 it->object = NILP (object) ? it->w->buffer : object;
3877 it->method = GET_FROM_IMAGE;
3878
3879 /* Say that we haven't consumed the characters with
3880 `display' property yet. The call to pop_it in
3881 set_iterator_to_next will clean this up. */
3882 *position = start_pos;
3883 }
3884 #endif /* HAVE_WINDOW_SYSTEM */
3885
3886 return 1;
3887 }
3888
3889 /* Invalid property or property not supported. Restore
3890 POSITION to what it was before. */
3891 *position = start_pos;
3892 return 0;
3893 }
3894
3895
3896 /* Check if SPEC is a display specification value whose text should be
3897 treated as intangible. */
3898
3899 static int
3900 single_display_spec_intangible_p (prop)
3901 Lisp_Object prop;
3902 {
3903 /* Skip over `when FORM'. */
3904 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
3905 {
3906 prop = XCDR (prop);
3907 if (!CONSP (prop))
3908 return 0;
3909 prop = XCDR (prop);
3910 }
3911
3912 if (STRINGP (prop))
3913 return 1;
3914
3915 if (!CONSP (prop))
3916 return 0;
3917
3918 /* Skip over `margin LOCATION'. If LOCATION is in the margins,
3919 we don't need to treat text as intangible. */
3920 if (EQ (XCAR (prop), Qmargin))
3921 {
3922 prop = XCDR (prop);
3923 if (!CONSP (prop))
3924 return 0;
3925
3926 prop = XCDR (prop);
3927 if (!CONSP (prop)
3928 || EQ (XCAR (prop), Qleft_margin)
3929 || EQ (XCAR (prop), Qright_margin))
3930 return 0;
3931 }
3932
3933 return (CONSP (prop)
3934 && (EQ (XCAR (prop), Qimage)
3935 || EQ (XCAR (prop), Qspace)));
3936 }
3937
3938
3939 /* Check if PROP is a display property value whose text should be
3940 treated as intangible. */
3941
3942 int
3943 display_prop_intangible_p (prop)
3944 Lisp_Object prop;
3945 {
3946 if (CONSP (prop)
3947 && CONSP (XCAR (prop))
3948 && !EQ (Qmargin, XCAR (XCAR (prop))))
3949 {
3950 /* A list of sub-properties. */
3951 while (CONSP (prop))
3952 {
3953 if (single_display_spec_intangible_p (XCAR (prop)))
3954 return 1;
3955 prop = XCDR (prop);
3956 }
3957 }
3958 else if (VECTORP (prop))
3959 {
3960 /* A vector of sub-properties. */
3961 int i;
3962 for (i = 0; i < ASIZE (prop); ++i)
3963 if (single_display_spec_intangible_p (AREF (prop, i)))
3964 return 1;
3965 }
3966 else
3967 return single_display_spec_intangible_p (prop);
3968
3969 return 0;
3970 }
3971
3972
3973 /* Return 1 if PROP is a display sub-property value containing STRING. */
3974
3975 static int
3976 single_display_spec_string_p (prop, string)
3977 Lisp_Object prop, string;
3978 {
3979 if (EQ (string, prop))
3980 return 1;
3981
3982 /* Skip over `when FORM'. */
3983 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
3984 {
3985 prop = XCDR (prop);
3986 if (!CONSP (prop))
3987 return 0;
3988 prop = XCDR (prop);
3989 }
3990
3991 if (CONSP (prop))
3992 /* Skip over `margin LOCATION'. */
3993 if (EQ (XCAR (prop), Qmargin))
3994 {
3995 prop = XCDR (prop);
3996 if (!CONSP (prop))
3997 return 0;
3998
3999 prop = XCDR (prop);
4000 if (!CONSP (prop))
4001 return 0;
4002 }
4003
4004 return CONSP (prop) && EQ (XCAR (prop), string);
4005 }
4006
4007
4008 /* Return 1 if STRING appears in the `display' property PROP. */
4009
4010 static int
4011 display_prop_string_p (prop, string)
4012 Lisp_Object prop, string;
4013 {
4014 if (CONSP (prop)
4015 && CONSP (XCAR (prop))
4016 && !EQ (Qmargin, XCAR (XCAR (prop))))
4017 {
4018 /* A list of sub-properties. */
4019 while (CONSP (prop))
4020 {
4021 if (single_display_spec_string_p (XCAR (prop), string))
4022 return 1;
4023 prop = XCDR (prop);
4024 }
4025 }
4026 else if (VECTORP (prop))
4027 {
4028 /* A vector of sub-properties. */
4029 int i;
4030 for (i = 0; i < ASIZE (prop); ++i)
4031 if (single_display_spec_string_p (AREF (prop, i), string))
4032 return 1;
4033 }
4034 else
4035 return single_display_spec_string_p (prop, string);
4036
4037 return 0;
4038 }
4039
4040
4041 /* Determine from which buffer position in W's buffer STRING comes
4042 from. AROUND_CHARPOS is an approximate position where it could
4043 be from. Value is the buffer position or 0 if it couldn't be
4044 determined.
4045
4046 W's buffer must be current.
4047
4048 This function is necessary because we don't record buffer positions
4049 in glyphs generated from strings (to keep struct glyph small).
4050 This function may only use code that doesn't eval because it is
4051 called asynchronously from note_mouse_highlight. */
4052
4053 int
4054 string_buffer_position (w, string, around_charpos)
4055 struct window *w;
4056 Lisp_Object string;
4057 int around_charpos;
4058 {
4059 Lisp_Object limit, prop, pos;
4060 const int MAX_DISTANCE = 1000;
4061 int found = 0;
4062
4063 pos = make_number (around_charpos);
4064 limit = make_number (min (XINT (pos) + MAX_DISTANCE, ZV));
4065 while (!found && !EQ (pos, limit))
4066 {
4067 prop = Fget_char_property (pos, Qdisplay, Qnil);
4068 if (!NILP (prop) && display_prop_string_p (prop, string))
4069 found = 1;
4070 else
4071 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil, limit);
4072 }
4073
4074 if (!found)
4075 {
4076 pos = make_number (around_charpos);
4077 limit = make_number (max (XINT (pos) - MAX_DISTANCE, BEGV));
4078 while (!found && !EQ (pos, limit))
4079 {
4080 prop = Fget_char_property (pos, Qdisplay, Qnil);
4081 if (!NILP (prop) && display_prop_string_p (prop, string))
4082 found = 1;
4083 else
4084 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
4085 limit);
4086 }
4087 }
4088
4089 return found ? XINT (pos) : 0;
4090 }
4091
4092
4093 \f
4094 /***********************************************************************
4095 `composition' property
4096 ***********************************************************************/
4097
4098 static enum prop_handled
4099 handle_auto_composed_prop (it)
4100 struct it *it;
4101 {
4102 enum prop_handled handled = HANDLED_NORMALLY;
4103
4104 if (FUNCTIONP (Vauto_composition_function))
4105 {
4106 Lisp_Object val;
4107 EMACS_INT pos, this_pos;
4108
4109 if (STRINGP (it->string))
4110 pos = IT_STRING_CHARPOS (*it);
4111 else
4112 pos = IT_CHARPOS (*it);
4113 this_pos = pos;
4114
4115 val =Fget_char_property (make_number (pos), Qauto_composed, it->string);
4116 if (! NILP (val))
4117 {
4118 Lisp_Object limit = Qnil, next;
4119
4120 /* As Fnext_single_char_property_change is very slow, we
4121 limit the search to the current line. */
4122 if (STRINGP (it->string))
4123 limit = make_number (SCHARS (it->string));
4124 else
4125 limit = make_number (find_next_newline_no_quit (pos, 1));
4126
4127 next = (Fnext_single_property_change
4128 (make_number (pos), Qauto_composed, it->string, limit));
4129 if (XINT (next) < XINT (limit))
4130 {
4131 /* The current point is auto-composed, but there exist
4132 characters not yet composed beyond the auto-composed
4133 region. There's a possiblity that the last
4134 characters in the region may be newly composed. */
4135 int charpos = XINT (next) - 1, bytepos, c;
4136
4137 if (STRINGP (it->string))
4138 {
4139 bytepos = string_char_to_byte (it->string, charpos);
4140 c = SDATA (it->string)[bytepos];
4141 }
4142 else
4143 {
4144 bytepos = CHAR_TO_BYTE (charpos);
4145 c = FETCH_BYTE (bytepos);
4146 }
4147 if (c != '\n')
4148 /* If the last character is not newline, it may be
4149 composed with the following characters. */
4150 val = Qnil, pos = charpos + 1;
4151 }
4152 }
4153 if (NILP (val))
4154 {
4155 int count = SPECPDL_INDEX ();
4156 Lisp_Object args[3];
4157
4158 args[0] = Vauto_composition_function;
4159 specbind (Qauto_composition_function, Qnil);
4160 args[1] = make_number (pos);
4161 args[2] = it->string;
4162 safe_call (3, args);
4163 unbind_to (count, Qnil);
4164
4165 if (this_pos == pos)
4166 {
4167 val = Fget_char_property (args[1], Qauto_composed, it->string);
4168 /* Return HANDLED_RECOMPUTE_PROPS only if function composed
4169 something. This avoids an endless loop if they failed to
4170 fontify the text for which reason ever. */
4171 if (! NILP (val))
4172 handled = HANDLED_RECOMPUTE_PROPS;
4173 }
4174 else
4175 handled = HANDLED_RECOMPUTE_PROPS;
4176 }
4177 }
4178
4179 return handled;
4180 }
4181
4182 /* Set up iterator IT from `composition' property at its current
4183 position. Called from handle_stop. */
4184
4185 static enum prop_handled
4186 handle_composition_prop (it)
4187 struct it *it;
4188 {
4189 Lisp_Object prop, string;
4190 EMACS_INT pos, pos_byte, start, end;
4191 enum prop_handled handled = HANDLED_NORMALLY;
4192
4193 if (STRINGP (it->string))
4194 {
4195 pos = IT_STRING_CHARPOS (*it);
4196 pos_byte = IT_STRING_BYTEPOS (*it);
4197 string = it->string;
4198 }
4199 else
4200 {
4201 pos = IT_CHARPOS (*it);
4202 pos_byte = IT_BYTEPOS (*it);
4203 string = Qnil;
4204 }
4205
4206 /* If there's a valid composition and point is not inside of the
4207 composition (in the case that the composition is from the current
4208 buffer), draw a glyph composed from the composition components. */
4209 if (find_composition (pos, -1, &start, &end, &prop, string)
4210 && COMPOSITION_VALID_P (start, end, prop)
4211 && (STRINGP (it->string) || (PT <= start || PT >= end)))
4212 {
4213 int id;
4214
4215 if (start != pos)
4216 {
4217 if (STRINGP (it->string))
4218 pos_byte = string_char_to_byte (it->string, start);
4219 else
4220 pos_byte = CHAR_TO_BYTE (start);
4221 }
4222 id = get_composition_id (start, pos_byte, end - start, prop, string);
4223
4224 if (id >= 0)
4225 {
4226 it->method = GET_FROM_COMPOSITION;
4227 it->cmp_id = id;
4228 it->cmp_len = COMPOSITION_LENGTH (prop);
4229 /* For a terminal, draw only the first character of the
4230 components. */
4231 it->c = COMPOSITION_GLYPH (composition_table[id], 0);
4232 it->len = (STRINGP (it->string)
4233 ? string_char_to_byte (it->string, end)
4234 : CHAR_TO_BYTE (end)) - pos_byte;
4235 it->stop_charpos = end;
4236 handled = HANDLED_RETURN;
4237 }
4238 }
4239
4240 return handled;
4241 }
4242
4243
4244 \f
4245 /***********************************************************************
4246 Overlay strings
4247 ***********************************************************************/
4248
4249 /* The following structure is used to record overlay strings for
4250 later sorting in load_overlay_strings. */
4251
4252 struct overlay_entry
4253 {
4254 Lisp_Object overlay;
4255 Lisp_Object string;
4256 int priority;
4257 int after_string_p;
4258 };
4259
4260
4261 /* Set up iterator IT from overlay strings at its current position.
4262 Called from handle_stop. */
4263
4264 static enum prop_handled
4265 handle_overlay_change (it)
4266 struct it *it;
4267 {
4268 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
4269 return HANDLED_RECOMPUTE_PROPS;
4270 else
4271 return HANDLED_NORMALLY;
4272 }
4273
4274
4275 /* Set up the next overlay string for delivery by IT, if there is an
4276 overlay string to deliver. Called by set_iterator_to_next when the
4277 end of the current overlay string is reached. If there are more
4278 overlay strings to display, IT->string and
4279 IT->current.overlay_string_index are set appropriately here.
4280 Otherwise IT->string is set to nil. */
4281
4282 static void
4283 next_overlay_string (it)
4284 struct it *it;
4285 {
4286 ++it->current.overlay_string_index;
4287 if (it->current.overlay_string_index == it->n_overlay_strings)
4288 {
4289 /* No more overlay strings. Restore IT's settings to what
4290 they were before overlay strings were processed, and
4291 continue to deliver from current_buffer. */
4292 int display_ellipsis_p = it->stack[it->sp - 1].display_ellipsis_p;
4293
4294 pop_it (it);
4295 xassert (it->stop_charpos >= BEGV
4296 && it->stop_charpos <= it->end_charpos);
4297 it->string = Qnil;
4298 it->current.overlay_string_index = -1;
4299 SET_TEXT_POS (it->current.string_pos, -1, -1);
4300 it->n_overlay_strings = 0;
4301 it->method = GET_FROM_BUFFER;
4302
4303 /* If we're at the end of the buffer, record that we have
4304 processed the overlay strings there already, so that
4305 next_element_from_buffer doesn't try it again. */
4306 if (IT_CHARPOS (*it) >= it->end_charpos)
4307 it->overlay_strings_at_end_processed_p = 1;
4308
4309 /* If we have to display `...' for invisible text, set
4310 the iterator up for that. */
4311 if (display_ellipsis_p)
4312 setup_for_ellipsis (it, 0);
4313 }
4314 else
4315 {
4316 /* There are more overlay strings to process. If
4317 IT->current.overlay_string_index has advanced to a position
4318 where we must load IT->overlay_strings with more strings, do
4319 it. */
4320 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
4321
4322 if (it->current.overlay_string_index && i == 0)
4323 load_overlay_strings (it, 0);
4324
4325 /* Initialize IT to deliver display elements from the overlay
4326 string. */
4327 it->string = it->overlay_strings[i];
4328 it->multibyte_p = STRING_MULTIBYTE (it->string);
4329 SET_TEXT_POS (it->current.string_pos, 0, 0);
4330 it->method = GET_FROM_STRING;
4331 it->stop_charpos = 0;
4332 }
4333
4334 CHECK_IT (it);
4335 }
4336
4337
4338 /* Compare two overlay_entry structures E1 and E2. Used as a
4339 comparison function for qsort in load_overlay_strings. Overlay
4340 strings for the same position are sorted so that
4341
4342 1. All after-strings come in front of before-strings, except
4343 when they come from the same overlay.
4344
4345 2. Within after-strings, strings are sorted so that overlay strings
4346 from overlays with higher priorities come first.
4347
4348 2. Within before-strings, strings are sorted so that overlay
4349 strings from overlays with higher priorities come last.
4350
4351 Value is analogous to strcmp. */
4352
4353
4354 static int
4355 compare_overlay_entries (e1, e2)
4356 void *e1, *e2;
4357 {
4358 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
4359 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
4360 int result;
4361
4362 if (entry1->after_string_p != entry2->after_string_p)
4363 {
4364 /* Let after-strings appear in front of before-strings if
4365 they come from different overlays. */
4366 if (EQ (entry1->overlay, entry2->overlay))
4367 result = entry1->after_string_p ? 1 : -1;
4368 else
4369 result = entry1->after_string_p ? -1 : 1;
4370 }
4371 else if (entry1->after_string_p)
4372 /* After-strings sorted in order of decreasing priority. */
4373 result = entry2->priority - entry1->priority;
4374 else
4375 /* Before-strings sorted in order of increasing priority. */
4376 result = entry1->priority - entry2->priority;
4377
4378 return result;
4379 }
4380
4381
4382 /* Load the vector IT->overlay_strings with overlay strings from IT's
4383 current buffer position, or from CHARPOS if that is > 0. Set
4384 IT->n_overlays to the total number of overlay strings found.
4385
4386 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
4387 a time. On entry into load_overlay_strings,
4388 IT->current.overlay_string_index gives the number of overlay
4389 strings that have already been loaded by previous calls to this
4390 function.
4391
4392 IT->add_overlay_start contains an additional overlay start
4393 position to consider for taking overlay strings from, if non-zero.
4394 This position comes into play when the overlay has an `invisible'
4395 property, and both before and after-strings. When we've skipped to
4396 the end of the overlay, because of its `invisible' property, we
4397 nevertheless want its before-string to appear.
4398 IT->add_overlay_start will contain the overlay start position
4399 in this case.
4400
4401 Overlay strings are sorted so that after-string strings come in
4402 front of before-string strings. Within before and after-strings,
4403 strings are sorted by overlay priority. See also function
4404 compare_overlay_entries. */
4405
4406 static void
4407 load_overlay_strings (it, charpos)
4408 struct it *it;
4409 int charpos;
4410 {
4411 extern Lisp_Object Qafter_string, Qbefore_string, Qwindow, Qpriority;
4412 Lisp_Object overlay, window, str, invisible;
4413 struct Lisp_Overlay *ov;
4414 int start, end;
4415 int size = 20;
4416 int n = 0, i, j, invis_p;
4417 struct overlay_entry *entries
4418 = (struct overlay_entry *) alloca (size * sizeof *entries);
4419
4420 if (charpos <= 0)
4421 charpos = IT_CHARPOS (*it);
4422
4423 /* Append the overlay string STRING of overlay OVERLAY to vector
4424 `entries' which has size `size' and currently contains `n'
4425 elements. AFTER_P non-zero means STRING is an after-string of
4426 OVERLAY. */
4427 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
4428 do \
4429 { \
4430 Lisp_Object priority; \
4431 \
4432 if (n == size) \
4433 { \
4434 int new_size = 2 * size; \
4435 struct overlay_entry *old = entries; \
4436 entries = \
4437 (struct overlay_entry *) alloca (new_size \
4438 * sizeof *entries); \
4439 bcopy (old, entries, size * sizeof *entries); \
4440 size = new_size; \
4441 } \
4442 \
4443 entries[n].string = (STRING); \
4444 entries[n].overlay = (OVERLAY); \
4445 priority = Foverlay_get ((OVERLAY), Qpriority); \
4446 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
4447 entries[n].after_string_p = (AFTER_P); \
4448 ++n; \
4449 } \
4450 while (0)
4451
4452 /* Process overlay before the overlay center. */
4453 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
4454 {
4455 XSETMISC (overlay, ov);
4456 xassert (OVERLAYP (overlay));
4457 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4458 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4459
4460 if (end < charpos)
4461 break;
4462
4463 /* Skip this overlay if it doesn't start or end at IT's current
4464 position. */
4465 if (end != charpos && start != charpos)
4466 continue;
4467
4468 /* Skip this overlay if it doesn't apply to IT->w. */
4469 window = Foverlay_get (overlay, Qwindow);
4470 if (WINDOWP (window) && XWINDOW (window) != it->w)
4471 continue;
4472
4473 /* If the text ``under'' the overlay is invisible, both before-
4474 and after-strings from this overlay are visible; start and
4475 end position are indistinguishable. */
4476 invisible = Foverlay_get (overlay, Qinvisible);
4477 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4478
4479 /* If overlay has a non-empty before-string, record it. */
4480 if ((start == charpos || (end == charpos && invis_p))
4481 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
4482 && SCHARS (str))
4483 RECORD_OVERLAY_STRING (overlay, str, 0);
4484
4485 /* If overlay has a non-empty after-string, record it. */
4486 if ((end == charpos || (start == charpos && invis_p))
4487 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
4488 && SCHARS (str))
4489 RECORD_OVERLAY_STRING (overlay, str, 1);
4490 }
4491
4492 /* Process overlays after the overlay center. */
4493 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
4494 {
4495 XSETMISC (overlay, ov);
4496 xassert (OVERLAYP (overlay));
4497 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4498 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4499
4500 if (start > charpos)
4501 break;
4502
4503 /* Skip this overlay if it doesn't start or end at IT's current
4504 position. */
4505 if (end != charpos && start != charpos)
4506 continue;
4507
4508 /* Skip this overlay if it doesn't apply to IT->w. */
4509 window = Foverlay_get (overlay, Qwindow);
4510 if (WINDOWP (window) && XWINDOW (window) != it->w)
4511 continue;
4512
4513 /* If the text ``under'' the overlay is invisible, it has a zero
4514 dimension, and both before- and after-strings apply. */
4515 invisible = Foverlay_get (overlay, Qinvisible);
4516 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4517
4518 /* If overlay has a non-empty before-string, record it. */
4519 if ((start == charpos || (end == charpos && invis_p))
4520 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
4521 && SCHARS (str))
4522 RECORD_OVERLAY_STRING (overlay, str, 0);
4523
4524 /* If overlay has a non-empty after-string, record it. */
4525 if ((end == charpos || (start == charpos && invis_p))
4526 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
4527 && SCHARS (str))
4528 RECORD_OVERLAY_STRING (overlay, str, 1);
4529 }
4530
4531 #undef RECORD_OVERLAY_STRING
4532
4533 /* Sort entries. */
4534 if (n > 1)
4535 qsort (entries, n, sizeof *entries, compare_overlay_entries);
4536
4537 /* Record the total number of strings to process. */
4538 it->n_overlay_strings = n;
4539
4540 /* IT->current.overlay_string_index is the number of overlay strings
4541 that have already been consumed by IT. Copy some of the
4542 remaining overlay strings to IT->overlay_strings. */
4543 i = 0;
4544 j = it->current.overlay_string_index;
4545 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
4546 it->overlay_strings[i++] = entries[j++].string;
4547
4548 CHECK_IT (it);
4549 }
4550
4551
4552 /* Get the first chunk of overlay strings at IT's current buffer
4553 position, or at CHARPOS if that is > 0. Value is non-zero if at
4554 least one overlay string was found. */
4555
4556 static int
4557 get_overlay_strings (it, charpos)
4558 struct it *it;
4559 int charpos;
4560 {
4561 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
4562 process. This fills IT->overlay_strings with strings, and sets
4563 IT->n_overlay_strings to the total number of strings to process.
4564 IT->pos.overlay_string_index has to be set temporarily to zero
4565 because load_overlay_strings needs this; it must be set to -1
4566 when no overlay strings are found because a zero value would
4567 indicate a position in the first overlay string. */
4568 it->current.overlay_string_index = 0;
4569 load_overlay_strings (it, charpos);
4570
4571 /* If we found overlay strings, set up IT to deliver display
4572 elements from the first one. Otherwise set up IT to deliver
4573 from current_buffer. */
4574 if (it->n_overlay_strings)
4575 {
4576 /* Make sure we know settings in current_buffer, so that we can
4577 restore meaningful values when we're done with the overlay
4578 strings. */
4579 compute_stop_pos (it);
4580 xassert (it->face_id >= 0);
4581
4582 /* Save IT's settings. They are restored after all overlay
4583 strings have been processed. */
4584 xassert (it->sp == 0);
4585 push_it (it);
4586
4587 /* Set up IT to deliver display elements from the first overlay
4588 string. */
4589 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
4590 it->string = it->overlay_strings[0];
4591 it->stop_charpos = 0;
4592 xassert (STRINGP (it->string));
4593 it->end_charpos = SCHARS (it->string);
4594 it->multibyte_p = STRING_MULTIBYTE (it->string);
4595 it->method = GET_FROM_STRING;
4596 }
4597 else
4598 {
4599 it->string = Qnil;
4600 it->current.overlay_string_index = -1;
4601 it->method = GET_FROM_BUFFER;
4602 }
4603
4604 CHECK_IT (it);
4605
4606 /* Value is non-zero if we found at least one overlay string. */
4607 return STRINGP (it->string);
4608 }
4609
4610
4611 \f
4612 /***********************************************************************
4613 Saving and restoring state
4614 ***********************************************************************/
4615
4616 /* Save current settings of IT on IT->stack. Called, for example,
4617 before setting up IT for an overlay string, to be able to restore
4618 IT's settings to what they were after the overlay string has been
4619 processed. */
4620
4621 static void
4622 push_it (it)
4623 struct it *it;
4624 {
4625 struct iterator_stack_entry *p;
4626
4627 xassert (it->sp < 2);
4628 p = it->stack + it->sp;
4629
4630 p->stop_charpos = it->stop_charpos;
4631 xassert (it->face_id >= 0);
4632 p->face_id = it->face_id;
4633 p->string = it->string;
4634 p->pos = it->current;
4635 p->end_charpos = it->end_charpos;
4636 p->string_nchars = it->string_nchars;
4637 p->area = it->area;
4638 p->multibyte_p = it->multibyte_p;
4639 p->slice = it->slice;
4640 p->space_width = it->space_width;
4641 p->font_height = it->font_height;
4642 p->voffset = it->voffset;
4643 p->string_from_display_prop_p = it->string_from_display_prop_p;
4644 p->display_ellipsis_p = 0;
4645 ++it->sp;
4646 }
4647
4648
4649 /* Restore IT's settings from IT->stack. Called, for example, when no
4650 more overlay strings must be processed, and we return to delivering
4651 display elements from a buffer, or when the end of a string from a
4652 `display' property is reached and we return to delivering display
4653 elements from an overlay string, or from a buffer. */
4654
4655 static void
4656 pop_it (it)
4657 struct it *it;
4658 {
4659 struct iterator_stack_entry *p;
4660
4661 xassert (it->sp > 0);
4662 --it->sp;
4663 p = it->stack + it->sp;
4664 it->stop_charpos = p->stop_charpos;
4665 it->face_id = p->face_id;
4666 it->string = p->string;
4667 it->current = p->pos;
4668 it->end_charpos = p->end_charpos;
4669 it->string_nchars = p->string_nchars;
4670 it->area = p->area;
4671 it->multibyte_p = p->multibyte_p;
4672 it->slice = p->slice;
4673 it->space_width = p->space_width;
4674 it->font_height = p->font_height;
4675 it->voffset = p->voffset;
4676 it->string_from_display_prop_p = p->string_from_display_prop_p;
4677 }
4678
4679
4680 \f
4681 /***********************************************************************
4682 Moving over lines
4683 ***********************************************************************/
4684
4685 /* Set IT's current position to the previous line start. */
4686
4687 static void
4688 back_to_previous_line_start (it)
4689 struct it *it;
4690 {
4691 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
4692 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
4693 }
4694
4695
4696 /* Move IT to the next line start.
4697
4698 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
4699 we skipped over part of the text (as opposed to moving the iterator
4700 continuously over the text). Otherwise, don't change the value
4701 of *SKIPPED_P.
4702
4703 Newlines may come from buffer text, overlay strings, or strings
4704 displayed via the `display' property. That's the reason we can't
4705 simply use find_next_newline_no_quit.
4706
4707 Note that this function may not skip over invisible text that is so
4708 because of text properties and immediately follows a newline. If
4709 it would, function reseat_at_next_visible_line_start, when called
4710 from set_iterator_to_next, would effectively make invisible
4711 characters following a newline part of the wrong glyph row, which
4712 leads to wrong cursor motion. */
4713
4714 static int
4715 forward_to_next_line_start (it, skipped_p)
4716 struct it *it;
4717 int *skipped_p;
4718 {
4719 int old_selective, newline_found_p, n;
4720 const int MAX_NEWLINE_DISTANCE = 500;
4721
4722 /* If already on a newline, just consume it to avoid unintended
4723 skipping over invisible text below. */
4724 if (it->what == IT_CHARACTER
4725 && it->c == '\n'
4726 && CHARPOS (it->position) == IT_CHARPOS (*it))
4727 {
4728 set_iterator_to_next (it, 0);
4729 it->c = 0;
4730 return 1;
4731 }
4732
4733 /* Don't handle selective display in the following. It's (a)
4734 unnecessary because it's done by the caller, and (b) leads to an
4735 infinite recursion because next_element_from_ellipsis indirectly
4736 calls this function. */
4737 old_selective = it->selective;
4738 it->selective = 0;
4739
4740 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
4741 from buffer text. */
4742 for (n = newline_found_p = 0;
4743 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
4744 n += STRINGP (it->string) ? 0 : 1)
4745 {
4746 if (!get_next_display_element (it))
4747 return 0;
4748 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
4749 set_iterator_to_next (it, 0);
4750 }
4751
4752 /* If we didn't find a newline near enough, see if we can use a
4753 short-cut. */
4754 if (!newline_found_p)
4755 {
4756 int start = IT_CHARPOS (*it);
4757 int limit = find_next_newline_no_quit (start, 1);
4758 Lisp_Object pos;
4759
4760 xassert (!STRINGP (it->string));
4761
4762 /* If there isn't any `display' property in sight, and no
4763 overlays, we can just use the position of the newline in
4764 buffer text. */
4765 if (it->stop_charpos >= limit
4766 || ((pos = Fnext_single_property_change (make_number (start),
4767 Qdisplay,
4768 Qnil, make_number (limit)),
4769 NILP (pos))
4770 && next_overlay_change (start) == ZV))
4771 {
4772 IT_CHARPOS (*it) = limit;
4773 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
4774 *skipped_p = newline_found_p = 1;
4775 }
4776 else
4777 {
4778 while (get_next_display_element (it)
4779 && !newline_found_p)
4780 {
4781 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
4782 set_iterator_to_next (it, 0);
4783 }
4784 }
4785 }
4786
4787 it->selective = old_selective;
4788 return newline_found_p;
4789 }
4790
4791
4792 /* Set IT's current position to the previous visible line start. Skip
4793 invisible text that is so either due to text properties or due to
4794 selective display. Caution: this does not change IT->current_x and
4795 IT->hpos. */
4796
4797 static void
4798 back_to_previous_visible_line_start (it)
4799 struct it *it;
4800 {
4801 while (IT_CHARPOS (*it) > BEGV)
4802 {
4803 back_to_previous_line_start (it);
4804 if (IT_CHARPOS (*it) <= BEGV)
4805 break;
4806
4807 /* If selective > 0, then lines indented more than that values
4808 are invisible. */
4809 if (it->selective > 0
4810 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
4811 (double) it->selective)) /* iftc */
4812 continue;
4813
4814 /* Check the newline before point for invisibility. */
4815 {
4816 Lisp_Object prop;
4817 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
4818 Qinvisible, it->window);
4819 if (TEXT_PROP_MEANS_INVISIBLE (prop))
4820 continue;
4821 }
4822
4823 /* If newline has a display property that replaces the newline with something
4824 else (image or text), find start of overlay or interval and continue search
4825 from that point. */
4826 if (IT_CHARPOS (*it) > BEGV)
4827 {
4828 struct it it2 = *it;
4829 int pos;
4830 int beg, end;
4831 Lisp_Object val, overlay;
4832
4833 pos = --IT_CHARPOS (it2);
4834 --IT_BYTEPOS (it2);
4835 it2.sp = 0;
4836 if (handle_display_prop (&it2) == HANDLED_RETURN
4837 && !NILP (val = get_char_property_and_overlay
4838 (make_number (pos), Qdisplay, Qnil, &overlay))
4839 && (OVERLAYP (overlay)
4840 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
4841 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
4842 {
4843 if (beg < BEGV)
4844 beg = BEGV;
4845 IT_CHARPOS (*it) = beg;
4846 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
4847 continue;
4848 }
4849 }
4850
4851 break;
4852 }
4853
4854 xassert (IT_CHARPOS (*it) >= BEGV);
4855 xassert (IT_CHARPOS (*it) == BEGV
4856 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
4857 CHECK_IT (it);
4858 }
4859
4860
4861 /* Reseat iterator IT at the previous visible line start. Skip
4862 invisible text that is so either due to text properties or due to
4863 selective display. At the end, update IT's overlay information,
4864 face information etc. */
4865
4866 void
4867 reseat_at_previous_visible_line_start (it)
4868 struct it *it;
4869 {
4870 back_to_previous_visible_line_start (it);
4871 reseat (it, it->current.pos, 1);
4872 CHECK_IT (it);
4873 }
4874
4875
4876 /* Reseat iterator IT on the next visible line start in the current
4877 buffer. ON_NEWLINE_P non-zero means position IT on the newline
4878 preceding the line start. Skip over invisible text that is so
4879 because of selective display. Compute faces, overlays etc at the
4880 new position. Note that this function does not skip over text that
4881 is invisible because of text properties. */
4882
4883 static void
4884 reseat_at_next_visible_line_start (it, on_newline_p)
4885 struct it *it;
4886 int on_newline_p;
4887 {
4888 int newline_found_p, skipped_p = 0;
4889
4890 newline_found_p = forward_to_next_line_start (it, &skipped_p);
4891
4892 /* Skip over lines that are invisible because they are indented
4893 more than the value of IT->selective. */
4894 if (it->selective > 0)
4895 while (IT_CHARPOS (*it) < ZV
4896 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
4897 (double) it->selective)) /* iftc */
4898 {
4899 xassert (FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
4900 newline_found_p = forward_to_next_line_start (it, &skipped_p);
4901 }
4902
4903 /* Position on the newline if that's what's requested. */
4904 if (on_newline_p && newline_found_p)
4905 {
4906 if (STRINGP (it->string))
4907 {
4908 if (IT_STRING_CHARPOS (*it) > 0)
4909 {
4910 --IT_STRING_CHARPOS (*it);
4911 --IT_STRING_BYTEPOS (*it);
4912 }
4913 }
4914 else if (IT_CHARPOS (*it) > BEGV)
4915 {
4916 --IT_CHARPOS (*it);
4917 --IT_BYTEPOS (*it);
4918 reseat (it, it->current.pos, 0);
4919 }
4920 }
4921 else if (skipped_p)
4922 reseat (it, it->current.pos, 0);
4923
4924 CHECK_IT (it);
4925 }
4926
4927
4928 \f
4929 /***********************************************************************
4930 Changing an iterator's position
4931 ***********************************************************************/
4932
4933 /* Change IT's current position to POS in current_buffer. If FORCE_P
4934 is non-zero, always check for text properties at the new position.
4935 Otherwise, text properties are only looked up if POS >=
4936 IT->check_charpos of a property. */
4937
4938 static void
4939 reseat (it, pos, force_p)
4940 struct it *it;
4941 struct text_pos pos;
4942 int force_p;
4943 {
4944 int original_pos = IT_CHARPOS (*it);
4945
4946 reseat_1 (it, pos, 0);
4947
4948 /* Determine where to check text properties. Avoid doing it
4949 where possible because text property lookup is very expensive. */
4950 if (force_p
4951 || CHARPOS (pos) > it->stop_charpos
4952 || CHARPOS (pos) < original_pos)
4953 handle_stop (it);
4954
4955 CHECK_IT (it);
4956 }
4957
4958
4959 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
4960 IT->stop_pos to POS, also. */
4961
4962 static void
4963 reseat_1 (it, pos, set_stop_p)
4964 struct it *it;
4965 struct text_pos pos;
4966 int set_stop_p;
4967 {
4968 /* Don't call this function when scanning a C string. */
4969 xassert (it->s == NULL);
4970
4971 /* POS must be a reasonable value. */
4972 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
4973
4974 it->current.pos = it->position = pos;
4975 XSETBUFFER (it->object, current_buffer);
4976 it->end_charpos = ZV;
4977 it->dpvec = NULL;
4978 it->current.dpvec_index = -1;
4979 it->current.overlay_string_index = -1;
4980 IT_STRING_CHARPOS (*it) = -1;
4981 IT_STRING_BYTEPOS (*it) = -1;
4982 it->string = Qnil;
4983 it->method = GET_FROM_BUFFER;
4984 /* RMS: I added this to fix a bug in move_it_vertically_backward
4985 where it->area continued to relate to the starting point
4986 for the backward motion. Bug report from
4987 Nick Roberts <nick@nick.uklinux.net> on 19 May 2003.
4988 However, I am not sure whether reseat still does the right thing
4989 in general after this change. */
4990 it->area = TEXT_AREA;
4991 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
4992 it->sp = 0;
4993 it->face_before_selective_p = 0;
4994
4995 if (set_stop_p)
4996 it->stop_charpos = CHARPOS (pos);
4997 }
4998
4999
5000 /* Set up IT for displaying a string, starting at CHARPOS in window W.
5001 If S is non-null, it is a C string to iterate over. Otherwise,
5002 STRING gives a Lisp string to iterate over.
5003
5004 If PRECISION > 0, don't return more then PRECISION number of
5005 characters from the string.
5006
5007 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
5008 characters have been returned. FIELD_WIDTH < 0 means an infinite
5009 field width.
5010
5011 MULTIBYTE = 0 means disable processing of multibyte characters,
5012 MULTIBYTE > 0 means enable it,
5013 MULTIBYTE < 0 means use IT->multibyte_p.
5014
5015 IT must be initialized via a prior call to init_iterator before
5016 calling this function. */
5017
5018 static void
5019 reseat_to_string (it, s, string, charpos, precision, field_width, multibyte)
5020 struct it *it;
5021 unsigned char *s;
5022 Lisp_Object string;
5023 int charpos;
5024 int precision, field_width, multibyte;
5025 {
5026 /* No region in strings. */
5027 it->region_beg_charpos = it->region_end_charpos = -1;
5028
5029 /* No text property checks performed by default, but see below. */
5030 it->stop_charpos = -1;
5031
5032 /* Set iterator position and end position. */
5033 bzero (&it->current, sizeof it->current);
5034 it->current.overlay_string_index = -1;
5035 it->current.dpvec_index = -1;
5036 xassert (charpos >= 0);
5037
5038 /* If STRING is specified, use its multibyteness, otherwise use the
5039 setting of MULTIBYTE, if specified. */
5040 if (multibyte >= 0)
5041 it->multibyte_p = multibyte > 0;
5042
5043 if (s == NULL)
5044 {
5045 xassert (STRINGP (string));
5046 it->string = string;
5047 it->s = NULL;
5048 it->end_charpos = it->string_nchars = SCHARS (string);
5049 it->method = GET_FROM_STRING;
5050 it->current.string_pos = string_pos (charpos, string);
5051 }
5052 else
5053 {
5054 it->s = s;
5055 it->string = Qnil;
5056
5057 /* Note that we use IT->current.pos, not it->current.string_pos,
5058 for displaying C strings. */
5059 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
5060 if (it->multibyte_p)
5061 {
5062 it->current.pos = c_string_pos (charpos, s, 1);
5063 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
5064 }
5065 else
5066 {
5067 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
5068 it->end_charpos = it->string_nchars = strlen (s);
5069 }
5070
5071 it->method = GET_FROM_C_STRING;
5072 }
5073
5074 /* PRECISION > 0 means don't return more than PRECISION characters
5075 from the string. */
5076 if (precision > 0 && it->end_charpos - charpos > precision)
5077 it->end_charpos = it->string_nchars = charpos + precision;
5078
5079 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
5080 characters have been returned. FIELD_WIDTH == 0 means don't pad,
5081 FIELD_WIDTH < 0 means infinite field width. This is useful for
5082 padding with `-' at the end of a mode line. */
5083 if (field_width < 0)
5084 field_width = INFINITY;
5085 if (field_width > it->end_charpos - charpos)
5086 it->end_charpos = charpos + field_width;
5087
5088 /* Use the standard display table for displaying strings. */
5089 if (DISP_TABLE_P (Vstandard_display_table))
5090 it->dp = XCHAR_TABLE (Vstandard_display_table);
5091
5092 it->stop_charpos = charpos;
5093 CHECK_IT (it);
5094 }
5095
5096
5097 \f
5098 /***********************************************************************
5099 Iteration
5100 ***********************************************************************/
5101
5102 /* Map enum it_method value to corresponding next_element_from_* function. */
5103
5104 static int (* get_next_element[NUM_IT_METHODS]) P_ ((struct it *it)) =
5105 {
5106 next_element_from_buffer,
5107 next_element_from_display_vector,
5108 next_element_from_composition,
5109 next_element_from_string,
5110 next_element_from_c_string,
5111 next_element_from_image,
5112 next_element_from_stretch
5113 };
5114
5115
5116 /* Load IT's display element fields with information about the next
5117 display element from the current position of IT. Value is zero if
5118 end of buffer (or C string) is reached. */
5119
5120 int
5121 get_next_display_element (it)
5122 struct it *it;
5123 {
5124 /* Non-zero means that we found a display element. Zero means that
5125 we hit the end of what we iterate over. Performance note: the
5126 function pointer `method' used here turns out to be faster than
5127 using a sequence of if-statements. */
5128 int success_p;
5129
5130 get_next:
5131 success_p = (*get_next_element[it->method]) (it);
5132
5133 if (it->what == IT_CHARACTER)
5134 {
5135 /* Map via display table or translate control characters.
5136 IT->c, IT->len etc. have been set to the next character by
5137 the function call above. If we have a display table, and it
5138 contains an entry for IT->c, translate it. Don't do this if
5139 IT->c itself comes from a display table, otherwise we could
5140 end up in an infinite recursion. (An alternative could be to
5141 count the recursion depth of this function and signal an
5142 error when a certain maximum depth is reached.) Is it worth
5143 it? */
5144 if (success_p && it->dpvec == NULL)
5145 {
5146 Lisp_Object dv;
5147
5148 if (it->dp
5149 && (dv = DISP_CHAR_VECTOR (it->dp, it->c),
5150 VECTORP (dv)))
5151 {
5152 struct Lisp_Vector *v = XVECTOR (dv);
5153
5154 /* Return the first character from the display table
5155 entry, if not empty. If empty, don't display the
5156 current character. */
5157 if (v->size)
5158 {
5159 it->dpvec_char_len = it->len;
5160 it->dpvec = v->contents;
5161 it->dpend = v->contents + v->size;
5162 it->current.dpvec_index = 0;
5163 it->dpvec_face_id = -1;
5164 it->saved_face_id = it->face_id;
5165 it->method = GET_FROM_DISPLAY_VECTOR;
5166 it->ellipsis_p = 0;
5167 }
5168 else
5169 {
5170 set_iterator_to_next (it, 0);
5171 }
5172 goto get_next;
5173 }
5174
5175 /* Translate control characters into `\003' or `^C' form.
5176 Control characters coming from a display table entry are
5177 currently not translated because we use IT->dpvec to hold
5178 the translation. This could easily be changed but I
5179 don't believe that it is worth doing.
5180
5181 If it->multibyte_p is nonzero, non-printable non-ASCII
5182 characters are also translated to octal form.
5183
5184 If it->multibyte_p is zero, eight-bit characters that
5185 don't have corresponding multibyte char code are also
5186 translated to octal form. */
5187 else if ((it->c < ' '
5188 ? (it->area != TEXT_AREA
5189 /* In mode line, treat \n, \t like other crl chars. */
5190 || (it->c != '\t'
5191 && it->glyph_row && it->glyph_row->mode_line_p)
5192 || (it->c != '\n' && it->c != '\t'))
5193 : (it->multibyte_p
5194 ? (!CHAR_PRINTABLE_P (it->c)
5195 || (!NILP (Vnobreak_char_display)
5196 && (it->c == 0xA0 /* NO-BREAK SPACE */
5197 || it->c == 0xAD /* SOFT HYPHEN */)))
5198 : (it->c >= 127
5199 && (! unibyte_display_via_language_environment
5200 || (UNIBYTE_CHAR_HAS_MULTIBYTE_P (it->c)))))))
5201 {
5202 /* IT->c is a control character which must be displayed
5203 either as '\003' or as `^C' where the '\\' and '^'
5204 can be defined in the display table. Fill
5205 IT->ctl_chars with glyphs for what we have to
5206 display. Then, set IT->dpvec to these glyphs. */
5207 GLYPH g;
5208 int ctl_len;
5209 int face_id, lface_id = 0 ;
5210 GLYPH escape_glyph;
5211
5212 /* Handle control characters with ^. */
5213
5214 if (it->c < 128 && it->ctl_arrow_p)
5215 {
5216 g = '^'; /* default glyph for Control */
5217 /* Set IT->ctl_chars[0] to the glyph for `^'. */
5218 if (it->dp
5219 && INTEGERP (DISP_CTRL_GLYPH (it->dp))
5220 && GLYPH_CHAR_VALID_P (XINT (DISP_CTRL_GLYPH (it->dp))))
5221 {
5222 g = XINT (DISP_CTRL_GLYPH (it->dp));
5223 lface_id = FAST_GLYPH_FACE (g);
5224 }
5225 if (lface_id)
5226 {
5227 g = FAST_GLYPH_CHAR (g);
5228 face_id = merge_faces (it->f, Qt, lface_id,
5229 it->face_id);
5230 }
5231 else
5232 {
5233 /* Merge the escape-glyph face into the current face. */
5234 face_id = merge_faces (it->f, Qescape_glyph, 0,
5235 it->face_id);
5236 }
5237
5238 XSETINT (it->ctl_chars[0], g);
5239 g = it->c ^ 0100;
5240 XSETINT (it->ctl_chars[1], g);
5241 ctl_len = 2;
5242 goto display_control;
5243 }
5244
5245 /* Handle non-break space in the mode where it only gets
5246 highlighting. */
5247
5248 if (EQ (Vnobreak_char_display, Qt)
5249 && it->c == 0xA0)
5250 {
5251 /* Merge the no-break-space face into the current face. */
5252 face_id = merge_faces (it->f, Qnobreak_space, 0,
5253 it->face_id);
5254
5255 g = it->c = ' ';
5256 XSETINT (it->ctl_chars[0], g);
5257 ctl_len = 1;
5258 goto display_control;
5259 }
5260
5261 /* Handle sequences that start with the "escape glyph". */
5262
5263 /* the default escape glyph is \. */
5264 escape_glyph = '\\';
5265
5266 if (it->dp
5267 && INTEGERP (DISP_ESCAPE_GLYPH (it->dp))
5268 && GLYPH_CHAR_VALID_P (XFASTINT (DISP_ESCAPE_GLYPH (it->dp))))
5269 {
5270 escape_glyph = XFASTINT (DISP_ESCAPE_GLYPH (it->dp));
5271 lface_id = FAST_GLYPH_FACE (escape_glyph);
5272 }
5273 if (lface_id)
5274 {
5275 /* The display table specified a face.
5276 Merge it into face_id and also into escape_glyph. */
5277 escape_glyph = FAST_GLYPH_CHAR (escape_glyph);
5278 face_id = merge_faces (it->f, Qt, lface_id,
5279 it->face_id);
5280 }
5281 else
5282 {
5283 /* Merge the escape-glyph face into the current face. */
5284 face_id = merge_faces (it->f, Qescape_glyph, 0,
5285 it->face_id);
5286 }
5287
5288 /* Handle soft hyphens in the mode where they only get
5289 highlighting. */
5290
5291 if (EQ (Vnobreak_char_display, Qt)
5292 && it->c == 0xAD)
5293 {
5294 g = it->c = '-';
5295 XSETINT (it->ctl_chars[0], g);
5296 ctl_len = 1;
5297 goto display_control;
5298 }
5299
5300 /* Handle non-break space and soft hyphen
5301 with the escape glyph. */
5302
5303 if (it->c == 0xA0 || it->c == 0xAD)
5304 {
5305 XSETINT (it->ctl_chars[0], escape_glyph);
5306 g = it->c = (it->c == 0xA0 ? ' ' : '-');
5307 XSETINT (it->ctl_chars[1], g);
5308 ctl_len = 2;
5309 goto display_control;
5310 }
5311
5312 {
5313 unsigned char str[MAX_MULTIBYTE_LENGTH];
5314 int len;
5315 int i;
5316
5317 /* Set IT->ctl_chars[0] to the glyph for `\\'. */
5318 if (CHAR_BYTE8_P (it->c))
5319 {
5320 str[0] = CHAR_TO_BYTE8 (it->c);
5321 len = 1;
5322 }
5323 else if (it->c < 256)
5324 {
5325 str[0] = it->c;
5326 len = 1;
5327 }
5328 else
5329 {
5330 /* It's an invalid character, which shouldn't
5331 happen actually, but due to bugs it may
5332 happen. Let's print the char as is, there's
5333 not much meaningful we can do with it. */
5334 str[0] = it->c;
5335 str[1] = it->c >> 8;
5336 str[2] = it->c >> 16;
5337 str[3] = it->c >> 24;
5338 len = 4;
5339 }
5340
5341 for (i = 0; i < len; i++)
5342 {
5343 XSETINT (it->ctl_chars[i * 4], escape_glyph);
5344 /* Insert three more glyphs into IT->ctl_chars for
5345 the octal display of the character. */
5346 g = ((str[i] >> 6) & 7) + '0';
5347 XSETINT (it->ctl_chars[i * 4 + 1], g);
5348 g = ((str[i] >> 3) & 7) + '0';
5349 XSETINT (it->ctl_chars[i * 4 + 2], g);
5350 g = (str[i] & 7) + '0';
5351 XSETINT (it->ctl_chars[i * 4 + 3], g);
5352 }
5353 ctl_len = len * 4;
5354 }
5355
5356 display_control:
5357 /* Set up IT->dpvec and return first character from it. */
5358 it->dpvec_char_len = it->len;
5359 it->dpvec = it->ctl_chars;
5360 it->dpend = it->dpvec + ctl_len;
5361 it->current.dpvec_index = 0;
5362 it->dpvec_face_id = face_id;
5363 it->saved_face_id = it->face_id;
5364 it->method = GET_FROM_DISPLAY_VECTOR;
5365 it->ellipsis_p = 0;
5366 goto get_next;
5367 }
5368 }
5369
5370 /* Adjust face id for a multibyte character. There are no
5371 multibyte character in unibyte text. */
5372 if (it->multibyte_p
5373 && success_p
5374 && FRAME_WINDOW_P (it->f))
5375 {
5376 struct face *face = FACE_FROM_ID (it->f, it->face_id);
5377 int pos = (it->s ? -1
5378 : STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
5379 : IT_CHARPOS (*it));
5380
5381 it->face_id = FACE_FOR_CHAR (it->f, face, it->c, pos, it->string);
5382 }
5383 }
5384
5385 /* Is this character the last one of a run of characters with
5386 box? If yes, set IT->end_of_box_run_p to 1. */
5387 if (it->face_box_p
5388 && it->s == NULL)
5389 {
5390 int face_id;
5391 struct face *face;
5392
5393 it->end_of_box_run_p
5394 = ((face_id = face_after_it_pos (it),
5395 face_id != it->face_id)
5396 && (face = FACE_FROM_ID (it->f, face_id),
5397 face->box == FACE_NO_BOX));
5398 }
5399
5400 /* Value is 0 if end of buffer or string reached. */
5401 return success_p;
5402 }
5403
5404
5405 /* Move IT to the next display element.
5406
5407 RESEAT_P non-zero means if called on a newline in buffer text,
5408 skip to the next visible line start.
5409
5410 Functions get_next_display_element and set_iterator_to_next are
5411 separate because I find this arrangement easier to handle than a
5412 get_next_display_element function that also increments IT's
5413 position. The way it is we can first look at an iterator's current
5414 display element, decide whether it fits on a line, and if it does,
5415 increment the iterator position. The other way around we probably
5416 would either need a flag indicating whether the iterator has to be
5417 incremented the next time, or we would have to implement a
5418 decrement position function which would not be easy to write. */
5419
5420 void
5421 set_iterator_to_next (it, reseat_p)
5422 struct it *it;
5423 int reseat_p;
5424 {
5425 /* Reset flags indicating start and end of a sequence of characters
5426 with box. Reset them at the start of this function because
5427 moving the iterator to a new position might set them. */
5428 it->start_of_box_run_p = it->end_of_box_run_p = 0;
5429
5430 switch (it->method)
5431 {
5432 case GET_FROM_BUFFER:
5433 /* The current display element of IT is a character from
5434 current_buffer. Advance in the buffer, and maybe skip over
5435 invisible lines that are so because of selective display. */
5436 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
5437 reseat_at_next_visible_line_start (it, 0);
5438 else
5439 {
5440 xassert (it->len != 0);
5441 IT_BYTEPOS (*it) += it->len;
5442 IT_CHARPOS (*it) += 1;
5443 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
5444 }
5445 break;
5446
5447 case GET_FROM_COMPOSITION:
5448 xassert (it->cmp_id >= 0 && it->cmp_id < n_compositions);
5449 if (STRINGP (it->string))
5450 {
5451 IT_STRING_BYTEPOS (*it) += it->len;
5452 IT_STRING_CHARPOS (*it) += it->cmp_len;
5453 it->method = GET_FROM_STRING;
5454 goto consider_string_end;
5455 }
5456 else
5457 {
5458 IT_BYTEPOS (*it) += it->len;
5459 IT_CHARPOS (*it) += it->cmp_len;
5460 it->method = GET_FROM_BUFFER;
5461 }
5462 break;
5463
5464 case GET_FROM_C_STRING:
5465 /* Current display element of IT is from a C string. */
5466 IT_BYTEPOS (*it) += it->len;
5467 IT_CHARPOS (*it) += 1;
5468 break;
5469
5470 case GET_FROM_DISPLAY_VECTOR:
5471 /* Current display element of IT is from a display table entry.
5472 Advance in the display table definition. Reset it to null if
5473 end reached, and continue with characters from buffers/
5474 strings. */
5475 ++it->current.dpvec_index;
5476
5477 /* Restore face of the iterator to what they were before the
5478 display vector entry (these entries may contain faces). */
5479 it->face_id = it->saved_face_id;
5480
5481 if (it->dpvec + it->current.dpvec_index == it->dpend)
5482 {
5483 if (it->s)
5484 it->method = GET_FROM_C_STRING;
5485 else if (STRINGP (it->string))
5486 it->method = GET_FROM_STRING;
5487 else
5488 it->method = GET_FROM_BUFFER;
5489
5490 it->dpvec = NULL;
5491 it->current.dpvec_index = -1;
5492
5493 /* Skip over characters which were displayed via IT->dpvec. */
5494 if (it->dpvec_char_len < 0)
5495 reseat_at_next_visible_line_start (it, 1);
5496 else if (it->dpvec_char_len > 0)
5497 {
5498 it->len = it->dpvec_char_len;
5499 set_iterator_to_next (it, reseat_p);
5500 }
5501
5502 /* Recheck faces after display vector */
5503 it->stop_charpos = IT_CHARPOS (*it);
5504 }
5505 break;
5506
5507 case GET_FROM_STRING:
5508 /* Current display element is a character from a Lisp string. */
5509 xassert (it->s == NULL && STRINGP (it->string));
5510 IT_STRING_BYTEPOS (*it) += it->len;
5511 IT_STRING_CHARPOS (*it) += 1;
5512
5513 consider_string_end:
5514
5515 if (it->current.overlay_string_index >= 0)
5516 {
5517 /* IT->string is an overlay string. Advance to the
5518 next, if there is one. */
5519 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
5520 next_overlay_string (it);
5521 }
5522 else
5523 {
5524 /* IT->string is not an overlay string. If we reached
5525 its end, and there is something on IT->stack, proceed
5526 with what is on the stack. This can be either another
5527 string, this time an overlay string, or a buffer. */
5528 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
5529 && it->sp > 0)
5530 {
5531 pop_it (it);
5532 if (STRINGP (it->string))
5533 goto consider_string_end;
5534 it->method = GET_FROM_BUFFER;
5535 }
5536 }
5537 break;
5538
5539 case GET_FROM_IMAGE:
5540 case GET_FROM_STRETCH:
5541 /* The position etc with which we have to proceed are on
5542 the stack. The position may be at the end of a string,
5543 if the `display' property takes up the whole string. */
5544 xassert (it->sp > 0);
5545 pop_it (it);
5546 it->image_id = 0;
5547 if (STRINGP (it->string))
5548 {
5549 it->method = GET_FROM_STRING;
5550 goto consider_string_end;
5551 }
5552 it->method = GET_FROM_BUFFER;
5553 break;
5554
5555 default:
5556 /* There are no other methods defined, so this should be a bug. */
5557 abort ();
5558 }
5559
5560 xassert (it->method != GET_FROM_STRING
5561 || (STRINGP (it->string)
5562 && IT_STRING_CHARPOS (*it) >= 0));
5563 }
5564
5565 /* Load IT's display element fields with information about the next
5566 display element which comes from a display table entry or from the
5567 result of translating a control character to one of the forms `^C'
5568 or `\003'.
5569
5570 IT->dpvec holds the glyphs to return as characters.
5571 IT->saved_face_id holds the face id before the display vector--
5572 it is restored into IT->face_idin set_iterator_to_next. */
5573
5574 static int
5575 next_element_from_display_vector (it)
5576 struct it *it;
5577 {
5578 /* Precondition. */
5579 xassert (it->dpvec && it->current.dpvec_index >= 0);
5580
5581 it->face_id = it->saved_face_id;
5582
5583 if (INTEGERP (*it->dpvec)
5584 && GLYPH_CHAR_VALID_P (XFASTINT (*it->dpvec)))
5585 {
5586 GLYPH g;
5587
5588 g = XFASTINT (it->dpvec[it->current.dpvec_index]);
5589 it->c = FAST_GLYPH_CHAR (g);
5590 it->len = CHAR_BYTES (it->c);
5591
5592 /* The entry may contain a face id to use. Such a face id is
5593 the id of a Lisp face, not a realized face. A face id of
5594 zero means no face is specified. */
5595 if (it->dpvec_face_id >= 0)
5596 it->face_id = it->dpvec_face_id;
5597 else
5598 {
5599 int lface_id = FAST_GLYPH_FACE (g);
5600 if (lface_id > 0)
5601 it->face_id = merge_faces (it->f, Qt, lface_id,
5602 it->saved_face_id);
5603 }
5604 }
5605 else
5606 /* Display table entry is invalid. Return a space. */
5607 it->c = ' ', it->len = 1;
5608
5609 /* Don't change position and object of the iterator here. They are
5610 still the values of the character that had this display table
5611 entry or was translated, and that's what we want. */
5612 it->what = IT_CHARACTER;
5613 return 1;
5614 }
5615
5616
5617 /* Load IT with the next display element from Lisp string IT->string.
5618 IT->current.string_pos is the current position within the string.
5619 If IT->current.overlay_string_index >= 0, the Lisp string is an
5620 overlay string. */
5621
5622 static int
5623 next_element_from_string (it)
5624 struct it *it;
5625 {
5626 struct text_pos position;
5627
5628 xassert (STRINGP (it->string));
5629 xassert (IT_STRING_CHARPOS (*it) >= 0);
5630 position = it->current.string_pos;
5631
5632 /* Time to check for invisible text? */
5633 if (IT_STRING_CHARPOS (*it) < it->end_charpos
5634 && IT_STRING_CHARPOS (*it) == it->stop_charpos)
5635 {
5636 handle_stop (it);
5637
5638 /* Since a handler may have changed IT->method, we must
5639 recurse here. */
5640 return get_next_display_element (it);
5641 }
5642
5643 if (it->current.overlay_string_index >= 0)
5644 {
5645 /* Get the next character from an overlay string. In overlay
5646 strings, There is no field width or padding with spaces to
5647 do. */
5648 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
5649 {
5650 it->what = IT_EOB;
5651 return 0;
5652 }
5653 else if (STRING_MULTIBYTE (it->string))
5654 {
5655 int remaining = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
5656 const unsigned char *s = (SDATA (it->string)
5657 + IT_STRING_BYTEPOS (*it));
5658 it->c = string_char_and_length (s, remaining, &it->len);
5659 }
5660 else
5661 {
5662 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
5663 it->len = 1;
5664 }
5665 }
5666 else
5667 {
5668 /* Get the next character from a Lisp string that is not an
5669 overlay string. Such strings come from the mode line, for
5670 example. We may have to pad with spaces, or truncate the
5671 string. See also next_element_from_c_string. */
5672 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
5673 {
5674 it->what = IT_EOB;
5675 return 0;
5676 }
5677 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
5678 {
5679 /* Pad with spaces. */
5680 it->c = ' ', it->len = 1;
5681 CHARPOS (position) = BYTEPOS (position) = -1;
5682 }
5683 else if (STRING_MULTIBYTE (it->string))
5684 {
5685 int maxlen = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
5686 const unsigned char *s = (SDATA (it->string)
5687 + IT_STRING_BYTEPOS (*it));
5688 it->c = string_char_and_length (s, maxlen, &it->len);
5689 }
5690 else
5691 {
5692 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
5693 it->len = 1;
5694 }
5695 }
5696
5697 /* Record what we have and where it came from. Note that we store a
5698 buffer position in IT->position although it could arguably be a
5699 string position. */
5700 it->what = IT_CHARACTER;
5701 it->object = it->string;
5702 it->position = position;
5703 return 1;
5704 }
5705
5706
5707 /* Load IT with next display element from C string IT->s.
5708 IT->string_nchars is the maximum number of characters to return
5709 from the string. IT->end_charpos may be greater than
5710 IT->string_nchars when this function is called, in which case we
5711 may have to return padding spaces. Value is zero if end of string
5712 reached, including padding spaces. */
5713
5714 static int
5715 next_element_from_c_string (it)
5716 struct it *it;
5717 {
5718 int success_p = 1;
5719
5720 xassert (it->s);
5721 it->what = IT_CHARACTER;
5722 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
5723 it->object = Qnil;
5724
5725 /* IT's position can be greater IT->string_nchars in case a field
5726 width or precision has been specified when the iterator was
5727 initialized. */
5728 if (IT_CHARPOS (*it) >= it->end_charpos)
5729 {
5730 /* End of the game. */
5731 it->what = IT_EOB;
5732 success_p = 0;
5733 }
5734 else if (IT_CHARPOS (*it) >= it->string_nchars)
5735 {
5736 /* Pad with spaces. */
5737 it->c = ' ', it->len = 1;
5738 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
5739 }
5740 else if (it->multibyte_p)
5741 {
5742 /* Implementation note: The calls to strlen apparently aren't a
5743 performance problem because there is no noticeable performance
5744 difference between Emacs running in unibyte or multibyte mode. */
5745 int maxlen = strlen (it->s) - IT_BYTEPOS (*it);
5746 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it),
5747 maxlen, &it->len);
5748 }
5749 else
5750 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
5751
5752 return success_p;
5753 }
5754
5755
5756 /* Set up IT to return characters from an ellipsis, if appropriate.
5757 The definition of the ellipsis glyphs may come from a display table
5758 entry. This function Fills IT with the first glyph from the
5759 ellipsis if an ellipsis is to be displayed. */
5760
5761 static int
5762 next_element_from_ellipsis (it)
5763 struct it *it;
5764 {
5765 if (it->selective_display_ellipsis_p)
5766 setup_for_ellipsis (it, it->len);
5767 else
5768 {
5769 /* The face at the current position may be different from the
5770 face we find after the invisible text. Remember what it
5771 was in IT->saved_face_id, and signal that it's there by
5772 setting face_before_selective_p. */
5773 it->saved_face_id = it->face_id;
5774 it->method = GET_FROM_BUFFER;
5775 reseat_at_next_visible_line_start (it, 1);
5776 it->face_before_selective_p = 1;
5777 }
5778
5779 return get_next_display_element (it);
5780 }
5781
5782
5783 /* Deliver an image display element. The iterator IT is already
5784 filled with image information (done in handle_display_prop). Value
5785 is always 1. */
5786
5787
5788 static int
5789 next_element_from_image (it)
5790 struct it *it;
5791 {
5792 it->what = IT_IMAGE;
5793 return 1;
5794 }
5795
5796
5797 /* Fill iterator IT with next display element from a stretch glyph
5798 property. IT->object is the value of the text property. Value is
5799 always 1. */
5800
5801 static int
5802 next_element_from_stretch (it)
5803 struct it *it;
5804 {
5805 it->what = IT_STRETCH;
5806 return 1;
5807 }
5808
5809
5810 /* Load IT with the next display element from current_buffer. Value
5811 is zero if end of buffer reached. IT->stop_charpos is the next
5812 position at which to stop and check for text properties or buffer
5813 end. */
5814
5815 static int
5816 next_element_from_buffer (it)
5817 struct it *it;
5818 {
5819 int success_p = 1;
5820
5821 /* Check this assumption, otherwise, we would never enter the
5822 if-statement, below. */
5823 xassert (IT_CHARPOS (*it) >= BEGV
5824 && IT_CHARPOS (*it) <= it->stop_charpos);
5825
5826 if (IT_CHARPOS (*it) >= it->stop_charpos)
5827 {
5828 if (IT_CHARPOS (*it) >= it->end_charpos)
5829 {
5830 int overlay_strings_follow_p;
5831
5832 /* End of the game, except when overlay strings follow that
5833 haven't been returned yet. */
5834 if (it->overlay_strings_at_end_processed_p)
5835 overlay_strings_follow_p = 0;
5836 else
5837 {
5838 it->overlay_strings_at_end_processed_p = 1;
5839 overlay_strings_follow_p = get_overlay_strings (it, 0);
5840 }
5841
5842 if (overlay_strings_follow_p)
5843 success_p = get_next_display_element (it);
5844 else
5845 {
5846 it->what = IT_EOB;
5847 it->position = it->current.pos;
5848 success_p = 0;
5849 }
5850 }
5851 else
5852 {
5853 handle_stop (it);
5854 return get_next_display_element (it);
5855 }
5856 }
5857 else
5858 {
5859 /* No face changes, overlays etc. in sight, so just return a
5860 character from current_buffer. */
5861 unsigned char *p;
5862
5863 /* Maybe run the redisplay end trigger hook. Performance note:
5864 This doesn't seem to cost measurable time. */
5865 if (it->redisplay_end_trigger_charpos
5866 && it->glyph_row
5867 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
5868 run_redisplay_end_trigger_hook (it);
5869
5870 /* Get the next character, maybe multibyte. */
5871 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
5872 if (it->multibyte_p && !ASCII_BYTE_P (*p))
5873 {
5874 int maxlen = ((IT_BYTEPOS (*it) >= GPT_BYTE ? ZV_BYTE : GPT_BYTE)
5875 - IT_BYTEPOS (*it));
5876 it->c = string_char_and_length (p, maxlen, &it->len);
5877 }
5878 else
5879 it->c = *p, it->len = 1;
5880
5881 /* Record what we have and where it came from. */
5882 it->what = IT_CHARACTER;;
5883 it->object = it->w->buffer;
5884 it->position = it->current.pos;
5885
5886 /* Normally we return the character found above, except when we
5887 really want to return an ellipsis for selective display. */
5888 if (it->selective)
5889 {
5890 if (it->c == '\n')
5891 {
5892 /* A value of selective > 0 means hide lines indented more
5893 than that number of columns. */
5894 if (it->selective > 0
5895 && IT_CHARPOS (*it) + 1 < ZV
5896 && indented_beyond_p (IT_CHARPOS (*it) + 1,
5897 IT_BYTEPOS (*it) + 1,
5898 (double) it->selective)) /* iftc */
5899 {
5900 success_p = next_element_from_ellipsis (it);
5901 it->dpvec_char_len = -1;
5902 }
5903 }
5904 else if (it->c == '\r' && it->selective == -1)
5905 {
5906 /* A value of selective == -1 means that everything from the
5907 CR to the end of the line is invisible, with maybe an
5908 ellipsis displayed for it. */
5909 success_p = next_element_from_ellipsis (it);
5910 it->dpvec_char_len = -1;
5911 }
5912 }
5913 }
5914
5915 /* Value is zero if end of buffer reached. */
5916 xassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
5917 return success_p;
5918 }
5919
5920
5921 /* Run the redisplay end trigger hook for IT. */
5922
5923 static void
5924 run_redisplay_end_trigger_hook (it)
5925 struct it *it;
5926 {
5927 Lisp_Object args[3];
5928
5929 /* IT->glyph_row should be non-null, i.e. we should be actually
5930 displaying something, or otherwise we should not run the hook. */
5931 xassert (it->glyph_row);
5932
5933 /* Set up hook arguments. */
5934 args[0] = Qredisplay_end_trigger_functions;
5935 args[1] = it->window;
5936 XSETINT (args[2], it->redisplay_end_trigger_charpos);
5937 it->redisplay_end_trigger_charpos = 0;
5938
5939 /* Since we are *trying* to run these functions, don't try to run
5940 them again, even if they get an error. */
5941 it->w->redisplay_end_trigger = Qnil;
5942 Frun_hook_with_args (3, args);
5943
5944 /* Notice if it changed the face of the character we are on. */
5945 handle_face_prop (it);
5946 }
5947
5948
5949 /* Deliver a composition display element. The iterator IT is already
5950 filled with composition information (done in
5951 handle_composition_prop). Value is always 1. */
5952
5953 static int
5954 next_element_from_composition (it)
5955 struct it *it;
5956 {
5957 it->what = IT_COMPOSITION;
5958 it->position = (STRINGP (it->string)
5959 ? it->current.string_pos
5960 : it->current.pos);
5961 return 1;
5962 }
5963
5964
5965 \f
5966 /***********************************************************************
5967 Moving an iterator without producing glyphs
5968 ***********************************************************************/
5969
5970 /* Check if iterator is at a position corresponding to a valid buffer
5971 position after some move_it_ call. */
5972
5973 #define IT_POS_VALID_AFTER_MOVE_P(it) \
5974 ((it)->method == GET_FROM_STRING \
5975 ? IT_STRING_CHARPOS (*it) == 0 \
5976 : 1)
5977
5978
5979 /* Move iterator IT to a specified buffer or X position within one
5980 line on the display without producing glyphs.
5981
5982 OP should be a bit mask including some or all of these bits:
5983 MOVE_TO_X: Stop on reaching x-position TO_X.
5984 MOVE_TO_POS: Stop on reaching buffer or string position TO_CHARPOS.
5985 Regardless of OP's value, stop in reaching the end of the display line.
5986
5987 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
5988 This means, in particular, that TO_X includes window's horizontal
5989 scroll amount.
5990
5991 The return value has several possible values that
5992 say what condition caused the scan to stop:
5993
5994 MOVE_POS_MATCH_OR_ZV
5995 - when TO_POS or ZV was reached.
5996
5997 MOVE_X_REACHED
5998 -when TO_X was reached before TO_POS or ZV were reached.
5999
6000 MOVE_LINE_CONTINUED
6001 - when we reached the end of the display area and the line must
6002 be continued.
6003
6004 MOVE_LINE_TRUNCATED
6005 - when we reached the end of the display area and the line is
6006 truncated.
6007
6008 MOVE_NEWLINE_OR_CR
6009 - when we stopped at a line end, i.e. a newline or a CR and selective
6010 display is on. */
6011
6012 static enum move_it_result
6013 move_it_in_display_line_to (it, to_charpos, to_x, op)
6014 struct it *it;
6015 int to_charpos, to_x, op;
6016 {
6017 enum move_it_result result = MOVE_UNDEFINED;
6018 struct glyph_row *saved_glyph_row;
6019
6020 /* Don't produce glyphs in produce_glyphs. */
6021 saved_glyph_row = it->glyph_row;
6022 it->glyph_row = NULL;
6023
6024 #define BUFFER_POS_REACHED_P() \
6025 ((op & MOVE_TO_POS) != 0 \
6026 && BUFFERP (it->object) \
6027 && IT_CHARPOS (*it) >= to_charpos \
6028 && (it->method == GET_FROM_BUFFER \
6029 || (it->method == GET_FROM_DISPLAY_VECTOR \
6030 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
6031
6032
6033 while (1)
6034 {
6035 int x, i, ascent = 0, descent = 0;
6036
6037 /* Stop if we move beyond TO_CHARPOS (after an image or stretch glyph). */
6038 if ((op & MOVE_TO_POS) != 0
6039 && BUFFERP (it->object)
6040 && it->method == GET_FROM_BUFFER
6041 && IT_CHARPOS (*it) > to_charpos)
6042 {
6043 result = MOVE_POS_MATCH_OR_ZV;
6044 break;
6045 }
6046
6047 /* Stop when ZV reached.
6048 We used to stop here when TO_CHARPOS reached as well, but that is
6049 too soon if this glyph does not fit on this line. So we handle it
6050 explicitly below. */
6051 if (!get_next_display_element (it)
6052 || (it->truncate_lines_p
6053 && BUFFER_POS_REACHED_P ()))
6054 {
6055 result = MOVE_POS_MATCH_OR_ZV;
6056 break;
6057 }
6058
6059 /* The call to produce_glyphs will get the metrics of the
6060 display element IT is loaded with. We record in x the
6061 x-position before this display element in case it does not
6062 fit on the line. */
6063 x = it->current_x;
6064
6065 /* Remember the line height so far in case the next element doesn't
6066 fit on the line. */
6067 if (!it->truncate_lines_p)
6068 {
6069 ascent = it->max_ascent;
6070 descent = it->max_descent;
6071 }
6072
6073 PRODUCE_GLYPHS (it);
6074
6075 if (it->area != TEXT_AREA)
6076 {
6077 set_iterator_to_next (it, 1);
6078 continue;
6079 }
6080
6081 /* The number of glyphs we get back in IT->nglyphs will normally
6082 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
6083 character on a terminal frame, or (iii) a line end. For the
6084 second case, IT->nglyphs - 1 padding glyphs will be present
6085 (on X frames, there is only one glyph produced for a
6086 composite character.
6087
6088 The behavior implemented below means, for continuation lines,
6089 that as many spaces of a TAB as fit on the current line are
6090 displayed there. For terminal frames, as many glyphs of a
6091 multi-glyph character are displayed in the current line, too.
6092 This is what the old redisplay code did, and we keep it that
6093 way. Under X, the whole shape of a complex character must
6094 fit on the line or it will be completely displayed in the
6095 next line.
6096
6097 Note that both for tabs and padding glyphs, all glyphs have
6098 the same width. */
6099 if (it->nglyphs)
6100 {
6101 /* More than one glyph or glyph doesn't fit on line. All
6102 glyphs have the same width. */
6103 int single_glyph_width = it->pixel_width / it->nglyphs;
6104 int new_x;
6105
6106 for (i = 0; i < it->nglyphs; ++i, x = new_x)
6107 {
6108 new_x = x + single_glyph_width;
6109
6110 /* We want to leave anything reaching TO_X to the caller. */
6111 if ((op & MOVE_TO_X) && new_x > to_x)
6112 {
6113 if (BUFFER_POS_REACHED_P ())
6114 goto buffer_pos_reached;
6115 it->current_x = x;
6116 result = MOVE_X_REACHED;
6117 break;
6118 }
6119 else if (/* Lines are continued. */
6120 !it->truncate_lines_p
6121 && (/* And glyph doesn't fit on the line. */
6122 new_x > it->last_visible_x
6123 /* Or it fits exactly and we're on a window
6124 system frame. */
6125 || (new_x == it->last_visible_x
6126 && FRAME_WINDOW_P (it->f))))
6127 {
6128 if (/* IT->hpos == 0 means the very first glyph
6129 doesn't fit on the line, e.g. a wide image. */
6130 it->hpos == 0
6131 || (new_x == it->last_visible_x
6132 && FRAME_WINDOW_P (it->f)))
6133 {
6134 ++it->hpos;
6135 it->current_x = new_x;
6136 if (i == it->nglyphs - 1)
6137 {
6138 set_iterator_to_next (it, 1);
6139 #ifdef HAVE_WINDOW_SYSTEM
6140 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
6141 {
6142 if (!get_next_display_element (it))
6143 {
6144 result = MOVE_POS_MATCH_OR_ZV;
6145 break;
6146 }
6147 if (BUFFER_POS_REACHED_P ())
6148 {
6149 if (ITERATOR_AT_END_OF_LINE_P (it))
6150 result = MOVE_POS_MATCH_OR_ZV;
6151 else
6152 result = MOVE_LINE_CONTINUED;
6153 break;
6154 }
6155 if (ITERATOR_AT_END_OF_LINE_P (it))
6156 {
6157 result = MOVE_NEWLINE_OR_CR;
6158 break;
6159 }
6160 }
6161 #endif /* HAVE_WINDOW_SYSTEM */
6162 }
6163 }
6164 else
6165 {
6166 it->current_x = x;
6167 it->max_ascent = ascent;
6168 it->max_descent = descent;
6169 }
6170
6171 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
6172 IT_CHARPOS (*it)));
6173 result = MOVE_LINE_CONTINUED;
6174 break;
6175 }
6176 else if (BUFFER_POS_REACHED_P ())
6177 goto buffer_pos_reached;
6178 else if (new_x > it->first_visible_x)
6179 {
6180 /* Glyph is visible. Increment number of glyphs that
6181 would be displayed. */
6182 ++it->hpos;
6183 }
6184 else
6185 {
6186 /* Glyph is completely off the left margin of the display
6187 area. Nothing to do. */
6188 }
6189 }
6190
6191 if (result != MOVE_UNDEFINED)
6192 break;
6193 }
6194 else if (BUFFER_POS_REACHED_P ())
6195 {
6196 buffer_pos_reached:
6197 it->current_x = x;
6198 it->max_ascent = ascent;
6199 it->max_descent = descent;
6200 result = MOVE_POS_MATCH_OR_ZV;
6201 break;
6202 }
6203 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
6204 {
6205 /* Stop when TO_X specified and reached. This check is
6206 necessary here because of lines consisting of a line end,
6207 only. The line end will not produce any glyphs and we
6208 would never get MOVE_X_REACHED. */
6209 xassert (it->nglyphs == 0);
6210 result = MOVE_X_REACHED;
6211 break;
6212 }
6213
6214 /* Is this a line end? If yes, we're done. */
6215 if (ITERATOR_AT_END_OF_LINE_P (it))
6216 {
6217 result = MOVE_NEWLINE_OR_CR;
6218 break;
6219 }
6220
6221 /* The current display element has been consumed. Advance
6222 to the next. */
6223 set_iterator_to_next (it, 1);
6224
6225 /* Stop if lines are truncated and IT's current x-position is
6226 past the right edge of the window now. */
6227 if (it->truncate_lines_p
6228 && it->current_x >= it->last_visible_x)
6229 {
6230 #ifdef HAVE_WINDOW_SYSTEM
6231 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
6232 {
6233 if (!get_next_display_element (it)
6234 || BUFFER_POS_REACHED_P ())
6235 {
6236 result = MOVE_POS_MATCH_OR_ZV;
6237 break;
6238 }
6239 if (ITERATOR_AT_END_OF_LINE_P (it))
6240 {
6241 result = MOVE_NEWLINE_OR_CR;
6242 break;
6243 }
6244 }
6245 #endif /* HAVE_WINDOW_SYSTEM */
6246 result = MOVE_LINE_TRUNCATED;
6247 break;
6248 }
6249 }
6250
6251 #undef BUFFER_POS_REACHED_P
6252
6253 /* Restore the iterator settings altered at the beginning of this
6254 function. */
6255 it->glyph_row = saved_glyph_row;
6256 return result;
6257 }
6258
6259
6260 /* Move IT forward until it satisfies one or more of the criteria in
6261 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
6262
6263 OP is a bit-mask that specifies where to stop, and in particular,
6264 which of those four position arguments makes a difference. See the
6265 description of enum move_operation_enum.
6266
6267 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
6268 screen line, this function will set IT to the next position >
6269 TO_CHARPOS. */
6270
6271 void
6272 move_it_to (it, to_charpos, to_x, to_y, to_vpos, op)
6273 struct it *it;
6274 int to_charpos, to_x, to_y, to_vpos;
6275 int op;
6276 {
6277 enum move_it_result skip, skip2 = MOVE_X_REACHED;
6278 int line_height;
6279 int reached = 0;
6280
6281 for (;;)
6282 {
6283 if (op & MOVE_TO_VPOS)
6284 {
6285 /* If no TO_CHARPOS and no TO_X specified, stop at the
6286 start of the line TO_VPOS. */
6287 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
6288 {
6289 if (it->vpos == to_vpos)
6290 {
6291 reached = 1;
6292 break;
6293 }
6294 else
6295 skip = move_it_in_display_line_to (it, -1, -1, 0);
6296 }
6297 else
6298 {
6299 /* TO_VPOS >= 0 means stop at TO_X in the line at
6300 TO_VPOS, or at TO_POS, whichever comes first. */
6301 if (it->vpos == to_vpos)
6302 {
6303 reached = 2;
6304 break;
6305 }
6306
6307 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
6308
6309 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
6310 {
6311 reached = 3;
6312 break;
6313 }
6314 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
6315 {
6316 /* We have reached TO_X but not in the line we want. */
6317 skip = move_it_in_display_line_to (it, to_charpos,
6318 -1, MOVE_TO_POS);
6319 if (skip == MOVE_POS_MATCH_OR_ZV)
6320 {
6321 reached = 4;
6322 break;
6323 }
6324 }
6325 }
6326 }
6327 else if (op & MOVE_TO_Y)
6328 {
6329 struct it it_backup;
6330
6331 /* TO_Y specified means stop at TO_X in the line containing
6332 TO_Y---or at TO_CHARPOS if this is reached first. The
6333 problem is that we can't really tell whether the line
6334 contains TO_Y before we have completely scanned it, and
6335 this may skip past TO_X. What we do is to first scan to
6336 TO_X.
6337
6338 If TO_X is not specified, use a TO_X of zero. The reason
6339 is to make the outcome of this function more predictable.
6340 If we didn't use TO_X == 0, we would stop at the end of
6341 the line which is probably not what a caller would expect
6342 to happen. */
6343 skip = move_it_in_display_line_to (it, to_charpos,
6344 ((op & MOVE_TO_X)
6345 ? to_x : 0),
6346 (MOVE_TO_X
6347 | (op & MOVE_TO_POS)));
6348
6349 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
6350 if (skip == MOVE_POS_MATCH_OR_ZV)
6351 {
6352 reached = 5;
6353 break;
6354 }
6355
6356 /* If TO_X was reached, we would like to know whether TO_Y
6357 is in the line. This can only be said if we know the
6358 total line height which requires us to scan the rest of
6359 the line. */
6360 if (skip == MOVE_X_REACHED)
6361 {
6362 /* Wait! We can conclude that TO_Y is in the line if
6363 the already scanned glyphs make the line tall enough
6364 because further scanning doesn't make it shorter. */
6365 line_height = it->max_ascent + it->max_descent;
6366 if (to_y >= it->current_y
6367 && to_y < it->current_y + line_height)
6368 {
6369 reached = 6;
6370 break;
6371 }
6372 it_backup = *it;
6373 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
6374 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
6375 op & MOVE_TO_POS);
6376 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
6377 }
6378
6379 /* Now, decide whether TO_Y is in this line. */
6380 line_height = it->max_ascent + it->max_descent;
6381 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
6382
6383 if (to_y >= it->current_y
6384 && to_y < it->current_y + line_height)
6385 {
6386 if (skip == MOVE_X_REACHED)
6387 /* If TO_Y is in this line and TO_X was reached above,
6388 we scanned too far. We have to restore IT's settings
6389 to the ones before skipping. */
6390 *it = it_backup;
6391 reached = 6;
6392 }
6393 else if (skip == MOVE_X_REACHED)
6394 {
6395 skip = skip2;
6396 if (skip == MOVE_POS_MATCH_OR_ZV)
6397 reached = 7;
6398 }
6399
6400 if (reached)
6401 break;
6402 }
6403 else
6404 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
6405
6406 switch (skip)
6407 {
6408 case MOVE_POS_MATCH_OR_ZV:
6409 reached = 8;
6410 goto out;
6411
6412 case MOVE_NEWLINE_OR_CR:
6413 set_iterator_to_next (it, 1);
6414 it->continuation_lines_width = 0;
6415 break;
6416
6417 case MOVE_LINE_TRUNCATED:
6418 it->continuation_lines_width = 0;
6419 reseat_at_next_visible_line_start (it, 0);
6420 if ((op & MOVE_TO_POS) != 0
6421 && IT_CHARPOS (*it) > to_charpos)
6422 {
6423 reached = 9;
6424 goto out;
6425 }
6426 break;
6427
6428 case MOVE_LINE_CONTINUED:
6429 it->continuation_lines_width += it->current_x;
6430 break;
6431
6432 default:
6433 abort ();
6434 }
6435
6436 /* Reset/increment for the next run. */
6437 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
6438 it->current_x = it->hpos = 0;
6439 it->current_y += it->max_ascent + it->max_descent;
6440 ++it->vpos;
6441 last_height = it->max_ascent + it->max_descent;
6442 last_max_ascent = it->max_ascent;
6443 it->max_ascent = it->max_descent = 0;
6444 }
6445
6446 out:
6447
6448 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
6449 }
6450
6451
6452 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
6453
6454 If DY > 0, move IT backward at least that many pixels. DY = 0
6455 means move IT backward to the preceding line start or BEGV. This
6456 function may move over more than DY pixels if IT->current_y - DY
6457 ends up in the middle of a line; in this case IT->current_y will be
6458 set to the top of the line moved to. */
6459
6460 void
6461 move_it_vertically_backward (it, dy)
6462 struct it *it;
6463 int dy;
6464 {
6465 int nlines, h;
6466 struct it it2, it3;
6467 int start_pos;
6468
6469 move_further_back:
6470 xassert (dy >= 0);
6471
6472 start_pos = IT_CHARPOS (*it);
6473
6474 /* Estimate how many newlines we must move back. */
6475 nlines = max (1, dy / FRAME_LINE_HEIGHT (it->f));
6476
6477 /* Set the iterator's position that many lines back. */
6478 while (nlines-- && IT_CHARPOS (*it) > BEGV)
6479 back_to_previous_visible_line_start (it);
6480
6481 /* Reseat the iterator here. When moving backward, we don't want
6482 reseat to skip forward over invisible text, set up the iterator
6483 to deliver from overlay strings at the new position etc. So,
6484 use reseat_1 here. */
6485 reseat_1 (it, it->current.pos, 1);
6486
6487 /* We are now surely at a line start. */
6488 it->current_x = it->hpos = 0;
6489 it->continuation_lines_width = 0;
6490
6491 /* Move forward and see what y-distance we moved. First move to the
6492 start of the next line so that we get its height. We need this
6493 height to be able to tell whether we reached the specified
6494 y-distance. */
6495 it2 = *it;
6496 it2.max_ascent = it2.max_descent = 0;
6497 do
6498 {
6499 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
6500 MOVE_TO_POS | MOVE_TO_VPOS);
6501 }
6502 while (!IT_POS_VALID_AFTER_MOVE_P (&it2));
6503 xassert (IT_CHARPOS (*it) >= BEGV);
6504 it3 = it2;
6505
6506 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
6507 xassert (IT_CHARPOS (*it) >= BEGV);
6508 /* H is the actual vertical distance from the position in *IT
6509 and the starting position. */
6510 h = it2.current_y - it->current_y;
6511 /* NLINES is the distance in number of lines. */
6512 nlines = it2.vpos - it->vpos;
6513
6514 /* Correct IT's y and vpos position
6515 so that they are relative to the starting point. */
6516 it->vpos -= nlines;
6517 it->current_y -= h;
6518
6519 if (dy == 0)
6520 {
6521 /* DY == 0 means move to the start of the screen line. The
6522 value of nlines is > 0 if continuation lines were involved. */
6523 if (nlines > 0)
6524 move_it_by_lines (it, nlines, 1);
6525 #if 0
6526 /* I think this assert is bogus if buffer contains
6527 invisible text or images. KFS. */
6528 xassert (IT_CHARPOS (*it) <= start_pos);
6529 #endif
6530 }
6531 else
6532 {
6533 /* The y-position we try to reach, relative to *IT.
6534 Note that H has been subtracted in front of the if-statement. */
6535 int target_y = it->current_y + h - dy;
6536 int y0 = it3.current_y;
6537 int y1 = line_bottom_y (&it3);
6538 int line_height = y1 - y0;
6539
6540 /* If we did not reach target_y, try to move further backward if
6541 we can. If we moved too far backward, try to move forward. */
6542 if (target_y < it->current_y
6543 /* This is heuristic. In a window that's 3 lines high, with
6544 a line height of 13 pixels each, recentering with point
6545 on the bottom line will try to move -39/2 = 19 pixels
6546 backward. Try to avoid moving into the first line. */
6547 && (it->current_y - target_y
6548 > min (window_box_height (it->w), line_height * 2 / 3))
6549 && IT_CHARPOS (*it) > BEGV)
6550 {
6551 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
6552 target_y - it->current_y));
6553 dy = it->current_y - target_y;
6554 goto move_further_back;
6555 }
6556 else if (target_y >= it->current_y + line_height
6557 && IT_CHARPOS (*it) < ZV)
6558 {
6559 /* Should move forward by at least one line, maybe more.
6560
6561 Note: Calling move_it_by_lines can be expensive on
6562 terminal frames, where compute_motion is used (via
6563 vmotion) to do the job, when there are very long lines
6564 and truncate-lines is nil. That's the reason for
6565 treating terminal frames specially here. */
6566
6567 if (!FRAME_WINDOW_P (it->f))
6568 move_it_vertically (it, target_y - (it->current_y + line_height));
6569 else
6570 {
6571 do
6572 {
6573 move_it_by_lines (it, 1, 1);
6574 }
6575 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
6576 }
6577
6578 #if 0
6579 /* I think this assert is bogus if buffer contains
6580 invisible text or images. KFS. */
6581 xassert (IT_CHARPOS (*it) >= BEGV);
6582 #endif
6583 }
6584 }
6585 }
6586
6587
6588 /* Move IT by a specified amount of pixel lines DY. DY negative means
6589 move backwards. DY = 0 means move to start of screen line. At the
6590 end, IT will be on the start of a screen line. */
6591
6592 void
6593 move_it_vertically (it, dy)
6594 struct it *it;
6595 int dy;
6596 {
6597 if (dy <= 0)
6598 move_it_vertically_backward (it, -dy);
6599 else
6600 {
6601 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
6602 move_it_to (it, ZV, -1, it->current_y + dy, -1,
6603 MOVE_TO_POS | MOVE_TO_Y);
6604 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
6605
6606 /* If buffer ends in ZV without a newline, move to the start of
6607 the line to satisfy the post-condition. */
6608 if (IT_CHARPOS (*it) == ZV
6609 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
6610 move_it_by_lines (it, 0, 0);
6611 }
6612 }
6613
6614
6615 /* Move iterator IT past the end of the text line it is in. */
6616
6617 void
6618 move_it_past_eol (it)
6619 struct it *it;
6620 {
6621 enum move_it_result rc;
6622
6623 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
6624 if (rc == MOVE_NEWLINE_OR_CR)
6625 set_iterator_to_next (it, 0);
6626 }
6627
6628
6629 #if 0 /* Currently not used. */
6630
6631 /* Return non-zero if some text between buffer positions START_CHARPOS
6632 and END_CHARPOS is invisible. IT->window is the window for text
6633 property lookup. */
6634
6635 static int
6636 invisible_text_between_p (it, start_charpos, end_charpos)
6637 struct it *it;
6638 int start_charpos, end_charpos;
6639 {
6640 Lisp_Object prop, limit;
6641 int invisible_found_p;
6642
6643 xassert (it != NULL && start_charpos <= end_charpos);
6644
6645 /* Is text at START invisible? */
6646 prop = Fget_char_property (make_number (start_charpos), Qinvisible,
6647 it->window);
6648 if (TEXT_PROP_MEANS_INVISIBLE (prop))
6649 invisible_found_p = 1;
6650 else
6651 {
6652 limit = Fnext_single_char_property_change (make_number (start_charpos),
6653 Qinvisible, Qnil,
6654 make_number (end_charpos));
6655 invisible_found_p = XFASTINT (limit) < end_charpos;
6656 }
6657
6658 return invisible_found_p;
6659 }
6660
6661 #endif /* 0 */
6662
6663
6664 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
6665 negative means move up. DVPOS == 0 means move to the start of the
6666 screen line. NEED_Y_P non-zero means calculate IT->current_y. If
6667 NEED_Y_P is zero, IT->current_y will be left unchanged.
6668
6669 Further optimization ideas: If we would know that IT->f doesn't use
6670 a face with proportional font, we could be faster for
6671 truncate-lines nil. */
6672
6673 void
6674 move_it_by_lines (it, dvpos, need_y_p)
6675 struct it *it;
6676 int dvpos, need_y_p;
6677 {
6678 struct position pos;
6679
6680 if (!FRAME_WINDOW_P (it->f))
6681 {
6682 struct text_pos textpos;
6683
6684 /* We can use vmotion on frames without proportional fonts. */
6685 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
6686 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
6687 reseat (it, textpos, 1);
6688 it->vpos += pos.vpos;
6689 it->current_y += pos.vpos;
6690 }
6691 else if (dvpos == 0)
6692 {
6693 /* DVPOS == 0 means move to the start of the screen line. */
6694 move_it_vertically_backward (it, 0);
6695 xassert (it->current_x == 0 && it->hpos == 0);
6696 /* Let next call to line_bottom_y calculate real line height */
6697 last_height = 0;
6698 }
6699 else if (dvpos > 0)
6700 {
6701 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
6702 if (!IT_POS_VALID_AFTER_MOVE_P (it))
6703 move_it_to (it, IT_CHARPOS (*it) + 1, -1, -1, -1, MOVE_TO_POS);
6704 }
6705 else
6706 {
6707 struct it it2;
6708 int start_charpos, i;
6709
6710 /* Start at the beginning of the screen line containing IT's
6711 position. This may actually move vertically backwards,
6712 in case of overlays, so adjust dvpos accordingly. */
6713 dvpos += it->vpos;
6714 move_it_vertically_backward (it, 0);
6715 dvpos -= it->vpos;
6716
6717 /* Go back -DVPOS visible lines and reseat the iterator there. */
6718 start_charpos = IT_CHARPOS (*it);
6719 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > BEGV; --i)
6720 back_to_previous_visible_line_start (it);
6721 reseat (it, it->current.pos, 1);
6722
6723 /* Move further back if we end up in a string or an image. */
6724 while (!IT_POS_VALID_AFTER_MOVE_P (it))
6725 {
6726 /* First try to move to start of display line. */
6727 dvpos += it->vpos;
6728 move_it_vertically_backward (it, 0);
6729 dvpos -= it->vpos;
6730 if (IT_POS_VALID_AFTER_MOVE_P (it))
6731 break;
6732 /* If start of line is still in string or image,
6733 move further back. */
6734 back_to_previous_visible_line_start (it);
6735 reseat (it, it->current.pos, 1);
6736 dvpos--;
6737 }
6738
6739 it->current_x = it->hpos = 0;
6740
6741 /* Above call may have moved too far if continuation lines
6742 are involved. Scan forward and see if it did. */
6743 it2 = *it;
6744 it2.vpos = it2.current_y = 0;
6745 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
6746 it->vpos -= it2.vpos;
6747 it->current_y -= it2.current_y;
6748 it->current_x = it->hpos = 0;
6749
6750 /* If we moved too far back, move IT some lines forward. */
6751 if (it2.vpos > -dvpos)
6752 {
6753 int delta = it2.vpos + dvpos;
6754 it2 = *it;
6755 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
6756 /* Move back again if we got too far ahead. */
6757 if (IT_CHARPOS (*it) >= start_charpos)
6758 *it = it2;
6759 }
6760 }
6761 }
6762
6763 /* Return 1 if IT points into the middle of a display vector. */
6764
6765 int
6766 in_display_vector_p (it)
6767 struct it *it;
6768 {
6769 return (it->method == GET_FROM_DISPLAY_VECTOR
6770 && it->current.dpvec_index > 0
6771 && it->dpvec + it->current.dpvec_index != it->dpend);
6772 }
6773
6774 \f
6775 /***********************************************************************
6776 Messages
6777 ***********************************************************************/
6778
6779
6780 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
6781 to *Messages*. */
6782
6783 void
6784 add_to_log (format, arg1, arg2)
6785 char *format;
6786 Lisp_Object arg1, arg2;
6787 {
6788 Lisp_Object args[3];
6789 Lisp_Object msg, fmt;
6790 char *buffer;
6791 int len;
6792 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
6793 USE_SAFE_ALLOCA;
6794
6795 /* Do nothing if called asynchronously. Inserting text into
6796 a buffer may call after-change-functions and alike and
6797 that would means running Lisp asynchronously. */
6798 if (handling_signal)
6799 return;
6800
6801 fmt = msg = Qnil;
6802 GCPRO4 (fmt, msg, arg1, arg2);
6803
6804 args[0] = fmt = build_string (format);
6805 args[1] = arg1;
6806 args[2] = arg2;
6807 msg = Fformat (3, args);
6808
6809 len = SBYTES (msg) + 1;
6810 SAFE_ALLOCA (buffer, char *, len);
6811 bcopy (SDATA (msg), buffer, len);
6812
6813 message_dolog (buffer, len - 1, 1, 0);
6814 SAFE_FREE ();
6815
6816 UNGCPRO;
6817 }
6818
6819
6820 /* Output a newline in the *Messages* buffer if "needs" one. */
6821
6822 void
6823 message_log_maybe_newline ()
6824 {
6825 if (message_log_need_newline)
6826 message_dolog ("", 0, 1, 0);
6827 }
6828
6829
6830 /* Add a string M of length NBYTES to the message log, optionally
6831 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
6832 nonzero, means interpret the contents of M as multibyte. This
6833 function calls low-level routines in order to bypass text property
6834 hooks, etc. which might not be safe to run. */
6835
6836 void
6837 message_dolog (m, nbytes, nlflag, multibyte)
6838 const char *m;
6839 int nbytes, nlflag, multibyte;
6840 {
6841 if (!NILP (Vmemory_full))
6842 return;
6843
6844 if (!NILP (Vmessage_log_max))
6845 {
6846 struct buffer *oldbuf;
6847 Lisp_Object oldpoint, oldbegv, oldzv;
6848 int old_windows_or_buffers_changed = windows_or_buffers_changed;
6849 int point_at_end = 0;
6850 int zv_at_end = 0;
6851 Lisp_Object old_deactivate_mark, tem;
6852 struct gcpro gcpro1;
6853
6854 old_deactivate_mark = Vdeactivate_mark;
6855 oldbuf = current_buffer;
6856 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
6857 current_buffer->undo_list = Qt;
6858
6859 oldpoint = message_dolog_marker1;
6860 set_marker_restricted (oldpoint, make_number (PT), Qnil);
6861 oldbegv = message_dolog_marker2;
6862 set_marker_restricted (oldbegv, make_number (BEGV), Qnil);
6863 oldzv = message_dolog_marker3;
6864 set_marker_restricted (oldzv, make_number (ZV), Qnil);
6865 GCPRO1 (old_deactivate_mark);
6866
6867 if (PT == Z)
6868 point_at_end = 1;
6869 if (ZV == Z)
6870 zv_at_end = 1;
6871
6872 BEGV = BEG;
6873 BEGV_BYTE = BEG_BYTE;
6874 ZV = Z;
6875 ZV_BYTE = Z_BYTE;
6876 TEMP_SET_PT_BOTH (Z, Z_BYTE);
6877
6878 /* Insert the string--maybe converting multibyte to single byte
6879 or vice versa, so that all the text fits the buffer. */
6880 if (multibyte
6881 && NILP (current_buffer->enable_multibyte_characters))
6882 {
6883 int i, c, char_bytes;
6884 unsigned char work[1];
6885
6886 /* Convert a multibyte string to single-byte
6887 for the *Message* buffer. */
6888 for (i = 0; i < nbytes; i += char_bytes)
6889 {
6890 c = string_char_and_length (m + i, nbytes - i, &char_bytes);
6891 work[0] = (ASCII_CHAR_P (c)
6892 ? c
6893 : multibyte_char_to_unibyte (c, Qnil));
6894 insert_1_both (work, 1, 1, 1, 0, 0);
6895 }
6896 }
6897 else if (! multibyte
6898 && ! NILP (current_buffer->enable_multibyte_characters))
6899 {
6900 int i, c, char_bytes;
6901 unsigned char *msg = (unsigned char *) m;
6902 unsigned char str[MAX_MULTIBYTE_LENGTH];
6903 /* Convert a single-byte string to multibyte
6904 for the *Message* buffer. */
6905 for (i = 0; i < nbytes; i++)
6906 {
6907 c = msg[i];
6908 c = unibyte_char_to_multibyte (c);
6909 char_bytes = CHAR_STRING (c, str);
6910 insert_1_both (str, 1, char_bytes, 1, 0, 0);
6911 }
6912 }
6913 else if (nbytes)
6914 insert_1 (m, nbytes, 1, 0, 0);
6915
6916 if (nlflag)
6917 {
6918 int this_bol, this_bol_byte, prev_bol, prev_bol_byte, dup;
6919 insert_1 ("\n", 1, 1, 0, 0);
6920
6921 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
6922 this_bol = PT;
6923 this_bol_byte = PT_BYTE;
6924
6925 /* See if this line duplicates the previous one.
6926 If so, combine duplicates. */
6927 if (this_bol > BEG)
6928 {
6929 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
6930 prev_bol = PT;
6931 prev_bol_byte = PT_BYTE;
6932
6933 dup = message_log_check_duplicate (prev_bol, prev_bol_byte,
6934 this_bol, this_bol_byte);
6935 if (dup)
6936 {
6937 del_range_both (prev_bol, prev_bol_byte,
6938 this_bol, this_bol_byte, 0);
6939 if (dup > 1)
6940 {
6941 char dupstr[40];
6942 int duplen;
6943
6944 /* If you change this format, don't forget to also
6945 change message_log_check_duplicate. */
6946 sprintf (dupstr, " [%d times]", dup);
6947 duplen = strlen (dupstr);
6948 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
6949 insert_1 (dupstr, duplen, 1, 0, 1);
6950 }
6951 }
6952 }
6953
6954 /* If we have more than the desired maximum number of lines
6955 in the *Messages* buffer now, delete the oldest ones.
6956 This is safe because we don't have undo in this buffer. */
6957
6958 if (NATNUMP (Vmessage_log_max))
6959 {
6960 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
6961 -XFASTINT (Vmessage_log_max) - 1, 0);
6962 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
6963 }
6964 }
6965 BEGV = XMARKER (oldbegv)->charpos;
6966 BEGV_BYTE = marker_byte_position (oldbegv);
6967
6968 if (zv_at_end)
6969 {
6970 ZV = Z;
6971 ZV_BYTE = Z_BYTE;
6972 }
6973 else
6974 {
6975 ZV = XMARKER (oldzv)->charpos;
6976 ZV_BYTE = marker_byte_position (oldzv);
6977 }
6978
6979 if (point_at_end)
6980 TEMP_SET_PT_BOTH (Z, Z_BYTE);
6981 else
6982 /* We can't do Fgoto_char (oldpoint) because it will run some
6983 Lisp code. */
6984 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
6985 XMARKER (oldpoint)->bytepos);
6986
6987 UNGCPRO;
6988 unchain_marker (XMARKER (oldpoint));
6989 unchain_marker (XMARKER (oldbegv));
6990 unchain_marker (XMARKER (oldzv));
6991
6992 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
6993 set_buffer_internal (oldbuf);
6994 if (NILP (tem))
6995 windows_or_buffers_changed = old_windows_or_buffers_changed;
6996 message_log_need_newline = !nlflag;
6997 Vdeactivate_mark = old_deactivate_mark;
6998 }
6999 }
7000
7001
7002 /* We are at the end of the buffer after just having inserted a newline.
7003 (Note: We depend on the fact we won't be crossing the gap.)
7004 Check to see if the most recent message looks a lot like the previous one.
7005 Return 0 if different, 1 if the new one should just replace it, or a
7006 value N > 1 if we should also append " [N times]". */
7007
7008 static int
7009 message_log_check_duplicate (prev_bol, prev_bol_byte, this_bol, this_bol_byte)
7010 int prev_bol, this_bol;
7011 int prev_bol_byte, this_bol_byte;
7012 {
7013 int i;
7014 int len = Z_BYTE - 1 - this_bol_byte;
7015 int seen_dots = 0;
7016 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
7017 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
7018
7019 for (i = 0; i < len; i++)
7020 {
7021 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
7022 seen_dots = 1;
7023 if (p1[i] != p2[i])
7024 return seen_dots;
7025 }
7026 p1 += len;
7027 if (*p1 == '\n')
7028 return 2;
7029 if (*p1++ == ' ' && *p1++ == '[')
7030 {
7031 int n = 0;
7032 while (*p1 >= '0' && *p1 <= '9')
7033 n = n * 10 + *p1++ - '0';
7034 if (strncmp (p1, " times]\n", 8) == 0)
7035 return n+1;
7036 }
7037 return 0;
7038 }
7039 \f
7040
7041 /* Display an echo area message M with a specified length of NBYTES
7042 bytes. The string may include null characters. If M is 0, clear
7043 out any existing message, and let the mini-buffer text show
7044 through.
7045
7046 The buffer M must continue to exist until after the echo area gets
7047 cleared or some other message gets displayed there. This means do
7048 not pass text that is stored in a Lisp string; do not pass text in
7049 a buffer that was alloca'd. */
7050
7051 void
7052 message2 (m, nbytes, multibyte)
7053 const char *m;
7054 int nbytes;
7055 int multibyte;
7056 {
7057 /* First flush out any partial line written with print. */
7058 message_log_maybe_newline ();
7059 if (m)
7060 message_dolog (m, nbytes, 1, multibyte);
7061 message2_nolog (m, nbytes, multibyte);
7062 }
7063
7064
7065 /* The non-logging counterpart of message2. */
7066
7067 void
7068 message2_nolog (m, nbytes, multibyte)
7069 const char *m;
7070 int nbytes, multibyte;
7071 {
7072 struct frame *sf = SELECTED_FRAME ();
7073 message_enable_multibyte = multibyte;
7074
7075 if (noninteractive)
7076 {
7077 if (noninteractive_need_newline)
7078 putc ('\n', stderr);
7079 noninteractive_need_newline = 0;
7080 if (m)
7081 fwrite (m, nbytes, 1, stderr);
7082 if (cursor_in_echo_area == 0)
7083 fprintf (stderr, "\n");
7084 fflush (stderr);
7085 }
7086 /* A null message buffer means that the frame hasn't really been
7087 initialized yet. Error messages get reported properly by
7088 cmd_error, so this must be just an informative message; toss it. */
7089 else if (INTERACTIVE
7090 && sf->glyphs_initialized_p
7091 && FRAME_MESSAGE_BUF (sf))
7092 {
7093 Lisp_Object mini_window;
7094 struct frame *f;
7095
7096 /* Get the frame containing the mini-buffer
7097 that the selected frame is using. */
7098 mini_window = FRAME_MINIBUF_WINDOW (sf);
7099 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
7100
7101 FRAME_SAMPLE_VISIBILITY (f);
7102 if (FRAME_VISIBLE_P (sf)
7103 && ! FRAME_VISIBLE_P (f))
7104 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
7105
7106 if (m)
7107 {
7108 set_message (m, Qnil, nbytes, multibyte);
7109 if (minibuffer_auto_raise)
7110 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
7111 }
7112 else
7113 clear_message (1, 1);
7114
7115 do_pending_window_change (0);
7116 echo_area_display (1);
7117 do_pending_window_change (0);
7118 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
7119 (*frame_up_to_date_hook) (f);
7120 }
7121 }
7122
7123
7124 /* Display an echo area message M with a specified length of NBYTES
7125 bytes. The string may include null characters. If M is not a
7126 string, clear out any existing message, and let the mini-buffer
7127 text show through.
7128
7129 This function cancels echoing. */
7130
7131 void
7132 message3 (m, nbytes, multibyte)
7133 Lisp_Object m;
7134 int nbytes;
7135 int multibyte;
7136 {
7137 struct gcpro gcpro1;
7138
7139 GCPRO1 (m);
7140 clear_message (1,1);
7141 cancel_echoing ();
7142
7143 /* First flush out any partial line written with print. */
7144 message_log_maybe_newline ();
7145 if (STRINGP (m))
7146 message_dolog (SDATA (m), nbytes, 1, multibyte);
7147 message3_nolog (m, nbytes, multibyte);
7148
7149 UNGCPRO;
7150 }
7151
7152
7153 /* The non-logging version of message3.
7154 This does not cancel echoing, because it is used for echoing.
7155 Perhaps we need to make a separate function for echoing
7156 and make this cancel echoing. */
7157
7158 void
7159 message3_nolog (m, nbytes, multibyte)
7160 Lisp_Object m;
7161 int nbytes, multibyte;
7162 {
7163 struct frame *sf = SELECTED_FRAME ();
7164 message_enable_multibyte = multibyte;
7165
7166 if (noninteractive)
7167 {
7168 if (noninteractive_need_newline)
7169 putc ('\n', stderr);
7170 noninteractive_need_newline = 0;
7171 if (STRINGP (m))
7172 fwrite (SDATA (m), nbytes, 1, stderr);
7173 if (cursor_in_echo_area == 0)
7174 fprintf (stderr, "\n");
7175 fflush (stderr);
7176 }
7177 /* A null message buffer means that the frame hasn't really been
7178 initialized yet. Error messages get reported properly by
7179 cmd_error, so this must be just an informative message; toss it. */
7180 else if (INTERACTIVE
7181 && sf->glyphs_initialized_p
7182 && FRAME_MESSAGE_BUF (sf))
7183 {
7184 Lisp_Object mini_window;
7185 Lisp_Object frame;
7186 struct frame *f;
7187
7188 /* Get the frame containing the mini-buffer
7189 that the selected frame is using. */
7190 mini_window = FRAME_MINIBUF_WINDOW (sf);
7191 frame = XWINDOW (mini_window)->frame;
7192 f = XFRAME (frame);
7193
7194 FRAME_SAMPLE_VISIBILITY (f);
7195 if (FRAME_VISIBLE_P (sf)
7196 && !FRAME_VISIBLE_P (f))
7197 Fmake_frame_visible (frame);
7198
7199 if (STRINGP (m) && SCHARS (m) > 0)
7200 {
7201 set_message (NULL, m, nbytes, multibyte);
7202 if (minibuffer_auto_raise)
7203 Fraise_frame (frame);
7204 }
7205 else
7206 clear_message (1, 1);
7207
7208 do_pending_window_change (0);
7209 echo_area_display (1);
7210 do_pending_window_change (0);
7211 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
7212 (*frame_up_to_date_hook) (f);
7213 }
7214 }
7215
7216
7217 /* Display a null-terminated echo area message M. If M is 0, clear
7218 out any existing message, and let the mini-buffer text show through.
7219
7220 The buffer M must continue to exist until after the echo area gets
7221 cleared or some other message gets displayed there. Do not pass
7222 text that is stored in a Lisp string. Do not pass text in a buffer
7223 that was alloca'd. */
7224
7225 void
7226 message1 (m)
7227 char *m;
7228 {
7229 message2 (m, (m ? strlen (m) : 0), 0);
7230 }
7231
7232
7233 /* The non-logging counterpart of message1. */
7234
7235 void
7236 message1_nolog (m)
7237 char *m;
7238 {
7239 message2_nolog (m, (m ? strlen (m) : 0), 0);
7240 }
7241
7242 /* Display a message M which contains a single %s
7243 which gets replaced with STRING. */
7244
7245 void
7246 message_with_string (m, string, log)
7247 char *m;
7248 Lisp_Object string;
7249 int log;
7250 {
7251 CHECK_STRING (string);
7252
7253 if (noninteractive)
7254 {
7255 if (m)
7256 {
7257 if (noninteractive_need_newline)
7258 putc ('\n', stderr);
7259 noninteractive_need_newline = 0;
7260 fprintf (stderr, m, SDATA (string));
7261 if (cursor_in_echo_area == 0)
7262 fprintf (stderr, "\n");
7263 fflush (stderr);
7264 }
7265 }
7266 else if (INTERACTIVE)
7267 {
7268 /* The frame whose minibuffer we're going to display the message on.
7269 It may be larger than the selected frame, so we need
7270 to use its buffer, not the selected frame's buffer. */
7271 Lisp_Object mini_window;
7272 struct frame *f, *sf = SELECTED_FRAME ();
7273
7274 /* Get the frame containing the minibuffer
7275 that the selected frame is using. */
7276 mini_window = FRAME_MINIBUF_WINDOW (sf);
7277 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
7278
7279 /* A null message buffer means that the frame hasn't really been
7280 initialized yet. Error messages get reported properly by
7281 cmd_error, so this must be just an informative message; toss it. */
7282 if (FRAME_MESSAGE_BUF (f))
7283 {
7284 Lisp_Object args[2], message;
7285 struct gcpro gcpro1, gcpro2;
7286
7287 args[0] = build_string (m);
7288 args[1] = message = string;
7289 GCPRO2 (args[0], message);
7290 gcpro1.nvars = 2;
7291
7292 message = Fformat (2, args);
7293
7294 if (log)
7295 message3 (message, SBYTES (message), STRING_MULTIBYTE (message));
7296 else
7297 message3_nolog (message, SBYTES (message), STRING_MULTIBYTE (message));
7298
7299 UNGCPRO;
7300
7301 /* Print should start at the beginning of the message
7302 buffer next time. */
7303 message_buf_print = 0;
7304 }
7305 }
7306 }
7307
7308
7309 /* Dump an informative message to the minibuf. If M is 0, clear out
7310 any existing message, and let the mini-buffer text show through. */
7311
7312 /* VARARGS 1 */
7313 void
7314 message (m, a1, a2, a3)
7315 char *m;
7316 EMACS_INT a1, a2, a3;
7317 {
7318 if (noninteractive)
7319 {
7320 if (m)
7321 {
7322 if (noninteractive_need_newline)
7323 putc ('\n', stderr);
7324 noninteractive_need_newline = 0;
7325 fprintf (stderr, m, a1, a2, a3);
7326 if (cursor_in_echo_area == 0)
7327 fprintf (stderr, "\n");
7328 fflush (stderr);
7329 }
7330 }
7331 else if (INTERACTIVE)
7332 {
7333 /* The frame whose mini-buffer we're going to display the message
7334 on. It may be larger than the selected frame, so we need to
7335 use its buffer, not the selected frame's buffer. */
7336 Lisp_Object mini_window;
7337 struct frame *f, *sf = SELECTED_FRAME ();
7338
7339 /* Get the frame containing the mini-buffer
7340 that the selected frame is using. */
7341 mini_window = FRAME_MINIBUF_WINDOW (sf);
7342 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
7343
7344 /* A null message buffer means that the frame hasn't really been
7345 initialized yet. Error messages get reported properly by
7346 cmd_error, so this must be just an informative message; toss
7347 it. */
7348 if (FRAME_MESSAGE_BUF (f))
7349 {
7350 if (m)
7351 {
7352 int len;
7353 #ifdef NO_ARG_ARRAY
7354 char *a[3];
7355 a[0] = (char *) a1;
7356 a[1] = (char *) a2;
7357 a[2] = (char *) a3;
7358
7359 len = doprnt (FRAME_MESSAGE_BUF (f),
7360 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
7361 #else
7362 len = doprnt (FRAME_MESSAGE_BUF (f),
7363 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3,
7364 (char **) &a1);
7365 #endif /* NO_ARG_ARRAY */
7366
7367 message2 (FRAME_MESSAGE_BUF (f), len, 0);
7368 }
7369 else
7370 message1 (0);
7371
7372 /* Print should start at the beginning of the message
7373 buffer next time. */
7374 message_buf_print = 0;
7375 }
7376 }
7377 }
7378
7379
7380 /* The non-logging version of message. */
7381
7382 void
7383 message_nolog (m, a1, a2, a3)
7384 char *m;
7385 EMACS_INT a1, a2, a3;
7386 {
7387 Lisp_Object old_log_max;
7388 old_log_max = Vmessage_log_max;
7389 Vmessage_log_max = Qnil;
7390 message (m, a1, a2, a3);
7391 Vmessage_log_max = old_log_max;
7392 }
7393
7394
7395 /* Display the current message in the current mini-buffer. This is
7396 only called from error handlers in process.c, and is not time
7397 critical. */
7398
7399 void
7400 update_echo_area ()
7401 {
7402 if (!NILP (echo_area_buffer[0]))
7403 {
7404 Lisp_Object string;
7405 string = Fcurrent_message ();
7406 message3 (string, SBYTES (string),
7407 !NILP (current_buffer->enable_multibyte_characters));
7408 }
7409 }
7410
7411
7412 /* Make sure echo area buffers in `echo_buffers' are live.
7413 If they aren't, make new ones. */
7414
7415 static void
7416 ensure_echo_area_buffers ()
7417 {
7418 int i;
7419
7420 for (i = 0; i < 2; ++i)
7421 if (!BUFFERP (echo_buffer[i])
7422 || NILP (XBUFFER (echo_buffer[i])->name))
7423 {
7424 char name[30];
7425 Lisp_Object old_buffer;
7426 int j;
7427
7428 old_buffer = echo_buffer[i];
7429 sprintf (name, " *Echo Area %d*", i);
7430 echo_buffer[i] = Fget_buffer_create (build_string (name));
7431 XBUFFER (echo_buffer[i])->truncate_lines = Qnil;
7432
7433 for (j = 0; j < 2; ++j)
7434 if (EQ (old_buffer, echo_area_buffer[j]))
7435 echo_area_buffer[j] = echo_buffer[i];
7436 }
7437 }
7438
7439
7440 /* Call FN with args A1..A4 with either the current or last displayed
7441 echo_area_buffer as current buffer.
7442
7443 WHICH zero means use the current message buffer
7444 echo_area_buffer[0]. If that is nil, choose a suitable buffer
7445 from echo_buffer[] and clear it.
7446
7447 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
7448 suitable buffer from echo_buffer[] and clear it.
7449
7450 Value is what FN returns. */
7451
7452 static int
7453 with_echo_area_buffer (w, which, fn, a1, a2, a3, a4)
7454 struct window *w;
7455 int which;
7456 int (*fn) P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
7457 EMACS_INT a1;
7458 Lisp_Object a2;
7459 EMACS_INT a3, a4;
7460 {
7461 Lisp_Object buffer;
7462 int this_one, the_other, clear_buffer_p, rc;
7463 int count = SPECPDL_INDEX ();
7464
7465 /* If buffers aren't live, make new ones. */
7466 ensure_echo_area_buffers ();
7467
7468 clear_buffer_p = 0;
7469
7470 if (which == 0)
7471 this_one = 0, the_other = 1;
7472 else if (which > 0)
7473 this_one = 1, the_other = 0;
7474
7475 /* Choose a suitable buffer from echo_buffer[] is we don't
7476 have one. */
7477 if (NILP (echo_area_buffer[this_one]))
7478 {
7479 echo_area_buffer[this_one]
7480 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
7481 ? echo_buffer[the_other]
7482 : echo_buffer[this_one]);
7483 clear_buffer_p = 1;
7484 }
7485
7486 buffer = echo_area_buffer[this_one];
7487
7488 /* Don't get confused by reusing the buffer used for echoing
7489 for a different purpose. */
7490 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
7491 cancel_echoing ();
7492
7493 record_unwind_protect (unwind_with_echo_area_buffer,
7494 with_echo_area_buffer_unwind_data (w));
7495
7496 /* Make the echo area buffer current. Note that for display
7497 purposes, it is not necessary that the displayed window's buffer
7498 == current_buffer, except for text property lookup. So, let's
7499 only set that buffer temporarily here without doing a full
7500 Fset_window_buffer. We must also change w->pointm, though,
7501 because otherwise an assertions in unshow_buffer fails, and Emacs
7502 aborts. */
7503 set_buffer_internal_1 (XBUFFER (buffer));
7504 if (w)
7505 {
7506 w->buffer = buffer;
7507 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
7508 }
7509
7510 current_buffer->undo_list = Qt;
7511 current_buffer->read_only = Qnil;
7512 specbind (Qinhibit_read_only, Qt);
7513 specbind (Qinhibit_modification_hooks, Qt);
7514
7515 if (clear_buffer_p && Z > BEG)
7516 del_range (BEG, Z);
7517
7518 xassert (BEGV >= BEG);
7519 xassert (ZV <= Z && ZV >= BEGV);
7520
7521 rc = fn (a1, a2, a3, a4);
7522
7523 xassert (BEGV >= BEG);
7524 xassert (ZV <= Z && ZV >= BEGV);
7525
7526 unbind_to (count, Qnil);
7527 return rc;
7528 }
7529
7530
7531 /* Save state that should be preserved around the call to the function
7532 FN called in with_echo_area_buffer. */
7533
7534 static Lisp_Object
7535 with_echo_area_buffer_unwind_data (w)
7536 struct window *w;
7537 {
7538 int i = 0;
7539 Lisp_Object vector;
7540
7541 /* Reduce consing by keeping one vector in
7542 Vwith_echo_area_save_vector. */
7543 vector = Vwith_echo_area_save_vector;
7544 Vwith_echo_area_save_vector = Qnil;
7545
7546 if (NILP (vector))
7547 vector = Fmake_vector (make_number (7), Qnil);
7548
7549 XSETBUFFER (AREF (vector, i), current_buffer); ++i;
7550 AREF (vector, i) = Vdeactivate_mark, ++i;
7551 AREF (vector, i) = make_number (windows_or_buffers_changed), ++i;
7552
7553 if (w)
7554 {
7555 XSETWINDOW (AREF (vector, i), w); ++i;
7556 AREF (vector, i) = w->buffer; ++i;
7557 AREF (vector, i) = make_number (XMARKER (w->pointm)->charpos); ++i;
7558 AREF (vector, i) = make_number (XMARKER (w->pointm)->bytepos); ++i;
7559 }
7560 else
7561 {
7562 int end = i + 4;
7563 for (; i < end; ++i)
7564 AREF (vector, i) = Qnil;
7565 }
7566
7567 xassert (i == ASIZE (vector));
7568 return vector;
7569 }
7570
7571
7572 /* Restore global state from VECTOR which was created by
7573 with_echo_area_buffer_unwind_data. */
7574
7575 static Lisp_Object
7576 unwind_with_echo_area_buffer (vector)
7577 Lisp_Object vector;
7578 {
7579 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
7580 Vdeactivate_mark = AREF (vector, 1);
7581 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
7582
7583 if (WINDOWP (AREF (vector, 3)))
7584 {
7585 struct window *w;
7586 Lisp_Object buffer, charpos, bytepos;
7587
7588 w = XWINDOW (AREF (vector, 3));
7589 buffer = AREF (vector, 4);
7590 charpos = AREF (vector, 5);
7591 bytepos = AREF (vector, 6);
7592
7593 w->buffer = buffer;
7594 set_marker_both (w->pointm, buffer,
7595 XFASTINT (charpos), XFASTINT (bytepos));
7596 }
7597
7598 Vwith_echo_area_save_vector = vector;
7599 return Qnil;
7600 }
7601
7602
7603 /* Set up the echo area for use by print functions. MULTIBYTE_P
7604 non-zero means we will print multibyte. */
7605
7606 void
7607 setup_echo_area_for_printing (multibyte_p)
7608 int multibyte_p;
7609 {
7610 /* If we can't find an echo area any more, exit. */
7611 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
7612 Fkill_emacs (Qnil);
7613
7614 ensure_echo_area_buffers ();
7615
7616 if (!message_buf_print)
7617 {
7618 /* A message has been output since the last time we printed.
7619 Choose a fresh echo area buffer. */
7620 if (EQ (echo_area_buffer[1], echo_buffer[0]))
7621 echo_area_buffer[0] = echo_buffer[1];
7622 else
7623 echo_area_buffer[0] = echo_buffer[0];
7624
7625 /* Switch to that buffer and clear it. */
7626 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
7627 current_buffer->truncate_lines = Qnil;
7628
7629 if (Z > BEG)
7630 {
7631 int count = SPECPDL_INDEX ();
7632 specbind (Qinhibit_read_only, Qt);
7633 /* Note that undo recording is always disabled. */
7634 del_range (BEG, Z);
7635 unbind_to (count, Qnil);
7636 }
7637 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
7638
7639 /* Set up the buffer for the multibyteness we need. */
7640 if (multibyte_p
7641 != !NILP (current_buffer->enable_multibyte_characters))
7642 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
7643
7644 /* Raise the frame containing the echo area. */
7645 if (minibuffer_auto_raise)
7646 {
7647 struct frame *sf = SELECTED_FRAME ();
7648 Lisp_Object mini_window;
7649 mini_window = FRAME_MINIBUF_WINDOW (sf);
7650 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
7651 }
7652
7653 message_log_maybe_newline ();
7654 message_buf_print = 1;
7655 }
7656 else
7657 {
7658 if (NILP (echo_area_buffer[0]))
7659 {
7660 if (EQ (echo_area_buffer[1], echo_buffer[0]))
7661 echo_area_buffer[0] = echo_buffer[1];
7662 else
7663 echo_area_buffer[0] = echo_buffer[0];
7664 }
7665
7666 if (current_buffer != XBUFFER (echo_area_buffer[0]))
7667 {
7668 /* Someone switched buffers between print requests. */
7669 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
7670 current_buffer->truncate_lines = Qnil;
7671 }
7672 }
7673 }
7674
7675
7676 /* Display an echo area message in window W. Value is non-zero if W's
7677 height is changed. If display_last_displayed_message_p is
7678 non-zero, display the message that was last displayed, otherwise
7679 display the current message. */
7680
7681 static int
7682 display_echo_area (w)
7683 struct window *w;
7684 {
7685 int i, no_message_p, window_height_changed_p, count;
7686
7687 /* Temporarily disable garbage collections while displaying the echo
7688 area. This is done because a GC can print a message itself.
7689 That message would modify the echo area buffer's contents while a
7690 redisplay of the buffer is going on, and seriously confuse
7691 redisplay. */
7692 count = inhibit_garbage_collection ();
7693
7694 /* If there is no message, we must call display_echo_area_1
7695 nevertheless because it resizes the window. But we will have to
7696 reset the echo_area_buffer in question to nil at the end because
7697 with_echo_area_buffer will sets it to an empty buffer. */
7698 i = display_last_displayed_message_p ? 1 : 0;
7699 no_message_p = NILP (echo_area_buffer[i]);
7700
7701 window_height_changed_p
7702 = with_echo_area_buffer (w, display_last_displayed_message_p,
7703 display_echo_area_1,
7704 (EMACS_INT) w, Qnil, 0, 0);
7705
7706 if (no_message_p)
7707 echo_area_buffer[i] = Qnil;
7708
7709 unbind_to (count, Qnil);
7710 return window_height_changed_p;
7711 }
7712
7713
7714 /* Helper for display_echo_area. Display the current buffer which
7715 contains the current echo area message in window W, a mini-window,
7716 a pointer to which is passed in A1. A2..A4 are currently not used.
7717 Change the height of W so that all of the message is displayed.
7718 Value is non-zero if height of W was changed. */
7719
7720 static int
7721 display_echo_area_1 (a1, a2, a3, a4)
7722 EMACS_INT a1;
7723 Lisp_Object a2;
7724 EMACS_INT a3, a4;
7725 {
7726 struct window *w = (struct window *) a1;
7727 Lisp_Object window;
7728 struct text_pos start;
7729 int window_height_changed_p = 0;
7730
7731 /* Do this before displaying, so that we have a large enough glyph
7732 matrix for the display. */
7733 window_height_changed_p = resize_mini_window (w, 0);
7734
7735 /* Display. */
7736 clear_glyph_matrix (w->desired_matrix);
7737 XSETWINDOW (window, w);
7738 SET_TEXT_POS (start, BEG, BEG_BYTE);
7739 try_window (window, start, 0);
7740
7741 return window_height_changed_p;
7742 }
7743
7744
7745 /* Resize the echo area window to exactly the size needed for the
7746 currently displayed message, if there is one. If a mini-buffer
7747 is active, don't shrink it. */
7748
7749 void
7750 resize_echo_area_exactly ()
7751 {
7752 if (BUFFERP (echo_area_buffer[0])
7753 && WINDOWP (echo_area_window))
7754 {
7755 struct window *w = XWINDOW (echo_area_window);
7756 int resized_p;
7757 Lisp_Object resize_exactly;
7758
7759 if (minibuf_level == 0)
7760 resize_exactly = Qt;
7761 else
7762 resize_exactly = Qnil;
7763
7764 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
7765 (EMACS_INT) w, resize_exactly, 0, 0);
7766 if (resized_p)
7767 {
7768 ++windows_or_buffers_changed;
7769 ++update_mode_lines;
7770 redisplay_internal (0);
7771 }
7772 }
7773 }
7774
7775
7776 /* Callback function for with_echo_area_buffer, when used from
7777 resize_echo_area_exactly. A1 contains a pointer to the window to
7778 resize, EXACTLY non-nil means resize the mini-window exactly to the
7779 size of the text displayed. A3 and A4 are not used. Value is what
7780 resize_mini_window returns. */
7781
7782 static int
7783 resize_mini_window_1 (a1, exactly, a3, a4)
7784 EMACS_INT a1;
7785 Lisp_Object exactly;
7786 EMACS_INT a3, a4;
7787 {
7788 return resize_mini_window ((struct window *) a1, !NILP (exactly));
7789 }
7790
7791
7792 /* Resize mini-window W to fit the size of its contents. EXACT:P
7793 means size the window exactly to the size needed. Otherwise, it's
7794 only enlarged until W's buffer is empty. Value is non-zero if
7795 the window height has been changed. */
7796
7797 int
7798 resize_mini_window (w, exact_p)
7799 struct window *w;
7800 int exact_p;
7801 {
7802 struct frame *f = XFRAME (w->frame);
7803 int window_height_changed_p = 0;
7804
7805 xassert (MINI_WINDOW_P (w));
7806
7807 /* Don't resize windows while redisplaying a window; it would
7808 confuse redisplay functions when the size of the window they are
7809 displaying changes from under them. Such a resizing can happen,
7810 for instance, when which-func prints a long message while
7811 we are running fontification-functions. We're running these
7812 functions with safe_call which binds inhibit-redisplay to t. */
7813 if (!NILP (Vinhibit_redisplay))
7814 return 0;
7815
7816 /* Nil means don't try to resize. */
7817 if (NILP (Vresize_mini_windows)
7818 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
7819 return 0;
7820
7821 if (!FRAME_MINIBUF_ONLY_P (f))
7822 {
7823 struct it it;
7824 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
7825 int total_height = WINDOW_TOTAL_LINES (root) + WINDOW_TOTAL_LINES (w);
7826 int height, max_height;
7827 int unit = FRAME_LINE_HEIGHT (f);
7828 struct text_pos start;
7829 struct buffer *old_current_buffer = NULL;
7830
7831 if (current_buffer != XBUFFER (w->buffer))
7832 {
7833 old_current_buffer = current_buffer;
7834 set_buffer_internal (XBUFFER (w->buffer));
7835 }
7836
7837 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
7838
7839 /* Compute the max. number of lines specified by the user. */
7840 if (FLOATP (Vmax_mini_window_height))
7841 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_LINES (f);
7842 else if (INTEGERP (Vmax_mini_window_height))
7843 max_height = XINT (Vmax_mini_window_height);
7844 else
7845 max_height = total_height / 4;
7846
7847 /* Correct that max. height if it's bogus. */
7848 max_height = max (1, max_height);
7849 max_height = min (total_height, max_height);
7850
7851 /* Find out the height of the text in the window. */
7852 if (it.truncate_lines_p)
7853 height = 1;
7854 else
7855 {
7856 last_height = 0;
7857 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
7858 if (it.max_ascent == 0 && it.max_descent == 0)
7859 height = it.current_y + last_height;
7860 else
7861 height = it.current_y + it.max_ascent + it.max_descent;
7862 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
7863 height = (height + unit - 1) / unit;
7864 }
7865
7866 /* Compute a suitable window start. */
7867 if (height > max_height)
7868 {
7869 height = max_height;
7870 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
7871 move_it_vertically_backward (&it, (height - 1) * unit);
7872 start = it.current.pos;
7873 }
7874 else
7875 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
7876 SET_MARKER_FROM_TEXT_POS (w->start, start);
7877
7878 if (EQ (Vresize_mini_windows, Qgrow_only))
7879 {
7880 /* Let it grow only, until we display an empty message, in which
7881 case the window shrinks again. */
7882 if (height > WINDOW_TOTAL_LINES (w))
7883 {
7884 int old_height = WINDOW_TOTAL_LINES (w);
7885 freeze_window_starts (f, 1);
7886 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
7887 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
7888 }
7889 else if (height < WINDOW_TOTAL_LINES (w)
7890 && (exact_p || BEGV == ZV))
7891 {
7892 int old_height = WINDOW_TOTAL_LINES (w);
7893 freeze_window_starts (f, 0);
7894 shrink_mini_window (w);
7895 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
7896 }
7897 }
7898 else
7899 {
7900 /* Always resize to exact size needed. */
7901 if (height > WINDOW_TOTAL_LINES (w))
7902 {
7903 int old_height = WINDOW_TOTAL_LINES (w);
7904 freeze_window_starts (f, 1);
7905 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
7906 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
7907 }
7908 else if (height < WINDOW_TOTAL_LINES (w))
7909 {
7910 int old_height = WINDOW_TOTAL_LINES (w);
7911 freeze_window_starts (f, 0);
7912 shrink_mini_window (w);
7913
7914 if (height)
7915 {
7916 freeze_window_starts (f, 1);
7917 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
7918 }
7919
7920 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
7921 }
7922 }
7923
7924 if (old_current_buffer)
7925 set_buffer_internal (old_current_buffer);
7926 }
7927
7928 return window_height_changed_p;
7929 }
7930
7931
7932 /* Value is the current message, a string, or nil if there is no
7933 current message. */
7934
7935 Lisp_Object
7936 current_message ()
7937 {
7938 Lisp_Object msg;
7939
7940 if (NILP (echo_area_buffer[0]))
7941 msg = Qnil;
7942 else
7943 {
7944 with_echo_area_buffer (0, 0, current_message_1,
7945 (EMACS_INT) &msg, Qnil, 0, 0);
7946 if (NILP (msg))
7947 echo_area_buffer[0] = Qnil;
7948 }
7949
7950 return msg;
7951 }
7952
7953
7954 static int
7955 current_message_1 (a1, a2, a3, a4)
7956 EMACS_INT a1;
7957 Lisp_Object a2;
7958 EMACS_INT a3, a4;
7959 {
7960 Lisp_Object *msg = (Lisp_Object *) a1;
7961
7962 if (Z > BEG)
7963 *msg = make_buffer_string (BEG, Z, 1);
7964 else
7965 *msg = Qnil;
7966 return 0;
7967 }
7968
7969
7970 /* Push the current message on Vmessage_stack for later restauration
7971 by restore_message. Value is non-zero if the current message isn't
7972 empty. This is a relatively infrequent operation, so it's not
7973 worth optimizing. */
7974
7975 int
7976 push_message ()
7977 {
7978 Lisp_Object msg;
7979 msg = current_message ();
7980 Vmessage_stack = Fcons (msg, Vmessage_stack);
7981 return STRINGP (msg);
7982 }
7983
7984
7985 /* Restore message display from the top of Vmessage_stack. */
7986
7987 void
7988 restore_message ()
7989 {
7990 Lisp_Object msg;
7991
7992 xassert (CONSP (Vmessage_stack));
7993 msg = XCAR (Vmessage_stack);
7994 if (STRINGP (msg))
7995 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
7996 else
7997 message3_nolog (msg, 0, 0);
7998 }
7999
8000
8001 /* Handler for record_unwind_protect calling pop_message. */
8002
8003 Lisp_Object
8004 pop_message_unwind (dummy)
8005 Lisp_Object dummy;
8006 {
8007 pop_message ();
8008 return Qnil;
8009 }
8010
8011 /* Pop the top-most entry off Vmessage_stack. */
8012
8013 void
8014 pop_message ()
8015 {
8016 xassert (CONSP (Vmessage_stack));
8017 Vmessage_stack = XCDR (Vmessage_stack);
8018 }
8019
8020
8021 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
8022 exits. If the stack is not empty, we have a missing pop_message
8023 somewhere. */
8024
8025 void
8026 check_message_stack ()
8027 {
8028 if (!NILP (Vmessage_stack))
8029 abort ();
8030 }
8031
8032
8033 /* Truncate to NCHARS what will be displayed in the echo area the next
8034 time we display it---but don't redisplay it now. */
8035
8036 void
8037 truncate_echo_area (nchars)
8038 int nchars;
8039 {
8040 if (nchars == 0)
8041 echo_area_buffer[0] = Qnil;
8042 /* A null message buffer means that the frame hasn't really been
8043 initialized yet. Error messages get reported properly by
8044 cmd_error, so this must be just an informative message; toss it. */
8045 else if (!noninteractive
8046 && INTERACTIVE
8047 && !NILP (echo_area_buffer[0]))
8048 {
8049 struct frame *sf = SELECTED_FRAME ();
8050 if (FRAME_MESSAGE_BUF (sf))
8051 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
8052 }
8053 }
8054
8055
8056 /* Helper function for truncate_echo_area. Truncate the current
8057 message to at most NCHARS characters. */
8058
8059 static int
8060 truncate_message_1 (nchars, a2, a3, a4)
8061 EMACS_INT nchars;
8062 Lisp_Object a2;
8063 EMACS_INT a3, a4;
8064 {
8065 if (BEG + nchars < Z)
8066 del_range (BEG + nchars, Z);
8067 if (Z == BEG)
8068 echo_area_buffer[0] = Qnil;
8069 return 0;
8070 }
8071
8072
8073 /* Set the current message to a substring of S or STRING.
8074
8075 If STRING is a Lisp string, set the message to the first NBYTES
8076 bytes from STRING. NBYTES zero means use the whole string. If
8077 STRING is multibyte, the message will be displayed multibyte.
8078
8079 If S is not null, set the message to the first LEN bytes of S. LEN
8080 zero means use the whole string. MULTIBYTE_P non-zero means S is
8081 multibyte. Display the message multibyte in that case. */
8082
8083 void
8084 set_message (s, string, nbytes, multibyte_p)
8085 const char *s;
8086 Lisp_Object string;
8087 int nbytes, multibyte_p;
8088 {
8089 message_enable_multibyte
8090 = ((s && multibyte_p)
8091 || (STRINGP (string) && STRING_MULTIBYTE (string)));
8092
8093 with_echo_area_buffer (0, 0, set_message_1,
8094 (EMACS_INT) s, string, nbytes, multibyte_p);
8095 message_buf_print = 0;
8096 help_echo_showing_p = 0;
8097 }
8098
8099
8100 /* Helper function for set_message. Arguments have the same meaning
8101 as there, with A1 corresponding to S and A2 corresponding to STRING
8102 This function is called with the echo area buffer being
8103 current. */
8104
8105 static int
8106 set_message_1 (a1, a2, nbytes, multibyte_p)
8107 EMACS_INT a1;
8108 Lisp_Object a2;
8109 EMACS_INT nbytes, multibyte_p;
8110 {
8111 const char *s = (const char *) a1;
8112 Lisp_Object string = a2;
8113
8114 /* Change multibyteness of the echo buffer appropriately. */
8115 if (message_enable_multibyte
8116 != !NILP (current_buffer->enable_multibyte_characters))
8117 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
8118
8119 current_buffer->truncate_lines = message_truncate_lines ? Qt : Qnil;
8120
8121 /* Insert new message at BEG. */
8122 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
8123 Ferase_buffer ();
8124
8125 if (STRINGP (string))
8126 {
8127 int nchars;
8128
8129 if (nbytes == 0)
8130 nbytes = SBYTES (string);
8131 nchars = string_byte_to_char (string, nbytes);
8132
8133 /* This function takes care of single/multibyte conversion. We
8134 just have to ensure that the echo area buffer has the right
8135 setting of enable_multibyte_characters. */
8136 insert_from_string (string, 0, 0, nchars, nbytes, 1);
8137 }
8138 else if (s)
8139 {
8140 if (nbytes == 0)
8141 nbytes = strlen (s);
8142
8143 if (multibyte_p && NILP (current_buffer->enable_multibyte_characters))
8144 {
8145 /* Convert from multi-byte to single-byte. */
8146 int i, c, n;
8147 unsigned char work[1];
8148
8149 /* Convert a multibyte string to single-byte. */
8150 for (i = 0; i < nbytes; i += n)
8151 {
8152 c = string_char_and_length (s + i, nbytes - i, &n);
8153 work[0] = (ASCII_CHAR_P (c)
8154 ? c
8155 : multibyte_char_to_unibyte (c, Qnil));
8156 insert_1_both (work, 1, 1, 1, 0, 0);
8157 }
8158 }
8159 else if (!multibyte_p
8160 && !NILP (current_buffer->enable_multibyte_characters))
8161 {
8162 /* Convert from single-byte to multi-byte. */
8163 int i, c, n;
8164 const unsigned char *msg = (const unsigned char *) s;
8165 unsigned char str[MAX_MULTIBYTE_LENGTH];
8166
8167 /* Convert a single-byte string to multibyte. */
8168 for (i = 0; i < nbytes; i++)
8169 {
8170 c = msg[i];
8171 c = unibyte_char_to_multibyte (c);
8172 n = CHAR_STRING (c, str);
8173 insert_1_both (str, 1, n, 1, 0, 0);
8174 }
8175 }
8176 else
8177 insert_1 (s, nbytes, 1, 0, 0);
8178 }
8179
8180 return 0;
8181 }
8182
8183
8184 /* Clear messages. CURRENT_P non-zero means clear the current
8185 message. LAST_DISPLAYED_P non-zero means clear the message
8186 last displayed. */
8187
8188 void
8189 clear_message (current_p, last_displayed_p)
8190 int current_p, last_displayed_p;
8191 {
8192 if (current_p)
8193 {
8194 echo_area_buffer[0] = Qnil;
8195 message_cleared_p = 1;
8196 }
8197
8198 if (last_displayed_p)
8199 echo_area_buffer[1] = Qnil;
8200
8201 message_buf_print = 0;
8202 }
8203
8204 /* Clear garbaged frames.
8205
8206 This function is used where the old redisplay called
8207 redraw_garbaged_frames which in turn called redraw_frame which in
8208 turn called clear_frame. The call to clear_frame was a source of
8209 flickering. I believe a clear_frame is not necessary. It should
8210 suffice in the new redisplay to invalidate all current matrices,
8211 and ensure a complete redisplay of all windows. */
8212
8213 static void
8214 clear_garbaged_frames ()
8215 {
8216 if (frame_garbaged)
8217 {
8218 Lisp_Object tail, frame;
8219 int changed_count = 0;
8220
8221 FOR_EACH_FRAME (tail, frame)
8222 {
8223 struct frame *f = XFRAME (frame);
8224
8225 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
8226 {
8227 if (f->resized_p)
8228 {
8229 Fredraw_frame (frame);
8230 f->force_flush_display_p = 1;
8231 }
8232 clear_current_matrices (f);
8233 changed_count++;
8234 f->garbaged = 0;
8235 f->resized_p = 0;
8236 }
8237 }
8238
8239 frame_garbaged = 0;
8240 if (changed_count)
8241 ++windows_or_buffers_changed;
8242 }
8243 }
8244
8245
8246 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
8247 is non-zero update selected_frame. Value is non-zero if the
8248 mini-windows height has been changed. */
8249
8250 static int
8251 echo_area_display (update_frame_p)
8252 int update_frame_p;
8253 {
8254 Lisp_Object mini_window;
8255 struct window *w;
8256 struct frame *f;
8257 int window_height_changed_p = 0;
8258 struct frame *sf = SELECTED_FRAME ();
8259
8260 mini_window = FRAME_MINIBUF_WINDOW (sf);
8261 w = XWINDOW (mini_window);
8262 f = XFRAME (WINDOW_FRAME (w));
8263
8264 /* Don't display if frame is invisible or not yet initialized. */
8265 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
8266 return 0;
8267
8268 /* The terminal frame is used as the first Emacs frame on the Mac OS. */
8269 #ifndef MAC_OS8
8270 #ifdef HAVE_WINDOW_SYSTEM
8271 /* When Emacs starts, selected_frame may be a visible terminal
8272 frame, even if we run under a window system. If we let this
8273 through, a message would be displayed on the terminal. */
8274 if (EQ (selected_frame, Vterminal_frame)
8275 && !NILP (Vwindow_system))
8276 return 0;
8277 #endif /* HAVE_WINDOW_SYSTEM */
8278 #endif
8279
8280 /* Redraw garbaged frames. */
8281 if (frame_garbaged)
8282 clear_garbaged_frames ();
8283
8284 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
8285 {
8286 echo_area_window = mini_window;
8287 window_height_changed_p = display_echo_area (w);
8288 w->must_be_updated_p = 1;
8289
8290 /* Update the display, unless called from redisplay_internal.
8291 Also don't update the screen during redisplay itself. The
8292 update will happen at the end of redisplay, and an update
8293 here could cause confusion. */
8294 if (update_frame_p && !redisplaying_p)
8295 {
8296 int n = 0;
8297
8298 /* If the display update has been interrupted by pending
8299 input, update mode lines in the frame. Due to the
8300 pending input, it might have been that redisplay hasn't
8301 been called, so that mode lines above the echo area are
8302 garbaged. This looks odd, so we prevent it here. */
8303 if (!display_completed)
8304 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
8305
8306 if (window_height_changed_p
8307 /* Don't do this if Emacs is shutting down. Redisplay
8308 needs to run hooks. */
8309 && !NILP (Vrun_hooks))
8310 {
8311 /* Must update other windows. Likewise as in other
8312 cases, don't let this update be interrupted by
8313 pending input. */
8314 int count = SPECPDL_INDEX ();
8315 specbind (Qredisplay_dont_pause, Qt);
8316 windows_or_buffers_changed = 1;
8317 redisplay_internal (0);
8318 unbind_to (count, Qnil);
8319 }
8320 else if (FRAME_WINDOW_P (f) && n == 0)
8321 {
8322 /* Window configuration is the same as before.
8323 Can do with a display update of the echo area,
8324 unless we displayed some mode lines. */
8325 update_single_window (w, 1);
8326 rif->flush_display (f);
8327 }
8328 else
8329 update_frame (f, 1, 1);
8330
8331 /* If cursor is in the echo area, make sure that the next
8332 redisplay displays the minibuffer, so that the cursor will
8333 be replaced with what the minibuffer wants. */
8334 if (cursor_in_echo_area)
8335 ++windows_or_buffers_changed;
8336 }
8337 }
8338 else if (!EQ (mini_window, selected_window))
8339 windows_or_buffers_changed++;
8340
8341 /* The current message is now also the last one displayed. */
8342 echo_area_buffer[1] = echo_area_buffer[0];
8343
8344 /* Prevent redisplay optimization in redisplay_internal by resetting
8345 this_line_start_pos. This is done because the mini-buffer now
8346 displays the message instead of its buffer text. */
8347 if (EQ (mini_window, selected_window))
8348 CHARPOS (this_line_start_pos) = 0;
8349
8350 return window_height_changed_p;
8351 }
8352
8353
8354 \f
8355 /***********************************************************************
8356 Mode Lines and Frame Titles
8357 ***********************************************************************/
8358
8359 /* A buffer for constructing non-propertized mode-line strings and
8360 frame titles in it; allocated from the heap in init_xdisp and
8361 resized as needed in store_mode_line_noprop_char. */
8362
8363 static char *mode_line_noprop_buf;
8364
8365 /* The buffer's end, and a current output position in it. */
8366
8367 static char *mode_line_noprop_buf_end;
8368 static char *mode_line_noprop_ptr;
8369
8370 #define MODE_LINE_NOPROP_LEN(start) \
8371 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
8372
8373 static enum {
8374 MODE_LINE_DISPLAY = 0,
8375 MODE_LINE_TITLE,
8376 MODE_LINE_NOPROP,
8377 MODE_LINE_STRING
8378 } mode_line_target;
8379
8380 /* Alist that caches the results of :propertize.
8381 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
8382 static Lisp_Object mode_line_proptrans_alist;
8383
8384 /* List of strings making up the mode-line. */
8385 static Lisp_Object mode_line_string_list;
8386
8387 /* Base face property when building propertized mode line string. */
8388 static Lisp_Object mode_line_string_face;
8389 static Lisp_Object mode_line_string_face_prop;
8390
8391
8392 /* Unwind data for mode line strings */
8393
8394 static Lisp_Object Vmode_line_unwind_vector;
8395
8396 static Lisp_Object
8397 format_mode_line_unwind_data (obuf)
8398 struct buffer *obuf;
8399 {
8400 Lisp_Object vector;
8401
8402 /* Reduce consing by keeping one vector in
8403 Vwith_echo_area_save_vector. */
8404 vector = Vmode_line_unwind_vector;
8405 Vmode_line_unwind_vector = Qnil;
8406
8407 if (NILP (vector))
8408 vector = Fmake_vector (make_number (7), Qnil);
8409
8410 AREF (vector, 0) = make_number (mode_line_target);
8411 AREF (vector, 1) = make_number (MODE_LINE_NOPROP_LEN (0));
8412 AREF (vector, 2) = mode_line_string_list;
8413 AREF (vector, 3) = mode_line_proptrans_alist;
8414 AREF (vector, 4) = mode_line_string_face;
8415 AREF (vector, 5) = mode_line_string_face_prop;
8416
8417 if (obuf)
8418 XSETBUFFER (AREF (vector, 6), obuf);
8419 else
8420 AREF (vector, 6) = Qnil;
8421
8422 return vector;
8423 }
8424
8425 static Lisp_Object
8426 unwind_format_mode_line (vector)
8427 Lisp_Object vector;
8428 {
8429 mode_line_target = XINT (AREF (vector, 0));
8430 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
8431 mode_line_string_list = AREF (vector, 2);
8432 mode_line_proptrans_alist = AREF (vector, 3);
8433 mode_line_string_face = AREF (vector, 4);
8434 mode_line_string_face_prop = AREF (vector, 5);
8435
8436 if (!NILP (AREF (vector, 6)))
8437 {
8438 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
8439 AREF (vector, 6) = Qnil;
8440 }
8441
8442 Vmode_line_unwind_vector = vector;
8443 return Qnil;
8444 }
8445
8446
8447 /* Store a single character C for the frame title in mode_line_noprop_buf.
8448 Re-allocate mode_line_noprop_buf if necessary. */
8449
8450 static void
8451 #ifdef PROTOTYPES
8452 store_mode_line_noprop_char (char c)
8453 #else
8454 store_mode_line_noprop_char (c)
8455 char c;
8456 #endif
8457 {
8458 /* If output position has reached the end of the allocated buffer,
8459 double the buffer's size. */
8460 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
8461 {
8462 int len = MODE_LINE_NOPROP_LEN (0);
8463 int new_size = 2 * len * sizeof *mode_line_noprop_buf;
8464 mode_line_noprop_buf = (char *) xrealloc (mode_line_noprop_buf, new_size);
8465 mode_line_noprop_buf_end = mode_line_noprop_buf + new_size;
8466 mode_line_noprop_ptr = mode_line_noprop_buf + len;
8467 }
8468
8469 *mode_line_noprop_ptr++ = c;
8470 }
8471
8472
8473 /* Store part of a frame title in mode_line_noprop_buf, beginning at
8474 mode_line_noprop_ptr. STR is the string to store. Do not copy
8475 characters that yield more columns than PRECISION; PRECISION <= 0
8476 means copy the whole string. Pad with spaces until FIELD_WIDTH
8477 number of characters have been copied; FIELD_WIDTH <= 0 means don't
8478 pad. Called from display_mode_element when it is used to build a
8479 frame title. */
8480
8481 static int
8482 store_mode_line_noprop (str, field_width, precision)
8483 const unsigned char *str;
8484 int field_width, precision;
8485 {
8486 int n = 0;
8487 int dummy, nbytes;
8488
8489 /* Copy at most PRECISION chars from STR. */
8490 nbytes = strlen (str);
8491 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
8492 while (nbytes--)
8493 store_mode_line_noprop_char (*str++);
8494
8495 /* Fill up with spaces until FIELD_WIDTH reached. */
8496 while (field_width > 0
8497 && n < field_width)
8498 {
8499 store_mode_line_noprop_char (' ');
8500 ++n;
8501 }
8502
8503 return n;
8504 }
8505
8506 /***********************************************************************
8507 Frame Titles
8508 ***********************************************************************/
8509
8510 #ifdef HAVE_WINDOW_SYSTEM
8511
8512 /* Set the title of FRAME, if it has changed. The title format is
8513 Vicon_title_format if FRAME is iconified, otherwise it is
8514 frame_title_format. */
8515
8516 static void
8517 x_consider_frame_title (frame)
8518 Lisp_Object frame;
8519 {
8520 struct frame *f = XFRAME (frame);
8521
8522 if (FRAME_WINDOW_P (f)
8523 || FRAME_MINIBUF_ONLY_P (f)
8524 || f->explicit_name)
8525 {
8526 /* Do we have more than one visible frame on this X display? */
8527 Lisp_Object tail;
8528 Lisp_Object fmt;
8529 int title_start;
8530 char *title;
8531 int len;
8532 struct it it;
8533 int count = SPECPDL_INDEX ();
8534
8535 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
8536 {
8537 Lisp_Object other_frame = XCAR (tail);
8538 struct frame *tf = XFRAME (other_frame);
8539
8540 if (tf != f
8541 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
8542 && !FRAME_MINIBUF_ONLY_P (tf)
8543 && !EQ (other_frame, tip_frame)
8544 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
8545 break;
8546 }
8547
8548 /* Set global variable indicating that multiple frames exist. */
8549 multiple_frames = CONSP (tail);
8550
8551 /* Switch to the buffer of selected window of the frame. Set up
8552 mode_line_target so that display_mode_element will output into
8553 mode_line_noprop_buf; then display the title. */
8554 record_unwind_protect (unwind_format_mode_line,
8555 format_mode_line_unwind_data (current_buffer));
8556
8557 set_buffer_internal_1 (XBUFFER (XWINDOW (f->selected_window)->buffer));
8558 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
8559
8560 mode_line_target = MODE_LINE_TITLE;
8561 title_start = MODE_LINE_NOPROP_LEN (0);
8562 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
8563 NULL, DEFAULT_FACE_ID);
8564 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
8565 len = MODE_LINE_NOPROP_LEN (title_start);
8566 title = mode_line_noprop_buf + title_start;
8567 unbind_to (count, Qnil);
8568
8569 /* Set the title only if it's changed. This avoids consing in
8570 the common case where it hasn't. (If it turns out that we've
8571 already wasted too much time by walking through the list with
8572 display_mode_element, then we might need to optimize at a
8573 higher level than this.) */
8574 if (! STRINGP (f->name)
8575 || SBYTES (f->name) != len
8576 || bcmp (title, SDATA (f->name), len) != 0)
8577 x_implicitly_set_name (f, make_string (title, len), Qnil);
8578 }
8579 }
8580
8581 #endif /* not HAVE_WINDOW_SYSTEM */
8582
8583
8584
8585 \f
8586 /***********************************************************************
8587 Menu Bars
8588 ***********************************************************************/
8589
8590
8591 /* Prepare for redisplay by updating menu-bar item lists when
8592 appropriate. This can call eval. */
8593
8594 void
8595 prepare_menu_bars ()
8596 {
8597 int all_windows;
8598 struct gcpro gcpro1, gcpro2;
8599 struct frame *f;
8600 Lisp_Object tooltip_frame;
8601
8602 #ifdef HAVE_WINDOW_SYSTEM
8603 tooltip_frame = tip_frame;
8604 #else
8605 tooltip_frame = Qnil;
8606 #endif
8607
8608 /* Update all frame titles based on their buffer names, etc. We do
8609 this before the menu bars so that the buffer-menu will show the
8610 up-to-date frame titles. */
8611 #ifdef HAVE_WINDOW_SYSTEM
8612 if (windows_or_buffers_changed || update_mode_lines)
8613 {
8614 Lisp_Object tail, frame;
8615
8616 FOR_EACH_FRAME (tail, frame)
8617 {
8618 f = XFRAME (frame);
8619 if (!EQ (frame, tooltip_frame)
8620 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
8621 x_consider_frame_title (frame);
8622 }
8623 }
8624 #endif /* HAVE_WINDOW_SYSTEM */
8625
8626 /* Update the menu bar item lists, if appropriate. This has to be
8627 done before any actual redisplay or generation of display lines. */
8628 all_windows = (update_mode_lines
8629 || buffer_shared > 1
8630 || windows_or_buffers_changed);
8631 if (all_windows)
8632 {
8633 Lisp_Object tail, frame;
8634 int count = SPECPDL_INDEX ();
8635
8636 record_unwind_save_match_data ();
8637
8638 FOR_EACH_FRAME (tail, frame)
8639 {
8640 f = XFRAME (frame);
8641
8642 /* Ignore tooltip frame. */
8643 if (EQ (frame, tooltip_frame))
8644 continue;
8645
8646 /* If a window on this frame changed size, report that to
8647 the user and clear the size-change flag. */
8648 if (FRAME_WINDOW_SIZES_CHANGED (f))
8649 {
8650 Lisp_Object functions;
8651
8652 /* Clear flag first in case we get an error below. */
8653 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
8654 functions = Vwindow_size_change_functions;
8655 GCPRO2 (tail, functions);
8656
8657 while (CONSP (functions))
8658 {
8659 call1 (XCAR (functions), frame);
8660 functions = XCDR (functions);
8661 }
8662 UNGCPRO;
8663 }
8664
8665 GCPRO1 (tail);
8666 update_menu_bar (f, 0);
8667 #ifdef HAVE_WINDOW_SYSTEM
8668 update_tool_bar (f, 0);
8669 #endif
8670 UNGCPRO;
8671 }
8672
8673 unbind_to (count, Qnil);
8674 }
8675 else
8676 {
8677 struct frame *sf = SELECTED_FRAME ();
8678 update_menu_bar (sf, 1);
8679 #ifdef HAVE_WINDOW_SYSTEM
8680 update_tool_bar (sf, 1);
8681 #endif
8682 }
8683
8684 /* Motif needs this. See comment in xmenu.c. Turn it off when
8685 pending_menu_activation is not defined. */
8686 #ifdef USE_X_TOOLKIT
8687 pending_menu_activation = 0;
8688 #endif
8689 }
8690
8691
8692 /* Update the menu bar item list for frame F. This has to be done
8693 before we start to fill in any display lines, because it can call
8694 eval.
8695
8696 If SAVE_MATCH_DATA is non-zero, we must save and restore it here. */
8697
8698 static void
8699 update_menu_bar (f, save_match_data)
8700 struct frame *f;
8701 int save_match_data;
8702 {
8703 Lisp_Object window;
8704 register struct window *w;
8705
8706 /* If called recursively during a menu update, do nothing. This can
8707 happen when, for instance, an activate-menubar-hook causes a
8708 redisplay. */
8709 if (inhibit_menubar_update)
8710 return;
8711
8712 window = FRAME_SELECTED_WINDOW (f);
8713 w = XWINDOW (window);
8714
8715 #if 0 /* The if statement below this if statement used to include the
8716 condition !NILP (w->update_mode_line), rather than using
8717 update_mode_lines directly, and this if statement may have
8718 been added to make that condition work. Now the if
8719 statement below matches its comment, this isn't needed. */
8720 if (update_mode_lines)
8721 w->update_mode_line = Qt;
8722 #endif
8723
8724 if (FRAME_WINDOW_P (f)
8725 ?
8726 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
8727 || defined (USE_GTK)
8728 FRAME_EXTERNAL_MENU_BAR (f)
8729 #else
8730 FRAME_MENU_BAR_LINES (f) > 0
8731 #endif
8732 : FRAME_MENU_BAR_LINES (f) > 0)
8733 {
8734 /* If the user has switched buffers or windows, we need to
8735 recompute to reflect the new bindings. But we'll
8736 recompute when update_mode_lines is set too; that means
8737 that people can use force-mode-line-update to request
8738 that the menu bar be recomputed. The adverse effect on
8739 the rest of the redisplay algorithm is about the same as
8740 windows_or_buffers_changed anyway. */
8741 if (windows_or_buffers_changed
8742 /* This used to test w->update_mode_line, but we believe
8743 there is no need to recompute the menu in that case. */
8744 || update_mode_lines
8745 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
8746 < BUF_MODIFF (XBUFFER (w->buffer)))
8747 != !NILP (w->last_had_star))
8748 || ((!NILP (Vtransient_mark_mode)
8749 && !NILP (XBUFFER (w->buffer)->mark_active))
8750 != !NILP (w->region_showing)))
8751 {
8752 struct buffer *prev = current_buffer;
8753 int count = SPECPDL_INDEX ();
8754
8755 specbind (Qinhibit_menubar_update, Qt);
8756
8757 set_buffer_internal_1 (XBUFFER (w->buffer));
8758 if (save_match_data)
8759 record_unwind_save_match_data ();
8760 if (NILP (Voverriding_local_map_menu_flag))
8761 {
8762 specbind (Qoverriding_terminal_local_map, Qnil);
8763 specbind (Qoverriding_local_map, Qnil);
8764 }
8765
8766 /* Run the Lucid hook. */
8767 safe_run_hooks (Qactivate_menubar_hook);
8768
8769 /* If it has changed current-menubar from previous value,
8770 really recompute the menu-bar from the value. */
8771 if (! NILP (Vlucid_menu_bar_dirty_flag))
8772 call0 (Qrecompute_lucid_menubar);
8773
8774 safe_run_hooks (Qmenu_bar_update_hook);
8775 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
8776
8777 /* Redisplay the menu bar in case we changed it. */
8778 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
8779 || defined (USE_GTK)
8780 if (FRAME_WINDOW_P (f)
8781 #if defined (MAC_OS)
8782 /* All frames on Mac OS share the same menubar. So only the
8783 selected frame should be allowed to set it. */
8784 && f == SELECTED_FRAME ()
8785 #endif
8786 )
8787 set_frame_menubar (f, 0, 0);
8788 else
8789 /* On a terminal screen, the menu bar is an ordinary screen
8790 line, and this makes it get updated. */
8791 w->update_mode_line = Qt;
8792 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || MAC_OS || USE_GTK) */
8793 /* In the non-toolkit version, the menu bar is an ordinary screen
8794 line, and this makes it get updated. */
8795 w->update_mode_line = Qt;
8796 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || MAC_OS || USE_GTK) */
8797
8798 unbind_to (count, Qnil);
8799 set_buffer_internal_1 (prev);
8800 }
8801 }
8802 }
8803
8804
8805 \f
8806 /***********************************************************************
8807 Output Cursor
8808 ***********************************************************************/
8809
8810 #ifdef HAVE_WINDOW_SYSTEM
8811
8812 /* EXPORT:
8813 Nominal cursor position -- where to draw output.
8814 HPOS and VPOS are window relative glyph matrix coordinates.
8815 X and Y are window relative pixel coordinates. */
8816
8817 struct cursor_pos output_cursor;
8818
8819
8820 /* EXPORT:
8821 Set the global variable output_cursor to CURSOR. All cursor
8822 positions are relative to updated_window. */
8823
8824 void
8825 set_output_cursor (cursor)
8826 struct cursor_pos *cursor;
8827 {
8828 output_cursor.hpos = cursor->hpos;
8829 output_cursor.vpos = cursor->vpos;
8830 output_cursor.x = cursor->x;
8831 output_cursor.y = cursor->y;
8832 }
8833
8834
8835 /* EXPORT for RIF:
8836 Set a nominal cursor position.
8837
8838 HPOS and VPOS are column/row positions in a window glyph matrix. X
8839 and Y are window text area relative pixel positions.
8840
8841 If this is done during an update, updated_window will contain the
8842 window that is being updated and the position is the future output
8843 cursor position for that window. If updated_window is null, use
8844 selected_window and display the cursor at the given position. */
8845
8846 void
8847 x_cursor_to (vpos, hpos, y, x)
8848 int vpos, hpos, y, x;
8849 {
8850 struct window *w;
8851
8852 /* If updated_window is not set, work on selected_window. */
8853 if (updated_window)
8854 w = updated_window;
8855 else
8856 w = XWINDOW (selected_window);
8857
8858 /* Set the output cursor. */
8859 output_cursor.hpos = hpos;
8860 output_cursor.vpos = vpos;
8861 output_cursor.x = x;
8862 output_cursor.y = y;
8863
8864 /* If not called as part of an update, really display the cursor.
8865 This will also set the cursor position of W. */
8866 if (updated_window == NULL)
8867 {
8868 BLOCK_INPUT;
8869 display_and_set_cursor (w, 1, hpos, vpos, x, y);
8870 if (rif->flush_display_optional)
8871 rif->flush_display_optional (SELECTED_FRAME ());
8872 UNBLOCK_INPUT;
8873 }
8874 }
8875
8876 #endif /* HAVE_WINDOW_SYSTEM */
8877
8878 \f
8879 /***********************************************************************
8880 Tool-bars
8881 ***********************************************************************/
8882
8883 #ifdef HAVE_WINDOW_SYSTEM
8884
8885 /* Where the mouse was last time we reported a mouse event. */
8886
8887 FRAME_PTR last_mouse_frame;
8888
8889 /* Tool-bar item index of the item on which a mouse button was pressed
8890 or -1. */
8891
8892 int last_tool_bar_item;
8893
8894
8895 /* Update the tool-bar item list for frame F. This has to be done
8896 before we start to fill in any display lines. Called from
8897 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
8898 and restore it here. */
8899
8900 static void
8901 update_tool_bar (f, save_match_data)
8902 struct frame *f;
8903 int save_match_data;
8904 {
8905 #ifdef USE_GTK
8906 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
8907 #else
8908 int do_update = WINDOWP (f->tool_bar_window)
8909 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0;
8910 #endif
8911
8912 if (do_update)
8913 {
8914 Lisp_Object window;
8915 struct window *w;
8916
8917 window = FRAME_SELECTED_WINDOW (f);
8918 w = XWINDOW (window);
8919
8920 /* If the user has switched buffers or windows, we need to
8921 recompute to reflect the new bindings. But we'll
8922 recompute when update_mode_lines is set too; that means
8923 that people can use force-mode-line-update to request
8924 that the menu bar be recomputed. The adverse effect on
8925 the rest of the redisplay algorithm is about the same as
8926 windows_or_buffers_changed anyway. */
8927 if (windows_or_buffers_changed
8928 || !NILP (w->update_mode_line)
8929 || update_mode_lines
8930 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
8931 < BUF_MODIFF (XBUFFER (w->buffer)))
8932 != !NILP (w->last_had_star))
8933 || ((!NILP (Vtransient_mark_mode)
8934 && !NILP (XBUFFER (w->buffer)->mark_active))
8935 != !NILP (w->region_showing)))
8936 {
8937 struct buffer *prev = current_buffer;
8938 int count = SPECPDL_INDEX ();
8939 Lisp_Object new_tool_bar;
8940 int new_n_tool_bar;
8941 struct gcpro gcpro1;
8942
8943 /* Set current_buffer to the buffer of the selected
8944 window of the frame, so that we get the right local
8945 keymaps. */
8946 set_buffer_internal_1 (XBUFFER (w->buffer));
8947
8948 /* Save match data, if we must. */
8949 if (save_match_data)
8950 record_unwind_save_match_data ();
8951
8952 /* Make sure that we don't accidentally use bogus keymaps. */
8953 if (NILP (Voverriding_local_map_menu_flag))
8954 {
8955 specbind (Qoverriding_terminal_local_map, Qnil);
8956 specbind (Qoverriding_local_map, Qnil);
8957 }
8958
8959 GCPRO1 (new_tool_bar);
8960
8961 /* Build desired tool-bar items from keymaps. */
8962 new_tool_bar = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
8963 &new_n_tool_bar);
8964
8965 /* Redisplay the tool-bar if we changed it. */
8966 if (NILP (Fequal (new_tool_bar, f->tool_bar_items)))
8967 {
8968 /* Redisplay that happens asynchronously due to an expose event
8969 may access f->tool_bar_items. Make sure we update both
8970 variables within BLOCK_INPUT so no such event interrupts. */
8971 BLOCK_INPUT;
8972 f->tool_bar_items = new_tool_bar;
8973 f->n_tool_bar_items = new_n_tool_bar;
8974 w->update_mode_line = Qt;
8975 UNBLOCK_INPUT;
8976 }
8977
8978 UNGCPRO;
8979
8980 unbind_to (count, Qnil);
8981 set_buffer_internal_1 (prev);
8982 }
8983 }
8984 }
8985
8986
8987 /* Set F->desired_tool_bar_string to a Lisp string representing frame
8988 F's desired tool-bar contents. F->tool_bar_items must have
8989 been set up previously by calling prepare_menu_bars. */
8990
8991 static void
8992 build_desired_tool_bar_string (f)
8993 struct frame *f;
8994 {
8995 int i, size, size_needed;
8996 struct gcpro gcpro1, gcpro2, gcpro3;
8997 Lisp_Object image, plist, props;
8998
8999 image = plist = props = Qnil;
9000 GCPRO3 (image, plist, props);
9001
9002 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
9003 Otherwise, make a new string. */
9004
9005 /* The size of the string we might be able to reuse. */
9006 size = (STRINGP (f->desired_tool_bar_string)
9007 ? SCHARS (f->desired_tool_bar_string)
9008 : 0);
9009
9010 /* We need one space in the string for each image. */
9011 size_needed = f->n_tool_bar_items;
9012
9013 /* Reuse f->desired_tool_bar_string, if possible. */
9014 if (size < size_needed || NILP (f->desired_tool_bar_string))
9015 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
9016 make_number (' '));
9017 else
9018 {
9019 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
9020 Fremove_text_properties (make_number (0), make_number (size),
9021 props, f->desired_tool_bar_string);
9022 }
9023
9024 /* Put a `display' property on the string for the images to display,
9025 put a `menu_item' property on tool-bar items with a value that
9026 is the index of the item in F's tool-bar item vector. */
9027 for (i = 0; i < f->n_tool_bar_items; ++i)
9028 {
9029 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
9030
9031 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
9032 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
9033 int hmargin, vmargin, relief, idx, end;
9034 extern Lisp_Object QCrelief, QCmargin, QCconversion;
9035
9036 /* If image is a vector, choose the image according to the
9037 button state. */
9038 image = PROP (TOOL_BAR_ITEM_IMAGES);
9039 if (VECTORP (image))
9040 {
9041 if (enabled_p)
9042 idx = (selected_p
9043 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
9044 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
9045 else
9046 idx = (selected_p
9047 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
9048 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
9049
9050 xassert (ASIZE (image) >= idx);
9051 image = AREF (image, idx);
9052 }
9053 else
9054 idx = -1;
9055
9056 /* Ignore invalid image specifications. */
9057 if (!valid_image_p (image))
9058 continue;
9059
9060 /* Display the tool-bar button pressed, or depressed. */
9061 plist = Fcopy_sequence (XCDR (image));
9062
9063 /* Compute margin and relief to draw. */
9064 relief = (tool_bar_button_relief >= 0
9065 ? tool_bar_button_relief
9066 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
9067 hmargin = vmargin = relief;
9068
9069 if (INTEGERP (Vtool_bar_button_margin)
9070 && XINT (Vtool_bar_button_margin) > 0)
9071 {
9072 hmargin += XFASTINT (Vtool_bar_button_margin);
9073 vmargin += XFASTINT (Vtool_bar_button_margin);
9074 }
9075 else if (CONSP (Vtool_bar_button_margin))
9076 {
9077 if (INTEGERP (XCAR (Vtool_bar_button_margin))
9078 && XINT (XCAR (Vtool_bar_button_margin)) > 0)
9079 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
9080
9081 if (INTEGERP (XCDR (Vtool_bar_button_margin))
9082 && XINT (XCDR (Vtool_bar_button_margin)) > 0)
9083 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
9084 }
9085
9086 if (auto_raise_tool_bar_buttons_p)
9087 {
9088 /* Add a `:relief' property to the image spec if the item is
9089 selected. */
9090 if (selected_p)
9091 {
9092 plist = Fplist_put (plist, QCrelief, make_number (-relief));
9093 hmargin -= relief;
9094 vmargin -= relief;
9095 }
9096 }
9097 else
9098 {
9099 /* If image is selected, display it pressed, i.e. with a
9100 negative relief. If it's not selected, display it with a
9101 raised relief. */
9102 plist = Fplist_put (plist, QCrelief,
9103 (selected_p
9104 ? make_number (-relief)
9105 : make_number (relief)));
9106 hmargin -= relief;
9107 vmargin -= relief;
9108 }
9109
9110 /* Put a margin around the image. */
9111 if (hmargin || vmargin)
9112 {
9113 if (hmargin == vmargin)
9114 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
9115 else
9116 plist = Fplist_put (plist, QCmargin,
9117 Fcons (make_number (hmargin),
9118 make_number (vmargin)));
9119 }
9120
9121 /* If button is not enabled, and we don't have special images
9122 for the disabled state, make the image appear disabled by
9123 applying an appropriate algorithm to it. */
9124 if (!enabled_p && idx < 0)
9125 plist = Fplist_put (plist, QCconversion, Qdisabled);
9126
9127 /* Put a `display' text property on the string for the image to
9128 display. Put a `menu-item' property on the string that gives
9129 the start of this item's properties in the tool-bar items
9130 vector. */
9131 image = Fcons (Qimage, plist);
9132 props = list4 (Qdisplay, image,
9133 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
9134
9135 /* Let the last image hide all remaining spaces in the tool bar
9136 string. The string can be longer than needed when we reuse a
9137 previous string. */
9138 if (i + 1 == f->n_tool_bar_items)
9139 end = SCHARS (f->desired_tool_bar_string);
9140 else
9141 end = i + 1;
9142 Fadd_text_properties (make_number (i), make_number (end),
9143 props, f->desired_tool_bar_string);
9144 #undef PROP
9145 }
9146
9147 UNGCPRO;
9148 }
9149
9150
9151 /* Display one line of the tool-bar of frame IT->f. */
9152
9153 static void
9154 display_tool_bar_line (it)
9155 struct it *it;
9156 {
9157 struct glyph_row *row = it->glyph_row;
9158 int max_x = it->last_visible_x;
9159 struct glyph *last;
9160
9161 prepare_desired_row (row);
9162 row->y = it->current_y;
9163
9164 /* Note that this isn't made use of if the face hasn't a box,
9165 so there's no need to check the face here. */
9166 it->start_of_box_run_p = 1;
9167
9168 while (it->current_x < max_x)
9169 {
9170 int x_before, x, n_glyphs_before, i, nglyphs;
9171
9172 /* Get the next display element. */
9173 if (!get_next_display_element (it))
9174 break;
9175
9176 /* Produce glyphs. */
9177 x_before = it->current_x;
9178 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
9179 PRODUCE_GLYPHS (it);
9180
9181 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
9182 i = 0;
9183 x = x_before;
9184 while (i < nglyphs)
9185 {
9186 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
9187
9188 if (x + glyph->pixel_width > max_x)
9189 {
9190 /* Glyph doesn't fit on line. */
9191 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
9192 it->current_x = x;
9193 goto out;
9194 }
9195
9196 ++it->hpos;
9197 x += glyph->pixel_width;
9198 ++i;
9199 }
9200
9201 /* Stop at line ends. */
9202 if (ITERATOR_AT_END_OF_LINE_P (it))
9203 break;
9204
9205 set_iterator_to_next (it, 1);
9206 }
9207
9208 out:;
9209
9210 row->displays_text_p = row->used[TEXT_AREA] != 0;
9211 extend_face_to_end_of_line (it);
9212 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
9213 last->right_box_line_p = 1;
9214 if (last == row->glyphs[TEXT_AREA])
9215 last->left_box_line_p = 1;
9216 compute_line_metrics (it);
9217
9218 /* If line is empty, make it occupy the rest of the tool-bar. */
9219 if (!row->displays_text_p)
9220 {
9221 row->height = row->phys_height = it->last_visible_y - row->y;
9222 row->ascent = row->phys_ascent = 0;
9223 row->extra_line_spacing = 0;
9224 }
9225
9226 row->full_width_p = 1;
9227 row->continued_p = 0;
9228 row->truncated_on_left_p = 0;
9229 row->truncated_on_right_p = 0;
9230
9231 it->current_x = it->hpos = 0;
9232 it->current_y += row->height;
9233 ++it->vpos;
9234 ++it->glyph_row;
9235 }
9236
9237
9238 /* Value is the number of screen lines needed to make all tool-bar
9239 items of frame F visible. */
9240
9241 static int
9242 tool_bar_lines_needed (f)
9243 struct frame *f;
9244 {
9245 struct window *w = XWINDOW (f->tool_bar_window);
9246 struct it it;
9247
9248 /* Initialize an iterator for iteration over
9249 F->desired_tool_bar_string in the tool-bar window of frame F. */
9250 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
9251 it.first_visible_x = 0;
9252 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
9253 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
9254
9255 while (!ITERATOR_AT_END_P (&it))
9256 {
9257 it.glyph_row = w->desired_matrix->rows;
9258 clear_glyph_row (it.glyph_row);
9259 display_tool_bar_line (&it);
9260 }
9261
9262 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
9263 }
9264
9265
9266 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
9267 0, 1, 0,
9268 doc: /* Return the number of lines occupied by the tool bar of FRAME. */)
9269 (frame)
9270 Lisp_Object frame;
9271 {
9272 struct frame *f;
9273 struct window *w;
9274 int nlines = 0;
9275
9276 if (NILP (frame))
9277 frame = selected_frame;
9278 else
9279 CHECK_FRAME (frame);
9280 f = XFRAME (frame);
9281
9282 if (WINDOWP (f->tool_bar_window)
9283 || (w = XWINDOW (f->tool_bar_window),
9284 WINDOW_TOTAL_LINES (w) > 0))
9285 {
9286 update_tool_bar (f, 1);
9287 if (f->n_tool_bar_items)
9288 {
9289 build_desired_tool_bar_string (f);
9290 nlines = tool_bar_lines_needed (f);
9291 }
9292 }
9293
9294 return make_number (nlines);
9295 }
9296
9297
9298 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
9299 height should be changed. */
9300
9301 static int
9302 redisplay_tool_bar (f)
9303 struct frame *f;
9304 {
9305 struct window *w;
9306 struct it it;
9307 struct glyph_row *row;
9308 int change_height_p = 0;
9309
9310 #ifdef USE_GTK
9311 if (FRAME_EXTERNAL_TOOL_BAR (f))
9312 update_frame_tool_bar (f);
9313 return 0;
9314 #endif
9315
9316 /* If frame hasn't a tool-bar window or if it is zero-height, don't
9317 do anything. This means you must start with tool-bar-lines
9318 non-zero to get the auto-sizing effect. Or in other words, you
9319 can turn off tool-bars by specifying tool-bar-lines zero. */
9320 if (!WINDOWP (f->tool_bar_window)
9321 || (w = XWINDOW (f->tool_bar_window),
9322 WINDOW_TOTAL_LINES (w) == 0))
9323 return 0;
9324
9325 /* Set up an iterator for the tool-bar window. */
9326 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
9327 it.first_visible_x = 0;
9328 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
9329 row = it.glyph_row;
9330
9331 /* Build a string that represents the contents of the tool-bar. */
9332 build_desired_tool_bar_string (f);
9333 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
9334
9335 /* Display as many lines as needed to display all tool-bar items. */
9336 while (it.current_y < it.last_visible_y)
9337 display_tool_bar_line (&it);
9338
9339 /* It doesn't make much sense to try scrolling in the tool-bar
9340 window, so don't do it. */
9341 w->desired_matrix->no_scrolling_p = 1;
9342 w->must_be_updated_p = 1;
9343
9344 if (auto_resize_tool_bars_p)
9345 {
9346 int nlines;
9347
9348 /* If we couldn't display everything, change the tool-bar's
9349 height. */
9350 if (IT_STRING_CHARPOS (it) < it.end_charpos)
9351 change_height_p = 1;
9352
9353 /* If there are blank lines at the end, except for a partially
9354 visible blank line at the end that is smaller than
9355 FRAME_LINE_HEIGHT, change the tool-bar's height. */
9356 row = it.glyph_row - 1;
9357 if (!row->displays_text_p
9358 && row->height >= FRAME_LINE_HEIGHT (f))
9359 change_height_p = 1;
9360
9361 /* If row displays tool-bar items, but is partially visible,
9362 change the tool-bar's height. */
9363 if (row->displays_text_p
9364 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y)
9365 change_height_p = 1;
9366
9367 /* Resize windows as needed by changing the `tool-bar-lines'
9368 frame parameter. */
9369 if (change_height_p
9370 && (nlines = tool_bar_lines_needed (f),
9371 nlines != WINDOW_TOTAL_LINES (w)))
9372 {
9373 extern Lisp_Object Qtool_bar_lines;
9374 Lisp_Object frame;
9375 int old_height = WINDOW_TOTAL_LINES (w);
9376
9377 XSETFRAME (frame, f);
9378 clear_glyph_matrix (w->desired_matrix);
9379 Fmodify_frame_parameters (frame,
9380 Fcons (Fcons (Qtool_bar_lines,
9381 make_number (nlines)),
9382 Qnil));
9383 if (WINDOW_TOTAL_LINES (w) != old_height)
9384 fonts_changed_p = 1;
9385 }
9386 }
9387
9388 return change_height_p;
9389 }
9390
9391
9392 /* Get information about the tool-bar item which is displayed in GLYPH
9393 on frame F. Return in *PROP_IDX the index where tool-bar item
9394 properties start in F->tool_bar_items. Value is zero if
9395 GLYPH doesn't display a tool-bar item. */
9396
9397 static int
9398 tool_bar_item_info (f, glyph, prop_idx)
9399 struct frame *f;
9400 struct glyph *glyph;
9401 int *prop_idx;
9402 {
9403 Lisp_Object prop;
9404 int success_p;
9405 int charpos;
9406
9407 /* This function can be called asynchronously, which means we must
9408 exclude any possibility that Fget_text_property signals an
9409 error. */
9410 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
9411 charpos = max (0, charpos);
9412
9413 /* Get the text property `menu-item' at pos. The value of that
9414 property is the start index of this item's properties in
9415 F->tool_bar_items. */
9416 prop = Fget_text_property (make_number (charpos),
9417 Qmenu_item, f->current_tool_bar_string);
9418 if (INTEGERP (prop))
9419 {
9420 *prop_idx = XINT (prop);
9421 success_p = 1;
9422 }
9423 else
9424 success_p = 0;
9425
9426 return success_p;
9427 }
9428
9429 \f
9430 /* Get information about the tool-bar item at position X/Y on frame F.
9431 Return in *GLYPH a pointer to the glyph of the tool-bar item in
9432 the current matrix of the tool-bar window of F, or NULL if not
9433 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
9434 item in F->tool_bar_items. Value is
9435
9436 -1 if X/Y is not on a tool-bar item
9437 0 if X/Y is on the same item that was highlighted before.
9438 1 otherwise. */
9439
9440 static int
9441 get_tool_bar_item (f, x, y, glyph, hpos, vpos, prop_idx)
9442 struct frame *f;
9443 int x, y;
9444 struct glyph **glyph;
9445 int *hpos, *vpos, *prop_idx;
9446 {
9447 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9448 struct window *w = XWINDOW (f->tool_bar_window);
9449 int area;
9450
9451 /* Find the glyph under X/Y. */
9452 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
9453 if (*glyph == NULL)
9454 return -1;
9455
9456 /* Get the start of this tool-bar item's properties in
9457 f->tool_bar_items. */
9458 if (!tool_bar_item_info (f, *glyph, prop_idx))
9459 return -1;
9460
9461 /* Is mouse on the highlighted item? */
9462 if (EQ (f->tool_bar_window, dpyinfo->mouse_face_window)
9463 && *vpos >= dpyinfo->mouse_face_beg_row
9464 && *vpos <= dpyinfo->mouse_face_end_row
9465 && (*vpos > dpyinfo->mouse_face_beg_row
9466 || *hpos >= dpyinfo->mouse_face_beg_col)
9467 && (*vpos < dpyinfo->mouse_face_end_row
9468 || *hpos < dpyinfo->mouse_face_end_col
9469 || dpyinfo->mouse_face_past_end))
9470 return 0;
9471
9472 return 1;
9473 }
9474
9475
9476 /* EXPORT:
9477 Handle mouse button event on the tool-bar of frame F, at
9478 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
9479 0 for button release. MODIFIERS is event modifiers for button
9480 release. */
9481
9482 void
9483 handle_tool_bar_click (f, x, y, down_p, modifiers)
9484 struct frame *f;
9485 int x, y, down_p;
9486 unsigned int modifiers;
9487 {
9488 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9489 struct window *w = XWINDOW (f->tool_bar_window);
9490 int hpos, vpos, prop_idx;
9491 struct glyph *glyph;
9492 Lisp_Object enabled_p;
9493
9494 /* If not on the highlighted tool-bar item, return. */
9495 frame_to_window_pixel_xy (w, &x, &y);
9496 if (get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
9497 return;
9498
9499 /* If item is disabled, do nothing. */
9500 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
9501 if (NILP (enabled_p))
9502 return;
9503
9504 if (down_p)
9505 {
9506 /* Show item in pressed state. */
9507 show_mouse_face (dpyinfo, DRAW_IMAGE_SUNKEN);
9508 dpyinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
9509 last_tool_bar_item = prop_idx;
9510 }
9511 else
9512 {
9513 Lisp_Object key, frame;
9514 struct input_event event;
9515 EVENT_INIT (event);
9516
9517 /* Show item in released state. */
9518 show_mouse_face (dpyinfo, DRAW_IMAGE_RAISED);
9519 dpyinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
9520
9521 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
9522
9523 XSETFRAME (frame, f);
9524 event.kind = TOOL_BAR_EVENT;
9525 event.frame_or_window = frame;
9526 event.arg = frame;
9527 kbd_buffer_store_event (&event);
9528
9529 event.kind = TOOL_BAR_EVENT;
9530 event.frame_or_window = frame;
9531 event.arg = key;
9532 event.modifiers = modifiers;
9533 kbd_buffer_store_event (&event);
9534 last_tool_bar_item = -1;
9535 }
9536 }
9537
9538
9539 /* Possibly highlight a tool-bar item on frame F when mouse moves to
9540 tool-bar window-relative coordinates X/Y. Called from
9541 note_mouse_highlight. */
9542
9543 static void
9544 note_tool_bar_highlight (f, x, y)
9545 struct frame *f;
9546 int x, y;
9547 {
9548 Lisp_Object window = f->tool_bar_window;
9549 struct window *w = XWINDOW (window);
9550 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9551 int hpos, vpos;
9552 struct glyph *glyph;
9553 struct glyph_row *row;
9554 int i;
9555 Lisp_Object enabled_p;
9556 int prop_idx;
9557 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
9558 int mouse_down_p, rc;
9559
9560 /* Function note_mouse_highlight is called with negative x(y
9561 values when mouse moves outside of the frame. */
9562 if (x <= 0 || y <= 0)
9563 {
9564 clear_mouse_face (dpyinfo);
9565 return;
9566 }
9567
9568 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
9569 if (rc < 0)
9570 {
9571 /* Not on tool-bar item. */
9572 clear_mouse_face (dpyinfo);
9573 return;
9574 }
9575 else if (rc == 0)
9576 /* On same tool-bar item as before. */
9577 goto set_help_echo;
9578
9579 clear_mouse_face (dpyinfo);
9580
9581 /* Mouse is down, but on different tool-bar item? */
9582 mouse_down_p = (dpyinfo->grabbed
9583 && f == last_mouse_frame
9584 && FRAME_LIVE_P (f));
9585 if (mouse_down_p
9586 && last_tool_bar_item != prop_idx)
9587 return;
9588
9589 dpyinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
9590 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
9591
9592 /* If tool-bar item is not enabled, don't highlight it. */
9593 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
9594 if (!NILP (enabled_p))
9595 {
9596 /* Compute the x-position of the glyph. In front and past the
9597 image is a space. We include this in the highlighted area. */
9598 row = MATRIX_ROW (w->current_matrix, vpos);
9599 for (i = x = 0; i < hpos; ++i)
9600 x += row->glyphs[TEXT_AREA][i].pixel_width;
9601
9602 /* Record this as the current active region. */
9603 dpyinfo->mouse_face_beg_col = hpos;
9604 dpyinfo->mouse_face_beg_row = vpos;
9605 dpyinfo->mouse_face_beg_x = x;
9606 dpyinfo->mouse_face_beg_y = row->y;
9607 dpyinfo->mouse_face_past_end = 0;
9608
9609 dpyinfo->mouse_face_end_col = hpos + 1;
9610 dpyinfo->mouse_face_end_row = vpos;
9611 dpyinfo->mouse_face_end_x = x + glyph->pixel_width;
9612 dpyinfo->mouse_face_end_y = row->y;
9613 dpyinfo->mouse_face_window = window;
9614 dpyinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
9615
9616 /* Display it as active. */
9617 show_mouse_face (dpyinfo, draw);
9618 dpyinfo->mouse_face_image_state = draw;
9619 }
9620
9621 set_help_echo:
9622
9623 /* Set help_echo_string to a help string to display for this tool-bar item.
9624 XTread_socket does the rest. */
9625 help_echo_object = help_echo_window = Qnil;
9626 help_echo_pos = -1;
9627 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
9628 if (NILP (help_echo_string))
9629 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
9630 }
9631
9632 #endif /* HAVE_WINDOW_SYSTEM */
9633
9634
9635 \f
9636 /************************************************************************
9637 Horizontal scrolling
9638 ************************************************************************/
9639
9640 static int hscroll_window_tree P_ ((Lisp_Object));
9641 static int hscroll_windows P_ ((Lisp_Object));
9642
9643 /* For all leaf windows in the window tree rooted at WINDOW, set their
9644 hscroll value so that PT is (i) visible in the window, and (ii) so
9645 that it is not within a certain margin at the window's left and
9646 right border. Value is non-zero if any window's hscroll has been
9647 changed. */
9648
9649 static int
9650 hscroll_window_tree (window)
9651 Lisp_Object window;
9652 {
9653 int hscrolled_p = 0;
9654 int hscroll_relative_p = FLOATP (Vhscroll_step);
9655 int hscroll_step_abs = 0;
9656 double hscroll_step_rel = 0;
9657
9658 if (hscroll_relative_p)
9659 {
9660 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
9661 if (hscroll_step_rel < 0)
9662 {
9663 hscroll_relative_p = 0;
9664 hscroll_step_abs = 0;
9665 }
9666 }
9667 else if (INTEGERP (Vhscroll_step))
9668 {
9669 hscroll_step_abs = XINT (Vhscroll_step);
9670 if (hscroll_step_abs < 0)
9671 hscroll_step_abs = 0;
9672 }
9673 else
9674 hscroll_step_abs = 0;
9675
9676 while (WINDOWP (window))
9677 {
9678 struct window *w = XWINDOW (window);
9679
9680 if (WINDOWP (w->hchild))
9681 hscrolled_p |= hscroll_window_tree (w->hchild);
9682 else if (WINDOWP (w->vchild))
9683 hscrolled_p |= hscroll_window_tree (w->vchild);
9684 else if (w->cursor.vpos >= 0)
9685 {
9686 int h_margin;
9687 int text_area_width;
9688 struct glyph_row *current_cursor_row
9689 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
9690 struct glyph_row *desired_cursor_row
9691 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
9692 struct glyph_row *cursor_row
9693 = (desired_cursor_row->enabled_p
9694 ? desired_cursor_row
9695 : current_cursor_row);
9696
9697 text_area_width = window_box_width (w, TEXT_AREA);
9698
9699 /* Scroll when cursor is inside this scroll margin. */
9700 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
9701
9702 if ((XFASTINT (w->hscroll)
9703 && w->cursor.x <= h_margin)
9704 || (cursor_row->enabled_p
9705 && cursor_row->truncated_on_right_p
9706 && (w->cursor.x >= text_area_width - h_margin)))
9707 {
9708 struct it it;
9709 int hscroll;
9710 struct buffer *saved_current_buffer;
9711 int pt;
9712 int wanted_x;
9713
9714 /* Find point in a display of infinite width. */
9715 saved_current_buffer = current_buffer;
9716 current_buffer = XBUFFER (w->buffer);
9717
9718 if (w == XWINDOW (selected_window))
9719 pt = BUF_PT (current_buffer);
9720 else
9721 {
9722 pt = marker_position (w->pointm);
9723 pt = max (BEGV, pt);
9724 pt = min (ZV, pt);
9725 }
9726
9727 /* Move iterator to pt starting at cursor_row->start in
9728 a line with infinite width. */
9729 init_to_row_start (&it, w, cursor_row);
9730 it.last_visible_x = INFINITY;
9731 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
9732 current_buffer = saved_current_buffer;
9733
9734 /* Position cursor in window. */
9735 if (!hscroll_relative_p && hscroll_step_abs == 0)
9736 hscroll = max (0, (it.current_x
9737 - (ITERATOR_AT_END_OF_LINE_P (&it)
9738 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
9739 : (text_area_width / 2))))
9740 / FRAME_COLUMN_WIDTH (it.f);
9741 else if (w->cursor.x >= text_area_width - h_margin)
9742 {
9743 if (hscroll_relative_p)
9744 wanted_x = text_area_width * (1 - hscroll_step_rel)
9745 - h_margin;
9746 else
9747 wanted_x = text_area_width
9748 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
9749 - h_margin;
9750 hscroll
9751 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
9752 }
9753 else
9754 {
9755 if (hscroll_relative_p)
9756 wanted_x = text_area_width * hscroll_step_rel
9757 + h_margin;
9758 else
9759 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
9760 + h_margin;
9761 hscroll
9762 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
9763 }
9764 hscroll = max (hscroll, XFASTINT (w->min_hscroll));
9765
9766 /* Don't call Fset_window_hscroll if value hasn't
9767 changed because it will prevent redisplay
9768 optimizations. */
9769 if (XFASTINT (w->hscroll) != hscroll)
9770 {
9771 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
9772 w->hscroll = make_number (hscroll);
9773 hscrolled_p = 1;
9774 }
9775 }
9776 }
9777
9778 window = w->next;
9779 }
9780
9781 /* Value is non-zero if hscroll of any leaf window has been changed. */
9782 return hscrolled_p;
9783 }
9784
9785
9786 /* Set hscroll so that cursor is visible and not inside horizontal
9787 scroll margins for all windows in the tree rooted at WINDOW. See
9788 also hscroll_window_tree above. Value is non-zero if any window's
9789 hscroll has been changed. If it has, desired matrices on the frame
9790 of WINDOW are cleared. */
9791
9792 static int
9793 hscroll_windows (window)
9794 Lisp_Object window;
9795 {
9796 int hscrolled_p;
9797
9798 if (automatic_hscrolling_p)
9799 {
9800 hscrolled_p = hscroll_window_tree (window);
9801 if (hscrolled_p)
9802 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
9803 }
9804 else
9805 hscrolled_p = 0;
9806 return hscrolled_p;
9807 }
9808
9809
9810 \f
9811 /************************************************************************
9812 Redisplay
9813 ************************************************************************/
9814
9815 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
9816 to a non-zero value. This is sometimes handy to have in a debugger
9817 session. */
9818
9819 #if GLYPH_DEBUG
9820
9821 /* First and last unchanged row for try_window_id. */
9822
9823 int debug_first_unchanged_at_end_vpos;
9824 int debug_last_unchanged_at_beg_vpos;
9825
9826 /* Delta vpos and y. */
9827
9828 int debug_dvpos, debug_dy;
9829
9830 /* Delta in characters and bytes for try_window_id. */
9831
9832 int debug_delta, debug_delta_bytes;
9833
9834 /* Values of window_end_pos and window_end_vpos at the end of
9835 try_window_id. */
9836
9837 EMACS_INT debug_end_pos, debug_end_vpos;
9838
9839 /* Append a string to W->desired_matrix->method. FMT is a printf
9840 format string. A1...A9 are a supplement for a variable-length
9841 argument list. If trace_redisplay_p is non-zero also printf the
9842 resulting string to stderr. */
9843
9844 static void
9845 debug_method_add (w, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9)
9846 struct window *w;
9847 char *fmt;
9848 int a1, a2, a3, a4, a5, a6, a7, a8, a9;
9849 {
9850 char buffer[512];
9851 char *method = w->desired_matrix->method;
9852 int len = strlen (method);
9853 int size = sizeof w->desired_matrix->method;
9854 int remaining = size - len - 1;
9855
9856 sprintf (buffer, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9);
9857 if (len && remaining)
9858 {
9859 method[len] = '|';
9860 --remaining, ++len;
9861 }
9862
9863 strncpy (method + len, buffer, remaining);
9864
9865 if (trace_redisplay_p)
9866 fprintf (stderr, "%p (%s): %s\n",
9867 w,
9868 ((BUFFERP (w->buffer)
9869 && STRINGP (XBUFFER (w->buffer)->name))
9870 ? (char *) SDATA (XBUFFER (w->buffer)->name)
9871 : "no buffer"),
9872 buffer);
9873 }
9874
9875 #endif /* GLYPH_DEBUG */
9876
9877
9878 /* Value is non-zero if all changes in window W, which displays
9879 current_buffer, are in the text between START and END. START is a
9880 buffer position, END is given as a distance from Z. Used in
9881 redisplay_internal for display optimization. */
9882
9883 static INLINE int
9884 text_outside_line_unchanged_p (w, start, end)
9885 struct window *w;
9886 int start, end;
9887 {
9888 int unchanged_p = 1;
9889
9890 /* If text or overlays have changed, see where. */
9891 if (XFASTINT (w->last_modified) < MODIFF
9892 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
9893 {
9894 /* Gap in the line? */
9895 if (GPT < start || Z - GPT < end)
9896 unchanged_p = 0;
9897
9898 /* Changes start in front of the line, or end after it? */
9899 if (unchanged_p
9900 && (BEG_UNCHANGED < start - 1
9901 || END_UNCHANGED < end))
9902 unchanged_p = 0;
9903
9904 /* If selective display, can't optimize if changes start at the
9905 beginning of the line. */
9906 if (unchanged_p
9907 && INTEGERP (current_buffer->selective_display)
9908 && XINT (current_buffer->selective_display) > 0
9909 && (BEG_UNCHANGED < start || GPT <= start))
9910 unchanged_p = 0;
9911
9912 /* If there are overlays at the start or end of the line, these
9913 may have overlay strings with newlines in them. A change at
9914 START, for instance, may actually concern the display of such
9915 overlay strings as well, and they are displayed on different
9916 lines. So, quickly rule out this case. (For the future, it
9917 might be desirable to implement something more telling than
9918 just BEG/END_UNCHANGED.) */
9919 if (unchanged_p)
9920 {
9921 if (BEG + BEG_UNCHANGED == start
9922 && overlay_touches_p (start))
9923 unchanged_p = 0;
9924 if (END_UNCHANGED == end
9925 && overlay_touches_p (Z - end))
9926 unchanged_p = 0;
9927 }
9928 }
9929
9930 return unchanged_p;
9931 }
9932
9933
9934 /* Do a frame update, taking possible shortcuts into account. This is
9935 the main external entry point for redisplay.
9936
9937 If the last redisplay displayed an echo area message and that message
9938 is no longer requested, we clear the echo area or bring back the
9939 mini-buffer if that is in use. */
9940
9941 void
9942 redisplay ()
9943 {
9944 redisplay_internal (0);
9945 }
9946
9947
9948 static Lisp_Object
9949 overlay_arrow_string_or_property (var)
9950 Lisp_Object var;
9951 {
9952 Lisp_Object val;
9953
9954 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
9955 return val;
9956
9957 return Voverlay_arrow_string;
9958 }
9959
9960 /* Return 1 if there are any overlay-arrows in current_buffer. */
9961 static int
9962 overlay_arrow_in_current_buffer_p ()
9963 {
9964 Lisp_Object vlist;
9965
9966 for (vlist = Voverlay_arrow_variable_list;
9967 CONSP (vlist);
9968 vlist = XCDR (vlist))
9969 {
9970 Lisp_Object var = XCAR (vlist);
9971 Lisp_Object val;
9972
9973 if (!SYMBOLP (var))
9974 continue;
9975 val = find_symbol_value (var);
9976 if (MARKERP (val)
9977 && current_buffer == XMARKER (val)->buffer)
9978 return 1;
9979 }
9980 return 0;
9981 }
9982
9983
9984 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
9985 has changed. */
9986
9987 static int
9988 overlay_arrows_changed_p ()
9989 {
9990 Lisp_Object vlist;
9991
9992 for (vlist = Voverlay_arrow_variable_list;
9993 CONSP (vlist);
9994 vlist = XCDR (vlist))
9995 {
9996 Lisp_Object var = XCAR (vlist);
9997 Lisp_Object val, pstr;
9998
9999 if (!SYMBOLP (var))
10000 continue;
10001 val = find_symbol_value (var);
10002 if (!MARKERP (val))
10003 continue;
10004 if (! EQ (COERCE_MARKER (val),
10005 Fget (var, Qlast_arrow_position))
10006 || ! (pstr = overlay_arrow_string_or_property (var),
10007 EQ (pstr, Fget (var, Qlast_arrow_string))))
10008 return 1;
10009 }
10010 return 0;
10011 }
10012
10013 /* Mark overlay arrows to be updated on next redisplay. */
10014
10015 static void
10016 update_overlay_arrows (up_to_date)
10017 int up_to_date;
10018 {
10019 Lisp_Object vlist;
10020
10021 for (vlist = Voverlay_arrow_variable_list;
10022 CONSP (vlist);
10023 vlist = XCDR (vlist))
10024 {
10025 Lisp_Object var = XCAR (vlist);
10026
10027 if (!SYMBOLP (var))
10028 continue;
10029
10030 if (up_to_date > 0)
10031 {
10032 Lisp_Object val = find_symbol_value (var);
10033 Fput (var, Qlast_arrow_position,
10034 COERCE_MARKER (val));
10035 Fput (var, Qlast_arrow_string,
10036 overlay_arrow_string_or_property (var));
10037 }
10038 else if (up_to_date < 0
10039 || !NILP (Fget (var, Qlast_arrow_position)))
10040 {
10041 Fput (var, Qlast_arrow_position, Qt);
10042 Fput (var, Qlast_arrow_string, Qt);
10043 }
10044 }
10045 }
10046
10047
10048 /* Return overlay arrow string to display at row.
10049 Return integer (bitmap number) for arrow bitmap in left fringe.
10050 Return nil if no overlay arrow. */
10051
10052 static Lisp_Object
10053 overlay_arrow_at_row (it, row)
10054 struct it *it;
10055 struct glyph_row *row;
10056 {
10057 Lisp_Object vlist;
10058
10059 for (vlist = Voverlay_arrow_variable_list;
10060 CONSP (vlist);
10061 vlist = XCDR (vlist))
10062 {
10063 Lisp_Object var = XCAR (vlist);
10064 Lisp_Object val;
10065
10066 if (!SYMBOLP (var))
10067 continue;
10068
10069 val = find_symbol_value (var);
10070
10071 if (MARKERP (val)
10072 && current_buffer == XMARKER (val)->buffer
10073 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
10074 {
10075 if (FRAME_WINDOW_P (it->f)
10076 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
10077 {
10078 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
10079 {
10080 int fringe_bitmap;
10081 if ((fringe_bitmap = lookup_fringe_bitmap (val)) != 0)
10082 return make_number (fringe_bitmap);
10083 }
10084 return make_number (-1); /* Use default arrow bitmap */
10085 }
10086 return overlay_arrow_string_or_property (var);
10087 }
10088 }
10089
10090 return Qnil;
10091 }
10092
10093 /* Return 1 if point moved out of or into a composition. Otherwise
10094 return 0. PREV_BUF and PREV_PT are the last point buffer and
10095 position. BUF and PT are the current point buffer and position. */
10096
10097 int
10098 check_point_in_composition (prev_buf, prev_pt, buf, pt)
10099 struct buffer *prev_buf, *buf;
10100 int prev_pt, pt;
10101 {
10102 EMACS_INT start, end;
10103 Lisp_Object prop;
10104 Lisp_Object buffer;
10105
10106 XSETBUFFER (buffer, buf);
10107 /* Check a composition at the last point if point moved within the
10108 same buffer. */
10109 if (prev_buf == buf)
10110 {
10111 if (prev_pt == pt)
10112 /* Point didn't move. */
10113 return 0;
10114
10115 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
10116 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
10117 && COMPOSITION_VALID_P (start, end, prop)
10118 && start < prev_pt && end > prev_pt)
10119 /* The last point was within the composition. Return 1 iff
10120 point moved out of the composition. */
10121 return (pt <= start || pt >= end);
10122 }
10123
10124 /* Check a composition at the current point. */
10125 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
10126 && find_composition (pt, -1, &start, &end, &prop, buffer)
10127 && COMPOSITION_VALID_P (start, end, prop)
10128 && start < pt && end > pt);
10129 }
10130
10131
10132 /* Reconsider the setting of B->clip_changed which is displayed
10133 in window W. */
10134
10135 static INLINE void
10136 reconsider_clip_changes (w, b)
10137 struct window *w;
10138 struct buffer *b;
10139 {
10140 if (b->clip_changed
10141 && !NILP (w->window_end_valid)
10142 && w->current_matrix->buffer == b
10143 && w->current_matrix->zv == BUF_ZV (b)
10144 && w->current_matrix->begv == BUF_BEGV (b))
10145 b->clip_changed = 0;
10146
10147 /* If display wasn't paused, and W is not a tool bar window, see if
10148 point has been moved into or out of a composition. In that case,
10149 we set b->clip_changed to 1 to force updating the screen. If
10150 b->clip_changed has already been set to 1, we can skip this
10151 check. */
10152 if (!b->clip_changed
10153 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
10154 {
10155 int pt;
10156
10157 if (w == XWINDOW (selected_window))
10158 pt = BUF_PT (current_buffer);
10159 else
10160 pt = marker_position (w->pointm);
10161
10162 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
10163 || pt != XINT (w->last_point))
10164 && check_point_in_composition (w->current_matrix->buffer,
10165 XINT (w->last_point),
10166 XBUFFER (w->buffer), pt))
10167 b->clip_changed = 1;
10168 }
10169 }
10170 \f
10171
10172 /* Select FRAME to forward the values of frame-local variables into C
10173 variables so that the redisplay routines can access those values
10174 directly. */
10175
10176 static void
10177 select_frame_for_redisplay (frame)
10178 Lisp_Object frame;
10179 {
10180 Lisp_Object tail, sym, val;
10181 Lisp_Object old = selected_frame;
10182
10183 selected_frame = frame;
10184
10185 for (tail = XFRAME (frame)->param_alist; CONSP (tail); tail = XCDR (tail))
10186 if (CONSP (XCAR (tail))
10187 && (sym = XCAR (XCAR (tail)),
10188 SYMBOLP (sym))
10189 && (sym = indirect_variable (sym),
10190 val = SYMBOL_VALUE (sym),
10191 (BUFFER_LOCAL_VALUEP (val)
10192 || SOME_BUFFER_LOCAL_VALUEP (val)))
10193 && XBUFFER_LOCAL_VALUE (val)->check_frame)
10194 /* Use find_symbol_value rather than Fsymbol_value
10195 to avoid an error if it is void. */
10196 find_symbol_value (sym);
10197
10198 for (tail = XFRAME (old)->param_alist; CONSP (tail); tail = XCDR (tail))
10199 if (CONSP (XCAR (tail))
10200 && (sym = XCAR (XCAR (tail)),
10201 SYMBOLP (sym))
10202 && (sym = indirect_variable (sym),
10203 val = SYMBOL_VALUE (sym),
10204 (BUFFER_LOCAL_VALUEP (val)
10205 || SOME_BUFFER_LOCAL_VALUEP (val)))
10206 && XBUFFER_LOCAL_VALUE (val)->check_frame)
10207 find_symbol_value (sym);
10208 }
10209
10210
10211 #define STOP_POLLING \
10212 do { if (! polling_stopped_here) stop_polling (); \
10213 polling_stopped_here = 1; } while (0)
10214
10215 #define RESUME_POLLING \
10216 do { if (polling_stopped_here) start_polling (); \
10217 polling_stopped_here = 0; } while (0)
10218
10219
10220 /* If PRESERVE_ECHO_AREA is nonzero, it means this redisplay is not in
10221 response to any user action; therefore, we should preserve the echo
10222 area. (Actually, our caller does that job.) Perhaps in the future
10223 avoid recentering windows if it is not necessary; currently that
10224 causes some problems. */
10225
10226 static void
10227 redisplay_internal (preserve_echo_area)
10228 int preserve_echo_area;
10229 {
10230 struct window *w = XWINDOW (selected_window);
10231 struct frame *f = XFRAME (w->frame);
10232 int pause;
10233 int must_finish = 0;
10234 struct text_pos tlbufpos, tlendpos;
10235 int number_of_visible_frames;
10236 int count;
10237 struct frame *sf = SELECTED_FRAME ();
10238 int polling_stopped_here = 0;
10239
10240 /* Non-zero means redisplay has to consider all windows on all
10241 frames. Zero means, only selected_window is considered. */
10242 int consider_all_windows_p;
10243
10244 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
10245
10246 /* No redisplay if running in batch mode or frame is not yet fully
10247 initialized, or redisplay is explicitly turned off by setting
10248 Vinhibit_redisplay. */
10249 if (noninteractive
10250 || !NILP (Vinhibit_redisplay)
10251 || !f->glyphs_initialized_p)
10252 return;
10253
10254 /* The flag redisplay_performed_directly_p is set by
10255 direct_output_for_insert when it already did the whole screen
10256 update necessary. */
10257 if (redisplay_performed_directly_p)
10258 {
10259 redisplay_performed_directly_p = 0;
10260 if (!hscroll_windows (selected_window))
10261 return;
10262 }
10263
10264 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
10265 if (popup_activated ())
10266 return;
10267 #endif
10268
10269 /* I don't think this happens but let's be paranoid. */
10270 if (redisplaying_p)
10271 return;
10272
10273 /* Record a function that resets redisplaying_p to its old value
10274 when we leave this function. */
10275 count = SPECPDL_INDEX ();
10276 record_unwind_protect (unwind_redisplay,
10277 Fcons (make_number (redisplaying_p), selected_frame));
10278 ++redisplaying_p;
10279 specbind (Qinhibit_free_realized_faces, Qnil);
10280
10281 {
10282 Lisp_Object tail, frame;
10283
10284 FOR_EACH_FRAME (tail, frame)
10285 {
10286 struct frame *f = XFRAME (frame);
10287 f->already_hscrolled_p = 0;
10288 }
10289 }
10290
10291 retry:
10292 pause = 0;
10293 reconsider_clip_changes (w, current_buffer);
10294
10295 /* If new fonts have been loaded that make a glyph matrix adjustment
10296 necessary, do it. */
10297 if (fonts_changed_p)
10298 {
10299 adjust_glyphs (NULL);
10300 ++windows_or_buffers_changed;
10301 fonts_changed_p = 0;
10302 }
10303
10304 /* If face_change_count is non-zero, init_iterator will free all
10305 realized faces, which includes the faces referenced from current
10306 matrices. So, we can't reuse current matrices in this case. */
10307 if (face_change_count)
10308 ++windows_or_buffers_changed;
10309
10310 if (! FRAME_WINDOW_P (sf)
10311 && previous_terminal_frame != sf)
10312 {
10313 /* Since frames on an ASCII terminal share the same display
10314 area, displaying a different frame means redisplay the whole
10315 thing. */
10316 windows_or_buffers_changed++;
10317 SET_FRAME_GARBAGED (sf);
10318 XSETFRAME (Vterminal_frame, sf);
10319 }
10320 previous_terminal_frame = sf;
10321
10322 /* Set the visible flags for all frames. Do this before checking
10323 for resized or garbaged frames; they want to know if their frames
10324 are visible. See the comment in frame.h for
10325 FRAME_SAMPLE_VISIBILITY. */
10326 {
10327 Lisp_Object tail, frame;
10328
10329 number_of_visible_frames = 0;
10330
10331 FOR_EACH_FRAME (tail, frame)
10332 {
10333 struct frame *f = XFRAME (frame);
10334
10335 FRAME_SAMPLE_VISIBILITY (f);
10336 if (FRAME_VISIBLE_P (f))
10337 ++number_of_visible_frames;
10338 clear_desired_matrices (f);
10339 }
10340 }
10341
10342 /* Notice any pending interrupt request to change frame size. */
10343 do_pending_window_change (1);
10344
10345 /* Clear frames marked as garbaged. */
10346 if (frame_garbaged)
10347 clear_garbaged_frames ();
10348
10349 /* Build menubar and tool-bar items. */
10350 prepare_menu_bars ();
10351
10352 if (windows_or_buffers_changed)
10353 update_mode_lines++;
10354
10355 /* Detect case that we need to write or remove a star in the mode line. */
10356 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
10357 {
10358 w->update_mode_line = Qt;
10359 if (buffer_shared > 1)
10360 update_mode_lines++;
10361 }
10362
10363 /* If %c is in the mode line, update it if needed. */
10364 if (!NILP (w->column_number_displayed)
10365 /* This alternative quickly identifies a common case
10366 where no change is needed. */
10367 && !(PT == XFASTINT (w->last_point)
10368 && XFASTINT (w->last_modified) >= MODIFF
10369 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
10370 && (XFASTINT (w->column_number_displayed)
10371 != (int) current_column ())) /* iftc */
10372 w->update_mode_line = Qt;
10373
10374 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
10375
10376 /* The variable buffer_shared is set in redisplay_window and
10377 indicates that we redisplay a buffer in different windows. See
10378 there. */
10379 consider_all_windows_p = (update_mode_lines || buffer_shared > 1
10380 || cursor_type_changed);
10381
10382 /* If specs for an arrow have changed, do thorough redisplay
10383 to ensure we remove any arrow that should no longer exist. */
10384 if (overlay_arrows_changed_p ())
10385 consider_all_windows_p = windows_or_buffers_changed = 1;
10386
10387 /* Normally the message* functions will have already displayed and
10388 updated the echo area, but the frame may have been trashed, or
10389 the update may have been preempted, so display the echo area
10390 again here. Checking message_cleared_p captures the case that
10391 the echo area should be cleared. */
10392 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
10393 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
10394 || (message_cleared_p
10395 && minibuf_level == 0
10396 /* If the mini-window is currently selected, this means the
10397 echo-area doesn't show through. */
10398 && !MINI_WINDOW_P (XWINDOW (selected_window))))
10399 {
10400 int window_height_changed_p = echo_area_display (0);
10401 must_finish = 1;
10402
10403 /* If we don't display the current message, don't clear the
10404 message_cleared_p flag, because, if we did, we wouldn't clear
10405 the echo area in the next redisplay which doesn't preserve
10406 the echo area. */
10407 if (!display_last_displayed_message_p)
10408 message_cleared_p = 0;
10409
10410 if (fonts_changed_p)
10411 goto retry;
10412 else if (window_height_changed_p)
10413 {
10414 consider_all_windows_p = 1;
10415 ++update_mode_lines;
10416 ++windows_or_buffers_changed;
10417
10418 /* If window configuration was changed, frames may have been
10419 marked garbaged. Clear them or we will experience
10420 surprises wrt scrolling. */
10421 if (frame_garbaged)
10422 clear_garbaged_frames ();
10423 }
10424 }
10425 else if (EQ (selected_window, minibuf_window)
10426 && (current_buffer->clip_changed
10427 || XFASTINT (w->last_modified) < MODIFF
10428 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
10429 && resize_mini_window (w, 0))
10430 {
10431 /* Resized active mini-window to fit the size of what it is
10432 showing if its contents might have changed. */
10433 must_finish = 1;
10434 consider_all_windows_p = 1;
10435 ++windows_or_buffers_changed;
10436 ++update_mode_lines;
10437
10438 /* If window configuration was changed, frames may have been
10439 marked garbaged. Clear them or we will experience
10440 surprises wrt scrolling. */
10441 if (frame_garbaged)
10442 clear_garbaged_frames ();
10443 }
10444
10445
10446 /* If showing the region, and mark has changed, we must redisplay
10447 the whole window. The assignment to this_line_start_pos prevents
10448 the optimization directly below this if-statement. */
10449 if (((!NILP (Vtransient_mark_mode)
10450 && !NILP (XBUFFER (w->buffer)->mark_active))
10451 != !NILP (w->region_showing))
10452 || (!NILP (w->region_showing)
10453 && !EQ (w->region_showing,
10454 Fmarker_position (XBUFFER (w->buffer)->mark))))
10455 CHARPOS (this_line_start_pos) = 0;
10456
10457 /* Optimize the case that only the line containing the cursor in the
10458 selected window has changed. Variables starting with this_ are
10459 set in display_line and record information about the line
10460 containing the cursor. */
10461 tlbufpos = this_line_start_pos;
10462 tlendpos = this_line_end_pos;
10463 if (!consider_all_windows_p
10464 && CHARPOS (tlbufpos) > 0
10465 && NILP (w->update_mode_line)
10466 && !current_buffer->clip_changed
10467 && !current_buffer->prevent_redisplay_optimizations_p
10468 && FRAME_VISIBLE_P (XFRAME (w->frame))
10469 && !FRAME_OBSCURED_P (XFRAME (w->frame))
10470 /* Make sure recorded data applies to current buffer, etc. */
10471 && this_line_buffer == current_buffer
10472 && current_buffer == XBUFFER (w->buffer)
10473 && NILP (w->force_start)
10474 && NILP (w->optional_new_start)
10475 /* Point must be on the line that we have info recorded about. */
10476 && PT >= CHARPOS (tlbufpos)
10477 && PT <= Z - CHARPOS (tlendpos)
10478 /* All text outside that line, including its final newline,
10479 must be unchanged */
10480 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
10481 CHARPOS (tlendpos)))
10482 {
10483 if (CHARPOS (tlbufpos) > BEGV
10484 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
10485 && (CHARPOS (tlbufpos) == ZV
10486 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
10487 /* Former continuation line has disappeared by becoming empty */
10488 goto cancel;
10489 else if (XFASTINT (w->last_modified) < MODIFF
10490 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
10491 || MINI_WINDOW_P (w))
10492 {
10493 /* We have to handle the case of continuation around a
10494 wide-column character (See the comment in indent.c around
10495 line 885).
10496
10497 For instance, in the following case:
10498
10499 -------- Insert --------
10500 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
10501 J_I_ ==> J_I_ `^^' are cursors.
10502 ^^ ^^
10503 -------- --------
10504
10505 As we have to redraw the line above, we should goto cancel. */
10506
10507 struct it it;
10508 int line_height_before = this_line_pixel_height;
10509
10510 /* Note that start_display will handle the case that the
10511 line starting at tlbufpos is a continuation lines. */
10512 start_display (&it, w, tlbufpos);
10513
10514 /* Implementation note: It this still necessary? */
10515 if (it.current_x != this_line_start_x)
10516 goto cancel;
10517
10518 TRACE ((stderr, "trying display optimization 1\n"));
10519 w->cursor.vpos = -1;
10520 overlay_arrow_seen = 0;
10521 it.vpos = this_line_vpos;
10522 it.current_y = this_line_y;
10523 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
10524 display_line (&it);
10525
10526 /* If line contains point, is not continued,
10527 and ends at same distance from eob as before, we win */
10528 if (w->cursor.vpos >= 0
10529 /* Line is not continued, otherwise this_line_start_pos
10530 would have been set to 0 in display_line. */
10531 && CHARPOS (this_line_start_pos)
10532 /* Line ends as before. */
10533 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
10534 /* Line has same height as before. Otherwise other lines
10535 would have to be shifted up or down. */
10536 && this_line_pixel_height == line_height_before)
10537 {
10538 /* If this is not the window's last line, we must adjust
10539 the charstarts of the lines below. */
10540 if (it.current_y < it.last_visible_y)
10541 {
10542 struct glyph_row *row
10543 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
10544 int delta, delta_bytes;
10545
10546 if (Z - CHARPOS (tlendpos) == ZV)
10547 {
10548 /* This line ends at end of (accessible part of)
10549 buffer. There is no newline to count. */
10550 delta = (Z
10551 - CHARPOS (tlendpos)
10552 - MATRIX_ROW_START_CHARPOS (row));
10553 delta_bytes = (Z_BYTE
10554 - BYTEPOS (tlendpos)
10555 - MATRIX_ROW_START_BYTEPOS (row));
10556 }
10557 else
10558 {
10559 /* This line ends in a newline. Must take
10560 account of the newline and the rest of the
10561 text that follows. */
10562 delta = (Z
10563 - CHARPOS (tlendpos)
10564 - MATRIX_ROW_START_CHARPOS (row));
10565 delta_bytes = (Z_BYTE
10566 - BYTEPOS (tlendpos)
10567 - MATRIX_ROW_START_BYTEPOS (row));
10568 }
10569
10570 increment_matrix_positions (w->current_matrix,
10571 this_line_vpos + 1,
10572 w->current_matrix->nrows,
10573 delta, delta_bytes);
10574 }
10575
10576 /* If this row displays text now but previously didn't,
10577 or vice versa, w->window_end_vpos may have to be
10578 adjusted. */
10579 if ((it.glyph_row - 1)->displays_text_p)
10580 {
10581 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
10582 XSETINT (w->window_end_vpos, this_line_vpos);
10583 }
10584 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
10585 && this_line_vpos > 0)
10586 XSETINT (w->window_end_vpos, this_line_vpos - 1);
10587 w->window_end_valid = Qnil;
10588
10589 /* Update hint: No need to try to scroll in update_window. */
10590 w->desired_matrix->no_scrolling_p = 1;
10591
10592 #if GLYPH_DEBUG
10593 *w->desired_matrix->method = 0;
10594 debug_method_add (w, "optimization 1");
10595 #endif
10596 #ifdef HAVE_WINDOW_SYSTEM
10597 update_window_fringes (w, 0);
10598 #endif
10599 goto update;
10600 }
10601 else
10602 goto cancel;
10603 }
10604 else if (/* Cursor position hasn't changed. */
10605 PT == XFASTINT (w->last_point)
10606 /* Make sure the cursor was last displayed
10607 in this window. Otherwise we have to reposition it. */
10608 && 0 <= w->cursor.vpos
10609 && WINDOW_TOTAL_LINES (w) > w->cursor.vpos)
10610 {
10611 if (!must_finish)
10612 {
10613 do_pending_window_change (1);
10614
10615 /* We used to always goto end_of_redisplay here, but this
10616 isn't enough if we have a blinking cursor. */
10617 if (w->cursor_off_p == w->last_cursor_off_p)
10618 goto end_of_redisplay;
10619 }
10620 goto update;
10621 }
10622 /* If highlighting the region, or if the cursor is in the echo area,
10623 then we can't just move the cursor. */
10624 else if (! (!NILP (Vtransient_mark_mode)
10625 && !NILP (current_buffer->mark_active))
10626 && (EQ (selected_window, current_buffer->last_selected_window)
10627 || highlight_nonselected_windows)
10628 && NILP (w->region_showing)
10629 && NILP (Vshow_trailing_whitespace)
10630 && !cursor_in_echo_area)
10631 {
10632 struct it it;
10633 struct glyph_row *row;
10634
10635 /* Skip from tlbufpos to PT and see where it is. Note that
10636 PT may be in invisible text. If so, we will end at the
10637 next visible position. */
10638 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
10639 NULL, DEFAULT_FACE_ID);
10640 it.current_x = this_line_start_x;
10641 it.current_y = this_line_y;
10642 it.vpos = this_line_vpos;
10643
10644 /* The call to move_it_to stops in front of PT, but
10645 moves over before-strings. */
10646 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
10647
10648 if (it.vpos == this_line_vpos
10649 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
10650 row->enabled_p))
10651 {
10652 xassert (this_line_vpos == it.vpos);
10653 xassert (this_line_y == it.current_y);
10654 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
10655 #if GLYPH_DEBUG
10656 *w->desired_matrix->method = 0;
10657 debug_method_add (w, "optimization 3");
10658 #endif
10659 goto update;
10660 }
10661 else
10662 goto cancel;
10663 }
10664
10665 cancel:
10666 /* Text changed drastically or point moved off of line. */
10667 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
10668 }
10669
10670 CHARPOS (this_line_start_pos) = 0;
10671 consider_all_windows_p |= buffer_shared > 1;
10672 ++clear_face_cache_count;
10673 #ifdef HAVE_WINDOW_SYSTEM
10674 ++clear_image_cache_count;
10675 #endif
10676
10677 /* Build desired matrices, and update the display. If
10678 consider_all_windows_p is non-zero, do it for all windows on all
10679 frames. Otherwise do it for selected_window, only. */
10680
10681 if (consider_all_windows_p)
10682 {
10683 Lisp_Object tail, frame;
10684 int i, n = 0, size = 50;
10685 struct frame **updated
10686 = (struct frame **) alloca (size * sizeof *updated);
10687
10688 /* Recompute # windows showing selected buffer. This will be
10689 incremented each time such a window is displayed. */
10690 buffer_shared = 0;
10691
10692 FOR_EACH_FRAME (tail, frame)
10693 {
10694 struct frame *f = XFRAME (frame);
10695
10696 if (FRAME_WINDOW_P (f) || f == sf)
10697 {
10698 if (! EQ (frame, selected_frame))
10699 /* Select the frame, for the sake of frame-local
10700 variables. */
10701 select_frame_for_redisplay (frame);
10702
10703 /* Mark all the scroll bars to be removed; we'll redeem
10704 the ones we want when we redisplay their windows. */
10705 if (condemn_scroll_bars_hook)
10706 condemn_scroll_bars_hook (f);
10707
10708 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
10709 redisplay_windows (FRAME_ROOT_WINDOW (f));
10710
10711 /* Any scroll bars which redisplay_windows should have
10712 nuked should now go away. */
10713 if (judge_scroll_bars_hook)
10714 judge_scroll_bars_hook (f);
10715
10716 /* If fonts changed, display again. */
10717 /* ??? rms: I suspect it is a mistake to jump all the way
10718 back to retry here. It should just retry this frame. */
10719 if (fonts_changed_p)
10720 goto retry;
10721
10722 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
10723 {
10724 /* See if we have to hscroll. */
10725 if (!f->already_hscrolled_p)
10726 {
10727 f->already_hscrolled_p = 1;
10728 if (hscroll_windows (f->root_window))
10729 goto retry;
10730 }
10731
10732 /* Prevent various kinds of signals during display
10733 update. stdio is not robust about handling
10734 signals, which can cause an apparent I/O
10735 error. */
10736 if (interrupt_input)
10737 unrequest_sigio ();
10738 STOP_POLLING;
10739
10740 /* Update the display. */
10741 set_window_update_flags (XWINDOW (f->root_window), 1);
10742 pause |= update_frame (f, 0, 0);
10743 #if 0 /* Exiting the loop can leave the wrong value for buffer_shared. */
10744 if (pause)
10745 break;
10746 #endif
10747
10748 if (n == size)
10749 {
10750 int nbytes = size * sizeof *updated;
10751 struct frame **p = (struct frame **) alloca (2 * nbytes);
10752 bcopy (updated, p, nbytes);
10753 size *= 2;
10754 }
10755
10756 updated[n++] = f;
10757 }
10758 }
10759 }
10760
10761 if (!pause)
10762 {
10763 /* Do the mark_window_display_accurate after all windows have
10764 been redisplayed because this call resets flags in buffers
10765 which are needed for proper redisplay. */
10766 for (i = 0; i < n; ++i)
10767 {
10768 struct frame *f = updated[i];
10769 mark_window_display_accurate (f->root_window, 1);
10770 if (frame_up_to_date_hook)
10771 frame_up_to_date_hook (f);
10772 }
10773 }
10774 }
10775 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
10776 {
10777 Lisp_Object mini_window;
10778 struct frame *mini_frame;
10779
10780 displayed_buffer = XBUFFER (XWINDOW (selected_window)->buffer);
10781 /* Use list_of_error, not Qerror, so that
10782 we catch only errors and don't run the debugger. */
10783 internal_condition_case_1 (redisplay_window_1, selected_window,
10784 list_of_error,
10785 redisplay_window_error);
10786
10787 /* Compare desired and current matrices, perform output. */
10788
10789 update:
10790 /* If fonts changed, display again. */
10791 if (fonts_changed_p)
10792 goto retry;
10793
10794 /* Prevent various kinds of signals during display update.
10795 stdio is not robust about handling signals,
10796 which can cause an apparent I/O error. */
10797 if (interrupt_input)
10798 unrequest_sigio ();
10799 STOP_POLLING;
10800
10801 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
10802 {
10803 if (hscroll_windows (selected_window))
10804 goto retry;
10805
10806 XWINDOW (selected_window)->must_be_updated_p = 1;
10807 pause = update_frame (sf, 0, 0);
10808 }
10809
10810 /* We may have called echo_area_display at the top of this
10811 function. If the echo area is on another frame, that may
10812 have put text on a frame other than the selected one, so the
10813 above call to update_frame would not have caught it. Catch
10814 it here. */
10815 mini_window = FRAME_MINIBUF_WINDOW (sf);
10816 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
10817
10818 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
10819 {
10820 XWINDOW (mini_window)->must_be_updated_p = 1;
10821 pause |= update_frame (mini_frame, 0, 0);
10822 if (!pause && hscroll_windows (mini_window))
10823 goto retry;
10824 }
10825 }
10826
10827 /* If display was paused because of pending input, make sure we do a
10828 thorough update the next time. */
10829 if (pause)
10830 {
10831 /* Prevent the optimization at the beginning of
10832 redisplay_internal that tries a single-line update of the
10833 line containing the cursor in the selected window. */
10834 CHARPOS (this_line_start_pos) = 0;
10835
10836 /* Let the overlay arrow be updated the next time. */
10837 update_overlay_arrows (0);
10838
10839 /* If we pause after scrolling, some rows in the current
10840 matrices of some windows are not valid. */
10841 if (!WINDOW_FULL_WIDTH_P (w)
10842 && !FRAME_WINDOW_P (XFRAME (w->frame)))
10843 update_mode_lines = 1;
10844 }
10845 else
10846 {
10847 if (!consider_all_windows_p)
10848 {
10849 /* This has already been done above if
10850 consider_all_windows_p is set. */
10851 mark_window_display_accurate_1 (w, 1);
10852
10853 /* Say overlay arrows are up to date. */
10854 update_overlay_arrows (1);
10855
10856 if (frame_up_to_date_hook != 0)
10857 frame_up_to_date_hook (sf);
10858 }
10859
10860 update_mode_lines = 0;
10861 windows_or_buffers_changed = 0;
10862 cursor_type_changed = 0;
10863 }
10864
10865 /* Start SIGIO interrupts coming again. Having them off during the
10866 code above makes it less likely one will discard output, but not
10867 impossible, since there might be stuff in the system buffer here.
10868 But it is much hairier to try to do anything about that. */
10869 if (interrupt_input)
10870 request_sigio ();
10871 RESUME_POLLING;
10872
10873 /* If a frame has become visible which was not before, redisplay
10874 again, so that we display it. Expose events for such a frame
10875 (which it gets when becoming visible) don't call the parts of
10876 redisplay constructing glyphs, so simply exposing a frame won't
10877 display anything in this case. So, we have to display these
10878 frames here explicitly. */
10879 if (!pause)
10880 {
10881 Lisp_Object tail, frame;
10882 int new_count = 0;
10883
10884 FOR_EACH_FRAME (tail, frame)
10885 {
10886 int this_is_visible = 0;
10887
10888 if (XFRAME (frame)->visible)
10889 this_is_visible = 1;
10890 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
10891 if (XFRAME (frame)->visible)
10892 this_is_visible = 1;
10893
10894 if (this_is_visible)
10895 new_count++;
10896 }
10897
10898 if (new_count != number_of_visible_frames)
10899 windows_or_buffers_changed++;
10900 }
10901
10902 /* Change frame size now if a change is pending. */
10903 do_pending_window_change (1);
10904
10905 /* If we just did a pending size change, or have additional
10906 visible frames, redisplay again. */
10907 if (windows_or_buffers_changed && !pause)
10908 goto retry;
10909
10910 /* Clear the face cache eventually. */
10911 if (consider_all_windows_p)
10912 {
10913 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
10914 {
10915 clear_face_cache (0);
10916 clear_face_cache_count = 0;
10917 }
10918 #ifdef HAVE_WINDOW_SYSTEM
10919 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
10920 {
10921 Lisp_Object tail, frame;
10922 FOR_EACH_FRAME (tail, frame)
10923 {
10924 struct frame *f = XFRAME (frame);
10925 if (FRAME_WINDOW_P (f))
10926 clear_image_cache (f, 0);
10927 }
10928 clear_image_cache_count = 0;
10929 }
10930 #endif /* HAVE_WINDOW_SYSTEM */
10931 }
10932
10933 end_of_redisplay:
10934 unbind_to (count, Qnil);
10935 RESUME_POLLING;
10936 }
10937
10938
10939 /* Redisplay, but leave alone any recent echo area message unless
10940 another message has been requested in its place.
10941
10942 This is useful in situations where you need to redisplay but no
10943 user action has occurred, making it inappropriate for the message
10944 area to be cleared. See tracking_off and
10945 wait_reading_process_output for examples of these situations.
10946
10947 FROM_WHERE is an integer saying from where this function was
10948 called. This is useful for debugging. */
10949
10950 void
10951 redisplay_preserve_echo_area (from_where)
10952 int from_where;
10953 {
10954 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
10955
10956 if (!NILP (echo_area_buffer[1]))
10957 {
10958 /* We have a previously displayed message, but no current
10959 message. Redisplay the previous message. */
10960 display_last_displayed_message_p = 1;
10961 redisplay_internal (1);
10962 display_last_displayed_message_p = 0;
10963 }
10964 else
10965 redisplay_internal (1);
10966
10967 if (rif != NULL && rif->flush_display_optional)
10968 rif->flush_display_optional (NULL);
10969 }
10970
10971
10972 /* Function registered with record_unwind_protect in
10973 redisplay_internal. Reset redisplaying_p to the value it had
10974 before redisplay_internal was called, and clear
10975 prevent_freeing_realized_faces_p. It also selects the previously
10976 selected frame. */
10977
10978 static Lisp_Object
10979 unwind_redisplay (val)
10980 Lisp_Object val;
10981 {
10982 Lisp_Object old_redisplaying_p, old_frame;
10983
10984 old_redisplaying_p = XCAR (val);
10985 redisplaying_p = XFASTINT (old_redisplaying_p);
10986 old_frame = XCDR (val);
10987 if (! EQ (old_frame, selected_frame))
10988 select_frame_for_redisplay (old_frame);
10989 return Qnil;
10990 }
10991
10992
10993 /* Mark the display of window W as accurate or inaccurate. If
10994 ACCURATE_P is non-zero mark display of W as accurate. If
10995 ACCURATE_P is zero, arrange for W to be redisplayed the next time
10996 redisplay_internal is called. */
10997
10998 static void
10999 mark_window_display_accurate_1 (w, accurate_p)
11000 struct window *w;
11001 int accurate_p;
11002 {
11003 if (BUFFERP (w->buffer))
11004 {
11005 struct buffer *b = XBUFFER (w->buffer);
11006
11007 w->last_modified
11008 = make_number (accurate_p ? BUF_MODIFF (b) : 0);
11009 w->last_overlay_modified
11010 = make_number (accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
11011 w->last_had_star
11012 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b) ? Qt : Qnil;
11013
11014 if (accurate_p)
11015 {
11016 b->clip_changed = 0;
11017 b->prevent_redisplay_optimizations_p = 0;
11018
11019 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
11020 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
11021 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
11022 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
11023
11024 w->current_matrix->buffer = b;
11025 w->current_matrix->begv = BUF_BEGV (b);
11026 w->current_matrix->zv = BUF_ZV (b);
11027
11028 w->last_cursor = w->cursor;
11029 w->last_cursor_off_p = w->cursor_off_p;
11030
11031 if (w == XWINDOW (selected_window))
11032 w->last_point = make_number (BUF_PT (b));
11033 else
11034 w->last_point = make_number (XMARKER (w->pointm)->charpos);
11035 }
11036 }
11037
11038 if (accurate_p)
11039 {
11040 w->window_end_valid = w->buffer;
11041 #if 0 /* This is incorrect with variable-height lines. */
11042 xassert (XINT (w->window_end_vpos)
11043 < (WINDOW_TOTAL_LINES (w)
11044 - (WINDOW_WANTS_MODELINE_P (w) ? 1 : 0)));
11045 #endif
11046 w->update_mode_line = Qnil;
11047 }
11048 }
11049
11050
11051 /* Mark the display of windows in the window tree rooted at WINDOW as
11052 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
11053 windows as accurate. If ACCURATE_P is zero, arrange for windows to
11054 be redisplayed the next time redisplay_internal is called. */
11055
11056 void
11057 mark_window_display_accurate (window, accurate_p)
11058 Lisp_Object window;
11059 int accurate_p;
11060 {
11061 struct window *w;
11062
11063 for (; !NILP (window); window = w->next)
11064 {
11065 w = XWINDOW (window);
11066 mark_window_display_accurate_1 (w, accurate_p);
11067
11068 if (!NILP (w->vchild))
11069 mark_window_display_accurate (w->vchild, accurate_p);
11070 if (!NILP (w->hchild))
11071 mark_window_display_accurate (w->hchild, accurate_p);
11072 }
11073
11074 if (accurate_p)
11075 {
11076 update_overlay_arrows (1);
11077 }
11078 else
11079 {
11080 /* Force a thorough redisplay the next time by setting
11081 last_arrow_position and last_arrow_string to t, which is
11082 unequal to any useful value of Voverlay_arrow_... */
11083 update_overlay_arrows (-1);
11084 }
11085 }
11086
11087
11088 /* Return value in display table DP (Lisp_Char_Table *) for character
11089 C. Since a display table doesn't have any parent, we don't have to
11090 follow parent. Do not call this function directly but use the
11091 macro DISP_CHAR_VECTOR. */
11092
11093 Lisp_Object
11094 disp_char_vector (dp, c)
11095 struct Lisp_Char_Table *dp;
11096 int c;
11097 {
11098 Lisp_Object val;
11099
11100 if (ASCII_CHAR_P (c))
11101 {
11102 val = dp->ascii;
11103 if (SUB_CHAR_TABLE_P (val))
11104 val = XSUB_CHAR_TABLE (val)->contents[c];
11105 }
11106 else
11107 {
11108 Lisp_Object table;
11109
11110 XSETCHAR_TABLE (table, dp);
11111 val = char_table_ref (table, c);
11112 }
11113 if (NILP (val))
11114 val = dp->defalt;
11115 return val;
11116 }
11117
11118
11119 \f
11120 /***********************************************************************
11121 Window Redisplay
11122 ***********************************************************************/
11123
11124 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
11125
11126 static void
11127 redisplay_windows (window)
11128 Lisp_Object window;
11129 {
11130 while (!NILP (window))
11131 {
11132 struct window *w = XWINDOW (window);
11133
11134 if (!NILP (w->hchild))
11135 redisplay_windows (w->hchild);
11136 else if (!NILP (w->vchild))
11137 redisplay_windows (w->vchild);
11138 else
11139 {
11140 displayed_buffer = XBUFFER (w->buffer);
11141 /* Use list_of_error, not Qerror, so that
11142 we catch only errors and don't run the debugger. */
11143 internal_condition_case_1 (redisplay_window_0, window,
11144 list_of_error,
11145 redisplay_window_error);
11146 }
11147
11148 window = w->next;
11149 }
11150 }
11151
11152 static Lisp_Object
11153 redisplay_window_error ()
11154 {
11155 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
11156 return Qnil;
11157 }
11158
11159 static Lisp_Object
11160 redisplay_window_0 (window)
11161 Lisp_Object window;
11162 {
11163 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
11164 redisplay_window (window, 0);
11165 return Qnil;
11166 }
11167
11168 static Lisp_Object
11169 redisplay_window_1 (window)
11170 Lisp_Object window;
11171 {
11172 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
11173 redisplay_window (window, 1);
11174 return Qnil;
11175 }
11176 \f
11177
11178 /* Increment GLYPH until it reaches END or CONDITION fails while
11179 adding (GLYPH)->pixel_width to X. */
11180
11181 #define SKIP_GLYPHS(glyph, end, x, condition) \
11182 do \
11183 { \
11184 (x) += (glyph)->pixel_width; \
11185 ++(glyph); \
11186 } \
11187 while ((glyph) < (end) && (condition))
11188
11189
11190 /* Set cursor position of W. PT is assumed to be displayed in ROW.
11191 DELTA is the number of bytes by which positions recorded in ROW
11192 differ from current buffer positions. */
11193
11194 void
11195 set_cursor_from_row (w, row, matrix, delta, delta_bytes, dy, dvpos)
11196 struct window *w;
11197 struct glyph_row *row;
11198 struct glyph_matrix *matrix;
11199 int delta, delta_bytes, dy, dvpos;
11200 {
11201 struct glyph *glyph = row->glyphs[TEXT_AREA];
11202 struct glyph *end = glyph + row->used[TEXT_AREA];
11203 struct glyph *cursor = NULL;
11204 /* The first glyph that starts a sequence of glyphs from string. */
11205 struct glyph *string_start;
11206 /* The X coordinate of string_start. */
11207 int string_start_x;
11208 /* The last known character position. */
11209 int last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
11210 /* The last known character position before string_start. */
11211 int string_before_pos;
11212 int x = row->x;
11213 int cursor_x = x;
11214 int cursor_from_overlay_pos = 0;
11215 int pt_old = PT - delta;
11216
11217 /* Skip over glyphs not having an object at the start of the row.
11218 These are special glyphs like truncation marks on terminal
11219 frames. */
11220 if (row->displays_text_p)
11221 while (glyph < end
11222 && INTEGERP (glyph->object)
11223 && glyph->charpos < 0)
11224 {
11225 x += glyph->pixel_width;
11226 ++glyph;
11227 }
11228
11229 string_start = NULL;
11230 while (glyph < end
11231 && !INTEGERP (glyph->object)
11232 && (!BUFFERP (glyph->object)
11233 || (last_pos = glyph->charpos) < pt_old))
11234 {
11235 if (! STRINGP (glyph->object))
11236 {
11237 string_start = NULL;
11238 x += glyph->pixel_width;
11239 ++glyph;
11240 if (cursor_from_overlay_pos
11241 && last_pos > cursor_from_overlay_pos)
11242 {
11243 cursor_from_overlay_pos = 0;
11244 cursor = 0;
11245 }
11246 }
11247 else
11248 {
11249 string_before_pos = last_pos;
11250 string_start = glyph;
11251 string_start_x = x;
11252 /* Skip all glyphs from string. */
11253 do
11254 {
11255 int pos;
11256 if ((cursor == NULL || glyph > cursor)
11257 && !NILP (Fget_char_property (make_number ((glyph)->charpos),
11258 Qcursor, (glyph)->object))
11259 && (pos = string_buffer_position (w, glyph->object,
11260 string_before_pos),
11261 (pos == 0 /* From overlay */
11262 || pos == pt_old)))
11263 {
11264 /* Estimate overlay buffer position from the buffer
11265 positions of the glyphs before and after the overlay.
11266 Add 1 to last_pos so that if point corresponds to the
11267 glyph right after the overlay, we still use a 'cursor'
11268 property found in that overlay. */
11269 cursor_from_overlay_pos = pos == 0 ? last_pos+1 : 0;
11270 cursor = glyph;
11271 cursor_x = x;
11272 }
11273 x += glyph->pixel_width;
11274 ++glyph;
11275 }
11276 while (glyph < end && STRINGP (glyph->object));
11277 }
11278 }
11279
11280 if (cursor != NULL)
11281 {
11282 glyph = cursor;
11283 x = cursor_x;
11284 }
11285 else if (row->ends_in_ellipsis_p && glyph == end)
11286 {
11287 /* Scan back over the ellipsis glyphs, decrementing positions. */
11288 while (glyph > row->glyphs[TEXT_AREA]
11289 && (glyph - 1)->charpos == last_pos)
11290 glyph--, x -= glyph->pixel_width;
11291 /* That loop always goes one position too far,
11292 including the glyph before the ellipsis.
11293 So scan forward over that one. */
11294 x += glyph->pixel_width;
11295 glyph++;
11296 }
11297 else if (string_start
11298 && (glyph == end || !BUFFERP (glyph->object) || last_pos > pt_old))
11299 {
11300 /* We may have skipped over point because the previous glyphs
11301 are from string. As there's no easy way to know the
11302 character position of the current glyph, find the correct
11303 glyph on point by scanning from string_start again. */
11304 Lisp_Object limit;
11305 Lisp_Object string;
11306 int pos;
11307
11308 limit = make_number (pt_old + 1);
11309 end = glyph;
11310 glyph = string_start;
11311 x = string_start_x;
11312 string = glyph->object;
11313 pos = string_buffer_position (w, string, string_before_pos);
11314 /* If STRING is from overlay, LAST_POS == 0. We skip such glyphs
11315 because we always put cursor after overlay strings. */
11316 while (pos == 0 && glyph < end)
11317 {
11318 string = glyph->object;
11319 SKIP_GLYPHS (glyph, end, x, EQ (glyph->object, string));
11320 if (glyph < end)
11321 pos = string_buffer_position (w, glyph->object, string_before_pos);
11322 }
11323
11324 while (glyph < end)
11325 {
11326 pos = XINT (Fnext_single_char_property_change
11327 (make_number (pos), Qdisplay, Qnil, limit));
11328 if (pos > pt_old)
11329 break;
11330 /* Skip glyphs from the same string. */
11331 string = glyph->object;
11332 SKIP_GLYPHS (glyph, end, x, EQ (glyph->object, string));
11333 /* Skip glyphs from an overlay. */
11334 while (glyph < end
11335 && ! string_buffer_position (w, glyph->object, pos))
11336 {
11337 string = glyph->object;
11338 SKIP_GLYPHS (glyph, end, x, EQ (glyph->object, string));
11339 }
11340 }
11341 }
11342
11343 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
11344 w->cursor.x = x;
11345 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
11346 w->cursor.y = row->y + dy;
11347
11348 if (w == XWINDOW (selected_window))
11349 {
11350 if (!row->continued_p
11351 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
11352 && row->x == 0)
11353 {
11354 this_line_buffer = XBUFFER (w->buffer);
11355
11356 CHARPOS (this_line_start_pos)
11357 = MATRIX_ROW_START_CHARPOS (row) + delta;
11358 BYTEPOS (this_line_start_pos)
11359 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
11360
11361 CHARPOS (this_line_end_pos)
11362 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
11363 BYTEPOS (this_line_end_pos)
11364 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
11365
11366 this_line_y = w->cursor.y;
11367 this_line_pixel_height = row->height;
11368 this_line_vpos = w->cursor.vpos;
11369 this_line_start_x = row->x;
11370 }
11371 else
11372 CHARPOS (this_line_start_pos) = 0;
11373 }
11374 }
11375
11376
11377 /* Run window scroll functions, if any, for WINDOW with new window
11378 start STARTP. Sets the window start of WINDOW to that position.
11379
11380 We assume that the window's buffer is really current. */
11381
11382 static INLINE struct text_pos
11383 run_window_scroll_functions (window, startp)
11384 Lisp_Object window;
11385 struct text_pos startp;
11386 {
11387 struct window *w = XWINDOW (window);
11388 SET_MARKER_FROM_TEXT_POS (w->start, startp);
11389
11390 if (current_buffer != XBUFFER (w->buffer))
11391 abort ();
11392
11393 if (!NILP (Vwindow_scroll_functions))
11394 {
11395 run_hook_with_args_2 (Qwindow_scroll_functions, window,
11396 make_number (CHARPOS (startp)));
11397 SET_TEXT_POS_FROM_MARKER (startp, w->start);
11398 /* In case the hook functions switch buffers. */
11399 if (current_buffer != XBUFFER (w->buffer))
11400 set_buffer_internal_1 (XBUFFER (w->buffer));
11401 }
11402
11403 return startp;
11404 }
11405
11406
11407 /* Make sure the line containing the cursor is fully visible.
11408 A value of 1 means there is nothing to be done.
11409 (Either the line is fully visible, or it cannot be made so,
11410 or we cannot tell.)
11411
11412 If FORCE_P is non-zero, return 0 even if partial visible cursor row
11413 is higher than window.
11414
11415 A value of 0 means the caller should do scrolling
11416 as if point had gone off the screen. */
11417
11418 static int
11419 cursor_row_fully_visible_p (w, force_p, current_matrix_p)
11420 struct window *w;
11421 int force_p;
11422 {
11423 struct glyph_matrix *matrix;
11424 struct glyph_row *row;
11425 int window_height;
11426
11427 if (!make_cursor_line_fully_visible_p)
11428 return 1;
11429
11430 /* It's not always possible to find the cursor, e.g, when a window
11431 is full of overlay strings. Don't do anything in that case. */
11432 if (w->cursor.vpos < 0)
11433 return 1;
11434
11435 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
11436 row = MATRIX_ROW (matrix, w->cursor.vpos);
11437
11438 /* If the cursor row is not partially visible, there's nothing to do. */
11439 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
11440 return 1;
11441
11442 /* If the row the cursor is in is taller than the window's height,
11443 it's not clear what to do, so do nothing. */
11444 window_height = window_box_height (w);
11445 if (row->height >= window_height)
11446 {
11447 if (!force_p || MINI_WINDOW_P (w) || w->vscroll)
11448 return 1;
11449 }
11450 return 0;
11451
11452 #if 0
11453 /* This code used to try to scroll the window just enough to make
11454 the line visible. It returned 0 to say that the caller should
11455 allocate larger glyph matrices. */
11456
11457 if (MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (w, row))
11458 {
11459 int dy = row->height - row->visible_height;
11460 w->vscroll = 0;
11461 w->cursor.y += dy;
11462 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
11463 }
11464 else /* MATRIX_ROW_PARTIALLY_VISIBLE_AT_BOTTOM_P (w, row)) */
11465 {
11466 int dy = - (row->height - row->visible_height);
11467 w->vscroll = dy;
11468 w->cursor.y += dy;
11469 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
11470 }
11471
11472 /* When we change the cursor y-position of the selected window,
11473 change this_line_y as well so that the display optimization for
11474 the cursor line of the selected window in redisplay_internal uses
11475 the correct y-position. */
11476 if (w == XWINDOW (selected_window))
11477 this_line_y = w->cursor.y;
11478
11479 /* If vscrolling requires a larger glyph matrix, arrange for a fresh
11480 redisplay with larger matrices. */
11481 if (matrix->nrows < required_matrix_height (w))
11482 {
11483 fonts_changed_p = 1;
11484 return 0;
11485 }
11486
11487 return 1;
11488 #endif /* 0 */
11489 }
11490
11491
11492 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
11493 non-zero means only WINDOW is redisplayed in redisplay_internal.
11494 TEMP_SCROLL_STEP has the same meaning as scroll_step, and is used
11495 in redisplay_window to bring a partially visible line into view in
11496 the case that only the cursor has moved.
11497
11498 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
11499 last screen line's vertical height extends past the end of the screen.
11500
11501 Value is
11502
11503 1 if scrolling succeeded
11504
11505 0 if scrolling didn't find point.
11506
11507 -1 if new fonts have been loaded so that we must interrupt
11508 redisplay, adjust glyph matrices, and try again. */
11509
11510 enum
11511 {
11512 SCROLLING_SUCCESS,
11513 SCROLLING_FAILED,
11514 SCROLLING_NEED_LARGER_MATRICES
11515 };
11516
11517 static int
11518 try_scrolling (window, just_this_one_p, scroll_conservatively,
11519 scroll_step, temp_scroll_step, last_line_misfit)
11520 Lisp_Object window;
11521 int just_this_one_p;
11522 EMACS_INT scroll_conservatively, scroll_step;
11523 int temp_scroll_step;
11524 int last_line_misfit;
11525 {
11526 struct window *w = XWINDOW (window);
11527 struct frame *f = XFRAME (w->frame);
11528 struct text_pos scroll_margin_pos;
11529 struct text_pos pos;
11530 struct text_pos startp;
11531 struct it it;
11532 Lisp_Object window_end;
11533 int this_scroll_margin;
11534 int dy = 0;
11535 int scroll_max;
11536 int rc;
11537 int amount_to_scroll = 0;
11538 Lisp_Object aggressive;
11539 int height;
11540 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
11541
11542 #if GLYPH_DEBUG
11543 debug_method_add (w, "try_scrolling");
11544 #endif
11545
11546 SET_TEXT_POS_FROM_MARKER (startp, w->start);
11547
11548 /* Compute scroll margin height in pixels. We scroll when point is
11549 within this distance from the top or bottom of the window. */
11550 if (scroll_margin > 0)
11551 {
11552 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
11553 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
11554 }
11555 else
11556 this_scroll_margin = 0;
11557
11558 /* Force scroll_conservatively to have a reasonable value so it doesn't
11559 cause an overflow while computing how much to scroll. */
11560 if (scroll_conservatively)
11561 scroll_conservatively = min (scroll_conservatively,
11562 MOST_POSITIVE_FIXNUM / FRAME_LINE_HEIGHT (f));
11563
11564 /* Compute how much we should try to scroll maximally to bring point
11565 into view. */
11566 if (scroll_step || scroll_conservatively || temp_scroll_step)
11567 scroll_max = max (scroll_step,
11568 max (scroll_conservatively, temp_scroll_step));
11569 else if (NUMBERP (current_buffer->scroll_down_aggressively)
11570 || NUMBERP (current_buffer->scroll_up_aggressively))
11571 /* We're trying to scroll because of aggressive scrolling
11572 but no scroll_step is set. Choose an arbitrary one. Maybe
11573 there should be a variable for this. */
11574 scroll_max = 10;
11575 else
11576 scroll_max = 0;
11577 scroll_max *= FRAME_LINE_HEIGHT (f);
11578
11579 /* Decide whether we have to scroll down. Start at the window end
11580 and move this_scroll_margin up to find the position of the scroll
11581 margin. */
11582 window_end = Fwindow_end (window, Qt);
11583
11584 too_near_end:
11585
11586 CHARPOS (scroll_margin_pos) = XINT (window_end);
11587 BYTEPOS (scroll_margin_pos) = CHAR_TO_BYTE (CHARPOS (scroll_margin_pos));
11588
11589 if (this_scroll_margin || extra_scroll_margin_lines)
11590 {
11591 start_display (&it, w, scroll_margin_pos);
11592 if (this_scroll_margin)
11593 move_it_vertically_backward (&it, this_scroll_margin);
11594 if (extra_scroll_margin_lines)
11595 move_it_by_lines (&it, - extra_scroll_margin_lines, 0);
11596 scroll_margin_pos = it.current.pos;
11597 }
11598
11599 if (PT >= CHARPOS (scroll_margin_pos))
11600 {
11601 int y0;
11602
11603 /* Point is in the scroll margin at the bottom of the window, or
11604 below. Compute a new window start that makes point visible. */
11605
11606 /* Compute the distance from the scroll margin to PT.
11607 Give up if the distance is greater than scroll_max. */
11608 start_display (&it, w, scroll_margin_pos);
11609 y0 = it.current_y;
11610 move_it_to (&it, PT, 0, it.last_visible_y, -1,
11611 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
11612
11613 /* To make point visible, we have to move the window start
11614 down so that the line the cursor is in is visible, which
11615 means we have to add in the height of the cursor line. */
11616 dy = line_bottom_y (&it) - y0;
11617
11618 if (dy > scroll_max)
11619 return SCROLLING_FAILED;
11620
11621 /* Move the window start down. If scrolling conservatively,
11622 move it just enough down to make point visible. If
11623 scroll_step is set, move it down by scroll_step. */
11624 start_display (&it, w, startp);
11625
11626 if (scroll_conservatively)
11627 /* Set AMOUNT_TO_SCROLL to at least one line,
11628 and at most scroll_conservatively lines. */
11629 amount_to_scroll
11630 = min (max (dy, FRAME_LINE_HEIGHT (f)),
11631 FRAME_LINE_HEIGHT (f) * scroll_conservatively);
11632 else if (scroll_step || temp_scroll_step)
11633 amount_to_scroll = scroll_max;
11634 else
11635 {
11636 aggressive = current_buffer->scroll_up_aggressively;
11637 height = WINDOW_BOX_TEXT_HEIGHT (w);
11638 if (NUMBERP (aggressive))
11639 {
11640 double float_amount = XFLOATINT (aggressive) * height;
11641 amount_to_scroll = float_amount;
11642 if (amount_to_scroll == 0 && float_amount > 0)
11643 amount_to_scroll = 1;
11644 }
11645 }
11646
11647 if (amount_to_scroll <= 0)
11648 return SCROLLING_FAILED;
11649
11650 /* If moving by amount_to_scroll leaves STARTP unchanged,
11651 move it down one screen line. */
11652
11653 move_it_vertically (&it, amount_to_scroll);
11654 if (CHARPOS (it.current.pos) == CHARPOS (startp))
11655 move_it_by_lines (&it, 1, 1);
11656 startp = it.current.pos;
11657 }
11658 else
11659 {
11660 /* See if point is inside the scroll margin at the top of the
11661 window. */
11662 scroll_margin_pos = startp;
11663 if (this_scroll_margin)
11664 {
11665 start_display (&it, w, startp);
11666 move_it_vertically (&it, this_scroll_margin);
11667 scroll_margin_pos = it.current.pos;
11668 }
11669
11670 if (PT < CHARPOS (scroll_margin_pos))
11671 {
11672 /* Point is in the scroll margin at the top of the window or
11673 above what is displayed in the window. */
11674 int y0;
11675
11676 /* Compute the vertical distance from PT to the scroll
11677 margin position. Give up if distance is greater than
11678 scroll_max. */
11679 SET_TEXT_POS (pos, PT, PT_BYTE);
11680 start_display (&it, w, pos);
11681 y0 = it.current_y;
11682 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
11683 it.last_visible_y, -1,
11684 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
11685 dy = it.current_y - y0;
11686 if (dy > scroll_max)
11687 return SCROLLING_FAILED;
11688
11689 /* Compute new window start. */
11690 start_display (&it, w, startp);
11691
11692 if (scroll_conservatively)
11693 amount_to_scroll
11694 = max (dy, FRAME_LINE_HEIGHT (f) * max (scroll_step, temp_scroll_step));
11695 else if (scroll_step || temp_scroll_step)
11696 amount_to_scroll = scroll_max;
11697 else
11698 {
11699 aggressive = current_buffer->scroll_down_aggressively;
11700 height = WINDOW_BOX_TEXT_HEIGHT (w);
11701 if (NUMBERP (aggressive))
11702 {
11703 double float_amount = XFLOATINT (aggressive) * height;
11704 amount_to_scroll = float_amount;
11705 if (amount_to_scroll == 0 && float_amount > 0)
11706 amount_to_scroll = 1;
11707 }
11708 }
11709
11710 if (amount_to_scroll <= 0)
11711 return SCROLLING_FAILED;
11712
11713 move_it_vertically_backward (&it, amount_to_scroll);
11714 startp = it.current.pos;
11715 }
11716 }
11717
11718 /* Run window scroll functions. */
11719 startp = run_window_scroll_functions (window, startp);
11720
11721 /* Display the window. Give up if new fonts are loaded, or if point
11722 doesn't appear. */
11723 if (!try_window (window, startp, 0))
11724 rc = SCROLLING_NEED_LARGER_MATRICES;
11725 else if (w->cursor.vpos < 0)
11726 {
11727 clear_glyph_matrix (w->desired_matrix);
11728 rc = SCROLLING_FAILED;
11729 }
11730 else
11731 {
11732 /* Maybe forget recorded base line for line number display. */
11733 if (!just_this_one_p
11734 || current_buffer->clip_changed
11735 || BEG_UNCHANGED < CHARPOS (startp))
11736 w->base_line_number = Qnil;
11737
11738 /* If cursor ends up on a partially visible line,
11739 treat that as being off the bottom of the screen. */
11740 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1, 0))
11741 {
11742 clear_glyph_matrix (w->desired_matrix);
11743 ++extra_scroll_margin_lines;
11744 goto too_near_end;
11745 }
11746 rc = SCROLLING_SUCCESS;
11747 }
11748
11749 return rc;
11750 }
11751
11752
11753 /* Compute a suitable window start for window W if display of W starts
11754 on a continuation line. Value is non-zero if a new window start
11755 was computed.
11756
11757 The new window start will be computed, based on W's width, starting
11758 from the start of the continued line. It is the start of the
11759 screen line with the minimum distance from the old start W->start. */
11760
11761 static int
11762 compute_window_start_on_continuation_line (w)
11763 struct window *w;
11764 {
11765 struct text_pos pos, start_pos;
11766 int window_start_changed_p = 0;
11767
11768 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
11769
11770 /* If window start is on a continuation line... Window start may be
11771 < BEGV in case there's invisible text at the start of the
11772 buffer (M-x rmail, for example). */
11773 if (CHARPOS (start_pos) > BEGV
11774 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
11775 {
11776 struct it it;
11777 struct glyph_row *row;
11778
11779 /* Handle the case that the window start is out of range. */
11780 if (CHARPOS (start_pos) < BEGV)
11781 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
11782 else if (CHARPOS (start_pos) > ZV)
11783 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
11784
11785 /* Find the start of the continued line. This should be fast
11786 because scan_buffer is fast (newline cache). */
11787 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
11788 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
11789 row, DEFAULT_FACE_ID);
11790 reseat_at_previous_visible_line_start (&it);
11791
11792 /* If the line start is "too far" away from the window start,
11793 say it takes too much time to compute a new window start. */
11794 if (CHARPOS (start_pos) - IT_CHARPOS (it)
11795 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
11796 {
11797 int min_distance, distance;
11798
11799 /* Move forward by display lines to find the new window
11800 start. If window width was enlarged, the new start can
11801 be expected to be > the old start. If window width was
11802 decreased, the new window start will be < the old start.
11803 So, we're looking for the display line start with the
11804 minimum distance from the old window start. */
11805 pos = it.current.pos;
11806 min_distance = INFINITY;
11807 while ((distance = abs (CHARPOS (start_pos) - IT_CHARPOS (it))),
11808 distance < min_distance)
11809 {
11810 min_distance = distance;
11811 pos = it.current.pos;
11812 move_it_by_lines (&it, 1, 0);
11813 }
11814
11815 /* Set the window start there. */
11816 SET_MARKER_FROM_TEXT_POS (w->start, pos);
11817 window_start_changed_p = 1;
11818 }
11819 }
11820
11821 return window_start_changed_p;
11822 }
11823
11824
11825 /* Try cursor movement in case text has not changed in window WINDOW,
11826 with window start STARTP. Value is
11827
11828 CURSOR_MOVEMENT_SUCCESS if successful
11829
11830 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
11831
11832 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
11833 display. *SCROLL_STEP is set to 1, under certain circumstances, if
11834 we want to scroll as if scroll-step were set to 1. See the code.
11835
11836 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
11837 which case we have to abort this redisplay, and adjust matrices
11838 first. */
11839
11840 enum
11841 {
11842 CURSOR_MOVEMENT_SUCCESS,
11843 CURSOR_MOVEMENT_CANNOT_BE_USED,
11844 CURSOR_MOVEMENT_MUST_SCROLL,
11845 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
11846 };
11847
11848 static int
11849 try_cursor_movement (window, startp, scroll_step)
11850 Lisp_Object window;
11851 struct text_pos startp;
11852 int *scroll_step;
11853 {
11854 struct window *w = XWINDOW (window);
11855 struct frame *f = XFRAME (w->frame);
11856 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
11857
11858 #if GLYPH_DEBUG
11859 if (inhibit_try_cursor_movement)
11860 return rc;
11861 #endif
11862
11863 /* Handle case where text has not changed, only point, and it has
11864 not moved off the frame. */
11865 if (/* Point may be in this window. */
11866 PT >= CHARPOS (startp)
11867 /* Selective display hasn't changed. */
11868 && !current_buffer->clip_changed
11869 /* Function force-mode-line-update is used to force a thorough
11870 redisplay. It sets either windows_or_buffers_changed or
11871 update_mode_lines. So don't take a shortcut here for these
11872 cases. */
11873 && !update_mode_lines
11874 && !windows_or_buffers_changed
11875 && !cursor_type_changed
11876 /* Can't use this case if highlighting a region. When a
11877 region exists, cursor movement has to do more than just
11878 set the cursor. */
11879 && !(!NILP (Vtransient_mark_mode)
11880 && !NILP (current_buffer->mark_active))
11881 && NILP (w->region_showing)
11882 && NILP (Vshow_trailing_whitespace)
11883 /* Right after splitting windows, last_point may be nil. */
11884 && INTEGERP (w->last_point)
11885 /* This code is not used for mini-buffer for the sake of the case
11886 of redisplaying to replace an echo area message; since in
11887 that case the mini-buffer contents per se are usually
11888 unchanged. This code is of no real use in the mini-buffer
11889 since the handling of this_line_start_pos, etc., in redisplay
11890 handles the same cases. */
11891 && !EQ (window, minibuf_window)
11892 /* When splitting windows or for new windows, it happens that
11893 redisplay is called with a nil window_end_vpos or one being
11894 larger than the window. This should really be fixed in
11895 window.c. I don't have this on my list, now, so we do
11896 approximately the same as the old redisplay code. --gerd. */
11897 && INTEGERP (w->window_end_vpos)
11898 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
11899 && (FRAME_WINDOW_P (f)
11900 || !overlay_arrow_in_current_buffer_p ()))
11901 {
11902 int this_scroll_margin, top_scroll_margin;
11903 struct glyph_row *row = NULL;
11904
11905 #if GLYPH_DEBUG
11906 debug_method_add (w, "cursor movement");
11907 #endif
11908
11909 /* Scroll if point within this distance from the top or bottom
11910 of the window. This is a pixel value. */
11911 this_scroll_margin = max (0, scroll_margin);
11912 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
11913 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
11914
11915 top_scroll_margin = this_scroll_margin;
11916 if (WINDOW_WANTS_HEADER_LINE_P (w))
11917 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
11918
11919 /* Start with the row the cursor was displayed during the last
11920 not paused redisplay. Give up if that row is not valid. */
11921 if (w->last_cursor.vpos < 0
11922 || w->last_cursor.vpos >= w->current_matrix->nrows)
11923 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11924 else
11925 {
11926 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
11927 if (row->mode_line_p)
11928 ++row;
11929 if (!row->enabled_p)
11930 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11931 }
11932
11933 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
11934 {
11935 int scroll_p = 0;
11936 int last_y = window_text_bottom_y (w) - this_scroll_margin;
11937
11938 if (PT > XFASTINT (w->last_point))
11939 {
11940 /* Point has moved forward. */
11941 while (MATRIX_ROW_END_CHARPOS (row) < PT
11942 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
11943 {
11944 xassert (row->enabled_p);
11945 ++row;
11946 }
11947
11948 /* The end position of a row equals the start position
11949 of the next row. If PT is there, we would rather
11950 display it in the next line. */
11951 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
11952 && MATRIX_ROW_END_CHARPOS (row) == PT
11953 && !cursor_row_p (w, row))
11954 ++row;
11955
11956 /* If within the scroll margin, scroll. Note that
11957 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
11958 the next line would be drawn, and that
11959 this_scroll_margin can be zero. */
11960 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
11961 || PT > MATRIX_ROW_END_CHARPOS (row)
11962 /* Line is completely visible last line in window
11963 and PT is to be set in the next line. */
11964 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
11965 && PT == MATRIX_ROW_END_CHARPOS (row)
11966 && !row->ends_at_zv_p
11967 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
11968 scroll_p = 1;
11969 }
11970 else if (PT < XFASTINT (w->last_point))
11971 {
11972 /* Cursor has to be moved backward. Note that PT >=
11973 CHARPOS (startp) because of the outer if-statement. */
11974 while (!row->mode_line_p
11975 && (MATRIX_ROW_START_CHARPOS (row) > PT
11976 || (MATRIX_ROW_START_CHARPOS (row) == PT
11977 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
11978 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
11979 row > w->current_matrix->rows
11980 && (row-1)->ends_in_newline_from_string_p))))
11981 && (row->y > top_scroll_margin
11982 || CHARPOS (startp) == BEGV))
11983 {
11984 xassert (row->enabled_p);
11985 --row;
11986 }
11987
11988 /* Consider the following case: Window starts at BEGV,
11989 there is invisible, intangible text at BEGV, so that
11990 display starts at some point START > BEGV. It can
11991 happen that we are called with PT somewhere between
11992 BEGV and START. Try to handle that case. */
11993 if (row < w->current_matrix->rows
11994 || row->mode_line_p)
11995 {
11996 row = w->current_matrix->rows;
11997 if (row->mode_line_p)
11998 ++row;
11999 }
12000
12001 /* Due to newlines in overlay strings, we may have to
12002 skip forward over overlay strings. */
12003 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
12004 && MATRIX_ROW_END_CHARPOS (row) == PT
12005 && !cursor_row_p (w, row))
12006 ++row;
12007
12008 /* If within the scroll margin, scroll. */
12009 if (row->y < top_scroll_margin
12010 && CHARPOS (startp) != BEGV)
12011 scroll_p = 1;
12012 }
12013 else
12014 {
12015 /* Cursor did not move. So don't scroll even if cursor line
12016 is partially visible, as it was so before. */
12017 rc = CURSOR_MOVEMENT_SUCCESS;
12018 }
12019
12020 if (PT < MATRIX_ROW_START_CHARPOS (row)
12021 || PT > MATRIX_ROW_END_CHARPOS (row))
12022 {
12023 /* if PT is not in the glyph row, give up. */
12024 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12025 }
12026 else if (rc != CURSOR_MOVEMENT_SUCCESS
12027 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
12028 && make_cursor_line_fully_visible_p)
12029 {
12030 if (PT == MATRIX_ROW_END_CHARPOS (row)
12031 && !row->ends_at_zv_p
12032 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
12033 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12034 else if (row->height > window_box_height (w))
12035 {
12036 /* If we end up in a partially visible line, let's
12037 make it fully visible, except when it's taller
12038 than the window, in which case we can't do much
12039 about it. */
12040 *scroll_step = 1;
12041 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12042 }
12043 else
12044 {
12045 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
12046 if (!cursor_row_fully_visible_p (w, 0, 1))
12047 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12048 else
12049 rc = CURSOR_MOVEMENT_SUCCESS;
12050 }
12051 }
12052 else if (scroll_p)
12053 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12054 else
12055 {
12056 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
12057 rc = CURSOR_MOVEMENT_SUCCESS;
12058 }
12059 }
12060 }
12061
12062 return rc;
12063 }
12064
12065 void
12066 set_vertical_scroll_bar (w)
12067 struct window *w;
12068 {
12069 int start, end, whole;
12070
12071 /* Calculate the start and end positions for the current window.
12072 At some point, it would be nice to choose between scrollbars
12073 which reflect the whole buffer size, with special markers
12074 indicating narrowing, and scrollbars which reflect only the
12075 visible region.
12076
12077 Note that mini-buffers sometimes aren't displaying any text. */
12078 if (!MINI_WINDOW_P (w)
12079 || (w == XWINDOW (minibuf_window)
12080 && NILP (echo_area_buffer[0])))
12081 {
12082 struct buffer *buf = XBUFFER (w->buffer);
12083 whole = BUF_ZV (buf) - BUF_BEGV (buf);
12084 start = marker_position (w->start) - BUF_BEGV (buf);
12085 /* I don't think this is guaranteed to be right. For the
12086 moment, we'll pretend it is. */
12087 end = BUF_Z (buf) - XFASTINT (w->window_end_pos) - BUF_BEGV (buf);
12088
12089 if (end < start)
12090 end = start;
12091 if (whole < (end - start))
12092 whole = end - start;
12093 }
12094 else
12095 start = end = whole = 0;
12096
12097 /* Indicate what this scroll bar ought to be displaying now. */
12098 set_vertical_scroll_bar_hook (w, end - start, whole, start);
12099 }
12100
12101
12102 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
12103 selected_window is redisplayed.
12104
12105 We can return without actually redisplaying the window if
12106 fonts_changed_p is nonzero. In that case, redisplay_internal will
12107 retry. */
12108
12109 static void
12110 redisplay_window (window, just_this_one_p)
12111 Lisp_Object window;
12112 int just_this_one_p;
12113 {
12114 struct window *w = XWINDOW (window);
12115 struct frame *f = XFRAME (w->frame);
12116 struct buffer *buffer = XBUFFER (w->buffer);
12117 struct buffer *old = current_buffer;
12118 struct text_pos lpoint, opoint, startp;
12119 int update_mode_line;
12120 int tem;
12121 struct it it;
12122 /* Record it now because it's overwritten. */
12123 int current_matrix_up_to_date_p = 0;
12124 int used_current_matrix_p = 0;
12125 /* This is less strict than current_matrix_up_to_date_p.
12126 It indictes that the buffer contents and narrowing are unchanged. */
12127 int buffer_unchanged_p = 0;
12128 int temp_scroll_step = 0;
12129 int count = SPECPDL_INDEX ();
12130 int rc;
12131 int centering_position = -1;
12132 int last_line_misfit = 0;
12133
12134 SET_TEXT_POS (lpoint, PT, PT_BYTE);
12135 opoint = lpoint;
12136
12137 /* W must be a leaf window here. */
12138 xassert (!NILP (w->buffer));
12139 #if GLYPH_DEBUG
12140 *w->desired_matrix->method = 0;
12141 #endif
12142
12143 specbind (Qinhibit_point_motion_hooks, Qt);
12144
12145 reconsider_clip_changes (w, buffer);
12146
12147 /* Has the mode line to be updated? */
12148 update_mode_line = (!NILP (w->update_mode_line)
12149 || update_mode_lines
12150 || buffer->clip_changed
12151 || buffer->prevent_redisplay_optimizations_p);
12152
12153 if (MINI_WINDOW_P (w))
12154 {
12155 if (w == XWINDOW (echo_area_window)
12156 && !NILP (echo_area_buffer[0]))
12157 {
12158 if (update_mode_line)
12159 /* We may have to update a tty frame's menu bar or a
12160 tool-bar. Example `M-x C-h C-h C-g'. */
12161 goto finish_menu_bars;
12162 else
12163 /* We've already displayed the echo area glyphs in this window. */
12164 goto finish_scroll_bars;
12165 }
12166 else if ((w != XWINDOW (minibuf_window)
12167 || minibuf_level == 0)
12168 /* When buffer is nonempty, redisplay window normally. */
12169 && BUF_Z (XBUFFER (w->buffer)) == BUF_BEG (XBUFFER (w->buffer))
12170 /* Quail displays non-mini buffers in minibuffer window.
12171 In that case, redisplay the window normally. */
12172 && !NILP (Fmemq (w->buffer, Vminibuffer_list)))
12173 {
12174 /* W is a mini-buffer window, but it's not active, so clear
12175 it. */
12176 int yb = window_text_bottom_y (w);
12177 struct glyph_row *row;
12178 int y;
12179
12180 for (y = 0, row = w->desired_matrix->rows;
12181 y < yb;
12182 y += row->height, ++row)
12183 blank_row (w, row, y);
12184 goto finish_scroll_bars;
12185 }
12186
12187 clear_glyph_matrix (w->desired_matrix);
12188 }
12189
12190 /* Otherwise set up data on this window; select its buffer and point
12191 value. */
12192 /* Really select the buffer, for the sake of buffer-local
12193 variables. */
12194 set_buffer_internal_1 (XBUFFER (w->buffer));
12195 SET_TEXT_POS (opoint, PT, PT_BYTE);
12196
12197 current_matrix_up_to_date_p
12198 = (!NILP (w->window_end_valid)
12199 && !current_buffer->clip_changed
12200 && !current_buffer->prevent_redisplay_optimizations_p
12201 && XFASTINT (w->last_modified) >= MODIFF
12202 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
12203
12204 buffer_unchanged_p
12205 = (!NILP (w->window_end_valid)
12206 && !current_buffer->clip_changed
12207 && XFASTINT (w->last_modified) >= MODIFF
12208 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
12209
12210 /* When windows_or_buffers_changed is non-zero, we can't rely on
12211 the window end being valid, so set it to nil there. */
12212 if (windows_or_buffers_changed)
12213 {
12214 /* If window starts on a continuation line, maybe adjust the
12215 window start in case the window's width changed. */
12216 if (XMARKER (w->start)->buffer == current_buffer)
12217 compute_window_start_on_continuation_line (w);
12218
12219 w->window_end_valid = Qnil;
12220 }
12221
12222 /* Some sanity checks. */
12223 CHECK_WINDOW_END (w);
12224 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
12225 abort ();
12226 if (BYTEPOS (opoint) < CHARPOS (opoint))
12227 abort ();
12228
12229 /* If %c is in mode line, update it if needed. */
12230 if (!NILP (w->column_number_displayed)
12231 /* This alternative quickly identifies a common case
12232 where no change is needed. */
12233 && !(PT == XFASTINT (w->last_point)
12234 && XFASTINT (w->last_modified) >= MODIFF
12235 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
12236 && (XFASTINT (w->column_number_displayed)
12237 != (int) current_column ())) /* iftc */
12238 update_mode_line = 1;
12239
12240 /* Count number of windows showing the selected buffer. An indirect
12241 buffer counts as its base buffer. */
12242 if (!just_this_one_p)
12243 {
12244 struct buffer *current_base, *window_base;
12245 current_base = current_buffer;
12246 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
12247 if (current_base->base_buffer)
12248 current_base = current_base->base_buffer;
12249 if (window_base->base_buffer)
12250 window_base = window_base->base_buffer;
12251 if (current_base == window_base)
12252 buffer_shared++;
12253 }
12254
12255 /* Point refers normally to the selected window. For any other
12256 window, set up appropriate value. */
12257 if (!EQ (window, selected_window))
12258 {
12259 int new_pt = XMARKER (w->pointm)->charpos;
12260 int new_pt_byte = marker_byte_position (w->pointm);
12261 if (new_pt < BEGV)
12262 {
12263 new_pt = BEGV;
12264 new_pt_byte = BEGV_BYTE;
12265 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
12266 }
12267 else if (new_pt > (ZV - 1))
12268 {
12269 new_pt = ZV;
12270 new_pt_byte = ZV_BYTE;
12271 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
12272 }
12273
12274 /* We don't use SET_PT so that the point-motion hooks don't run. */
12275 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
12276 }
12277
12278 /* If any of the character widths specified in the display table
12279 have changed, invalidate the width run cache. It's true that
12280 this may be a bit late to catch such changes, but the rest of
12281 redisplay goes (non-fatally) haywire when the display table is
12282 changed, so why should we worry about doing any better? */
12283 if (current_buffer->width_run_cache)
12284 {
12285 struct Lisp_Char_Table *disptab = buffer_display_table ();
12286
12287 if (! disptab_matches_widthtab (disptab,
12288 XVECTOR (current_buffer->width_table)))
12289 {
12290 invalidate_region_cache (current_buffer,
12291 current_buffer->width_run_cache,
12292 BEG, Z);
12293 recompute_width_table (current_buffer, disptab);
12294 }
12295 }
12296
12297 /* If window-start is screwed up, choose a new one. */
12298 if (XMARKER (w->start)->buffer != current_buffer)
12299 goto recenter;
12300
12301 SET_TEXT_POS_FROM_MARKER (startp, w->start);
12302
12303 /* If someone specified a new starting point but did not insist,
12304 check whether it can be used. */
12305 if (!NILP (w->optional_new_start)
12306 && CHARPOS (startp) >= BEGV
12307 && CHARPOS (startp) <= ZV)
12308 {
12309 w->optional_new_start = Qnil;
12310 start_display (&it, w, startp);
12311 move_it_to (&it, PT, 0, it.last_visible_y, -1,
12312 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
12313 if (IT_CHARPOS (it) == PT)
12314 w->force_start = Qt;
12315 /* IT may overshoot PT if text at PT is invisible. */
12316 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
12317 w->force_start = Qt;
12318
12319
12320 }
12321
12322 /* Handle case where place to start displaying has been specified,
12323 unless the specified location is outside the accessible range. */
12324 if (!NILP (w->force_start)
12325 || w->frozen_window_start_p)
12326 {
12327 /* We set this later on if we have to adjust point. */
12328 int new_vpos = -1;
12329 int val;
12330
12331 w->force_start = Qnil;
12332 w->vscroll = 0;
12333 w->window_end_valid = Qnil;
12334
12335 /* Forget any recorded base line for line number display. */
12336 if (!buffer_unchanged_p)
12337 w->base_line_number = Qnil;
12338
12339 /* Redisplay the mode line. Select the buffer properly for that.
12340 Also, run the hook window-scroll-functions
12341 because we have scrolled. */
12342 /* Note, we do this after clearing force_start because
12343 if there's an error, it is better to forget about force_start
12344 than to get into an infinite loop calling the hook functions
12345 and having them get more errors. */
12346 if (!update_mode_line
12347 || ! NILP (Vwindow_scroll_functions))
12348 {
12349 update_mode_line = 1;
12350 w->update_mode_line = Qt;
12351 startp = run_window_scroll_functions (window, startp);
12352 }
12353
12354 w->last_modified = make_number (0);
12355 w->last_overlay_modified = make_number (0);
12356 if (CHARPOS (startp) < BEGV)
12357 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
12358 else if (CHARPOS (startp) > ZV)
12359 SET_TEXT_POS (startp, ZV, ZV_BYTE);
12360
12361 /* Redisplay, then check if cursor has been set during the
12362 redisplay. Give up if new fonts were loaded. */
12363 val = try_window (window, startp, 1);
12364 if (!val)
12365 {
12366 w->force_start = Qt;
12367 clear_glyph_matrix (w->desired_matrix);
12368 goto need_larger_matrices;
12369 }
12370 /* Point was outside the scroll margins. */
12371 if (val < 0)
12372 new_vpos = window_box_height (w) / 2;
12373
12374 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
12375 {
12376 /* If point does not appear, try to move point so it does
12377 appear. The desired matrix has been built above, so we
12378 can use it here. */
12379 new_vpos = window_box_height (w) / 2;
12380 }
12381
12382 if (!cursor_row_fully_visible_p (w, 0, 0))
12383 {
12384 /* Point does appear, but on a line partly visible at end of window.
12385 Move it back to a fully-visible line. */
12386 new_vpos = window_box_height (w);
12387 }
12388
12389 /* If we need to move point for either of the above reasons,
12390 now actually do it. */
12391 if (new_vpos >= 0)
12392 {
12393 struct glyph_row *row;
12394
12395 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
12396 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
12397 ++row;
12398
12399 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
12400 MATRIX_ROW_START_BYTEPOS (row));
12401
12402 if (w != XWINDOW (selected_window))
12403 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
12404 else if (current_buffer == old)
12405 SET_TEXT_POS (lpoint, PT, PT_BYTE);
12406
12407 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
12408
12409 /* If we are highlighting the region, then we just changed
12410 the region, so redisplay to show it. */
12411 if (!NILP (Vtransient_mark_mode)
12412 && !NILP (current_buffer->mark_active))
12413 {
12414 clear_glyph_matrix (w->desired_matrix);
12415 if (!try_window (window, startp, 0))
12416 goto need_larger_matrices;
12417 }
12418 }
12419
12420 #if GLYPH_DEBUG
12421 debug_method_add (w, "forced window start");
12422 #endif
12423 goto done;
12424 }
12425
12426 /* Handle case where text has not changed, only point, and it has
12427 not moved off the frame, and we are not retrying after hscroll.
12428 (current_matrix_up_to_date_p is nonzero when retrying.) */
12429 if (current_matrix_up_to_date_p
12430 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
12431 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
12432 {
12433 switch (rc)
12434 {
12435 case CURSOR_MOVEMENT_SUCCESS:
12436 used_current_matrix_p = 1;
12437 goto done;
12438
12439 #if 0 /* try_cursor_movement never returns this value. */
12440 case CURSOR_MOVEMENT_NEED_LARGER_MATRICES:
12441 goto need_larger_matrices;
12442 #endif
12443
12444 case CURSOR_MOVEMENT_MUST_SCROLL:
12445 goto try_to_scroll;
12446
12447 default:
12448 abort ();
12449 }
12450 }
12451 /* If current starting point was originally the beginning of a line
12452 but no longer is, find a new starting point. */
12453 else if (!NILP (w->start_at_line_beg)
12454 && !(CHARPOS (startp) <= BEGV
12455 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
12456 {
12457 #if GLYPH_DEBUG
12458 debug_method_add (w, "recenter 1");
12459 #endif
12460 goto recenter;
12461 }
12462
12463 /* Try scrolling with try_window_id. Value is > 0 if update has
12464 been done, it is -1 if we know that the same window start will
12465 not work. It is 0 if unsuccessful for some other reason. */
12466 else if ((tem = try_window_id (w)) != 0)
12467 {
12468 #if GLYPH_DEBUG
12469 debug_method_add (w, "try_window_id %d", tem);
12470 #endif
12471
12472 if (fonts_changed_p)
12473 goto need_larger_matrices;
12474 if (tem > 0)
12475 goto done;
12476
12477 /* Otherwise try_window_id has returned -1 which means that we
12478 don't want the alternative below this comment to execute. */
12479 }
12480 else if (CHARPOS (startp) >= BEGV
12481 && CHARPOS (startp) <= ZV
12482 && PT >= CHARPOS (startp)
12483 && (CHARPOS (startp) < ZV
12484 /* Avoid starting at end of buffer. */
12485 || CHARPOS (startp) == BEGV
12486 || (XFASTINT (w->last_modified) >= MODIFF
12487 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
12488 {
12489 #if GLYPH_DEBUG
12490 debug_method_add (w, "same window start");
12491 #endif
12492
12493 /* Try to redisplay starting at same place as before.
12494 If point has not moved off frame, accept the results. */
12495 if (!current_matrix_up_to_date_p
12496 /* Don't use try_window_reusing_current_matrix in this case
12497 because a window scroll function can have changed the
12498 buffer. */
12499 || !NILP (Vwindow_scroll_functions)
12500 || MINI_WINDOW_P (w)
12501 || !(used_current_matrix_p
12502 = try_window_reusing_current_matrix (w)))
12503 {
12504 IF_DEBUG (debug_method_add (w, "1"));
12505 if (try_window (window, startp, 1) < 0)
12506 /* -1 means we need to scroll.
12507 0 means we need new matrices, but fonts_changed_p
12508 is set in that case, so we will detect it below. */
12509 goto try_to_scroll;
12510 }
12511
12512 if (fonts_changed_p)
12513 goto need_larger_matrices;
12514
12515 if (w->cursor.vpos >= 0)
12516 {
12517 if (!just_this_one_p
12518 || current_buffer->clip_changed
12519 || BEG_UNCHANGED < CHARPOS (startp))
12520 /* Forget any recorded base line for line number display. */
12521 w->base_line_number = Qnil;
12522
12523 if (!cursor_row_fully_visible_p (w, 1, 0))
12524 {
12525 clear_glyph_matrix (w->desired_matrix);
12526 last_line_misfit = 1;
12527 }
12528 /* Drop through and scroll. */
12529 else
12530 goto done;
12531 }
12532 else
12533 clear_glyph_matrix (w->desired_matrix);
12534 }
12535
12536 try_to_scroll:
12537
12538 w->last_modified = make_number (0);
12539 w->last_overlay_modified = make_number (0);
12540
12541 /* Redisplay the mode line. Select the buffer properly for that. */
12542 if (!update_mode_line)
12543 {
12544 update_mode_line = 1;
12545 w->update_mode_line = Qt;
12546 }
12547
12548 /* Try to scroll by specified few lines. */
12549 if ((scroll_conservatively
12550 || scroll_step
12551 || temp_scroll_step
12552 || NUMBERP (current_buffer->scroll_up_aggressively)
12553 || NUMBERP (current_buffer->scroll_down_aggressively))
12554 && !current_buffer->clip_changed
12555 && CHARPOS (startp) >= BEGV
12556 && CHARPOS (startp) <= ZV)
12557 {
12558 /* The function returns -1 if new fonts were loaded, 1 if
12559 successful, 0 if not successful. */
12560 int rc = try_scrolling (window, just_this_one_p,
12561 scroll_conservatively,
12562 scroll_step,
12563 temp_scroll_step, last_line_misfit);
12564 switch (rc)
12565 {
12566 case SCROLLING_SUCCESS:
12567 goto done;
12568
12569 case SCROLLING_NEED_LARGER_MATRICES:
12570 goto need_larger_matrices;
12571
12572 case SCROLLING_FAILED:
12573 break;
12574
12575 default:
12576 abort ();
12577 }
12578 }
12579
12580 /* Finally, just choose place to start which centers point */
12581
12582 recenter:
12583 if (centering_position < 0)
12584 centering_position = window_box_height (w) / 2;
12585
12586 #if GLYPH_DEBUG
12587 debug_method_add (w, "recenter");
12588 #endif
12589
12590 /* w->vscroll = 0; */
12591
12592 /* Forget any previously recorded base line for line number display. */
12593 if (!buffer_unchanged_p)
12594 w->base_line_number = Qnil;
12595
12596 /* Move backward half the height of the window. */
12597 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
12598 it.current_y = it.last_visible_y;
12599 move_it_vertically_backward (&it, centering_position);
12600 xassert (IT_CHARPOS (it) >= BEGV);
12601
12602 /* The function move_it_vertically_backward may move over more
12603 than the specified y-distance. If it->w is small, e.g. a
12604 mini-buffer window, we may end up in front of the window's
12605 display area. Start displaying at the start of the line
12606 containing PT in this case. */
12607 if (it.current_y <= 0)
12608 {
12609 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
12610 move_it_vertically_backward (&it, 0);
12611 #if 0
12612 /* I think this assert is bogus if buffer contains
12613 invisible text or images. KFS. */
12614 xassert (IT_CHARPOS (it) <= PT);
12615 #endif
12616 it.current_y = 0;
12617 }
12618
12619 it.current_x = it.hpos = 0;
12620
12621 /* Set startp here explicitly in case that helps avoid an infinite loop
12622 in case the window-scroll-functions functions get errors. */
12623 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
12624
12625 /* Run scroll hooks. */
12626 startp = run_window_scroll_functions (window, it.current.pos);
12627
12628 /* Redisplay the window. */
12629 if (!current_matrix_up_to_date_p
12630 || windows_or_buffers_changed
12631 || cursor_type_changed
12632 /* Don't use try_window_reusing_current_matrix in this case
12633 because it can have changed the buffer. */
12634 || !NILP (Vwindow_scroll_functions)
12635 || !just_this_one_p
12636 || MINI_WINDOW_P (w)
12637 || !(used_current_matrix_p
12638 = try_window_reusing_current_matrix (w)))
12639 try_window (window, startp, 0);
12640
12641 /* If new fonts have been loaded (due to fontsets), give up. We
12642 have to start a new redisplay since we need to re-adjust glyph
12643 matrices. */
12644 if (fonts_changed_p)
12645 goto need_larger_matrices;
12646
12647 /* If cursor did not appear assume that the middle of the window is
12648 in the first line of the window. Do it again with the next line.
12649 (Imagine a window of height 100, displaying two lines of height
12650 60. Moving back 50 from it->last_visible_y will end in the first
12651 line.) */
12652 if (w->cursor.vpos < 0)
12653 {
12654 if (!NILP (w->window_end_valid)
12655 && PT >= Z - XFASTINT (w->window_end_pos))
12656 {
12657 clear_glyph_matrix (w->desired_matrix);
12658 move_it_by_lines (&it, 1, 0);
12659 try_window (window, it.current.pos, 0);
12660 }
12661 else if (PT < IT_CHARPOS (it))
12662 {
12663 clear_glyph_matrix (w->desired_matrix);
12664 move_it_by_lines (&it, -1, 0);
12665 try_window (window, it.current.pos, 0);
12666 }
12667 else
12668 {
12669 /* Not much we can do about it. */
12670 }
12671 }
12672
12673 /* Consider the following case: Window starts at BEGV, there is
12674 invisible, intangible text at BEGV, so that display starts at
12675 some point START > BEGV. It can happen that we are called with
12676 PT somewhere between BEGV and START. Try to handle that case. */
12677 if (w->cursor.vpos < 0)
12678 {
12679 struct glyph_row *row = w->current_matrix->rows;
12680 if (row->mode_line_p)
12681 ++row;
12682 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
12683 }
12684
12685 if (!cursor_row_fully_visible_p (w, 0, 0))
12686 {
12687 /* If vscroll is enabled, disable it and try again. */
12688 if (w->vscroll)
12689 {
12690 w->vscroll = 0;
12691 clear_glyph_matrix (w->desired_matrix);
12692 goto recenter;
12693 }
12694
12695 /* If centering point failed to make the whole line visible,
12696 put point at the top instead. That has to make the whole line
12697 visible, if it can be done. */
12698 if (centering_position == 0)
12699 goto done;
12700
12701 clear_glyph_matrix (w->desired_matrix);
12702 centering_position = 0;
12703 goto recenter;
12704 }
12705
12706 done:
12707
12708 SET_TEXT_POS_FROM_MARKER (startp, w->start);
12709 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
12710 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
12711 ? Qt : Qnil);
12712
12713 /* Display the mode line, if we must. */
12714 if ((update_mode_line
12715 /* If window not full width, must redo its mode line
12716 if (a) the window to its side is being redone and
12717 (b) we do a frame-based redisplay. This is a consequence
12718 of how inverted lines are drawn in frame-based redisplay. */
12719 || (!just_this_one_p
12720 && !FRAME_WINDOW_P (f)
12721 && !WINDOW_FULL_WIDTH_P (w))
12722 /* Line number to display. */
12723 || INTEGERP (w->base_line_pos)
12724 /* Column number is displayed and different from the one displayed. */
12725 || (!NILP (w->column_number_displayed)
12726 && (XFASTINT (w->column_number_displayed)
12727 != (int) current_column ()))) /* iftc */
12728 /* This means that the window has a mode line. */
12729 && (WINDOW_WANTS_MODELINE_P (w)
12730 || WINDOW_WANTS_HEADER_LINE_P (w)))
12731 {
12732 display_mode_lines (w);
12733
12734 /* If mode line height has changed, arrange for a thorough
12735 immediate redisplay using the correct mode line height. */
12736 if (WINDOW_WANTS_MODELINE_P (w)
12737 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
12738 {
12739 fonts_changed_p = 1;
12740 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
12741 = DESIRED_MODE_LINE_HEIGHT (w);
12742 }
12743
12744 /* If top line height has changed, arrange for a thorough
12745 immediate redisplay using the correct mode line height. */
12746 if (WINDOW_WANTS_HEADER_LINE_P (w)
12747 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
12748 {
12749 fonts_changed_p = 1;
12750 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
12751 = DESIRED_HEADER_LINE_HEIGHT (w);
12752 }
12753
12754 if (fonts_changed_p)
12755 goto need_larger_matrices;
12756 }
12757
12758 if (!line_number_displayed
12759 && !BUFFERP (w->base_line_pos))
12760 {
12761 w->base_line_pos = Qnil;
12762 w->base_line_number = Qnil;
12763 }
12764
12765 finish_menu_bars:
12766
12767 /* When we reach a frame's selected window, redo the frame's menu bar. */
12768 if (update_mode_line
12769 && EQ (FRAME_SELECTED_WINDOW (f), window))
12770 {
12771 int redisplay_menu_p = 0;
12772 int redisplay_tool_bar_p = 0;
12773
12774 if (FRAME_WINDOW_P (f))
12775 {
12776 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
12777 || defined (USE_GTK)
12778 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
12779 #else
12780 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
12781 #endif
12782 }
12783 else
12784 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
12785
12786 if (redisplay_menu_p)
12787 display_menu_bar (w);
12788
12789 #ifdef HAVE_WINDOW_SYSTEM
12790 #ifdef USE_GTK
12791 redisplay_tool_bar_p = FRAME_EXTERNAL_TOOL_BAR (f);
12792 #else
12793 redisplay_tool_bar_p = WINDOWP (f->tool_bar_window)
12794 && (FRAME_TOOL_BAR_LINES (f) > 0
12795 || auto_resize_tool_bars_p);
12796
12797 #endif
12798
12799 if (redisplay_tool_bar_p)
12800 redisplay_tool_bar (f);
12801 #endif
12802 }
12803
12804 #ifdef HAVE_WINDOW_SYSTEM
12805 if (FRAME_WINDOW_P (f)
12806 && update_window_fringes (w, 0)
12807 && !just_this_one_p
12808 && (used_current_matrix_p || overlay_arrow_seen)
12809 && !w->pseudo_window_p)
12810 {
12811 update_begin (f);
12812 BLOCK_INPUT;
12813 if (draw_window_fringes (w, 1))
12814 x_draw_vertical_border (w);
12815 UNBLOCK_INPUT;
12816 update_end (f);
12817 }
12818 #endif /* HAVE_WINDOW_SYSTEM */
12819
12820 /* We go to this label, with fonts_changed_p nonzero,
12821 if it is necessary to try again using larger glyph matrices.
12822 We have to redeem the scroll bar even in this case,
12823 because the loop in redisplay_internal expects that. */
12824 need_larger_matrices:
12825 ;
12826 finish_scroll_bars:
12827
12828 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
12829 {
12830 /* Set the thumb's position and size. */
12831 set_vertical_scroll_bar (w);
12832
12833 /* Note that we actually used the scroll bar attached to this
12834 window, so it shouldn't be deleted at the end of redisplay. */
12835 redeem_scroll_bar_hook (w);
12836 }
12837
12838 /* Restore current_buffer and value of point in it. */
12839 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
12840 set_buffer_internal_1 (old);
12841 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
12842
12843 unbind_to (count, Qnil);
12844 }
12845
12846
12847 /* Build the complete desired matrix of WINDOW with a window start
12848 buffer position POS.
12849
12850 Value is 1 if successful. It is zero if fonts were loaded during
12851 redisplay which makes re-adjusting glyph matrices necessary, and -1
12852 if point would appear in the scroll margins.
12853 (We check that only if CHECK_MARGINS is nonzero. */
12854
12855 int
12856 try_window (window, pos, check_margins)
12857 Lisp_Object window;
12858 struct text_pos pos;
12859 int check_margins;
12860 {
12861 struct window *w = XWINDOW (window);
12862 struct it it;
12863 struct glyph_row *last_text_row = NULL;
12864
12865 /* Make POS the new window start. */
12866 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
12867
12868 /* Mark cursor position as unknown. No overlay arrow seen. */
12869 w->cursor.vpos = -1;
12870 overlay_arrow_seen = 0;
12871
12872 /* Initialize iterator and info to start at POS. */
12873 start_display (&it, w, pos);
12874
12875 /* Display all lines of W. */
12876 while (it.current_y < it.last_visible_y)
12877 {
12878 if (display_line (&it))
12879 last_text_row = it.glyph_row - 1;
12880 if (fonts_changed_p)
12881 return 0;
12882 }
12883
12884 /* Don't let the cursor end in the scroll margins. */
12885 if (check_margins
12886 && !MINI_WINDOW_P (w))
12887 {
12888 int this_scroll_margin, cursor_height;
12889
12890 this_scroll_margin = max (0, scroll_margin);
12891 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
12892 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
12893 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
12894
12895 if ((w->cursor.y < this_scroll_margin
12896 && CHARPOS (pos) > BEGV)
12897 /* rms: considering make_cursor_line_fully_visible_p here
12898 seems to give wrong results. We don't want to recenter
12899 when the last line is partly visible, we want to allow
12900 that case to be handled in the usual way. */
12901 || (w->cursor.y + 1) > it.last_visible_y)
12902 {
12903 w->cursor.vpos = -1;
12904 clear_glyph_matrix (w->desired_matrix);
12905 return -1;
12906 }
12907 }
12908
12909 /* If bottom moved off end of frame, change mode line percentage. */
12910 if (XFASTINT (w->window_end_pos) <= 0
12911 && Z != IT_CHARPOS (it))
12912 w->update_mode_line = Qt;
12913
12914 /* Set window_end_pos to the offset of the last character displayed
12915 on the window from the end of current_buffer. Set
12916 window_end_vpos to its row number. */
12917 if (last_text_row)
12918 {
12919 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
12920 w->window_end_bytepos
12921 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
12922 w->window_end_pos
12923 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
12924 w->window_end_vpos
12925 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
12926 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
12927 ->displays_text_p);
12928 }
12929 else
12930 {
12931 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
12932 w->window_end_pos = make_number (Z - ZV);
12933 w->window_end_vpos = make_number (0);
12934 }
12935
12936 /* But that is not valid info until redisplay finishes. */
12937 w->window_end_valid = Qnil;
12938 return 1;
12939 }
12940
12941
12942 \f
12943 /************************************************************************
12944 Window redisplay reusing current matrix when buffer has not changed
12945 ************************************************************************/
12946
12947 /* Try redisplay of window W showing an unchanged buffer with a
12948 different window start than the last time it was displayed by
12949 reusing its current matrix. Value is non-zero if successful.
12950 W->start is the new window start. */
12951
12952 static int
12953 try_window_reusing_current_matrix (w)
12954 struct window *w;
12955 {
12956 struct frame *f = XFRAME (w->frame);
12957 struct glyph_row *row, *bottom_row;
12958 struct it it;
12959 struct run run;
12960 struct text_pos start, new_start;
12961 int nrows_scrolled, i;
12962 struct glyph_row *last_text_row;
12963 struct glyph_row *last_reused_text_row;
12964 struct glyph_row *start_row;
12965 int start_vpos, min_y, max_y;
12966
12967 #if GLYPH_DEBUG
12968 if (inhibit_try_window_reusing)
12969 return 0;
12970 #endif
12971
12972 if (/* This function doesn't handle terminal frames. */
12973 !FRAME_WINDOW_P (f)
12974 /* Don't try to reuse the display if windows have been split
12975 or such. */
12976 || windows_or_buffers_changed
12977 || cursor_type_changed)
12978 return 0;
12979
12980 /* Can't do this if region may have changed. */
12981 if ((!NILP (Vtransient_mark_mode)
12982 && !NILP (current_buffer->mark_active))
12983 || !NILP (w->region_showing)
12984 || !NILP (Vshow_trailing_whitespace))
12985 return 0;
12986
12987 /* If top-line visibility has changed, give up. */
12988 if (WINDOW_WANTS_HEADER_LINE_P (w)
12989 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
12990 return 0;
12991
12992 /* Give up if old or new display is scrolled vertically. We could
12993 make this function handle this, but right now it doesn't. */
12994 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
12995 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
12996 return 0;
12997
12998 /* The variable new_start now holds the new window start. The old
12999 start `start' can be determined from the current matrix. */
13000 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
13001 start = start_row->start.pos;
13002 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
13003
13004 /* Clear the desired matrix for the display below. */
13005 clear_glyph_matrix (w->desired_matrix);
13006
13007 if (CHARPOS (new_start) <= CHARPOS (start))
13008 {
13009 int first_row_y;
13010
13011 /* Don't use this method if the display starts with an ellipsis
13012 displayed for invisible text. It's not easy to handle that case
13013 below, and it's certainly not worth the effort since this is
13014 not a frequent case. */
13015 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
13016 return 0;
13017
13018 IF_DEBUG (debug_method_add (w, "twu1"));
13019
13020 /* Display up to a row that can be reused. The variable
13021 last_text_row is set to the last row displayed that displays
13022 text. Note that it.vpos == 0 if or if not there is a
13023 header-line; it's not the same as the MATRIX_ROW_VPOS! */
13024 start_display (&it, w, new_start);
13025 first_row_y = it.current_y;
13026 w->cursor.vpos = -1;
13027 last_text_row = last_reused_text_row = NULL;
13028
13029 while (it.current_y < it.last_visible_y
13030 && !fonts_changed_p)
13031 {
13032 /* If we have reached into the characters in the START row,
13033 that means the line boundaries have changed. So we
13034 can't start copying with the row START. Maybe it will
13035 work to start copying with the following row. */
13036 while (IT_CHARPOS (it) > CHARPOS (start))
13037 {
13038 /* Advance to the next row as the "start". */
13039 start_row++;
13040 start = start_row->start.pos;
13041 /* If there are no more rows to try, or just one, give up. */
13042 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
13043 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
13044 || CHARPOS (start) == ZV)
13045 {
13046 clear_glyph_matrix (w->desired_matrix);
13047 return 0;
13048 }
13049
13050 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
13051 }
13052 /* If we have reached alignment,
13053 we can copy the rest of the rows. */
13054 if (IT_CHARPOS (it) == CHARPOS (start))
13055 break;
13056
13057 if (display_line (&it))
13058 last_text_row = it.glyph_row - 1;
13059 }
13060
13061 /* A value of current_y < last_visible_y means that we stopped
13062 at the previous window start, which in turn means that we
13063 have at least one reusable row. */
13064 if (it.current_y < it.last_visible_y)
13065 {
13066 /* IT.vpos always starts from 0; it counts text lines. */
13067 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
13068
13069 /* Find PT if not already found in the lines displayed. */
13070 if (w->cursor.vpos < 0)
13071 {
13072 int dy = it.current_y - start_row->y;
13073
13074 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
13075 row = row_containing_pos (w, PT, row, NULL, dy);
13076 if (row)
13077 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
13078 dy, nrows_scrolled);
13079 else
13080 {
13081 clear_glyph_matrix (w->desired_matrix);
13082 return 0;
13083 }
13084 }
13085
13086 /* Scroll the display. Do it before the current matrix is
13087 changed. The problem here is that update has not yet
13088 run, i.e. part of the current matrix is not up to date.
13089 scroll_run_hook will clear the cursor, and use the
13090 current matrix to get the height of the row the cursor is
13091 in. */
13092 run.current_y = start_row->y;
13093 run.desired_y = it.current_y;
13094 run.height = it.last_visible_y - it.current_y;
13095
13096 if (run.height > 0 && run.current_y != run.desired_y)
13097 {
13098 update_begin (f);
13099 rif->update_window_begin_hook (w);
13100 rif->clear_window_mouse_face (w);
13101 rif->scroll_run_hook (w, &run);
13102 rif->update_window_end_hook (w, 0, 0);
13103 update_end (f);
13104 }
13105
13106 /* Shift current matrix down by nrows_scrolled lines. */
13107 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
13108 rotate_matrix (w->current_matrix,
13109 start_vpos,
13110 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
13111 nrows_scrolled);
13112
13113 /* Disable lines that must be updated. */
13114 for (i = 0; i < it.vpos; ++i)
13115 (start_row + i)->enabled_p = 0;
13116
13117 /* Re-compute Y positions. */
13118 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
13119 max_y = it.last_visible_y;
13120 for (row = start_row + nrows_scrolled;
13121 row < bottom_row;
13122 ++row)
13123 {
13124 row->y = it.current_y;
13125 row->visible_height = row->height;
13126
13127 if (row->y < min_y)
13128 row->visible_height -= min_y - row->y;
13129 if (row->y + row->height > max_y)
13130 row->visible_height -= row->y + row->height - max_y;
13131 row->redraw_fringe_bitmaps_p = 1;
13132
13133 it.current_y += row->height;
13134
13135 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
13136 last_reused_text_row = row;
13137 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
13138 break;
13139 }
13140
13141 /* Disable lines in the current matrix which are now
13142 below the window. */
13143 for (++row; row < bottom_row; ++row)
13144 row->enabled_p = 0;
13145 }
13146
13147 /* Update window_end_pos etc.; last_reused_text_row is the last
13148 reused row from the current matrix containing text, if any.
13149 The value of last_text_row is the last displayed line
13150 containing text. */
13151 if (last_reused_text_row)
13152 {
13153 w->window_end_bytepos
13154 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
13155 w->window_end_pos
13156 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
13157 w->window_end_vpos
13158 = make_number (MATRIX_ROW_VPOS (last_reused_text_row,
13159 w->current_matrix));
13160 }
13161 else if (last_text_row)
13162 {
13163 w->window_end_bytepos
13164 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
13165 w->window_end_pos
13166 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
13167 w->window_end_vpos
13168 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
13169 }
13170 else
13171 {
13172 /* This window must be completely empty. */
13173 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
13174 w->window_end_pos = make_number (Z - ZV);
13175 w->window_end_vpos = make_number (0);
13176 }
13177 w->window_end_valid = Qnil;
13178
13179 /* Update hint: don't try scrolling again in update_window. */
13180 w->desired_matrix->no_scrolling_p = 1;
13181
13182 #if GLYPH_DEBUG
13183 debug_method_add (w, "try_window_reusing_current_matrix 1");
13184 #endif
13185 return 1;
13186 }
13187 else if (CHARPOS (new_start) > CHARPOS (start))
13188 {
13189 struct glyph_row *pt_row, *row;
13190 struct glyph_row *first_reusable_row;
13191 struct glyph_row *first_row_to_display;
13192 int dy;
13193 int yb = window_text_bottom_y (w);
13194
13195 /* Find the row starting at new_start, if there is one. Don't
13196 reuse a partially visible line at the end. */
13197 first_reusable_row = start_row;
13198 while (first_reusable_row->enabled_p
13199 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
13200 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
13201 < CHARPOS (new_start)))
13202 ++first_reusable_row;
13203
13204 /* Give up if there is no row to reuse. */
13205 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
13206 || !first_reusable_row->enabled_p
13207 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
13208 != CHARPOS (new_start)))
13209 return 0;
13210
13211 /* We can reuse fully visible rows beginning with
13212 first_reusable_row to the end of the window. Set
13213 first_row_to_display to the first row that cannot be reused.
13214 Set pt_row to the row containing point, if there is any. */
13215 pt_row = NULL;
13216 for (first_row_to_display = first_reusable_row;
13217 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
13218 ++first_row_to_display)
13219 {
13220 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
13221 && PT < MATRIX_ROW_END_CHARPOS (first_row_to_display))
13222 pt_row = first_row_to_display;
13223 }
13224
13225 /* Start displaying at the start of first_row_to_display. */
13226 xassert (first_row_to_display->y < yb);
13227 init_to_row_start (&it, w, first_row_to_display);
13228
13229 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
13230 - start_vpos);
13231 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
13232 - nrows_scrolled);
13233 it.current_y = (first_row_to_display->y - first_reusable_row->y
13234 + WINDOW_HEADER_LINE_HEIGHT (w));
13235
13236 /* Display lines beginning with first_row_to_display in the
13237 desired matrix. Set last_text_row to the last row displayed
13238 that displays text. */
13239 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
13240 if (pt_row == NULL)
13241 w->cursor.vpos = -1;
13242 last_text_row = NULL;
13243 while (it.current_y < it.last_visible_y && !fonts_changed_p)
13244 if (display_line (&it))
13245 last_text_row = it.glyph_row - 1;
13246
13247 /* Give up If point isn't in a row displayed or reused. */
13248 if (w->cursor.vpos < 0)
13249 {
13250 clear_glyph_matrix (w->desired_matrix);
13251 return 0;
13252 }
13253
13254 /* If point is in a reused row, adjust y and vpos of the cursor
13255 position. */
13256 if (pt_row)
13257 {
13258 w->cursor.vpos -= nrows_scrolled;
13259 w->cursor.y -= first_reusable_row->y - start_row->y;
13260 }
13261
13262 /* Scroll the display. */
13263 run.current_y = first_reusable_row->y;
13264 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
13265 run.height = it.last_visible_y - run.current_y;
13266 dy = run.current_y - run.desired_y;
13267
13268 if (run.height)
13269 {
13270 update_begin (f);
13271 rif->update_window_begin_hook (w);
13272 rif->clear_window_mouse_face (w);
13273 rif->scroll_run_hook (w, &run);
13274 rif->update_window_end_hook (w, 0, 0);
13275 update_end (f);
13276 }
13277
13278 /* Adjust Y positions of reused rows. */
13279 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
13280 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
13281 max_y = it.last_visible_y;
13282 for (row = first_reusable_row; row < first_row_to_display; ++row)
13283 {
13284 row->y -= dy;
13285 row->visible_height = row->height;
13286 if (row->y < min_y)
13287 row->visible_height -= min_y - row->y;
13288 if (row->y + row->height > max_y)
13289 row->visible_height -= row->y + row->height - max_y;
13290 row->redraw_fringe_bitmaps_p = 1;
13291 }
13292
13293 /* Scroll the current matrix. */
13294 xassert (nrows_scrolled > 0);
13295 rotate_matrix (w->current_matrix,
13296 start_vpos,
13297 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
13298 -nrows_scrolled);
13299
13300 /* Disable rows not reused. */
13301 for (row -= nrows_scrolled; row < bottom_row; ++row)
13302 row->enabled_p = 0;
13303
13304 /* Point may have moved to a different line, so we cannot assume that
13305 the previous cursor position is valid; locate the correct row. */
13306 if (pt_row)
13307 {
13308 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
13309 row < bottom_row && PT >= MATRIX_ROW_END_CHARPOS (row);
13310 row++)
13311 {
13312 w->cursor.vpos++;
13313 w->cursor.y = row->y;
13314 }
13315 if (row < bottom_row)
13316 {
13317 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
13318 while (glyph->charpos < PT)
13319 {
13320 w->cursor.hpos++;
13321 w->cursor.x += glyph->pixel_width;
13322 glyph++;
13323 }
13324 }
13325 }
13326
13327 /* Adjust window end. A null value of last_text_row means that
13328 the window end is in reused rows which in turn means that
13329 only its vpos can have changed. */
13330 if (last_text_row)
13331 {
13332 w->window_end_bytepos
13333 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
13334 w->window_end_pos
13335 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
13336 w->window_end_vpos
13337 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
13338 }
13339 else
13340 {
13341 w->window_end_vpos
13342 = make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled);
13343 }
13344
13345 w->window_end_valid = Qnil;
13346 w->desired_matrix->no_scrolling_p = 1;
13347
13348 #if GLYPH_DEBUG
13349 debug_method_add (w, "try_window_reusing_current_matrix 2");
13350 #endif
13351 return 1;
13352 }
13353
13354 return 0;
13355 }
13356
13357
13358 \f
13359 /************************************************************************
13360 Window redisplay reusing current matrix when buffer has changed
13361 ************************************************************************/
13362
13363 static struct glyph_row *find_last_unchanged_at_beg_row P_ ((struct window *));
13364 static struct glyph_row *find_first_unchanged_at_end_row P_ ((struct window *,
13365 int *, int *));
13366 static struct glyph_row *
13367 find_last_row_displaying_text P_ ((struct glyph_matrix *, struct it *,
13368 struct glyph_row *));
13369
13370
13371 /* Return the last row in MATRIX displaying text. If row START is
13372 non-null, start searching with that row. IT gives the dimensions
13373 of the display. Value is null if matrix is empty; otherwise it is
13374 a pointer to the row found. */
13375
13376 static struct glyph_row *
13377 find_last_row_displaying_text (matrix, it, start)
13378 struct glyph_matrix *matrix;
13379 struct it *it;
13380 struct glyph_row *start;
13381 {
13382 struct glyph_row *row, *row_found;
13383
13384 /* Set row_found to the last row in IT->w's current matrix
13385 displaying text. The loop looks funny but think of partially
13386 visible lines. */
13387 row_found = NULL;
13388 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
13389 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
13390 {
13391 xassert (row->enabled_p);
13392 row_found = row;
13393 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
13394 break;
13395 ++row;
13396 }
13397
13398 return row_found;
13399 }
13400
13401
13402 /* Return the last row in the current matrix of W that is not affected
13403 by changes at the start of current_buffer that occurred since W's
13404 current matrix was built. Value is null if no such row exists.
13405
13406 BEG_UNCHANGED us the number of characters unchanged at the start of
13407 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
13408 first changed character in current_buffer. Characters at positions <
13409 BEG + BEG_UNCHANGED are at the same buffer positions as they were
13410 when the current matrix was built. */
13411
13412 static struct glyph_row *
13413 find_last_unchanged_at_beg_row (w)
13414 struct window *w;
13415 {
13416 int first_changed_pos = BEG + BEG_UNCHANGED;
13417 struct glyph_row *row;
13418 struct glyph_row *row_found = NULL;
13419 int yb = window_text_bottom_y (w);
13420
13421 /* Find the last row displaying unchanged text. */
13422 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
13423 while (MATRIX_ROW_DISPLAYS_TEXT_P (row)
13424 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos)
13425 {
13426 if (/* If row ends before first_changed_pos, it is unchanged,
13427 except in some case. */
13428 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
13429 /* When row ends in ZV and we write at ZV it is not
13430 unchanged. */
13431 && !row->ends_at_zv_p
13432 /* When first_changed_pos is the end of a continued line,
13433 row is not unchanged because it may be no longer
13434 continued. */
13435 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
13436 && (row->continued_p
13437 || row->exact_window_width_line_p)))
13438 row_found = row;
13439
13440 /* Stop if last visible row. */
13441 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
13442 break;
13443
13444 ++row;
13445 }
13446
13447 return row_found;
13448 }
13449
13450
13451 /* Find the first glyph row in the current matrix of W that is not
13452 affected by changes at the end of current_buffer since the
13453 time W's current matrix was built.
13454
13455 Return in *DELTA the number of chars by which buffer positions in
13456 unchanged text at the end of current_buffer must be adjusted.
13457
13458 Return in *DELTA_BYTES the corresponding number of bytes.
13459
13460 Value is null if no such row exists, i.e. all rows are affected by
13461 changes. */
13462
13463 static struct glyph_row *
13464 find_first_unchanged_at_end_row (w, delta, delta_bytes)
13465 struct window *w;
13466 int *delta, *delta_bytes;
13467 {
13468 struct glyph_row *row;
13469 struct glyph_row *row_found = NULL;
13470
13471 *delta = *delta_bytes = 0;
13472
13473 /* Display must not have been paused, otherwise the current matrix
13474 is not up to date. */
13475 if (NILP (w->window_end_valid))
13476 abort ();
13477
13478 /* A value of window_end_pos >= END_UNCHANGED means that the window
13479 end is in the range of changed text. If so, there is no
13480 unchanged row at the end of W's current matrix. */
13481 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
13482 return NULL;
13483
13484 /* Set row to the last row in W's current matrix displaying text. */
13485 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
13486
13487 /* If matrix is entirely empty, no unchanged row exists. */
13488 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
13489 {
13490 /* The value of row is the last glyph row in the matrix having a
13491 meaningful buffer position in it. The end position of row
13492 corresponds to window_end_pos. This allows us to translate
13493 buffer positions in the current matrix to current buffer
13494 positions for characters not in changed text. */
13495 int Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
13496 int Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
13497 int last_unchanged_pos, last_unchanged_pos_old;
13498 struct glyph_row *first_text_row
13499 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
13500
13501 *delta = Z - Z_old;
13502 *delta_bytes = Z_BYTE - Z_BYTE_old;
13503
13504 /* Set last_unchanged_pos to the buffer position of the last
13505 character in the buffer that has not been changed. Z is the
13506 index + 1 of the last character in current_buffer, i.e. by
13507 subtracting END_UNCHANGED we get the index of the last
13508 unchanged character, and we have to add BEG to get its buffer
13509 position. */
13510 last_unchanged_pos = Z - END_UNCHANGED + BEG;
13511 last_unchanged_pos_old = last_unchanged_pos - *delta;
13512
13513 /* Search backward from ROW for a row displaying a line that
13514 starts at a minimum position >= last_unchanged_pos_old. */
13515 for (; row > first_text_row; --row)
13516 {
13517 /* This used to abort, but it can happen.
13518 It is ok to just stop the search instead here. KFS. */
13519 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
13520 break;
13521
13522 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
13523 row_found = row;
13524 }
13525 }
13526
13527 if (row_found && !MATRIX_ROW_DISPLAYS_TEXT_P (row_found))
13528 abort ();
13529
13530 return row_found;
13531 }
13532
13533
13534 /* Make sure that glyph rows in the current matrix of window W
13535 reference the same glyph memory as corresponding rows in the
13536 frame's frame matrix. This function is called after scrolling W's
13537 current matrix on a terminal frame in try_window_id and
13538 try_window_reusing_current_matrix. */
13539
13540 static void
13541 sync_frame_with_window_matrix_rows (w)
13542 struct window *w;
13543 {
13544 struct frame *f = XFRAME (w->frame);
13545 struct glyph_row *window_row, *window_row_end, *frame_row;
13546
13547 /* Preconditions: W must be a leaf window and full-width. Its frame
13548 must have a frame matrix. */
13549 xassert (NILP (w->hchild) && NILP (w->vchild));
13550 xassert (WINDOW_FULL_WIDTH_P (w));
13551 xassert (!FRAME_WINDOW_P (f));
13552
13553 /* If W is a full-width window, glyph pointers in W's current matrix
13554 have, by definition, to be the same as glyph pointers in the
13555 corresponding frame matrix. Note that frame matrices have no
13556 marginal areas (see build_frame_matrix). */
13557 window_row = w->current_matrix->rows;
13558 window_row_end = window_row + w->current_matrix->nrows;
13559 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
13560 while (window_row < window_row_end)
13561 {
13562 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
13563 struct glyph *end = window_row->glyphs[LAST_AREA];
13564
13565 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
13566 frame_row->glyphs[TEXT_AREA] = start;
13567 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
13568 frame_row->glyphs[LAST_AREA] = end;
13569
13570 /* Disable frame rows whose corresponding window rows have
13571 been disabled in try_window_id. */
13572 if (!window_row->enabled_p)
13573 frame_row->enabled_p = 0;
13574
13575 ++window_row, ++frame_row;
13576 }
13577 }
13578
13579
13580 /* Find the glyph row in window W containing CHARPOS. Consider all
13581 rows between START and END (not inclusive). END null means search
13582 all rows to the end of the display area of W. Value is the row
13583 containing CHARPOS or null. */
13584
13585 struct glyph_row *
13586 row_containing_pos (w, charpos, start, end, dy)
13587 struct window *w;
13588 int charpos;
13589 struct glyph_row *start, *end;
13590 int dy;
13591 {
13592 struct glyph_row *row = start;
13593 int last_y;
13594
13595 /* If we happen to start on a header-line, skip that. */
13596 if (row->mode_line_p)
13597 ++row;
13598
13599 if ((end && row >= end) || !row->enabled_p)
13600 return NULL;
13601
13602 last_y = window_text_bottom_y (w) - dy;
13603
13604 while (1)
13605 {
13606 /* Give up if we have gone too far. */
13607 if (end && row >= end)
13608 return NULL;
13609 /* This formerly returned if they were equal.
13610 I think that both quantities are of a "last plus one" type;
13611 if so, when they are equal, the row is within the screen. -- rms. */
13612 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
13613 return NULL;
13614
13615 /* If it is in this row, return this row. */
13616 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
13617 || (MATRIX_ROW_END_CHARPOS (row) == charpos
13618 /* The end position of a row equals the start
13619 position of the next row. If CHARPOS is there, we
13620 would rather display it in the next line, except
13621 when this line ends in ZV. */
13622 && !row->ends_at_zv_p
13623 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
13624 && charpos >= MATRIX_ROW_START_CHARPOS (row))
13625 return row;
13626 ++row;
13627 }
13628 }
13629
13630
13631 /* Try to redisplay window W by reusing its existing display. W's
13632 current matrix must be up to date when this function is called,
13633 i.e. window_end_valid must not be nil.
13634
13635 Value is
13636
13637 1 if display has been updated
13638 0 if otherwise unsuccessful
13639 -1 if redisplay with same window start is known not to succeed
13640
13641 The following steps are performed:
13642
13643 1. Find the last row in the current matrix of W that is not
13644 affected by changes at the start of current_buffer. If no such row
13645 is found, give up.
13646
13647 2. Find the first row in W's current matrix that is not affected by
13648 changes at the end of current_buffer. Maybe there is no such row.
13649
13650 3. Display lines beginning with the row + 1 found in step 1 to the
13651 row found in step 2 or, if step 2 didn't find a row, to the end of
13652 the window.
13653
13654 4. If cursor is not known to appear on the window, give up.
13655
13656 5. If display stopped at the row found in step 2, scroll the
13657 display and current matrix as needed.
13658
13659 6. Maybe display some lines at the end of W, if we must. This can
13660 happen under various circumstances, like a partially visible line
13661 becoming fully visible, or because newly displayed lines are displayed
13662 in smaller font sizes.
13663
13664 7. Update W's window end information. */
13665
13666 static int
13667 try_window_id (w)
13668 struct window *w;
13669 {
13670 struct frame *f = XFRAME (w->frame);
13671 struct glyph_matrix *current_matrix = w->current_matrix;
13672 struct glyph_matrix *desired_matrix = w->desired_matrix;
13673 struct glyph_row *last_unchanged_at_beg_row;
13674 struct glyph_row *first_unchanged_at_end_row;
13675 struct glyph_row *row;
13676 struct glyph_row *bottom_row;
13677 int bottom_vpos;
13678 struct it it;
13679 int delta = 0, delta_bytes = 0, stop_pos, dvpos, dy;
13680 struct text_pos start_pos;
13681 struct run run;
13682 int first_unchanged_at_end_vpos = 0;
13683 struct glyph_row *last_text_row, *last_text_row_at_end;
13684 struct text_pos start;
13685 int first_changed_charpos, last_changed_charpos;
13686
13687 #if GLYPH_DEBUG
13688 if (inhibit_try_window_id)
13689 return 0;
13690 #endif
13691
13692 /* This is handy for debugging. */
13693 #if 0
13694 #define GIVE_UP(X) \
13695 do { \
13696 fprintf (stderr, "try_window_id give up %d\n", (X)); \
13697 return 0; \
13698 } while (0)
13699 #else
13700 #define GIVE_UP(X) return 0
13701 #endif
13702
13703 SET_TEXT_POS_FROM_MARKER (start, w->start);
13704
13705 /* Don't use this for mini-windows because these can show
13706 messages and mini-buffers, and we don't handle that here. */
13707 if (MINI_WINDOW_P (w))
13708 GIVE_UP (1);
13709
13710 /* This flag is used to prevent redisplay optimizations. */
13711 if (windows_or_buffers_changed || cursor_type_changed)
13712 GIVE_UP (2);
13713
13714 /* Verify that narrowing has not changed.
13715 Also verify that we were not told to prevent redisplay optimizations.
13716 It would be nice to further
13717 reduce the number of cases where this prevents try_window_id. */
13718 if (current_buffer->clip_changed
13719 || current_buffer->prevent_redisplay_optimizations_p)
13720 GIVE_UP (3);
13721
13722 /* Window must either use window-based redisplay or be full width. */
13723 if (!FRAME_WINDOW_P (f)
13724 && (!line_ins_del_ok
13725 || !WINDOW_FULL_WIDTH_P (w)))
13726 GIVE_UP (4);
13727
13728 /* Give up if point is not known NOT to appear in W. */
13729 if (PT < CHARPOS (start))
13730 GIVE_UP (5);
13731
13732 /* Another way to prevent redisplay optimizations. */
13733 if (XFASTINT (w->last_modified) == 0)
13734 GIVE_UP (6);
13735
13736 /* Verify that window is not hscrolled. */
13737 if (XFASTINT (w->hscroll) != 0)
13738 GIVE_UP (7);
13739
13740 /* Verify that display wasn't paused. */
13741 if (NILP (w->window_end_valid))
13742 GIVE_UP (8);
13743
13744 /* Can't use this if highlighting a region because a cursor movement
13745 will do more than just set the cursor. */
13746 if (!NILP (Vtransient_mark_mode)
13747 && !NILP (current_buffer->mark_active))
13748 GIVE_UP (9);
13749
13750 /* Likewise if highlighting trailing whitespace. */
13751 if (!NILP (Vshow_trailing_whitespace))
13752 GIVE_UP (11);
13753
13754 /* Likewise if showing a region. */
13755 if (!NILP (w->region_showing))
13756 GIVE_UP (10);
13757
13758 /* Can use this if overlay arrow position and or string have changed. */
13759 if (overlay_arrows_changed_p ())
13760 GIVE_UP (12);
13761
13762
13763 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
13764 only if buffer has really changed. The reason is that the gap is
13765 initially at Z for freshly visited files. The code below would
13766 set end_unchanged to 0 in that case. */
13767 if (MODIFF > SAVE_MODIFF
13768 /* This seems to happen sometimes after saving a buffer. */
13769 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
13770 {
13771 if (GPT - BEG < BEG_UNCHANGED)
13772 BEG_UNCHANGED = GPT - BEG;
13773 if (Z - GPT < END_UNCHANGED)
13774 END_UNCHANGED = Z - GPT;
13775 }
13776
13777 /* The position of the first and last character that has been changed. */
13778 first_changed_charpos = BEG + BEG_UNCHANGED;
13779 last_changed_charpos = Z - END_UNCHANGED;
13780
13781 /* If window starts after a line end, and the last change is in
13782 front of that newline, then changes don't affect the display.
13783 This case happens with stealth-fontification. Note that although
13784 the display is unchanged, glyph positions in the matrix have to
13785 be adjusted, of course. */
13786 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
13787 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
13788 && ((last_changed_charpos < CHARPOS (start)
13789 && CHARPOS (start) == BEGV)
13790 || (last_changed_charpos < CHARPOS (start) - 1
13791 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
13792 {
13793 int Z_old, delta, Z_BYTE_old, delta_bytes;
13794 struct glyph_row *r0;
13795
13796 /* Compute how many chars/bytes have been added to or removed
13797 from the buffer. */
13798 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
13799 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
13800 delta = Z - Z_old;
13801 delta_bytes = Z_BYTE - Z_BYTE_old;
13802
13803 /* Give up if PT is not in the window. Note that it already has
13804 been checked at the start of try_window_id that PT is not in
13805 front of the window start. */
13806 if (PT >= MATRIX_ROW_END_CHARPOS (row) + delta)
13807 GIVE_UP (13);
13808
13809 /* If window start is unchanged, we can reuse the whole matrix
13810 as is, after adjusting glyph positions. No need to compute
13811 the window end again, since its offset from Z hasn't changed. */
13812 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
13813 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + delta
13814 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + delta_bytes
13815 /* PT must not be in a partially visible line. */
13816 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + delta
13817 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
13818 {
13819 /* Adjust positions in the glyph matrix. */
13820 if (delta || delta_bytes)
13821 {
13822 struct glyph_row *r1
13823 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
13824 increment_matrix_positions (w->current_matrix,
13825 MATRIX_ROW_VPOS (r0, current_matrix),
13826 MATRIX_ROW_VPOS (r1, current_matrix),
13827 delta, delta_bytes);
13828 }
13829
13830 /* Set the cursor. */
13831 row = row_containing_pos (w, PT, r0, NULL, 0);
13832 if (row)
13833 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
13834 else
13835 abort ();
13836 return 1;
13837 }
13838 }
13839
13840 /* Handle the case that changes are all below what is displayed in
13841 the window, and that PT is in the window. This shortcut cannot
13842 be taken if ZV is visible in the window, and text has been added
13843 there that is visible in the window. */
13844 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
13845 /* ZV is not visible in the window, or there are no
13846 changes at ZV, actually. */
13847 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
13848 || first_changed_charpos == last_changed_charpos))
13849 {
13850 struct glyph_row *r0;
13851
13852 /* Give up if PT is not in the window. Note that it already has
13853 been checked at the start of try_window_id that PT is not in
13854 front of the window start. */
13855 if (PT >= MATRIX_ROW_END_CHARPOS (row))
13856 GIVE_UP (14);
13857
13858 /* If window start is unchanged, we can reuse the whole matrix
13859 as is, without changing glyph positions since no text has
13860 been added/removed in front of the window end. */
13861 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
13862 if (TEXT_POS_EQUAL_P (start, r0->start.pos)
13863 /* PT must not be in a partially visible line. */
13864 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
13865 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
13866 {
13867 /* We have to compute the window end anew since text
13868 can have been added/removed after it. */
13869 w->window_end_pos
13870 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
13871 w->window_end_bytepos
13872 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
13873
13874 /* Set the cursor. */
13875 row = row_containing_pos (w, PT, r0, NULL, 0);
13876 if (row)
13877 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
13878 else
13879 abort ();
13880 return 2;
13881 }
13882 }
13883
13884 /* Give up if window start is in the changed area.
13885
13886 The condition used to read
13887
13888 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
13889
13890 but why that was tested escapes me at the moment. */
13891 if (CHARPOS (start) >= first_changed_charpos
13892 && CHARPOS (start) <= last_changed_charpos)
13893 GIVE_UP (15);
13894
13895 /* Check that window start agrees with the start of the first glyph
13896 row in its current matrix. Check this after we know the window
13897 start is not in changed text, otherwise positions would not be
13898 comparable. */
13899 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
13900 if (!TEXT_POS_EQUAL_P (start, row->start.pos))
13901 GIVE_UP (16);
13902
13903 /* Give up if the window ends in strings. Overlay strings
13904 at the end are difficult to handle, so don't try. */
13905 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
13906 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
13907 GIVE_UP (20);
13908
13909 /* Compute the position at which we have to start displaying new
13910 lines. Some of the lines at the top of the window might be
13911 reusable because they are not displaying changed text. Find the
13912 last row in W's current matrix not affected by changes at the
13913 start of current_buffer. Value is null if changes start in the
13914 first line of window. */
13915 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
13916 if (last_unchanged_at_beg_row)
13917 {
13918 /* Avoid starting to display in the moddle of a character, a TAB
13919 for instance. This is easier than to set up the iterator
13920 exactly, and it's not a frequent case, so the additional
13921 effort wouldn't really pay off. */
13922 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
13923 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
13924 && last_unchanged_at_beg_row > w->current_matrix->rows)
13925 --last_unchanged_at_beg_row;
13926
13927 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
13928 GIVE_UP (17);
13929
13930 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
13931 GIVE_UP (18);
13932 start_pos = it.current.pos;
13933
13934 /* Start displaying new lines in the desired matrix at the same
13935 vpos we would use in the current matrix, i.e. below
13936 last_unchanged_at_beg_row. */
13937 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
13938 current_matrix);
13939 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
13940 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
13941
13942 xassert (it.hpos == 0 && it.current_x == 0);
13943 }
13944 else
13945 {
13946 /* There are no reusable lines at the start of the window.
13947 Start displaying in the first text line. */
13948 start_display (&it, w, start);
13949 it.vpos = it.first_vpos;
13950 start_pos = it.current.pos;
13951 }
13952
13953 /* Find the first row that is not affected by changes at the end of
13954 the buffer. Value will be null if there is no unchanged row, in
13955 which case we must redisplay to the end of the window. delta
13956 will be set to the value by which buffer positions beginning with
13957 first_unchanged_at_end_row have to be adjusted due to text
13958 changes. */
13959 first_unchanged_at_end_row
13960 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
13961 IF_DEBUG (debug_delta = delta);
13962 IF_DEBUG (debug_delta_bytes = delta_bytes);
13963
13964 /* Set stop_pos to the buffer position up to which we will have to
13965 display new lines. If first_unchanged_at_end_row != NULL, this
13966 is the buffer position of the start of the line displayed in that
13967 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
13968 that we don't stop at a buffer position. */
13969 stop_pos = 0;
13970 if (first_unchanged_at_end_row)
13971 {
13972 xassert (last_unchanged_at_beg_row == NULL
13973 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
13974
13975 /* If this is a continuation line, move forward to the next one
13976 that isn't. Changes in lines above affect this line.
13977 Caution: this may move first_unchanged_at_end_row to a row
13978 not displaying text. */
13979 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
13980 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
13981 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
13982 < it.last_visible_y))
13983 ++first_unchanged_at_end_row;
13984
13985 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
13986 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
13987 >= it.last_visible_y))
13988 first_unchanged_at_end_row = NULL;
13989 else
13990 {
13991 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
13992 + delta);
13993 first_unchanged_at_end_vpos
13994 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
13995 xassert (stop_pos >= Z - END_UNCHANGED);
13996 }
13997 }
13998 else if (last_unchanged_at_beg_row == NULL)
13999 GIVE_UP (19);
14000
14001
14002 #if GLYPH_DEBUG
14003
14004 /* Either there is no unchanged row at the end, or the one we have
14005 now displays text. This is a necessary condition for the window
14006 end pos calculation at the end of this function. */
14007 xassert (first_unchanged_at_end_row == NULL
14008 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
14009
14010 debug_last_unchanged_at_beg_vpos
14011 = (last_unchanged_at_beg_row
14012 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
14013 : -1);
14014 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
14015
14016 #endif /* GLYPH_DEBUG != 0 */
14017
14018
14019 /* Display new lines. Set last_text_row to the last new line
14020 displayed which has text on it, i.e. might end up as being the
14021 line where the window_end_vpos is. */
14022 w->cursor.vpos = -1;
14023 last_text_row = NULL;
14024 overlay_arrow_seen = 0;
14025 while (it.current_y < it.last_visible_y
14026 && !fonts_changed_p
14027 && (first_unchanged_at_end_row == NULL
14028 || IT_CHARPOS (it) < stop_pos))
14029 {
14030 if (display_line (&it))
14031 last_text_row = it.glyph_row - 1;
14032 }
14033
14034 if (fonts_changed_p)
14035 return -1;
14036
14037
14038 /* Compute differences in buffer positions, y-positions etc. for
14039 lines reused at the bottom of the window. Compute what we can
14040 scroll. */
14041 if (first_unchanged_at_end_row
14042 /* No lines reused because we displayed everything up to the
14043 bottom of the window. */
14044 && it.current_y < it.last_visible_y)
14045 {
14046 dvpos = (it.vpos
14047 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
14048 current_matrix));
14049 dy = it.current_y - first_unchanged_at_end_row->y;
14050 run.current_y = first_unchanged_at_end_row->y;
14051 run.desired_y = run.current_y + dy;
14052 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
14053 }
14054 else
14055 {
14056 delta = dvpos = dy = run.current_y = run.desired_y = run.height = 0;
14057 first_unchanged_at_end_row = NULL;
14058 }
14059 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
14060
14061
14062 /* Find the cursor if not already found. We have to decide whether
14063 PT will appear on this window (it sometimes doesn't, but this is
14064 not a very frequent case.) This decision has to be made before
14065 the current matrix is altered. A value of cursor.vpos < 0 means
14066 that PT is either in one of the lines beginning at
14067 first_unchanged_at_end_row or below the window. Don't care for
14068 lines that might be displayed later at the window end; as
14069 mentioned, this is not a frequent case. */
14070 if (w->cursor.vpos < 0)
14071 {
14072 /* Cursor in unchanged rows at the top? */
14073 if (PT < CHARPOS (start_pos)
14074 && last_unchanged_at_beg_row)
14075 {
14076 row = row_containing_pos (w, PT,
14077 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
14078 last_unchanged_at_beg_row + 1, 0);
14079 if (row)
14080 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
14081 }
14082
14083 /* Start from first_unchanged_at_end_row looking for PT. */
14084 else if (first_unchanged_at_end_row)
14085 {
14086 row = row_containing_pos (w, PT - delta,
14087 first_unchanged_at_end_row, NULL, 0);
14088 if (row)
14089 set_cursor_from_row (w, row, w->current_matrix, delta,
14090 delta_bytes, dy, dvpos);
14091 }
14092
14093 /* Give up if cursor was not found. */
14094 if (w->cursor.vpos < 0)
14095 {
14096 clear_glyph_matrix (w->desired_matrix);
14097 return -1;
14098 }
14099 }
14100
14101 /* Don't let the cursor end in the scroll margins. */
14102 {
14103 int this_scroll_margin, cursor_height;
14104
14105 this_scroll_margin = max (0, scroll_margin);
14106 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
14107 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
14108 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
14109
14110 if ((w->cursor.y < this_scroll_margin
14111 && CHARPOS (start) > BEGV)
14112 /* Old redisplay didn't take scroll margin into account at the bottom,
14113 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
14114 || (w->cursor.y + (make_cursor_line_fully_visible_p
14115 ? cursor_height + this_scroll_margin
14116 : 1)) > it.last_visible_y)
14117 {
14118 w->cursor.vpos = -1;
14119 clear_glyph_matrix (w->desired_matrix);
14120 return -1;
14121 }
14122 }
14123
14124 /* Scroll the display. Do it before changing the current matrix so
14125 that xterm.c doesn't get confused about where the cursor glyph is
14126 found. */
14127 if (dy && run.height)
14128 {
14129 update_begin (f);
14130
14131 if (FRAME_WINDOW_P (f))
14132 {
14133 rif->update_window_begin_hook (w);
14134 rif->clear_window_mouse_face (w);
14135 rif->scroll_run_hook (w, &run);
14136 rif->update_window_end_hook (w, 0, 0);
14137 }
14138 else
14139 {
14140 /* Terminal frame. In this case, dvpos gives the number of
14141 lines to scroll by; dvpos < 0 means scroll up. */
14142 int first_unchanged_at_end_vpos
14143 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
14144 int from = WINDOW_TOP_EDGE_LINE (w) + first_unchanged_at_end_vpos;
14145 int end = (WINDOW_TOP_EDGE_LINE (w)
14146 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
14147 + window_internal_height (w));
14148
14149 /* Perform the operation on the screen. */
14150 if (dvpos > 0)
14151 {
14152 /* Scroll last_unchanged_at_beg_row to the end of the
14153 window down dvpos lines. */
14154 set_terminal_window (end);
14155
14156 /* On dumb terminals delete dvpos lines at the end
14157 before inserting dvpos empty lines. */
14158 if (!scroll_region_ok)
14159 ins_del_lines (end - dvpos, -dvpos);
14160
14161 /* Insert dvpos empty lines in front of
14162 last_unchanged_at_beg_row. */
14163 ins_del_lines (from, dvpos);
14164 }
14165 else if (dvpos < 0)
14166 {
14167 /* Scroll up last_unchanged_at_beg_vpos to the end of
14168 the window to last_unchanged_at_beg_vpos - |dvpos|. */
14169 set_terminal_window (end);
14170
14171 /* Delete dvpos lines in front of
14172 last_unchanged_at_beg_vpos. ins_del_lines will set
14173 the cursor to the given vpos and emit |dvpos| delete
14174 line sequences. */
14175 ins_del_lines (from + dvpos, dvpos);
14176
14177 /* On a dumb terminal insert dvpos empty lines at the
14178 end. */
14179 if (!scroll_region_ok)
14180 ins_del_lines (end + dvpos, -dvpos);
14181 }
14182
14183 set_terminal_window (0);
14184 }
14185
14186 update_end (f);
14187 }
14188
14189 /* Shift reused rows of the current matrix to the right position.
14190 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
14191 text. */
14192 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
14193 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
14194 if (dvpos < 0)
14195 {
14196 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
14197 bottom_vpos, dvpos);
14198 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
14199 bottom_vpos, 0);
14200 }
14201 else if (dvpos > 0)
14202 {
14203 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
14204 bottom_vpos, dvpos);
14205 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
14206 first_unchanged_at_end_vpos + dvpos, 0);
14207 }
14208
14209 /* For frame-based redisplay, make sure that current frame and window
14210 matrix are in sync with respect to glyph memory. */
14211 if (!FRAME_WINDOW_P (f))
14212 sync_frame_with_window_matrix_rows (w);
14213
14214 /* Adjust buffer positions in reused rows. */
14215 if (delta)
14216 increment_matrix_positions (current_matrix,
14217 first_unchanged_at_end_vpos + dvpos,
14218 bottom_vpos, delta, delta_bytes);
14219
14220 /* Adjust Y positions. */
14221 if (dy)
14222 shift_glyph_matrix (w, current_matrix,
14223 first_unchanged_at_end_vpos + dvpos,
14224 bottom_vpos, dy);
14225
14226 if (first_unchanged_at_end_row)
14227 {
14228 first_unchanged_at_end_row += dvpos;
14229 if (first_unchanged_at_end_row->y >= it.last_visible_y
14230 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
14231 first_unchanged_at_end_row = NULL;
14232 }
14233
14234 /* If scrolling up, there may be some lines to display at the end of
14235 the window. */
14236 last_text_row_at_end = NULL;
14237 if (dy < 0)
14238 {
14239 /* Scrolling up can leave for example a partially visible line
14240 at the end of the window to be redisplayed. */
14241 /* Set last_row to the glyph row in the current matrix where the
14242 window end line is found. It has been moved up or down in
14243 the matrix by dvpos. */
14244 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
14245 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
14246
14247 /* If last_row is the window end line, it should display text. */
14248 xassert (last_row->displays_text_p);
14249
14250 /* If window end line was partially visible before, begin
14251 displaying at that line. Otherwise begin displaying with the
14252 line following it. */
14253 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
14254 {
14255 init_to_row_start (&it, w, last_row);
14256 it.vpos = last_vpos;
14257 it.current_y = last_row->y;
14258 }
14259 else
14260 {
14261 init_to_row_end (&it, w, last_row);
14262 it.vpos = 1 + last_vpos;
14263 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
14264 ++last_row;
14265 }
14266
14267 /* We may start in a continuation line. If so, we have to
14268 get the right continuation_lines_width and current_x. */
14269 it.continuation_lines_width = last_row->continuation_lines_width;
14270 it.hpos = it.current_x = 0;
14271
14272 /* Display the rest of the lines at the window end. */
14273 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
14274 while (it.current_y < it.last_visible_y
14275 && !fonts_changed_p)
14276 {
14277 /* Is it always sure that the display agrees with lines in
14278 the current matrix? I don't think so, so we mark rows
14279 displayed invalid in the current matrix by setting their
14280 enabled_p flag to zero. */
14281 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
14282 if (display_line (&it))
14283 last_text_row_at_end = it.glyph_row - 1;
14284 }
14285 }
14286
14287 /* Update window_end_pos and window_end_vpos. */
14288 if (first_unchanged_at_end_row
14289 && !last_text_row_at_end)
14290 {
14291 /* Window end line if one of the preserved rows from the current
14292 matrix. Set row to the last row displaying text in current
14293 matrix starting at first_unchanged_at_end_row, after
14294 scrolling. */
14295 xassert (first_unchanged_at_end_row->displays_text_p);
14296 row = find_last_row_displaying_text (w->current_matrix, &it,
14297 first_unchanged_at_end_row);
14298 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
14299
14300 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
14301 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
14302 w->window_end_vpos
14303 = make_number (MATRIX_ROW_VPOS (row, w->current_matrix));
14304 xassert (w->window_end_bytepos >= 0);
14305 IF_DEBUG (debug_method_add (w, "A"));
14306 }
14307 else if (last_text_row_at_end)
14308 {
14309 w->window_end_pos
14310 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
14311 w->window_end_bytepos
14312 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
14313 w->window_end_vpos
14314 = make_number (MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
14315 xassert (w->window_end_bytepos >= 0);
14316 IF_DEBUG (debug_method_add (w, "B"));
14317 }
14318 else if (last_text_row)
14319 {
14320 /* We have displayed either to the end of the window or at the
14321 end of the window, i.e. the last row with text is to be found
14322 in the desired matrix. */
14323 w->window_end_pos
14324 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
14325 w->window_end_bytepos
14326 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
14327 w->window_end_vpos
14328 = make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix));
14329 xassert (w->window_end_bytepos >= 0);
14330 }
14331 else if (first_unchanged_at_end_row == NULL
14332 && last_text_row == NULL
14333 && last_text_row_at_end == NULL)
14334 {
14335 /* Displayed to end of window, but no line containing text was
14336 displayed. Lines were deleted at the end of the window. */
14337 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
14338 int vpos = XFASTINT (w->window_end_vpos);
14339 struct glyph_row *current_row = current_matrix->rows + vpos;
14340 struct glyph_row *desired_row = desired_matrix->rows + vpos;
14341
14342 for (row = NULL;
14343 row == NULL && vpos >= first_vpos;
14344 --vpos, --current_row, --desired_row)
14345 {
14346 if (desired_row->enabled_p)
14347 {
14348 if (desired_row->displays_text_p)
14349 row = desired_row;
14350 }
14351 else if (current_row->displays_text_p)
14352 row = current_row;
14353 }
14354
14355 xassert (row != NULL);
14356 w->window_end_vpos = make_number (vpos + 1);
14357 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
14358 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
14359 xassert (w->window_end_bytepos >= 0);
14360 IF_DEBUG (debug_method_add (w, "C"));
14361 }
14362 else
14363 abort ();
14364
14365 #if 0 /* This leads to problems, for instance when the cursor is
14366 at ZV, and the cursor line displays no text. */
14367 /* Disable rows below what's displayed in the window. This makes
14368 debugging easier. */
14369 enable_glyph_matrix_rows (current_matrix,
14370 XFASTINT (w->window_end_vpos) + 1,
14371 bottom_vpos, 0);
14372 #endif
14373
14374 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
14375 debug_end_vpos = XFASTINT (w->window_end_vpos));
14376
14377 /* Record that display has not been completed. */
14378 w->window_end_valid = Qnil;
14379 w->desired_matrix->no_scrolling_p = 1;
14380 return 3;
14381
14382 #undef GIVE_UP
14383 }
14384
14385
14386 \f
14387 /***********************************************************************
14388 More debugging support
14389 ***********************************************************************/
14390
14391 #if GLYPH_DEBUG
14392
14393 void dump_glyph_row P_ ((struct glyph_row *, int, int));
14394 void dump_glyph_matrix P_ ((struct glyph_matrix *, int));
14395 void dump_glyph P_ ((struct glyph_row *, struct glyph *, int));
14396
14397
14398 /* Dump the contents of glyph matrix MATRIX on stderr.
14399
14400 GLYPHS 0 means don't show glyph contents.
14401 GLYPHS 1 means show glyphs in short form
14402 GLYPHS > 1 means show glyphs in long form. */
14403
14404 void
14405 dump_glyph_matrix (matrix, glyphs)
14406 struct glyph_matrix *matrix;
14407 int glyphs;
14408 {
14409 int i;
14410 for (i = 0; i < matrix->nrows; ++i)
14411 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
14412 }
14413
14414
14415 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
14416 the glyph row and area where the glyph comes from. */
14417
14418 void
14419 dump_glyph (row, glyph, area)
14420 struct glyph_row *row;
14421 struct glyph *glyph;
14422 int area;
14423 {
14424 if (glyph->type == CHAR_GLYPH)
14425 {
14426 fprintf (stderr,
14427 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
14428 glyph - row->glyphs[TEXT_AREA],
14429 'C',
14430 glyph->charpos,
14431 (BUFFERP (glyph->object)
14432 ? 'B'
14433 : (STRINGP (glyph->object)
14434 ? 'S'
14435 : '-')),
14436 glyph->pixel_width,
14437 glyph->u.ch,
14438 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
14439 ? glyph->u.ch
14440 : '.'),
14441 glyph->face_id,
14442 glyph->left_box_line_p,
14443 glyph->right_box_line_p);
14444 }
14445 else if (glyph->type == STRETCH_GLYPH)
14446 {
14447 fprintf (stderr,
14448 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
14449 glyph - row->glyphs[TEXT_AREA],
14450 'S',
14451 glyph->charpos,
14452 (BUFFERP (glyph->object)
14453 ? 'B'
14454 : (STRINGP (glyph->object)
14455 ? 'S'
14456 : '-')),
14457 glyph->pixel_width,
14458 0,
14459 '.',
14460 glyph->face_id,
14461 glyph->left_box_line_p,
14462 glyph->right_box_line_p);
14463 }
14464 else if (glyph->type == IMAGE_GLYPH)
14465 {
14466 fprintf (stderr,
14467 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
14468 glyph - row->glyphs[TEXT_AREA],
14469 'I',
14470 glyph->charpos,
14471 (BUFFERP (glyph->object)
14472 ? 'B'
14473 : (STRINGP (glyph->object)
14474 ? 'S'
14475 : '-')),
14476 glyph->pixel_width,
14477 glyph->u.img_id,
14478 '.',
14479 glyph->face_id,
14480 glyph->left_box_line_p,
14481 glyph->right_box_line_p);
14482 }
14483 }
14484
14485
14486 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
14487 GLYPHS 0 means don't show glyph contents.
14488 GLYPHS 1 means show glyphs in short form
14489 GLYPHS > 1 means show glyphs in long form. */
14490
14491 void
14492 dump_glyph_row (row, vpos, glyphs)
14493 struct glyph_row *row;
14494 int vpos, glyphs;
14495 {
14496 if (glyphs != 1)
14497 {
14498 fprintf (stderr, "Row Start End Used oEI><\\CTZFesm X Y W H V A P\n");
14499 fprintf (stderr, "======================================================================\n");
14500
14501 fprintf (stderr, "%3d %5d %5d %4d %1.1d%1.1d%1.1d%1.1d\
14502 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
14503 vpos,
14504 MATRIX_ROW_START_CHARPOS (row),
14505 MATRIX_ROW_END_CHARPOS (row),
14506 row->used[TEXT_AREA],
14507 row->contains_overlapping_glyphs_p,
14508 row->enabled_p,
14509 row->truncated_on_left_p,
14510 row->truncated_on_right_p,
14511 row->continued_p,
14512 MATRIX_ROW_CONTINUATION_LINE_P (row),
14513 row->displays_text_p,
14514 row->ends_at_zv_p,
14515 row->fill_line_p,
14516 row->ends_in_middle_of_char_p,
14517 row->starts_in_middle_of_char_p,
14518 row->mouse_face_p,
14519 row->x,
14520 row->y,
14521 row->pixel_width,
14522 row->height,
14523 row->visible_height,
14524 row->ascent,
14525 row->phys_ascent);
14526 fprintf (stderr, "%9d %5d\t%5d\n", row->start.overlay_string_index,
14527 row->end.overlay_string_index,
14528 row->continuation_lines_width);
14529 fprintf (stderr, "%9d %5d\n",
14530 CHARPOS (row->start.string_pos),
14531 CHARPOS (row->end.string_pos));
14532 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
14533 row->end.dpvec_index);
14534 }
14535
14536 if (glyphs > 1)
14537 {
14538 int area;
14539
14540 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
14541 {
14542 struct glyph *glyph = row->glyphs[area];
14543 struct glyph *glyph_end = glyph + row->used[area];
14544
14545 /* Glyph for a line end in text. */
14546 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
14547 ++glyph_end;
14548
14549 if (glyph < glyph_end)
14550 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
14551
14552 for (; glyph < glyph_end; ++glyph)
14553 dump_glyph (row, glyph, area);
14554 }
14555 }
14556 else if (glyphs == 1)
14557 {
14558 int area;
14559
14560 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
14561 {
14562 char *s = (char *) alloca (row->used[area] + 1);
14563 int i;
14564
14565 for (i = 0; i < row->used[area]; ++i)
14566 {
14567 struct glyph *glyph = row->glyphs[area] + i;
14568 if (glyph->type == CHAR_GLYPH
14569 && glyph->u.ch < 0x80
14570 && glyph->u.ch >= ' ')
14571 s[i] = glyph->u.ch;
14572 else
14573 s[i] = '.';
14574 }
14575
14576 s[i] = '\0';
14577 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
14578 }
14579 }
14580 }
14581
14582
14583 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
14584 Sdump_glyph_matrix, 0, 1, "p",
14585 doc: /* Dump the current matrix of the selected window to stderr.
14586 Shows contents of glyph row structures. With non-nil
14587 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
14588 glyphs in short form, otherwise show glyphs in long form. */)
14589 (glyphs)
14590 Lisp_Object glyphs;
14591 {
14592 struct window *w = XWINDOW (selected_window);
14593 struct buffer *buffer = XBUFFER (w->buffer);
14594
14595 fprintf (stderr, "PT = %d, BEGV = %d. ZV = %d\n",
14596 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
14597 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
14598 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
14599 fprintf (stderr, "=============================================\n");
14600 dump_glyph_matrix (w->current_matrix,
14601 NILP (glyphs) ? 0 : XINT (glyphs));
14602 return Qnil;
14603 }
14604
14605
14606 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
14607 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
14608 ()
14609 {
14610 struct frame *f = XFRAME (selected_frame);
14611 dump_glyph_matrix (f->current_matrix, 1);
14612 return Qnil;
14613 }
14614
14615
14616 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
14617 doc: /* Dump glyph row ROW to stderr.
14618 GLYPH 0 means don't dump glyphs.
14619 GLYPH 1 means dump glyphs in short form.
14620 GLYPH > 1 or omitted means dump glyphs in long form. */)
14621 (row, glyphs)
14622 Lisp_Object row, glyphs;
14623 {
14624 struct glyph_matrix *matrix;
14625 int vpos;
14626
14627 CHECK_NUMBER (row);
14628 matrix = XWINDOW (selected_window)->current_matrix;
14629 vpos = XINT (row);
14630 if (vpos >= 0 && vpos < matrix->nrows)
14631 dump_glyph_row (MATRIX_ROW (matrix, vpos),
14632 vpos,
14633 INTEGERP (glyphs) ? XINT (glyphs) : 2);
14634 return Qnil;
14635 }
14636
14637
14638 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
14639 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
14640 GLYPH 0 means don't dump glyphs.
14641 GLYPH 1 means dump glyphs in short form.
14642 GLYPH > 1 or omitted means dump glyphs in long form. */)
14643 (row, glyphs)
14644 Lisp_Object row, glyphs;
14645 {
14646 struct frame *sf = SELECTED_FRAME ();
14647 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
14648 int vpos;
14649
14650 CHECK_NUMBER (row);
14651 vpos = XINT (row);
14652 if (vpos >= 0 && vpos < m->nrows)
14653 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
14654 INTEGERP (glyphs) ? XINT (glyphs) : 2);
14655 return Qnil;
14656 }
14657
14658
14659 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
14660 doc: /* Toggle tracing of redisplay.
14661 With ARG, turn tracing on if and only if ARG is positive. */)
14662 (arg)
14663 Lisp_Object arg;
14664 {
14665 if (NILP (arg))
14666 trace_redisplay_p = !trace_redisplay_p;
14667 else
14668 {
14669 arg = Fprefix_numeric_value (arg);
14670 trace_redisplay_p = XINT (arg) > 0;
14671 }
14672
14673 return Qnil;
14674 }
14675
14676
14677 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
14678 doc: /* Like `format', but print result to stderr.
14679 usage: (trace-to-stderr STRING &rest OBJECTS) */)
14680 (nargs, args)
14681 int nargs;
14682 Lisp_Object *args;
14683 {
14684 Lisp_Object s = Fformat (nargs, args);
14685 fprintf (stderr, "%s", SDATA (s));
14686 return Qnil;
14687 }
14688
14689 #endif /* GLYPH_DEBUG */
14690
14691
14692 \f
14693 /***********************************************************************
14694 Building Desired Matrix Rows
14695 ***********************************************************************/
14696
14697 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
14698 Used for non-window-redisplay windows, and for windows w/o left fringe. */
14699
14700 static struct glyph_row *
14701 get_overlay_arrow_glyph_row (w, overlay_arrow_string)
14702 struct window *w;
14703 Lisp_Object overlay_arrow_string;
14704 {
14705 struct frame *f = XFRAME (WINDOW_FRAME (w));
14706 struct buffer *buffer = XBUFFER (w->buffer);
14707 struct buffer *old = current_buffer;
14708 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
14709 int arrow_len = SCHARS (overlay_arrow_string);
14710 const unsigned char *arrow_end = arrow_string + arrow_len;
14711 const unsigned char *p;
14712 struct it it;
14713 int multibyte_p;
14714 int n_glyphs_before;
14715
14716 set_buffer_temp (buffer);
14717 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
14718 it.glyph_row->used[TEXT_AREA] = 0;
14719 SET_TEXT_POS (it.position, 0, 0);
14720
14721 multibyte_p = !NILP (buffer->enable_multibyte_characters);
14722 p = arrow_string;
14723 while (p < arrow_end)
14724 {
14725 Lisp_Object face, ilisp;
14726
14727 /* Get the next character. */
14728 if (multibyte_p)
14729 it.c = string_char_and_length (p, arrow_len, &it.len);
14730 else
14731 it.c = *p, it.len = 1;
14732 p += it.len;
14733
14734 /* Get its face. */
14735 ilisp = make_number (p - arrow_string);
14736 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
14737 it.face_id = compute_char_face (f, it.c, face);
14738
14739 /* Compute its width, get its glyphs. */
14740 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
14741 SET_TEXT_POS (it.position, -1, -1);
14742 PRODUCE_GLYPHS (&it);
14743
14744 /* If this character doesn't fit any more in the line, we have
14745 to remove some glyphs. */
14746 if (it.current_x > it.last_visible_x)
14747 {
14748 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
14749 break;
14750 }
14751 }
14752
14753 set_buffer_temp (old);
14754 return it.glyph_row;
14755 }
14756
14757
14758 /* Insert truncation glyphs at the start of IT->glyph_row. Truncation
14759 glyphs are only inserted for terminal frames since we can't really
14760 win with truncation glyphs when partially visible glyphs are
14761 involved. Which glyphs to insert is determined by
14762 produce_special_glyphs. */
14763
14764 static void
14765 insert_left_trunc_glyphs (it)
14766 struct it *it;
14767 {
14768 struct it truncate_it;
14769 struct glyph *from, *end, *to, *toend;
14770
14771 xassert (!FRAME_WINDOW_P (it->f));
14772
14773 /* Get the truncation glyphs. */
14774 truncate_it = *it;
14775 truncate_it.current_x = 0;
14776 truncate_it.face_id = DEFAULT_FACE_ID;
14777 truncate_it.glyph_row = &scratch_glyph_row;
14778 truncate_it.glyph_row->used[TEXT_AREA] = 0;
14779 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
14780 truncate_it.object = make_number (0);
14781 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
14782
14783 /* Overwrite glyphs from IT with truncation glyphs. */
14784 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
14785 end = from + truncate_it.glyph_row->used[TEXT_AREA];
14786 to = it->glyph_row->glyphs[TEXT_AREA];
14787 toend = to + it->glyph_row->used[TEXT_AREA];
14788
14789 while (from < end)
14790 *to++ = *from++;
14791
14792 /* There may be padding glyphs left over. Overwrite them too. */
14793 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
14794 {
14795 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
14796 while (from < end)
14797 *to++ = *from++;
14798 }
14799
14800 if (to > toend)
14801 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
14802 }
14803
14804
14805 /* Compute the pixel height and width of IT->glyph_row.
14806
14807 Most of the time, ascent and height of a display line will be equal
14808 to the max_ascent and max_height values of the display iterator
14809 structure. This is not the case if
14810
14811 1. We hit ZV without displaying anything. In this case, max_ascent
14812 and max_height will be zero.
14813
14814 2. We have some glyphs that don't contribute to the line height.
14815 (The glyph row flag contributes_to_line_height_p is for future
14816 pixmap extensions).
14817
14818 The first case is easily covered by using default values because in
14819 these cases, the line height does not really matter, except that it
14820 must not be zero. */
14821
14822 static void
14823 compute_line_metrics (it)
14824 struct it *it;
14825 {
14826 struct glyph_row *row = it->glyph_row;
14827 int area, i;
14828
14829 if (FRAME_WINDOW_P (it->f))
14830 {
14831 int i, min_y, max_y;
14832
14833 /* The line may consist of one space only, that was added to
14834 place the cursor on it. If so, the row's height hasn't been
14835 computed yet. */
14836 if (row->height == 0)
14837 {
14838 if (it->max_ascent + it->max_descent == 0)
14839 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
14840 row->ascent = it->max_ascent;
14841 row->height = it->max_ascent + it->max_descent;
14842 row->phys_ascent = it->max_phys_ascent;
14843 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
14844 row->extra_line_spacing = it->max_extra_line_spacing;
14845 }
14846
14847 /* Compute the width of this line. */
14848 row->pixel_width = row->x;
14849 for (i = 0; i < row->used[TEXT_AREA]; ++i)
14850 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
14851
14852 xassert (row->pixel_width >= 0);
14853 xassert (row->ascent >= 0 && row->height > 0);
14854
14855 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
14856 || MATRIX_ROW_OVERLAPS_PRED_P (row));
14857
14858 /* If first line's physical ascent is larger than its logical
14859 ascent, use the physical ascent, and make the row taller.
14860 This makes accented characters fully visible. */
14861 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
14862 && row->phys_ascent > row->ascent)
14863 {
14864 row->height += row->phys_ascent - row->ascent;
14865 row->ascent = row->phys_ascent;
14866 }
14867
14868 /* Compute how much of the line is visible. */
14869 row->visible_height = row->height;
14870
14871 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
14872 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
14873
14874 if (row->y < min_y)
14875 row->visible_height -= min_y - row->y;
14876 if (row->y + row->height > max_y)
14877 row->visible_height -= row->y + row->height - max_y;
14878 }
14879 else
14880 {
14881 row->pixel_width = row->used[TEXT_AREA];
14882 if (row->continued_p)
14883 row->pixel_width -= it->continuation_pixel_width;
14884 else if (row->truncated_on_right_p)
14885 row->pixel_width -= it->truncation_pixel_width;
14886 row->ascent = row->phys_ascent = 0;
14887 row->height = row->phys_height = row->visible_height = 1;
14888 row->extra_line_spacing = 0;
14889 }
14890
14891 /* Compute a hash code for this row. */
14892 row->hash = 0;
14893 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
14894 for (i = 0; i < row->used[area]; ++i)
14895 row->hash = ((((row->hash << 4) + (row->hash >> 24)) & 0x0fffffff)
14896 + row->glyphs[area][i].u.val
14897 + row->glyphs[area][i].face_id
14898 + row->glyphs[area][i].padding_p
14899 + (row->glyphs[area][i].type << 2));
14900
14901 it->max_ascent = it->max_descent = 0;
14902 it->max_phys_ascent = it->max_phys_descent = 0;
14903 }
14904
14905
14906 /* Append one space to the glyph row of iterator IT if doing a
14907 window-based redisplay. The space has the same face as
14908 IT->face_id. Value is non-zero if a space was added.
14909
14910 This function is called to make sure that there is always one glyph
14911 at the end of a glyph row that the cursor can be set on under
14912 window-systems. (If there weren't such a glyph we would not know
14913 how wide and tall a box cursor should be displayed).
14914
14915 At the same time this space let's a nicely handle clearing to the
14916 end of the line if the row ends in italic text. */
14917
14918 static int
14919 append_space_for_newline (it, default_face_p)
14920 struct it *it;
14921 int default_face_p;
14922 {
14923 if (FRAME_WINDOW_P (it->f))
14924 {
14925 int n = it->glyph_row->used[TEXT_AREA];
14926
14927 if (it->glyph_row->glyphs[TEXT_AREA] + n
14928 < it->glyph_row->glyphs[1 + TEXT_AREA])
14929 {
14930 /* Save some values that must not be changed.
14931 Must save IT->c and IT->len because otherwise
14932 ITERATOR_AT_END_P wouldn't work anymore after
14933 append_space_for_newline has been called. */
14934 enum display_element_type saved_what = it->what;
14935 int saved_c = it->c, saved_len = it->len;
14936 int saved_x = it->current_x;
14937 int saved_face_id = it->face_id;
14938 struct text_pos saved_pos;
14939 Lisp_Object saved_object;
14940 struct face *face;
14941
14942 saved_object = it->object;
14943 saved_pos = it->position;
14944
14945 it->what = IT_CHARACTER;
14946 bzero (&it->position, sizeof it->position);
14947 it->object = make_number (0);
14948 it->c = ' ';
14949 it->len = 1;
14950
14951 if (default_face_p)
14952 it->face_id = DEFAULT_FACE_ID;
14953 else if (it->face_before_selective_p)
14954 it->face_id = it->saved_face_id;
14955 face = FACE_FROM_ID (it->f, it->face_id);
14956 it->face_id = FACE_FOR_CHAR (it->f, face, 0, -1, Qnil);
14957
14958 PRODUCE_GLYPHS (it);
14959
14960 it->override_ascent = -1;
14961 it->constrain_row_ascent_descent_p = 0;
14962 it->current_x = saved_x;
14963 it->object = saved_object;
14964 it->position = saved_pos;
14965 it->what = saved_what;
14966 it->face_id = saved_face_id;
14967 it->len = saved_len;
14968 it->c = saved_c;
14969 return 1;
14970 }
14971 }
14972
14973 return 0;
14974 }
14975
14976
14977 /* Extend the face of the last glyph in the text area of IT->glyph_row
14978 to the end of the display line. Called from display_line.
14979 If the glyph row is empty, add a space glyph to it so that we
14980 know the face to draw. Set the glyph row flag fill_line_p. */
14981
14982 static void
14983 extend_face_to_end_of_line (it)
14984 struct it *it;
14985 {
14986 struct face *face;
14987 struct frame *f = it->f;
14988
14989 /* If line is already filled, do nothing. */
14990 if (it->current_x >= it->last_visible_x)
14991 return;
14992
14993 /* Face extension extends the background and box of IT->face_id
14994 to the end of the line. If the background equals the background
14995 of the frame, we don't have to do anything. */
14996 if (it->face_before_selective_p)
14997 face = FACE_FROM_ID (it->f, it->saved_face_id);
14998 else
14999 face = FACE_FROM_ID (f, it->face_id);
15000
15001 if (FRAME_WINDOW_P (f)
15002 && face->box == FACE_NO_BOX
15003 && face->background == FRAME_BACKGROUND_PIXEL (f)
15004 && !face->stipple)
15005 return;
15006
15007 /* Set the glyph row flag indicating that the face of the last glyph
15008 in the text area has to be drawn to the end of the text area. */
15009 it->glyph_row->fill_line_p = 1;
15010
15011 /* If current character of IT is not ASCII, make sure we have the
15012 ASCII face. This will be automatically undone the next time
15013 get_next_display_element returns a multibyte character. Note
15014 that the character will always be single byte in unibyte text. */
15015 if (!ASCII_CHAR_P (it->c))
15016 {
15017 it->face_id = FACE_FOR_CHAR (f, face, 0, -1, Qnil);
15018 }
15019
15020 if (FRAME_WINDOW_P (f))
15021 {
15022 /* If the row is empty, add a space with the current face of IT,
15023 so that we know which face to draw. */
15024 if (it->glyph_row->used[TEXT_AREA] == 0)
15025 {
15026 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
15027 it->glyph_row->glyphs[TEXT_AREA][0].face_id = it->face_id;
15028 it->glyph_row->used[TEXT_AREA] = 1;
15029 }
15030 }
15031 else
15032 {
15033 /* Save some values that must not be changed. */
15034 int saved_x = it->current_x;
15035 struct text_pos saved_pos;
15036 Lisp_Object saved_object;
15037 enum display_element_type saved_what = it->what;
15038 int saved_face_id = it->face_id;
15039
15040 saved_object = it->object;
15041 saved_pos = it->position;
15042
15043 it->what = IT_CHARACTER;
15044 bzero (&it->position, sizeof it->position);
15045 it->object = make_number (0);
15046 it->c = ' ';
15047 it->len = 1;
15048 it->face_id = face->id;
15049
15050 PRODUCE_GLYPHS (it);
15051
15052 while (it->current_x <= it->last_visible_x)
15053 PRODUCE_GLYPHS (it);
15054
15055 /* Don't count these blanks really. It would let us insert a left
15056 truncation glyph below and make us set the cursor on them, maybe. */
15057 it->current_x = saved_x;
15058 it->object = saved_object;
15059 it->position = saved_pos;
15060 it->what = saved_what;
15061 it->face_id = saved_face_id;
15062 }
15063 }
15064
15065
15066 /* Value is non-zero if text starting at CHARPOS in current_buffer is
15067 trailing whitespace. */
15068
15069 static int
15070 trailing_whitespace_p (charpos)
15071 int charpos;
15072 {
15073 int bytepos = CHAR_TO_BYTE (charpos);
15074 int c = 0;
15075
15076 while (bytepos < ZV_BYTE
15077 && (c = FETCH_CHAR (bytepos),
15078 c == ' ' || c == '\t'))
15079 ++bytepos;
15080
15081 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
15082 {
15083 if (bytepos != PT_BYTE)
15084 return 1;
15085 }
15086 return 0;
15087 }
15088
15089
15090 /* Highlight trailing whitespace, if any, in ROW. */
15091
15092 void
15093 highlight_trailing_whitespace (f, row)
15094 struct frame *f;
15095 struct glyph_row *row;
15096 {
15097 int used = row->used[TEXT_AREA];
15098
15099 if (used)
15100 {
15101 struct glyph *start = row->glyphs[TEXT_AREA];
15102 struct glyph *glyph = start + used - 1;
15103
15104 /* Skip over glyphs inserted to display the cursor at the
15105 end of a line, for extending the face of the last glyph
15106 to the end of the line on terminals, and for truncation
15107 and continuation glyphs. */
15108 while (glyph >= start
15109 && glyph->type == CHAR_GLYPH
15110 && INTEGERP (glyph->object))
15111 --glyph;
15112
15113 /* If last glyph is a space or stretch, and it's trailing
15114 whitespace, set the face of all trailing whitespace glyphs in
15115 IT->glyph_row to `trailing-whitespace'. */
15116 if (glyph >= start
15117 && BUFFERP (glyph->object)
15118 && (glyph->type == STRETCH_GLYPH
15119 || (glyph->type == CHAR_GLYPH
15120 && glyph->u.ch == ' '))
15121 && trailing_whitespace_p (glyph->charpos))
15122 {
15123 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
15124 if (face_id < 0)
15125 return;
15126
15127 while (glyph >= start
15128 && BUFFERP (glyph->object)
15129 && (glyph->type == STRETCH_GLYPH
15130 || (glyph->type == CHAR_GLYPH
15131 && glyph->u.ch == ' ')))
15132 (glyph--)->face_id = face_id;
15133 }
15134 }
15135 }
15136
15137
15138 /* Value is non-zero if glyph row ROW in window W should be
15139 used to hold the cursor. */
15140
15141 static int
15142 cursor_row_p (w, row)
15143 struct window *w;
15144 struct glyph_row *row;
15145 {
15146 int cursor_row_p = 1;
15147
15148 if (PT == MATRIX_ROW_END_CHARPOS (row))
15149 {
15150 /* If the row ends with a newline from a string, we don't want
15151 the cursor there, but we still want it at the start of the
15152 string if the string starts in this row.
15153 If the row is continued it doesn't end in a newline. */
15154 if (CHARPOS (row->end.string_pos) >= 0)
15155 cursor_row_p = (row->continued_p
15156 || PT >= MATRIX_ROW_START_CHARPOS (row));
15157 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
15158 {
15159 /* If the row ends in middle of a real character,
15160 and the line is continued, we want the cursor here.
15161 That's because MATRIX_ROW_END_CHARPOS would equal
15162 PT if PT is before the character. */
15163 if (!row->ends_in_ellipsis_p)
15164 cursor_row_p = row->continued_p;
15165 else
15166 /* If the row ends in an ellipsis, then
15167 MATRIX_ROW_END_CHARPOS will equal point after the invisible text.
15168 We want that position to be displayed after the ellipsis. */
15169 cursor_row_p = 0;
15170 }
15171 /* If the row ends at ZV, display the cursor at the end of that
15172 row instead of at the start of the row below. */
15173 else if (row->ends_at_zv_p)
15174 cursor_row_p = 1;
15175 else
15176 cursor_row_p = 0;
15177 }
15178
15179 return cursor_row_p;
15180 }
15181
15182
15183 /* Construct the glyph row IT->glyph_row in the desired matrix of
15184 IT->w from text at the current position of IT. See dispextern.h
15185 for an overview of struct it. Value is non-zero if
15186 IT->glyph_row displays text, as opposed to a line displaying ZV
15187 only. */
15188
15189 static int
15190 display_line (it)
15191 struct it *it;
15192 {
15193 struct glyph_row *row = it->glyph_row;
15194 Lisp_Object overlay_arrow_string;
15195
15196 /* We always start displaying at hpos zero even if hscrolled. */
15197 xassert (it->hpos == 0 && it->current_x == 0);
15198
15199 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
15200 >= it->w->desired_matrix->nrows)
15201 {
15202 it->w->nrows_scale_factor++;
15203 fonts_changed_p = 1;
15204 return 0;
15205 }
15206
15207 /* Is IT->w showing the region? */
15208 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
15209
15210 /* Clear the result glyph row and enable it. */
15211 prepare_desired_row (row);
15212
15213 row->y = it->current_y;
15214 row->start = it->start;
15215 row->continuation_lines_width = it->continuation_lines_width;
15216 row->displays_text_p = 1;
15217 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
15218 it->starts_in_middle_of_char_p = 0;
15219
15220 /* Arrange the overlays nicely for our purposes. Usually, we call
15221 display_line on only one line at a time, in which case this
15222 can't really hurt too much, or we call it on lines which appear
15223 one after another in the buffer, in which case all calls to
15224 recenter_overlay_lists but the first will be pretty cheap. */
15225 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
15226
15227 /* Move over display elements that are not visible because we are
15228 hscrolled. This may stop at an x-position < IT->first_visible_x
15229 if the first glyph is partially visible or if we hit a line end. */
15230 if (it->current_x < it->first_visible_x)
15231 {
15232 move_it_in_display_line_to (it, ZV, it->first_visible_x,
15233 MOVE_TO_POS | MOVE_TO_X);
15234 }
15235
15236 /* Get the initial row height. This is either the height of the
15237 text hscrolled, if there is any, or zero. */
15238 row->ascent = it->max_ascent;
15239 row->height = it->max_ascent + it->max_descent;
15240 row->phys_ascent = it->max_phys_ascent;
15241 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
15242 row->extra_line_spacing = it->max_extra_line_spacing;
15243
15244 /* Loop generating characters. The loop is left with IT on the next
15245 character to display. */
15246 while (1)
15247 {
15248 int n_glyphs_before, hpos_before, x_before;
15249 int x, i, nglyphs;
15250 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
15251
15252 /* Retrieve the next thing to display. Value is zero if end of
15253 buffer reached. */
15254 if (!get_next_display_element (it))
15255 {
15256 /* Maybe add a space at the end of this line that is used to
15257 display the cursor there under X. Set the charpos of the
15258 first glyph of blank lines not corresponding to any text
15259 to -1. */
15260 #ifdef HAVE_WINDOW_SYSTEM
15261 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
15262 row->exact_window_width_line_p = 1;
15263 else
15264 #endif /* HAVE_WINDOW_SYSTEM */
15265 if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
15266 || row->used[TEXT_AREA] == 0)
15267 {
15268 row->glyphs[TEXT_AREA]->charpos = -1;
15269 row->displays_text_p = 0;
15270
15271 if (!NILP (XBUFFER (it->w->buffer)->indicate_empty_lines)
15272 && (!MINI_WINDOW_P (it->w)
15273 || (minibuf_level && EQ (it->window, minibuf_window))))
15274 row->indicate_empty_line_p = 1;
15275 }
15276
15277 it->continuation_lines_width = 0;
15278 row->ends_at_zv_p = 1;
15279 break;
15280 }
15281
15282 /* Now, get the metrics of what we want to display. This also
15283 generates glyphs in `row' (which is IT->glyph_row). */
15284 n_glyphs_before = row->used[TEXT_AREA];
15285 x = it->current_x;
15286
15287 /* Remember the line height so far in case the next element doesn't
15288 fit on the line. */
15289 if (!it->truncate_lines_p)
15290 {
15291 ascent = it->max_ascent;
15292 descent = it->max_descent;
15293 phys_ascent = it->max_phys_ascent;
15294 phys_descent = it->max_phys_descent;
15295 }
15296
15297 PRODUCE_GLYPHS (it);
15298
15299 /* If this display element was in marginal areas, continue with
15300 the next one. */
15301 if (it->area != TEXT_AREA)
15302 {
15303 row->ascent = max (row->ascent, it->max_ascent);
15304 row->height = max (row->height, it->max_ascent + it->max_descent);
15305 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
15306 row->phys_height = max (row->phys_height,
15307 it->max_phys_ascent + it->max_phys_descent);
15308 row->extra_line_spacing = max (row->extra_line_spacing,
15309 it->max_extra_line_spacing);
15310 set_iterator_to_next (it, 1);
15311 continue;
15312 }
15313
15314 /* Does the display element fit on the line? If we truncate
15315 lines, we should draw past the right edge of the window. If
15316 we don't truncate, we want to stop so that we can display the
15317 continuation glyph before the right margin. If lines are
15318 continued, there are two possible strategies for characters
15319 resulting in more than 1 glyph (e.g. tabs): Display as many
15320 glyphs as possible in this line and leave the rest for the
15321 continuation line, or display the whole element in the next
15322 line. Original redisplay did the former, so we do it also. */
15323 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
15324 hpos_before = it->hpos;
15325 x_before = x;
15326
15327 if (/* Not a newline. */
15328 nglyphs > 0
15329 /* Glyphs produced fit entirely in the line. */
15330 && it->current_x < it->last_visible_x)
15331 {
15332 it->hpos += nglyphs;
15333 row->ascent = max (row->ascent, it->max_ascent);
15334 row->height = max (row->height, it->max_ascent + it->max_descent);
15335 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
15336 row->phys_height = max (row->phys_height,
15337 it->max_phys_ascent + it->max_phys_descent);
15338 row->extra_line_spacing = max (row->extra_line_spacing,
15339 it->max_extra_line_spacing);
15340 if (it->current_x - it->pixel_width < it->first_visible_x)
15341 row->x = x - it->first_visible_x;
15342 }
15343 else
15344 {
15345 int new_x;
15346 struct glyph *glyph;
15347
15348 for (i = 0; i < nglyphs; ++i, x = new_x)
15349 {
15350 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
15351 new_x = x + glyph->pixel_width;
15352
15353 if (/* Lines are continued. */
15354 !it->truncate_lines_p
15355 && (/* Glyph doesn't fit on the line. */
15356 new_x > it->last_visible_x
15357 /* Or it fits exactly on a window system frame. */
15358 || (new_x == it->last_visible_x
15359 && FRAME_WINDOW_P (it->f))))
15360 {
15361 /* End of a continued line. */
15362
15363 if (it->hpos == 0
15364 || (new_x == it->last_visible_x
15365 && FRAME_WINDOW_P (it->f)))
15366 {
15367 /* Current glyph is the only one on the line or
15368 fits exactly on the line. We must continue
15369 the line because we can't draw the cursor
15370 after the glyph. */
15371 row->continued_p = 1;
15372 it->current_x = new_x;
15373 it->continuation_lines_width += new_x;
15374 ++it->hpos;
15375 if (i == nglyphs - 1)
15376 {
15377 set_iterator_to_next (it, 1);
15378 #ifdef HAVE_WINDOW_SYSTEM
15379 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
15380 {
15381 if (!get_next_display_element (it))
15382 {
15383 row->exact_window_width_line_p = 1;
15384 it->continuation_lines_width = 0;
15385 row->continued_p = 0;
15386 row->ends_at_zv_p = 1;
15387 }
15388 else if (ITERATOR_AT_END_OF_LINE_P (it))
15389 {
15390 row->continued_p = 0;
15391 row->exact_window_width_line_p = 1;
15392 }
15393 }
15394 #endif /* HAVE_WINDOW_SYSTEM */
15395 }
15396 }
15397 else if (CHAR_GLYPH_PADDING_P (*glyph)
15398 && !FRAME_WINDOW_P (it->f))
15399 {
15400 /* A padding glyph that doesn't fit on this line.
15401 This means the whole character doesn't fit
15402 on the line. */
15403 row->used[TEXT_AREA] = n_glyphs_before;
15404
15405 /* Fill the rest of the row with continuation
15406 glyphs like in 20.x. */
15407 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
15408 < row->glyphs[1 + TEXT_AREA])
15409 produce_special_glyphs (it, IT_CONTINUATION);
15410
15411 row->continued_p = 1;
15412 it->current_x = x_before;
15413 it->continuation_lines_width += x_before;
15414
15415 /* Restore the height to what it was before the
15416 element not fitting on the line. */
15417 it->max_ascent = ascent;
15418 it->max_descent = descent;
15419 it->max_phys_ascent = phys_ascent;
15420 it->max_phys_descent = phys_descent;
15421 }
15422 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
15423 {
15424 /* A TAB that extends past the right edge of the
15425 window. This produces a single glyph on
15426 window system frames. We leave the glyph in
15427 this row and let it fill the row, but don't
15428 consume the TAB. */
15429 it->continuation_lines_width += it->last_visible_x;
15430 row->ends_in_middle_of_char_p = 1;
15431 row->continued_p = 1;
15432 glyph->pixel_width = it->last_visible_x - x;
15433 it->starts_in_middle_of_char_p = 1;
15434 }
15435 else
15436 {
15437 /* Something other than a TAB that draws past
15438 the right edge of the window. Restore
15439 positions to values before the element. */
15440 row->used[TEXT_AREA] = n_glyphs_before + i;
15441
15442 /* Display continuation glyphs. */
15443 if (!FRAME_WINDOW_P (it->f))
15444 produce_special_glyphs (it, IT_CONTINUATION);
15445 row->continued_p = 1;
15446
15447 it->continuation_lines_width += x;
15448
15449 if (nglyphs > 1 && i > 0)
15450 {
15451 row->ends_in_middle_of_char_p = 1;
15452 it->starts_in_middle_of_char_p = 1;
15453 }
15454
15455 /* Restore the height to what it was before the
15456 element not fitting on the line. */
15457 it->max_ascent = ascent;
15458 it->max_descent = descent;
15459 it->max_phys_ascent = phys_ascent;
15460 it->max_phys_descent = phys_descent;
15461 }
15462
15463 break;
15464 }
15465 else if (new_x > it->first_visible_x)
15466 {
15467 /* Increment number of glyphs actually displayed. */
15468 ++it->hpos;
15469
15470 if (x < it->first_visible_x)
15471 /* Glyph is partially visible, i.e. row starts at
15472 negative X position. */
15473 row->x = x - it->first_visible_x;
15474 }
15475 else
15476 {
15477 /* Glyph is completely off the left margin of the
15478 window. This should not happen because of the
15479 move_it_in_display_line at the start of this
15480 function, unless the text display area of the
15481 window is empty. */
15482 xassert (it->first_visible_x <= it->last_visible_x);
15483 }
15484 }
15485
15486 row->ascent = max (row->ascent, it->max_ascent);
15487 row->height = max (row->height, it->max_ascent + it->max_descent);
15488 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
15489 row->phys_height = max (row->phys_height,
15490 it->max_phys_ascent + it->max_phys_descent);
15491 row->extra_line_spacing = max (row->extra_line_spacing,
15492 it->max_extra_line_spacing);
15493
15494 /* End of this display line if row is continued. */
15495 if (row->continued_p || row->ends_at_zv_p)
15496 break;
15497 }
15498
15499 at_end_of_line:
15500 /* Is this a line end? If yes, we're also done, after making
15501 sure that a non-default face is extended up to the right
15502 margin of the window. */
15503 if (ITERATOR_AT_END_OF_LINE_P (it))
15504 {
15505 int used_before = row->used[TEXT_AREA];
15506
15507 row->ends_in_newline_from_string_p = STRINGP (it->object);
15508
15509 #ifdef HAVE_WINDOW_SYSTEM
15510 /* Add a space at the end of the line that is used to
15511 display the cursor there. */
15512 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
15513 append_space_for_newline (it, 0);
15514 #endif /* HAVE_WINDOW_SYSTEM */
15515
15516 /* Extend the face to the end of the line. */
15517 extend_face_to_end_of_line (it);
15518
15519 /* Make sure we have the position. */
15520 if (used_before == 0)
15521 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
15522
15523 /* Consume the line end. This skips over invisible lines. */
15524 set_iterator_to_next (it, 1);
15525 it->continuation_lines_width = 0;
15526 break;
15527 }
15528
15529 /* Proceed with next display element. Note that this skips
15530 over lines invisible because of selective display. */
15531 set_iterator_to_next (it, 1);
15532
15533 /* If we truncate lines, we are done when the last displayed
15534 glyphs reach past the right margin of the window. */
15535 if (it->truncate_lines_p
15536 && (FRAME_WINDOW_P (it->f)
15537 ? (it->current_x >= it->last_visible_x)
15538 : (it->current_x > it->last_visible_x)))
15539 {
15540 /* Maybe add truncation glyphs. */
15541 if (!FRAME_WINDOW_P (it->f))
15542 {
15543 int i, n;
15544
15545 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
15546 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
15547 break;
15548
15549 for (n = row->used[TEXT_AREA]; i < n; ++i)
15550 {
15551 row->used[TEXT_AREA] = i;
15552 produce_special_glyphs (it, IT_TRUNCATION);
15553 }
15554 }
15555 #ifdef HAVE_WINDOW_SYSTEM
15556 else
15557 {
15558 /* Don't truncate if we can overflow newline into fringe. */
15559 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
15560 {
15561 if (!get_next_display_element (it))
15562 {
15563 it->continuation_lines_width = 0;
15564 row->ends_at_zv_p = 1;
15565 row->exact_window_width_line_p = 1;
15566 break;
15567 }
15568 if (ITERATOR_AT_END_OF_LINE_P (it))
15569 {
15570 row->exact_window_width_line_p = 1;
15571 goto at_end_of_line;
15572 }
15573 }
15574 }
15575 #endif /* HAVE_WINDOW_SYSTEM */
15576
15577 row->truncated_on_right_p = 1;
15578 it->continuation_lines_width = 0;
15579 reseat_at_next_visible_line_start (it, 0);
15580 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
15581 it->hpos = hpos_before;
15582 it->current_x = x_before;
15583 break;
15584 }
15585 }
15586
15587 /* If line is not empty and hscrolled, maybe insert truncation glyphs
15588 at the left window margin. */
15589 if (it->first_visible_x
15590 && IT_CHARPOS (*it) != MATRIX_ROW_START_CHARPOS (row))
15591 {
15592 if (!FRAME_WINDOW_P (it->f))
15593 insert_left_trunc_glyphs (it);
15594 row->truncated_on_left_p = 1;
15595 }
15596
15597 /* If the start of this line is the overlay arrow-position, then
15598 mark this glyph row as the one containing the overlay arrow.
15599 This is clearly a mess with variable size fonts. It would be
15600 better to let it be displayed like cursors under X. */
15601 if ((row->displays_text_p || !overlay_arrow_seen)
15602 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
15603 !NILP (overlay_arrow_string)))
15604 {
15605 /* Overlay arrow in window redisplay is a fringe bitmap. */
15606 if (STRINGP (overlay_arrow_string))
15607 {
15608 struct glyph_row *arrow_row
15609 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
15610 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
15611 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
15612 struct glyph *p = row->glyphs[TEXT_AREA];
15613 struct glyph *p2, *end;
15614
15615 /* Copy the arrow glyphs. */
15616 while (glyph < arrow_end)
15617 *p++ = *glyph++;
15618
15619 /* Throw away padding glyphs. */
15620 p2 = p;
15621 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
15622 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
15623 ++p2;
15624 if (p2 > p)
15625 {
15626 while (p2 < end)
15627 *p++ = *p2++;
15628 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
15629 }
15630 }
15631 else
15632 {
15633 xassert (INTEGERP (overlay_arrow_string));
15634 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
15635 }
15636 overlay_arrow_seen = 1;
15637 }
15638
15639 /* Compute pixel dimensions of this line. */
15640 compute_line_metrics (it);
15641
15642 /* Remember the position at which this line ends. */
15643 row->end = it->current;
15644
15645 /* Record whether this row ends inside an ellipsis. */
15646 row->ends_in_ellipsis_p
15647 = (it->method == GET_FROM_DISPLAY_VECTOR
15648 && it->ellipsis_p);
15649
15650 /* Save fringe bitmaps in this row. */
15651 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
15652 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
15653 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
15654 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
15655
15656 it->left_user_fringe_bitmap = 0;
15657 it->left_user_fringe_face_id = 0;
15658 it->right_user_fringe_bitmap = 0;
15659 it->right_user_fringe_face_id = 0;
15660
15661 /* Maybe set the cursor. */
15662 if (it->w->cursor.vpos < 0
15663 && PT >= MATRIX_ROW_START_CHARPOS (row)
15664 && PT <= MATRIX_ROW_END_CHARPOS (row)
15665 && cursor_row_p (it->w, row))
15666 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
15667
15668 /* Highlight trailing whitespace. */
15669 if (!NILP (Vshow_trailing_whitespace))
15670 highlight_trailing_whitespace (it->f, it->glyph_row);
15671
15672 /* Prepare for the next line. This line starts horizontally at (X
15673 HPOS) = (0 0). Vertical positions are incremented. As a
15674 convenience for the caller, IT->glyph_row is set to the next
15675 row to be used. */
15676 it->current_x = it->hpos = 0;
15677 it->current_y += row->height;
15678 ++it->vpos;
15679 ++it->glyph_row;
15680 it->start = it->current;
15681 return row->displays_text_p;
15682 }
15683
15684
15685 \f
15686 /***********************************************************************
15687 Menu Bar
15688 ***********************************************************************/
15689
15690 /* Redisplay the menu bar in the frame for window W.
15691
15692 The menu bar of X frames that don't have X toolkit support is
15693 displayed in a special window W->frame->menu_bar_window.
15694
15695 The menu bar of terminal frames is treated specially as far as
15696 glyph matrices are concerned. Menu bar lines are not part of
15697 windows, so the update is done directly on the frame matrix rows
15698 for the menu bar. */
15699
15700 static void
15701 display_menu_bar (w)
15702 struct window *w;
15703 {
15704 struct frame *f = XFRAME (WINDOW_FRAME (w));
15705 struct it it;
15706 Lisp_Object items;
15707 int i;
15708
15709 /* Don't do all this for graphical frames. */
15710 #ifdef HAVE_NTGUI
15711 if (!NILP (Vwindow_system))
15712 return;
15713 #endif
15714 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
15715 if (FRAME_X_P (f))
15716 return;
15717 #endif
15718 #ifdef MAC_OS
15719 if (FRAME_MAC_P (f))
15720 return;
15721 #endif
15722
15723 #ifdef USE_X_TOOLKIT
15724 xassert (!FRAME_WINDOW_P (f));
15725 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
15726 it.first_visible_x = 0;
15727 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
15728 #else /* not USE_X_TOOLKIT */
15729 if (FRAME_WINDOW_P (f))
15730 {
15731 /* Menu bar lines are displayed in the desired matrix of the
15732 dummy window menu_bar_window. */
15733 struct window *menu_w;
15734 xassert (WINDOWP (f->menu_bar_window));
15735 menu_w = XWINDOW (f->menu_bar_window);
15736 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
15737 MENU_FACE_ID);
15738 it.first_visible_x = 0;
15739 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
15740 }
15741 else
15742 {
15743 /* This is a TTY frame, i.e. character hpos/vpos are used as
15744 pixel x/y. */
15745 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
15746 MENU_FACE_ID);
15747 it.first_visible_x = 0;
15748 it.last_visible_x = FRAME_COLS (f);
15749 }
15750 #endif /* not USE_X_TOOLKIT */
15751
15752 if (! mode_line_inverse_video)
15753 /* Force the menu-bar to be displayed in the default face. */
15754 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
15755
15756 /* Clear all rows of the menu bar. */
15757 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
15758 {
15759 struct glyph_row *row = it.glyph_row + i;
15760 clear_glyph_row (row);
15761 row->enabled_p = 1;
15762 row->full_width_p = 1;
15763 }
15764
15765 /* Display all items of the menu bar. */
15766 items = FRAME_MENU_BAR_ITEMS (it.f);
15767 for (i = 0; i < XVECTOR (items)->size; i += 4)
15768 {
15769 Lisp_Object string;
15770
15771 /* Stop at nil string. */
15772 string = AREF (items, i + 1);
15773 if (NILP (string))
15774 break;
15775
15776 /* Remember where item was displayed. */
15777 AREF (items, i + 3) = make_number (it.hpos);
15778
15779 /* Display the item, pad with one space. */
15780 if (it.current_x < it.last_visible_x)
15781 display_string (NULL, string, Qnil, 0, 0, &it,
15782 SCHARS (string) + 1, 0, 0, -1);
15783 }
15784
15785 /* Fill out the line with spaces. */
15786 if (it.current_x < it.last_visible_x)
15787 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
15788
15789 /* Compute the total height of the lines. */
15790 compute_line_metrics (&it);
15791 }
15792
15793
15794 \f
15795 /***********************************************************************
15796 Mode Line
15797 ***********************************************************************/
15798
15799 /* Redisplay mode lines in the window tree whose root is WINDOW. If
15800 FORCE is non-zero, redisplay mode lines unconditionally.
15801 Otherwise, redisplay only mode lines that are garbaged. Value is
15802 the number of windows whose mode lines were redisplayed. */
15803
15804 static int
15805 redisplay_mode_lines (window, force)
15806 Lisp_Object window;
15807 int force;
15808 {
15809 int nwindows = 0;
15810
15811 while (!NILP (window))
15812 {
15813 struct window *w = XWINDOW (window);
15814
15815 if (WINDOWP (w->hchild))
15816 nwindows += redisplay_mode_lines (w->hchild, force);
15817 else if (WINDOWP (w->vchild))
15818 nwindows += redisplay_mode_lines (w->vchild, force);
15819 else if (force
15820 || FRAME_GARBAGED_P (XFRAME (w->frame))
15821 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
15822 {
15823 struct text_pos lpoint;
15824 struct buffer *old = current_buffer;
15825
15826 /* Set the window's buffer for the mode line display. */
15827 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15828 set_buffer_internal_1 (XBUFFER (w->buffer));
15829
15830 /* Point refers normally to the selected window. For any
15831 other window, set up appropriate value. */
15832 if (!EQ (window, selected_window))
15833 {
15834 struct text_pos pt;
15835
15836 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
15837 if (CHARPOS (pt) < BEGV)
15838 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
15839 else if (CHARPOS (pt) > (ZV - 1))
15840 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
15841 else
15842 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
15843 }
15844
15845 /* Display mode lines. */
15846 clear_glyph_matrix (w->desired_matrix);
15847 if (display_mode_lines (w))
15848 {
15849 ++nwindows;
15850 w->must_be_updated_p = 1;
15851 }
15852
15853 /* Restore old settings. */
15854 set_buffer_internal_1 (old);
15855 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
15856 }
15857
15858 window = w->next;
15859 }
15860
15861 return nwindows;
15862 }
15863
15864
15865 /* Display the mode and/or top line of window W. Value is the number
15866 of mode lines displayed. */
15867
15868 static int
15869 display_mode_lines (w)
15870 struct window *w;
15871 {
15872 Lisp_Object old_selected_window, old_selected_frame;
15873 int n = 0;
15874
15875 old_selected_frame = selected_frame;
15876 selected_frame = w->frame;
15877 old_selected_window = selected_window;
15878 XSETWINDOW (selected_window, w);
15879
15880 /* These will be set while the mode line specs are processed. */
15881 line_number_displayed = 0;
15882 w->column_number_displayed = Qnil;
15883
15884 if (WINDOW_WANTS_MODELINE_P (w))
15885 {
15886 struct window *sel_w = XWINDOW (old_selected_window);
15887
15888 /* Select mode line face based on the real selected window. */
15889 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
15890 current_buffer->mode_line_format);
15891 ++n;
15892 }
15893
15894 if (WINDOW_WANTS_HEADER_LINE_P (w))
15895 {
15896 display_mode_line (w, HEADER_LINE_FACE_ID,
15897 current_buffer->header_line_format);
15898 ++n;
15899 }
15900
15901 selected_frame = old_selected_frame;
15902 selected_window = old_selected_window;
15903 return n;
15904 }
15905
15906
15907 /* Display mode or top line of window W. FACE_ID specifies which line
15908 to display; it is either MODE_LINE_FACE_ID or HEADER_LINE_FACE_ID.
15909 FORMAT is the mode line format to display. Value is the pixel
15910 height of the mode line displayed. */
15911
15912 static int
15913 display_mode_line (w, face_id, format)
15914 struct window *w;
15915 enum face_id face_id;
15916 Lisp_Object format;
15917 {
15918 struct it it;
15919 struct face *face;
15920 int count = SPECPDL_INDEX ();
15921
15922 init_iterator (&it, w, -1, -1, NULL, face_id);
15923 prepare_desired_row (it.glyph_row);
15924
15925 it.glyph_row->mode_line_p = 1;
15926
15927 if (! mode_line_inverse_video)
15928 /* Force the mode-line to be displayed in the default face. */
15929 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
15930
15931 record_unwind_protect (unwind_format_mode_line,
15932 format_mode_line_unwind_data (NULL));
15933
15934 mode_line_target = MODE_LINE_DISPLAY;
15935
15936 /* Temporarily make frame's keyboard the current kboard so that
15937 kboard-local variables in the mode_line_format will get the right
15938 values. */
15939 push_frame_kboard (it.f);
15940 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
15941 pop_frame_kboard ();
15942
15943 unbind_to (count, Qnil);
15944
15945 /* Fill up with spaces. */
15946 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
15947
15948 compute_line_metrics (&it);
15949 it.glyph_row->full_width_p = 1;
15950 it.glyph_row->continued_p = 0;
15951 it.glyph_row->truncated_on_left_p = 0;
15952 it.glyph_row->truncated_on_right_p = 0;
15953
15954 /* Make a 3D mode-line have a shadow at its right end. */
15955 face = FACE_FROM_ID (it.f, face_id);
15956 extend_face_to_end_of_line (&it);
15957 if (face->box != FACE_NO_BOX)
15958 {
15959 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
15960 + it.glyph_row->used[TEXT_AREA] - 1);
15961 last->right_box_line_p = 1;
15962 }
15963
15964 return it.glyph_row->height;
15965 }
15966
15967 /* Contribute ELT to the mode line for window IT->w. How it
15968 translates into text depends on its data type.
15969
15970 IT describes the display environment in which we display, as usual.
15971
15972 DEPTH is the depth in recursion. It is used to prevent
15973 infinite recursion here.
15974
15975 FIELD_WIDTH is the number of characters the display of ELT should
15976 occupy in the mode line, and PRECISION is the maximum number of
15977 characters to display from ELT's representation. See
15978 display_string for details.
15979
15980 Returns the hpos of the end of the text generated by ELT.
15981
15982 PROPS is a property list to add to any string we encounter.
15983
15984 If RISKY is nonzero, remove (disregard) any properties in any string
15985 we encounter, and ignore :eval and :propertize.
15986
15987 The global variable `mode_line_target' determines whether the
15988 output is passed to `store_mode_line_noprop',
15989 `store_mode_line_string', or `display_string'. */
15990
15991 static int
15992 display_mode_element (it, depth, field_width, precision, elt, props, risky)
15993 struct it *it;
15994 int depth;
15995 int field_width, precision;
15996 Lisp_Object elt, props;
15997 int risky;
15998 {
15999 int n = 0, field, prec;
16000 int literal = 0;
16001
16002 tail_recurse:
16003 if (depth > 100)
16004 elt = build_string ("*too-deep*");
16005
16006 depth++;
16007
16008 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
16009 {
16010 case Lisp_String:
16011 {
16012 /* A string: output it and check for %-constructs within it. */
16013 unsigned char c;
16014 const unsigned char *this, *lisp_string;
16015
16016 if (!NILP (props) || risky)
16017 {
16018 Lisp_Object oprops, aelt;
16019 oprops = Ftext_properties_at (make_number (0), elt);
16020
16021 /* If the starting string's properties are not what
16022 we want, translate the string. Also, if the string
16023 is risky, do that anyway. */
16024
16025 if (NILP (Fequal (props, oprops)) || risky)
16026 {
16027 /* If the starting string has properties,
16028 merge the specified ones onto the existing ones. */
16029 if (! NILP (oprops) && !risky)
16030 {
16031 Lisp_Object tem;
16032
16033 oprops = Fcopy_sequence (oprops);
16034 tem = props;
16035 while (CONSP (tem))
16036 {
16037 oprops = Fplist_put (oprops, XCAR (tem),
16038 XCAR (XCDR (tem)));
16039 tem = XCDR (XCDR (tem));
16040 }
16041 props = oprops;
16042 }
16043
16044 aelt = Fassoc (elt, mode_line_proptrans_alist);
16045 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
16046 {
16047 mode_line_proptrans_alist
16048 = Fcons (aelt, Fdelq (aelt, mode_line_proptrans_alist));
16049 elt = XCAR (aelt);
16050 }
16051 else
16052 {
16053 Lisp_Object tem;
16054
16055 elt = Fcopy_sequence (elt);
16056 Fset_text_properties (make_number (0), Flength (elt),
16057 props, elt);
16058 /* Add this item to mode_line_proptrans_alist. */
16059 mode_line_proptrans_alist
16060 = Fcons (Fcons (elt, props),
16061 mode_line_proptrans_alist);
16062 /* Truncate mode_line_proptrans_alist
16063 to at most 50 elements. */
16064 tem = Fnthcdr (make_number (50),
16065 mode_line_proptrans_alist);
16066 if (! NILP (tem))
16067 XSETCDR (tem, Qnil);
16068 }
16069 }
16070 }
16071
16072 this = SDATA (elt);
16073 lisp_string = this;
16074
16075 if (literal)
16076 {
16077 prec = precision - n;
16078 switch (mode_line_target)
16079 {
16080 case MODE_LINE_NOPROP:
16081 case MODE_LINE_TITLE:
16082 n += store_mode_line_noprop (SDATA (elt), -1, prec);
16083 break;
16084 case MODE_LINE_STRING:
16085 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
16086 break;
16087 case MODE_LINE_DISPLAY:
16088 n += display_string (NULL, elt, Qnil, 0, 0, it,
16089 0, prec, 0, STRING_MULTIBYTE (elt));
16090 break;
16091 }
16092
16093 break;
16094 }
16095
16096 while ((precision <= 0 || n < precision)
16097 && *this
16098 && (mode_line_target != MODE_LINE_DISPLAY
16099 || it->current_x < it->last_visible_x))
16100 {
16101 const unsigned char *last = this;
16102
16103 /* Advance to end of string or next format specifier. */
16104 while ((c = *this++) != '\0' && c != '%')
16105 ;
16106
16107 if (this - 1 != last)
16108 {
16109 int nchars, nbytes;
16110
16111 /* Output to end of string or up to '%'. Field width
16112 is length of string. Don't output more than
16113 PRECISION allows us. */
16114 --this;
16115
16116 prec = c_string_width (last, this - last, precision - n,
16117 &nchars, &nbytes);
16118
16119 switch (mode_line_target)
16120 {
16121 case MODE_LINE_NOPROP:
16122 case MODE_LINE_TITLE:
16123 n += store_mode_line_noprop (last, 0, prec);
16124 break;
16125 case MODE_LINE_STRING:
16126 {
16127 int bytepos = last - lisp_string;
16128 int charpos = string_byte_to_char (elt, bytepos);
16129 int endpos = (precision <= 0
16130 ? string_byte_to_char (elt,
16131 this - lisp_string)
16132 : charpos + nchars);
16133
16134 n += store_mode_line_string (NULL,
16135 Fsubstring (elt, make_number (charpos),
16136 make_number (endpos)),
16137 0, 0, 0, Qnil);
16138 }
16139 break;
16140 case MODE_LINE_DISPLAY:
16141 {
16142 int bytepos = last - lisp_string;
16143 int charpos = string_byte_to_char (elt, bytepos);
16144 n += display_string (NULL, elt, Qnil, 0, charpos,
16145 it, 0, prec, 0,
16146 STRING_MULTIBYTE (elt));
16147 }
16148 break;
16149 }
16150 }
16151 else /* c == '%' */
16152 {
16153 const unsigned char *percent_position = this;
16154
16155 /* Get the specified minimum width. Zero means
16156 don't pad. */
16157 field = 0;
16158 while ((c = *this++) >= '0' && c <= '9')
16159 field = field * 10 + c - '0';
16160
16161 /* Don't pad beyond the total padding allowed. */
16162 if (field_width - n > 0 && field > field_width - n)
16163 field = field_width - n;
16164
16165 /* Note that either PRECISION <= 0 or N < PRECISION. */
16166 prec = precision - n;
16167
16168 if (c == 'M')
16169 n += display_mode_element (it, depth, field, prec,
16170 Vglobal_mode_string, props,
16171 risky);
16172 else if (c != 0)
16173 {
16174 int multibyte;
16175 int bytepos, charpos;
16176 unsigned char *spec;
16177
16178 bytepos = percent_position - lisp_string;
16179 charpos = (STRING_MULTIBYTE (elt)
16180 ? string_byte_to_char (elt, bytepos)
16181 : bytepos);
16182
16183 spec
16184 = decode_mode_spec (it->w, c, field, prec, &multibyte);
16185
16186 switch (mode_line_target)
16187 {
16188 case MODE_LINE_NOPROP:
16189 case MODE_LINE_TITLE:
16190 n += store_mode_line_noprop (spec, field, prec);
16191 break;
16192 case MODE_LINE_STRING:
16193 {
16194 int len = strlen (spec);
16195 Lisp_Object tem = make_string (spec, len);
16196 props = Ftext_properties_at (make_number (charpos), elt);
16197 /* Should only keep face property in props */
16198 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
16199 }
16200 break;
16201 case MODE_LINE_DISPLAY:
16202 {
16203 int nglyphs_before, nwritten;
16204
16205 nglyphs_before = it->glyph_row->used[TEXT_AREA];
16206 nwritten = display_string (spec, Qnil, elt,
16207 charpos, 0, it,
16208 field, prec, 0,
16209 multibyte);
16210
16211 /* Assign to the glyphs written above the
16212 string where the `%x' came from, position
16213 of the `%'. */
16214 if (nwritten > 0)
16215 {
16216 struct glyph *glyph
16217 = (it->glyph_row->glyphs[TEXT_AREA]
16218 + nglyphs_before);
16219 int i;
16220
16221 for (i = 0; i < nwritten; ++i)
16222 {
16223 glyph[i].object = elt;
16224 glyph[i].charpos = charpos;
16225 }
16226
16227 n += nwritten;
16228 }
16229 }
16230 break;
16231 }
16232 }
16233 else /* c == 0 */
16234 break;
16235 }
16236 }
16237 }
16238 break;
16239
16240 case Lisp_Symbol:
16241 /* A symbol: process the value of the symbol recursively
16242 as if it appeared here directly. Avoid error if symbol void.
16243 Special case: if value of symbol is a string, output the string
16244 literally. */
16245 {
16246 register Lisp_Object tem;
16247
16248 /* If the variable is not marked as risky to set
16249 then its contents are risky to use. */
16250 if (NILP (Fget (elt, Qrisky_local_variable)))
16251 risky = 1;
16252
16253 tem = Fboundp (elt);
16254 if (!NILP (tem))
16255 {
16256 tem = Fsymbol_value (elt);
16257 /* If value is a string, output that string literally:
16258 don't check for % within it. */
16259 if (STRINGP (tem))
16260 literal = 1;
16261
16262 if (!EQ (tem, elt))
16263 {
16264 /* Give up right away for nil or t. */
16265 elt = tem;
16266 goto tail_recurse;
16267 }
16268 }
16269 }
16270 break;
16271
16272 case Lisp_Cons:
16273 {
16274 register Lisp_Object car, tem;
16275
16276 /* A cons cell: five distinct cases.
16277 If first element is :eval or :propertize, do something special.
16278 If first element is a string or a cons, process all the elements
16279 and effectively concatenate them.
16280 If first element is a negative number, truncate displaying cdr to
16281 at most that many characters. If positive, pad (with spaces)
16282 to at least that many characters.
16283 If first element is a symbol, process the cadr or caddr recursively
16284 according to whether the symbol's value is non-nil or nil. */
16285 car = XCAR (elt);
16286 if (EQ (car, QCeval))
16287 {
16288 /* An element of the form (:eval FORM) means evaluate FORM
16289 and use the result as mode line elements. */
16290
16291 if (risky)
16292 break;
16293
16294 if (CONSP (XCDR (elt)))
16295 {
16296 Lisp_Object spec;
16297 spec = safe_eval (XCAR (XCDR (elt)));
16298 n += display_mode_element (it, depth, field_width - n,
16299 precision - n, spec, props,
16300 risky);
16301 }
16302 }
16303 else if (EQ (car, QCpropertize))
16304 {
16305 /* An element of the form (:propertize ELT PROPS...)
16306 means display ELT but applying properties PROPS. */
16307
16308 if (risky)
16309 break;
16310
16311 if (CONSP (XCDR (elt)))
16312 n += display_mode_element (it, depth, field_width - n,
16313 precision - n, XCAR (XCDR (elt)),
16314 XCDR (XCDR (elt)), risky);
16315 }
16316 else if (SYMBOLP (car))
16317 {
16318 tem = Fboundp (car);
16319 elt = XCDR (elt);
16320 if (!CONSP (elt))
16321 goto invalid;
16322 /* elt is now the cdr, and we know it is a cons cell.
16323 Use its car if CAR has a non-nil value. */
16324 if (!NILP (tem))
16325 {
16326 tem = Fsymbol_value (car);
16327 if (!NILP (tem))
16328 {
16329 elt = XCAR (elt);
16330 goto tail_recurse;
16331 }
16332 }
16333 /* Symbol's value is nil (or symbol is unbound)
16334 Get the cddr of the original list
16335 and if possible find the caddr and use that. */
16336 elt = XCDR (elt);
16337 if (NILP (elt))
16338 break;
16339 else if (!CONSP (elt))
16340 goto invalid;
16341 elt = XCAR (elt);
16342 goto tail_recurse;
16343 }
16344 else if (INTEGERP (car))
16345 {
16346 register int lim = XINT (car);
16347 elt = XCDR (elt);
16348 if (lim < 0)
16349 {
16350 /* Negative int means reduce maximum width. */
16351 if (precision <= 0)
16352 precision = -lim;
16353 else
16354 precision = min (precision, -lim);
16355 }
16356 else if (lim > 0)
16357 {
16358 /* Padding specified. Don't let it be more than
16359 current maximum. */
16360 if (precision > 0)
16361 lim = min (precision, lim);
16362
16363 /* If that's more padding than already wanted, queue it.
16364 But don't reduce padding already specified even if
16365 that is beyond the current truncation point. */
16366 field_width = max (lim, field_width);
16367 }
16368 goto tail_recurse;
16369 }
16370 else if (STRINGP (car) || CONSP (car))
16371 {
16372 register int limit = 50;
16373 /* Limit is to protect against circular lists. */
16374 while (CONSP (elt)
16375 && --limit > 0
16376 && (precision <= 0 || n < precision))
16377 {
16378 n += display_mode_element (it, depth,
16379 /* Do padding only after the last
16380 element in the list. */
16381 (! CONSP (XCDR (elt))
16382 ? field_width - n
16383 : 0),
16384 precision - n, XCAR (elt),
16385 props, risky);
16386 elt = XCDR (elt);
16387 }
16388 }
16389 }
16390 break;
16391
16392 default:
16393 invalid:
16394 elt = build_string ("*invalid*");
16395 goto tail_recurse;
16396 }
16397
16398 /* Pad to FIELD_WIDTH. */
16399 if (field_width > 0 && n < field_width)
16400 {
16401 switch (mode_line_target)
16402 {
16403 case MODE_LINE_NOPROP:
16404 case MODE_LINE_TITLE:
16405 n += store_mode_line_noprop ("", field_width - n, 0);
16406 break;
16407 case MODE_LINE_STRING:
16408 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
16409 break;
16410 case MODE_LINE_DISPLAY:
16411 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
16412 0, 0, 0);
16413 break;
16414 }
16415 }
16416
16417 return n;
16418 }
16419
16420 /* Store a mode-line string element in mode_line_string_list.
16421
16422 If STRING is non-null, display that C string. Otherwise, the Lisp
16423 string LISP_STRING is displayed.
16424
16425 FIELD_WIDTH is the minimum number of output glyphs to produce.
16426 If STRING has fewer characters than FIELD_WIDTH, pad to the right
16427 with spaces. FIELD_WIDTH <= 0 means don't pad.
16428
16429 PRECISION is the maximum number of characters to output from
16430 STRING. PRECISION <= 0 means don't truncate the string.
16431
16432 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
16433 properties to the string.
16434
16435 PROPS are the properties to add to the string.
16436 The mode_line_string_face face property is always added to the string.
16437 */
16438
16439 static int
16440 store_mode_line_string (string, lisp_string, copy_string, field_width, precision, props)
16441 char *string;
16442 Lisp_Object lisp_string;
16443 int copy_string;
16444 int field_width;
16445 int precision;
16446 Lisp_Object props;
16447 {
16448 int len;
16449 int n = 0;
16450
16451 if (string != NULL)
16452 {
16453 len = strlen (string);
16454 if (precision > 0 && len > precision)
16455 len = precision;
16456 lisp_string = make_string (string, len);
16457 if (NILP (props))
16458 props = mode_line_string_face_prop;
16459 else if (!NILP (mode_line_string_face))
16460 {
16461 Lisp_Object face = Fplist_get (props, Qface);
16462 props = Fcopy_sequence (props);
16463 if (NILP (face))
16464 face = mode_line_string_face;
16465 else
16466 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
16467 props = Fplist_put (props, Qface, face);
16468 }
16469 Fadd_text_properties (make_number (0), make_number (len),
16470 props, lisp_string);
16471 }
16472 else
16473 {
16474 len = XFASTINT (Flength (lisp_string));
16475 if (precision > 0 && len > precision)
16476 {
16477 len = precision;
16478 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
16479 precision = -1;
16480 }
16481 if (!NILP (mode_line_string_face))
16482 {
16483 Lisp_Object face;
16484 if (NILP (props))
16485 props = Ftext_properties_at (make_number (0), lisp_string);
16486 face = Fplist_get (props, Qface);
16487 if (NILP (face))
16488 face = mode_line_string_face;
16489 else
16490 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
16491 props = Fcons (Qface, Fcons (face, Qnil));
16492 if (copy_string)
16493 lisp_string = Fcopy_sequence (lisp_string);
16494 }
16495 if (!NILP (props))
16496 Fadd_text_properties (make_number (0), make_number (len),
16497 props, lisp_string);
16498 }
16499
16500 if (len > 0)
16501 {
16502 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
16503 n += len;
16504 }
16505
16506 if (field_width > len)
16507 {
16508 field_width -= len;
16509 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
16510 if (!NILP (props))
16511 Fadd_text_properties (make_number (0), make_number (field_width),
16512 props, lisp_string);
16513 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
16514 n += field_width;
16515 }
16516
16517 return n;
16518 }
16519
16520
16521 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
16522 1, 4, 0,
16523 doc: /* Format a string out of a mode line format specification.
16524 First arg FORMAT specifies the mode line format (see `mode-line-format'
16525 for details) to use.
16526
16527 Optional second arg FACE specifies the face property to put
16528 on all characters for which no face is specified.
16529 t means whatever face the window's mode line currently uses
16530 \(either `mode-line' or `mode-line-inactive', depending).
16531 nil means the default is no face property.
16532 If FACE is an integer, the value string has no text properties.
16533
16534 Optional third and fourth args WINDOW and BUFFER specify the window
16535 and buffer to use as the context for the formatting (defaults
16536 are the selected window and the window's buffer). */)
16537 (format, face, window, buffer)
16538 Lisp_Object format, face, window, buffer;
16539 {
16540 struct it it;
16541 int len;
16542 struct window *w;
16543 struct buffer *old_buffer = NULL;
16544 int face_id = -1;
16545 int no_props = INTEGERP (face);
16546 int count = SPECPDL_INDEX ();
16547 Lisp_Object str;
16548 int string_start = 0;
16549
16550 if (NILP (window))
16551 window = selected_window;
16552 CHECK_WINDOW (window);
16553 w = XWINDOW (window);
16554
16555 if (NILP (buffer))
16556 buffer = w->buffer;
16557 CHECK_BUFFER (buffer);
16558
16559 if (NILP (format))
16560 return build_string ("");
16561
16562 if (no_props)
16563 face = Qnil;
16564
16565 if (!NILP (face))
16566 {
16567 if (EQ (face, Qt))
16568 face = (EQ (window, selected_window) ? Qmode_line : Qmode_line_inactive);
16569 face_id = lookup_named_face (XFRAME (WINDOW_FRAME (w)), face, 0);
16570 }
16571
16572 if (face_id < 0)
16573 face_id = DEFAULT_FACE_ID;
16574
16575 if (XBUFFER (buffer) != current_buffer)
16576 old_buffer = current_buffer;
16577
16578 record_unwind_protect (unwind_format_mode_line,
16579 format_mode_line_unwind_data (old_buffer));
16580
16581 if (old_buffer)
16582 set_buffer_internal_1 (XBUFFER (buffer));
16583
16584 init_iterator (&it, w, -1, -1, NULL, face_id);
16585
16586 if (no_props)
16587 {
16588 mode_line_target = MODE_LINE_NOPROP;
16589 mode_line_string_face_prop = Qnil;
16590 mode_line_string_list = Qnil;
16591 string_start = MODE_LINE_NOPROP_LEN (0);
16592 }
16593 else
16594 {
16595 mode_line_target = MODE_LINE_STRING;
16596 mode_line_string_list = Qnil;
16597 mode_line_string_face = face;
16598 mode_line_string_face_prop
16599 = (NILP (face) ? Qnil : Fcons (Qface, Fcons (face, Qnil)));
16600 }
16601
16602 push_frame_kboard (it.f);
16603 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
16604 pop_frame_kboard ();
16605
16606 if (no_props)
16607 {
16608 len = MODE_LINE_NOPROP_LEN (string_start);
16609 str = make_string (mode_line_noprop_buf + string_start, len);
16610 }
16611 else
16612 {
16613 mode_line_string_list = Fnreverse (mode_line_string_list);
16614 str = Fmapconcat (intern ("identity"), mode_line_string_list,
16615 make_string ("", 0));
16616 }
16617
16618 unbind_to (count, Qnil);
16619 return str;
16620 }
16621
16622 /* Write a null-terminated, right justified decimal representation of
16623 the positive integer D to BUF using a minimal field width WIDTH. */
16624
16625 static void
16626 pint2str (buf, width, d)
16627 register char *buf;
16628 register int width;
16629 register int d;
16630 {
16631 register char *p = buf;
16632
16633 if (d <= 0)
16634 *p++ = '0';
16635 else
16636 {
16637 while (d > 0)
16638 {
16639 *p++ = d % 10 + '0';
16640 d /= 10;
16641 }
16642 }
16643
16644 for (width -= (int) (p - buf); width > 0; --width)
16645 *p++ = ' ';
16646 *p-- = '\0';
16647 while (p > buf)
16648 {
16649 d = *buf;
16650 *buf++ = *p;
16651 *p-- = d;
16652 }
16653 }
16654
16655 /* Write a null-terminated, right justified decimal and "human
16656 readable" representation of the nonnegative integer D to BUF using
16657 a minimal field width WIDTH. D should be smaller than 999.5e24. */
16658
16659 static const char power_letter[] =
16660 {
16661 0, /* not used */
16662 'k', /* kilo */
16663 'M', /* mega */
16664 'G', /* giga */
16665 'T', /* tera */
16666 'P', /* peta */
16667 'E', /* exa */
16668 'Z', /* zetta */
16669 'Y' /* yotta */
16670 };
16671
16672 static void
16673 pint2hrstr (buf, width, d)
16674 char *buf;
16675 int width;
16676 int d;
16677 {
16678 /* We aim to represent the nonnegative integer D as
16679 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
16680 int quotient = d;
16681 int remainder = 0;
16682 /* -1 means: do not use TENTHS. */
16683 int tenths = -1;
16684 int exponent = 0;
16685
16686 /* Length of QUOTIENT.TENTHS as a string. */
16687 int length;
16688
16689 char * psuffix;
16690 char * p;
16691
16692 if (1000 <= quotient)
16693 {
16694 /* Scale to the appropriate EXPONENT. */
16695 do
16696 {
16697 remainder = quotient % 1000;
16698 quotient /= 1000;
16699 exponent++;
16700 }
16701 while (1000 <= quotient);
16702
16703 /* Round to nearest and decide whether to use TENTHS or not. */
16704 if (quotient <= 9)
16705 {
16706 tenths = remainder / 100;
16707 if (50 <= remainder % 100)
16708 {
16709 if (tenths < 9)
16710 tenths++;
16711 else
16712 {
16713 quotient++;
16714 if (quotient == 10)
16715 tenths = -1;
16716 else
16717 tenths = 0;
16718 }
16719 }
16720 }
16721 else
16722 if (500 <= remainder)
16723 {
16724 if (quotient < 999)
16725 quotient++;
16726 else
16727 {
16728 quotient = 1;
16729 exponent++;
16730 tenths = 0;
16731 }
16732 }
16733 }
16734
16735 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
16736 if (tenths == -1 && quotient <= 99)
16737 if (quotient <= 9)
16738 length = 1;
16739 else
16740 length = 2;
16741 else
16742 length = 3;
16743 p = psuffix = buf + max (width, length);
16744
16745 /* Print EXPONENT. */
16746 if (exponent)
16747 *psuffix++ = power_letter[exponent];
16748 *psuffix = '\0';
16749
16750 /* Print TENTHS. */
16751 if (tenths >= 0)
16752 {
16753 *--p = '0' + tenths;
16754 *--p = '.';
16755 }
16756
16757 /* Print QUOTIENT. */
16758 do
16759 {
16760 int digit = quotient % 10;
16761 *--p = '0' + digit;
16762 }
16763 while ((quotient /= 10) != 0);
16764
16765 /* Print leading spaces. */
16766 while (buf < p)
16767 *--p = ' ';
16768 }
16769
16770 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
16771 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
16772 type of CODING_SYSTEM. Return updated pointer into BUF. */
16773
16774 static unsigned char invalid_eol_type[] = "(*invalid*)";
16775
16776 static char *
16777 decode_mode_spec_coding (coding_system, buf, eol_flag)
16778 Lisp_Object coding_system;
16779 register char *buf;
16780 int eol_flag;
16781 {
16782 Lisp_Object val;
16783 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
16784 const unsigned char *eol_str;
16785 int eol_str_len;
16786 /* The EOL conversion we are using. */
16787 Lisp_Object eoltype;
16788
16789 val = CODING_SYSTEM_SPEC (coding_system);
16790 eoltype = Qnil;
16791
16792 if (!VECTORP (val)) /* Not yet decided. */
16793 {
16794 if (multibyte)
16795 *buf++ = '-';
16796 if (eol_flag)
16797 eoltype = eol_mnemonic_undecided;
16798 /* Don't mention EOL conversion if it isn't decided. */
16799 }
16800 else
16801 {
16802 Lisp_Object attrs;
16803 Lisp_Object eolvalue;
16804
16805 attrs = AREF (val, 0);
16806 eolvalue = AREF (val, 2);
16807
16808 if (multibyte)
16809 *buf++ = XFASTINT (CODING_ATTR_MNEMONIC (attrs));
16810
16811 if (eol_flag)
16812 {
16813 /* The EOL conversion that is normal on this system. */
16814
16815 if (NILP (eolvalue)) /* Not yet decided. */
16816 eoltype = eol_mnemonic_undecided;
16817 else if (VECTORP (eolvalue)) /* Not yet decided. */
16818 eoltype = eol_mnemonic_undecided;
16819 else /* eolvalue is Qunix, Qdos, or Qmac. */
16820 eoltype = (EQ (eolvalue, Qunix)
16821 ? eol_mnemonic_unix
16822 : (EQ (eolvalue, Qdos) == 1
16823 ? eol_mnemonic_dos : eol_mnemonic_mac));
16824 }
16825 }
16826
16827 if (eol_flag)
16828 {
16829 /* Mention the EOL conversion if it is not the usual one. */
16830 if (STRINGP (eoltype))
16831 {
16832 eol_str = SDATA (eoltype);
16833 eol_str_len = SBYTES (eoltype);
16834 }
16835 else if (CHARACTERP (eoltype))
16836 {
16837 unsigned char *tmp = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
16838 eol_str_len = CHAR_STRING (XINT (eoltype), tmp);
16839 eol_str = tmp;
16840 }
16841 else
16842 {
16843 eol_str = invalid_eol_type;
16844 eol_str_len = sizeof (invalid_eol_type) - 1;
16845 }
16846 bcopy (eol_str, buf, eol_str_len);
16847 buf += eol_str_len;
16848 }
16849
16850 return buf;
16851 }
16852
16853 /* Return a string for the output of a mode line %-spec for window W,
16854 generated by character C. PRECISION >= 0 means don't return a
16855 string longer than that value. FIELD_WIDTH > 0 means pad the
16856 string returned with spaces to that value. Return 1 in *MULTIBYTE
16857 if the result is multibyte text.
16858
16859 Note we operate on the current buffer for most purposes,
16860 the exception being w->base_line_pos. */
16861
16862 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
16863
16864 static char *
16865 decode_mode_spec (w, c, field_width, precision, multibyte)
16866 struct window *w;
16867 register int c;
16868 int field_width, precision;
16869 int *multibyte;
16870 {
16871 Lisp_Object obj;
16872 struct frame *f = XFRAME (WINDOW_FRAME (w));
16873 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
16874 struct buffer *b = current_buffer;
16875
16876 obj = Qnil;
16877 *multibyte = 0;
16878
16879 switch (c)
16880 {
16881 case '*':
16882 if (!NILP (b->read_only))
16883 return "%";
16884 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
16885 return "*";
16886 return "-";
16887
16888 case '+':
16889 /* This differs from %* only for a modified read-only buffer. */
16890 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
16891 return "*";
16892 if (!NILP (b->read_only))
16893 return "%";
16894 return "-";
16895
16896 case '&':
16897 /* This differs from %* in ignoring read-only-ness. */
16898 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
16899 return "*";
16900 return "-";
16901
16902 case '%':
16903 return "%";
16904
16905 case '[':
16906 {
16907 int i;
16908 char *p;
16909
16910 if (command_loop_level > 5)
16911 return "[[[... ";
16912 p = decode_mode_spec_buf;
16913 for (i = 0; i < command_loop_level; i++)
16914 *p++ = '[';
16915 *p = 0;
16916 return decode_mode_spec_buf;
16917 }
16918
16919 case ']':
16920 {
16921 int i;
16922 char *p;
16923
16924 if (command_loop_level > 5)
16925 return " ...]]]";
16926 p = decode_mode_spec_buf;
16927 for (i = 0; i < command_loop_level; i++)
16928 *p++ = ']';
16929 *p = 0;
16930 return decode_mode_spec_buf;
16931 }
16932
16933 case '-':
16934 {
16935 register int i;
16936
16937 /* Let lots_of_dashes be a string of infinite length. */
16938 if (mode_line_target == MODE_LINE_NOPROP ||
16939 mode_line_target == MODE_LINE_STRING)
16940 return "--";
16941 if (field_width <= 0
16942 || field_width > sizeof (lots_of_dashes))
16943 {
16944 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
16945 decode_mode_spec_buf[i] = '-';
16946 decode_mode_spec_buf[i] = '\0';
16947 return decode_mode_spec_buf;
16948 }
16949 else
16950 return lots_of_dashes;
16951 }
16952
16953 case 'b':
16954 obj = b->name;
16955 break;
16956
16957 case 'c':
16958 {
16959 int col = (int) current_column (); /* iftc */
16960 w->column_number_displayed = make_number (col);
16961 pint2str (decode_mode_spec_buf, field_width, col);
16962 return decode_mode_spec_buf;
16963 }
16964
16965 case 'F':
16966 /* %F displays the frame name. */
16967 if (!NILP (f->title))
16968 return (char *) SDATA (f->title);
16969 if (f->explicit_name || ! FRAME_WINDOW_P (f))
16970 return (char *) SDATA (f->name);
16971 return "Emacs";
16972
16973 case 'f':
16974 obj = b->filename;
16975 break;
16976
16977 case 'i':
16978 {
16979 int size = ZV - BEGV;
16980 pint2str (decode_mode_spec_buf, field_width, size);
16981 return decode_mode_spec_buf;
16982 }
16983
16984 case 'I':
16985 {
16986 int size = ZV - BEGV;
16987 pint2hrstr (decode_mode_spec_buf, field_width, size);
16988 return decode_mode_spec_buf;
16989 }
16990
16991 case 'l':
16992 {
16993 int startpos = XMARKER (w->start)->charpos;
16994 int startpos_byte = marker_byte_position (w->start);
16995 int line, linepos, linepos_byte, topline;
16996 int nlines, junk;
16997 int height = WINDOW_TOTAL_LINES (w);
16998
16999 /* If we decided that this buffer isn't suitable for line numbers,
17000 don't forget that too fast. */
17001 if (EQ (w->base_line_pos, w->buffer))
17002 goto no_value;
17003 /* But do forget it, if the window shows a different buffer now. */
17004 else if (BUFFERP (w->base_line_pos))
17005 w->base_line_pos = Qnil;
17006
17007 /* If the buffer is very big, don't waste time. */
17008 if (INTEGERP (Vline_number_display_limit)
17009 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
17010 {
17011 w->base_line_pos = Qnil;
17012 w->base_line_number = Qnil;
17013 goto no_value;
17014 }
17015
17016 if (!NILP (w->base_line_number)
17017 && !NILP (w->base_line_pos)
17018 && XFASTINT (w->base_line_pos) <= startpos)
17019 {
17020 line = XFASTINT (w->base_line_number);
17021 linepos = XFASTINT (w->base_line_pos);
17022 linepos_byte = buf_charpos_to_bytepos (b, linepos);
17023 }
17024 else
17025 {
17026 line = 1;
17027 linepos = BUF_BEGV (b);
17028 linepos_byte = BUF_BEGV_BYTE (b);
17029 }
17030
17031 /* Count lines from base line to window start position. */
17032 nlines = display_count_lines (linepos, linepos_byte,
17033 startpos_byte,
17034 startpos, &junk);
17035
17036 topline = nlines + line;
17037
17038 /* Determine a new base line, if the old one is too close
17039 or too far away, or if we did not have one.
17040 "Too close" means it's plausible a scroll-down would
17041 go back past it. */
17042 if (startpos == BUF_BEGV (b))
17043 {
17044 w->base_line_number = make_number (topline);
17045 w->base_line_pos = make_number (BUF_BEGV (b));
17046 }
17047 else if (nlines < height + 25 || nlines > height * 3 + 50
17048 || linepos == BUF_BEGV (b))
17049 {
17050 int limit = BUF_BEGV (b);
17051 int limit_byte = BUF_BEGV_BYTE (b);
17052 int position;
17053 int distance = (height * 2 + 30) * line_number_display_limit_width;
17054
17055 if (startpos - distance > limit)
17056 {
17057 limit = startpos - distance;
17058 limit_byte = CHAR_TO_BYTE (limit);
17059 }
17060
17061 nlines = display_count_lines (startpos, startpos_byte,
17062 limit_byte,
17063 - (height * 2 + 30),
17064 &position);
17065 /* If we couldn't find the lines we wanted within
17066 line_number_display_limit_width chars per line,
17067 give up on line numbers for this window. */
17068 if (position == limit_byte && limit == startpos - distance)
17069 {
17070 w->base_line_pos = w->buffer;
17071 w->base_line_number = Qnil;
17072 goto no_value;
17073 }
17074
17075 w->base_line_number = make_number (topline - nlines);
17076 w->base_line_pos = make_number (BYTE_TO_CHAR (position));
17077 }
17078
17079 /* Now count lines from the start pos to point. */
17080 nlines = display_count_lines (startpos, startpos_byte,
17081 PT_BYTE, PT, &junk);
17082
17083 /* Record that we did display the line number. */
17084 line_number_displayed = 1;
17085
17086 /* Make the string to show. */
17087 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
17088 return decode_mode_spec_buf;
17089 no_value:
17090 {
17091 char* p = decode_mode_spec_buf;
17092 int pad = field_width - 2;
17093 while (pad-- > 0)
17094 *p++ = ' ';
17095 *p++ = '?';
17096 *p++ = '?';
17097 *p = '\0';
17098 return decode_mode_spec_buf;
17099 }
17100 }
17101 break;
17102
17103 case 'm':
17104 obj = b->mode_name;
17105 break;
17106
17107 case 'n':
17108 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
17109 return " Narrow";
17110 break;
17111
17112 case 'p':
17113 {
17114 int pos = marker_position (w->start);
17115 int total = BUF_ZV (b) - BUF_BEGV (b);
17116
17117 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
17118 {
17119 if (pos <= BUF_BEGV (b))
17120 return "All";
17121 else
17122 return "Bottom";
17123 }
17124 else if (pos <= BUF_BEGV (b))
17125 return "Top";
17126 else
17127 {
17128 if (total > 1000000)
17129 /* Do it differently for a large value, to avoid overflow. */
17130 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
17131 else
17132 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
17133 /* We can't normally display a 3-digit number,
17134 so get us a 2-digit number that is close. */
17135 if (total == 100)
17136 total = 99;
17137 sprintf (decode_mode_spec_buf, "%2d%%", total);
17138 return decode_mode_spec_buf;
17139 }
17140 }
17141
17142 /* Display percentage of size above the bottom of the screen. */
17143 case 'P':
17144 {
17145 int toppos = marker_position (w->start);
17146 int botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
17147 int total = BUF_ZV (b) - BUF_BEGV (b);
17148
17149 if (botpos >= BUF_ZV (b))
17150 {
17151 if (toppos <= BUF_BEGV (b))
17152 return "All";
17153 else
17154 return "Bottom";
17155 }
17156 else
17157 {
17158 if (total > 1000000)
17159 /* Do it differently for a large value, to avoid overflow. */
17160 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
17161 else
17162 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
17163 /* We can't normally display a 3-digit number,
17164 so get us a 2-digit number that is close. */
17165 if (total == 100)
17166 total = 99;
17167 if (toppos <= BUF_BEGV (b))
17168 sprintf (decode_mode_spec_buf, "Top%2d%%", total);
17169 else
17170 sprintf (decode_mode_spec_buf, "%2d%%", total);
17171 return decode_mode_spec_buf;
17172 }
17173 }
17174
17175 case 's':
17176 /* status of process */
17177 obj = Fget_buffer_process (Fcurrent_buffer ());
17178 if (NILP (obj))
17179 return "no process";
17180 #ifdef subprocesses
17181 obj = Fsymbol_name (Fprocess_status (obj));
17182 #endif
17183 break;
17184
17185 case 't': /* indicate TEXT or BINARY */
17186 #ifdef MODE_LINE_BINARY_TEXT
17187 return MODE_LINE_BINARY_TEXT (b);
17188 #else
17189 return "T";
17190 #endif
17191
17192 case 'z':
17193 /* coding-system (not including end-of-line format) */
17194 case 'Z':
17195 /* coding-system (including end-of-line type) */
17196 {
17197 int eol_flag = (c == 'Z');
17198 char *p = decode_mode_spec_buf;
17199
17200 if (! FRAME_WINDOW_P (f))
17201 {
17202 /* No need to mention EOL here--the terminal never needs
17203 to do EOL conversion. */
17204 p = decode_mode_spec_coding (CODING_ID_NAME (keyboard_coding.id),
17205 p, 0);
17206 p = decode_mode_spec_coding (CODING_ID_NAME (terminal_coding.id),
17207 p, 0);
17208 }
17209 p = decode_mode_spec_coding (b->buffer_file_coding_system,
17210 p, eol_flag);
17211
17212 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
17213 #ifdef subprocesses
17214 obj = Fget_buffer_process (Fcurrent_buffer ());
17215 if (PROCESSP (obj))
17216 {
17217 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
17218 p, eol_flag);
17219 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
17220 p, eol_flag);
17221 }
17222 #endif /* subprocesses */
17223 #endif /* 0 */
17224 *p = 0;
17225 return decode_mode_spec_buf;
17226 }
17227 }
17228
17229 if (STRINGP (obj))
17230 {
17231 *multibyte = STRING_MULTIBYTE (obj);
17232 return (char *) SDATA (obj);
17233 }
17234 else
17235 return "";
17236 }
17237
17238
17239 /* Count up to COUNT lines starting from START / START_BYTE.
17240 But don't go beyond LIMIT_BYTE.
17241 Return the number of lines thus found (always nonnegative).
17242
17243 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
17244
17245 static int
17246 display_count_lines (start, start_byte, limit_byte, count, byte_pos_ptr)
17247 int start, start_byte, limit_byte, count;
17248 int *byte_pos_ptr;
17249 {
17250 register unsigned char *cursor;
17251 unsigned char *base;
17252
17253 register int ceiling;
17254 register unsigned char *ceiling_addr;
17255 int orig_count = count;
17256
17257 /* If we are not in selective display mode,
17258 check only for newlines. */
17259 int selective_display = (!NILP (current_buffer->selective_display)
17260 && !INTEGERP (current_buffer->selective_display));
17261
17262 if (count > 0)
17263 {
17264 while (start_byte < limit_byte)
17265 {
17266 ceiling = BUFFER_CEILING_OF (start_byte);
17267 ceiling = min (limit_byte - 1, ceiling);
17268 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
17269 base = (cursor = BYTE_POS_ADDR (start_byte));
17270 while (1)
17271 {
17272 if (selective_display)
17273 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
17274 ;
17275 else
17276 while (*cursor != '\n' && ++cursor != ceiling_addr)
17277 ;
17278
17279 if (cursor != ceiling_addr)
17280 {
17281 if (--count == 0)
17282 {
17283 start_byte += cursor - base + 1;
17284 *byte_pos_ptr = start_byte;
17285 return orig_count;
17286 }
17287 else
17288 if (++cursor == ceiling_addr)
17289 break;
17290 }
17291 else
17292 break;
17293 }
17294 start_byte += cursor - base;
17295 }
17296 }
17297 else
17298 {
17299 while (start_byte > limit_byte)
17300 {
17301 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
17302 ceiling = max (limit_byte, ceiling);
17303 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
17304 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
17305 while (1)
17306 {
17307 if (selective_display)
17308 while (--cursor != ceiling_addr
17309 && *cursor != '\n' && *cursor != 015)
17310 ;
17311 else
17312 while (--cursor != ceiling_addr && *cursor != '\n')
17313 ;
17314
17315 if (cursor != ceiling_addr)
17316 {
17317 if (++count == 0)
17318 {
17319 start_byte += cursor - base + 1;
17320 *byte_pos_ptr = start_byte;
17321 /* When scanning backwards, we should
17322 not count the newline posterior to which we stop. */
17323 return - orig_count - 1;
17324 }
17325 }
17326 else
17327 break;
17328 }
17329 /* Here we add 1 to compensate for the last decrement
17330 of CURSOR, which took it past the valid range. */
17331 start_byte += cursor - base + 1;
17332 }
17333 }
17334
17335 *byte_pos_ptr = limit_byte;
17336
17337 if (count < 0)
17338 return - orig_count + count;
17339 return orig_count - count;
17340
17341 }
17342
17343
17344 \f
17345 /***********************************************************************
17346 Displaying strings
17347 ***********************************************************************/
17348
17349 /* Display a NUL-terminated string, starting with index START.
17350
17351 If STRING is non-null, display that C string. Otherwise, the Lisp
17352 string LISP_STRING is displayed.
17353
17354 If FACE_STRING is not nil, FACE_STRING_POS is a position in
17355 FACE_STRING. Display STRING or LISP_STRING with the face at
17356 FACE_STRING_POS in FACE_STRING:
17357
17358 Display the string in the environment given by IT, but use the
17359 standard display table, temporarily.
17360
17361 FIELD_WIDTH is the minimum number of output glyphs to produce.
17362 If STRING has fewer characters than FIELD_WIDTH, pad to the right
17363 with spaces. If STRING has more characters, more than FIELD_WIDTH
17364 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
17365
17366 PRECISION is the maximum number of characters to output from
17367 STRING. PRECISION < 0 means don't truncate the string.
17368
17369 This is roughly equivalent to printf format specifiers:
17370
17371 FIELD_WIDTH PRECISION PRINTF
17372 ----------------------------------------
17373 -1 -1 %s
17374 -1 10 %.10s
17375 10 -1 %10s
17376 20 10 %20.10s
17377
17378 MULTIBYTE zero means do not display multibyte chars, > 0 means do
17379 display them, and < 0 means obey the current buffer's value of
17380 enable_multibyte_characters.
17381
17382 Value is the number of glyphs produced. */
17383
17384 static int
17385 display_string (string, lisp_string, face_string, face_string_pos,
17386 start, it, field_width, precision, max_x, multibyte)
17387 unsigned char *string;
17388 Lisp_Object lisp_string;
17389 Lisp_Object face_string;
17390 int face_string_pos;
17391 int start;
17392 struct it *it;
17393 int field_width, precision, max_x;
17394 int multibyte;
17395 {
17396 int hpos_at_start = it->hpos;
17397 int saved_face_id = it->face_id;
17398 struct glyph_row *row = it->glyph_row;
17399
17400 /* Initialize the iterator IT for iteration over STRING beginning
17401 with index START. */
17402 reseat_to_string (it, string, lisp_string, start,
17403 precision, field_width, multibyte);
17404
17405 /* If displaying STRING, set up the face of the iterator
17406 from LISP_STRING, if that's given. */
17407 if (STRINGP (face_string))
17408 {
17409 int endptr;
17410 struct face *face;
17411
17412 it->face_id
17413 = face_at_string_position (it->w, face_string, face_string_pos,
17414 0, it->region_beg_charpos,
17415 it->region_end_charpos,
17416 &endptr, it->base_face_id, 0);
17417 face = FACE_FROM_ID (it->f, it->face_id);
17418 it->face_box_p = face->box != FACE_NO_BOX;
17419 }
17420
17421 /* Set max_x to the maximum allowed X position. Don't let it go
17422 beyond the right edge of the window. */
17423 if (max_x <= 0)
17424 max_x = it->last_visible_x;
17425 else
17426 max_x = min (max_x, it->last_visible_x);
17427
17428 /* Skip over display elements that are not visible. because IT->w is
17429 hscrolled. */
17430 if (it->current_x < it->first_visible_x)
17431 move_it_in_display_line_to (it, 100000, it->first_visible_x,
17432 MOVE_TO_POS | MOVE_TO_X);
17433
17434 row->ascent = it->max_ascent;
17435 row->height = it->max_ascent + it->max_descent;
17436 row->phys_ascent = it->max_phys_ascent;
17437 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
17438 row->extra_line_spacing = it->max_extra_line_spacing;
17439
17440 /* This condition is for the case that we are called with current_x
17441 past last_visible_x. */
17442 while (it->current_x < max_x)
17443 {
17444 int x_before, x, n_glyphs_before, i, nglyphs;
17445
17446 /* Get the next display element. */
17447 if (!get_next_display_element (it))
17448 break;
17449
17450 /* Produce glyphs. */
17451 x_before = it->current_x;
17452 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
17453 PRODUCE_GLYPHS (it);
17454
17455 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
17456 i = 0;
17457 x = x_before;
17458 while (i < nglyphs)
17459 {
17460 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
17461
17462 if (!it->truncate_lines_p
17463 && x + glyph->pixel_width > max_x)
17464 {
17465 /* End of continued line or max_x reached. */
17466 if (CHAR_GLYPH_PADDING_P (*glyph))
17467 {
17468 /* A wide character is unbreakable. */
17469 it->glyph_row->used[TEXT_AREA] = n_glyphs_before;
17470 it->current_x = x_before;
17471 }
17472 else
17473 {
17474 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
17475 it->current_x = x;
17476 }
17477 break;
17478 }
17479 else if (x + glyph->pixel_width >= it->first_visible_x)
17480 {
17481 /* Glyph is at least partially visible. */
17482 ++it->hpos;
17483 if (x < it->first_visible_x)
17484 it->glyph_row->x = x - it->first_visible_x;
17485 }
17486 else
17487 {
17488 /* Glyph is off the left margin of the display area.
17489 Should not happen. */
17490 abort ();
17491 }
17492
17493 row->ascent = max (row->ascent, it->max_ascent);
17494 row->height = max (row->height, it->max_ascent + it->max_descent);
17495 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
17496 row->phys_height = max (row->phys_height,
17497 it->max_phys_ascent + it->max_phys_descent);
17498 row->extra_line_spacing = max (row->extra_line_spacing,
17499 it->max_extra_line_spacing);
17500 x += glyph->pixel_width;
17501 ++i;
17502 }
17503
17504 /* Stop if max_x reached. */
17505 if (i < nglyphs)
17506 break;
17507
17508 /* Stop at line ends. */
17509 if (ITERATOR_AT_END_OF_LINE_P (it))
17510 {
17511 it->continuation_lines_width = 0;
17512 break;
17513 }
17514
17515 set_iterator_to_next (it, 1);
17516
17517 /* Stop if truncating at the right edge. */
17518 if (it->truncate_lines_p
17519 && it->current_x >= it->last_visible_x)
17520 {
17521 /* Add truncation mark, but don't do it if the line is
17522 truncated at a padding space. */
17523 if (IT_CHARPOS (*it) < it->string_nchars)
17524 {
17525 if (!FRAME_WINDOW_P (it->f))
17526 {
17527 int i, n;
17528
17529 if (it->current_x > it->last_visible_x)
17530 {
17531 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
17532 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
17533 break;
17534 for (n = row->used[TEXT_AREA]; i < n; ++i)
17535 {
17536 row->used[TEXT_AREA] = i;
17537 produce_special_glyphs (it, IT_TRUNCATION);
17538 }
17539 }
17540 produce_special_glyphs (it, IT_TRUNCATION);
17541 }
17542 it->glyph_row->truncated_on_right_p = 1;
17543 }
17544 break;
17545 }
17546 }
17547
17548 /* Maybe insert a truncation at the left. */
17549 if (it->first_visible_x
17550 && IT_CHARPOS (*it) > 0)
17551 {
17552 if (!FRAME_WINDOW_P (it->f))
17553 insert_left_trunc_glyphs (it);
17554 it->glyph_row->truncated_on_left_p = 1;
17555 }
17556
17557 it->face_id = saved_face_id;
17558
17559 /* Value is number of columns displayed. */
17560 return it->hpos - hpos_at_start;
17561 }
17562
17563
17564 \f
17565 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
17566 appears as an element of LIST or as the car of an element of LIST.
17567 If PROPVAL is a list, compare each element against LIST in that
17568 way, and return 1/2 if any element of PROPVAL is found in LIST.
17569 Otherwise return 0. This function cannot quit.
17570 The return value is 2 if the text is invisible but with an ellipsis
17571 and 1 if it's invisible and without an ellipsis. */
17572
17573 int
17574 invisible_p (propval, list)
17575 register Lisp_Object propval;
17576 Lisp_Object list;
17577 {
17578 register Lisp_Object tail, proptail;
17579
17580 for (tail = list; CONSP (tail); tail = XCDR (tail))
17581 {
17582 register Lisp_Object tem;
17583 tem = XCAR (tail);
17584 if (EQ (propval, tem))
17585 return 1;
17586 if (CONSP (tem) && EQ (propval, XCAR (tem)))
17587 return NILP (XCDR (tem)) ? 1 : 2;
17588 }
17589
17590 if (CONSP (propval))
17591 {
17592 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
17593 {
17594 Lisp_Object propelt;
17595 propelt = XCAR (proptail);
17596 for (tail = list; CONSP (tail); tail = XCDR (tail))
17597 {
17598 register Lisp_Object tem;
17599 tem = XCAR (tail);
17600 if (EQ (propelt, tem))
17601 return 1;
17602 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
17603 return NILP (XCDR (tem)) ? 1 : 2;
17604 }
17605 }
17606 }
17607
17608 return 0;
17609 }
17610
17611 /* Calculate a width or height in pixels from a specification using
17612 the following elements:
17613
17614 SPEC ::=
17615 NUM - a (fractional) multiple of the default font width/height
17616 (NUM) - specifies exactly NUM pixels
17617 UNIT - a fixed number of pixels, see below.
17618 ELEMENT - size of a display element in pixels, see below.
17619 (NUM . SPEC) - equals NUM * SPEC
17620 (+ SPEC SPEC ...) - add pixel values
17621 (- SPEC SPEC ...) - subtract pixel values
17622 (- SPEC) - negate pixel value
17623
17624 NUM ::=
17625 INT or FLOAT - a number constant
17626 SYMBOL - use symbol's (buffer local) variable binding.
17627
17628 UNIT ::=
17629 in - pixels per inch *)
17630 mm - pixels per 1/1000 meter *)
17631 cm - pixels per 1/100 meter *)
17632 width - width of current font in pixels.
17633 height - height of current font in pixels.
17634
17635 *) using the ratio(s) defined in display-pixels-per-inch.
17636
17637 ELEMENT ::=
17638
17639 left-fringe - left fringe width in pixels
17640 right-fringe - right fringe width in pixels
17641
17642 left-margin - left margin width in pixels
17643 right-margin - right margin width in pixels
17644
17645 scroll-bar - scroll-bar area width in pixels
17646
17647 Examples:
17648
17649 Pixels corresponding to 5 inches:
17650 (5 . in)
17651
17652 Total width of non-text areas on left side of window (if scroll-bar is on left):
17653 '(space :width (+ left-fringe left-margin scroll-bar))
17654
17655 Align to first text column (in header line):
17656 '(space :align-to 0)
17657
17658 Align to middle of text area minus half the width of variable `my-image'
17659 containing a loaded image:
17660 '(space :align-to (0.5 . (- text my-image)))
17661
17662 Width of left margin minus width of 1 character in the default font:
17663 '(space :width (- left-margin 1))
17664
17665 Width of left margin minus width of 2 characters in the current font:
17666 '(space :width (- left-margin (2 . width)))
17667
17668 Center 1 character over left-margin (in header line):
17669 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
17670
17671 Different ways to express width of left fringe plus left margin minus one pixel:
17672 '(space :width (- (+ left-fringe left-margin) (1)))
17673 '(space :width (+ left-fringe left-margin (- (1))))
17674 '(space :width (+ left-fringe left-margin (-1)))
17675
17676 */
17677
17678 #define NUMVAL(X) \
17679 ((INTEGERP (X) || FLOATP (X)) \
17680 ? XFLOATINT (X) \
17681 : - 1)
17682
17683 int
17684 calc_pixel_width_or_height (res, it, prop, font, width_p, align_to)
17685 double *res;
17686 struct it *it;
17687 Lisp_Object prop;
17688 void *font;
17689 int width_p, *align_to;
17690 {
17691 double pixels;
17692
17693 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
17694 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
17695
17696 if (NILP (prop))
17697 return OK_PIXELS (0);
17698
17699 if (SYMBOLP (prop))
17700 {
17701 if (SCHARS (SYMBOL_NAME (prop)) == 2)
17702 {
17703 char *unit = SDATA (SYMBOL_NAME (prop));
17704
17705 if (unit[0] == 'i' && unit[1] == 'n')
17706 pixels = 1.0;
17707 else if (unit[0] == 'm' && unit[1] == 'm')
17708 pixels = 25.4;
17709 else if (unit[0] == 'c' && unit[1] == 'm')
17710 pixels = 2.54;
17711 else
17712 pixels = 0;
17713 if (pixels > 0)
17714 {
17715 double ppi;
17716 if ((ppi = NUMVAL (Vdisplay_pixels_per_inch), ppi > 0)
17717 || (CONSP (Vdisplay_pixels_per_inch)
17718 && (ppi = (width_p
17719 ? NUMVAL (XCAR (Vdisplay_pixels_per_inch))
17720 : NUMVAL (XCDR (Vdisplay_pixels_per_inch))),
17721 ppi > 0)))
17722 return OK_PIXELS (ppi / pixels);
17723
17724 return 0;
17725 }
17726 }
17727
17728 #ifdef HAVE_WINDOW_SYSTEM
17729 if (EQ (prop, Qheight))
17730 return OK_PIXELS (font ? FONT_HEIGHT ((XFontStruct *)font) : FRAME_LINE_HEIGHT (it->f));
17731 if (EQ (prop, Qwidth))
17732 return OK_PIXELS (font ? FONT_WIDTH ((XFontStruct *)font) : FRAME_COLUMN_WIDTH (it->f));
17733 #else
17734 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
17735 return OK_PIXELS (1);
17736 #endif
17737
17738 if (EQ (prop, Qtext))
17739 return OK_PIXELS (width_p
17740 ? window_box_width (it->w, TEXT_AREA)
17741 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
17742
17743 if (align_to && *align_to < 0)
17744 {
17745 *res = 0;
17746 if (EQ (prop, Qleft))
17747 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
17748 if (EQ (prop, Qright))
17749 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
17750 if (EQ (prop, Qcenter))
17751 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
17752 + window_box_width (it->w, TEXT_AREA) / 2);
17753 if (EQ (prop, Qleft_fringe))
17754 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
17755 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
17756 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
17757 if (EQ (prop, Qright_fringe))
17758 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
17759 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
17760 : window_box_right_offset (it->w, TEXT_AREA));
17761 if (EQ (prop, Qleft_margin))
17762 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
17763 if (EQ (prop, Qright_margin))
17764 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
17765 if (EQ (prop, Qscroll_bar))
17766 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
17767 ? 0
17768 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
17769 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
17770 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
17771 : 0)));
17772 }
17773 else
17774 {
17775 if (EQ (prop, Qleft_fringe))
17776 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
17777 if (EQ (prop, Qright_fringe))
17778 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
17779 if (EQ (prop, Qleft_margin))
17780 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
17781 if (EQ (prop, Qright_margin))
17782 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
17783 if (EQ (prop, Qscroll_bar))
17784 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
17785 }
17786
17787 prop = Fbuffer_local_value (prop, it->w->buffer);
17788 }
17789
17790 if (INTEGERP (prop) || FLOATP (prop))
17791 {
17792 int base_unit = (width_p
17793 ? FRAME_COLUMN_WIDTH (it->f)
17794 : FRAME_LINE_HEIGHT (it->f));
17795 return OK_PIXELS (XFLOATINT (prop) * base_unit);
17796 }
17797
17798 if (CONSP (prop))
17799 {
17800 Lisp_Object car = XCAR (prop);
17801 Lisp_Object cdr = XCDR (prop);
17802
17803 if (SYMBOLP (car))
17804 {
17805 #ifdef HAVE_WINDOW_SYSTEM
17806 if (valid_image_p (prop))
17807 {
17808 int id = lookup_image (it->f, prop);
17809 struct image *img = IMAGE_FROM_ID (it->f, id);
17810
17811 return OK_PIXELS (width_p ? img->width : img->height);
17812 }
17813 #endif
17814 if (EQ (car, Qplus) || EQ (car, Qminus))
17815 {
17816 int first = 1;
17817 double px;
17818
17819 pixels = 0;
17820 while (CONSP (cdr))
17821 {
17822 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
17823 font, width_p, align_to))
17824 return 0;
17825 if (first)
17826 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
17827 else
17828 pixels += px;
17829 cdr = XCDR (cdr);
17830 }
17831 if (EQ (car, Qminus))
17832 pixels = -pixels;
17833 return OK_PIXELS (pixels);
17834 }
17835
17836 car = Fbuffer_local_value (car, it->w->buffer);
17837 }
17838
17839 if (INTEGERP (car) || FLOATP (car))
17840 {
17841 double fact;
17842 pixels = XFLOATINT (car);
17843 if (NILP (cdr))
17844 return OK_PIXELS (pixels);
17845 if (calc_pixel_width_or_height (&fact, it, cdr,
17846 font, width_p, align_to))
17847 return OK_PIXELS (pixels * fact);
17848 return 0;
17849 }
17850
17851 return 0;
17852 }
17853
17854 return 0;
17855 }
17856
17857 \f
17858 /***********************************************************************
17859 Glyph Display
17860 ***********************************************************************/
17861
17862 #ifdef HAVE_WINDOW_SYSTEM
17863
17864 #if GLYPH_DEBUG
17865
17866 void
17867 dump_glyph_string (s)
17868 struct glyph_string *s;
17869 {
17870 fprintf (stderr, "glyph string\n");
17871 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
17872 s->x, s->y, s->width, s->height);
17873 fprintf (stderr, " ybase = %d\n", s->ybase);
17874 fprintf (stderr, " hl = %d\n", s->hl);
17875 fprintf (stderr, " left overhang = %d, right = %d\n",
17876 s->left_overhang, s->right_overhang);
17877 fprintf (stderr, " nchars = %d\n", s->nchars);
17878 fprintf (stderr, " extends to end of line = %d\n",
17879 s->extends_to_end_of_line_p);
17880 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
17881 fprintf (stderr, " bg width = %d\n", s->background_width);
17882 }
17883
17884 #endif /* GLYPH_DEBUG */
17885
17886 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
17887 of XChar2b structures for S; it can't be allocated in
17888 init_glyph_string because it must be allocated via `alloca'. W
17889 is the window on which S is drawn. ROW and AREA are the glyph row
17890 and area within the row from which S is constructed. START is the
17891 index of the first glyph structure covered by S. HL is a
17892 face-override for drawing S. */
17893
17894 #ifdef HAVE_NTGUI
17895 #define OPTIONAL_HDC(hdc) hdc,
17896 #define DECLARE_HDC(hdc) HDC hdc;
17897 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
17898 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
17899 #endif
17900
17901 #ifndef OPTIONAL_HDC
17902 #define OPTIONAL_HDC(hdc)
17903 #define DECLARE_HDC(hdc)
17904 #define ALLOCATE_HDC(hdc, f)
17905 #define RELEASE_HDC(hdc, f)
17906 #endif
17907
17908 static void
17909 init_glyph_string (s, OPTIONAL_HDC (hdc) char2b, w, row, area, start, hl)
17910 struct glyph_string *s;
17911 DECLARE_HDC (hdc)
17912 XChar2b *char2b;
17913 struct window *w;
17914 struct glyph_row *row;
17915 enum glyph_row_area area;
17916 int start;
17917 enum draw_glyphs_face hl;
17918 {
17919 bzero (s, sizeof *s);
17920 s->w = w;
17921 s->f = XFRAME (w->frame);
17922 #ifdef HAVE_NTGUI
17923 s->hdc = hdc;
17924 #endif
17925 s->display = FRAME_X_DISPLAY (s->f);
17926 s->window = FRAME_X_WINDOW (s->f);
17927 s->char2b = char2b;
17928 s->hl = hl;
17929 s->row = row;
17930 s->area = area;
17931 s->first_glyph = row->glyphs[area] + start;
17932 s->height = row->height;
17933 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
17934
17935 /* Display the internal border below the tool-bar window. */
17936 if (WINDOWP (s->f->tool_bar_window)
17937 && s->w == XWINDOW (s->f->tool_bar_window))
17938 s->y -= FRAME_INTERNAL_BORDER_WIDTH (s->f);
17939
17940 s->ybase = s->y + row->ascent;
17941 }
17942
17943
17944 /* Append the list of glyph strings with head H and tail T to the list
17945 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
17946
17947 static INLINE void
17948 append_glyph_string_lists (head, tail, h, t)
17949 struct glyph_string **head, **tail;
17950 struct glyph_string *h, *t;
17951 {
17952 if (h)
17953 {
17954 if (*head)
17955 (*tail)->next = h;
17956 else
17957 *head = h;
17958 h->prev = *tail;
17959 *tail = t;
17960 }
17961 }
17962
17963
17964 /* Prepend the list of glyph strings with head H and tail T to the
17965 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
17966 result. */
17967
17968 static INLINE void
17969 prepend_glyph_string_lists (head, tail, h, t)
17970 struct glyph_string **head, **tail;
17971 struct glyph_string *h, *t;
17972 {
17973 if (h)
17974 {
17975 if (*head)
17976 (*head)->prev = t;
17977 else
17978 *tail = t;
17979 t->next = *head;
17980 *head = h;
17981 }
17982 }
17983
17984
17985 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
17986 Set *HEAD and *TAIL to the resulting list. */
17987
17988 static INLINE void
17989 append_glyph_string (head, tail, s)
17990 struct glyph_string **head, **tail;
17991 struct glyph_string *s;
17992 {
17993 s->next = s->prev = NULL;
17994 append_glyph_string_lists (head, tail, s, s);
17995 }
17996
17997
17998 /* Get face and two-byte form of character glyph GLYPH on frame F.
17999 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
18000 a pointer to a realized face that is ready for display. */
18001
18002 static INLINE struct face *
18003 get_glyph_face_and_encoding (f, glyph, char2b, two_byte_p)
18004 struct frame *f;
18005 struct glyph *glyph;
18006 XChar2b *char2b;
18007 int *two_byte_p;
18008 {
18009 struct face *face;
18010
18011 xassert (glyph->type == CHAR_GLYPH);
18012 face = FACE_FROM_ID (f, glyph->face_id);
18013
18014 if (two_byte_p)
18015 *two_byte_p = 0;
18016
18017 if (!glyph->multibyte_p)
18018 {
18019 /* Unibyte case. We don't have to encode, but we have to make
18020 sure to use a face suitable for unibyte. */
18021 STORE_XCHAR2B (char2b, 0, glyph->u.ch);
18022 }
18023 else if (glyph->u.ch < 128
18024 && glyph->face_id < BASIC_FACE_ID_SENTINEL)
18025 {
18026 /* Case of ASCII in a face known to fit ASCII. */
18027 STORE_XCHAR2B (char2b, 0, glyph->u.ch);
18028 }
18029 else
18030 {
18031 struct font_info *font_info
18032 = FONT_INFO_FROM_ID (f, face->font_info_id);
18033 if (font_info)
18034 {
18035 struct charset *charset = CHARSET_FROM_ID (font_info->charset);
18036 unsigned code = ENCODE_CHAR (charset, glyph->u.ch);
18037
18038 if (CHARSET_DIMENSION (charset) == 1)
18039 STORE_XCHAR2B (char2b, 0, code);
18040 else
18041 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
18042
18043 /* Maybe encode the character in *CHAR2B. */
18044 if (CHARSET_ID (charset) != charset_ascii)
18045 {
18046 glyph->font_type
18047 = rif->encode_char (glyph->u.ch, char2b, font_info, charset,
18048 two_byte_p);
18049 }
18050 }
18051 }
18052
18053 /* Make sure X resources of the face are allocated. */
18054 xassert (face != NULL);
18055 PREPARE_FACE_FOR_DISPLAY (f, face);
18056 return face;
18057 }
18058
18059
18060 /* Fill glyph string S with composition components specified by S->cmp.
18061
18062 FACES is an array of faces for all components of this composition.
18063 S->gidx is the index of the first component for S.
18064 OVERLAPS_P non-zero means S should draw the foreground only, and
18065 use its physical height for clipping.
18066
18067 Value is the index of a component not in S. */
18068
18069 static int
18070 fill_composite_glyph_string (s, faces, overlaps_p)
18071 struct glyph_string *s;
18072 struct face **faces;
18073 int overlaps_p;
18074 {
18075 int i;
18076
18077 xassert (s);
18078
18079 s->for_overlaps_p = overlaps_p;
18080
18081 s->face = faces[s->gidx];
18082 s->font = s->face->font;
18083 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
18084
18085 /* For all glyphs of this composition, starting at the offset
18086 S->gidx, until we reach the end of the definition or encounter a
18087 glyph that requires the different face, add it to S. */
18088 ++s->nchars;
18089 for (i = s->gidx + 1; i < s->cmp->glyph_len && faces[i] == s->face; ++i)
18090 ++s->nchars;
18091
18092 /* All glyph strings for the same composition has the same width,
18093 i.e. the width set for the first component of the composition. */
18094
18095 s->width = s->first_glyph->pixel_width;
18096
18097 /* If the specified font could not be loaded, use the frame's
18098 default font, but record the fact that we couldn't load it in
18099 the glyph string so that we can draw rectangles for the
18100 characters of the glyph string. */
18101 if (s->font == NULL)
18102 {
18103 s->font_not_found_p = 1;
18104 s->font = FRAME_FONT (s->f);
18105 }
18106
18107 /* Adjust base line for subscript/superscript text. */
18108 s->ybase += s->first_glyph->voffset;
18109
18110 xassert (s->face && s->face->gc);
18111
18112 /* This glyph string must always be drawn with 16-bit functions. */
18113 s->two_byte_p = 1;
18114
18115 return s->gidx + s->nchars;
18116 }
18117
18118
18119 /* Fill glyph string S from a sequence of character glyphs.
18120
18121 FACE_ID is the face id of the string. START is the index of the
18122 first glyph to consider, END is the index of the last + 1.
18123 OVERLAPS_P non-zero means S should draw the foreground only, and
18124 use its physical height for clipping.
18125
18126 Value is the index of the first glyph not in S. */
18127
18128 static int
18129 fill_glyph_string (s, face_id, start, end, overlaps_p)
18130 struct glyph_string *s;
18131 int face_id;
18132 int start, end, overlaps_p;
18133 {
18134 struct glyph *glyph, *last;
18135 int voffset;
18136 int glyph_not_available_p;
18137
18138 xassert (s->f == XFRAME (s->w->frame));
18139 xassert (s->nchars == 0);
18140 xassert (start >= 0 && end > start);
18141
18142 s->for_overlaps_p = overlaps_p,
18143 glyph = s->row->glyphs[s->area] + start;
18144 last = s->row->glyphs[s->area] + end;
18145 voffset = glyph->voffset;
18146
18147 glyph_not_available_p = glyph->glyph_not_available_p;
18148
18149 while (glyph < last
18150 && glyph->type == CHAR_GLYPH
18151 && glyph->voffset == voffset
18152 /* Same face id implies same font, nowadays. */
18153 && glyph->face_id == face_id
18154 && glyph->glyph_not_available_p == glyph_not_available_p)
18155 {
18156 int two_byte_p;
18157
18158 s->face = get_glyph_face_and_encoding (s->f, glyph,
18159 s->char2b + s->nchars,
18160 &two_byte_p);
18161 s->two_byte_p = two_byte_p;
18162 ++s->nchars;
18163 xassert (s->nchars <= end - start);
18164 s->width += glyph->pixel_width;
18165 ++glyph;
18166 }
18167
18168 s->font = s->face->font;
18169 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
18170
18171 /* If the specified font could not be loaded, use the frame's font,
18172 but record the fact that we couldn't load it in
18173 S->font_not_found_p so that we can draw rectangles for the
18174 characters of the glyph string. */
18175 if (s->font == NULL || glyph_not_available_p)
18176 {
18177 s->font_not_found_p = 1;
18178 s->font = FRAME_FONT (s->f);
18179 }
18180
18181 /* Adjust base line for subscript/superscript text. */
18182 s->ybase += voffset;
18183
18184 xassert (s->face && s->face->gc);
18185 return glyph - s->row->glyphs[s->area];
18186 }
18187
18188
18189 /* Fill glyph string S from image glyph S->first_glyph. */
18190
18191 static void
18192 fill_image_glyph_string (s)
18193 struct glyph_string *s;
18194 {
18195 xassert (s->first_glyph->type == IMAGE_GLYPH);
18196 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
18197 xassert (s->img);
18198 s->slice = s->first_glyph->slice;
18199 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
18200 s->font = s->face->font;
18201 s->width = s->first_glyph->pixel_width;
18202
18203 /* Adjust base line for subscript/superscript text. */
18204 s->ybase += s->first_glyph->voffset;
18205 }
18206
18207
18208 /* Fill glyph string S from a sequence of stretch glyphs.
18209
18210 ROW is the glyph row in which the glyphs are found, AREA is the
18211 area within the row. START is the index of the first glyph to
18212 consider, END is the index of the last + 1.
18213
18214 Value is the index of the first glyph not in S. */
18215
18216 static int
18217 fill_stretch_glyph_string (s, row, area, start, end)
18218 struct glyph_string *s;
18219 struct glyph_row *row;
18220 enum glyph_row_area area;
18221 int start, end;
18222 {
18223 struct glyph *glyph, *last;
18224 int voffset, face_id;
18225
18226 xassert (s->first_glyph->type == STRETCH_GLYPH);
18227
18228 glyph = s->row->glyphs[s->area] + start;
18229 last = s->row->glyphs[s->area] + end;
18230 face_id = glyph->face_id;
18231 s->face = FACE_FROM_ID (s->f, face_id);
18232 s->font = s->face->font;
18233 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
18234 s->width = glyph->pixel_width;
18235 voffset = glyph->voffset;
18236
18237 for (++glyph;
18238 (glyph < last
18239 && glyph->type == STRETCH_GLYPH
18240 && glyph->voffset == voffset
18241 && glyph->face_id == face_id);
18242 ++glyph)
18243 s->width += glyph->pixel_width;
18244
18245 /* Adjust base line for subscript/superscript text. */
18246 s->ybase += voffset;
18247
18248 /* The case that face->gc == 0 is handled when drawing the glyph
18249 string by calling PREPARE_FACE_FOR_DISPLAY. */
18250 xassert (s->face);
18251 return glyph - s->row->glyphs[s->area];
18252 }
18253
18254
18255 /* EXPORT for RIF:
18256 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
18257 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
18258 assumed to be zero. */
18259
18260 void
18261 x_get_glyph_overhangs (glyph, f, left, right)
18262 struct glyph *glyph;
18263 struct frame *f;
18264 int *left, *right;
18265 {
18266 *left = *right = 0;
18267
18268 if (glyph->type == CHAR_GLYPH)
18269 {
18270 XFontStruct *font;
18271 struct face *face;
18272 struct font_info *font_info;
18273 XChar2b char2b;
18274 XCharStruct *pcm;
18275
18276 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
18277 font = face->font;
18278 font_info = FONT_INFO_FROM_ID (f, face->font_info_id);
18279 if (font /* ++KFS: Should this be font_info ? */
18280 && (pcm = rif->per_char_metric (font, &char2b, glyph->font_type)))
18281 {
18282 if (pcm->rbearing > pcm->width)
18283 *right = pcm->rbearing - pcm->width;
18284 if (pcm->lbearing < 0)
18285 *left = -pcm->lbearing;
18286 }
18287 }
18288 else if (glyph->type == COMPOSITE_GLYPH)
18289 {
18290 struct composition *cmp = composition_table[glyph->u.cmp_id];
18291
18292 *right = cmp->rbearing - cmp->pixel_width;
18293 *left = - cmp->lbearing;
18294 }
18295 }
18296
18297
18298 /* Return the index of the first glyph preceding glyph string S that
18299 is overwritten by S because of S's left overhang. Value is -1
18300 if no glyphs are overwritten. */
18301
18302 static int
18303 left_overwritten (s)
18304 struct glyph_string *s;
18305 {
18306 int k;
18307
18308 if (s->left_overhang)
18309 {
18310 int x = 0, i;
18311 struct glyph *glyphs = s->row->glyphs[s->area];
18312 int first = s->first_glyph - glyphs;
18313
18314 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
18315 x -= glyphs[i].pixel_width;
18316
18317 k = i + 1;
18318 }
18319 else
18320 k = -1;
18321
18322 return k;
18323 }
18324
18325
18326 /* Return the index of the first glyph preceding glyph string S that
18327 is overwriting S because of its right overhang. Value is -1 if no
18328 glyph in front of S overwrites S. */
18329
18330 static int
18331 left_overwriting (s)
18332 struct glyph_string *s;
18333 {
18334 int i, k, x;
18335 struct glyph *glyphs = s->row->glyphs[s->area];
18336 int first = s->first_glyph - glyphs;
18337
18338 k = -1;
18339 x = 0;
18340 for (i = first - 1; i >= 0; --i)
18341 {
18342 int left, right;
18343 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
18344 if (x + right > 0)
18345 k = i;
18346 x -= glyphs[i].pixel_width;
18347 }
18348
18349 return k;
18350 }
18351
18352
18353 /* Return the index of the last glyph following glyph string S that is
18354 not overwritten by S because of S's right overhang. Value is -1 if
18355 no such glyph is found. */
18356
18357 static int
18358 right_overwritten (s)
18359 struct glyph_string *s;
18360 {
18361 int k = -1;
18362
18363 if (s->right_overhang)
18364 {
18365 int x = 0, i;
18366 struct glyph *glyphs = s->row->glyphs[s->area];
18367 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
18368 int end = s->row->used[s->area];
18369
18370 for (i = first; i < end && s->right_overhang > x; ++i)
18371 x += glyphs[i].pixel_width;
18372
18373 k = i;
18374 }
18375
18376 return k;
18377 }
18378
18379
18380 /* Return the index of the last glyph following glyph string S that
18381 overwrites S because of its left overhang. Value is negative
18382 if no such glyph is found. */
18383
18384 static int
18385 right_overwriting (s)
18386 struct glyph_string *s;
18387 {
18388 int i, k, x;
18389 int end = s->row->used[s->area];
18390 struct glyph *glyphs = s->row->glyphs[s->area];
18391 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
18392
18393 k = -1;
18394 x = 0;
18395 for (i = first; i < end; ++i)
18396 {
18397 int left, right;
18398 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
18399 if (x - left < 0)
18400 k = i;
18401 x += glyphs[i].pixel_width;
18402 }
18403
18404 return k;
18405 }
18406
18407
18408 /* Get face and two-byte form of character C in face FACE_ID on frame
18409 F. The encoding of C is returned in *CHAR2B. MULTIBYTE_P non-zero
18410 means we want to display multibyte text. DISPLAY_P non-zero means
18411 make sure that X resources for the face returned are allocated.
18412 Value is a pointer to a realized face that is ready for display if
18413 DISPLAY_P is non-zero. */
18414
18415 static INLINE struct face *
18416 get_char_face_and_encoding (f, c, face_id, char2b, multibyte_p, display_p)
18417 struct frame *f;
18418 int c, face_id;
18419 XChar2b *char2b;
18420 int multibyte_p, display_p;
18421 {
18422 struct face *face = FACE_FROM_ID (f, face_id);
18423
18424 if (!multibyte_p)
18425 {
18426 /* Unibyte case. We don't have to encode, but we have to make
18427 sure to use a face suitable for unibyte. */
18428 STORE_XCHAR2B (char2b, 0, c);
18429 face_id = FACE_FOR_CHAR (f, face, c, -1, Qnil);
18430 face = FACE_FROM_ID (f, face_id);
18431 }
18432 else if (c < 128 && face_id < BASIC_FACE_ID_SENTINEL)
18433 {
18434 /* Case of ASCII in a face known to fit ASCII. */
18435 STORE_XCHAR2B (char2b, 0, c);
18436 }
18437 else if (face->font != NULL)
18438 {
18439 struct font_info *font_info
18440 = FONT_INFO_FROM_ID (f, face->font_info_id);
18441 struct charset *charset = CHARSET_FROM_ID (font_info->charset);
18442 unsigned code = ENCODE_CHAR (charset, c);
18443
18444 if (CHARSET_DIMENSION (charset) == 1)
18445 STORE_XCHAR2B (char2b, 0, code);
18446 else
18447 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
18448 /* Maybe encode the character in *CHAR2B. */
18449 rif->encode_char (c, char2b, font_info, charset, NULL);
18450 }
18451
18452 /* Make sure X resources of the face are allocated. */
18453 #ifdef HAVE_X_WINDOWS
18454 if (display_p)
18455 #endif
18456 {
18457 xassert (face != NULL);
18458 PREPARE_FACE_FOR_DISPLAY (f, face);
18459 }
18460
18461 return face;
18462 }
18463
18464
18465 /* Set background width of glyph string S. START is the index of the
18466 first glyph following S. LAST_X is the right-most x-position + 1
18467 in the drawing area. */
18468
18469 static INLINE void
18470 set_glyph_string_background_width (s, start, last_x)
18471 struct glyph_string *s;
18472 int start;
18473 int last_x;
18474 {
18475 /* If the face of this glyph string has to be drawn to the end of
18476 the drawing area, set S->extends_to_end_of_line_p. */
18477 struct face *default_face = FACE_FROM_ID (s->f, DEFAULT_FACE_ID);
18478
18479 if (start == s->row->used[s->area]
18480 && s->area == TEXT_AREA
18481 && ((s->hl == DRAW_NORMAL_TEXT
18482 && (s->row->fill_line_p
18483 || s->face->background != default_face->background
18484 || s->face->stipple != default_face->stipple
18485 || s->row->mouse_face_p))
18486 || s->hl == DRAW_MOUSE_FACE
18487 || ((s->hl == DRAW_IMAGE_RAISED || s->hl == DRAW_IMAGE_SUNKEN)
18488 && s->row->fill_line_p)))
18489 s->extends_to_end_of_line_p = 1;
18490
18491 /* If S extends its face to the end of the line, set its
18492 background_width to the distance to the right edge of the drawing
18493 area. */
18494 if (s->extends_to_end_of_line_p)
18495 s->background_width = last_x - s->x + 1;
18496 else
18497 s->background_width = s->width;
18498 }
18499
18500
18501 /* Compute overhangs and x-positions for glyph string S and its
18502 predecessors, or successors. X is the starting x-position for S.
18503 BACKWARD_P non-zero means process predecessors. */
18504
18505 static void
18506 compute_overhangs_and_x (s, x, backward_p)
18507 struct glyph_string *s;
18508 int x;
18509 int backward_p;
18510 {
18511 if (backward_p)
18512 {
18513 while (s)
18514 {
18515 if (rif->compute_glyph_string_overhangs)
18516 rif->compute_glyph_string_overhangs (s);
18517 x -= s->width;
18518 s->x = x;
18519 s = s->prev;
18520 }
18521 }
18522 else
18523 {
18524 while (s)
18525 {
18526 if (rif->compute_glyph_string_overhangs)
18527 rif->compute_glyph_string_overhangs (s);
18528 s->x = x;
18529 x += s->width;
18530 s = s->next;
18531 }
18532 }
18533 }
18534
18535
18536
18537 /* The following macros are only called from draw_glyphs below.
18538 They reference the following parameters of that function directly:
18539 `w', `row', `area', and `overlap_p'
18540 as well as the following local variables:
18541 `s', `f', and `hdc' (in W32) */
18542
18543 #ifdef HAVE_NTGUI
18544 /* On W32, silently add local `hdc' variable to argument list of
18545 init_glyph_string. */
18546 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
18547 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
18548 #else
18549 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
18550 init_glyph_string (s, char2b, w, row, area, start, hl)
18551 #endif
18552
18553 /* Add a glyph string for a stretch glyph to the list of strings
18554 between HEAD and TAIL. START is the index of the stretch glyph in
18555 row area AREA of glyph row ROW. END is the index of the last glyph
18556 in that glyph row area. X is the current output position assigned
18557 to the new glyph string constructed. HL overrides that face of the
18558 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
18559 is the right-most x-position of the drawing area. */
18560
18561 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
18562 and below -- keep them on one line. */
18563 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
18564 do \
18565 { \
18566 s = (struct glyph_string *) alloca (sizeof *s); \
18567 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
18568 START = fill_stretch_glyph_string (s, row, area, START, END); \
18569 append_glyph_string (&HEAD, &TAIL, s); \
18570 s->x = (X); \
18571 } \
18572 while (0)
18573
18574
18575 /* Add a glyph string for an image glyph to the list of strings
18576 between HEAD and TAIL. START is the index of the image glyph in
18577 row area AREA of glyph row ROW. END is the index of the last glyph
18578 in that glyph row area. X is the current output position assigned
18579 to the new glyph string constructed. HL overrides that face of the
18580 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
18581 is the right-most x-position of the drawing area. */
18582
18583 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
18584 do \
18585 { \
18586 s = (struct glyph_string *) alloca (sizeof *s); \
18587 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
18588 fill_image_glyph_string (s); \
18589 append_glyph_string (&HEAD, &TAIL, s); \
18590 ++START; \
18591 s->x = (X); \
18592 } \
18593 while (0)
18594
18595
18596 /* Add a glyph string for a sequence of character glyphs to the list
18597 of strings between HEAD and TAIL. START is the index of the first
18598 glyph in row area AREA of glyph row ROW that is part of the new
18599 glyph string. END is the index of the last glyph in that glyph row
18600 area. X is the current output position assigned to the new glyph
18601 string constructed. HL overrides that face of the glyph; e.g. it
18602 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
18603 right-most x-position of the drawing area. */
18604
18605 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
18606 do \
18607 { \
18608 int face_id; \
18609 XChar2b *char2b; \
18610 \
18611 face_id = (row)->glyphs[area][START].face_id; \
18612 \
18613 s = (struct glyph_string *) alloca (sizeof *s); \
18614 char2b = (XChar2b *) alloca ((END - START) * sizeof *char2b); \
18615 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
18616 append_glyph_string (&HEAD, &TAIL, s); \
18617 s->x = (X); \
18618 START = fill_glyph_string (s, face_id, START, END, overlaps_p); \
18619 } \
18620 while (0)
18621
18622
18623 /* Add a glyph string for a composite sequence to the list of strings
18624 between HEAD and TAIL. START is the index of the first glyph in
18625 row area AREA of glyph row ROW that is part of the new glyph
18626 string. END is the index of the last glyph in that glyph row area.
18627 X is the current output position assigned to the new glyph string
18628 constructed. HL overrides that face of the glyph; e.g. it is
18629 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
18630 x-position of the drawing area. */
18631
18632 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
18633 do { \
18634 int cmp_id = (row)->glyphs[area][START].u.cmp_id; \
18635 int face_id = (row)->glyphs[area][START].face_id; \
18636 struct face *base_face = FACE_FROM_ID (f, face_id); \
18637 struct composition *cmp = composition_table[cmp_id]; \
18638 int glyph_len = cmp->glyph_len; \
18639 XChar2b *char2b; \
18640 struct face **faces; \
18641 struct glyph_string *first_s = NULL; \
18642 int n; \
18643 \
18644 base_face = base_face->ascii_face; \
18645 char2b = (XChar2b *) alloca ((sizeof *char2b) * glyph_len); \
18646 faces = (struct face **) alloca ((sizeof *faces) * glyph_len); \
18647 /* At first, fill in `char2b' and `faces'. */ \
18648 for (n = 0; n < glyph_len; n++) \
18649 { \
18650 int c = COMPOSITION_GLYPH (cmp, n); \
18651 int this_face_id = FACE_FOR_CHAR (f, base_face, c, -1, Qnil); \
18652 faces[n] = FACE_FROM_ID (f, this_face_id); \
18653 get_char_face_and_encoding (f, c, this_face_id, \
18654 char2b + n, 1, 1); \
18655 } \
18656 \
18657 /* Make glyph_strings for each glyph sequence that is drawable by \
18658 the same face, and append them to HEAD/TAIL. */ \
18659 for (n = 0; n < cmp->glyph_len;) \
18660 { \
18661 s = (struct glyph_string *) alloca (sizeof *s); \
18662 INIT_GLYPH_STRING (s, char2b + n, w, row, area, START, HL); \
18663 append_glyph_string (&(HEAD), &(TAIL), s); \
18664 s->cmp = cmp; \
18665 s->gidx = n; \
18666 s->x = (X); \
18667 \
18668 if (n == 0) \
18669 first_s = s; \
18670 \
18671 n = fill_composite_glyph_string (s, faces, overlaps_p); \
18672 } \
18673 \
18674 ++START; \
18675 s = first_s; \
18676 } while (0)
18677
18678
18679 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
18680 of AREA of glyph row ROW on window W between indices START and END.
18681 HL overrides the face for drawing glyph strings, e.g. it is
18682 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
18683 x-positions of the drawing area.
18684
18685 This is an ugly monster macro construct because we must use alloca
18686 to allocate glyph strings (because draw_glyphs can be called
18687 asynchronously). */
18688
18689 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
18690 do \
18691 { \
18692 HEAD = TAIL = NULL; \
18693 while (START < END) \
18694 { \
18695 struct glyph *first_glyph = (row)->glyphs[area] + START; \
18696 switch (first_glyph->type) \
18697 { \
18698 case CHAR_GLYPH: \
18699 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
18700 HL, X, LAST_X); \
18701 break; \
18702 \
18703 case COMPOSITE_GLYPH: \
18704 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
18705 HL, X, LAST_X); \
18706 break; \
18707 \
18708 case STRETCH_GLYPH: \
18709 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
18710 HL, X, LAST_X); \
18711 break; \
18712 \
18713 case IMAGE_GLYPH: \
18714 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
18715 HL, X, LAST_X); \
18716 break; \
18717 \
18718 default: \
18719 abort (); \
18720 } \
18721 \
18722 if (s) \
18723 { \
18724 set_glyph_string_background_width (s, START, LAST_X); \
18725 (X) += s->width; \
18726 } \
18727 } \
18728 } \
18729 while (0)
18730
18731
18732 /* Draw glyphs between START and END in AREA of ROW on window W,
18733 starting at x-position X. X is relative to AREA in W. HL is a
18734 face-override with the following meaning:
18735
18736 DRAW_NORMAL_TEXT draw normally
18737 DRAW_CURSOR draw in cursor face
18738 DRAW_MOUSE_FACE draw in mouse face.
18739 DRAW_INVERSE_VIDEO draw in mode line face
18740 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
18741 DRAW_IMAGE_RAISED draw an image with a raised relief around it
18742
18743 If OVERLAPS_P is non-zero, draw only the foreground of characters
18744 and clip to the physical height of ROW.
18745
18746 Value is the x-position reached, relative to AREA of W. */
18747
18748 static int
18749 draw_glyphs (w, x, row, area, start, end, hl, overlaps_p)
18750 struct window *w;
18751 int x;
18752 struct glyph_row *row;
18753 enum glyph_row_area area;
18754 EMACS_INT start, end;
18755 enum draw_glyphs_face hl;
18756 int overlaps_p;
18757 {
18758 struct glyph_string *head, *tail;
18759 struct glyph_string *s;
18760 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
18761 int last_x, area_width;
18762 int x_reached;
18763 int i, j;
18764 struct frame *f = XFRAME (WINDOW_FRAME (w));
18765 DECLARE_HDC (hdc);
18766
18767 ALLOCATE_HDC (hdc, f);
18768
18769 /* Let's rather be paranoid than getting a SEGV. */
18770 end = min (end, row->used[area]);
18771 start = max (0, start);
18772 start = min (end, start);
18773
18774 /* Translate X to frame coordinates. Set last_x to the right
18775 end of the drawing area. */
18776 if (row->full_width_p)
18777 {
18778 /* X is relative to the left edge of W, without scroll bars
18779 or fringes. */
18780 x += WINDOW_LEFT_EDGE_X (w);
18781 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
18782 }
18783 else
18784 {
18785 int area_left = window_box_left (w, area);
18786 x += area_left;
18787 area_width = window_box_width (w, area);
18788 last_x = area_left + area_width;
18789 }
18790
18791 /* Build a doubly-linked list of glyph_string structures between
18792 head and tail from what we have to draw. Note that the macro
18793 BUILD_GLYPH_STRINGS will modify its start parameter. That's
18794 the reason we use a separate variable `i'. */
18795 i = start;
18796 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
18797 if (tail)
18798 x_reached = tail->x + tail->background_width;
18799 else
18800 x_reached = x;
18801
18802 /* If there are any glyphs with lbearing < 0 or rbearing > width in
18803 the row, redraw some glyphs in front or following the glyph
18804 strings built above. */
18805 if (head && !overlaps_p && row->contains_overlapping_glyphs_p)
18806 {
18807 int dummy_x = 0;
18808 struct glyph_string *h, *t;
18809
18810 /* Compute overhangs for all glyph strings. */
18811 if (rif->compute_glyph_string_overhangs)
18812 for (s = head; s; s = s->next)
18813 rif->compute_glyph_string_overhangs (s);
18814
18815 /* Prepend glyph strings for glyphs in front of the first glyph
18816 string that are overwritten because of the first glyph
18817 string's left overhang. The background of all strings
18818 prepended must be drawn because the first glyph string
18819 draws over it. */
18820 i = left_overwritten (head);
18821 if (i >= 0)
18822 {
18823 j = i;
18824 BUILD_GLYPH_STRINGS (j, start, h, t,
18825 DRAW_NORMAL_TEXT, dummy_x, last_x);
18826 start = i;
18827 compute_overhangs_and_x (t, head->x, 1);
18828 prepend_glyph_string_lists (&head, &tail, h, t);
18829 clip_head = head;
18830 }
18831
18832 /* Prepend glyph strings for glyphs in front of the first glyph
18833 string that overwrite that glyph string because of their
18834 right overhang. For these strings, only the foreground must
18835 be drawn, because it draws over the glyph string at `head'.
18836 The background must not be drawn because this would overwrite
18837 right overhangs of preceding glyphs for which no glyph
18838 strings exist. */
18839 i = left_overwriting (head);
18840 if (i >= 0)
18841 {
18842 clip_head = head;
18843 BUILD_GLYPH_STRINGS (i, start, h, t,
18844 DRAW_NORMAL_TEXT, dummy_x, last_x);
18845 for (s = h; s; s = s->next)
18846 s->background_filled_p = 1;
18847 compute_overhangs_and_x (t, head->x, 1);
18848 prepend_glyph_string_lists (&head, &tail, h, t);
18849 }
18850
18851 /* Append glyphs strings for glyphs following the last glyph
18852 string tail that are overwritten by tail. The background of
18853 these strings has to be drawn because tail's foreground draws
18854 over it. */
18855 i = right_overwritten (tail);
18856 if (i >= 0)
18857 {
18858 BUILD_GLYPH_STRINGS (end, i, h, t,
18859 DRAW_NORMAL_TEXT, x, last_x);
18860 compute_overhangs_and_x (h, tail->x + tail->width, 0);
18861 append_glyph_string_lists (&head, &tail, h, t);
18862 clip_tail = tail;
18863 }
18864
18865 /* Append glyph strings for glyphs following the last glyph
18866 string tail that overwrite tail. The foreground of such
18867 glyphs has to be drawn because it writes into the background
18868 of tail. The background must not be drawn because it could
18869 paint over the foreground of following glyphs. */
18870 i = right_overwriting (tail);
18871 if (i >= 0)
18872 {
18873 clip_tail = tail;
18874 BUILD_GLYPH_STRINGS (end, i, h, t,
18875 DRAW_NORMAL_TEXT, x, last_x);
18876 for (s = h; s; s = s->next)
18877 s->background_filled_p = 1;
18878 compute_overhangs_and_x (h, tail->x + tail->width, 0);
18879 append_glyph_string_lists (&head, &tail, h, t);
18880 }
18881 if (clip_head || clip_tail)
18882 for (s = head; s; s = s->next)
18883 {
18884 s->clip_head = clip_head;
18885 s->clip_tail = clip_tail;
18886 }
18887 }
18888
18889 /* Draw all strings. */
18890 for (s = head; s; s = s->next)
18891 rif->draw_glyph_string (s);
18892
18893 if (area == TEXT_AREA
18894 && !row->full_width_p
18895 /* When drawing overlapping rows, only the glyph strings'
18896 foreground is drawn, which doesn't erase a cursor
18897 completely. */
18898 && !overlaps_p)
18899 {
18900 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
18901 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
18902 : (tail ? tail->x + tail->background_width : x));
18903
18904 int text_left = window_box_left (w, TEXT_AREA);
18905 x0 -= text_left;
18906 x1 -= text_left;
18907
18908 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
18909 row->y, MATRIX_ROW_BOTTOM_Y (row));
18910 }
18911
18912 /* Value is the x-position up to which drawn, relative to AREA of W.
18913 This doesn't include parts drawn because of overhangs. */
18914 if (row->full_width_p)
18915 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
18916 else
18917 x_reached -= window_box_left (w, area);
18918
18919 RELEASE_HDC (hdc, f);
18920
18921 return x_reached;
18922 }
18923
18924 /* Expand row matrix if too narrow. Don't expand if area
18925 is not present. */
18926
18927 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
18928 { \
18929 if (!fonts_changed_p \
18930 && (it->glyph_row->glyphs[area] \
18931 < it->glyph_row->glyphs[area + 1])) \
18932 { \
18933 it->w->ncols_scale_factor++; \
18934 fonts_changed_p = 1; \
18935 } \
18936 }
18937
18938 /* Store one glyph for IT->char_to_display in IT->glyph_row.
18939 Called from x_produce_glyphs when IT->glyph_row is non-null. */
18940
18941 static INLINE void
18942 append_glyph (it)
18943 struct it *it;
18944 {
18945 struct glyph *glyph;
18946 enum glyph_row_area area = it->area;
18947
18948 xassert (it->glyph_row);
18949 xassert (it->char_to_display != '\n' && it->char_to_display != '\t');
18950
18951 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
18952 if (glyph < it->glyph_row->glyphs[area + 1])
18953 {
18954 glyph->charpos = CHARPOS (it->position);
18955 glyph->object = it->object;
18956 glyph->pixel_width = it->pixel_width;
18957 glyph->ascent = it->ascent;
18958 glyph->descent = it->descent;
18959 glyph->voffset = it->voffset;
18960 glyph->type = CHAR_GLYPH;
18961 glyph->multibyte_p = it->multibyte_p;
18962 glyph->left_box_line_p = it->start_of_box_run_p;
18963 glyph->right_box_line_p = it->end_of_box_run_p;
18964 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
18965 || it->phys_descent > it->descent);
18966 glyph->padding_p = 0;
18967 glyph->glyph_not_available_p = it->glyph_not_available_p;
18968 glyph->face_id = it->face_id;
18969 glyph->u.ch = it->char_to_display;
18970 glyph->slice = null_glyph_slice;
18971 glyph->font_type = FONT_TYPE_UNKNOWN;
18972 ++it->glyph_row->used[area];
18973 }
18974 else
18975 IT_EXPAND_MATRIX_WIDTH (it, area);
18976 }
18977
18978 /* Store one glyph for the composition IT->cmp_id in IT->glyph_row.
18979 Called from x_produce_glyphs when IT->glyph_row is non-null. */
18980
18981 static INLINE void
18982 append_composite_glyph (it)
18983 struct it *it;
18984 {
18985 struct glyph *glyph;
18986 enum glyph_row_area area = it->area;
18987
18988 xassert (it->glyph_row);
18989
18990 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
18991 if (glyph < it->glyph_row->glyphs[area + 1])
18992 {
18993 glyph->charpos = CHARPOS (it->position);
18994 glyph->object = it->object;
18995 glyph->pixel_width = it->pixel_width;
18996 glyph->ascent = it->ascent;
18997 glyph->descent = it->descent;
18998 glyph->voffset = it->voffset;
18999 glyph->type = COMPOSITE_GLYPH;
19000 glyph->multibyte_p = it->multibyte_p;
19001 glyph->left_box_line_p = it->start_of_box_run_p;
19002 glyph->right_box_line_p = it->end_of_box_run_p;
19003 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
19004 || it->phys_descent > it->descent);
19005 glyph->padding_p = 0;
19006 glyph->glyph_not_available_p = 0;
19007 glyph->face_id = it->face_id;
19008 glyph->u.cmp_id = it->cmp_id;
19009 glyph->slice = null_glyph_slice;
19010 glyph->font_type = FONT_TYPE_UNKNOWN;
19011 ++it->glyph_row->used[area];
19012 }
19013 else
19014 IT_EXPAND_MATRIX_WIDTH (it, area);
19015 }
19016
19017
19018 /* Change IT->ascent and IT->height according to the setting of
19019 IT->voffset. */
19020
19021 static INLINE void
19022 take_vertical_position_into_account (it)
19023 struct it *it;
19024 {
19025 if (it->voffset)
19026 {
19027 if (it->voffset < 0)
19028 /* Increase the ascent so that we can display the text higher
19029 in the line. */
19030 it->ascent -= it->voffset;
19031 else
19032 /* Increase the descent so that we can display the text lower
19033 in the line. */
19034 it->descent += it->voffset;
19035 }
19036 }
19037
19038
19039 /* Produce glyphs/get display metrics for the image IT is loaded with.
19040 See the description of struct display_iterator in dispextern.h for
19041 an overview of struct display_iterator. */
19042
19043 static void
19044 produce_image_glyph (it)
19045 struct it *it;
19046 {
19047 struct image *img;
19048 struct face *face;
19049 int glyph_ascent;
19050 struct glyph_slice slice;
19051
19052 xassert (it->what == IT_IMAGE);
19053
19054 face = FACE_FROM_ID (it->f, it->face_id);
19055 xassert (face);
19056 /* Make sure X resources of the face is loaded. */
19057 PREPARE_FACE_FOR_DISPLAY (it->f, face);
19058
19059 if (it->image_id < 0)
19060 {
19061 /* Fringe bitmap. */
19062 it->ascent = it->phys_ascent = 0;
19063 it->descent = it->phys_descent = 0;
19064 it->pixel_width = 0;
19065 it->nglyphs = 0;
19066 return;
19067 }
19068
19069 img = IMAGE_FROM_ID (it->f, it->image_id);
19070 xassert (img);
19071 /* Make sure X resources of the image is loaded. */
19072 prepare_image_for_display (it->f, img);
19073
19074 slice.x = slice.y = 0;
19075 slice.width = img->width;
19076 slice.height = img->height;
19077
19078 if (INTEGERP (it->slice.x))
19079 slice.x = XINT (it->slice.x);
19080 else if (FLOATP (it->slice.x))
19081 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
19082
19083 if (INTEGERP (it->slice.y))
19084 slice.y = XINT (it->slice.y);
19085 else if (FLOATP (it->slice.y))
19086 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
19087
19088 if (INTEGERP (it->slice.width))
19089 slice.width = XINT (it->slice.width);
19090 else if (FLOATP (it->slice.width))
19091 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
19092
19093 if (INTEGERP (it->slice.height))
19094 slice.height = XINT (it->slice.height);
19095 else if (FLOATP (it->slice.height))
19096 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
19097
19098 if (slice.x >= img->width)
19099 slice.x = img->width;
19100 if (slice.y >= img->height)
19101 slice.y = img->height;
19102 if (slice.x + slice.width >= img->width)
19103 slice.width = img->width - slice.x;
19104 if (slice.y + slice.height > img->height)
19105 slice.height = img->height - slice.y;
19106
19107 if (slice.width == 0 || slice.height == 0)
19108 return;
19109
19110 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
19111
19112 it->descent = slice.height - glyph_ascent;
19113 if (slice.y == 0)
19114 it->descent += img->vmargin;
19115 if (slice.y + slice.height == img->height)
19116 it->descent += img->vmargin;
19117 it->phys_descent = it->descent;
19118
19119 it->pixel_width = slice.width;
19120 if (slice.x == 0)
19121 it->pixel_width += img->hmargin;
19122 if (slice.x + slice.width == img->width)
19123 it->pixel_width += img->hmargin;
19124
19125 /* It's quite possible for images to have an ascent greater than
19126 their height, so don't get confused in that case. */
19127 if (it->descent < 0)
19128 it->descent = 0;
19129
19130 #if 0 /* this breaks image tiling */
19131 /* If this glyph is alone on the last line, adjust it.ascent to minimum row ascent. */
19132 int face_ascent = face->font ? FONT_BASE (face->font) : FRAME_BASELINE_OFFSET (it->f);
19133 if (face_ascent > it->ascent)
19134 it->ascent = it->phys_ascent = face_ascent;
19135 #endif
19136
19137 it->nglyphs = 1;
19138
19139 if (face->box != FACE_NO_BOX)
19140 {
19141 if (face->box_line_width > 0)
19142 {
19143 if (slice.y == 0)
19144 it->ascent += face->box_line_width;
19145 if (slice.y + slice.height == img->height)
19146 it->descent += face->box_line_width;
19147 }
19148
19149 if (it->start_of_box_run_p && slice.x == 0)
19150 it->pixel_width += abs (face->box_line_width);
19151 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
19152 it->pixel_width += abs (face->box_line_width);
19153 }
19154
19155 take_vertical_position_into_account (it);
19156
19157 if (it->glyph_row)
19158 {
19159 struct glyph *glyph;
19160 enum glyph_row_area area = it->area;
19161
19162 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
19163 if (glyph < it->glyph_row->glyphs[area + 1])
19164 {
19165 glyph->charpos = CHARPOS (it->position);
19166 glyph->object = it->object;
19167 glyph->pixel_width = it->pixel_width;
19168 glyph->ascent = glyph_ascent;
19169 glyph->descent = it->descent;
19170 glyph->voffset = it->voffset;
19171 glyph->type = IMAGE_GLYPH;
19172 glyph->multibyte_p = it->multibyte_p;
19173 glyph->left_box_line_p = it->start_of_box_run_p;
19174 glyph->right_box_line_p = it->end_of_box_run_p;
19175 glyph->overlaps_vertically_p = 0;
19176 glyph->padding_p = 0;
19177 glyph->glyph_not_available_p = 0;
19178 glyph->face_id = it->face_id;
19179 glyph->u.img_id = img->id;
19180 glyph->slice = slice;
19181 glyph->font_type = FONT_TYPE_UNKNOWN;
19182 ++it->glyph_row->used[area];
19183 }
19184 else
19185 IT_EXPAND_MATRIX_WIDTH (it, area);
19186 }
19187 }
19188
19189
19190 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
19191 of the glyph, WIDTH and HEIGHT are the width and height of the
19192 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
19193
19194 static void
19195 append_stretch_glyph (it, object, width, height, ascent)
19196 struct it *it;
19197 Lisp_Object object;
19198 int width, height;
19199 int ascent;
19200 {
19201 struct glyph *glyph;
19202 enum glyph_row_area area = it->area;
19203
19204 xassert (ascent >= 0 && ascent <= height);
19205
19206 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
19207 if (glyph < it->glyph_row->glyphs[area + 1])
19208 {
19209 glyph->charpos = CHARPOS (it->position);
19210 glyph->object = object;
19211 glyph->pixel_width = width;
19212 glyph->ascent = ascent;
19213 glyph->descent = height - ascent;
19214 glyph->voffset = it->voffset;
19215 glyph->type = STRETCH_GLYPH;
19216 glyph->multibyte_p = it->multibyte_p;
19217 glyph->left_box_line_p = it->start_of_box_run_p;
19218 glyph->right_box_line_p = it->end_of_box_run_p;
19219 glyph->overlaps_vertically_p = 0;
19220 glyph->padding_p = 0;
19221 glyph->glyph_not_available_p = 0;
19222 glyph->face_id = it->face_id;
19223 glyph->u.stretch.ascent = ascent;
19224 glyph->u.stretch.height = height;
19225 glyph->slice = null_glyph_slice;
19226 glyph->font_type = FONT_TYPE_UNKNOWN;
19227 ++it->glyph_row->used[area];
19228 }
19229 else
19230 IT_EXPAND_MATRIX_WIDTH (it, area);
19231 }
19232
19233
19234 /* Produce a stretch glyph for iterator IT. IT->object is the value
19235 of the glyph property displayed. The value must be a list
19236 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
19237 being recognized:
19238
19239 1. `:width WIDTH' specifies that the space should be WIDTH *
19240 canonical char width wide. WIDTH may be an integer or floating
19241 point number.
19242
19243 2. `:relative-width FACTOR' specifies that the width of the stretch
19244 should be computed from the width of the first character having the
19245 `glyph' property, and should be FACTOR times that width.
19246
19247 3. `:align-to HPOS' specifies that the space should be wide enough
19248 to reach HPOS, a value in canonical character units.
19249
19250 Exactly one of the above pairs must be present.
19251
19252 4. `:height HEIGHT' specifies that the height of the stretch produced
19253 should be HEIGHT, measured in canonical character units.
19254
19255 5. `:relative-height FACTOR' specifies that the height of the
19256 stretch should be FACTOR times the height of the characters having
19257 the glyph property.
19258
19259 Either none or exactly one of 4 or 5 must be present.
19260
19261 6. `:ascent ASCENT' specifies that ASCENT percent of the height
19262 of the stretch should be used for the ascent of the stretch.
19263 ASCENT must be in the range 0 <= ASCENT <= 100. */
19264
19265 static void
19266 produce_stretch_glyph (it)
19267 struct it *it;
19268 {
19269 /* (space :width WIDTH :height HEIGHT ...) */
19270 Lisp_Object prop, plist;
19271 int width = 0, height = 0, align_to = -1;
19272 int zero_width_ok_p = 0, zero_height_ok_p = 0;
19273 int ascent = 0;
19274 double tem;
19275 struct face *face = FACE_FROM_ID (it->f, it->face_id);
19276 XFontStruct *font = face->font ? face->font : FRAME_FONT (it->f);
19277
19278 PREPARE_FACE_FOR_DISPLAY (it->f, face);
19279
19280 /* List should start with `space'. */
19281 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
19282 plist = XCDR (it->object);
19283
19284 /* Compute the width of the stretch. */
19285 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
19286 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
19287 {
19288 /* Absolute width `:width WIDTH' specified and valid. */
19289 zero_width_ok_p = 1;
19290 width = (int)tem;
19291 }
19292 else if (prop = Fplist_get (plist, QCrelative_width),
19293 NUMVAL (prop) > 0)
19294 {
19295 /* Relative width `:relative-width FACTOR' specified and valid.
19296 Compute the width of the characters having the `glyph'
19297 property. */
19298 struct it it2;
19299 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
19300
19301 it2 = *it;
19302 if (it->multibyte_p)
19303 {
19304 int maxlen = ((IT_BYTEPOS (*it) >= GPT ? ZV : GPT)
19305 - IT_BYTEPOS (*it));
19306 it2.c = STRING_CHAR_AND_LENGTH (p, maxlen, it2.len);
19307 }
19308 else
19309 it2.c = *p, it2.len = 1;
19310
19311 it2.glyph_row = NULL;
19312 it2.what = IT_CHARACTER;
19313 x_produce_glyphs (&it2);
19314 width = NUMVAL (prop) * it2.pixel_width;
19315 }
19316 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
19317 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
19318 {
19319 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
19320 align_to = (align_to < 0
19321 ? 0
19322 : align_to - window_box_left_offset (it->w, TEXT_AREA));
19323 else if (align_to < 0)
19324 align_to = window_box_left_offset (it->w, TEXT_AREA);
19325 width = max (0, (int)tem + align_to - it->current_x);
19326 zero_width_ok_p = 1;
19327 }
19328 else
19329 /* Nothing specified -> width defaults to canonical char width. */
19330 width = FRAME_COLUMN_WIDTH (it->f);
19331
19332 if (width <= 0 && (width < 0 || !zero_width_ok_p))
19333 width = 1;
19334
19335 /* Compute height. */
19336 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
19337 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
19338 {
19339 height = (int)tem;
19340 zero_height_ok_p = 1;
19341 }
19342 else if (prop = Fplist_get (plist, QCrelative_height),
19343 NUMVAL (prop) > 0)
19344 height = FONT_HEIGHT (font) * NUMVAL (prop);
19345 else
19346 height = FONT_HEIGHT (font);
19347
19348 if (height <= 0 && (height < 0 || !zero_height_ok_p))
19349 height = 1;
19350
19351 /* Compute percentage of height used for ascent. If
19352 `:ascent ASCENT' is present and valid, use that. Otherwise,
19353 derive the ascent from the font in use. */
19354 if (prop = Fplist_get (plist, QCascent),
19355 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
19356 ascent = height * NUMVAL (prop) / 100.0;
19357 else if (!NILP (prop)
19358 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
19359 ascent = min (max (0, (int)tem), height);
19360 else
19361 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
19362
19363 if (width > 0 && height > 0 && it->glyph_row)
19364 {
19365 Lisp_Object object = it->stack[it->sp - 1].string;
19366 if (!STRINGP (object))
19367 object = it->w->buffer;
19368 append_stretch_glyph (it, object, width, height, ascent);
19369 }
19370
19371 it->pixel_width = width;
19372 it->ascent = it->phys_ascent = ascent;
19373 it->descent = it->phys_descent = height - it->ascent;
19374 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
19375
19376 if (width > 0 && height > 0 && face->box != FACE_NO_BOX)
19377 {
19378 if (face->box_line_width > 0)
19379 {
19380 it->ascent += face->box_line_width;
19381 it->descent += face->box_line_width;
19382 }
19383
19384 if (it->start_of_box_run_p)
19385 it->pixel_width += abs (face->box_line_width);
19386 if (it->end_of_box_run_p)
19387 it->pixel_width += abs (face->box_line_width);
19388 }
19389
19390 take_vertical_position_into_account (it);
19391 }
19392
19393 /* Get line-height and line-spacing property at point.
19394 If line-height has format (HEIGHT TOTAL), return TOTAL
19395 in TOTAL_HEIGHT. */
19396
19397 static Lisp_Object
19398 get_line_height_property (it, prop)
19399 struct it *it;
19400 Lisp_Object prop;
19401 {
19402 Lisp_Object position;
19403
19404 if (STRINGP (it->object))
19405 position = make_number (IT_STRING_CHARPOS (*it));
19406 else if (BUFFERP (it->object))
19407 position = make_number (IT_CHARPOS (*it));
19408 else
19409 return Qnil;
19410
19411 return Fget_char_property (position, prop, it->object);
19412 }
19413
19414 /* Calculate line-height and line-spacing properties.
19415 An integer value specifies explicit pixel value.
19416 A float value specifies relative value to current face height.
19417 A cons (float . face-name) specifies relative value to
19418 height of specified face font.
19419
19420 Returns height in pixels, or nil. */
19421
19422
19423 static Lisp_Object
19424 calc_line_height_property (it, val, font, boff, override)
19425 struct it *it;
19426 Lisp_Object val;
19427 XFontStruct *font;
19428 int boff, override;
19429 {
19430 Lisp_Object face_name = Qnil;
19431 int ascent, descent, height;
19432
19433 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
19434 return val;
19435
19436 if (CONSP (val))
19437 {
19438 face_name = XCAR (val);
19439 val = XCDR (val);
19440 if (!NUMBERP (val))
19441 val = make_number (1);
19442 if (NILP (face_name))
19443 {
19444 height = it->ascent + it->descent;
19445 goto scale;
19446 }
19447 }
19448
19449 if (NILP (face_name))
19450 {
19451 font = FRAME_FONT (it->f);
19452 boff = FRAME_BASELINE_OFFSET (it->f);
19453 }
19454 else if (EQ (face_name, Qt))
19455 {
19456 override = 0;
19457 }
19458 else
19459 {
19460 int face_id;
19461 struct face *face;
19462 struct font_info *font_info;
19463
19464 face_id = lookup_named_face (it->f, face_name, 0);
19465 if (face_id < 0)
19466 return make_number (-1);
19467
19468 face = FACE_FROM_ID (it->f, face_id);
19469 font = face->font;
19470 if (font == NULL)
19471 return make_number (-1);
19472
19473 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
19474 boff = font_info->baseline_offset;
19475 if (font_info->vertical_centering)
19476 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
19477 }
19478
19479 ascent = FONT_BASE (font) + boff;
19480 descent = FONT_DESCENT (font) - boff;
19481
19482 if (override)
19483 {
19484 it->override_ascent = ascent;
19485 it->override_descent = descent;
19486 it->override_boff = boff;
19487 }
19488
19489 height = ascent + descent;
19490
19491 scale:
19492 if (FLOATP (val))
19493 height = (int)(XFLOAT_DATA (val) * height);
19494 else if (INTEGERP (val))
19495 height *= XINT (val);
19496
19497 return make_number (height);
19498 }
19499
19500
19501 /* RIF:
19502 Produce glyphs/get display metrics for the display element IT is
19503 loaded with. See the description of struct display_iterator in
19504 dispextern.h for an overview of struct display_iterator. */
19505
19506 void
19507 x_produce_glyphs (it)
19508 struct it *it;
19509 {
19510 int extra_line_spacing = it->extra_line_spacing;
19511
19512 it->glyph_not_available_p = 0;
19513
19514 if (it->what == IT_CHARACTER)
19515 {
19516 XChar2b char2b;
19517 XFontStruct *font;
19518 struct face *face = FACE_FROM_ID (it->f, it->face_id);
19519 XCharStruct *pcm;
19520 int font_not_found_p;
19521 struct font_info *font_info;
19522 int boff; /* baseline offset */
19523 /* We may change it->multibyte_p upon unibyte<->multibyte
19524 conversion. So, save the current value now and restore it
19525 later.
19526
19527 Note: It seems that we don't have to record multibyte_p in
19528 struct glyph because the character code itself tells if or
19529 not the character is multibyte. Thus, in the future, we must
19530 consider eliminating the field `multibyte_p' in the struct
19531 glyph. */
19532 int saved_multibyte_p = it->multibyte_p;
19533
19534 /* Maybe translate single-byte characters to multibyte, or the
19535 other way. */
19536 it->char_to_display = it->c;
19537 if (!ASCII_BYTE_P (it->c)
19538 && ! it->multibyte_p)
19539 {
19540 if (SINGLE_BYTE_CHAR_P (it->c)
19541 && unibyte_display_via_language_environment)
19542 it->char_to_display = unibyte_char_to_multibyte (it->c);
19543 if (! SINGLE_BYTE_CHAR_P (it->c))
19544 {
19545 it->multibyte_p = 1;
19546 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display,
19547 -1, Qnil);
19548 face = FACE_FROM_ID (it->f, it->face_id);
19549 }
19550 }
19551
19552 /* Get font to use. Encode IT->char_to_display. */
19553 get_char_face_and_encoding (it->f, it->char_to_display, it->face_id,
19554 &char2b, it->multibyte_p, 0);
19555 font = face->font;
19556
19557 /* When no suitable font found, use the default font. */
19558 font_not_found_p = font == NULL;
19559 if (font_not_found_p)
19560 {
19561 font = FRAME_FONT (it->f);
19562 boff = FRAME_BASELINE_OFFSET (it->f);
19563 font_info = NULL;
19564 }
19565 else
19566 {
19567 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
19568 boff = font_info->baseline_offset;
19569 if (font_info->vertical_centering)
19570 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
19571 }
19572
19573 if (it->char_to_display >= ' '
19574 && (!it->multibyte_p || it->char_to_display < 128))
19575 {
19576 /* Either unibyte or ASCII. */
19577 int stretched_p;
19578
19579 it->nglyphs = 1;
19580
19581 pcm = rif->per_char_metric (font, &char2b,
19582 FONT_TYPE_FOR_UNIBYTE (font, it->char_to_display));
19583
19584 if (it->override_ascent >= 0)
19585 {
19586 it->ascent = it->override_ascent;
19587 it->descent = it->override_descent;
19588 boff = it->override_boff;
19589 }
19590 else
19591 {
19592 it->ascent = FONT_BASE (font) + boff;
19593 it->descent = FONT_DESCENT (font) - boff;
19594 }
19595
19596 if (pcm)
19597 {
19598 it->phys_ascent = pcm->ascent + boff;
19599 it->phys_descent = pcm->descent - boff;
19600 it->pixel_width = pcm->width;
19601 }
19602 else
19603 {
19604 it->glyph_not_available_p = 1;
19605 it->phys_ascent = it->ascent;
19606 it->phys_descent = it->descent;
19607 it->pixel_width = FONT_WIDTH (font);
19608 }
19609
19610 if (it->constrain_row_ascent_descent_p)
19611 {
19612 if (it->descent > it->max_descent)
19613 {
19614 it->ascent += it->descent - it->max_descent;
19615 it->descent = it->max_descent;
19616 }
19617 if (it->ascent > it->max_ascent)
19618 {
19619 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
19620 it->ascent = it->max_ascent;
19621 }
19622 it->phys_ascent = min (it->phys_ascent, it->ascent);
19623 it->phys_descent = min (it->phys_descent, it->descent);
19624 extra_line_spacing = 0;
19625 }
19626
19627 /* If this is a space inside a region of text with
19628 `space-width' property, change its width. */
19629 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
19630 if (stretched_p)
19631 it->pixel_width *= XFLOATINT (it->space_width);
19632
19633 /* If face has a box, add the box thickness to the character
19634 height. If character has a box line to the left and/or
19635 right, add the box line width to the character's width. */
19636 if (face->box != FACE_NO_BOX)
19637 {
19638 int thick = face->box_line_width;
19639
19640 if (thick > 0)
19641 {
19642 it->ascent += thick;
19643 it->descent += thick;
19644 }
19645 else
19646 thick = -thick;
19647
19648 if (it->start_of_box_run_p)
19649 it->pixel_width += thick;
19650 if (it->end_of_box_run_p)
19651 it->pixel_width += thick;
19652 }
19653
19654 /* If face has an overline, add the height of the overline
19655 (1 pixel) and a 1 pixel margin to the character height. */
19656 if (face->overline_p)
19657 it->ascent += 2;
19658
19659 if (it->constrain_row_ascent_descent_p)
19660 {
19661 if (it->ascent > it->max_ascent)
19662 it->ascent = it->max_ascent;
19663 if (it->descent > it->max_descent)
19664 it->descent = it->max_descent;
19665 }
19666
19667 take_vertical_position_into_account (it);
19668
19669 /* If we have to actually produce glyphs, do it. */
19670 if (it->glyph_row)
19671 {
19672 if (stretched_p)
19673 {
19674 /* Translate a space with a `space-width' property
19675 into a stretch glyph. */
19676 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
19677 / FONT_HEIGHT (font));
19678 append_stretch_glyph (it, it->object, it->pixel_width,
19679 it->ascent + it->descent, ascent);
19680 }
19681 else
19682 append_glyph (it);
19683
19684 /* If characters with lbearing or rbearing are displayed
19685 in this line, record that fact in a flag of the
19686 glyph row. This is used to optimize X output code. */
19687 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
19688 it->glyph_row->contains_overlapping_glyphs_p = 1;
19689 }
19690 }
19691 else if (it->char_to_display == '\n')
19692 {
19693 /* A newline has no width but we need the height of the line.
19694 But if previous part of the line set a height, don't
19695 increase that height */
19696
19697 Lisp_Object height;
19698 Lisp_Object total_height = Qnil;
19699
19700 it->override_ascent = -1;
19701 it->pixel_width = 0;
19702 it->nglyphs = 0;
19703
19704 height = get_line_height_property(it, Qline_height);
19705 /* Split (line-height total-height) list */
19706 if (CONSP (height)
19707 && CONSP (XCDR (height))
19708 && NILP (XCDR (XCDR (height))))
19709 {
19710 total_height = XCAR (XCDR (height));
19711 height = XCAR (height);
19712 }
19713 height = calc_line_height_property(it, height, font, boff, 1);
19714
19715 if (it->override_ascent >= 0)
19716 {
19717 it->ascent = it->override_ascent;
19718 it->descent = it->override_descent;
19719 boff = it->override_boff;
19720 }
19721 else
19722 {
19723 it->ascent = FONT_BASE (font) + boff;
19724 it->descent = FONT_DESCENT (font) - boff;
19725 }
19726
19727 if (EQ (height, Qt))
19728 {
19729 if (it->descent > it->max_descent)
19730 {
19731 it->ascent += it->descent - it->max_descent;
19732 it->descent = it->max_descent;
19733 }
19734 if (it->ascent > it->max_ascent)
19735 {
19736 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
19737 it->ascent = it->max_ascent;
19738 }
19739 it->phys_ascent = min (it->phys_ascent, it->ascent);
19740 it->phys_descent = min (it->phys_descent, it->descent);
19741 it->constrain_row_ascent_descent_p = 1;
19742 extra_line_spacing = 0;
19743 }
19744 else
19745 {
19746 Lisp_Object spacing;
19747
19748 it->phys_ascent = it->ascent;
19749 it->phys_descent = it->descent;
19750
19751 if ((it->max_ascent > 0 || it->max_descent > 0)
19752 && face->box != FACE_NO_BOX
19753 && face->box_line_width > 0)
19754 {
19755 it->ascent += face->box_line_width;
19756 it->descent += face->box_line_width;
19757 }
19758 if (!NILP (height)
19759 && XINT (height) > it->ascent + it->descent)
19760 it->ascent = XINT (height) - it->descent;
19761
19762 if (!NILP (total_height))
19763 spacing = calc_line_height_property(it, total_height, font, boff, 0);
19764 else
19765 {
19766 spacing = get_line_height_property(it, Qline_spacing);
19767 spacing = calc_line_height_property(it, spacing, font, boff, 0);
19768 }
19769 if (INTEGERP (spacing))
19770 {
19771 extra_line_spacing = XINT (spacing);
19772 if (!NILP (total_height))
19773 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
19774 }
19775 }
19776 }
19777 else if (it->char_to_display == '\t')
19778 {
19779 int tab_width = it->tab_width * FRAME_SPACE_WIDTH (it->f);
19780 int x = it->current_x + it->continuation_lines_width;
19781 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
19782
19783 /* If the distance from the current position to the next tab
19784 stop is less than a space character width, use the
19785 tab stop after that. */
19786 if (next_tab_x - x < FRAME_SPACE_WIDTH (it->f))
19787 next_tab_x += tab_width;
19788
19789 it->pixel_width = next_tab_x - x;
19790 it->nglyphs = 1;
19791 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
19792 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
19793
19794 if (it->glyph_row)
19795 {
19796 append_stretch_glyph (it, it->object, it->pixel_width,
19797 it->ascent + it->descent, it->ascent);
19798 }
19799 }
19800 else
19801 {
19802 /* A multi-byte character. Assume that the display width of the
19803 character is the width of the character multiplied by the
19804 width of the font. */
19805
19806 /* If we found a font, this font should give us the right
19807 metrics. If we didn't find a font, use the frame's
19808 default font and calculate the width of the character by
19809 multiplying the width of font by the width of the
19810 character. */
19811
19812 pcm = rif->per_char_metric (font, &char2b,
19813 FONT_TYPE_FOR_MULTIBYTE (font, it->c));
19814
19815 if (font_not_found_p || !pcm)
19816 {
19817 it->glyph_not_available_p = 1;
19818 it->pixel_width = (FRAME_COLUMN_WIDTH (it->f)
19819 * CHAR_WIDTH (it->char_to_display));
19820 it->phys_ascent = FONT_BASE (font) + boff;
19821 it->phys_descent = FONT_DESCENT (font) - boff;
19822 }
19823 else
19824 {
19825 it->pixel_width = pcm->width;
19826 it->phys_ascent = pcm->ascent + boff;
19827 it->phys_descent = pcm->descent - boff;
19828 if (it->glyph_row
19829 && (pcm->lbearing < 0
19830 || pcm->rbearing > pcm->width))
19831 it->glyph_row->contains_overlapping_glyphs_p = 1;
19832 }
19833 it->nglyphs = 1;
19834 it->ascent = FONT_BASE (font) + boff;
19835 it->descent = FONT_DESCENT (font) - boff;
19836 if (face->box != FACE_NO_BOX)
19837 {
19838 int thick = face->box_line_width;
19839
19840 if (thick > 0)
19841 {
19842 it->ascent += thick;
19843 it->descent += thick;
19844 }
19845 else
19846 thick = - thick;
19847
19848 if (it->start_of_box_run_p)
19849 it->pixel_width += thick;
19850 if (it->end_of_box_run_p)
19851 it->pixel_width += thick;
19852 }
19853
19854 /* If face has an overline, add the height of the overline
19855 (1 pixel) and a 1 pixel margin to the character height. */
19856 if (face->overline_p)
19857 it->ascent += 2;
19858
19859 take_vertical_position_into_account (it);
19860
19861 if (it->glyph_row)
19862 append_glyph (it);
19863 }
19864 it->multibyte_p = saved_multibyte_p;
19865 }
19866 else if (it->what == IT_COMPOSITION)
19867 {
19868 /* Note: A composition is represented as one glyph in the
19869 glyph matrix. There are no padding glyphs. */
19870 XChar2b char2b;
19871 XFontStruct *font;
19872 struct face *face = FACE_FROM_ID (it->f, it->face_id);
19873 XCharStruct *pcm;
19874 int font_not_found_p;
19875 struct font_info *font_info;
19876 int boff; /* baseline offset */
19877 struct composition *cmp = composition_table[it->cmp_id];
19878 int pos;
19879
19880 /* Maybe translate single-byte characters to multibyte. */
19881 it->char_to_display = it->c;
19882 if (unibyte_display_via_language_environment
19883 && it->c >= 0200)
19884 {
19885 it->char_to_display = unibyte_char_to_multibyte (it->c);
19886 }
19887
19888 /* Get face and font to use. Encode IT->char_to_display. */
19889 pos = STRINGP (it->string) ? IT_STRING_CHARPOS (*it) : IT_CHARPOS (*it);
19890 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display,
19891 pos, it->string);
19892 face = FACE_FROM_ID (it->f, it->face_id);
19893 get_char_face_and_encoding (it->f, it->char_to_display, it->face_id,
19894 &char2b, it->multibyte_p, 0);
19895 font = face->font;
19896
19897 /* When no suitable font found, use the default font. */
19898 font_not_found_p = font == NULL;
19899 if (font_not_found_p)
19900 {
19901 font = FRAME_FONT (it->f);
19902 boff = FRAME_BASELINE_OFFSET (it->f);
19903 font_info = NULL;
19904 }
19905 else
19906 {
19907 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
19908 boff = font_info->baseline_offset;
19909 if (font_info->vertical_centering)
19910 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
19911 }
19912
19913 /* There are no padding glyphs, so there is only one glyph to
19914 produce for the composition. Important is that pixel_width,
19915 ascent and descent are the values of what is drawn by
19916 draw_glyphs (i.e. the values of the overall glyphs composed). */
19917 it->nglyphs = 1;
19918
19919 /* If we have not yet calculated pixel size data of glyphs of
19920 the composition for the current face font, calculate them
19921 now. Theoretically, we have to check all fonts for the
19922 glyphs, but that requires much time and memory space. So,
19923 here we check only the font of the first glyph. This leads
19924 to incorrect display, but it's very rare, and C-l (recenter)
19925 can correct the display anyway. */
19926 if (cmp->glyph_len == 0)
19927 {
19928 cmp->lbearing = cmp->rbearing = 0;
19929 cmp->pixel_width = cmp->ascent = cmp->descent = 0;
19930 }
19931 else if (cmp->font != (void *) font)
19932 {
19933 /* Ascent and descent of the font of the first character of
19934 this composition (adjusted by baseline offset). Ascent
19935 and descent of overall glyphs should not be less than
19936 them respectively. */
19937 int font_ascent = FONT_BASE (font) + boff;
19938 int font_descent = FONT_DESCENT (font) - boff;
19939 int font_height = FONT_HEIGHT (font);
19940 /* Bounding box of the overall glyphs. */
19941 int leftmost, rightmost, lowest, highest;
19942 int lbearing, rbearing;
19943 int i, width, ascent, descent;
19944
19945 cmp->font = (void *) font;
19946
19947 /* Initialize the bounding box. */
19948 if (font_info
19949 && (pcm = rif->per_char_metric (font, &char2b,
19950 FONT_TYPE_FOR_MULTIBYTE (font, it->c))))
19951 {
19952 width = pcm->width;
19953 ascent = pcm->ascent;
19954 descent = pcm->descent;
19955 lbearing = pcm->lbearing;
19956 if (lbearing > 0)
19957 lbearing = 0;
19958 rbearing = pcm->rbearing;
19959 if (rbearing < width)
19960 rbearing = width;
19961 }
19962 else
19963 {
19964 width = FONT_WIDTH (font);
19965 ascent = FONT_BASE (font);
19966 descent = FONT_DESCENT (font);
19967 lbearing = 0;
19968 rbearing = width;
19969 }
19970
19971 rightmost = width;
19972 lowest = - descent + boff;
19973 highest = ascent + boff;
19974 leftmost = 0;
19975
19976 if (font_info
19977 && font_info->default_ascent
19978 && CHAR_TABLE_P (Vuse_default_ascent)
19979 && !NILP (Faref (Vuse_default_ascent,
19980 make_number (it->char_to_display))))
19981 highest = font_info->default_ascent + boff;
19982
19983 /* Draw the first glyph at the normal position. It may be
19984 shifted to right later if some other glyphs are drawn at
19985 the left. */
19986 cmp->offsets[0] = 0;
19987 cmp->offsets[1] = boff;
19988 cmp->lbearing = lbearing;
19989 cmp->rbearing = rbearing;
19990
19991 /* Set cmp->offsets for the remaining glyphs. */
19992 for (i = 1; i < cmp->glyph_len; i++)
19993 {
19994 int left, right, btm, top;
19995 int ch = COMPOSITION_GLYPH (cmp, i);
19996 int face_id = FACE_FOR_CHAR (it->f, face, ch, pos, it->string);
19997
19998 face = FACE_FROM_ID (it->f, face_id);
19999 get_char_face_and_encoding (it->f, ch, face->id,
20000 &char2b, it->multibyte_p, 0);
20001 font = face->font;
20002 if (font == NULL)
20003 {
20004 font = FRAME_FONT (it->f);
20005 boff = FRAME_BASELINE_OFFSET (it->f);
20006 font_info = NULL;
20007 }
20008 else
20009 {
20010 font_info
20011 = FONT_INFO_FROM_ID (it->f, face->font_info_id);
20012 boff = font_info->baseline_offset;
20013 if (font_info->vertical_centering)
20014 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
20015 }
20016
20017 if (font_info
20018 && (pcm = rif->per_char_metric (font, &char2b,
20019 FONT_TYPE_FOR_MULTIBYTE (font, ch))))
20020 {
20021 width = pcm->width;
20022 ascent = pcm->ascent;
20023 descent = pcm->descent;
20024 lbearing = pcm->lbearing;
20025 if (lbearing > 0)
20026 lbearing = 0;
20027 rbearing = pcm->rbearing;
20028 if (rbearing < width)
20029 rbearing = width;
20030 }
20031 else
20032 {
20033 width = FONT_WIDTH (font);
20034 ascent = 1;
20035 descent = 0;
20036 lbearing = 0;
20037 rbearing = width;
20038 }
20039
20040 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
20041 {
20042 /* Relative composition with or without
20043 alternate chars. */
20044 left = (leftmost + rightmost - width) / 2;
20045 btm = - descent + boff;
20046 if (font_info && font_info->relative_compose
20047 && (! CHAR_TABLE_P (Vignore_relative_composition)
20048 || NILP (Faref (Vignore_relative_composition,
20049 make_number (ch)))))
20050 {
20051
20052 if (- descent >= font_info->relative_compose)
20053 /* One extra pixel between two glyphs. */
20054 btm = highest + 1;
20055 else if (ascent <= 0)
20056 /* One extra pixel between two glyphs. */
20057 btm = lowest - 1 - ascent - descent;
20058 }
20059 }
20060 else
20061 {
20062 /* A composition rule is specified by an integer
20063 value that encodes global and new reference
20064 points (GREF and NREF). GREF and NREF are
20065 specified by numbers as below:
20066
20067 0---1---2 -- ascent
20068 | |
20069 | |
20070 | |
20071 9--10--11 -- center
20072 | |
20073 ---3---4---5--- baseline
20074 | |
20075 6---7---8 -- descent
20076 */
20077 int rule = COMPOSITION_RULE (cmp, i);
20078 int gref, nref, grefx, grefy, nrefx, nrefy, xoff, yoff;
20079
20080 COMPOSITION_DECODE_RULE (rule, gref, nref, xoff, yoff);
20081 grefx = gref % 3, nrefx = nref % 3;
20082 grefy = gref / 3, nrefy = nref / 3;
20083 if (xoff)
20084 xoff = font_height * (xoff - 128) / 256;
20085 if (yoff)
20086 yoff = font_height * (yoff - 128) / 256;
20087
20088 left = (leftmost
20089 + grefx * (rightmost - leftmost) / 2
20090 - nrefx * width / 2
20091 + xoff);
20092
20093 btm = ((grefy == 0 ? highest
20094 : grefy == 1 ? 0
20095 : grefy == 2 ? lowest
20096 : (highest + lowest) / 2)
20097 - (nrefy == 0 ? ascent + descent
20098 : nrefy == 1 ? descent - boff
20099 : nrefy == 2 ? 0
20100 : (ascent + descent) / 2)
20101 + yoff);
20102 }
20103
20104 cmp->offsets[i * 2] = left;
20105 cmp->offsets[i * 2 + 1] = btm + descent;
20106
20107 /* Update the bounding box of the overall glyphs. */
20108 if (width > 0)
20109 {
20110 right = left + width;
20111 if (left < leftmost)
20112 leftmost = left;
20113 if (right > rightmost)
20114 rightmost = right;
20115 }
20116 top = btm + descent + ascent;
20117 if (top > highest)
20118 highest = top;
20119 if (btm < lowest)
20120 lowest = btm;
20121
20122 if (cmp->lbearing > left + lbearing)
20123 cmp->lbearing = left + lbearing;
20124 if (cmp->rbearing < left + rbearing)
20125 cmp->rbearing = left + rbearing;
20126 }
20127
20128 /* If there are glyphs whose x-offsets are negative,
20129 shift all glyphs to the right and make all x-offsets
20130 non-negative. */
20131 if (leftmost < 0)
20132 {
20133 for (i = 0; i < cmp->glyph_len; i++)
20134 cmp->offsets[i * 2] -= leftmost;
20135 rightmost -= leftmost;
20136 cmp->lbearing -= leftmost;
20137 cmp->rbearing -= leftmost;
20138 }
20139
20140 cmp->pixel_width = rightmost;
20141 cmp->ascent = highest;
20142 cmp->descent = - lowest;
20143 if (cmp->ascent < font_ascent)
20144 cmp->ascent = font_ascent;
20145 if (cmp->descent < font_descent)
20146 cmp->descent = font_descent;
20147 }
20148
20149 if (it->glyph_row
20150 && (cmp->lbearing < 0
20151 || cmp->rbearing > cmp->pixel_width))
20152 it->glyph_row->contains_overlapping_glyphs_p = 1;
20153
20154 it->pixel_width = cmp->pixel_width;
20155 it->ascent = it->phys_ascent = cmp->ascent;
20156 it->descent = it->phys_descent = cmp->descent;
20157
20158 if (face->box != FACE_NO_BOX)
20159 {
20160 int thick = face->box_line_width;
20161
20162 if (thick > 0)
20163 {
20164 it->ascent += thick;
20165 it->descent += thick;
20166 }
20167 else
20168 thick = - thick;
20169
20170 if (it->start_of_box_run_p)
20171 it->pixel_width += thick;
20172 if (it->end_of_box_run_p)
20173 it->pixel_width += thick;
20174 }
20175
20176 /* If face has an overline, add the height of the overline
20177 (1 pixel) and a 1 pixel margin to the character height. */
20178 if (face->overline_p)
20179 it->ascent += 2;
20180
20181 take_vertical_position_into_account (it);
20182
20183 if (it->glyph_row)
20184 append_composite_glyph (it);
20185 }
20186 else if (it->what == IT_IMAGE)
20187 produce_image_glyph (it);
20188 else if (it->what == IT_STRETCH)
20189 produce_stretch_glyph (it);
20190
20191 /* Accumulate dimensions. Note: can't assume that it->descent > 0
20192 because this isn't true for images with `:ascent 100'. */
20193 xassert (it->ascent >= 0 && it->descent >= 0);
20194 if (it->area == TEXT_AREA)
20195 it->current_x += it->pixel_width;
20196
20197 if (extra_line_spacing > 0)
20198 {
20199 it->descent += extra_line_spacing;
20200 if (extra_line_spacing > it->max_extra_line_spacing)
20201 it->max_extra_line_spacing = extra_line_spacing;
20202 }
20203
20204 it->max_ascent = max (it->max_ascent, it->ascent);
20205 it->max_descent = max (it->max_descent, it->descent);
20206 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
20207 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
20208 }
20209
20210 /* EXPORT for RIF:
20211 Output LEN glyphs starting at START at the nominal cursor position.
20212 Advance the nominal cursor over the text. The global variable
20213 updated_window contains the window being updated, updated_row is
20214 the glyph row being updated, and updated_area is the area of that
20215 row being updated. */
20216
20217 void
20218 x_write_glyphs (start, len)
20219 struct glyph *start;
20220 int len;
20221 {
20222 int x, hpos;
20223
20224 xassert (updated_window && updated_row);
20225 BLOCK_INPUT;
20226
20227 /* Write glyphs. */
20228
20229 hpos = start - updated_row->glyphs[updated_area];
20230 x = draw_glyphs (updated_window, output_cursor.x,
20231 updated_row, updated_area,
20232 hpos, hpos + len,
20233 DRAW_NORMAL_TEXT, 0);
20234
20235 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
20236 if (updated_area == TEXT_AREA
20237 && updated_window->phys_cursor_on_p
20238 && updated_window->phys_cursor.vpos == output_cursor.vpos
20239 && updated_window->phys_cursor.hpos >= hpos
20240 && updated_window->phys_cursor.hpos < hpos + len)
20241 updated_window->phys_cursor_on_p = 0;
20242
20243 UNBLOCK_INPUT;
20244
20245 /* Advance the output cursor. */
20246 output_cursor.hpos += len;
20247 output_cursor.x = x;
20248 }
20249
20250
20251 /* EXPORT for RIF:
20252 Insert LEN glyphs from START at the nominal cursor position. */
20253
20254 void
20255 x_insert_glyphs (start, len)
20256 struct glyph *start;
20257 int len;
20258 {
20259 struct frame *f;
20260 struct window *w;
20261 int line_height, shift_by_width, shifted_region_width;
20262 struct glyph_row *row;
20263 struct glyph *glyph;
20264 int frame_x, frame_y;
20265 EMACS_INT hpos;
20266
20267 xassert (updated_window && updated_row);
20268 BLOCK_INPUT;
20269 w = updated_window;
20270 f = XFRAME (WINDOW_FRAME (w));
20271
20272 /* Get the height of the line we are in. */
20273 row = updated_row;
20274 line_height = row->height;
20275
20276 /* Get the width of the glyphs to insert. */
20277 shift_by_width = 0;
20278 for (glyph = start; glyph < start + len; ++glyph)
20279 shift_by_width += glyph->pixel_width;
20280
20281 /* Get the width of the region to shift right. */
20282 shifted_region_width = (window_box_width (w, updated_area)
20283 - output_cursor.x
20284 - shift_by_width);
20285
20286 /* Shift right. */
20287 frame_x = window_box_left (w, updated_area) + output_cursor.x;
20288 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
20289
20290 rif->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
20291 line_height, shift_by_width);
20292
20293 /* Write the glyphs. */
20294 hpos = start - row->glyphs[updated_area];
20295 draw_glyphs (w, output_cursor.x, row, updated_area,
20296 hpos, hpos + len,
20297 DRAW_NORMAL_TEXT, 0);
20298
20299 /* Advance the output cursor. */
20300 output_cursor.hpos += len;
20301 output_cursor.x += shift_by_width;
20302 UNBLOCK_INPUT;
20303 }
20304
20305
20306 /* EXPORT for RIF:
20307 Erase the current text line from the nominal cursor position
20308 (inclusive) to pixel column TO_X (exclusive). The idea is that
20309 everything from TO_X onward is already erased.
20310
20311 TO_X is a pixel position relative to updated_area of
20312 updated_window. TO_X == -1 means clear to the end of this area. */
20313
20314 void
20315 x_clear_end_of_line (to_x)
20316 int to_x;
20317 {
20318 struct frame *f;
20319 struct window *w = updated_window;
20320 int max_x, min_y, max_y;
20321 int from_x, from_y, to_y;
20322
20323 xassert (updated_window && updated_row);
20324 f = XFRAME (w->frame);
20325
20326 if (updated_row->full_width_p)
20327 max_x = WINDOW_TOTAL_WIDTH (w);
20328 else
20329 max_x = window_box_width (w, updated_area);
20330 max_y = window_text_bottom_y (w);
20331
20332 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
20333 of window. For TO_X > 0, truncate to end of drawing area. */
20334 if (to_x == 0)
20335 return;
20336 else if (to_x < 0)
20337 to_x = max_x;
20338 else
20339 to_x = min (to_x, max_x);
20340
20341 to_y = min (max_y, output_cursor.y + updated_row->height);
20342
20343 /* Notice if the cursor will be cleared by this operation. */
20344 if (!updated_row->full_width_p)
20345 notice_overwritten_cursor (w, updated_area,
20346 output_cursor.x, -1,
20347 updated_row->y,
20348 MATRIX_ROW_BOTTOM_Y (updated_row));
20349
20350 from_x = output_cursor.x;
20351
20352 /* Translate to frame coordinates. */
20353 if (updated_row->full_width_p)
20354 {
20355 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
20356 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
20357 }
20358 else
20359 {
20360 int area_left = window_box_left (w, updated_area);
20361 from_x += area_left;
20362 to_x += area_left;
20363 }
20364
20365 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
20366 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
20367 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
20368
20369 /* Prevent inadvertently clearing to end of the X window. */
20370 if (to_x > from_x && to_y > from_y)
20371 {
20372 BLOCK_INPUT;
20373 rif->clear_frame_area (f, from_x, from_y,
20374 to_x - from_x, to_y - from_y);
20375 UNBLOCK_INPUT;
20376 }
20377 }
20378
20379 #endif /* HAVE_WINDOW_SYSTEM */
20380
20381
20382 \f
20383 /***********************************************************************
20384 Cursor types
20385 ***********************************************************************/
20386
20387 /* Value is the internal representation of the specified cursor type
20388 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
20389 of the bar cursor. */
20390
20391 static enum text_cursor_kinds
20392 get_specified_cursor_type (arg, width)
20393 Lisp_Object arg;
20394 int *width;
20395 {
20396 enum text_cursor_kinds type;
20397
20398 if (NILP (arg))
20399 return NO_CURSOR;
20400
20401 if (EQ (arg, Qbox))
20402 return FILLED_BOX_CURSOR;
20403
20404 if (EQ (arg, Qhollow))
20405 return HOLLOW_BOX_CURSOR;
20406
20407 if (EQ (arg, Qbar))
20408 {
20409 *width = 2;
20410 return BAR_CURSOR;
20411 }
20412
20413 if (CONSP (arg)
20414 && EQ (XCAR (arg), Qbar)
20415 && INTEGERP (XCDR (arg))
20416 && XINT (XCDR (arg)) >= 0)
20417 {
20418 *width = XINT (XCDR (arg));
20419 return BAR_CURSOR;
20420 }
20421
20422 if (EQ (arg, Qhbar))
20423 {
20424 *width = 2;
20425 return HBAR_CURSOR;
20426 }
20427
20428 if (CONSP (arg)
20429 && EQ (XCAR (arg), Qhbar)
20430 && INTEGERP (XCDR (arg))
20431 && XINT (XCDR (arg)) >= 0)
20432 {
20433 *width = XINT (XCDR (arg));
20434 return HBAR_CURSOR;
20435 }
20436
20437 /* Treat anything unknown as "hollow box cursor".
20438 It was bad to signal an error; people have trouble fixing
20439 .Xdefaults with Emacs, when it has something bad in it. */
20440 type = HOLLOW_BOX_CURSOR;
20441
20442 return type;
20443 }
20444
20445 /* Set the default cursor types for specified frame. */
20446 void
20447 set_frame_cursor_types (f, arg)
20448 struct frame *f;
20449 Lisp_Object arg;
20450 {
20451 int width;
20452 Lisp_Object tem;
20453
20454 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
20455 FRAME_CURSOR_WIDTH (f) = width;
20456
20457 /* By default, set up the blink-off state depending on the on-state. */
20458
20459 tem = Fassoc (arg, Vblink_cursor_alist);
20460 if (!NILP (tem))
20461 {
20462 FRAME_BLINK_OFF_CURSOR (f)
20463 = get_specified_cursor_type (XCDR (tem), &width);
20464 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
20465 }
20466 else
20467 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
20468 }
20469
20470
20471 /* Return the cursor we want to be displayed in window W. Return
20472 width of bar/hbar cursor through WIDTH arg. Return with
20473 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
20474 (i.e. if the `system caret' should track this cursor).
20475
20476 In a mini-buffer window, we want the cursor only to appear if we
20477 are reading input from this window. For the selected window, we
20478 want the cursor type given by the frame parameter or buffer local
20479 setting of cursor-type. If explicitly marked off, draw no cursor.
20480 In all other cases, we want a hollow box cursor. */
20481
20482 static enum text_cursor_kinds
20483 get_window_cursor_type (w, glyph, width, active_cursor)
20484 struct window *w;
20485 struct glyph *glyph;
20486 int *width;
20487 int *active_cursor;
20488 {
20489 struct frame *f = XFRAME (w->frame);
20490 struct buffer *b = XBUFFER (w->buffer);
20491 int cursor_type = DEFAULT_CURSOR;
20492 Lisp_Object alt_cursor;
20493 int non_selected = 0;
20494
20495 *active_cursor = 1;
20496
20497 /* Echo area */
20498 if (cursor_in_echo_area
20499 && FRAME_HAS_MINIBUF_P (f)
20500 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
20501 {
20502 if (w == XWINDOW (echo_area_window))
20503 {
20504 *width = FRAME_CURSOR_WIDTH (f);
20505 return FRAME_DESIRED_CURSOR (f);
20506 }
20507
20508 *active_cursor = 0;
20509 non_selected = 1;
20510 }
20511
20512 /* Nonselected window or nonselected frame. */
20513 else if (w != XWINDOW (f->selected_window)
20514 #ifdef HAVE_WINDOW_SYSTEM
20515 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame
20516 #endif
20517 )
20518 {
20519 *active_cursor = 0;
20520
20521 if (MINI_WINDOW_P (w) && minibuf_level == 0)
20522 return NO_CURSOR;
20523
20524 non_selected = 1;
20525 }
20526
20527 /* Never display a cursor in a window in which cursor-type is nil. */
20528 if (NILP (b->cursor_type))
20529 return NO_CURSOR;
20530
20531 /* Use cursor-in-non-selected-windows for non-selected window or frame. */
20532 if (non_selected)
20533 {
20534 alt_cursor = XBUFFER (w->buffer)->cursor_in_non_selected_windows;
20535 return get_specified_cursor_type (alt_cursor, width);
20536 }
20537
20538 /* Get the normal cursor type for this window. */
20539 if (EQ (b->cursor_type, Qt))
20540 {
20541 cursor_type = FRAME_DESIRED_CURSOR (f);
20542 *width = FRAME_CURSOR_WIDTH (f);
20543 }
20544 else
20545 cursor_type = get_specified_cursor_type (b->cursor_type, width);
20546
20547 /* Use normal cursor if not blinked off. */
20548 if (!w->cursor_off_p)
20549 {
20550 if (glyph != NULL && glyph->type == IMAGE_GLYPH) {
20551 if (cursor_type == FILLED_BOX_CURSOR)
20552 cursor_type = HOLLOW_BOX_CURSOR;
20553 }
20554 return cursor_type;
20555 }
20556
20557 /* Cursor is blinked off, so determine how to "toggle" it. */
20558
20559 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
20560 if ((alt_cursor = Fassoc (b->cursor_type, Vblink_cursor_alist), !NILP (alt_cursor)))
20561 return get_specified_cursor_type (XCDR (alt_cursor), width);
20562
20563 /* Then see if frame has specified a specific blink off cursor type. */
20564 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
20565 {
20566 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
20567 return FRAME_BLINK_OFF_CURSOR (f);
20568 }
20569
20570 #if 0
20571 /* Some people liked having a permanently visible blinking cursor,
20572 while others had very strong opinions against it. So it was
20573 decided to remove it. KFS 2003-09-03 */
20574
20575 /* Finally perform built-in cursor blinking:
20576 filled box <-> hollow box
20577 wide [h]bar <-> narrow [h]bar
20578 narrow [h]bar <-> no cursor
20579 other type <-> no cursor */
20580
20581 if (cursor_type == FILLED_BOX_CURSOR)
20582 return HOLLOW_BOX_CURSOR;
20583
20584 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
20585 {
20586 *width = 1;
20587 return cursor_type;
20588 }
20589 #endif
20590
20591 return NO_CURSOR;
20592 }
20593
20594
20595 #ifdef HAVE_WINDOW_SYSTEM
20596
20597 /* Notice when the text cursor of window W has been completely
20598 overwritten by a drawing operation that outputs glyphs in AREA
20599 starting at X0 and ending at X1 in the line starting at Y0 and
20600 ending at Y1. X coordinates are area-relative. X1 < 0 means all
20601 the rest of the line after X0 has been written. Y coordinates
20602 are window-relative. */
20603
20604 static void
20605 notice_overwritten_cursor (w, area, x0, x1, y0, y1)
20606 struct window *w;
20607 enum glyph_row_area area;
20608 int x0, y0, x1, y1;
20609 {
20610 int cx0, cx1, cy0, cy1;
20611 struct glyph_row *row;
20612
20613 if (!w->phys_cursor_on_p)
20614 return;
20615 if (area != TEXT_AREA)
20616 return;
20617
20618 if (w->phys_cursor.vpos < 0
20619 || w->phys_cursor.vpos >= w->current_matrix->nrows
20620 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
20621 !(row->enabled_p && row->displays_text_p)))
20622 return;
20623
20624 if (row->cursor_in_fringe_p)
20625 {
20626 row->cursor_in_fringe_p = 0;
20627 draw_fringe_bitmap (w, row, 0);
20628 w->phys_cursor_on_p = 0;
20629 return;
20630 }
20631
20632 cx0 = w->phys_cursor.x;
20633 cx1 = cx0 + w->phys_cursor_width;
20634 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
20635 return;
20636
20637 /* The cursor image will be completely removed from the
20638 screen if the output area intersects the cursor area in
20639 y-direction. When we draw in [y0 y1[, and some part of
20640 the cursor is at y < y0, that part must have been drawn
20641 before. When scrolling, the cursor is erased before
20642 actually scrolling, so we don't come here. When not
20643 scrolling, the rows above the old cursor row must have
20644 changed, and in this case these rows must have written
20645 over the cursor image.
20646
20647 Likewise if part of the cursor is below y1, with the
20648 exception of the cursor being in the first blank row at
20649 the buffer and window end because update_text_area
20650 doesn't draw that row. (Except when it does, but
20651 that's handled in update_text_area.) */
20652
20653 cy0 = w->phys_cursor.y;
20654 cy1 = cy0 + w->phys_cursor_height;
20655 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
20656 return;
20657
20658 w->phys_cursor_on_p = 0;
20659 }
20660
20661 #endif /* HAVE_WINDOW_SYSTEM */
20662
20663 \f
20664 /************************************************************************
20665 Mouse Face
20666 ************************************************************************/
20667
20668 #ifdef HAVE_WINDOW_SYSTEM
20669
20670 /* EXPORT for RIF:
20671 Fix the display of area AREA of overlapping row ROW in window W. */
20672
20673 void
20674 x_fix_overlapping_area (w, row, area)
20675 struct window *w;
20676 struct glyph_row *row;
20677 enum glyph_row_area area;
20678 {
20679 int i, x;
20680
20681 BLOCK_INPUT;
20682
20683 x = 0;
20684 for (i = 0; i < row->used[area];)
20685 {
20686 if (row->glyphs[area][i].overlaps_vertically_p)
20687 {
20688 int start = i, start_x = x;
20689
20690 do
20691 {
20692 x += row->glyphs[area][i].pixel_width;
20693 ++i;
20694 }
20695 while (i < row->used[area]
20696 && row->glyphs[area][i].overlaps_vertically_p);
20697
20698 draw_glyphs (w, start_x, row, area,
20699 start, i,
20700 DRAW_NORMAL_TEXT, 1);
20701 }
20702 else
20703 {
20704 x += row->glyphs[area][i].pixel_width;
20705 ++i;
20706 }
20707 }
20708
20709 UNBLOCK_INPUT;
20710 }
20711
20712
20713 /* EXPORT:
20714 Draw the cursor glyph of window W in glyph row ROW. See the
20715 comment of draw_glyphs for the meaning of HL. */
20716
20717 void
20718 draw_phys_cursor_glyph (w, row, hl)
20719 struct window *w;
20720 struct glyph_row *row;
20721 enum draw_glyphs_face hl;
20722 {
20723 /* If cursor hpos is out of bounds, don't draw garbage. This can
20724 happen in mini-buffer windows when switching between echo area
20725 glyphs and mini-buffer. */
20726 if (w->phys_cursor.hpos < row->used[TEXT_AREA])
20727 {
20728 int on_p = w->phys_cursor_on_p;
20729 int x1;
20730 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA,
20731 w->phys_cursor.hpos, w->phys_cursor.hpos + 1,
20732 hl, 0);
20733 w->phys_cursor_on_p = on_p;
20734
20735 if (hl == DRAW_CURSOR)
20736 w->phys_cursor_width = x1 - w->phys_cursor.x;
20737 /* When we erase the cursor, and ROW is overlapped by other
20738 rows, make sure that these overlapping parts of other rows
20739 are redrawn. */
20740 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
20741 {
20742 if (row > w->current_matrix->rows
20743 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
20744 x_fix_overlapping_area (w, row - 1, TEXT_AREA);
20745
20746 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
20747 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
20748 x_fix_overlapping_area (w, row + 1, TEXT_AREA);
20749 }
20750 }
20751 }
20752
20753
20754 /* EXPORT:
20755 Erase the image of a cursor of window W from the screen. */
20756
20757 void
20758 erase_phys_cursor (w)
20759 struct window *w;
20760 {
20761 struct frame *f = XFRAME (w->frame);
20762 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
20763 int hpos = w->phys_cursor.hpos;
20764 int vpos = w->phys_cursor.vpos;
20765 int mouse_face_here_p = 0;
20766 struct glyph_matrix *active_glyphs = w->current_matrix;
20767 struct glyph_row *cursor_row;
20768 struct glyph *cursor_glyph;
20769 enum draw_glyphs_face hl;
20770
20771 /* No cursor displayed or row invalidated => nothing to do on the
20772 screen. */
20773 if (w->phys_cursor_type == NO_CURSOR)
20774 goto mark_cursor_off;
20775
20776 /* VPOS >= active_glyphs->nrows means that window has been resized.
20777 Don't bother to erase the cursor. */
20778 if (vpos >= active_glyphs->nrows)
20779 goto mark_cursor_off;
20780
20781 /* If row containing cursor is marked invalid, there is nothing we
20782 can do. */
20783 cursor_row = MATRIX_ROW (active_glyphs, vpos);
20784 if (!cursor_row->enabled_p)
20785 goto mark_cursor_off;
20786
20787 /* If line spacing is > 0, old cursor may only be partially visible in
20788 window after split-window. So adjust visible height. */
20789 cursor_row->visible_height = min (cursor_row->visible_height,
20790 window_text_bottom_y (w) - cursor_row->y);
20791
20792 /* If row is completely invisible, don't attempt to delete a cursor which
20793 isn't there. This can happen if cursor is at top of a window, and
20794 we switch to a buffer with a header line in that window. */
20795 if (cursor_row->visible_height <= 0)
20796 goto mark_cursor_off;
20797
20798 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
20799 if (cursor_row->cursor_in_fringe_p)
20800 {
20801 cursor_row->cursor_in_fringe_p = 0;
20802 draw_fringe_bitmap (w, cursor_row, 0);
20803 goto mark_cursor_off;
20804 }
20805
20806 /* This can happen when the new row is shorter than the old one.
20807 In this case, either draw_glyphs or clear_end_of_line
20808 should have cleared the cursor. Note that we wouldn't be
20809 able to erase the cursor in this case because we don't have a
20810 cursor glyph at hand. */
20811 if (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])
20812 goto mark_cursor_off;
20813
20814 /* If the cursor is in the mouse face area, redisplay that when
20815 we clear the cursor. */
20816 if (! NILP (dpyinfo->mouse_face_window)
20817 && w == XWINDOW (dpyinfo->mouse_face_window)
20818 && (vpos > dpyinfo->mouse_face_beg_row
20819 || (vpos == dpyinfo->mouse_face_beg_row
20820 && hpos >= dpyinfo->mouse_face_beg_col))
20821 && (vpos < dpyinfo->mouse_face_end_row
20822 || (vpos == dpyinfo->mouse_face_end_row
20823 && hpos < dpyinfo->mouse_face_end_col))
20824 /* Don't redraw the cursor's spot in mouse face if it is at the
20825 end of a line (on a newline). The cursor appears there, but
20826 mouse highlighting does not. */
20827 && cursor_row->used[TEXT_AREA] > hpos)
20828 mouse_face_here_p = 1;
20829
20830 /* Maybe clear the display under the cursor. */
20831 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
20832 {
20833 int x, y;
20834 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
20835 int width;
20836
20837 cursor_glyph = get_phys_cursor_glyph (w);
20838 if (cursor_glyph == NULL)
20839 goto mark_cursor_off;
20840
20841 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
20842 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
20843 width = min (cursor_glyph->pixel_width,
20844 window_box_width (w, TEXT_AREA) - w->phys_cursor.x);
20845
20846 rif->clear_frame_area (f, x, y, width, cursor_row->visible_height);
20847 }
20848
20849 /* Erase the cursor by redrawing the character underneath it. */
20850 if (mouse_face_here_p)
20851 hl = DRAW_MOUSE_FACE;
20852 else
20853 hl = DRAW_NORMAL_TEXT;
20854 draw_phys_cursor_glyph (w, cursor_row, hl);
20855
20856 mark_cursor_off:
20857 w->phys_cursor_on_p = 0;
20858 w->phys_cursor_type = NO_CURSOR;
20859 }
20860
20861
20862 /* EXPORT:
20863 Display or clear cursor of window W. If ON is zero, clear the
20864 cursor. If it is non-zero, display the cursor. If ON is nonzero,
20865 where to put the cursor is specified by HPOS, VPOS, X and Y. */
20866
20867 void
20868 display_and_set_cursor (w, on, hpos, vpos, x, y)
20869 struct window *w;
20870 int on, hpos, vpos, x, y;
20871 {
20872 struct frame *f = XFRAME (w->frame);
20873 int new_cursor_type;
20874 int new_cursor_width;
20875 int active_cursor;
20876 struct glyph_row *glyph_row;
20877 struct glyph *glyph;
20878
20879 /* This is pointless on invisible frames, and dangerous on garbaged
20880 windows and frames; in the latter case, the frame or window may
20881 be in the midst of changing its size, and x and y may be off the
20882 window. */
20883 if (! FRAME_VISIBLE_P (f)
20884 || FRAME_GARBAGED_P (f)
20885 || vpos >= w->current_matrix->nrows
20886 || hpos >= w->current_matrix->matrix_w)
20887 return;
20888
20889 /* If cursor is off and we want it off, return quickly. */
20890 if (!on && !w->phys_cursor_on_p)
20891 return;
20892
20893 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
20894 /* If cursor row is not enabled, we don't really know where to
20895 display the cursor. */
20896 if (!glyph_row->enabled_p)
20897 {
20898 w->phys_cursor_on_p = 0;
20899 return;
20900 }
20901
20902 glyph = NULL;
20903 if (!glyph_row->exact_window_width_line_p
20904 || hpos < glyph_row->used[TEXT_AREA])
20905 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
20906
20907 xassert (interrupt_input_blocked);
20908
20909 /* Set new_cursor_type to the cursor we want to be displayed. */
20910 new_cursor_type = get_window_cursor_type (w, glyph,
20911 &new_cursor_width, &active_cursor);
20912
20913 /* If cursor is currently being shown and we don't want it to be or
20914 it is in the wrong place, or the cursor type is not what we want,
20915 erase it. */
20916 if (w->phys_cursor_on_p
20917 && (!on
20918 || w->phys_cursor.x != x
20919 || w->phys_cursor.y != y
20920 || new_cursor_type != w->phys_cursor_type
20921 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
20922 && new_cursor_width != w->phys_cursor_width)))
20923 erase_phys_cursor (w);
20924
20925 /* Don't check phys_cursor_on_p here because that flag is only set
20926 to zero in some cases where we know that the cursor has been
20927 completely erased, to avoid the extra work of erasing the cursor
20928 twice. In other words, phys_cursor_on_p can be 1 and the cursor
20929 still not be visible, or it has only been partly erased. */
20930 if (on)
20931 {
20932 w->phys_cursor_ascent = glyph_row->ascent;
20933 w->phys_cursor_height = glyph_row->height;
20934
20935 /* Set phys_cursor_.* before x_draw_.* is called because some
20936 of them may need the information. */
20937 w->phys_cursor.x = x;
20938 w->phys_cursor.y = glyph_row->y;
20939 w->phys_cursor.hpos = hpos;
20940 w->phys_cursor.vpos = vpos;
20941 }
20942
20943 rif->draw_window_cursor (w, glyph_row, x, y,
20944 new_cursor_type, new_cursor_width,
20945 on, active_cursor);
20946 }
20947
20948
20949 /* Switch the display of W's cursor on or off, according to the value
20950 of ON. */
20951
20952 static void
20953 update_window_cursor (w, on)
20954 struct window *w;
20955 int on;
20956 {
20957 /* Don't update cursor in windows whose frame is in the process
20958 of being deleted. */
20959 if (w->current_matrix)
20960 {
20961 BLOCK_INPUT;
20962 display_and_set_cursor (w, on, w->phys_cursor.hpos, w->phys_cursor.vpos,
20963 w->phys_cursor.x, w->phys_cursor.y);
20964 UNBLOCK_INPUT;
20965 }
20966 }
20967
20968
20969 /* Call update_window_cursor with parameter ON_P on all leaf windows
20970 in the window tree rooted at W. */
20971
20972 static void
20973 update_cursor_in_window_tree (w, on_p)
20974 struct window *w;
20975 int on_p;
20976 {
20977 while (w)
20978 {
20979 if (!NILP (w->hchild))
20980 update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
20981 else if (!NILP (w->vchild))
20982 update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
20983 else
20984 update_window_cursor (w, on_p);
20985
20986 w = NILP (w->next) ? 0 : XWINDOW (w->next);
20987 }
20988 }
20989
20990
20991 /* EXPORT:
20992 Display the cursor on window W, or clear it, according to ON_P.
20993 Don't change the cursor's position. */
20994
20995 void
20996 x_update_cursor (f, on_p)
20997 struct frame *f;
20998 int on_p;
20999 {
21000 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
21001 }
21002
21003
21004 /* EXPORT:
21005 Clear the cursor of window W to background color, and mark the
21006 cursor as not shown. This is used when the text where the cursor
21007 is is about to be rewritten. */
21008
21009 void
21010 x_clear_cursor (w)
21011 struct window *w;
21012 {
21013 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
21014 update_window_cursor (w, 0);
21015 }
21016
21017
21018 /* EXPORT:
21019 Display the active region described by mouse_face_* according to DRAW. */
21020
21021 void
21022 show_mouse_face (dpyinfo, draw)
21023 Display_Info *dpyinfo;
21024 enum draw_glyphs_face draw;
21025 {
21026 struct window *w = XWINDOW (dpyinfo->mouse_face_window);
21027 struct frame *f = XFRAME (WINDOW_FRAME (w));
21028
21029 if (/* If window is in the process of being destroyed, don't bother
21030 to do anything. */
21031 w->current_matrix != NULL
21032 /* Don't update mouse highlight if hidden */
21033 && (draw != DRAW_MOUSE_FACE || !dpyinfo->mouse_face_hidden)
21034 /* Recognize when we are called to operate on rows that don't exist
21035 anymore. This can happen when a window is split. */
21036 && dpyinfo->mouse_face_end_row < w->current_matrix->nrows)
21037 {
21038 int phys_cursor_on_p = w->phys_cursor_on_p;
21039 struct glyph_row *row, *first, *last;
21040
21041 first = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_beg_row);
21042 last = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_end_row);
21043
21044 for (row = first; row <= last && row->enabled_p; ++row)
21045 {
21046 int start_hpos, end_hpos, start_x;
21047
21048 /* For all but the first row, the highlight starts at column 0. */
21049 if (row == first)
21050 {
21051 start_hpos = dpyinfo->mouse_face_beg_col;
21052 start_x = dpyinfo->mouse_face_beg_x;
21053 }
21054 else
21055 {
21056 start_hpos = 0;
21057 start_x = 0;
21058 }
21059
21060 if (row == last)
21061 end_hpos = dpyinfo->mouse_face_end_col;
21062 else
21063 end_hpos = row->used[TEXT_AREA];
21064
21065 if (end_hpos > start_hpos)
21066 {
21067 draw_glyphs (w, start_x, row, TEXT_AREA,
21068 start_hpos, end_hpos,
21069 draw, 0);
21070
21071 row->mouse_face_p
21072 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
21073 }
21074 }
21075
21076 /* When we've written over the cursor, arrange for it to
21077 be displayed again. */
21078 if (phys_cursor_on_p && !w->phys_cursor_on_p)
21079 {
21080 BLOCK_INPUT;
21081 display_and_set_cursor (w, 1,
21082 w->phys_cursor.hpos, w->phys_cursor.vpos,
21083 w->phys_cursor.x, w->phys_cursor.y);
21084 UNBLOCK_INPUT;
21085 }
21086 }
21087
21088 /* Change the mouse cursor. */
21089 if (draw == DRAW_NORMAL_TEXT)
21090 rif->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
21091 else if (draw == DRAW_MOUSE_FACE)
21092 rif->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
21093 else
21094 rif->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
21095 }
21096
21097 /* EXPORT:
21098 Clear out the mouse-highlighted active region.
21099 Redraw it un-highlighted first. Value is non-zero if mouse
21100 face was actually drawn unhighlighted. */
21101
21102 int
21103 clear_mouse_face (dpyinfo)
21104 Display_Info *dpyinfo;
21105 {
21106 int cleared = 0;
21107
21108 if (!dpyinfo->mouse_face_hidden && !NILP (dpyinfo->mouse_face_window))
21109 {
21110 show_mouse_face (dpyinfo, DRAW_NORMAL_TEXT);
21111 cleared = 1;
21112 }
21113
21114 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
21115 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
21116 dpyinfo->mouse_face_window = Qnil;
21117 dpyinfo->mouse_face_overlay = Qnil;
21118 return cleared;
21119 }
21120
21121
21122 /* EXPORT:
21123 Non-zero if physical cursor of window W is within mouse face. */
21124
21125 int
21126 cursor_in_mouse_face_p (w)
21127 struct window *w;
21128 {
21129 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
21130 int in_mouse_face = 0;
21131
21132 if (WINDOWP (dpyinfo->mouse_face_window)
21133 && XWINDOW (dpyinfo->mouse_face_window) == w)
21134 {
21135 int hpos = w->phys_cursor.hpos;
21136 int vpos = w->phys_cursor.vpos;
21137
21138 if (vpos >= dpyinfo->mouse_face_beg_row
21139 && vpos <= dpyinfo->mouse_face_end_row
21140 && (vpos > dpyinfo->mouse_face_beg_row
21141 || hpos >= dpyinfo->mouse_face_beg_col)
21142 && (vpos < dpyinfo->mouse_face_end_row
21143 || hpos < dpyinfo->mouse_face_end_col
21144 || dpyinfo->mouse_face_past_end))
21145 in_mouse_face = 1;
21146 }
21147
21148 return in_mouse_face;
21149 }
21150
21151
21152
21153 \f
21154 /* Find the glyph matrix position of buffer position CHARPOS in window
21155 *W. HPOS, *VPOS, *X, and *Y are set to the positions found. W's
21156 current glyphs must be up to date. If CHARPOS is above window
21157 start return (0, 0, 0, 0). If CHARPOS is after end of W, return end
21158 of last line in W. In the row containing CHARPOS, stop before glyphs
21159 having STOP as object. */
21160
21161 #if 1 /* This is a version of fast_find_position that's more correct
21162 in the presence of hscrolling, for example. I didn't install
21163 it right away because the problem fixed is minor, it failed
21164 in 20.x as well, and I think it's too risky to install
21165 so near the release of 21.1. 2001-09-25 gerd. */
21166
21167 static int
21168 fast_find_position (w, charpos, hpos, vpos, x, y, stop)
21169 struct window *w;
21170 EMACS_INT charpos;
21171 int *hpos, *vpos, *x, *y;
21172 Lisp_Object stop;
21173 {
21174 struct glyph_row *row, *first;
21175 struct glyph *glyph, *end;
21176 int past_end = 0;
21177
21178 first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
21179 if (charpos < MATRIX_ROW_START_CHARPOS (first))
21180 {
21181 *x = first->x;
21182 *y = first->y;
21183 *hpos = 0;
21184 *vpos = MATRIX_ROW_VPOS (first, w->current_matrix);
21185 return 1;
21186 }
21187
21188 row = row_containing_pos (w, charpos, first, NULL, 0);
21189 if (row == NULL)
21190 {
21191 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
21192 past_end = 1;
21193 }
21194
21195 /* If whole rows or last part of a row came from a display overlay,
21196 row_containing_pos will skip over such rows because their end pos
21197 equals the start pos of the overlay or interval.
21198
21199 Move back if we have a STOP object and previous row's
21200 end glyph came from STOP. */
21201 if (!NILP (stop))
21202 {
21203 struct glyph_row *prev;
21204 while ((prev = row - 1, prev >= first)
21205 && MATRIX_ROW_END_CHARPOS (prev) == charpos
21206 && prev->used[TEXT_AREA] > 0)
21207 {
21208 struct glyph *beg = prev->glyphs[TEXT_AREA];
21209 glyph = beg + prev->used[TEXT_AREA];
21210 while (--glyph >= beg
21211 && INTEGERP (glyph->object));
21212 if (glyph < beg
21213 || !EQ (stop, glyph->object))
21214 break;
21215 row = prev;
21216 }
21217 }
21218
21219 *x = row->x;
21220 *y = row->y;
21221 *vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
21222
21223 glyph = row->glyphs[TEXT_AREA];
21224 end = glyph + row->used[TEXT_AREA];
21225
21226 /* Skip over glyphs not having an object at the start of the row.
21227 These are special glyphs like truncation marks on terminal
21228 frames. */
21229 if (row->displays_text_p)
21230 while (glyph < end
21231 && INTEGERP (glyph->object)
21232 && !EQ (stop, glyph->object)
21233 && glyph->charpos < 0)
21234 {
21235 *x += glyph->pixel_width;
21236 ++glyph;
21237 }
21238
21239 while (glyph < end
21240 && !INTEGERP (glyph->object)
21241 && !EQ (stop, glyph->object)
21242 && (!BUFFERP (glyph->object)
21243 || glyph->charpos < charpos))
21244 {
21245 *x += glyph->pixel_width;
21246 ++glyph;
21247 }
21248
21249 *hpos = glyph - row->glyphs[TEXT_AREA];
21250 return !past_end;
21251 }
21252
21253 #else /* not 1 */
21254
21255 static int
21256 fast_find_position (w, pos, hpos, vpos, x, y, stop)
21257 struct window *w;
21258 EMACS_INT pos;
21259 int *hpos, *vpos, *x, *y;
21260 Lisp_Object stop;
21261 {
21262 int i;
21263 int lastcol;
21264 int maybe_next_line_p = 0;
21265 int line_start_position;
21266 int yb = window_text_bottom_y (w);
21267 struct glyph_row *row, *best_row;
21268 int row_vpos, best_row_vpos;
21269 int current_x;
21270
21271 row = best_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
21272 row_vpos = best_row_vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
21273
21274 while (row->y < yb)
21275 {
21276 if (row->used[TEXT_AREA])
21277 line_start_position = row->glyphs[TEXT_AREA]->charpos;
21278 else
21279 line_start_position = 0;
21280
21281 if (line_start_position > pos)
21282 break;
21283 /* If the position sought is the end of the buffer,
21284 don't include the blank lines at the bottom of the window. */
21285 else if (line_start_position == pos
21286 && pos == BUF_ZV (XBUFFER (w->buffer)))
21287 {
21288 maybe_next_line_p = 1;
21289 break;
21290 }
21291 else if (line_start_position > 0)
21292 {
21293 best_row = row;
21294 best_row_vpos = row_vpos;
21295 }
21296
21297 if (row->y + row->height >= yb)
21298 break;
21299
21300 ++row;
21301 ++row_vpos;
21302 }
21303
21304 /* Find the right column within BEST_ROW. */
21305 lastcol = 0;
21306 current_x = best_row->x;
21307 for (i = 0; i < best_row->used[TEXT_AREA]; i++)
21308 {
21309 struct glyph *glyph = best_row->glyphs[TEXT_AREA] + i;
21310 int charpos = glyph->charpos;
21311
21312 if (BUFFERP (glyph->object))
21313 {
21314 if (charpos == pos)
21315 {
21316 *hpos = i;
21317 *vpos = best_row_vpos;
21318 *x = current_x;
21319 *y = best_row->y;
21320 return 1;
21321 }
21322 else if (charpos > pos)
21323 break;
21324 }
21325 else if (EQ (glyph->object, stop))
21326 break;
21327
21328 if (charpos > 0)
21329 lastcol = i;
21330 current_x += glyph->pixel_width;
21331 }
21332
21333 /* If we're looking for the end of the buffer,
21334 and we didn't find it in the line we scanned,
21335 use the start of the following line. */
21336 if (maybe_next_line_p)
21337 {
21338 ++best_row;
21339 ++best_row_vpos;
21340 lastcol = 0;
21341 current_x = best_row->x;
21342 }
21343
21344 *vpos = best_row_vpos;
21345 *hpos = lastcol + 1;
21346 *x = current_x;
21347 *y = best_row->y;
21348 return 0;
21349 }
21350
21351 #endif /* not 1 */
21352
21353
21354 /* Find the position of the glyph for position POS in OBJECT in
21355 window W's current matrix, and return in *X, *Y the pixel
21356 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
21357
21358 RIGHT_P non-zero means return the position of the right edge of the
21359 glyph, RIGHT_P zero means return the left edge position.
21360
21361 If no glyph for POS exists in the matrix, return the position of
21362 the glyph with the next smaller position that is in the matrix, if
21363 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
21364 exists in the matrix, return the position of the glyph with the
21365 next larger position in OBJECT.
21366
21367 Value is non-zero if a glyph was found. */
21368
21369 static int
21370 fast_find_string_pos (w, pos, object, hpos, vpos, x, y, right_p)
21371 struct window *w;
21372 EMACS_INT pos;
21373 Lisp_Object object;
21374 int *hpos, *vpos, *x, *y;
21375 int right_p;
21376 {
21377 int yb = window_text_bottom_y (w);
21378 struct glyph_row *r;
21379 struct glyph *best_glyph = NULL;
21380 struct glyph_row *best_row = NULL;
21381 int best_x = 0;
21382
21383 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
21384 r->enabled_p && r->y < yb;
21385 ++r)
21386 {
21387 struct glyph *g = r->glyphs[TEXT_AREA];
21388 struct glyph *e = g + r->used[TEXT_AREA];
21389 int gx;
21390
21391 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
21392 if (EQ (g->object, object))
21393 {
21394 if (g->charpos == pos)
21395 {
21396 best_glyph = g;
21397 best_x = gx;
21398 best_row = r;
21399 goto found;
21400 }
21401 else if (best_glyph == NULL
21402 || ((abs (g->charpos - pos)
21403 < abs (best_glyph->charpos - pos))
21404 && (right_p
21405 ? g->charpos < pos
21406 : g->charpos > pos)))
21407 {
21408 best_glyph = g;
21409 best_x = gx;
21410 best_row = r;
21411 }
21412 }
21413 }
21414
21415 found:
21416
21417 if (best_glyph)
21418 {
21419 *x = best_x;
21420 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
21421
21422 if (right_p)
21423 {
21424 *x += best_glyph->pixel_width;
21425 ++*hpos;
21426 }
21427
21428 *y = best_row->y;
21429 *vpos = best_row - w->current_matrix->rows;
21430 }
21431
21432 return best_glyph != NULL;
21433 }
21434
21435
21436 /* See if position X, Y is within a hot-spot of an image. */
21437
21438 static int
21439 on_hot_spot_p (hot_spot, x, y)
21440 Lisp_Object hot_spot;
21441 int x, y;
21442 {
21443 if (!CONSP (hot_spot))
21444 return 0;
21445
21446 if (EQ (XCAR (hot_spot), Qrect))
21447 {
21448 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
21449 Lisp_Object rect = XCDR (hot_spot);
21450 Lisp_Object tem;
21451 if (!CONSP (rect))
21452 return 0;
21453 if (!CONSP (XCAR (rect)))
21454 return 0;
21455 if (!CONSP (XCDR (rect)))
21456 return 0;
21457 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
21458 return 0;
21459 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
21460 return 0;
21461 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
21462 return 0;
21463 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
21464 return 0;
21465 return 1;
21466 }
21467 else if (EQ (XCAR (hot_spot), Qcircle))
21468 {
21469 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
21470 Lisp_Object circ = XCDR (hot_spot);
21471 Lisp_Object lr, lx0, ly0;
21472 if (CONSP (circ)
21473 && CONSP (XCAR (circ))
21474 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
21475 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
21476 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
21477 {
21478 double r = XFLOATINT (lr);
21479 double dx = XINT (lx0) - x;
21480 double dy = XINT (ly0) - y;
21481 return (dx * dx + dy * dy <= r * r);
21482 }
21483 }
21484 else if (EQ (XCAR (hot_spot), Qpoly))
21485 {
21486 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
21487 if (VECTORP (XCDR (hot_spot)))
21488 {
21489 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
21490 Lisp_Object *poly = v->contents;
21491 int n = v->size;
21492 int i;
21493 int inside = 0;
21494 Lisp_Object lx, ly;
21495 int x0, y0;
21496
21497 /* Need an even number of coordinates, and at least 3 edges. */
21498 if (n < 6 || n & 1)
21499 return 0;
21500
21501 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
21502 If count is odd, we are inside polygon. Pixels on edges
21503 may or may not be included depending on actual geometry of the
21504 polygon. */
21505 if ((lx = poly[n-2], !INTEGERP (lx))
21506 || (ly = poly[n-1], !INTEGERP (lx)))
21507 return 0;
21508 x0 = XINT (lx), y0 = XINT (ly);
21509 for (i = 0; i < n; i += 2)
21510 {
21511 int x1 = x0, y1 = y0;
21512 if ((lx = poly[i], !INTEGERP (lx))
21513 || (ly = poly[i+1], !INTEGERP (ly)))
21514 return 0;
21515 x0 = XINT (lx), y0 = XINT (ly);
21516
21517 /* Does this segment cross the X line? */
21518 if (x0 >= x)
21519 {
21520 if (x1 >= x)
21521 continue;
21522 }
21523 else if (x1 < x)
21524 continue;
21525 if (y > y0 && y > y1)
21526 continue;
21527 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
21528 inside = !inside;
21529 }
21530 return inside;
21531 }
21532 }
21533 /* If we don't understand the format, pretend we're not in the hot-spot. */
21534 return 0;
21535 }
21536
21537 Lisp_Object
21538 find_hot_spot (map, x, y)
21539 Lisp_Object map;
21540 int x, y;
21541 {
21542 while (CONSP (map))
21543 {
21544 if (CONSP (XCAR (map))
21545 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
21546 return XCAR (map);
21547 map = XCDR (map);
21548 }
21549
21550 return Qnil;
21551 }
21552
21553 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
21554 3, 3, 0,
21555 doc: /* Lookup in image map MAP coordinates X and Y.
21556 An image map is an alist where each element has the format (AREA ID PLIST).
21557 An AREA is specified as either a rectangle, a circle, or a polygon:
21558 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
21559 pixel coordinates of the upper left and bottom right corners.
21560 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
21561 and the radius of the circle; r may be a float or integer.
21562 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
21563 vector describes one corner in the polygon.
21564 Returns the alist element for the first matching AREA in MAP. */)
21565 (map, x, y)
21566 Lisp_Object map;
21567 Lisp_Object x, y;
21568 {
21569 if (NILP (map))
21570 return Qnil;
21571
21572 CHECK_NUMBER (x);
21573 CHECK_NUMBER (y);
21574
21575 return find_hot_spot (map, XINT (x), XINT (y));
21576 }
21577
21578
21579 /* Display frame CURSOR, optionally using shape defined by POINTER. */
21580 static void
21581 define_frame_cursor1 (f, cursor, pointer)
21582 struct frame *f;
21583 Cursor cursor;
21584 Lisp_Object pointer;
21585 {
21586 /* Do not change cursor shape while dragging mouse. */
21587 if (!NILP (do_mouse_tracking))
21588 return;
21589
21590 if (!NILP (pointer))
21591 {
21592 if (EQ (pointer, Qarrow))
21593 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
21594 else if (EQ (pointer, Qhand))
21595 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
21596 else if (EQ (pointer, Qtext))
21597 cursor = FRAME_X_OUTPUT (f)->text_cursor;
21598 else if (EQ (pointer, intern ("hdrag")))
21599 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
21600 #ifdef HAVE_X_WINDOWS
21601 else if (EQ (pointer, intern ("vdrag")))
21602 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
21603 #endif
21604 else if (EQ (pointer, intern ("hourglass")))
21605 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
21606 else if (EQ (pointer, Qmodeline))
21607 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
21608 else
21609 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
21610 }
21611
21612 if (cursor != No_Cursor)
21613 rif->define_frame_cursor (f, cursor);
21614 }
21615
21616 /* Take proper action when mouse has moved to the mode or header line
21617 or marginal area AREA of window W, x-position X and y-position Y.
21618 X is relative to the start of the text display area of W, so the
21619 width of bitmap areas and scroll bars must be subtracted to get a
21620 position relative to the start of the mode line. */
21621
21622 static void
21623 note_mode_line_or_margin_highlight (window, x, y, area)
21624 Lisp_Object window;
21625 int x, y;
21626 enum window_part area;
21627 {
21628 struct window *w = XWINDOW (window);
21629 struct frame *f = XFRAME (w->frame);
21630 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
21631 Cursor cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
21632 Lisp_Object pointer = Qnil;
21633 int charpos, dx, dy, width, height;
21634 Lisp_Object string, object = Qnil;
21635 Lisp_Object pos, help;
21636
21637 Lisp_Object mouse_face;
21638 int original_x_pixel = x;
21639 struct glyph * glyph = NULL;
21640 struct glyph_row *row;
21641
21642 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
21643 {
21644 int x0;
21645 struct glyph *end;
21646
21647 string = mode_line_string (w, area, &x, &y, &charpos,
21648 &object, &dx, &dy, &width, &height);
21649
21650 row = (area == ON_MODE_LINE
21651 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
21652 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
21653
21654 /* Find glyph */
21655 if (row->mode_line_p && row->enabled_p)
21656 {
21657 glyph = row->glyphs[TEXT_AREA];
21658 end = glyph + row->used[TEXT_AREA];
21659
21660 for (x0 = original_x_pixel;
21661 glyph < end && x0 >= glyph->pixel_width;
21662 ++glyph)
21663 x0 -= glyph->pixel_width;
21664
21665 if (glyph >= end)
21666 glyph = NULL;
21667 }
21668 }
21669 else
21670 {
21671 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
21672 string = marginal_area_string (w, area, &x, &y, &charpos,
21673 &object, &dx, &dy, &width, &height);
21674 }
21675
21676 help = Qnil;
21677
21678 if (IMAGEP (object))
21679 {
21680 Lisp_Object image_map, hotspot;
21681 if ((image_map = Fplist_get (XCDR (object), QCmap),
21682 !NILP (image_map))
21683 && (hotspot = find_hot_spot (image_map, dx, dy),
21684 CONSP (hotspot))
21685 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
21686 {
21687 Lisp_Object area_id, plist;
21688
21689 area_id = XCAR (hotspot);
21690 /* Could check AREA_ID to see if we enter/leave this hot-spot.
21691 If so, we could look for mouse-enter, mouse-leave
21692 properties in PLIST (and do something...). */
21693 hotspot = XCDR (hotspot);
21694 if (CONSP (hotspot)
21695 && (plist = XCAR (hotspot), CONSP (plist)))
21696 {
21697 pointer = Fplist_get (plist, Qpointer);
21698 if (NILP (pointer))
21699 pointer = Qhand;
21700 help = Fplist_get (plist, Qhelp_echo);
21701 if (!NILP (help))
21702 {
21703 help_echo_string = help;
21704 /* Is this correct? ++kfs */
21705 XSETWINDOW (help_echo_window, w);
21706 help_echo_object = w->buffer;
21707 help_echo_pos = charpos;
21708 }
21709 }
21710 }
21711 if (NILP (pointer))
21712 pointer = Fplist_get (XCDR (object), QCpointer);
21713 }
21714
21715 if (STRINGP (string))
21716 {
21717 pos = make_number (charpos);
21718 /* If we're on a string with `help-echo' text property, arrange
21719 for the help to be displayed. This is done by setting the
21720 global variable help_echo_string to the help string. */
21721 if (NILP (help))
21722 {
21723 help = Fget_text_property (pos, Qhelp_echo, string);
21724 if (!NILP (help))
21725 {
21726 help_echo_string = help;
21727 XSETWINDOW (help_echo_window, w);
21728 help_echo_object = string;
21729 help_echo_pos = charpos;
21730 }
21731 }
21732
21733 if (NILP (pointer))
21734 pointer = Fget_text_property (pos, Qpointer, string);
21735
21736 /* Change the mouse pointer according to what is under X/Y. */
21737 if (NILP (pointer) && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
21738 {
21739 Lisp_Object map;
21740 map = Fget_text_property (pos, Qlocal_map, string);
21741 if (!KEYMAPP (map))
21742 map = Fget_text_property (pos, Qkeymap, string);
21743 if (!KEYMAPP (map))
21744 cursor = dpyinfo->vertical_scroll_bar_cursor;
21745 }
21746
21747 /* Change the mouse face according to what is under X/Y. */
21748 mouse_face = Fget_text_property (pos, Qmouse_face, string);
21749 if (!NILP (mouse_face)
21750 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
21751 && glyph)
21752 {
21753 Lisp_Object b, e;
21754
21755 struct glyph * tmp_glyph;
21756
21757 int gpos;
21758 int gseq_length;
21759 int total_pixel_width;
21760 int ignore;
21761
21762 int vpos, hpos;
21763
21764 b = Fprevious_single_property_change (make_number (charpos + 1),
21765 Qmouse_face, string, Qnil);
21766 if (NILP (b))
21767 b = make_number (0);
21768
21769 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
21770 if (NILP (e))
21771 e = make_number (SCHARS (string));
21772
21773 /* Calculate the position(glyph position: GPOS) of GLYPH in
21774 displayed string. GPOS is different from CHARPOS.
21775
21776 CHARPOS is the position of glyph in internal string
21777 object. A mode line string format has structures which
21778 is converted to a flatten by emacs lisp interpreter.
21779 The internal string is an element of the structures.
21780 The displayed string is the flatten string. */
21781 for (tmp_glyph = glyph - 1, gpos = 0;
21782 tmp_glyph->charpos >= XINT (b);
21783 tmp_glyph--, gpos++)
21784 {
21785 if (!EQ (tmp_glyph->object, glyph->object))
21786 break;
21787 }
21788
21789 /* Calculate the lenght(glyph sequence length: GSEQ_LENGTH) of
21790 displayed string holding GLYPH.
21791
21792 GSEQ_LENGTH is different from SCHARS (STRING).
21793 SCHARS (STRING) returns the length of the internal string. */
21794 for (tmp_glyph = glyph, gseq_length = gpos;
21795 tmp_glyph->charpos < XINT (e);
21796 tmp_glyph++, gseq_length++)
21797 {
21798 if (!EQ (tmp_glyph->object, glyph->object))
21799 break;
21800 }
21801
21802 total_pixel_width = 0;
21803 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
21804 total_pixel_width += tmp_glyph->pixel_width;
21805
21806 /* Pre calculation of re-rendering position */
21807 vpos = (x - gpos);
21808 hpos = (area == ON_MODE_LINE
21809 ? (w->current_matrix)->nrows - 1
21810 : 0);
21811
21812 /* If the re-rendering position is included in the last
21813 re-rendering area, we should do nothing. */
21814 if ( EQ (window, dpyinfo->mouse_face_window)
21815 && dpyinfo->mouse_face_beg_col <= vpos
21816 && vpos < dpyinfo->mouse_face_end_col
21817 && dpyinfo->mouse_face_beg_row == hpos )
21818 return;
21819
21820 if (clear_mouse_face (dpyinfo))
21821 cursor = No_Cursor;
21822
21823 dpyinfo->mouse_face_beg_col = vpos;
21824 dpyinfo->mouse_face_beg_row = hpos;
21825
21826 dpyinfo->mouse_face_beg_x = original_x_pixel - (total_pixel_width + dx);
21827 dpyinfo->mouse_face_beg_y = 0;
21828
21829 dpyinfo->mouse_face_end_col = vpos + gseq_length;
21830 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_beg_row;
21831
21832 dpyinfo->mouse_face_end_x = 0;
21833 dpyinfo->mouse_face_end_y = 0;
21834
21835 dpyinfo->mouse_face_past_end = 0;
21836 dpyinfo->mouse_face_window = window;
21837
21838 dpyinfo->mouse_face_face_id = face_at_string_position (w, string,
21839 charpos,
21840 0, 0, 0, &ignore,
21841 glyph->face_id, 1);
21842 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
21843
21844 if (NILP (pointer))
21845 pointer = Qhand;
21846 }
21847 else if ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
21848 clear_mouse_face (dpyinfo);
21849 }
21850 define_frame_cursor1 (f, cursor, pointer);
21851 }
21852
21853
21854 /* EXPORT:
21855 Take proper action when the mouse has moved to position X, Y on
21856 frame F as regards highlighting characters that have mouse-face
21857 properties. Also de-highlighting chars where the mouse was before.
21858 X and Y can be negative or out of range. */
21859
21860 void
21861 note_mouse_highlight (f, x, y)
21862 struct frame *f;
21863 int x, y;
21864 {
21865 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
21866 enum window_part part;
21867 Lisp_Object window;
21868 struct window *w;
21869 Cursor cursor = No_Cursor;
21870 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
21871 struct buffer *b;
21872
21873 /* When a menu is active, don't highlight because this looks odd. */
21874 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NTGUI)
21875 if (popup_activated ())
21876 return;
21877 #endif
21878
21879 if (NILP (Vmouse_highlight)
21880 || !f->glyphs_initialized_p)
21881 return;
21882
21883 dpyinfo->mouse_face_mouse_x = x;
21884 dpyinfo->mouse_face_mouse_y = y;
21885 dpyinfo->mouse_face_mouse_frame = f;
21886
21887 if (dpyinfo->mouse_face_defer)
21888 return;
21889
21890 if (gc_in_progress)
21891 {
21892 dpyinfo->mouse_face_deferred_gc = 1;
21893 return;
21894 }
21895
21896 /* Which window is that in? */
21897 window = window_from_coordinates (f, x, y, &part, 0, 0, 1);
21898
21899 /* If we were displaying active text in another window, clear that.
21900 Also clear if we move out of text area in same window. */
21901 if (! EQ (window, dpyinfo->mouse_face_window)
21902 || (part != ON_TEXT && part != ON_MODE_LINE && part != ON_HEADER_LINE
21903 && !NILP (dpyinfo->mouse_face_window)))
21904 clear_mouse_face (dpyinfo);
21905
21906 /* Not on a window -> return. */
21907 if (!WINDOWP (window))
21908 return;
21909
21910 /* Reset help_echo_string. It will get recomputed below. */
21911 help_echo_string = Qnil;
21912
21913 /* Convert to window-relative pixel coordinates. */
21914 w = XWINDOW (window);
21915 frame_to_window_pixel_xy (w, &x, &y);
21916
21917 /* Handle tool-bar window differently since it doesn't display a
21918 buffer. */
21919 if (EQ (window, f->tool_bar_window))
21920 {
21921 note_tool_bar_highlight (f, x, y);
21922 return;
21923 }
21924
21925 /* Mouse is on the mode, header line or margin? */
21926 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
21927 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
21928 {
21929 note_mode_line_or_margin_highlight (window, x, y, part);
21930 return;
21931 }
21932
21933 if (part == ON_VERTICAL_BORDER)
21934 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
21935 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
21936 || part == ON_SCROLL_BAR)
21937 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
21938 else
21939 cursor = FRAME_X_OUTPUT (f)->text_cursor;
21940
21941 /* Are we in a window whose display is up to date?
21942 And verify the buffer's text has not changed. */
21943 b = XBUFFER (w->buffer);
21944 if (part == ON_TEXT
21945 && EQ (w->window_end_valid, w->buffer)
21946 && XFASTINT (w->last_modified) == BUF_MODIFF (b)
21947 && XFASTINT (w->last_overlay_modified) == BUF_OVERLAY_MODIFF (b))
21948 {
21949 int hpos, vpos, pos, i, dx, dy, area;
21950 struct glyph *glyph;
21951 Lisp_Object object;
21952 Lisp_Object mouse_face = Qnil, overlay = Qnil, position;
21953 Lisp_Object *overlay_vec = NULL;
21954 int noverlays;
21955 struct buffer *obuf;
21956 int obegv, ozv, same_region;
21957
21958 /* Find the glyph under X/Y. */
21959 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
21960
21961 /* Look for :pointer property on image. */
21962 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
21963 {
21964 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
21965 if (img != NULL && IMAGEP (img->spec))
21966 {
21967 Lisp_Object image_map, hotspot;
21968 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
21969 !NILP (image_map))
21970 && (hotspot = find_hot_spot (image_map,
21971 glyph->slice.x + dx,
21972 glyph->slice.y + dy),
21973 CONSP (hotspot))
21974 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
21975 {
21976 Lisp_Object area_id, plist;
21977
21978 area_id = XCAR (hotspot);
21979 /* Could check AREA_ID to see if we enter/leave this hot-spot.
21980 If so, we could look for mouse-enter, mouse-leave
21981 properties in PLIST (and do something...). */
21982 hotspot = XCDR (hotspot);
21983 if (CONSP (hotspot)
21984 && (plist = XCAR (hotspot), CONSP (plist)))
21985 {
21986 pointer = Fplist_get (plist, Qpointer);
21987 if (NILP (pointer))
21988 pointer = Qhand;
21989 help_echo_string = Fplist_get (plist, Qhelp_echo);
21990 if (!NILP (help_echo_string))
21991 {
21992 help_echo_window = window;
21993 help_echo_object = glyph->object;
21994 help_echo_pos = glyph->charpos;
21995 }
21996 }
21997 }
21998 if (NILP (pointer))
21999 pointer = Fplist_get (XCDR (img->spec), QCpointer);
22000 }
22001 }
22002
22003 /* Clear mouse face if X/Y not over text. */
22004 if (glyph == NULL
22005 || area != TEXT_AREA
22006 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p)
22007 {
22008 if (clear_mouse_face (dpyinfo))
22009 cursor = No_Cursor;
22010 if (NILP (pointer))
22011 {
22012 if (area != TEXT_AREA)
22013 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
22014 else
22015 pointer = Vvoid_text_area_pointer;
22016 }
22017 goto set_cursor;
22018 }
22019
22020 pos = glyph->charpos;
22021 object = glyph->object;
22022 if (!STRINGP (object) && !BUFFERP (object))
22023 goto set_cursor;
22024
22025 /* If we get an out-of-range value, return now; avoid an error. */
22026 if (BUFFERP (object) && pos > BUF_Z (b))
22027 goto set_cursor;
22028
22029 /* Make the window's buffer temporarily current for
22030 overlays_at and compute_char_face. */
22031 obuf = current_buffer;
22032 current_buffer = b;
22033 obegv = BEGV;
22034 ozv = ZV;
22035 BEGV = BEG;
22036 ZV = Z;
22037
22038 /* Is this char mouse-active or does it have help-echo? */
22039 position = make_number (pos);
22040
22041 if (BUFFERP (object))
22042 {
22043 /* Put all the overlays we want in a vector in overlay_vec. */
22044 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
22045 /* Sort overlays into increasing priority order. */
22046 noverlays = sort_overlays (overlay_vec, noverlays, w);
22047 }
22048 else
22049 noverlays = 0;
22050
22051 same_region = (EQ (window, dpyinfo->mouse_face_window)
22052 && vpos >= dpyinfo->mouse_face_beg_row
22053 && vpos <= dpyinfo->mouse_face_end_row
22054 && (vpos > dpyinfo->mouse_face_beg_row
22055 || hpos >= dpyinfo->mouse_face_beg_col)
22056 && (vpos < dpyinfo->mouse_face_end_row
22057 || hpos < dpyinfo->mouse_face_end_col
22058 || dpyinfo->mouse_face_past_end));
22059
22060 if (same_region)
22061 cursor = No_Cursor;
22062
22063 /* Check mouse-face highlighting. */
22064 if (! same_region
22065 /* If there exists an overlay with mouse-face overlapping
22066 the one we are currently highlighting, we have to
22067 check if we enter the overlapping overlay, and then
22068 highlight only that. */
22069 || (OVERLAYP (dpyinfo->mouse_face_overlay)
22070 && mouse_face_overlay_overlaps (dpyinfo->mouse_face_overlay)))
22071 {
22072 /* Find the highest priority overlay that has a mouse-face
22073 property. */
22074 overlay = Qnil;
22075 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
22076 {
22077 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
22078 if (!NILP (mouse_face))
22079 overlay = overlay_vec[i];
22080 }
22081
22082 /* If we're actually highlighting the same overlay as
22083 before, there's no need to do that again. */
22084 if (!NILP (overlay)
22085 && EQ (overlay, dpyinfo->mouse_face_overlay))
22086 goto check_help_echo;
22087
22088 dpyinfo->mouse_face_overlay = overlay;
22089
22090 /* Clear the display of the old active region, if any. */
22091 if (clear_mouse_face (dpyinfo))
22092 cursor = No_Cursor;
22093
22094 /* If no overlay applies, get a text property. */
22095 if (NILP (overlay))
22096 mouse_face = Fget_text_property (position, Qmouse_face, object);
22097
22098 /* Handle the overlay case. */
22099 if (!NILP (overlay))
22100 {
22101 /* Find the range of text around this char that
22102 should be active. */
22103 Lisp_Object before, after;
22104 int ignore;
22105
22106 before = Foverlay_start (overlay);
22107 after = Foverlay_end (overlay);
22108 /* Record this as the current active region. */
22109 fast_find_position (w, XFASTINT (before),
22110 &dpyinfo->mouse_face_beg_col,
22111 &dpyinfo->mouse_face_beg_row,
22112 &dpyinfo->mouse_face_beg_x,
22113 &dpyinfo->mouse_face_beg_y, Qnil);
22114
22115 dpyinfo->mouse_face_past_end
22116 = !fast_find_position (w, XFASTINT (after),
22117 &dpyinfo->mouse_face_end_col,
22118 &dpyinfo->mouse_face_end_row,
22119 &dpyinfo->mouse_face_end_x,
22120 &dpyinfo->mouse_face_end_y, Qnil);
22121 dpyinfo->mouse_face_window = window;
22122
22123 dpyinfo->mouse_face_face_id
22124 = face_at_buffer_position (w, pos, 0, 0,
22125 &ignore, pos + 1,
22126 !dpyinfo->mouse_face_hidden);
22127
22128 /* Display it as active. */
22129 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
22130 cursor = No_Cursor;
22131 }
22132 /* Handle the text property case. */
22133 else if (!NILP (mouse_face) && BUFFERP (object))
22134 {
22135 /* Find the range of text around this char that
22136 should be active. */
22137 Lisp_Object before, after, beginning, end;
22138 int ignore;
22139
22140 beginning = Fmarker_position (w->start);
22141 end = make_number (BUF_Z (XBUFFER (object))
22142 - XFASTINT (w->window_end_pos));
22143 before
22144 = Fprevious_single_property_change (make_number (pos + 1),
22145 Qmouse_face,
22146 object, beginning);
22147 after
22148 = Fnext_single_property_change (position, Qmouse_face,
22149 object, end);
22150
22151 /* Record this as the current active region. */
22152 fast_find_position (w, XFASTINT (before),
22153 &dpyinfo->mouse_face_beg_col,
22154 &dpyinfo->mouse_face_beg_row,
22155 &dpyinfo->mouse_face_beg_x,
22156 &dpyinfo->mouse_face_beg_y, Qnil);
22157 dpyinfo->mouse_face_past_end
22158 = !fast_find_position (w, XFASTINT (after),
22159 &dpyinfo->mouse_face_end_col,
22160 &dpyinfo->mouse_face_end_row,
22161 &dpyinfo->mouse_face_end_x,
22162 &dpyinfo->mouse_face_end_y, Qnil);
22163 dpyinfo->mouse_face_window = window;
22164
22165 if (BUFFERP (object))
22166 dpyinfo->mouse_face_face_id
22167 = face_at_buffer_position (w, pos, 0, 0,
22168 &ignore, pos + 1,
22169 !dpyinfo->mouse_face_hidden);
22170
22171 /* Display it as active. */
22172 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
22173 cursor = No_Cursor;
22174 }
22175 else if (!NILP (mouse_face) && STRINGP (object))
22176 {
22177 Lisp_Object b, e;
22178 int ignore;
22179
22180 b = Fprevious_single_property_change (make_number (pos + 1),
22181 Qmouse_face,
22182 object, Qnil);
22183 e = Fnext_single_property_change (position, Qmouse_face,
22184 object, Qnil);
22185 if (NILP (b))
22186 b = make_number (0);
22187 if (NILP (e))
22188 e = make_number (SCHARS (object) - 1);
22189
22190 fast_find_string_pos (w, XINT (b), object,
22191 &dpyinfo->mouse_face_beg_col,
22192 &dpyinfo->mouse_face_beg_row,
22193 &dpyinfo->mouse_face_beg_x,
22194 &dpyinfo->mouse_face_beg_y, 0);
22195 fast_find_string_pos (w, XINT (e), object,
22196 &dpyinfo->mouse_face_end_col,
22197 &dpyinfo->mouse_face_end_row,
22198 &dpyinfo->mouse_face_end_x,
22199 &dpyinfo->mouse_face_end_y, 1);
22200 dpyinfo->mouse_face_past_end = 0;
22201 dpyinfo->mouse_face_window = window;
22202 dpyinfo->mouse_face_face_id
22203 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
22204 glyph->face_id, 1);
22205 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
22206 cursor = No_Cursor;
22207 }
22208 else if (STRINGP (object) && NILP (mouse_face))
22209 {
22210 /* A string which doesn't have mouse-face, but
22211 the text ``under'' it might have. */
22212 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
22213 int start = MATRIX_ROW_START_CHARPOS (r);
22214
22215 pos = string_buffer_position (w, object, start);
22216 if (pos > 0)
22217 mouse_face = get_char_property_and_overlay (make_number (pos),
22218 Qmouse_face,
22219 w->buffer,
22220 &overlay);
22221 if (!NILP (mouse_face) && !NILP (overlay))
22222 {
22223 Lisp_Object before = Foverlay_start (overlay);
22224 Lisp_Object after = Foverlay_end (overlay);
22225 int ignore;
22226
22227 /* Note that we might not be able to find position
22228 BEFORE in the glyph matrix if the overlay is
22229 entirely covered by a `display' property. In
22230 this case, we overshoot. So let's stop in
22231 the glyph matrix before glyphs for OBJECT. */
22232 fast_find_position (w, XFASTINT (before),
22233 &dpyinfo->mouse_face_beg_col,
22234 &dpyinfo->mouse_face_beg_row,
22235 &dpyinfo->mouse_face_beg_x,
22236 &dpyinfo->mouse_face_beg_y,
22237 object);
22238
22239 dpyinfo->mouse_face_past_end
22240 = !fast_find_position (w, XFASTINT (after),
22241 &dpyinfo->mouse_face_end_col,
22242 &dpyinfo->mouse_face_end_row,
22243 &dpyinfo->mouse_face_end_x,
22244 &dpyinfo->mouse_face_end_y,
22245 Qnil);
22246 dpyinfo->mouse_face_window = window;
22247 dpyinfo->mouse_face_face_id
22248 = face_at_buffer_position (w, pos, 0, 0,
22249 &ignore, pos + 1,
22250 !dpyinfo->mouse_face_hidden);
22251
22252 /* Display it as active. */
22253 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
22254 cursor = No_Cursor;
22255 }
22256 }
22257 }
22258
22259 check_help_echo:
22260
22261 /* Look for a `help-echo' property. */
22262 if (NILP (help_echo_string)) {
22263 Lisp_Object help, overlay;
22264
22265 /* Check overlays first. */
22266 help = overlay = Qnil;
22267 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
22268 {
22269 overlay = overlay_vec[i];
22270 help = Foverlay_get (overlay, Qhelp_echo);
22271 }
22272
22273 if (!NILP (help))
22274 {
22275 help_echo_string = help;
22276 help_echo_window = window;
22277 help_echo_object = overlay;
22278 help_echo_pos = pos;
22279 }
22280 else
22281 {
22282 Lisp_Object object = glyph->object;
22283 int charpos = glyph->charpos;
22284
22285 /* Try text properties. */
22286 if (STRINGP (object)
22287 && charpos >= 0
22288 && charpos < SCHARS (object))
22289 {
22290 help = Fget_text_property (make_number (charpos),
22291 Qhelp_echo, object);
22292 if (NILP (help))
22293 {
22294 /* If the string itself doesn't specify a help-echo,
22295 see if the buffer text ``under'' it does. */
22296 struct glyph_row *r
22297 = MATRIX_ROW (w->current_matrix, vpos);
22298 int start = MATRIX_ROW_START_CHARPOS (r);
22299 int pos = string_buffer_position (w, object, start);
22300 if (pos > 0)
22301 {
22302 help = Fget_char_property (make_number (pos),
22303 Qhelp_echo, w->buffer);
22304 if (!NILP (help))
22305 {
22306 charpos = pos;
22307 object = w->buffer;
22308 }
22309 }
22310 }
22311 }
22312 else if (BUFFERP (object)
22313 && charpos >= BEGV
22314 && charpos < ZV)
22315 help = Fget_text_property (make_number (charpos), Qhelp_echo,
22316 object);
22317
22318 if (!NILP (help))
22319 {
22320 help_echo_string = help;
22321 help_echo_window = window;
22322 help_echo_object = object;
22323 help_echo_pos = charpos;
22324 }
22325 }
22326 }
22327
22328 /* Look for a `pointer' property. */
22329 if (NILP (pointer))
22330 {
22331 /* Check overlays first. */
22332 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
22333 pointer = Foverlay_get (overlay_vec[i], Qpointer);
22334
22335 if (NILP (pointer))
22336 {
22337 Lisp_Object object = glyph->object;
22338 int charpos = glyph->charpos;
22339
22340 /* Try text properties. */
22341 if (STRINGP (object)
22342 && charpos >= 0
22343 && charpos < SCHARS (object))
22344 {
22345 pointer = Fget_text_property (make_number (charpos),
22346 Qpointer, object);
22347 if (NILP (pointer))
22348 {
22349 /* If the string itself doesn't specify a pointer,
22350 see if the buffer text ``under'' it does. */
22351 struct glyph_row *r
22352 = MATRIX_ROW (w->current_matrix, vpos);
22353 int start = MATRIX_ROW_START_CHARPOS (r);
22354 int pos = string_buffer_position (w, object, start);
22355 if (pos > 0)
22356 pointer = Fget_char_property (make_number (pos),
22357 Qpointer, w->buffer);
22358 }
22359 }
22360 else if (BUFFERP (object)
22361 && charpos >= BEGV
22362 && charpos < ZV)
22363 pointer = Fget_text_property (make_number (charpos),
22364 Qpointer, object);
22365 }
22366 }
22367
22368 BEGV = obegv;
22369 ZV = ozv;
22370 current_buffer = obuf;
22371 }
22372
22373 set_cursor:
22374
22375 define_frame_cursor1 (f, cursor, pointer);
22376 }
22377
22378
22379 /* EXPORT for RIF:
22380 Clear any mouse-face on window W. This function is part of the
22381 redisplay interface, and is called from try_window_id and similar
22382 functions to ensure the mouse-highlight is off. */
22383
22384 void
22385 x_clear_window_mouse_face (w)
22386 struct window *w;
22387 {
22388 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
22389 Lisp_Object window;
22390
22391 BLOCK_INPUT;
22392 XSETWINDOW (window, w);
22393 if (EQ (window, dpyinfo->mouse_face_window))
22394 clear_mouse_face (dpyinfo);
22395 UNBLOCK_INPUT;
22396 }
22397
22398
22399 /* EXPORT:
22400 Just discard the mouse face information for frame F, if any.
22401 This is used when the size of F is changed. */
22402
22403 void
22404 cancel_mouse_face (f)
22405 struct frame *f;
22406 {
22407 Lisp_Object window;
22408 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
22409
22410 window = dpyinfo->mouse_face_window;
22411 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
22412 {
22413 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
22414 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
22415 dpyinfo->mouse_face_window = Qnil;
22416 }
22417 }
22418
22419
22420 #endif /* HAVE_WINDOW_SYSTEM */
22421
22422 \f
22423 /***********************************************************************
22424 Exposure Events
22425 ***********************************************************************/
22426
22427 #ifdef HAVE_WINDOW_SYSTEM
22428
22429 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
22430 which intersects rectangle R. R is in window-relative coordinates. */
22431
22432 static void
22433 expose_area (w, row, r, area)
22434 struct window *w;
22435 struct glyph_row *row;
22436 XRectangle *r;
22437 enum glyph_row_area area;
22438 {
22439 struct glyph *first = row->glyphs[area];
22440 struct glyph *end = row->glyphs[area] + row->used[area];
22441 struct glyph *last;
22442 int first_x, start_x, x;
22443
22444 if (area == TEXT_AREA && row->fill_line_p)
22445 /* If row extends face to end of line write the whole line. */
22446 draw_glyphs (w, 0, row, area,
22447 0, row->used[area],
22448 DRAW_NORMAL_TEXT, 0);
22449 else
22450 {
22451 /* Set START_X to the window-relative start position for drawing glyphs of
22452 AREA. The first glyph of the text area can be partially visible.
22453 The first glyphs of other areas cannot. */
22454 start_x = window_box_left_offset (w, area);
22455 x = start_x;
22456 if (area == TEXT_AREA)
22457 x += row->x;
22458
22459 /* Find the first glyph that must be redrawn. */
22460 while (first < end
22461 && x + first->pixel_width < r->x)
22462 {
22463 x += first->pixel_width;
22464 ++first;
22465 }
22466
22467 /* Find the last one. */
22468 last = first;
22469 first_x = x;
22470 while (last < end
22471 && x < r->x + r->width)
22472 {
22473 x += last->pixel_width;
22474 ++last;
22475 }
22476
22477 /* Repaint. */
22478 if (last > first)
22479 draw_glyphs (w, first_x - start_x, row, area,
22480 first - row->glyphs[area], last - row->glyphs[area],
22481 DRAW_NORMAL_TEXT, 0);
22482 }
22483 }
22484
22485
22486 /* Redraw the parts of the glyph row ROW on window W intersecting
22487 rectangle R. R is in window-relative coordinates. Value is
22488 non-zero if mouse-face was overwritten. */
22489
22490 static int
22491 expose_line (w, row, r)
22492 struct window *w;
22493 struct glyph_row *row;
22494 XRectangle *r;
22495 {
22496 xassert (row->enabled_p);
22497
22498 if (row->mode_line_p || w->pseudo_window_p)
22499 draw_glyphs (w, 0, row, TEXT_AREA,
22500 0, row->used[TEXT_AREA],
22501 DRAW_NORMAL_TEXT, 0);
22502 else
22503 {
22504 if (row->used[LEFT_MARGIN_AREA])
22505 expose_area (w, row, r, LEFT_MARGIN_AREA);
22506 if (row->used[TEXT_AREA])
22507 expose_area (w, row, r, TEXT_AREA);
22508 if (row->used[RIGHT_MARGIN_AREA])
22509 expose_area (w, row, r, RIGHT_MARGIN_AREA);
22510 draw_row_fringe_bitmaps (w, row);
22511 }
22512
22513 return row->mouse_face_p;
22514 }
22515
22516
22517 /* Redraw those parts of glyphs rows during expose event handling that
22518 overlap other rows. Redrawing of an exposed line writes over parts
22519 of lines overlapping that exposed line; this function fixes that.
22520
22521 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
22522 row in W's current matrix that is exposed and overlaps other rows.
22523 LAST_OVERLAPPING_ROW is the last such row. */
22524
22525 static void
22526 expose_overlaps (w, first_overlapping_row, last_overlapping_row)
22527 struct window *w;
22528 struct glyph_row *first_overlapping_row;
22529 struct glyph_row *last_overlapping_row;
22530 {
22531 struct glyph_row *row;
22532
22533 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
22534 if (row->overlapping_p)
22535 {
22536 xassert (row->enabled_p && !row->mode_line_p);
22537
22538 if (row->used[LEFT_MARGIN_AREA])
22539 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA);
22540
22541 if (row->used[TEXT_AREA])
22542 x_fix_overlapping_area (w, row, TEXT_AREA);
22543
22544 if (row->used[RIGHT_MARGIN_AREA])
22545 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA);
22546 }
22547 }
22548
22549
22550 /* Return non-zero if W's cursor intersects rectangle R. */
22551
22552 static int
22553 phys_cursor_in_rect_p (w, r)
22554 struct window *w;
22555 XRectangle *r;
22556 {
22557 XRectangle cr, result;
22558 struct glyph *cursor_glyph;
22559
22560 cursor_glyph = get_phys_cursor_glyph (w);
22561 if (cursor_glyph)
22562 {
22563 /* r is relative to W's box, but w->phys_cursor.x is relative
22564 to left edge of W's TEXT area. Adjust it. */
22565 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
22566 cr.y = w->phys_cursor.y;
22567 cr.width = cursor_glyph->pixel_width;
22568 cr.height = w->phys_cursor_height;
22569 /* ++KFS: W32 version used W32-specific IntersectRect here, but
22570 I assume the effect is the same -- and this is portable. */
22571 return x_intersect_rectangles (&cr, r, &result);
22572 }
22573 else
22574 return 0;
22575 }
22576
22577
22578 /* EXPORT:
22579 Draw a vertical window border to the right of window W if W doesn't
22580 have vertical scroll bars. */
22581
22582 void
22583 x_draw_vertical_border (w)
22584 struct window *w;
22585 {
22586 /* We could do better, if we knew what type of scroll-bar the adjacent
22587 windows (on either side) have... But we don't :-(
22588 However, I think this works ok. ++KFS 2003-04-25 */
22589
22590 /* Redraw borders between horizontally adjacent windows. Don't
22591 do it for frames with vertical scroll bars because either the
22592 right scroll bar of a window, or the left scroll bar of its
22593 neighbor will suffice as a border. */
22594 if (FRAME_HAS_VERTICAL_SCROLL_BARS (XFRAME (w->frame)))
22595 return;
22596
22597 if (!WINDOW_RIGHTMOST_P (w)
22598 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
22599 {
22600 int x0, x1, y0, y1;
22601
22602 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
22603 y1 -= 1;
22604
22605 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
22606 x1 -= 1;
22607
22608 rif->draw_vertical_window_border (w, x1, y0, y1);
22609 }
22610 else if (!WINDOW_LEFTMOST_P (w)
22611 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
22612 {
22613 int x0, x1, y0, y1;
22614
22615 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
22616 y1 -= 1;
22617
22618 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
22619 x0 -= 1;
22620
22621 rif->draw_vertical_window_border (w, x0, y0, y1);
22622 }
22623 }
22624
22625
22626 /* Redraw the part of window W intersection rectangle FR. Pixel
22627 coordinates in FR are frame-relative. Call this function with
22628 input blocked. Value is non-zero if the exposure overwrites
22629 mouse-face. */
22630
22631 static int
22632 expose_window (w, fr)
22633 struct window *w;
22634 XRectangle *fr;
22635 {
22636 struct frame *f = XFRAME (w->frame);
22637 XRectangle wr, r;
22638 int mouse_face_overwritten_p = 0;
22639
22640 /* If window is not yet fully initialized, do nothing. This can
22641 happen when toolkit scroll bars are used and a window is split.
22642 Reconfiguring the scroll bar will generate an expose for a newly
22643 created window. */
22644 if (w->current_matrix == NULL)
22645 return 0;
22646
22647 /* When we're currently updating the window, display and current
22648 matrix usually don't agree. Arrange for a thorough display
22649 later. */
22650 if (w == updated_window)
22651 {
22652 SET_FRAME_GARBAGED (f);
22653 return 0;
22654 }
22655
22656 /* Frame-relative pixel rectangle of W. */
22657 wr.x = WINDOW_LEFT_EDGE_X (w);
22658 wr.y = WINDOW_TOP_EDGE_Y (w);
22659 wr.width = WINDOW_TOTAL_WIDTH (w);
22660 wr.height = WINDOW_TOTAL_HEIGHT (w);
22661
22662 if (x_intersect_rectangles (fr, &wr, &r))
22663 {
22664 int yb = window_text_bottom_y (w);
22665 struct glyph_row *row;
22666 int cursor_cleared_p;
22667 struct glyph_row *first_overlapping_row, *last_overlapping_row;
22668
22669 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
22670 r.x, r.y, r.width, r.height));
22671
22672 /* Convert to window coordinates. */
22673 r.x -= WINDOW_LEFT_EDGE_X (w);
22674 r.y -= WINDOW_TOP_EDGE_Y (w);
22675
22676 /* Turn off the cursor. */
22677 if (!w->pseudo_window_p
22678 && phys_cursor_in_rect_p (w, &r))
22679 {
22680 x_clear_cursor (w);
22681 cursor_cleared_p = 1;
22682 }
22683 else
22684 cursor_cleared_p = 0;
22685
22686 /* Update lines intersecting rectangle R. */
22687 first_overlapping_row = last_overlapping_row = NULL;
22688 for (row = w->current_matrix->rows;
22689 row->enabled_p;
22690 ++row)
22691 {
22692 int y0 = row->y;
22693 int y1 = MATRIX_ROW_BOTTOM_Y (row);
22694
22695 if ((y0 >= r.y && y0 < r.y + r.height)
22696 || (y1 > r.y && y1 < r.y + r.height)
22697 || (r.y >= y0 && r.y < y1)
22698 || (r.y + r.height > y0 && r.y + r.height < y1))
22699 {
22700 /* A header line may be overlapping, but there is no need
22701 to fix overlapping areas for them. KFS 2005-02-12 */
22702 if (row->overlapping_p && !row->mode_line_p)
22703 {
22704 if (first_overlapping_row == NULL)
22705 first_overlapping_row = row;
22706 last_overlapping_row = row;
22707 }
22708
22709 if (expose_line (w, row, &r))
22710 mouse_face_overwritten_p = 1;
22711 }
22712
22713 if (y1 >= yb)
22714 break;
22715 }
22716
22717 /* Display the mode line if there is one. */
22718 if (WINDOW_WANTS_MODELINE_P (w)
22719 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
22720 row->enabled_p)
22721 && row->y < r.y + r.height)
22722 {
22723 if (expose_line (w, row, &r))
22724 mouse_face_overwritten_p = 1;
22725 }
22726
22727 if (!w->pseudo_window_p)
22728 {
22729 /* Fix the display of overlapping rows. */
22730 if (first_overlapping_row)
22731 expose_overlaps (w, first_overlapping_row, last_overlapping_row);
22732
22733 /* Draw border between windows. */
22734 x_draw_vertical_border (w);
22735
22736 /* Turn the cursor on again. */
22737 if (cursor_cleared_p)
22738 update_window_cursor (w, 1);
22739 }
22740 }
22741
22742 return mouse_face_overwritten_p;
22743 }
22744
22745
22746
22747 /* Redraw (parts) of all windows in the window tree rooted at W that
22748 intersect R. R contains frame pixel coordinates. Value is
22749 non-zero if the exposure overwrites mouse-face. */
22750
22751 static int
22752 expose_window_tree (w, r)
22753 struct window *w;
22754 XRectangle *r;
22755 {
22756 struct frame *f = XFRAME (w->frame);
22757 int mouse_face_overwritten_p = 0;
22758
22759 while (w && !FRAME_GARBAGED_P (f))
22760 {
22761 if (!NILP (w->hchild))
22762 mouse_face_overwritten_p
22763 |= expose_window_tree (XWINDOW (w->hchild), r);
22764 else if (!NILP (w->vchild))
22765 mouse_face_overwritten_p
22766 |= expose_window_tree (XWINDOW (w->vchild), r);
22767 else
22768 mouse_face_overwritten_p |= expose_window (w, r);
22769
22770 w = NILP (w->next) ? NULL : XWINDOW (w->next);
22771 }
22772
22773 return mouse_face_overwritten_p;
22774 }
22775
22776
22777 /* EXPORT:
22778 Redisplay an exposed area of frame F. X and Y are the upper-left
22779 corner of the exposed rectangle. W and H are width and height of
22780 the exposed area. All are pixel values. W or H zero means redraw
22781 the entire frame. */
22782
22783 void
22784 expose_frame (f, x, y, w, h)
22785 struct frame *f;
22786 int x, y, w, h;
22787 {
22788 XRectangle r;
22789 int mouse_face_overwritten_p = 0;
22790
22791 TRACE ((stderr, "expose_frame "));
22792
22793 /* No need to redraw if frame will be redrawn soon. */
22794 if (FRAME_GARBAGED_P (f))
22795 {
22796 TRACE ((stderr, " garbaged\n"));
22797 return;
22798 }
22799
22800 /* If basic faces haven't been realized yet, there is no point in
22801 trying to redraw anything. This can happen when we get an expose
22802 event while Emacs is starting, e.g. by moving another window. */
22803 if (FRAME_FACE_CACHE (f) == NULL
22804 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
22805 {
22806 TRACE ((stderr, " no faces\n"));
22807 return;
22808 }
22809
22810 if (w == 0 || h == 0)
22811 {
22812 r.x = r.y = 0;
22813 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
22814 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
22815 }
22816 else
22817 {
22818 r.x = x;
22819 r.y = y;
22820 r.width = w;
22821 r.height = h;
22822 }
22823
22824 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
22825 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
22826
22827 if (WINDOWP (f->tool_bar_window))
22828 mouse_face_overwritten_p
22829 |= expose_window (XWINDOW (f->tool_bar_window), &r);
22830
22831 #ifdef HAVE_X_WINDOWS
22832 #ifndef MSDOS
22833 #ifndef USE_X_TOOLKIT
22834 if (WINDOWP (f->menu_bar_window))
22835 mouse_face_overwritten_p
22836 |= expose_window (XWINDOW (f->menu_bar_window), &r);
22837 #endif /* not USE_X_TOOLKIT */
22838 #endif
22839 #endif
22840
22841 /* Some window managers support a focus-follows-mouse style with
22842 delayed raising of frames. Imagine a partially obscured frame,
22843 and moving the mouse into partially obscured mouse-face on that
22844 frame. The visible part of the mouse-face will be highlighted,
22845 then the WM raises the obscured frame. With at least one WM, KDE
22846 2.1, Emacs is not getting any event for the raising of the frame
22847 (even tried with SubstructureRedirectMask), only Expose events.
22848 These expose events will draw text normally, i.e. not
22849 highlighted. Which means we must redo the highlight here.
22850 Subsume it under ``we love X''. --gerd 2001-08-15 */
22851 /* Included in Windows version because Windows most likely does not
22852 do the right thing if any third party tool offers
22853 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
22854 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
22855 {
22856 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
22857 if (f == dpyinfo->mouse_face_mouse_frame)
22858 {
22859 int x = dpyinfo->mouse_face_mouse_x;
22860 int y = dpyinfo->mouse_face_mouse_y;
22861 clear_mouse_face (dpyinfo);
22862 note_mouse_highlight (f, x, y);
22863 }
22864 }
22865 }
22866
22867
22868 /* EXPORT:
22869 Determine the intersection of two rectangles R1 and R2. Return
22870 the intersection in *RESULT. Value is non-zero if RESULT is not
22871 empty. */
22872
22873 int
22874 x_intersect_rectangles (r1, r2, result)
22875 XRectangle *r1, *r2, *result;
22876 {
22877 XRectangle *left, *right;
22878 XRectangle *upper, *lower;
22879 int intersection_p = 0;
22880
22881 /* Rearrange so that R1 is the left-most rectangle. */
22882 if (r1->x < r2->x)
22883 left = r1, right = r2;
22884 else
22885 left = r2, right = r1;
22886
22887 /* X0 of the intersection is right.x0, if this is inside R1,
22888 otherwise there is no intersection. */
22889 if (right->x <= left->x + left->width)
22890 {
22891 result->x = right->x;
22892
22893 /* The right end of the intersection is the minimum of the
22894 the right ends of left and right. */
22895 result->width = (min (left->x + left->width, right->x + right->width)
22896 - result->x);
22897
22898 /* Same game for Y. */
22899 if (r1->y < r2->y)
22900 upper = r1, lower = r2;
22901 else
22902 upper = r2, lower = r1;
22903
22904 /* The upper end of the intersection is lower.y0, if this is inside
22905 of upper. Otherwise, there is no intersection. */
22906 if (lower->y <= upper->y + upper->height)
22907 {
22908 result->y = lower->y;
22909
22910 /* The lower end of the intersection is the minimum of the lower
22911 ends of upper and lower. */
22912 result->height = (min (lower->y + lower->height,
22913 upper->y + upper->height)
22914 - result->y);
22915 intersection_p = 1;
22916 }
22917 }
22918
22919 return intersection_p;
22920 }
22921
22922 #endif /* HAVE_WINDOW_SYSTEM */
22923
22924 \f
22925 /***********************************************************************
22926 Initialization
22927 ***********************************************************************/
22928
22929 void
22930 syms_of_xdisp ()
22931 {
22932 Vwith_echo_area_save_vector = Qnil;
22933 staticpro (&Vwith_echo_area_save_vector);
22934
22935 Vmessage_stack = Qnil;
22936 staticpro (&Vmessage_stack);
22937
22938 Qinhibit_redisplay = intern ("inhibit-redisplay");
22939 staticpro (&Qinhibit_redisplay);
22940
22941 message_dolog_marker1 = Fmake_marker ();
22942 staticpro (&message_dolog_marker1);
22943 message_dolog_marker2 = Fmake_marker ();
22944 staticpro (&message_dolog_marker2);
22945 message_dolog_marker3 = Fmake_marker ();
22946 staticpro (&message_dolog_marker3);
22947
22948 #if GLYPH_DEBUG
22949 defsubr (&Sdump_frame_glyph_matrix);
22950 defsubr (&Sdump_glyph_matrix);
22951 defsubr (&Sdump_glyph_row);
22952 defsubr (&Sdump_tool_bar_row);
22953 defsubr (&Strace_redisplay);
22954 defsubr (&Strace_to_stderr);
22955 #endif
22956 #ifdef HAVE_WINDOW_SYSTEM
22957 defsubr (&Stool_bar_lines_needed);
22958 defsubr (&Slookup_image_map);
22959 #endif
22960 defsubr (&Sformat_mode_line);
22961
22962 staticpro (&Qmenu_bar_update_hook);
22963 Qmenu_bar_update_hook = intern ("menu-bar-update-hook");
22964
22965 staticpro (&Qoverriding_terminal_local_map);
22966 Qoverriding_terminal_local_map = intern ("overriding-terminal-local-map");
22967
22968 staticpro (&Qoverriding_local_map);
22969 Qoverriding_local_map = intern ("overriding-local-map");
22970
22971 staticpro (&Qwindow_scroll_functions);
22972 Qwindow_scroll_functions = intern ("window-scroll-functions");
22973
22974 staticpro (&Qredisplay_end_trigger_functions);
22975 Qredisplay_end_trigger_functions = intern ("redisplay-end-trigger-functions");
22976
22977 staticpro (&Qinhibit_point_motion_hooks);
22978 Qinhibit_point_motion_hooks = intern ("inhibit-point-motion-hooks");
22979
22980 QCdata = intern (":data");
22981 staticpro (&QCdata);
22982 Qdisplay = intern ("display");
22983 staticpro (&Qdisplay);
22984 Qspace_width = intern ("space-width");
22985 staticpro (&Qspace_width);
22986 Qraise = intern ("raise");
22987 staticpro (&Qraise);
22988 Qslice = intern ("slice");
22989 staticpro (&Qslice);
22990 Qspace = intern ("space");
22991 staticpro (&Qspace);
22992 Qmargin = intern ("margin");
22993 staticpro (&Qmargin);
22994 Qpointer = intern ("pointer");
22995 staticpro (&Qpointer);
22996 Qleft_margin = intern ("left-margin");
22997 staticpro (&Qleft_margin);
22998 Qright_margin = intern ("right-margin");
22999 staticpro (&Qright_margin);
23000 Qcenter = intern ("center");
23001 staticpro (&Qcenter);
23002 Qline_height = intern ("line-height");
23003 staticpro (&Qline_height);
23004 QCalign_to = intern (":align-to");
23005 staticpro (&QCalign_to);
23006 QCrelative_width = intern (":relative-width");
23007 staticpro (&QCrelative_width);
23008 QCrelative_height = intern (":relative-height");
23009 staticpro (&QCrelative_height);
23010 QCeval = intern (":eval");
23011 staticpro (&QCeval);
23012 QCpropertize = intern (":propertize");
23013 staticpro (&QCpropertize);
23014 QCfile = intern (":file");
23015 staticpro (&QCfile);
23016 Qfontified = intern ("fontified");
23017 staticpro (&Qfontified);
23018 Qfontification_functions = intern ("fontification-functions");
23019 staticpro (&Qfontification_functions);
23020 Qtrailing_whitespace = intern ("trailing-whitespace");
23021 staticpro (&Qtrailing_whitespace);
23022 Qescape_glyph = intern ("escape-glyph");
23023 staticpro (&Qescape_glyph);
23024 Qnobreak_space = intern ("nobreak-space");
23025 staticpro (&Qnobreak_space);
23026 Qimage = intern ("image");
23027 staticpro (&Qimage);
23028 QCmap = intern (":map");
23029 staticpro (&QCmap);
23030 QCpointer = intern (":pointer");
23031 staticpro (&QCpointer);
23032 Qrect = intern ("rect");
23033 staticpro (&Qrect);
23034 Qcircle = intern ("circle");
23035 staticpro (&Qcircle);
23036 Qpoly = intern ("poly");
23037 staticpro (&Qpoly);
23038 Qmessage_truncate_lines = intern ("message-truncate-lines");
23039 staticpro (&Qmessage_truncate_lines);
23040 Qgrow_only = intern ("grow-only");
23041 staticpro (&Qgrow_only);
23042 Qinhibit_menubar_update = intern ("inhibit-menubar-update");
23043 staticpro (&Qinhibit_menubar_update);
23044 Qinhibit_eval_during_redisplay = intern ("inhibit-eval-during-redisplay");
23045 staticpro (&Qinhibit_eval_during_redisplay);
23046 Qposition = intern ("position");
23047 staticpro (&Qposition);
23048 Qbuffer_position = intern ("buffer-position");
23049 staticpro (&Qbuffer_position);
23050 Qobject = intern ("object");
23051 staticpro (&Qobject);
23052 Qbar = intern ("bar");
23053 staticpro (&Qbar);
23054 Qhbar = intern ("hbar");
23055 staticpro (&Qhbar);
23056 Qbox = intern ("box");
23057 staticpro (&Qbox);
23058 Qhollow = intern ("hollow");
23059 staticpro (&Qhollow);
23060 Qhand = intern ("hand");
23061 staticpro (&Qhand);
23062 Qarrow = intern ("arrow");
23063 staticpro (&Qarrow);
23064 Qtext = intern ("text");
23065 staticpro (&Qtext);
23066 Qrisky_local_variable = intern ("risky-local-variable");
23067 staticpro (&Qrisky_local_variable);
23068 Qinhibit_free_realized_faces = intern ("inhibit-free-realized-faces");
23069 staticpro (&Qinhibit_free_realized_faces);
23070
23071 list_of_error = Fcons (Fcons (intern ("error"),
23072 Fcons (intern ("void-variable"), Qnil)),
23073 Qnil);
23074 staticpro (&list_of_error);
23075
23076 Qlast_arrow_position = intern ("last-arrow-position");
23077 staticpro (&Qlast_arrow_position);
23078 Qlast_arrow_string = intern ("last-arrow-string");
23079 staticpro (&Qlast_arrow_string);
23080
23081 Qoverlay_arrow_string = intern ("overlay-arrow-string");
23082 staticpro (&Qoverlay_arrow_string);
23083 Qoverlay_arrow_bitmap = intern ("overlay-arrow-bitmap");
23084 staticpro (&Qoverlay_arrow_bitmap);
23085
23086 echo_buffer[0] = echo_buffer[1] = Qnil;
23087 staticpro (&echo_buffer[0]);
23088 staticpro (&echo_buffer[1]);
23089
23090 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
23091 staticpro (&echo_area_buffer[0]);
23092 staticpro (&echo_area_buffer[1]);
23093
23094 Vmessages_buffer_name = build_string ("*Messages*");
23095 staticpro (&Vmessages_buffer_name);
23096
23097 mode_line_proptrans_alist = Qnil;
23098 staticpro (&mode_line_proptrans_alist);
23099 mode_line_string_list = Qnil;
23100 staticpro (&mode_line_string_list);
23101 mode_line_string_face = Qnil;
23102 staticpro (&mode_line_string_face);
23103 mode_line_string_face_prop = Qnil;
23104 staticpro (&mode_line_string_face_prop);
23105 Vmode_line_unwind_vector = Qnil;
23106 staticpro (&Vmode_line_unwind_vector);
23107
23108 help_echo_string = Qnil;
23109 staticpro (&help_echo_string);
23110 help_echo_object = Qnil;
23111 staticpro (&help_echo_object);
23112 help_echo_window = Qnil;
23113 staticpro (&help_echo_window);
23114 previous_help_echo_string = Qnil;
23115 staticpro (&previous_help_echo_string);
23116 help_echo_pos = -1;
23117
23118 #ifdef HAVE_WINDOW_SYSTEM
23119 DEFVAR_BOOL ("x-stretch-cursor", &x_stretch_cursor_p,
23120 doc: /* *Non-nil means draw block cursor as wide as the glyph under it.
23121 For example, if a block cursor is over a tab, it will be drawn as
23122 wide as that tab on the display. */);
23123 x_stretch_cursor_p = 0;
23124 #endif
23125
23126 DEFVAR_LISP ("show-trailing-whitespace", &Vshow_trailing_whitespace,
23127 doc: /* *Non-nil means highlight trailing whitespace.
23128 The face used for trailing whitespace is `trailing-whitespace'. */);
23129 Vshow_trailing_whitespace = Qnil;
23130
23131 DEFVAR_LISP ("nobreak-char-display", &Vnobreak_char_display,
23132 doc: /* *Control highlighting of nobreak space and soft hyphen.
23133 A value of t means highlight the character itself (for nobreak space,
23134 use face `nobreak-space').
23135 A value of nil means no highlighting.
23136 Other values mean display the escape glyph followed by an ordinary
23137 space or ordinary hyphen. */);
23138 Vnobreak_char_display = Qt;
23139
23140 DEFVAR_LISP ("void-text-area-pointer", &Vvoid_text_area_pointer,
23141 doc: /* *The pointer shape to show in void text areas.
23142 A value of nil means to show the text pointer. Other options are `arrow',
23143 `text', `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
23144 Vvoid_text_area_pointer = Qarrow;
23145
23146 DEFVAR_LISP ("inhibit-redisplay", &Vinhibit_redisplay,
23147 doc: /* Non-nil means don't actually do any redisplay.
23148 This is used for internal purposes. */);
23149 Vinhibit_redisplay = Qnil;
23150
23151 DEFVAR_LISP ("global-mode-string", &Vglobal_mode_string,
23152 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
23153 Vglobal_mode_string = Qnil;
23154
23155 DEFVAR_LISP ("overlay-arrow-position", &Voverlay_arrow_position,
23156 doc: /* Marker for where to display an arrow on top of the buffer text.
23157 This must be the beginning of a line in order to work.
23158 See also `overlay-arrow-string'. */);
23159 Voverlay_arrow_position = Qnil;
23160
23161 DEFVAR_LISP ("overlay-arrow-string", &Voverlay_arrow_string,
23162 doc: /* String to display as an arrow in non-window frames.
23163 See also `overlay-arrow-position'. */);
23164 Voverlay_arrow_string = build_string ("=>");
23165
23166 DEFVAR_LISP ("overlay-arrow-variable-list", &Voverlay_arrow_variable_list,
23167 doc: /* List of variables (symbols) which hold markers for overlay arrows.
23168 The symbols on this list are examined during redisplay to determine
23169 where to display overlay arrows. */);
23170 Voverlay_arrow_variable_list
23171 = Fcons (intern ("overlay-arrow-position"), Qnil);
23172
23173 DEFVAR_INT ("scroll-step", &scroll_step,
23174 doc: /* *The number of lines to try scrolling a window by when point moves out.
23175 If that fails to bring point back on frame, point is centered instead.
23176 If this is zero, point is always centered after it moves off frame.
23177 If you want scrolling to always be a line at a time, you should set
23178 `scroll-conservatively' to a large value rather than set this to 1. */);
23179
23180 DEFVAR_INT ("scroll-conservatively", &scroll_conservatively,
23181 doc: /* *Scroll up to this many lines, to bring point back on screen.
23182 A value of zero means to scroll the text to center point vertically
23183 in the window. */);
23184 scroll_conservatively = 0;
23185
23186 DEFVAR_INT ("scroll-margin", &scroll_margin,
23187 doc: /* *Number of lines of margin at the top and bottom of a window.
23188 Recenter the window whenever point gets within this many lines
23189 of the top or bottom of the window. */);
23190 scroll_margin = 0;
23191
23192 DEFVAR_LISP ("display-pixels-per-inch", &Vdisplay_pixels_per_inch,
23193 doc: /* Pixels per inch on current display.
23194 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
23195 Vdisplay_pixels_per_inch = make_float (72.0);
23196
23197 #if GLYPH_DEBUG
23198 DEFVAR_INT ("debug-end-pos", &debug_end_pos, doc: /* Don't ask. */);
23199 #endif
23200
23201 DEFVAR_BOOL ("truncate-partial-width-windows",
23202 &truncate_partial_width_windows,
23203 doc: /* *Non-nil means truncate lines in all windows less than full frame wide. */);
23204 truncate_partial_width_windows = 1;
23205
23206 DEFVAR_BOOL ("mode-line-inverse-video", &mode_line_inverse_video,
23207 doc: /* nil means display the mode-line/header-line/menu-bar in the default face.
23208 Any other value means to use the appropriate face, `mode-line',
23209 `header-line', or `menu' respectively. */);
23210 mode_line_inverse_video = 1;
23211
23212 DEFVAR_LISP ("line-number-display-limit", &Vline_number_display_limit,
23213 doc: /* *Maximum buffer size for which line number should be displayed.
23214 If the buffer is bigger than this, the line number does not appear
23215 in the mode line. A value of nil means no limit. */);
23216 Vline_number_display_limit = Qnil;
23217
23218 DEFVAR_INT ("line-number-display-limit-width",
23219 &line_number_display_limit_width,
23220 doc: /* *Maximum line width (in characters) for line number display.
23221 If the average length of the lines near point is bigger than this, then the
23222 line number may be omitted from the mode line. */);
23223 line_number_display_limit_width = 200;
23224
23225 DEFVAR_BOOL ("highlight-nonselected-windows", &highlight_nonselected_windows,
23226 doc: /* *Non-nil means highlight region even in nonselected windows. */);
23227 highlight_nonselected_windows = 0;
23228
23229 DEFVAR_BOOL ("multiple-frames", &multiple_frames,
23230 doc: /* Non-nil if more than one frame is visible on this display.
23231 Minibuffer-only frames don't count, but iconified frames do.
23232 This variable is not guaranteed to be accurate except while processing
23233 `frame-title-format' and `icon-title-format'. */);
23234
23235 DEFVAR_LISP ("frame-title-format", &Vframe_title_format,
23236 doc: /* Template for displaying the title bar of visible frames.
23237 \(Assuming the window manager supports this feature.)
23238 This variable has the same structure as `mode-line-format' (which see),
23239 and is used only on frames for which no explicit name has been set
23240 \(see `modify-frame-parameters'). */);
23241
23242 DEFVAR_LISP ("icon-title-format", &Vicon_title_format,
23243 doc: /* Template for displaying the title bar of an iconified frame.
23244 \(Assuming the window manager supports this feature.)
23245 This variable has the same structure as `mode-line-format' (which see),
23246 and is used only on frames for which no explicit name has been set
23247 \(see `modify-frame-parameters'). */);
23248 Vicon_title_format
23249 = Vframe_title_format
23250 = Fcons (intern ("multiple-frames"),
23251 Fcons (build_string ("%b"),
23252 Fcons (Fcons (empty_string,
23253 Fcons (intern ("invocation-name"),
23254 Fcons (build_string ("@"),
23255 Fcons (intern ("system-name"),
23256 Qnil)))),
23257 Qnil)));
23258
23259 DEFVAR_LISP ("message-log-max", &Vmessage_log_max,
23260 doc: /* Maximum number of lines to keep in the message log buffer.
23261 If nil, disable message logging. If t, log messages but don't truncate
23262 the buffer when it becomes large. */);
23263 Vmessage_log_max = make_number (50);
23264
23265 DEFVAR_LISP ("window-size-change-functions", &Vwindow_size_change_functions,
23266 doc: /* Functions called before redisplay, if window sizes have changed.
23267 The value should be a list of functions that take one argument.
23268 Just before redisplay, for each frame, if any of its windows have changed
23269 size since the last redisplay, or have been split or deleted,
23270 all the functions in the list are called, with the frame as argument. */);
23271 Vwindow_size_change_functions = Qnil;
23272
23273 DEFVAR_LISP ("window-scroll-functions", &Vwindow_scroll_functions,
23274 doc: /* List of functions to call before redisplaying a window with scrolling.
23275 Each function is called with two arguments, the window
23276 and its new display-start position. Note that the value of `window-end'
23277 is not valid when these functions are called. */);
23278 Vwindow_scroll_functions = Qnil;
23279
23280 DEFVAR_BOOL ("mouse-autoselect-window", &mouse_autoselect_window,
23281 doc: /* *Non-nil means autoselect window with mouse pointer. */);
23282 mouse_autoselect_window = 0;
23283
23284 DEFVAR_BOOL ("auto-resize-tool-bars", &auto_resize_tool_bars_p,
23285 doc: /* *Non-nil means automatically resize tool-bars.
23286 This increases a tool-bar's height if not all tool-bar items are visible.
23287 It decreases a tool-bar's height when it would display blank lines
23288 otherwise. */);
23289 auto_resize_tool_bars_p = 1;
23290
23291 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", &auto_raise_tool_bar_buttons_p,
23292 doc: /* *Non-nil means raise tool-bar buttons when the mouse moves over them. */);
23293 auto_raise_tool_bar_buttons_p = 1;
23294
23295 DEFVAR_BOOL ("make-cursor-line-fully-visible", &make_cursor_line_fully_visible_p,
23296 doc: /* *Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
23297 make_cursor_line_fully_visible_p = 1;
23298
23299 DEFVAR_LISP ("tool-bar-button-margin", &Vtool_bar_button_margin,
23300 doc: /* *Margin around tool-bar buttons in pixels.
23301 If an integer, use that for both horizontal and vertical margins.
23302 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
23303 HORZ specifying the horizontal margin, and VERT specifying the
23304 vertical margin. */);
23305 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
23306
23307 DEFVAR_INT ("tool-bar-button-relief", &tool_bar_button_relief,
23308 doc: /* *Relief thickness of tool-bar buttons. */);
23309 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
23310
23311 DEFVAR_LISP ("fontification-functions", &Vfontification_functions,
23312 doc: /* List of functions to call to fontify regions of text.
23313 Each function is called with one argument POS. Functions must
23314 fontify a region starting at POS in the current buffer, and give
23315 fontified regions the property `fontified'. */);
23316 Vfontification_functions = Qnil;
23317 Fmake_variable_buffer_local (Qfontification_functions);
23318
23319 DEFVAR_BOOL ("unibyte-display-via-language-environment",
23320 &unibyte_display_via_language_environment,
23321 doc: /* *Non-nil means display unibyte text according to language environment.
23322 Specifically this means that unibyte non-ASCII characters
23323 are displayed by converting them to the equivalent multibyte characters
23324 according to the current language environment. As a result, they are
23325 displayed according to the current fontset. */);
23326 unibyte_display_via_language_environment = 0;
23327
23328 DEFVAR_LISP ("max-mini-window-height", &Vmax_mini_window_height,
23329 doc: /* *Maximum height for resizing mini-windows.
23330 If a float, it specifies a fraction of the mini-window frame's height.
23331 If an integer, it specifies a number of lines. */);
23332 Vmax_mini_window_height = make_float (0.25);
23333
23334 DEFVAR_LISP ("resize-mini-windows", &Vresize_mini_windows,
23335 doc: /* *How to resize mini-windows.
23336 A value of nil means don't automatically resize mini-windows.
23337 A value of t means resize them to fit the text displayed in them.
23338 A value of `grow-only', the default, means let mini-windows grow
23339 only, until their display becomes empty, at which point the windows
23340 go back to their normal size. */);
23341 Vresize_mini_windows = Qgrow_only;
23342
23343 DEFVAR_LISP ("blink-cursor-alist", &Vblink_cursor_alist,
23344 doc: /* Alist specifying how to blink the cursor off.
23345 Each element has the form (ON-STATE . OFF-STATE). Whenever the
23346 `cursor-type' frame-parameter or variable equals ON-STATE,
23347 comparing using `equal', Emacs uses OFF-STATE to specify
23348 how to blink it off. */);
23349 Vblink_cursor_alist = Qnil;
23350
23351 DEFVAR_BOOL ("auto-hscroll-mode", &automatic_hscrolling_p,
23352 doc: /* *Non-nil means scroll the display automatically to make point visible. */);
23353 automatic_hscrolling_p = 1;
23354
23355 DEFVAR_INT ("hscroll-margin", &hscroll_margin,
23356 doc: /* *How many columns away from the window edge point is allowed to get
23357 before automatic hscrolling will horizontally scroll the window. */);
23358 hscroll_margin = 5;
23359
23360 DEFVAR_LISP ("hscroll-step", &Vhscroll_step,
23361 doc: /* *How many columns to scroll the window when point gets too close to the edge.
23362 When point is less than `automatic-hscroll-margin' columns from the window
23363 edge, automatic hscrolling will scroll the window by the amount of columns
23364 determined by this variable. If its value is a positive integer, scroll that
23365 many columns. If it's a positive floating-point number, it specifies the
23366 fraction of the window's width to scroll. If it's nil or zero, point will be
23367 centered horizontally after the scroll. Any other value, including negative
23368 numbers, are treated as if the value were zero.
23369
23370 Automatic hscrolling always moves point outside the scroll margin, so if
23371 point was more than scroll step columns inside the margin, the window will
23372 scroll more than the value given by the scroll step.
23373
23374 Note that the lower bound for automatic hscrolling specified by `scroll-left'
23375 and `scroll-right' overrides this variable's effect. */);
23376 Vhscroll_step = make_number (0);
23377
23378 DEFVAR_BOOL ("message-truncate-lines", &message_truncate_lines,
23379 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
23380 Bind this around calls to `message' to let it take effect. */);
23381 message_truncate_lines = 0;
23382
23383 DEFVAR_LISP ("menu-bar-update-hook", &Vmenu_bar_update_hook,
23384 doc: /* Normal hook run to update the menu bar definitions.
23385 Redisplay runs this hook before it redisplays the menu bar.
23386 This is used to update submenus such as Buffers,
23387 whose contents depend on various data. */);
23388 Vmenu_bar_update_hook = Qnil;
23389
23390 DEFVAR_BOOL ("inhibit-menubar-update", &inhibit_menubar_update,
23391 doc: /* Non-nil means don't update menu bars. Internal use only. */);
23392 inhibit_menubar_update = 0;
23393
23394 DEFVAR_BOOL ("inhibit-eval-during-redisplay", &inhibit_eval_during_redisplay,
23395 doc: /* Non-nil means don't eval Lisp during redisplay. */);
23396 inhibit_eval_during_redisplay = 0;
23397
23398 DEFVAR_BOOL ("inhibit-free-realized-faces", &inhibit_free_realized_faces,
23399 doc: /* Non-nil means don't free realized faces. Internal use only. */);
23400 inhibit_free_realized_faces = 0;
23401
23402 #if GLYPH_DEBUG
23403 DEFVAR_BOOL ("inhibit-try-window-id", &inhibit_try_window_id,
23404 doc: /* Inhibit try_window_id display optimization. */);
23405 inhibit_try_window_id = 0;
23406
23407 DEFVAR_BOOL ("inhibit-try-window-reusing", &inhibit_try_window_reusing,
23408 doc: /* Inhibit try_window_reusing display optimization. */);
23409 inhibit_try_window_reusing = 0;
23410
23411 DEFVAR_BOOL ("inhibit-try-cursor-movement", &inhibit_try_cursor_movement,
23412 doc: /* Inhibit try_cursor_movement display optimization. */);
23413 inhibit_try_cursor_movement = 0;
23414 #endif /* GLYPH_DEBUG */
23415 }
23416
23417
23418 /* Initialize this module when Emacs starts. */
23419
23420 void
23421 init_xdisp ()
23422 {
23423 Lisp_Object root_window;
23424 struct window *mini_w;
23425
23426 current_header_line_height = current_mode_line_height = -1;
23427
23428 CHARPOS (this_line_start_pos) = 0;
23429
23430 mini_w = XWINDOW (minibuf_window);
23431 root_window = FRAME_ROOT_WINDOW (XFRAME (WINDOW_FRAME (mini_w)));
23432
23433 if (!noninteractive)
23434 {
23435 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (root_window)));
23436 int i;
23437
23438 XWINDOW (root_window)->top_line = make_number (FRAME_TOP_MARGIN (f));
23439 set_window_height (root_window,
23440 FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f),
23441 0);
23442 mini_w->top_line = make_number (FRAME_LINES (f) - 1);
23443 set_window_height (minibuf_window, 1, 0);
23444
23445 XWINDOW (root_window)->total_cols = make_number (FRAME_COLS (f));
23446 mini_w->total_cols = make_number (FRAME_COLS (f));
23447
23448 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
23449 scratch_glyph_row.glyphs[TEXT_AREA + 1]
23450 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
23451
23452 /* The default ellipsis glyphs `...'. */
23453 for (i = 0; i < 3; ++i)
23454 default_invis_vector[i] = make_number ('.');
23455 }
23456
23457 {
23458 /* Allocate the buffer for frame titles.
23459 Also used for `format-mode-line'. */
23460 int size = 100;
23461 mode_line_noprop_buf = (char *) xmalloc (size);
23462 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
23463 mode_line_noprop_ptr = mode_line_noprop_buf;
23464 mode_line_target = MODE_LINE_DISPLAY;
23465 }
23466
23467 help_echo_showing_p = 0;
23468 }
23469
23470
23471 /* arch-tag: eacc864d-bb6a-4b74-894a-1a4399a1358b
23472 (do not change this comment) */