]> code.delx.au - gnu-emacs/blob - src/xdisp.c
(archive-l-e): New optional argument `float' means generate a float value.
[gnu-emacs] / src / xdisp.c
1 /* Display generation from window structure and buffer text.
2 Copyright (C) 1985, 1986, 1987, 1988, 1993, 1994, 1995,
3 1997, 1998, 1999, 2000, 2001, 2002, 2003,
4 2004, 2005, 2006 Free Software Foundation, Inc.
5
6 This file is part of GNU Emacs.
7
8 GNU Emacs is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
11 any later version.
12
13 GNU Emacs is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GNU Emacs; see the file COPYING. If not, write to
20 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 Boston, MA 02110-1301, USA. */
22
23 /* New redisplay written by Gerd Moellmann <gerd@gnu.org>.
24
25 Redisplay.
26
27 Emacs separates the task of updating the display from code
28 modifying global state, e.g. buffer text. This way functions
29 operating on buffers don't also have to be concerned with updating
30 the display.
31
32 Updating the display is triggered by the Lisp interpreter when it
33 decides it's time to do it. This is done either automatically for
34 you as part of the interpreter's command loop or as the result of
35 calling Lisp functions like `sit-for'. The C function `redisplay'
36 in xdisp.c is the only entry into the inner redisplay code. (Or,
37 let's say almost---see the description of direct update
38 operations, below.)
39
40 The following diagram shows how redisplay code is invoked. As you
41 can see, Lisp calls redisplay and vice versa. Under window systems
42 like X, some portions of the redisplay code are also called
43 asynchronously during mouse movement or expose events. It is very
44 important that these code parts do NOT use the C library (malloc,
45 free) because many C libraries under Unix are not reentrant. They
46 may also NOT call functions of the Lisp interpreter which could
47 change the interpreter's state. If you don't follow these rules,
48 you will encounter bugs which are very hard to explain.
49
50 (Direct functions, see below)
51 direct_output_for_insert,
52 direct_forward_char (dispnew.c)
53 +---------------------------------+
54 | |
55 | V
56 +--------------+ redisplay +----------------+
57 | Lisp machine |---------------->| Redisplay code |<--+
58 +--------------+ (xdisp.c) +----------------+ |
59 ^ | |
60 +----------------------------------+ |
61 Don't use this path when called |
62 asynchronously! |
63 |
64 expose_window (asynchronous) |
65 |
66 X expose events -----+
67
68 What does redisplay do? Obviously, it has to figure out somehow what
69 has been changed since the last time the display has been updated,
70 and to make these changes visible. Preferably it would do that in
71 a moderately intelligent way, i.e. fast.
72
73 Changes in buffer text can be deduced from window and buffer
74 structures, and from some global variables like `beg_unchanged' and
75 `end_unchanged'. The contents of the display are additionally
76 recorded in a `glyph matrix', a two-dimensional matrix of glyph
77 structures. Each row in such a matrix corresponds to a line on the
78 display, and each glyph in a row corresponds to a column displaying
79 a character, an image, or what else. This matrix is called the
80 `current glyph matrix' or `current matrix' in redisplay
81 terminology.
82
83 For buffer parts that have been changed since the last update, a
84 second glyph matrix is constructed, the so called `desired glyph
85 matrix' or short `desired matrix'. Current and desired matrix are
86 then compared to find a cheap way to update the display, e.g. by
87 reusing part of the display by scrolling lines.
88
89
90 Direct operations.
91
92 You will find a lot of redisplay optimizations when you start
93 looking at the innards of redisplay. The overall goal of all these
94 optimizations is to make redisplay fast because it is done
95 frequently.
96
97 Two optimizations are not found in xdisp.c. These are the direct
98 operations mentioned above. As the name suggests they follow a
99 different principle than the rest of redisplay. Instead of
100 building a desired matrix and then comparing it with the current
101 display, they perform their actions directly on the display and on
102 the current matrix.
103
104 One direct operation updates the display after one character has
105 been entered. The other one moves the cursor by one position
106 forward or backward. You find these functions under the names
107 `direct_output_for_insert' and `direct_output_forward_char' in
108 dispnew.c.
109
110
111 Desired matrices.
112
113 Desired matrices are always built per Emacs window. The function
114 `display_line' is the central function to look at if you are
115 interested. It constructs one row in a desired matrix given an
116 iterator structure containing both a buffer position and a
117 description of the environment in which the text is to be
118 displayed. But this is too early, read on.
119
120 Characters and pixmaps displayed for a range of buffer text depend
121 on various settings of buffers and windows, on overlays and text
122 properties, on display tables, on selective display. The good news
123 is that all this hairy stuff is hidden behind a small set of
124 interface functions taking an iterator structure (struct it)
125 argument.
126
127 Iteration over things to be displayed is then simple. It is
128 started by initializing an iterator with a call to init_iterator.
129 Calls to get_next_display_element fill the iterator structure with
130 relevant information about the next thing to display. Calls to
131 set_iterator_to_next move the iterator to the next thing.
132
133 Besides this, an iterator also contains information about the
134 display environment in which glyphs for display elements are to be
135 produced. It has fields for the width and height of the display,
136 the information whether long lines are truncated or continued, a
137 current X and Y position, and lots of other stuff you can better
138 see in dispextern.h.
139
140 Glyphs in a desired matrix are normally constructed in a loop
141 calling get_next_display_element and then produce_glyphs. The call
142 to produce_glyphs will fill the iterator structure with pixel
143 information about the element being displayed and at the same time
144 produce glyphs for it. If the display element fits on the line
145 being displayed, set_iterator_to_next is called next, otherwise the
146 glyphs produced are discarded.
147
148
149 Frame matrices.
150
151 That just couldn't be all, could it? What about terminal types not
152 supporting operations on sub-windows of the screen? To update the
153 display on such a terminal, window-based glyph matrices are not
154 well suited. To be able to reuse part of the display (scrolling
155 lines up and down), we must instead have a view of the whole
156 screen. This is what `frame matrices' are for. They are a trick.
157
158 Frames on terminals like above have a glyph pool. Windows on such
159 a frame sub-allocate their glyph memory from their frame's glyph
160 pool. The frame itself is given its own glyph matrices. By
161 coincidence---or maybe something else---rows in window glyph
162 matrices are slices of corresponding rows in frame matrices. Thus
163 writing to window matrices implicitly updates a frame matrix which
164 provides us with the view of the whole screen that we originally
165 wanted to have without having to move many bytes around. To be
166 honest, there is a little bit more done, but not much more. If you
167 plan to extend that code, take a look at dispnew.c. The function
168 build_frame_matrix is a good starting point. */
169
170 #include <config.h>
171 #include <stdio.h>
172
173 #include "lisp.h"
174 #include "keyboard.h"
175 #include "frame.h"
176 #include "window.h"
177 #include "termchar.h"
178 #include "dispextern.h"
179 #include "buffer.h"
180 #include "charset.h"
181 #include "indent.h"
182 #include "commands.h"
183 #include "keymap.h"
184 #include "macros.h"
185 #include "disptab.h"
186 #include "termhooks.h"
187 #include "intervals.h"
188 #include "coding.h"
189 #include "process.h"
190 #include "region-cache.h"
191 #include "fontset.h"
192 #include "blockinput.h"
193
194 #ifdef HAVE_X_WINDOWS
195 #include "xterm.h"
196 #endif
197 #ifdef WINDOWSNT
198 #include "w32term.h"
199 #endif
200 #ifdef MAC_OS
201 #include "macterm.h"
202 #endif
203
204 #ifndef FRAME_X_OUTPUT
205 #define FRAME_X_OUTPUT(f) ((f)->output_data.x)
206 #endif
207
208 #define INFINITY 10000000
209
210 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
211 || defined (USE_GTK)
212 extern void set_frame_menubar P_ ((struct frame *f, int, int));
213 extern int pending_menu_activation;
214 #endif
215
216 extern int interrupt_input;
217 extern int command_loop_level;
218
219 extern Lisp_Object do_mouse_tracking;
220
221 extern int minibuffer_auto_raise;
222 extern Lisp_Object Vminibuffer_list;
223
224 extern Lisp_Object Qface;
225 extern Lisp_Object Qmode_line, Qmode_line_inactive, Qheader_line;
226
227 extern Lisp_Object Voverriding_local_map;
228 extern Lisp_Object Voverriding_local_map_menu_flag;
229 extern Lisp_Object Qmenu_item;
230 extern Lisp_Object Qwhen;
231 extern Lisp_Object Qhelp_echo;
232
233 Lisp_Object Qoverriding_local_map, Qoverriding_terminal_local_map;
234 Lisp_Object Qwindow_scroll_functions, Vwindow_scroll_functions;
235 Lisp_Object Qredisplay_end_trigger_functions, Vredisplay_end_trigger_functions;
236 Lisp_Object Qinhibit_point_motion_hooks;
237 Lisp_Object QCeval, QCfile, QCdata, QCpropertize;
238 Lisp_Object Qfontified;
239 Lisp_Object Qgrow_only;
240 Lisp_Object Qinhibit_eval_during_redisplay;
241 Lisp_Object Qbuffer_position, Qposition, Qobject;
242
243 /* Cursor shapes */
244 Lisp_Object Qbar, Qhbar, Qbox, Qhollow;
245
246 /* Pointer shapes */
247 Lisp_Object Qarrow, Qhand, Qtext;
248
249 Lisp_Object Qrisky_local_variable;
250
251 /* Holds the list (error). */
252 Lisp_Object list_of_error;
253
254 /* Functions called to fontify regions of text. */
255
256 Lisp_Object Vfontification_functions;
257 Lisp_Object Qfontification_functions;
258
259 /* Non-zero means automatically select any window when the mouse
260 cursor moves into it. */
261 int mouse_autoselect_window;
262
263 /* Non-zero means draw tool bar buttons raised when the mouse moves
264 over them. */
265
266 int auto_raise_tool_bar_buttons_p;
267
268 /* Non-zero means to reposition window if cursor line is only partially visible. */
269
270 int make_cursor_line_fully_visible_p;
271
272 /* Margin below tool bar in pixels. 0 or nil means no margin.
273 If value is `internal-border-width' or `border-width',
274 the corresponding frame parameter is used. */
275
276 Lisp_Object Vtool_bar_border;
277
278 /* Margin around tool bar buttons in pixels. */
279
280 Lisp_Object Vtool_bar_button_margin;
281
282 /* Thickness of shadow to draw around tool bar buttons. */
283
284 EMACS_INT tool_bar_button_relief;
285
286 /* Non-zero means automatically resize tool-bars so that all tool-bar
287 items are visible, and no blank lines remain. */
288
289 int auto_resize_tool_bars_p;
290
291 /* Non-zero means draw block and hollow cursor as wide as the glyph
292 under it. For example, if a block cursor is over a tab, it will be
293 drawn as wide as that tab on the display. */
294
295 int x_stretch_cursor_p;
296
297 /* Non-nil means don't actually do any redisplay. */
298
299 Lisp_Object Vinhibit_redisplay, Qinhibit_redisplay;
300
301 /* Non-zero means Lisp evaluation during redisplay is inhibited. */
302
303 int inhibit_eval_during_redisplay;
304
305 /* Names of text properties relevant for redisplay. */
306
307 Lisp_Object Qdisplay;
308 extern Lisp_Object Qface, Qinvisible, Qwidth;
309
310 /* Symbols used in text property values. */
311
312 Lisp_Object Vdisplay_pixels_per_inch;
313 Lisp_Object Qspace, QCalign_to, QCrelative_width, QCrelative_height;
314 Lisp_Object Qleft_margin, Qright_margin, Qspace_width, Qraise;
315 Lisp_Object Qslice;
316 Lisp_Object Qcenter;
317 Lisp_Object Qmargin, Qpointer;
318 Lisp_Object Qline_height;
319 extern Lisp_Object Qheight;
320 extern Lisp_Object QCwidth, QCheight, QCascent;
321 extern Lisp_Object Qscroll_bar;
322 extern Lisp_Object Qcursor;
323
324 /* Non-nil means highlight trailing whitespace. */
325
326 Lisp_Object Vshow_trailing_whitespace;
327
328 /* Non-nil means escape non-break space and hyphens. */
329
330 Lisp_Object Vnobreak_char_display;
331
332 #ifdef HAVE_WINDOW_SYSTEM
333 extern Lisp_Object Voverflow_newline_into_fringe;
334
335 /* Test if overflow newline into fringe. Called with iterator IT
336 at or past right window margin, and with IT->current_x set. */
337
338 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(it) \
339 (!NILP (Voverflow_newline_into_fringe) \
340 && FRAME_WINDOW_P (it->f) \
341 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) > 0 \
342 && it->current_x == it->last_visible_x)
343
344 #endif /* HAVE_WINDOW_SYSTEM */
345
346 /* Non-nil means show the text cursor in void text areas
347 i.e. in blank areas after eol and eob. This used to be
348 the default in 21.3. */
349
350 Lisp_Object Vvoid_text_area_pointer;
351
352 /* Name of the face used to highlight trailing whitespace. */
353
354 Lisp_Object Qtrailing_whitespace;
355
356 /* Name and number of the face used to highlight escape glyphs. */
357
358 Lisp_Object Qescape_glyph;
359
360 /* Name and number of the face used to highlight non-breaking spaces. */
361
362 Lisp_Object Qnobreak_space;
363
364 /* The symbol `image' which is the car of the lists used to represent
365 images in Lisp. */
366
367 Lisp_Object Qimage;
368
369 /* The image map types. */
370 Lisp_Object QCmap, QCpointer;
371 Lisp_Object Qrect, Qcircle, Qpoly;
372
373 /* Non-zero means print newline to stdout before next mini-buffer
374 message. */
375
376 int noninteractive_need_newline;
377
378 /* Non-zero means print newline to message log before next message. */
379
380 static int message_log_need_newline;
381
382 /* Three markers that message_dolog uses.
383 It could allocate them itself, but that causes trouble
384 in handling memory-full errors. */
385 static Lisp_Object message_dolog_marker1;
386 static Lisp_Object message_dolog_marker2;
387 static Lisp_Object message_dolog_marker3;
388 \f
389 /* The buffer position of the first character appearing entirely or
390 partially on the line of the selected window which contains the
391 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
392 redisplay optimization in redisplay_internal. */
393
394 static struct text_pos this_line_start_pos;
395
396 /* Number of characters past the end of the line above, including the
397 terminating newline. */
398
399 static struct text_pos this_line_end_pos;
400
401 /* The vertical positions and the height of this line. */
402
403 static int this_line_vpos;
404 static int this_line_y;
405 static int this_line_pixel_height;
406
407 /* X position at which this display line starts. Usually zero;
408 negative if first character is partially visible. */
409
410 static int this_line_start_x;
411
412 /* Buffer that this_line_.* variables are referring to. */
413
414 static struct buffer *this_line_buffer;
415
416 /* Nonzero means truncate lines in all windows less wide than the
417 frame. */
418
419 int truncate_partial_width_windows;
420
421 /* A flag to control how to display unibyte 8-bit character. */
422
423 int unibyte_display_via_language_environment;
424
425 /* Nonzero means we have more than one non-mini-buffer-only frame.
426 Not guaranteed to be accurate except while parsing
427 frame-title-format. */
428
429 int multiple_frames;
430
431 Lisp_Object Vglobal_mode_string;
432
433
434 /* List of variables (symbols) which hold markers for overlay arrows.
435 The symbols on this list are examined during redisplay to determine
436 where to display overlay arrows. */
437
438 Lisp_Object Voverlay_arrow_variable_list;
439
440 /* Marker for where to display an arrow on top of the buffer text. */
441
442 Lisp_Object Voverlay_arrow_position;
443
444 /* String to display for the arrow. Only used on terminal frames. */
445
446 Lisp_Object Voverlay_arrow_string;
447
448 /* Values of those variables at last redisplay are stored as
449 properties on `overlay-arrow-position' symbol. However, if
450 Voverlay_arrow_position is a marker, last-arrow-position is its
451 numerical position. */
452
453 Lisp_Object Qlast_arrow_position, Qlast_arrow_string;
454
455 /* Alternative overlay-arrow-string and overlay-arrow-bitmap
456 properties on a symbol in overlay-arrow-variable-list. */
457
458 Lisp_Object Qoverlay_arrow_string, Qoverlay_arrow_bitmap;
459
460 /* Like mode-line-format, but for the title bar on a visible frame. */
461
462 Lisp_Object Vframe_title_format;
463
464 /* Like mode-line-format, but for the title bar on an iconified frame. */
465
466 Lisp_Object Vicon_title_format;
467
468 /* List of functions to call when a window's size changes. These
469 functions get one arg, a frame on which one or more windows' sizes
470 have changed. */
471
472 static Lisp_Object Vwindow_size_change_functions;
473
474 Lisp_Object Qmenu_bar_update_hook, Vmenu_bar_update_hook;
475
476 /* Nonzero if an overlay arrow has been displayed in this window. */
477
478 static int overlay_arrow_seen;
479
480 /* Nonzero means highlight the region even in nonselected windows. */
481
482 int highlight_nonselected_windows;
483
484 /* If cursor motion alone moves point off frame, try scrolling this
485 many lines up or down if that will bring it back. */
486
487 static EMACS_INT scroll_step;
488
489 /* Nonzero means scroll just far enough to bring point back on the
490 screen, when appropriate. */
491
492 static EMACS_INT scroll_conservatively;
493
494 /* Recenter the window whenever point gets within this many lines of
495 the top or bottom of the window. This value is translated into a
496 pixel value by multiplying it with FRAME_LINE_HEIGHT, which means
497 that there is really a fixed pixel height scroll margin. */
498
499 EMACS_INT scroll_margin;
500
501 /* Number of windows showing the buffer of the selected window (or
502 another buffer with the same base buffer). keyboard.c refers to
503 this. */
504
505 int buffer_shared;
506
507 /* Vector containing glyphs for an ellipsis `...'. */
508
509 static Lisp_Object default_invis_vector[3];
510
511 /* Zero means display the mode-line/header-line/menu-bar in the default face
512 (this slightly odd definition is for compatibility with previous versions
513 of emacs), non-zero means display them using their respective faces.
514
515 This variable is deprecated. */
516
517 int mode_line_inverse_video;
518
519 /* Prompt to display in front of the mini-buffer contents. */
520
521 Lisp_Object minibuf_prompt;
522
523 /* Width of current mini-buffer prompt. Only set after display_line
524 of the line that contains the prompt. */
525
526 int minibuf_prompt_width;
527
528 /* This is the window where the echo area message was displayed. It
529 is always a mini-buffer window, but it may not be the same window
530 currently active as a mini-buffer. */
531
532 Lisp_Object echo_area_window;
533
534 /* List of pairs (MESSAGE . MULTIBYTE). The function save_message
535 pushes the current message and the value of
536 message_enable_multibyte on the stack, the function restore_message
537 pops the stack and displays MESSAGE again. */
538
539 Lisp_Object Vmessage_stack;
540
541 /* Nonzero means multibyte characters were enabled when the echo area
542 message was specified. */
543
544 int message_enable_multibyte;
545
546 /* Nonzero if we should redraw the mode lines on the next redisplay. */
547
548 int update_mode_lines;
549
550 /* Nonzero if window sizes or contents have changed since last
551 redisplay that finished. */
552
553 int windows_or_buffers_changed;
554
555 /* Nonzero means a frame's cursor type has been changed. */
556
557 int cursor_type_changed;
558
559 /* Nonzero after display_mode_line if %l was used and it displayed a
560 line number. */
561
562 int line_number_displayed;
563
564 /* Maximum buffer size for which to display line numbers. */
565
566 Lisp_Object Vline_number_display_limit;
567
568 /* Line width to consider when repositioning for line number display. */
569
570 static EMACS_INT line_number_display_limit_width;
571
572 /* Number of lines to keep in the message log buffer. t means
573 infinite. nil means don't log at all. */
574
575 Lisp_Object Vmessage_log_max;
576
577 /* The name of the *Messages* buffer, a string. */
578
579 static Lisp_Object Vmessages_buffer_name;
580
581 /* Index 0 is the buffer that holds the current (desired) echo area message,
582 or nil if none is desired right now.
583
584 Index 1 is the buffer that holds the previously displayed echo area message,
585 or nil to indicate no message. This is normally what's on the screen now.
586
587 These two can point to the same buffer. That happens when the last
588 message output by the user (or made by echoing) has been displayed. */
589
590 Lisp_Object echo_area_buffer[2];
591
592 /* Permanent pointers to the two buffers that are used for echo area
593 purposes. Once the two buffers are made, and their pointers are
594 placed here, these two slots remain unchanged unless those buffers
595 need to be created afresh. */
596
597 static Lisp_Object echo_buffer[2];
598
599 /* A vector saved used in with_area_buffer to reduce consing. */
600
601 static Lisp_Object Vwith_echo_area_save_vector;
602
603 /* Non-zero means display_echo_area should display the last echo area
604 message again. Set by redisplay_preserve_echo_area. */
605
606 static int display_last_displayed_message_p;
607
608 /* Nonzero if echo area is being used by print; zero if being used by
609 message. */
610
611 int message_buf_print;
612
613 /* The symbol `inhibit-menubar-update' and its DEFVAR_BOOL variable. */
614
615 Lisp_Object Qinhibit_menubar_update;
616 int inhibit_menubar_update;
617
618 /* Maximum height for resizing mini-windows. Either a float
619 specifying a fraction of the available height, or an integer
620 specifying a number of lines. */
621
622 Lisp_Object Vmax_mini_window_height;
623
624 /* Non-zero means messages should be displayed with truncated
625 lines instead of being continued. */
626
627 int message_truncate_lines;
628 Lisp_Object Qmessage_truncate_lines;
629
630 /* Set to 1 in clear_message to make redisplay_internal aware
631 of an emptied echo area. */
632
633 static int message_cleared_p;
634
635 /* How to blink the default frame cursor off. */
636 Lisp_Object Vblink_cursor_alist;
637
638 /* A scratch glyph row with contents used for generating truncation
639 glyphs. Also used in direct_output_for_insert. */
640
641 #define MAX_SCRATCH_GLYPHS 100
642 struct glyph_row scratch_glyph_row;
643 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
644
645 /* Ascent and height of the last line processed by move_it_to. */
646
647 static int last_max_ascent, last_height;
648
649 /* Non-zero if there's a help-echo in the echo area. */
650
651 int help_echo_showing_p;
652
653 /* If >= 0, computed, exact values of mode-line and header-line height
654 to use in the macros CURRENT_MODE_LINE_HEIGHT and
655 CURRENT_HEADER_LINE_HEIGHT. */
656
657 int current_mode_line_height, current_header_line_height;
658
659 /* The maximum distance to look ahead for text properties. Values
660 that are too small let us call compute_char_face and similar
661 functions too often which is expensive. Values that are too large
662 let us call compute_char_face and alike too often because we
663 might not be interested in text properties that far away. */
664
665 #define TEXT_PROP_DISTANCE_LIMIT 100
666
667 #if GLYPH_DEBUG
668
669 /* Variables to turn off display optimizations from Lisp. */
670
671 int inhibit_try_window_id, inhibit_try_window_reusing;
672 int inhibit_try_cursor_movement;
673
674 /* Non-zero means print traces of redisplay if compiled with
675 GLYPH_DEBUG != 0. */
676
677 int trace_redisplay_p;
678
679 #endif /* GLYPH_DEBUG */
680
681 #ifdef DEBUG_TRACE_MOVE
682 /* Non-zero means trace with TRACE_MOVE to stderr. */
683 int trace_move;
684
685 #define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
686 #else
687 #define TRACE_MOVE(x) (void) 0
688 #endif
689
690 /* Non-zero means automatically scroll windows horizontally to make
691 point visible. */
692
693 int automatic_hscrolling_p;
694
695 /* How close to the margin can point get before the window is scrolled
696 horizontally. */
697 EMACS_INT hscroll_margin;
698
699 /* How much to scroll horizontally when point is inside the above margin. */
700 Lisp_Object Vhscroll_step;
701
702 /* The variable `resize-mini-windows'. If nil, don't resize
703 mini-windows. If t, always resize them to fit the text they
704 display. If `grow-only', let mini-windows grow only until they
705 become empty. */
706
707 Lisp_Object Vresize_mini_windows;
708
709 /* Buffer being redisplayed -- for redisplay_window_error. */
710
711 struct buffer *displayed_buffer;
712
713 /* Value returned from text property handlers (see below). */
714
715 enum prop_handled
716 {
717 HANDLED_NORMALLY,
718 HANDLED_RECOMPUTE_PROPS,
719 HANDLED_OVERLAY_STRING_CONSUMED,
720 HANDLED_RETURN
721 };
722
723 /* A description of text properties that redisplay is interested
724 in. */
725
726 struct props
727 {
728 /* The name of the property. */
729 Lisp_Object *name;
730
731 /* A unique index for the property. */
732 enum prop_idx idx;
733
734 /* A handler function called to set up iterator IT from the property
735 at IT's current position. Value is used to steer handle_stop. */
736 enum prop_handled (*handler) P_ ((struct it *it));
737 };
738
739 static enum prop_handled handle_face_prop P_ ((struct it *));
740 static enum prop_handled handle_invisible_prop P_ ((struct it *));
741 static enum prop_handled handle_display_prop P_ ((struct it *));
742 static enum prop_handled handle_composition_prop P_ ((struct it *));
743 static enum prop_handled handle_overlay_change P_ ((struct it *));
744 static enum prop_handled handle_fontified_prop P_ ((struct it *));
745
746 /* Properties handled by iterators. */
747
748 static struct props it_props[] =
749 {
750 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
751 /* Handle `face' before `display' because some sub-properties of
752 `display' need to know the face. */
753 {&Qface, FACE_PROP_IDX, handle_face_prop},
754 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
755 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
756 {&Qcomposition, COMPOSITION_PROP_IDX, handle_composition_prop},
757 {NULL, 0, NULL}
758 };
759
760 /* Value is the position described by X. If X is a marker, value is
761 the marker_position of X. Otherwise, value is X. */
762
763 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
764
765 /* Enumeration returned by some move_it_.* functions internally. */
766
767 enum move_it_result
768 {
769 /* Not used. Undefined value. */
770 MOVE_UNDEFINED,
771
772 /* Move ended at the requested buffer position or ZV. */
773 MOVE_POS_MATCH_OR_ZV,
774
775 /* Move ended at the requested X pixel position. */
776 MOVE_X_REACHED,
777
778 /* Move within a line ended at the end of a line that must be
779 continued. */
780 MOVE_LINE_CONTINUED,
781
782 /* Move within a line ended at the end of a line that would
783 be displayed truncated. */
784 MOVE_LINE_TRUNCATED,
785
786 /* Move within a line ended at a line end. */
787 MOVE_NEWLINE_OR_CR
788 };
789
790 /* This counter is used to clear the face cache every once in a while
791 in redisplay_internal. It is incremented for each redisplay.
792 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
793 cleared. */
794
795 #define CLEAR_FACE_CACHE_COUNT 500
796 static int clear_face_cache_count;
797
798 /* Similarly for the image cache. */
799
800 #ifdef HAVE_WINDOW_SYSTEM
801 #define CLEAR_IMAGE_CACHE_COUNT 101
802 static int clear_image_cache_count;
803 #endif
804
805 /* Record the previous terminal frame we displayed. */
806
807 static struct frame *previous_terminal_frame;
808
809 /* Non-zero while redisplay_internal is in progress. */
810
811 int redisplaying_p;
812
813 /* Non-zero means don't free realized faces. Bound while freeing
814 realized faces is dangerous because glyph matrices might still
815 reference them. */
816
817 int inhibit_free_realized_faces;
818 Lisp_Object Qinhibit_free_realized_faces;
819
820 /* If a string, XTread_socket generates an event to display that string.
821 (The display is done in read_char.) */
822
823 Lisp_Object help_echo_string;
824 Lisp_Object help_echo_window;
825 Lisp_Object help_echo_object;
826 int help_echo_pos;
827
828 /* Temporary variable for XTread_socket. */
829
830 Lisp_Object previous_help_echo_string;
831
832 /* Null glyph slice */
833
834 static struct glyph_slice null_glyph_slice = { 0, 0, 0, 0 };
835
836 \f
837 /* Function prototypes. */
838
839 static void setup_for_ellipsis P_ ((struct it *, int));
840 static void mark_window_display_accurate_1 P_ ((struct window *, int));
841 static int single_display_spec_string_p P_ ((Lisp_Object, Lisp_Object));
842 static int display_prop_string_p P_ ((Lisp_Object, Lisp_Object));
843 static int cursor_row_p P_ ((struct window *, struct glyph_row *));
844 static int redisplay_mode_lines P_ ((Lisp_Object, int));
845 static char *decode_mode_spec_coding P_ ((Lisp_Object, char *, int));
846
847 #if 0
848 static int invisible_text_between_p P_ ((struct it *, int, int));
849 #endif
850
851 static void pint2str P_ ((char *, int, int));
852 static void pint2hrstr P_ ((char *, int, int));
853 static struct text_pos run_window_scroll_functions P_ ((Lisp_Object,
854 struct text_pos));
855 static void reconsider_clip_changes P_ ((struct window *, struct buffer *));
856 static int text_outside_line_unchanged_p P_ ((struct window *, int, int));
857 static void store_mode_line_noprop_char P_ ((char));
858 static int store_mode_line_noprop P_ ((const unsigned char *, int, int));
859 static void x_consider_frame_title P_ ((Lisp_Object));
860 static void handle_stop P_ ((struct it *));
861 static int tool_bar_lines_needed P_ ((struct frame *, int *));
862 static int single_display_spec_intangible_p P_ ((Lisp_Object));
863 static void ensure_echo_area_buffers P_ ((void));
864 static Lisp_Object unwind_with_echo_area_buffer P_ ((Lisp_Object));
865 static Lisp_Object with_echo_area_buffer_unwind_data P_ ((struct window *));
866 static int with_echo_area_buffer P_ ((struct window *, int,
867 int (*) (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT),
868 EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
869 static void clear_garbaged_frames P_ ((void));
870 static int current_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
871 static int truncate_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
872 static int set_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
873 static int display_echo_area P_ ((struct window *));
874 static int display_echo_area_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
875 static int resize_mini_window_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
876 static Lisp_Object unwind_redisplay P_ ((Lisp_Object));
877 static int string_char_and_length P_ ((const unsigned char *, int, int *));
878 static struct text_pos display_prop_end P_ ((struct it *, Lisp_Object,
879 struct text_pos));
880 static int compute_window_start_on_continuation_line P_ ((struct window *));
881 static Lisp_Object safe_eval_handler P_ ((Lisp_Object));
882 static void insert_left_trunc_glyphs P_ ((struct it *));
883 static struct glyph_row *get_overlay_arrow_glyph_row P_ ((struct window *,
884 Lisp_Object));
885 static void extend_face_to_end_of_line P_ ((struct it *));
886 static int append_space_for_newline P_ ((struct it *, int));
887 static int cursor_row_fully_visible_p P_ ((struct window *, int, int));
888 static int try_scrolling P_ ((Lisp_Object, int, EMACS_INT, EMACS_INT, int, int));
889 static int try_cursor_movement P_ ((Lisp_Object, struct text_pos, int *));
890 static int trailing_whitespace_p P_ ((int));
891 static int message_log_check_duplicate P_ ((int, int, int, int));
892 static void push_it P_ ((struct it *));
893 static void pop_it P_ ((struct it *));
894 static void sync_frame_with_window_matrix_rows P_ ((struct window *));
895 static void select_frame_for_redisplay P_ ((Lisp_Object));
896 static void redisplay_internal P_ ((int));
897 static int echo_area_display P_ ((int));
898 static void redisplay_windows P_ ((Lisp_Object));
899 static void redisplay_window P_ ((Lisp_Object, int));
900 static Lisp_Object redisplay_window_error ();
901 static Lisp_Object redisplay_window_0 P_ ((Lisp_Object));
902 static Lisp_Object redisplay_window_1 P_ ((Lisp_Object));
903 static void update_menu_bar P_ ((struct frame *, int));
904 static int try_window_reusing_current_matrix P_ ((struct window *));
905 static int try_window_id P_ ((struct window *));
906 static int display_line P_ ((struct it *));
907 static int display_mode_lines P_ ((struct window *));
908 static int display_mode_line P_ ((struct window *, enum face_id, Lisp_Object));
909 static int display_mode_element P_ ((struct it *, int, int, int, Lisp_Object, Lisp_Object, int));
910 static int store_mode_line_string P_ ((char *, Lisp_Object, int, int, int, Lisp_Object));
911 static char *decode_mode_spec P_ ((struct window *, int, int, int, int *));
912 static void display_menu_bar P_ ((struct window *));
913 static int display_count_lines P_ ((int, int, int, int, int *));
914 static int display_string P_ ((unsigned char *, Lisp_Object, Lisp_Object,
915 int, int, struct it *, int, int, int, int));
916 static void compute_line_metrics P_ ((struct it *));
917 static void run_redisplay_end_trigger_hook P_ ((struct it *));
918 static int get_overlay_strings P_ ((struct it *, int));
919 static void next_overlay_string P_ ((struct it *));
920 static void reseat P_ ((struct it *, struct text_pos, int));
921 static void reseat_1 P_ ((struct it *, struct text_pos, int));
922 static void back_to_previous_visible_line_start P_ ((struct it *));
923 void reseat_at_previous_visible_line_start P_ ((struct it *));
924 static void reseat_at_next_visible_line_start P_ ((struct it *, int));
925 static int next_element_from_ellipsis P_ ((struct it *));
926 static int next_element_from_display_vector P_ ((struct it *));
927 static int next_element_from_string P_ ((struct it *));
928 static int next_element_from_c_string P_ ((struct it *));
929 static int next_element_from_buffer P_ ((struct it *));
930 static int next_element_from_composition P_ ((struct it *));
931 static int next_element_from_image P_ ((struct it *));
932 static int next_element_from_stretch P_ ((struct it *));
933 static void load_overlay_strings P_ ((struct it *, int));
934 static int init_from_display_pos P_ ((struct it *, struct window *,
935 struct display_pos *));
936 static void reseat_to_string P_ ((struct it *, unsigned char *,
937 Lisp_Object, int, int, int, int));
938 static enum move_it_result move_it_in_display_line_to P_ ((struct it *,
939 int, int, int));
940 void move_it_vertically_backward P_ ((struct it *, int));
941 static void init_to_row_start P_ ((struct it *, struct window *,
942 struct glyph_row *));
943 static int init_to_row_end P_ ((struct it *, struct window *,
944 struct glyph_row *));
945 static void back_to_previous_line_start P_ ((struct it *));
946 static int forward_to_next_line_start P_ ((struct it *, int *));
947 static struct text_pos string_pos_nchars_ahead P_ ((struct text_pos,
948 Lisp_Object, int));
949 static struct text_pos string_pos P_ ((int, Lisp_Object));
950 static struct text_pos c_string_pos P_ ((int, unsigned char *, int));
951 static int number_of_chars P_ ((unsigned char *, int));
952 static void compute_stop_pos P_ ((struct it *));
953 static void compute_string_pos P_ ((struct text_pos *, struct text_pos,
954 Lisp_Object));
955 static int face_before_or_after_it_pos P_ ((struct it *, int));
956 static int next_overlay_change P_ ((int));
957 static int handle_single_display_spec P_ ((struct it *, Lisp_Object,
958 Lisp_Object, struct text_pos *,
959 int));
960 static int underlying_face_id P_ ((struct it *));
961 static int in_ellipses_for_invisible_text_p P_ ((struct display_pos *,
962 struct window *));
963
964 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
965 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
966
967 #ifdef HAVE_WINDOW_SYSTEM
968
969 static void update_tool_bar P_ ((struct frame *, int));
970 static void build_desired_tool_bar_string P_ ((struct frame *f));
971 static int redisplay_tool_bar P_ ((struct frame *));
972 static void display_tool_bar_line P_ ((struct it *, int));
973 static void notice_overwritten_cursor P_ ((struct window *,
974 enum glyph_row_area,
975 int, int, int, int));
976
977
978
979 #endif /* HAVE_WINDOW_SYSTEM */
980
981 \f
982 /***********************************************************************
983 Window display dimensions
984 ***********************************************************************/
985
986 /* Return the bottom boundary y-position for text lines in window W.
987 This is the first y position at which a line cannot start.
988 It is relative to the top of the window.
989
990 This is the height of W minus the height of a mode line, if any. */
991
992 INLINE int
993 window_text_bottom_y (w)
994 struct window *w;
995 {
996 int height = WINDOW_TOTAL_HEIGHT (w);
997
998 if (WINDOW_WANTS_MODELINE_P (w))
999 height -= CURRENT_MODE_LINE_HEIGHT (w);
1000 return height;
1001 }
1002
1003 /* Return the pixel width of display area AREA of window W. AREA < 0
1004 means return the total width of W, not including fringes to
1005 the left and right of the window. */
1006
1007 INLINE int
1008 window_box_width (w, area)
1009 struct window *w;
1010 int area;
1011 {
1012 int cols = XFASTINT (w->total_cols);
1013 int pixels = 0;
1014
1015 if (!w->pseudo_window_p)
1016 {
1017 cols -= WINDOW_SCROLL_BAR_COLS (w);
1018
1019 if (area == TEXT_AREA)
1020 {
1021 if (INTEGERP (w->left_margin_cols))
1022 cols -= XFASTINT (w->left_margin_cols);
1023 if (INTEGERP (w->right_margin_cols))
1024 cols -= XFASTINT (w->right_margin_cols);
1025 pixels = -WINDOW_TOTAL_FRINGE_WIDTH (w);
1026 }
1027 else if (area == LEFT_MARGIN_AREA)
1028 {
1029 cols = (INTEGERP (w->left_margin_cols)
1030 ? XFASTINT (w->left_margin_cols) : 0);
1031 pixels = 0;
1032 }
1033 else if (area == RIGHT_MARGIN_AREA)
1034 {
1035 cols = (INTEGERP (w->right_margin_cols)
1036 ? XFASTINT (w->right_margin_cols) : 0);
1037 pixels = 0;
1038 }
1039 }
1040
1041 return cols * WINDOW_FRAME_COLUMN_WIDTH (w) + pixels;
1042 }
1043
1044
1045 /* Return the pixel height of the display area of window W, not
1046 including mode lines of W, if any. */
1047
1048 INLINE int
1049 window_box_height (w)
1050 struct window *w;
1051 {
1052 struct frame *f = XFRAME (w->frame);
1053 int height = WINDOW_TOTAL_HEIGHT (w);
1054
1055 xassert (height >= 0);
1056
1057 /* Note: the code below that determines the mode-line/header-line
1058 height is essentially the same as that contained in the macro
1059 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
1060 the appropriate glyph row has its `mode_line_p' flag set,
1061 and if it doesn't, uses estimate_mode_line_height instead. */
1062
1063 if (WINDOW_WANTS_MODELINE_P (w))
1064 {
1065 struct glyph_row *ml_row
1066 = (w->current_matrix && w->current_matrix->rows
1067 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
1068 : 0);
1069 if (ml_row && ml_row->mode_line_p)
1070 height -= ml_row->height;
1071 else
1072 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
1073 }
1074
1075 if (WINDOW_WANTS_HEADER_LINE_P (w))
1076 {
1077 struct glyph_row *hl_row
1078 = (w->current_matrix && w->current_matrix->rows
1079 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
1080 : 0);
1081 if (hl_row && hl_row->mode_line_p)
1082 height -= hl_row->height;
1083 else
1084 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
1085 }
1086
1087 /* With a very small font and a mode-line that's taller than
1088 default, we might end up with a negative height. */
1089 return max (0, height);
1090 }
1091
1092 /* Return the window-relative coordinate of the left edge of display
1093 area AREA of window W. AREA < 0 means return the left edge of the
1094 whole window, to the right of the left fringe of W. */
1095
1096 INLINE int
1097 window_box_left_offset (w, area)
1098 struct window *w;
1099 int area;
1100 {
1101 int x;
1102
1103 if (w->pseudo_window_p)
1104 return 0;
1105
1106 x = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
1107
1108 if (area == TEXT_AREA)
1109 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1110 + window_box_width (w, LEFT_MARGIN_AREA));
1111 else if (area == RIGHT_MARGIN_AREA)
1112 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1113 + window_box_width (w, LEFT_MARGIN_AREA)
1114 + window_box_width (w, TEXT_AREA)
1115 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1116 ? 0
1117 : WINDOW_RIGHT_FRINGE_WIDTH (w)));
1118 else if (area == LEFT_MARGIN_AREA
1119 && WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1120 x += WINDOW_LEFT_FRINGE_WIDTH (w);
1121
1122 return x;
1123 }
1124
1125
1126 /* Return the window-relative coordinate of the right edge of display
1127 area AREA of window W. AREA < 0 means return the left edge of the
1128 whole window, to the left of the right fringe of W. */
1129
1130 INLINE int
1131 window_box_right_offset (w, area)
1132 struct window *w;
1133 int area;
1134 {
1135 return window_box_left_offset (w, area) + window_box_width (w, area);
1136 }
1137
1138 /* Return the frame-relative coordinate of the left edge of display
1139 area AREA of window W. AREA < 0 means return the left edge of the
1140 whole window, to the right of the left fringe of W. */
1141
1142 INLINE int
1143 window_box_left (w, area)
1144 struct window *w;
1145 int area;
1146 {
1147 struct frame *f = XFRAME (w->frame);
1148 int x;
1149
1150 if (w->pseudo_window_p)
1151 return FRAME_INTERNAL_BORDER_WIDTH (f);
1152
1153 x = (WINDOW_LEFT_EDGE_X (w)
1154 + window_box_left_offset (w, area));
1155
1156 return x;
1157 }
1158
1159
1160 /* Return the frame-relative coordinate of the right edge of display
1161 area AREA of window W. AREA < 0 means return the left edge of the
1162 whole window, to the left of the right fringe of W. */
1163
1164 INLINE int
1165 window_box_right (w, area)
1166 struct window *w;
1167 int area;
1168 {
1169 return window_box_left (w, area) + window_box_width (w, area);
1170 }
1171
1172 /* Get the bounding box of the display area AREA of window W, without
1173 mode lines, in frame-relative coordinates. AREA < 0 means the
1174 whole window, not including the left and right fringes of
1175 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1176 coordinates of the upper-left corner of the box. Return in
1177 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1178
1179 INLINE void
1180 window_box (w, area, box_x, box_y, box_width, box_height)
1181 struct window *w;
1182 int area;
1183 int *box_x, *box_y, *box_width, *box_height;
1184 {
1185 if (box_width)
1186 *box_width = window_box_width (w, area);
1187 if (box_height)
1188 *box_height = window_box_height (w);
1189 if (box_x)
1190 *box_x = window_box_left (w, area);
1191 if (box_y)
1192 {
1193 *box_y = WINDOW_TOP_EDGE_Y (w);
1194 if (WINDOW_WANTS_HEADER_LINE_P (w))
1195 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1196 }
1197 }
1198
1199
1200 /* Get the bounding box of the display area AREA of window W, without
1201 mode lines. AREA < 0 means the whole window, not including the
1202 left and right fringe of the window. Return in *TOP_LEFT_X
1203 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1204 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1205 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1206 box. */
1207
1208 INLINE void
1209 window_box_edges (w, area, top_left_x, top_left_y,
1210 bottom_right_x, bottom_right_y)
1211 struct window *w;
1212 int area;
1213 int *top_left_x, *top_left_y, *bottom_right_x, *bottom_right_y;
1214 {
1215 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
1216 bottom_right_y);
1217 *bottom_right_x += *top_left_x;
1218 *bottom_right_y += *top_left_y;
1219 }
1220
1221
1222 \f
1223 /***********************************************************************
1224 Utilities
1225 ***********************************************************************/
1226
1227 /* Return the bottom y-position of the line the iterator IT is in.
1228 This can modify IT's settings. */
1229
1230 int
1231 line_bottom_y (it)
1232 struct it *it;
1233 {
1234 int line_height = it->max_ascent + it->max_descent;
1235 int line_top_y = it->current_y;
1236
1237 if (line_height == 0)
1238 {
1239 if (last_height)
1240 line_height = last_height;
1241 else if (IT_CHARPOS (*it) < ZV)
1242 {
1243 move_it_by_lines (it, 1, 1);
1244 line_height = (it->max_ascent || it->max_descent
1245 ? it->max_ascent + it->max_descent
1246 : last_height);
1247 }
1248 else
1249 {
1250 struct glyph_row *row = it->glyph_row;
1251
1252 /* Use the default character height. */
1253 it->glyph_row = NULL;
1254 it->what = IT_CHARACTER;
1255 it->c = ' ';
1256 it->len = 1;
1257 PRODUCE_GLYPHS (it);
1258 line_height = it->ascent + it->descent;
1259 it->glyph_row = row;
1260 }
1261 }
1262
1263 return line_top_y + line_height;
1264 }
1265
1266
1267 /* Return 1 if position CHARPOS is visible in window W.
1268 If visible, set *X and *Y to pixel coordinates of top left corner.
1269 Set *RTOP and *RBOT to pixel height of an invisible area of glyph at POS.
1270 EXACT_MODE_LINE_HEIGHTS_P non-zero means compute exact mode-line
1271 and header-lines heights. */
1272
1273 int
1274 pos_visible_p (w, charpos, x, y, rtop, rbot, exact_mode_line_heights_p)
1275 struct window *w;
1276 int charpos, *x, *y, *rtop, *rbot, exact_mode_line_heights_p;
1277 {
1278 struct it it;
1279 struct text_pos top;
1280 int visible_p = 0;
1281 struct buffer *old_buffer = NULL;
1282
1283 if (noninteractive)
1284 return visible_p;
1285
1286 if (XBUFFER (w->buffer) != current_buffer)
1287 {
1288 old_buffer = current_buffer;
1289 set_buffer_internal_1 (XBUFFER (w->buffer));
1290 }
1291
1292 SET_TEXT_POS_FROM_MARKER (top, w->start);
1293
1294 /* Compute exact mode line heights, if requested. */
1295 if (exact_mode_line_heights_p)
1296 {
1297 if (WINDOW_WANTS_MODELINE_P (w))
1298 current_mode_line_height
1299 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1300 current_buffer->mode_line_format);
1301
1302 if (WINDOW_WANTS_HEADER_LINE_P (w))
1303 current_header_line_height
1304 = display_mode_line (w, HEADER_LINE_FACE_ID,
1305 current_buffer->header_line_format);
1306 }
1307
1308 start_display (&it, w, top);
1309 move_it_to (&it, charpos, -1, it.last_visible_y, -1,
1310 MOVE_TO_POS | MOVE_TO_Y);
1311
1312 /* Note that we may overshoot because of invisible text. */
1313 if (IT_CHARPOS (it) >= charpos)
1314 {
1315 int top_x = it.current_x;
1316 int top_y = it.current_y;
1317 int bottom_y = (last_height = 0, line_bottom_y (&it));
1318 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
1319
1320 if (top_y < window_top_y)
1321 visible_p = bottom_y > window_top_y;
1322 else if (top_y < it.last_visible_y)
1323 visible_p = 1;
1324 if (visible_p)
1325 {
1326 *x = top_x;
1327 *y = max (top_y + max (0, it.max_ascent - it.ascent), window_top_y);
1328 *rtop = max (0, window_top_y - top_y);
1329 *rbot = max (0, bottom_y - it.last_visible_y);
1330 }
1331 }
1332 else
1333 {
1334 struct it it2;
1335
1336 it2 = it;
1337 if (IT_CHARPOS (it) < ZV && FETCH_BYTE (IT_BYTEPOS (it)) != '\n')
1338 move_it_by_lines (&it, 1, 0);
1339 if (charpos < IT_CHARPOS (it))
1340 {
1341 visible_p = 1;
1342 move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS);
1343 *x = it2.current_x;
1344 *y = it2.current_y + it2.max_ascent - it2.ascent;
1345 *rtop = max (0, -it2.current_y);
1346 *rbot = max (0, ((it2.current_y + it2.max_ascent + it2.max_descent)
1347 - it.last_visible_y));
1348 }
1349 }
1350
1351 if (old_buffer)
1352 set_buffer_internal_1 (old_buffer);
1353
1354 current_header_line_height = current_mode_line_height = -1;
1355
1356 if (visible_p && XFASTINT (w->hscroll) > 0)
1357 *x -= XFASTINT (w->hscroll) * WINDOW_FRAME_COLUMN_WIDTH (w);
1358
1359 return visible_p;
1360 }
1361
1362
1363 /* Return the next character from STR which is MAXLEN bytes long.
1364 Return in *LEN the length of the character. This is like
1365 STRING_CHAR_AND_LENGTH but never returns an invalid character. If
1366 we find one, we return a `?', but with the length of the invalid
1367 character. */
1368
1369 static INLINE int
1370 string_char_and_length (str, maxlen, len)
1371 const unsigned char *str;
1372 int maxlen, *len;
1373 {
1374 int c;
1375
1376 c = STRING_CHAR_AND_LENGTH (str, maxlen, *len);
1377 if (!CHAR_VALID_P (c, 1))
1378 /* We may not change the length here because other places in Emacs
1379 don't use this function, i.e. they silently accept invalid
1380 characters. */
1381 c = '?';
1382
1383 return c;
1384 }
1385
1386
1387
1388 /* Given a position POS containing a valid character and byte position
1389 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1390
1391 static struct text_pos
1392 string_pos_nchars_ahead (pos, string, nchars)
1393 struct text_pos pos;
1394 Lisp_Object string;
1395 int nchars;
1396 {
1397 xassert (STRINGP (string) && nchars >= 0);
1398
1399 if (STRING_MULTIBYTE (string))
1400 {
1401 int rest = SBYTES (string) - BYTEPOS (pos);
1402 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1403 int len;
1404
1405 while (nchars--)
1406 {
1407 string_char_and_length (p, rest, &len);
1408 p += len, rest -= len;
1409 xassert (rest >= 0);
1410 CHARPOS (pos) += 1;
1411 BYTEPOS (pos) += len;
1412 }
1413 }
1414 else
1415 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1416
1417 return pos;
1418 }
1419
1420
1421 /* Value is the text position, i.e. character and byte position,
1422 for character position CHARPOS in STRING. */
1423
1424 static INLINE struct text_pos
1425 string_pos (charpos, string)
1426 int charpos;
1427 Lisp_Object string;
1428 {
1429 struct text_pos pos;
1430 xassert (STRINGP (string));
1431 xassert (charpos >= 0);
1432 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1433 return pos;
1434 }
1435
1436
1437 /* Value is a text position, i.e. character and byte position, for
1438 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1439 means recognize multibyte characters. */
1440
1441 static struct text_pos
1442 c_string_pos (charpos, s, multibyte_p)
1443 int charpos;
1444 unsigned char *s;
1445 int multibyte_p;
1446 {
1447 struct text_pos pos;
1448
1449 xassert (s != NULL);
1450 xassert (charpos >= 0);
1451
1452 if (multibyte_p)
1453 {
1454 int rest = strlen (s), len;
1455
1456 SET_TEXT_POS (pos, 0, 0);
1457 while (charpos--)
1458 {
1459 string_char_and_length (s, rest, &len);
1460 s += len, rest -= len;
1461 xassert (rest >= 0);
1462 CHARPOS (pos) += 1;
1463 BYTEPOS (pos) += len;
1464 }
1465 }
1466 else
1467 SET_TEXT_POS (pos, charpos, charpos);
1468
1469 return pos;
1470 }
1471
1472
1473 /* Value is the number of characters in C string S. MULTIBYTE_P
1474 non-zero means recognize multibyte characters. */
1475
1476 static int
1477 number_of_chars (s, multibyte_p)
1478 unsigned char *s;
1479 int multibyte_p;
1480 {
1481 int nchars;
1482
1483 if (multibyte_p)
1484 {
1485 int rest = strlen (s), len;
1486 unsigned char *p = (unsigned char *) s;
1487
1488 for (nchars = 0; rest > 0; ++nchars)
1489 {
1490 string_char_and_length (p, rest, &len);
1491 rest -= len, p += len;
1492 }
1493 }
1494 else
1495 nchars = strlen (s);
1496
1497 return nchars;
1498 }
1499
1500
1501 /* Compute byte position NEWPOS->bytepos corresponding to
1502 NEWPOS->charpos. POS is a known position in string STRING.
1503 NEWPOS->charpos must be >= POS.charpos. */
1504
1505 static void
1506 compute_string_pos (newpos, pos, string)
1507 struct text_pos *newpos, pos;
1508 Lisp_Object string;
1509 {
1510 xassert (STRINGP (string));
1511 xassert (CHARPOS (*newpos) >= CHARPOS (pos));
1512
1513 if (STRING_MULTIBYTE (string))
1514 *newpos = string_pos_nchars_ahead (pos, string,
1515 CHARPOS (*newpos) - CHARPOS (pos));
1516 else
1517 BYTEPOS (*newpos) = CHARPOS (*newpos);
1518 }
1519
1520 /* EXPORT:
1521 Return an estimation of the pixel height of mode or top lines on
1522 frame F. FACE_ID specifies what line's height to estimate. */
1523
1524 int
1525 estimate_mode_line_height (f, face_id)
1526 struct frame *f;
1527 enum face_id face_id;
1528 {
1529 #ifdef HAVE_WINDOW_SYSTEM
1530 if (FRAME_WINDOW_P (f))
1531 {
1532 int height = FONT_HEIGHT (FRAME_FONT (f));
1533
1534 /* This function is called so early when Emacs starts that the face
1535 cache and mode line face are not yet initialized. */
1536 if (FRAME_FACE_CACHE (f))
1537 {
1538 struct face *face = FACE_FROM_ID (f, face_id);
1539 if (face)
1540 {
1541 if (face->font)
1542 height = FONT_HEIGHT (face->font);
1543 if (face->box_line_width > 0)
1544 height += 2 * face->box_line_width;
1545 }
1546 }
1547
1548 return height;
1549 }
1550 #endif
1551
1552 return 1;
1553 }
1554
1555 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1556 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1557 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
1558 not force the value into range. */
1559
1560 void
1561 pixel_to_glyph_coords (f, pix_x, pix_y, x, y, bounds, noclip)
1562 FRAME_PTR f;
1563 register int pix_x, pix_y;
1564 int *x, *y;
1565 NativeRectangle *bounds;
1566 int noclip;
1567 {
1568
1569 #ifdef HAVE_WINDOW_SYSTEM
1570 if (FRAME_WINDOW_P (f))
1571 {
1572 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1573 even for negative values. */
1574 if (pix_x < 0)
1575 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1576 if (pix_y < 0)
1577 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1578
1579 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1580 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1581
1582 if (bounds)
1583 STORE_NATIVE_RECT (*bounds,
1584 FRAME_COL_TO_PIXEL_X (f, pix_x),
1585 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1586 FRAME_COLUMN_WIDTH (f) - 1,
1587 FRAME_LINE_HEIGHT (f) - 1);
1588
1589 if (!noclip)
1590 {
1591 if (pix_x < 0)
1592 pix_x = 0;
1593 else if (pix_x > FRAME_TOTAL_COLS (f))
1594 pix_x = FRAME_TOTAL_COLS (f);
1595
1596 if (pix_y < 0)
1597 pix_y = 0;
1598 else if (pix_y > FRAME_LINES (f))
1599 pix_y = FRAME_LINES (f);
1600 }
1601 }
1602 #endif
1603
1604 *x = pix_x;
1605 *y = pix_y;
1606 }
1607
1608
1609 /* Given HPOS/VPOS in the current matrix of W, return corresponding
1610 frame-relative pixel positions in *FRAME_X and *FRAME_Y. If we
1611 can't tell the positions because W's display is not up to date,
1612 return 0. */
1613
1614 int
1615 glyph_to_pixel_coords (w, hpos, vpos, frame_x, frame_y)
1616 struct window *w;
1617 int hpos, vpos;
1618 int *frame_x, *frame_y;
1619 {
1620 #ifdef HAVE_WINDOW_SYSTEM
1621 if (FRAME_WINDOW_P (XFRAME (WINDOW_FRAME (w))))
1622 {
1623 int success_p;
1624
1625 xassert (hpos >= 0 && hpos < w->current_matrix->matrix_w);
1626 xassert (vpos >= 0 && vpos < w->current_matrix->matrix_h);
1627
1628 if (display_completed)
1629 {
1630 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
1631 struct glyph *glyph = row->glyphs[TEXT_AREA];
1632 struct glyph *end = glyph + min (hpos, row->used[TEXT_AREA]);
1633
1634 hpos = row->x;
1635 vpos = row->y;
1636 while (glyph < end)
1637 {
1638 hpos += glyph->pixel_width;
1639 ++glyph;
1640 }
1641
1642 /* If first glyph is partially visible, its first visible position is still 0. */
1643 if (hpos < 0)
1644 hpos = 0;
1645
1646 success_p = 1;
1647 }
1648 else
1649 {
1650 hpos = vpos = 0;
1651 success_p = 0;
1652 }
1653
1654 *frame_x = WINDOW_TO_FRAME_PIXEL_X (w, hpos);
1655 *frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, vpos);
1656 return success_p;
1657 }
1658 #endif
1659
1660 *frame_x = hpos;
1661 *frame_y = vpos;
1662 return 1;
1663 }
1664
1665
1666 #ifdef HAVE_WINDOW_SYSTEM
1667
1668 /* Find the glyph under window-relative coordinates X/Y in window W.
1669 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1670 strings. Return in *HPOS and *VPOS the row and column number of
1671 the glyph found. Return in *AREA the glyph area containing X.
1672 Value is a pointer to the glyph found or null if X/Y is not on
1673 text, or we can't tell because W's current matrix is not up to
1674 date. */
1675
1676 static struct glyph *
1677 x_y_to_hpos_vpos (w, x, y, hpos, vpos, dx, dy, area)
1678 struct window *w;
1679 int x, y;
1680 int *hpos, *vpos, *dx, *dy, *area;
1681 {
1682 struct glyph *glyph, *end;
1683 struct glyph_row *row = NULL;
1684 int x0, i;
1685
1686 /* Find row containing Y. Give up if some row is not enabled. */
1687 for (i = 0; i < w->current_matrix->nrows; ++i)
1688 {
1689 row = MATRIX_ROW (w->current_matrix, i);
1690 if (!row->enabled_p)
1691 return NULL;
1692 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1693 break;
1694 }
1695
1696 *vpos = i;
1697 *hpos = 0;
1698
1699 /* Give up if Y is not in the window. */
1700 if (i == w->current_matrix->nrows)
1701 return NULL;
1702
1703 /* Get the glyph area containing X. */
1704 if (w->pseudo_window_p)
1705 {
1706 *area = TEXT_AREA;
1707 x0 = 0;
1708 }
1709 else
1710 {
1711 if (x < window_box_left_offset (w, TEXT_AREA))
1712 {
1713 *area = LEFT_MARGIN_AREA;
1714 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1715 }
1716 else if (x < window_box_right_offset (w, TEXT_AREA))
1717 {
1718 *area = TEXT_AREA;
1719 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
1720 }
1721 else
1722 {
1723 *area = RIGHT_MARGIN_AREA;
1724 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1725 }
1726 }
1727
1728 /* Find glyph containing X. */
1729 glyph = row->glyphs[*area];
1730 end = glyph + row->used[*area];
1731 x -= x0;
1732 while (glyph < end && x >= glyph->pixel_width)
1733 {
1734 x -= glyph->pixel_width;
1735 ++glyph;
1736 }
1737
1738 if (glyph == end)
1739 return NULL;
1740
1741 if (dx)
1742 {
1743 *dx = x;
1744 *dy = y - (row->y + row->ascent - glyph->ascent);
1745 }
1746
1747 *hpos = glyph - row->glyphs[*area];
1748 return glyph;
1749 }
1750
1751
1752 /* EXPORT:
1753 Convert frame-relative x/y to coordinates relative to window W.
1754 Takes pseudo-windows into account. */
1755
1756 void
1757 frame_to_window_pixel_xy (w, x, y)
1758 struct window *w;
1759 int *x, *y;
1760 {
1761 if (w->pseudo_window_p)
1762 {
1763 /* A pseudo-window is always full-width, and starts at the
1764 left edge of the frame, plus a frame border. */
1765 struct frame *f = XFRAME (w->frame);
1766 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
1767 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1768 }
1769 else
1770 {
1771 *x -= WINDOW_LEFT_EDGE_X (w);
1772 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1773 }
1774 }
1775
1776 /* EXPORT:
1777 Return in RECTS[] at most N clipping rectangles for glyph string S.
1778 Return the number of stored rectangles. */
1779
1780 int
1781 get_glyph_string_clip_rects (s, rects, n)
1782 struct glyph_string *s;
1783 NativeRectangle *rects;
1784 int n;
1785 {
1786 XRectangle r;
1787
1788 if (n <= 0)
1789 return 0;
1790
1791 if (s->row->full_width_p)
1792 {
1793 /* Draw full-width. X coordinates are relative to S->w->left_col. */
1794 r.x = WINDOW_LEFT_EDGE_X (s->w);
1795 r.width = WINDOW_TOTAL_WIDTH (s->w);
1796
1797 /* Unless displaying a mode or menu bar line, which are always
1798 fully visible, clip to the visible part of the row. */
1799 if (s->w->pseudo_window_p)
1800 r.height = s->row->visible_height;
1801 else
1802 r.height = s->height;
1803 }
1804 else
1805 {
1806 /* This is a text line that may be partially visible. */
1807 r.x = window_box_left (s->w, s->area);
1808 r.width = window_box_width (s->w, s->area);
1809 r.height = s->row->visible_height;
1810 }
1811
1812 if (s->clip_head)
1813 if (r.x < s->clip_head->x)
1814 {
1815 if (r.width >= s->clip_head->x - r.x)
1816 r.width -= s->clip_head->x - r.x;
1817 else
1818 r.width = 0;
1819 r.x = s->clip_head->x;
1820 }
1821 if (s->clip_tail)
1822 if (r.x + r.width > s->clip_tail->x + s->clip_tail->background_width)
1823 {
1824 if (s->clip_tail->x + s->clip_tail->background_width >= r.x)
1825 r.width = s->clip_tail->x + s->clip_tail->background_width - r.x;
1826 else
1827 r.width = 0;
1828 }
1829
1830 /* If S draws overlapping rows, it's sufficient to use the top and
1831 bottom of the window for clipping because this glyph string
1832 intentionally draws over other lines. */
1833 if (s->for_overlaps)
1834 {
1835 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1836 r.height = window_text_bottom_y (s->w) - r.y;
1837
1838 /* Alas, the above simple strategy does not work for the
1839 environments with anti-aliased text: if the same text is
1840 drawn onto the same place multiple times, it gets thicker.
1841 If the overlap we are processing is for the erased cursor, we
1842 take the intersection with the rectagle of the cursor. */
1843 if (s->for_overlaps & OVERLAPS_ERASED_CURSOR)
1844 {
1845 XRectangle rc, r_save = r;
1846
1847 rc.x = WINDOW_TEXT_TO_FRAME_PIXEL_X (s->w, s->w->phys_cursor.x);
1848 rc.y = s->w->phys_cursor.y;
1849 rc.width = s->w->phys_cursor_width;
1850 rc.height = s->w->phys_cursor_height;
1851
1852 x_intersect_rectangles (&r_save, &rc, &r);
1853 }
1854 }
1855 else
1856 {
1857 /* Don't use S->y for clipping because it doesn't take partially
1858 visible lines into account. For example, it can be negative for
1859 partially visible lines at the top of a window. */
1860 if (!s->row->full_width_p
1861 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
1862 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1863 else
1864 r.y = max (0, s->row->y);
1865
1866 /* If drawing a tool-bar window, draw it over the internal border
1867 at the top of the window. */
1868 if (WINDOWP (s->f->tool_bar_window)
1869 && s->w == XWINDOW (s->f->tool_bar_window))
1870 r.y -= FRAME_INTERNAL_BORDER_WIDTH (s->f);
1871 }
1872
1873 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
1874
1875 /* If drawing the cursor, don't let glyph draw outside its
1876 advertised boundaries. Cleartype does this under some circumstances. */
1877 if (s->hl == DRAW_CURSOR)
1878 {
1879 struct glyph *glyph = s->first_glyph;
1880 int height, max_y;
1881
1882 if (s->x > r.x)
1883 {
1884 r.width -= s->x - r.x;
1885 r.x = s->x;
1886 }
1887 r.width = min (r.width, glyph->pixel_width);
1888
1889 /* If r.y is below window bottom, ensure that we still see a cursor. */
1890 height = min (glyph->ascent + glyph->descent,
1891 min (FRAME_LINE_HEIGHT (s->f), s->row->visible_height));
1892 max_y = window_text_bottom_y (s->w) - height;
1893 max_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, max_y);
1894 if (s->ybase - glyph->ascent > max_y)
1895 {
1896 r.y = max_y;
1897 r.height = height;
1898 }
1899 else
1900 {
1901 /* Don't draw cursor glyph taller than our actual glyph. */
1902 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
1903 if (height < r.height)
1904 {
1905 max_y = r.y + r.height;
1906 r.y = min (max_y, max (r.y, s->ybase + glyph->descent - height));
1907 r.height = min (max_y - r.y, height);
1908 }
1909 }
1910 }
1911
1912 if ((s->for_overlaps & OVERLAPS_BOTH) == 0
1913 || ((s->for_overlaps & OVERLAPS_BOTH) == OVERLAPS_BOTH && n == 1))
1914 {
1915 #ifdef CONVERT_FROM_XRECT
1916 CONVERT_FROM_XRECT (r, *rects);
1917 #else
1918 *rects = r;
1919 #endif
1920 return 1;
1921 }
1922 else
1923 {
1924 /* If we are processing overlapping and allowed to return
1925 multiple clipping rectangles, we exclude the row of the glyph
1926 string from the clipping rectangle. This is to avoid drawing
1927 the same text on the environment with anti-aliasing. */
1928 #ifdef CONVERT_FROM_XRECT
1929 XRectangle rs[2];
1930 #else
1931 XRectangle *rs = rects;
1932 #endif
1933 int i = 0, row_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, s->row->y);
1934
1935 if (s->for_overlaps & OVERLAPS_PRED)
1936 {
1937 rs[i] = r;
1938 if (r.y + r.height > row_y)
1939 {
1940 if (r.y < row_y)
1941 rs[i].height = row_y - r.y;
1942 else
1943 rs[i].height = 0;
1944 }
1945 i++;
1946 }
1947 if (s->for_overlaps & OVERLAPS_SUCC)
1948 {
1949 rs[i] = r;
1950 if (r.y < row_y + s->row->visible_height)
1951 {
1952 if (r.y + r.height > row_y + s->row->visible_height)
1953 {
1954 rs[i].y = row_y + s->row->visible_height;
1955 rs[i].height = r.y + r.height - rs[i].y;
1956 }
1957 else
1958 rs[i].height = 0;
1959 }
1960 i++;
1961 }
1962
1963 n = i;
1964 #ifdef CONVERT_FROM_XRECT
1965 for (i = 0; i < n; i++)
1966 CONVERT_FROM_XRECT (rs[i], rects[i]);
1967 #endif
1968 return n;
1969 }
1970 }
1971
1972 /* EXPORT:
1973 Return in *NR the clipping rectangle for glyph string S. */
1974
1975 void
1976 get_glyph_string_clip_rect (s, nr)
1977 struct glyph_string *s;
1978 NativeRectangle *nr;
1979 {
1980 get_glyph_string_clip_rects (s, nr, 1);
1981 }
1982
1983
1984 /* EXPORT:
1985 Return the position and height of the phys cursor in window W.
1986 Set w->phys_cursor_width to width of phys cursor.
1987 */
1988
1989 int
1990 get_phys_cursor_geometry (w, row, glyph, heightp)
1991 struct window *w;
1992 struct glyph_row *row;
1993 struct glyph *glyph;
1994 int *heightp;
1995 {
1996 struct frame *f = XFRAME (WINDOW_FRAME (w));
1997 int y, wd, h, h0, y0;
1998
1999 /* Compute the width of the rectangle to draw. If on a stretch
2000 glyph, and `x-stretch-block-cursor' is nil, don't draw a
2001 rectangle as wide as the glyph, but use a canonical character
2002 width instead. */
2003 wd = glyph->pixel_width - 1;
2004 #ifdef HAVE_NTGUI
2005 wd++; /* Why? */
2006 #endif
2007 if (glyph->type == STRETCH_GLYPH
2008 && !x_stretch_cursor_p)
2009 wd = min (FRAME_COLUMN_WIDTH (f), wd);
2010 w->phys_cursor_width = wd;
2011
2012 y = w->phys_cursor.y + row->ascent - glyph->ascent;
2013
2014 /* If y is below window bottom, ensure that we still see a cursor. */
2015 h0 = min (FRAME_LINE_HEIGHT (f), row->visible_height);
2016
2017 h = max (h0, glyph->ascent + glyph->descent);
2018 h0 = min (h0, glyph->ascent + glyph->descent);
2019
2020 y0 = WINDOW_HEADER_LINE_HEIGHT (w);
2021 if (y < y0)
2022 {
2023 h = max (h - (y0 - y) + 1, h0);
2024 y = y0 - 1;
2025 }
2026 else
2027 {
2028 y0 = window_text_bottom_y (w) - h0;
2029 if (y > y0)
2030 {
2031 h += y - y0;
2032 y = y0;
2033 }
2034 }
2035
2036 *heightp = h;
2037 return WINDOW_TO_FRAME_PIXEL_Y (w, y);
2038 }
2039
2040 /*
2041 * Remember which glyph the mouse is over.
2042 */
2043
2044 void
2045 remember_mouse_glyph (f, gx, gy, rect)
2046 struct frame *f;
2047 int gx, gy;
2048 NativeRectangle *rect;
2049 {
2050 Lisp_Object window;
2051 struct window *w;
2052 struct glyph_row *r, *gr, *end_row;
2053 enum window_part part;
2054 enum glyph_row_area area;
2055 int x, y, width, height;
2056
2057 /* Try to determine frame pixel position and size of the glyph under
2058 frame pixel coordinates X/Y on frame F. */
2059
2060 window = window_from_coordinates (f, gx, gy, &part, &x, &y, 0);
2061 if (NILP (window))
2062 {
2063 width = FRAME_SMALLEST_CHAR_WIDTH (f);
2064 height = FRAME_SMALLEST_FONT_HEIGHT (f);
2065 goto virtual_glyph;
2066 }
2067
2068 w = XWINDOW (window);
2069 width = WINDOW_FRAME_COLUMN_WIDTH (w);
2070 height = WINDOW_FRAME_LINE_HEIGHT (w);
2071
2072 r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
2073 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
2074
2075 if (w->pseudo_window_p)
2076 {
2077 area = TEXT_AREA;
2078 part = ON_MODE_LINE; /* Don't adjust margin. */
2079 goto text_glyph;
2080 }
2081
2082 switch (part)
2083 {
2084 case ON_LEFT_MARGIN:
2085 area = LEFT_MARGIN_AREA;
2086 goto text_glyph;
2087
2088 case ON_RIGHT_MARGIN:
2089 area = RIGHT_MARGIN_AREA;
2090 goto text_glyph;
2091
2092 case ON_HEADER_LINE:
2093 case ON_MODE_LINE:
2094 gr = (part == ON_HEADER_LINE
2095 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
2096 : MATRIX_MODE_LINE_ROW (w->current_matrix));
2097 gy = gr->y;
2098 area = TEXT_AREA;
2099 goto text_glyph_row_found;
2100
2101 case ON_TEXT:
2102 area = TEXT_AREA;
2103
2104 text_glyph:
2105 gr = 0; gy = 0;
2106 for (; r <= end_row && r->enabled_p; ++r)
2107 if (r->y + r->height > y)
2108 {
2109 gr = r; gy = r->y;
2110 break;
2111 }
2112
2113 text_glyph_row_found:
2114 if (gr && gy <= y)
2115 {
2116 struct glyph *g = gr->glyphs[area];
2117 struct glyph *end = g + gr->used[area];
2118
2119 height = gr->height;
2120 for (gx = gr->x; g < end; gx += g->pixel_width, ++g)
2121 if (gx + g->pixel_width > x)
2122 break;
2123
2124 if (g < end)
2125 {
2126 if (g->type == IMAGE_GLYPH)
2127 {
2128 /* Don't remember when mouse is over image, as
2129 image may have hot-spots. */
2130 STORE_NATIVE_RECT (*rect, 0, 0, 0, 0);
2131 return;
2132 }
2133 width = g->pixel_width;
2134 }
2135 else
2136 {
2137 /* Use nominal char spacing at end of line. */
2138 x -= gx;
2139 gx += (x / width) * width;
2140 }
2141
2142 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2143 gx += window_box_left_offset (w, area);
2144 }
2145 else
2146 {
2147 /* Use nominal line height at end of window. */
2148 gx = (x / width) * width;
2149 y -= gy;
2150 gy += (y / height) * height;
2151 }
2152 break;
2153
2154 case ON_LEFT_FRINGE:
2155 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2156 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w)
2157 : window_box_right_offset (w, LEFT_MARGIN_AREA));
2158 width = WINDOW_LEFT_FRINGE_WIDTH (w);
2159 goto row_glyph;
2160
2161 case ON_RIGHT_FRINGE:
2162 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2163 ? window_box_right_offset (w, RIGHT_MARGIN_AREA)
2164 : window_box_right_offset (w, TEXT_AREA));
2165 width = WINDOW_RIGHT_FRINGE_WIDTH (w);
2166 goto row_glyph;
2167
2168 case ON_SCROLL_BAR:
2169 gx = (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
2170 ? 0
2171 : (window_box_right_offset (w, RIGHT_MARGIN_AREA)
2172 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2173 ? WINDOW_RIGHT_FRINGE_WIDTH (w)
2174 : 0)));
2175 width = WINDOW_SCROLL_BAR_AREA_WIDTH (w);
2176
2177 row_glyph:
2178 gr = 0, gy = 0;
2179 for (; r <= end_row && r->enabled_p; ++r)
2180 if (r->y + r->height > y)
2181 {
2182 gr = r; gy = r->y;
2183 break;
2184 }
2185
2186 if (gr && gy <= y)
2187 height = gr->height;
2188 else
2189 {
2190 /* Use nominal line height at end of window. */
2191 y -= gy;
2192 gy += (y / height) * height;
2193 }
2194 break;
2195
2196 default:
2197 ;
2198 virtual_glyph:
2199 /* If there is no glyph under the mouse, then we divide the screen
2200 into a grid of the smallest glyph in the frame, and use that
2201 as our "glyph". */
2202
2203 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to
2204 round down even for negative values. */
2205 if (gx < 0)
2206 gx -= width - 1;
2207 if (gy < 0)
2208 gy -= height - 1;
2209
2210 gx = (gx / width) * width;
2211 gy = (gy / height) * height;
2212
2213 goto store_rect;
2214 }
2215
2216 gx += WINDOW_LEFT_EDGE_X (w);
2217 gy += WINDOW_TOP_EDGE_Y (w);
2218
2219 store_rect:
2220 STORE_NATIVE_RECT (*rect, gx, gy, width, height);
2221
2222 /* Visible feedback for debugging. */
2223 #if 0
2224 #if HAVE_X_WINDOWS
2225 XDrawRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2226 f->output_data.x->normal_gc,
2227 gx, gy, width, height);
2228 #endif
2229 #endif
2230 }
2231
2232
2233 #endif /* HAVE_WINDOW_SYSTEM */
2234
2235 \f
2236 /***********************************************************************
2237 Lisp form evaluation
2238 ***********************************************************************/
2239
2240 /* Error handler for safe_eval and safe_call. */
2241
2242 static Lisp_Object
2243 safe_eval_handler (arg)
2244 Lisp_Object arg;
2245 {
2246 add_to_log ("Error during redisplay: %s", arg, Qnil);
2247 return Qnil;
2248 }
2249
2250
2251 /* Evaluate SEXPR and return the result, or nil if something went
2252 wrong. Prevent redisplay during the evaluation. */
2253
2254 Lisp_Object
2255 safe_eval (sexpr)
2256 Lisp_Object sexpr;
2257 {
2258 Lisp_Object val;
2259
2260 if (inhibit_eval_during_redisplay)
2261 val = Qnil;
2262 else
2263 {
2264 int count = SPECPDL_INDEX ();
2265 struct gcpro gcpro1;
2266
2267 GCPRO1 (sexpr);
2268 specbind (Qinhibit_redisplay, Qt);
2269 /* Use Qt to ensure debugger does not run,
2270 so there is no possibility of wanting to redisplay. */
2271 val = internal_condition_case_1 (Feval, sexpr, Qt,
2272 safe_eval_handler);
2273 UNGCPRO;
2274 val = unbind_to (count, val);
2275 }
2276
2277 return val;
2278 }
2279
2280
2281 /* Call function ARGS[0] with arguments ARGS[1] to ARGS[NARGS - 1].
2282 Return the result, or nil if something went wrong. Prevent
2283 redisplay during the evaluation. */
2284
2285 Lisp_Object
2286 safe_call (nargs, args)
2287 int nargs;
2288 Lisp_Object *args;
2289 {
2290 Lisp_Object val;
2291
2292 if (inhibit_eval_during_redisplay)
2293 val = Qnil;
2294 else
2295 {
2296 int count = SPECPDL_INDEX ();
2297 struct gcpro gcpro1;
2298
2299 GCPRO1 (args[0]);
2300 gcpro1.nvars = nargs;
2301 specbind (Qinhibit_redisplay, Qt);
2302 /* Use Qt to ensure debugger does not run,
2303 so there is no possibility of wanting to redisplay. */
2304 val = internal_condition_case_2 (Ffuncall, nargs, args, Qt,
2305 safe_eval_handler);
2306 UNGCPRO;
2307 val = unbind_to (count, val);
2308 }
2309
2310 return val;
2311 }
2312
2313
2314 /* Call function FN with one argument ARG.
2315 Return the result, or nil if something went wrong. */
2316
2317 Lisp_Object
2318 safe_call1 (fn, arg)
2319 Lisp_Object fn, arg;
2320 {
2321 Lisp_Object args[2];
2322 args[0] = fn;
2323 args[1] = arg;
2324 return safe_call (2, args);
2325 }
2326
2327
2328 \f
2329 /***********************************************************************
2330 Debugging
2331 ***********************************************************************/
2332
2333 #if 0
2334
2335 /* Define CHECK_IT to perform sanity checks on iterators.
2336 This is for debugging. It is too slow to do unconditionally. */
2337
2338 static void
2339 check_it (it)
2340 struct it *it;
2341 {
2342 if (it->method == GET_FROM_STRING)
2343 {
2344 xassert (STRINGP (it->string));
2345 xassert (IT_STRING_CHARPOS (*it) >= 0);
2346 }
2347 else
2348 {
2349 xassert (IT_STRING_CHARPOS (*it) < 0);
2350 if (it->method == GET_FROM_BUFFER)
2351 {
2352 /* Check that character and byte positions agree. */
2353 xassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
2354 }
2355 }
2356
2357 if (it->dpvec)
2358 xassert (it->current.dpvec_index >= 0);
2359 else
2360 xassert (it->current.dpvec_index < 0);
2361 }
2362
2363 #define CHECK_IT(IT) check_it ((IT))
2364
2365 #else /* not 0 */
2366
2367 #define CHECK_IT(IT) (void) 0
2368
2369 #endif /* not 0 */
2370
2371
2372 #if GLYPH_DEBUG
2373
2374 /* Check that the window end of window W is what we expect it
2375 to be---the last row in the current matrix displaying text. */
2376
2377 static void
2378 check_window_end (w)
2379 struct window *w;
2380 {
2381 if (!MINI_WINDOW_P (w)
2382 && !NILP (w->window_end_valid))
2383 {
2384 struct glyph_row *row;
2385 xassert ((row = MATRIX_ROW (w->current_matrix,
2386 XFASTINT (w->window_end_vpos)),
2387 !row->enabled_p
2388 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
2389 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
2390 }
2391 }
2392
2393 #define CHECK_WINDOW_END(W) check_window_end ((W))
2394
2395 #else /* not GLYPH_DEBUG */
2396
2397 #define CHECK_WINDOW_END(W) (void) 0
2398
2399 #endif /* not GLYPH_DEBUG */
2400
2401
2402 \f
2403 /***********************************************************************
2404 Iterator initialization
2405 ***********************************************************************/
2406
2407 /* Initialize IT for displaying current_buffer in window W, starting
2408 at character position CHARPOS. CHARPOS < 0 means that no buffer
2409 position is specified which is useful when the iterator is assigned
2410 a position later. BYTEPOS is the byte position corresponding to
2411 CHARPOS. BYTEPOS < 0 means compute it from CHARPOS.
2412
2413 If ROW is not null, calls to produce_glyphs with IT as parameter
2414 will produce glyphs in that row.
2415
2416 BASE_FACE_ID is the id of a base face to use. It must be one of
2417 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2418 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2419 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2420
2421 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2422 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2423 will be initialized to use the corresponding mode line glyph row of
2424 the desired matrix of W. */
2425
2426 void
2427 init_iterator (it, w, charpos, bytepos, row, base_face_id)
2428 struct it *it;
2429 struct window *w;
2430 int charpos, bytepos;
2431 struct glyph_row *row;
2432 enum face_id base_face_id;
2433 {
2434 int highlight_region_p;
2435
2436 /* Some precondition checks. */
2437 xassert (w != NULL && it != NULL);
2438 xassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2439 && charpos <= ZV));
2440
2441 /* If face attributes have been changed since the last redisplay,
2442 free realized faces now because they depend on face definitions
2443 that might have changed. Don't free faces while there might be
2444 desired matrices pending which reference these faces. */
2445 if (face_change_count && !inhibit_free_realized_faces)
2446 {
2447 face_change_count = 0;
2448 free_all_realized_faces (Qnil);
2449 }
2450
2451 /* Use one of the mode line rows of W's desired matrix if
2452 appropriate. */
2453 if (row == NULL)
2454 {
2455 if (base_face_id == MODE_LINE_FACE_ID
2456 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2457 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2458 else if (base_face_id == HEADER_LINE_FACE_ID)
2459 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2460 }
2461
2462 /* Clear IT. */
2463 bzero (it, sizeof *it);
2464 it->current.overlay_string_index = -1;
2465 it->current.dpvec_index = -1;
2466 it->base_face_id = base_face_id;
2467 it->string = Qnil;
2468 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2469
2470 /* The window in which we iterate over current_buffer: */
2471 XSETWINDOW (it->window, w);
2472 it->w = w;
2473 it->f = XFRAME (w->frame);
2474
2475 /* Extra space between lines (on window systems only). */
2476 if (base_face_id == DEFAULT_FACE_ID
2477 && FRAME_WINDOW_P (it->f))
2478 {
2479 if (NATNUMP (current_buffer->extra_line_spacing))
2480 it->extra_line_spacing = XFASTINT (current_buffer->extra_line_spacing);
2481 else if (FLOATP (current_buffer->extra_line_spacing))
2482 it->extra_line_spacing = (XFLOAT_DATA (current_buffer->extra_line_spacing)
2483 * FRAME_LINE_HEIGHT (it->f));
2484 else if (it->f->extra_line_spacing > 0)
2485 it->extra_line_spacing = it->f->extra_line_spacing;
2486 it->max_extra_line_spacing = 0;
2487 }
2488
2489 /* If realized faces have been removed, e.g. because of face
2490 attribute changes of named faces, recompute them. When running
2491 in batch mode, the face cache of Vterminal_frame is null. If
2492 we happen to get called, make a dummy face cache. */
2493 if (noninteractive && FRAME_FACE_CACHE (it->f) == NULL)
2494 init_frame_faces (it->f);
2495 if (FRAME_FACE_CACHE (it->f)->used == 0)
2496 recompute_basic_faces (it->f);
2497
2498 /* Current value of the `slice', `space-width', and 'height' properties. */
2499 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
2500 it->space_width = Qnil;
2501 it->font_height = Qnil;
2502 it->override_ascent = -1;
2503
2504 /* Are control characters displayed as `^C'? */
2505 it->ctl_arrow_p = !NILP (current_buffer->ctl_arrow);
2506
2507 /* -1 means everything between a CR and the following line end
2508 is invisible. >0 means lines indented more than this value are
2509 invisible. */
2510 it->selective = (INTEGERP (current_buffer->selective_display)
2511 ? XFASTINT (current_buffer->selective_display)
2512 : (!NILP (current_buffer->selective_display)
2513 ? -1 : 0));
2514 it->selective_display_ellipsis_p
2515 = !NILP (current_buffer->selective_display_ellipses);
2516
2517 /* Display table to use. */
2518 it->dp = window_display_table (w);
2519
2520 /* Are multibyte characters enabled in current_buffer? */
2521 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
2522
2523 /* Non-zero if we should highlight the region. */
2524 highlight_region_p
2525 = (!NILP (Vtransient_mark_mode)
2526 && !NILP (current_buffer->mark_active)
2527 && XMARKER (current_buffer->mark)->buffer != 0);
2528
2529 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
2530 start and end of a visible region in window IT->w. Set both to
2531 -1 to indicate no region. */
2532 if (highlight_region_p
2533 /* Maybe highlight only in selected window. */
2534 && (/* Either show region everywhere. */
2535 highlight_nonselected_windows
2536 /* Or show region in the selected window. */
2537 || w == XWINDOW (selected_window)
2538 /* Or show the region if we are in the mini-buffer and W is
2539 the window the mini-buffer refers to. */
2540 || (MINI_WINDOW_P (XWINDOW (selected_window))
2541 && WINDOWP (minibuf_selected_window)
2542 && w == XWINDOW (minibuf_selected_window))))
2543 {
2544 int charpos = marker_position (current_buffer->mark);
2545 it->region_beg_charpos = min (PT, charpos);
2546 it->region_end_charpos = max (PT, charpos);
2547 }
2548 else
2549 it->region_beg_charpos = it->region_end_charpos = -1;
2550
2551 /* Get the position at which the redisplay_end_trigger hook should
2552 be run, if it is to be run at all. */
2553 if (MARKERP (w->redisplay_end_trigger)
2554 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2555 it->redisplay_end_trigger_charpos
2556 = marker_position (w->redisplay_end_trigger);
2557 else if (INTEGERP (w->redisplay_end_trigger))
2558 it->redisplay_end_trigger_charpos = XINT (w->redisplay_end_trigger);
2559
2560 /* Correct bogus values of tab_width. */
2561 it->tab_width = XINT (current_buffer->tab_width);
2562 if (it->tab_width <= 0 || it->tab_width > 1000)
2563 it->tab_width = 8;
2564
2565 /* Are lines in the display truncated? */
2566 it->truncate_lines_p
2567 = (base_face_id != DEFAULT_FACE_ID
2568 || XINT (it->w->hscroll)
2569 || (truncate_partial_width_windows
2570 && !WINDOW_FULL_WIDTH_P (it->w))
2571 || !NILP (current_buffer->truncate_lines));
2572
2573 /* Get dimensions of truncation and continuation glyphs. These are
2574 displayed as fringe bitmaps under X, so we don't need them for such
2575 frames. */
2576 if (!FRAME_WINDOW_P (it->f))
2577 {
2578 if (it->truncate_lines_p)
2579 {
2580 /* We will need the truncation glyph. */
2581 xassert (it->glyph_row == NULL);
2582 produce_special_glyphs (it, IT_TRUNCATION);
2583 it->truncation_pixel_width = it->pixel_width;
2584 }
2585 else
2586 {
2587 /* We will need the continuation glyph. */
2588 xassert (it->glyph_row == NULL);
2589 produce_special_glyphs (it, IT_CONTINUATION);
2590 it->continuation_pixel_width = it->pixel_width;
2591 }
2592
2593 /* Reset these values to zero because the produce_special_glyphs
2594 above has changed them. */
2595 it->pixel_width = it->ascent = it->descent = 0;
2596 it->phys_ascent = it->phys_descent = 0;
2597 }
2598
2599 /* Set this after getting the dimensions of truncation and
2600 continuation glyphs, so that we don't produce glyphs when calling
2601 produce_special_glyphs, above. */
2602 it->glyph_row = row;
2603 it->area = TEXT_AREA;
2604
2605 /* Get the dimensions of the display area. The display area
2606 consists of the visible window area plus a horizontally scrolled
2607 part to the left of the window. All x-values are relative to the
2608 start of this total display area. */
2609 if (base_face_id != DEFAULT_FACE_ID)
2610 {
2611 /* Mode lines, menu bar in terminal frames. */
2612 it->first_visible_x = 0;
2613 it->last_visible_x = WINDOW_TOTAL_WIDTH (w);
2614 }
2615 else
2616 {
2617 it->first_visible_x
2618 = XFASTINT (it->w->hscroll) * FRAME_COLUMN_WIDTH (it->f);
2619 it->last_visible_x = (it->first_visible_x
2620 + window_box_width (w, TEXT_AREA));
2621
2622 /* If we truncate lines, leave room for the truncator glyph(s) at
2623 the right margin. Otherwise, leave room for the continuation
2624 glyph(s). Truncation and continuation glyphs are not inserted
2625 for window-based redisplay. */
2626 if (!FRAME_WINDOW_P (it->f))
2627 {
2628 if (it->truncate_lines_p)
2629 it->last_visible_x -= it->truncation_pixel_width;
2630 else
2631 it->last_visible_x -= it->continuation_pixel_width;
2632 }
2633
2634 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2635 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2636 }
2637
2638 /* Leave room for a border glyph. */
2639 if (!FRAME_WINDOW_P (it->f)
2640 && !WINDOW_RIGHTMOST_P (it->w))
2641 it->last_visible_x -= 1;
2642
2643 it->last_visible_y = window_text_bottom_y (w);
2644
2645 /* For mode lines and alike, arrange for the first glyph having a
2646 left box line if the face specifies a box. */
2647 if (base_face_id != DEFAULT_FACE_ID)
2648 {
2649 struct face *face;
2650
2651 it->face_id = base_face_id;
2652
2653 /* If we have a boxed mode line, make the first character appear
2654 with a left box line. */
2655 face = FACE_FROM_ID (it->f, base_face_id);
2656 if (face->box != FACE_NO_BOX)
2657 it->start_of_box_run_p = 1;
2658 }
2659
2660 /* If a buffer position was specified, set the iterator there,
2661 getting overlays and face properties from that position. */
2662 if (charpos >= BUF_BEG (current_buffer))
2663 {
2664 it->end_charpos = ZV;
2665 it->face_id = -1;
2666 IT_CHARPOS (*it) = charpos;
2667
2668 /* Compute byte position if not specified. */
2669 if (bytepos < charpos)
2670 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
2671 else
2672 IT_BYTEPOS (*it) = bytepos;
2673
2674 it->start = it->current;
2675
2676 /* Compute faces etc. */
2677 reseat (it, it->current.pos, 1);
2678 }
2679
2680 CHECK_IT (it);
2681 }
2682
2683
2684 /* Initialize IT for the display of window W with window start POS. */
2685
2686 void
2687 start_display (it, w, pos)
2688 struct it *it;
2689 struct window *w;
2690 struct text_pos pos;
2691 {
2692 struct glyph_row *row;
2693 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
2694
2695 row = w->desired_matrix->rows + first_vpos;
2696 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
2697 it->first_vpos = first_vpos;
2698
2699 /* Don't reseat to previous visible line start if current start
2700 position is in a string or image. */
2701 if (it->method == GET_FROM_BUFFER && !it->truncate_lines_p)
2702 {
2703 int start_at_line_beg_p;
2704 int first_y = it->current_y;
2705
2706 /* If window start is not at a line start, skip forward to POS to
2707 get the correct continuation lines width. */
2708 start_at_line_beg_p = (CHARPOS (pos) == BEGV
2709 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
2710 if (!start_at_line_beg_p)
2711 {
2712 int new_x;
2713
2714 reseat_at_previous_visible_line_start (it);
2715 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
2716
2717 new_x = it->current_x + it->pixel_width;
2718
2719 /* If lines are continued, this line may end in the middle
2720 of a multi-glyph character (e.g. a control character
2721 displayed as \003, or in the middle of an overlay
2722 string). In this case move_it_to above will not have
2723 taken us to the start of the continuation line but to the
2724 end of the continued line. */
2725 if (it->current_x > 0
2726 && !it->truncate_lines_p /* Lines are continued. */
2727 && (/* And glyph doesn't fit on the line. */
2728 new_x > it->last_visible_x
2729 /* Or it fits exactly and we're on a window
2730 system frame. */
2731 || (new_x == it->last_visible_x
2732 && FRAME_WINDOW_P (it->f))))
2733 {
2734 if (it->current.dpvec_index >= 0
2735 || it->current.overlay_string_index >= 0)
2736 {
2737 set_iterator_to_next (it, 1);
2738 move_it_in_display_line_to (it, -1, -1, 0);
2739 }
2740
2741 it->continuation_lines_width += it->current_x;
2742 }
2743
2744 /* We're starting a new display line, not affected by the
2745 height of the continued line, so clear the appropriate
2746 fields in the iterator structure. */
2747 it->max_ascent = it->max_descent = 0;
2748 it->max_phys_ascent = it->max_phys_descent = 0;
2749
2750 it->current_y = first_y;
2751 it->vpos = 0;
2752 it->current_x = it->hpos = 0;
2753 }
2754 }
2755
2756 #if 0 /* Don't assert the following because start_display is sometimes
2757 called intentionally with a window start that is not at a
2758 line start. Please leave this code in as a comment. */
2759
2760 /* Window start should be on a line start, now. */
2761 xassert (it->continuation_lines_width
2762 || IT_CHARPOS (it) == BEGV
2763 || FETCH_BYTE (IT_BYTEPOS (it) - 1) == '\n');
2764 #endif /* 0 */
2765 }
2766
2767
2768 /* Return 1 if POS is a position in ellipses displayed for invisible
2769 text. W is the window we display, for text property lookup. */
2770
2771 static int
2772 in_ellipses_for_invisible_text_p (pos, w)
2773 struct display_pos *pos;
2774 struct window *w;
2775 {
2776 Lisp_Object prop, window;
2777 int ellipses_p = 0;
2778 int charpos = CHARPOS (pos->pos);
2779
2780 /* If POS specifies a position in a display vector, this might
2781 be for an ellipsis displayed for invisible text. We won't
2782 get the iterator set up for delivering that ellipsis unless
2783 we make sure that it gets aware of the invisible text. */
2784 if (pos->dpvec_index >= 0
2785 && pos->overlay_string_index < 0
2786 && CHARPOS (pos->string_pos) < 0
2787 && charpos > BEGV
2788 && (XSETWINDOW (window, w),
2789 prop = Fget_char_property (make_number (charpos),
2790 Qinvisible, window),
2791 !TEXT_PROP_MEANS_INVISIBLE (prop)))
2792 {
2793 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
2794 window);
2795 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
2796 }
2797
2798 return ellipses_p;
2799 }
2800
2801
2802 /* Initialize IT for stepping through current_buffer in window W,
2803 starting at position POS that includes overlay string and display
2804 vector/ control character translation position information. Value
2805 is zero if there are overlay strings with newlines at POS. */
2806
2807 static int
2808 init_from_display_pos (it, w, pos)
2809 struct it *it;
2810 struct window *w;
2811 struct display_pos *pos;
2812 {
2813 int charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
2814 int i, overlay_strings_with_newlines = 0;
2815
2816 /* If POS specifies a position in a display vector, this might
2817 be for an ellipsis displayed for invisible text. We won't
2818 get the iterator set up for delivering that ellipsis unless
2819 we make sure that it gets aware of the invisible text. */
2820 if (in_ellipses_for_invisible_text_p (pos, w))
2821 {
2822 --charpos;
2823 bytepos = 0;
2824 }
2825
2826 /* Keep in mind: the call to reseat in init_iterator skips invisible
2827 text, so we might end up at a position different from POS. This
2828 is only a problem when POS is a row start after a newline and an
2829 overlay starts there with an after-string, and the overlay has an
2830 invisible property. Since we don't skip invisible text in
2831 display_line and elsewhere immediately after consuming the
2832 newline before the row start, such a POS will not be in a string,
2833 but the call to init_iterator below will move us to the
2834 after-string. */
2835 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
2836
2837 /* This only scans the current chunk -- it should scan all chunks.
2838 However, OVERLAY_STRING_CHUNK_SIZE has been increased from 3 in 21.1
2839 to 16 in 22.1 to make this a lesser problem. */
2840 for (i = 0; i < it->n_overlay_strings && i < OVERLAY_STRING_CHUNK_SIZE; ++i)
2841 {
2842 const char *s = SDATA (it->overlay_strings[i]);
2843 const char *e = s + SBYTES (it->overlay_strings[i]);
2844
2845 while (s < e && *s != '\n')
2846 ++s;
2847
2848 if (s < e)
2849 {
2850 overlay_strings_with_newlines = 1;
2851 break;
2852 }
2853 }
2854
2855 /* If position is within an overlay string, set up IT to the right
2856 overlay string. */
2857 if (pos->overlay_string_index >= 0)
2858 {
2859 int relative_index;
2860
2861 /* If the first overlay string happens to have a `display'
2862 property for an image, the iterator will be set up for that
2863 image, and we have to undo that setup first before we can
2864 correct the overlay string index. */
2865 if (it->method == GET_FROM_IMAGE)
2866 pop_it (it);
2867
2868 /* We already have the first chunk of overlay strings in
2869 IT->overlay_strings. Load more until the one for
2870 pos->overlay_string_index is in IT->overlay_strings. */
2871 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
2872 {
2873 int n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
2874 it->current.overlay_string_index = 0;
2875 while (n--)
2876 {
2877 load_overlay_strings (it, 0);
2878 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
2879 }
2880 }
2881
2882 it->current.overlay_string_index = pos->overlay_string_index;
2883 relative_index = (it->current.overlay_string_index
2884 % OVERLAY_STRING_CHUNK_SIZE);
2885 it->string = it->overlay_strings[relative_index];
2886 xassert (STRINGP (it->string));
2887 it->current.string_pos = pos->string_pos;
2888 it->method = GET_FROM_STRING;
2889 }
2890
2891 #if 0 /* This is bogus because POS not having an overlay string
2892 position does not mean it's after the string. Example: A
2893 line starting with a before-string and initialization of IT
2894 to the previous row's end position. */
2895 else if (it->current.overlay_string_index >= 0)
2896 {
2897 /* If POS says we're already after an overlay string ending at
2898 POS, make sure to pop the iterator because it will be in
2899 front of that overlay string. When POS is ZV, we've thereby
2900 also ``processed'' overlay strings at ZV. */
2901 while (it->sp)
2902 pop_it (it);
2903 it->current.overlay_string_index = -1;
2904 it->method = GET_FROM_BUFFER;
2905 if (CHARPOS (pos->pos) == ZV)
2906 it->overlay_strings_at_end_processed_p = 1;
2907 }
2908 #endif /* 0 */
2909
2910 if (CHARPOS (pos->string_pos) >= 0)
2911 {
2912 /* Recorded position is not in an overlay string, but in another
2913 string. This can only be a string from a `display' property.
2914 IT should already be filled with that string. */
2915 it->current.string_pos = pos->string_pos;
2916 xassert (STRINGP (it->string));
2917 }
2918
2919 /* Restore position in display vector translations, control
2920 character translations or ellipses. */
2921 if (pos->dpvec_index >= 0)
2922 {
2923 if (it->dpvec == NULL)
2924 get_next_display_element (it);
2925 xassert (it->dpvec && it->current.dpvec_index == 0);
2926 it->current.dpvec_index = pos->dpvec_index;
2927 }
2928
2929 CHECK_IT (it);
2930 return !overlay_strings_with_newlines;
2931 }
2932
2933
2934 /* Initialize IT for stepping through current_buffer in window W
2935 starting at ROW->start. */
2936
2937 static void
2938 init_to_row_start (it, w, row)
2939 struct it *it;
2940 struct window *w;
2941 struct glyph_row *row;
2942 {
2943 init_from_display_pos (it, w, &row->start);
2944 it->start = row->start;
2945 it->continuation_lines_width = row->continuation_lines_width;
2946 CHECK_IT (it);
2947 }
2948
2949
2950 /* Initialize IT for stepping through current_buffer in window W
2951 starting in the line following ROW, i.e. starting at ROW->end.
2952 Value is zero if there are overlay strings with newlines at ROW's
2953 end position. */
2954
2955 static int
2956 init_to_row_end (it, w, row)
2957 struct it *it;
2958 struct window *w;
2959 struct glyph_row *row;
2960 {
2961 int success = 0;
2962
2963 if (init_from_display_pos (it, w, &row->end))
2964 {
2965 if (row->continued_p)
2966 it->continuation_lines_width
2967 = row->continuation_lines_width + row->pixel_width;
2968 CHECK_IT (it);
2969 success = 1;
2970 }
2971
2972 return success;
2973 }
2974
2975
2976
2977 \f
2978 /***********************************************************************
2979 Text properties
2980 ***********************************************************************/
2981
2982 /* Called when IT reaches IT->stop_charpos. Handle text property and
2983 overlay changes. Set IT->stop_charpos to the next position where
2984 to stop. */
2985
2986 static void
2987 handle_stop (it)
2988 struct it *it;
2989 {
2990 enum prop_handled handled;
2991 int handle_overlay_change_p;
2992 struct props *p;
2993
2994 it->dpvec = NULL;
2995 it->current.dpvec_index = -1;
2996 handle_overlay_change_p = !it->ignore_overlay_strings_at_pos_p;
2997 it->ignore_overlay_strings_at_pos_p = 0;
2998
2999 /* Use face of preceding text for ellipsis (if invisible) */
3000 if (it->selective_display_ellipsis_p)
3001 it->saved_face_id = it->face_id;
3002
3003 do
3004 {
3005 handled = HANDLED_NORMALLY;
3006
3007 /* Call text property handlers. */
3008 for (p = it_props; p->handler; ++p)
3009 {
3010 handled = p->handler (it);
3011
3012 if (handled == HANDLED_RECOMPUTE_PROPS)
3013 break;
3014 else if (handled == HANDLED_RETURN)
3015 return;
3016 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
3017 handle_overlay_change_p = 0;
3018 }
3019
3020 if (handled != HANDLED_RECOMPUTE_PROPS)
3021 {
3022 /* Don't check for overlay strings below when set to deliver
3023 characters from a display vector. */
3024 if (it->method == GET_FROM_DISPLAY_VECTOR)
3025 handle_overlay_change_p = 0;
3026
3027 /* Handle overlay changes. */
3028 if (handle_overlay_change_p)
3029 handled = handle_overlay_change (it);
3030
3031 /* Determine where to stop next. */
3032 if (handled == HANDLED_NORMALLY)
3033 compute_stop_pos (it);
3034 }
3035 }
3036 while (handled == HANDLED_RECOMPUTE_PROPS);
3037 }
3038
3039
3040 /* Compute IT->stop_charpos from text property and overlay change
3041 information for IT's current position. */
3042
3043 static void
3044 compute_stop_pos (it)
3045 struct it *it;
3046 {
3047 register INTERVAL iv, next_iv;
3048 Lisp_Object object, limit, position;
3049
3050 /* If nowhere else, stop at the end. */
3051 it->stop_charpos = it->end_charpos;
3052
3053 if (STRINGP (it->string))
3054 {
3055 /* Strings are usually short, so don't limit the search for
3056 properties. */
3057 object = it->string;
3058 limit = Qnil;
3059 position = make_number (IT_STRING_CHARPOS (*it));
3060 }
3061 else
3062 {
3063 int charpos;
3064
3065 /* If next overlay change is in front of the current stop pos
3066 (which is IT->end_charpos), stop there. Note: value of
3067 next_overlay_change is point-max if no overlay change
3068 follows. */
3069 charpos = next_overlay_change (IT_CHARPOS (*it));
3070 if (charpos < it->stop_charpos)
3071 it->stop_charpos = charpos;
3072
3073 /* If showing the region, we have to stop at the region
3074 start or end because the face might change there. */
3075 if (it->region_beg_charpos > 0)
3076 {
3077 if (IT_CHARPOS (*it) < it->region_beg_charpos)
3078 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
3079 else if (IT_CHARPOS (*it) < it->region_end_charpos)
3080 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
3081 }
3082
3083 /* Set up variables for computing the stop position from text
3084 property changes. */
3085 XSETBUFFER (object, current_buffer);
3086 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
3087 position = make_number (IT_CHARPOS (*it));
3088
3089 }
3090
3091 /* Get the interval containing IT's position. Value is a null
3092 interval if there isn't such an interval. */
3093 iv = validate_interval_range (object, &position, &position, 0);
3094 if (!NULL_INTERVAL_P (iv))
3095 {
3096 Lisp_Object values_here[LAST_PROP_IDX];
3097 struct props *p;
3098
3099 /* Get properties here. */
3100 for (p = it_props; p->handler; ++p)
3101 values_here[p->idx] = textget (iv->plist, *p->name);
3102
3103 /* Look for an interval following iv that has different
3104 properties. */
3105 for (next_iv = next_interval (iv);
3106 (!NULL_INTERVAL_P (next_iv)
3107 && (NILP (limit)
3108 || XFASTINT (limit) > next_iv->position));
3109 next_iv = next_interval (next_iv))
3110 {
3111 for (p = it_props; p->handler; ++p)
3112 {
3113 Lisp_Object new_value;
3114
3115 new_value = textget (next_iv->plist, *p->name);
3116 if (!EQ (values_here[p->idx], new_value))
3117 break;
3118 }
3119
3120 if (p->handler)
3121 break;
3122 }
3123
3124 if (!NULL_INTERVAL_P (next_iv))
3125 {
3126 if (INTEGERP (limit)
3127 && next_iv->position >= XFASTINT (limit))
3128 /* No text property change up to limit. */
3129 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
3130 else
3131 /* Text properties change in next_iv. */
3132 it->stop_charpos = min (it->stop_charpos, next_iv->position);
3133 }
3134 }
3135
3136 xassert (STRINGP (it->string)
3137 || (it->stop_charpos >= BEGV
3138 && it->stop_charpos >= IT_CHARPOS (*it)));
3139 }
3140
3141
3142 /* Return the position of the next overlay change after POS in
3143 current_buffer. Value is point-max if no overlay change
3144 follows. This is like `next-overlay-change' but doesn't use
3145 xmalloc. */
3146
3147 static int
3148 next_overlay_change (pos)
3149 int pos;
3150 {
3151 int noverlays;
3152 int endpos;
3153 Lisp_Object *overlays;
3154 int i;
3155
3156 /* Get all overlays at the given position. */
3157 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, 1);
3158
3159 /* If any of these overlays ends before endpos,
3160 use its ending point instead. */
3161 for (i = 0; i < noverlays; ++i)
3162 {
3163 Lisp_Object oend;
3164 int oendpos;
3165
3166 oend = OVERLAY_END (overlays[i]);
3167 oendpos = OVERLAY_POSITION (oend);
3168 endpos = min (endpos, oendpos);
3169 }
3170
3171 return endpos;
3172 }
3173
3174
3175 \f
3176 /***********************************************************************
3177 Fontification
3178 ***********************************************************************/
3179
3180 /* Handle changes in the `fontified' property of the current buffer by
3181 calling hook functions from Qfontification_functions to fontify
3182 regions of text. */
3183
3184 static enum prop_handled
3185 handle_fontified_prop (it)
3186 struct it *it;
3187 {
3188 Lisp_Object prop, pos;
3189 enum prop_handled handled = HANDLED_NORMALLY;
3190
3191 if (!NILP (Vmemory_full))
3192 return handled;
3193
3194 /* Get the value of the `fontified' property at IT's current buffer
3195 position. (The `fontified' property doesn't have a special
3196 meaning in strings.) If the value is nil, call functions from
3197 Qfontification_functions. */
3198 if (!STRINGP (it->string)
3199 && it->s == NULL
3200 && !NILP (Vfontification_functions)
3201 && !NILP (Vrun_hooks)
3202 && (pos = make_number (IT_CHARPOS (*it)),
3203 prop = Fget_char_property (pos, Qfontified, Qnil),
3204 NILP (prop)))
3205 {
3206 int count = SPECPDL_INDEX ();
3207 Lisp_Object val;
3208
3209 val = Vfontification_functions;
3210 specbind (Qfontification_functions, Qnil);
3211
3212 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
3213 safe_call1 (val, pos);
3214 else
3215 {
3216 Lisp_Object globals, fn;
3217 struct gcpro gcpro1, gcpro2;
3218
3219 globals = Qnil;
3220 GCPRO2 (val, globals);
3221
3222 for (; CONSP (val); val = XCDR (val))
3223 {
3224 fn = XCAR (val);
3225
3226 if (EQ (fn, Qt))
3227 {
3228 /* A value of t indicates this hook has a local
3229 binding; it means to run the global binding too.
3230 In a global value, t should not occur. If it
3231 does, we must ignore it to avoid an endless
3232 loop. */
3233 for (globals = Fdefault_value (Qfontification_functions);
3234 CONSP (globals);
3235 globals = XCDR (globals))
3236 {
3237 fn = XCAR (globals);
3238 if (!EQ (fn, Qt))
3239 safe_call1 (fn, pos);
3240 }
3241 }
3242 else
3243 safe_call1 (fn, pos);
3244 }
3245
3246 UNGCPRO;
3247 }
3248
3249 unbind_to (count, Qnil);
3250
3251 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
3252 something. This avoids an endless loop if they failed to
3253 fontify the text for which reason ever. */
3254 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
3255 handled = HANDLED_RECOMPUTE_PROPS;
3256 }
3257
3258 return handled;
3259 }
3260
3261
3262 \f
3263 /***********************************************************************
3264 Faces
3265 ***********************************************************************/
3266
3267 /* Set up iterator IT from face properties at its current position.
3268 Called from handle_stop. */
3269
3270 static enum prop_handled
3271 handle_face_prop (it)
3272 struct it *it;
3273 {
3274 int new_face_id, next_stop;
3275
3276 if (!STRINGP (it->string))
3277 {
3278 new_face_id
3279 = face_at_buffer_position (it->w,
3280 IT_CHARPOS (*it),
3281 it->region_beg_charpos,
3282 it->region_end_charpos,
3283 &next_stop,
3284 (IT_CHARPOS (*it)
3285 + TEXT_PROP_DISTANCE_LIMIT),
3286 0);
3287
3288 /* Is this a start of a run of characters with box face?
3289 Caveat: this can be called for a freshly initialized
3290 iterator; face_id is -1 in this case. We know that the new
3291 face will not change until limit, i.e. if the new face has a
3292 box, all characters up to limit will have one. But, as
3293 usual, we don't know whether limit is really the end. */
3294 if (new_face_id != it->face_id)
3295 {
3296 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3297
3298 /* If new face has a box but old face has not, this is
3299 the start of a run of characters with box, i.e. it has
3300 a shadow on the left side. The value of face_id of the
3301 iterator will be -1 if this is the initial call that gets
3302 the face. In this case, we have to look in front of IT's
3303 position and see whether there is a face != new_face_id. */
3304 it->start_of_box_run_p
3305 = (new_face->box != FACE_NO_BOX
3306 && (it->face_id >= 0
3307 || IT_CHARPOS (*it) == BEG
3308 || new_face_id != face_before_it_pos (it)));
3309 it->face_box_p = new_face->box != FACE_NO_BOX;
3310 }
3311 }
3312 else
3313 {
3314 int base_face_id, bufpos;
3315
3316 if (it->current.overlay_string_index >= 0)
3317 bufpos = IT_CHARPOS (*it);
3318 else
3319 bufpos = 0;
3320
3321 /* For strings from a buffer, i.e. overlay strings or strings
3322 from a `display' property, use the face at IT's current
3323 buffer position as the base face to merge with, so that
3324 overlay strings appear in the same face as surrounding
3325 text, unless they specify their own faces. */
3326 base_face_id = underlying_face_id (it);
3327
3328 new_face_id = face_at_string_position (it->w,
3329 it->string,
3330 IT_STRING_CHARPOS (*it),
3331 bufpos,
3332 it->region_beg_charpos,
3333 it->region_end_charpos,
3334 &next_stop,
3335 base_face_id, 0);
3336
3337 #if 0 /* This shouldn't be neccessary. Let's check it. */
3338 /* If IT is used to display a mode line we would really like to
3339 use the mode line face instead of the frame's default face. */
3340 if (it->glyph_row == MATRIX_MODE_LINE_ROW (it->w->desired_matrix)
3341 && new_face_id == DEFAULT_FACE_ID)
3342 new_face_id = CURRENT_MODE_LINE_FACE_ID (it->w);
3343 #endif
3344
3345 /* Is this a start of a run of characters with box? Caveat:
3346 this can be called for a freshly allocated iterator; face_id
3347 is -1 is this case. We know that the new face will not
3348 change until the next check pos, i.e. if the new face has a
3349 box, all characters up to that position will have a
3350 box. But, as usual, we don't know whether that position
3351 is really the end. */
3352 if (new_face_id != it->face_id)
3353 {
3354 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3355 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3356
3357 /* If new face has a box but old face hasn't, this is the
3358 start of a run of characters with box, i.e. it has a
3359 shadow on the left side. */
3360 it->start_of_box_run_p
3361 = new_face->box && (old_face == NULL || !old_face->box);
3362 it->face_box_p = new_face->box != FACE_NO_BOX;
3363 }
3364 }
3365
3366 it->face_id = new_face_id;
3367 return HANDLED_NORMALLY;
3368 }
3369
3370
3371 /* Return the ID of the face ``underlying'' IT's current position,
3372 which is in a string. If the iterator is associated with a
3373 buffer, return the face at IT's current buffer position.
3374 Otherwise, use the iterator's base_face_id. */
3375
3376 static int
3377 underlying_face_id (it)
3378 struct it *it;
3379 {
3380 int face_id = it->base_face_id, i;
3381
3382 xassert (STRINGP (it->string));
3383
3384 for (i = it->sp - 1; i >= 0; --i)
3385 if (NILP (it->stack[i].string))
3386 face_id = it->stack[i].face_id;
3387
3388 return face_id;
3389 }
3390
3391
3392 /* Compute the face one character before or after the current position
3393 of IT. BEFORE_P non-zero means get the face in front of IT's
3394 position. Value is the id of the face. */
3395
3396 static int
3397 face_before_or_after_it_pos (it, before_p)
3398 struct it *it;
3399 int before_p;
3400 {
3401 int face_id, limit;
3402 int next_check_charpos;
3403 struct text_pos pos;
3404
3405 xassert (it->s == NULL);
3406
3407 if (STRINGP (it->string))
3408 {
3409 int bufpos, base_face_id;
3410
3411 /* No face change past the end of the string (for the case
3412 we are padding with spaces). No face change before the
3413 string start. */
3414 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
3415 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
3416 return it->face_id;
3417
3418 /* Set pos to the position before or after IT's current position. */
3419 if (before_p)
3420 pos = string_pos (IT_STRING_CHARPOS (*it) - 1, it->string);
3421 else
3422 /* For composition, we must check the character after the
3423 composition. */
3424 pos = (it->what == IT_COMPOSITION
3425 ? string_pos (IT_STRING_CHARPOS (*it) + it->cmp_len, it->string)
3426 : string_pos (IT_STRING_CHARPOS (*it) + 1, it->string));
3427
3428 if (it->current.overlay_string_index >= 0)
3429 bufpos = IT_CHARPOS (*it);
3430 else
3431 bufpos = 0;
3432
3433 base_face_id = underlying_face_id (it);
3434
3435 /* Get the face for ASCII, or unibyte. */
3436 face_id = face_at_string_position (it->w,
3437 it->string,
3438 CHARPOS (pos),
3439 bufpos,
3440 it->region_beg_charpos,
3441 it->region_end_charpos,
3442 &next_check_charpos,
3443 base_face_id, 0);
3444
3445 /* Correct the face for charsets different from ASCII. Do it
3446 for the multibyte case only. The face returned above is
3447 suitable for unibyte text if IT->string is unibyte. */
3448 if (STRING_MULTIBYTE (it->string))
3449 {
3450 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos);
3451 int rest = SBYTES (it->string) - BYTEPOS (pos);
3452 int c, len;
3453 struct face *face = FACE_FROM_ID (it->f, face_id);
3454
3455 c = string_char_and_length (p, rest, &len);
3456 face_id = FACE_FOR_CHAR (it->f, face, c);
3457 }
3458 }
3459 else
3460 {
3461 if ((IT_CHARPOS (*it) >= ZV && !before_p)
3462 || (IT_CHARPOS (*it) <= BEGV && before_p))
3463 return it->face_id;
3464
3465 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
3466 pos = it->current.pos;
3467
3468 if (before_p)
3469 DEC_TEXT_POS (pos, it->multibyte_p);
3470 else
3471 {
3472 if (it->what == IT_COMPOSITION)
3473 /* For composition, we must check the position after the
3474 composition. */
3475 pos.charpos += it->cmp_len, pos.bytepos += it->len;
3476 else
3477 INC_TEXT_POS (pos, it->multibyte_p);
3478 }
3479
3480 /* Determine face for CHARSET_ASCII, or unibyte. */
3481 face_id = face_at_buffer_position (it->w,
3482 CHARPOS (pos),
3483 it->region_beg_charpos,
3484 it->region_end_charpos,
3485 &next_check_charpos,
3486 limit, 0);
3487
3488 /* Correct the face for charsets different from ASCII. Do it
3489 for the multibyte case only. The face returned above is
3490 suitable for unibyte text if current_buffer is unibyte. */
3491 if (it->multibyte_p)
3492 {
3493 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
3494 struct face *face = FACE_FROM_ID (it->f, face_id);
3495 face_id = FACE_FOR_CHAR (it->f, face, c);
3496 }
3497 }
3498
3499 return face_id;
3500 }
3501
3502
3503 \f
3504 /***********************************************************************
3505 Invisible text
3506 ***********************************************************************/
3507
3508 /* Set up iterator IT from invisible properties at its current
3509 position. Called from handle_stop. */
3510
3511 static enum prop_handled
3512 handle_invisible_prop (it)
3513 struct it *it;
3514 {
3515 enum prop_handled handled = HANDLED_NORMALLY;
3516
3517 if (STRINGP (it->string))
3518 {
3519 extern Lisp_Object Qinvisible;
3520 Lisp_Object prop, end_charpos, limit, charpos;
3521
3522 /* Get the value of the invisible text property at the
3523 current position. Value will be nil if there is no such
3524 property. */
3525 charpos = make_number (IT_STRING_CHARPOS (*it));
3526 prop = Fget_text_property (charpos, Qinvisible, it->string);
3527
3528 if (!NILP (prop)
3529 && IT_STRING_CHARPOS (*it) < it->end_charpos)
3530 {
3531 handled = HANDLED_RECOMPUTE_PROPS;
3532
3533 /* Get the position at which the next change of the
3534 invisible text property can be found in IT->string.
3535 Value will be nil if the property value is the same for
3536 all the rest of IT->string. */
3537 XSETINT (limit, SCHARS (it->string));
3538 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
3539 it->string, limit);
3540
3541 /* Text at current position is invisible. The next
3542 change in the property is at position end_charpos.
3543 Move IT's current position to that position. */
3544 if (INTEGERP (end_charpos)
3545 && XFASTINT (end_charpos) < XFASTINT (limit))
3546 {
3547 struct text_pos old;
3548 old = it->current.string_pos;
3549 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
3550 compute_string_pos (&it->current.string_pos, old, it->string);
3551 }
3552 else
3553 {
3554 /* The rest of the string is invisible. If this is an
3555 overlay string, proceed with the next overlay string
3556 or whatever comes and return a character from there. */
3557 if (it->current.overlay_string_index >= 0)
3558 {
3559 next_overlay_string (it);
3560 /* Don't check for overlay strings when we just
3561 finished processing them. */
3562 handled = HANDLED_OVERLAY_STRING_CONSUMED;
3563 }
3564 else
3565 {
3566 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
3567 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
3568 }
3569 }
3570 }
3571 }
3572 else
3573 {
3574 int invis_p, newpos, next_stop, start_charpos;
3575 Lisp_Object pos, prop, overlay;
3576
3577 /* First of all, is there invisible text at this position? */
3578 start_charpos = IT_CHARPOS (*it);
3579 pos = make_number (IT_CHARPOS (*it));
3580 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
3581 &overlay);
3582 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3583
3584 /* If we are on invisible text, skip over it. */
3585 if (invis_p && IT_CHARPOS (*it) < it->end_charpos)
3586 {
3587 /* Record whether we have to display an ellipsis for the
3588 invisible text. */
3589 int display_ellipsis_p = invis_p == 2;
3590
3591 handled = HANDLED_RECOMPUTE_PROPS;
3592
3593 /* Loop skipping over invisible text. The loop is left at
3594 ZV or with IT on the first char being visible again. */
3595 do
3596 {
3597 /* Try to skip some invisible text. Return value is the
3598 position reached which can be equal to IT's position
3599 if there is nothing invisible here. This skips both
3600 over invisible text properties and overlays with
3601 invisible property. */
3602 newpos = skip_invisible (IT_CHARPOS (*it),
3603 &next_stop, ZV, it->window);
3604
3605 /* If we skipped nothing at all we weren't at invisible
3606 text in the first place. If everything to the end of
3607 the buffer was skipped, end the loop. */
3608 if (newpos == IT_CHARPOS (*it) || newpos >= ZV)
3609 invis_p = 0;
3610 else
3611 {
3612 /* We skipped some characters but not necessarily
3613 all there are. Check if we ended up on visible
3614 text. Fget_char_property returns the property of
3615 the char before the given position, i.e. if we
3616 get invis_p = 0, this means that the char at
3617 newpos is visible. */
3618 pos = make_number (newpos);
3619 prop = Fget_char_property (pos, Qinvisible, it->window);
3620 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3621 }
3622
3623 /* If we ended up on invisible text, proceed to
3624 skip starting with next_stop. */
3625 if (invis_p)
3626 IT_CHARPOS (*it) = next_stop;
3627
3628 /* If there are adjacent invisible texts, don't lose the
3629 second one's ellipsis. */
3630 if (invis_p == 2)
3631 display_ellipsis_p = 1;
3632 }
3633 while (invis_p);
3634
3635 /* The position newpos is now either ZV or on visible text. */
3636 IT_CHARPOS (*it) = newpos;
3637 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
3638
3639 /* If there are before-strings at the start of invisible
3640 text, and the text is invisible because of a text
3641 property, arrange to show before-strings because 20.x did
3642 it that way. (If the text is invisible because of an
3643 overlay property instead of a text property, this is
3644 already handled in the overlay code.) */
3645 if (NILP (overlay)
3646 && get_overlay_strings (it, start_charpos))
3647 {
3648 handled = HANDLED_RECOMPUTE_PROPS;
3649 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
3650 }
3651 else if (display_ellipsis_p)
3652 {
3653 /* Make sure that the glyphs of the ellipsis will get
3654 correct `charpos' values. If we would not update
3655 it->position here, the glyphs would belong to the
3656 last visible character _before_ the invisible
3657 text, which confuses `set_cursor_from_row'.
3658
3659 We use the last invisible position instead of the
3660 first because this way the cursor is always drawn on
3661 the first "." of the ellipsis, whenever PT is inside
3662 the invisible text. Otherwise the cursor would be
3663 placed _after_ the ellipsis when the point is after the
3664 first invisible character. */
3665 if (!STRINGP (it->object))
3666 {
3667 it->position.charpos = IT_CHARPOS (*it) - 1;
3668 it->position.bytepos = CHAR_TO_BYTE (it->position.charpos);
3669 }
3670 setup_for_ellipsis (it, 0);
3671 }
3672 }
3673 }
3674
3675 return handled;
3676 }
3677
3678
3679 /* Make iterator IT return `...' next.
3680 Replaces LEN characters from buffer. */
3681
3682 static void
3683 setup_for_ellipsis (it, len)
3684 struct it *it;
3685 int len;
3686 {
3687 /* Use the display table definition for `...'. Invalid glyphs
3688 will be handled by the method returning elements from dpvec. */
3689 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
3690 {
3691 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
3692 it->dpvec = v->contents;
3693 it->dpend = v->contents + v->size;
3694 }
3695 else
3696 {
3697 /* Default `...'. */
3698 it->dpvec = default_invis_vector;
3699 it->dpend = default_invis_vector + 3;
3700 }
3701
3702 it->dpvec_char_len = len;
3703 it->current.dpvec_index = 0;
3704 it->dpvec_face_id = -1;
3705
3706 /* Remember the current face id in case glyphs specify faces.
3707 IT's face is restored in set_iterator_to_next.
3708 saved_face_id was set to preceding char's face in handle_stop. */
3709 if (it->saved_face_id < 0 || it->saved_face_id != it->face_id)
3710 it->saved_face_id = it->face_id = DEFAULT_FACE_ID;
3711
3712 it->method = GET_FROM_DISPLAY_VECTOR;
3713 it->ellipsis_p = 1;
3714 }
3715
3716
3717 \f
3718 /***********************************************************************
3719 'display' property
3720 ***********************************************************************/
3721
3722 /* Set up iterator IT from `display' property at its current position.
3723 Called from handle_stop.
3724 We return HANDLED_RETURN if some part of the display property
3725 overrides the display of the buffer text itself.
3726 Otherwise we return HANDLED_NORMALLY. */
3727
3728 static enum prop_handled
3729 handle_display_prop (it)
3730 struct it *it;
3731 {
3732 Lisp_Object prop, object;
3733 struct text_pos *position;
3734 /* Nonzero if some property replaces the display of the text itself. */
3735 int display_replaced_p = 0;
3736
3737 if (STRINGP (it->string))
3738 {
3739 object = it->string;
3740 position = &it->current.string_pos;
3741 }
3742 else
3743 {
3744 XSETWINDOW (object, it->w);
3745 position = &it->current.pos;
3746 }
3747
3748 /* Reset those iterator values set from display property values. */
3749 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
3750 it->space_width = Qnil;
3751 it->font_height = Qnil;
3752 it->voffset = 0;
3753
3754 /* We don't support recursive `display' properties, i.e. string
3755 values that have a string `display' property, that have a string
3756 `display' property etc. */
3757 if (!it->string_from_display_prop_p)
3758 it->area = TEXT_AREA;
3759
3760 prop = Fget_char_property (make_number (position->charpos),
3761 Qdisplay, object);
3762 if (NILP (prop))
3763 return HANDLED_NORMALLY;
3764
3765 if (!STRINGP (it->string))
3766 object = it->w->buffer;
3767
3768 if (CONSP (prop)
3769 /* Simple properties. */
3770 && !EQ (XCAR (prop), Qimage)
3771 && !EQ (XCAR (prop), Qspace)
3772 && !EQ (XCAR (prop), Qwhen)
3773 && !EQ (XCAR (prop), Qslice)
3774 && !EQ (XCAR (prop), Qspace_width)
3775 && !EQ (XCAR (prop), Qheight)
3776 && !EQ (XCAR (prop), Qraise)
3777 /* Marginal area specifications. */
3778 && !(CONSP (XCAR (prop)) && EQ (XCAR (XCAR (prop)), Qmargin))
3779 && !EQ (XCAR (prop), Qleft_fringe)
3780 && !EQ (XCAR (prop), Qright_fringe)
3781 && !NILP (XCAR (prop)))
3782 {
3783 for (; CONSP (prop); prop = XCDR (prop))
3784 {
3785 if (handle_single_display_spec (it, XCAR (prop), object,
3786 position, display_replaced_p))
3787 display_replaced_p = 1;
3788 }
3789 }
3790 else if (VECTORP (prop))
3791 {
3792 int i;
3793 for (i = 0; i < ASIZE (prop); ++i)
3794 if (handle_single_display_spec (it, AREF (prop, i), object,
3795 position, display_replaced_p))
3796 display_replaced_p = 1;
3797 }
3798 else
3799 {
3800 int ret = handle_single_display_spec (it, prop, object, position, 0);
3801 if (ret < 0) /* Replaced by "", i.e. nothing. */
3802 return HANDLED_RECOMPUTE_PROPS;
3803 if (ret)
3804 display_replaced_p = 1;
3805 }
3806
3807 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
3808 }
3809
3810
3811 /* Value is the position of the end of the `display' property starting
3812 at START_POS in OBJECT. */
3813
3814 static struct text_pos
3815 display_prop_end (it, object, start_pos)
3816 struct it *it;
3817 Lisp_Object object;
3818 struct text_pos start_pos;
3819 {
3820 Lisp_Object end;
3821 struct text_pos end_pos;
3822
3823 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
3824 Qdisplay, object, Qnil);
3825 CHARPOS (end_pos) = XFASTINT (end);
3826 if (STRINGP (object))
3827 compute_string_pos (&end_pos, start_pos, it->string);
3828 else
3829 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
3830
3831 return end_pos;
3832 }
3833
3834
3835 /* Set up IT from a single `display' specification PROP. OBJECT
3836 is the object in which the `display' property was found. *POSITION
3837 is the position at which it was found. DISPLAY_REPLACED_P non-zero
3838 means that we previously saw a display specification which already
3839 replaced text display with something else, for example an image;
3840 we ignore such properties after the first one has been processed.
3841
3842 If PROP is a `space' or `image' specification, and in some other
3843 cases too, set *POSITION to the position where the `display'
3844 property ends.
3845
3846 Value is non-zero if something was found which replaces the display
3847 of buffer or string text. Specifically, the value is -1 if that
3848 "something" is "nothing". */
3849
3850 static int
3851 handle_single_display_spec (it, spec, object, position,
3852 display_replaced_before_p)
3853 struct it *it;
3854 Lisp_Object spec;
3855 Lisp_Object object;
3856 struct text_pos *position;
3857 int display_replaced_before_p;
3858 {
3859 Lisp_Object form;
3860 Lisp_Object location, value;
3861 struct text_pos start_pos;
3862 int valid_p;
3863
3864 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
3865 If the result is non-nil, use VALUE instead of SPEC. */
3866 form = Qt;
3867 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
3868 {
3869 spec = XCDR (spec);
3870 if (!CONSP (spec))
3871 return 0;
3872 form = XCAR (spec);
3873 spec = XCDR (spec);
3874 }
3875
3876 if (!NILP (form) && !EQ (form, Qt))
3877 {
3878 int count = SPECPDL_INDEX ();
3879 struct gcpro gcpro1;
3880
3881 /* Bind `object' to the object having the `display' property, a
3882 buffer or string. Bind `position' to the position in the
3883 object where the property was found, and `buffer-position'
3884 to the current position in the buffer. */
3885 specbind (Qobject, object);
3886 specbind (Qposition, make_number (CHARPOS (*position)));
3887 specbind (Qbuffer_position,
3888 make_number (STRINGP (object)
3889 ? IT_CHARPOS (*it) : CHARPOS (*position)));
3890 GCPRO1 (form);
3891 form = safe_eval (form);
3892 UNGCPRO;
3893 unbind_to (count, Qnil);
3894 }
3895
3896 if (NILP (form))
3897 return 0;
3898
3899 /* Handle `(height HEIGHT)' specifications. */
3900 if (CONSP (spec)
3901 && EQ (XCAR (spec), Qheight)
3902 && CONSP (XCDR (spec)))
3903 {
3904 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3905 return 0;
3906
3907 it->font_height = XCAR (XCDR (spec));
3908 if (!NILP (it->font_height))
3909 {
3910 struct face *face = FACE_FROM_ID (it->f, it->face_id);
3911 int new_height = -1;
3912
3913 if (CONSP (it->font_height)
3914 && (EQ (XCAR (it->font_height), Qplus)
3915 || EQ (XCAR (it->font_height), Qminus))
3916 && CONSP (XCDR (it->font_height))
3917 && INTEGERP (XCAR (XCDR (it->font_height))))
3918 {
3919 /* `(+ N)' or `(- N)' where N is an integer. */
3920 int steps = XINT (XCAR (XCDR (it->font_height)));
3921 if (EQ (XCAR (it->font_height), Qplus))
3922 steps = - steps;
3923 it->face_id = smaller_face (it->f, it->face_id, steps);
3924 }
3925 else if (FUNCTIONP (it->font_height))
3926 {
3927 /* Call function with current height as argument.
3928 Value is the new height. */
3929 Lisp_Object height;
3930 height = safe_call1 (it->font_height,
3931 face->lface[LFACE_HEIGHT_INDEX]);
3932 if (NUMBERP (height))
3933 new_height = XFLOATINT (height);
3934 }
3935 else if (NUMBERP (it->font_height))
3936 {
3937 /* Value is a multiple of the canonical char height. */
3938 struct face *face;
3939
3940 face = FACE_FROM_ID (it->f, DEFAULT_FACE_ID);
3941 new_height = (XFLOATINT (it->font_height)
3942 * XINT (face->lface[LFACE_HEIGHT_INDEX]));
3943 }
3944 else
3945 {
3946 /* Evaluate IT->font_height with `height' bound to the
3947 current specified height to get the new height. */
3948 int count = SPECPDL_INDEX ();
3949
3950 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
3951 value = safe_eval (it->font_height);
3952 unbind_to (count, Qnil);
3953
3954 if (NUMBERP (value))
3955 new_height = XFLOATINT (value);
3956 }
3957
3958 if (new_height > 0)
3959 it->face_id = face_with_height (it->f, it->face_id, new_height);
3960 }
3961
3962 return 0;
3963 }
3964
3965 /* Handle `(space_width WIDTH)'. */
3966 if (CONSP (spec)
3967 && EQ (XCAR (spec), Qspace_width)
3968 && CONSP (XCDR (spec)))
3969 {
3970 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3971 return 0;
3972
3973 value = XCAR (XCDR (spec));
3974 if (NUMBERP (value) && XFLOATINT (value) > 0)
3975 it->space_width = value;
3976
3977 return 0;
3978 }
3979
3980 /* Handle `(slice X Y WIDTH HEIGHT)'. */
3981 if (CONSP (spec)
3982 && EQ (XCAR (spec), Qslice))
3983 {
3984 Lisp_Object tem;
3985
3986 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3987 return 0;
3988
3989 if (tem = XCDR (spec), CONSP (tem))
3990 {
3991 it->slice.x = XCAR (tem);
3992 if (tem = XCDR (tem), CONSP (tem))
3993 {
3994 it->slice.y = XCAR (tem);
3995 if (tem = XCDR (tem), CONSP (tem))
3996 {
3997 it->slice.width = XCAR (tem);
3998 if (tem = XCDR (tem), CONSP (tem))
3999 it->slice.height = XCAR (tem);
4000 }
4001 }
4002 }
4003
4004 return 0;
4005 }
4006
4007 /* Handle `(raise FACTOR)'. */
4008 if (CONSP (spec)
4009 && EQ (XCAR (spec), Qraise)
4010 && CONSP (XCDR (spec)))
4011 {
4012 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
4013 return 0;
4014
4015 #ifdef HAVE_WINDOW_SYSTEM
4016 value = XCAR (XCDR (spec));
4017 if (NUMBERP (value))
4018 {
4019 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4020 it->voffset = - (XFLOATINT (value)
4021 * (FONT_HEIGHT (face->font)));
4022 }
4023 #endif /* HAVE_WINDOW_SYSTEM */
4024
4025 return 0;
4026 }
4027
4028 /* Don't handle the other kinds of display specifications
4029 inside a string that we got from a `display' property. */
4030 if (it->string_from_display_prop_p)
4031 return 0;
4032
4033 /* Characters having this form of property are not displayed, so
4034 we have to find the end of the property. */
4035 start_pos = *position;
4036 *position = display_prop_end (it, object, start_pos);
4037 value = Qnil;
4038
4039 /* Stop the scan at that end position--we assume that all
4040 text properties change there. */
4041 it->stop_charpos = position->charpos;
4042
4043 /* Handle `(left-fringe BITMAP [FACE])'
4044 and `(right-fringe BITMAP [FACE])'. */
4045 if (CONSP (spec)
4046 && (EQ (XCAR (spec), Qleft_fringe)
4047 || EQ (XCAR (spec), Qright_fringe))
4048 && CONSP (XCDR (spec)))
4049 {
4050 int face_id = DEFAULT_FACE_ID;
4051 int fringe_bitmap;
4052
4053 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
4054 /* If we return here, POSITION has been advanced
4055 across the text with this property. */
4056 return 0;
4057
4058 #ifdef HAVE_WINDOW_SYSTEM
4059 value = XCAR (XCDR (spec));
4060 if (!SYMBOLP (value)
4061 || !(fringe_bitmap = lookup_fringe_bitmap (value)))
4062 /* If we return here, POSITION has been advanced
4063 across the text with this property. */
4064 return 0;
4065
4066 if (CONSP (XCDR (XCDR (spec))))
4067 {
4068 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
4069 int face_id2 = lookup_derived_face (it->f, face_name,
4070 'A', FRINGE_FACE_ID, 0);
4071 if (face_id2 >= 0)
4072 face_id = face_id2;
4073 }
4074
4075 /* Save current settings of IT so that we can restore them
4076 when we are finished with the glyph property value. */
4077
4078 push_it (it);
4079
4080 it->area = TEXT_AREA;
4081 it->what = IT_IMAGE;
4082 it->image_id = -1; /* no image */
4083 it->position = start_pos;
4084 it->object = NILP (object) ? it->w->buffer : object;
4085 it->method = GET_FROM_IMAGE;
4086 it->face_id = face_id;
4087
4088 /* Say that we haven't consumed the characters with
4089 `display' property yet. The call to pop_it in
4090 set_iterator_to_next will clean this up. */
4091 *position = start_pos;
4092
4093 if (EQ (XCAR (spec), Qleft_fringe))
4094 {
4095 it->left_user_fringe_bitmap = fringe_bitmap;
4096 it->left_user_fringe_face_id = face_id;
4097 }
4098 else
4099 {
4100 it->right_user_fringe_bitmap = fringe_bitmap;
4101 it->right_user_fringe_face_id = face_id;
4102 }
4103 #endif /* HAVE_WINDOW_SYSTEM */
4104 return 1;
4105 }
4106
4107 /* Prepare to handle `((margin left-margin) ...)',
4108 `((margin right-margin) ...)' and `((margin nil) ...)'
4109 prefixes for display specifications. */
4110 location = Qunbound;
4111 if (CONSP (spec) && CONSP (XCAR (spec)))
4112 {
4113 Lisp_Object tem;
4114
4115 value = XCDR (spec);
4116 if (CONSP (value))
4117 value = XCAR (value);
4118
4119 tem = XCAR (spec);
4120 if (EQ (XCAR (tem), Qmargin)
4121 && (tem = XCDR (tem),
4122 tem = CONSP (tem) ? XCAR (tem) : Qnil,
4123 (NILP (tem)
4124 || EQ (tem, Qleft_margin)
4125 || EQ (tem, Qright_margin))))
4126 location = tem;
4127 }
4128
4129 if (EQ (location, Qunbound))
4130 {
4131 location = Qnil;
4132 value = spec;
4133 }
4134
4135 /* After this point, VALUE is the property after any
4136 margin prefix has been stripped. It must be a string,
4137 an image specification, or `(space ...)'.
4138
4139 LOCATION specifies where to display: `left-margin',
4140 `right-margin' or nil. */
4141
4142 valid_p = (STRINGP (value)
4143 #ifdef HAVE_WINDOW_SYSTEM
4144 || (!FRAME_TERMCAP_P (it->f) && valid_image_p (value))
4145 #endif /* not HAVE_WINDOW_SYSTEM */
4146 || (CONSP (value) && EQ (XCAR (value), Qspace)));
4147
4148 if (valid_p && !display_replaced_before_p)
4149 {
4150 /* Save current settings of IT so that we can restore them
4151 when we are finished with the glyph property value. */
4152 push_it (it);
4153
4154 if (NILP (location))
4155 it->area = TEXT_AREA;
4156 else if (EQ (location, Qleft_margin))
4157 it->area = LEFT_MARGIN_AREA;
4158 else
4159 it->area = RIGHT_MARGIN_AREA;
4160
4161 if (STRINGP (value))
4162 {
4163 if (SCHARS (value) == 0)
4164 {
4165 pop_it (it);
4166 return -1; /* Replaced by "", i.e. nothing. */
4167 }
4168 it->string = value;
4169 it->multibyte_p = STRING_MULTIBYTE (it->string);
4170 it->current.overlay_string_index = -1;
4171 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
4172 it->end_charpos = it->string_nchars = SCHARS (it->string);
4173 it->method = GET_FROM_STRING;
4174 it->stop_charpos = 0;
4175 it->string_from_display_prop_p = 1;
4176 /* Say that we haven't consumed the characters with
4177 `display' property yet. The call to pop_it in
4178 set_iterator_to_next will clean this up. */
4179 *position = start_pos;
4180 }
4181 else if (CONSP (value) && EQ (XCAR (value), Qspace))
4182 {
4183 it->method = GET_FROM_STRETCH;
4184 it->object = value;
4185 *position = it->position = start_pos;
4186 }
4187 #ifdef HAVE_WINDOW_SYSTEM
4188 else
4189 {
4190 it->what = IT_IMAGE;
4191 it->image_id = lookup_image (it->f, value);
4192 it->position = start_pos;
4193 it->object = NILP (object) ? it->w->buffer : object;
4194 it->method = GET_FROM_IMAGE;
4195
4196 /* Say that we haven't consumed the characters with
4197 `display' property yet. The call to pop_it in
4198 set_iterator_to_next will clean this up. */
4199 *position = start_pos;
4200 }
4201 #endif /* HAVE_WINDOW_SYSTEM */
4202
4203 return 1;
4204 }
4205
4206 /* Invalid property or property not supported. Restore
4207 POSITION to what it was before. */
4208 *position = start_pos;
4209 return 0;
4210 }
4211
4212
4213 /* Check if SPEC is a display specification value whose text should be
4214 treated as intangible. */
4215
4216 static int
4217 single_display_spec_intangible_p (prop)
4218 Lisp_Object prop;
4219 {
4220 /* Skip over `when FORM'. */
4221 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
4222 {
4223 prop = XCDR (prop);
4224 if (!CONSP (prop))
4225 return 0;
4226 prop = XCDR (prop);
4227 }
4228
4229 if (STRINGP (prop))
4230 return 1;
4231
4232 if (!CONSP (prop))
4233 return 0;
4234
4235 /* Skip over `margin LOCATION'. If LOCATION is in the margins,
4236 we don't need to treat text as intangible. */
4237 if (EQ (XCAR (prop), Qmargin))
4238 {
4239 prop = XCDR (prop);
4240 if (!CONSP (prop))
4241 return 0;
4242
4243 prop = XCDR (prop);
4244 if (!CONSP (prop)
4245 || EQ (XCAR (prop), Qleft_margin)
4246 || EQ (XCAR (prop), Qright_margin))
4247 return 0;
4248 }
4249
4250 return (CONSP (prop)
4251 && (EQ (XCAR (prop), Qimage)
4252 || EQ (XCAR (prop), Qspace)));
4253 }
4254
4255
4256 /* Check if PROP is a display property value whose text should be
4257 treated as intangible. */
4258
4259 int
4260 display_prop_intangible_p (prop)
4261 Lisp_Object prop;
4262 {
4263 if (CONSP (prop)
4264 && CONSP (XCAR (prop))
4265 && !EQ (Qmargin, XCAR (XCAR (prop))))
4266 {
4267 /* A list of sub-properties. */
4268 while (CONSP (prop))
4269 {
4270 if (single_display_spec_intangible_p (XCAR (prop)))
4271 return 1;
4272 prop = XCDR (prop);
4273 }
4274 }
4275 else if (VECTORP (prop))
4276 {
4277 /* A vector of sub-properties. */
4278 int i;
4279 for (i = 0; i < ASIZE (prop); ++i)
4280 if (single_display_spec_intangible_p (AREF (prop, i)))
4281 return 1;
4282 }
4283 else
4284 return single_display_spec_intangible_p (prop);
4285
4286 return 0;
4287 }
4288
4289
4290 /* Return 1 if PROP is a display sub-property value containing STRING. */
4291
4292 static int
4293 single_display_spec_string_p (prop, string)
4294 Lisp_Object prop, string;
4295 {
4296 if (EQ (string, prop))
4297 return 1;
4298
4299 /* Skip over `when FORM'. */
4300 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
4301 {
4302 prop = XCDR (prop);
4303 if (!CONSP (prop))
4304 return 0;
4305 prop = XCDR (prop);
4306 }
4307
4308 if (CONSP (prop))
4309 /* Skip over `margin LOCATION'. */
4310 if (EQ (XCAR (prop), Qmargin))
4311 {
4312 prop = XCDR (prop);
4313 if (!CONSP (prop))
4314 return 0;
4315
4316 prop = XCDR (prop);
4317 if (!CONSP (prop))
4318 return 0;
4319 }
4320
4321 return CONSP (prop) && EQ (XCAR (prop), string);
4322 }
4323
4324
4325 /* Return 1 if STRING appears in the `display' property PROP. */
4326
4327 static int
4328 display_prop_string_p (prop, string)
4329 Lisp_Object prop, string;
4330 {
4331 if (CONSP (prop)
4332 && CONSP (XCAR (prop))
4333 && !EQ (Qmargin, XCAR (XCAR (prop))))
4334 {
4335 /* A list of sub-properties. */
4336 while (CONSP (prop))
4337 {
4338 if (single_display_spec_string_p (XCAR (prop), string))
4339 return 1;
4340 prop = XCDR (prop);
4341 }
4342 }
4343 else if (VECTORP (prop))
4344 {
4345 /* A vector of sub-properties. */
4346 int i;
4347 for (i = 0; i < ASIZE (prop); ++i)
4348 if (single_display_spec_string_p (AREF (prop, i), string))
4349 return 1;
4350 }
4351 else
4352 return single_display_spec_string_p (prop, string);
4353
4354 return 0;
4355 }
4356
4357
4358 /* Determine from which buffer position in W's buffer STRING comes
4359 from. AROUND_CHARPOS is an approximate position where it could
4360 be from. Value is the buffer position or 0 if it couldn't be
4361 determined.
4362
4363 W's buffer must be current.
4364
4365 This function is necessary because we don't record buffer positions
4366 in glyphs generated from strings (to keep struct glyph small).
4367 This function may only use code that doesn't eval because it is
4368 called asynchronously from note_mouse_highlight. */
4369
4370 int
4371 string_buffer_position (w, string, around_charpos)
4372 struct window *w;
4373 Lisp_Object string;
4374 int around_charpos;
4375 {
4376 Lisp_Object limit, prop, pos;
4377 const int MAX_DISTANCE = 1000;
4378 int found = 0;
4379
4380 pos = make_number (around_charpos);
4381 limit = make_number (min (XINT (pos) + MAX_DISTANCE, ZV));
4382 while (!found && !EQ (pos, limit))
4383 {
4384 prop = Fget_char_property (pos, Qdisplay, Qnil);
4385 if (!NILP (prop) && display_prop_string_p (prop, string))
4386 found = 1;
4387 else
4388 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil, limit);
4389 }
4390
4391 if (!found)
4392 {
4393 pos = make_number (around_charpos);
4394 limit = make_number (max (XINT (pos) - MAX_DISTANCE, BEGV));
4395 while (!found && !EQ (pos, limit))
4396 {
4397 prop = Fget_char_property (pos, Qdisplay, Qnil);
4398 if (!NILP (prop) && display_prop_string_p (prop, string))
4399 found = 1;
4400 else
4401 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
4402 limit);
4403 }
4404 }
4405
4406 return found ? XINT (pos) : 0;
4407 }
4408
4409
4410 \f
4411 /***********************************************************************
4412 `composition' property
4413 ***********************************************************************/
4414
4415 /* Set up iterator IT from `composition' property at its current
4416 position. Called from handle_stop. */
4417
4418 static enum prop_handled
4419 handle_composition_prop (it)
4420 struct it *it;
4421 {
4422 Lisp_Object prop, string;
4423 int pos, pos_byte, end;
4424 enum prop_handled handled = HANDLED_NORMALLY;
4425
4426 if (STRINGP (it->string))
4427 {
4428 pos = IT_STRING_CHARPOS (*it);
4429 pos_byte = IT_STRING_BYTEPOS (*it);
4430 string = it->string;
4431 }
4432 else
4433 {
4434 pos = IT_CHARPOS (*it);
4435 pos_byte = IT_BYTEPOS (*it);
4436 string = Qnil;
4437 }
4438
4439 /* If there's a valid composition and point is not inside of the
4440 composition (in the case that the composition is from the current
4441 buffer), draw a glyph composed from the composition components. */
4442 if (find_composition (pos, -1, &pos, &end, &prop, string)
4443 && COMPOSITION_VALID_P (pos, end, prop)
4444 && (STRINGP (it->string) || (PT <= pos || PT >= end)))
4445 {
4446 int id = get_composition_id (pos, pos_byte, end - pos, prop, string);
4447
4448 if (id >= 0)
4449 {
4450 it->method = GET_FROM_COMPOSITION;
4451 it->cmp_id = id;
4452 it->cmp_len = COMPOSITION_LENGTH (prop);
4453 /* For a terminal, draw only the first character of the
4454 components. */
4455 it->c = COMPOSITION_GLYPH (composition_table[id], 0);
4456 it->len = (STRINGP (it->string)
4457 ? string_char_to_byte (it->string, end)
4458 : CHAR_TO_BYTE (end)) - pos_byte;
4459 it->stop_charpos = end;
4460 handled = HANDLED_RETURN;
4461 }
4462 }
4463
4464 return handled;
4465 }
4466
4467
4468 \f
4469 /***********************************************************************
4470 Overlay strings
4471 ***********************************************************************/
4472
4473 /* The following structure is used to record overlay strings for
4474 later sorting in load_overlay_strings. */
4475
4476 struct overlay_entry
4477 {
4478 Lisp_Object overlay;
4479 Lisp_Object string;
4480 int priority;
4481 int after_string_p;
4482 };
4483
4484
4485 /* Set up iterator IT from overlay strings at its current position.
4486 Called from handle_stop. */
4487
4488 static enum prop_handled
4489 handle_overlay_change (it)
4490 struct it *it;
4491 {
4492 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
4493 return HANDLED_RECOMPUTE_PROPS;
4494 else
4495 return HANDLED_NORMALLY;
4496 }
4497
4498
4499 /* Set up the next overlay string for delivery by IT, if there is an
4500 overlay string to deliver. Called by set_iterator_to_next when the
4501 end of the current overlay string is reached. If there are more
4502 overlay strings to display, IT->string and
4503 IT->current.overlay_string_index are set appropriately here.
4504 Otherwise IT->string is set to nil. */
4505
4506 static void
4507 next_overlay_string (it)
4508 struct it *it;
4509 {
4510 ++it->current.overlay_string_index;
4511 if (it->current.overlay_string_index == it->n_overlay_strings)
4512 {
4513 /* No more overlay strings. Restore IT's settings to what
4514 they were before overlay strings were processed, and
4515 continue to deliver from current_buffer. */
4516 int display_ellipsis_p = it->stack[it->sp - 1].display_ellipsis_p;
4517
4518 pop_it (it);
4519 xassert (it->stop_charpos >= BEGV
4520 && it->stop_charpos <= it->end_charpos);
4521 it->string = Qnil;
4522 it->current.overlay_string_index = -1;
4523 SET_TEXT_POS (it->current.string_pos, -1, -1);
4524 it->n_overlay_strings = 0;
4525 it->method = GET_FROM_BUFFER;
4526
4527 /* If we're at the end of the buffer, record that we have
4528 processed the overlay strings there already, so that
4529 next_element_from_buffer doesn't try it again. */
4530 if (IT_CHARPOS (*it) >= it->end_charpos)
4531 it->overlay_strings_at_end_processed_p = 1;
4532
4533 /* If we have to display `...' for invisible text, set
4534 the iterator up for that. */
4535 if (display_ellipsis_p)
4536 setup_for_ellipsis (it, 0);
4537 }
4538 else
4539 {
4540 /* There are more overlay strings to process. If
4541 IT->current.overlay_string_index has advanced to a position
4542 where we must load IT->overlay_strings with more strings, do
4543 it. */
4544 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
4545
4546 if (it->current.overlay_string_index && i == 0)
4547 load_overlay_strings (it, 0);
4548
4549 /* Initialize IT to deliver display elements from the overlay
4550 string. */
4551 it->string = it->overlay_strings[i];
4552 it->multibyte_p = STRING_MULTIBYTE (it->string);
4553 SET_TEXT_POS (it->current.string_pos, 0, 0);
4554 it->method = GET_FROM_STRING;
4555 it->stop_charpos = 0;
4556 }
4557
4558 CHECK_IT (it);
4559 }
4560
4561
4562 /* Compare two overlay_entry structures E1 and E2. Used as a
4563 comparison function for qsort in load_overlay_strings. Overlay
4564 strings for the same position are sorted so that
4565
4566 1. All after-strings come in front of before-strings, except
4567 when they come from the same overlay.
4568
4569 2. Within after-strings, strings are sorted so that overlay strings
4570 from overlays with higher priorities come first.
4571
4572 2. Within before-strings, strings are sorted so that overlay
4573 strings from overlays with higher priorities come last.
4574
4575 Value is analogous to strcmp. */
4576
4577
4578 static int
4579 compare_overlay_entries (e1, e2)
4580 void *e1, *e2;
4581 {
4582 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
4583 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
4584 int result;
4585
4586 if (entry1->after_string_p != entry2->after_string_p)
4587 {
4588 /* Let after-strings appear in front of before-strings if
4589 they come from different overlays. */
4590 if (EQ (entry1->overlay, entry2->overlay))
4591 result = entry1->after_string_p ? 1 : -1;
4592 else
4593 result = entry1->after_string_p ? -1 : 1;
4594 }
4595 else if (entry1->after_string_p)
4596 /* After-strings sorted in order of decreasing priority. */
4597 result = entry2->priority - entry1->priority;
4598 else
4599 /* Before-strings sorted in order of increasing priority. */
4600 result = entry1->priority - entry2->priority;
4601
4602 return result;
4603 }
4604
4605
4606 /* Load the vector IT->overlay_strings with overlay strings from IT's
4607 current buffer position, or from CHARPOS if that is > 0. Set
4608 IT->n_overlays to the total number of overlay strings found.
4609
4610 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
4611 a time. On entry into load_overlay_strings,
4612 IT->current.overlay_string_index gives the number of overlay
4613 strings that have already been loaded by previous calls to this
4614 function.
4615
4616 IT->add_overlay_start contains an additional overlay start
4617 position to consider for taking overlay strings from, if non-zero.
4618 This position comes into play when the overlay has an `invisible'
4619 property, and both before and after-strings. When we've skipped to
4620 the end of the overlay, because of its `invisible' property, we
4621 nevertheless want its before-string to appear.
4622 IT->add_overlay_start will contain the overlay start position
4623 in this case.
4624
4625 Overlay strings are sorted so that after-string strings come in
4626 front of before-string strings. Within before and after-strings,
4627 strings are sorted by overlay priority. See also function
4628 compare_overlay_entries. */
4629
4630 static void
4631 load_overlay_strings (it, charpos)
4632 struct it *it;
4633 int charpos;
4634 {
4635 extern Lisp_Object Qafter_string, Qbefore_string, Qwindow, Qpriority;
4636 Lisp_Object overlay, window, str, invisible;
4637 struct Lisp_Overlay *ov;
4638 int start, end;
4639 int size = 20;
4640 int n = 0, i, j, invis_p;
4641 struct overlay_entry *entries
4642 = (struct overlay_entry *) alloca (size * sizeof *entries);
4643
4644 if (charpos <= 0)
4645 charpos = IT_CHARPOS (*it);
4646
4647 /* Append the overlay string STRING of overlay OVERLAY to vector
4648 `entries' which has size `size' and currently contains `n'
4649 elements. AFTER_P non-zero means STRING is an after-string of
4650 OVERLAY. */
4651 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
4652 do \
4653 { \
4654 Lisp_Object priority; \
4655 \
4656 if (n == size) \
4657 { \
4658 int new_size = 2 * size; \
4659 struct overlay_entry *old = entries; \
4660 entries = \
4661 (struct overlay_entry *) alloca (new_size \
4662 * sizeof *entries); \
4663 bcopy (old, entries, size * sizeof *entries); \
4664 size = new_size; \
4665 } \
4666 \
4667 entries[n].string = (STRING); \
4668 entries[n].overlay = (OVERLAY); \
4669 priority = Foverlay_get ((OVERLAY), Qpriority); \
4670 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
4671 entries[n].after_string_p = (AFTER_P); \
4672 ++n; \
4673 } \
4674 while (0)
4675
4676 /* Process overlay before the overlay center. */
4677 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
4678 {
4679 XSETMISC (overlay, ov);
4680 xassert (OVERLAYP (overlay));
4681 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4682 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4683
4684 if (end < charpos)
4685 break;
4686
4687 /* Skip this overlay if it doesn't start or end at IT's current
4688 position. */
4689 if (end != charpos && start != charpos)
4690 continue;
4691
4692 /* Skip this overlay if it doesn't apply to IT->w. */
4693 window = Foverlay_get (overlay, Qwindow);
4694 if (WINDOWP (window) && XWINDOW (window) != it->w)
4695 continue;
4696
4697 /* If the text ``under'' the overlay is invisible, both before-
4698 and after-strings from this overlay are visible; start and
4699 end position are indistinguishable. */
4700 invisible = Foverlay_get (overlay, Qinvisible);
4701 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4702
4703 /* If overlay has a non-empty before-string, record it. */
4704 if ((start == charpos || (end == charpos && invis_p))
4705 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
4706 && SCHARS (str))
4707 RECORD_OVERLAY_STRING (overlay, str, 0);
4708
4709 /* If overlay has a non-empty after-string, record it. */
4710 if ((end == charpos || (start == charpos && invis_p))
4711 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
4712 && SCHARS (str))
4713 RECORD_OVERLAY_STRING (overlay, str, 1);
4714 }
4715
4716 /* Process overlays after the overlay center. */
4717 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
4718 {
4719 XSETMISC (overlay, ov);
4720 xassert (OVERLAYP (overlay));
4721 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4722 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4723
4724 if (start > charpos)
4725 break;
4726
4727 /* Skip this overlay if it doesn't start or end at IT's current
4728 position. */
4729 if (end != charpos && start != charpos)
4730 continue;
4731
4732 /* Skip this overlay if it doesn't apply to IT->w. */
4733 window = Foverlay_get (overlay, Qwindow);
4734 if (WINDOWP (window) && XWINDOW (window) != it->w)
4735 continue;
4736
4737 /* If the text ``under'' the overlay is invisible, it has a zero
4738 dimension, and both before- and after-strings apply. */
4739 invisible = Foverlay_get (overlay, Qinvisible);
4740 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4741
4742 /* If overlay has a non-empty before-string, record it. */
4743 if ((start == charpos || (end == charpos && invis_p))
4744 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
4745 && SCHARS (str))
4746 RECORD_OVERLAY_STRING (overlay, str, 0);
4747
4748 /* If overlay has a non-empty after-string, record it. */
4749 if ((end == charpos || (start == charpos && invis_p))
4750 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
4751 && SCHARS (str))
4752 RECORD_OVERLAY_STRING (overlay, str, 1);
4753 }
4754
4755 #undef RECORD_OVERLAY_STRING
4756
4757 /* Sort entries. */
4758 if (n > 1)
4759 qsort (entries, n, sizeof *entries, compare_overlay_entries);
4760
4761 /* Record the total number of strings to process. */
4762 it->n_overlay_strings = n;
4763
4764 /* IT->current.overlay_string_index is the number of overlay strings
4765 that have already been consumed by IT. Copy some of the
4766 remaining overlay strings to IT->overlay_strings. */
4767 i = 0;
4768 j = it->current.overlay_string_index;
4769 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
4770 it->overlay_strings[i++] = entries[j++].string;
4771
4772 CHECK_IT (it);
4773 }
4774
4775
4776 /* Get the first chunk of overlay strings at IT's current buffer
4777 position, or at CHARPOS if that is > 0. Value is non-zero if at
4778 least one overlay string was found. */
4779
4780 static int
4781 get_overlay_strings (it, charpos)
4782 struct it *it;
4783 int charpos;
4784 {
4785 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
4786 process. This fills IT->overlay_strings with strings, and sets
4787 IT->n_overlay_strings to the total number of strings to process.
4788 IT->pos.overlay_string_index has to be set temporarily to zero
4789 because load_overlay_strings needs this; it must be set to -1
4790 when no overlay strings are found because a zero value would
4791 indicate a position in the first overlay string. */
4792 it->current.overlay_string_index = 0;
4793 load_overlay_strings (it, charpos);
4794
4795 /* If we found overlay strings, set up IT to deliver display
4796 elements from the first one. Otherwise set up IT to deliver
4797 from current_buffer. */
4798 if (it->n_overlay_strings)
4799 {
4800 /* Make sure we know settings in current_buffer, so that we can
4801 restore meaningful values when we're done with the overlay
4802 strings. */
4803 compute_stop_pos (it);
4804 xassert (it->face_id >= 0);
4805
4806 /* Save IT's settings. They are restored after all overlay
4807 strings have been processed. */
4808 xassert (it->sp == 0);
4809 push_it (it);
4810
4811 /* Set up IT to deliver display elements from the first overlay
4812 string. */
4813 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
4814 it->string = it->overlay_strings[0];
4815 it->stop_charpos = 0;
4816 xassert (STRINGP (it->string));
4817 it->end_charpos = SCHARS (it->string);
4818 it->multibyte_p = STRING_MULTIBYTE (it->string);
4819 it->method = GET_FROM_STRING;
4820 }
4821 else
4822 {
4823 it->string = Qnil;
4824 it->current.overlay_string_index = -1;
4825 it->method = GET_FROM_BUFFER;
4826 }
4827
4828 CHECK_IT (it);
4829
4830 /* Value is non-zero if we found at least one overlay string. */
4831 return STRINGP (it->string);
4832 }
4833
4834
4835 \f
4836 /***********************************************************************
4837 Saving and restoring state
4838 ***********************************************************************/
4839
4840 /* Save current settings of IT on IT->stack. Called, for example,
4841 before setting up IT for an overlay string, to be able to restore
4842 IT's settings to what they were after the overlay string has been
4843 processed. */
4844
4845 static void
4846 push_it (it)
4847 struct it *it;
4848 {
4849 struct iterator_stack_entry *p;
4850
4851 xassert (it->sp < 2);
4852 p = it->stack + it->sp;
4853
4854 p->stop_charpos = it->stop_charpos;
4855 xassert (it->face_id >= 0);
4856 p->face_id = it->face_id;
4857 p->string = it->string;
4858 p->pos = it->current;
4859 p->end_charpos = it->end_charpos;
4860 p->string_nchars = it->string_nchars;
4861 p->area = it->area;
4862 p->multibyte_p = it->multibyte_p;
4863 p->slice = it->slice;
4864 p->space_width = it->space_width;
4865 p->font_height = it->font_height;
4866 p->voffset = it->voffset;
4867 p->string_from_display_prop_p = it->string_from_display_prop_p;
4868 p->display_ellipsis_p = 0;
4869 ++it->sp;
4870 }
4871
4872
4873 /* Restore IT's settings from IT->stack. Called, for example, when no
4874 more overlay strings must be processed, and we return to delivering
4875 display elements from a buffer, or when the end of a string from a
4876 `display' property is reached and we return to delivering display
4877 elements from an overlay string, or from a buffer. */
4878
4879 static void
4880 pop_it (it)
4881 struct it *it;
4882 {
4883 struct iterator_stack_entry *p;
4884
4885 xassert (it->sp > 0);
4886 --it->sp;
4887 p = it->stack + it->sp;
4888 it->stop_charpos = p->stop_charpos;
4889 it->face_id = p->face_id;
4890 it->string = p->string;
4891 it->current = p->pos;
4892 it->end_charpos = p->end_charpos;
4893 it->string_nchars = p->string_nchars;
4894 it->area = p->area;
4895 it->multibyte_p = p->multibyte_p;
4896 it->slice = p->slice;
4897 it->space_width = p->space_width;
4898 it->font_height = p->font_height;
4899 it->voffset = p->voffset;
4900 it->string_from_display_prop_p = p->string_from_display_prop_p;
4901 }
4902
4903
4904 \f
4905 /***********************************************************************
4906 Moving over lines
4907 ***********************************************************************/
4908
4909 /* Set IT's current position to the previous line start. */
4910
4911 static void
4912 back_to_previous_line_start (it)
4913 struct it *it;
4914 {
4915 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
4916 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
4917 }
4918
4919
4920 /* Move IT to the next line start.
4921
4922 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
4923 we skipped over part of the text (as opposed to moving the iterator
4924 continuously over the text). Otherwise, don't change the value
4925 of *SKIPPED_P.
4926
4927 Newlines may come from buffer text, overlay strings, or strings
4928 displayed via the `display' property. That's the reason we can't
4929 simply use find_next_newline_no_quit.
4930
4931 Note that this function may not skip over invisible text that is so
4932 because of text properties and immediately follows a newline. If
4933 it would, function reseat_at_next_visible_line_start, when called
4934 from set_iterator_to_next, would effectively make invisible
4935 characters following a newline part of the wrong glyph row, which
4936 leads to wrong cursor motion. */
4937
4938 static int
4939 forward_to_next_line_start (it, skipped_p)
4940 struct it *it;
4941 int *skipped_p;
4942 {
4943 int old_selective, newline_found_p, n;
4944 const int MAX_NEWLINE_DISTANCE = 500;
4945
4946 /* If already on a newline, just consume it to avoid unintended
4947 skipping over invisible text below. */
4948 if (it->what == IT_CHARACTER
4949 && it->c == '\n'
4950 && CHARPOS (it->position) == IT_CHARPOS (*it))
4951 {
4952 set_iterator_to_next (it, 0);
4953 it->c = 0;
4954 return 1;
4955 }
4956
4957 /* Don't handle selective display in the following. It's (a)
4958 unnecessary because it's done by the caller, and (b) leads to an
4959 infinite recursion because next_element_from_ellipsis indirectly
4960 calls this function. */
4961 old_selective = it->selective;
4962 it->selective = 0;
4963
4964 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
4965 from buffer text. */
4966 for (n = newline_found_p = 0;
4967 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
4968 n += STRINGP (it->string) ? 0 : 1)
4969 {
4970 if (!get_next_display_element (it))
4971 return 0;
4972 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
4973 set_iterator_to_next (it, 0);
4974 }
4975
4976 /* If we didn't find a newline near enough, see if we can use a
4977 short-cut. */
4978 if (!newline_found_p)
4979 {
4980 int start = IT_CHARPOS (*it);
4981 int limit = find_next_newline_no_quit (start, 1);
4982 Lisp_Object pos;
4983
4984 xassert (!STRINGP (it->string));
4985
4986 /* If there isn't any `display' property in sight, and no
4987 overlays, we can just use the position of the newline in
4988 buffer text. */
4989 if (it->stop_charpos >= limit
4990 || ((pos = Fnext_single_property_change (make_number (start),
4991 Qdisplay,
4992 Qnil, make_number (limit)),
4993 NILP (pos))
4994 && next_overlay_change (start) == ZV))
4995 {
4996 IT_CHARPOS (*it) = limit;
4997 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
4998 *skipped_p = newline_found_p = 1;
4999 }
5000 else
5001 {
5002 while (get_next_display_element (it)
5003 && !newline_found_p)
5004 {
5005 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
5006 set_iterator_to_next (it, 0);
5007 }
5008 }
5009 }
5010
5011 it->selective = old_selective;
5012 return newline_found_p;
5013 }
5014
5015
5016 /* Set IT's current position to the previous visible line start. Skip
5017 invisible text that is so either due to text properties or due to
5018 selective display. Caution: this does not change IT->current_x and
5019 IT->hpos. */
5020
5021 static void
5022 back_to_previous_visible_line_start (it)
5023 struct it *it;
5024 {
5025 while (IT_CHARPOS (*it) > BEGV)
5026 {
5027 back_to_previous_line_start (it);
5028 if (IT_CHARPOS (*it) <= BEGV)
5029 break;
5030
5031 /* If selective > 0, then lines indented more than that values
5032 are invisible. */
5033 if (it->selective > 0
5034 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
5035 (double) it->selective)) /* iftc */
5036 continue;
5037
5038 /* Check the newline before point for invisibility. */
5039 {
5040 Lisp_Object prop;
5041 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
5042 Qinvisible, it->window);
5043 if (TEXT_PROP_MEANS_INVISIBLE (prop))
5044 continue;
5045 }
5046
5047 /* If newline has a display property that replaces the newline with something
5048 else (image or text), find start of overlay or interval and continue search
5049 from that point. */
5050 if (IT_CHARPOS (*it) > BEGV)
5051 {
5052 struct it it2 = *it;
5053 int pos;
5054 int beg, end;
5055 Lisp_Object val, overlay;
5056
5057 pos = --IT_CHARPOS (it2);
5058 --IT_BYTEPOS (it2);
5059 it2.sp = 0;
5060 if (handle_display_prop (&it2) == HANDLED_RETURN
5061 && !NILP (val = get_char_property_and_overlay
5062 (make_number (pos), Qdisplay, Qnil, &overlay))
5063 && (OVERLAYP (overlay)
5064 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
5065 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
5066 {
5067 if (beg < BEGV)
5068 beg = BEGV;
5069 IT_CHARPOS (*it) = beg;
5070 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
5071 continue;
5072 }
5073 }
5074
5075 break;
5076 }
5077
5078 xassert (IT_CHARPOS (*it) >= BEGV);
5079 xassert (IT_CHARPOS (*it) == BEGV
5080 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
5081 CHECK_IT (it);
5082 }
5083
5084
5085 /* Reseat iterator IT at the previous visible line start. Skip
5086 invisible text that is so either due to text properties or due to
5087 selective display. At the end, update IT's overlay information,
5088 face information etc. */
5089
5090 void
5091 reseat_at_previous_visible_line_start (it)
5092 struct it *it;
5093 {
5094 back_to_previous_visible_line_start (it);
5095 reseat (it, it->current.pos, 1);
5096 CHECK_IT (it);
5097 }
5098
5099
5100 /* Reseat iterator IT on the next visible line start in the current
5101 buffer. ON_NEWLINE_P non-zero means position IT on the newline
5102 preceding the line start. Skip over invisible text that is so
5103 because of selective display. Compute faces, overlays etc at the
5104 new position. Note that this function does not skip over text that
5105 is invisible because of text properties. */
5106
5107 static void
5108 reseat_at_next_visible_line_start (it, on_newline_p)
5109 struct it *it;
5110 int on_newline_p;
5111 {
5112 int newline_found_p, skipped_p = 0;
5113
5114 newline_found_p = forward_to_next_line_start (it, &skipped_p);
5115
5116 /* Skip over lines that are invisible because they are indented
5117 more than the value of IT->selective. */
5118 if (it->selective > 0)
5119 while (IT_CHARPOS (*it) < ZV
5120 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
5121 (double) it->selective)) /* iftc */
5122 {
5123 xassert (IT_BYTEPOS (*it) == BEGV
5124 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
5125 newline_found_p = forward_to_next_line_start (it, &skipped_p);
5126 }
5127
5128 /* Position on the newline if that's what's requested. */
5129 if (on_newline_p && newline_found_p)
5130 {
5131 if (STRINGP (it->string))
5132 {
5133 if (IT_STRING_CHARPOS (*it) > 0)
5134 {
5135 --IT_STRING_CHARPOS (*it);
5136 --IT_STRING_BYTEPOS (*it);
5137 }
5138 }
5139 else if (IT_CHARPOS (*it) > BEGV)
5140 {
5141 --IT_CHARPOS (*it);
5142 --IT_BYTEPOS (*it);
5143 reseat (it, it->current.pos, 0);
5144 }
5145 }
5146 else if (skipped_p)
5147 reseat (it, it->current.pos, 0);
5148
5149 CHECK_IT (it);
5150 }
5151
5152
5153 \f
5154 /***********************************************************************
5155 Changing an iterator's position
5156 ***********************************************************************/
5157
5158 /* Change IT's current position to POS in current_buffer. If FORCE_P
5159 is non-zero, always check for text properties at the new position.
5160 Otherwise, text properties are only looked up if POS >=
5161 IT->check_charpos of a property. */
5162
5163 static void
5164 reseat (it, pos, force_p)
5165 struct it *it;
5166 struct text_pos pos;
5167 int force_p;
5168 {
5169 int original_pos = IT_CHARPOS (*it);
5170
5171 reseat_1 (it, pos, 0);
5172
5173 /* Determine where to check text properties. Avoid doing it
5174 where possible because text property lookup is very expensive. */
5175 if (force_p
5176 || CHARPOS (pos) > it->stop_charpos
5177 || CHARPOS (pos) < original_pos)
5178 handle_stop (it);
5179
5180 CHECK_IT (it);
5181 }
5182
5183
5184 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
5185 IT->stop_pos to POS, also. */
5186
5187 static void
5188 reseat_1 (it, pos, set_stop_p)
5189 struct it *it;
5190 struct text_pos pos;
5191 int set_stop_p;
5192 {
5193 /* Don't call this function when scanning a C string. */
5194 xassert (it->s == NULL);
5195
5196 /* POS must be a reasonable value. */
5197 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
5198
5199 it->current.pos = it->position = pos;
5200 XSETBUFFER (it->object, current_buffer);
5201 it->end_charpos = ZV;
5202 it->dpvec = NULL;
5203 it->current.dpvec_index = -1;
5204 it->current.overlay_string_index = -1;
5205 IT_STRING_CHARPOS (*it) = -1;
5206 IT_STRING_BYTEPOS (*it) = -1;
5207 it->string = Qnil;
5208 it->method = GET_FROM_BUFFER;
5209 /* RMS: I added this to fix a bug in move_it_vertically_backward
5210 where it->area continued to relate to the starting point
5211 for the backward motion. Bug report from
5212 Nick Roberts <nick@nick.uklinux.net> on 19 May 2003.
5213 However, I am not sure whether reseat still does the right thing
5214 in general after this change. */
5215 it->area = TEXT_AREA;
5216 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
5217 it->sp = 0;
5218 it->face_before_selective_p = 0;
5219
5220 if (set_stop_p)
5221 it->stop_charpos = CHARPOS (pos);
5222 }
5223
5224
5225 /* Set up IT for displaying a string, starting at CHARPOS in window W.
5226 If S is non-null, it is a C string to iterate over. Otherwise,
5227 STRING gives a Lisp string to iterate over.
5228
5229 If PRECISION > 0, don't return more then PRECISION number of
5230 characters from the string.
5231
5232 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
5233 characters have been returned. FIELD_WIDTH < 0 means an infinite
5234 field width.
5235
5236 MULTIBYTE = 0 means disable processing of multibyte characters,
5237 MULTIBYTE > 0 means enable it,
5238 MULTIBYTE < 0 means use IT->multibyte_p.
5239
5240 IT must be initialized via a prior call to init_iterator before
5241 calling this function. */
5242
5243 static void
5244 reseat_to_string (it, s, string, charpos, precision, field_width, multibyte)
5245 struct it *it;
5246 unsigned char *s;
5247 Lisp_Object string;
5248 int charpos;
5249 int precision, field_width, multibyte;
5250 {
5251 /* No region in strings. */
5252 it->region_beg_charpos = it->region_end_charpos = -1;
5253
5254 /* No text property checks performed by default, but see below. */
5255 it->stop_charpos = -1;
5256
5257 /* Set iterator position and end position. */
5258 bzero (&it->current, sizeof it->current);
5259 it->current.overlay_string_index = -1;
5260 it->current.dpvec_index = -1;
5261 xassert (charpos >= 0);
5262
5263 /* If STRING is specified, use its multibyteness, otherwise use the
5264 setting of MULTIBYTE, if specified. */
5265 if (multibyte >= 0)
5266 it->multibyte_p = multibyte > 0;
5267
5268 if (s == NULL)
5269 {
5270 xassert (STRINGP (string));
5271 it->string = string;
5272 it->s = NULL;
5273 it->end_charpos = it->string_nchars = SCHARS (string);
5274 it->method = GET_FROM_STRING;
5275 it->current.string_pos = string_pos (charpos, string);
5276 }
5277 else
5278 {
5279 it->s = s;
5280 it->string = Qnil;
5281
5282 /* Note that we use IT->current.pos, not it->current.string_pos,
5283 for displaying C strings. */
5284 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
5285 if (it->multibyte_p)
5286 {
5287 it->current.pos = c_string_pos (charpos, s, 1);
5288 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
5289 }
5290 else
5291 {
5292 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
5293 it->end_charpos = it->string_nchars = strlen (s);
5294 }
5295
5296 it->method = GET_FROM_C_STRING;
5297 }
5298
5299 /* PRECISION > 0 means don't return more than PRECISION characters
5300 from the string. */
5301 if (precision > 0 && it->end_charpos - charpos > precision)
5302 it->end_charpos = it->string_nchars = charpos + precision;
5303
5304 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
5305 characters have been returned. FIELD_WIDTH == 0 means don't pad,
5306 FIELD_WIDTH < 0 means infinite field width. This is useful for
5307 padding with `-' at the end of a mode line. */
5308 if (field_width < 0)
5309 field_width = INFINITY;
5310 if (field_width > it->end_charpos - charpos)
5311 it->end_charpos = charpos + field_width;
5312
5313 /* Use the standard display table for displaying strings. */
5314 if (DISP_TABLE_P (Vstandard_display_table))
5315 it->dp = XCHAR_TABLE (Vstandard_display_table);
5316
5317 it->stop_charpos = charpos;
5318 CHECK_IT (it);
5319 }
5320
5321
5322 \f
5323 /***********************************************************************
5324 Iteration
5325 ***********************************************************************/
5326
5327 /* Map enum it_method value to corresponding next_element_from_* function. */
5328
5329 static int (* get_next_element[NUM_IT_METHODS]) P_ ((struct it *it)) =
5330 {
5331 next_element_from_buffer,
5332 next_element_from_display_vector,
5333 next_element_from_composition,
5334 next_element_from_string,
5335 next_element_from_c_string,
5336 next_element_from_image,
5337 next_element_from_stretch
5338 };
5339
5340
5341 /* Load IT's display element fields with information about the next
5342 display element from the current position of IT. Value is zero if
5343 end of buffer (or C string) is reached. */
5344
5345 static struct frame *last_escape_glyph_frame = NULL;
5346 static unsigned last_escape_glyph_face_id = (1 << FACE_ID_BITS);
5347 static int last_escape_glyph_merged_face_id = 0;
5348
5349 int
5350 get_next_display_element (it)
5351 struct it *it;
5352 {
5353 /* Non-zero means that we found a display element. Zero means that
5354 we hit the end of what we iterate over. Performance note: the
5355 function pointer `method' used here turns out to be faster than
5356 using a sequence of if-statements. */
5357 int success_p;
5358
5359 get_next:
5360 success_p = (*get_next_element[it->method]) (it);
5361
5362 if (it->what == IT_CHARACTER)
5363 {
5364 /* Map via display table or translate control characters.
5365 IT->c, IT->len etc. have been set to the next character by
5366 the function call above. If we have a display table, and it
5367 contains an entry for IT->c, translate it. Don't do this if
5368 IT->c itself comes from a display table, otherwise we could
5369 end up in an infinite recursion. (An alternative could be to
5370 count the recursion depth of this function and signal an
5371 error when a certain maximum depth is reached.) Is it worth
5372 it? */
5373 if (success_p && it->dpvec == NULL)
5374 {
5375 Lisp_Object dv;
5376
5377 if (it->dp
5378 && (dv = DISP_CHAR_VECTOR (it->dp, it->c),
5379 VECTORP (dv)))
5380 {
5381 struct Lisp_Vector *v = XVECTOR (dv);
5382
5383 /* Return the first character from the display table
5384 entry, if not empty. If empty, don't display the
5385 current character. */
5386 if (v->size)
5387 {
5388 it->dpvec_char_len = it->len;
5389 it->dpvec = v->contents;
5390 it->dpend = v->contents + v->size;
5391 it->current.dpvec_index = 0;
5392 it->dpvec_face_id = -1;
5393 it->saved_face_id = it->face_id;
5394 it->method = GET_FROM_DISPLAY_VECTOR;
5395 it->ellipsis_p = 0;
5396 }
5397 else
5398 {
5399 set_iterator_to_next (it, 0);
5400 }
5401 goto get_next;
5402 }
5403
5404 /* Translate control characters into `\003' or `^C' form.
5405 Control characters coming from a display table entry are
5406 currently not translated because we use IT->dpvec to hold
5407 the translation. This could easily be changed but I
5408 don't believe that it is worth doing.
5409
5410 If it->multibyte_p is nonzero, eight-bit characters and
5411 non-printable multibyte characters are also translated to
5412 octal form.
5413
5414 If it->multibyte_p is zero, eight-bit characters that
5415 don't have corresponding multibyte char code are also
5416 translated to octal form. */
5417 else if ((it->c < ' '
5418 && (it->area != TEXT_AREA
5419 /* In mode line, treat \n like other crl chars. */
5420 || (it->c != '\t'
5421 && it->glyph_row && it->glyph_row->mode_line_p)
5422 || (it->c != '\n' && it->c != '\t')))
5423 || (it->multibyte_p
5424 ? ((it->c >= 127
5425 && it->len == 1)
5426 || !CHAR_PRINTABLE_P (it->c)
5427 || (!NILP (Vnobreak_char_display)
5428 && (it->c == 0x8a0 || it->c == 0x8ad
5429 || it->c == 0x920 || it->c == 0x92d
5430 || it->c == 0xe20 || it->c == 0xe2d
5431 || it->c == 0xf20 || it->c == 0xf2d)))
5432 : (it->c >= 127
5433 && (!unibyte_display_via_language_environment
5434 || it->c == unibyte_char_to_multibyte (it->c)))))
5435 {
5436 /* IT->c is a control character which must be displayed
5437 either as '\003' or as `^C' where the '\\' and '^'
5438 can be defined in the display table. Fill
5439 IT->ctl_chars with glyphs for what we have to
5440 display. Then, set IT->dpvec to these glyphs. */
5441 GLYPH g;
5442 int ctl_len;
5443 int face_id, lface_id = 0 ;
5444 GLYPH escape_glyph;
5445
5446 /* Handle control characters with ^. */
5447
5448 if (it->c < 128 && it->ctl_arrow_p)
5449 {
5450 g = '^'; /* default glyph for Control */
5451 /* Set IT->ctl_chars[0] to the glyph for `^'. */
5452 if (it->dp
5453 && INTEGERP (DISP_CTRL_GLYPH (it->dp))
5454 && GLYPH_CHAR_VALID_P (XINT (DISP_CTRL_GLYPH (it->dp))))
5455 {
5456 g = XINT (DISP_CTRL_GLYPH (it->dp));
5457 lface_id = FAST_GLYPH_FACE (g);
5458 }
5459 if (lface_id)
5460 {
5461 g = FAST_GLYPH_CHAR (g);
5462 face_id = merge_faces (it->f, Qt, lface_id,
5463 it->face_id);
5464 }
5465 else if (it->f == last_escape_glyph_frame
5466 && it->face_id == last_escape_glyph_face_id)
5467 {
5468 face_id = last_escape_glyph_merged_face_id;
5469 }
5470 else
5471 {
5472 /* Merge the escape-glyph face into the current face. */
5473 face_id = merge_faces (it->f, Qescape_glyph, 0,
5474 it->face_id);
5475 last_escape_glyph_frame = it->f;
5476 last_escape_glyph_face_id = it->face_id;
5477 last_escape_glyph_merged_face_id = face_id;
5478 }
5479
5480 XSETINT (it->ctl_chars[0], g);
5481 g = it->c ^ 0100;
5482 XSETINT (it->ctl_chars[1], g);
5483 ctl_len = 2;
5484 goto display_control;
5485 }
5486
5487 /* Handle non-break space in the mode where it only gets
5488 highlighting. */
5489
5490 if (EQ (Vnobreak_char_display, Qt)
5491 && (it->c == 0x8a0 || it->c == 0x920
5492 || it->c == 0xe20 || it->c == 0xf20))
5493 {
5494 /* Merge the no-break-space face into the current face. */
5495 face_id = merge_faces (it->f, Qnobreak_space, 0,
5496 it->face_id);
5497
5498 g = it->c = ' ';
5499 XSETINT (it->ctl_chars[0], g);
5500 ctl_len = 1;
5501 goto display_control;
5502 }
5503
5504 /* Handle sequences that start with the "escape glyph". */
5505
5506 /* the default escape glyph is \. */
5507 escape_glyph = '\\';
5508
5509 if (it->dp
5510 && INTEGERP (DISP_ESCAPE_GLYPH (it->dp))
5511 && GLYPH_CHAR_VALID_P (XFASTINT (DISP_ESCAPE_GLYPH (it->dp))))
5512 {
5513 escape_glyph = XFASTINT (DISP_ESCAPE_GLYPH (it->dp));
5514 lface_id = FAST_GLYPH_FACE (escape_glyph);
5515 }
5516 if (lface_id)
5517 {
5518 /* The display table specified a face.
5519 Merge it into face_id and also into escape_glyph. */
5520 escape_glyph = FAST_GLYPH_CHAR (escape_glyph);
5521 face_id = merge_faces (it->f, Qt, lface_id,
5522 it->face_id);
5523 }
5524 else if (it->f == last_escape_glyph_frame
5525 && it->face_id == last_escape_glyph_face_id)
5526 {
5527 face_id = last_escape_glyph_merged_face_id;
5528 }
5529 else
5530 {
5531 /* Merge the escape-glyph face into the current face. */
5532 face_id = merge_faces (it->f, Qescape_glyph, 0,
5533 it->face_id);
5534 last_escape_glyph_frame = it->f;
5535 last_escape_glyph_face_id = it->face_id;
5536 last_escape_glyph_merged_face_id = face_id;
5537 }
5538
5539 /* Handle soft hyphens in the mode where they only get
5540 highlighting. */
5541
5542 if (EQ (Vnobreak_char_display, Qt)
5543 && (it->c == 0x8ad || it->c == 0x92d
5544 || it->c == 0xe2d || it->c == 0xf2d))
5545 {
5546 g = it->c = '-';
5547 XSETINT (it->ctl_chars[0], g);
5548 ctl_len = 1;
5549 goto display_control;
5550 }
5551
5552 /* Handle non-break space and soft hyphen
5553 with the escape glyph. */
5554
5555 if (it->c == 0x8a0 || it->c == 0x8ad
5556 || it->c == 0x920 || it->c == 0x92d
5557 || it->c == 0xe20 || it->c == 0xe2d
5558 || it->c == 0xf20 || it->c == 0xf2d)
5559 {
5560 XSETINT (it->ctl_chars[0], escape_glyph);
5561 g = it->c = ((it->c & 0xf) == 0 ? ' ' : '-');
5562 XSETINT (it->ctl_chars[1], g);
5563 ctl_len = 2;
5564 goto display_control;
5565 }
5566
5567 {
5568 unsigned char str[MAX_MULTIBYTE_LENGTH];
5569 int len;
5570 int i;
5571
5572 /* Set IT->ctl_chars[0] to the glyph for `\\'. */
5573 if (SINGLE_BYTE_CHAR_P (it->c))
5574 str[0] = it->c, len = 1;
5575 else
5576 {
5577 len = CHAR_STRING_NO_SIGNAL (it->c, str);
5578 if (len < 0)
5579 {
5580 /* It's an invalid character, which shouldn't
5581 happen actually, but due to bugs it may
5582 happen. Let's print the char as is, there's
5583 not much meaningful we can do with it. */
5584 str[0] = it->c;
5585 str[1] = it->c >> 8;
5586 str[2] = it->c >> 16;
5587 str[3] = it->c >> 24;
5588 len = 4;
5589 }
5590 }
5591
5592 for (i = 0; i < len; i++)
5593 {
5594 XSETINT (it->ctl_chars[i * 4], escape_glyph);
5595 /* Insert three more glyphs into IT->ctl_chars for
5596 the octal display of the character. */
5597 g = ((str[i] >> 6) & 7) + '0';
5598 XSETINT (it->ctl_chars[i * 4 + 1], g);
5599 g = ((str[i] >> 3) & 7) + '0';
5600 XSETINT (it->ctl_chars[i * 4 + 2], g);
5601 g = (str[i] & 7) + '0';
5602 XSETINT (it->ctl_chars[i * 4 + 3], g);
5603 }
5604 ctl_len = len * 4;
5605 }
5606
5607 display_control:
5608 /* Set up IT->dpvec and return first character from it. */
5609 it->dpvec_char_len = it->len;
5610 it->dpvec = it->ctl_chars;
5611 it->dpend = it->dpvec + ctl_len;
5612 it->current.dpvec_index = 0;
5613 it->dpvec_face_id = face_id;
5614 it->saved_face_id = it->face_id;
5615 it->method = GET_FROM_DISPLAY_VECTOR;
5616 it->ellipsis_p = 0;
5617 goto get_next;
5618 }
5619 }
5620
5621 /* Adjust face id for a multibyte character. There are no
5622 multibyte character in unibyte text. */
5623 if (it->multibyte_p
5624 && success_p
5625 && FRAME_WINDOW_P (it->f))
5626 {
5627 struct face *face = FACE_FROM_ID (it->f, it->face_id);
5628 it->face_id = FACE_FOR_CHAR (it->f, face, it->c);
5629 }
5630 }
5631
5632 /* Is this character the last one of a run of characters with
5633 box? If yes, set IT->end_of_box_run_p to 1. */
5634 if (it->face_box_p
5635 && it->s == NULL)
5636 {
5637 int face_id;
5638 struct face *face;
5639
5640 it->end_of_box_run_p
5641 = ((face_id = face_after_it_pos (it),
5642 face_id != it->face_id)
5643 && (face = FACE_FROM_ID (it->f, face_id),
5644 face->box == FACE_NO_BOX));
5645 }
5646
5647 /* Value is 0 if end of buffer or string reached. */
5648 return success_p;
5649 }
5650
5651
5652 /* Move IT to the next display element.
5653
5654 RESEAT_P non-zero means if called on a newline in buffer text,
5655 skip to the next visible line start.
5656
5657 Functions get_next_display_element and set_iterator_to_next are
5658 separate because I find this arrangement easier to handle than a
5659 get_next_display_element function that also increments IT's
5660 position. The way it is we can first look at an iterator's current
5661 display element, decide whether it fits on a line, and if it does,
5662 increment the iterator position. The other way around we probably
5663 would either need a flag indicating whether the iterator has to be
5664 incremented the next time, or we would have to implement a
5665 decrement position function which would not be easy to write. */
5666
5667 void
5668 set_iterator_to_next (it, reseat_p)
5669 struct it *it;
5670 int reseat_p;
5671 {
5672 /* Reset flags indicating start and end of a sequence of characters
5673 with box. Reset them at the start of this function because
5674 moving the iterator to a new position might set them. */
5675 it->start_of_box_run_p = it->end_of_box_run_p = 0;
5676
5677 switch (it->method)
5678 {
5679 case GET_FROM_BUFFER:
5680 /* The current display element of IT is a character from
5681 current_buffer. Advance in the buffer, and maybe skip over
5682 invisible lines that are so because of selective display. */
5683 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
5684 reseat_at_next_visible_line_start (it, 0);
5685 else
5686 {
5687 xassert (it->len != 0);
5688 IT_BYTEPOS (*it) += it->len;
5689 IT_CHARPOS (*it) += 1;
5690 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
5691 }
5692 break;
5693
5694 case GET_FROM_COMPOSITION:
5695 xassert (it->cmp_id >= 0 && it->cmp_id < n_compositions);
5696 if (STRINGP (it->string))
5697 {
5698 IT_STRING_BYTEPOS (*it) += it->len;
5699 IT_STRING_CHARPOS (*it) += it->cmp_len;
5700 it->method = GET_FROM_STRING;
5701 goto consider_string_end;
5702 }
5703 else
5704 {
5705 IT_BYTEPOS (*it) += it->len;
5706 IT_CHARPOS (*it) += it->cmp_len;
5707 it->method = GET_FROM_BUFFER;
5708 }
5709 break;
5710
5711 case GET_FROM_C_STRING:
5712 /* Current display element of IT is from a C string. */
5713 IT_BYTEPOS (*it) += it->len;
5714 IT_CHARPOS (*it) += 1;
5715 break;
5716
5717 case GET_FROM_DISPLAY_VECTOR:
5718 /* Current display element of IT is from a display table entry.
5719 Advance in the display table definition. Reset it to null if
5720 end reached, and continue with characters from buffers/
5721 strings. */
5722 ++it->current.dpvec_index;
5723
5724 /* Restore face of the iterator to what they were before the
5725 display vector entry (these entries may contain faces). */
5726 it->face_id = it->saved_face_id;
5727
5728 if (it->dpvec + it->current.dpvec_index == it->dpend)
5729 {
5730 int recheck_faces = it->ellipsis_p;
5731
5732 if (it->s)
5733 it->method = GET_FROM_C_STRING;
5734 else if (STRINGP (it->string))
5735 it->method = GET_FROM_STRING;
5736 else
5737 it->method = GET_FROM_BUFFER;
5738
5739 it->dpvec = NULL;
5740 it->current.dpvec_index = -1;
5741
5742 /* Skip over characters which were displayed via IT->dpvec. */
5743 if (it->dpvec_char_len < 0)
5744 reseat_at_next_visible_line_start (it, 1);
5745 else if (it->dpvec_char_len > 0)
5746 {
5747 if (it->method == GET_FROM_STRING
5748 && it->n_overlay_strings > 0)
5749 it->ignore_overlay_strings_at_pos_p = 1;
5750 it->len = it->dpvec_char_len;
5751 set_iterator_to_next (it, reseat_p);
5752 }
5753
5754 /* Maybe recheck faces after display vector */
5755 if (recheck_faces)
5756 it->stop_charpos = IT_CHARPOS (*it);
5757 }
5758 break;
5759
5760 case GET_FROM_STRING:
5761 /* Current display element is a character from a Lisp string. */
5762 xassert (it->s == NULL && STRINGP (it->string));
5763 IT_STRING_BYTEPOS (*it) += it->len;
5764 IT_STRING_CHARPOS (*it) += 1;
5765
5766 consider_string_end:
5767
5768 if (it->current.overlay_string_index >= 0)
5769 {
5770 /* IT->string is an overlay string. Advance to the
5771 next, if there is one. */
5772 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
5773 next_overlay_string (it);
5774 }
5775 else
5776 {
5777 /* IT->string is not an overlay string. If we reached
5778 its end, and there is something on IT->stack, proceed
5779 with what is on the stack. This can be either another
5780 string, this time an overlay string, or a buffer. */
5781 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
5782 && it->sp > 0)
5783 {
5784 pop_it (it);
5785 if (STRINGP (it->string))
5786 goto consider_string_end;
5787 it->method = GET_FROM_BUFFER;
5788 }
5789 }
5790 break;
5791
5792 case GET_FROM_IMAGE:
5793 case GET_FROM_STRETCH:
5794 /* The position etc with which we have to proceed are on
5795 the stack. The position may be at the end of a string,
5796 if the `display' property takes up the whole string. */
5797 xassert (it->sp > 0);
5798 pop_it (it);
5799 it->image_id = 0;
5800 if (STRINGP (it->string))
5801 {
5802 it->method = GET_FROM_STRING;
5803 goto consider_string_end;
5804 }
5805 it->method = GET_FROM_BUFFER;
5806 break;
5807
5808 default:
5809 /* There are no other methods defined, so this should be a bug. */
5810 abort ();
5811 }
5812
5813 xassert (it->method != GET_FROM_STRING
5814 || (STRINGP (it->string)
5815 && IT_STRING_CHARPOS (*it) >= 0));
5816 }
5817
5818 /* Load IT's display element fields with information about the next
5819 display element which comes from a display table entry or from the
5820 result of translating a control character to one of the forms `^C'
5821 or `\003'.
5822
5823 IT->dpvec holds the glyphs to return as characters.
5824 IT->saved_face_id holds the face id before the display vector--
5825 it is restored into IT->face_idin set_iterator_to_next. */
5826
5827 static int
5828 next_element_from_display_vector (it)
5829 struct it *it;
5830 {
5831 /* Precondition. */
5832 xassert (it->dpvec && it->current.dpvec_index >= 0);
5833
5834 it->face_id = it->saved_face_id;
5835
5836 if (INTEGERP (*it->dpvec)
5837 && GLYPH_CHAR_VALID_P (XFASTINT (*it->dpvec)))
5838 {
5839 GLYPH g;
5840
5841 g = XFASTINT (it->dpvec[it->current.dpvec_index]);
5842 it->c = FAST_GLYPH_CHAR (g);
5843 it->len = CHAR_BYTES (it->c);
5844
5845 /* The entry may contain a face id to use. Such a face id is
5846 the id of a Lisp face, not a realized face. A face id of
5847 zero means no face is specified. */
5848 if (it->dpvec_face_id >= 0)
5849 it->face_id = it->dpvec_face_id;
5850 else
5851 {
5852 int lface_id = FAST_GLYPH_FACE (g);
5853 if (lface_id > 0)
5854 it->face_id = merge_faces (it->f, Qt, lface_id,
5855 it->saved_face_id);
5856 }
5857 }
5858 else
5859 /* Display table entry is invalid. Return a space. */
5860 it->c = ' ', it->len = 1;
5861
5862 /* Don't change position and object of the iterator here. They are
5863 still the values of the character that had this display table
5864 entry or was translated, and that's what we want. */
5865 it->what = IT_CHARACTER;
5866 return 1;
5867 }
5868
5869
5870 /* Load IT with the next display element from Lisp string IT->string.
5871 IT->current.string_pos is the current position within the string.
5872 If IT->current.overlay_string_index >= 0, the Lisp string is an
5873 overlay string. */
5874
5875 static int
5876 next_element_from_string (it)
5877 struct it *it;
5878 {
5879 struct text_pos position;
5880
5881 xassert (STRINGP (it->string));
5882 xassert (IT_STRING_CHARPOS (*it) >= 0);
5883 position = it->current.string_pos;
5884
5885 /* Time to check for invisible text? */
5886 if (IT_STRING_CHARPOS (*it) < it->end_charpos
5887 && IT_STRING_CHARPOS (*it) == it->stop_charpos)
5888 {
5889 handle_stop (it);
5890
5891 /* Since a handler may have changed IT->method, we must
5892 recurse here. */
5893 return get_next_display_element (it);
5894 }
5895
5896 if (it->current.overlay_string_index >= 0)
5897 {
5898 /* Get the next character from an overlay string. In overlay
5899 strings, There is no field width or padding with spaces to
5900 do. */
5901 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
5902 {
5903 it->what = IT_EOB;
5904 return 0;
5905 }
5906 else if (STRING_MULTIBYTE (it->string))
5907 {
5908 int remaining = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
5909 const unsigned char *s = (SDATA (it->string)
5910 + IT_STRING_BYTEPOS (*it));
5911 it->c = string_char_and_length (s, remaining, &it->len);
5912 }
5913 else
5914 {
5915 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
5916 it->len = 1;
5917 }
5918 }
5919 else
5920 {
5921 /* Get the next character from a Lisp string that is not an
5922 overlay string. Such strings come from the mode line, for
5923 example. We may have to pad with spaces, or truncate the
5924 string. See also next_element_from_c_string. */
5925 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
5926 {
5927 it->what = IT_EOB;
5928 return 0;
5929 }
5930 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
5931 {
5932 /* Pad with spaces. */
5933 it->c = ' ', it->len = 1;
5934 CHARPOS (position) = BYTEPOS (position) = -1;
5935 }
5936 else if (STRING_MULTIBYTE (it->string))
5937 {
5938 int maxlen = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
5939 const unsigned char *s = (SDATA (it->string)
5940 + IT_STRING_BYTEPOS (*it));
5941 it->c = string_char_and_length (s, maxlen, &it->len);
5942 }
5943 else
5944 {
5945 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
5946 it->len = 1;
5947 }
5948 }
5949
5950 /* Record what we have and where it came from. Note that we store a
5951 buffer position in IT->position although it could arguably be a
5952 string position. */
5953 it->what = IT_CHARACTER;
5954 it->object = it->string;
5955 it->position = position;
5956 return 1;
5957 }
5958
5959
5960 /* Load IT with next display element from C string IT->s.
5961 IT->string_nchars is the maximum number of characters to return
5962 from the string. IT->end_charpos may be greater than
5963 IT->string_nchars when this function is called, in which case we
5964 may have to return padding spaces. Value is zero if end of string
5965 reached, including padding spaces. */
5966
5967 static int
5968 next_element_from_c_string (it)
5969 struct it *it;
5970 {
5971 int success_p = 1;
5972
5973 xassert (it->s);
5974 it->what = IT_CHARACTER;
5975 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
5976 it->object = Qnil;
5977
5978 /* IT's position can be greater IT->string_nchars in case a field
5979 width or precision has been specified when the iterator was
5980 initialized. */
5981 if (IT_CHARPOS (*it) >= it->end_charpos)
5982 {
5983 /* End of the game. */
5984 it->what = IT_EOB;
5985 success_p = 0;
5986 }
5987 else if (IT_CHARPOS (*it) >= it->string_nchars)
5988 {
5989 /* Pad with spaces. */
5990 it->c = ' ', it->len = 1;
5991 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
5992 }
5993 else if (it->multibyte_p)
5994 {
5995 /* Implementation note: The calls to strlen apparently aren't a
5996 performance problem because there is no noticeable performance
5997 difference between Emacs running in unibyte or multibyte mode. */
5998 int maxlen = strlen (it->s) - IT_BYTEPOS (*it);
5999 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it),
6000 maxlen, &it->len);
6001 }
6002 else
6003 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
6004
6005 return success_p;
6006 }
6007
6008
6009 /* Set up IT to return characters from an ellipsis, if appropriate.
6010 The definition of the ellipsis glyphs may come from a display table
6011 entry. This function Fills IT with the first glyph from the
6012 ellipsis if an ellipsis is to be displayed. */
6013
6014 static int
6015 next_element_from_ellipsis (it)
6016 struct it *it;
6017 {
6018 if (it->selective_display_ellipsis_p)
6019 setup_for_ellipsis (it, it->len);
6020 else
6021 {
6022 /* The face at the current position may be different from the
6023 face we find after the invisible text. Remember what it
6024 was in IT->saved_face_id, and signal that it's there by
6025 setting face_before_selective_p. */
6026 it->saved_face_id = it->face_id;
6027 it->method = GET_FROM_BUFFER;
6028 reseat_at_next_visible_line_start (it, 1);
6029 it->face_before_selective_p = 1;
6030 }
6031
6032 return get_next_display_element (it);
6033 }
6034
6035
6036 /* Deliver an image display element. The iterator IT is already
6037 filled with image information (done in handle_display_prop). Value
6038 is always 1. */
6039
6040
6041 static int
6042 next_element_from_image (it)
6043 struct it *it;
6044 {
6045 it->what = IT_IMAGE;
6046 return 1;
6047 }
6048
6049
6050 /* Fill iterator IT with next display element from a stretch glyph
6051 property. IT->object is the value of the text property. Value is
6052 always 1. */
6053
6054 static int
6055 next_element_from_stretch (it)
6056 struct it *it;
6057 {
6058 it->what = IT_STRETCH;
6059 return 1;
6060 }
6061
6062
6063 /* Load IT with the next display element from current_buffer. Value
6064 is zero if end of buffer reached. IT->stop_charpos is the next
6065 position at which to stop and check for text properties or buffer
6066 end. */
6067
6068 static int
6069 next_element_from_buffer (it)
6070 struct it *it;
6071 {
6072 int success_p = 1;
6073
6074 /* Check this assumption, otherwise, we would never enter the
6075 if-statement, below. */
6076 xassert (IT_CHARPOS (*it) >= BEGV
6077 && IT_CHARPOS (*it) <= it->stop_charpos);
6078
6079 if (IT_CHARPOS (*it) >= it->stop_charpos)
6080 {
6081 if (IT_CHARPOS (*it) >= it->end_charpos)
6082 {
6083 int overlay_strings_follow_p;
6084
6085 /* End of the game, except when overlay strings follow that
6086 haven't been returned yet. */
6087 if (it->overlay_strings_at_end_processed_p)
6088 overlay_strings_follow_p = 0;
6089 else
6090 {
6091 it->overlay_strings_at_end_processed_p = 1;
6092 overlay_strings_follow_p = get_overlay_strings (it, 0);
6093 }
6094
6095 if (overlay_strings_follow_p)
6096 success_p = get_next_display_element (it);
6097 else
6098 {
6099 it->what = IT_EOB;
6100 it->position = it->current.pos;
6101 success_p = 0;
6102 }
6103 }
6104 else
6105 {
6106 handle_stop (it);
6107 return get_next_display_element (it);
6108 }
6109 }
6110 else
6111 {
6112 /* No face changes, overlays etc. in sight, so just return a
6113 character from current_buffer. */
6114 unsigned char *p;
6115
6116 /* Maybe run the redisplay end trigger hook. Performance note:
6117 This doesn't seem to cost measurable time. */
6118 if (it->redisplay_end_trigger_charpos
6119 && it->glyph_row
6120 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
6121 run_redisplay_end_trigger_hook (it);
6122
6123 /* Get the next character, maybe multibyte. */
6124 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
6125 if (it->multibyte_p && !ASCII_BYTE_P (*p))
6126 {
6127 int maxlen = ((IT_BYTEPOS (*it) >= GPT_BYTE ? ZV_BYTE : GPT_BYTE)
6128 - IT_BYTEPOS (*it));
6129 it->c = string_char_and_length (p, maxlen, &it->len);
6130 }
6131 else
6132 it->c = *p, it->len = 1;
6133
6134 /* Record what we have and where it came from. */
6135 it->what = IT_CHARACTER;;
6136 it->object = it->w->buffer;
6137 it->position = it->current.pos;
6138
6139 /* Normally we return the character found above, except when we
6140 really want to return an ellipsis for selective display. */
6141 if (it->selective)
6142 {
6143 if (it->c == '\n')
6144 {
6145 /* A value of selective > 0 means hide lines indented more
6146 than that number of columns. */
6147 if (it->selective > 0
6148 && IT_CHARPOS (*it) + 1 < ZV
6149 && indented_beyond_p (IT_CHARPOS (*it) + 1,
6150 IT_BYTEPOS (*it) + 1,
6151 (double) it->selective)) /* iftc */
6152 {
6153 success_p = next_element_from_ellipsis (it);
6154 it->dpvec_char_len = -1;
6155 }
6156 }
6157 else if (it->c == '\r' && it->selective == -1)
6158 {
6159 /* A value of selective == -1 means that everything from the
6160 CR to the end of the line is invisible, with maybe an
6161 ellipsis displayed for it. */
6162 success_p = next_element_from_ellipsis (it);
6163 it->dpvec_char_len = -1;
6164 }
6165 }
6166 }
6167
6168 /* Value is zero if end of buffer reached. */
6169 xassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
6170 return success_p;
6171 }
6172
6173
6174 /* Run the redisplay end trigger hook for IT. */
6175
6176 static void
6177 run_redisplay_end_trigger_hook (it)
6178 struct it *it;
6179 {
6180 Lisp_Object args[3];
6181
6182 /* IT->glyph_row should be non-null, i.e. we should be actually
6183 displaying something, or otherwise we should not run the hook. */
6184 xassert (it->glyph_row);
6185
6186 /* Set up hook arguments. */
6187 args[0] = Qredisplay_end_trigger_functions;
6188 args[1] = it->window;
6189 XSETINT (args[2], it->redisplay_end_trigger_charpos);
6190 it->redisplay_end_trigger_charpos = 0;
6191
6192 /* Since we are *trying* to run these functions, don't try to run
6193 them again, even if they get an error. */
6194 it->w->redisplay_end_trigger = Qnil;
6195 Frun_hook_with_args (3, args);
6196
6197 /* Notice if it changed the face of the character we are on. */
6198 handle_face_prop (it);
6199 }
6200
6201
6202 /* Deliver a composition display element. The iterator IT is already
6203 filled with composition information (done in
6204 handle_composition_prop). Value is always 1. */
6205
6206 static int
6207 next_element_from_composition (it)
6208 struct it *it;
6209 {
6210 it->what = IT_COMPOSITION;
6211 it->position = (STRINGP (it->string)
6212 ? it->current.string_pos
6213 : it->current.pos);
6214 return 1;
6215 }
6216
6217
6218 \f
6219 /***********************************************************************
6220 Moving an iterator without producing glyphs
6221 ***********************************************************************/
6222
6223 /* Check if iterator is at a position corresponding to a valid buffer
6224 position after some move_it_ call. */
6225
6226 #define IT_POS_VALID_AFTER_MOVE_P(it) \
6227 ((it)->method == GET_FROM_STRING \
6228 ? IT_STRING_CHARPOS (*it) == 0 \
6229 : 1)
6230
6231
6232 /* Move iterator IT to a specified buffer or X position within one
6233 line on the display without producing glyphs.
6234
6235 OP should be a bit mask including some or all of these bits:
6236 MOVE_TO_X: Stop on reaching x-position TO_X.
6237 MOVE_TO_POS: Stop on reaching buffer or string position TO_CHARPOS.
6238 Regardless of OP's value, stop in reaching the end of the display line.
6239
6240 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
6241 This means, in particular, that TO_X includes window's horizontal
6242 scroll amount.
6243
6244 The return value has several possible values that
6245 say what condition caused the scan to stop:
6246
6247 MOVE_POS_MATCH_OR_ZV
6248 - when TO_POS or ZV was reached.
6249
6250 MOVE_X_REACHED
6251 -when TO_X was reached before TO_POS or ZV were reached.
6252
6253 MOVE_LINE_CONTINUED
6254 - when we reached the end of the display area and the line must
6255 be continued.
6256
6257 MOVE_LINE_TRUNCATED
6258 - when we reached the end of the display area and the line is
6259 truncated.
6260
6261 MOVE_NEWLINE_OR_CR
6262 - when we stopped at a line end, i.e. a newline or a CR and selective
6263 display is on. */
6264
6265 static enum move_it_result
6266 move_it_in_display_line_to (it, to_charpos, to_x, op)
6267 struct it *it;
6268 int to_charpos, to_x, op;
6269 {
6270 enum move_it_result result = MOVE_UNDEFINED;
6271 struct glyph_row *saved_glyph_row;
6272
6273 /* Don't produce glyphs in produce_glyphs. */
6274 saved_glyph_row = it->glyph_row;
6275 it->glyph_row = NULL;
6276
6277 #define BUFFER_POS_REACHED_P() \
6278 ((op & MOVE_TO_POS) != 0 \
6279 && BUFFERP (it->object) \
6280 && IT_CHARPOS (*it) >= to_charpos \
6281 && (it->method == GET_FROM_BUFFER \
6282 || (it->method == GET_FROM_DISPLAY_VECTOR \
6283 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
6284
6285
6286 while (1)
6287 {
6288 int x, i, ascent = 0, descent = 0;
6289
6290 /* Stop if we move beyond TO_CHARPOS (after an image or stretch glyph). */
6291 if ((op & MOVE_TO_POS) != 0
6292 && BUFFERP (it->object)
6293 && it->method == GET_FROM_BUFFER
6294 && IT_CHARPOS (*it) > to_charpos)
6295 {
6296 result = MOVE_POS_MATCH_OR_ZV;
6297 break;
6298 }
6299
6300 /* Stop when ZV reached.
6301 We used to stop here when TO_CHARPOS reached as well, but that is
6302 too soon if this glyph does not fit on this line. So we handle it
6303 explicitly below. */
6304 if (!get_next_display_element (it)
6305 || (it->truncate_lines_p
6306 && BUFFER_POS_REACHED_P ()))
6307 {
6308 result = MOVE_POS_MATCH_OR_ZV;
6309 break;
6310 }
6311
6312 /* The call to produce_glyphs will get the metrics of the
6313 display element IT is loaded with. We record in x the
6314 x-position before this display element in case it does not
6315 fit on the line. */
6316 x = it->current_x;
6317
6318 /* Remember the line height so far in case the next element doesn't
6319 fit on the line. */
6320 if (!it->truncate_lines_p)
6321 {
6322 ascent = it->max_ascent;
6323 descent = it->max_descent;
6324 }
6325
6326 PRODUCE_GLYPHS (it);
6327
6328 if (it->area != TEXT_AREA)
6329 {
6330 set_iterator_to_next (it, 1);
6331 continue;
6332 }
6333
6334 /* The number of glyphs we get back in IT->nglyphs will normally
6335 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
6336 character on a terminal frame, or (iii) a line end. For the
6337 second case, IT->nglyphs - 1 padding glyphs will be present
6338 (on X frames, there is only one glyph produced for a
6339 composite character.
6340
6341 The behavior implemented below means, for continuation lines,
6342 that as many spaces of a TAB as fit on the current line are
6343 displayed there. For terminal frames, as many glyphs of a
6344 multi-glyph character are displayed in the current line, too.
6345 This is what the old redisplay code did, and we keep it that
6346 way. Under X, the whole shape of a complex character must
6347 fit on the line or it will be completely displayed in the
6348 next line.
6349
6350 Note that both for tabs and padding glyphs, all glyphs have
6351 the same width. */
6352 if (it->nglyphs)
6353 {
6354 /* More than one glyph or glyph doesn't fit on line. All
6355 glyphs have the same width. */
6356 int single_glyph_width = it->pixel_width / it->nglyphs;
6357 int new_x;
6358 int x_before_this_char = x;
6359 int hpos_before_this_char = it->hpos;
6360
6361 for (i = 0; i < it->nglyphs; ++i, x = new_x)
6362 {
6363 new_x = x + single_glyph_width;
6364
6365 /* We want to leave anything reaching TO_X to the caller. */
6366 if ((op & MOVE_TO_X) && new_x > to_x)
6367 {
6368 if (BUFFER_POS_REACHED_P ())
6369 goto buffer_pos_reached;
6370 it->current_x = x;
6371 result = MOVE_X_REACHED;
6372 break;
6373 }
6374 else if (/* Lines are continued. */
6375 !it->truncate_lines_p
6376 && (/* And glyph doesn't fit on the line. */
6377 new_x > it->last_visible_x
6378 /* Or it fits exactly and we're on a window
6379 system frame. */
6380 || (new_x == it->last_visible_x
6381 && FRAME_WINDOW_P (it->f))))
6382 {
6383 if (/* IT->hpos == 0 means the very first glyph
6384 doesn't fit on the line, e.g. a wide image. */
6385 it->hpos == 0
6386 || (new_x == it->last_visible_x
6387 && FRAME_WINDOW_P (it->f)))
6388 {
6389 ++it->hpos;
6390 it->current_x = new_x;
6391
6392 /* The character's last glyph just barely fits
6393 in this row. */
6394 if (i == it->nglyphs - 1)
6395 {
6396 /* If this is the destination position,
6397 return a position *before* it in this row,
6398 now that we know it fits in this row. */
6399 if (BUFFER_POS_REACHED_P ())
6400 {
6401 it->hpos = hpos_before_this_char;
6402 it->current_x = x_before_this_char;
6403 result = MOVE_POS_MATCH_OR_ZV;
6404 break;
6405 }
6406
6407 set_iterator_to_next (it, 1);
6408 #ifdef HAVE_WINDOW_SYSTEM
6409 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
6410 {
6411 if (!get_next_display_element (it))
6412 {
6413 result = MOVE_POS_MATCH_OR_ZV;
6414 break;
6415 }
6416 if (BUFFER_POS_REACHED_P ())
6417 {
6418 if (ITERATOR_AT_END_OF_LINE_P (it))
6419 result = MOVE_POS_MATCH_OR_ZV;
6420 else
6421 result = MOVE_LINE_CONTINUED;
6422 break;
6423 }
6424 if (ITERATOR_AT_END_OF_LINE_P (it))
6425 {
6426 result = MOVE_NEWLINE_OR_CR;
6427 break;
6428 }
6429 }
6430 #endif /* HAVE_WINDOW_SYSTEM */
6431 }
6432 }
6433 else
6434 {
6435 it->current_x = x;
6436 it->max_ascent = ascent;
6437 it->max_descent = descent;
6438 }
6439
6440 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
6441 IT_CHARPOS (*it)));
6442 result = MOVE_LINE_CONTINUED;
6443 break;
6444 }
6445 else if (BUFFER_POS_REACHED_P ())
6446 goto buffer_pos_reached;
6447 else if (new_x > it->first_visible_x)
6448 {
6449 /* Glyph is visible. Increment number of glyphs that
6450 would be displayed. */
6451 ++it->hpos;
6452 }
6453 else
6454 {
6455 /* Glyph is completely off the left margin of the display
6456 area. Nothing to do. */
6457 }
6458 }
6459
6460 if (result != MOVE_UNDEFINED)
6461 break;
6462 }
6463 else if (BUFFER_POS_REACHED_P ())
6464 {
6465 buffer_pos_reached:
6466 it->current_x = x;
6467 it->max_ascent = ascent;
6468 it->max_descent = descent;
6469 result = MOVE_POS_MATCH_OR_ZV;
6470 break;
6471 }
6472 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
6473 {
6474 /* Stop when TO_X specified and reached. This check is
6475 necessary here because of lines consisting of a line end,
6476 only. The line end will not produce any glyphs and we
6477 would never get MOVE_X_REACHED. */
6478 xassert (it->nglyphs == 0);
6479 result = MOVE_X_REACHED;
6480 break;
6481 }
6482
6483 /* Is this a line end? If yes, we're done. */
6484 if (ITERATOR_AT_END_OF_LINE_P (it))
6485 {
6486 result = MOVE_NEWLINE_OR_CR;
6487 break;
6488 }
6489
6490 /* The current display element has been consumed. Advance
6491 to the next. */
6492 set_iterator_to_next (it, 1);
6493
6494 /* Stop if lines are truncated and IT's current x-position is
6495 past the right edge of the window now. */
6496 if (it->truncate_lines_p
6497 && it->current_x >= it->last_visible_x)
6498 {
6499 #ifdef HAVE_WINDOW_SYSTEM
6500 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
6501 {
6502 if (!get_next_display_element (it)
6503 || BUFFER_POS_REACHED_P ())
6504 {
6505 result = MOVE_POS_MATCH_OR_ZV;
6506 break;
6507 }
6508 if (ITERATOR_AT_END_OF_LINE_P (it))
6509 {
6510 result = MOVE_NEWLINE_OR_CR;
6511 break;
6512 }
6513 }
6514 #endif /* HAVE_WINDOW_SYSTEM */
6515 result = MOVE_LINE_TRUNCATED;
6516 break;
6517 }
6518 }
6519
6520 #undef BUFFER_POS_REACHED_P
6521
6522 /* Restore the iterator settings altered at the beginning of this
6523 function. */
6524 it->glyph_row = saved_glyph_row;
6525 return result;
6526 }
6527
6528
6529 /* Move IT forward until it satisfies one or more of the criteria in
6530 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
6531
6532 OP is a bit-mask that specifies where to stop, and in particular,
6533 which of those four position arguments makes a difference. See the
6534 description of enum move_operation_enum.
6535
6536 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
6537 screen line, this function will set IT to the next position >
6538 TO_CHARPOS. */
6539
6540 void
6541 move_it_to (it, to_charpos, to_x, to_y, to_vpos, op)
6542 struct it *it;
6543 int to_charpos, to_x, to_y, to_vpos;
6544 int op;
6545 {
6546 enum move_it_result skip, skip2 = MOVE_X_REACHED;
6547 int line_height;
6548 int reached = 0;
6549
6550 for (;;)
6551 {
6552 if (op & MOVE_TO_VPOS)
6553 {
6554 /* If no TO_CHARPOS and no TO_X specified, stop at the
6555 start of the line TO_VPOS. */
6556 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
6557 {
6558 if (it->vpos == to_vpos)
6559 {
6560 reached = 1;
6561 break;
6562 }
6563 else
6564 skip = move_it_in_display_line_to (it, -1, -1, 0);
6565 }
6566 else
6567 {
6568 /* TO_VPOS >= 0 means stop at TO_X in the line at
6569 TO_VPOS, or at TO_POS, whichever comes first. */
6570 if (it->vpos == to_vpos)
6571 {
6572 reached = 2;
6573 break;
6574 }
6575
6576 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
6577
6578 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
6579 {
6580 reached = 3;
6581 break;
6582 }
6583 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
6584 {
6585 /* We have reached TO_X but not in the line we want. */
6586 skip = move_it_in_display_line_to (it, to_charpos,
6587 -1, MOVE_TO_POS);
6588 if (skip == MOVE_POS_MATCH_OR_ZV)
6589 {
6590 reached = 4;
6591 break;
6592 }
6593 }
6594 }
6595 }
6596 else if (op & MOVE_TO_Y)
6597 {
6598 struct it it_backup;
6599
6600 /* TO_Y specified means stop at TO_X in the line containing
6601 TO_Y---or at TO_CHARPOS if this is reached first. The
6602 problem is that we can't really tell whether the line
6603 contains TO_Y before we have completely scanned it, and
6604 this may skip past TO_X. What we do is to first scan to
6605 TO_X.
6606
6607 If TO_X is not specified, use a TO_X of zero. The reason
6608 is to make the outcome of this function more predictable.
6609 If we didn't use TO_X == 0, we would stop at the end of
6610 the line which is probably not what a caller would expect
6611 to happen. */
6612 skip = move_it_in_display_line_to (it, to_charpos,
6613 ((op & MOVE_TO_X)
6614 ? to_x : 0),
6615 (MOVE_TO_X
6616 | (op & MOVE_TO_POS)));
6617
6618 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
6619 if (skip == MOVE_POS_MATCH_OR_ZV)
6620 {
6621 reached = 5;
6622 break;
6623 }
6624
6625 /* If TO_X was reached, we would like to know whether TO_Y
6626 is in the line. This can only be said if we know the
6627 total line height which requires us to scan the rest of
6628 the line. */
6629 if (skip == MOVE_X_REACHED)
6630 {
6631 it_backup = *it;
6632 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
6633 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
6634 op & MOVE_TO_POS);
6635 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
6636 }
6637
6638 /* Now, decide whether TO_Y is in this line. */
6639 line_height = it->max_ascent + it->max_descent;
6640 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
6641
6642 if (to_y >= it->current_y
6643 && to_y < it->current_y + line_height)
6644 {
6645 if (skip == MOVE_X_REACHED)
6646 /* If TO_Y is in this line and TO_X was reached above,
6647 we scanned too far. We have to restore IT's settings
6648 to the ones before skipping. */
6649 *it = it_backup;
6650 reached = 6;
6651 }
6652 else if (skip == MOVE_X_REACHED)
6653 {
6654 skip = skip2;
6655 if (skip == MOVE_POS_MATCH_OR_ZV)
6656 reached = 7;
6657 }
6658
6659 if (reached)
6660 break;
6661 }
6662 else
6663 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
6664
6665 switch (skip)
6666 {
6667 case MOVE_POS_MATCH_OR_ZV:
6668 reached = 8;
6669 goto out;
6670
6671 case MOVE_NEWLINE_OR_CR:
6672 set_iterator_to_next (it, 1);
6673 it->continuation_lines_width = 0;
6674 break;
6675
6676 case MOVE_LINE_TRUNCATED:
6677 it->continuation_lines_width = 0;
6678 reseat_at_next_visible_line_start (it, 0);
6679 if ((op & MOVE_TO_POS) != 0
6680 && IT_CHARPOS (*it) > to_charpos)
6681 {
6682 reached = 9;
6683 goto out;
6684 }
6685 break;
6686
6687 case MOVE_LINE_CONTINUED:
6688 it->continuation_lines_width += it->current_x;
6689 break;
6690
6691 default:
6692 abort ();
6693 }
6694
6695 /* Reset/increment for the next run. */
6696 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
6697 it->current_x = it->hpos = 0;
6698 it->current_y += it->max_ascent + it->max_descent;
6699 ++it->vpos;
6700 last_height = it->max_ascent + it->max_descent;
6701 last_max_ascent = it->max_ascent;
6702 it->max_ascent = it->max_descent = 0;
6703 }
6704
6705 out:
6706
6707 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
6708 }
6709
6710
6711 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
6712
6713 If DY > 0, move IT backward at least that many pixels. DY = 0
6714 means move IT backward to the preceding line start or BEGV. This
6715 function may move over more than DY pixels if IT->current_y - DY
6716 ends up in the middle of a line; in this case IT->current_y will be
6717 set to the top of the line moved to. */
6718
6719 void
6720 move_it_vertically_backward (it, dy)
6721 struct it *it;
6722 int dy;
6723 {
6724 int nlines, h;
6725 struct it it2, it3;
6726 int start_pos;
6727
6728 move_further_back:
6729 xassert (dy >= 0);
6730
6731 start_pos = IT_CHARPOS (*it);
6732
6733 /* Estimate how many newlines we must move back. */
6734 nlines = max (1, dy / FRAME_LINE_HEIGHT (it->f));
6735
6736 /* Set the iterator's position that many lines back. */
6737 while (nlines-- && IT_CHARPOS (*it) > BEGV)
6738 back_to_previous_visible_line_start (it);
6739
6740 /* Reseat the iterator here. When moving backward, we don't want
6741 reseat to skip forward over invisible text, set up the iterator
6742 to deliver from overlay strings at the new position etc. So,
6743 use reseat_1 here. */
6744 reseat_1 (it, it->current.pos, 1);
6745
6746 /* We are now surely at a line start. */
6747 it->current_x = it->hpos = 0;
6748 it->continuation_lines_width = 0;
6749
6750 /* Move forward and see what y-distance we moved. First move to the
6751 start of the next line so that we get its height. We need this
6752 height to be able to tell whether we reached the specified
6753 y-distance. */
6754 it2 = *it;
6755 it2.max_ascent = it2.max_descent = 0;
6756 do
6757 {
6758 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
6759 MOVE_TO_POS | MOVE_TO_VPOS);
6760 }
6761 while (!IT_POS_VALID_AFTER_MOVE_P (&it2));
6762 xassert (IT_CHARPOS (*it) >= BEGV);
6763 it3 = it2;
6764
6765 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
6766 xassert (IT_CHARPOS (*it) >= BEGV);
6767 /* H is the actual vertical distance from the position in *IT
6768 and the starting position. */
6769 h = it2.current_y - it->current_y;
6770 /* NLINES is the distance in number of lines. */
6771 nlines = it2.vpos - it->vpos;
6772
6773 /* Correct IT's y and vpos position
6774 so that they are relative to the starting point. */
6775 it->vpos -= nlines;
6776 it->current_y -= h;
6777
6778 if (dy == 0)
6779 {
6780 /* DY == 0 means move to the start of the screen line. The
6781 value of nlines is > 0 if continuation lines were involved. */
6782 if (nlines > 0)
6783 move_it_by_lines (it, nlines, 1);
6784 #if 0
6785 /* I think this assert is bogus if buffer contains
6786 invisible text or images. KFS. */
6787 xassert (IT_CHARPOS (*it) <= start_pos);
6788 #endif
6789 }
6790 else
6791 {
6792 /* The y-position we try to reach, relative to *IT.
6793 Note that H has been subtracted in front of the if-statement. */
6794 int target_y = it->current_y + h - dy;
6795 int y0 = it3.current_y;
6796 int y1 = line_bottom_y (&it3);
6797 int line_height = y1 - y0;
6798
6799 /* If we did not reach target_y, try to move further backward if
6800 we can. If we moved too far backward, try to move forward. */
6801 if (target_y < it->current_y
6802 /* This is heuristic. In a window that's 3 lines high, with
6803 a line height of 13 pixels each, recentering with point
6804 on the bottom line will try to move -39/2 = 19 pixels
6805 backward. Try to avoid moving into the first line. */
6806 && (it->current_y - target_y
6807 > min (window_box_height (it->w), line_height * 2 / 3))
6808 && IT_CHARPOS (*it) > BEGV)
6809 {
6810 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
6811 target_y - it->current_y));
6812 dy = it->current_y - target_y;
6813 goto move_further_back;
6814 }
6815 else if (target_y >= it->current_y + line_height
6816 && IT_CHARPOS (*it) < ZV)
6817 {
6818 /* Should move forward by at least one line, maybe more.
6819
6820 Note: Calling move_it_by_lines can be expensive on
6821 terminal frames, where compute_motion is used (via
6822 vmotion) to do the job, when there are very long lines
6823 and truncate-lines is nil. That's the reason for
6824 treating terminal frames specially here. */
6825
6826 if (!FRAME_WINDOW_P (it->f))
6827 move_it_vertically (it, target_y - (it->current_y + line_height));
6828 else
6829 {
6830 do
6831 {
6832 move_it_by_lines (it, 1, 1);
6833 }
6834 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
6835 }
6836
6837 #if 0
6838 /* I think this assert is bogus if buffer contains
6839 invisible text or images. KFS. */
6840 xassert (IT_CHARPOS (*it) >= BEGV);
6841 #endif
6842 }
6843 }
6844 }
6845
6846
6847 /* Move IT by a specified amount of pixel lines DY. DY negative means
6848 move backwards. DY = 0 means move to start of screen line. At the
6849 end, IT will be on the start of a screen line. */
6850
6851 void
6852 move_it_vertically (it, dy)
6853 struct it *it;
6854 int dy;
6855 {
6856 if (dy <= 0)
6857 move_it_vertically_backward (it, -dy);
6858 else
6859 {
6860 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
6861 move_it_to (it, ZV, -1, it->current_y + dy, -1,
6862 MOVE_TO_POS | MOVE_TO_Y);
6863 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
6864
6865 /* If buffer ends in ZV without a newline, move to the start of
6866 the line to satisfy the post-condition. */
6867 if (IT_CHARPOS (*it) == ZV
6868 && ZV > BEGV
6869 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
6870 move_it_by_lines (it, 0, 0);
6871 }
6872 }
6873
6874
6875 /* Move iterator IT past the end of the text line it is in. */
6876
6877 void
6878 move_it_past_eol (it)
6879 struct it *it;
6880 {
6881 enum move_it_result rc;
6882
6883 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
6884 if (rc == MOVE_NEWLINE_OR_CR)
6885 set_iterator_to_next (it, 0);
6886 }
6887
6888
6889 #if 0 /* Currently not used. */
6890
6891 /* Return non-zero if some text between buffer positions START_CHARPOS
6892 and END_CHARPOS is invisible. IT->window is the window for text
6893 property lookup. */
6894
6895 static int
6896 invisible_text_between_p (it, start_charpos, end_charpos)
6897 struct it *it;
6898 int start_charpos, end_charpos;
6899 {
6900 Lisp_Object prop, limit;
6901 int invisible_found_p;
6902
6903 xassert (it != NULL && start_charpos <= end_charpos);
6904
6905 /* Is text at START invisible? */
6906 prop = Fget_char_property (make_number (start_charpos), Qinvisible,
6907 it->window);
6908 if (TEXT_PROP_MEANS_INVISIBLE (prop))
6909 invisible_found_p = 1;
6910 else
6911 {
6912 limit = Fnext_single_char_property_change (make_number (start_charpos),
6913 Qinvisible, Qnil,
6914 make_number (end_charpos));
6915 invisible_found_p = XFASTINT (limit) < end_charpos;
6916 }
6917
6918 return invisible_found_p;
6919 }
6920
6921 #endif /* 0 */
6922
6923
6924 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
6925 negative means move up. DVPOS == 0 means move to the start of the
6926 screen line. NEED_Y_P non-zero means calculate IT->current_y. If
6927 NEED_Y_P is zero, IT->current_y will be left unchanged.
6928
6929 Further optimization ideas: If we would know that IT->f doesn't use
6930 a face with proportional font, we could be faster for
6931 truncate-lines nil. */
6932
6933 void
6934 move_it_by_lines (it, dvpos, need_y_p)
6935 struct it *it;
6936 int dvpos, need_y_p;
6937 {
6938 struct position pos;
6939
6940 if (!FRAME_WINDOW_P (it->f))
6941 {
6942 struct text_pos textpos;
6943
6944 /* We can use vmotion on frames without proportional fonts. */
6945 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
6946 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
6947 reseat (it, textpos, 1);
6948 it->vpos += pos.vpos;
6949 it->current_y += pos.vpos;
6950 }
6951 else if (dvpos == 0)
6952 {
6953 /* DVPOS == 0 means move to the start of the screen line. */
6954 move_it_vertically_backward (it, 0);
6955 xassert (it->current_x == 0 && it->hpos == 0);
6956 /* Let next call to line_bottom_y calculate real line height */
6957 last_height = 0;
6958 }
6959 else if (dvpos > 0)
6960 {
6961 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
6962 if (!IT_POS_VALID_AFTER_MOVE_P (it))
6963 move_it_to (it, IT_CHARPOS (*it) + 1, -1, -1, -1, MOVE_TO_POS);
6964 }
6965 else
6966 {
6967 struct it it2;
6968 int start_charpos, i;
6969
6970 /* Start at the beginning of the screen line containing IT's
6971 position. This may actually move vertically backwards,
6972 in case of overlays, so adjust dvpos accordingly. */
6973 dvpos += it->vpos;
6974 move_it_vertically_backward (it, 0);
6975 dvpos -= it->vpos;
6976
6977 /* Go back -DVPOS visible lines and reseat the iterator there. */
6978 start_charpos = IT_CHARPOS (*it);
6979 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > BEGV; --i)
6980 back_to_previous_visible_line_start (it);
6981 reseat (it, it->current.pos, 1);
6982
6983 /* Move further back if we end up in a string or an image. */
6984 while (!IT_POS_VALID_AFTER_MOVE_P (it))
6985 {
6986 /* First try to move to start of display line. */
6987 dvpos += it->vpos;
6988 move_it_vertically_backward (it, 0);
6989 dvpos -= it->vpos;
6990 if (IT_POS_VALID_AFTER_MOVE_P (it))
6991 break;
6992 /* If start of line is still in string or image,
6993 move further back. */
6994 back_to_previous_visible_line_start (it);
6995 reseat (it, it->current.pos, 1);
6996 dvpos--;
6997 }
6998
6999 it->current_x = it->hpos = 0;
7000
7001 /* Above call may have moved too far if continuation lines
7002 are involved. Scan forward and see if it did. */
7003 it2 = *it;
7004 it2.vpos = it2.current_y = 0;
7005 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
7006 it->vpos -= it2.vpos;
7007 it->current_y -= it2.current_y;
7008 it->current_x = it->hpos = 0;
7009
7010 /* If we moved too far back, move IT some lines forward. */
7011 if (it2.vpos > -dvpos)
7012 {
7013 int delta = it2.vpos + dvpos;
7014 it2 = *it;
7015 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
7016 /* Move back again if we got too far ahead. */
7017 if (IT_CHARPOS (*it) >= start_charpos)
7018 *it = it2;
7019 }
7020 }
7021 }
7022
7023 /* Return 1 if IT points into the middle of a display vector. */
7024
7025 int
7026 in_display_vector_p (it)
7027 struct it *it;
7028 {
7029 return (it->method == GET_FROM_DISPLAY_VECTOR
7030 && it->current.dpvec_index > 0
7031 && it->dpvec + it->current.dpvec_index != it->dpend);
7032 }
7033
7034 \f
7035 /***********************************************************************
7036 Messages
7037 ***********************************************************************/
7038
7039
7040 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
7041 to *Messages*. */
7042
7043 void
7044 add_to_log (format, arg1, arg2)
7045 char *format;
7046 Lisp_Object arg1, arg2;
7047 {
7048 Lisp_Object args[3];
7049 Lisp_Object msg, fmt;
7050 char *buffer;
7051 int len;
7052 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
7053 USE_SAFE_ALLOCA;
7054
7055 /* Do nothing if called asynchronously. Inserting text into
7056 a buffer may call after-change-functions and alike and
7057 that would means running Lisp asynchronously. */
7058 if (handling_signal)
7059 return;
7060
7061 fmt = msg = Qnil;
7062 GCPRO4 (fmt, msg, arg1, arg2);
7063
7064 args[0] = fmt = build_string (format);
7065 args[1] = arg1;
7066 args[2] = arg2;
7067 msg = Fformat (3, args);
7068
7069 len = SBYTES (msg) + 1;
7070 SAFE_ALLOCA (buffer, char *, len);
7071 bcopy (SDATA (msg), buffer, len);
7072
7073 message_dolog (buffer, len - 1, 1, 0);
7074 SAFE_FREE ();
7075
7076 UNGCPRO;
7077 }
7078
7079
7080 /* Output a newline in the *Messages* buffer if "needs" one. */
7081
7082 void
7083 message_log_maybe_newline ()
7084 {
7085 if (message_log_need_newline)
7086 message_dolog ("", 0, 1, 0);
7087 }
7088
7089
7090 /* Add a string M of length NBYTES to the message log, optionally
7091 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
7092 nonzero, means interpret the contents of M as multibyte. This
7093 function calls low-level routines in order to bypass text property
7094 hooks, etc. which might not be safe to run.
7095
7096 This may GC (insert may run before/after change hooks),
7097 so the buffer M must NOT point to a Lisp string. */
7098
7099 void
7100 message_dolog (m, nbytes, nlflag, multibyte)
7101 const char *m;
7102 int nbytes, nlflag, multibyte;
7103 {
7104 if (!NILP (Vmemory_full))
7105 return;
7106
7107 if (!NILP (Vmessage_log_max))
7108 {
7109 struct buffer *oldbuf;
7110 Lisp_Object oldpoint, oldbegv, oldzv;
7111 int old_windows_or_buffers_changed = windows_or_buffers_changed;
7112 int point_at_end = 0;
7113 int zv_at_end = 0;
7114 Lisp_Object old_deactivate_mark, tem;
7115 struct gcpro gcpro1;
7116
7117 old_deactivate_mark = Vdeactivate_mark;
7118 oldbuf = current_buffer;
7119 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
7120 current_buffer->undo_list = Qt;
7121
7122 oldpoint = message_dolog_marker1;
7123 set_marker_restricted (oldpoint, make_number (PT), Qnil);
7124 oldbegv = message_dolog_marker2;
7125 set_marker_restricted (oldbegv, make_number (BEGV), Qnil);
7126 oldzv = message_dolog_marker3;
7127 set_marker_restricted (oldzv, make_number (ZV), Qnil);
7128 GCPRO1 (old_deactivate_mark);
7129
7130 if (PT == Z)
7131 point_at_end = 1;
7132 if (ZV == Z)
7133 zv_at_end = 1;
7134
7135 BEGV = BEG;
7136 BEGV_BYTE = BEG_BYTE;
7137 ZV = Z;
7138 ZV_BYTE = Z_BYTE;
7139 TEMP_SET_PT_BOTH (Z, Z_BYTE);
7140
7141 /* Insert the string--maybe converting multibyte to single byte
7142 or vice versa, so that all the text fits the buffer. */
7143 if (multibyte
7144 && NILP (current_buffer->enable_multibyte_characters))
7145 {
7146 int i, c, char_bytes;
7147 unsigned char work[1];
7148
7149 /* Convert a multibyte string to single-byte
7150 for the *Message* buffer. */
7151 for (i = 0; i < nbytes; i += char_bytes)
7152 {
7153 c = string_char_and_length (m + i, nbytes - i, &char_bytes);
7154 work[0] = (SINGLE_BYTE_CHAR_P (c)
7155 ? c
7156 : multibyte_char_to_unibyte (c, Qnil));
7157 insert_1_both (work, 1, 1, 1, 0, 0);
7158 }
7159 }
7160 else if (! multibyte
7161 && ! NILP (current_buffer->enable_multibyte_characters))
7162 {
7163 int i, c, char_bytes;
7164 unsigned char *msg = (unsigned char *) m;
7165 unsigned char str[MAX_MULTIBYTE_LENGTH];
7166 /* Convert a single-byte string to multibyte
7167 for the *Message* buffer. */
7168 for (i = 0; i < nbytes; i++)
7169 {
7170 c = unibyte_char_to_multibyte (msg[i]);
7171 char_bytes = CHAR_STRING (c, str);
7172 insert_1_both (str, 1, char_bytes, 1, 0, 0);
7173 }
7174 }
7175 else if (nbytes)
7176 insert_1 (m, nbytes, 1, 0, 0);
7177
7178 if (nlflag)
7179 {
7180 int this_bol, this_bol_byte, prev_bol, prev_bol_byte, dup;
7181 insert_1 ("\n", 1, 1, 0, 0);
7182
7183 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
7184 this_bol = PT;
7185 this_bol_byte = PT_BYTE;
7186
7187 /* See if this line duplicates the previous one.
7188 If so, combine duplicates. */
7189 if (this_bol > BEG)
7190 {
7191 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
7192 prev_bol = PT;
7193 prev_bol_byte = PT_BYTE;
7194
7195 dup = message_log_check_duplicate (prev_bol, prev_bol_byte,
7196 this_bol, this_bol_byte);
7197 if (dup)
7198 {
7199 del_range_both (prev_bol, prev_bol_byte,
7200 this_bol, this_bol_byte, 0);
7201 if (dup > 1)
7202 {
7203 char dupstr[40];
7204 int duplen;
7205
7206 /* If you change this format, don't forget to also
7207 change message_log_check_duplicate. */
7208 sprintf (dupstr, " [%d times]", dup);
7209 duplen = strlen (dupstr);
7210 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
7211 insert_1 (dupstr, duplen, 1, 0, 1);
7212 }
7213 }
7214 }
7215
7216 /* If we have more than the desired maximum number of lines
7217 in the *Messages* buffer now, delete the oldest ones.
7218 This is safe because we don't have undo in this buffer. */
7219
7220 if (NATNUMP (Vmessage_log_max))
7221 {
7222 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
7223 -XFASTINT (Vmessage_log_max) - 1, 0);
7224 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
7225 }
7226 }
7227 BEGV = XMARKER (oldbegv)->charpos;
7228 BEGV_BYTE = marker_byte_position (oldbegv);
7229
7230 if (zv_at_end)
7231 {
7232 ZV = Z;
7233 ZV_BYTE = Z_BYTE;
7234 }
7235 else
7236 {
7237 ZV = XMARKER (oldzv)->charpos;
7238 ZV_BYTE = marker_byte_position (oldzv);
7239 }
7240
7241 if (point_at_end)
7242 TEMP_SET_PT_BOTH (Z, Z_BYTE);
7243 else
7244 /* We can't do Fgoto_char (oldpoint) because it will run some
7245 Lisp code. */
7246 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
7247 XMARKER (oldpoint)->bytepos);
7248
7249 UNGCPRO;
7250 unchain_marker (XMARKER (oldpoint));
7251 unchain_marker (XMARKER (oldbegv));
7252 unchain_marker (XMARKER (oldzv));
7253
7254 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
7255 set_buffer_internal (oldbuf);
7256 if (NILP (tem))
7257 windows_or_buffers_changed = old_windows_or_buffers_changed;
7258 message_log_need_newline = !nlflag;
7259 Vdeactivate_mark = old_deactivate_mark;
7260 }
7261 }
7262
7263
7264 /* We are at the end of the buffer after just having inserted a newline.
7265 (Note: We depend on the fact we won't be crossing the gap.)
7266 Check to see if the most recent message looks a lot like the previous one.
7267 Return 0 if different, 1 if the new one should just replace it, or a
7268 value N > 1 if we should also append " [N times]". */
7269
7270 static int
7271 message_log_check_duplicate (prev_bol, prev_bol_byte, this_bol, this_bol_byte)
7272 int prev_bol, this_bol;
7273 int prev_bol_byte, this_bol_byte;
7274 {
7275 int i;
7276 int len = Z_BYTE - 1 - this_bol_byte;
7277 int seen_dots = 0;
7278 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
7279 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
7280
7281 for (i = 0; i < len; i++)
7282 {
7283 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
7284 seen_dots = 1;
7285 if (p1[i] != p2[i])
7286 return seen_dots;
7287 }
7288 p1 += len;
7289 if (*p1 == '\n')
7290 return 2;
7291 if (*p1++ == ' ' && *p1++ == '[')
7292 {
7293 int n = 0;
7294 while (*p1 >= '0' && *p1 <= '9')
7295 n = n * 10 + *p1++ - '0';
7296 if (strncmp (p1, " times]\n", 8) == 0)
7297 return n+1;
7298 }
7299 return 0;
7300 }
7301 \f
7302
7303 /* Display an echo area message M with a specified length of NBYTES
7304 bytes. The string may include null characters. If M is 0, clear
7305 out any existing message, and let the mini-buffer text show
7306 through.
7307
7308 This may GC, so the buffer M must NOT point to a Lisp string. */
7309
7310 void
7311 message2 (m, nbytes, multibyte)
7312 const char *m;
7313 int nbytes;
7314 int multibyte;
7315 {
7316 /* First flush out any partial line written with print. */
7317 message_log_maybe_newline ();
7318 if (m)
7319 message_dolog (m, nbytes, 1, multibyte);
7320 message2_nolog (m, nbytes, multibyte);
7321 }
7322
7323
7324 /* The non-logging counterpart of message2. */
7325
7326 void
7327 message2_nolog (m, nbytes, multibyte)
7328 const char *m;
7329 int nbytes, multibyte;
7330 {
7331 struct frame *sf = SELECTED_FRAME ();
7332 message_enable_multibyte = multibyte;
7333
7334 if (noninteractive)
7335 {
7336 if (noninteractive_need_newline)
7337 putc ('\n', stderr);
7338 noninteractive_need_newline = 0;
7339 if (m)
7340 fwrite (m, nbytes, 1, stderr);
7341 if (cursor_in_echo_area == 0)
7342 fprintf (stderr, "\n");
7343 fflush (stderr);
7344 }
7345 /* A null message buffer means that the frame hasn't really been
7346 initialized yet. Error messages get reported properly by
7347 cmd_error, so this must be just an informative message; toss it. */
7348 else if (INTERACTIVE
7349 && sf->glyphs_initialized_p
7350 && FRAME_MESSAGE_BUF (sf))
7351 {
7352 Lisp_Object mini_window;
7353 struct frame *f;
7354
7355 /* Get the frame containing the mini-buffer
7356 that the selected frame is using. */
7357 mini_window = FRAME_MINIBUF_WINDOW (sf);
7358 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
7359
7360 FRAME_SAMPLE_VISIBILITY (f);
7361 if (FRAME_VISIBLE_P (sf)
7362 && ! FRAME_VISIBLE_P (f))
7363 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
7364
7365 if (m)
7366 {
7367 set_message (m, Qnil, nbytes, multibyte);
7368 if (minibuffer_auto_raise)
7369 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
7370 }
7371 else
7372 clear_message (1, 1);
7373
7374 do_pending_window_change (0);
7375 echo_area_display (1);
7376 do_pending_window_change (0);
7377 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
7378 (*frame_up_to_date_hook) (f);
7379 }
7380 }
7381
7382
7383 /* Display an echo area message M with a specified length of NBYTES
7384 bytes. The string may include null characters. If M is not a
7385 string, clear out any existing message, and let the mini-buffer
7386 text show through.
7387
7388 This function cancels echoing. */
7389
7390 void
7391 message3 (m, nbytes, multibyte)
7392 Lisp_Object m;
7393 int nbytes;
7394 int multibyte;
7395 {
7396 struct gcpro gcpro1;
7397
7398 GCPRO1 (m);
7399 clear_message (1,1);
7400 cancel_echoing ();
7401
7402 /* First flush out any partial line written with print. */
7403 message_log_maybe_newline ();
7404 if (STRINGP (m))
7405 {
7406 char *buffer;
7407 USE_SAFE_ALLOCA;
7408
7409 SAFE_ALLOCA (buffer, char *, nbytes);
7410 bcopy (SDATA (m), buffer, nbytes);
7411 message_dolog (buffer, nbytes, 1, multibyte);
7412 SAFE_FREE ();
7413 }
7414 message3_nolog (m, nbytes, multibyte);
7415
7416 UNGCPRO;
7417 }
7418
7419
7420 /* The non-logging version of message3.
7421 This does not cancel echoing, because it is used for echoing.
7422 Perhaps we need to make a separate function for echoing
7423 and make this cancel echoing. */
7424
7425 void
7426 message3_nolog (m, nbytes, multibyte)
7427 Lisp_Object m;
7428 int nbytes, multibyte;
7429 {
7430 struct frame *sf = SELECTED_FRAME ();
7431 message_enable_multibyte = multibyte;
7432
7433 if (noninteractive)
7434 {
7435 if (noninteractive_need_newline)
7436 putc ('\n', stderr);
7437 noninteractive_need_newline = 0;
7438 if (STRINGP (m))
7439 fwrite (SDATA (m), nbytes, 1, stderr);
7440 if (cursor_in_echo_area == 0)
7441 fprintf (stderr, "\n");
7442 fflush (stderr);
7443 }
7444 /* A null message buffer means that the frame hasn't really been
7445 initialized yet. Error messages get reported properly by
7446 cmd_error, so this must be just an informative message; toss it. */
7447 else if (INTERACTIVE
7448 && sf->glyphs_initialized_p
7449 && FRAME_MESSAGE_BUF (sf))
7450 {
7451 Lisp_Object mini_window;
7452 Lisp_Object frame;
7453 struct frame *f;
7454
7455 /* Get the frame containing the mini-buffer
7456 that the selected frame is using. */
7457 mini_window = FRAME_MINIBUF_WINDOW (sf);
7458 frame = XWINDOW (mini_window)->frame;
7459 f = XFRAME (frame);
7460
7461 FRAME_SAMPLE_VISIBILITY (f);
7462 if (FRAME_VISIBLE_P (sf)
7463 && !FRAME_VISIBLE_P (f))
7464 Fmake_frame_visible (frame);
7465
7466 if (STRINGP (m) && SCHARS (m) > 0)
7467 {
7468 set_message (NULL, m, nbytes, multibyte);
7469 if (minibuffer_auto_raise)
7470 Fraise_frame (frame);
7471 /* Assume we are not echoing.
7472 (If we are, echo_now will override this.) */
7473 echo_message_buffer = Qnil;
7474 }
7475 else
7476 clear_message (1, 1);
7477
7478 do_pending_window_change (0);
7479 echo_area_display (1);
7480 do_pending_window_change (0);
7481 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
7482 (*frame_up_to_date_hook) (f);
7483 }
7484 }
7485
7486
7487 /* Display a null-terminated echo area message M. If M is 0, clear
7488 out any existing message, and let the mini-buffer text show through.
7489
7490 The buffer M must continue to exist until after the echo area gets
7491 cleared or some other message gets displayed there. Do not pass
7492 text that is stored in a Lisp string. Do not pass text in a buffer
7493 that was alloca'd. */
7494
7495 void
7496 message1 (m)
7497 char *m;
7498 {
7499 message2 (m, (m ? strlen (m) : 0), 0);
7500 }
7501
7502
7503 /* The non-logging counterpart of message1. */
7504
7505 void
7506 message1_nolog (m)
7507 char *m;
7508 {
7509 message2_nolog (m, (m ? strlen (m) : 0), 0);
7510 }
7511
7512 /* Display a message M which contains a single %s
7513 which gets replaced with STRING. */
7514
7515 void
7516 message_with_string (m, string, log)
7517 char *m;
7518 Lisp_Object string;
7519 int log;
7520 {
7521 CHECK_STRING (string);
7522
7523 if (noninteractive)
7524 {
7525 if (m)
7526 {
7527 if (noninteractive_need_newline)
7528 putc ('\n', stderr);
7529 noninteractive_need_newline = 0;
7530 fprintf (stderr, m, SDATA (string));
7531 if (cursor_in_echo_area == 0)
7532 fprintf (stderr, "\n");
7533 fflush (stderr);
7534 }
7535 }
7536 else if (INTERACTIVE)
7537 {
7538 /* The frame whose minibuffer we're going to display the message on.
7539 It may be larger than the selected frame, so we need
7540 to use its buffer, not the selected frame's buffer. */
7541 Lisp_Object mini_window;
7542 struct frame *f, *sf = SELECTED_FRAME ();
7543
7544 /* Get the frame containing the minibuffer
7545 that the selected frame is using. */
7546 mini_window = FRAME_MINIBUF_WINDOW (sf);
7547 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
7548
7549 /* A null message buffer means that the frame hasn't really been
7550 initialized yet. Error messages get reported properly by
7551 cmd_error, so this must be just an informative message; toss it. */
7552 if (FRAME_MESSAGE_BUF (f))
7553 {
7554 Lisp_Object args[2], message;
7555 struct gcpro gcpro1, gcpro2;
7556
7557 args[0] = build_string (m);
7558 args[1] = message = string;
7559 GCPRO2 (args[0], message);
7560 gcpro1.nvars = 2;
7561
7562 message = Fformat (2, args);
7563
7564 if (log)
7565 message3 (message, SBYTES (message), STRING_MULTIBYTE (message));
7566 else
7567 message3_nolog (message, SBYTES (message), STRING_MULTIBYTE (message));
7568
7569 UNGCPRO;
7570
7571 /* Print should start at the beginning of the message
7572 buffer next time. */
7573 message_buf_print = 0;
7574 }
7575 }
7576 }
7577
7578
7579 /* Dump an informative message to the minibuf. If M is 0, clear out
7580 any existing message, and let the mini-buffer text show through. */
7581
7582 /* VARARGS 1 */
7583 void
7584 message (m, a1, a2, a3)
7585 char *m;
7586 EMACS_INT a1, a2, a3;
7587 {
7588 if (noninteractive)
7589 {
7590 if (m)
7591 {
7592 if (noninteractive_need_newline)
7593 putc ('\n', stderr);
7594 noninteractive_need_newline = 0;
7595 fprintf (stderr, m, a1, a2, a3);
7596 if (cursor_in_echo_area == 0)
7597 fprintf (stderr, "\n");
7598 fflush (stderr);
7599 }
7600 }
7601 else if (INTERACTIVE)
7602 {
7603 /* The frame whose mini-buffer we're going to display the message
7604 on. It may be larger than the selected frame, so we need to
7605 use its buffer, not the selected frame's buffer. */
7606 Lisp_Object mini_window;
7607 struct frame *f, *sf = SELECTED_FRAME ();
7608
7609 /* Get the frame containing the mini-buffer
7610 that the selected frame is using. */
7611 mini_window = FRAME_MINIBUF_WINDOW (sf);
7612 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
7613
7614 /* A null message buffer means that the frame hasn't really been
7615 initialized yet. Error messages get reported properly by
7616 cmd_error, so this must be just an informative message; toss
7617 it. */
7618 if (FRAME_MESSAGE_BUF (f))
7619 {
7620 if (m)
7621 {
7622 int len;
7623 #ifdef NO_ARG_ARRAY
7624 char *a[3];
7625 a[0] = (char *) a1;
7626 a[1] = (char *) a2;
7627 a[2] = (char *) a3;
7628
7629 len = doprnt (FRAME_MESSAGE_BUF (f),
7630 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
7631 #else
7632 len = doprnt (FRAME_MESSAGE_BUF (f),
7633 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3,
7634 (char **) &a1);
7635 #endif /* NO_ARG_ARRAY */
7636
7637 message2 (FRAME_MESSAGE_BUF (f), len, 0);
7638 }
7639 else
7640 message1 (0);
7641
7642 /* Print should start at the beginning of the message
7643 buffer next time. */
7644 message_buf_print = 0;
7645 }
7646 }
7647 }
7648
7649
7650 /* The non-logging version of message. */
7651
7652 void
7653 message_nolog (m, a1, a2, a3)
7654 char *m;
7655 EMACS_INT a1, a2, a3;
7656 {
7657 Lisp_Object old_log_max;
7658 old_log_max = Vmessage_log_max;
7659 Vmessage_log_max = Qnil;
7660 message (m, a1, a2, a3);
7661 Vmessage_log_max = old_log_max;
7662 }
7663
7664
7665 /* Display the current message in the current mini-buffer. This is
7666 only called from error handlers in process.c, and is not time
7667 critical. */
7668
7669 void
7670 update_echo_area ()
7671 {
7672 if (!NILP (echo_area_buffer[0]))
7673 {
7674 Lisp_Object string;
7675 string = Fcurrent_message ();
7676 message3 (string, SBYTES (string),
7677 !NILP (current_buffer->enable_multibyte_characters));
7678 }
7679 }
7680
7681
7682 /* Make sure echo area buffers in `echo_buffers' are live.
7683 If they aren't, make new ones. */
7684
7685 static void
7686 ensure_echo_area_buffers ()
7687 {
7688 int i;
7689
7690 for (i = 0; i < 2; ++i)
7691 if (!BUFFERP (echo_buffer[i])
7692 || NILP (XBUFFER (echo_buffer[i])->name))
7693 {
7694 char name[30];
7695 Lisp_Object old_buffer;
7696 int j;
7697
7698 old_buffer = echo_buffer[i];
7699 sprintf (name, " *Echo Area %d*", i);
7700 echo_buffer[i] = Fget_buffer_create (build_string (name));
7701 XBUFFER (echo_buffer[i])->truncate_lines = Qnil;
7702
7703 for (j = 0; j < 2; ++j)
7704 if (EQ (old_buffer, echo_area_buffer[j]))
7705 echo_area_buffer[j] = echo_buffer[i];
7706 }
7707 }
7708
7709
7710 /* Call FN with args A1..A4 with either the current or last displayed
7711 echo_area_buffer as current buffer.
7712
7713 WHICH zero means use the current message buffer
7714 echo_area_buffer[0]. If that is nil, choose a suitable buffer
7715 from echo_buffer[] and clear it.
7716
7717 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
7718 suitable buffer from echo_buffer[] and clear it.
7719
7720 Value is what FN returns. */
7721
7722 static int
7723 with_echo_area_buffer (w, which, fn, a1, a2, a3, a4)
7724 struct window *w;
7725 int which;
7726 int (*fn) P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
7727 EMACS_INT a1;
7728 Lisp_Object a2;
7729 EMACS_INT a3, a4;
7730 {
7731 Lisp_Object buffer;
7732 int this_one, the_other, clear_buffer_p, rc;
7733 int count = SPECPDL_INDEX ();
7734
7735 /* If buffers aren't live, make new ones. */
7736 ensure_echo_area_buffers ();
7737
7738 clear_buffer_p = 0;
7739
7740 if (which == 0)
7741 this_one = 0, the_other = 1;
7742 else if (which > 0)
7743 this_one = 1, the_other = 0;
7744
7745 /* Choose a suitable buffer from echo_buffer[] is we don't
7746 have one. */
7747 if (NILP (echo_area_buffer[this_one]))
7748 {
7749 echo_area_buffer[this_one]
7750 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
7751 ? echo_buffer[the_other]
7752 : echo_buffer[this_one]);
7753 clear_buffer_p = 1;
7754 }
7755
7756 buffer = echo_area_buffer[this_one];
7757
7758 /* Don't get confused by reusing the buffer used for echoing
7759 for a different purpose. */
7760 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
7761 cancel_echoing ();
7762
7763 record_unwind_protect (unwind_with_echo_area_buffer,
7764 with_echo_area_buffer_unwind_data (w));
7765
7766 /* Make the echo area buffer current. Note that for display
7767 purposes, it is not necessary that the displayed window's buffer
7768 == current_buffer, except for text property lookup. So, let's
7769 only set that buffer temporarily here without doing a full
7770 Fset_window_buffer. We must also change w->pointm, though,
7771 because otherwise an assertions in unshow_buffer fails, and Emacs
7772 aborts. */
7773 set_buffer_internal_1 (XBUFFER (buffer));
7774 if (w)
7775 {
7776 w->buffer = buffer;
7777 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
7778 }
7779
7780 current_buffer->undo_list = Qt;
7781 current_buffer->read_only = Qnil;
7782 specbind (Qinhibit_read_only, Qt);
7783 specbind (Qinhibit_modification_hooks, Qt);
7784
7785 if (clear_buffer_p && Z > BEG)
7786 del_range (BEG, Z);
7787
7788 xassert (BEGV >= BEG);
7789 xassert (ZV <= Z && ZV >= BEGV);
7790
7791 rc = fn (a1, a2, a3, a4);
7792
7793 xassert (BEGV >= BEG);
7794 xassert (ZV <= Z && ZV >= BEGV);
7795
7796 unbind_to (count, Qnil);
7797 return rc;
7798 }
7799
7800
7801 /* Save state that should be preserved around the call to the function
7802 FN called in with_echo_area_buffer. */
7803
7804 static Lisp_Object
7805 with_echo_area_buffer_unwind_data (w)
7806 struct window *w;
7807 {
7808 int i = 0;
7809 Lisp_Object vector;
7810
7811 /* Reduce consing by keeping one vector in
7812 Vwith_echo_area_save_vector. */
7813 vector = Vwith_echo_area_save_vector;
7814 Vwith_echo_area_save_vector = Qnil;
7815
7816 if (NILP (vector))
7817 vector = Fmake_vector (make_number (7), Qnil);
7818
7819 XSETBUFFER (AREF (vector, i), current_buffer); ++i;
7820 AREF (vector, i) = Vdeactivate_mark, ++i;
7821 AREF (vector, i) = make_number (windows_or_buffers_changed), ++i;
7822
7823 if (w)
7824 {
7825 XSETWINDOW (AREF (vector, i), w); ++i;
7826 AREF (vector, i) = w->buffer; ++i;
7827 AREF (vector, i) = make_number (XMARKER (w->pointm)->charpos); ++i;
7828 AREF (vector, i) = make_number (XMARKER (w->pointm)->bytepos); ++i;
7829 }
7830 else
7831 {
7832 int end = i + 4;
7833 for (; i < end; ++i)
7834 AREF (vector, i) = Qnil;
7835 }
7836
7837 xassert (i == ASIZE (vector));
7838 return vector;
7839 }
7840
7841
7842 /* Restore global state from VECTOR which was created by
7843 with_echo_area_buffer_unwind_data. */
7844
7845 static Lisp_Object
7846 unwind_with_echo_area_buffer (vector)
7847 Lisp_Object vector;
7848 {
7849 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
7850 Vdeactivate_mark = AREF (vector, 1);
7851 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
7852
7853 if (WINDOWP (AREF (vector, 3)))
7854 {
7855 struct window *w;
7856 Lisp_Object buffer, charpos, bytepos;
7857
7858 w = XWINDOW (AREF (vector, 3));
7859 buffer = AREF (vector, 4);
7860 charpos = AREF (vector, 5);
7861 bytepos = AREF (vector, 6);
7862
7863 w->buffer = buffer;
7864 set_marker_both (w->pointm, buffer,
7865 XFASTINT (charpos), XFASTINT (bytepos));
7866 }
7867
7868 Vwith_echo_area_save_vector = vector;
7869 return Qnil;
7870 }
7871
7872
7873 /* Set up the echo area for use by print functions. MULTIBYTE_P
7874 non-zero means we will print multibyte. */
7875
7876 void
7877 setup_echo_area_for_printing (multibyte_p)
7878 int multibyte_p;
7879 {
7880 /* If we can't find an echo area any more, exit. */
7881 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
7882 Fkill_emacs (Qnil);
7883
7884 ensure_echo_area_buffers ();
7885
7886 if (!message_buf_print)
7887 {
7888 /* A message has been output since the last time we printed.
7889 Choose a fresh echo area buffer. */
7890 if (EQ (echo_area_buffer[1], echo_buffer[0]))
7891 echo_area_buffer[0] = echo_buffer[1];
7892 else
7893 echo_area_buffer[0] = echo_buffer[0];
7894
7895 /* Switch to that buffer and clear it. */
7896 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
7897 current_buffer->truncate_lines = Qnil;
7898
7899 if (Z > BEG)
7900 {
7901 int count = SPECPDL_INDEX ();
7902 specbind (Qinhibit_read_only, Qt);
7903 /* Note that undo recording is always disabled. */
7904 del_range (BEG, Z);
7905 unbind_to (count, Qnil);
7906 }
7907 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
7908
7909 /* Set up the buffer for the multibyteness we need. */
7910 if (multibyte_p
7911 != !NILP (current_buffer->enable_multibyte_characters))
7912 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
7913
7914 /* Raise the frame containing the echo area. */
7915 if (minibuffer_auto_raise)
7916 {
7917 struct frame *sf = SELECTED_FRAME ();
7918 Lisp_Object mini_window;
7919 mini_window = FRAME_MINIBUF_WINDOW (sf);
7920 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
7921 }
7922
7923 message_log_maybe_newline ();
7924 message_buf_print = 1;
7925 }
7926 else
7927 {
7928 if (NILP (echo_area_buffer[0]))
7929 {
7930 if (EQ (echo_area_buffer[1], echo_buffer[0]))
7931 echo_area_buffer[0] = echo_buffer[1];
7932 else
7933 echo_area_buffer[0] = echo_buffer[0];
7934 }
7935
7936 if (current_buffer != XBUFFER (echo_area_buffer[0]))
7937 {
7938 /* Someone switched buffers between print requests. */
7939 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
7940 current_buffer->truncate_lines = Qnil;
7941 }
7942 }
7943 }
7944
7945
7946 /* Display an echo area message in window W. Value is non-zero if W's
7947 height is changed. If display_last_displayed_message_p is
7948 non-zero, display the message that was last displayed, otherwise
7949 display the current message. */
7950
7951 static int
7952 display_echo_area (w)
7953 struct window *w;
7954 {
7955 int i, no_message_p, window_height_changed_p, count;
7956
7957 /* Temporarily disable garbage collections while displaying the echo
7958 area. This is done because a GC can print a message itself.
7959 That message would modify the echo area buffer's contents while a
7960 redisplay of the buffer is going on, and seriously confuse
7961 redisplay. */
7962 count = inhibit_garbage_collection ();
7963
7964 /* If there is no message, we must call display_echo_area_1
7965 nevertheless because it resizes the window. But we will have to
7966 reset the echo_area_buffer in question to nil at the end because
7967 with_echo_area_buffer will sets it to an empty buffer. */
7968 i = display_last_displayed_message_p ? 1 : 0;
7969 no_message_p = NILP (echo_area_buffer[i]);
7970
7971 window_height_changed_p
7972 = with_echo_area_buffer (w, display_last_displayed_message_p,
7973 display_echo_area_1,
7974 (EMACS_INT) w, Qnil, 0, 0);
7975
7976 if (no_message_p)
7977 echo_area_buffer[i] = Qnil;
7978
7979 unbind_to (count, Qnil);
7980 return window_height_changed_p;
7981 }
7982
7983
7984 /* Helper for display_echo_area. Display the current buffer which
7985 contains the current echo area message in window W, a mini-window,
7986 a pointer to which is passed in A1. A2..A4 are currently not used.
7987 Change the height of W so that all of the message is displayed.
7988 Value is non-zero if height of W was changed. */
7989
7990 static int
7991 display_echo_area_1 (a1, a2, a3, a4)
7992 EMACS_INT a1;
7993 Lisp_Object a2;
7994 EMACS_INT a3, a4;
7995 {
7996 struct window *w = (struct window *) a1;
7997 Lisp_Object window;
7998 struct text_pos start;
7999 int window_height_changed_p = 0;
8000
8001 /* Do this before displaying, so that we have a large enough glyph
8002 matrix for the display. If we can't get enough space for the
8003 whole text, display the last N lines. That works by setting w->start. */
8004 window_height_changed_p = resize_mini_window (w, 0);
8005
8006 /* Use the starting position chosen by resize_mini_window. */
8007 SET_TEXT_POS_FROM_MARKER (start, w->start);
8008
8009 /* Display. */
8010 clear_glyph_matrix (w->desired_matrix);
8011 XSETWINDOW (window, w);
8012 try_window (window, start, 0);
8013
8014 return window_height_changed_p;
8015 }
8016
8017
8018 /* Resize the echo area window to exactly the size needed for the
8019 currently displayed message, if there is one. If a mini-buffer
8020 is active, don't shrink it. */
8021
8022 void
8023 resize_echo_area_exactly ()
8024 {
8025 if (BUFFERP (echo_area_buffer[0])
8026 && WINDOWP (echo_area_window))
8027 {
8028 struct window *w = XWINDOW (echo_area_window);
8029 int resized_p;
8030 Lisp_Object resize_exactly;
8031
8032 if (minibuf_level == 0)
8033 resize_exactly = Qt;
8034 else
8035 resize_exactly = Qnil;
8036
8037 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
8038 (EMACS_INT) w, resize_exactly, 0, 0);
8039 if (resized_p)
8040 {
8041 ++windows_or_buffers_changed;
8042 ++update_mode_lines;
8043 redisplay_internal (0);
8044 }
8045 }
8046 }
8047
8048
8049 /* Callback function for with_echo_area_buffer, when used from
8050 resize_echo_area_exactly. A1 contains a pointer to the window to
8051 resize, EXACTLY non-nil means resize the mini-window exactly to the
8052 size of the text displayed. A3 and A4 are not used. Value is what
8053 resize_mini_window returns. */
8054
8055 static int
8056 resize_mini_window_1 (a1, exactly, a3, a4)
8057 EMACS_INT a1;
8058 Lisp_Object exactly;
8059 EMACS_INT a3, a4;
8060 {
8061 return resize_mini_window ((struct window *) a1, !NILP (exactly));
8062 }
8063
8064
8065 /* Resize mini-window W to fit the size of its contents. EXACT:P
8066 means size the window exactly to the size needed. Otherwise, it's
8067 only enlarged until W's buffer is empty.
8068
8069 Set W->start to the right place to begin display. If the whole
8070 contents fit, start at the beginning. Otherwise, start so as
8071 to make the end of the contents appear. This is particularly
8072 important for y-or-n-p, but seems desirable generally.
8073
8074 Value is non-zero if the window height has been changed. */
8075
8076 int
8077 resize_mini_window (w, exact_p)
8078 struct window *w;
8079 int exact_p;
8080 {
8081 struct frame *f = XFRAME (w->frame);
8082 int window_height_changed_p = 0;
8083
8084 xassert (MINI_WINDOW_P (w));
8085
8086 /* By default, start display at the beginning. */
8087 set_marker_both (w->start, w->buffer,
8088 BUF_BEGV (XBUFFER (w->buffer)),
8089 BUF_BEGV_BYTE (XBUFFER (w->buffer)));
8090
8091 /* Don't resize windows while redisplaying a window; it would
8092 confuse redisplay functions when the size of the window they are
8093 displaying changes from under them. Such a resizing can happen,
8094 for instance, when which-func prints a long message while
8095 we are running fontification-functions. We're running these
8096 functions with safe_call which binds inhibit-redisplay to t. */
8097 if (!NILP (Vinhibit_redisplay))
8098 return 0;
8099
8100 /* Nil means don't try to resize. */
8101 if (NILP (Vresize_mini_windows)
8102 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
8103 return 0;
8104
8105 if (!FRAME_MINIBUF_ONLY_P (f))
8106 {
8107 struct it it;
8108 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
8109 int total_height = WINDOW_TOTAL_LINES (root) + WINDOW_TOTAL_LINES (w);
8110 int height, max_height;
8111 int unit = FRAME_LINE_HEIGHT (f);
8112 struct text_pos start;
8113 struct buffer *old_current_buffer = NULL;
8114
8115 if (current_buffer != XBUFFER (w->buffer))
8116 {
8117 old_current_buffer = current_buffer;
8118 set_buffer_internal (XBUFFER (w->buffer));
8119 }
8120
8121 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
8122
8123 /* Compute the max. number of lines specified by the user. */
8124 if (FLOATP (Vmax_mini_window_height))
8125 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_LINES (f);
8126 else if (INTEGERP (Vmax_mini_window_height))
8127 max_height = XINT (Vmax_mini_window_height);
8128 else
8129 max_height = total_height / 4;
8130
8131 /* Correct that max. height if it's bogus. */
8132 max_height = max (1, max_height);
8133 max_height = min (total_height, max_height);
8134
8135 /* Find out the height of the text in the window. */
8136 if (it.truncate_lines_p)
8137 height = 1;
8138 else
8139 {
8140 last_height = 0;
8141 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
8142 if (it.max_ascent == 0 && it.max_descent == 0)
8143 height = it.current_y + last_height;
8144 else
8145 height = it.current_y + it.max_ascent + it.max_descent;
8146 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
8147 height = (height + unit - 1) / unit;
8148 }
8149
8150 /* Compute a suitable window start. */
8151 if (height > max_height)
8152 {
8153 height = max_height;
8154 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
8155 move_it_vertically_backward (&it, (height - 1) * unit);
8156 start = it.current.pos;
8157 }
8158 else
8159 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
8160 SET_MARKER_FROM_TEXT_POS (w->start, start);
8161
8162 if (EQ (Vresize_mini_windows, Qgrow_only))
8163 {
8164 /* Let it grow only, until we display an empty message, in which
8165 case the window shrinks again. */
8166 if (height > WINDOW_TOTAL_LINES (w))
8167 {
8168 int old_height = WINDOW_TOTAL_LINES (w);
8169 freeze_window_starts (f, 1);
8170 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
8171 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8172 }
8173 else if (height < WINDOW_TOTAL_LINES (w)
8174 && (exact_p || BEGV == ZV))
8175 {
8176 int old_height = WINDOW_TOTAL_LINES (w);
8177 freeze_window_starts (f, 0);
8178 shrink_mini_window (w);
8179 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8180 }
8181 }
8182 else
8183 {
8184 /* Always resize to exact size needed. */
8185 if (height > WINDOW_TOTAL_LINES (w))
8186 {
8187 int old_height = WINDOW_TOTAL_LINES (w);
8188 freeze_window_starts (f, 1);
8189 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
8190 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8191 }
8192 else if (height < WINDOW_TOTAL_LINES (w))
8193 {
8194 int old_height = WINDOW_TOTAL_LINES (w);
8195 freeze_window_starts (f, 0);
8196 shrink_mini_window (w);
8197
8198 if (height)
8199 {
8200 freeze_window_starts (f, 1);
8201 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
8202 }
8203
8204 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8205 }
8206 }
8207
8208 if (old_current_buffer)
8209 set_buffer_internal (old_current_buffer);
8210 }
8211
8212 return window_height_changed_p;
8213 }
8214
8215
8216 /* Value is the current message, a string, or nil if there is no
8217 current message. */
8218
8219 Lisp_Object
8220 current_message ()
8221 {
8222 Lisp_Object msg;
8223
8224 if (NILP (echo_area_buffer[0]))
8225 msg = Qnil;
8226 else
8227 {
8228 with_echo_area_buffer (0, 0, current_message_1,
8229 (EMACS_INT) &msg, Qnil, 0, 0);
8230 if (NILP (msg))
8231 echo_area_buffer[0] = Qnil;
8232 }
8233
8234 return msg;
8235 }
8236
8237
8238 static int
8239 current_message_1 (a1, a2, a3, a4)
8240 EMACS_INT a1;
8241 Lisp_Object a2;
8242 EMACS_INT a3, a4;
8243 {
8244 Lisp_Object *msg = (Lisp_Object *) a1;
8245
8246 if (Z > BEG)
8247 *msg = make_buffer_string (BEG, Z, 1);
8248 else
8249 *msg = Qnil;
8250 return 0;
8251 }
8252
8253
8254 /* Push the current message on Vmessage_stack for later restauration
8255 by restore_message. Value is non-zero if the current message isn't
8256 empty. This is a relatively infrequent operation, so it's not
8257 worth optimizing. */
8258
8259 int
8260 push_message ()
8261 {
8262 Lisp_Object msg;
8263 msg = current_message ();
8264 Vmessage_stack = Fcons (msg, Vmessage_stack);
8265 return STRINGP (msg);
8266 }
8267
8268
8269 /* Restore message display from the top of Vmessage_stack. */
8270
8271 void
8272 restore_message ()
8273 {
8274 Lisp_Object msg;
8275
8276 xassert (CONSP (Vmessage_stack));
8277 msg = XCAR (Vmessage_stack);
8278 if (STRINGP (msg))
8279 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
8280 else
8281 message3_nolog (msg, 0, 0);
8282 }
8283
8284
8285 /* Handler for record_unwind_protect calling pop_message. */
8286
8287 Lisp_Object
8288 pop_message_unwind (dummy)
8289 Lisp_Object dummy;
8290 {
8291 pop_message ();
8292 return Qnil;
8293 }
8294
8295 /* Pop the top-most entry off Vmessage_stack. */
8296
8297 void
8298 pop_message ()
8299 {
8300 xassert (CONSP (Vmessage_stack));
8301 Vmessage_stack = XCDR (Vmessage_stack);
8302 }
8303
8304
8305 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
8306 exits. If the stack is not empty, we have a missing pop_message
8307 somewhere. */
8308
8309 void
8310 check_message_stack ()
8311 {
8312 if (!NILP (Vmessage_stack))
8313 abort ();
8314 }
8315
8316
8317 /* Truncate to NCHARS what will be displayed in the echo area the next
8318 time we display it---but don't redisplay it now. */
8319
8320 void
8321 truncate_echo_area (nchars)
8322 int nchars;
8323 {
8324 if (nchars == 0)
8325 echo_area_buffer[0] = Qnil;
8326 /* A null message buffer means that the frame hasn't really been
8327 initialized yet. Error messages get reported properly by
8328 cmd_error, so this must be just an informative message; toss it. */
8329 else if (!noninteractive
8330 && INTERACTIVE
8331 && !NILP (echo_area_buffer[0]))
8332 {
8333 struct frame *sf = SELECTED_FRAME ();
8334 if (FRAME_MESSAGE_BUF (sf))
8335 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
8336 }
8337 }
8338
8339
8340 /* Helper function for truncate_echo_area. Truncate the current
8341 message to at most NCHARS characters. */
8342
8343 static int
8344 truncate_message_1 (nchars, a2, a3, a4)
8345 EMACS_INT nchars;
8346 Lisp_Object a2;
8347 EMACS_INT a3, a4;
8348 {
8349 if (BEG + nchars < Z)
8350 del_range (BEG + nchars, Z);
8351 if (Z == BEG)
8352 echo_area_buffer[0] = Qnil;
8353 return 0;
8354 }
8355
8356
8357 /* Set the current message to a substring of S or STRING.
8358
8359 If STRING is a Lisp string, set the message to the first NBYTES
8360 bytes from STRING. NBYTES zero means use the whole string. If
8361 STRING is multibyte, the message will be displayed multibyte.
8362
8363 If S is not null, set the message to the first LEN bytes of S. LEN
8364 zero means use the whole string. MULTIBYTE_P non-zero means S is
8365 multibyte. Display the message multibyte in that case.
8366
8367 Doesn't GC, as with_echo_area_buffer binds Qinhibit_modification_hooks
8368 to t before calling set_message_1 (which calls insert).
8369 */
8370
8371 void
8372 set_message (s, string, nbytes, multibyte_p)
8373 const char *s;
8374 Lisp_Object string;
8375 int nbytes, multibyte_p;
8376 {
8377 message_enable_multibyte
8378 = ((s && multibyte_p)
8379 || (STRINGP (string) && STRING_MULTIBYTE (string)));
8380
8381 with_echo_area_buffer (0, 0, set_message_1,
8382 (EMACS_INT) s, string, nbytes, multibyte_p);
8383 message_buf_print = 0;
8384 help_echo_showing_p = 0;
8385 }
8386
8387
8388 /* Helper function for set_message. Arguments have the same meaning
8389 as there, with A1 corresponding to S and A2 corresponding to STRING
8390 This function is called with the echo area buffer being
8391 current. */
8392
8393 static int
8394 set_message_1 (a1, a2, nbytes, multibyte_p)
8395 EMACS_INT a1;
8396 Lisp_Object a2;
8397 EMACS_INT nbytes, multibyte_p;
8398 {
8399 const char *s = (const char *) a1;
8400 Lisp_Object string = a2;
8401
8402 /* Change multibyteness of the echo buffer appropriately. */
8403 if (message_enable_multibyte
8404 != !NILP (current_buffer->enable_multibyte_characters))
8405 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
8406
8407 current_buffer->truncate_lines = message_truncate_lines ? Qt : Qnil;
8408
8409 /* Insert new message at BEG. */
8410 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
8411 Ferase_buffer ();
8412
8413 if (STRINGP (string))
8414 {
8415 int nchars;
8416
8417 if (nbytes == 0)
8418 nbytes = SBYTES (string);
8419 nchars = string_byte_to_char (string, nbytes);
8420
8421 /* This function takes care of single/multibyte conversion. We
8422 just have to ensure that the echo area buffer has the right
8423 setting of enable_multibyte_characters. */
8424 insert_from_string (string, 0, 0, nchars, nbytes, 1);
8425 }
8426 else if (s)
8427 {
8428 if (nbytes == 0)
8429 nbytes = strlen (s);
8430
8431 if (multibyte_p && NILP (current_buffer->enable_multibyte_characters))
8432 {
8433 /* Convert from multi-byte to single-byte. */
8434 int i, c, n;
8435 unsigned char work[1];
8436
8437 /* Convert a multibyte string to single-byte. */
8438 for (i = 0; i < nbytes; i += n)
8439 {
8440 c = string_char_and_length (s + i, nbytes - i, &n);
8441 work[0] = (SINGLE_BYTE_CHAR_P (c)
8442 ? c
8443 : multibyte_char_to_unibyte (c, Qnil));
8444 insert_1_both (work, 1, 1, 1, 0, 0);
8445 }
8446 }
8447 else if (!multibyte_p
8448 && !NILP (current_buffer->enable_multibyte_characters))
8449 {
8450 /* Convert from single-byte to multi-byte. */
8451 int i, c, n;
8452 const unsigned char *msg = (const unsigned char *) s;
8453 unsigned char str[MAX_MULTIBYTE_LENGTH];
8454
8455 /* Convert a single-byte string to multibyte. */
8456 for (i = 0; i < nbytes; i++)
8457 {
8458 c = unibyte_char_to_multibyte (msg[i]);
8459 n = CHAR_STRING (c, str);
8460 insert_1_both (str, 1, n, 1, 0, 0);
8461 }
8462 }
8463 else
8464 insert_1 (s, nbytes, 1, 0, 0);
8465 }
8466
8467 return 0;
8468 }
8469
8470
8471 /* Clear messages. CURRENT_P non-zero means clear the current
8472 message. LAST_DISPLAYED_P non-zero means clear the message
8473 last displayed. */
8474
8475 void
8476 clear_message (current_p, last_displayed_p)
8477 int current_p, last_displayed_p;
8478 {
8479 if (current_p)
8480 {
8481 echo_area_buffer[0] = Qnil;
8482 message_cleared_p = 1;
8483 }
8484
8485 if (last_displayed_p)
8486 echo_area_buffer[1] = Qnil;
8487
8488 message_buf_print = 0;
8489 }
8490
8491 /* Clear garbaged frames.
8492
8493 This function is used where the old redisplay called
8494 redraw_garbaged_frames which in turn called redraw_frame which in
8495 turn called clear_frame. The call to clear_frame was a source of
8496 flickering. I believe a clear_frame is not necessary. It should
8497 suffice in the new redisplay to invalidate all current matrices,
8498 and ensure a complete redisplay of all windows. */
8499
8500 static void
8501 clear_garbaged_frames ()
8502 {
8503 if (frame_garbaged)
8504 {
8505 Lisp_Object tail, frame;
8506 int changed_count = 0;
8507
8508 FOR_EACH_FRAME (tail, frame)
8509 {
8510 struct frame *f = XFRAME (frame);
8511
8512 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
8513 {
8514 if (f->resized_p)
8515 {
8516 Fredraw_frame (frame);
8517 f->force_flush_display_p = 1;
8518 }
8519 clear_current_matrices (f);
8520 changed_count++;
8521 f->garbaged = 0;
8522 f->resized_p = 0;
8523 }
8524 }
8525
8526 frame_garbaged = 0;
8527 if (changed_count)
8528 ++windows_or_buffers_changed;
8529 }
8530 }
8531
8532
8533 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
8534 is non-zero update selected_frame. Value is non-zero if the
8535 mini-windows height has been changed. */
8536
8537 static int
8538 echo_area_display (update_frame_p)
8539 int update_frame_p;
8540 {
8541 Lisp_Object mini_window;
8542 struct window *w;
8543 struct frame *f;
8544 int window_height_changed_p = 0;
8545 struct frame *sf = SELECTED_FRAME ();
8546
8547 mini_window = FRAME_MINIBUF_WINDOW (sf);
8548 w = XWINDOW (mini_window);
8549 f = XFRAME (WINDOW_FRAME (w));
8550
8551 /* Don't display if frame is invisible or not yet initialized. */
8552 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
8553 return 0;
8554
8555 /* The terminal frame is used as the first Emacs frame on the Mac OS. */
8556 #ifndef MAC_OS8
8557 #ifdef HAVE_WINDOW_SYSTEM
8558 /* When Emacs starts, selected_frame may be a visible terminal
8559 frame, even if we run under a window system. If we let this
8560 through, a message would be displayed on the terminal. */
8561 if (EQ (selected_frame, Vterminal_frame)
8562 && !NILP (Vwindow_system))
8563 return 0;
8564 #endif /* HAVE_WINDOW_SYSTEM */
8565 #endif
8566
8567 /* Redraw garbaged frames. */
8568 if (frame_garbaged)
8569 clear_garbaged_frames ();
8570
8571 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
8572 {
8573 echo_area_window = mini_window;
8574 window_height_changed_p = display_echo_area (w);
8575 w->must_be_updated_p = 1;
8576
8577 /* Update the display, unless called from redisplay_internal.
8578 Also don't update the screen during redisplay itself. The
8579 update will happen at the end of redisplay, and an update
8580 here could cause confusion. */
8581 if (update_frame_p && !redisplaying_p)
8582 {
8583 int n = 0;
8584
8585 /* If the display update has been interrupted by pending
8586 input, update mode lines in the frame. Due to the
8587 pending input, it might have been that redisplay hasn't
8588 been called, so that mode lines above the echo area are
8589 garbaged. This looks odd, so we prevent it here. */
8590 if (!display_completed)
8591 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
8592
8593 if (window_height_changed_p
8594 /* Don't do this if Emacs is shutting down. Redisplay
8595 needs to run hooks. */
8596 && !NILP (Vrun_hooks))
8597 {
8598 /* Must update other windows. Likewise as in other
8599 cases, don't let this update be interrupted by
8600 pending input. */
8601 int count = SPECPDL_INDEX ();
8602 specbind (Qredisplay_dont_pause, Qt);
8603 windows_or_buffers_changed = 1;
8604 redisplay_internal (0);
8605 unbind_to (count, Qnil);
8606 }
8607 else if (FRAME_WINDOW_P (f) && n == 0)
8608 {
8609 /* Window configuration is the same as before.
8610 Can do with a display update of the echo area,
8611 unless we displayed some mode lines. */
8612 update_single_window (w, 1);
8613 rif->flush_display (f);
8614 }
8615 else
8616 update_frame (f, 1, 1);
8617
8618 /* If cursor is in the echo area, make sure that the next
8619 redisplay displays the minibuffer, so that the cursor will
8620 be replaced with what the minibuffer wants. */
8621 if (cursor_in_echo_area)
8622 ++windows_or_buffers_changed;
8623 }
8624 }
8625 else if (!EQ (mini_window, selected_window))
8626 windows_or_buffers_changed++;
8627
8628 /* The current message is now also the last one displayed. */
8629 echo_area_buffer[1] = echo_area_buffer[0];
8630
8631 /* Prevent redisplay optimization in redisplay_internal by resetting
8632 this_line_start_pos. This is done because the mini-buffer now
8633 displays the message instead of its buffer text. */
8634 if (EQ (mini_window, selected_window))
8635 CHARPOS (this_line_start_pos) = 0;
8636
8637 return window_height_changed_p;
8638 }
8639
8640
8641 \f
8642 /***********************************************************************
8643 Mode Lines and Frame Titles
8644 ***********************************************************************/
8645
8646 /* A buffer for constructing non-propertized mode-line strings and
8647 frame titles in it; allocated from the heap in init_xdisp and
8648 resized as needed in store_mode_line_noprop_char. */
8649
8650 static char *mode_line_noprop_buf;
8651
8652 /* The buffer's end, and a current output position in it. */
8653
8654 static char *mode_line_noprop_buf_end;
8655 static char *mode_line_noprop_ptr;
8656
8657 #define MODE_LINE_NOPROP_LEN(start) \
8658 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
8659
8660 static enum {
8661 MODE_LINE_DISPLAY = 0,
8662 MODE_LINE_TITLE,
8663 MODE_LINE_NOPROP,
8664 MODE_LINE_STRING
8665 } mode_line_target;
8666
8667 /* Alist that caches the results of :propertize.
8668 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
8669 static Lisp_Object mode_line_proptrans_alist;
8670
8671 /* List of strings making up the mode-line. */
8672 static Lisp_Object mode_line_string_list;
8673
8674 /* Base face property when building propertized mode line string. */
8675 static Lisp_Object mode_line_string_face;
8676 static Lisp_Object mode_line_string_face_prop;
8677
8678
8679 /* Unwind data for mode line strings */
8680
8681 static Lisp_Object Vmode_line_unwind_vector;
8682
8683 static Lisp_Object
8684 format_mode_line_unwind_data (obuf, save_proptrans)
8685 struct buffer *obuf;
8686 {
8687 Lisp_Object vector;
8688
8689 /* Reduce consing by keeping one vector in
8690 Vwith_echo_area_save_vector. */
8691 vector = Vmode_line_unwind_vector;
8692 Vmode_line_unwind_vector = Qnil;
8693
8694 if (NILP (vector))
8695 vector = Fmake_vector (make_number (7), Qnil);
8696
8697 AREF (vector, 0) = make_number (mode_line_target);
8698 AREF (vector, 1) = make_number (MODE_LINE_NOPROP_LEN (0));
8699 AREF (vector, 2) = mode_line_string_list;
8700 AREF (vector, 3) = (save_proptrans ? mode_line_proptrans_alist : Qt);
8701 AREF (vector, 4) = mode_line_string_face;
8702 AREF (vector, 5) = mode_line_string_face_prop;
8703
8704 if (obuf)
8705 XSETBUFFER (AREF (vector, 6), obuf);
8706 else
8707 AREF (vector, 6) = Qnil;
8708
8709 return vector;
8710 }
8711
8712 static Lisp_Object
8713 unwind_format_mode_line (vector)
8714 Lisp_Object vector;
8715 {
8716 mode_line_target = XINT (AREF (vector, 0));
8717 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
8718 mode_line_string_list = AREF (vector, 2);
8719 if (! EQ (AREF (vector, 3), Qt))
8720 mode_line_proptrans_alist = AREF (vector, 3);
8721 mode_line_string_face = AREF (vector, 4);
8722 mode_line_string_face_prop = AREF (vector, 5);
8723
8724 if (!NILP (AREF (vector, 6)))
8725 {
8726 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
8727 AREF (vector, 6) = Qnil;
8728 }
8729
8730 Vmode_line_unwind_vector = vector;
8731 return Qnil;
8732 }
8733
8734
8735 /* Store a single character C for the frame title in mode_line_noprop_buf.
8736 Re-allocate mode_line_noprop_buf if necessary. */
8737
8738 static void
8739 #ifdef PROTOTYPES
8740 store_mode_line_noprop_char (char c)
8741 #else
8742 store_mode_line_noprop_char (c)
8743 char c;
8744 #endif
8745 {
8746 /* If output position has reached the end of the allocated buffer,
8747 double the buffer's size. */
8748 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
8749 {
8750 int len = MODE_LINE_NOPROP_LEN (0);
8751 int new_size = 2 * len * sizeof *mode_line_noprop_buf;
8752 mode_line_noprop_buf = (char *) xrealloc (mode_line_noprop_buf, new_size);
8753 mode_line_noprop_buf_end = mode_line_noprop_buf + new_size;
8754 mode_line_noprop_ptr = mode_line_noprop_buf + len;
8755 }
8756
8757 *mode_line_noprop_ptr++ = c;
8758 }
8759
8760
8761 /* Store part of a frame title in mode_line_noprop_buf, beginning at
8762 mode_line_noprop_ptr. STR is the string to store. Do not copy
8763 characters that yield more columns than PRECISION; PRECISION <= 0
8764 means copy the whole string. Pad with spaces until FIELD_WIDTH
8765 number of characters have been copied; FIELD_WIDTH <= 0 means don't
8766 pad. Called from display_mode_element when it is used to build a
8767 frame title. */
8768
8769 static int
8770 store_mode_line_noprop (str, field_width, precision)
8771 const unsigned char *str;
8772 int field_width, precision;
8773 {
8774 int n = 0;
8775 int dummy, nbytes;
8776
8777 /* Copy at most PRECISION chars from STR. */
8778 nbytes = strlen (str);
8779 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
8780 while (nbytes--)
8781 store_mode_line_noprop_char (*str++);
8782
8783 /* Fill up with spaces until FIELD_WIDTH reached. */
8784 while (field_width > 0
8785 && n < field_width)
8786 {
8787 store_mode_line_noprop_char (' ');
8788 ++n;
8789 }
8790
8791 return n;
8792 }
8793
8794 /***********************************************************************
8795 Frame Titles
8796 ***********************************************************************/
8797
8798 #ifdef HAVE_WINDOW_SYSTEM
8799
8800 /* Set the title of FRAME, if it has changed. The title format is
8801 Vicon_title_format if FRAME is iconified, otherwise it is
8802 frame_title_format. */
8803
8804 static void
8805 x_consider_frame_title (frame)
8806 Lisp_Object frame;
8807 {
8808 struct frame *f = XFRAME (frame);
8809
8810 if (FRAME_WINDOW_P (f)
8811 || FRAME_MINIBUF_ONLY_P (f)
8812 || f->explicit_name)
8813 {
8814 /* Do we have more than one visible frame on this X display? */
8815 Lisp_Object tail;
8816 Lisp_Object fmt;
8817 int title_start;
8818 char *title;
8819 int len;
8820 struct it it;
8821 int count = SPECPDL_INDEX ();
8822
8823 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
8824 {
8825 Lisp_Object other_frame = XCAR (tail);
8826 struct frame *tf = XFRAME (other_frame);
8827
8828 if (tf != f
8829 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
8830 && !FRAME_MINIBUF_ONLY_P (tf)
8831 && !EQ (other_frame, tip_frame)
8832 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
8833 break;
8834 }
8835
8836 /* Set global variable indicating that multiple frames exist. */
8837 multiple_frames = CONSP (tail);
8838
8839 /* Switch to the buffer of selected window of the frame. Set up
8840 mode_line_target so that display_mode_element will output into
8841 mode_line_noprop_buf; then display the title. */
8842 record_unwind_protect (unwind_format_mode_line,
8843 format_mode_line_unwind_data (current_buffer, 0));
8844
8845 set_buffer_internal_1 (XBUFFER (XWINDOW (f->selected_window)->buffer));
8846 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
8847
8848 mode_line_target = MODE_LINE_TITLE;
8849 title_start = MODE_LINE_NOPROP_LEN (0);
8850 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
8851 NULL, DEFAULT_FACE_ID);
8852 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
8853 len = MODE_LINE_NOPROP_LEN (title_start);
8854 title = mode_line_noprop_buf + title_start;
8855 unbind_to (count, Qnil);
8856
8857 /* Set the title only if it's changed. This avoids consing in
8858 the common case where it hasn't. (If it turns out that we've
8859 already wasted too much time by walking through the list with
8860 display_mode_element, then we might need to optimize at a
8861 higher level than this.) */
8862 if (! STRINGP (f->name)
8863 || SBYTES (f->name) != len
8864 || bcmp (title, SDATA (f->name), len) != 0)
8865 x_implicitly_set_name (f, make_string (title, len), Qnil);
8866 }
8867 }
8868
8869 #endif /* not HAVE_WINDOW_SYSTEM */
8870
8871
8872
8873 \f
8874 /***********************************************************************
8875 Menu Bars
8876 ***********************************************************************/
8877
8878
8879 /* Prepare for redisplay by updating menu-bar item lists when
8880 appropriate. This can call eval. */
8881
8882 void
8883 prepare_menu_bars ()
8884 {
8885 int all_windows;
8886 struct gcpro gcpro1, gcpro2;
8887 struct frame *f;
8888 Lisp_Object tooltip_frame;
8889
8890 #ifdef HAVE_WINDOW_SYSTEM
8891 tooltip_frame = tip_frame;
8892 #else
8893 tooltip_frame = Qnil;
8894 #endif
8895
8896 /* Update all frame titles based on their buffer names, etc. We do
8897 this before the menu bars so that the buffer-menu will show the
8898 up-to-date frame titles. */
8899 #ifdef HAVE_WINDOW_SYSTEM
8900 if (windows_or_buffers_changed || update_mode_lines)
8901 {
8902 Lisp_Object tail, frame;
8903
8904 FOR_EACH_FRAME (tail, frame)
8905 {
8906 f = XFRAME (frame);
8907 if (!EQ (frame, tooltip_frame)
8908 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
8909 x_consider_frame_title (frame);
8910 }
8911 }
8912 #endif /* HAVE_WINDOW_SYSTEM */
8913
8914 /* Update the menu bar item lists, if appropriate. This has to be
8915 done before any actual redisplay or generation of display lines. */
8916 all_windows = (update_mode_lines
8917 || buffer_shared > 1
8918 || windows_or_buffers_changed);
8919 if (all_windows)
8920 {
8921 Lisp_Object tail, frame;
8922 int count = SPECPDL_INDEX ();
8923
8924 record_unwind_save_match_data ();
8925
8926 FOR_EACH_FRAME (tail, frame)
8927 {
8928 f = XFRAME (frame);
8929
8930 /* Ignore tooltip frame. */
8931 if (EQ (frame, tooltip_frame))
8932 continue;
8933
8934 /* If a window on this frame changed size, report that to
8935 the user and clear the size-change flag. */
8936 if (FRAME_WINDOW_SIZES_CHANGED (f))
8937 {
8938 Lisp_Object functions;
8939
8940 /* Clear flag first in case we get an error below. */
8941 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
8942 functions = Vwindow_size_change_functions;
8943 GCPRO2 (tail, functions);
8944
8945 while (CONSP (functions))
8946 {
8947 call1 (XCAR (functions), frame);
8948 functions = XCDR (functions);
8949 }
8950 UNGCPRO;
8951 }
8952
8953 GCPRO1 (tail);
8954 update_menu_bar (f, 0);
8955 #ifdef HAVE_WINDOW_SYSTEM
8956 update_tool_bar (f, 0);
8957 #ifdef MAC_OS
8958 mac_update_title_bar (f, 0);
8959 #endif
8960 #endif
8961 UNGCPRO;
8962 }
8963
8964 unbind_to (count, Qnil);
8965 }
8966 else
8967 {
8968 struct frame *sf = SELECTED_FRAME ();
8969 update_menu_bar (sf, 1);
8970 #ifdef HAVE_WINDOW_SYSTEM
8971 update_tool_bar (sf, 1);
8972 #ifdef MAC_OS
8973 mac_update_title_bar (sf, 1);
8974 #endif
8975 #endif
8976 }
8977
8978 /* Motif needs this. See comment in xmenu.c. Turn it off when
8979 pending_menu_activation is not defined. */
8980 #ifdef USE_X_TOOLKIT
8981 pending_menu_activation = 0;
8982 #endif
8983 }
8984
8985
8986 /* Update the menu bar item list for frame F. This has to be done
8987 before we start to fill in any display lines, because it can call
8988 eval.
8989
8990 If SAVE_MATCH_DATA is non-zero, we must save and restore it here. */
8991
8992 static void
8993 update_menu_bar (f, save_match_data)
8994 struct frame *f;
8995 int save_match_data;
8996 {
8997 Lisp_Object window;
8998 register struct window *w;
8999
9000 /* If called recursively during a menu update, do nothing. This can
9001 happen when, for instance, an activate-menubar-hook causes a
9002 redisplay. */
9003 if (inhibit_menubar_update)
9004 return;
9005
9006 window = FRAME_SELECTED_WINDOW (f);
9007 w = XWINDOW (window);
9008
9009 #if 0 /* The if statement below this if statement used to include the
9010 condition !NILP (w->update_mode_line), rather than using
9011 update_mode_lines directly, and this if statement may have
9012 been added to make that condition work. Now the if
9013 statement below matches its comment, this isn't needed. */
9014 if (update_mode_lines)
9015 w->update_mode_line = Qt;
9016 #endif
9017
9018 if (FRAME_WINDOW_P (f)
9019 ?
9020 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
9021 || defined (USE_GTK)
9022 FRAME_EXTERNAL_MENU_BAR (f)
9023 #else
9024 FRAME_MENU_BAR_LINES (f) > 0
9025 #endif
9026 : FRAME_MENU_BAR_LINES (f) > 0)
9027 {
9028 /* If the user has switched buffers or windows, we need to
9029 recompute to reflect the new bindings. But we'll
9030 recompute when update_mode_lines is set too; that means
9031 that people can use force-mode-line-update to request
9032 that the menu bar be recomputed. The adverse effect on
9033 the rest of the redisplay algorithm is about the same as
9034 windows_or_buffers_changed anyway. */
9035 if (windows_or_buffers_changed
9036 /* This used to test w->update_mode_line, but we believe
9037 there is no need to recompute the menu in that case. */
9038 || update_mode_lines
9039 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
9040 < BUF_MODIFF (XBUFFER (w->buffer)))
9041 != !NILP (w->last_had_star))
9042 || ((!NILP (Vtransient_mark_mode)
9043 && !NILP (XBUFFER (w->buffer)->mark_active))
9044 != !NILP (w->region_showing)))
9045 {
9046 struct buffer *prev = current_buffer;
9047 int count = SPECPDL_INDEX ();
9048
9049 specbind (Qinhibit_menubar_update, Qt);
9050
9051 set_buffer_internal_1 (XBUFFER (w->buffer));
9052 if (save_match_data)
9053 record_unwind_save_match_data ();
9054 if (NILP (Voverriding_local_map_menu_flag))
9055 {
9056 specbind (Qoverriding_terminal_local_map, Qnil);
9057 specbind (Qoverriding_local_map, Qnil);
9058 }
9059
9060 /* Run the Lucid hook. */
9061 safe_run_hooks (Qactivate_menubar_hook);
9062
9063 /* If it has changed current-menubar from previous value,
9064 really recompute the menu-bar from the value. */
9065 if (! NILP (Vlucid_menu_bar_dirty_flag))
9066 call0 (Qrecompute_lucid_menubar);
9067
9068 safe_run_hooks (Qmenu_bar_update_hook);
9069 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
9070
9071 /* Redisplay the menu bar in case we changed it. */
9072 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
9073 || defined (USE_GTK)
9074 if (FRAME_WINDOW_P (f))
9075 {
9076 #ifdef MAC_OS
9077 /* All frames on Mac OS share the same menubar. So only
9078 the selected frame should be allowed to set it. */
9079 if (f == SELECTED_FRAME ())
9080 #endif
9081 set_frame_menubar (f, 0, 0);
9082 }
9083 else
9084 /* On a terminal screen, the menu bar is an ordinary screen
9085 line, and this makes it get updated. */
9086 w->update_mode_line = Qt;
9087 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || MAC_OS || USE_GTK) */
9088 /* In the non-toolkit version, the menu bar is an ordinary screen
9089 line, and this makes it get updated. */
9090 w->update_mode_line = Qt;
9091 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || MAC_OS || USE_GTK) */
9092
9093 unbind_to (count, Qnil);
9094 set_buffer_internal_1 (prev);
9095 }
9096 }
9097 }
9098
9099
9100 \f
9101 /***********************************************************************
9102 Output Cursor
9103 ***********************************************************************/
9104
9105 #ifdef HAVE_WINDOW_SYSTEM
9106
9107 /* EXPORT:
9108 Nominal cursor position -- where to draw output.
9109 HPOS and VPOS are window relative glyph matrix coordinates.
9110 X and Y are window relative pixel coordinates. */
9111
9112 struct cursor_pos output_cursor;
9113
9114
9115 /* EXPORT:
9116 Set the global variable output_cursor to CURSOR. All cursor
9117 positions are relative to updated_window. */
9118
9119 void
9120 set_output_cursor (cursor)
9121 struct cursor_pos *cursor;
9122 {
9123 output_cursor.hpos = cursor->hpos;
9124 output_cursor.vpos = cursor->vpos;
9125 output_cursor.x = cursor->x;
9126 output_cursor.y = cursor->y;
9127 }
9128
9129
9130 /* EXPORT for RIF:
9131 Set a nominal cursor position.
9132
9133 HPOS and VPOS are column/row positions in a window glyph matrix. X
9134 and Y are window text area relative pixel positions.
9135
9136 If this is done during an update, updated_window will contain the
9137 window that is being updated and the position is the future output
9138 cursor position for that window. If updated_window is null, use
9139 selected_window and display the cursor at the given position. */
9140
9141 void
9142 x_cursor_to (vpos, hpos, y, x)
9143 int vpos, hpos, y, x;
9144 {
9145 struct window *w;
9146
9147 /* If updated_window is not set, work on selected_window. */
9148 if (updated_window)
9149 w = updated_window;
9150 else
9151 w = XWINDOW (selected_window);
9152
9153 /* Set the output cursor. */
9154 output_cursor.hpos = hpos;
9155 output_cursor.vpos = vpos;
9156 output_cursor.x = x;
9157 output_cursor.y = y;
9158
9159 /* If not called as part of an update, really display the cursor.
9160 This will also set the cursor position of W. */
9161 if (updated_window == NULL)
9162 {
9163 BLOCK_INPUT;
9164 display_and_set_cursor (w, 1, hpos, vpos, x, y);
9165 if (rif->flush_display_optional)
9166 rif->flush_display_optional (SELECTED_FRAME ());
9167 UNBLOCK_INPUT;
9168 }
9169 }
9170
9171 #endif /* HAVE_WINDOW_SYSTEM */
9172
9173 \f
9174 /***********************************************************************
9175 Tool-bars
9176 ***********************************************************************/
9177
9178 #ifdef HAVE_WINDOW_SYSTEM
9179
9180 /* Where the mouse was last time we reported a mouse event. */
9181
9182 FRAME_PTR last_mouse_frame;
9183
9184 /* Tool-bar item index of the item on which a mouse button was pressed
9185 or -1. */
9186
9187 int last_tool_bar_item;
9188
9189
9190 /* Update the tool-bar item list for frame F. This has to be done
9191 before we start to fill in any display lines. Called from
9192 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
9193 and restore it here. */
9194
9195 static void
9196 update_tool_bar (f, save_match_data)
9197 struct frame *f;
9198 int save_match_data;
9199 {
9200 #ifdef USE_GTK
9201 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
9202 #else
9203 int do_update = WINDOWP (f->tool_bar_window)
9204 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0;
9205 #endif
9206
9207 if (do_update)
9208 {
9209 Lisp_Object window;
9210 struct window *w;
9211
9212 window = FRAME_SELECTED_WINDOW (f);
9213 w = XWINDOW (window);
9214
9215 /* If the user has switched buffers or windows, we need to
9216 recompute to reflect the new bindings. But we'll
9217 recompute when update_mode_lines is set too; that means
9218 that people can use force-mode-line-update to request
9219 that the menu bar be recomputed. The adverse effect on
9220 the rest of the redisplay algorithm is about the same as
9221 windows_or_buffers_changed anyway. */
9222 if (windows_or_buffers_changed
9223 || !NILP (w->update_mode_line)
9224 || update_mode_lines
9225 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
9226 < BUF_MODIFF (XBUFFER (w->buffer)))
9227 != !NILP (w->last_had_star))
9228 || ((!NILP (Vtransient_mark_mode)
9229 && !NILP (XBUFFER (w->buffer)->mark_active))
9230 != !NILP (w->region_showing)))
9231 {
9232 struct buffer *prev = current_buffer;
9233 int count = SPECPDL_INDEX ();
9234 Lisp_Object new_tool_bar;
9235 int new_n_tool_bar;
9236 struct gcpro gcpro1;
9237
9238 /* Set current_buffer to the buffer of the selected
9239 window of the frame, so that we get the right local
9240 keymaps. */
9241 set_buffer_internal_1 (XBUFFER (w->buffer));
9242
9243 /* Save match data, if we must. */
9244 if (save_match_data)
9245 record_unwind_save_match_data ();
9246
9247 /* Make sure that we don't accidentally use bogus keymaps. */
9248 if (NILP (Voverriding_local_map_menu_flag))
9249 {
9250 specbind (Qoverriding_terminal_local_map, Qnil);
9251 specbind (Qoverriding_local_map, Qnil);
9252 }
9253
9254 GCPRO1 (new_tool_bar);
9255
9256 /* Build desired tool-bar items from keymaps. */
9257 new_tool_bar = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
9258 &new_n_tool_bar);
9259
9260 /* Redisplay the tool-bar if we changed it. */
9261 if (NILP (Fequal (new_tool_bar, f->tool_bar_items)))
9262 {
9263 /* Redisplay that happens asynchronously due to an expose event
9264 may access f->tool_bar_items. Make sure we update both
9265 variables within BLOCK_INPUT so no such event interrupts. */
9266 BLOCK_INPUT;
9267 f->tool_bar_items = new_tool_bar;
9268 f->n_tool_bar_items = new_n_tool_bar;
9269 w->update_mode_line = Qt;
9270 UNBLOCK_INPUT;
9271 }
9272
9273 UNGCPRO;
9274
9275 unbind_to (count, Qnil);
9276 set_buffer_internal_1 (prev);
9277 }
9278 }
9279 }
9280
9281
9282 /* Set F->desired_tool_bar_string to a Lisp string representing frame
9283 F's desired tool-bar contents. F->tool_bar_items must have
9284 been set up previously by calling prepare_menu_bars. */
9285
9286 static void
9287 build_desired_tool_bar_string (f)
9288 struct frame *f;
9289 {
9290 int i, size, size_needed;
9291 struct gcpro gcpro1, gcpro2, gcpro3;
9292 Lisp_Object image, plist, props;
9293
9294 image = plist = props = Qnil;
9295 GCPRO3 (image, plist, props);
9296
9297 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
9298 Otherwise, make a new string. */
9299
9300 /* The size of the string we might be able to reuse. */
9301 size = (STRINGP (f->desired_tool_bar_string)
9302 ? SCHARS (f->desired_tool_bar_string)
9303 : 0);
9304
9305 /* We need one space in the string for each image. */
9306 size_needed = f->n_tool_bar_items;
9307
9308 /* Reuse f->desired_tool_bar_string, if possible. */
9309 if (size < size_needed || NILP (f->desired_tool_bar_string))
9310 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
9311 make_number (' '));
9312 else
9313 {
9314 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
9315 Fremove_text_properties (make_number (0), make_number (size),
9316 props, f->desired_tool_bar_string);
9317 }
9318
9319 /* Put a `display' property on the string for the images to display,
9320 put a `menu_item' property on tool-bar items with a value that
9321 is the index of the item in F's tool-bar item vector. */
9322 for (i = 0; i < f->n_tool_bar_items; ++i)
9323 {
9324 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
9325
9326 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
9327 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
9328 int hmargin, vmargin, relief, idx, end;
9329 extern Lisp_Object QCrelief, QCmargin, QCconversion;
9330
9331 /* If image is a vector, choose the image according to the
9332 button state. */
9333 image = PROP (TOOL_BAR_ITEM_IMAGES);
9334 if (VECTORP (image))
9335 {
9336 if (enabled_p)
9337 idx = (selected_p
9338 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
9339 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
9340 else
9341 idx = (selected_p
9342 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
9343 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
9344
9345 xassert (ASIZE (image) >= idx);
9346 image = AREF (image, idx);
9347 }
9348 else
9349 idx = -1;
9350
9351 /* Ignore invalid image specifications. */
9352 if (!valid_image_p (image))
9353 continue;
9354
9355 /* Display the tool-bar button pressed, or depressed. */
9356 plist = Fcopy_sequence (XCDR (image));
9357
9358 /* Compute margin and relief to draw. */
9359 relief = (tool_bar_button_relief >= 0
9360 ? tool_bar_button_relief
9361 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
9362 hmargin = vmargin = relief;
9363
9364 if (INTEGERP (Vtool_bar_button_margin)
9365 && XINT (Vtool_bar_button_margin) > 0)
9366 {
9367 hmargin += XFASTINT (Vtool_bar_button_margin);
9368 vmargin += XFASTINT (Vtool_bar_button_margin);
9369 }
9370 else if (CONSP (Vtool_bar_button_margin))
9371 {
9372 if (INTEGERP (XCAR (Vtool_bar_button_margin))
9373 && XINT (XCAR (Vtool_bar_button_margin)) > 0)
9374 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
9375
9376 if (INTEGERP (XCDR (Vtool_bar_button_margin))
9377 && XINT (XCDR (Vtool_bar_button_margin)) > 0)
9378 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
9379 }
9380
9381 if (auto_raise_tool_bar_buttons_p)
9382 {
9383 /* Add a `:relief' property to the image spec if the item is
9384 selected. */
9385 if (selected_p)
9386 {
9387 plist = Fplist_put (plist, QCrelief, make_number (-relief));
9388 hmargin -= relief;
9389 vmargin -= relief;
9390 }
9391 }
9392 else
9393 {
9394 /* If image is selected, display it pressed, i.e. with a
9395 negative relief. If it's not selected, display it with a
9396 raised relief. */
9397 plist = Fplist_put (plist, QCrelief,
9398 (selected_p
9399 ? make_number (-relief)
9400 : make_number (relief)));
9401 hmargin -= relief;
9402 vmargin -= relief;
9403 }
9404
9405 /* Put a margin around the image. */
9406 if (hmargin || vmargin)
9407 {
9408 if (hmargin == vmargin)
9409 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
9410 else
9411 plist = Fplist_put (plist, QCmargin,
9412 Fcons (make_number (hmargin),
9413 make_number (vmargin)));
9414 }
9415
9416 /* If button is not enabled, and we don't have special images
9417 for the disabled state, make the image appear disabled by
9418 applying an appropriate algorithm to it. */
9419 if (!enabled_p && idx < 0)
9420 plist = Fplist_put (plist, QCconversion, Qdisabled);
9421
9422 /* Put a `display' text property on the string for the image to
9423 display. Put a `menu-item' property on the string that gives
9424 the start of this item's properties in the tool-bar items
9425 vector. */
9426 image = Fcons (Qimage, plist);
9427 props = list4 (Qdisplay, image,
9428 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
9429
9430 /* Let the last image hide all remaining spaces in the tool bar
9431 string. The string can be longer than needed when we reuse a
9432 previous string. */
9433 if (i + 1 == f->n_tool_bar_items)
9434 end = SCHARS (f->desired_tool_bar_string);
9435 else
9436 end = i + 1;
9437 Fadd_text_properties (make_number (i), make_number (end),
9438 props, f->desired_tool_bar_string);
9439 #undef PROP
9440 }
9441
9442 UNGCPRO;
9443 }
9444
9445
9446 /* Display one line of the tool-bar of frame IT->f.
9447
9448 HEIGHT specifies the desired height of the tool-bar line.
9449 If the actual height of the glyph row is less than HEIGHT, the
9450 row's height is increased to HEIGHT, and the icons are centered
9451 vertically in the new height.
9452
9453 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
9454 count a final empty row in case the tool-bar width exactly matches
9455 the window width.
9456 */
9457
9458 static void
9459 display_tool_bar_line (it, height)
9460 struct it *it;
9461 int height;
9462 {
9463 struct glyph_row *row = it->glyph_row;
9464 int max_x = it->last_visible_x;
9465 struct glyph *last;
9466
9467 prepare_desired_row (row);
9468 row->y = it->current_y;
9469
9470 /* Note that this isn't made use of if the face hasn't a box,
9471 so there's no need to check the face here. */
9472 it->start_of_box_run_p = 1;
9473
9474 while (it->current_x < max_x)
9475 {
9476 int x_before, x, n_glyphs_before, i, nglyphs;
9477
9478 /* Get the next display element. */
9479 if (!get_next_display_element (it))
9480 {
9481 /* Don't count empty row if we are counting needed tool-bar lines. */
9482 if (height < 0 && !it->hpos)
9483 return;
9484 break;
9485 }
9486
9487 /* Produce glyphs. */
9488 x_before = it->current_x;
9489 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
9490 PRODUCE_GLYPHS (it);
9491
9492 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
9493 i = 0;
9494 x = x_before;
9495 while (i < nglyphs)
9496 {
9497 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
9498
9499 if (x + glyph->pixel_width > max_x)
9500 {
9501 /* Glyph doesn't fit on line. */
9502 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
9503 it->current_x = x;
9504 goto out;
9505 }
9506
9507 ++it->hpos;
9508 x += glyph->pixel_width;
9509 ++i;
9510 }
9511
9512 /* Stop at line ends. */
9513 if (ITERATOR_AT_END_OF_LINE_P (it))
9514 break;
9515
9516 set_iterator_to_next (it, 1);
9517 }
9518
9519 out:;
9520
9521 row->displays_text_p = row->used[TEXT_AREA] != 0;
9522 /* Use default face for the border below the tool bar. */
9523 if (!row->displays_text_p)
9524 it->face_id = DEFAULT_FACE_ID;
9525 extend_face_to_end_of_line (it);
9526 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
9527 last->right_box_line_p = 1;
9528 if (last == row->glyphs[TEXT_AREA])
9529 last->left_box_line_p = 1;
9530
9531 /* Make line the desired height and center it vertically. */
9532 if ((height -= it->max_ascent + it->max_descent) > 0)
9533 {
9534 it->max_ascent += height / 2;
9535 it->max_descent += (height + 1) / 2;
9536 }
9537
9538 compute_line_metrics (it);
9539
9540 /* If line is empty, make it occupy the rest of the tool-bar. */
9541 if (!row->displays_text_p)
9542 {
9543 row->height = row->phys_height = it->last_visible_y - row->y;
9544 row->ascent = row->phys_ascent = 0;
9545 row->extra_line_spacing = 0;
9546 }
9547
9548 row->full_width_p = 1;
9549 row->continued_p = 0;
9550 row->truncated_on_left_p = 0;
9551 row->truncated_on_right_p = 0;
9552
9553 it->current_x = it->hpos = 0;
9554 it->current_y += row->height;
9555 ++it->vpos;
9556 ++it->glyph_row;
9557 }
9558
9559
9560 /* Value is the number of screen lines needed to make all tool-bar
9561 items of frame F visible. The number of actual rows needed is
9562 returned in *N_ROWS if non-NULL. */
9563
9564 static int
9565 tool_bar_lines_needed (f, n_rows)
9566 struct frame *f;
9567 int *n_rows;
9568 {
9569 struct window *w = XWINDOW (f->tool_bar_window);
9570 struct it it;
9571 struct glyph_row *temp_row = w->desired_matrix->rows;
9572
9573 /* Initialize an iterator for iteration over
9574 F->desired_tool_bar_string in the tool-bar window of frame F. */
9575 init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
9576 it.first_visible_x = 0;
9577 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
9578 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
9579
9580 while (!ITERATOR_AT_END_P (&it))
9581 {
9582 clear_glyph_row (temp_row);
9583 it.glyph_row = temp_row;
9584 display_tool_bar_line (&it, -1);
9585 }
9586 clear_glyph_row (temp_row);
9587
9588 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
9589 if (n_rows)
9590 *n_rows = it.vpos > 0 ? it.vpos : -1;
9591
9592 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
9593 }
9594
9595
9596 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
9597 0, 1, 0,
9598 doc: /* Return the number of lines occupied by the tool bar of FRAME. */)
9599 (frame)
9600 Lisp_Object frame;
9601 {
9602 struct frame *f;
9603 struct window *w;
9604 int nlines = 0;
9605
9606 if (NILP (frame))
9607 frame = selected_frame;
9608 else
9609 CHECK_FRAME (frame);
9610 f = XFRAME (frame);
9611
9612 if (WINDOWP (f->tool_bar_window)
9613 || (w = XWINDOW (f->tool_bar_window),
9614 WINDOW_TOTAL_LINES (w) > 0))
9615 {
9616 update_tool_bar (f, 1);
9617 if (f->n_tool_bar_items)
9618 {
9619 build_desired_tool_bar_string (f);
9620 nlines = tool_bar_lines_needed (f, NULL);
9621 }
9622 }
9623
9624 return make_number (nlines);
9625 }
9626
9627
9628 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
9629 height should be changed. */
9630
9631 static int
9632 redisplay_tool_bar (f)
9633 struct frame *f;
9634 {
9635 struct window *w;
9636 struct it it;
9637 struct glyph_row *row;
9638 int change_height_p = 0;
9639
9640 #ifdef USE_GTK
9641 if (FRAME_EXTERNAL_TOOL_BAR (f))
9642 update_frame_tool_bar (f);
9643 return 0;
9644 #endif
9645
9646 /* If frame hasn't a tool-bar window or if it is zero-height, don't
9647 do anything. This means you must start with tool-bar-lines
9648 non-zero to get the auto-sizing effect. Or in other words, you
9649 can turn off tool-bars by specifying tool-bar-lines zero. */
9650 if (!WINDOWP (f->tool_bar_window)
9651 || (w = XWINDOW (f->tool_bar_window),
9652 WINDOW_TOTAL_LINES (w) == 0))
9653 return 0;
9654
9655 /* Set up an iterator for the tool-bar window. */
9656 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
9657 it.first_visible_x = 0;
9658 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
9659 row = it.glyph_row;
9660
9661 /* Build a string that represents the contents of the tool-bar. */
9662 build_desired_tool_bar_string (f);
9663 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
9664
9665 if (f->n_tool_bar_rows == 0)
9666 {
9667 int nlines;
9668
9669 if ((nlines = tool_bar_lines_needed (f, &f->n_tool_bar_rows),
9670 nlines != WINDOW_TOTAL_LINES (w)))
9671 {
9672 extern Lisp_Object Qtool_bar_lines;
9673 Lisp_Object frame;
9674 int old_height = WINDOW_TOTAL_LINES (w);
9675
9676 XSETFRAME (frame, f);
9677 clear_glyph_matrix (w->desired_matrix);
9678 Fmodify_frame_parameters (frame,
9679 Fcons (Fcons (Qtool_bar_lines,
9680 make_number (nlines)),
9681 Qnil));
9682 if (WINDOW_TOTAL_LINES (w) != old_height)
9683 {
9684 fonts_changed_p = 1;
9685 return 1;
9686 }
9687 }
9688 }
9689
9690 /* Display as many lines as needed to display all tool-bar items. */
9691
9692 if (f->n_tool_bar_rows > 0)
9693 {
9694 int border, rows, height, extra;
9695
9696 if (INTEGERP (Vtool_bar_border))
9697 border = XINT (Vtool_bar_border);
9698 else if (EQ (Vtool_bar_border, Qinternal_border_width))
9699 border = FRAME_INTERNAL_BORDER_WIDTH (f);
9700 else if (EQ (Vtool_bar_border, Qborder_width))
9701 border = f->border_width;
9702 else
9703 border = 0;
9704 if (border < 0)
9705 border = 0;
9706
9707 rows = f->n_tool_bar_rows;
9708 height = (it.last_visible_y - border) / rows;
9709 extra = it.last_visible_y - border - height * rows;
9710
9711 while (it.current_y < it.last_visible_y)
9712 {
9713 int h = 0;
9714 if (extra > 0 && rows-- > 0)
9715 {
9716 h = (extra + rows - 1) / rows;
9717 extra -= h;
9718 }
9719 display_tool_bar_line (&it, height + h);
9720 }
9721 }
9722 else
9723 {
9724 while (it.current_y < it.last_visible_y)
9725 display_tool_bar_line (&it, 0);
9726 }
9727
9728 /* It doesn't make much sense to try scrolling in the tool-bar
9729 window, so don't do it. */
9730 w->desired_matrix->no_scrolling_p = 1;
9731 w->must_be_updated_p = 1;
9732
9733 if (auto_resize_tool_bars_p)
9734 {
9735 int nlines;
9736
9737 /* If we couldn't display everything, change the tool-bar's
9738 height. */
9739 if (IT_STRING_CHARPOS (it) < it.end_charpos)
9740 change_height_p = 1;
9741
9742 /* If there are blank lines at the end, except for a partially
9743 visible blank line at the end that is smaller than
9744 FRAME_LINE_HEIGHT, change the tool-bar's height. */
9745 row = it.glyph_row - 1;
9746 if (!row->displays_text_p
9747 && row->height >= FRAME_LINE_HEIGHT (f))
9748 change_height_p = 1;
9749
9750 /* If row displays tool-bar items, but is partially visible,
9751 change the tool-bar's height. */
9752 if (row->displays_text_p
9753 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y)
9754 change_height_p = 1;
9755
9756 /* Resize windows as needed by changing the `tool-bar-lines'
9757 frame parameter. */
9758 if (change_height_p
9759 && (nlines = tool_bar_lines_needed (f, &f->n_tool_bar_rows),
9760 nlines != WINDOW_TOTAL_LINES (w)))
9761 {
9762 extern Lisp_Object Qtool_bar_lines;
9763 Lisp_Object frame;
9764 int old_height = WINDOW_TOTAL_LINES (w);
9765
9766 XSETFRAME (frame, f);
9767 clear_glyph_matrix (w->desired_matrix);
9768 Fmodify_frame_parameters (frame,
9769 Fcons (Fcons (Qtool_bar_lines,
9770 make_number (nlines)),
9771 Qnil));
9772 if (WINDOW_TOTAL_LINES (w) != old_height)
9773 fonts_changed_p = 1;
9774 }
9775 }
9776
9777 return change_height_p;
9778 }
9779
9780
9781 /* Get information about the tool-bar item which is displayed in GLYPH
9782 on frame F. Return in *PROP_IDX the index where tool-bar item
9783 properties start in F->tool_bar_items. Value is zero if
9784 GLYPH doesn't display a tool-bar item. */
9785
9786 static int
9787 tool_bar_item_info (f, glyph, prop_idx)
9788 struct frame *f;
9789 struct glyph *glyph;
9790 int *prop_idx;
9791 {
9792 Lisp_Object prop;
9793 int success_p;
9794 int charpos;
9795
9796 /* This function can be called asynchronously, which means we must
9797 exclude any possibility that Fget_text_property signals an
9798 error. */
9799 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
9800 charpos = max (0, charpos);
9801
9802 /* Get the text property `menu-item' at pos. The value of that
9803 property is the start index of this item's properties in
9804 F->tool_bar_items. */
9805 prop = Fget_text_property (make_number (charpos),
9806 Qmenu_item, f->current_tool_bar_string);
9807 if (INTEGERP (prop))
9808 {
9809 *prop_idx = XINT (prop);
9810 success_p = 1;
9811 }
9812 else
9813 success_p = 0;
9814
9815 return success_p;
9816 }
9817
9818 \f
9819 /* Get information about the tool-bar item at position X/Y on frame F.
9820 Return in *GLYPH a pointer to the glyph of the tool-bar item in
9821 the current matrix of the tool-bar window of F, or NULL if not
9822 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
9823 item in F->tool_bar_items. Value is
9824
9825 -1 if X/Y is not on a tool-bar item
9826 0 if X/Y is on the same item that was highlighted before.
9827 1 otherwise. */
9828
9829 static int
9830 get_tool_bar_item (f, x, y, glyph, hpos, vpos, prop_idx)
9831 struct frame *f;
9832 int x, y;
9833 struct glyph **glyph;
9834 int *hpos, *vpos, *prop_idx;
9835 {
9836 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9837 struct window *w = XWINDOW (f->tool_bar_window);
9838 int area;
9839
9840 /* Find the glyph under X/Y. */
9841 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
9842 if (*glyph == NULL)
9843 return -1;
9844
9845 /* Get the start of this tool-bar item's properties in
9846 f->tool_bar_items. */
9847 if (!tool_bar_item_info (f, *glyph, prop_idx))
9848 return -1;
9849
9850 /* Is mouse on the highlighted item? */
9851 if (EQ (f->tool_bar_window, dpyinfo->mouse_face_window)
9852 && *vpos >= dpyinfo->mouse_face_beg_row
9853 && *vpos <= dpyinfo->mouse_face_end_row
9854 && (*vpos > dpyinfo->mouse_face_beg_row
9855 || *hpos >= dpyinfo->mouse_face_beg_col)
9856 && (*vpos < dpyinfo->mouse_face_end_row
9857 || *hpos < dpyinfo->mouse_face_end_col
9858 || dpyinfo->mouse_face_past_end))
9859 return 0;
9860
9861 return 1;
9862 }
9863
9864
9865 /* EXPORT:
9866 Handle mouse button event on the tool-bar of frame F, at
9867 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
9868 0 for button release. MODIFIERS is event modifiers for button
9869 release. */
9870
9871 void
9872 handle_tool_bar_click (f, x, y, down_p, modifiers)
9873 struct frame *f;
9874 int x, y, down_p;
9875 unsigned int modifiers;
9876 {
9877 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9878 struct window *w = XWINDOW (f->tool_bar_window);
9879 int hpos, vpos, prop_idx;
9880 struct glyph *glyph;
9881 Lisp_Object enabled_p;
9882
9883 /* If not on the highlighted tool-bar item, return. */
9884 frame_to_window_pixel_xy (w, &x, &y);
9885 if (get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
9886 return;
9887
9888 /* If item is disabled, do nothing. */
9889 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
9890 if (NILP (enabled_p))
9891 return;
9892
9893 if (down_p)
9894 {
9895 /* Show item in pressed state. */
9896 show_mouse_face (dpyinfo, DRAW_IMAGE_SUNKEN);
9897 dpyinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
9898 last_tool_bar_item = prop_idx;
9899 }
9900 else
9901 {
9902 Lisp_Object key, frame;
9903 struct input_event event;
9904 EVENT_INIT (event);
9905
9906 /* Show item in released state. */
9907 show_mouse_face (dpyinfo, DRAW_IMAGE_RAISED);
9908 dpyinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
9909
9910 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
9911
9912 XSETFRAME (frame, f);
9913 event.kind = TOOL_BAR_EVENT;
9914 event.frame_or_window = frame;
9915 event.arg = frame;
9916 kbd_buffer_store_event (&event);
9917
9918 event.kind = TOOL_BAR_EVENT;
9919 event.frame_or_window = frame;
9920 event.arg = key;
9921 event.modifiers = modifiers;
9922 kbd_buffer_store_event (&event);
9923 last_tool_bar_item = -1;
9924 }
9925 }
9926
9927
9928 /* Possibly highlight a tool-bar item on frame F when mouse moves to
9929 tool-bar window-relative coordinates X/Y. Called from
9930 note_mouse_highlight. */
9931
9932 static void
9933 note_tool_bar_highlight (f, x, y)
9934 struct frame *f;
9935 int x, y;
9936 {
9937 Lisp_Object window = f->tool_bar_window;
9938 struct window *w = XWINDOW (window);
9939 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9940 int hpos, vpos;
9941 struct glyph *glyph;
9942 struct glyph_row *row;
9943 int i;
9944 Lisp_Object enabled_p;
9945 int prop_idx;
9946 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
9947 int mouse_down_p, rc;
9948
9949 /* Function note_mouse_highlight is called with negative x(y
9950 values when mouse moves outside of the frame. */
9951 if (x <= 0 || y <= 0)
9952 {
9953 clear_mouse_face (dpyinfo);
9954 return;
9955 }
9956
9957 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
9958 if (rc < 0)
9959 {
9960 /* Not on tool-bar item. */
9961 clear_mouse_face (dpyinfo);
9962 return;
9963 }
9964 else if (rc == 0)
9965 /* On same tool-bar item as before. */
9966 goto set_help_echo;
9967
9968 clear_mouse_face (dpyinfo);
9969
9970 /* Mouse is down, but on different tool-bar item? */
9971 mouse_down_p = (dpyinfo->grabbed
9972 && f == last_mouse_frame
9973 && FRAME_LIVE_P (f));
9974 if (mouse_down_p
9975 && last_tool_bar_item != prop_idx)
9976 return;
9977
9978 dpyinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
9979 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
9980
9981 /* If tool-bar item is not enabled, don't highlight it. */
9982 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
9983 if (!NILP (enabled_p))
9984 {
9985 /* Compute the x-position of the glyph. In front and past the
9986 image is a space. We include this in the highlighted area. */
9987 row = MATRIX_ROW (w->current_matrix, vpos);
9988 for (i = x = 0; i < hpos; ++i)
9989 x += row->glyphs[TEXT_AREA][i].pixel_width;
9990
9991 /* Record this as the current active region. */
9992 dpyinfo->mouse_face_beg_col = hpos;
9993 dpyinfo->mouse_face_beg_row = vpos;
9994 dpyinfo->mouse_face_beg_x = x;
9995 dpyinfo->mouse_face_beg_y = row->y;
9996 dpyinfo->mouse_face_past_end = 0;
9997
9998 dpyinfo->mouse_face_end_col = hpos + 1;
9999 dpyinfo->mouse_face_end_row = vpos;
10000 dpyinfo->mouse_face_end_x = x + glyph->pixel_width;
10001 dpyinfo->mouse_face_end_y = row->y;
10002 dpyinfo->mouse_face_window = window;
10003 dpyinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
10004
10005 /* Display it as active. */
10006 show_mouse_face (dpyinfo, draw);
10007 dpyinfo->mouse_face_image_state = draw;
10008 }
10009
10010 set_help_echo:
10011
10012 /* Set help_echo_string to a help string to display for this tool-bar item.
10013 XTread_socket does the rest. */
10014 help_echo_object = help_echo_window = Qnil;
10015 help_echo_pos = -1;
10016 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
10017 if (NILP (help_echo_string))
10018 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
10019 }
10020
10021 #endif /* HAVE_WINDOW_SYSTEM */
10022
10023
10024 \f
10025 /************************************************************************
10026 Horizontal scrolling
10027 ************************************************************************/
10028
10029 static int hscroll_window_tree P_ ((Lisp_Object));
10030 static int hscroll_windows P_ ((Lisp_Object));
10031
10032 /* For all leaf windows in the window tree rooted at WINDOW, set their
10033 hscroll value so that PT is (i) visible in the window, and (ii) so
10034 that it is not within a certain margin at the window's left and
10035 right border. Value is non-zero if any window's hscroll has been
10036 changed. */
10037
10038 static int
10039 hscroll_window_tree (window)
10040 Lisp_Object window;
10041 {
10042 int hscrolled_p = 0;
10043 int hscroll_relative_p = FLOATP (Vhscroll_step);
10044 int hscroll_step_abs = 0;
10045 double hscroll_step_rel = 0;
10046
10047 if (hscroll_relative_p)
10048 {
10049 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
10050 if (hscroll_step_rel < 0)
10051 {
10052 hscroll_relative_p = 0;
10053 hscroll_step_abs = 0;
10054 }
10055 }
10056 else if (INTEGERP (Vhscroll_step))
10057 {
10058 hscroll_step_abs = XINT (Vhscroll_step);
10059 if (hscroll_step_abs < 0)
10060 hscroll_step_abs = 0;
10061 }
10062 else
10063 hscroll_step_abs = 0;
10064
10065 while (WINDOWP (window))
10066 {
10067 struct window *w = XWINDOW (window);
10068
10069 if (WINDOWP (w->hchild))
10070 hscrolled_p |= hscroll_window_tree (w->hchild);
10071 else if (WINDOWP (w->vchild))
10072 hscrolled_p |= hscroll_window_tree (w->vchild);
10073 else if (w->cursor.vpos >= 0)
10074 {
10075 int h_margin;
10076 int text_area_width;
10077 struct glyph_row *current_cursor_row
10078 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
10079 struct glyph_row *desired_cursor_row
10080 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
10081 struct glyph_row *cursor_row
10082 = (desired_cursor_row->enabled_p
10083 ? desired_cursor_row
10084 : current_cursor_row);
10085
10086 text_area_width = window_box_width (w, TEXT_AREA);
10087
10088 /* Scroll when cursor is inside this scroll margin. */
10089 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
10090
10091 if ((XFASTINT (w->hscroll)
10092 && w->cursor.x <= h_margin)
10093 || (cursor_row->enabled_p
10094 && cursor_row->truncated_on_right_p
10095 && (w->cursor.x >= text_area_width - h_margin)))
10096 {
10097 struct it it;
10098 int hscroll;
10099 struct buffer *saved_current_buffer;
10100 int pt;
10101 int wanted_x;
10102
10103 /* Find point in a display of infinite width. */
10104 saved_current_buffer = current_buffer;
10105 current_buffer = XBUFFER (w->buffer);
10106
10107 if (w == XWINDOW (selected_window))
10108 pt = BUF_PT (current_buffer);
10109 else
10110 {
10111 pt = marker_position (w->pointm);
10112 pt = max (BEGV, pt);
10113 pt = min (ZV, pt);
10114 }
10115
10116 /* Move iterator to pt starting at cursor_row->start in
10117 a line with infinite width. */
10118 init_to_row_start (&it, w, cursor_row);
10119 it.last_visible_x = INFINITY;
10120 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
10121 current_buffer = saved_current_buffer;
10122
10123 /* Position cursor in window. */
10124 if (!hscroll_relative_p && hscroll_step_abs == 0)
10125 hscroll = max (0, (it.current_x
10126 - (ITERATOR_AT_END_OF_LINE_P (&it)
10127 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
10128 : (text_area_width / 2))))
10129 / FRAME_COLUMN_WIDTH (it.f);
10130 else if (w->cursor.x >= text_area_width - h_margin)
10131 {
10132 if (hscroll_relative_p)
10133 wanted_x = text_area_width * (1 - hscroll_step_rel)
10134 - h_margin;
10135 else
10136 wanted_x = text_area_width
10137 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
10138 - h_margin;
10139 hscroll
10140 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
10141 }
10142 else
10143 {
10144 if (hscroll_relative_p)
10145 wanted_x = text_area_width * hscroll_step_rel
10146 + h_margin;
10147 else
10148 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
10149 + h_margin;
10150 hscroll
10151 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
10152 }
10153 hscroll = max (hscroll, XFASTINT (w->min_hscroll));
10154
10155 /* Don't call Fset_window_hscroll if value hasn't
10156 changed because it will prevent redisplay
10157 optimizations. */
10158 if (XFASTINT (w->hscroll) != hscroll)
10159 {
10160 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
10161 w->hscroll = make_number (hscroll);
10162 hscrolled_p = 1;
10163 }
10164 }
10165 }
10166
10167 window = w->next;
10168 }
10169
10170 /* Value is non-zero if hscroll of any leaf window has been changed. */
10171 return hscrolled_p;
10172 }
10173
10174
10175 /* Set hscroll so that cursor is visible and not inside horizontal
10176 scroll margins for all windows in the tree rooted at WINDOW. See
10177 also hscroll_window_tree above. Value is non-zero if any window's
10178 hscroll has been changed. If it has, desired matrices on the frame
10179 of WINDOW are cleared. */
10180
10181 static int
10182 hscroll_windows (window)
10183 Lisp_Object window;
10184 {
10185 int hscrolled_p;
10186
10187 if (automatic_hscrolling_p)
10188 {
10189 hscrolled_p = hscroll_window_tree (window);
10190 if (hscrolled_p)
10191 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
10192 }
10193 else
10194 hscrolled_p = 0;
10195 return hscrolled_p;
10196 }
10197
10198
10199 \f
10200 /************************************************************************
10201 Redisplay
10202 ************************************************************************/
10203
10204 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
10205 to a non-zero value. This is sometimes handy to have in a debugger
10206 session. */
10207
10208 #if GLYPH_DEBUG
10209
10210 /* First and last unchanged row for try_window_id. */
10211
10212 int debug_first_unchanged_at_end_vpos;
10213 int debug_last_unchanged_at_beg_vpos;
10214
10215 /* Delta vpos and y. */
10216
10217 int debug_dvpos, debug_dy;
10218
10219 /* Delta in characters and bytes for try_window_id. */
10220
10221 int debug_delta, debug_delta_bytes;
10222
10223 /* Values of window_end_pos and window_end_vpos at the end of
10224 try_window_id. */
10225
10226 EMACS_INT debug_end_pos, debug_end_vpos;
10227
10228 /* Append a string to W->desired_matrix->method. FMT is a printf
10229 format string. A1...A9 are a supplement for a variable-length
10230 argument list. If trace_redisplay_p is non-zero also printf the
10231 resulting string to stderr. */
10232
10233 static void
10234 debug_method_add (w, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9)
10235 struct window *w;
10236 char *fmt;
10237 int a1, a2, a3, a4, a5, a6, a7, a8, a9;
10238 {
10239 char buffer[512];
10240 char *method = w->desired_matrix->method;
10241 int len = strlen (method);
10242 int size = sizeof w->desired_matrix->method;
10243 int remaining = size - len - 1;
10244
10245 sprintf (buffer, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9);
10246 if (len && remaining)
10247 {
10248 method[len] = '|';
10249 --remaining, ++len;
10250 }
10251
10252 strncpy (method + len, buffer, remaining);
10253
10254 if (trace_redisplay_p)
10255 fprintf (stderr, "%p (%s): %s\n",
10256 w,
10257 ((BUFFERP (w->buffer)
10258 && STRINGP (XBUFFER (w->buffer)->name))
10259 ? (char *) SDATA (XBUFFER (w->buffer)->name)
10260 : "no buffer"),
10261 buffer);
10262 }
10263
10264 #endif /* GLYPH_DEBUG */
10265
10266
10267 /* Value is non-zero if all changes in window W, which displays
10268 current_buffer, are in the text between START and END. START is a
10269 buffer position, END is given as a distance from Z. Used in
10270 redisplay_internal for display optimization. */
10271
10272 static INLINE int
10273 text_outside_line_unchanged_p (w, start, end)
10274 struct window *w;
10275 int start, end;
10276 {
10277 int unchanged_p = 1;
10278
10279 /* If text or overlays have changed, see where. */
10280 if (XFASTINT (w->last_modified) < MODIFF
10281 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
10282 {
10283 /* Gap in the line? */
10284 if (GPT < start || Z - GPT < end)
10285 unchanged_p = 0;
10286
10287 /* Changes start in front of the line, or end after it? */
10288 if (unchanged_p
10289 && (BEG_UNCHANGED < start - 1
10290 || END_UNCHANGED < end))
10291 unchanged_p = 0;
10292
10293 /* If selective display, can't optimize if changes start at the
10294 beginning of the line. */
10295 if (unchanged_p
10296 && INTEGERP (current_buffer->selective_display)
10297 && XINT (current_buffer->selective_display) > 0
10298 && (BEG_UNCHANGED < start || GPT <= start))
10299 unchanged_p = 0;
10300
10301 /* If there are overlays at the start or end of the line, these
10302 may have overlay strings with newlines in them. A change at
10303 START, for instance, may actually concern the display of such
10304 overlay strings as well, and they are displayed on different
10305 lines. So, quickly rule out this case. (For the future, it
10306 might be desirable to implement something more telling than
10307 just BEG/END_UNCHANGED.) */
10308 if (unchanged_p)
10309 {
10310 if (BEG + BEG_UNCHANGED == start
10311 && overlay_touches_p (start))
10312 unchanged_p = 0;
10313 if (END_UNCHANGED == end
10314 && overlay_touches_p (Z - end))
10315 unchanged_p = 0;
10316 }
10317 }
10318
10319 return unchanged_p;
10320 }
10321
10322
10323 /* Do a frame update, taking possible shortcuts into account. This is
10324 the main external entry point for redisplay.
10325
10326 If the last redisplay displayed an echo area message and that message
10327 is no longer requested, we clear the echo area or bring back the
10328 mini-buffer if that is in use. */
10329
10330 void
10331 redisplay ()
10332 {
10333 redisplay_internal (0);
10334 }
10335
10336
10337 static Lisp_Object
10338 overlay_arrow_string_or_property (var)
10339 Lisp_Object var;
10340 {
10341 Lisp_Object val;
10342
10343 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
10344 return val;
10345
10346 return Voverlay_arrow_string;
10347 }
10348
10349 /* Return 1 if there are any overlay-arrows in current_buffer. */
10350 static int
10351 overlay_arrow_in_current_buffer_p ()
10352 {
10353 Lisp_Object vlist;
10354
10355 for (vlist = Voverlay_arrow_variable_list;
10356 CONSP (vlist);
10357 vlist = XCDR (vlist))
10358 {
10359 Lisp_Object var = XCAR (vlist);
10360 Lisp_Object val;
10361
10362 if (!SYMBOLP (var))
10363 continue;
10364 val = find_symbol_value (var);
10365 if (MARKERP (val)
10366 && current_buffer == XMARKER (val)->buffer)
10367 return 1;
10368 }
10369 return 0;
10370 }
10371
10372
10373 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
10374 has changed. */
10375
10376 static int
10377 overlay_arrows_changed_p ()
10378 {
10379 Lisp_Object vlist;
10380
10381 for (vlist = Voverlay_arrow_variable_list;
10382 CONSP (vlist);
10383 vlist = XCDR (vlist))
10384 {
10385 Lisp_Object var = XCAR (vlist);
10386 Lisp_Object val, pstr;
10387
10388 if (!SYMBOLP (var))
10389 continue;
10390 val = find_symbol_value (var);
10391 if (!MARKERP (val))
10392 continue;
10393 if (! EQ (COERCE_MARKER (val),
10394 Fget (var, Qlast_arrow_position))
10395 || ! (pstr = overlay_arrow_string_or_property (var),
10396 EQ (pstr, Fget (var, Qlast_arrow_string))))
10397 return 1;
10398 }
10399 return 0;
10400 }
10401
10402 /* Mark overlay arrows to be updated on next redisplay. */
10403
10404 static void
10405 update_overlay_arrows (up_to_date)
10406 int up_to_date;
10407 {
10408 Lisp_Object vlist;
10409
10410 for (vlist = Voverlay_arrow_variable_list;
10411 CONSP (vlist);
10412 vlist = XCDR (vlist))
10413 {
10414 Lisp_Object var = XCAR (vlist);
10415
10416 if (!SYMBOLP (var))
10417 continue;
10418
10419 if (up_to_date > 0)
10420 {
10421 Lisp_Object val = find_symbol_value (var);
10422 Fput (var, Qlast_arrow_position,
10423 COERCE_MARKER (val));
10424 Fput (var, Qlast_arrow_string,
10425 overlay_arrow_string_or_property (var));
10426 }
10427 else if (up_to_date < 0
10428 || !NILP (Fget (var, Qlast_arrow_position)))
10429 {
10430 Fput (var, Qlast_arrow_position, Qt);
10431 Fput (var, Qlast_arrow_string, Qt);
10432 }
10433 }
10434 }
10435
10436
10437 /* Return overlay arrow string to display at row.
10438 Return integer (bitmap number) for arrow bitmap in left fringe.
10439 Return nil if no overlay arrow. */
10440
10441 static Lisp_Object
10442 overlay_arrow_at_row (it, row)
10443 struct it *it;
10444 struct glyph_row *row;
10445 {
10446 Lisp_Object vlist;
10447
10448 for (vlist = Voverlay_arrow_variable_list;
10449 CONSP (vlist);
10450 vlist = XCDR (vlist))
10451 {
10452 Lisp_Object var = XCAR (vlist);
10453 Lisp_Object val;
10454
10455 if (!SYMBOLP (var))
10456 continue;
10457
10458 val = find_symbol_value (var);
10459
10460 if (MARKERP (val)
10461 && current_buffer == XMARKER (val)->buffer
10462 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
10463 {
10464 if (FRAME_WINDOW_P (it->f)
10465 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
10466 {
10467 #ifdef HAVE_WINDOW_SYSTEM
10468 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
10469 {
10470 int fringe_bitmap;
10471 if ((fringe_bitmap = lookup_fringe_bitmap (val)) != 0)
10472 return make_number (fringe_bitmap);
10473 }
10474 #endif
10475 return make_number (-1); /* Use default arrow bitmap */
10476 }
10477 return overlay_arrow_string_or_property (var);
10478 }
10479 }
10480
10481 return Qnil;
10482 }
10483
10484 /* Return 1 if point moved out of or into a composition. Otherwise
10485 return 0. PREV_BUF and PREV_PT are the last point buffer and
10486 position. BUF and PT are the current point buffer and position. */
10487
10488 int
10489 check_point_in_composition (prev_buf, prev_pt, buf, pt)
10490 struct buffer *prev_buf, *buf;
10491 int prev_pt, pt;
10492 {
10493 int start, end;
10494 Lisp_Object prop;
10495 Lisp_Object buffer;
10496
10497 XSETBUFFER (buffer, buf);
10498 /* Check a composition at the last point if point moved within the
10499 same buffer. */
10500 if (prev_buf == buf)
10501 {
10502 if (prev_pt == pt)
10503 /* Point didn't move. */
10504 return 0;
10505
10506 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
10507 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
10508 && COMPOSITION_VALID_P (start, end, prop)
10509 && start < prev_pt && end > prev_pt)
10510 /* The last point was within the composition. Return 1 iff
10511 point moved out of the composition. */
10512 return (pt <= start || pt >= end);
10513 }
10514
10515 /* Check a composition at the current point. */
10516 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
10517 && find_composition (pt, -1, &start, &end, &prop, buffer)
10518 && COMPOSITION_VALID_P (start, end, prop)
10519 && start < pt && end > pt);
10520 }
10521
10522
10523 /* Reconsider the setting of B->clip_changed which is displayed
10524 in window W. */
10525
10526 static INLINE void
10527 reconsider_clip_changes (w, b)
10528 struct window *w;
10529 struct buffer *b;
10530 {
10531 if (b->clip_changed
10532 && !NILP (w->window_end_valid)
10533 && w->current_matrix->buffer == b
10534 && w->current_matrix->zv == BUF_ZV (b)
10535 && w->current_matrix->begv == BUF_BEGV (b))
10536 b->clip_changed = 0;
10537
10538 /* If display wasn't paused, and W is not a tool bar window, see if
10539 point has been moved into or out of a composition. In that case,
10540 we set b->clip_changed to 1 to force updating the screen. If
10541 b->clip_changed has already been set to 1, we can skip this
10542 check. */
10543 if (!b->clip_changed
10544 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
10545 {
10546 int pt;
10547
10548 if (w == XWINDOW (selected_window))
10549 pt = BUF_PT (current_buffer);
10550 else
10551 pt = marker_position (w->pointm);
10552
10553 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
10554 || pt != XINT (w->last_point))
10555 && check_point_in_composition (w->current_matrix->buffer,
10556 XINT (w->last_point),
10557 XBUFFER (w->buffer), pt))
10558 b->clip_changed = 1;
10559 }
10560 }
10561 \f
10562
10563 /* Select FRAME to forward the values of frame-local variables into C
10564 variables so that the redisplay routines can access those values
10565 directly. */
10566
10567 static void
10568 select_frame_for_redisplay (frame)
10569 Lisp_Object frame;
10570 {
10571 Lisp_Object tail, sym, val;
10572 Lisp_Object old = selected_frame;
10573
10574 selected_frame = frame;
10575
10576 for (tail = XFRAME (frame)->param_alist; CONSP (tail); tail = XCDR (tail))
10577 if (CONSP (XCAR (tail))
10578 && (sym = XCAR (XCAR (tail)),
10579 SYMBOLP (sym))
10580 && (sym = indirect_variable (sym),
10581 val = SYMBOL_VALUE (sym),
10582 (BUFFER_LOCAL_VALUEP (val)
10583 || SOME_BUFFER_LOCAL_VALUEP (val)))
10584 && XBUFFER_LOCAL_VALUE (val)->check_frame)
10585 /* Use find_symbol_value rather than Fsymbol_value
10586 to avoid an error if it is void. */
10587 find_symbol_value (sym);
10588
10589 for (tail = XFRAME (old)->param_alist; CONSP (tail); tail = XCDR (tail))
10590 if (CONSP (XCAR (tail))
10591 && (sym = XCAR (XCAR (tail)),
10592 SYMBOLP (sym))
10593 && (sym = indirect_variable (sym),
10594 val = SYMBOL_VALUE (sym),
10595 (BUFFER_LOCAL_VALUEP (val)
10596 || SOME_BUFFER_LOCAL_VALUEP (val)))
10597 && XBUFFER_LOCAL_VALUE (val)->check_frame)
10598 find_symbol_value (sym);
10599 }
10600
10601
10602 #define STOP_POLLING \
10603 do { if (! polling_stopped_here) stop_polling (); \
10604 polling_stopped_here = 1; } while (0)
10605
10606 #define RESUME_POLLING \
10607 do { if (polling_stopped_here) start_polling (); \
10608 polling_stopped_here = 0; } while (0)
10609
10610
10611 /* If PRESERVE_ECHO_AREA is nonzero, it means this redisplay is not in
10612 response to any user action; therefore, we should preserve the echo
10613 area. (Actually, our caller does that job.) Perhaps in the future
10614 avoid recentering windows if it is not necessary; currently that
10615 causes some problems. */
10616
10617 static void
10618 redisplay_internal (preserve_echo_area)
10619 int preserve_echo_area;
10620 {
10621 struct window *w = XWINDOW (selected_window);
10622 struct frame *f = XFRAME (w->frame);
10623 int pause;
10624 int must_finish = 0;
10625 struct text_pos tlbufpos, tlendpos;
10626 int number_of_visible_frames;
10627 int count;
10628 struct frame *sf = SELECTED_FRAME ();
10629 int polling_stopped_here = 0;
10630
10631 /* Non-zero means redisplay has to consider all windows on all
10632 frames. Zero means, only selected_window is considered. */
10633 int consider_all_windows_p;
10634
10635 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
10636
10637 /* No redisplay if running in batch mode or frame is not yet fully
10638 initialized, or redisplay is explicitly turned off by setting
10639 Vinhibit_redisplay. */
10640 if (noninteractive
10641 || !NILP (Vinhibit_redisplay)
10642 || !f->glyphs_initialized_p)
10643 return;
10644
10645 /* The flag redisplay_performed_directly_p is set by
10646 direct_output_for_insert when it already did the whole screen
10647 update necessary. */
10648 if (redisplay_performed_directly_p)
10649 {
10650 redisplay_performed_directly_p = 0;
10651 if (!hscroll_windows (selected_window))
10652 return;
10653 }
10654
10655 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
10656 if (popup_activated ())
10657 return;
10658 #endif
10659
10660 /* I don't think this happens but let's be paranoid. */
10661 if (redisplaying_p)
10662 return;
10663
10664 /* Record a function that resets redisplaying_p to its old value
10665 when we leave this function. */
10666 count = SPECPDL_INDEX ();
10667 record_unwind_protect (unwind_redisplay,
10668 Fcons (make_number (redisplaying_p), selected_frame));
10669 ++redisplaying_p;
10670 specbind (Qinhibit_free_realized_faces, Qnil);
10671
10672 {
10673 Lisp_Object tail, frame;
10674
10675 FOR_EACH_FRAME (tail, frame)
10676 {
10677 struct frame *f = XFRAME (frame);
10678 f->already_hscrolled_p = 0;
10679 }
10680 }
10681
10682 retry:
10683 pause = 0;
10684 reconsider_clip_changes (w, current_buffer);
10685 last_escape_glyph_frame = NULL;
10686 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
10687
10688 /* If new fonts have been loaded that make a glyph matrix adjustment
10689 necessary, do it. */
10690 if (fonts_changed_p)
10691 {
10692 adjust_glyphs (NULL);
10693 ++windows_or_buffers_changed;
10694 fonts_changed_p = 0;
10695 }
10696
10697 /* If face_change_count is non-zero, init_iterator will free all
10698 realized faces, which includes the faces referenced from current
10699 matrices. So, we can't reuse current matrices in this case. */
10700 if (face_change_count)
10701 ++windows_or_buffers_changed;
10702
10703 if (! FRAME_WINDOW_P (sf)
10704 && previous_terminal_frame != sf)
10705 {
10706 /* Since frames on an ASCII terminal share the same display
10707 area, displaying a different frame means redisplay the whole
10708 thing. */
10709 windows_or_buffers_changed++;
10710 SET_FRAME_GARBAGED (sf);
10711 XSETFRAME (Vterminal_frame, sf);
10712 }
10713 previous_terminal_frame = sf;
10714
10715 /* Set the visible flags for all frames. Do this before checking
10716 for resized or garbaged frames; they want to know if their frames
10717 are visible. See the comment in frame.h for
10718 FRAME_SAMPLE_VISIBILITY. */
10719 {
10720 Lisp_Object tail, frame;
10721
10722 number_of_visible_frames = 0;
10723
10724 FOR_EACH_FRAME (tail, frame)
10725 {
10726 struct frame *f = XFRAME (frame);
10727
10728 FRAME_SAMPLE_VISIBILITY (f);
10729 if (FRAME_VISIBLE_P (f))
10730 ++number_of_visible_frames;
10731 clear_desired_matrices (f);
10732 }
10733 }
10734
10735 /* Notice any pending interrupt request to change frame size. */
10736 do_pending_window_change (1);
10737
10738 /* Clear frames marked as garbaged. */
10739 if (frame_garbaged)
10740 clear_garbaged_frames ();
10741
10742 /* Build menubar and tool-bar items. */
10743 if (NILP (Vmemory_full))
10744 prepare_menu_bars ();
10745
10746 if (windows_or_buffers_changed)
10747 update_mode_lines++;
10748
10749 /* Detect case that we need to write or remove a star in the mode line. */
10750 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
10751 {
10752 w->update_mode_line = Qt;
10753 if (buffer_shared > 1)
10754 update_mode_lines++;
10755 }
10756
10757 /* If %c is in the mode line, update it if needed. */
10758 if (!NILP (w->column_number_displayed)
10759 /* This alternative quickly identifies a common case
10760 where no change is needed. */
10761 && !(PT == XFASTINT (w->last_point)
10762 && XFASTINT (w->last_modified) >= MODIFF
10763 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
10764 && (XFASTINT (w->column_number_displayed)
10765 != (int) current_column ())) /* iftc */
10766 w->update_mode_line = Qt;
10767
10768 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
10769
10770 /* The variable buffer_shared is set in redisplay_window and
10771 indicates that we redisplay a buffer in different windows. See
10772 there. */
10773 consider_all_windows_p = (update_mode_lines || buffer_shared > 1
10774 || cursor_type_changed);
10775
10776 /* If specs for an arrow have changed, do thorough redisplay
10777 to ensure we remove any arrow that should no longer exist. */
10778 if (overlay_arrows_changed_p ())
10779 consider_all_windows_p = windows_or_buffers_changed = 1;
10780
10781 /* Normally the message* functions will have already displayed and
10782 updated the echo area, but the frame may have been trashed, or
10783 the update may have been preempted, so display the echo area
10784 again here. Checking message_cleared_p captures the case that
10785 the echo area should be cleared. */
10786 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
10787 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
10788 || (message_cleared_p
10789 && minibuf_level == 0
10790 /* If the mini-window is currently selected, this means the
10791 echo-area doesn't show through. */
10792 && !MINI_WINDOW_P (XWINDOW (selected_window))))
10793 {
10794 int window_height_changed_p = echo_area_display (0);
10795 must_finish = 1;
10796
10797 /* If we don't display the current message, don't clear the
10798 message_cleared_p flag, because, if we did, we wouldn't clear
10799 the echo area in the next redisplay which doesn't preserve
10800 the echo area. */
10801 if (!display_last_displayed_message_p)
10802 message_cleared_p = 0;
10803
10804 if (fonts_changed_p)
10805 goto retry;
10806 else if (window_height_changed_p)
10807 {
10808 consider_all_windows_p = 1;
10809 ++update_mode_lines;
10810 ++windows_or_buffers_changed;
10811
10812 /* If window configuration was changed, frames may have been
10813 marked garbaged. Clear them or we will experience
10814 surprises wrt scrolling. */
10815 if (frame_garbaged)
10816 clear_garbaged_frames ();
10817 }
10818 }
10819 else if (EQ (selected_window, minibuf_window)
10820 && (current_buffer->clip_changed
10821 || XFASTINT (w->last_modified) < MODIFF
10822 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
10823 && resize_mini_window (w, 0))
10824 {
10825 /* Resized active mini-window to fit the size of what it is
10826 showing if its contents might have changed. */
10827 must_finish = 1;
10828 consider_all_windows_p = 1;
10829 ++windows_or_buffers_changed;
10830 ++update_mode_lines;
10831
10832 /* If window configuration was changed, frames may have been
10833 marked garbaged. Clear them or we will experience
10834 surprises wrt scrolling. */
10835 if (frame_garbaged)
10836 clear_garbaged_frames ();
10837 }
10838
10839
10840 /* If showing the region, and mark has changed, we must redisplay
10841 the whole window. The assignment to this_line_start_pos prevents
10842 the optimization directly below this if-statement. */
10843 if (((!NILP (Vtransient_mark_mode)
10844 && !NILP (XBUFFER (w->buffer)->mark_active))
10845 != !NILP (w->region_showing))
10846 || (!NILP (w->region_showing)
10847 && !EQ (w->region_showing,
10848 Fmarker_position (XBUFFER (w->buffer)->mark))))
10849 CHARPOS (this_line_start_pos) = 0;
10850
10851 /* Optimize the case that only the line containing the cursor in the
10852 selected window has changed. Variables starting with this_ are
10853 set in display_line and record information about the line
10854 containing the cursor. */
10855 tlbufpos = this_line_start_pos;
10856 tlendpos = this_line_end_pos;
10857 if (!consider_all_windows_p
10858 && CHARPOS (tlbufpos) > 0
10859 && NILP (w->update_mode_line)
10860 && !current_buffer->clip_changed
10861 && !current_buffer->prevent_redisplay_optimizations_p
10862 && FRAME_VISIBLE_P (XFRAME (w->frame))
10863 && !FRAME_OBSCURED_P (XFRAME (w->frame))
10864 /* Make sure recorded data applies to current buffer, etc. */
10865 && this_line_buffer == current_buffer
10866 && current_buffer == XBUFFER (w->buffer)
10867 && NILP (w->force_start)
10868 && NILP (w->optional_new_start)
10869 /* Point must be on the line that we have info recorded about. */
10870 && PT >= CHARPOS (tlbufpos)
10871 && PT <= Z - CHARPOS (tlendpos)
10872 /* All text outside that line, including its final newline,
10873 must be unchanged */
10874 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
10875 CHARPOS (tlendpos)))
10876 {
10877 if (CHARPOS (tlbufpos) > BEGV
10878 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
10879 && (CHARPOS (tlbufpos) == ZV
10880 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
10881 /* Former continuation line has disappeared by becoming empty */
10882 goto cancel;
10883 else if (XFASTINT (w->last_modified) < MODIFF
10884 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
10885 || MINI_WINDOW_P (w))
10886 {
10887 /* We have to handle the case of continuation around a
10888 wide-column character (See the comment in indent.c around
10889 line 885).
10890
10891 For instance, in the following case:
10892
10893 -------- Insert --------
10894 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
10895 J_I_ ==> J_I_ `^^' are cursors.
10896 ^^ ^^
10897 -------- --------
10898
10899 As we have to redraw the line above, we should goto cancel. */
10900
10901 struct it it;
10902 int line_height_before = this_line_pixel_height;
10903
10904 /* Note that start_display will handle the case that the
10905 line starting at tlbufpos is a continuation lines. */
10906 start_display (&it, w, tlbufpos);
10907
10908 /* Implementation note: It this still necessary? */
10909 if (it.current_x != this_line_start_x)
10910 goto cancel;
10911
10912 TRACE ((stderr, "trying display optimization 1\n"));
10913 w->cursor.vpos = -1;
10914 overlay_arrow_seen = 0;
10915 it.vpos = this_line_vpos;
10916 it.current_y = this_line_y;
10917 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
10918 display_line (&it);
10919
10920 /* If line contains point, is not continued,
10921 and ends at same distance from eob as before, we win */
10922 if (w->cursor.vpos >= 0
10923 /* Line is not continued, otherwise this_line_start_pos
10924 would have been set to 0 in display_line. */
10925 && CHARPOS (this_line_start_pos)
10926 /* Line ends as before. */
10927 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
10928 /* Line has same height as before. Otherwise other lines
10929 would have to be shifted up or down. */
10930 && this_line_pixel_height == line_height_before)
10931 {
10932 /* If this is not the window's last line, we must adjust
10933 the charstarts of the lines below. */
10934 if (it.current_y < it.last_visible_y)
10935 {
10936 struct glyph_row *row
10937 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
10938 int delta, delta_bytes;
10939
10940 if (Z - CHARPOS (tlendpos) == ZV)
10941 {
10942 /* This line ends at end of (accessible part of)
10943 buffer. There is no newline to count. */
10944 delta = (Z
10945 - CHARPOS (tlendpos)
10946 - MATRIX_ROW_START_CHARPOS (row));
10947 delta_bytes = (Z_BYTE
10948 - BYTEPOS (tlendpos)
10949 - MATRIX_ROW_START_BYTEPOS (row));
10950 }
10951 else
10952 {
10953 /* This line ends in a newline. Must take
10954 account of the newline and the rest of the
10955 text that follows. */
10956 delta = (Z
10957 - CHARPOS (tlendpos)
10958 - MATRIX_ROW_START_CHARPOS (row));
10959 delta_bytes = (Z_BYTE
10960 - BYTEPOS (tlendpos)
10961 - MATRIX_ROW_START_BYTEPOS (row));
10962 }
10963
10964 increment_matrix_positions (w->current_matrix,
10965 this_line_vpos + 1,
10966 w->current_matrix->nrows,
10967 delta, delta_bytes);
10968 }
10969
10970 /* If this row displays text now but previously didn't,
10971 or vice versa, w->window_end_vpos may have to be
10972 adjusted. */
10973 if ((it.glyph_row - 1)->displays_text_p)
10974 {
10975 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
10976 XSETINT (w->window_end_vpos, this_line_vpos);
10977 }
10978 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
10979 && this_line_vpos > 0)
10980 XSETINT (w->window_end_vpos, this_line_vpos - 1);
10981 w->window_end_valid = Qnil;
10982
10983 /* Update hint: No need to try to scroll in update_window. */
10984 w->desired_matrix->no_scrolling_p = 1;
10985
10986 #if GLYPH_DEBUG
10987 *w->desired_matrix->method = 0;
10988 debug_method_add (w, "optimization 1");
10989 #endif
10990 #ifdef HAVE_WINDOW_SYSTEM
10991 update_window_fringes (w, 0);
10992 #endif
10993 goto update;
10994 }
10995 else
10996 goto cancel;
10997 }
10998 else if (/* Cursor position hasn't changed. */
10999 PT == XFASTINT (w->last_point)
11000 /* Make sure the cursor was last displayed
11001 in this window. Otherwise we have to reposition it. */
11002 && 0 <= w->cursor.vpos
11003 && WINDOW_TOTAL_LINES (w) > w->cursor.vpos)
11004 {
11005 if (!must_finish)
11006 {
11007 do_pending_window_change (1);
11008
11009 /* We used to always goto end_of_redisplay here, but this
11010 isn't enough if we have a blinking cursor. */
11011 if (w->cursor_off_p == w->last_cursor_off_p)
11012 goto end_of_redisplay;
11013 }
11014 goto update;
11015 }
11016 /* If highlighting the region, or if the cursor is in the echo area,
11017 then we can't just move the cursor. */
11018 else if (! (!NILP (Vtransient_mark_mode)
11019 && !NILP (current_buffer->mark_active))
11020 && (EQ (selected_window, current_buffer->last_selected_window)
11021 || highlight_nonselected_windows)
11022 && NILP (w->region_showing)
11023 && NILP (Vshow_trailing_whitespace)
11024 && !cursor_in_echo_area)
11025 {
11026 struct it it;
11027 struct glyph_row *row;
11028
11029 /* Skip from tlbufpos to PT and see where it is. Note that
11030 PT may be in invisible text. If so, we will end at the
11031 next visible position. */
11032 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
11033 NULL, DEFAULT_FACE_ID);
11034 it.current_x = this_line_start_x;
11035 it.current_y = this_line_y;
11036 it.vpos = this_line_vpos;
11037
11038 /* The call to move_it_to stops in front of PT, but
11039 moves over before-strings. */
11040 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
11041
11042 if (it.vpos == this_line_vpos
11043 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
11044 row->enabled_p))
11045 {
11046 xassert (this_line_vpos == it.vpos);
11047 xassert (this_line_y == it.current_y);
11048 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
11049 #if GLYPH_DEBUG
11050 *w->desired_matrix->method = 0;
11051 debug_method_add (w, "optimization 3");
11052 #endif
11053 goto update;
11054 }
11055 else
11056 goto cancel;
11057 }
11058
11059 cancel:
11060 /* Text changed drastically or point moved off of line. */
11061 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
11062 }
11063
11064 CHARPOS (this_line_start_pos) = 0;
11065 consider_all_windows_p |= buffer_shared > 1;
11066 ++clear_face_cache_count;
11067 #ifdef HAVE_WINDOW_SYSTEM
11068 ++clear_image_cache_count;
11069 #endif
11070
11071 /* Build desired matrices, and update the display. If
11072 consider_all_windows_p is non-zero, do it for all windows on all
11073 frames. Otherwise do it for selected_window, only. */
11074
11075 if (consider_all_windows_p)
11076 {
11077 Lisp_Object tail, frame;
11078
11079 FOR_EACH_FRAME (tail, frame)
11080 XFRAME (frame)->updated_p = 0;
11081
11082 /* Recompute # windows showing selected buffer. This will be
11083 incremented each time such a window is displayed. */
11084 buffer_shared = 0;
11085
11086 FOR_EACH_FRAME (tail, frame)
11087 {
11088 struct frame *f = XFRAME (frame);
11089
11090 if (FRAME_WINDOW_P (f) || f == sf)
11091 {
11092 if (! EQ (frame, selected_frame))
11093 /* Select the frame, for the sake of frame-local
11094 variables. */
11095 select_frame_for_redisplay (frame);
11096
11097 /* Mark all the scroll bars to be removed; we'll redeem
11098 the ones we want when we redisplay their windows. */
11099 if (condemn_scroll_bars_hook)
11100 condemn_scroll_bars_hook (f);
11101
11102 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
11103 redisplay_windows (FRAME_ROOT_WINDOW (f));
11104
11105 /* Any scroll bars which redisplay_windows should have
11106 nuked should now go away. */
11107 if (judge_scroll_bars_hook)
11108 judge_scroll_bars_hook (f);
11109
11110 /* If fonts changed, display again. */
11111 /* ??? rms: I suspect it is a mistake to jump all the way
11112 back to retry here. It should just retry this frame. */
11113 if (fonts_changed_p)
11114 goto retry;
11115
11116 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
11117 {
11118 /* See if we have to hscroll. */
11119 if (!f->already_hscrolled_p)
11120 {
11121 f->already_hscrolled_p = 1;
11122 if (hscroll_windows (f->root_window))
11123 goto retry;
11124 }
11125
11126 /* Prevent various kinds of signals during display
11127 update. stdio is not robust about handling
11128 signals, which can cause an apparent I/O
11129 error. */
11130 if (interrupt_input)
11131 unrequest_sigio ();
11132 STOP_POLLING;
11133
11134 /* Update the display. */
11135 set_window_update_flags (XWINDOW (f->root_window), 1);
11136 pause |= update_frame (f, 0, 0);
11137 #if 0 /* Exiting the loop can leave the wrong value for buffer_shared. */
11138 if (pause)
11139 break;
11140 #endif
11141
11142 f->updated_p = 1;
11143 }
11144 }
11145 }
11146
11147 if (!pause)
11148 {
11149 /* Do the mark_window_display_accurate after all windows have
11150 been redisplayed because this call resets flags in buffers
11151 which are needed for proper redisplay. */
11152 FOR_EACH_FRAME (tail, frame)
11153 {
11154 struct frame *f = XFRAME (frame);
11155 if (f->updated_p)
11156 {
11157 mark_window_display_accurate (f->root_window, 1);
11158 if (frame_up_to_date_hook)
11159 frame_up_to_date_hook (f);
11160 }
11161 }
11162 }
11163 }
11164 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
11165 {
11166 Lisp_Object mini_window;
11167 struct frame *mini_frame;
11168
11169 displayed_buffer = XBUFFER (XWINDOW (selected_window)->buffer);
11170 /* Use list_of_error, not Qerror, so that
11171 we catch only errors and don't run the debugger. */
11172 internal_condition_case_1 (redisplay_window_1, selected_window,
11173 list_of_error,
11174 redisplay_window_error);
11175
11176 /* Compare desired and current matrices, perform output. */
11177
11178 update:
11179 /* If fonts changed, display again. */
11180 if (fonts_changed_p)
11181 goto retry;
11182
11183 /* Prevent various kinds of signals during display update.
11184 stdio is not robust about handling signals,
11185 which can cause an apparent I/O error. */
11186 if (interrupt_input)
11187 unrequest_sigio ();
11188 STOP_POLLING;
11189
11190 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
11191 {
11192 if (hscroll_windows (selected_window))
11193 goto retry;
11194
11195 XWINDOW (selected_window)->must_be_updated_p = 1;
11196 pause = update_frame (sf, 0, 0);
11197 }
11198
11199 /* We may have called echo_area_display at the top of this
11200 function. If the echo area is on another frame, that may
11201 have put text on a frame other than the selected one, so the
11202 above call to update_frame would not have caught it. Catch
11203 it here. */
11204 mini_window = FRAME_MINIBUF_WINDOW (sf);
11205 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
11206
11207 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
11208 {
11209 XWINDOW (mini_window)->must_be_updated_p = 1;
11210 pause |= update_frame (mini_frame, 0, 0);
11211 if (!pause && hscroll_windows (mini_window))
11212 goto retry;
11213 }
11214 }
11215
11216 /* If display was paused because of pending input, make sure we do a
11217 thorough update the next time. */
11218 if (pause)
11219 {
11220 /* Prevent the optimization at the beginning of
11221 redisplay_internal that tries a single-line update of the
11222 line containing the cursor in the selected window. */
11223 CHARPOS (this_line_start_pos) = 0;
11224
11225 /* Let the overlay arrow be updated the next time. */
11226 update_overlay_arrows (0);
11227
11228 /* If we pause after scrolling, some rows in the current
11229 matrices of some windows are not valid. */
11230 if (!WINDOW_FULL_WIDTH_P (w)
11231 && !FRAME_WINDOW_P (XFRAME (w->frame)))
11232 update_mode_lines = 1;
11233 }
11234 else
11235 {
11236 if (!consider_all_windows_p)
11237 {
11238 /* This has already been done above if
11239 consider_all_windows_p is set. */
11240 mark_window_display_accurate_1 (w, 1);
11241
11242 /* Say overlay arrows are up to date. */
11243 update_overlay_arrows (1);
11244
11245 if (frame_up_to_date_hook != 0)
11246 frame_up_to_date_hook (sf);
11247 }
11248
11249 update_mode_lines = 0;
11250 windows_or_buffers_changed = 0;
11251 cursor_type_changed = 0;
11252 }
11253
11254 /* Start SIGIO interrupts coming again. Having them off during the
11255 code above makes it less likely one will discard output, but not
11256 impossible, since there might be stuff in the system buffer here.
11257 But it is much hairier to try to do anything about that. */
11258 if (interrupt_input)
11259 request_sigio ();
11260 RESUME_POLLING;
11261
11262 /* If a frame has become visible which was not before, redisplay
11263 again, so that we display it. Expose events for such a frame
11264 (which it gets when becoming visible) don't call the parts of
11265 redisplay constructing glyphs, so simply exposing a frame won't
11266 display anything in this case. So, we have to display these
11267 frames here explicitly. */
11268 if (!pause)
11269 {
11270 Lisp_Object tail, frame;
11271 int new_count = 0;
11272
11273 FOR_EACH_FRAME (tail, frame)
11274 {
11275 int this_is_visible = 0;
11276
11277 if (XFRAME (frame)->visible)
11278 this_is_visible = 1;
11279 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
11280 if (XFRAME (frame)->visible)
11281 this_is_visible = 1;
11282
11283 if (this_is_visible)
11284 new_count++;
11285 }
11286
11287 if (new_count != number_of_visible_frames)
11288 windows_or_buffers_changed++;
11289 }
11290
11291 /* Change frame size now if a change is pending. */
11292 do_pending_window_change (1);
11293
11294 /* If we just did a pending size change, or have additional
11295 visible frames, redisplay again. */
11296 if (windows_or_buffers_changed && !pause)
11297 goto retry;
11298
11299 /* Clear the face cache eventually. */
11300 if (consider_all_windows_p)
11301 {
11302 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
11303 {
11304 clear_face_cache (0);
11305 clear_face_cache_count = 0;
11306 }
11307 #ifdef HAVE_WINDOW_SYSTEM
11308 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
11309 {
11310 Lisp_Object tail, frame;
11311 FOR_EACH_FRAME (tail, frame)
11312 {
11313 struct frame *f = XFRAME (frame);
11314 if (FRAME_WINDOW_P (f))
11315 clear_image_cache (f, 0);
11316 }
11317 clear_image_cache_count = 0;
11318 }
11319 #endif /* HAVE_WINDOW_SYSTEM */
11320 }
11321
11322 end_of_redisplay:
11323 unbind_to (count, Qnil);
11324 RESUME_POLLING;
11325 }
11326
11327
11328 /* Redisplay, but leave alone any recent echo area message unless
11329 another message has been requested in its place.
11330
11331 This is useful in situations where you need to redisplay but no
11332 user action has occurred, making it inappropriate for the message
11333 area to be cleared. See tracking_off and
11334 wait_reading_process_output for examples of these situations.
11335
11336 FROM_WHERE is an integer saying from where this function was
11337 called. This is useful for debugging. */
11338
11339 void
11340 redisplay_preserve_echo_area (from_where)
11341 int from_where;
11342 {
11343 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
11344
11345 if (!NILP (echo_area_buffer[1]))
11346 {
11347 /* We have a previously displayed message, but no current
11348 message. Redisplay the previous message. */
11349 display_last_displayed_message_p = 1;
11350 redisplay_internal (1);
11351 display_last_displayed_message_p = 0;
11352 }
11353 else
11354 redisplay_internal (1);
11355
11356 if (rif != NULL && rif->flush_display_optional)
11357 rif->flush_display_optional (NULL);
11358 }
11359
11360
11361 /* Function registered with record_unwind_protect in
11362 redisplay_internal. Reset redisplaying_p to the value it had
11363 before redisplay_internal was called, and clear
11364 prevent_freeing_realized_faces_p. It also selects the previously
11365 selected frame. */
11366
11367 static Lisp_Object
11368 unwind_redisplay (val)
11369 Lisp_Object val;
11370 {
11371 Lisp_Object old_redisplaying_p, old_frame;
11372
11373 old_redisplaying_p = XCAR (val);
11374 redisplaying_p = XFASTINT (old_redisplaying_p);
11375 old_frame = XCDR (val);
11376 if (! EQ (old_frame, selected_frame))
11377 select_frame_for_redisplay (old_frame);
11378 return Qnil;
11379 }
11380
11381
11382 /* Mark the display of window W as accurate or inaccurate. If
11383 ACCURATE_P is non-zero mark display of W as accurate. If
11384 ACCURATE_P is zero, arrange for W to be redisplayed the next time
11385 redisplay_internal is called. */
11386
11387 static void
11388 mark_window_display_accurate_1 (w, accurate_p)
11389 struct window *w;
11390 int accurate_p;
11391 {
11392 if (BUFFERP (w->buffer))
11393 {
11394 struct buffer *b = XBUFFER (w->buffer);
11395
11396 w->last_modified
11397 = make_number (accurate_p ? BUF_MODIFF (b) : 0);
11398 w->last_overlay_modified
11399 = make_number (accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
11400 w->last_had_star
11401 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b) ? Qt : Qnil;
11402
11403 if (accurate_p)
11404 {
11405 b->clip_changed = 0;
11406 b->prevent_redisplay_optimizations_p = 0;
11407
11408 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
11409 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
11410 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
11411 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
11412
11413 w->current_matrix->buffer = b;
11414 w->current_matrix->begv = BUF_BEGV (b);
11415 w->current_matrix->zv = BUF_ZV (b);
11416
11417 w->last_cursor = w->cursor;
11418 w->last_cursor_off_p = w->cursor_off_p;
11419
11420 if (w == XWINDOW (selected_window))
11421 w->last_point = make_number (BUF_PT (b));
11422 else
11423 w->last_point = make_number (XMARKER (w->pointm)->charpos);
11424 }
11425 }
11426
11427 if (accurate_p)
11428 {
11429 w->window_end_valid = w->buffer;
11430 #if 0 /* This is incorrect with variable-height lines. */
11431 xassert (XINT (w->window_end_vpos)
11432 < (WINDOW_TOTAL_LINES (w)
11433 - (WINDOW_WANTS_MODELINE_P (w) ? 1 : 0)));
11434 #endif
11435 w->update_mode_line = Qnil;
11436 }
11437 }
11438
11439
11440 /* Mark the display of windows in the window tree rooted at WINDOW as
11441 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
11442 windows as accurate. If ACCURATE_P is zero, arrange for windows to
11443 be redisplayed the next time redisplay_internal is called. */
11444
11445 void
11446 mark_window_display_accurate (window, accurate_p)
11447 Lisp_Object window;
11448 int accurate_p;
11449 {
11450 struct window *w;
11451
11452 for (; !NILP (window); window = w->next)
11453 {
11454 w = XWINDOW (window);
11455 mark_window_display_accurate_1 (w, accurate_p);
11456
11457 if (!NILP (w->vchild))
11458 mark_window_display_accurate (w->vchild, accurate_p);
11459 if (!NILP (w->hchild))
11460 mark_window_display_accurate (w->hchild, accurate_p);
11461 }
11462
11463 if (accurate_p)
11464 {
11465 update_overlay_arrows (1);
11466 }
11467 else
11468 {
11469 /* Force a thorough redisplay the next time by setting
11470 last_arrow_position and last_arrow_string to t, which is
11471 unequal to any useful value of Voverlay_arrow_... */
11472 update_overlay_arrows (-1);
11473 }
11474 }
11475
11476
11477 /* Return value in display table DP (Lisp_Char_Table *) for character
11478 C. Since a display table doesn't have any parent, we don't have to
11479 follow parent. Do not call this function directly but use the
11480 macro DISP_CHAR_VECTOR. */
11481
11482 Lisp_Object
11483 disp_char_vector (dp, c)
11484 struct Lisp_Char_Table *dp;
11485 int c;
11486 {
11487 int code[4], i;
11488 Lisp_Object val;
11489
11490 if (SINGLE_BYTE_CHAR_P (c))
11491 return (dp->contents[c]);
11492
11493 SPLIT_CHAR (c, code[0], code[1], code[2]);
11494 if (code[1] < 32)
11495 code[1] = -1;
11496 else if (code[2] < 32)
11497 code[2] = -1;
11498
11499 /* Here, the possible range of code[0] (== charset ID) is
11500 128..max_charset. Since the top level char table contains data
11501 for multibyte characters after 256th element, we must increment
11502 code[0] by 128 to get a correct index. */
11503 code[0] += 128;
11504 code[3] = -1; /* anchor */
11505
11506 for (i = 0; code[i] >= 0; i++, dp = XCHAR_TABLE (val))
11507 {
11508 val = dp->contents[code[i]];
11509 if (!SUB_CHAR_TABLE_P (val))
11510 return (NILP (val) ? dp->defalt : val);
11511 }
11512
11513 /* Here, val is a sub char table. We return the default value of
11514 it. */
11515 return (dp->defalt);
11516 }
11517
11518
11519 \f
11520 /***********************************************************************
11521 Window Redisplay
11522 ***********************************************************************/
11523
11524 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
11525
11526 static void
11527 redisplay_windows (window)
11528 Lisp_Object window;
11529 {
11530 while (!NILP (window))
11531 {
11532 struct window *w = XWINDOW (window);
11533
11534 if (!NILP (w->hchild))
11535 redisplay_windows (w->hchild);
11536 else if (!NILP (w->vchild))
11537 redisplay_windows (w->vchild);
11538 else
11539 {
11540 displayed_buffer = XBUFFER (w->buffer);
11541 /* Use list_of_error, not Qerror, so that
11542 we catch only errors and don't run the debugger. */
11543 internal_condition_case_1 (redisplay_window_0, window,
11544 list_of_error,
11545 redisplay_window_error);
11546 }
11547
11548 window = w->next;
11549 }
11550 }
11551
11552 static Lisp_Object
11553 redisplay_window_error ()
11554 {
11555 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
11556 return Qnil;
11557 }
11558
11559 static Lisp_Object
11560 redisplay_window_0 (window)
11561 Lisp_Object window;
11562 {
11563 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
11564 redisplay_window (window, 0);
11565 return Qnil;
11566 }
11567
11568 static Lisp_Object
11569 redisplay_window_1 (window)
11570 Lisp_Object window;
11571 {
11572 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
11573 redisplay_window (window, 1);
11574 return Qnil;
11575 }
11576 \f
11577
11578 /* Increment GLYPH until it reaches END or CONDITION fails while
11579 adding (GLYPH)->pixel_width to X. */
11580
11581 #define SKIP_GLYPHS(glyph, end, x, condition) \
11582 do \
11583 { \
11584 (x) += (glyph)->pixel_width; \
11585 ++(glyph); \
11586 } \
11587 while ((glyph) < (end) && (condition))
11588
11589
11590 /* Set cursor position of W. PT is assumed to be displayed in ROW.
11591 DELTA is the number of bytes by which positions recorded in ROW
11592 differ from current buffer positions. */
11593
11594 void
11595 set_cursor_from_row (w, row, matrix, delta, delta_bytes, dy, dvpos)
11596 struct window *w;
11597 struct glyph_row *row;
11598 struct glyph_matrix *matrix;
11599 int delta, delta_bytes, dy, dvpos;
11600 {
11601 struct glyph *glyph = row->glyphs[TEXT_AREA];
11602 struct glyph *end = glyph + row->used[TEXT_AREA];
11603 struct glyph *cursor = NULL;
11604 /* The first glyph that starts a sequence of glyphs from string. */
11605 struct glyph *string_start;
11606 /* The X coordinate of string_start. */
11607 int string_start_x;
11608 /* The last known character position. */
11609 int last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
11610 /* The last known character position before string_start. */
11611 int string_before_pos;
11612 int x = row->x;
11613 int cursor_x = x;
11614 int cursor_from_overlay_pos = 0;
11615 int pt_old = PT - delta;
11616
11617 /* Skip over glyphs not having an object at the start of the row.
11618 These are special glyphs like truncation marks on terminal
11619 frames. */
11620 if (row->displays_text_p)
11621 while (glyph < end
11622 && INTEGERP (glyph->object)
11623 && glyph->charpos < 0)
11624 {
11625 x += glyph->pixel_width;
11626 ++glyph;
11627 }
11628
11629 string_start = NULL;
11630 while (glyph < end
11631 && !INTEGERP (glyph->object)
11632 && (!BUFFERP (glyph->object)
11633 || (last_pos = glyph->charpos) < pt_old))
11634 {
11635 if (! STRINGP (glyph->object))
11636 {
11637 string_start = NULL;
11638 x += glyph->pixel_width;
11639 ++glyph;
11640 if (cursor_from_overlay_pos
11641 && last_pos >= cursor_from_overlay_pos)
11642 {
11643 cursor_from_overlay_pos = 0;
11644 cursor = 0;
11645 }
11646 }
11647 else
11648 {
11649 string_before_pos = last_pos;
11650 string_start = glyph;
11651 string_start_x = x;
11652 /* Skip all glyphs from string. */
11653 do
11654 {
11655 Lisp_Object cprop;
11656 int pos;
11657 if ((cursor == NULL || glyph > cursor)
11658 && (cprop = Fget_char_property (make_number ((glyph)->charpos),
11659 Qcursor, (glyph)->object),
11660 !NILP (cprop))
11661 && (pos = string_buffer_position (w, glyph->object,
11662 string_before_pos),
11663 (pos == 0 /* From overlay */
11664 || pos == pt_old)))
11665 {
11666 /* Estimate overlay buffer position from the buffer
11667 positions of the glyphs before and after the overlay.
11668 Add 1 to last_pos so that if point corresponds to the
11669 glyph right after the overlay, we still use a 'cursor'
11670 property found in that overlay. */
11671 cursor_from_overlay_pos = (pos ? 0 : last_pos
11672 + (INTEGERP (cprop) ? XINT (cprop) : 0));
11673 cursor = glyph;
11674 cursor_x = x;
11675 }
11676 x += glyph->pixel_width;
11677 ++glyph;
11678 }
11679 while (glyph < end && EQ (glyph->object, string_start->object));
11680 }
11681 }
11682
11683 if (cursor != NULL)
11684 {
11685 glyph = cursor;
11686 x = cursor_x;
11687 }
11688 else if (row->ends_in_ellipsis_p && glyph == end)
11689 {
11690 /* Scan back over the ellipsis glyphs, decrementing positions. */
11691 while (glyph > row->glyphs[TEXT_AREA]
11692 && (glyph - 1)->charpos == last_pos)
11693 glyph--, x -= glyph->pixel_width;
11694 /* That loop always goes one position too far,
11695 including the glyph before the ellipsis.
11696 So scan forward over that one. */
11697 x += glyph->pixel_width;
11698 glyph++;
11699 }
11700 else if (string_start
11701 && (glyph == end || !BUFFERP (glyph->object) || last_pos > pt_old))
11702 {
11703 /* We may have skipped over point because the previous glyphs
11704 are from string. As there's no easy way to know the
11705 character position of the current glyph, find the correct
11706 glyph on point by scanning from string_start again. */
11707 Lisp_Object limit;
11708 Lisp_Object string;
11709 int pos;
11710
11711 limit = make_number (pt_old + 1);
11712 end = glyph;
11713 glyph = string_start;
11714 x = string_start_x;
11715 string = glyph->object;
11716 pos = string_buffer_position (w, string, string_before_pos);
11717 /* If STRING is from overlay, LAST_POS == 0. We skip such glyphs
11718 because we always put cursor after overlay strings. */
11719 while (pos == 0 && glyph < end)
11720 {
11721 string = glyph->object;
11722 SKIP_GLYPHS (glyph, end, x, EQ (glyph->object, string));
11723 if (glyph < end)
11724 pos = string_buffer_position (w, glyph->object, string_before_pos);
11725 }
11726
11727 while (glyph < end)
11728 {
11729 pos = XINT (Fnext_single_char_property_change
11730 (make_number (pos), Qdisplay, Qnil, limit));
11731 if (pos > pt_old)
11732 break;
11733 /* Skip glyphs from the same string. */
11734 string = glyph->object;
11735 SKIP_GLYPHS (glyph, end, x, EQ (glyph->object, string));
11736 /* Skip glyphs from an overlay. */
11737 while (glyph < end
11738 && ! string_buffer_position (w, glyph->object, pos))
11739 {
11740 string = glyph->object;
11741 SKIP_GLYPHS (glyph, end, x, EQ (glyph->object, string));
11742 }
11743 }
11744 }
11745
11746 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
11747 w->cursor.x = x;
11748 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
11749 w->cursor.y = row->y + dy;
11750
11751 if (w == XWINDOW (selected_window))
11752 {
11753 if (!row->continued_p
11754 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
11755 && row->x == 0)
11756 {
11757 this_line_buffer = XBUFFER (w->buffer);
11758
11759 CHARPOS (this_line_start_pos)
11760 = MATRIX_ROW_START_CHARPOS (row) + delta;
11761 BYTEPOS (this_line_start_pos)
11762 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
11763
11764 CHARPOS (this_line_end_pos)
11765 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
11766 BYTEPOS (this_line_end_pos)
11767 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
11768
11769 this_line_y = w->cursor.y;
11770 this_line_pixel_height = row->height;
11771 this_line_vpos = w->cursor.vpos;
11772 this_line_start_x = row->x;
11773 }
11774 else
11775 CHARPOS (this_line_start_pos) = 0;
11776 }
11777 }
11778
11779
11780 /* Run window scroll functions, if any, for WINDOW with new window
11781 start STARTP. Sets the window start of WINDOW to that position.
11782
11783 We assume that the window's buffer is really current. */
11784
11785 static INLINE struct text_pos
11786 run_window_scroll_functions (window, startp)
11787 Lisp_Object window;
11788 struct text_pos startp;
11789 {
11790 struct window *w = XWINDOW (window);
11791 SET_MARKER_FROM_TEXT_POS (w->start, startp);
11792
11793 if (current_buffer != XBUFFER (w->buffer))
11794 abort ();
11795
11796 if (!NILP (Vwindow_scroll_functions))
11797 {
11798 run_hook_with_args_2 (Qwindow_scroll_functions, window,
11799 make_number (CHARPOS (startp)));
11800 SET_TEXT_POS_FROM_MARKER (startp, w->start);
11801 /* In case the hook functions switch buffers. */
11802 if (current_buffer != XBUFFER (w->buffer))
11803 set_buffer_internal_1 (XBUFFER (w->buffer));
11804 }
11805
11806 return startp;
11807 }
11808
11809
11810 /* Make sure the line containing the cursor is fully visible.
11811 A value of 1 means there is nothing to be done.
11812 (Either the line is fully visible, or it cannot be made so,
11813 or we cannot tell.)
11814
11815 If FORCE_P is non-zero, return 0 even if partial visible cursor row
11816 is higher than window.
11817
11818 A value of 0 means the caller should do scrolling
11819 as if point had gone off the screen. */
11820
11821 static int
11822 cursor_row_fully_visible_p (w, force_p, current_matrix_p)
11823 struct window *w;
11824 int force_p;
11825 int current_matrix_p;
11826 {
11827 struct glyph_matrix *matrix;
11828 struct glyph_row *row;
11829 int window_height;
11830
11831 if (!make_cursor_line_fully_visible_p)
11832 return 1;
11833
11834 /* It's not always possible to find the cursor, e.g, when a window
11835 is full of overlay strings. Don't do anything in that case. */
11836 if (w->cursor.vpos < 0)
11837 return 1;
11838
11839 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
11840 row = MATRIX_ROW (matrix, w->cursor.vpos);
11841
11842 /* If the cursor row is not partially visible, there's nothing to do. */
11843 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
11844 return 1;
11845
11846 /* If the row the cursor is in is taller than the window's height,
11847 it's not clear what to do, so do nothing. */
11848 window_height = window_box_height (w);
11849 if (row->height >= window_height)
11850 {
11851 if (!force_p || MINI_WINDOW_P (w) || w->vscroll)
11852 return 1;
11853 }
11854 return 0;
11855
11856 #if 0
11857 /* This code used to try to scroll the window just enough to make
11858 the line visible. It returned 0 to say that the caller should
11859 allocate larger glyph matrices. */
11860
11861 if (MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (w, row))
11862 {
11863 int dy = row->height - row->visible_height;
11864 w->vscroll = 0;
11865 w->cursor.y += dy;
11866 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
11867 }
11868 else /* MATRIX_ROW_PARTIALLY_VISIBLE_AT_BOTTOM_P (w, row)) */
11869 {
11870 int dy = - (row->height - row->visible_height);
11871 w->vscroll = dy;
11872 w->cursor.y += dy;
11873 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
11874 }
11875
11876 /* When we change the cursor y-position of the selected window,
11877 change this_line_y as well so that the display optimization for
11878 the cursor line of the selected window in redisplay_internal uses
11879 the correct y-position. */
11880 if (w == XWINDOW (selected_window))
11881 this_line_y = w->cursor.y;
11882
11883 /* If vscrolling requires a larger glyph matrix, arrange for a fresh
11884 redisplay with larger matrices. */
11885 if (matrix->nrows < required_matrix_height (w))
11886 {
11887 fonts_changed_p = 1;
11888 return 0;
11889 }
11890
11891 return 1;
11892 #endif /* 0 */
11893 }
11894
11895
11896 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
11897 non-zero means only WINDOW is redisplayed in redisplay_internal.
11898 TEMP_SCROLL_STEP has the same meaning as scroll_step, and is used
11899 in redisplay_window to bring a partially visible line into view in
11900 the case that only the cursor has moved.
11901
11902 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
11903 last screen line's vertical height extends past the end of the screen.
11904
11905 Value is
11906
11907 1 if scrolling succeeded
11908
11909 0 if scrolling didn't find point.
11910
11911 -1 if new fonts have been loaded so that we must interrupt
11912 redisplay, adjust glyph matrices, and try again. */
11913
11914 enum
11915 {
11916 SCROLLING_SUCCESS,
11917 SCROLLING_FAILED,
11918 SCROLLING_NEED_LARGER_MATRICES
11919 };
11920
11921 static int
11922 try_scrolling (window, just_this_one_p, scroll_conservatively,
11923 scroll_step, temp_scroll_step, last_line_misfit)
11924 Lisp_Object window;
11925 int just_this_one_p;
11926 EMACS_INT scroll_conservatively, scroll_step;
11927 int temp_scroll_step;
11928 int last_line_misfit;
11929 {
11930 struct window *w = XWINDOW (window);
11931 struct frame *f = XFRAME (w->frame);
11932 struct text_pos scroll_margin_pos;
11933 struct text_pos pos;
11934 struct text_pos startp;
11935 struct it it;
11936 Lisp_Object window_end;
11937 int this_scroll_margin;
11938 int dy = 0;
11939 int scroll_max;
11940 int rc;
11941 int amount_to_scroll = 0;
11942 Lisp_Object aggressive;
11943 int height;
11944 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
11945
11946 #if GLYPH_DEBUG
11947 debug_method_add (w, "try_scrolling");
11948 #endif
11949
11950 SET_TEXT_POS_FROM_MARKER (startp, w->start);
11951
11952 /* Compute scroll margin height in pixels. We scroll when point is
11953 within this distance from the top or bottom of the window. */
11954 if (scroll_margin > 0)
11955 {
11956 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
11957 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
11958 }
11959 else
11960 this_scroll_margin = 0;
11961
11962 /* Force scroll_conservatively to have a reasonable value so it doesn't
11963 cause an overflow while computing how much to scroll. */
11964 if (scroll_conservatively)
11965 scroll_conservatively = min (scroll_conservatively,
11966 MOST_POSITIVE_FIXNUM / FRAME_LINE_HEIGHT (f));
11967
11968 /* Compute how much we should try to scroll maximally to bring point
11969 into view. */
11970 if (scroll_step || scroll_conservatively || temp_scroll_step)
11971 scroll_max = max (scroll_step,
11972 max (scroll_conservatively, temp_scroll_step));
11973 else if (NUMBERP (current_buffer->scroll_down_aggressively)
11974 || NUMBERP (current_buffer->scroll_up_aggressively))
11975 /* We're trying to scroll because of aggressive scrolling
11976 but no scroll_step is set. Choose an arbitrary one. Maybe
11977 there should be a variable for this. */
11978 scroll_max = 10;
11979 else
11980 scroll_max = 0;
11981 scroll_max *= FRAME_LINE_HEIGHT (f);
11982
11983 /* Decide whether we have to scroll down. Start at the window end
11984 and move this_scroll_margin up to find the position of the scroll
11985 margin. */
11986 window_end = Fwindow_end (window, Qt);
11987
11988 too_near_end:
11989
11990 CHARPOS (scroll_margin_pos) = XINT (window_end);
11991 BYTEPOS (scroll_margin_pos) = CHAR_TO_BYTE (CHARPOS (scroll_margin_pos));
11992
11993 if (this_scroll_margin || extra_scroll_margin_lines)
11994 {
11995 start_display (&it, w, scroll_margin_pos);
11996 if (this_scroll_margin)
11997 move_it_vertically_backward (&it, this_scroll_margin);
11998 if (extra_scroll_margin_lines)
11999 move_it_by_lines (&it, - extra_scroll_margin_lines, 0);
12000 scroll_margin_pos = it.current.pos;
12001 }
12002
12003 if (PT >= CHARPOS (scroll_margin_pos))
12004 {
12005 int y0;
12006
12007 /* Point is in the scroll margin at the bottom of the window, or
12008 below. Compute a new window start that makes point visible. */
12009
12010 /* Compute the distance from the scroll margin to PT.
12011 Give up if the distance is greater than scroll_max. */
12012 start_display (&it, w, scroll_margin_pos);
12013 y0 = it.current_y;
12014 move_it_to (&it, PT, 0, it.last_visible_y, -1,
12015 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
12016
12017 /* To make point visible, we have to move the window start
12018 down so that the line the cursor is in is visible, which
12019 means we have to add in the height of the cursor line. */
12020 dy = line_bottom_y (&it) - y0;
12021
12022 if (dy > scroll_max)
12023 return SCROLLING_FAILED;
12024
12025 /* Move the window start down. If scrolling conservatively,
12026 move it just enough down to make point visible. If
12027 scroll_step is set, move it down by scroll_step. */
12028 start_display (&it, w, startp);
12029
12030 if (scroll_conservatively)
12031 /* Set AMOUNT_TO_SCROLL to at least one line,
12032 and at most scroll_conservatively lines. */
12033 amount_to_scroll
12034 = min (max (dy, FRAME_LINE_HEIGHT (f)),
12035 FRAME_LINE_HEIGHT (f) * scroll_conservatively);
12036 else if (scroll_step || temp_scroll_step)
12037 amount_to_scroll = scroll_max;
12038 else
12039 {
12040 aggressive = current_buffer->scroll_up_aggressively;
12041 height = WINDOW_BOX_TEXT_HEIGHT (w);
12042 if (NUMBERP (aggressive))
12043 {
12044 double float_amount = XFLOATINT (aggressive) * height;
12045 amount_to_scroll = float_amount;
12046 if (amount_to_scroll == 0 && float_amount > 0)
12047 amount_to_scroll = 1;
12048 }
12049 }
12050
12051 if (amount_to_scroll <= 0)
12052 return SCROLLING_FAILED;
12053
12054 /* If moving by amount_to_scroll leaves STARTP unchanged,
12055 move it down one screen line. */
12056
12057 move_it_vertically (&it, amount_to_scroll);
12058 if (CHARPOS (it.current.pos) == CHARPOS (startp))
12059 move_it_by_lines (&it, 1, 1);
12060 startp = it.current.pos;
12061 }
12062 else
12063 {
12064 /* See if point is inside the scroll margin at the top of the
12065 window. */
12066 scroll_margin_pos = startp;
12067 if (this_scroll_margin)
12068 {
12069 start_display (&it, w, startp);
12070 move_it_vertically (&it, this_scroll_margin);
12071 scroll_margin_pos = it.current.pos;
12072 }
12073
12074 if (PT < CHARPOS (scroll_margin_pos))
12075 {
12076 /* Point is in the scroll margin at the top of the window or
12077 above what is displayed in the window. */
12078 int y0;
12079
12080 /* Compute the vertical distance from PT to the scroll
12081 margin position. Give up if distance is greater than
12082 scroll_max. */
12083 SET_TEXT_POS (pos, PT, PT_BYTE);
12084 start_display (&it, w, pos);
12085 y0 = it.current_y;
12086 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
12087 it.last_visible_y, -1,
12088 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
12089 dy = it.current_y - y0;
12090 if (dy > scroll_max)
12091 return SCROLLING_FAILED;
12092
12093 /* Compute new window start. */
12094 start_display (&it, w, startp);
12095
12096 if (scroll_conservatively)
12097 amount_to_scroll
12098 = max (dy, FRAME_LINE_HEIGHT (f) * max (scroll_step, temp_scroll_step));
12099 else if (scroll_step || temp_scroll_step)
12100 amount_to_scroll = scroll_max;
12101 else
12102 {
12103 aggressive = current_buffer->scroll_down_aggressively;
12104 height = WINDOW_BOX_TEXT_HEIGHT (w);
12105 if (NUMBERP (aggressive))
12106 {
12107 double float_amount = XFLOATINT (aggressive) * height;
12108 amount_to_scroll = float_amount;
12109 if (amount_to_scroll == 0 && float_amount > 0)
12110 amount_to_scroll = 1;
12111 }
12112 }
12113
12114 if (amount_to_scroll <= 0)
12115 return SCROLLING_FAILED;
12116
12117 move_it_vertically_backward (&it, amount_to_scroll);
12118 startp = it.current.pos;
12119 }
12120 }
12121
12122 /* Run window scroll functions. */
12123 startp = run_window_scroll_functions (window, startp);
12124
12125 /* Display the window. Give up if new fonts are loaded, or if point
12126 doesn't appear. */
12127 if (!try_window (window, startp, 0))
12128 rc = SCROLLING_NEED_LARGER_MATRICES;
12129 else if (w->cursor.vpos < 0)
12130 {
12131 clear_glyph_matrix (w->desired_matrix);
12132 rc = SCROLLING_FAILED;
12133 }
12134 else
12135 {
12136 /* Maybe forget recorded base line for line number display. */
12137 if (!just_this_one_p
12138 || current_buffer->clip_changed
12139 || BEG_UNCHANGED < CHARPOS (startp))
12140 w->base_line_number = Qnil;
12141
12142 /* If cursor ends up on a partially visible line,
12143 treat that as being off the bottom of the screen. */
12144 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1, 0))
12145 {
12146 clear_glyph_matrix (w->desired_matrix);
12147 ++extra_scroll_margin_lines;
12148 goto too_near_end;
12149 }
12150 rc = SCROLLING_SUCCESS;
12151 }
12152
12153 return rc;
12154 }
12155
12156
12157 /* Compute a suitable window start for window W if display of W starts
12158 on a continuation line. Value is non-zero if a new window start
12159 was computed.
12160
12161 The new window start will be computed, based on W's width, starting
12162 from the start of the continued line. It is the start of the
12163 screen line with the minimum distance from the old start W->start. */
12164
12165 static int
12166 compute_window_start_on_continuation_line (w)
12167 struct window *w;
12168 {
12169 struct text_pos pos, start_pos;
12170 int window_start_changed_p = 0;
12171
12172 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
12173
12174 /* If window start is on a continuation line... Window start may be
12175 < BEGV in case there's invisible text at the start of the
12176 buffer (M-x rmail, for example). */
12177 if (CHARPOS (start_pos) > BEGV
12178 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
12179 {
12180 struct it it;
12181 struct glyph_row *row;
12182
12183 /* Handle the case that the window start is out of range. */
12184 if (CHARPOS (start_pos) < BEGV)
12185 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
12186 else if (CHARPOS (start_pos) > ZV)
12187 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
12188
12189 /* Find the start of the continued line. This should be fast
12190 because scan_buffer is fast (newline cache). */
12191 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
12192 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
12193 row, DEFAULT_FACE_ID);
12194 reseat_at_previous_visible_line_start (&it);
12195
12196 /* If the line start is "too far" away from the window start,
12197 say it takes too much time to compute a new window start. */
12198 if (CHARPOS (start_pos) - IT_CHARPOS (it)
12199 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
12200 {
12201 int min_distance, distance;
12202
12203 /* Move forward by display lines to find the new window
12204 start. If window width was enlarged, the new start can
12205 be expected to be > the old start. If window width was
12206 decreased, the new window start will be < the old start.
12207 So, we're looking for the display line start with the
12208 minimum distance from the old window start. */
12209 pos = it.current.pos;
12210 min_distance = INFINITY;
12211 while ((distance = abs (CHARPOS (start_pos) - IT_CHARPOS (it))),
12212 distance < min_distance)
12213 {
12214 min_distance = distance;
12215 pos = it.current.pos;
12216 move_it_by_lines (&it, 1, 0);
12217 }
12218
12219 /* Set the window start there. */
12220 SET_MARKER_FROM_TEXT_POS (w->start, pos);
12221 window_start_changed_p = 1;
12222 }
12223 }
12224
12225 return window_start_changed_p;
12226 }
12227
12228
12229 /* Try cursor movement in case text has not changed in window WINDOW,
12230 with window start STARTP. Value is
12231
12232 CURSOR_MOVEMENT_SUCCESS if successful
12233
12234 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
12235
12236 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
12237 display. *SCROLL_STEP is set to 1, under certain circumstances, if
12238 we want to scroll as if scroll-step were set to 1. See the code.
12239
12240 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
12241 which case we have to abort this redisplay, and adjust matrices
12242 first. */
12243
12244 enum
12245 {
12246 CURSOR_MOVEMENT_SUCCESS,
12247 CURSOR_MOVEMENT_CANNOT_BE_USED,
12248 CURSOR_MOVEMENT_MUST_SCROLL,
12249 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
12250 };
12251
12252 static int
12253 try_cursor_movement (window, startp, scroll_step)
12254 Lisp_Object window;
12255 struct text_pos startp;
12256 int *scroll_step;
12257 {
12258 struct window *w = XWINDOW (window);
12259 struct frame *f = XFRAME (w->frame);
12260 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
12261
12262 #if GLYPH_DEBUG
12263 if (inhibit_try_cursor_movement)
12264 return rc;
12265 #endif
12266
12267 /* Handle case where text has not changed, only point, and it has
12268 not moved off the frame. */
12269 if (/* Point may be in this window. */
12270 PT >= CHARPOS (startp)
12271 /* Selective display hasn't changed. */
12272 && !current_buffer->clip_changed
12273 /* Function force-mode-line-update is used to force a thorough
12274 redisplay. It sets either windows_or_buffers_changed or
12275 update_mode_lines. So don't take a shortcut here for these
12276 cases. */
12277 && !update_mode_lines
12278 && !windows_or_buffers_changed
12279 && !cursor_type_changed
12280 /* Can't use this case if highlighting a region. When a
12281 region exists, cursor movement has to do more than just
12282 set the cursor. */
12283 && !(!NILP (Vtransient_mark_mode)
12284 && !NILP (current_buffer->mark_active))
12285 && NILP (w->region_showing)
12286 && NILP (Vshow_trailing_whitespace)
12287 /* Right after splitting windows, last_point may be nil. */
12288 && INTEGERP (w->last_point)
12289 /* This code is not used for mini-buffer for the sake of the case
12290 of redisplaying to replace an echo area message; since in
12291 that case the mini-buffer contents per se are usually
12292 unchanged. This code is of no real use in the mini-buffer
12293 since the handling of this_line_start_pos, etc., in redisplay
12294 handles the same cases. */
12295 && !EQ (window, minibuf_window)
12296 /* When splitting windows or for new windows, it happens that
12297 redisplay is called with a nil window_end_vpos or one being
12298 larger than the window. This should really be fixed in
12299 window.c. I don't have this on my list, now, so we do
12300 approximately the same as the old redisplay code. --gerd. */
12301 && INTEGERP (w->window_end_vpos)
12302 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
12303 && (FRAME_WINDOW_P (f)
12304 || !overlay_arrow_in_current_buffer_p ()))
12305 {
12306 int this_scroll_margin, top_scroll_margin;
12307 struct glyph_row *row = NULL;
12308
12309 #if GLYPH_DEBUG
12310 debug_method_add (w, "cursor movement");
12311 #endif
12312
12313 /* Scroll if point within this distance from the top or bottom
12314 of the window. This is a pixel value. */
12315 this_scroll_margin = max (0, scroll_margin);
12316 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
12317 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
12318
12319 top_scroll_margin = this_scroll_margin;
12320 if (WINDOW_WANTS_HEADER_LINE_P (w))
12321 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
12322
12323 /* Start with the row the cursor was displayed during the last
12324 not paused redisplay. Give up if that row is not valid. */
12325 if (w->last_cursor.vpos < 0
12326 || w->last_cursor.vpos >= w->current_matrix->nrows)
12327 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12328 else
12329 {
12330 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
12331 if (row->mode_line_p)
12332 ++row;
12333 if (!row->enabled_p)
12334 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12335 }
12336
12337 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
12338 {
12339 int scroll_p = 0;
12340 int last_y = window_text_bottom_y (w) - this_scroll_margin;
12341
12342 if (PT > XFASTINT (w->last_point))
12343 {
12344 /* Point has moved forward. */
12345 while (MATRIX_ROW_END_CHARPOS (row) < PT
12346 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
12347 {
12348 xassert (row->enabled_p);
12349 ++row;
12350 }
12351
12352 /* The end position of a row equals the start position
12353 of the next row. If PT is there, we would rather
12354 display it in the next line. */
12355 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
12356 && MATRIX_ROW_END_CHARPOS (row) == PT
12357 && !cursor_row_p (w, row))
12358 ++row;
12359
12360 /* If within the scroll margin, scroll. Note that
12361 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
12362 the next line would be drawn, and that
12363 this_scroll_margin can be zero. */
12364 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
12365 || PT > MATRIX_ROW_END_CHARPOS (row)
12366 /* Line is completely visible last line in window
12367 and PT is to be set in the next line. */
12368 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
12369 && PT == MATRIX_ROW_END_CHARPOS (row)
12370 && !row->ends_at_zv_p
12371 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
12372 scroll_p = 1;
12373 }
12374 else if (PT < XFASTINT (w->last_point))
12375 {
12376 /* Cursor has to be moved backward. Note that PT >=
12377 CHARPOS (startp) because of the outer if-statement. */
12378 while (!row->mode_line_p
12379 && (MATRIX_ROW_START_CHARPOS (row) > PT
12380 || (MATRIX_ROW_START_CHARPOS (row) == PT
12381 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
12382 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
12383 row > w->current_matrix->rows
12384 && (row-1)->ends_in_newline_from_string_p))))
12385 && (row->y > top_scroll_margin
12386 || CHARPOS (startp) == BEGV))
12387 {
12388 xassert (row->enabled_p);
12389 --row;
12390 }
12391
12392 /* Consider the following case: Window starts at BEGV,
12393 there is invisible, intangible text at BEGV, so that
12394 display starts at some point START > BEGV. It can
12395 happen that we are called with PT somewhere between
12396 BEGV and START. Try to handle that case. */
12397 if (row < w->current_matrix->rows
12398 || row->mode_line_p)
12399 {
12400 row = w->current_matrix->rows;
12401 if (row->mode_line_p)
12402 ++row;
12403 }
12404
12405 /* Due to newlines in overlay strings, we may have to
12406 skip forward over overlay strings. */
12407 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
12408 && MATRIX_ROW_END_CHARPOS (row) == PT
12409 && !cursor_row_p (w, row))
12410 ++row;
12411
12412 /* If within the scroll margin, scroll. */
12413 if (row->y < top_scroll_margin
12414 && CHARPOS (startp) != BEGV)
12415 scroll_p = 1;
12416 }
12417 else
12418 {
12419 /* Cursor did not move. So don't scroll even if cursor line
12420 is partially visible, as it was so before. */
12421 rc = CURSOR_MOVEMENT_SUCCESS;
12422 }
12423
12424 if (PT < MATRIX_ROW_START_CHARPOS (row)
12425 || PT > MATRIX_ROW_END_CHARPOS (row))
12426 {
12427 /* if PT is not in the glyph row, give up. */
12428 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12429 }
12430 else if (rc != CURSOR_MOVEMENT_SUCCESS
12431 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
12432 && make_cursor_line_fully_visible_p)
12433 {
12434 if (PT == MATRIX_ROW_END_CHARPOS (row)
12435 && !row->ends_at_zv_p
12436 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
12437 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12438 else if (row->height > window_box_height (w))
12439 {
12440 /* If we end up in a partially visible line, let's
12441 make it fully visible, except when it's taller
12442 than the window, in which case we can't do much
12443 about it. */
12444 *scroll_step = 1;
12445 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12446 }
12447 else
12448 {
12449 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
12450 if (!cursor_row_fully_visible_p (w, 0, 1))
12451 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12452 else
12453 rc = CURSOR_MOVEMENT_SUCCESS;
12454 }
12455 }
12456 else if (scroll_p)
12457 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12458 else
12459 {
12460 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
12461 rc = CURSOR_MOVEMENT_SUCCESS;
12462 }
12463 }
12464 }
12465
12466 return rc;
12467 }
12468
12469 void
12470 set_vertical_scroll_bar (w)
12471 struct window *w;
12472 {
12473 int start, end, whole;
12474
12475 /* Calculate the start and end positions for the current window.
12476 At some point, it would be nice to choose between scrollbars
12477 which reflect the whole buffer size, with special markers
12478 indicating narrowing, and scrollbars which reflect only the
12479 visible region.
12480
12481 Note that mini-buffers sometimes aren't displaying any text. */
12482 if (!MINI_WINDOW_P (w)
12483 || (w == XWINDOW (minibuf_window)
12484 && NILP (echo_area_buffer[0])))
12485 {
12486 struct buffer *buf = XBUFFER (w->buffer);
12487 whole = BUF_ZV (buf) - BUF_BEGV (buf);
12488 start = marker_position (w->start) - BUF_BEGV (buf);
12489 /* I don't think this is guaranteed to be right. For the
12490 moment, we'll pretend it is. */
12491 end = BUF_Z (buf) - XFASTINT (w->window_end_pos) - BUF_BEGV (buf);
12492
12493 if (end < start)
12494 end = start;
12495 if (whole < (end - start))
12496 whole = end - start;
12497 }
12498 else
12499 start = end = whole = 0;
12500
12501 /* Indicate what this scroll bar ought to be displaying now. */
12502 set_vertical_scroll_bar_hook (w, end - start, whole, start);
12503 }
12504
12505
12506 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
12507 selected_window is redisplayed.
12508
12509 We can return without actually redisplaying the window if
12510 fonts_changed_p is nonzero. In that case, redisplay_internal will
12511 retry. */
12512
12513 static void
12514 redisplay_window (window, just_this_one_p)
12515 Lisp_Object window;
12516 int just_this_one_p;
12517 {
12518 struct window *w = XWINDOW (window);
12519 struct frame *f = XFRAME (w->frame);
12520 struct buffer *buffer = XBUFFER (w->buffer);
12521 struct buffer *old = current_buffer;
12522 struct text_pos lpoint, opoint, startp;
12523 int update_mode_line;
12524 int tem;
12525 struct it it;
12526 /* Record it now because it's overwritten. */
12527 int current_matrix_up_to_date_p = 0;
12528 int used_current_matrix_p = 0;
12529 /* This is less strict than current_matrix_up_to_date_p.
12530 It indictes that the buffer contents and narrowing are unchanged. */
12531 int buffer_unchanged_p = 0;
12532 int temp_scroll_step = 0;
12533 int count = SPECPDL_INDEX ();
12534 int rc;
12535 int centering_position = -1;
12536 int last_line_misfit = 0;
12537
12538 SET_TEXT_POS (lpoint, PT, PT_BYTE);
12539 opoint = lpoint;
12540
12541 /* W must be a leaf window here. */
12542 xassert (!NILP (w->buffer));
12543 #if GLYPH_DEBUG
12544 *w->desired_matrix->method = 0;
12545 #endif
12546
12547 specbind (Qinhibit_point_motion_hooks, Qt);
12548
12549 reconsider_clip_changes (w, buffer);
12550
12551 /* Has the mode line to be updated? */
12552 update_mode_line = (!NILP (w->update_mode_line)
12553 || update_mode_lines
12554 || buffer->clip_changed
12555 || buffer->prevent_redisplay_optimizations_p);
12556
12557 if (MINI_WINDOW_P (w))
12558 {
12559 if (w == XWINDOW (echo_area_window)
12560 && !NILP (echo_area_buffer[0]))
12561 {
12562 if (update_mode_line)
12563 /* We may have to update a tty frame's menu bar or a
12564 tool-bar. Example `M-x C-h C-h C-g'. */
12565 goto finish_menu_bars;
12566 else
12567 /* We've already displayed the echo area glyphs in this window. */
12568 goto finish_scroll_bars;
12569 }
12570 else if ((w != XWINDOW (minibuf_window)
12571 || minibuf_level == 0)
12572 /* When buffer is nonempty, redisplay window normally. */
12573 && BUF_Z (XBUFFER (w->buffer)) == BUF_BEG (XBUFFER (w->buffer))
12574 /* Quail displays non-mini buffers in minibuffer window.
12575 In that case, redisplay the window normally. */
12576 && !NILP (Fmemq (w->buffer, Vminibuffer_list)))
12577 {
12578 /* W is a mini-buffer window, but it's not active, so clear
12579 it. */
12580 int yb = window_text_bottom_y (w);
12581 struct glyph_row *row;
12582 int y;
12583
12584 for (y = 0, row = w->desired_matrix->rows;
12585 y < yb;
12586 y += row->height, ++row)
12587 blank_row (w, row, y);
12588 goto finish_scroll_bars;
12589 }
12590
12591 clear_glyph_matrix (w->desired_matrix);
12592 }
12593
12594 /* Otherwise set up data on this window; select its buffer and point
12595 value. */
12596 /* Really select the buffer, for the sake of buffer-local
12597 variables. */
12598 set_buffer_internal_1 (XBUFFER (w->buffer));
12599 SET_TEXT_POS (opoint, PT, PT_BYTE);
12600
12601 current_matrix_up_to_date_p
12602 = (!NILP (w->window_end_valid)
12603 && !current_buffer->clip_changed
12604 && !current_buffer->prevent_redisplay_optimizations_p
12605 && XFASTINT (w->last_modified) >= MODIFF
12606 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
12607
12608 buffer_unchanged_p
12609 = (!NILP (w->window_end_valid)
12610 && !current_buffer->clip_changed
12611 && XFASTINT (w->last_modified) >= MODIFF
12612 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
12613
12614 /* When windows_or_buffers_changed is non-zero, we can't rely on
12615 the window end being valid, so set it to nil there. */
12616 if (windows_or_buffers_changed)
12617 {
12618 /* If window starts on a continuation line, maybe adjust the
12619 window start in case the window's width changed. */
12620 if (XMARKER (w->start)->buffer == current_buffer)
12621 compute_window_start_on_continuation_line (w);
12622
12623 w->window_end_valid = Qnil;
12624 }
12625
12626 /* Some sanity checks. */
12627 CHECK_WINDOW_END (w);
12628 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
12629 abort ();
12630 if (BYTEPOS (opoint) < CHARPOS (opoint))
12631 abort ();
12632
12633 /* If %c is in mode line, update it if needed. */
12634 if (!NILP (w->column_number_displayed)
12635 /* This alternative quickly identifies a common case
12636 where no change is needed. */
12637 && !(PT == XFASTINT (w->last_point)
12638 && XFASTINT (w->last_modified) >= MODIFF
12639 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
12640 && (XFASTINT (w->column_number_displayed)
12641 != (int) current_column ())) /* iftc */
12642 update_mode_line = 1;
12643
12644 /* Count number of windows showing the selected buffer. An indirect
12645 buffer counts as its base buffer. */
12646 if (!just_this_one_p)
12647 {
12648 struct buffer *current_base, *window_base;
12649 current_base = current_buffer;
12650 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
12651 if (current_base->base_buffer)
12652 current_base = current_base->base_buffer;
12653 if (window_base->base_buffer)
12654 window_base = window_base->base_buffer;
12655 if (current_base == window_base)
12656 buffer_shared++;
12657 }
12658
12659 /* Point refers normally to the selected window. For any other
12660 window, set up appropriate value. */
12661 if (!EQ (window, selected_window))
12662 {
12663 int new_pt = XMARKER (w->pointm)->charpos;
12664 int new_pt_byte = marker_byte_position (w->pointm);
12665 if (new_pt < BEGV)
12666 {
12667 new_pt = BEGV;
12668 new_pt_byte = BEGV_BYTE;
12669 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
12670 }
12671 else if (new_pt > (ZV - 1))
12672 {
12673 new_pt = ZV;
12674 new_pt_byte = ZV_BYTE;
12675 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
12676 }
12677
12678 /* We don't use SET_PT so that the point-motion hooks don't run. */
12679 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
12680 }
12681
12682 /* If any of the character widths specified in the display table
12683 have changed, invalidate the width run cache. It's true that
12684 this may be a bit late to catch such changes, but the rest of
12685 redisplay goes (non-fatally) haywire when the display table is
12686 changed, so why should we worry about doing any better? */
12687 if (current_buffer->width_run_cache)
12688 {
12689 struct Lisp_Char_Table *disptab = buffer_display_table ();
12690
12691 if (! disptab_matches_widthtab (disptab,
12692 XVECTOR (current_buffer->width_table)))
12693 {
12694 invalidate_region_cache (current_buffer,
12695 current_buffer->width_run_cache,
12696 BEG, Z);
12697 recompute_width_table (current_buffer, disptab);
12698 }
12699 }
12700
12701 /* If window-start is screwed up, choose a new one. */
12702 if (XMARKER (w->start)->buffer != current_buffer)
12703 goto recenter;
12704
12705 SET_TEXT_POS_FROM_MARKER (startp, w->start);
12706
12707 /* If someone specified a new starting point but did not insist,
12708 check whether it can be used. */
12709 if (!NILP (w->optional_new_start)
12710 && CHARPOS (startp) >= BEGV
12711 && CHARPOS (startp) <= ZV)
12712 {
12713 w->optional_new_start = Qnil;
12714 start_display (&it, w, startp);
12715 move_it_to (&it, PT, 0, it.last_visible_y, -1,
12716 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
12717 if (IT_CHARPOS (it) == PT)
12718 w->force_start = Qt;
12719 /* IT may overshoot PT if text at PT is invisible. */
12720 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
12721 w->force_start = Qt;
12722 }
12723
12724 /* Handle case where place to start displaying has been specified,
12725 unless the specified location is outside the accessible range. */
12726 if (!NILP (w->force_start)
12727 || w->frozen_window_start_p)
12728 {
12729 /* We set this later on if we have to adjust point. */
12730 int new_vpos = -1;
12731 int val;
12732
12733 w->force_start = Qnil;
12734 w->vscroll = 0;
12735 w->window_end_valid = Qnil;
12736
12737 /* Forget any recorded base line for line number display. */
12738 if (!buffer_unchanged_p)
12739 w->base_line_number = Qnil;
12740
12741 /* Redisplay the mode line. Select the buffer properly for that.
12742 Also, run the hook window-scroll-functions
12743 because we have scrolled. */
12744 /* Note, we do this after clearing force_start because
12745 if there's an error, it is better to forget about force_start
12746 than to get into an infinite loop calling the hook functions
12747 and having them get more errors. */
12748 if (!update_mode_line
12749 || ! NILP (Vwindow_scroll_functions))
12750 {
12751 update_mode_line = 1;
12752 w->update_mode_line = Qt;
12753 startp = run_window_scroll_functions (window, startp);
12754 }
12755
12756 w->last_modified = make_number (0);
12757 w->last_overlay_modified = make_number (0);
12758 if (CHARPOS (startp) < BEGV)
12759 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
12760 else if (CHARPOS (startp) > ZV)
12761 SET_TEXT_POS (startp, ZV, ZV_BYTE);
12762
12763 /* Redisplay, then check if cursor has been set during the
12764 redisplay. Give up if new fonts were loaded. */
12765 val = try_window (window, startp, 1);
12766 if (!val)
12767 {
12768 w->force_start = Qt;
12769 clear_glyph_matrix (w->desired_matrix);
12770 goto need_larger_matrices;
12771 }
12772 /* Point was outside the scroll margins. */
12773 if (val < 0)
12774 new_vpos = window_box_height (w) / 2;
12775
12776 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
12777 {
12778 /* If point does not appear, try to move point so it does
12779 appear. The desired matrix has been built above, so we
12780 can use it here. */
12781 new_vpos = window_box_height (w) / 2;
12782 }
12783
12784 if (!cursor_row_fully_visible_p (w, 0, 0))
12785 {
12786 /* Point does appear, but on a line partly visible at end of window.
12787 Move it back to a fully-visible line. */
12788 new_vpos = window_box_height (w);
12789 }
12790
12791 /* If we need to move point for either of the above reasons,
12792 now actually do it. */
12793 if (new_vpos >= 0)
12794 {
12795 struct glyph_row *row;
12796
12797 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
12798 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
12799 ++row;
12800
12801 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
12802 MATRIX_ROW_START_BYTEPOS (row));
12803
12804 if (w != XWINDOW (selected_window))
12805 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
12806 else if (current_buffer == old)
12807 SET_TEXT_POS (lpoint, PT, PT_BYTE);
12808
12809 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
12810
12811 /* If we are highlighting the region, then we just changed
12812 the region, so redisplay to show it. */
12813 if (!NILP (Vtransient_mark_mode)
12814 && !NILP (current_buffer->mark_active))
12815 {
12816 clear_glyph_matrix (w->desired_matrix);
12817 if (!try_window (window, startp, 0))
12818 goto need_larger_matrices;
12819 }
12820 }
12821
12822 #if GLYPH_DEBUG
12823 debug_method_add (w, "forced window start");
12824 #endif
12825 goto done;
12826 }
12827
12828 /* Handle case where text has not changed, only point, and it has
12829 not moved off the frame, and we are not retrying after hscroll.
12830 (current_matrix_up_to_date_p is nonzero when retrying.) */
12831 if (current_matrix_up_to_date_p
12832 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
12833 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
12834 {
12835 switch (rc)
12836 {
12837 case CURSOR_MOVEMENT_SUCCESS:
12838 used_current_matrix_p = 1;
12839 goto done;
12840
12841 #if 0 /* try_cursor_movement never returns this value. */
12842 case CURSOR_MOVEMENT_NEED_LARGER_MATRICES:
12843 goto need_larger_matrices;
12844 #endif
12845
12846 case CURSOR_MOVEMENT_MUST_SCROLL:
12847 goto try_to_scroll;
12848
12849 default:
12850 abort ();
12851 }
12852 }
12853 /* If current starting point was originally the beginning of a line
12854 but no longer is, find a new starting point. */
12855 else if (!NILP (w->start_at_line_beg)
12856 && !(CHARPOS (startp) <= BEGV
12857 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
12858 {
12859 #if GLYPH_DEBUG
12860 debug_method_add (w, "recenter 1");
12861 #endif
12862 goto recenter;
12863 }
12864
12865 /* Try scrolling with try_window_id. Value is > 0 if update has
12866 been done, it is -1 if we know that the same window start will
12867 not work. It is 0 if unsuccessful for some other reason. */
12868 else if ((tem = try_window_id (w)) != 0)
12869 {
12870 #if GLYPH_DEBUG
12871 debug_method_add (w, "try_window_id %d", tem);
12872 #endif
12873
12874 if (fonts_changed_p)
12875 goto need_larger_matrices;
12876 if (tem > 0)
12877 goto done;
12878
12879 /* Otherwise try_window_id has returned -1 which means that we
12880 don't want the alternative below this comment to execute. */
12881 }
12882 else if (CHARPOS (startp) >= BEGV
12883 && CHARPOS (startp) <= ZV
12884 && PT >= CHARPOS (startp)
12885 && (CHARPOS (startp) < ZV
12886 /* Avoid starting at end of buffer. */
12887 || CHARPOS (startp) == BEGV
12888 || (XFASTINT (w->last_modified) >= MODIFF
12889 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
12890 {
12891
12892 /* If first window line is a continuation line, and window start
12893 is inside the modified region, but the first change is before
12894 current window start, we must select a new window start.*/
12895 if (NILP (w->start_at_line_beg)
12896 && CHARPOS (startp) > BEGV)
12897 {
12898 /* Make sure beg_unchanged and end_unchanged are up to date.
12899 Do it only if buffer has really changed. This may or may
12900 not have been done by try_window_id (see which) already. */
12901 if (MODIFF > SAVE_MODIFF
12902 /* This seems to happen sometimes after saving a buffer. */
12903 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
12904 {
12905 if (GPT - BEG < BEG_UNCHANGED)
12906 BEG_UNCHANGED = GPT - BEG;
12907 if (Z - GPT < END_UNCHANGED)
12908 END_UNCHANGED = Z - GPT;
12909 }
12910
12911 if (CHARPOS (startp) > BEG + BEG_UNCHANGED
12912 && CHARPOS (startp) <= Z - END_UNCHANGED)
12913 {
12914 /* There doesn't seems to be a simple way to find a new
12915 window start that is near the old window start, so
12916 we just recenter. */
12917 goto recenter;
12918 }
12919 }
12920
12921 #if GLYPH_DEBUG
12922 debug_method_add (w, "same window start");
12923 #endif
12924
12925 /* Try to redisplay starting at same place as before.
12926 If point has not moved off frame, accept the results. */
12927 if (!current_matrix_up_to_date_p
12928 /* Don't use try_window_reusing_current_matrix in this case
12929 because a window scroll function can have changed the
12930 buffer. */
12931 || !NILP (Vwindow_scroll_functions)
12932 || MINI_WINDOW_P (w)
12933 || !(used_current_matrix_p
12934 = try_window_reusing_current_matrix (w)))
12935 {
12936 IF_DEBUG (debug_method_add (w, "1"));
12937 if (try_window (window, startp, 1) < 0)
12938 /* -1 means we need to scroll.
12939 0 means we need new matrices, but fonts_changed_p
12940 is set in that case, so we will detect it below. */
12941 goto try_to_scroll;
12942 }
12943
12944 if (fonts_changed_p)
12945 goto need_larger_matrices;
12946
12947 if (w->cursor.vpos >= 0)
12948 {
12949 if (!just_this_one_p
12950 || current_buffer->clip_changed
12951 || BEG_UNCHANGED < CHARPOS (startp))
12952 /* Forget any recorded base line for line number display. */
12953 w->base_line_number = Qnil;
12954
12955 if (!cursor_row_fully_visible_p (w, 1, 0))
12956 {
12957 clear_glyph_matrix (w->desired_matrix);
12958 last_line_misfit = 1;
12959 }
12960 /* Drop through and scroll. */
12961 else
12962 goto done;
12963 }
12964 else
12965 clear_glyph_matrix (w->desired_matrix);
12966 }
12967
12968 try_to_scroll:
12969
12970 w->last_modified = make_number (0);
12971 w->last_overlay_modified = make_number (0);
12972
12973 /* Redisplay the mode line. Select the buffer properly for that. */
12974 if (!update_mode_line)
12975 {
12976 update_mode_line = 1;
12977 w->update_mode_line = Qt;
12978 }
12979
12980 /* Try to scroll by specified few lines. */
12981 if ((scroll_conservatively
12982 || scroll_step
12983 || temp_scroll_step
12984 || NUMBERP (current_buffer->scroll_up_aggressively)
12985 || NUMBERP (current_buffer->scroll_down_aggressively))
12986 && !current_buffer->clip_changed
12987 && CHARPOS (startp) >= BEGV
12988 && CHARPOS (startp) <= ZV)
12989 {
12990 /* The function returns -1 if new fonts were loaded, 1 if
12991 successful, 0 if not successful. */
12992 int rc = try_scrolling (window, just_this_one_p,
12993 scroll_conservatively,
12994 scroll_step,
12995 temp_scroll_step, last_line_misfit);
12996 switch (rc)
12997 {
12998 case SCROLLING_SUCCESS:
12999 goto done;
13000
13001 case SCROLLING_NEED_LARGER_MATRICES:
13002 goto need_larger_matrices;
13003
13004 case SCROLLING_FAILED:
13005 break;
13006
13007 default:
13008 abort ();
13009 }
13010 }
13011
13012 /* Finally, just choose place to start which centers point */
13013
13014 recenter:
13015 if (centering_position < 0)
13016 centering_position = window_box_height (w) / 2;
13017
13018 #if GLYPH_DEBUG
13019 debug_method_add (w, "recenter");
13020 #endif
13021
13022 /* w->vscroll = 0; */
13023
13024 /* Forget any previously recorded base line for line number display. */
13025 if (!buffer_unchanged_p)
13026 w->base_line_number = Qnil;
13027
13028 /* Move backward half the height of the window. */
13029 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
13030 it.current_y = it.last_visible_y;
13031 move_it_vertically_backward (&it, centering_position);
13032 xassert (IT_CHARPOS (it) >= BEGV);
13033
13034 /* The function move_it_vertically_backward may move over more
13035 than the specified y-distance. If it->w is small, e.g. a
13036 mini-buffer window, we may end up in front of the window's
13037 display area. Start displaying at the start of the line
13038 containing PT in this case. */
13039 if (it.current_y <= 0)
13040 {
13041 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
13042 move_it_vertically_backward (&it, 0);
13043 #if 0
13044 /* I think this assert is bogus if buffer contains
13045 invisible text or images. KFS. */
13046 xassert (IT_CHARPOS (it) <= PT);
13047 #endif
13048 it.current_y = 0;
13049 }
13050
13051 it.current_x = it.hpos = 0;
13052
13053 /* Set startp here explicitly in case that helps avoid an infinite loop
13054 in case the window-scroll-functions functions get errors. */
13055 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
13056
13057 /* Run scroll hooks. */
13058 startp = run_window_scroll_functions (window, it.current.pos);
13059
13060 /* Redisplay the window. */
13061 if (!current_matrix_up_to_date_p
13062 || windows_or_buffers_changed
13063 || cursor_type_changed
13064 /* Don't use try_window_reusing_current_matrix in this case
13065 because it can have changed the buffer. */
13066 || !NILP (Vwindow_scroll_functions)
13067 || !just_this_one_p
13068 || MINI_WINDOW_P (w)
13069 || !(used_current_matrix_p
13070 = try_window_reusing_current_matrix (w)))
13071 try_window (window, startp, 0);
13072
13073 /* If new fonts have been loaded (due to fontsets), give up. We
13074 have to start a new redisplay since we need to re-adjust glyph
13075 matrices. */
13076 if (fonts_changed_p)
13077 goto need_larger_matrices;
13078
13079 /* If cursor did not appear assume that the middle of the window is
13080 in the first line of the window. Do it again with the next line.
13081 (Imagine a window of height 100, displaying two lines of height
13082 60. Moving back 50 from it->last_visible_y will end in the first
13083 line.) */
13084 if (w->cursor.vpos < 0)
13085 {
13086 if (!NILP (w->window_end_valid)
13087 && PT >= Z - XFASTINT (w->window_end_pos))
13088 {
13089 clear_glyph_matrix (w->desired_matrix);
13090 move_it_by_lines (&it, 1, 0);
13091 try_window (window, it.current.pos, 0);
13092 }
13093 else if (PT < IT_CHARPOS (it))
13094 {
13095 clear_glyph_matrix (w->desired_matrix);
13096 move_it_by_lines (&it, -1, 0);
13097 try_window (window, it.current.pos, 0);
13098 }
13099 else
13100 {
13101 /* Not much we can do about it. */
13102 }
13103 }
13104
13105 /* Consider the following case: Window starts at BEGV, there is
13106 invisible, intangible text at BEGV, so that display starts at
13107 some point START > BEGV. It can happen that we are called with
13108 PT somewhere between BEGV and START. Try to handle that case. */
13109 if (w->cursor.vpos < 0)
13110 {
13111 struct glyph_row *row = w->current_matrix->rows;
13112 if (row->mode_line_p)
13113 ++row;
13114 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13115 }
13116
13117 if (!cursor_row_fully_visible_p (w, 0, 0))
13118 {
13119 /* If vscroll is enabled, disable it and try again. */
13120 if (w->vscroll)
13121 {
13122 w->vscroll = 0;
13123 clear_glyph_matrix (w->desired_matrix);
13124 goto recenter;
13125 }
13126
13127 /* If centering point failed to make the whole line visible,
13128 put point at the top instead. That has to make the whole line
13129 visible, if it can be done. */
13130 if (centering_position == 0)
13131 goto done;
13132
13133 clear_glyph_matrix (w->desired_matrix);
13134 centering_position = 0;
13135 goto recenter;
13136 }
13137
13138 done:
13139
13140 SET_TEXT_POS_FROM_MARKER (startp, w->start);
13141 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
13142 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
13143 ? Qt : Qnil);
13144
13145 /* Display the mode line, if we must. */
13146 if ((update_mode_line
13147 /* If window not full width, must redo its mode line
13148 if (a) the window to its side is being redone and
13149 (b) we do a frame-based redisplay. This is a consequence
13150 of how inverted lines are drawn in frame-based redisplay. */
13151 || (!just_this_one_p
13152 && !FRAME_WINDOW_P (f)
13153 && !WINDOW_FULL_WIDTH_P (w))
13154 /* Line number to display. */
13155 || INTEGERP (w->base_line_pos)
13156 /* Column number is displayed and different from the one displayed. */
13157 || (!NILP (w->column_number_displayed)
13158 && (XFASTINT (w->column_number_displayed)
13159 != (int) current_column ()))) /* iftc */
13160 /* This means that the window has a mode line. */
13161 && (WINDOW_WANTS_MODELINE_P (w)
13162 || WINDOW_WANTS_HEADER_LINE_P (w)))
13163 {
13164 display_mode_lines (w);
13165
13166 /* If mode line height has changed, arrange for a thorough
13167 immediate redisplay using the correct mode line height. */
13168 if (WINDOW_WANTS_MODELINE_P (w)
13169 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
13170 {
13171 fonts_changed_p = 1;
13172 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
13173 = DESIRED_MODE_LINE_HEIGHT (w);
13174 }
13175
13176 /* If top line height has changed, arrange for a thorough
13177 immediate redisplay using the correct mode line height. */
13178 if (WINDOW_WANTS_HEADER_LINE_P (w)
13179 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
13180 {
13181 fonts_changed_p = 1;
13182 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
13183 = DESIRED_HEADER_LINE_HEIGHT (w);
13184 }
13185
13186 if (fonts_changed_p)
13187 goto need_larger_matrices;
13188 }
13189
13190 if (!line_number_displayed
13191 && !BUFFERP (w->base_line_pos))
13192 {
13193 w->base_line_pos = Qnil;
13194 w->base_line_number = Qnil;
13195 }
13196
13197 finish_menu_bars:
13198
13199 /* When we reach a frame's selected window, redo the frame's menu bar. */
13200 if (update_mode_line
13201 && EQ (FRAME_SELECTED_WINDOW (f), window))
13202 {
13203 int redisplay_menu_p = 0;
13204 int redisplay_tool_bar_p = 0;
13205
13206 if (FRAME_WINDOW_P (f))
13207 {
13208 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
13209 || defined (USE_GTK)
13210 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
13211 #else
13212 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
13213 #endif
13214 }
13215 else
13216 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
13217
13218 if (redisplay_menu_p)
13219 display_menu_bar (w);
13220
13221 #ifdef HAVE_WINDOW_SYSTEM
13222 #ifdef USE_GTK
13223 redisplay_tool_bar_p = FRAME_EXTERNAL_TOOL_BAR (f);
13224 #else
13225 redisplay_tool_bar_p = WINDOWP (f->tool_bar_window)
13226 && (FRAME_TOOL_BAR_LINES (f) > 0
13227 || auto_resize_tool_bars_p);
13228
13229 #endif
13230
13231 if (redisplay_tool_bar_p)
13232 redisplay_tool_bar (f);
13233 #endif
13234 }
13235
13236 #ifdef HAVE_WINDOW_SYSTEM
13237 if (FRAME_WINDOW_P (f)
13238 && update_window_fringes (w, (just_this_one_p
13239 || (!used_current_matrix_p && !overlay_arrow_seen)
13240 || w->pseudo_window_p)))
13241 {
13242 update_begin (f);
13243 BLOCK_INPUT;
13244 if (draw_window_fringes (w, 1))
13245 x_draw_vertical_border (w);
13246 UNBLOCK_INPUT;
13247 update_end (f);
13248 }
13249 #endif /* HAVE_WINDOW_SYSTEM */
13250
13251 /* We go to this label, with fonts_changed_p nonzero,
13252 if it is necessary to try again using larger glyph matrices.
13253 We have to redeem the scroll bar even in this case,
13254 because the loop in redisplay_internal expects that. */
13255 need_larger_matrices:
13256 ;
13257 finish_scroll_bars:
13258
13259 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
13260 {
13261 /* Set the thumb's position and size. */
13262 set_vertical_scroll_bar (w);
13263
13264 /* Note that we actually used the scroll bar attached to this
13265 window, so it shouldn't be deleted at the end of redisplay. */
13266 redeem_scroll_bar_hook (w);
13267 }
13268
13269 /* Restore current_buffer and value of point in it. */
13270 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
13271 set_buffer_internal_1 (old);
13272 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
13273
13274 unbind_to (count, Qnil);
13275 }
13276
13277
13278 /* Build the complete desired matrix of WINDOW with a window start
13279 buffer position POS.
13280
13281 Value is 1 if successful. It is zero if fonts were loaded during
13282 redisplay which makes re-adjusting glyph matrices necessary, and -1
13283 if point would appear in the scroll margins.
13284 (We check that only if CHECK_MARGINS is nonzero. */
13285
13286 int
13287 try_window (window, pos, check_margins)
13288 Lisp_Object window;
13289 struct text_pos pos;
13290 int check_margins;
13291 {
13292 struct window *w = XWINDOW (window);
13293 struct it it;
13294 struct glyph_row *last_text_row = NULL;
13295
13296 /* Make POS the new window start. */
13297 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
13298
13299 /* Mark cursor position as unknown. No overlay arrow seen. */
13300 w->cursor.vpos = -1;
13301 overlay_arrow_seen = 0;
13302
13303 /* Initialize iterator and info to start at POS. */
13304 start_display (&it, w, pos);
13305
13306 /* Display all lines of W. */
13307 while (it.current_y < it.last_visible_y)
13308 {
13309 if (display_line (&it))
13310 last_text_row = it.glyph_row - 1;
13311 if (fonts_changed_p)
13312 return 0;
13313 }
13314
13315 /* Don't let the cursor end in the scroll margins. */
13316 if (check_margins
13317 && !MINI_WINDOW_P (w))
13318 {
13319 int this_scroll_margin;
13320
13321 this_scroll_margin = max (0, scroll_margin);
13322 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
13323 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
13324
13325 if ((w->cursor.y < this_scroll_margin
13326 && CHARPOS (pos) > BEGV
13327 && IT_CHARPOS (it) < ZV)
13328 /* rms: considering make_cursor_line_fully_visible_p here
13329 seems to give wrong results. We don't want to recenter
13330 when the last line is partly visible, we want to allow
13331 that case to be handled in the usual way. */
13332 || (w->cursor.y + 1) > it.last_visible_y)
13333 {
13334 w->cursor.vpos = -1;
13335 clear_glyph_matrix (w->desired_matrix);
13336 return -1;
13337 }
13338 }
13339
13340 /* If bottom moved off end of frame, change mode line percentage. */
13341 if (XFASTINT (w->window_end_pos) <= 0
13342 && Z != IT_CHARPOS (it))
13343 w->update_mode_line = Qt;
13344
13345 /* Set window_end_pos to the offset of the last character displayed
13346 on the window from the end of current_buffer. Set
13347 window_end_vpos to its row number. */
13348 if (last_text_row)
13349 {
13350 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
13351 w->window_end_bytepos
13352 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
13353 w->window_end_pos
13354 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
13355 w->window_end_vpos
13356 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
13357 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
13358 ->displays_text_p);
13359 }
13360 else
13361 {
13362 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
13363 w->window_end_pos = make_number (Z - ZV);
13364 w->window_end_vpos = make_number (0);
13365 }
13366
13367 /* But that is not valid info until redisplay finishes. */
13368 w->window_end_valid = Qnil;
13369 return 1;
13370 }
13371
13372
13373 \f
13374 /************************************************************************
13375 Window redisplay reusing current matrix when buffer has not changed
13376 ************************************************************************/
13377
13378 /* Try redisplay of window W showing an unchanged buffer with a
13379 different window start than the last time it was displayed by
13380 reusing its current matrix. Value is non-zero if successful.
13381 W->start is the new window start. */
13382
13383 static int
13384 try_window_reusing_current_matrix (w)
13385 struct window *w;
13386 {
13387 struct frame *f = XFRAME (w->frame);
13388 struct glyph_row *row, *bottom_row;
13389 struct it it;
13390 struct run run;
13391 struct text_pos start, new_start;
13392 int nrows_scrolled, i;
13393 struct glyph_row *last_text_row;
13394 struct glyph_row *last_reused_text_row;
13395 struct glyph_row *start_row;
13396 int start_vpos, min_y, max_y;
13397
13398 #if GLYPH_DEBUG
13399 if (inhibit_try_window_reusing)
13400 return 0;
13401 #endif
13402
13403 if (/* This function doesn't handle terminal frames. */
13404 !FRAME_WINDOW_P (f)
13405 /* Don't try to reuse the display if windows have been split
13406 or such. */
13407 || windows_or_buffers_changed
13408 || cursor_type_changed)
13409 return 0;
13410
13411 /* Can't do this if region may have changed. */
13412 if ((!NILP (Vtransient_mark_mode)
13413 && !NILP (current_buffer->mark_active))
13414 || !NILP (w->region_showing)
13415 || !NILP (Vshow_trailing_whitespace))
13416 return 0;
13417
13418 /* If top-line visibility has changed, give up. */
13419 if (WINDOW_WANTS_HEADER_LINE_P (w)
13420 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
13421 return 0;
13422
13423 /* Give up if old or new display is scrolled vertically. We could
13424 make this function handle this, but right now it doesn't. */
13425 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
13426 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
13427 return 0;
13428
13429 /* The variable new_start now holds the new window start. The old
13430 start `start' can be determined from the current matrix. */
13431 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
13432 start = start_row->start.pos;
13433 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
13434
13435 /* Clear the desired matrix for the display below. */
13436 clear_glyph_matrix (w->desired_matrix);
13437
13438 if (CHARPOS (new_start) <= CHARPOS (start))
13439 {
13440 int first_row_y;
13441
13442 /* Don't use this method if the display starts with an ellipsis
13443 displayed for invisible text. It's not easy to handle that case
13444 below, and it's certainly not worth the effort since this is
13445 not a frequent case. */
13446 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
13447 return 0;
13448
13449 IF_DEBUG (debug_method_add (w, "twu1"));
13450
13451 /* Display up to a row that can be reused. The variable
13452 last_text_row is set to the last row displayed that displays
13453 text. Note that it.vpos == 0 if or if not there is a
13454 header-line; it's not the same as the MATRIX_ROW_VPOS! */
13455 start_display (&it, w, new_start);
13456 first_row_y = it.current_y;
13457 w->cursor.vpos = -1;
13458 last_text_row = last_reused_text_row = NULL;
13459
13460 while (it.current_y < it.last_visible_y
13461 && !fonts_changed_p)
13462 {
13463 /* If we have reached into the characters in the START row,
13464 that means the line boundaries have changed. So we
13465 can't start copying with the row START. Maybe it will
13466 work to start copying with the following row. */
13467 while (IT_CHARPOS (it) > CHARPOS (start))
13468 {
13469 /* Advance to the next row as the "start". */
13470 start_row++;
13471 start = start_row->start.pos;
13472 /* If there are no more rows to try, or just one, give up. */
13473 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
13474 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
13475 || CHARPOS (start) == ZV)
13476 {
13477 clear_glyph_matrix (w->desired_matrix);
13478 return 0;
13479 }
13480
13481 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
13482 }
13483 /* If we have reached alignment,
13484 we can copy the rest of the rows. */
13485 if (IT_CHARPOS (it) == CHARPOS (start))
13486 break;
13487
13488 if (display_line (&it))
13489 last_text_row = it.glyph_row - 1;
13490 }
13491
13492 /* A value of current_y < last_visible_y means that we stopped
13493 at the previous window start, which in turn means that we
13494 have at least one reusable row. */
13495 if (it.current_y < it.last_visible_y)
13496 {
13497 /* IT.vpos always starts from 0; it counts text lines. */
13498 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
13499
13500 /* Find PT if not already found in the lines displayed. */
13501 if (w->cursor.vpos < 0)
13502 {
13503 int dy = it.current_y - start_row->y;
13504
13505 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
13506 row = row_containing_pos (w, PT, row, NULL, dy);
13507 if (row)
13508 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
13509 dy, nrows_scrolled);
13510 else
13511 {
13512 clear_glyph_matrix (w->desired_matrix);
13513 return 0;
13514 }
13515 }
13516
13517 /* Scroll the display. Do it before the current matrix is
13518 changed. The problem here is that update has not yet
13519 run, i.e. part of the current matrix is not up to date.
13520 scroll_run_hook will clear the cursor, and use the
13521 current matrix to get the height of the row the cursor is
13522 in. */
13523 run.current_y = start_row->y;
13524 run.desired_y = it.current_y;
13525 run.height = it.last_visible_y - it.current_y;
13526
13527 if (run.height > 0 && run.current_y != run.desired_y)
13528 {
13529 update_begin (f);
13530 rif->update_window_begin_hook (w);
13531 rif->clear_window_mouse_face (w);
13532 rif->scroll_run_hook (w, &run);
13533 rif->update_window_end_hook (w, 0, 0);
13534 update_end (f);
13535 }
13536
13537 /* Shift current matrix down by nrows_scrolled lines. */
13538 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
13539 rotate_matrix (w->current_matrix,
13540 start_vpos,
13541 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
13542 nrows_scrolled);
13543
13544 /* Disable lines that must be updated. */
13545 for (i = 0; i < it.vpos; ++i)
13546 (start_row + i)->enabled_p = 0;
13547
13548 /* Re-compute Y positions. */
13549 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
13550 max_y = it.last_visible_y;
13551 for (row = start_row + nrows_scrolled;
13552 row < bottom_row;
13553 ++row)
13554 {
13555 row->y = it.current_y;
13556 row->visible_height = row->height;
13557
13558 if (row->y < min_y)
13559 row->visible_height -= min_y - row->y;
13560 if (row->y + row->height > max_y)
13561 row->visible_height -= row->y + row->height - max_y;
13562 row->redraw_fringe_bitmaps_p = 1;
13563
13564 it.current_y += row->height;
13565
13566 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
13567 last_reused_text_row = row;
13568 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
13569 break;
13570 }
13571
13572 /* Disable lines in the current matrix which are now
13573 below the window. */
13574 for (++row; row < bottom_row; ++row)
13575 row->enabled_p = row->mode_line_p = 0;
13576 }
13577
13578 /* Update window_end_pos etc.; last_reused_text_row is the last
13579 reused row from the current matrix containing text, if any.
13580 The value of last_text_row is the last displayed line
13581 containing text. */
13582 if (last_reused_text_row)
13583 {
13584 w->window_end_bytepos
13585 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
13586 w->window_end_pos
13587 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
13588 w->window_end_vpos
13589 = make_number (MATRIX_ROW_VPOS (last_reused_text_row,
13590 w->current_matrix));
13591 }
13592 else if (last_text_row)
13593 {
13594 w->window_end_bytepos
13595 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
13596 w->window_end_pos
13597 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
13598 w->window_end_vpos
13599 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
13600 }
13601 else
13602 {
13603 /* This window must be completely empty. */
13604 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
13605 w->window_end_pos = make_number (Z - ZV);
13606 w->window_end_vpos = make_number (0);
13607 }
13608 w->window_end_valid = Qnil;
13609
13610 /* Update hint: don't try scrolling again in update_window. */
13611 w->desired_matrix->no_scrolling_p = 1;
13612
13613 #if GLYPH_DEBUG
13614 debug_method_add (w, "try_window_reusing_current_matrix 1");
13615 #endif
13616 return 1;
13617 }
13618 else if (CHARPOS (new_start) > CHARPOS (start))
13619 {
13620 struct glyph_row *pt_row, *row;
13621 struct glyph_row *first_reusable_row;
13622 struct glyph_row *first_row_to_display;
13623 int dy;
13624 int yb = window_text_bottom_y (w);
13625
13626 /* Find the row starting at new_start, if there is one. Don't
13627 reuse a partially visible line at the end. */
13628 first_reusable_row = start_row;
13629 while (first_reusable_row->enabled_p
13630 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
13631 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
13632 < CHARPOS (new_start)))
13633 ++first_reusable_row;
13634
13635 /* Give up if there is no row to reuse. */
13636 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
13637 || !first_reusable_row->enabled_p
13638 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
13639 != CHARPOS (new_start)))
13640 return 0;
13641
13642 /* We can reuse fully visible rows beginning with
13643 first_reusable_row to the end of the window. Set
13644 first_row_to_display to the first row that cannot be reused.
13645 Set pt_row to the row containing point, if there is any. */
13646 pt_row = NULL;
13647 for (first_row_to_display = first_reusable_row;
13648 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
13649 ++first_row_to_display)
13650 {
13651 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
13652 && PT < MATRIX_ROW_END_CHARPOS (first_row_to_display))
13653 pt_row = first_row_to_display;
13654 }
13655
13656 /* Start displaying at the start of first_row_to_display. */
13657 xassert (first_row_to_display->y < yb);
13658 init_to_row_start (&it, w, first_row_to_display);
13659
13660 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
13661 - start_vpos);
13662 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
13663 - nrows_scrolled);
13664 it.current_y = (first_row_to_display->y - first_reusable_row->y
13665 + WINDOW_HEADER_LINE_HEIGHT (w));
13666
13667 /* Display lines beginning with first_row_to_display in the
13668 desired matrix. Set last_text_row to the last row displayed
13669 that displays text. */
13670 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
13671 if (pt_row == NULL)
13672 w->cursor.vpos = -1;
13673 last_text_row = NULL;
13674 while (it.current_y < it.last_visible_y && !fonts_changed_p)
13675 if (display_line (&it))
13676 last_text_row = it.glyph_row - 1;
13677
13678 /* Give up If point isn't in a row displayed or reused. */
13679 if (w->cursor.vpos < 0)
13680 {
13681 clear_glyph_matrix (w->desired_matrix);
13682 return 0;
13683 }
13684
13685 /* If point is in a reused row, adjust y and vpos of the cursor
13686 position. */
13687 if (pt_row)
13688 {
13689 w->cursor.vpos -= nrows_scrolled;
13690 w->cursor.y -= first_reusable_row->y - start_row->y;
13691 }
13692
13693 /* Scroll the display. */
13694 run.current_y = first_reusable_row->y;
13695 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
13696 run.height = it.last_visible_y - run.current_y;
13697 dy = run.current_y - run.desired_y;
13698
13699 if (run.height)
13700 {
13701 update_begin (f);
13702 rif->update_window_begin_hook (w);
13703 rif->clear_window_mouse_face (w);
13704 rif->scroll_run_hook (w, &run);
13705 rif->update_window_end_hook (w, 0, 0);
13706 update_end (f);
13707 }
13708
13709 /* Adjust Y positions of reused rows. */
13710 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
13711 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
13712 max_y = it.last_visible_y;
13713 for (row = first_reusable_row; row < first_row_to_display; ++row)
13714 {
13715 row->y -= dy;
13716 row->visible_height = row->height;
13717 if (row->y < min_y)
13718 row->visible_height -= min_y - row->y;
13719 if (row->y + row->height > max_y)
13720 row->visible_height -= row->y + row->height - max_y;
13721 row->redraw_fringe_bitmaps_p = 1;
13722 }
13723
13724 /* Scroll the current matrix. */
13725 xassert (nrows_scrolled > 0);
13726 rotate_matrix (w->current_matrix,
13727 start_vpos,
13728 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
13729 -nrows_scrolled);
13730
13731 /* Disable rows not reused. */
13732 for (row -= nrows_scrolled; row < bottom_row; ++row)
13733 row->enabled_p = 0;
13734
13735 /* Point may have moved to a different line, so we cannot assume that
13736 the previous cursor position is valid; locate the correct row. */
13737 if (pt_row)
13738 {
13739 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
13740 row < bottom_row && PT >= MATRIX_ROW_END_CHARPOS (row);
13741 row++)
13742 {
13743 w->cursor.vpos++;
13744 w->cursor.y = row->y;
13745 }
13746 if (row < bottom_row)
13747 {
13748 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
13749 while (glyph->charpos < PT)
13750 {
13751 w->cursor.hpos++;
13752 w->cursor.x += glyph->pixel_width;
13753 glyph++;
13754 }
13755 }
13756 }
13757
13758 /* Adjust window end. A null value of last_text_row means that
13759 the window end is in reused rows which in turn means that
13760 only its vpos can have changed. */
13761 if (last_text_row)
13762 {
13763 w->window_end_bytepos
13764 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
13765 w->window_end_pos
13766 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
13767 w->window_end_vpos
13768 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
13769 }
13770 else
13771 {
13772 w->window_end_vpos
13773 = make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled);
13774 }
13775
13776 w->window_end_valid = Qnil;
13777 w->desired_matrix->no_scrolling_p = 1;
13778
13779 #if GLYPH_DEBUG
13780 debug_method_add (w, "try_window_reusing_current_matrix 2");
13781 #endif
13782 return 1;
13783 }
13784
13785 return 0;
13786 }
13787
13788
13789 \f
13790 /************************************************************************
13791 Window redisplay reusing current matrix when buffer has changed
13792 ************************************************************************/
13793
13794 static struct glyph_row *find_last_unchanged_at_beg_row P_ ((struct window *));
13795 static struct glyph_row *find_first_unchanged_at_end_row P_ ((struct window *,
13796 int *, int *));
13797 static struct glyph_row *
13798 find_last_row_displaying_text P_ ((struct glyph_matrix *, struct it *,
13799 struct glyph_row *));
13800
13801
13802 /* Return the last row in MATRIX displaying text. If row START is
13803 non-null, start searching with that row. IT gives the dimensions
13804 of the display. Value is null if matrix is empty; otherwise it is
13805 a pointer to the row found. */
13806
13807 static struct glyph_row *
13808 find_last_row_displaying_text (matrix, it, start)
13809 struct glyph_matrix *matrix;
13810 struct it *it;
13811 struct glyph_row *start;
13812 {
13813 struct glyph_row *row, *row_found;
13814
13815 /* Set row_found to the last row in IT->w's current matrix
13816 displaying text. The loop looks funny but think of partially
13817 visible lines. */
13818 row_found = NULL;
13819 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
13820 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
13821 {
13822 xassert (row->enabled_p);
13823 row_found = row;
13824 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
13825 break;
13826 ++row;
13827 }
13828
13829 return row_found;
13830 }
13831
13832
13833 /* Return the last row in the current matrix of W that is not affected
13834 by changes at the start of current_buffer that occurred since W's
13835 current matrix was built. Value is null if no such row exists.
13836
13837 BEG_UNCHANGED us the number of characters unchanged at the start of
13838 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
13839 first changed character in current_buffer. Characters at positions <
13840 BEG + BEG_UNCHANGED are at the same buffer positions as they were
13841 when the current matrix was built. */
13842
13843 static struct glyph_row *
13844 find_last_unchanged_at_beg_row (w)
13845 struct window *w;
13846 {
13847 int first_changed_pos = BEG + BEG_UNCHANGED;
13848 struct glyph_row *row;
13849 struct glyph_row *row_found = NULL;
13850 int yb = window_text_bottom_y (w);
13851
13852 /* Find the last row displaying unchanged text. */
13853 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
13854 while (MATRIX_ROW_DISPLAYS_TEXT_P (row)
13855 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos)
13856 {
13857 if (/* If row ends before first_changed_pos, it is unchanged,
13858 except in some case. */
13859 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
13860 /* When row ends in ZV and we write at ZV it is not
13861 unchanged. */
13862 && !row->ends_at_zv_p
13863 /* When first_changed_pos is the end of a continued line,
13864 row is not unchanged because it may be no longer
13865 continued. */
13866 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
13867 && (row->continued_p
13868 || row->exact_window_width_line_p)))
13869 row_found = row;
13870
13871 /* Stop if last visible row. */
13872 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
13873 break;
13874
13875 ++row;
13876 }
13877
13878 return row_found;
13879 }
13880
13881
13882 /* Find the first glyph row in the current matrix of W that is not
13883 affected by changes at the end of current_buffer since the
13884 time W's current matrix was built.
13885
13886 Return in *DELTA the number of chars by which buffer positions in
13887 unchanged text at the end of current_buffer must be adjusted.
13888
13889 Return in *DELTA_BYTES the corresponding number of bytes.
13890
13891 Value is null if no such row exists, i.e. all rows are affected by
13892 changes. */
13893
13894 static struct glyph_row *
13895 find_first_unchanged_at_end_row (w, delta, delta_bytes)
13896 struct window *w;
13897 int *delta, *delta_bytes;
13898 {
13899 struct glyph_row *row;
13900 struct glyph_row *row_found = NULL;
13901
13902 *delta = *delta_bytes = 0;
13903
13904 /* Display must not have been paused, otherwise the current matrix
13905 is not up to date. */
13906 if (NILP (w->window_end_valid))
13907 abort ();
13908
13909 /* A value of window_end_pos >= END_UNCHANGED means that the window
13910 end is in the range of changed text. If so, there is no
13911 unchanged row at the end of W's current matrix. */
13912 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
13913 return NULL;
13914
13915 /* Set row to the last row in W's current matrix displaying text. */
13916 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
13917
13918 /* If matrix is entirely empty, no unchanged row exists. */
13919 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
13920 {
13921 /* The value of row is the last glyph row in the matrix having a
13922 meaningful buffer position in it. The end position of row
13923 corresponds to window_end_pos. This allows us to translate
13924 buffer positions in the current matrix to current buffer
13925 positions for characters not in changed text. */
13926 int Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
13927 int Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
13928 int last_unchanged_pos, last_unchanged_pos_old;
13929 struct glyph_row *first_text_row
13930 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
13931
13932 *delta = Z - Z_old;
13933 *delta_bytes = Z_BYTE - Z_BYTE_old;
13934
13935 /* Set last_unchanged_pos to the buffer position of the last
13936 character in the buffer that has not been changed. Z is the
13937 index + 1 of the last character in current_buffer, i.e. by
13938 subtracting END_UNCHANGED we get the index of the last
13939 unchanged character, and we have to add BEG to get its buffer
13940 position. */
13941 last_unchanged_pos = Z - END_UNCHANGED + BEG;
13942 last_unchanged_pos_old = last_unchanged_pos - *delta;
13943
13944 /* Search backward from ROW for a row displaying a line that
13945 starts at a minimum position >= last_unchanged_pos_old. */
13946 for (; row > first_text_row; --row)
13947 {
13948 /* This used to abort, but it can happen.
13949 It is ok to just stop the search instead here. KFS. */
13950 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
13951 break;
13952
13953 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
13954 row_found = row;
13955 }
13956 }
13957
13958 if (row_found && !MATRIX_ROW_DISPLAYS_TEXT_P (row_found))
13959 abort ();
13960
13961 return row_found;
13962 }
13963
13964
13965 /* Make sure that glyph rows in the current matrix of window W
13966 reference the same glyph memory as corresponding rows in the
13967 frame's frame matrix. This function is called after scrolling W's
13968 current matrix on a terminal frame in try_window_id and
13969 try_window_reusing_current_matrix. */
13970
13971 static void
13972 sync_frame_with_window_matrix_rows (w)
13973 struct window *w;
13974 {
13975 struct frame *f = XFRAME (w->frame);
13976 struct glyph_row *window_row, *window_row_end, *frame_row;
13977
13978 /* Preconditions: W must be a leaf window and full-width. Its frame
13979 must have a frame matrix. */
13980 xassert (NILP (w->hchild) && NILP (w->vchild));
13981 xassert (WINDOW_FULL_WIDTH_P (w));
13982 xassert (!FRAME_WINDOW_P (f));
13983
13984 /* If W is a full-width window, glyph pointers in W's current matrix
13985 have, by definition, to be the same as glyph pointers in the
13986 corresponding frame matrix. Note that frame matrices have no
13987 marginal areas (see build_frame_matrix). */
13988 window_row = w->current_matrix->rows;
13989 window_row_end = window_row + w->current_matrix->nrows;
13990 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
13991 while (window_row < window_row_end)
13992 {
13993 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
13994 struct glyph *end = window_row->glyphs[LAST_AREA];
13995
13996 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
13997 frame_row->glyphs[TEXT_AREA] = start;
13998 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
13999 frame_row->glyphs[LAST_AREA] = end;
14000
14001 /* Disable frame rows whose corresponding window rows have
14002 been disabled in try_window_id. */
14003 if (!window_row->enabled_p)
14004 frame_row->enabled_p = 0;
14005
14006 ++window_row, ++frame_row;
14007 }
14008 }
14009
14010
14011 /* Find the glyph row in window W containing CHARPOS. Consider all
14012 rows between START and END (not inclusive). END null means search
14013 all rows to the end of the display area of W. Value is the row
14014 containing CHARPOS or null. */
14015
14016 struct glyph_row *
14017 row_containing_pos (w, charpos, start, end, dy)
14018 struct window *w;
14019 int charpos;
14020 struct glyph_row *start, *end;
14021 int dy;
14022 {
14023 struct glyph_row *row = start;
14024 int last_y;
14025
14026 /* If we happen to start on a header-line, skip that. */
14027 if (row->mode_line_p)
14028 ++row;
14029
14030 if ((end && row >= end) || !row->enabled_p)
14031 return NULL;
14032
14033 last_y = window_text_bottom_y (w) - dy;
14034
14035 while (1)
14036 {
14037 /* Give up if we have gone too far. */
14038 if (end && row >= end)
14039 return NULL;
14040 /* This formerly returned if they were equal.
14041 I think that both quantities are of a "last plus one" type;
14042 if so, when they are equal, the row is within the screen. -- rms. */
14043 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
14044 return NULL;
14045
14046 /* If it is in this row, return this row. */
14047 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
14048 || (MATRIX_ROW_END_CHARPOS (row) == charpos
14049 /* The end position of a row equals the start
14050 position of the next row. If CHARPOS is there, we
14051 would rather display it in the next line, except
14052 when this line ends in ZV. */
14053 && !row->ends_at_zv_p
14054 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
14055 && charpos >= MATRIX_ROW_START_CHARPOS (row))
14056 return row;
14057 ++row;
14058 }
14059 }
14060
14061
14062 /* Try to redisplay window W by reusing its existing display. W's
14063 current matrix must be up to date when this function is called,
14064 i.e. window_end_valid must not be nil.
14065
14066 Value is
14067
14068 1 if display has been updated
14069 0 if otherwise unsuccessful
14070 -1 if redisplay with same window start is known not to succeed
14071
14072 The following steps are performed:
14073
14074 1. Find the last row in the current matrix of W that is not
14075 affected by changes at the start of current_buffer. If no such row
14076 is found, give up.
14077
14078 2. Find the first row in W's current matrix that is not affected by
14079 changes at the end of current_buffer. Maybe there is no such row.
14080
14081 3. Display lines beginning with the row + 1 found in step 1 to the
14082 row found in step 2 or, if step 2 didn't find a row, to the end of
14083 the window.
14084
14085 4. If cursor is not known to appear on the window, give up.
14086
14087 5. If display stopped at the row found in step 2, scroll the
14088 display and current matrix as needed.
14089
14090 6. Maybe display some lines at the end of W, if we must. This can
14091 happen under various circumstances, like a partially visible line
14092 becoming fully visible, or because newly displayed lines are displayed
14093 in smaller font sizes.
14094
14095 7. Update W's window end information. */
14096
14097 static int
14098 try_window_id (w)
14099 struct window *w;
14100 {
14101 struct frame *f = XFRAME (w->frame);
14102 struct glyph_matrix *current_matrix = w->current_matrix;
14103 struct glyph_matrix *desired_matrix = w->desired_matrix;
14104 struct glyph_row *last_unchanged_at_beg_row;
14105 struct glyph_row *first_unchanged_at_end_row;
14106 struct glyph_row *row;
14107 struct glyph_row *bottom_row;
14108 int bottom_vpos;
14109 struct it it;
14110 int delta = 0, delta_bytes = 0, stop_pos, dvpos, dy;
14111 struct text_pos start_pos;
14112 struct run run;
14113 int first_unchanged_at_end_vpos = 0;
14114 struct glyph_row *last_text_row, *last_text_row_at_end;
14115 struct text_pos start;
14116 int first_changed_charpos, last_changed_charpos;
14117
14118 #if GLYPH_DEBUG
14119 if (inhibit_try_window_id)
14120 return 0;
14121 #endif
14122
14123 /* This is handy for debugging. */
14124 #if 0
14125 #define GIVE_UP(X) \
14126 do { \
14127 fprintf (stderr, "try_window_id give up %d\n", (X)); \
14128 return 0; \
14129 } while (0)
14130 #else
14131 #define GIVE_UP(X) return 0
14132 #endif
14133
14134 SET_TEXT_POS_FROM_MARKER (start, w->start);
14135
14136 /* Don't use this for mini-windows because these can show
14137 messages and mini-buffers, and we don't handle that here. */
14138 if (MINI_WINDOW_P (w))
14139 GIVE_UP (1);
14140
14141 /* This flag is used to prevent redisplay optimizations. */
14142 if (windows_or_buffers_changed || cursor_type_changed)
14143 GIVE_UP (2);
14144
14145 /* Verify that narrowing has not changed.
14146 Also verify that we were not told to prevent redisplay optimizations.
14147 It would be nice to further
14148 reduce the number of cases where this prevents try_window_id. */
14149 if (current_buffer->clip_changed
14150 || current_buffer->prevent_redisplay_optimizations_p)
14151 GIVE_UP (3);
14152
14153 /* Window must either use window-based redisplay or be full width. */
14154 if (!FRAME_WINDOW_P (f)
14155 && (!line_ins_del_ok
14156 || !WINDOW_FULL_WIDTH_P (w)))
14157 GIVE_UP (4);
14158
14159 /* Give up if point is not known NOT to appear in W. */
14160 if (PT < CHARPOS (start))
14161 GIVE_UP (5);
14162
14163 /* Another way to prevent redisplay optimizations. */
14164 if (XFASTINT (w->last_modified) == 0)
14165 GIVE_UP (6);
14166
14167 /* Verify that window is not hscrolled. */
14168 if (XFASTINT (w->hscroll) != 0)
14169 GIVE_UP (7);
14170
14171 /* Verify that display wasn't paused. */
14172 if (NILP (w->window_end_valid))
14173 GIVE_UP (8);
14174
14175 /* Can't use this if highlighting a region because a cursor movement
14176 will do more than just set the cursor. */
14177 if (!NILP (Vtransient_mark_mode)
14178 && !NILP (current_buffer->mark_active))
14179 GIVE_UP (9);
14180
14181 /* Likewise if highlighting trailing whitespace. */
14182 if (!NILP (Vshow_trailing_whitespace))
14183 GIVE_UP (11);
14184
14185 /* Likewise if showing a region. */
14186 if (!NILP (w->region_showing))
14187 GIVE_UP (10);
14188
14189 /* Can use this if overlay arrow position and or string have changed. */
14190 if (overlay_arrows_changed_p ())
14191 GIVE_UP (12);
14192
14193
14194 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
14195 only if buffer has really changed. The reason is that the gap is
14196 initially at Z for freshly visited files. The code below would
14197 set end_unchanged to 0 in that case. */
14198 if (MODIFF > SAVE_MODIFF
14199 /* This seems to happen sometimes after saving a buffer. */
14200 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
14201 {
14202 if (GPT - BEG < BEG_UNCHANGED)
14203 BEG_UNCHANGED = GPT - BEG;
14204 if (Z - GPT < END_UNCHANGED)
14205 END_UNCHANGED = Z - GPT;
14206 }
14207
14208 /* The position of the first and last character that has been changed. */
14209 first_changed_charpos = BEG + BEG_UNCHANGED;
14210 last_changed_charpos = Z - END_UNCHANGED;
14211
14212 /* If window starts after a line end, and the last change is in
14213 front of that newline, then changes don't affect the display.
14214 This case happens with stealth-fontification. Note that although
14215 the display is unchanged, glyph positions in the matrix have to
14216 be adjusted, of course. */
14217 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
14218 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
14219 && ((last_changed_charpos < CHARPOS (start)
14220 && CHARPOS (start) == BEGV)
14221 || (last_changed_charpos < CHARPOS (start) - 1
14222 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
14223 {
14224 int Z_old, delta, Z_BYTE_old, delta_bytes;
14225 struct glyph_row *r0;
14226
14227 /* Compute how many chars/bytes have been added to or removed
14228 from the buffer. */
14229 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
14230 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
14231 delta = Z - Z_old;
14232 delta_bytes = Z_BYTE - Z_BYTE_old;
14233
14234 /* Give up if PT is not in the window. Note that it already has
14235 been checked at the start of try_window_id that PT is not in
14236 front of the window start. */
14237 if (PT >= MATRIX_ROW_END_CHARPOS (row) + delta)
14238 GIVE_UP (13);
14239
14240 /* If window start is unchanged, we can reuse the whole matrix
14241 as is, after adjusting glyph positions. No need to compute
14242 the window end again, since its offset from Z hasn't changed. */
14243 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
14244 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + delta
14245 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + delta_bytes
14246 /* PT must not be in a partially visible line. */
14247 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + delta
14248 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
14249 {
14250 /* Adjust positions in the glyph matrix. */
14251 if (delta || delta_bytes)
14252 {
14253 struct glyph_row *r1
14254 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
14255 increment_matrix_positions (w->current_matrix,
14256 MATRIX_ROW_VPOS (r0, current_matrix),
14257 MATRIX_ROW_VPOS (r1, current_matrix),
14258 delta, delta_bytes);
14259 }
14260
14261 /* Set the cursor. */
14262 row = row_containing_pos (w, PT, r0, NULL, 0);
14263 if (row)
14264 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
14265 else
14266 abort ();
14267 return 1;
14268 }
14269 }
14270
14271 /* Handle the case that changes are all below what is displayed in
14272 the window, and that PT is in the window. This shortcut cannot
14273 be taken if ZV is visible in the window, and text has been added
14274 there that is visible in the window. */
14275 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
14276 /* ZV is not visible in the window, or there are no
14277 changes at ZV, actually. */
14278 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
14279 || first_changed_charpos == last_changed_charpos))
14280 {
14281 struct glyph_row *r0;
14282
14283 /* Give up if PT is not in the window. Note that it already has
14284 been checked at the start of try_window_id that PT is not in
14285 front of the window start. */
14286 if (PT >= MATRIX_ROW_END_CHARPOS (row))
14287 GIVE_UP (14);
14288
14289 /* If window start is unchanged, we can reuse the whole matrix
14290 as is, without changing glyph positions since no text has
14291 been added/removed in front of the window end. */
14292 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
14293 if (TEXT_POS_EQUAL_P (start, r0->start.pos)
14294 /* PT must not be in a partially visible line. */
14295 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
14296 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
14297 {
14298 /* We have to compute the window end anew since text
14299 can have been added/removed after it. */
14300 w->window_end_pos
14301 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
14302 w->window_end_bytepos
14303 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
14304
14305 /* Set the cursor. */
14306 row = row_containing_pos (w, PT, r0, NULL, 0);
14307 if (row)
14308 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
14309 else
14310 abort ();
14311 return 2;
14312 }
14313 }
14314
14315 /* Give up if window start is in the changed area.
14316
14317 The condition used to read
14318
14319 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
14320
14321 but why that was tested escapes me at the moment. */
14322 if (CHARPOS (start) >= first_changed_charpos
14323 && CHARPOS (start) <= last_changed_charpos)
14324 GIVE_UP (15);
14325
14326 /* Check that window start agrees with the start of the first glyph
14327 row in its current matrix. Check this after we know the window
14328 start is not in changed text, otherwise positions would not be
14329 comparable. */
14330 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
14331 if (!TEXT_POS_EQUAL_P (start, row->start.pos))
14332 GIVE_UP (16);
14333
14334 /* Give up if the window ends in strings. Overlay strings
14335 at the end are difficult to handle, so don't try. */
14336 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
14337 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
14338 GIVE_UP (20);
14339
14340 /* Compute the position at which we have to start displaying new
14341 lines. Some of the lines at the top of the window might be
14342 reusable because they are not displaying changed text. Find the
14343 last row in W's current matrix not affected by changes at the
14344 start of current_buffer. Value is null if changes start in the
14345 first line of window. */
14346 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
14347 if (last_unchanged_at_beg_row)
14348 {
14349 /* Avoid starting to display in the moddle of a character, a TAB
14350 for instance. This is easier than to set up the iterator
14351 exactly, and it's not a frequent case, so the additional
14352 effort wouldn't really pay off. */
14353 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
14354 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
14355 && last_unchanged_at_beg_row > w->current_matrix->rows)
14356 --last_unchanged_at_beg_row;
14357
14358 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
14359 GIVE_UP (17);
14360
14361 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
14362 GIVE_UP (18);
14363 start_pos = it.current.pos;
14364
14365 /* Start displaying new lines in the desired matrix at the same
14366 vpos we would use in the current matrix, i.e. below
14367 last_unchanged_at_beg_row. */
14368 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
14369 current_matrix);
14370 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
14371 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
14372
14373 xassert (it.hpos == 0 && it.current_x == 0);
14374 }
14375 else
14376 {
14377 /* There are no reusable lines at the start of the window.
14378 Start displaying in the first text line. */
14379 start_display (&it, w, start);
14380 it.vpos = it.first_vpos;
14381 start_pos = it.current.pos;
14382 }
14383
14384 /* Find the first row that is not affected by changes at the end of
14385 the buffer. Value will be null if there is no unchanged row, in
14386 which case we must redisplay to the end of the window. delta
14387 will be set to the value by which buffer positions beginning with
14388 first_unchanged_at_end_row have to be adjusted due to text
14389 changes. */
14390 first_unchanged_at_end_row
14391 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
14392 IF_DEBUG (debug_delta = delta);
14393 IF_DEBUG (debug_delta_bytes = delta_bytes);
14394
14395 /* Set stop_pos to the buffer position up to which we will have to
14396 display new lines. If first_unchanged_at_end_row != NULL, this
14397 is the buffer position of the start of the line displayed in that
14398 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
14399 that we don't stop at a buffer position. */
14400 stop_pos = 0;
14401 if (first_unchanged_at_end_row)
14402 {
14403 xassert (last_unchanged_at_beg_row == NULL
14404 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
14405
14406 /* If this is a continuation line, move forward to the next one
14407 that isn't. Changes in lines above affect this line.
14408 Caution: this may move first_unchanged_at_end_row to a row
14409 not displaying text. */
14410 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
14411 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
14412 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
14413 < it.last_visible_y))
14414 ++first_unchanged_at_end_row;
14415
14416 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
14417 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
14418 >= it.last_visible_y))
14419 first_unchanged_at_end_row = NULL;
14420 else
14421 {
14422 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
14423 + delta);
14424 first_unchanged_at_end_vpos
14425 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
14426 xassert (stop_pos >= Z - END_UNCHANGED);
14427 }
14428 }
14429 else if (last_unchanged_at_beg_row == NULL)
14430 GIVE_UP (19);
14431
14432
14433 #if GLYPH_DEBUG
14434
14435 /* Either there is no unchanged row at the end, or the one we have
14436 now displays text. This is a necessary condition for the window
14437 end pos calculation at the end of this function. */
14438 xassert (first_unchanged_at_end_row == NULL
14439 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
14440
14441 debug_last_unchanged_at_beg_vpos
14442 = (last_unchanged_at_beg_row
14443 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
14444 : -1);
14445 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
14446
14447 #endif /* GLYPH_DEBUG != 0 */
14448
14449
14450 /* Display new lines. Set last_text_row to the last new line
14451 displayed which has text on it, i.e. might end up as being the
14452 line where the window_end_vpos is. */
14453 w->cursor.vpos = -1;
14454 last_text_row = NULL;
14455 overlay_arrow_seen = 0;
14456 while (it.current_y < it.last_visible_y
14457 && !fonts_changed_p
14458 && (first_unchanged_at_end_row == NULL
14459 || IT_CHARPOS (it) < stop_pos))
14460 {
14461 if (display_line (&it))
14462 last_text_row = it.glyph_row - 1;
14463 }
14464
14465 if (fonts_changed_p)
14466 return -1;
14467
14468
14469 /* Compute differences in buffer positions, y-positions etc. for
14470 lines reused at the bottom of the window. Compute what we can
14471 scroll. */
14472 if (first_unchanged_at_end_row
14473 /* No lines reused because we displayed everything up to the
14474 bottom of the window. */
14475 && it.current_y < it.last_visible_y)
14476 {
14477 dvpos = (it.vpos
14478 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
14479 current_matrix));
14480 dy = it.current_y - first_unchanged_at_end_row->y;
14481 run.current_y = first_unchanged_at_end_row->y;
14482 run.desired_y = run.current_y + dy;
14483 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
14484 }
14485 else
14486 {
14487 delta = dvpos = dy = run.current_y = run.desired_y = run.height = 0;
14488 first_unchanged_at_end_row = NULL;
14489 }
14490 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
14491
14492
14493 /* Find the cursor if not already found. We have to decide whether
14494 PT will appear on this window (it sometimes doesn't, but this is
14495 not a very frequent case.) This decision has to be made before
14496 the current matrix is altered. A value of cursor.vpos < 0 means
14497 that PT is either in one of the lines beginning at
14498 first_unchanged_at_end_row or below the window. Don't care for
14499 lines that might be displayed later at the window end; as
14500 mentioned, this is not a frequent case. */
14501 if (w->cursor.vpos < 0)
14502 {
14503 /* Cursor in unchanged rows at the top? */
14504 if (PT < CHARPOS (start_pos)
14505 && last_unchanged_at_beg_row)
14506 {
14507 row = row_containing_pos (w, PT,
14508 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
14509 last_unchanged_at_beg_row + 1, 0);
14510 if (row)
14511 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
14512 }
14513
14514 /* Start from first_unchanged_at_end_row looking for PT. */
14515 else if (first_unchanged_at_end_row)
14516 {
14517 row = row_containing_pos (w, PT - delta,
14518 first_unchanged_at_end_row, NULL, 0);
14519 if (row)
14520 set_cursor_from_row (w, row, w->current_matrix, delta,
14521 delta_bytes, dy, dvpos);
14522 }
14523
14524 /* Give up if cursor was not found. */
14525 if (w->cursor.vpos < 0)
14526 {
14527 clear_glyph_matrix (w->desired_matrix);
14528 return -1;
14529 }
14530 }
14531
14532 /* Don't let the cursor end in the scroll margins. */
14533 {
14534 int this_scroll_margin, cursor_height;
14535
14536 this_scroll_margin = max (0, scroll_margin);
14537 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
14538 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
14539 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
14540
14541 if ((w->cursor.y < this_scroll_margin
14542 && CHARPOS (start) > BEGV)
14543 /* Old redisplay didn't take scroll margin into account at the bottom,
14544 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
14545 || (w->cursor.y + (make_cursor_line_fully_visible_p
14546 ? cursor_height + this_scroll_margin
14547 : 1)) > it.last_visible_y)
14548 {
14549 w->cursor.vpos = -1;
14550 clear_glyph_matrix (w->desired_matrix);
14551 return -1;
14552 }
14553 }
14554
14555 /* Scroll the display. Do it before changing the current matrix so
14556 that xterm.c doesn't get confused about where the cursor glyph is
14557 found. */
14558 if (dy && run.height)
14559 {
14560 update_begin (f);
14561
14562 if (FRAME_WINDOW_P (f))
14563 {
14564 rif->update_window_begin_hook (w);
14565 rif->clear_window_mouse_face (w);
14566 rif->scroll_run_hook (w, &run);
14567 rif->update_window_end_hook (w, 0, 0);
14568 }
14569 else
14570 {
14571 /* Terminal frame. In this case, dvpos gives the number of
14572 lines to scroll by; dvpos < 0 means scroll up. */
14573 int first_unchanged_at_end_vpos
14574 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
14575 int from = WINDOW_TOP_EDGE_LINE (w) + first_unchanged_at_end_vpos;
14576 int end = (WINDOW_TOP_EDGE_LINE (w)
14577 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
14578 + window_internal_height (w));
14579
14580 /* Perform the operation on the screen. */
14581 if (dvpos > 0)
14582 {
14583 /* Scroll last_unchanged_at_beg_row to the end of the
14584 window down dvpos lines. */
14585 set_terminal_window (end);
14586
14587 /* On dumb terminals delete dvpos lines at the end
14588 before inserting dvpos empty lines. */
14589 if (!scroll_region_ok)
14590 ins_del_lines (end - dvpos, -dvpos);
14591
14592 /* Insert dvpos empty lines in front of
14593 last_unchanged_at_beg_row. */
14594 ins_del_lines (from, dvpos);
14595 }
14596 else if (dvpos < 0)
14597 {
14598 /* Scroll up last_unchanged_at_beg_vpos to the end of
14599 the window to last_unchanged_at_beg_vpos - |dvpos|. */
14600 set_terminal_window (end);
14601
14602 /* Delete dvpos lines in front of
14603 last_unchanged_at_beg_vpos. ins_del_lines will set
14604 the cursor to the given vpos and emit |dvpos| delete
14605 line sequences. */
14606 ins_del_lines (from + dvpos, dvpos);
14607
14608 /* On a dumb terminal insert dvpos empty lines at the
14609 end. */
14610 if (!scroll_region_ok)
14611 ins_del_lines (end + dvpos, -dvpos);
14612 }
14613
14614 set_terminal_window (0);
14615 }
14616
14617 update_end (f);
14618 }
14619
14620 /* Shift reused rows of the current matrix to the right position.
14621 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
14622 text. */
14623 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
14624 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
14625 if (dvpos < 0)
14626 {
14627 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
14628 bottom_vpos, dvpos);
14629 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
14630 bottom_vpos, 0);
14631 }
14632 else if (dvpos > 0)
14633 {
14634 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
14635 bottom_vpos, dvpos);
14636 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
14637 first_unchanged_at_end_vpos + dvpos, 0);
14638 }
14639
14640 /* For frame-based redisplay, make sure that current frame and window
14641 matrix are in sync with respect to glyph memory. */
14642 if (!FRAME_WINDOW_P (f))
14643 sync_frame_with_window_matrix_rows (w);
14644
14645 /* Adjust buffer positions in reused rows. */
14646 if (delta)
14647 increment_matrix_positions (current_matrix,
14648 first_unchanged_at_end_vpos + dvpos,
14649 bottom_vpos, delta, delta_bytes);
14650
14651 /* Adjust Y positions. */
14652 if (dy)
14653 shift_glyph_matrix (w, current_matrix,
14654 first_unchanged_at_end_vpos + dvpos,
14655 bottom_vpos, dy);
14656
14657 if (first_unchanged_at_end_row)
14658 {
14659 first_unchanged_at_end_row += dvpos;
14660 if (first_unchanged_at_end_row->y >= it.last_visible_y
14661 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
14662 first_unchanged_at_end_row = NULL;
14663 }
14664
14665 /* If scrolling up, there may be some lines to display at the end of
14666 the window. */
14667 last_text_row_at_end = NULL;
14668 if (dy < 0)
14669 {
14670 /* Scrolling up can leave for example a partially visible line
14671 at the end of the window to be redisplayed. */
14672 /* Set last_row to the glyph row in the current matrix where the
14673 window end line is found. It has been moved up or down in
14674 the matrix by dvpos. */
14675 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
14676 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
14677
14678 /* If last_row is the window end line, it should display text. */
14679 xassert (last_row->displays_text_p);
14680
14681 /* If window end line was partially visible before, begin
14682 displaying at that line. Otherwise begin displaying with the
14683 line following it. */
14684 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
14685 {
14686 init_to_row_start (&it, w, last_row);
14687 it.vpos = last_vpos;
14688 it.current_y = last_row->y;
14689 }
14690 else
14691 {
14692 init_to_row_end (&it, w, last_row);
14693 it.vpos = 1 + last_vpos;
14694 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
14695 ++last_row;
14696 }
14697
14698 /* We may start in a continuation line. If so, we have to
14699 get the right continuation_lines_width and current_x. */
14700 it.continuation_lines_width = last_row->continuation_lines_width;
14701 it.hpos = it.current_x = 0;
14702
14703 /* Display the rest of the lines at the window end. */
14704 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
14705 while (it.current_y < it.last_visible_y
14706 && !fonts_changed_p)
14707 {
14708 /* Is it always sure that the display agrees with lines in
14709 the current matrix? I don't think so, so we mark rows
14710 displayed invalid in the current matrix by setting their
14711 enabled_p flag to zero. */
14712 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
14713 if (display_line (&it))
14714 last_text_row_at_end = it.glyph_row - 1;
14715 }
14716 }
14717
14718 /* Update window_end_pos and window_end_vpos. */
14719 if (first_unchanged_at_end_row
14720 && !last_text_row_at_end)
14721 {
14722 /* Window end line if one of the preserved rows from the current
14723 matrix. Set row to the last row displaying text in current
14724 matrix starting at first_unchanged_at_end_row, after
14725 scrolling. */
14726 xassert (first_unchanged_at_end_row->displays_text_p);
14727 row = find_last_row_displaying_text (w->current_matrix, &it,
14728 first_unchanged_at_end_row);
14729 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
14730
14731 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
14732 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
14733 w->window_end_vpos
14734 = make_number (MATRIX_ROW_VPOS (row, w->current_matrix));
14735 xassert (w->window_end_bytepos >= 0);
14736 IF_DEBUG (debug_method_add (w, "A"));
14737 }
14738 else if (last_text_row_at_end)
14739 {
14740 w->window_end_pos
14741 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
14742 w->window_end_bytepos
14743 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
14744 w->window_end_vpos
14745 = make_number (MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
14746 xassert (w->window_end_bytepos >= 0);
14747 IF_DEBUG (debug_method_add (w, "B"));
14748 }
14749 else if (last_text_row)
14750 {
14751 /* We have displayed either to the end of the window or at the
14752 end of the window, i.e. the last row with text is to be found
14753 in the desired matrix. */
14754 w->window_end_pos
14755 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
14756 w->window_end_bytepos
14757 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
14758 w->window_end_vpos
14759 = make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix));
14760 xassert (w->window_end_bytepos >= 0);
14761 }
14762 else if (first_unchanged_at_end_row == NULL
14763 && last_text_row == NULL
14764 && last_text_row_at_end == NULL)
14765 {
14766 /* Displayed to end of window, but no line containing text was
14767 displayed. Lines were deleted at the end of the window. */
14768 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
14769 int vpos = XFASTINT (w->window_end_vpos);
14770 struct glyph_row *current_row = current_matrix->rows + vpos;
14771 struct glyph_row *desired_row = desired_matrix->rows + vpos;
14772
14773 for (row = NULL;
14774 row == NULL && vpos >= first_vpos;
14775 --vpos, --current_row, --desired_row)
14776 {
14777 if (desired_row->enabled_p)
14778 {
14779 if (desired_row->displays_text_p)
14780 row = desired_row;
14781 }
14782 else if (current_row->displays_text_p)
14783 row = current_row;
14784 }
14785
14786 xassert (row != NULL);
14787 w->window_end_vpos = make_number (vpos + 1);
14788 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
14789 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
14790 xassert (w->window_end_bytepos >= 0);
14791 IF_DEBUG (debug_method_add (w, "C"));
14792 }
14793 else
14794 abort ();
14795
14796 #if 0 /* This leads to problems, for instance when the cursor is
14797 at ZV, and the cursor line displays no text. */
14798 /* Disable rows below what's displayed in the window. This makes
14799 debugging easier. */
14800 enable_glyph_matrix_rows (current_matrix,
14801 XFASTINT (w->window_end_vpos) + 1,
14802 bottom_vpos, 0);
14803 #endif
14804
14805 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
14806 debug_end_vpos = XFASTINT (w->window_end_vpos));
14807
14808 /* Record that display has not been completed. */
14809 w->window_end_valid = Qnil;
14810 w->desired_matrix->no_scrolling_p = 1;
14811 return 3;
14812
14813 #undef GIVE_UP
14814 }
14815
14816
14817 \f
14818 /***********************************************************************
14819 More debugging support
14820 ***********************************************************************/
14821
14822 #if GLYPH_DEBUG
14823
14824 void dump_glyph_row P_ ((struct glyph_row *, int, int));
14825 void dump_glyph_matrix P_ ((struct glyph_matrix *, int));
14826 void dump_glyph P_ ((struct glyph_row *, struct glyph *, int));
14827
14828
14829 /* Dump the contents of glyph matrix MATRIX on stderr.
14830
14831 GLYPHS 0 means don't show glyph contents.
14832 GLYPHS 1 means show glyphs in short form
14833 GLYPHS > 1 means show glyphs in long form. */
14834
14835 void
14836 dump_glyph_matrix (matrix, glyphs)
14837 struct glyph_matrix *matrix;
14838 int glyphs;
14839 {
14840 int i;
14841 for (i = 0; i < matrix->nrows; ++i)
14842 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
14843 }
14844
14845
14846 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
14847 the glyph row and area where the glyph comes from. */
14848
14849 void
14850 dump_glyph (row, glyph, area)
14851 struct glyph_row *row;
14852 struct glyph *glyph;
14853 int area;
14854 {
14855 if (glyph->type == CHAR_GLYPH)
14856 {
14857 fprintf (stderr,
14858 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
14859 glyph - row->glyphs[TEXT_AREA],
14860 'C',
14861 glyph->charpos,
14862 (BUFFERP (glyph->object)
14863 ? 'B'
14864 : (STRINGP (glyph->object)
14865 ? 'S'
14866 : '-')),
14867 glyph->pixel_width,
14868 glyph->u.ch,
14869 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
14870 ? glyph->u.ch
14871 : '.'),
14872 glyph->face_id,
14873 glyph->left_box_line_p,
14874 glyph->right_box_line_p);
14875 }
14876 else if (glyph->type == STRETCH_GLYPH)
14877 {
14878 fprintf (stderr,
14879 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
14880 glyph - row->glyphs[TEXT_AREA],
14881 'S',
14882 glyph->charpos,
14883 (BUFFERP (glyph->object)
14884 ? 'B'
14885 : (STRINGP (glyph->object)
14886 ? 'S'
14887 : '-')),
14888 glyph->pixel_width,
14889 0,
14890 '.',
14891 glyph->face_id,
14892 glyph->left_box_line_p,
14893 glyph->right_box_line_p);
14894 }
14895 else if (glyph->type == IMAGE_GLYPH)
14896 {
14897 fprintf (stderr,
14898 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
14899 glyph - row->glyphs[TEXT_AREA],
14900 'I',
14901 glyph->charpos,
14902 (BUFFERP (glyph->object)
14903 ? 'B'
14904 : (STRINGP (glyph->object)
14905 ? 'S'
14906 : '-')),
14907 glyph->pixel_width,
14908 glyph->u.img_id,
14909 '.',
14910 glyph->face_id,
14911 glyph->left_box_line_p,
14912 glyph->right_box_line_p);
14913 }
14914 }
14915
14916
14917 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
14918 GLYPHS 0 means don't show glyph contents.
14919 GLYPHS 1 means show glyphs in short form
14920 GLYPHS > 1 means show glyphs in long form. */
14921
14922 void
14923 dump_glyph_row (row, vpos, glyphs)
14924 struct glyph_row *row;
14925 int vpos, glyphs;
14926 {
14927 if (glyphs != 1)
14928 {
14929 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
14930 fprintf (stderr, "======================================================================\n");
14931
14932 fprintf (stderr, "%3d %5d %5d %4d %1.1d%1.1d%1.1d%1.1d\
14933 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
14934 vpos,
14935 MATRIX_ROW_START_CHARPOS (row),
14936 MATRIX_ROW_END_CHARPOS (row),
14937 row->used[TEXT_AREA],
14938 row->contains_overlapping_glyphs_p,
14939 row->enabled_p,
14940 row->truncated_on_left_p,
14941 row->truncated_on_right_p,
14942 row->continued_p,
14943 MATRIX_ROW_CONTINUATION_LINE_P (row),
14944 row->displays_text_p,
14945 row->ends_at_zv_p,
14946 row->fill_line_p,
14947 row->ends_in_middle_of_char_p,
14948 row->starts_in_middle_of_char_p,
14949 row->mouse_face_p,
14950 row->x,
14951 row->y,
14952 row->pixel_width,
14953 row->height,
14954 row->visible_height,
14955 row->ascent,
14956 row->phys_ascent);
14957 fprintf (stderr, "%9d %5d\t%5d\n", row->start.overlay_string_index,
14958 row->end.overlay_string_index,
14959 row->continuation_lines_width);
14960 fprintf (stderr, "%9d %5d\n",
14961 CHARPOS (row->start.string_pos),
14962 CHARPOS (row->end.string_pos));
14963 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
14964 row->end.dpvec_index);
14965 }
14966
14967 if (glyphs > 1)
14968 {
14969 int area;
14970
14971 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
14972 {
14973 struct glyph *glyph = row->glyphs[area];
14974 struct glyph *glyph_end = glyph + row->used[area];
14975
14976 /* Glyph for a line end in text. */
14977 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
14978 ++glyph_end;
14979
14980 if (glyph < glyph_end)
14981 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
14982
14983 for (; glyph < glyph_end; ++glyph)
14984 dump_glyph (row, glyph, area);
14985 }
14986 }
14987 else if (glyphs == 1)
14988 {
14989 int area;
14990
14991 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
14992 {
14993 char *s = (char *) alloca (row->used[area] + 1);
14994 int i;
14995
14996 for (i = 0; i < row->used[area]; ++i)
14997 {
14998 struct glyph *glyph = row->glyphs[area] + i;
14999 if (glyph->type == CHAR_GLYPH
15000 && glyph->u.ch < 0x80
15001 && glyph->u.ch >= ' ')
15002 s[i] = glyph->u.ch;
15003 else
15004 s[i] = '.';
15005 }
15006
15007 s[i] = '\0';
15008 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
15009 }
15010 }
15011 }
15012
15013
15014 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
15015 Sdump_glyph_matrix, 0, 1, "p",
15016 doc: /* Dump the current matrix of the selected window to stderr.
15017 Shows contents of glyph row structures. With non-nil
15018 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
15019 glyphs in short form, otherwise show glyphs in long form. */)
15020 (glyphs)
15021 Lisp_Object glyphs;
15022 {
15023 struct window *w = XWINDOW (selected_window);
15024 struct buffer *buffer = XBUFFER (w->buffer);
15025
15026 fprintf (stderr, "PT = %d, BEGV = %d. ZV = %d\n",
15027 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
15028 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
15029 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
15030 fprintf (stderr, "=============================================\n");
15031 dump_glyph_matrix (w->current_matrix,
15032 NILP (glyphs) ? 0 : XINT (glyphs));
15033 return Qnil;
15034 }
15035
15036
15037 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
15038 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
15039 ()
15040 {
15041 struct frame *f = XFRAME (selected_frame);
15042 dump_glyph_matrix (f->current_matrix, 1);
15043 return Qnil;
15044 }
15045
15046
15047 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
15048 doc: /* Dump glyph row ROW to stderr.
15049 GLYPH 0 means don't dump glyphs.
15050 GLYPH 1 means dump glyphs in short form.
15051 GLYPH > 1 or omitted means dump glyphs in long form. */)
15052 (row, glyphs)
15053 Lisp_Object row, glyphs;
15054 {
15055 struct glyph_matrix *matrix;
15056 int vpos;
15057
15058 CHECK_NUMBER (row);
15059 matrix = XWINDOW (selected_window)->current_matrix;
15060 vpos = XINT (row);
15061 if (vpos >= 0 && vpos < matrix->nrows)
15062 dump_glyph_row (MATRIX_ROW (matrix, vpos),
15063 vpos,
15064 INTEGERP (glyphs) ? XINT (glyphs) : 2);
15065 return Qnil;
15066 }
15067
15068
15069 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
15070 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
15071 GLYPH 0 means don't dump glyphs.
15072 GLYPH 1 means dump glyphs in short form.
15073 GLYPH > 1 or omitted means dump glyphs in long form. */)
15074 (row, glyphs)
15075 Lisp_Object row, glyphs;
15076 {
15077 struct frame *sf = SELECTED_FRAME ();
15078 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
15079 int vpos;
15080
15081 CHECK_NUMBER (row);
15082 vpos = XINT (row);
15083 if (vpos >= 0 && vpos < m->nrows)
15084 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
15085 INTEGERP (glyphs) ? XINT (glyphs) : 2);
15086 return Qnil;
15087 }
15088
15089
15090 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
15091 doc: /* Toggle tracing of redisplay.
15092 With ARG, turn tracing on if and only if ARG is positive. */)
15093 (arg)
15094 Lisp_Object arg;
15095 {
15096 if (NILP (arg))
15097 trace_redisplay_p = !trace_redisplay_p;
15098 else
15099 {
15100 arg = Fprefix_numeric_value (arg);
15101 trace_redisplay_p = XINT (arg) > 0;
15102 }
15103
15104 return Qnil;
15105 }
15106
15107
15108 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
15109 doc: /* Like `format', but print result to stderr.
15110 usage: (trace-to-stderr STRING &rest OBJECTS) */)
15111 (nargs, args)
15112 int nargs;
15113 Lisp_Object *args;
15114 {
15115 Lisp_Object s = Fformat (nargs, args);
15116 fprintf (stderr, "%s", SDATA (s));
15117 return Qnil;
15118 }
15119
15120 #endif /* GLYPH_DEBUG */
15121
15122
15123 \f
15124 /***********************************************************************
15125 Building Desired Matrix Rows
15126 ***********************************************************************/
15127
15128 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
15129 Used for non-window-redisplay windows, and for windows w/o left fringe. */
15130
15131 static struct glyph_row *
15132 get_overlay_arrow_glyph_row (w, overlay_arrow_string)
15133 struct window *w;
15134 Lisp_Object overlay_arrow_string;
15135 {
15136 struct frame *f = XFRAME (WINDOW_FRAME (w));
15137 struct buffer *buffer = XBUFFER (w->buffer);
15138 struct buffer *old = current_buffer;
15139 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
15140 int arrow_len = SCHARS (overlay_arrow_string);
15141 const unsigned char *arrow_end = arrow_string + arrow_len;
15142 const unsigned char *p;
15143 struct it it;
15144 int multibyte_p;
15145 int n_glyphs_before;
15146
15147 set_buffer_temp (buffer);
15148 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
15149 it.glyph_row->used[TEXT_AREA] = 0;
15150 SET_TEXT_POS (it.position, 0, 0);
15151
15152 multibyte_p = !NILP (buffer->enable_multibyte_characters);
15153 p = arrow_string;
15154 while (p < arrow_end)
15155 {
15156 Lisp_Object face, ilisp;
15157
15158 /* Get the next character. */
15159 if (multibyte_p)
15160 it.c = string_char_and_length (p, arrow_len, &it.len);
15161 else
15162 it.c = *p, it.len = 1;
15163 p += it.len;
15164
15165 /* Get its face. */
15166 ilisp = make_number (p - arrow_string);
15167 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
15168 it.face_id = compute_char_face (f, it.c, face);
15169
15170 /* Compute its width, get its glyphs. */
15171 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
15172 SET_TEXT_POS (it.position, -1, -1);
15173 PRODUCE_GLYPHS (&it);
15174
15175 /* If this character doesn't fit any more in the line, we have
15176 to remove some glyphs. */
15177 if (it.current_x > it.last_visible_x)
15178 {
15179 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
15180 break;
15181 }
15182 }
15183
15184 set_buffer_temp (old);
15185 return it.glyph_row;
15186 }
15187
15188
15189 /* Insert truncation glyphs at the start of IT->glyph_row. Truncation
15190 glyphs are only inserted for terminal frames since we can't really
15191 win with truncation glyphs when partially visible glyphs are
15192 involved. Which glyphs to insert is determined by
15193 produce_special_glyphs. */
15194
15195 static void
15196 insert_left_trunc_glyphs (it)
15197 struct it *it;
15198 {
15199 struct it truncate_it;
15200 struct glyph *from, *end, *to, *toend;
15201
15202 xassert (!FRAME_WINDOW_P (it->f));
15203
15204 /* Get the truncation glyphs. */
15205 truncate_it = *it;
15206 truncate_it.current_x = 0;
15207 truncate_it.face_id = DEFAULT_FACE_ID;
15208 truncate_it.glyph_row = &scratch_glyph_row;
15209 truncate_it.glyph_row->used[TEXT_AREA] = 0;
15210 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
15211 truncate_it.object = make_number (0);
15212 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
15213
15214 /* Overwrite glyphs from IT with truncation glyphs. */
15215 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
15216 end = from + truncate_it.glyph_row->used[TEXT_AREA];
15217 to = it->glyph_row->glyphs[TEXT_AREA];
15218 toend = to + it->glyph_row->used[TEXT_AREA];
15219
15220 while (from < end)
15221 *to++ = *from++;
15222
15223 /* There may be padding glyphs left over. Overwrite them too. */
15224 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
15225 {
15226 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
15227 while (from < end)
15228 *to++ = *from++;
15229 }
15230
15231 if (to > toend)
15232 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
15233 }
15234
15235
15236 /* Compute the pixel height and width of IT->glyph_row.
15237
15238 Most of the time, ascent and height of a display line will be equal
15239 to the max_ascent and max_height values of the display iterator
15240 structure. This is not the case if
15241
15242 1. We hit ZV without displaying anything. In this case, max_ascent
15243 and max_height will be zero.
15244
15245 2. We have some glyphs that don't contribute to the line height.
15246 (The glyph row flag contributes_to_line_height_p is for future
15247 pixmap extensions).
15248
15249 The first case is easily covered by using default values because in
15250 these cases, the line height does not really matter, except that it
15251 must not be zero. */
15252
15253 static void
15254 compute_line_metrics (it)
15255 struct it *it;
15256 {
15257 struct glyph_row *row = it->glyph_row;
15258 int area, i;
15259
15260 if (FRAME_WINDOW_P (it->f))
15261 {
15262 int i, min_y, max_y;
15263
15264 /* The line may consist of one space only, that was added to
15265 place the cursor on it. If so, the row's height hasn't been
15266 computed yet. */
15267 if (row->height == 0)
15268 {
15269 if (it->max_ascent + it->max_descent == 0)
15270 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
15271 row->ascent = it->max_ascent;
15272 row->height = it->max_ascent + it->max_descent;
15273 row->phys_ascent = it->max_phys_ascent;
15274 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
15275 row->extra_line_spacing = it->max_extra_line_spacing;
15276 }
15277
15278 /* Compute the width of this line. */
15279 row->pixel_width = row->x;
15280 for (i = 0; i < row->used[TEXT_AREA]; ++i)
15281 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
15282
15283 xassert (row->pixel_width >= 0);
15284 xassert (row->ascent >= 0 && row->height > 0);
15285
15286 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
15287 || MATRIX_ROW_OVERLAPS_PRED_P (row));
15288
15289 /* If first line's physical ascent is larger than its logical
15290 ascent, use the physical ascent, and make the row taller.
15291 This makes accented characters fully visible. */
15292 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
15293 && row->phys_ascent > row->ascent)
15294 {
15295 row->height += row->phys_ascent - row->ascent;
15296 row->ascent = row->phys_ascent;
15297 }
15298
15299 /* Compute how much of the line is visible. */
15300 row->visible_height = row->height;
15301
15302 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
15303 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
15304
15305 if (row->y < min_y)
15306 row->visible_height -= min_y - row->y;
15307 if (row->y + row->height > max_y)
15308 row->visible_height -= row->y + row->height - max_y;
15309 }
15310 else
15311 {
15312 row->pixel_width = row->used[TEXT_AREA];
15313 if (row->continued_p)
15314 row->pixel_width -= it->continuation_pixel_width;
15315 else if (row->truncated_on_right_p)
15316 row->pixel_width -= it->truncation_pixel_width;
15317 row->ascent = row->phys_ascent = 0;
15318 row->height = row->phys_height = row->visible_height = 1;
15319 row->extra_line_spacing = 0;
15320 }
15321
15322 /* Compute a hash code for this row. */
15323 row->hash = 0;
15324 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
15325 for (i = 0; i < row->used[area]; ++i)
15326 row->hash = ((((row->hash << 4) + (row->hash >> 24)) & 0x0fffffff)
15327 + row->glyphs[area][i].u.val
15328 + row->glyphs[area][i].face_id
15329 + row->glyphs[area][i].padding_p
15330 + (row->glyphs[area][i].type << 2));
15331
15332 it->max_ascent = it->max_descent = 0;
15333 it->max_phys_ascent = it->max_phys_descent = 0;
15334 }
15335
15336
15337 /* Append one space to the glyph row of iterator IT if doing a
15338 window-based redisplay. The space has the same face as
15339 IT->face_id. Value is non-zero if a space was added.
15340
15341 This function is called to make sure that there is always one glyph
15342 at the end of a glyph row that the cursor can be set on under
15343 window-systems. (If there weren't such a glyph we would not know
15344 how wide and tall a box cursor should be displayed).
15345
15346 At the same time this space let's a nicely handle clearing to the
15347 end of the line if the row ends in italic text. */
15348
15349 static int
15350 append_space_for_newline (it, default_face_p)
15351 struct it *it;
15352 int default_face_p;
15353 {
15354 if (FRAME_WINDOW_P (it->f))
15355 {
15356 int n = it->glyph_row->used[TEXT_AREA];
15357
15358 if (it->glyph_row->glyphs[TEXT_AREA] + n
15359 < it->glyph_row->glyphs[1 + TEXT_AREA])
15360 {
15361 /* Save some values that must not be changed.
15362 Must save IT->c and IT->len because otherwise
15363 ITERATOR_AT_END_P wouldn't work anymore after
15364 append_space_for_newline has been called. */
15365 enum display_element_type saved_what = it->what;
15366 int saved_c = it->c, saved_len = it->len;
15367 int saved_x = it->current_x;
15368 int saved_face_id = it->face_id;
15369 struct text_pos saved_pos;
15370 Lisp_Object saved_object;
15371 struct face *face;
15372
15373 saved_object = it->object;
15374 saved_pos = it->position;
15375
15376 it->what = IT_CHARACTER;
15377 bzero (&it->position, sizeof it->position);
15378 it->object = make_number (0);
15379 it->c = ' ';
15380 it->len = 1;
15381
15382 if (default_face_p)
15383 it->face_id = DEFAULT_FACE_ID;
15384 else if (it->face_before_selective_p)
15385 it->face_id = it->saved_face_id;
15386 face = FACE_FROM_ID (it->f, it->face_id);
15387 it->face_id = FACE_FOR_CHAR (it->f, face, 0);
15388
15389 PRODUCE_GLYPHS (it);
15390
15391 it->override_ascent = -1;
15392 it->constrain_row_ascent_descent_p = 0;
15393 it->current_x = saved_x;
15394 it->object = saved_object;
15395 it->position = saved_pos;
15396 it->what = saved_what;
15397 it->face_id = saved_face_id;
15398 it->len = saved_len;
15399 it->c = saved_c;
15400 return 1;
15401 }
15402 }
15403
15404 return 0;
15405 }
15406
15407
15408 /* Extend the face of the last glyph in the text area of IT->glyph_row
15409 to the end of the display line. Called from display_line.
15410 If the glyph row is empty, add a space glyph to it so that we
15411 know the face to draw. Set the glyph row flag fill_line_p. */
15412
15413 static void
15414 extend_face_to_end_of_line (it)
15415 struct it *it;
15416 {
15417 struct face *face;
15418 struct frame *f = it->f;
15419
15420 /* If line is already filled, do nothing. */
15421 if (it->current_x >= it->last_visible_x)
15422 return;
15423
15424 /* Face extension extends the background and box of IT->face_id
15425 to the end of the line. If the background equals the background
15426 of the frame, we don't have to do anything. */
15427 if (it->face_before_selective_p)
15428 face = FACE_FROM_ID (it->f, it->saved_face_id);
15429 else
15430 face = FACE_FROM_ID (f, it->face_id);
15431
15432 if (FRAME_WINDOW_P (f)
15433 && it->glyph_row->displays_text_p
15434 && face->box == FACE_NO_BOX
15435 && face->background == FRAME_BACKGROUND_PIXEL (f)
15436 && !face->stipple)
15437 return;
15438
15439 /* Set the glyph row flag indicating that the face of the last glyph
15440 in the text area has to be drawn to the end of the text area. */
15441 it->glyph_row->fill_line_p = 1;
15442
15443 /* If current character of IT is not ASCII, make sure we have the
15444 ASCII face. This will be automatically undone the next time
15445 get_next_display_element returns a multibyte character. Note
15446 that the character will always be single byte in unibyte text. */
15447 if (!SINGLE_BYTE_CHAR_P (it->c))
15448 {
15449 it->face_id = FACE_FOR_CHAR (f, face, 0);
15450 }
15451
15452 if (FRAME_WINDOW_P (f))
15453 {
15454 /* If the row is empty, add a space with the current face of IT,
15455 so that we know which face to draw. */
15456 if (it->glyph_row->used[TEXT_AREA] == 0)
15457 {
15458 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
15459 it->glyph_row->glyphs[TEXT_AREA][0].face_id = it->face_id;
15460 it->glyph_row->used[TEXT_AREA] = 1;
15461 }
15462 }
15463 else
15464 {
15465 /* Save some values that must not be changed. */
15466 int saved_x = it->current_x;
15467 struct text_pos saved_pos;
15468 Lisp_Object saved_object;
15469 enum display_element_type saved_what = it->what;
15470 int saved_face_id = it->face_id;
15471
15472 saved_object = it->object;
15473 saved_pos = it->position;
15474
15475 it->what = IT_CHARACTER;
15476 bzero (&it->position, sizeof it->position);
15477 it->object = make_number (0);
15478 it->c = ' ';
15479 it->len = 1;
15480 it->face_id = face->id;
15481
15482 PRODUCE_GLYPHS (it);
15483
15484 while (it->current_x <= it->last_visible_x)
15485 PRODUCE_GLYPHS (it);
15486
15487 /* Don't count these blanks really. It would let us insert a left
15488 truncation glyph below and make us set the cursor on them, maybe. */
15489 it->current_x = saved_x;
15490 it->object = saved_object;
15491 it->position = saved_pos;
15492 it->what = saved_what;
15493 it->face_id = saved_face_id;
15494 }
15495 }
15496
15497
15498 /* Value is non-zero if text starting at CHARPOS in current_buffer is
15499 trailing whitespace. */
15500
15501 static int
15502 trailing_whitespace_p (charpos)
15503 int charpos;
15504 {
15505 int bytepos = CHAR_TO_BYTE (charpos);
15506 int c = 0;
15507
15508 while (bytepos < ZV_BYTE
15509 && (c = FETCH_CHAR (bytepos),
15510 c == ' ' || c == '\t'))
15511 ++bytepos;
15512
15513 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
15514 {
15515 if (bytepos != PT_BYTE)
15516 return 1;
15517 }
15518 return 0;
15519 }
15520
15521
15522 /* Highlight trailing whitespace, if any, in ROW. */
15523
15524 void
15525 highlight_trailing_whitespace (f, row)
15526 struct frame *f;
15527 struct glyph_row *row;
15528 {
15529 int used = row->used[TEXT_AREA];
15530
15531 if (used)
15532 {
15533 struct glyph *start = row->glyphs[TEXT_AREA];
15534 struct glyph *glyph = start + used - 1;
15535
15536 /* Skip over glyphs inserted to display the cursor at the
15537 end of a line, for extending the face of the last glyph
15538 to the end of the line on terminals, and for truncation
15539 and continuation glyphs. */
15540 while (glyph >= start
15541 && glyph->type == CHAR_GLYPH
15542 && INTEGERP (glyph->object))
15543 --glyph;
15544
15545 /* If last glyph is a space or stretch, and it's trailing
15546 whitespace, set the face of all trailing whitespace glyphs in
15547 IT->glyph_row to `trailing-whitespace'. */
15548 if (glyph >= start
15549 && BUFFERP (glyph->object)
15550 && (glyph->type == STRETCH_GLYPH
15551 || (glyph->type == CHAR_GLYPH
15552 && glyph->u.ch == ' '))
15553 && trailing_whitespace_p (glyph->charpos))
15554 {
15555 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0, 0);
15556 if (face_id < 0)
15557 return;
15558
15559 while (glyph >= start
15560 && BUFFERP (glyph->object)
15561 && (glyph->type == STRETCH_GLYPH
15562 || (glyph->type == CHAR_GLYPH
15563 && glyph->u.ch == ' ')))
15564 (glyph--)->face_id = face_id;
15565 }
15566 }
15567 }
15568
15569
15570 /* Value is non-zero if glyph row ROW in window W should be
15571 used to hold the cursor. */
15572
15573 static int
15574 cursor_row_p (w, row)
15575 struct window *w;
15576 struct glyph_row *row;
15577 {
15578 int cursor_row_p = 1;
15579
15580 if (PT == MATRIX_ROW_END_CHARPOS (row))
15581 {
15582 /* If the row ends with a newline from a string, we don't want
15583 the cursor there, but we still want it at the start of the
15584 string if the string starts in this row.
15585 If the row is continued it doesn't end in a newline. */
15586 if (CHARPOS (row->end.string_pos) >= 0)
15587 cursor_row_p = (row->continued_p
15588 || PT >= MATRIX_ROW_START_CHARPOS (row));
15589 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
15590 {
15591 /* If the row ends in middle of a real character,
15592 and the line is continued, we want the cursor here.
15593 That's because MATRIX_ROW_END_CHARPOS would equal
15594 PT if PT is before the character. */
15595 if (!row->ends_in_ellipsis_p)
15596 cursor_row_p = row->continued_p;
15597 else
15598 /* If the row ends in an ellipsis, then
15599 MATRIX_ROW_END_CHARPOS will equal point after the invisible text.
15600 We want that position to be displayed after the ellipsis. */
15601 cursor_row_p = 0;
15602 }
15603 /* If the row ends at ZV, display the cursor at the end of that
15604 row instead of at the start of the row below. */
15605 else if (row->ends_at_zv_p)
15606 cursor_row_p = 1;
15607 else
15608 cursor_row_p = 0;
15609 }
15610
15611 return cursor_row_p;
15612 }
15613
15614
15615 /* Construct the glyph row IT->glyph_row in the desired matrix of
15616 IT->w from text at the current position of IT. See dispextern.h
15617 for an overview of struct it. Value is non-zero if
15618 IT->glyph_row displays text, as opposed to a line displaying ZV
15619 only. */
15620
15621 static int
15622 display_line (it)
15623 struct it *it;
15624 {
15625 struct glyph_row *row = it->glyph_row;
15626 Lisp_Object overlay_arrow_string;
15627
15628 /* We always start displaying at hpos zero even if hscrolled. */
15629 xassert (it->hpos == 0 && it->current_x == 0);
15630
15631 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
15632 >= it->w->desired_matrix->nrows)
15633 {
15634 it->w->nrows_scale_factor++;
15635 fonts_changed_p = 1;
15636 return 0;
15637 }
15638
15639 /* Is IT->w showing the region? */
15640 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
15641
15642 /* Clear the result glyph row and enable it. */
15643 prepare_desired_row (row);
15644
15645 row->y = it->current_y;
15646 row->start = it->start;
15647 row->continuation_lines_width = it->continuation_lines_width;
15648 row->displays_text_p = 1;
15649 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
15650 it->starts_in_middle_of_char_p = 0;
15651
15652 /* Arrange the overlays nicely for our purposes. Usually, we call
15653 display_line on only one line at a time, in which case this
15654 can't really hurt too much, or we call it on lines which appear
15655 one after another in the buffer, in which case all calls to
15656 recenter_overlay_lists but the first will be pretty cheap. */
15657 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
15658
15659 /* Move over display elements that are not visible because we are
15660 hscrolled. This may stop at an x-position < IT->first_visible_x
15661 if the first glyph is partially visible or if we hit a line end. */
15662 if (it->current_x < it->first_visible_x)
15663 {
15664 move_it_in_display_line_to (it, ZV, it->first_visible_x,
15665 MOVE_TO_POS | MOVE_TO_X);
15666 }
15667
15668 /* Get the initial row height. This is either the height of the
15669 text hscrolled, if there is any, or zero. */
15670 row->ascent = it->max_ascent;
15671 row->height = it->max_ascent + it->max_descent;
15672 row->phys_ascent = it->max_phys_ascent;
15673 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
15674 row->extra_line_spacing = it->max_extra_line_spacing;
15675
15676 /* Loop generating characters. The loop is left with IT on the next
15677 character to display. */
15678 while (1)
15679 {
15680 int n_glyphs_before, hpos_before, x_before;
15681 int x, i, nglyphs;
15682 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
15683
15684 /* Retrieve the next thing to display. Value is zero if end of
15685 buffer reached. */
15686 if (!get_next_display_element (it))
15687 {
15688 /* Maybe add a space at the end of this line that is used to
15689 display the cursor there under X. Set the charpos of the
15690 first glyph of blank lines not corresponding to any text
15691 to -1. */
15692 #ifdef HAVE_WINDOW_SYSTEM
15693 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
15694 row->exact_window_width_line_p = 1;
15695 else
15696 #endif /* HAVE_WINDOW_SYSTEM */
15697 if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
15698 || row->used[TEXT_AREA] == 0)
15699 {
15700 row->glyphs[TEXT_AREA]->charpos = -1;
15701 row->displays_text_p = 0;
15702
15703 if (!NILP (XBUFFER (it->w->buffer)->indicate_empty_lines)
15704 && (!MINI_WINDOW_P (it->w)
15705 || (minibuf_level && EQ (it->window, minibuf_window))))
15706 row->indicate_empty_line_p = 1;
15707 }
15708
15709 it->continuation_lines_width = 0;
15710 row->ends_at_zv_p = 1;
15711 break;
15712 }
15713
15714 /* Now, get the metrics of what we want to display. This also
15715 generates glyphs in `row' (which is IT->glyph_row). */
15716 n_glyphs_before = row->used[TEXT_AREA];
15717 x = it->current_x;
15718
15719 /* Remember the line height so far in case the next element doesn't
15720 fit on the line. */
15721 if (!it->truncate_lines_p)
15722 {
15723 ascent = it->max_ascent;
15724 descent = it->max_descent;
15725 phys_ascent = it->max_phys_ascent;
15726 phys_descent = it->max_phys_descent;
15727 }
15728
15729 PRODUCE_GLYPHS (it);
15730
15731 /* If this display element was in marginal areas, continue with
15732 the next one. */
15733 if (it->area != TEXT_AREA)
15734 {
15735 row->ascent = max (row->ascent, it->max_ascent);
15736 row->height = max (row->height, it->max_ascent + it->max_descent);
15737 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
15738 row->phys_height = max (row->phys_height,
15739 it->max_phys_ascent + it->max_phys_descent);
15740 row->extra_line_spacing = max (row->extra_line_spacing,
15741 it->max_extra_line_spacing);
15742 set_iterator_to_next (it, 1);
15743 continue;
15744 }
15745
15746 /* Does the display element fit on the line? If we truncate
15747 lines, we should draw past the right edge of the window. If
15748 we don't truncate, we want to stop so that we can display the
15749 continuation glyph before the right margin. If lines are
15750 continued, there are two possible strategies for characters
15751 resulting in more than 1 glyph (e.g. tabs): Display as many
15752 glyphs as possible in this line and leave the rest for the
15753 continuation line, or display the whole element in the next
15754 line. Original redisplay did the former, so we do it also. */
15755 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
15756 hpos_before = it->hpos;
15757 x_before = x;
15758
15759 if (/* Not a newline. */
15760 nglyphs > 0
15761 /* Glyphs produced fit entirely in the line. */
15762 && it->current_x < it->last_visible_x)
15763 {
15764 it->hpos += nglyphs;
15765 row->ascent = max (row->ascent, it->max_ascent);
15766 row->height = max (row->height, it->max_ascent + it->max_descent);
15767 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
15768 row->phys_height = max (row->phys_height,
15769 it->max_phys_ascent + it->max_phys_descent);
15770 row->extra_line_spacing = max (row->extra_line_spacing,
15771 it->max_extra_line_spacing);
15772 if (it->current_x - it->pixel_width < it->first_visible_x)
15773 row->x = x - it->first_visible_x;
15774 }
15775 else
15776 {
15777 int new_x;
15778 struct glyph *glyph;
15779
15780 for (i = 0; i < nglyphs; ++i, x = new_x)
15781 {
15782 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
15783 new_x = x + glyph->pixel_width;
15784
15785 if (/* Lines are continued. */
15786 !it->truncate_lines_p
15787 && (/* Glyph doesn't fit on the line. */
15788 new_x > it->last_visible_x
15789 /* Or it fits exactly on a window system frame. */
15790 || (new_x == it->last_visible_x
15791 && FRAME_WINDOW_P (it->f))))
15792 {
15793 /* End of a continued line. */
15794
15795 if (it->hpos == 0
15796 || (new_x == it->last_visible_x
15797 && FRAME_WINDOW_P (it->f)))
15798 {
15799 /* Current glyph is the only one on the line or
15800 fits exactly on the line. We must continue
15801 the line because we can't draw the cursor
15802 after the glyph. */
15803 row->continued_p = 1;
15804 it->current_x = new_x;
15805 it->continuation_lines_width += new_x;
15806 ++it->hpos;
15807 if (i == nglyphs - 1)
15808 {
15809 set_iterator_to_next (it, 1);
15810 #ifdef HAVE_WINDOW_SYSTEM
15811 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
15812 {
15813 if (!get_next_display_element (it))
15814 {
15815 row->exact_window_width_line_p = 1;
15816 it->continuation_lines_width = 0;
15817 row->continued_p = 0;
15818 row->ends_at_zv_p = 1;
15819 }
15820 else if (ITERATOR_AT_END_OF_LINE_P (it))
15821 {
15822 row->continued_p = 0;
15823 row->exact_window_width_line_p = 1;
15824 }
15825 }
15826 #endif /* HAVE_WINDOW_SYSTEM */
15827 }
15828 }
15829 else if (CHAR_GLYPH_PADDING_P (*glyph)
15830 && !FRAME_WINDOW_P (it->f))
15831 {
15832 /* A padding glyph that doesn't fit on this line.
15833 This means the whole character doesn't fit
15834 on the line. */
15835 row->used[TEXT_AREA] = n_glyphs_before;
15836
15837 /* Fill the rest of the row with continuation
15838 glyphs like in 20.x. */
15839 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
15840 < row->glyphs[1 + TEXT_AREA])
15841 produce_special_glyphs (it, IT_CONTINUATION);
15842
15843 row->continued_p = 1;
15844 it->current_x = x_before;
15845 it->continuation_lines_width += x_before;
15846
15847 /* Restore the height to what it was before the
15848 element not fitting on the line. */
15849 it->max_ascent = ascent;
15850 it->max_descent = descent;
15851 it->max_phys_ascent = phys_ascent;
15852 it->max_phys_descent = phys_descent;
15853 }
15854 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
15855 {
15856 /* A TAB that extends past the right edge of the
15857 window. This produces a single glyph on
15858 window system frames. We leave the glyph in
15859 this row and let it fill the row, but don't
15860 consume the TAB. */
15861 it->continuation_lines_width += it->last_visible_x;
15862 row->ends_in_middle_of_char_p = 1;
15863 row->continued_p = 1;
15864 glyph->pixel_width = it->last_visible_x - x;
15865 it->starts_in_middle_of_char_p = 1;
15866 }
15867 else
15868 {
15869 /* Something other than a TAB that draws past
15870 the right edge of the window. Restore
15871 positions to values before the element. */
15872 row->used[TEXT_AREA] = n_glyphs_before + i;
15873
15874 /* Display continuation glyphs. */
15875 if (!FRAME_WINDOW_P (it->f))
15876 produce_special_glyphs (it, IT_CONTINUATION);
15877 row->continued_p = 1;
15878
15879 it->current_x = x_before;
15880 it->continuation_lines_width += x;
15881 extend_face_to_end_of_line (it);
15882
15883 if (nglyphs > 1 && i > 0)
15884 {
15885 row->ends_in_middle_of_char_p = 1;
15886 it->starts_in_middle_of_char_p = 1;
15887 }
15888
15889 /* Restore the height to what it was before the
15890 element not fitting on the line. */
15891 it->max_ascent = ascent;
15892 it->max_descent = descent;
15893 it->max_phys_ascent = phys_ascent;
15894 it->max_phys_descent = phys_descent;
15895 }
15896
15897 break;
15898 }
15899 else if (new_x > it->first_visible_x)
15900 {
15901 /* Increment number of glyphs actually displayed. */
15902 ++it->hpos;
15903
15904 if (x < it->first_visible_x)
15905 /* Glyph is partially visible, i.e. row starts at
15906 negative X position. */
15907 row->x = x - it->first_visible_x;
15908 }
15909 else
15910 {
15911 /* Glyph is completely off the left margin of the
15912 window. This should not happen because of the
15913 move_it_in_display_line at the start of this
15914 function, unless the text display area of the
15915 window is empty. */
15916 xassert (it->first_visible_x <= it->last_visible_x);
15917 }
15918 }
15919
15920 row->ascent = max (row->ascent, it->max_ascent);
15921 row->height = max (row->height, it->max_ascent + it->max_descent);
15922 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
15923 row->phys_height = max (row->phys_height,
15924 it->max_phys_ascent + it->max_phys_descent);
15925 row->extra_line_spacing = max (row->extra_line_spacing,
15926 it->max_extra_line_spacing);
15927
15928 /* End of this display line if row is continued. */
15929 if (row->continued_p || row->ends_at_zv_p)
15930 break;
15931 }
15932
15933 at_end_of_line:
15934 /* Is this a line end? If yes, we're also done, after making
15935 sure that a non-default face is extended up to the right
15936 margin of the window. */
15937 if (ITERATOR_AT_END_OF_LINE_P (it))
15938 {
15939 int used_before = row->used[TEXT_AREA];
15940
15941 row->ends_in_newline_from_string_p = STRINGP (it->object);
15942
15943 #ifdef HAVE_WINDOW_SYSTEM
15944 /* Add a space at the end of the line that is used to
15945 display the cursor there. */
15946 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
15947 append_space_for_newline (it, 0);
15948 #endif /* HAVE_WINDOW_SYSTEM */
15949
15950 /* Extend the face to the end of the line. */
15951 extend_face_to_end_of_line (it);
15952
15953 /* Make sure we have the position. */
15954 if (used_before == 0)
15955 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
15956
15957 /* Consume the line end. This skips over invisible lines. */
15958 set_iterator_to_next (it, 1);
15959 it->continuation_lines_width = 0;
15960 break;
15961 }
15962
15963 /* Proceed with next display element. Note that this skips
15964 over lines invisible because of selective display. */
15965 set_iterator_to_next (it, 1);
15966
15967 /* If we truncate lines, we are done when the last displayed
15968 glyphs reach past the right margin of the window. */
15969 if (it->truncate_lines_p
15970 && (FRAME_WINDOW_P (it->f)
15971 ? (it->current_x >= it->last_visible_x)
15972 : (it->current_x > it->last_visible_x)))
15973 {
15974 /* Maybe add truncation glyphs. */
15975 if (!FRAME_WINDOW_P (it->f))
15976 {
15977 int i, n;
15978
15979 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
15980 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
15981 break;
15982
15983 for (n = row->used[TEXT_AREA]; i < n; ++i)
15984 {
15985 row->used[TEXT_AREA] = i;
15986 produce_special_glyphs (it, IT_TRUNCATION);
15987 }
15988 }
15989 #ifdef HAVE_WINDOW_SYSTEM
15990 else
15991 {
15992 /* Don't truncate if we can overflow newline into fringe. */
15993 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
15994 {
15995 if (!get_next_display_element (it))
15996 {
15997 it->continuation_lines_width = 0;
15998 row->ends_at_zv_p = 1;
15999 row->exact_window_width_line_p = 1;
16000 break;
16001 }
16002 if (ITERATOR_AT_END_OF_LINE_P (it))
16003 {
16004 row->exact_window_width_line_p = 1;
16005 goto at_end_of_line;
16006 }
16007 }
16008 }
16009 #endif /* HAVE_WINDOW_SYSTEM */
16010
16011 row->truncated_on_right_p = 1;
16012 it->continuation_lines_width = 0;
16013 reseat_at_next_visible_line_start (it, 0);
16014 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
16015 it->hpos = hpos_before;
16016 it->current_x = x_before;
16017 break;
16018 }
16019 }
16020
16021 /* If line is not empty and hscrolled, maybe insert truncation glyphs
16022 at the left window margin. */
16023 if (it->first_visible_x
16024 && IT_CHARPOS (*it) != MATRIX_ROW_START_CHARPOS (row))
16025 {
16026 if (!FRAME_WINDOW_P (it->f))
16027 insert_left_trunc_glyphs (it);
16028 row->truncated_on_left_p = 1;
16029 }
16030
16031 /* If the start of this line is the overlay arrow-position, then
16032 mark this glyph row as the one containing the overlay arrow.
16033 This is clearly a mess with variable size fonts. It would be
16034 better to let it be displayed like cursors under X. */
16035 if ((row->displays_text_p || !overlay_arrow_seen)
16036 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
16037 !NILP (overlay_arrow_string)))
16038 {
16039 /* Overlay arrow in window redisplay is a fringe bitmap. */
16040 if (STRINGP (overlay_arrow_string))
16041 {
16042 struct glyph_row *arrow_row
16043 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
16044 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
16045 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
16046 struct glyph *p = row->glyphs[TEXT_AREA];
16047 struct glyph *p2, *end;
16048
16049 /* Copy the arrow glyphs. */
16050 while (glyph < arrow_end)
16051 *p++ = *glyph++;
16052
16053 /* Throw away padding glyphs. */
16054 p2 = p;
16055 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
16056 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
16057 ++p2;
16058 if (p2 > p)
16059 {
16060 while (p2 < end)
16061 *p++ = *p2++;
16062 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
16063 }
16064 }
16065 else
16066 {
16067 xassert (INTEGERP (overlay_arrow_string));
16068 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
16069 }
16070 overlay_arrow_seen = 1;
16071 }
16072
16073 /* Compute pixel dimensions of this line. */
16074 compute_line_metrics (it);
16075
16076 /* Remember the position at which this line ends. */
16077 row->end = it->current;
16078
16079 /* Record whether this row ends inside an ellipsis. */
16080 row->ends_in_ellipsis_p
16081 = (it->method == GET_FROM_DISPLAY_VECTOR
16082 && it->ellipsis_p);
16083
16084 /* Save fringe bitmaps in this row. */
16085 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
16086 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
16087 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
16088 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
16089
16090 it->left_user_fringe_bitmap = 0;
16091 it->left_user_fringe_face_id = 0;
16092 it->right_user_fringe_bitmap = 0;
16093 it->right_user_fringe_face_id = 0;
16094
16095 /* Maybe set the cursor. */
16096 if (it->w->cursor.vpos < 0
16097 && PT >= MATRIX_ROW_START_CHARPOS (row)
16098 && PT <= MATRIX_ROW_END_CHARPOS (row)
16099 && cursor_row_p (it->w, row))
16100 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
16101
16102 /* Highlight trailing whitespace. */
16103 if (!NILP (Vshow_trailing_whitespace))
16104 highlight_trailing_whitespace (it->f, it->glyph_row);
16105
16106 /* Prepare for the next line. This line starts horizontally at (X
16107 HPOS) = (0 0). Vertical positions are incremented. As a
16108 convenience for the caller, IT->glyph_row is set to the next
16109 row to be used. */
16110 it->current_x = it->hpos = 0;
16111 it->current_y += row->height;
16112 ++it->vpos;
16113 ++it->glyph_row;
16114 it->start = it->current;
16115 return row->displays_text_p;
16116 }
16117
16118
16119 \f
16120 /***********************************************************************
16121 Menu Bar
16122 ***********************************************************************/
16123
16124 /* Redisplay the menu bar in the frame for window W.
16125
16126 The menu bar of X frames that don't have X toolkit support is
16127 displayed in a special window W->frame->menu_bar_window.
16128
16129 The menu bar of terminal frames is treated specially as far as
16130 glyph matrices are concerned. Menu bar lines are not part of
16131 windows, so the update is done directly on the frame matrix rows
16132 for the menu bar. */
16133
16134 static void
16135 display_menu_bar (w)
16136 struct window *w;
16137 {
16138 struct frame *f = XFRAME (WINDOW_FRAME (w));
16139 struct it it;
16140 Lisp_Object items;
16141 int i;
16142
16143 /* Don't do all this for graphical frames. */
16144 #ifdef HAVE_NTGUI
16145 if (!NILP (Vwindow_system))
16146 return;
16147 #endif
16148 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
16149 if (FRAME_X_P (f))
16150 return;
16151 #endif
16152 #ifdef MAC_OS
16153 if (FRAME_MAC_P (f))
16154 return;
16155 #endif
16156
16157 #ifdef USE_X_TOOLKIT
16158 xassert (!FRAME_WINDOW_P (f));
16159 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
16160 it.first_visible_x = 0;
16161 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
16162 #else /* not USE_X_TOOLKIT */
16163 if (FRAME_WINDOW_P (f))
16164 {
16165 /* Menu bar lines are displayed in the desired matrix of the
16166 dummy window menu_bar_window. */
16167 struct window *menu_w;
16168 xassert (WINDOWP (f->menu_bar_window));
16169 menu_w = XWINDOW (f->menu_bar_window);
16170 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
16171 MENU_FACE_ID);
16172 it.first_visible_x = 0;
16173 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
16174 }
16175 else
16176 {
16177 /* This is a TTY frame, i.e. character hpos/vpos are used as
16178 pixel x/y. */
16179 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
16180 MENU_FACE_ID);
16181 it.first_visible_x = 0;
16182 it.last_visible_x = FRAME_COLS (f);
16183 }
16184 #endif /* not USE_X_TOOLKIT */
16185
16186 if (! mode_line_inverse_video)
16187 /* Force the menu-bar to be displayed in the default face. */
16188 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
16189
16190 /* Clear all rows of the menu bar. */
16191 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
16192 {
16193 struct glyph_row *row = it.glyph_row + i;
16194 clear_glyph_row (row);
16195 row->enabled_p = 1;
16196 row->full_width_p = 1;
16197 }
16198
16199 /* Display all items of the menu bar. */
16200 items = FRAME_MENU_BAR_ITEMS (it.f);
16201 for (i = 0; i < XVECTOR (items)->size; i += 4)
16202 {
16203 Lisp_Object string;
16204
16205 /* Stop at nil string. */
16206 string = AREF (items, i + 1);
16207 if (NILP (string))
16208 break;
16209
16210 /* Remember where item was displayed. */
16211 AREF (items, i + 3) = make_number (it.hpos);
16212
16213 /* Display the item, pad with one space. */
16214 if (it.current_x < it.last_visible_x)
16215 display_string (NULL, string, Qnil, 0, 0, &it,
16216 SCHARS (string) + 1, 0, 0, -1);
16217 }
16218
16219 /* Fill out the line with spaces. */
16220 if (it.current_x < it.last_visible_x)
16221 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
16222
16223 /* Compute the total height of the lines. */
16224 compute_line_metrics (&it);
16225 }
16226
16227
16228 \f
16229 /***********************************************************************
16230 Mode Line
16231 ***********************************************************************/
16232
16233 /* Redisplay mode lines in the window tree whose root is WINDOW. If
16234 FORCE is non-zero, redisplay mode lines unconditionally.
16235 Otherwise, redisplay only mode lines that are garbaged. Value is
16236 the number of windows whose mode lines were redisplayed. */
16237
16238 static int
16239 redisplay_mode_lines (window, force)
16240 Lisp_Object window;
16241 int force;
16242 {
16243 int nwindows = 0;
16244
16245 while (!NILP (window))
16246 {
16247 struct window *w = XWINDOW (window);
16248
16249 if (WINDOWP (w->hchild))
16250 nwindows += redisplay_mode_lines (w->hchild, force);
16251 else if (WINDOWP (w->vchild))
16252 nwindows += redisplay_mode_lines (w->vchild, force);
16253 else if (force
16254 || FRAME_GARBAGED_P (XFRAME (w->frame))
16255 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
16256 {
16257 struct text_pos lpoint;
16258 struct buffer *old = current_buffer;
16259
16260 /* Set the window's buffer for the mode line display. */
16261 SET_TEXT_POS (lpoint, PT, PT_BYTE);
16262 set_buffer_internal_1 (XBUFFER (w->buffer));
16263
16264 /* Point refers normally to the selected window. For any
16265 other window, set up appropriate value. */
16266 if (!EQ (window, selected_window))
16267 {
16268 struct text_pos pt;
16269
16270 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
16271 if (CHARPOS (pt) < BEGV)
16272 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
16273 else if (CHARPOS (pt) > (ZV - 1))
16274 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
16275 else
16276 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
16277 }
16278
16279 /* Display mode lines. */
16280 clear_glyph_matrix (w->desired_matrix);
16281 if (display_mode_lines (w))
16282 {
16283 ++nwindows;
16284 w->must_be_updated_p = 1;
16285 }
16286
16287 /* Restore old settings. */
16288 set_buffer_internal_1 (old);
16289 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
16290 }
16291
16292 window = w->next;
16293 }
16294
16295 return nwindows;
16296 }
16297
16298
16299 /* Display the mode and/or top line of window W. Value is the number
16300 of mode lines displayed. */
16301
16302 static int
16303 display_mode_lines (w)
16304 struct window *w;
16305 {
16306 Lisp_Object old_selected_window, old_selected_frame;
16307 int n = 0;
16308
16309 old_selected_frame = selected_frame;
16310 selected_frame = w->frame;
16311 old_selected_window = selected_window;
16312 XSETWINDOW (selected_window, w);
16313
16314 /* These will be set while the mode line specs are processed. */
16315 line_number_displayed = 0;
16316 w->column_number_displayed = Qnil;
16317
16318 if (WINDOW_WANTS_MODELINE_P (w))
16319 {
16320 struct window *sel_w = XWINDOW (old_selected_window);
16321
16322 /* Select mode line face based on the real selected window. */
16323 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
16324 current_buffer->mode_line_format);
16325 ++n;
16326 }
16327
16328 if (WINDOW_WANTS_HEADER_LINE_P (w))
16329 {
16330 display_mode_line (w, HEADER_LINE_FACE_ID,
16331 current_buffer->header_line_format);
16332 ++n;
16333 }
16334
16335 selected_frame = old_selected_frame;
16336 selected_window = old_selected_window;
16337 return n;
16338 }
16339
16340
16341 /* Display mode or top line of window W. FACE_ID specifies which line
16342 to display; it is either MODE_LINE_FACE_ID or HEADER_LINE_FACE_ID.
16343 FORMAT is the mode line format to display. Value is the pixel
16344 height of the mode line displayed. */
16345
16346 static int
16347 display_mode_line (w, face_id, format)
16348 struct window *w;
16349 enum face_id face_id;
16350 Lisp_Object format;
16351 {
16352 struct it it;
16353 struct face *face;
16354 int count = SPECPDL_INDEX ();
16355
16356 init_iterator (&it, w, -1, -1, NULL, face_id);
16357 prepare_desired_row (it.glyph_row);
16358
16359 it.glyph_row->mode_line_p = 1;
16360
16361 if (! mode_line_inverse_video)
16362 /* Force the mode-line to be displayed in the default face. */
16363 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
16364
16365 record_unwind_protect (unwind_format_mode_line,
16366 format_mode_line_unwind_data (NULL, 0));
16367
16368 mode_line_target = MODE_LINE_DISPLAY;
16369
16370 /* Temporarily make frame's keyboard the current kboard so that
16371 kboard-local variables in the mode_line_format will get the right
16372 values. */
16373 push_frame_kboard (it.f);
16374 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
16375 pop_frame_kboard ();
16376
16377 unbind_to (count, Qnil);
16378
16379 /* Fill up with spaces. */
16380 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
16381
16382 compute_line_metrics (&it);
16383 it.glyph_row->full_width_p = 1;
16384 it.glyph_row->continued_p = 0;
16385 it.glyph_row->truncated_on_left_p = 0;
16386 it.glyph_row->truncated_on_right_p = 0;
16387
16388 /* Make a 3D mode-line have a shadow at its right end. */
16389 face = FACE_FROM_ID (it.f, face_id);
16390 extend_face_to_end_of_line (&it);
16391 if (face->box != FACE_NO_BOX)
16392 {
16393 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
16394 + it.glyph_row->used[TEXT_AREA] - 1);
16395 last->right_box_line_p = 1;
16396 }
16397
16398 return it.glyph_row->height;
16399 }
16400
16401 /* Move element ELT in LIST to the front of LIST.
16402 Return the updated list. */
16403
16404 static Lisp_Object
16405 move_elt_to_front (elt, list)
16406 Lisp_Object elt, list;
16407 {
16408 register Lisp_Object tail, prev;
16409 register Lisp_Object tem;
16410
16411 tail = list;
16412 prev = Qnil;
16413 while (CONSP (tail))
16414 {
16415 tem = XCAR (tail);
16416
16417 if (EQ (elt, tem))
16418 {
16419 /* Splice out the link TAIL. */
16420 if (NILP (prev))
16421 list = XCDR (tail);
16422 else
16423 Fsetcdr (prev, XCDR (tail));
16424
16425 /* Now make it the first. */
16426 Fsetcdr (tail, list);
16427 return tail;
16428 }
16429 else
16430 prev = tail;
16431 tail = XCDR (tail);
16432 QUIT;
16433 }
16434
16435 /* Not found--return unchanged LIST. */
16436 return list;
16437 }
16438
16439 /* Contribute ELT to the mode line for window IT->w. How it
16440 translates into text depends on its data type.
16441
16442 IT describes the display environment in which we display, as usual.
16443
16444 DEPTH is the depth in recursion. It is used to prevent
16445 infinite recursion here.
16446
16447 FIELD_WIDTH is the number of characters the display of ELT should
16448 occupy in the mode line, and PRECISION is the maximum number of
16449 characters to display from ELT's representation. See
16450 display_string for details.
16451
16452 Returns the hpos of the end of the text generated by ELT.
16453
16454 PROPS is a property list to add to any string we encounter.
16455
16456 If RISKY is nonzero, remove (disregard) any properties in any string
16457 we encounter, and ignore :eval and :propertize.
16458
16459 The global variable `mode_line_target' determines whether the
16460 output is passed to `store_mode_line_noprop',
16461 `store_mode_line_string', or `display_string'. */
16462
16463 static int
16464 display_mode_element (it, depth, field_width, precision, elt, props, risky)
16465 struct it *it;
16466 int depth;
16467 int field_width, precision;
16468 Lisp_Object elt, props;
16469 int risky;
16470 {
16471 int n = 0, field, prec;
16472 int literal = 0;
16473
16474 tail_recurse:
16475 if (depth > 100)
16476 elt = build_string ("*too-deep*");
16477
16478 depth++;
16479
16480 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
16481 {
16482 case Lisp_String:
16483 {
16484 /* A string: output it and check for %-constructs within it. */
16485 unsigned char c;
16486 int offset = 0;
16487
16488 if (SCHARS (elt) > 0
16489 && (!NILP (props) || risky))
16490 {
16491 Lisp_Object oprops, aelt;
16492 oprops = Ftext_properties_at (make_number (0), elt);
16493
16494 /* If the starting string's properties are not what
16495 we want, translate the string. Also, if the string
16496 is risky, do that anyway. */
16497
16498 if (NILP (Fequal (props, oprops)) || risky)
16499 {
16500 /* If the starting string has properties,
16501 merge the specified ones onto the existing ones. */
16502 if (! NILP (oprops) && !risky)
16503 {
16504 Lisp_Object tem;
16505
16506 oprops = Fcopy_sequence (oprops);
16507 tem = props;
16508 while (CONSP (tem))
16509 {
16510 oprops = Fplist_put (oprops, XCAR (tem),
16511 XCAR (XCDR (tem)));
16512 tem = XCDR (XCDR (tem));
16513 }
16514 props = oprops;
16515 }
16516
16517 aelt = Fassoc (elt, mode_line_proptrans_alist);
16518 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
16519 {
16520 /* AELT is what we want. Move it to the front
16521 without consing. */
16522 elt = XCAR (aelt);
16523 mode_line_proptrans_alist
16524 = move_elt_to_front (aelt, mode_line_proptrans_alist);
16525 }
16526 else
16527 {
16528 Lisp_Object tem;
16529
16530 /* If AELT has the wrong props, it is useless.
16531 so get rid of it. */
16532 if (! NILP (aelt))
16533 mode_line_proptrans_alist
16534 = Fdelq (aelt, mode_line_proptrans_alist);
16535
16536 elt = Fcopy_sequence (elt);
16537 Fset_text_properties (make_number (0), Flength (elt),
16538 props, elt);
16539 /* Add this item to mode_line_proptrans_alist. */
16540 mode_line_proptrans_alist
16541 = Fcons (Fcons (elt, props),
16542 mode_line_proptrans_alist);
16543 /* Truncate mode_line_proptrans_alist
16544 to at most 50 elements. */
16545 tem = Fnthcdr (make_number (50),
16546 mode_line_proptrans_alist);
16547 if (! NILP (tem))
16548 XSETCDR (tem, Qnil);
16549 }
16550 }
16551 }
16552
16553 offset = 0;
16554
16555 if (literal)
16556 {
16557 prec = precision - n;
16558 switch (mode_line_target)
16559 {
16560 case MODE_LINE_NOPROP:
16561 case MODE_LINE_TITLE:
16562 n += store_mode_line_noprop (SDATA (elt), -1, prec);
16563 break;
16564 case MODE_LINE_STRING:
16565 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
16566 break;
16567 case MODE_LINE_DISPLAY:
16568 n += display_string (NULL, elt, Qnil, 0, 0, it,
16569 0, prec, 0, STRING_MULTIBYTE (elt));
16570 break;
16571 }
16572
16573 break;
16574 }
16575
16576 /* Handle the non-literal case. */
16577
16578 while ((precision <= 0 || n < precision)
16579 && SREF (elt, offset) != 0
16580 && (mode_line_target != MODE_LINE_DISPLAY
16581 || it->current_x < it->last_visible_x))
16582 {
16583 int last_offset = offset;
16584
16585 /* Advance to end of string or next format specifier. */
16586 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
16587 ;
16588
16589 if (offset - 1 != last_offset)
16590 {
16591 int nchars, nbytes;
16592
16593 /* Output to end of string or up to '%'. Field width
16594 is length of string. Don't output more than
16595 PRECISION allows us. */
16596 offset--;
16597
16598 prec = c_string_width (SDATA (elt) + last_offset,
16599 offset - last_offset, precision - n,
16600 &nchars, &nbytes);
16601
16602 switch (mode_line_target)
16603 {
16604 case MODE_LINE_NOPROP:
16605 case MODE_LINE_TITLE:
16606 n += store_mode_line_noprop (SDATA (elt) + last_offset, 0, prec);
16607 break;
16608 case MODE_LINE_STRING:
16609 {
16610 int bytepos = last_offset;
16611 int charpos = string_byte_to_char (elt, bytepos);
16612 int endpos = (precision <= 0
16613 ? string_byte_to_char (elt, offset)
16614 : charpos + nchars);
16615
16616 n += store_mode_line_string (NULL,
16617 Fsubstring (elt, make_number (charpos),
16618 make_number (endpos)),
16619 0, 0, 0, Qnil);
16620 }
16621 break;
16622 case MODE_LINE_DISPLAY:
16623 {
16624 int bytepos = last_offset;
16625 int charpos = string_byte_to_char (elt, bytepos);
16626
16627 if (precision <= 0)
16628 nchars = string_byte_to_char (elt, offset) - charpos;
16629 n += display_string (NULL, elt, Qnil, 0, charpos,
16630 it, 0, nchars, 0,
16631 STRING_MULTIBYTE (elt));
16632 }
16633 break;
16634 }
16635 }
16636 else /* c == '%' */
16637 {
16638 int percent_position = offset;
16639
16640 /* Get the specified minimum width. Zero means
16641 don't pad. */
16642 field = 0;
16643 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
16644 field = field * 10 + c - '0';
16645
16646 /* Don't pad beyond the total padding allowed. */
16647 if (field_width - n > 0 && field > field_width - n)
16648 field = field_width - n;
16649
16650 /* Note that either PRECISION <= 0 or N < PRECISION. */
16651 prec = precision - n;
16652
16653 if (c == 'M')
16654 n += display_mode_element (it, depth, field, prec,
16655 Vglobal_mode_string, props,
16656 risky);
16657 else if (c != 0)
16658 {
16659 int multibyte;
16660 int bytepos, charpos;
16661 unsigned char *spec;
16662
16663 bytepos = percent_position;
16664 charpos = (STRING_MULTIBYTE (elt)
16665 ? string_byte_to_char (elt, bytepos)
16666 : bytepos);
16667
16668 spec
16669 = decode_mode_spec (it->w, c, field, prec, &multibyte);
16670
16671 switch (mode_line_target)
16672 {
16673 case MODE_LINE_NOPROP:
16674 case MODE_LINE_TITLE:
16675 n += store_mode_line_noprop (spec, field, prec);
16676 break;
16677 case MODE_LINE_STRING:
16678 {
16679 int len = strlen (spec);
16680 Lisp_Object tem = make_string (spec, len);
16681 props = Ftext_properties_at (make_number (charpos), elt);
16682 /* Should only keep face property in props */
16683 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
16684 }
16685 break;
16686 case MODE_LINE_DISPLAY:
16687 {
16688 int nglyphs_before, nwritten;
16689
16690 nglyphs_before = it->glyph_row->used[TEXT_AREA];
16691 nwritten = display_string (spec, Qnil, elt,
16692 charpos, 0, it,
16693 field, prec, 0,
16694 multibyte);
16695
16696 /* Assign to the glyphs written above the
16697 string where the `%x' came from, position
16698 of the `%'. */
16699 if (nwritten > 0)
16700 {
16701 struct glyph *glyph
16702 = (it->glyph_row->glyphs[TEXT_AREA]
16703 + nglyphs_before);
16704 int i;
16705
16706 for (i = 0; i < nwritten; ++i)
16707 {
16708 glyph[i].object = elt;
16709 glyph[i].charpos = charpos;
16710 }
16711
16712 n += nwritten;
16713 }
16714 }
16715 break;
16716 }
16717 }
16718 else /* c == 0 */
16719 break;
16720 }
16721 }
16722 }
16723 break;
16724
16725 case Lisp_Symbol:
16726 /* A symbol: process the value of the symbol recursively
16727 as if it appeared here directly. Avoid error if symbol void.
16728 Special case: if value of symbol is a string, output the string
16729 literally. */
16730 {
16731 register Lisp_Object tem;
16732
16733 /* If the variable is not marked as risky to set
16734 then its contents are risky to use. */
16735 if (NILP (Fget (elt, Qrisky_local_variable)))
16736 risky = 1;
16737
16738 tem = Fboundp (elt);
16739 if (!NILP (tem))
16740 {
16741 tem = Fsymbol_value (elt);
16742 /* If value is a string, output that string literally:
16743 don't check for % within it. */
16744 if (STRINGP (tem))
16745 literal = 1;
16746
16747 if (!EQ (tem, elt))
16748 {
16749 /* Give up right away for nil or t. */
16750 elt = tem;
16751 goto tail_recurse;
16752 }
16753 }
16754 }
16755 break;
16756
16757 case Lisp_Cons:
16758 {
16759 register Lisp_Object car, tem;
16760
16761 /* A cons cell: five distinct cases.
16762 If first element is :eval or :propertize, do something special.
16763 If first element is a string or a cons, process all the elements
16764 and effectively concatenate them.
16765 If first element is a negative number, truncate displaying cdr to
16766 at most that many characters. If positive, pad (with spaces)
16767 to at least that many characters.
16768 If first element is a symbol, process the cadr or caddr recursively
16769 according to whether the symbol's value is non-nil or nil. */
16770 car = XCAR (elt);
16771 if (EQ (car, QCeval))
16772 {
16773 /* An element of the form (:eval FORM) means evaluate FORM
16774 and use the result as mode line elements. */
16775
16776 if (risky)
16777 break;
16778
16779 if (CONSP (XCDR (elt)))
16780 {
16781 Lisp_Object spec;
16782 spec = safe_eval (XCAR (XCDR (elt)));
16783 n += display_mode_element (it, depth, field_width - n,
16784 precision - n, spec, props,
16785 risky);
16786 }
16787 }
16788 else if (EQ (car, QCpropertize))
16789 {
16790 /* An element of the form (:propertize ELT PROPS...)
16791 means display ELT but applying properties PROPS. */
16792
16793 if (risky)
16794 break;
16795
16796 if (CONSP (XCDR (elt)))
16797 n += display_mode_element (it, depth, field_width - n,
16798 precision - n, XCAR (XCDR (elt)),
16799 XCDR (XCDR (elt)), risky);
16800 }
16801 else if (SYMBOLP (car))
16802 {
16803 tem = Fboundp (car);
16804 elt = XCDR (elt);
16805 if (!CONSP (elt))
16806 goto invalid;
16807 /* elt is now the cdr, and we know it is a cons cell.
16808 Use its car if CAR has a non-nil value. */
16809 if (!NILP (tem))
16810 {
16811 tem = Fsymbol_value (car);
16812 if (!NILP (tem))
16813 {
16814 elt = XCAR (elt);
16815 goto tail_recurse;
16816 }
16817 }
16818 /* Symbol's value is nil (or symbol is unbound)
16819 Get the cddr of the original list
16820 and if possible find the caddr and use that. */
16821 elt = XCDR (elt);
16822 if (NILP (elt))
16823 break;
16824 else if (!CONSP (elt))
16825 goto invalid;
16826 elt = XCAR (elt);
16827 goto tail_recurse;
16828 }
16829 else if (INTEGERP (car))
16830 {
16831 register int lim = XINT (car);
16832 elt = XCDR (elt);
16833 if (lim < 0)
16834 {
16835 /* Negative int means reduce maximum width. */
16836 if (precision <= 0)
16837 precision = -lim;
16838 else
16839 precision = min (precision, -lim);
16840 }
16841 else if (lim > 0)
16842 {
16843 /* Padding specified. Don't let it be more than
16844 current maximum. */
16845 if (precision > 0)
16846 lim = min (precision, lim);
16847
16848 /* If that's more padding than already wanted, queue it.
16849 But don't reduce padding already specified even if
16850 that is beyond the current truncation point. */
16851 field_width = max (lim, field_width);
16852 }
16853 goto tail_recurse;
16854 }
16855 else if (STRINGP (car) || CONSP (car))
16856 {
16857 register int limit = 50;
16858 /* Limit is to protect against circular lists. */
16859 while (CONSP (elt)
16860 && --limit > 0
16861 && (precision <= 0 || n < precision))
16862 {
16863 n += display_mode_element (it, depth,
16864 /* Do padding only after the last
16865 element in the list. */
16866 (! CONSP (XCDR (elt))
16867 ? field_width - n
16868 : 0),
16869 precision - n, XCAR (elt),
16870 props, risky);
16871 elt = XCDR (elt);
16872 }
16873 }
16874 }
16875 break;
16876
16877 default:
16878 invalid:
16879 elt = build_string ("*invalid*");
16880 goto tail_recurse;
16881 }
16882
16883 /* Pad to FIELD_WIDTH. */
16884 if (field_width > 0 && n < field_width)
16885 {
16886 switch (mode_line_target)
16887 {
16888 case MODE_LINE_NOPROP:
16889 case MODE_LINE_TITLE:
16890 n += store_mode_line_noprop ("", field_width - n, 0);
16891 break;
16892 case MODE_LINE_STRING:
16893 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
16894 break;
16895 case MODE_LINE_DISPLAY:
16896 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
16897 0, 0, 0);
16898 break;
16899 }
16900 }
16901
16902 return n;
16903 }
16904
16905 /* Store a mode-line string element in mode_line_string_list.
16906
16907 If STRING is non-null, display that C string. Otherwise, the Lisp
16908 string LISP_STRING is displayed.
16909
16910 FIELD_WIDTH is the minimum number of output glyphs to produce.
16911 If STRING has fewer characters than FIELD_WIDTH, pad to the right
16912 with spaces. FIELD_WIDTH <= 0 means don't pad.
16913
16914 PRECISION is the maximum number of characters to output from
16915 STRING. PRECISION <= 0 means don't truncate the string.
16916
16917 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
16918 properties to the string.
16919
16920 PROPS are the properties to add to the string.
16921 The mode_line_string_face face property is always added to the string.
16922 */
16923
16924 static int
16925 store_mode_line_string (string, lisp_string, copy_string, field_width, precision, props)
16926 char *string;
16927 Lisp_Object lisp_string;
16928 int copy_string;
16929 int field_width;
16930 int precision;
16931 Lisp_Object props;
16932 {
16933 int len;
16934 int n = 0;
16935
16936 if (string != NULL)
16937 {
16938 len = strlen (string);
16939 if (precision > 0 && len > precision)
16940 len = precision;
16941 lisp_string = make_string (string, len);
16942 if (NILP (props))
16943 props = mode_line_string_face_prop;
16944 else if (!NILP (mode_line_string_face))
16945 {
16946 Lisp_Object face = Fplist_get (props, Qface);
16947 props = Fcopy_sequence (props);
16948 if (NILP (face))
16949 face = mode_line_string_face;
16950 else
16951 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
16952 props = Fplist_put (props, Qface, face);
16953 }
16954 Fadd_text_properties (make_number (0), make_number (len),
16955 props, lisp_string);
16956 }
16957 else
16958 {
16959 len = XFASTINT (Flength (lisp_string));
16960 if (precision > 0 && len > precision)
16961 {
16962 len = precision;
16963 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
16964 precision = -1;
16965 }
16966 if (!NILP (mode_line_string_face))
16967 {
16968 Lisp_Object face;
16969 if (NILP (props))
16970 props = Ftext_properties_at (make_number (0), lisp_string);
16971 face = Fplist_get (props, Qface);
16972 if (NILP (face))
16973 face = mode_line_string_face;
16974 else
16975 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
16976 props = Fcons (Qface, Fcons (face, Qnil));
16977 if (copy_string)
16978 lisp_string = Fcopy_sequence (lisp_string);
16979 }
16980 if (!NILP (props))
16981 Fadd_text_properties (make_number (0), make_number (len),
16982 props, lisp_string);
16983 }
16984
16985 if (len > 0)
16986 {
16987 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
16988 n += len;
16989 }
16990
16991 if (field_width > len)
16992 {
16993 field_width -= len;
16994 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
16995 if (!NILP (props))
16996 Fadd_text_properties (make_number (0), make_number (field_width),
16997 props, lisp_string);
16998 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
16999 n += field_width;
17000 }
17001
17002 return n;
17003 }
17004
17005
17006 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
17007 1, 4, 0,
17008 doc: /* Format a string out of a mode line format specification.
17009 First arg FORMAT specifies the mode line format (see `mode-line-format'
17010 for details) to use.
17011
17012 Optional second arg FACE specifies the face property to put
17013 on all characters for which no face is specified.
17014 t means whatever face the window's mode line currently uses
17015 \(either `mode-line' or `mode-line-inactive', depending).
17016 nil means the default is no face property.
17017 If FACE is an integer, the value string has no text properties.
17018
17019 Optional third and fourth args WINDOW and BUFFER specify the window
17020 and buffer to use as the context for the formatting (defaults
17021 are the selected window and the window's buffer). */)
17022 (format, face, window, buffer)
17023 Lisp_Object format, face, window, buffer;
17024 {
17025 struct it it;
17026 int len;
17027 struct window *w;
17028 struct buffer *old_buffer = NULL;
17029 int face_id = -1;
17030 int no_props = INTEGERP (face);
17031 int count = SPECPDL_INDEX ();
17032 Lisp_Object str;
17033 int string_start = 0;
17034
17035 if (NILP (window))
17036 window = selected_window;
17037 CHECK_WINDOW (window);
17038 w = XWINDOW (window);
17039
17040 if (NILP (buffer))
17041 buffer = w->buffer;
17042 CHECK_BUFFER (buffer);
17043
17044 if (NILP (format))
17045 return build_string ("");
17046
17047 if (no_props)
17048 face = Qnil;
17049
17050 if (!NILP (face))
17051 {
17052 if (EQ (face, Qt))
17053 face = (EQ (window, selected_window) ? Qmode_line : Qmode_line_inactive);
17054 face_id = lookup_named_face (XFRAME (WINDOW_FRAME (w)), face, 0, 0);
17055 }
17056
17057 if (face_id < 0)
17058 face_id = DEFAULT_FACE_ID;
17059
17060 if (XBUFFER (buffer) != current_buffer)
17061 old_buffer = current_buffer;
17062
17063 /* Save things including mode_line_proptrans_alist,
17064 and set that to nil so that we don't alter the outer value. */
17065 record_unwind_protect (unwind_format_mode_line,
17066 format_mode_line_unwind_data (old_buffer, 1));
17067 mode_line_proptrans_alist = Qnil;
17068
17069 if (old_buffer)
17070 set_buffer_internal_1 (XBUFFER (buffer));
17071
17072 init_iterator (&it, w, -1, -1, NULL, face_id);
17073
17074 if (no_props)
17075 {
17076 mode_line_target = MODE_LINE_NOPROP;
17077 mode_line_string_face_prop = Qnil;
17078 mode_line_string_list = Qnil;
17079 string_start = MODE_LINE_NOPROP_LEN (0);
17080 }
17081 else
17082 {
17083 mode_line_target = MODE_LINE_STRING;
17084 mode_line_string_list = Qnil;
17085 mode_line_string_face = face;
17086 mode_line_string_face_prop
17087 = (NILP (face) ? Qnil : Fcons (Qface, Fcons (face, Qnil)));
17088 }
17089
17090 push_frame_kboard (it.f);
17091 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
17092 pop_frame_kboard ();
17093
17094 if (no_props)
17095 {
17096 len = MODE_LINE_NOPROP_LEN (string_start);
17097 str = make_string (mode_line_noprop_buf + string_start, len);
17098 }
17099 else
17100 {
17101 mode_line_string_list = Fnreverse (mode_line_string_list);
17102 str = Fmapconcat (intern ("identity"), mode_line_string_list,
17103 make_string ("", 0));
17104 }
17105
17106 unbind_to (count, Qnil);
17107 return str;
17108 }
17109
17110 /* Write a null-terminated, right justified decimal representation of
17111 the positive integer D to BUF using a minimal field width WIDTH. */
17112
17113 static void
17114 pint2str (buf, width, d)
17115 register char *buf;
17116 register int width;
17117 register int d;
17118 {
17119 register char *p = buf;
17120
17121 if (d <= 0)
17122 *p++ = '0';
17123 else
17124 {
17125 while (d > 0)
17126 {
17127 *p++ = d % 10 + '0';
17128 d /= 10;
17129 }
17130 }
17131
17132 for (width -= (int) (p - buf); width > 0; --width)
17133 *p++ = ' ';
17134 *p-- = '\0';
17135 while (p > buf)
17136 {
17137 d = *buf;
17138 *buf++ = *p;
17139 *p-- = d;
17140 }
17141 }
17142
17143 /* Write a null-terminated, right justified decimal and "human
17144 readable" representation of the nonnegative integer D to BUF using
17145 a minimal field width WIDTH. D should be smaller than 999.5e24. */
17146
17147 static const char power_letter[] =
17148 {
17149 0, /* not used */
17150 'k', /* kilo */
17151 'M', /* mega */
17152 'G', /* giga */
17153 'T', /* tera */
17154 'P', /* peta */
17155 'E', /* exa */
17156 'Z', /* zetta */
17157 'Y' /* yotta */
17158 };
17159
17160 static void
17161 pint2hrstr (buf, width, d)
17162 char *buf;
17163 int width;
17164 int d;
17165 {
17166 /* We aim to represent the nonnegative integer D as
17167 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
17168 int quotient = d;
17169 int remainder = 0;
17170 /* -1 means: do not use TENTHS. */
17171 int tenths = -1;
17172 int exponent = 0;
17173
17174 /* Length of QUOTIENT.TENTHS as a string. */
17175 int length;
17176
17177 char * psuffix;
17178 char * p;
17179
17180 if (1000 <= quotient)
17181 {
17182 /* Scale to the appropriate EXPONENT. */
17183 do
17184 {
17185 remainder = quotient % 1000;
17186 quotient /= 1000;
17187 exponent++;
17188 }
17189 while (1000 <= quotient);
17190
17191 /* Round to nearest and decide whether to use TENTHS or not. */
17192 if (quotient <= 9)
17193 {
17194 tenths = remainder / 100;
17195 if (50 <= remainder % 100)
17196 {
17197 if (tenths < 9)
17198 tenths++;
17199 else
17200 {
17201 quotient++;
17202 if (quotient == 10)
17203 tenths = -1;
17204 else
17205 tenths = 0;
17206 }
17207 }
17208 }
17209 else
17210 if (500 <= remainder)
17211 {
17212 if (quotient < 999)
17213 quotient++;
17214 else
17215 {
17216 quotient = 1;
17217 exponent++;
17218 tenths = 0;
17219 }
17220 }
17221 }
17222
17223 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
17224 if (tenths == -1 && quotient <= 99)
17225 if (quotient <= 9)
17226 length = 1;
17227 else
17228 length = 2;
17229 else
17230 length = 3;
17231 p = psuffix = buf + max (width, length);
17232
17233 /* Print EXPONENT. */
17234 if (exponent)
17235 *psuffix++ = power_letter[exponent];
17236 *psuffix = '\0';
17237
17238 /* Print TENTHS. */
17239 if (tenths >= 0)
17240 {
17241 *--p = '0' + tenths;
17242 *--p = '.';
17243 }
17244
17245 /* Print QUOTIENT. */
17246 do
17247 {
17248 int digit = quotient % 10;
17249 *--p = '0' + digit;
17250 }
17251 while ((quotient /= 10) != 0);
17252
17253 /* Print leading spaces. */
17254 while (buf < p)
17255 *--p = ' ';
17256 }
17257
17258 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
17259 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
17260 type of CODING_SYSTEM. Return updated pointer into BUF. */
17261
17262 static unsigned char invalid_eol_type[] = "(*invalid*)";
17263
17264 static char *
17265 decode_mode_spec_coding (coding_system, buf, eol_flag)
17266 Lisp_Object coding_system;
17267 register char *buf;
17268 int eol_flag;
17269 {
17270 Lisp_Object val;
17271 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
17272 const unsigned char *eol_str;
17273 int eol_str_len;
17274 /* The EOL conversion we are using. */
17275 Lisp_Object eoltype;
17276
17277 val = Fget (coding_system, Qcoding_system);
17278 eoltype = Qnil;
17279
17280 if (!VECTORP (val)) /* Not yet decided. */
17281 {
17282 if (multibyte)
17283 *buf++ = '-';
17284 if (eol_flag)
17285 eoltype = eol_mnemonic_undecided;
17286 /* Don't mention EOL conversion if it isn't decided. */
17287 }
17288 else
17289 {
17290 Lisp_Object eolvalue;
17291
17292 eolvalue = Fget (coding_system, Qeol_type);
17293
17294 if (multibyte)
17295 *buf++ = XFASTINT (AREF (val, 1));
17296
17297 if (eol_flag)
17298 {
17299 /* The EOL conversion that is normal on this system. */
17300
17301 if (NILP (eolvalue)) /* Not yet decided. */
17302 eoltype = eol_mnemonic_undecided;
17303 else if (VECTORP (eolvalue)) /* Not yet decided. */
17304 eoltype = eol_mnemonic_undecided;
17305 else /* INTEGERP (eolvalue) -- 0:LF, 1:CRLF, 2:CR */
17306 eoltype = (XFASTINT (eolvalue) == 0
17307 ? eol_mnemonic_unix
17308 : (XFASTINT (eolvalue) == 1
17309 ? eol_mnemonic_dos : eol_mnemonic_mac));
17310 }
17311 }
17312
17313 if (eol_flag)
17314 {
17315 /* Mention the EOL conversion if it is not the usual one. */
17316 if (STRINGP (eoltype))
17317 {
17318 eol_str = SDATA (eoltype);
17319 eol_str_len = SBYTES (eoltype);
17320 }
17321 else if (INTEGERP (eoltype)
17322 && CHAR_VALID_P (XINT (eoltype), 0))
17323 {
17324 unsigned char *tmp = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
17325 eol_str_len = CHAR_STRING (XINT (eoltype), tmp);
17326 eol_str = tmp;
17327 }
17328 else
17329 {
17330 eol_str = invalid_eol_type;
17331 eol_str_len = sizeof (invalid_eol_type) - 1;
17332 }
17333 bcopy (eol_str, buf, eol_str_len);
17334 buf += eol_str_len;
17335 }
17336
17337 return buf;
17338 }
17339
17340 /* Return a string for the output of a mode line %-spec for window W,
17341 generated by character C. PRECISION >= 0 means don't return a
17342 string longer than that value. FIELD_WIDTH > 0 means pad the
17343 string returned with spaces to that value. Return 1 in *MULTIBYTE
17344 if the result is multibyte text.
17345
17346 Note we operate on the current buffer for most purposes,
17347 the exception being w->base_line_pos. */
17348
17349 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
17350
17351 static char *
17352 decode_mode_spec (w, c, field_width, precision, multibyte)
17353 struct window *w;
17354 register int c;
17355 int field_width, precision;
17356 int *multibyte;
17357 {
17358 Lisp_Object obj;
17359 struct frame *f = XFRAME (WINDOW_FRAME (w));
17360 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
17361 struct buffer *b = current_buffer;
17362
17363 obj = Qnil;
17364 *multibyte = 0;
17365
17366 switch (c)
17367 {
17368 case '*':
17369 if (!NILP (b->read_only))
17370 return "%";
17371 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
17372 return "*";
17373 return "-";
17374
17375 case '+':
17376 /* This differs from %* only for a modified read-only buffer. */
17377 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
17378 return "*";
17379 if (!NILP (b->read_only))
17380 return "%";
17381 return "-";
17382
17383 case '&':
17384 /* This differs from %* in ignoring read-only-ness. */
17385 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
17386 return "*";
17387 return "-";
17388
17389 case '%':
17390 return "%";
17391
17392 case '[':
17393 {
17394 int i;
17395 char *p;
17396
17397 if (command_loop_level > 5)
17398 return "[[[... ";
17399 p = decode_mode_spec_buf;
17400 for (i = 0; i < command_loop_level; i++)
17401 *p++ = '[';
17402 *p = 0;
17403 return decode_mode_spec_buf;
17404 }
17405
17406 case ']':
17407 {
17408 int i;
17409 char *p;
17410
17411 if (command_loop_level > 5)
17412 return " ...]]]";
17413 p = decode_mode_spec_buf;
17414 for (i = 0; i < command_loop_level; i++)
17415 *p++ = ']';
17416 *p = 0;
17417 return decode_mode_spec_buf;
17418 }
17419
17420 case '-':
17421 {
17422 register int i;
17423
17424 /* Let lots_of_dashes be a string of infinite length. */
17425 if (mode_line_target == MODE_LINE_NOPROP ||
17426 mode_line_target == MODE_LINE_STRING)
17427 return "--";
17428 if (field_width <= 0
17429 || field_width > sizeof (lots_of_dashes))
17430 {
17431 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
17432 decode_mode_spec_buf[i] = '-';
17433 decode_mode_spec_buf[i] = '\0';
17434 return decode_mode_spec_buf;
17435 }
17436 else
17437 return lots_of_dashes;
17438 }
17439
17440 case 'b':
17441 obj = b->name;
17442 break;
17443
17444 case 'c':
17445 {
17446 int col = (int) current_column (); /* iftc */
17447 w->column_number_displayed = make_number (col);
17448 pint2str (decode_mode_spec_buf, field_width, col);
17449 return decode_mode_spec_buf;
17450 }
17451
17452 case 'e':
17453 #ifndef SYSTEM_MALLOC
17454 {
17455 if (NILP (Vmemory_full))
17456 return "";
17457 else
17458 return "!MEM FULL! ";
17459 }
17460 #else
17461 return "";
17462 #endif
17463
17464 case 'F':
17465 /* %F displays the frame name. */
17466 if (!NILP (f->title))
17467 return (char *) SDATA (f->title);
17468 if (f->explicit_name || ! FRAME_WINDOW_P (f))
17469 return (char *) SDATA (f->name);
17470 return "Emacs";
17471
17472 case 'f':
17473 obj = b->filename;
17474 break;
17475
17476 case 'i':
17477 {
17478 int size = ZV - BEGV;
17479 pint2str (decode_mode_spec_buf, field_width, size);
17480 return decode_mode_spec_buf;
17481 }
17482
17483 case 'I':
17484 {
17485 int size = ZV - BEGV;
17486 pint2hrstr (decode_mode_spec_buf, field_width, size);
17487 return decode_mode_spec_buf;
17488 }
17489
17490 case 'l':
17491 {
17492 int startpos = XMARKER (w->start)->charpos;
17493 int startpos_byte = marker_byte_position (w->start);
17494 int line, linepos, linepos_byte, topline;
17495 int nlines, junk;
17496 int height = WINDOW_TOTAL_LINES (w);
17497
17498 /* If we decided that this buffer isn't suitable for line numbers,
17499 don't forget that too fast. */
17500 if (EQ (w->base_line_pos, w->buffer))
17501 goto no_value;
17502 /* But do forget it, if the window shows a different buffer now. */
17503 else if (BUFFERP (w->base_line_pos))
17504 w->base_line_pos = Qnil;
17505
17506 /* If the buffer is very big, don't waste time. */
17507 if (INTEGERP (Vline_number_display_limit)
17508 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
17509 {
17510 w->base_line_pos = Qnil;
17511 w->base_line_number = Qnil;
17512 goto no_value;
17513 }
17514
17515 if (!NILP (w->base_line_number)
17516 && !NILP (w->base_line_pos)
17517 && XFASTINT (w->base_line_pos) <= startpos)
17518 {
17519 line = XFASTINT (w->base_line_number);
17520 linepos = XFASTINT (w->base_line_pos);
17521 linepos_byte = buf_charpos_to_bytepos (b, linepos);
17522 }
17523 else
17524 {
17525 line = 1;
17526 linepos = BUF_BEGV (b);
17527 linepos_byte = BUF_BEGV_BYTE (b);
17528 }
17529
17530 /* Count lines from base line to window start position. */
17531 nlines = display_count_lines (linepos, linepos_byte,
17532 startpos_byte,
17533 startpos, &junk);
17534
17535 topline = nlines + line;
17536
17537 /* Determine a new base line, if the old one is too close
17538 or too far away, or if we did not have one.
17539 "Too close" means it's plausible a scroll-down would
17540 go back past it. */
17541 if (startpos == BUF_BEGV (b))
17542 {
17543 w->base_line_number = make_number (topline);
17544 w->base_line_pos = make_number (BUF_BEGV (b));
17545 }
17546 else if (nlines < height + 25 || nlines > height * 3 + 50
17547 || linepos == BUF_BEGV (b))
17548 {
17549 int limit = BUF_BEGV (b);
17550 int limit_byte = BUF_BEGV_BYTE (b);
17551 int position;
17552 int distance = (height * 2 + 30) * line_number_display_limit_width;
17553
17554 if (startpos - distance > limit)
17555 {
17556 limit = startpos - distance;
17557 limit_byte = CHAR_TO_BYTE (limit);
17558 }
17559
17560 nlines = display_count_lines (startpos, startpos_byte,
17561 limit_byte,
17562 - (height * 2 + 30),
17563 &position);
17564 /* If we couldn't find the lines we wanted within
17565 line_number_display_limit_width chars per line,
17566 give up on line numbers for this window. */
17567 if (position == limit_byte && limit == startpos - distance)
17568 {
17569 w->base_line_pos = w->buffer;
17570 w->base_line_number = Qnil;
17571 goto no_value;
17572 }
17573
17574 w->base_line_number = make_number (topline - nlines);
17575 w->base_line_pos = make_number (BYTE_TO_CHAR (position));
17576 }
17577
17578 /* Now count lines from the start pos to point. */
17579 nlines = display_count_lines (startpos, startpos_byte,
17580 PT_BYTE, PT, &junk);
17581
17582 /* Record that we did display the line number. */
17583 line_number_displayed = 1;
17584
17585 /* Make the string to show. */
17586 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
17587 return decode_mode_spec_buf;
17588 no_value:
17589 {
17590 char* p = decode_mode_spec_buf;
17591 int pad = field_width - 2;
17592 while (pad-- > 0)
17593 *p++ = ' ';
17594 *p++ = '?';
17595 *p++ = '?';
17596 *p = '\0';
17597 return decode_mode_spec_buf;
17598 }
17599 }
17600 break;
17601
17602 case 'm':
17603 obj = b->mode_name;
17604 break;
17605
17606 case 'n':
17607 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
17608 return " Narrow";
17609 break;
17610
17611 case 'p':
17612 {
17613 int pos = marker_position (w->start);
17614 int total = BUF_ZV (b) - BUF_BEGV (b);
17615
17616 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
17617 {
17618 if (pos <= BUF_BEGV (b))
17619 return "All";
17620 else
17621 return "Bottom";
17622 }
17623 else if (pos <= BUF_BEGV (b))
17624 return "Top";
17625 else
17626 {
17627 if (total > 1000000)
17628 /* Do it differently for a large value, to avoid overflow. */
17629 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
17630 else
17631 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
17632 /* We can't normally display a 3-digit number,
17633 so get us a 2-digit number that is close. */
17634 if (total == 100)
17635 total = 99;
17636 sprintf (decode_mode_spec_buf, "%2d%%", total);
17637 return decode_mode_spec_buf;
17638 }
17639 }
17640
17641 /* Display percentage of size above the bottom of the screen. */
17642 case 'P':
17643 {
17644 int toppos = marker_position (w->start);
17645 int botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
17646 int total = BUF_ZV (b) - BUF_BEGV (b);
17647
17648 if (botpos >= BUF_ZV (b))
17649 {
17650 if (toppos <= BUF_BEGV (b))
17651 return "All";
17652 else
17653 return "Bottom";
17654 }
17655 else
17656 {
17657 if (total > 1000000)
17658 /* Do it differently for a large value, to avoid overflow. */
17659 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
17660 else
17661 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
17662 /* We can't normally display a 3-digit number,
17663 so get us a 2-digit number that is close. */
17664 if (total == 100)
17665 total = 99;
17666 if (toppos <= BUF_BEGV (b))
17667 sprintf (decode_mode_spec_buf, "Top%2d%%", total);
17668 else
17669 sprintf (decode_mode_spec_buf, "%2d%%", total);
17670 return decode_mode_spec_buf;
17671 }
17672 }
17673
17674 case 's':
17675 /* status of process */
17676 obj = Fget_buffer_process (Fcurrent_buffer ());
17677 if (NILP (obj))
17678 return "no process";
17679 #ifdef subprocesses
17680 obj = Fsymbol_name (Fprocess_status (obj));
17681 #endif
17682 break;
17683
17684 case 't': /* indicate TEXT or BINARY */
17685 #ifdef MODE_LINE_BINARY_TEXT
17686 return MODE_LINE_BINARY_TEXT (b);
17687 #else
17688 return "T";
17689 #endif
17690
17691 case 'z':
17692 /* coding-system (not including end-of-line format) */
17693 case 'Z':
17694 /* coding-system (including end-of-line type) */
17695 {
17696 int eol_flag = (c == 'Z');
17697 char *p = decode_mode_spec_buf;
17698
17699 if (! FRAME_WINDOW_P (f))
17700 {
17701 /* No need to mention EOL here--the terminal never needs
17702 to do EOL conversion. */
17703 p = decode_mode_spec_coding (keyboard_coding.symbol, p, 0);
17704 p = decode_mode_spec_coding (terminal_coding.symbol, p, 0);
17705 }
17706 p = decode_mode_spec_coding (b->buffer_file_coding_system,
17707 p, eol_flag);
17708
17709 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
17710 #ifdef subprocesses
17711 obj = Fget_buffer_process (Fcurrent_buffer ());
17712 if (PROCESSP (obj))
17713 {
17714 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
17715 p, eol_flag);
17716 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
17717 p, eol_flag);
17718 }
17719 #endif /* subprocesses */
17720 #endif /* 0 */
17721 *p = 0;
17722 return decode_mode_spec_buf;
17723 }
17724 }
17725
17726 if (STRINGP (obj))
17727 {
17728 *multibyte = STRING_MULTIBYTE (obj);
17729 return (char *) SDATA (obj);
17730 }
17731 else
17732 return "";
17733 }
17734
17735
17736 /* Count up to COUNT lines starting from START / START_BYTE.
17737 But don't go beyond LIMIT_BYTE.
17738 Return the number of lines thus found (always nonnegative).
17739
17740 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
17741
17742 static int
17743 display_count_lines (start, start_byte, limit_byte, count, byte_pos_ptr)
17744 int start, start_byte, limit_byte, count;
17745 int *byte_pos_ptr;
17746 {
17747 register unsigned char *cursor;
17748 unsigned char *base;
17749
17750 register int ceiling;
17751 register unsigned char *ceiling_addr;
17752 int orig_count = count;
17753
17754 /* If we are not in selective display mode,
17755 check only for newlines. */
17756 int selective_display = (!NILP (current_buffer->selective_display)
17757 && !INTEGERP (current_buffer->selective_display));
17758
17759 if (count > 0)
17760 {
17761 while (start_byte < limit_byte)
17762 {
17763 ceiling = BUFFER_CEILING_OF (start_byte);
17764 ceiling = min (limit_byte - 1, ceiling);
17765 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
17766 base = (cursor = BYTE_POS_ADDR (start_byte));
17767 while (1)
17768 {
17769 if (selective_display)
17770 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
17771 ;
17772 else
17773 while (*cursor != '\n' && ++cursor != ceiling_addr)
17774 ;
17775
17776 if (cursor != ceiling_addr)
17777 {
17778 if (--count == 0)
17779 {
17780 start_byte += cursor - base + 1;
17781 *byte_pos_ptr = start_byte;
17782 return orig_count;
17783 }
17784 else
17785 if (++cursor == ceiling_addr)
17786 break;
17787 }
17788 else
17789 break;
17790 }
17791 start_byte += cursor - base;
17792 }
17793 }
17794 else
17795 {
17796 while (start_byte > limit_byte)
17797 {
17798 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
17799 ceiling = max (limit_byte, ceiling);
17800 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
17801 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
17802 while (1)
17803 {
17804 if (selective_display)
17805 while (--cursor != ceiling_addr
17806 && *cursor != '\n' && *cursor != 015)
17807 ;
17808 else
17809 while (--cursor != ceiling_addr && *cursor != '\n')
17810 ;
17811
17812 if (cursor != ceiling_addr)
17813 {
17814 if (++count == 0)
17815 {
17816 start_byte += cursor - base + 1;
17817 *byte_pos_ptr = start_byte;
17818 /* When scanning backwards, we should
17819 not count the newline posterior to which we stop. */
17820 return - orig_count - 1;
17821 }
17822 }
17823 else
17824 break;
17825 }
17826 /* Here we add 1 to compensate for the last decrement
17827 of CURSOR, which took it past the valid range. */
17828 start_byte += cursor - base + 1;
17829 }
17830 }
17831
17832 *byte_pos_ptr = limit_byte;
17833
17834 if (count < 0)
17835 return - orig_count + count;
17836 return orig_count - count;
17837
17838 }
17839
17840
17841 \f
17842 /***********************************************************************
17843 Displaying strings
17844 ***********************************************************************/
17845
17846 /* Display a NUL-terminated string, starting with index START.
17847
17848 If STRING is non-null, display that C string. Otherwise, the Lisp
17849 string LISP_STRING is displayed.
17850
17851 If FACE_STRING is not nil, FACE_STRING_POS is a position in
17852 FACE_STRING. Display STRING or LISP_STRING with the face at
17853 FACE_STRING_POS in FACE_STRING:
17854
17855 Display the string in the environment given by IT, but use the
17856 standard display table, temporarily.
17857
17858 FIELD_WIDTH is the minimum number of output glyphs to produce.
17859 If STRING has fewer characters than FIELD_WIDTH, pad to the right
17860 with spaces. If STRING has more characters, more than FIELD_WIDTH
17861 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
17862
17863 PRECISION is the maximum number of characters to output from
17864 STRING. PRECISION < 0 means don't truncate the string.
17865
17866 This is roughly equivalent to printf format specifiers:
17867
17868 FIELD_WIDTH PRECISION PRINTF
17869 ----------------------------------------
17870 -1 -1 %s
17871 -1 10 %.10s
17872 10 -1 %10s
17873 20 10 %20.10s
17874
17875 MULTIBYTE zero means do not display multibyte chars, > 0 means do
17876 display them, and < 0 means obey the current buffer's value of
17877 enable_multibyte_characters.
17878
17879 Value is the number of columns displayed. */
17880
17881 static int
17882 display_string (string, lisp_string, face_string, face_string_pos,
17883 start, it, field_width, precision, max_x, multibyte)
17884 unsigned char *string;
17885 Lisp_Object lisp_string;
17886 Lisp_Object face_string;
17887 int face_string_pos;
17888 int start;
17889 struct it *it;
17890 int field_width, precision, max_x;
17891 int multibyte;
17892 {
17893 int hpos_at_start = it->hpos;
17894 int saved_face_id = it->face_id;
17895 struct glyph_row *row = it->glyph_row;
17896
17897 /* Initialize the iterator IT for iteration over STRING beginning
17898 with index START. */
17899 reseat_to_string (it, string, lisp_string, start,
17900 precision, field_width, multibyte);
17901
17902 /* If displaying STRING, set up the face of the iterator
17903 from LISP_STRING, if that's given. */
17904 if (STRINGP (face_string))
17905 {
17906 int endptr;
17907 struct face *face;
17908
17909 it->face_id
17910 = face_at_string_position (it->w, face_string, face_string_pos,
17911 0, it->region_beg_charpos,
17912 it->region_end_charpos,
17913 &endptr, it->base_face_id, 0);
17914 face = FACE_FROM_ID (it->f, it->face_id);
17915 it->face_box_p = face->box != FACE_NO_BOX;
17916 }
17917
17918 /* Set max_x to the maximum allowed X position. Don't let it go
17919 beyond the right edge of the window. */
17920 if (max_x <= 0)
17921 max_x = it->last_visible_x;
17922 else
17923 max_x = min (max_x, it->last_visible_x);
17924
17925 /* Skip over display elements that are not visible. because IT->w is
17926 hscrolled. */
17927 if (it->current_x < it->first_visible_x)
17928 move_it_in_display_line_to (it, 100000, it->first_visible_x,
17929 MOVE_TO_POS | MOVE_TO_X);
17930
17931 row->ascent = it->max_ascent;
17932 row->height = it->max_ascent + it->max_descent;
17933 row->phys_ascent = it->max_phys_ascent;
17934 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
17935 row->extra_line_spacing = it->max_extra_line_spacing;
17936
17937 /* This condition is for the case that we are called with current_x
17938 past last_visible_x. */
17939 while (it->current_x < max_x)
17940 {
17941 int x_before, x, n_glyphs_before, i, nglyphs;
17942
17943 /* Get the next display element. */
17944 if (!get_next_display_element (it))
17945 break;
17946
17947 /* Produce glyphs. */
17948 x_before = it->current_x;
17949 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
17950 PRODUCE_GLYPHS (it);
17951
17952 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
17953 i = 0;
17954 x = x_before;
17955 while (i < nglyphs)
17956 {
17957 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
17958
17959 if (!it->truncate_lines_p
17960 && x + glyph->pixel_width > max_x)
17961 {
17962 /* End of continued line or max_x reached. */
17963 if (CHAR_GLYPH_PADDING_P (*glyph))
17964 {
17965 /* A wide character is unbreakable. */
17966 it->glyph_row->used[TEXT_AREA] = n_glyphs_before;
17967 it->current_x = x_before;
17968 }
17969 else
17970 {
17971 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
17972 it->current_x = x;
17973 }
17974 break;
17975 }
17976 else if (x + glyph->pixel_width > it->first_visible_x)
17977 {
17978 /* Glyph is at least partially visible. */
17979 ++it->hpos;
17980 if (x < it->first_visible_x)
17981 it->glyph_row->x = x - it->first_visible_x;
17982 }
17983 else
17984 {
17985 /* Glyph is off the left margin of the display area.
17986 Should not happen. */
17987 abort ();
17988 }
17989
17990 row->ascent = max (row->ascent, it->max_ascent);
17991 row->height = max (row->height, it->max_ascent + it->max_descent);
17992 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
17993 row->phys_height = max (row->phys_height,
17994 it->max_phys_ascent + it->max_phys_descent);
17995 row->extra_line_spacing = max (row->extra_line_spacing,
17996 it->max_extra_line_spacing);
17997 x += glyph->pixel_width;
17998 ++i;
17999 }
18000
18001 /* Stop if max_x reached. */
18002 if (i < nglyphs)
18003 break;
18004
18005 /* Stop at line ends. */
18006 if (ITERATOR_AT_END_OF_LINE_P (it))
18007 {
18008 it->continuation_lines_width = 0;
18009 break;
18010 }
18011
18012 set_iterator_to_next (it, 1);
18013
18014 /* Stop if truncating at the right edge. */
18015 if (it->truncate_lines_p
18016 && it->current_x >= it->last_visible_x)
18017 {
18018 /* Add truncation mark, but don't do it if the line is
18019 truncated at a padding space. */
18020 if (IT_CHARPOS (*it) < it->string_nchars)
18021 {
18022 if (!FRAME_WINDOW_P (it->f))
18023 {
18024 int i, n;
18025
18026 if (it->current_x > it->last_visible_x)
18027 {
18028 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
18029 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
18030 break;
18031 for (n = row->used[TEXT_AREA]; i < n; ++i)
18032 {
18033 row->used[TEXT_AREA] = i;
18034 produce_special_glyphs (it, IT_TRUNCATION);
18035 }
18036 }
18037 produce_special_glyphs (it, IT_TRUNCATION);
18038 }
18039 it->glyph_row->truncated_on_right_p = 1;
18040 }
18041 break;
18042 }
18043 }
18044
18045 /* Maybe insert a truncation at the left. */
18046 if (it->first_visible_x
18047 && IT_CHARPOS (*it) > 0)
18048 {
18049 if (!FRAME_WINDOW_P (it->f))
18050 insert_left_trunc_glyphs (it);
18051 it->glyph_row->truncated_on_left_p = 1;
18052 }
18053
18054 it->face_id = saved_face_id;
18055
18056 /* Value is number of columns displayed. */
18057 return it->hpos - hpos_at_start;
18058 }
18059
18060
18061 \f
18062 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
18063 appears as an element of LIST or as the car of an element of LIST.
18064 If PROPVAL is a list, compare each element against LIST in that
18065 way, and return 1/2 if any element of PROPVAL is found in LIST.
18066 Otherwise return 0. This function cannot quit.
18067 The return value is 2 if the text is invisible but with an ellipsis
18068 and 1 if it's invisible and without an ellipsis. */
18069
18070 int
18071 invisible_p (propval, list)
18072 register Lisp_Object propval;
18073 Lisp_Object list;
18074 {
18075 register Lisp_Object tail, proptail;
18076
18077 for (tail = list; CONSP (tail); tail = XCDR (tail))
18078 {
18079 register Lisp_Object tem;
18080 tem = XCAR (tail);
18081 if (EQ (propval, tem))
18082 return 1;
18083 if (CONSP (tem) && EQ (propval, XCAR (tem)))
18084 return NILP (XCDR (tem)) ? 1 : 2;
18085 }
18086
18087 if (CONSP (propval))
18088 {
18089 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
18090 {
18091 Lisp_Object propelt;
18092 propelt = XCAR (proptail);
18093 for (tail = list; CONSP (tail); tail = XCDR (tail))
18094 {
18095 register Lisp_Object tem;
18096 tem = XCAR (tail);
18097 if (EQ (propelt, tem))
18098 return 1;
18099 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
18100 return NILP (XCDR (tem)) ? 1 : 2;
18101 }
18102 }
18103 }
18104
18105 return 0;
18106 }
18107
18108 /* Calculate a width or height in pixels from a specification using
18109 the following elements:
18110
18111 SPEC ::=
18112 NUM - a (fractional) multiple of the default font width/height
18113 (NUM) - specifies exactly NUM pixels
18114 UNIT - a fixed number of pixels, see below.
18115 ELEMENT - size of a display element in pixels, see below.
18116 (NUM . SPEC) - equals NUM * SPEC
18117 (+ SPEC SPEC ...) - add pixel values
18118 (- SPEC SPEC ...) - subtract pixel values
18119 (- SPEC) - negate pixel value
18120
18121 NUM ::=
18122 INT or FLOAT - a number constant
18123 SYMBOL - use symbol's (buffer local) variable binding.
18124
18125 UNIT ::=
18126 in - pixels per inch *)
18127 mm - pixels per 1/1000 meter *)
18128 cm - pixels per 1/100 meter *)
18129 width - width of current font in pixels.
18130 height - height of current font in pixels.
18131
18132 *) using the ratio(s) defined in display-pixels-per-inch.
18133
18134 ELEMENT ::=
18135
18136 left-fringe - left fringe width in pixels
18137 right-fringe - right fringe width in pixels
18138
18139 left-margin - left margin width in pixels
18140 right-margin - right margin width in pixels
18141
18142 scroll-bar - scroll-bar area width in pixels
18143
18144 Examples:
18145
18146 Pixels corresponding to 5 inches:
18147 (5 . in)
18148
18149 Total width of non-text areas on left side of window (if scroll-bar is on left):
18150 '(space :width (+ left-fringe left-margin scroll-bar))
18151
18152 Align to first text column (in header line):
18153 '(space :align-to 0)
18154
18155 Align to middle of text area minus half the width of variable `my-image'
18156 containing a loaded image:
18157 '(space :align-to (0.5 . (- text my-image)))
18158
18159 Width of left margin minus width of 1 character in the default font:
18160 '(space :width (- left-margin 1))
18161
18162 Width of left margin minus width of 2 characters in the current font:
18163 '(space :width (- left-margin (2 . width)))
18164
18165 Center 1 character over left-margin (in header line):
18166 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
18167
18168 Different ways to express width of left fringe plus left margin minus one pixel:
18169 '(space :width (- (+ left-fringe left-margin) (1)))
18170 '(space :width (+ left-fringe left-margin (- (1))))
18171 '(space :width (+ left-fringe left-margin (-1)))
18172
18173 */
18174
18175 #define NUMVAL(X) \
18176 ((INTEGERP (X) || FLOATP (X)) \
18177 ? XFLOATINT (X) \
18178 : - 1)
18179
18180 int
18181 calc_pixel_width_or_height (res, it, prop, font, width_p, align_to)
18182 double *res;
18183 struct it *it;
18184 Lisp_Object prop;
18185 void *font;
18186 int width_p, *align_to;
18187 {
18188 double pixels;
18189
18190 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
18191 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
18192
18193 if (NILP (prop))
18194 return OK_PIXELS (0);
18195
18196 if (SYMBOLP (prop))
18197 {
18198 if (SCHARS (SYMBOL_NAME (prop)) == 2)
18199 {
18200 char *unit = SDATA (SYMBOL_NAME (prop));
18201
18202 if (unit[0] == 'i' && unit[1] == 'n')
18203 pixels = 1.0;
18204 else if (unit[0] == 'm' && unit[1] == 'm')
18205 pixels = 25.4;
18206 else if (unit[0] == 'c' && unit[1] == 'm')
18207 pixels = 2.54;
18208 else
18209 pixels = 0;
18210 if (pixels > 0)
18211 {
18212 double ppi;
18213 #ifdef HAVE_WINDOW_SYSTEM
18214 if (FRAME_WINDOW_P (it->f)
18215 && (ppi = (width_p
18216 ? FRAME_X_DISPLAY_INFO (it->f)->resx
18217 : FRAME_X_DISPLAY_INFO (it->f)->resy),
18218 ppi > 0))
18219 return OK_PIXELS (ppi / pixels);
18220 #endif
18221
18222 if ((ppi = NUMVAL (Vdisplay_pixels_per_inch), ppi > 0)
18223 || (CONSP (Vdisplay_pixels_per_inch)
18224 && (ppi = (width_p
18225 ? NUMVAL (XCAR (Vdisplay_pixels_per_inch))
18226 : NUMVAL (XCDR (Vdisplay_pixels_per_inch))),
18227 ppi > 0)))
18228 return OK_PIXELS (ppi / pixels);
18229
18230 return 0;
18231 }
18232 }
18233
18234 #ifdef HAVE_WINDOW_SYSTEM
18235 if (EQ (prop, Qheight))
18236 return OK_PIXELS (font ? FONT_HEIGHT ((XFontStruct *)font) : FRAME_LINE_HEIGHT (it->f));
18237 if (EQ (prop, Qwidth))
18238 return OK_PIXELS (font ? FONT_WIDTH ((XFontStruct *)font) : FRAME_COLUMN_WIDTH (it->f));
18239 #else
18240 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
18241 return OK_PIXELS (1);
18242 #endif
18243
18244 if (EQ (prop, Qtext))
18245 return OK_PIXELS (width_p
18246 ? window_box_width (it->w, TEXT_AREA)
18247 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
18248
18249 if (align_to && *align_to < 0)
18250 {
18251 *res = 0;
18252 if (EQ (prop, Qleft))
18253 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
18254 if (EQ (prop, Qright))
18255 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
18256 if (EQ (prop, Qcenter))
18257 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
18258 + window_box_width (it->w, TEXT_AREA) / 2);
18259 if (EQ (prop, Qleft_fringe))
18260 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
18261 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
18262 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
18263 if (EQ (prop, Qright_fringe))
18264 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
18265 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
18266 : window_box_right_offset (it->w, TEXT_AREA));
18267 if (EQ (prop, Qleft_margin))
18268 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
18269 if (EQ (prop, Qright_margin))
18270 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
18271 if (EQ (prop, Qscroll_bar))
18272 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
18273 ? 0
18274 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
18275 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
18276 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
18277 : 0)));
18278 }
18279 else
18280 {
18281 if (EQ (prop, Qleft_fringe))
18282 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
18283 if (EQ (prop, Qright_fringe))
18284 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
18285 if (EQ (prop, Qleft_margin))
18286 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
18287 if (EQ (prop, Qright_margin))
18288 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
18289 if (EQ (prop, Qscroll_bar))
18290 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
18291 }
18292
18293 prop = Fbuffer_local_value (prop, it->w->buffer);
18294 }
18295
18296 if (INTEGERP (prop) || FLOATP (prop))
18297 {
18298 int base_unit = (width_p
18299 ? FRAME_COLUMN_WIDTH (it->f)
18300 : FRAME_LINE_HEIGHT (it->f));
18301 return OK_PIXELS (XFLOATINT (prop) * base_unit);
18302 }
18303
18304 if (CONSP (prop))
18305 {
18306 Lisp_Object car = XCAR (prop);
18307 Lisp_Object cdr = XCDR (prop);
18308
18309 if (SYMBOLP (car))
18310 {
18311 #ifdef HAVE_WINDOW_SYSTEM
18312 if (valid_image_p (prop))
18313 {
18314 int id = lookup_image (it->f, prop);
18315 struct image *img = IMAGE_FROM_ID (it->f, id);
18316
18317 return OK_PIXELS (width_p ? img->width : img->height);
18318 }
18319 #endif
18320 if (EQ (car, Qplus) || EQ (car, Qminus))
18321 {
18322 int first = 1;
18323 double px;
18324
18325 pixels = 0;
18326 while (CONSP (cdr))
18327 {
18328 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
18329 font, width_p, align_to))
18330 return 0;
18331 if (first)
18332 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
18333 else
18334 pixels += px;
18335 cdr = XCDR (cdr);
18336 }
18337 if (EQ (car, Qminus))
18338 pixels = -pixels;
18339 return OK_PIXELS (pixels);
18340 }
18341
18342 car = Fbuffer_local_value (car, it->w->buffer);
18343 }
18344
18345 if (INTEGERP (car) || FLOATP (car))
18346 {
18347 double fact;
18348 pixels = XFLOATINT (car);
18349 if (NILP (cdr))
18350 return OK_PIXELS (pixels);
18351 if (calc_pixel_width_or_height (&fact, it, cdr,
18352 font, width_p, align_to))
18353 return OK_PIXELS (pixels * fact);
18354 return 0;
18355 }
18356
18357 return 0;
18358 }
18359
18360 return 0;
18361 }
18362
18363 \f
18364 /***********************************************************************
18365 Glyph Display
18366 ***********************************************************************/
18367
18368 #ifdef HAVE_WINDOW_SYSTEM
18369
18370 #if GLYPH_DEBUG
18371
18372 void
18373 dump_glyph_string (s)
18374 struct glyph_string *s;
18375 {
18376 fprintf (stderr, "glyph string\n");
18377 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
18378 s->x, s->y, s->width, s->height);
18379 fprintf (stderr, " ybase = %d\n", s->ybase);
18380 fprintf (stderr, " hl = %d\n", s->hl);
18381 fprintf (stderr, " left overhang = %d, right = %d\n",
18382 s->left_overhang, s->right_overhang);
18383 fprintf (stderr, " nchars = %d\n", s->nchars);
18384 fprintf (stderr, " extends to end of line = %d\n",
18385 s->extends_to_end_of_line_p);
18386 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
18387 fprintf (stderr, " bg width = %d\n", s->background_width);
18388 }
18389
18390 #endif /* GLYPH_DEBUG */
18391
18392 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
18393 of XChar2b structures for S; it can't be allocated in
18394 init_glyph_string because it must be allocated via `alloca'. W
18395 is the window on which S is drawn. ROW and AREA are the glyph row
18396 and area within the row from which S is constructed. START is the
18397 index of the first glyph structure covered by S. HL is a
18398 face-override for drawing S. */
18399
18400 #ifdef HAVE_NTGUI
18401 #define OPTIONAL_HDC(hdc) hdc,
18402 #define DECLARE_HDC(hdc) HDC hdc;
18403 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
18404 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
18405 #endif
18406
18407 #ifndef OPTIONAL_HDC
18408 #define OPTIONAL_HDC(hdc)
18409 #define DECLARE_HDC(hdc)
18410 #define ALLOCATE_HDC(hdc, f)
18411 #define RELEASE_HDC(hdc, f)
18412 #endif
18413
18414 static void
18415 init_glyph_string (s, OPTIONAL_HDC (hdc) char2b, w, row, area, start, hl)
18416 struct glyph_string *s;
18417 DECLARE_HDC (hdc)
18418 XChar2b *char2b;
18419 struct window *w;
18420 struct glyph_row *row;
18421 enum glyph_row_area area;
18422 int start;
18423 enum draw_glyphs_face hl;
18424 {
18425 bzero (s, sizeof *s);
18426 s->w = w;
18427 s->f = XFRAME (w->frame);
18428 #ifdef HAVE_NTGUI
18429 s->hdc = hdc;
18430 #endif
18431 s->display = FRAME_X_DISPLAY (s->f);
18432 s->window = FRAME_X_WINDOW (s->f);
18433 s->char2b = char2b;
18434 s->hl = hl;
18435 s->row = row;
18436 s->area = area;
18437 s->first_glyph = row->glyphs[area] + start;
18438 s->height = row->height;
18439 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
18440
18441 /* Display the internal border below the tool-bar window. */
18442 if (WINDOWP (s->f->tool_bar_window)
18443 && s->w == XWINDOW (s->f->tool_bar_window))
18444 s->y -= FRAME_INTERNAL_BORDER_WIDTH (s->f);
18445
18446 s->ybase = s->y + row->ascent;
18447 }
18448
18449
18450 /* Append the list of glyph strings with head H and tail T to the list
18451 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
18452
18453 static INLINE void
18454 append_glyph_string_lists (head, tail, h, t)
18455 struct glyph_string **head, **tail;
18456 struct glyph_string *h, *t;
18457 {
18458 if (h)
18459 {
18460 if (*head)
18461 (*tail)->next = h;
18462 else
18463 *head = h;
18464 h->prev = *tail;
18465 *tail = t;
18466 }
18467 }
18468
18469
18470 /* Prepend the list of glyph strings with head H and tail T to the
18471 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
18472 result. */
18473
18474 static INLINE void
18475 prepend_glyph_string_lists (head, tail, h, t)
18476 struct glyph_string **head, **tail;
18477 struct glyph_string *h, *t;
18478 {
18479 if (h)
18480 {
18481 if (*head)
18482 (*head)->prev = t;
18483 else
18484 *tail = t;
18485 t->next = *head;
18486 *head = h;
18487 }
18488 }
18489
18490
18491 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
18492 Set *HEAD and *TAIL to the resulting list. */
18493
18494 static INLINE void
18495 append_glyph_string (head, tail, s)
18496 struct glyph_string **head, **tail;
18497 struct glyph_string *s;
18498 {
18499 s->next = s->prev = NULL;
18500 append_glyph_string_lists (head, tail, s, s);
18501 }
18502
18503
18504 /* Get face and two-byte form of character glyph GLYPH on frame F.
18505 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
18506 a pointer to a realized face that is ready for display. */
18507
18508 static INLINE struct face *
18509 get_glyph_face_and_encoding (f, glyph, char2b, two_byte_p)
18510 struct frame *f;
18511 struct glyph *glyph;
18512 XChar2b *char2b;
18513 int *two_byte_p;
18514 {
18515 struct face *face;
18516
18517 xassert (glyph->type == CHAR_GLYPH);
18518 face = FACE_FROM_ID (f, glyph->face_id);
18519
18520 if (two_byte_p)
18521 *two_byte_p = 0;
18522
18523 if (!glyph->multibyte_p)
18524 {
18525 /* Unibyte case. We don't have to encode, but we have to make
18526 sure to use a face suitable for unibyte. */
18527 STORE_XCHAR2B (char2b, 0, glyph->u.ch);
18528 }
18529 else if (glyph->u.ch < 128)
18530 {
18531 /* Case of ASCII in a face known to fit ASCII. */
18532 STORE_XCHAR2B (char2b, 0, glyph->u.ch);
18533 }
18534 else
18535 {
18536 int c1, c2, charset;
18537
18538 /* Split characters into bytes. If c2 is -1 afterwards, C is
18539 really a one-byte character so that byte1 is zero. */
18540 SPLIT_CHAR (glyph->u.ch, charset, c1, c2);
18541 if (c2 > 0)
18542 STORE_XCHAR2B (char2b, c1, c2);
18543 else
18544 STORE_XCHAR2B (char2b, 0, c1);
18545
18546 /* Maybe encode the character in *CHAR2B. */
18547 if (charset != CHARSET_ASCII)
18548 {
18549 struct font_info *font_info
18550 = FONT_INFO_FROM_ID (f, face->font_info_id);
18551 if (font_info)
18552 glyph->font_type
18553 = rif->encode_char (glyph->u.ch, char2b, font_info, two_byte_p);
18554 }
18555 }
18556
18557 /* Make sure X resources of the face are allocated. */
18558 xassert (face != NULL);
18559 PREPARE_FACE_FOR_DISPLAY (f, face);
18560 return face;
18561 }
18562
18563
18564 /* Fill glyph string S with composition components specified by S->cmp.
18565
18566 FACES is an array of faces for all components of this composition.
18567 S->gidx is the index of the first component for S.
18568
18569 OVERLAPS non-zero means S should draw the foreground only, and use
18570 its physical height for clipping. See also draw_glyphs.
18571
18572 Value is the index of a component not in S. */
18573
18574 static int
18575 fill_composite_glyph_string (s, faces, overlaps)
18576 struct glyph_string *s;
18577 struct face **faces;
18578 int overlaps;
18579 {
18580 int i;
18581
18582 xassert (s);
18583
18584 s->for_overlaps = overlaps;
18585
18586 s->face = faces[s->gidx];
18587 s->font = s->face->font;
18588 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
18589
18590 /* For all glyphs of this composition, starting at the offset
18591 S->gidx, until we reach the end of the definition or encounter a
18592 glyph that requires the different face, add it to S. */
18593 ++s->nchars;
18594 for (i = s->gidx + 1; i < s->cmp->glyph_len && faces[i] == s->face; ++i)
18595 ++s->nchars;
18596
18597 /* All glyph strings for the same composition has the same width,
18598 i.e. the width set for the first component of the composition. */
18599
18600 s->width = s->first_glyph->pixel_width;
18601
18602 /* If the specified font could not be loaded, use the frame's
18603 default font, but record the fact that we couldn't load it in
18604 the glyph string so that we can draw rectangles for the
18605 characters of the glyph string. */
18606 if (s->font == NULL)
18607 {
18608 s->font_not_found_p = 1;
18609 s->font = FRAME_FONT (s->f);
18610 }
18611
18612 /* Adjust base line for subscript/superscript text. */
18613 s->ybase += s->first_glyph->voffset;
18614
18615 xassert (s->face && s->face->gc);
18616
18617 /* This glyph string must always be drawn with 16-bit functions. */
18618 s->two_byte_p = 1;
18619
18620 return s->gidx + s->nchars;
18621 }
18622
18623
18624 /* Fill glyph string S from a sequence of character glyphs.
18625
18626 FACE_ID is the face id of the string. START is the index of the
18627 first glyph to consider, END is the index of the last + 1.
18628 OVERLAPS non-zero means S should draw the foreground only, and use
18629 its physical height for clipping. See also draw_glyphs.
18630
18631 Value is the index of the first glyph not in S. */
18632
18633 static int
18634 fill_glyph_string (s, face_id, start, end, overlaps)
18635 struct glyph_string *s;
18636 int face_id;
18637 int start, end, overlaps;
18638 {
18639 struct glyph *glyph, *last;
18640 int voffset;
18641 int glyph_not_available_p;
18642
18643 xassert (s->f == XFRAME (s->w->frame));
18644 xassert (s->nchars == 0);
18645 xassert (start >= 0 && end > start);
18646
18647 s->for_overlaps = overlaps,
18648 glyph = s->row->glyphs[s->area] + start;
18649 last = s->row->glyphs[s->area] + end;
18650 voffset = glyph->voffset;
18651
18652 glyph_not_available_p = glyph->glyph_not_available_p;
18653
18654 while (glyph < last
18655 && glyph->type == CHAR_GLYPH
18656 && glyph->voffset == voffset
18657 /* Same face id implies same font, nowadays. */
18658 && glyph->face_id == face_id
18659 && glyph->glyph_not_available_p == glyph_not_available_p)
18660 {
18661 int two_byte_p;
18662
18663 s->face = get_glyph_face_and_encoding (s->f, glyph,
18664 s->char2b + s->nchars,
18665 &two_byte_p);
18666 s->two_byte_p = two_byte_p;
18667 ++s->nchars;
18668 xassert (s->nchars <= end - start);
18669 s->width += glyph->pixel_width;
18670 ++glyph;
18671 }
18672
18673 s->font = s->face->font;
18674 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
18675
18676 /* If the specified font could not be loaded, use the frame's font,
18677 but record the fact that we couldn't load it in
18678 S->font_not_found_p so that we can draw rectangles for the
18679 characters of the glyph string. */
18680 if (s->font == NULL || glyph_not_available_p)
18681 {
18682 s->font_not_found_p = 1;
18683 s->font = FRAME_FONT (s->f);
18684 }
18685
18686 /* Adjust base line for subscript/superscript text. */
18687 s->ybase += voffset;
18688
18689 xassert (s->face && s->face->gc);
18690 return glyph - s->row->glyphs[s->area];
18691 }
18692
18693
18694 /* Fill glyph string S from image glyph S->first_glyph. */
18695
18696 static void
18697 fill_image_glyph_string (s)
18698 struct glyph_string *s;
18699 {
18700 xassert (s->first_glyph->type == IMAGE_GLYPH);
18701 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
18702 xassert (s->img);
18703 s->slice = s->first_glyph->slice;
18704 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
18705 s->font = s->face->font;
18706 s->width = s->first_glyph->pixel_width;
18707
18708 /* Adjust base line for subscript/superscript text. */
18709 s->ybase += s->first_glyph->voffset;
18710 }
18711
18712
18713 /* Fill glyph string S from a sequence of stretch glyphs.
18714
18715 ROW is the glyph row in which the glyphs are found, AREA is the
18716 area within the row. START is the index of the first glyph to
18717 consider, END is the index of the last + 1.
18718
18719 Value is the index of the first glyph not in S. */
18720
18721 static int
18722 fill_stretch_glyph_string (s, row, area, start, end)
18723 struct glyph_string *s;
18724 struct glyph_row *row;
18725 enum glyph_row_area area;
18726 int start, end;
18727 {
18728 struct glyph *glyph, *last;
18729 int voffset, face_id;
18730
18731 xassert (s->first_glyph->type == STRETCH_GLYPH);
18732
18733 glyph = s->row->glyphs[s->area] + start;
18734 last = s->row->glyphs[s->area] + end;
18735 face_id = glyph->face_id;
18736 s->face = FACE_FROM_ID (s->f, face_id);
18737 s->font = s->face->font;
18738 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
18739 s->width = glyph->pixel_width;
18740 s->nchars = 1;
18741 voffset = glyph->voffset;
18742
18743 for (++glyph;
18744 (glyph < last
18745 && glyph->type == STRETCH_GLYPH
18746 && glyph->voffset == voffset
18747 && glyph->face_id == face_id);
18748 ++glyph)
18749 s->width += glyph->pixel_width;
18750
18751 /* Adjust base line for subscript/superscript text. */
18752 s->ybase += voffset;
18753
18754 /* The case that face->gc == 0 is handled when drawing the glyph
18755 string by calling PREPARE_FACE_FOR_DISPLAY. */
18756 xassert (s->face);
18757 return glyph - s->row->glyphs[s->area];
18758 }
18759
18760
18761 /* EXPORT for RIF:
18762 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
18763 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
18764 assumed to be zero. */
18765
18766 void
18767 x_get_glyph_overhangs (glyph, f, left, right)
18768 struct glyph *glyph;
18769 struct frame *f;
18770 int *left, *right;
18771 {
18772 *left = *right = 0;
18773
18774 if (glyph->type == CHAR_GLYPH)
18775 {
18776 XFontStruct *font;
18777 struct face *face;
18778 struct font_info *font_info;
18779 XChar2b char2b;
18780 XCharStruct *pcm;
18781
18782 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
18783 font = face->font;
18784 font_info = FONT_INFO_FROM_ID (f, face->font_info_id);
18785 if (font /* ++KFS: Should this be font_info ? */
18786 && (pcm = rif->per_char_metric (font, &char2b, glyph->font_type)))
18787 {
18788 if (pcm->rbearing > pcm->width)
18789 *right = pcm->rbearing - pcm->width;
18790 if (pcm->lbearing < 0)
18791 *left = -pcm->lbearing;
18792 }
18793 }
18794 }
18795
18796
18797 /* Return the index of the first glyph preceding glyph string S that
18798 is overwritten by S because of S's left overhang. Value is -1
18799 if no glyphs are overwritten. */
18800
18801 static int
18802 left_overwritten (s)
18803 struct glyph_string *s;
18804 {
18805 int k;
18806
18807 if (s->left_overhang)
18808 {
18809 int x = 0, i;
18810 struct glyph *glyphs = s->row->glyphs[s->area];
18811 int first = s->first_glyph - glyphs;
18812
18813 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
18814 x -= glyphs[i].pixel_width;
18815
18816 k = i + 1;
18817 }
18818 else
18819 k = -1;
18820
18821 return k;
18822 }
18823
18824
18825 /* Return the index of the first glyph preceding glyph string S that
18826 is overwriting S because of its right overhang. Value is -1 if no
18827 glyph in front of S overwrites S. */
18828
18829 static int
18830 left_overwriting (s)
18831 struct glyph_string *s;
18832 {
18833 int i, k, x;
18834 struct glyph *glyphs = s->row->glyphs[s->area];
18835 int first = s->first_glyph - glyphs;
18836
18837 k = -1;
18838 x = 0;
18839 for (i = first - 1; i >= 0; --i)
18840 {
18841 int left, right;
18842 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
18843 if (x + right > 0)
18844 k = i;
18845 x -= glyphs[i].pixel_width;
18846 }
18847
18848 return k;
18849 }
18850
18851
18852 /* Return the index of the last glyph following glyph string S that is
18853 not overwritten by S because of S's right overhang. Value is -1 if
18854 no such glyph is found. */
18855
18856 static int
18857 right_overwritten (s)
18858 struct glyph_string *s;
18859 {
18860 int k = -1;
18861
18862 if (s->right_overhang)
18863 {
18864 int x = 0, i;
18865 struct glyph *glyphs = s->row->glyphs[s->area];
18866 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
18867 int end = s->row->used[s->area];
18868
18869 for (i = first; i < end && s->right_overhang > x; ++i)
18870 x += glyphs[i].pixel_width;
18871
18872 k = i;
18873 }
18874
18875 return k;
18876 }
18877
18878
18879 /* Return the index of the last glyph following glyph string S that
18880 overwrites S because of its left overhang. Value is negative
18881 if no such glyph is found. */
18882
18883 static int
18884 right_overwriting (s)
18885 struct glyph_string *s;
18886 {
18887 int i, k, x;
18888 int end = s->row->used[s->area];
18889 struct glyph *glyphs = s->row->glyphs[s->area];
18890 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
18891
18892 k = -1;
18893 x = 0;
18894 for (i = first; i < end; ++i)
18895 {
18896 int left, right;
18897 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
18898 if (x - left < 0)
18899 k = i;
18900 x += glyphs[i].pixel_width;
18901 }
18902
18903 return k;
18904 }
18905
18906
18907 /* Get face and two-byte form of character C in face FACE_ID on frame
18908 F. The encoding of C is returned in *CHAR2B. MULTIBYTE_P non-zero
18909 means we want to display multibyte text. DISPLAY_P non-zero means
18910 make sure that X resources for the face returned are allocated.
18911 Value is a pointer to a realized face that is ready for display if
18912 DISPLAY_P is non-zero. */
18913
18914 static INLINE struct face *
18915 get_char_face_and_encoding (f, c, face_id, char2b, multibyte_p, display_p)
18916 struct frame *f;
18917 int c, face_id;
18918 XChar2b *char2b;
18919 int multibyte_p, display_p;
18920 {
18921 struct face *face = FACE_FROM_ID (f, face_id);
18922
18923 if (!multibyte_p)
18924 {
18925 /* Unibyte case. We don't have to encode, but we have to make
18926 sure to use a face suitable for unibyte. */
18927 STORE_XCHAR2B (char2b, 0, c);
18928 face_id = FACE_FOR_CHAR (f, face, c);
18929 face = FACE_FROM_ID (f, face_id);
18930 }
18931 else if (c < 128)
18932 {
18933 /* Case of ASCII in a face known to fit ASCII. */
18934 STORE_XCHAR2B (char2b, 0, c);
18935 }
18936 else
18937 {
18938 int c1, c2, charset;
18939
18940 /* Split characters into bytes. If c2 is -1 afterwards, C is
18941 really a one-byte character so that byte1 is zero. */
18942 SPLIT_CHAR (c, charset, c1, c2);
18943 if (c2 > 0)
18944 STORE_XCHAR2B (char2b, c1, c2);
18945 else
18946 STORE_XCHAR2B (char2b, 0, c1);
18947
18948 /* Maybe encode the character in *CHAR2B. */
18949 if (face->font != NULL)
18950 {
18951 struct font_info *font_info
18952 = FONT_INFO_FROM_ID (f, face->font_info_id);
18953 if (font_info)
18954 rif->encode_char (c, char2b, font_info, 0);
18955 }
18956 }
18957
18958 /* Make sure X resources of the face are allocated. */
18959 #ifdef HAVE_X_WINDOWS
18960 if (display_p)
18961 #endif
18962 {
18963 xassert (face != NULL);
18964 PREPARE_FACE_FOR_DISPLAY (f, face);
18965 }
18966
18967 return face;
18968 }
18969
18970
18971 /* Set background width of glyph string S. START is the index of the
18972 first glyph following S. LAST_X is the right-most x-position + 1
18973 in the drawing area. */
18974
18975 static INLINE void
18976 set_glyph_string_background_width (s, start, last_x)
18977 struct glyph_string *s;
18978 int start;
18979 int last_x;
18980 {
18981 /* If the face of this glyph string has to be drawn to the end of
18982 the drawing area, set S->extends_to_end_of_line_p. */
18983
18984 if (start == s->row->used[s->area]
18985 && s->area == TEXT_AREA
18986 && ((s->row->fill_line_p
18987 && (s->hl == DRAW_NORMAL_TEXT
18988 || s->hl == DRAW_IMAGE_RAISED
18989 || s->hl == DRAW_IMAGE_SUNKEN))
18990 || s->hl == DRAW_MOUSE_FACE))
18991 s->extends_to_end_of_line_p = 1;
18992
18993 /* If S extends its face to the end of the line, set its
18994 background_width to the distance to the right edge of the drawing
18995 area. */
18996 if (s->extends_to_end_of_line_p)
18997 s->background_width = last_x - s->x + 1;
18998 else
18999 s->background_width = s->width;
19000 }
19001
19002
19003 /* Compute overhangs and x-positions for glyph string S and its
19004 predecessors, or successors. X is the starting x-position for S.
19005 BACKWARD_P non-zero means process predecessors. */
19006
19007 static void
19008 compute_overhangs_and_x (s, x, backward_p)
19009 struct glyph_string *s;
19010 int x;
19011 int backward_p;
19012 {
19013 if (backward_p)
19014 {
19015 while (s)
19016 {
19017 if (rif->compute_glyph_string_overhangs)
19018 rif->compute_glyph_string_overhangs (s);
19019 x -= s->width;
19020 s->x = x;
19021 s = s->prev;
19022 }
19023 }
19024 else
19025 {
19026 while (s)
19027 {
19028 if (rif->compute_glyph_string_overhangs)
19029 rif->compute_glyph_string_overhangs (s);
19030 s->x = x;
19031 x += s->width;
19032 s = s->next;
19033 }
19034 }
19035 }
19036
19037
19038
19039 /* The following macros are only called from draw_glyphs below.
19040 They reference the following parameters of that function directly:
19041 `w', `row', `area', and `overlap_p'
19042 as well as the following local variables:
19043 `s', `f', and `hdc' (in W32) */
19044
19045 #ifdef HAVE_NTGUI
19046 /* On W32, silently add local `hdc' variable to argument list of
19047 init_glyph_string. */
19048 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
19049 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
19050 #else
19051 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
19052 init_glyph_string (s, char2b, w, row, area, start, hl)
19053 #endif
19054
19055 /* Add a glyph string for a stretch glyph to the list of strings
19056 between HEAD and TAIL. START is the index of the stretch glyph in
19057 row area AREA of glyph row ROW. END is the index of the last glyph
19058 in that glyph row area. X is the current output position assigned
19059 to the new glyph string constructed. HL overrides that face of the
19060 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
19061 is the right-most x-position of the drawing area. */
19062
19063 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
19064 and below -- keep them on one line. */
19065 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
19066 do \
19067 { \
19068 s = (struct glyph_string *) alloca (sizeof *s); \
19069 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
19070 START = fill_stretch_glyph_string (s, row, area, START, END); \
19071 append_glyph_string (&HEAD, &TAIL, s); \
19072 s->x = (X); \
19073 } \
19074 while (0)
19075
19076
19077 /* Add a glyph string for an image glyph to the list of strings
19078 between HEAD and TAIL. START is the index of the image glyph in
19079 row area AREA of glyph row ROW. END is the index of the last glyph
19080 in that glyph row area. X is the current output position assigned
19081 to the new glyph string constructed. HL overrides that face of the
19082 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
19083 is the right-most x-position of the drawing area. */
19084
19085 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
19086 do \
19087 { \
19088 s = (struct glyph_string *) alloca (sizeof *s); \
19089 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
19090 fill_image_glyph_string (s); \
19091 append_glyph_string (&HEAD, &TAIL, s); \
19092 ++START; \
19093 s->x = (X); \
19094 } \
19095 while (0)
19096
19097
19098 /* Add a glyph string for a sequence of character glyphs to the list
19099 of strings between HEAD and TAIL. START is the index of the first
19100 glyph in row area AREA of glyph row ROW that is part of the new
19101 glyph string. END is the index of the last glyph in that glyph row
19102 area. X is the current output position assigned to the new glyph
19103 string constructed. HL overrides that face of the glyph; e.g. it
19104 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
19105 right-most x-position of the drawing area. */
19106
19107 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
19108 do \
19109 { \
19110 int c, face_id; \
19111 XChar2b *char2b; \
19112 \
19113 c = (row)->glyphs[area][START].u.ch; \
19114 face_id = (row)->glyphs[area][START].face_id; \
19115 \
19116 s = (struct glyph_string *) alloca (sizeof *s); \
19117 char2b = (XChar2b *) alloca ((END - START) * sizeof *char2b); \
19118 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
19119 append_glyph_string (&HEAD, &TAIL, s); \
19120 s->x = (X); \
19121 START = fill_glyph_string (s, face_id, START, END, overlaps); \
19122 } \
19123 while (0)
19124
19125
19126 /* Add a glyph string for a composite sequence to the list of strings
19127 between HEAD and TAIL. START is the index of the first glyph in
19128 row area AREA of glyph row ROW that is part of the new glyph
19129 string. END is the index of the last glyph in that glyph row area.
19130 X is the current output position assigned to the new glyph string
19131 constructed. HL overrides that face of the glyph; e.g. it is
19132 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
19133 x-position of the drawing area. */
19134
19135 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
19136 do { \
19137 int cmp_id = (row)->glyphs[area][START].u.cmp_id; \
19138 int face_id = (row)->glyphs[area][START].face_id; \
19139 struct face *base_face = FACE_FROM_ID (f, face_id); \
19140 struct composition *cmp = composition_table[cmp_id]; \
19141 int glyph_len = cmp->glyph_len; \
19142 XChar2b *char2b; \
19143 struct face **faces; \
19144 struct glyph_string *first_s = NULL; \
19145 int n; \
19146 \
19147 base_face = base_face->ascii_face; \
19148 char2b = (XChar2b *) alloca ((sizeof *char2b) * glyph_len); \
19149 faces = (struct face **) alloca ((sizeof *faces) * glyph_len); \
19150 /* At first, fill in `char2b' and `faces'. */ \
19151 for (n = 0; n < glyph_len; n++) \
19152 { \
19153 int c = COMPOSITION_GLYPH (cmp, n); \
19154 int this_face_id = FACE_FOR_CHAR (f, base_face, c); \
19155 faces[n] = FACE_FROM_ID (f, this_face_id); \
19156 get_char_face_and_encoding (f, c, this_face_id, \
19157 char2b + n, 1, 1); \
19158 } \
19159 \
19160 /* Make glyph_strings for each glyph sequence that is drawable by \
19161 the same face, and append them to HEAD/TAIL. */ \
19162 for (n = 0; n < cmp->glyph_len;) \
19163 { \
19164 s = (struct glyph_string *) alloca (sizeof *s); \
19165 INIT_GLYPH_STRING (s, char2b + n, w, row, area, START, HL); \
19166 append_glyph_string (&(HEAD), &(TAIL), s); \
19167 s->cmp = cmp; \
19168 s->gidx = n; \
19169 s->x = (X); \
19170 \
19171 if (n == 0) \
19172 first_s = s; \
19173 \
19174 n = fill_composite_glyph_string (s, faces, overlaps); \
19175 } \
19176 \
19177 ++START; \
19178 s = first_s; \
19179 } while (0)
19180
19181
19182 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
19183 of AREA of glyph row ROW on window W between indices START and END.
19184 HL overrides the face for drawing glyph strings, e.g. it is
19185 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
19186 x-positions of the drawing area.
19187
19188 This is an ugly monster macro construct because we must use alloca
19189 to allocate glyph strings (because draw_glyphs can be called
19190 asynchronously). */
19191
19192 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
19193 do \
19194 { \
19195 HEAD = TAIL = NULL; \
19196 while (START < END) \
19197 { \
19198 struct glyph *first_glyph = (row)->glyphs[area] + START; \
19199 switch (first_glyph->type) \
19200 { \
19201 case CHAR_GLYPH: \
19202 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
19203 HL, X, LAST_X); \
19204 break; \
19205 \
19206 case COMPOSITE_GLYPH: \
19207 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
19208 HL, X, LAST_X); \
19209 break; \
19210 \
19211 case STRETCH_GLYPH: \
19212 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
19213 HL, X, LAST_X); \
19214 break; \
19215 \
19216 case IMAGE_GLYPH: \
19217 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
19218 HL, X, LAST_X); \
19219 break; \
19220 \
19221 default: \
19222 abort (); \
19223 } \
19224 \
19225 set_glyph_string_background_width (s, START, LAST_X); \
19226 (X) += s->width; \
19227 } \
19228 } \
19229 while (0)
19230
19231
19232 /* Draw glyphs between START and END in AREA of ROW on window W,
19233 starting at x-position X. X is relative to AREA in W. HL is a
19234 face-override with the following meaning:
19235
19236 DRAW_NORMAL_TEXT draw normally
19237 DRAW_CURSOR draw in cursor face
19238 DRAW_MOUSE_FACE draw in mouse face.
19239 DRAW_INVERSE_VIDEO draw in mode line face
19240 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
19241 DRAW_IMAGE_RAISED draw an image with a raised relief around it
19242
19243 If OVERLAPS is non-zero, draw only the foreground of characters and
19244 clip to the physical height of ROW. Non-zero value also defines
19245 the overlapping part to be drawn:
19246
19247 OVERLAPS_PRED overlap with preceding rows
19248 OVERLAPS_SUCC overlap with succeeding rows
19249 OVERLAPS_BOTH overlap with both preceding/succeeding rows
19250 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
19251
19252 Value is the x-position reached, relative to AREA of W. */
19253
19254 static int
19255 draw_glyphs (w, x, row, area, start, end, hl, overlaps)
19256 struct window *w;
19257 int x;
19258 struct glyph_row *row;
19259 enum glyph_row_area area;
19260 int start, end;
19261 enum draw_glyphs_face hl;
19262 int overlaps;
19263 {
19264 struct glyph_string *head, *tail;
19265 struct glyph_string *s;
19266 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
19267 int last_x, area_width;
19268 int x_reached;
19269 int i, j;
19270 struct frame *f = XFRAME (WINDOW_FRAME (w));
19271 DECLARE_HDC (hdc);
19272
19273 ALLOCATE_HDC (hdc, f);
19274
19275 /* Let's rather be paranoid than getting a SEGV. */
19276 end = min (end, row->used[area]);
19277 start = max (0, start);
19278 start = min (end, start);
19279
19280 /* Translate X to frame coordinates. Set last_x to the right
19281 end of the drawing area. */
19282 if (row->full_width_p)
19283 {
19284 /* X is relative to the left edge of W, without scroll bars
19285 or fringes. */
19286 x += WINDOW_LEFT_EDGE_X (w);
19287 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
19288 }
19289 else
19290 {
19291 int area_left = window_box_left (w, area);
19292 x += area_left;
19293 area_width = window_box_width (w, area);
19294 last_x = area_left + area_width;
19295 }
19296
19297 /* Build a doubly-linked list of glyph_string structures between
19298 head and tail from what we have to draw. Note that the macro
19299 BUILD_GLYPH_STRINGS will modify its start parameter. That's
19300 the reason we use a separate variable `i'. */
19301 i = start;
19302 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
19303 if (tail)
19304 x_reached = tail->x + tail->background_width;
19305 else
19306 x_reached = x;
19307
19308 /* If there are any glyphs with lbearing < 0 or rbearing > width in
19309 the row, redraw some glyphs in front or following the glyph
19310 strings built above. */
19311 if (head && !overlaps && row->contains_overlapping_glyphs_p)
19312 {
19313 int dummy_x = 0;
19314 struct glyph_string *h, *t;
19315
19316 /* Compute overhangs for all glyph strings. */
19317 if (rif->compute_glyph_string_overhangs)
19318 for (s = head; s; s = s->next)
19319 rif->compute_glyph_string_overhangs (s);
19320
19321 /* Prepend glyph strings for glyphs in front of the first glyph
19322 string that are overwritten because of the first glyph
19323 string's left overhang. The background of all strings
19324 prepended must be drawn because the first glyph string
19325 draws over it. */
19326 i = left_overwritten (head);
19327 if (i >= 0)
19328 {
19329 j = i;
19330 BUILD_GLYPH_STRINGS (j, start, h, t,
19331 DRAW_NORMAL_TEXT, dummy_x, last_x);
19332 start = i;
19333 compute_overhangs_and_x (t, head->x, 1);
19334 prepend_glyph_string_lists (&head, &tail, h, t);
19335 clip_head = head;
19336 }
19337
19338 /* Prepend glyph strings for glyphs in front of the first glyph
19339 string that overwrite that glyph string because of their
19340 right overhang. For these strings, only the foreground must
19341 be drawn, because it draws over the glyph string at `head'.
19342 The background must not be drawn because this would overwrite
19343 right overhangs of preceding glyphs for which no glyph
19344 strings exist. */
19345 i = left_overwriting (head);
19346 if (i >= 0)
19347 {
19348 clip_head = head;
19349 BUILD_GLYPH_STRINGS (i, start, h, t,
19350 DRAW_NORMAL_TEXT, dummy_x, last_x);
19351 for (s = h; s; s = s->next)
19352 s->background_filled_p = 1;
19353 compute_overhangs_and_x (t, head->x, 1);
19354 prepend_glyph_string_lists (&head, &tail, h, t);
19355 }
19356
19357 /* Append glyphs strings for glyphs following the last glyph
19358 string tail that are overwritten by tail. The background of
19359 these strings has to be drawn because tail's foreground draws
19360 over it. */
19361 i = right_overwritten (tail);
19362 if (i >= 0)
19363 {
19364 BUILD_GLYPH_STRINGS (end, i, h, t,
19365 DRAW_NORMAL_TEXT, x, last_x);
19366 compute_overhangs_and_x (h, tail->x + tail->width, 0);
19367 append_glyph_string_lists (&head, &tail, h, t);
19368 clip_tail = tail;
19369 }
19370
19371 /* Append glyph strings for glyphs following the last glyph
19372 string tail that overwrite tail. The foreground of such
19373 glyphs has to be drawn because it writes into the background
19374 of tail. The background must not be drawn because it could
19375 paint over the foreground of following glyphs. */
19376 i = right_overwriting (tail);
19377 if (i >= 0)
19378 {
19379 clip_tail = tail;
19380 BUILD_GLYPH_STRINGS (end, i, h, t,
19381 DRAW_NORMAL_TEXT, x, last_x);
19382 for (s = h; s; s = s->next)
19383 s->background_filled_p = 1;
19384 compute_overhangs_and_x (h, tail->x + tail->width, 0);
19385 append_glyph_string_lists (&head, &tail, h, t);
19386 }
19387 if (clip_head || clip_tail)
19388 for (s = head; s; s = s->next)
19389 {
19390 s->clip_head = clip_head;
19391 s->clip_tail = clip_tail;
19392 }
19393 }
19394
19395 /* Draw all strings. */
19396 for (s = head; s; s = s->next)
19397 rif->draw_glyph_string (s);
19398
19399 if (area == TEXT_AREA
19400 && !row->full_width_p
19401 /* When drawing overlapping rows, only the glyph strings'
19402 foreground is drawn, which doesn't erase a cursor
19403 completely. */
19404 && !overlaps)
19405 {
19406 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
19407 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
19408 : (tail ? tail->x + tail->background_width : x));
19409
19410 int text_left = window_box_left (w, TEXT_AREA);
19411 x0 -= text_left;
19412 x1 -= text_left;
19413
19414 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
19415 row->y, MATRIX_ROW_BOTTOM_Y (row));
19416 }
19417
19418 /* Value is the x-position up to which drawn, relative to AREA of W.
19419 This doesn't include parts drawn because of overhangs. */
19420 if (row->full_width_p)
19421 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
19422 else
19423 x_reached -= window_box_left (w, area);
19424
19425 RELEASE_HDC (hdc, f);
19426
19427 return x_reached;
19428 }
19429
19430 /* Expand row matrix if too narrow. Don't expand if area
19431 is not present. */
19432
19433 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
19434 { \
19435 if (!fonts_changed_p \
19436 && (it->glyph_row->glyphs[area] \
19437 < it->glyph_row->glyphs[area + 1])) \
19438 { \
19439 it->w->ncols_scale_factor++; \
19440 fonts_changed_p = 1; \
19441 } \
19442 }
19443
19444 /* Store one glyph for IT->char_to_display in IT->glyph_row.
19445 Called from x_produce_glyphs when IT->glyph_row is non-null. */
19446
19447 static INLINE void
19448 append_glyph (it)
19449 struct it *it;
19450 {
19451 struct glyph *glyph;
19452 enum glyph_row_area area = it->area;
19453
19454 xassert (it->glyph_row);
19455 xassert (it->char_to_display != '\n' && it->char_to_display != '\t');
19456
19457 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
19458 if (glyph < it->glyph_row->glyphs[area + 1])
19459 {
19460 glyph->charpos = CHARPOS (it->position);
19461 glyph->object = it->object;
19462 glyph->pixel_width = it->pixel_width;
19463 glyph->ascent = it->ascent;
19464 glyph->descent = it->descent;
19465 glyph->voffset = it->voffset;
19466 glyph->type = CHAR_GLYPH;
19467 glyph->multibyte_p = it->multibyte_p;
19468 glyph->left_box_line_p = it->start_of_box_run_p;
19469 glyph->right_box_line_p = it->end_of_box_run_p;
19470 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
19471 || it->phys_descent > it->descent);
19472 glyph->padding_p = 0;
19473 glyph->glyph_not_available_p = it->glyph_not_available_p;
19474 glyph->face_id = it->face_id;
19475 glyph->u.ch = it->char_to_display;
19476 glyph->slice = null_glyph_slice;
19477 glyph->font_type = FONT_TYPE_UNKNOWN;
19478 ++it->glyph_row->used[area];
19479 }
19480 else
19481 IT_EXPAND_MATRIX_WIDTH (it, area);
19482 }
19483
19484 /* Store one glyph for the composition IT->cmp_id in IT->glyph_row.
19485 Called from x_produce_glyphs when IT->glyph_row is non-null. */
19486
19487 static INLINE void
19488 append_composite_glyph (it)
19489 struct it *it;
19490 {
19491 struct glyph *glyph;
19492 enum glyph_row_area area = it->area;
19493
19494 xassert (it->glyph_row);
19495
19496 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
19497 if (glyph < it->glyph_row->glyphs[area + 1])
19498 {
19499 glyph->charpos = CHARPOS (it->position);
19500 glyph->object = it->object;
19501 glyph->pixel_width = it->pixel_width;
19502 glyph->ascent = it->ascent;
19503 glyph->descent = it->descent;
19504 glyph->voffset = it->voffset;
19505 glyph->type = COMPOSITE_GLYPH;
19506 glyph->multibyte_p = it->multibyte_p;
19507 glyph->left_box_line_p = it->start_of_box_run_p;
19508 glyph->right_box_line_p = it->end_of_box_run_p;
19509 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
19510 || it->phys_descent > it->descent);
19511 glyph->padding_p = 0;
19512 glyph->glyph_not_available_p = 0;
19513 glyph->face_id = it->face_id;
19514 glyph->u.cmp_id = it->cmp_id;
19515 glyph->slice = null_glyph_slice;
19516 glyph->font_type = FONT_TYPE_UNKNOWN;
19517 ++it->glyph_row->used[area];
19518 }
19519 else
19520 IT_EXPAND_MATRIX_WIDTH (it, area);
19521 }
19522
19523
19524 /* Change IT->ascent and IT->height according to the setting of
19525 IT->voffset. */
19526
19527 static INLINE void
19528 take_vertical_position_into_account (it)
19529 struct it *it;
19530 {
19531 if (it->voffset)
19532 {
19533 if (it->voffset < 0)
19534 /* Increase the ascent so that we can display the text higher
19535 in the line. */
19536 it->ascent -= it->voffset;
19537 else
19538 /* Increase the descent so that we can display the text lower
19539 in the line. */
19540 it->descent += it->voffset;
19541 }
19542 }
19543
19544
19545 /* Produce glyphs/get display metrics for the image IT is loaded with.
19546 See the description of struct display_iterator in dispextern.h for
19547 an overview of struct display_iterator. */
19548
19549 static void
19550 produce_image_glyph (it)
19551 struct it *it;
19552 {
19553 struct image *img;
19554 struct face *face;
19555 int glyph_ascent;
19556 struct glyph_slice slice;
19557
19558 xassert (it->what == IT_IMAGE);
19559
19560 face = FACE_FROM_ID (it->f, it->face_id);
19561 xassert (face);
19562 /* Make sure X resources of the face is loaded. */
19563 PREPARE_FACE_FOR_DISPLAY (it->f, face);
19564
19565 if (it->image_id < 0)
19566 {
19567 /* Fringe bitmap. */
19568 it->ascent = it->phys_ascent = 0;
19569 it->descent = it->phys_descent = 0;
19570 it->pixel_width = 0;
19571 it->nglyphs = 0;
19572 return;
19573 }
19574
19575 img = IMAGE_FROM_ID (it->f, it->image_id);
19576 xassert (img);
19577 /* Make sure X resources of the image is loaded. */
19578 prepare_image_for_display (it->f, img);
19579
19580 slice.x = slice.y = 0;
19581 slice.width = img->width;
19582 slice.height = img->height;
19583
19584 if (INTEGERP (it->slice.x))
19585 slice.x = XINT (it->slice.x);
19586 else if (FLOATP (it->slice.x))
19587 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
19588
19589 if (INTEGERP (it->slice.y))
19590 slice.y = XINT (it->slice.y);
19591 else if (FLOATP (it->slice.y))
19592 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
19593
19594 if (INTEGERP (it->slice.width))
19595 slice.width = XINT (it->slice.width);
19596 else if (FLOATP (it->slice.width))
19597 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
19598
19599 if (INTEGERP (it->slice.height))
19600 slice.height = XINT (it->slice.height);
19601 else if (FLOATP (it->slice.height))
19602 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
19603
19604 if (slice.x >= img->width)
19605 slice.x = img->width;
19606 if (slice.y >= img->height)
19607 slice.y = img->height;
19608 if (slice.x + slice.width >= img->width)
19609 slice.width = img->width - slice.x;
19610 if (slice.y + slice.height > img->height)
19611 slice.height = img->height - slice.y;
19612
19613 if (slice.width == 0 || slice.height == 0)
19614 return;
19615
19616 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
19617
19618 it->descent = slice.height - glyph_ascent;
19619 if (slice.y == 0)
19620 it->descent += img->vmargin;
19621 if (slice.y + slice.height == img->height)
19622 it->descent += img->vmargin;
19623 it->phys_descent = it->descent;
19624
19625 it->pixel_width = slice.width;
19626 if (slice.x == 0)
19627 it->pixel_width += img->hmargin;
19628 if (slice.x + slice.width == img->width)
19629 it->pixel_width += img->hmargin;
19630
19631 /* It's quite possible for images to have an ascent greater than
19632 their height, so don't get confused in that case. */
19633 if (it->descent < 0)
19634 it->descent = 0;
19635
19636 #if 0 /* this breaks image tiling */
19637 /* If this glyph is alone on the last line, adjust it.ascent to minimum row ascent. */
19638 int face_ascent = face->font ? FONT_BASE (face->font) : FRAME_BASELINE_OFFSET (it->f);
19639 if (face_ascent > it->ascent)
19640 it->ascent = it->phys_ascent = face_ascent;
19641 #endif
19642
19643 it->nglyphs = 1;
19644
19645 if (face->box != FACE_NO_BOX)
19646 {
19647 if (face->box_line_width > 0)
19648 {
19649 if (slice.y == 0)
19650 it->ascent += face->box_line_width;
19651 if (slice.y + slice.height == img->height)
19652 it->descent += face->box_line_width;
19653 }
19654
19655 if (it->start_of_box_run_p && slice.x == 0)
19656 it->pixel_width += abs (face->box_line_width);
19657 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
19658 it->pixel_width += abs (face->box_line_width);
19659 }
19660
19661 take_vertical_position_into_account (it);
19662
19663 if (it->glyph_row)
19664 {
19665 struct glyph *glyph;
19666 enum glyph_row_area area = it->area;
19667
19668 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
19669 if (glyph < it->glyph_row->glyphs[area + 1])
19670 {
19671 glyph->charpos = CHARPOS (it->position);
19672 glyph->object = it->object;
19673 glyph->pixel_width = it->pixel_width;
19674 glyph->ascent = glyph_ascent;
19675 glyph->descent = it->descent;
19676 glyph->voffset = it->voffset;
19677 glyph->type = IMAGE_GLYPH;
19678 glyph->multibyte_p = it->multibyte_p;
19679 glyph->left_box_line_p = it->start_of_box_run_p;
19680 glyph->right_box_line_p = it->end_of_box_run_p;
19681 glyph->overlaps_vertically_p = 0;
19682 glyph->padding_p = 0;
19683 glyph->glyph_not_available_p = 0;
19684 glyph->face_id = it->face_id;
19685 glyph->u.img_id = img->id;
19686 glyph->slice = slice;
19687 glyph->font_type = FONT_TYPE_UNKNOWN;
19688 ++it->glyph_row->used[area];
19689 }
19690 else
19691 IT_EXPAND_MATRIX_WIDTH (it, area);
19692 }
19693 }
19694
19695
19696 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
19697 of the glyph, WIDTH and HEIGHT are the width and height of the
19698 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
19699
19700 static void
19701 append_stretch_glyph (it, object, width, height, ascent)
19702 struct it *it;
19703 Lisp_Object object;
19704 int width, height;
19705 int ascent;
19706 {
19707 struct glyph *glyph;
19708 enum glyph_row_area area = it->area;
19709
19710 xassert (ascent >= 0 && ascent <= height);
19711
19712 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
19713 if (glyph < it->glyph_row->glyphs[area + 1])
19714 {
19715 glyph->charpos = CHARPOS (it->position);
19716 glyph->object = object;
19717 glyph->pixel_width = width;
19718 glyph->ascent = ascent;
19719 glyph->descent = height - ascent;
19720 glyph->voffset = it->voffset;
19721 glyph->type = STRETCH_GLYPH;
19722 glyph->multibyte_p = it->multibyte_p;
19723 glyph->left_box_line_p = it->start_of_box_run_p;
19724 glyph->right_box_line_p = it->end_of_box_run_p;
19725 glyph->overlaps_vertically_p = 0;
19726 glyph->padding_p = 0;
19727 glyph->glyph_not_available_p = 0;
19728 glyph->face_id = it->face_id;
19729 glyph->u.stretch.ascent = ascent;
19730 glyph->u.stretch.height = height;
19731 glyph->slice = null_glyph_slice;
19732 glyph->font_type = FONT_TYPE_UNKNOWN;
19733 ++it->glyph_row->used[area];
19734 }
19735 else
19736 IT_EXPAND_MATRIX_WIDTH (it, area);
19737 }
19738
19739
19740 /* Produce a stretch glyph for iterator IT. IT->object is the value
19741 of the glyph property displayed. The value must be a list
19742 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
19743 being recognized:
19744
19745 1. `:width WIDTH' specifies that the space should be WIDTH *
19746 canonical char width wide. WIDTH may be an integer or floating
19747 point number.
19748
19749 2. `:relative-width FACTOR' specifies that the width of the stretch
19750 should be computed from the width of the first character having the
19751 `glyph' property, and should be FACTOR times that width.
19752
19753 3. `:align-to HPOS' specifies that the space should be wide enough
19754 to reach HPOS, a value in canonical character units.
19755
19756 Exactly one of the above pairs must be present.
19757
19758 4. `:height HEIGHT' specifies that the height of the stretch produced
19759 should be HEIGHT, measured in canonical character units.
19760
19761 5. `:relative-height FACTOR' specifies that the height of the
19762 stretch should be FACTOR times the height of the characters having
19763 the glyph property.
19764
19765 Either none or exactly one of 4 or 5 must be present.
19766
19767 6. `:ascent ASCENT' specifies that ASCENT percent of the height
19768 of the stretch should be used for the ascent of the stretch.
19769 ASCENT must be in the range 0 <= ASCENT <= 100. */
19770
19771 static void
19772 produce_stretch_glyph (it)
19773 struct it *it;
19774 {
19775 /* (space :width WIDTH :height HEIGHT ...) */
19776 Lisp_Object prop, plist;
19777 int width = 0, height = 0, align_to = -1;
19778 int zero_width_ok_p = 0, zero_height_ok_p = 0;
19779 int ascent = 0;
19780 double tem;
19781 struct face *face = FACE_FROM_ID (it->f, it->face_id);
19782 XFontStruct *font = face->font ? face->font : FRAME_FONT (it->f);
19783
19784 PREPARE_FACE_FOR_DISPLAY (it->f, face);
19785
19786 /* List should start with `space'. */
19787 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
19788 plist = XCDR (it->object);
19789
19790 /* Compute the width of the stretch. */
19791 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
19792 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
19793 {
19794 /* Absolute width `:width WIDTH' specified and valid. */
19795 zero_width_ok_p = 1;
19796 width = (int)tem;
19797 }
19798 else if (prop = Fplist_get (plist, QCrelative_width),
19799 NUMVAL (prop) > 0)
19800 {
19801 /* Relative width `:relative-width FACTOR' specified and valid.
19802 Compute the width of the characters having the `glyph'
19803 property. */
19804 struct it it2;
19805 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
19806
19807 it2 = *it;
19808 if (it->multibyte_p)
19809 {
19810 int maxlen = ((IT_BYTEPOS (*it) >= GPT ? ZV : GPT)
19811 - IT_BYTEPOS (*it));
19812 it2.c = STRING_CHAR_AND_LENGTH (p, maxlen, it2.len);
19813 }
19814 else
19815 it2.c = *p, it2.len = 1;
19816
19817 it2.glyph_row = NULL;
19818 it2.what = IT_CHARACTER;
19819 x_produce_glyphs (&it2);
19820 width = NUMVAL (prop) * it2.pixel_width;
19821 }
19822 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
19823 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
19824 {
19825 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
19826 align_to = (align_to < 0
19827 ? 0
19828 : align_to - window_box_left_offset (it->w, TEXT_AREA));
19829 else if (align_to < 0)
19830 align_to = window_box_left_offset (it->w, TEXT_AREA);
19831 width = max (0, (int)tem + align_to - it->current_x);
19832 zero_width_ok_p = 1;
19833 }
19834 else
19835 /* Nothing specified -> width defaults to canonical char width. */
19836 width = FRAME_COLUMN_WIDTH (it->f);
19837
19838 if (width <= 0 && (width < 0 || !zero_width_ok_p))
19839 width = 1;
19840
19841 /* Compute height. */
19842 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
19843 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
19844 {
19845 height = (int)tem;
19846 zero_height_ok_p = 1;
19847 }
19848 else if (prop = Fplist_get (plist, QCrelative_height),
19849 NUMVAL (prop) > 0)
19850 height = FONT_HEIGHT (font) * NUMVAL (prop);
19851 else
19852 height = FONT_HEIGHT (font);
19853
19854 if (height <= 0 && (height < 0 || !zero_height_ok_p))
19855 height = 1;
19856
19857 /* Compute percentage of height used for ascent. If
19858 `:ascent ASCENT' is present and valid, use that. Otherwise,
19859 derive the ascent from the font in use. */
19860 if (prop = Fplist_get (plist, QCascent),
19861 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
19862 ascent = height * NUMVAL (prop) / 100.0;
19863 else if (!NILP (prop)
19864 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
19865 ascent = min (max (0, (int)tem), height);
19866 else
19867 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
19868
19869 if (width > 0 && !it->truncate_lines_p
19870 && it->current_x + width > it->last_visible_x)
19871 width = it->last_visible_x - it->current_x - 1;
19872
19873 if (width > 0 && height > 0 && it->glyph_row)
19874 {
19875 Lisp_Object object = it->stack[it->sp - 1].string;
19876 if (!STRINGP (object))
19877 object = it->w->buffer;
19878 append_stretch_glyph (it, object, width, height, ascent);
19879 }
19880
19881 it->pixel_width = width;
19882 it->ascent = it->phys_ascent = ascent;
19883 it->descent = it->phys_descent = height - it->ascent;
19884 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
19885
19886 take_vertical_position_into_account (it);
19887 }
19888
19889 /* Get line-height and line-spacing property at point.
19890 If line-height has format (HEIGHT TOTAL), return TOTAL
19891 in TOTAL_HEIGHT. */
19892
19893 static Lisp_Object
19894 get_line_height_property (it, prop)
19895 struct it *it;
19896 Lisp_Object prop;
19897 {
19898 Lisp_Object position;
19899
19900 if (STRINGP (it->object))
19901 position = make_number (IT_STRING_CHARPOS (*it));
19902 else if (BUFFERP (it->object))
19903 position = make_number (IT_CHARPOS (*it));
19904 else
19905 return Qnil;
19906
19907 return Fget_char_property (position, prop, it->object);
19908 }
19909
19910 /* Calculate line-height and line-spacing properties.
19911 An integer value specifies explicit pixel value.
19912 A float value specifies relative value to current face height.
19913 A cons (float . face-name) specifies relative value to
19914 height of specified face font.
19915
19916 Returns height in pixels, or nil. */
19917
19918
19919 static Lisp_Object
19920 calc_line_height_property (it, val, font, boff, override)
19921 struct it *it;
19922 Lisp_Object val;
19923 XFontStruct *font;
19924 int boff, override;
19925 {
19926 Lisp_Object face_name = Qnil;
19927 int ascent, descent, height;
19928
19929 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
19930 return val;
19931
19932 if (CONSP (val))
19933 {
19934 face_name = XCAR (val);
19935 val = XCDR (val);
19936 if (!NUMBERP (val))
19937 val = make_number (1);
19938 if (NILP (face_name))
19939 {
19940 height = it->ascent + it->descent;
19941 goto scale;
19942 }
19943 }
19944
19945 if (NILP (face_name))
19946 {
19947 font = FRAME_FONT (it->f);
19948 boff = FRAME_BASELINE_OFFSET (it->f);
19949 }
19950 else if (EQ (face_name, Qt))
19951 {
19952 override = 0;
19953 }
19954 else
19955 {
19956 int face_id;
19957 struct face *face;
19958 struct font_info *font_info;
19959
19960 face_id = lookup_named_face (it->f, face_name, ' ', 0);
19961 if (face_id < 0)
19962 return make_number (-1);
19963
19964 face = FACE_FROM_ID (it->f, face_id);
19965 font = face->font;
19966 if (font == NULL)
19967 return make_number (-1);
19968
19969 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
19970 boff = font_info->baseline_offset;
19971 if (font_info->vertical_centering)
19972 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
19973 }
19974
19975 ascent = FONT_BASE (font) + boff;
19976 descent = FONT_DESCENT (font) - boff;
19977
19978 if (override)
19979 {
19980 it->override_ascent = ascent;
19981 it->override_descent = descent;
19982 it->override_boff = boff;
19983 }
19984
19985 height = ascent + descent;
19986
19987 scale:
19988 if (FLOATP (val))
19989 height = (int)(XFLOAT_DATA (val) * height);
19990 else if (INTEGERP (val))
19991 height *= XINT (val);
19992
19993 return make_number (height);
19994 }
19995
19996
19997 /* RIF:
19998 Produce glyphs/get display metrics for the display element IT is
19999 loaded with. See the description of struct it in dispextern.h
20000 for an overview of struct it. */
20001
20002 void
20003 x_produce_glyphs (it)
20004 struct it *it;
20005 {
20006 int extra_line_spacing = it->extra_line_spacing;
20007
20008 it->glyph_not_available_p = 0;
20009
20010 if (it->what == IT_CHARACTER)
20011 {
20012 XChar2b char2b;
20013 XFontStruct *font;
20014 struct face *face = FACE_FROM_ID (it->f, it->face_id);
20015 XCharStruct *pcm;
20016 int font_not_found_p;
20017 struct font_info *font_info;
20018 int boff; /* baseline offset */
20019 /* We may change it->multibyte_p upon unibyte<->multibyte
20020 conversion. So, save the current value now and restore it
20021 later.
20022
20023 Note: It seems that we don't have to record multibyte_p in
20024 struct glyph because the character code itself tells if or
20025 not the character is multibyte. Thus, in the future, we must
20026 consider eliminating the field `multibyte_p' in the struct
20027 glyph. */
20028 int saved_multibyte_p = it->multibyte_p;
20029
20030 /* Maybe translate single-byte characters to multibyte, or the
20031 other way. */
20032 it->char_to_display = it->c;
20033 if (!ASCII_BYTE_P (it->c))
20034 {
20035 if (unibyte_display_via_language_environment
20036 && SINGLE_BYTE_CHAR_P (it->c)
20037 && (it->c >= 0240
20038 || !NILP (Vnonascii_translation_table)))
20039 {
20040 it->char_to_display = unibyte_char_to_multibyte (it->c);
20041 it->multibyte_p = 1;
20042 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
20043 face = FACE_FROM_ID (it->f, it->face_id);
20044 }
20045 else if (!SINGLE_BYTE_CHAR_P (it->c)
20046 && !it->multibyte_p)
20047 {
20048 it->multibyte_p = 1;
20049 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
20050 face = FACE_FROM_ID (it->f, it->face_id);
20051 }
20052 }
20053
20054 /* Get font to use. Encode IT->char_to_display. */
20055 get_char_face_and_encoding (it->f, it->char_to_display, it->face_id,
20056 &char2b, it->multibyte_p, 0);
20057 font = face->font;
20058
20059 /* When no suitable font found, use the default font. */
20060 font_not_found_p = font == NULL;
20061 if (font_not_found_p)
20062 {
20063 font = FRAME_FONT (it->f);
20064 boff = FRAME_BASELINE_OFFSET (it->f);
20065 font_info = NULL;
20066 }
20067 else
20068 {
20069 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
20070 boff = font_info->baseline_offset;
20071 if (font_info->vertical_centering)
20072 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
20073 }
20074
20075 if (it->char_to_display >= ' '
20076 && (!it->multibyte_p || it->char_to_display < 128))
20077 {
20078 /* Either unibyte or ASCII. */
20079 int stretched_p;
20080
20081 it->nglyphs = 1;
20082
20083 pcm = rif->per_char_metric (font, &char2b,
20084 FONT_TYPE_FOR_UNIBYTE (font, it->char_to_display));
20085
20086 if (it->override_ascent >= 0)
20087 {
20088 it->ascent = it->override_ascent;
20089 it->descent = it->override_descent;
20090 boff = it->override_boff;
20091 }
20092 else
20093 {
20094 it->ascent = FONT_BASE (font) + boff;
20095 it->descent = FONT_DESCENT (font) - boff;
20096 }
20097
20098 if (pcm)
20099 {
20100 it->phys_ascent = pcm->ascent + boff;
20101 it->phys_descent = pcm->descent - boff;
20102 it->pixel_width = pcm->width;
20103 }
20104 else
20105 {
20106 it->glyph_not_available_p = 1;
20107 it->phys_ascent = it->ascent;
20108 it->phys_descent = it->descent;
20109 it->pixel_width = FONT_WIDTH (font);
20110 }
20111
20112 if (it->constrain_row_ascent_descent_p)
20113 {
20114 if (it->descent > it->max_descent)
20115 {
20116 it->ascent += it->descent - it->max_descent;
20117 it->descent = it->max_descent;
20118 }
20119 if (it->ascent > it->max_ascent)
20120 {
20121 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
20122 it->ascent = it->max_ascent;
20123 }
20124 it->phys_ascent = min (it->phys_ascent, it->ascent);
20125 it->phys_descent = min (it->phys_descent, it->descent);
20126 extra_line_spacing = 0;
20127 }
20128
20129 /* If this is a space inside a region of text with
20130 `space-width' property, change its width. */
20131 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
20132 if (stretched_p)
20133 it->pixel_width *= XFLOATINT (it->space_width);
20134
20135 /* If face has a box, add the box thickness to the character
20136 height. If character has a box line to the left and/or
20137 right, add the box line width to the character's width. */
20138 if (face->box != FACE_NO_BOX)
20139 {
20140 int thick = face->box_line_width;
20141
20142 if (thick > 0)
20143 {
20144 it->ascent += thick;
20145 it->descent += thick;
20146 }
20147 else
20148 thick = -thick;
20149
20150 if (it->start_of_box_run_p)
20151 it->pixel_width += thick;
20152 if (it->end_of_box_run_p)
20153 it->pixel_width += thick;
20154 }
20155
20156 /* If face has an overline, add the height of the overline
20157 (1 pixel) and a 1 pixel margin to the character height. */
20158 if (face->overline_p)
20159 it->ascent += 2;
20160
20161 if (it->constrain_row_ascent_descent_p)
20162 {
20163 if (it->ascent > it->max_ascent)
20164 it->ascent = it->max_ascent;
20165 if (it->descent > it->max_descent)
20166 it->descent = it->max_descent;
20167 }
20168
20169 take_vertical_position_into_account (it);
20170
20171 /* If we have to actually produce glyphs, do it. */
20172 if (it->glyph_row)
20173 {
20174 if (stretched_p)
20175 {
20176 /* Translate a space with a `space-width' property
20177 into a stretch glyph. */
20178 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
20179 / FONT_HEIGHT (font));
20180 append_stretch_glyph (it, it->object, it->pixel_width,
20181 it->ascent + it->descent, ascent);
20182 }
20183 else
20184 append_glyph (it);
20185
20186 /* If characters with lbearing or rbearing are displayed
20187 in this line, record that fact in a flag of the
20188 glyph row. This is used to optimize X output code. */
20189 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
20190 it->glyph_row->contains_overlapping_glyphs_p = 1;
20191 }
20192 }
20193 else if (it->char_to_display == '\n')
20194 {
20195 /* A newline has no width but we need the height of the line.
20196 But if previous part of the line set a height, don't
20197 increase that height */
20198
20199 Lisp_Object height;
20200 Lisp_Object total_height = Qnil;
20201
20202 it->override_ascent = -1;
20203 it->pixel_width = 0;
20204 it->nglyphs = 0;
20205
20206 height = get_line_height_property(it, Qline_height);
20207 /* Split (line-height total-height) list */
20208 if (CONSP (height)
20209 && CONSP (XCDR (height))
20210 && NILP (XCDR (XCDR (height))))
20211 {
20212 total_height = XCAR (XCDR (height));
20213 height = XCAR (height);
20214 }
20215 height = calc_line_height_property(it, height, font, boff, 1);
20216
20217 if (it->override_ascent >= 0)
20218 {
20219 it->ascent = it->override_ascent;
20220 it->descent = it->override_descent;
20221 boff = it->override_boff;
20222 }
20223 else
20224 {
20225 it->ascent = FONT_BASE (font) + boff;
20226 it->descent = FONT_DESCENT (font) - boff;
20227 }
20228
20229 if (EQ (height, Qt))
20230 {
20231 if (it->descent > it->max_descent)
20232 {
20233 it->ascent += it->descent - it->max_descent;
20234 it->descent = it->max_descent;
20235 }
20236 if (it->ascent > it->max_ascent)
20237 {
20238 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
20239 it->ascent = it->max_ascent;
20240 }
20241 it->phys_ascent = min (it->phys_ascent, it->ascent);
20242 it->phys_descent = min (it->phys_descent, it->descent);
20243 it->constrain_row_ascent_descent_p = 1;
20244 extra_line_spacing = 0;
20245 }
20246 else
20247 {
20248 Lisp_Object spacing;
20249
20250 it->phys_ascent = it->ascent;
20251 it->phys_descent = it->descent;
20252
20253 if ((it->max_ascent > 0 || it->max_descent > 0)
20254 && face->box != FACE_NO_BOX
20255 && face->box_line_width > 0)
20256 {
20257 it->ascent += face->box_line_width;
20258 it->descent += face->box_line_width;
20259 }
20260 if (!NILP (height)
20261 && XINT (height) > it->ascent + it->descent)
20262 it->ascent = XINT (height) - it->descent;
20263
20264 if (!NILP (total_height))
20265 spacing = calc_line_height_property(it, total_height, font, boff, 0);
20266 else
20267 {
20268 spacing = get_line_height_property(it, Qline_spacing);
20269 spacing = calc_line_height_property(it, spacing, font, boff, 0);
20270 }
20271 if (INTEGERP (spacing))
20272 {
20273 extra_line_spacing = XINT (spacing);
20274 if (!NILP (total_height))
20275 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
20276 }
20277 }
20278 }
20279 else if (it->char_to_display == '\t')
20280 {
20281 int tab_width = it->tab_width * FRAME_SPACE_WIDTH (it->f);
20282 int x = it->current_x + it->continuation_lines_width;
20283 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
20284
20285 /* If the distance from the current position to the next tab
20286 stop is less than a space character width, use the
20287 tab stop after that. */
20288 if (next_tab_x - x < FRAME_SPACE_WIDTH (it->f))
20289 next_tab_x += tab_width;
20290
20291 it->pixel_width = next_tab_x - x;
20292 it->nglyphs = 1;
20293 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
20294 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
20295
20296 if (it->glyph_row)
20297 {
20298 append_stretch_glyph (it, it->object, it->pixel_width,
20299 it->ascent + it->descent, it->ascent);
20300 }
20301 }
20302 else
20303 {
20304 /* A multi-byte character. Assume that the display width of the
20305 character is the width of the character multiplied by the
20306 width of the font. */
20307
20308 /* If we found a font, this font should give us the right
20309 metrics. If we didn't find a font, use the frame's
20310 default font and calculate the width of the character
20311 from the charset width; this is what old redisplay code
20312 did. */
20313
20314 pcm = rif->per_char_metric (font, &char2b,
20315 FONT_TYPE_FOR_MULTIBYTE (font, it->c));
20316
20317 if (font_not_found_p || !pcm)
20318 {
20319 int charset = CHAR_CHARSET (it->char_to_display);
20320
20321 it->glyph_not_available_p = 1;
20322 it->pixel_width = (FRAME_COLUMN_WIDTH (it->f)
20323 * CHARSET_WIDTH (charset));
20324 it->phys_ascent = FONT_BASE (font) + boff;
20325 it->phys_descent = FONT_DESCENT (font) - boff;
20326 }
20327 else
20328 {
20329 it->pixel_width = pcm->width;
20330 it->phys_ascent = pcm->ascent + boff;
20331 it->phys_descent = pcm->descent - boff;
20332 if (it->glyph_row
20333 && (pcm->lbearing < 0
20334 || pcm->rbearing > pcm->width))
20335 it->glyph_row->contains_overlapping_glyphs_p = 1;
20336 }
20337 it->nglyphs = 1;
20338 it->ascent = FONT_BASE (font) + boff;
20339 it->descent = FONT_DESCENT (font) - boff;
20340 if (face->box != FACE_NO_BOX)
20341 {
20342 int thick = face->box_line_width;
20343
20344 if (thick > 0)
20345 {
20346 it->ascent += thick;
20347 it->descent += thick;
20348 }
20349 else
20350 thick = - thick;
20351
20352 if (it->start_of_box_run_p)
20353 it->pixel_width += thick;
20354 if (it->end_of_box_run_p)
20355 it->pixel_width += thick;
20356 }
20357
20358 /* If face has an overline, add the height of the overline
20359 (1 pixel) and a 1 pixel margin to the character height. */
20360 if (face->overline_p)
20361 it->ascent += 2;
20362
20363 take_vertical_position_into_account (it);
20364
20365 if (it->glyph_row)
20366 append_glyph (it);
20367 }
20368 it->multibyte_p = saved_multibyte_p;
20369 }
20370 else if (it->what == IT_COMPOSITION)
20371 {
20372 /* Note: A composition is represented as one glyph in the
20373 glyph matrix. There are no padding glyphs. */
20374 XChar2b char2b;
20375 XFontStruct *font;
20376 struct face *face = FACE_FROM_ID (it->f, it->face_id);
20377 XCharStruct *pcm;
20378 int font_not_found_p;
20379 struct font_info *font_info;
20380 int boff; /* baseline offset */
20381 struct composition *cmp = composition_table[it->cmp_id];
20382
20383 /* Maybe translate single-byte characters to multibyte. */
20384 it->char_to_display = it->c;
20385 if (unibyte_display_via_language_environment
20386 && SINGLE_BYTE_CHAR_P (it->c)
20387 && (it->c >= 0240
20388 || (it->c >= 0200
20389 && !NILP (Vnonascii_translation_table))))
20390 {
20391 it->char_to_display = unibyte_char_to_multibyte (it->c);
20392 }
20393
20394 /* Get face and font to use. Encode IT->char_to_display. */
20395 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
20396 face = FACE_FROM_ID (it->f, it->face_id);
20397 get_char_face_and_encoding (it->f, it->char_to_display, it->face_id,
20398 &char2b, it->multibyte_p, 0);
20399 font = face->font;
20400
20401 /* When no suitable font found, use the default font. */
20402 font_not_found_p = font == NULL;
20403 if (font_not_found_p)
20404 {
20405 font = FRAME_FONT (it->f);
20406 boff = FRAME_BASELINE_OFFSET (it->f);
20407 font_info = NULL;
20408 }
20409 else
20410 {
20411 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
20412 boff = font_info->baseline_offset;
20413 if (font_info->vertical_centering)
20414 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
20415 }
20416
20417 /* There are no padding glyphs, so there is only one glyph to
20418 produce for the composition. Important is that pixel_width,
20419 ascent and descent are the values of what is drawn by
20420 draw_glyphs (i.e. the values of the overall glyphs composed). */
20421 it->nglyphs = 1;
20422
20423 /* If we have not yet calculated pixel size data of glyphs of
20424 the composition for the current face font, calculate them
20425 now. Theoretically, we have to check all fonts for the
20426 glyphs, but that requires much time and memory space. So,
20427 here we check only the font of the first glyph. This leads
20428 to incorrect display very rarely, and C-l (recenter) can
20429 correct the display anyway. */
20430 if (cmp->font != (void *) font)
20431 {
20432 /* Ascent and descent of the font of the first character of
20433 this composition (adjusted by baseline offset). Ascent
20434 and descent of overall glyphs should not be less than
20435 them respectively. */
20436 int font_ascent = FONT_BASE (font) + boff;
20437 int font_descent = FONT_DESCENT (font) - boff;
20438 /* Bounding box of the overall glyphs. */
20439 int leftmost, rightmost, lowest, highest;
20440 int i, width, ascent, descent;
20441
20442 cmp->font = (void *) font;
20443
20444 /* Initialize the bounding box. */
20445 if (font_info
20446 && (pcm = rif->per_char_metric (font, &char2b,
20447 FONT_TYPE_FOR_MULTIBYTE (font, it->c))))
20448 {
20449 width = pcm->width;
20450 ascent = pcm->ascent;
20451 descent = pcm->descent;
20452 }
20453 else
20454 {
20455 width = FONT_WIDTH (font);
20456 ascent = FONT_BASE (font);
20457 descent = FONT_DESCENT (font);
20458 }
20459
20460 rightmost = width;
20461 lowest = - descent + boff;
20462 highest = ascent + boff;
20463 leftmost = 0;
20464
20465 if (font_info
20466 && font_info->default_ascent
20467 && CHAR_TABLE_P (Vuse_default_ascent)
20468 && !NILP (Faref (Vuse_default_ascent,
20469 make_number (it->char_to_display))))
20470 highest = font_info->default_ascent + boff;
20471
20472 /* Draw the first glyph at the normal position. It may be
20473 shifted to right later if some other glyphs are drawn at
20474 the left. */
20475 cmp->offsets[0] = 0;
20476 cmp->offsets[1] = boff;
20477
20478 /* Set cmp->offsets for the remaining glyphs. */
20479 for (i = 1; i < cmp->glyph_len; i++)
20480 {
20481 int left, right, btm, top;
20482 int ch = COMPOSITION_GLYPH (cmp, i);
20483 int face_id = FACE_FOR_CHAR (it->f, face, ch);
20484
20485 face = FACE_FROM_ID (it->f, face_id);
20486 get_char_face_and_encoding (it->f, ch, face->id,
20487 &char2b, it->multibyte_p, 0);
20488 font = face->font;
20489 if (font == NULL)
20490 {
20491 font = FRAME_FONT (it->f);
20492 boff = FRAME_BASELINE_OFFSET (it->f);
20493 font_info = NULL;
20494 }
20495 else
20496 {
20497 font_info
20498 = FONT_INFO_FROM_ID (it->f, face->font_info_id);
20499 boff = font_info->baseline_offset;
20500 if (font_info->vertical_centering)
20501 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
20502 }
20503
20504 if (font_info
20505 && (pcm = rif->per_char_metric (font, &char2b,
20506 FONT_TYPE_FOR_MULTIBYTE (font, ch))))
20507 {
20508 width = pcm->width;
20509 ascent = pcm->ascent;
20510 descent = pcm->descent;
20511 }
20512 else
20513 {
20514 width = FONT_WIDTH (font);
20515 ascent = 1;
20516 descent = 0;
20517 }
20518
20519 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
20520 {
20521 /* Relative composition with or without
20522 alternate chars. */
20523 left = (leftmost + rightmost - width) / 2;
20524 btm = - descent + boff;
20525 if (font_info && font_info->relative_compose
20526 && (! CHAR_TABLE_P (Vignore_relative_composition)
20527 || NILP (Faref (Vignore_relative_composition,
20528 make_number (ch)))))
20529 {
20530
20531 if (- descent >= font_info->relative_compose)
20532 /* One extra pixel between two glyphs. */
20533 btm = highest + 1;
20534 else if (ascent <= 0)
20535 /* One extra pixel between two glyphs. */
20536 btm = lowest - 1 - ascent - descent;
20537 }
20538 }
20539 else
20540 {
20541 /* A composition rule is specified by an integer
20542 value that encodes global and new reference
20543 points (GREF and NREF). GREF and NREF are
20544 specified by numbers as below:
20545
20546 0---1---2 -- ascent
20547 | |
20548 | |
20549 | |
20550 9--10--11 -- center
20551 | |
20552 ---3---4---5--- baseline
20553 | |
20554 6---7---8 -- descent
20555 */
20556 int rule = COMPOSITION_RULE (cmp, i);
20557 int gref, nref, grefx, grefy, nrefx, nrefy;
20558
20559 COMPOSITION_DECODE_RULE (rule, gref, nref);
20560 grefx = gref % 3, nrefx = nref % 3;
20561 grefy = gref / 3, nrefy = nref / 3;
20562
20563 left = (leftmost
20564 + grefx * (rightmost - leftmost) / 2
20565 - nrefx * width / 2);
20566 btm = ((grefy == 0 ? highest
20567 : grefy == 1 ? 0
20568 : grefy == 2 ? lowest
20569 : (highest + lowest) / 2)
20570 - (nrefy == 0 ? ascent + descent
20571 : nrefy == 1 ? descent - boff
20572 : nrefy == 2 ? 0
20573 : (ascent + descent) / 2));
20574 }
20575
20576 cmp->offsets[i * 2] = left;
20577 cmp->offsets[i * 2 + 1] = btm + descent;
20578
20579 /* Update the bounding box of the overall glyphs. */
20580 right = left + width;
20581 top = btm + descent + ascent;
20582 if (left < leftmost)
20583 leftmost = left;
20584 if (right > rightmost)
20585 rightmost = right;
20586 if (top > highest)
20587 highest = top;
20588 if (btm < lowest)
20589 lowest = btm;
20590 }
20591
20592 /* If there are glyphs whose x-offsets are negative,
20593 shift all glyphs to the right and make all x-offsets
20594 non-negative. */
20595 if (leftmost < 0)
20596 {
20597 for (i = 0; i < cmp->glyph_len; i++)
20598 cmp->offsets[i * 2] -= leftmost;
20599 rightmost -= leftmost;
20600 }
20601
20602 cmp->pixel_width = rightmost;
20603 cmp->ascent = highest;
20604 cmp->descent = - lowest;
20605 if (cmp->ascent < font_ascent)
20606 cmp->ascent = font_ascent;
20607 if (cmp->descent < font_descent)
20608 cmp->descent = font_descent;
20609 }
20610
20611 it->pixel_width = cmp->pixel_width;
20612 it->ascent = it->phys_ascent = cmp->ascent;
20613 it->descent = it->phys_descent = cmp->descent;
20614
20615 if (face->box != FACE_NO_BOX)
20616 {
20617 int thick = face->box_line_width;
20618
20619 if (thick > 0)
20620 {
20621 it->ascent += thick;
20622 it->descent += thick;
20623 }
20624 else
20625 thick = - thick;
20626
20627 if (it->start_of_box_run_p)
20628 it->pixel_width += thick;
20629 if (it->end_of_box_run_p)
20630 it->pixel_width += thick;
20631 }
20632
20633 /* If face has an overline, add the height of the overline
20634 (1 pixel) and a 1 pixel margin to the character height. */
20635 if (face->overline_p)
20636 it->ascent += 2;
20637
20638 take_vertical_position_into_account (it);
20639
20640 if (it->glyph_row)
20641 append_composite_glyph (it);
20642 }
20643 else if (it->what == IT_IMAGE)
20644 produce_image_glyph (it);
20645 else if (it->what == IT_STRETCH)
20646 produce_stretch_glyph (it);
20647
20648 /* Accumulate dimensions. Note: can't assume that it->descent > 0
20649 because this isn't true for images with `:ascent 100'. */
20650 xassert (it->ascent >= 0 && it->descent >= 0);
20651 if (it->area == TEXT_AREA)
20652 it->current_x += it->pixel_width;
20653
20654 if (extra_line_spacing > 0)
20655 {
20656 it->descent += extra_line_spacing;
20657 if (extra_line_spacing > it->max_extra_line_spacing)
20658 it->max_extra_line_spacing = extra_line_spacing;
20659 }
20660
20661 it->max_ascent = max (it->max_ascent, it->ascent);
20662 it->max_descent = max (it->max_descent, it->descent);
20663 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
20664 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
20665 }
20666
20667 /* EXPORT for RIF:
20668 Output LEN glyphs starting at START at the nominal cursor position.
20669 Advance the nominal cursor over the text. The global variable
20670 updated_window contains the window being updated, updated_row is
20671 the glyph row being updated, and updated_area is the area of that
20672 row being updated. */
20673
20674 void
20675 x_write_glyphs (start, len)
20676 struct glyph *start;
20677 int len;
20678 {
20679 int x, hpos;
20680
20681 xassert (updated_window && updated_row);
20682 BLOCK_INPUT;
20683
20684 /* Write glyphs. */
20685
20686 hpos = start - updated_row->glyphs[updated_area];
20687 x = draw_glyphs (updated_window, output_cursor.x,
20688 updated_row, updated_area,
20689 hpos, hpos + len,
20690 DRAW_NORMAL_TEXT, 0);
20691
20692 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
20693 if (updated_area == TEXT_AREA
20694 && updated_window->phys_cursor_on_p
20695 && updated_window->phys_cursor.vpos == output_cursor.vpos
20696 && updated_window->phys_cursor.hpos >= hpos
20697 && updated_window->phys_cursor.hpos < hpos + len)
20698 updated_window->phys_cursor_on_p = 0;
20699
20700 UNBLOCK_INPUT;
20701
20702 /* Advance the output cursor. */
20703 output_cursor.hpos += len;
20704 output_cursor.x = x;
20705 }
20706
20707
20708 /* EXPORT for RIF:
20709 Insert LEN glyphs from START at the nominal cursor position. */
20710
20711 void
20712 x_insert_glyphs (start, len)
20713 struct glyph *start;
20714 int len;
20715 {
20716 struct frame *f;
20717 struct window *w;
20718 int line_height, shift_by_width, shifted_region_width;
20719 struct glyph_row *row;
20720 struct glyph *glyph;
20721 int frame_x, frame_y, hpos;
20722
20723 xassert (updated_window && updated_row);
20724 BLOCK_INPUT;
20725 w = updated_window;
20726 f = XFRAME (WINDOW_FRAME (w));
20727
20728 /* Get the height of the line we are in. */
20729 row = updated_row;
20730 line_height = row->height;
20731
20732 /* Get the width of the glyphs to insert. */
20733 shift_by_width = 0;
20734 for (glyph = start; glyph < start + len; ++glyph)
20735 shift_by_width += glyph->pixel_width;
20736
20737 /* Get the width of the region to shift right. */
20738 shifted_region_width = (window_box_width (w, updated_area)
20739 - output_cursor.x
20740 - shift_by_width);
20741
20742 /* Shift right. */
20743 frame_x = window_box_left (w, updated_area) + output_cursor.x;
20744 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
20745
20746 rif->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
20747 line_height, shift_by_width);
20748
20749 /* Write the glyphs. */
20750 hpos = start - row->glyphs[updated_area];
20751 draw_glyphs (w, output_cursor.x, row, updated_area,
20752 hpos, hpos + len,
20753 DRAW_NORMAL_TEXT, 0);
20754
20755 /* Advance the output cursor. */
20756 output_cursor.hpos += len;
20757 output_cursor.x += shift_by_width;
20758 UNBLOCK_INPUT;
20759 }
20760
20761
20762 /* EXPORT for RIF:
20763 Erase the current text line from the nominal cursor position
20764 (inclusive) to pixel column TO_X (exclusive). The idea is that
20765 everything from TO_X onward is already erased.
20766
20767 TO_X is a pixel position relative to updated_area of
20768 updated_window. TO_X == -1 means clear to the end of this area. */
20769
20770 void
20771 x_clear_end_of_line (to_x)
20772 int to_x;
20773 {
20774 struct frame *f;
20775 struct window *w = updated_window;
20776 int max_x, min_y, max_y;
20777 int from_x, from_y, to_y;
20778
20779 xassert (updated_window && updated_row);
20780 f = XFRAME (w->frame);
20781
20782 if (updated_row->full_width_p)
20783 max_x = WINDOW_TOTAL_WIDTH (w);
20784 else
20785 max_x = window_box_width (w, updated_area);
20786 max_y = window_text_bottom_y (w);
20787
20788 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
20789 of window. For TO_X > 0, truncate to end of drawing area. */
20790 if (to_x == 0)
20791 return;
20792 else if (to_x < 0)
20793 to_x = max_x;
20794 else
20795 to_x = min (to_x, max_x);
20796
20797 to_y = min (max_y, output_cursor.y + updated_row->height);
20798
20799 /* Notice if the cursor will be cleared by this operation. */
20800 if (!updated_row->full_width_p)
20801 notice_overwritten_cursor (w, updated_area,
20802 output_cursor.x, -1,
20803 updated_row->y,
20804 MATRIX_ROW_BOTTOM_Y (updated_row));
20805
20806 from_x = output_cursor.x;
20807
20808 /* Translate to frame coordinates. */
20809 if (updated_row->full_width_p)
20810 {
20811 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
20812 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
20813 }
20814 else
20815 {
20816 int area_left = window_box_left (w, updated_area);
20817 from_x += area_left;
20818 to_x += area_left;
20819 }
20820
20821 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
20822 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
20823 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
20824
20825 /* Prevent inadvertently clearing to end of the X window. */
20826 if (to_x > from_x && to_y > from_y)
20827 {
20828 BLOCK_INPUT;
20829 rif->clear_frame_area (f, from_x, from_y,
20830 to_x - from_x, to_y - from_y);
20831 UNBLOCK_INPUT;
20832 }
20833 }
20834
20835 #endif /* HAVE_WINDOW_SYSTEM */
20836
20837
20838 \f
20839 /***********************************************************************
20840 Cursor types
20841 ***********************************************************************/
20842
20843 /* Value is the internal representation of the specified cursor type
20844 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
20845 of the bar cursor. */
20846
20847 static enum text_cursor_kinds
20848 get_specified_cursor_type (arg, width)
20849 Lisp_Object arg;
20850 int *width;
20851 {
20852 enum text_cursor_kinds type;
20853
20854 if (NILP (arg))
20855 return NO_CURSOR;
20856
20857 if (EQ (arg, Qbox))
20858 return FILLED_BOX_CURSOR;
20859
20860 if (EQ (arg, Qhollow))
20861 return HOLLOW_BOX_CURSOR;
20862
20863 if (EQ (arg, Qbar))
20864 {
20865 *width = 2;
20866 return BAR_CURSOR;
20867 }
20868
20869 if (CONSP (arg)
20870 && EQ (XCAR (arg), Qbar)
20871 && INTEGERP (XCDR (arg))
20872 && XINT (XCDR (arg)) >= 0)
20873 {
20874 *width = XINT (XCDR (arg));
20875 return BAR_CURSOR;
20876 }
20877
20878 if (EQ (arg, Qhbar))
20879 {
20880 *width = 2;
20881 return HBAR_CURSOR;
20882 }
20883
20884 if (CONSP (arg)
20885 && EQ (XCAR (arg), Qhbar)
20886 && INTEGERP (XCDR (arg))
20887 && XINT (XCDR (arg)) >= 0)
20888 {
20889 *width = XINT (XCDR (arg));
20890 return HBAR_CURSOR;
20891 }
20892
20893 /* Treat anything unknown as "hollow box cursor".
20894 It was bad to signal an error; people have trouble fixing
20895 .Xdefaults with Emacs, when it has something bad in it. */
20896 type = HOLLOW_BOX_CURSOR;
20897
20898 return type;
20899 }
20900
20901 /* Set the default cursor types for specified frame. */
20902 void
20903 set_frame_cursor_types (f, arg)
20904 struct frame *f;
20905 Lisp_Object arg;
20906 {
20907 int width;
20908 Lisp_Object tem;
20909
20910 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
20911 FRAME_CURSOR_WIDTH (f) = width;
20912
20913 /* By default, set up the blink-off state depending on the on-state. */
20914
20915 tem = Fassoc (arg, Vblink_cursor_alist);
20916 if (!NILP (tem))
20917 {
20918 FRAME_BLINK_OFF_CURSOR (f)
20919 = get_specified_cursor_type (XCDR (tem), &width);
20920 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
20921 }
20922 else
20923 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
20924 }
20925
20926
20927 /* Return the cursor we want to be displayed in window W. Return
20928 width of bar/hbar cursor through WIDTH arg. Return with
20929 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
20930 (i.e. if the `system caret' should track this cursor).
20931
20932 In a mini-buffer window, we want the cursor only to appear if we
20933 are reading input from this window. For the selected window, we
20934 want the cursor type given by the frame parameter or buffer local
20935 setting of cursor-type. If explicitly marked off, draw no cursor.
20936 In all other cases, we want a hollow box cursor. */
20937
20938 static enum text_cursor_kinds
20939 get_window_cursor_type (w, glyph, width, active_cursor)
20940 struct window *w;
20941 struct glyph *glyph;
20942 int *width;
20943 int *active_cursor;
20944 {
20945 struct frame *f = XFRAME (w->frame);
20946 struct buffer *b = XBUFFER (w->buffer);
20947 int cursor_type = DEFAULT_CURSOR;
20948 Lisp_Object alt_cursor;
20949 int non_selected = 0;
20950
20951 *active_cursor = 1;
20952
20953 /* Echo area */
20954 if (cursor_in_echo_area
20955 && FRAME_HAS_MINIBUF_P (f)
20956 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
20957 {
20958 if (w == XWINDOW (echo_area_window))
20959 {
20960 if (EQ (b->cursor_type, Qt) || NILP (b->cursor_type))
20961 {
20962 *width = FRAME_CURSOR_WIDTH (f);
20963 return FRAME_DESIRED_CURSOR (f);
20964 }
20965 else
20966 return get_specified_cursor_type (b->cursor_type, width);
20967 }
20968
20969 *active_cursor = 0;
20970 non_selected = 1;
20971 }
20972
20973 /* Nonselected window or nonselected frame. */
20974 else if (w != XWINDOW (f->selected_window)
20975 #ifdef HAVE_WINDOW_SYSTEM
20976 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame
20977 #endif
20978 )
20979 {
20980 *active_cursor = 0;
20981
20982 if (MINI_WINDOW_P (w) && minibuf_level == 0)
20983 return NO_CURSOR;
20984
20985 non_selected = 1;
20986 }
20987
20988 /* Never display a cursor in a window in which cursor-type is nil. */
20989 if (NILP (b->cursor_type))
20990 return NO_CURSOR;
20991
20992 /* Use cursor-in-non-selected-windows for non-selected window or frame. */
20993 if (non_selected)
20994 {
20995 alt_cursor = b->cursor_in_non_selected_windows;
20996 return get_specified_cursor_type (alt_cursor, width);
20997 }
20998
20999 /* Get the normal cursor type for this window. */
21000 if (EQ (b->cursor_type, Qt))
21001 {
21002 cursor_type = FRAME_DESIRED_CURSOR (f);
21003 *width = FRAME_CURSOR_WIDTH (f);
21004 }
21005 else
21006 cursor_type = get_specified_cursor_type (b->cursor_type, width);
21007
21008 /* Use normal cursor if not blinked off. */
21009 if (!w->cursor_off_p)
21010 {
21011 if (glyph != NULL && glyph->type == IMAGE_GLYPH) {
21012 if (cursor_type == FILLED_BOX_CURSOR)
21013 cursor_type = HOLLOW_BOX_CURSOR;
21014 }
21015 return cursor_type;
21016 }
21017
21018 /* Cursor is blinked off, so determine how to "toggle" it. */
21019
21020 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
21021 if ((alt_cursor = Fassoc (b->cursor_type, Vblink_cursor_alist), !NILP (alt_cursor)))
21022 return get_specified_cursor_type (XCDR (alt_cursor), width);
21023
21024 /* Then see if frame has specified a specific blink off cursor type. */
21025 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
21026 {
21027 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
21028 return FRAME_BLINK_OFF_CURSOR (f);
21029 }
21030
21031 #if 0
21032 /* Some people liked having a permanently visible blinking cursor,
21033 while others had very strong opinions against it. So it was
21034 decided to remove it. KFS 2003-09-03 */
21035
21036 /* Finally perform built-in cursor blinking:
21037 filled box <-> hollow box
21038 wide [h]bar <-> narrow [h]bar
21039 narrow [h]bar <-> no cursor
21040 other type <-> no cursor */
21041
21042 if (cursor_type == FILLED_BOX_CURSOR)
21043 return HOLLOW_BOX_CURSOR;
21044
21045 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
21046 {
21047 *width = 1;
21048 return cursor_type;
21049 }
21050 #endif
21051
21052 return NO_CURSOR;
21053 }
21054
21055
21056 #ifdef HAVE_WINDOW_SYSTEM
21057
21058 /* Notice when the text cursor of window W has been completely
21059 overwritten by a drawing operation that outputs glyphs in AREA
21060 starting at X0 and ending at X1 in the line starting at Y0 and
21061 ending at Y1. X coordinates are area-relative. X1 < 0 means all
21062 the rest of the line after X0 has been written. Y coordinates
21063 are window-relative. */
21064
21065 static void
21066 notice_overwritten_cursor (w, area, x0, x1, y0, y1)
21067 struct window *w;
21068 enum glyph_row_area area;
21069 int x0, y0, x1, y1;
21070 {
21071 int cx0, cx1, cy0, cy1;
21072 struct glyph_row *row;
21073
21074 if (!w->phys_cursor_on_p)
21075 return;
21076 if (area != TEXT_AREA)
21077 return;
21078
21079 if (w->phys_cursor.vpos < 0
21080 || w->phys_cursor.vpos >= w->current_matrix->nrows
21081 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
21082 !(row->enabled_p && row->displays_text_p)))
21083 return;
21084
21085 if (row->cursor_in_fringe_p)
21086 {
21087 row->cursor_in_fringe_p = 0;
21088 draw_fringe_bitmap (w, row, 0);
21089 w->phys_cursor_on_p = 0;
21090 return;
21091 }
21092
21093 cx0 = w->phys_cursor.x;
21094 cx1 = cx0 + w->phys_cursor_width;
21095 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
21096 return;
21097
21098 /* The cursor image will be completely removed from the
21099 screen if the output area intersects the cursor area in
21100 y-direction. When we draw in [y0 y1[, and some part of
21101 the cursor is at y < y0, that part must have been drawn
21102 before. When scrolling, the cursor is erased before
21103 actually scrolling, so we don't come here. When not
21104 scrolling, the rows above the old cursor row must have
21105 changed, and in this case these rows must have written
21106 over the cursor image.
21107
21108 Likewise if part of the cursor is below y1, with the
21109 exception of the cursor being in the first blank row at
21110 the buffer and window end because update_text_area
21111 doesn't draw that row. (Except when it does, but
21112 that's handled in update_text_area.) */
21113
21114 cy0 = w->phys_cursor.y;
21115 cy1 = cy0 + w->phys_cursor_height;
21116 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
21117 return;
21118
21119 w->phys_cursor_on_p = 0;
21120 }
21121
21122 #endif /* HAVE_WINDOW_SYSTEM */
21123
21124 \f
21125 /************************************************************************
21126 Mouse Face
21127 ************************************************************************/
21128
21129 #ifdef HAVE_WINDOW_SYSTEM
21130
21131 /* EXPORT for RIF:
21132 Fix the display of area AREA of overlapping row ROW in window W
21133 with respect to the overlapping part OVERLAPS. */
21134
21135 void
21136 x_fix_overlapping_area (w, row, area, overlaps)
21137 struct window *w;
21138 struct glyph_row *row;
21139 enum glyph_row_area area;
21140 int overlaps;
21141 {
21142 int i, x;
21143
21144 BLOCK_INPUT;
21145
21146 x = 0;
21147 for (i = 0; i < row->used[area];)
21148 {
21149 if (row->glyphs[area][i].overlaps_vertically_p)
21150 {
21151 int start = i, start_x = x;
21152
21153 do
21154 {
21155 x += row->glyphs[area][i].pixel_width;
21156 ++i;
21157 }
21158 while (i < row->used[area]
21159 && row->glyphs[area][i].overlaps_vertically_p);
21160
21161 draw_glyphs (w, start_x, row, area,
21162 start, i,
21163 DRAW_NORMAL_TEXT, overlaps);
21164 }
21165 else
21166 {
21167 x += row->glyphs[area][i].pixel_width;
21168 ++i;
21169 }
21170 }
21171
21172 UNBLOCK_INPUT;
21173 }
21174
21175
21176 /* EXPORT:
21177 Draw the cursor glyph of window W in glyph row ROW. See the
21178 comment of draw_glyphs for the meaning of HL. */
21179
21180 void
21181 draw_phys_cursor_glyph (w, row, hl)
21182 struct window *w;
21183 struct glyph_row *row;
21184 enum draw_glyphs_face hl;
21185 {
21186 /* If cursor hpos is out of bounds, don't draw garbage. This can
21187 happen in mini-buffer windows when switching between echo area
21188 glyphs and mini-buffer. */
21189 if (w->phys_cursor.hpos < row->used[TEXT_AREA])
21190 {
21191 int on_p = w->phys_cursor_on_p;
21192 int x1;
21193 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA,
21194 w->phys_cursor.hpos, w->phys_cursor.hpos + 1,
21195 hl, 0);
21196 w->phys_cursor_on_p = on_p;
21197
21198 if (hl == DRAW_CURSOR)
21199 w->phys_cursor_width = x1 - w->phys_cursor.x;
21200 /* When we erase the cursor, and ROW is overlapped by other
21201 rows, make sure that these overlapping parts of other rows
21202 are redrawn. */
21203 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
21204 {
21205 w->phys_cursor_width = x1 - w->phys_cursor.x;
21206
21207 if (row > w->current_matrix->rows
21208 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
21209 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
21210 OVERLAPS_ERASED_CURSOR);
21211
21212 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
21213 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
21214 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
21215 OVERLAPS_ERASED_CURSOR);
21216 }
21217 }
21218 }
21219
21220
21221 /* EXPORT:
21222 Erase the image of a cursor of window W from the screen. */
21223
21224 void
21225 erase_phys_cursor (w)
21226 struct window *w;
21227 {
21228 struct frame *f = XFRAME (w->frame);
21229 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
21230 int hpos = w->phys_cursor.hpos;
21231 int vpos = w->phys_cursor.vpos;
21232 int mouse_face_here_p = 0;
21233 struct glyph_matrix *active_glyphs = w->current_matrix;
21234 struct glyph_row *cursor_row;
21235 struct glyph *cursor_glyph;
21236 enum draw_glyphs_face hl;
21237
21238 /* No cursor displayed or row invalidated => nothing to do on the
21239 screen. */
21240 if (w->phys_cursor_type == NO_CURSOR)
21241 goto mark_cursor_off;
21242
21243 /* VPOS >= active_glyphs->nrows means that window has been resized.
21244 Don't bother to erase the cursor. */
21245 if (vpos >= active_glyphs->nrows)
21246 goto mark_cursor_off;
21247
21248 /* If row containing cursor is marked invalid, there is nothing we
21249 can do. */
21250 cursor_row = MATRIX_ROW (active_glyphs, vpos);
21251 if (!cursor_row->enabled_p)
21252 goto mark_cursor_off;
21253
21254 /* If line spacing is > 0, old cursor may only be partially visible in
21255 window after split-window. So adjust visible height. */
21256 cursor_row->visible_height = min (cursor_row->visible_height,
21257 window_text_bottom_y (w) - cursor_row->y);
21258
21259 /* If row is completely invisible, don't attempt to delete a cursor which
21260 isn't there. This can happen if cursor is at top of a window, and
21261 we switch to a buffer with a header line in that window. */
21262 if (cursor_row->visible_height <= 0)
21263 goto mark_cursor_off;
21264
21265 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
21266 if (cursor_row->cursor_in_fringe_p)
21267 {
21268 cursor_row->cursor_in_fringe_p = 0;
21269 draw_fringe_bitmap (w, cursor_row, 0);
21270 goto mark_cursor_off;
21271 }
21272
21273 /* This can happen when the new row is shorter than the old one.
21274 In this case, either draw_glyphs or clear_end_of_line
21275 should have cleared the cursor. Note that we wouldn't be
21276 able to erase the cursor in this case because we don't have a
21277 cursor glyph at hand. */
21278 if (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])
21279 goto mark_cursor_off;
21280
21281 /* If the cursor is in the mouse face area, redisplay that when
21282 we clear the cursor. */
21283 if (! NILP (dpyinfo->mouse_face_window)
21284 && w == XWINDOW (dpyinfo->mouse_face_window)
21285 && (vpos > dpyinfo->mouse_face_beg_row
21286 || (vpos == dpyinfo->mouse_face_beg_row
21287 && hpos >= dpyinfo->mouse_face_beg_col))
21288 && (vpos < dpyinfo->mouse_face_end_row
21289 || (vpos == dpyinfo->mouse_face_end_row
21290 && hpos < dpyinfo->mouse_face_end_col))
21291 /* Don't redraw the cursor's spot in mouse face if it is at the
21292 end of a line (on a newline). The cursor appears there, but
21293 mouse highlighting does not. */
21294 && cursor_row->used[TEXT_AREA] > hpos)
21295 mouse_face_here_p = 1;
21296
21297 /* Maybe clear the display under the cursor. */
21298 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
21299 {
21300 int x, y;
21301 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
21302 int width;
21303
21304 cursor_glyph = get_phys_cursor_glyph (w);
21305 if (cursor_glyph == NULL)
21306 goto mark_cursor_off;
21307
21308 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
21309 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
21310 width = min (cursor_glyph->pixel_width,
21311 window_box_width (w, TEXT_AREA) - w->phys_cursor.x);
21312
21313 rif->clear_frame_area (f, x, y, width, cursor_row->visible_height);
21314 }
21315
21316 /* Erase the cursor by redrawing the character underneath it. */
21317 if (mouse_face_here_p)
21318 hl = DRAW_MOUSE_FACE;
21319 else
21320 hl = DRAW_NORMAL_TEXT;
21321 draw_phys_cursor_glyph (w, cursor_row, hl);
21322
21323 mark_cursor_off:
21324 w->phys_cursor_on_p = 0;
21325 w->phys_cursor_type = NO_CURSOR;
21326 }
21327
21328
21329 /* EXPORT:
21330 Display or clear cursor of window W. If ON is zero, clear the
21331 cursor. If it is non-zero, display the cursor. If ON is nonzero,
21332 where to put the cursor is specified by HPOS, VPOS, X and Y. */
21333
21334 void
21335 display_and_set_cursor (w, on, hpos, vpos, x, y)
21336 struct window *w;
21337 int on, hpos, vpos, x, y;
21338 {
21339 struct frame *f = XFRAME (w->frame);
21340 int new_cursor_type;
21341 int new_cursor_width;
21342 int active_cursor;
21343 struct glyph_row *glyph_row;
21344 struct glyph *glyph;
21345
21346 /* This is pointless on invisible frames, and dangerous on garbaged
21347 windows and frames; in the latter case, the frame or window may
21348 be in the midst of changing its size, and x and y may be off the
21349 window. */
21350 if (! FRAME_VISIBLE_P (f)
21351 || FRAME_GARBAGED_P (f)
21352 || vpos >= w->current_matrix->nrows
21353 || hpos >= w->current_matrix->matrix_w)
21354 return;
21355
21356 /* If cursor is off and we want it off, return quickly. */
21357 if (!on && !w->phys_cursor_on_p)
21358 return;
21359
21360 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
21361 /* If cursor row is not enabled, we don't really know where to
21362 display the cursor. */
21363 if (!glyph_row->enabled_p)
21364 {
21365 w->phys_cursor_on_p = 0;
21366 return;
21367 }
21368
21369 glyph = NULL;
21370 if (!glyph_row->exact_window_width_line_p
21371 || hpos < glyph_row->used[TEXT_AREA])
21372 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
21373
21374 xassert (interrupt_input_blocked);
21375
21376 /* Set new_cursor_type to the cursor we want to be displayed. */
21377 new_cursor_type = get_window_cursor_type (w, glyph,
21378 &new_cursor_width, &active_cursor);
21379
21380 /* If cursor is currently being shown and we don't want it to be or
21381 it is in the wrong place, or the cursor type is not what we want,
21382 erase it. */
21383 if (w->phys_cursor_on_p
21384 && (!on
21385 || w->phys_cursor.x != x
21386 || w->phys_cursor.y != y
21387 || new_cursor_type != w->phys_cursor_type
21388 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
21389 && new_cursor_width != w->phys_cursor_width)))
21390 erase_phys_cursor (w);
21391
21392 /* Don't check phys_cursor_on_p here because that flag is only set
21393 to zero in some cases where we know that the cursor has been
21394 completely erased, to avoid the extra work of erasing the cursor
21395 twice. In other words, phys_cursor_on_p can be 1 and the cursor
21396 still not be visible, or it has only been partly erased. */
21397 if (on)
21398 {
21399 w->phys_cursor_ascent = glyph_row->ascent;
21400 w->phys_cursor_height = glyph_row->height;
21401
21402 /* Set phys_cursor_.* before x_draw_.* is called because some
21403 of them may need the information. */
21404 w->phys_cursor.x = x;
21405 w->phys_cursor.y = glyph_row->y;
21406 w->phys_cursor.hpos = hpos;
21407 w->phys_cursor.vpos = vpos;
21408 }
21409
21410 rif->draw_window_cursor (w, glyph_row, x, y,
21411 new_cursor_type, new_cursor_width,
21412 on, active_cursor);
21413 }
21414
21415
21416 /* Switch the display of W's cursor on or off, according to the value
21417 of ON. */
21418
21419 static void
21420 update_window_cursor (w, on)
21421 struct window *w;
21422 int on;
21423 {
21424 /* Don't update cursor in windows whose frame is in the process
21425 of being deleted. */
21426 if (w->current_matrix)
21427 {
21428 BLOCK_INPUT;
21429 display_and_set_cursor (w, on, w->phys_cursor.hpos, w->phys_cursor.vpos,
21430 w->phys_cursor.x, w->phys_cursor.y);
21431 UNBLOCK_INPUT;
21432 }
21433 }
21434
21435
21436 /* Call update_window_cursor with parameter ON_P on all leaf windows
21437 in the window tree rooted at W. */
21438
21439 static void
21440 update_cursor_in_window_tree (w, on_p)
21441 struct window *w;
21442 int on_p;
21443 {
21444 while (w)
21445 {
21446 if (!NILP (w->hchild))
21447 update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
21448 else if (!NILP (w->vchild))
21449 update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
21450 else
21451 update_window_cursor (w, on_p);
21452
21453 w = NILP (w->next) ? 0 : XWINDOW (w->next);
21454 }
21455 }
21456
21457
21458 /* EXPORT:
21459 Display the cursor on window W, or clear it, according to ON_P.
21460 Don't change the cursor's position. */
21461
21462 void
21463 x_update_cursor (f, on_p)
21464 struct frame *f;
21465 int on_p;
21466 {
21467 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
21468 }
21469
21470
21471 /* EXPORT:
21472 Clear the cursor of window W to background color, and mark the
21473 cursor as not shown. This is used when the text where the cursor
21474 is is about to be rewritten. */
21475
21476 void
21477 x_clear_cursor (w)
21478 struct window *w;
21479 {
21480 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
21481 update_window_cursor (w, 0);
21482 }
21483
21484
21485 /* EXPORT:
21486 Display the active region described by mouse_face_* according to DRAW. */
21487
21488 void
21489 show_mouse_face (dpyinfo, draw)
21490 Display_Info *dpyinfo;
21491 enum draw_glyphs_face draw;
21492 {
21493 struct window *w = XWINDOW (dpyinfo->mouse_face_window);
21494 struct frame *f = XFRAME (WINDOW_FRAME (w));
21495
21496 if (/* If window is in the process of being destroyed, don't bother
21497 to do anything. */
21498 w->current_matrix != NULL
21499 /* Don't update mouse highlight if hidden */
21500 && (draw != DRAW_MOUSE_FACE || !dpyinfo->mouse_face_hidden)
21501 /* Recognize when we are called to operate on rows that don't exist
21502 anymore. This can happen when a window is split. */
21503 && dpyinfo->mouse_face_end_row < w->current_matrix->nrows)
21504 {
21505 int phys_cursor_on_p = w->phys_cursor_on_p;
21506 struct glyph_row *row, *first, *last;
21507
21508 first = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_beg_row);
21509 last = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_end_row);
21510
21511 for (row = first; row <= last && row->enabled_p; ++row)
21512 {
21513 int start_hpos, end_hpos, start_x;
21514
21515 /* For all but the first row, the highlight starts at column 0. */
21516 if (row == first)
21517 {
21518 start_hpos = dpyinfo->mouse_face_beg_col;
21519 start_x = dpyinfo->mouse_face_beg_x;
21520 }
21521 else
21522 {
21523 start_hpos = 0;
21524 start_x = 0;
21525 }
21526
21527 if (row == last)
21528 end_hpos = dpyinfo->mouse_face_end_col;
21529 else
21530 {
21531 end_hpos = row->used[TEXT_AREA];
21532 if (draw == DRAW_NORMAL_TEXT)
21533 row->fill_line_p = 1; /* Clear to end of line */
21534 }
21535
21536 if (end_hpos > start_hpos)
21537 {
21538 draw_glyphs (w, start_x, row, TEXT_AREA,
21539 start_hpos, end_hpos,
21540 draw, 0);
21541
21542 row->mouse_face_p
21543 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
21544 }
21545 }
21546
21547 /* When we've written over the cursor, arrange for it to
21548 be displayed again. */
21549 if (phys_cursor_on_p && !w->phys_cursor_on_p)
21550 {
21551 BLOCK_INPUT;
21552 display_and_set_cursor (w, 1,
21553 w->phys_cursor.hpos, w->phys_cursor.vpos,
21554 w->phys_cursor.x, w->phys_cursor.y);
21555 UNBLOCK_INPUT;
21556 }
21557 }
21558
21559 /* Change the mouse cursor. */
21560 if (draw == DRAW_NORMAL_TEXT)
21561 rif->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
21562 else if (draw == DRAW_MOUSE_FACE)
21563 rif->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
21564 else
21565 rif->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
21566 }
21567
21568 /* EXPORT:
21569 Clear out the mouse-highlighted active region.
21570 Redraw it un-highlighted first. Value is non-zero if mouse
21571 face was actually drawn unhighlighted. */
21572
21573 int
21574 clear_mouse_face (dpyinfo)
21575 Display_Info *dpyinfo;
21576 {
21577 int cleared = 0;
21578
21579 if (!dpyinfo->mouse_face_hidden && !NILP (dpyinfo->mouse_face_window))
21580 {
21581 show_mouse_face (dpyinfo, DRAW_NORMAL_TEXT);
21582 cleared = 1;
21583 }
21584
21585 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
21586 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
21587 dpyinfo->mouse_face_window = Qnil;
21588 dpyinfo->mouse_face_overlay = Qnil;
21589 return cleared;
21590 }
21591
21592
21593 /* EXPORT:
21594 Non-zero if physical cursor of window W is within mouse face. */
21595
21596 int
21597 cursor_in_mouse_face_p (w)
21598 struct window *w;
21599 {
21600 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
21601 int in_mouse_face = 0;
21602
21603 if (WINDOWP (dpyinfo->mouse_face_window)
21604 && XWINDOW (dpyinfo->mouse_face_window) == w)
21605 {
21606 int hpos = w->phys_cursor.hpos;
21607 int vpos = w->phys_cursor.vpos;
21608
21609 if (vpos >= dpyinfo->mouse_face_beg_row
21610 && vpos <= dpyinfo->mouse_face_end_row
21611 && (vpos > dpyinfo->mouse_face_beg_row
21612 || hpos >= dpyinfo->mouse_face_beg_col)
21613 && (vpos < dpyinfo->mouse_face_end_row
21614 || hpos < dpyinfo->mouse_face_end_col
21615 || dpyinfo->mouse_face_past_end))
21616 in_mouse_face = 1;
21617 }
21618
21619 return in_mouse_face;
21620 }
21621
21622
21623
21624 \f
21625 /* Find the glyph matrix position of buffer position CHARPOS in window
21626 *W. HPOS, *VPOS, *X, and *Y are set to the positions found. W's
21627 current glyphs must be up to date. If CHARPOS is above window
21628 start return (0, 0, 0, 0). If CHARPOS is after end of W, return end
21629 of last line in W. In the row containing CHARPOS, stop before glyphs
21630 having STOP as object. */
21631
21632 #if 1 /* This is a version of fast_find_position that's more correct
21633 in the presence of hscrolling, for example. I didn't install
21634 it right away because the problem fixed is minor, it failed
21635 in 20.x as well, and I think it's too risky to install
21636 so near the release of 21.1. 2001-09-25 gerd. */
21637
21638 static int
21639 fast_find_position (w, charpos, hpos, vpos, x, y, stop)
21640 struct window *w;
21641 int charpos;
21642 int *hpos, *vpos, *x, *y;
21643 Lisp_Object stop;
21644 {
21645 struct glyph_row *row, *first;
21646 struct glyph *glyph, *end;
21647 int past_end = 0;
21648
21649 first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
21650 if (charpos < MATRIX_ROW_START_CHARPOS (first))
21651 {
21652 *x = first->x;
21653 *y = first->y;
21654 *hpos = 0;
21655 *vpos = MATRIX_ROW_VPOS (first, w->current_matrix);
21656 return 1;
21657 }
21658
21659 row = row_containing_pos (w, charpos, first, NULL, 0);
21660 if (row == NULL)
21661 {
21662 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
21663 past_end = 1;
21664 }
21665
21666 /* If whole rows or last part of a row came from a display overlay,
21667 row_containing_pos will skip over such rows because their end pos
21668 equals the start pos of the overlay or interval.
21669
21670 Move back if we have a STOP object and previous row's
21671 end glyph came from STOP. */
21672 if (!NILP (stop))
21673 {
21674 struct glyph_row *prev;
21675 while ((prev = row - 1, prev >= first)
21676 && MATRIX_ROW_END_CHARPOS (prev) == charpos
21677 && prev->used[TEXT_AREA] > 0)
21678 {
21679 struct glyph *beg = prev->glyphs[TEXT_AREA];
21680 glyph = beg + prev->used[TEXT_AREA];
21681 while (--glyph >= beg
21682 && INTEGERP (glyph->object));
21683 if (glyph < beg
21684 || !EQ (stop, glyph->object))
21685 break;
21686 row = prev;
21687 }
21688 }
21689
21690 *x = row->x;
21691 *y = row->y;
21692 *vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
21693
21694 glyph = row->glyphs[TEXT_AREA];
21695 end = glyph + row->used[TEXT_AREA];
21696
21697 /* Skip over glyphs not having an object at the start of the row.
21698 These are special glyphs like truncation marks on terminal
21699 frames. */
21700 if (row->displays_text_p)
21701 while (glyph < end
21702 && INTEGERP (glyph->object)
21703 && !EQ (stop, glyph->object)
21704 && glyph->charpos < 0)
21705 {
21706 *x += glyph->pixel_width;
21707 ++glyph;
21708 }
21709
21710 while (glyph < end
21711 && !INTEGERP (glyph->object)
21712 && !EQ (stop, glyph->object)
21713 && (!BUFFERP (glyph->object)
21714 || glyph->charpos < charpos))
21715 {
21716 *x += glyph->pixel_width;
21717 ++glyph;
21718 }
21719
21720 *hpos = glyph - row->glyphs[TEXT_AREA];
21721 return !past_end;
21722 }
21723
21724 #else /* not 1 */
21725
21726 static int
21727 fast_find_position (w, pos, hpos, vpos, x, y, stop)
21728 struct window *w;
21729 int pos;
21730 int *hpos, *vpos, *x, *y;
21731 Lisp_Object stop;
21732 {
21733 int i;
21734 int lastcol;
21735 int maybe_next_line_p = 0;
21736 int line_start_position;
21737 int yb = window_text_bottom_y (w);
21738 struct glyph_row *row, *best_row;
21739 int row_vpos, best_row_vpos;
21740 int current_x;
21741
21742 row = best_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
21743 row_vpos = best_row_vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
21744
21745 while (row->y < yb)
21746 {
21747 if (row->used[TEXT_AREA])
21748 line_start_position = row->glyphs[TEXT_AREA]->charpos;
21749 else
21750 line_start_position = 0;
21751
21752 if (line_start_position > pos)
21753 break;
21754 /* If the position sought is the end of the buffer,
21755 don't include the blank lines at the bottom of the window. */
21756 else if (line_start_position == pos
21757 && pos == BUF_ZV (XBUFFER (w->buffer)))
21758 {
21759 maybe_next_line_p = 1;
21760 break;
21761 }
21762 else if (line_start_position > 0)
21763 {
21764 best_row = row;
21765 best_row_vpos = row_vpos;
21766 }
21767
21768 if (row->y + row->height >= yb)
21769 break;
21770
21771 ++row;
21772 ++row_vpos;
21773 }
21774
21775 /* Find the right column within BEST_ROW. */
21776 lastcol = 0;
21777 current_x = best_row->x;
21778 for (i = 0; i < best_row->used[TEXT_AREA]; i++)
21779 {
21780 struct glyph *glyph = best_row->glyphs[TEXT_AREA] + i;
21781 int charpos = glyph->charpos;
21782
21783 if (BUFFERP (glyph->object))
21784 {
21785 if (charpos == pos)
21786 {
21787 *hpos = i;
21788 *vpos = best_row_vpos;
21789 *x = current_x;
21790 *y = best_row->y;
21791 return 1;
21792 }
21793 else if (charpos > pos)
21794 break;
21795 }
21796 else if (EQ (glyph->object, stop))
21797 break;
21798
21799 if (charpos > 0)
21800 lastcol = i;
21801 current_x += glyph->pixel_width;
21802 }
21803
21804 /* If we're looking for the end of the buffer,
21805 and we didn't find it in the line we scanned,
21806 use the start of the following line. */
21807 if (maybe_next_line_p)
21808 {
21809 ++best_row;
21810 ++best_row_vpos;
21811 lastcol = 0;
21812 current_x = best_row->x;
21813 }
21814
21815 *vpos = best_row_vpos;
21816 *hpos = lastcol + 1;
21817 *x = current_x;
21818 *y = best_row->y;
21819 return 0;
21820 }
21821
21822 #endif /* not 1 */
21823
21824
21825 /* Find the position of the glyph for position POS in OBJECT in
21826 window W's current matrix, and return in *X, *Y the pixel
21827 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
21828
21829 RIGHT_P non-zero means return the position of the right edge of the
21830 glyph, RIGHT_P zero means return the left edge position.
21831
21832 If no glyph for POS exists in the matrix, return the position of
21833 the glyph with the next smaller position that is in the matrix, if
21834 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
21835 exists in the matrix, return the position of the glyph with the
21836 next larger position in OBJECT.
21837
21838 Value is non-zero if a glyph was found. */
21839
21840 static int
21841 fast_find_string_pos (w, pos, object, hpos, vpos, x, y, right_p)
21842 struct window *w;
21843 int pos;
21844 Lisp_Object object;
21845 int *hpos, *vpos, *x, *y;
21846 int right_p;
21847 {
21848 int yb = window_text_bottom_y (w);
21849 struct glyph_row *r;
21850 struct glyph *best_glyph = NULL;
21851 struct glyph_row *best_row = NULL;
21852 int best_x = 0;
21853
21854 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
21855 r->enabled_p && r->y < yb;
21856 ++r)
21857 {
21858 struct glyph *g = r->glyphs[TEXT_AREA];
21859 struct glyph *e = g + r->used[TEXT_AREA];
21860 int gx;
21861
21862 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
21863 if (EQ (g->object, object))
21864 {
21865 if (g->charpos == pos)
21866 {
21867 best_glyph = g;
21868 best_x = gx;
21869 best_row = r;
21870 goto found;
21871 }
21872 else if (best_glyph == NULL
21873 || ((abs (g->charpos - pos)
21874 < abs (best_glyph->charpos - pos))
21875 && (right_p
21876 ? g->charpos < pos
21877 : g->charpos > pos)))
21878 {
21879 best_glyph = g;
21880 best_x = gx;
21881 best_row = r;
21882 }
21883 }
21884 }
21885
21886 found:
21887
21888 if (best_glyph)
21889 {
21890 *x = best_x;
21891 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
21892
21893 if (right_p)
21894 {
21895 *x += best_glyph->pixel_width;
21896 ++*hpos;
21897 }
21898
21899 *y = best_row->y;
21900 *vpos = best_row - w->current_matrix->rows;
21901 }
21902
21903 return best_glyph != NULL;
21904 }
21905
21906
21907 /* See if position X, Y is within a hot-spot of an image. */
21908
21909 static int
21910 on_hot_spot_p (hot_spot, x, y)
21911 Lisp_Object hot_spot;
21912 int x, y;
21913 {
21914 if (!CONSP (hot_spot))
21915 return 0;
21916
21917 if (EQ (XCAR (hot_spot), Qrect))
21918 {
21919 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
21920 Lisp_Object rect = XCDR (hot_spot);
21921 Lisp_Object tem;
21922 if (!CONSP (rect))
21923 return 0;
21924 if (!CONSP (XCAR (rect)))
21925 return 0;
21926 if (!CONSP (XCDR (rect)))
21927 return 0;
21928 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
21929 return 0;
21930 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
21931 return 0;
21932 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
21933 return 0;
21934 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
21935 return 0;
21936 return 1;
21937 }
21938 else if (EQ (XCAR (hot_spot), Qcircle))
21939 {
21940 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
21941 Lisp_Object circ = XCDR (hot_spot);
21942 Lisp_Object lr, lx0, ly0;
21943 if (CONSP (circ)
21944 && CONSP (XCAR (circ))
21945 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
21946 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
21947 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
21948 {
21949 double r = XFLOATINT (lr);
21950 double dx = XINT (lx0) - x;
21951 double dy = XINT (ly0) - y;
21952 return (dx * dx + dy * dy <= r * r);
21953 }
21954 }
21955 else if (EQ (XCAR (hot_spot), Qpoly))
21956 {
21957 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
21958 if (VECTORP (XCDR (hot_spot)))
21959 {
21960 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
21961 Lisp_Object *poly = v->contents;
21962 int n = v->size;
21963 int i;
21964 int inside = 0;
21965 Lisp_Object lx, ly;
21966 int x0, y0;
21967
21968 /* Need an even number of coordinates, and at least 3 edges. */
21969 if (n < 6 || n & 1)
21970 return 0;
21971
21972 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
21973 If count is odd, we are inside polygon. Pixels on edges
21974 may or may not be included depending on actual geometry of the
21975 polygon. */
21976 if ((lx = poly[n-2], !INTEGERP (lx))
21977 || (ly = poly[n-1], !INTEGERP (lx)))
21978 return 0;
21979 x0 = XINT (lx), y0 = XINT (ly);
21980 for (i = 0; i < n; i += 2)
21981 {
21982 int x1 = x0, y1 = y0;
21983 if ((lx = poly[i], !INTEGERP (lx))
21984 || (ly = poly[i+1], !INTEGERP (ly)))
21985 return 0;
21986 x0 = XINT (lx), y0 = XINT (ly);
21987
21988 /* Does this segment cross the X line? */
21989 if (x0 >= x)
21990 {
21991 if (x1 >= x)
21992 continue;
21993 }
21994 else if (x1 < x)
21995 continue;
21996 if (y > y0 && y > y1)
21997 continue;
21998 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
21999 inside = !inside;
22000 }
22001 return inside;
22002 }
22003 }
22004 /* If we don't understand the format, pretend we're not in the hot-spot. */
22005 return 0;
22006 }
22007
22008 Lisp_Object
22009 find_hot_spot (map, x, y)
22010 Lisp_Object map;
22011 int x, y;
22012 {
22013 while (CONSP (map))
22014 {
22015 if (CONSP (XCAR (map))
22016 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
22017 return XCAR (map);
22018 map = XCDR (map);
22019 }
22020
22021 return Qnil;
22022 }
22023
22024 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
22025 3, 3, 0,
22026 doc: /* Lookup in image map MAP coordinates X and Y.
22027 An image map is an alist where each element has the format (AREA ID PLIST).
22028 An AREA is specified as either a rectangle, a circle, or a polygon:
22029 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
22030 pixel coordinates of the upper left and bottom right corners.
22031 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
22032 and the radius of the circle; r may be a float or integer.
22033 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
22034 vector describes one corner in the polygon.
22035 Returns the alist element for the first matching AREA in MAP. */)
22036 (map, x, y)
22037 Lisp_Object map;
22038 Lisp_Object x, y;
22039 {
22040 if (NILP (map))
22041 return Qnil;
22042
22043 CHECK_NUMBER (x);
22044 CHECK_NUMBER (y);
22045
22046 return find_hot_spot (map, XINT (x), XINT (y));
22047 }
22048
22049
22050 /* Display frame CURSOR, optionally using shape defined by POINTER. */
22051 static void
22052 define_frame_cursor1 (f, cursor, pointer)
22053 struct frame *f;
22054 Cursor cursor;
22055 Lisp_Object pointer;
22056 {
22057 /* Do not change cursor shape while dragging mouse. */
22058 if (!NILP (do_mouse_tracking))
22059 return;
22060
22061 if (!NILP (pointer))
22062 {
22063 if (EQ (pointer, Qarrow))
22064 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
22065 else if (EQ (pointer, Qhand))
22066 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
22067 else if (EQ (pointer, Qtext))
22068 cursor = FRAME_X_OUTPUT (f)->text_cursor;
22069 else if (EQ (pointer, intern ("hdrag")))
22070 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
22071 #ifdef HAVE_X_WINDOWS
22072 else if (EQ (pointer, intern ("vdrag")))
22073 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
22074 #endif
22075 else if (EQ (pointer, intern ("hourglass")))
22076 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
22077 else if (EQ (pointer, Qmodeline))
22078 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
22079 else
22080 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
22081 }
22082
22083 if (cursor != No_Cursor)
22084 rif->define_frame_cursor (f, cursor);
22085 }
22086
22087 /* Take proper action when mouse has moved to the mode or header line
22088 or marginal area AREA of window W, x-position X and y-position Y.
22089 X is relative to the start of the text display area of W, so the
22090 width of bitmap areas and scroll bars must be subtracted to get a
22091 position relative to the start of the mode line. */
22092
22093 static void
22094 note_mode_line_or_margin_highlight (window, x, y, area)
22095 Lisp_Object window;
22096 int x, y;
22097 enum window_part area;
22098 {
22099 struct window *w = XWINDOW (window);
22100 struct frame *f = XFRAME (w->frame);
22101 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
22102 Cursor cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
22103 Lisp_Object pointer = Qnil;
22104 int charpos, dx, dy, width, height;
22105 Lisp_Object string, object = Qnil;
22106 Lisp_Object pos, help;
22107
22108 Lisp_Object mouse_face;
22109 int original_x_pixel = x;
22110 struct glyph * glyph = NULL;
22111 struct glyph_row *row;
22112
22113 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
22114 {
22115 int x0;
22116 struct glyph *end;
22117
22118 string = mode_line_string (w, area, &x, &y, &charpos,
22119 &object, &dx, &dy, &width, &height);
22120
22121 row = (area == ON_MODE_LINE
22122 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
22123 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
22124
22125 /* Find glyph */
22126 if (row->mode_line_p && row->enabled_p)
22127 {
22128 glyph = row->glyphs[TEXT_AREA];
22129 end = glyph + row->used[TEXT_AREA];
22130
22131 for (x0 = original_x_pixel;
22132 glyph < end && x0 >= glyph->pixel_width;
22133 ++glyph)
22134 x0 -= glyph->pixel_width;
22135
22136 if (glyph >= end)
22137 glyph = NULL;
22138 }
22139 }
22140 else
22141 {
22142 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
22143 string = marginal_area_string (w, area, &x, &y, &charpos,
22144 &object, &dx, &dy, &width, &height);
22145 }
22146
22147 help = Qnil;
22148
22149 if (IMAGEP (object))
22150 {
22151 Lisp_Object image_map, hotspot;
22152 if ((image_map = Fplist_get (XCDR (object), QCmap),
22153 !NILP (image_map))
22154 && (hotspot = find_hot_spot (image_map, dx, dy),
22155 CONSP (hotspot))
22156 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
22157 {
22158 Lisp_Object area_id, plist;
22159
22160 area_id = XCAR (hotspot);
22161 /* Could check AREA_ID to see if we enter/leave this hot-spot.
22162 If so, we could look for mouse-enter, mouse-leave
22163 properties in PLIST (and do something...). */
22164 hotspot = XCDR (hotspot);
22165 if (CONSP (hotspot)
22166 && (plist = XCAR (hotspot), CONSP (plist)))
22167 {
22168 pointer = Fplist_get (plist, Qpointer);
22169 if (NILP (pointer))
22170 pointer = Qhand;
22171 help = Fplist_get (plist, Qhelp_echo);
22172 if (!NILP (help))
22173 {
22174 help_echo_string = help;
22175 /* Is this correct? ++kfs */
22176 XSETWINDOW (help_echo_window, w);
22177 help_echo_object = w->buffer;
22178 help_echo_pos = charpos;
22179 }
22180 }
22181 }
22182 if (NILP (pointer))
22183 pointer = Fplist_get (XCDR (object), QCpointer);
22184 }
22185
22186 if (STRINGP (string))
22187 {
22188 pos = make_number (charpos);
22189 /* If we're on a string with `help-echo' text property, arrange
22190 for the help to be displayed. This is done by setting the
22191 global variable help_echo_string to the help string. */
22192 if (NILP (help))
22193 {
22194 help = Fget_text_property (pos, Qhelp_echo, string);
22195 if (!NILP (help))
22196 {
22197 help_echo_string = help;
22198 XSETWINDOW (help_echo_window, w);
22199 help_echo_object = string;
22200 help_echo_pos = charpos;
22201 }
22202 }
22203
22204 if (NILP (pointer))
22205 pointer = Fget_text_property (pos, Qpointer, string);
22206
22207 /* Change the mouse pointer according to what is under X/Y. */
22208 if (NILP (pointer) && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
22209 {
22210 Lisp_Object map;
22211 map = Fget_text_property (pos, Qlocal_map, string);
22212 if (!KEYMAPP (map))
22213 map = Fget_text_property (pos, Qkeymap, string);
22214 if (!KEYMAPP (map))
22215 cursor = dpyinfo->vertical_scroll_bar_cursor;
22216 }
22217
22218 /* Change the mouse face according to what is under X/Y. */
22219 mouse_face = Fget_text_property (pos, Qmouse_face, string);
22220 if (!NILP (mouse_face)
22221 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
22222 && glyph)
22223 {
22224 Lisp_Object b, e;
22225
22226 struct glyph * tmp_glyph;
22227
22228 int gpos;
22229 int gseq_length;
22230 int total_pixel_width;
22231 int ignore;
22232
22233 int vpos, hpos;
22234
22235 b = Fprevious_single_property_change (make_number (charpos + 1),
22236 Qmouse_face, string, Qnil);
22237 if (NILP (b))
22238 b = make_number (0);
22239
22240 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
22241 if (NILP (e))
22242 e = make_number (SCHARS (string));
22243
22244 /* Calculate the position(glyph position: GPOS) of GLYPH in
22245 displayed string. GPOS is different from CHARPOS.
22246
22247 CHARPOS is the position of glyph in internal string
22248 object. A mode line string format has structures which
22249 is converted to a flatten by emacs lisp interpreter.
22250 The internal string is an element of the structures.
22251 The displayed string is the flatten string. */
22252 for (tmp_glyph = glyph - 1, gpos = 0;
22253 tmp_glyph->charpos >= XINT (b);
22254 tmp_glyph--, gpos++)
22255 {
22256 if (!EQ (tmp_glyph->object, glyph->object))
22257 break;
22258 }
22259
22260 /* Calculate the lenght(glyph sequence length: GSEQ_LENGTH) of
22261 displayed string holding GLYPH.
22262
22263 GSEQ_LENGTH is different from SCHARS (STRING).
22264 SCHARS (STRING) returns the length of the internal string. */
22265 for (tmp_glyph = glyph, gseq_length = gpos;
22266 tmp_glyph->charpos < XINT (e);
22267 tmp_glyph++, gseq_length++)
22268 {
22269 if (!EQ (tmp_glyph->object, glyph->object))
22270 break;
22271 }
22272
22273 total_pixel_width = 0;
22274 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
22275 total_pixel_width += tmp_glyph->pixel_width;
22276
22277 /* Pre calculation of re-rendering position */
22278 vpos = (x - gpos);
22279 hpos = (area == ON_MODE_LINE
22280 ? (w->current_matrix)->nrows - 1
22281 : 0);
22282
22283 /* If the re-rendering position is included in the last
22284 re-rendering area, we should do nothing. */
22285 if ( EQ (window, dpyinfo->mouse_face_window)
22286 && dpyinfo->mouse_face_beg_col <= vpos
22287 && vpos < dpyinfo->mouse_face_end_col
22288 && dpyinfo->mouse_face_beg_row == hpos )
22289 return;
22290
22291 if (clear_mouse_face (dpyinfo))
22292 cursor = No_Cursor;
22293
22294 dpyinfo->mouse_face_beg_col = vpos;
22295 dpyinfo->mouse_face_beg_row = hpos;
22296
22297 dpyinfo->mouse_face_beg_x = original_x_pixel - (total_pixel_width + dx);
22298 dpyinfo->mouse_face_beg_y = 0;
22299
22300 dpyinfo->mouse_face_end_col = vpos + gseq_length;
22301 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_beg_row;
22302
22303 dpyinfo->mouse_face_end_x = 0;
22304 dpyinfo->mouse_face_end_y = 0;
22305
22306 dpyinfo->mouse_face_past_end = 0;
22307 dpyinfo->mouse_face_window = window;
22308
22309 dpyinfo->mouse_face_face_id = face_at_string_position (w, string,
22310 charpos,
22311 0, 0, 0, &ignore,
22312 glyph->face_id, 1);
22313 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
22314
22315 if (NILP (pointer))
22316 pointer = Qhand;
22317 }
22318 else if ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
22319 clear_mouse_face (dpyinfo);
22320 }
22321 define_frame_cursor1 (f, cursor, pointer);
22322 }
22323
22324
22325 /* EXPORT:
22326 Take proper action when the mouse has moved to position X, Y on
22327 frame F as regards highlighting characters that have mouse-face
22328 properties. Also de-highlighting chars where the mouse was before.
22329 X and Y can be negative or out of range. */
22330
22331 void
22332 note_mouse_highlight (f, x, y)
22333 struct frame *f;
22334 int x, y;
22335 {
22336 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
22337 enum window_part part;
22338 Lisp_Object window;
22339 struct window *w;
22340 Cursor cursor = No_Cursor;
22341 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
22342 struct buffer *b;
22343
22344 /* When a menu is active, don't highlight because this looks odd. */
22345 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NTGUI)
22346 if (popup_activated ())
22347 return;
22348 #endif
22349
22350 if (NILP (Vmouse_highlight)
22351 || !f->glyphs_initialized_p)
22352 return;
22353
22354 dpyinfo->mouse_face_mouse_x = x;
22355 dpyinfo->mouse_face_mouse_y = y;
22356 dpyinfo->mouse_face_mouse_frame = f;
22357
22358 if (dpyinfo->mouse_face_defer)
22359 return;
22360
22361 if (gc_in_progress)
22362 {
22363 dpyinfo->mouse_face_deferred_gc = 1;
22364 return;
22365 }
22366
22367 /* Which window is that in? */
22368 window = window_from_coordinates (f, x, y, &part, 0, 0, 1);
22369
22370 /* If we were displaying active text in another window, clear that.
22371 Also clear if we move out of text area in same window. */
22372 if (! EQ (window, dpyinfo->mouse_face_window)
22373 || (part != ON_TEXT && part != ON_MODE_LINE && part != ON_HEADER_LINE
22374 && !NILP (dpyinfo->mouse_face_window)))
22375 clear_mouse_face (dpyinfo);
22376
22377 /* Not on a window -> return. */
22378 if (!WINDOWP (window))
22379 return;
22380
22381 /* Reset help_echo_string. It will get recomputed below. */
22382 help_echo_string = Qnil;
22383
22384 /* Convert to window-relative pixel coordinates. */
22385 w = XWINDOW (window);
22386 frame_to_window_pixel_xy (w, &x, &y);
22387
22388 /* Handle tool-bar window differently since it doesn't display a
22389 buffer. */
22390 if (EQ (window, f->tool_bar_window))
22391 {
22392 note_tool_bar_highlight (f, x, y);
22393 return;
22394 }
22395
22396 /* Mouse is on the mode, header line or margin? */
22397 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
22398 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
22399 {
22400 note_mode_line_or_margin_highlight (window, x, y, part);
22401 return;
22402 }
22403
22404 if (part == ON_VERTICAL_BORDER)
22405 {
22406 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
22407 help_echo_string = build_string ("drag-mouse-1: resize");
22408 }
22409 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
22410 || part == ON_SCROLL_BAR)
22411 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
22412 else
22413 cursor = FRAME_X_OUTPUT (f)->text_cursor;
22414
22415 /* Are we in a window whose display is up to date?
22416 And verify the buffer's text has not changed. */
22417 b = XBUFFER (w->buffer);
22418 if (part == ON_TEXT
22419 && EQ (w->window_end_valid, w->buffer)
22420 && XFASTINT (w->last_modified) == BUF_MODIFF (b)
22421 && XFASTINT (w->last_overlay_modified) == BUF_OVERLAY_MODIFF (b))
22422 {
22423 int hpos, vpos, pos, i, dx, dy, area;
22424 struct glyph *glyph;
22425 Lisp_Object object;
22426 Lisp_Object mouse_face = Qnil, overlay = Qnil, position;
22427 Lisp_Object *overlay_vec = NULL;
22428 int noverlays;
22429 struct buffer *obuf;
22430 int obegv, ozv, same_region;
22431
22432 /* Find the glyph under X/Y. */
22433 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
22434
22435 /* Look for :pointer property on image. */
22436 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
22437 {
22438 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
22439 if (img != NULL && IMAGEP (img->spec))
22440 {
22441 Lisp_Object image_map, hotspot;
22442 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
22443 !NILP (image_map))
22444 && (hotspot = find_hot_spot (image_map,
22445 glyph->slice.x + dx,
22446 glyph->slice.y + dy),
22447 CONSP (hotspot))
22448 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
22449 {
22450 Lisp_Object area_id, plist;
22451
22452 area_id = XCAR (hotspot);
22453 /* Could check AREA_ID to see if we enter/leave this hot-spot.
22454 If so, we could look for mouse-enter, mouse-leave
22455 properties in PLIST (and do something...). */
22456 hotspot = XCDR (hotspot);
22457 if (CONSP (hotspot)
22458 && (plist = XCAR (hotspot), CONSP (plist)))
22459 {
22460 pointer = Fplist_get (plist, Qpointer);
22461 if (NILP (pointer))
22462 pointer = Qhand;
22463 help_echo_string = Fplist_get (plist, Qhelp_echo);
22464 if (!NILP (help_echo_string))
22465 {
22466 help_echo_window = window;
22467 help_echo_object = glyph->object;
22468 help_echo_pos = glyph->charpos;
22469 }
22470 }
22471 }
22472 if (NILP (pointer))
22473 pointer = Fplist_get (XCDR (img->spec), QCpointer);
22474 }
22475 }
22476
22477 /* Clear mouse face if X/Y not over text. */
22478 if (glyph == NULL
22479 || area != TEXT_AREA
22480 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p)
22481 {
22482 if (clear_mouse_face (dpyinfo))
22483 cursor = No_Cursor;
22484 if (NILP (pointer))
22485 {
22486 if (area != TEXT_AREA)
22487 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
22488 else
22489 pointer = Vvoid_text_area_pointer;
22490 }
22491 goto set_cursor;
22492 }
22493
22494 pos = glyph->charpos;
22495 object = glyph->object;
22496 if (!STRINGP (object) && !BUFFERP (object))
22497 goto set_cursor;
22498
22499 /* If we get an out-of-range value, return now; avoid an error. */
22500 if (BUFFERP (object) && pos > BUF_Z (b))
22501 goto set_cursor;
22502
22503 /* Make the window's buffer temporarily current for
22504 overlays_at and compute_char_face. */
22505 obuf = current_buffer;
22506 current_buffer = b;
22507 obegv = BEGV;
22508 ozv = ZV;
22509 BEGV = BEG;
22510 ZV = Z;
22511
22512 /* Is this char mouse-active or does it have help-echo? */
22513 position = make_number (pos);
22514
22515 if (BUFFERP (object))
22516 {
22517 /* Put all the overlays we want in a vector in overlay_vec. */
22518 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
22519 /* Sort overlays into increasing priority order. */
22520 noverlays = sort_overlays (overlay_vec, noverlays, w);
22521 }
22522 else
22523 noverlays = 0;
22524
22525 same_region = (EQ (window, dpyinfo->mouse_face_window)
22526 && vpos >= dpyinfo->mouse_face_beg_row
22527 && vpos <= dpyinfo->mouse_face_end_row
22528 && (vpos > dpyinfo->mouse_face_beg_row
22529 || hpos >= dpyinfo->mouse_face_beg_col)
22530 && (vpos < dpyinfo->mouse_face_end_row
22531 || hpos < dpyinfo->mouse_face_end_col
22532 || dpyinfo->mouse_face_past_end));
22533
22534 if (same_region)
22535 cursor = No_Cursor;
22536
22537 /* Check mouse-face highlighting. */
22538 if (! same_region
22539 /* If there exists an overlay with mouse-face overlapping
22540 the one we are currently highlighting, we have to
22541 check if we enter the overlapping overlay, and then
22542 highlight only that. */
22543 || (OVERLAYP (dpyinfo->mouse_face_overlay)
22544 && mouse_face_overlay_overlaps (dpyinfo->mouse_face_overlay)))
22545 {
22546 /* Find the highest priority overlay that has a mouse-face
22547 property. */
22548 overlay = Qnil;
22549 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
22550 {
22551 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
22552 if (!NILP (mouse_face))
22553 overlay = overlay_vec[i];
22554 }
22555
22556 /* If we're actually highlighting the same overlay as
22557 before, there's no need to do that again. */
22558 if (!NILP (overlay)
22559 && EQ (overlay, dpyinfo->mouse_face_overlay))
22560 goto check_help_echo;
22561
22562 dpyinfo->mouse_face_overlay = overlay;
22563
22564 /* Clear the display of the old active region, if any. */
22565 if (clear_mouse_face (dpyinfo))
22566 cursor = No_Cursor;
22567
22568 /* If no overlay applies, get a text property. */
22569 if (NILP (overlay))
22570 mouse_face = Fget_text_property (position, Qmouse_face, object);
22571
22572 /* Handle the overlay case. */
22573 if (!NILP (overlay))
22574 {
22575 /* Find the range of text around this char that
22576 should be active. */
22577 Lisp_Object before, after;
22578 int ignore;
22579
22580 before = Foverlay_start (overlay);
22581 after = Foverlay_end (overlay);
22582 /* Record this as the current active region. */
22583 fast_find_position (w, XFASTINT (before),
22584 &dpyinfo->mouse_face_beg_col,
22585 &dpyinfo->mouse_face_beg_row,
22586 &dpyinfo->mouse_face_beg_x,
22587 &dpyinfo->mouse_face_beg_y, Qnil);
22588
22589 dpyinfo->mouse_face_past_end
22590 = !fast_find_position (w, XFASTINT (after),
22591 &dpyinfo->mouse_face_end_col,
22592 &dpyinfo->mouse_face_end_row,
22593 &dpyinfo->mouse_face_end_x,
22594 &dpyinfo->mouse_face_end_y, Qnil);
22595 dpyinfo->mouse_face_window = window;
22596
22597 dpyinfo->mouse_face_face_id
22598 = face_at_buffer_position (w, pos, 0, 0,
22599 &ignore, pos + 1,
22600 !dpyinfo->mouse_face_hidden);
22601
22602 /* Display it as active. */
22603 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
22604 cursor = No_Cursor;
22605 }
22606 /* Handle the text property case. */
22607 else if (!NILP (mouse_face) && BUFFERP (object))
22608 {
22609 /* Find the range of text around this char that
22610 should be active. */
22611 Lisp_Object before, after, beginning, end;
22612 int ignore;
22613
22614 beginning = Fmarker_position (w->start);
22615 end = make_number (BUF_Z (XBUFFER (object))
22616 - XFASTINT (w->window_end_pos));
22617 before
22618 = Fprevious_single_property_change (make_number (pos + 1),
22619 Qmouse_face,
22620 object, beginning);
22621 after
22622 = Fnext_single_property_change (position, Qmouse_face,
22623 object, end);
22624
22625 /* Record this as the current active region. */
22626 fast_find_position (w, XFASTINT (before),
22627 &dpyinfo->mouse_face_beg_col,
22628 &dpyinfo->mouse_face_beg_row,
22629 &dpyinfo->mouse_face_beg_x,
22630 &dpyinfo->mouse_face_beg_y, Qnil);
22631 dpyinfo->mouse_face_past_end
22632 = !fast_find_position (w, XFASTINT (after),
22633 &dpyinfo->mouse_face_end_col,
22634 &dpyinfo->mouse_face_end_row,
22635 &dpyinfo->mouse_face_end_x,
22636 &dpyinfo->mouse_face_end_y, Qnil);
22637 dpyinfo->mouse_face_window = window;
22638
22639 if (BUFFERP (object))
22640 dpyinfo->mouse_face_face_id
22641 = face_at_buffer_position (w, pos, 0, 0,
22642 &ignore, pos + 1,
22643 !dpyinfo->mouse_face_hidden);
22644
22645 /* Display it as active. */
22646 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
22647 cursor = No_Cursor;
22648 }
22649 else if (!NILP (mouse_face) && STRINGP (object))
22650 {
22651 Lisp_Object b, e;
22652 int ignore;
22653
22654 b = Fprevious_single_property_change (make_number (pos + 1),
22655 Qmouse_face,
22656 object, Qnil);
22657 e = Fnext_single_property_change (position, Qmouse_face,
22658 object, Qnil);
22659 if (NILP (b))
22660 b = make_number (0);
22661 if (NILP (e))
22662 e = make_number (SCHARS (object) - 1);
22663
22664 fast_find_string_pos (w, XINT (b), object,
22665 &dpyinfo->mouse_face_beg_col,
22666 &dpyinfo->mouse_face_beg_row,
22667 &dpyinfo->mouse_face_beg_x,
22668 &dpyinfo->mouse_face_beg_y, 0);
22669 fast_find_string_pos (w, XINT (e), object,
22670 &dpyinfo->mouse_face_end_col,
22671 &dpyinfo->mouse_face_end_row,
22672 &dpyinfo->mouse_face_end_x,
22673 &dpyinfo->mouse_face_end_y, 1);
22674 dpyinfo->mouse_face_past_end = 0;
22675 dpyinfo->mouse_face_window = window;
22676 dpyinfo->mouse_face_face_id
22677 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
22678 glyph->face_id, 1);
22679 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
22680 cursor = No_Cursor;
22681 }
22682 else if (STRINGP (object) && NILP (mouse_face))
22683 {
22684 /* A string which doesn't have mouse-face, but
22685 the text ``under'' it might have. */
22686 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
22687 int start = MATRIX_ROW_START_CHARPOS (r);
22688
22689 pos = string_buffer_position (w, object, start);
22690 if (pos > 0)
22691 mouse_face = get_char_property_and_overlay (make_number (pos),
22692 Qmouse_face,
22693 w->buffer,
22694 &overlay);
22695 if (!NILP (mouse_face) && !NILP (overlay))
22696 {
22697 Lisp_Object before = Foverlay_start (overlay);
22698 Lisp_Object after = Foverlay_end (overlay);
22699 int ignore;
22700
22701 /* Note that we might not be able to find position
22702 BEFORE in the glyph matrix if the overlay is
22703 entirely covered by a `display' property. In
22704 this case, we overshoot. So let's stop in
22705 the glyph matrix before glyphs for OBJECT. */
22706 fast_find_position (w, XFASTINT (before),
22707 &dpyinfo->mouse_face_beg_col,
22708 &dpyinfo->mouse_face_beg_row,
22709 &dpyinfo->mouse_face_beg_x,
22710 &dpyinfo->mouse_face_beg_y,
22711 object);
22712
22713 dpyinfo->mouse_face_past_end
22714 = !fast_find_position (w, XFASTINT (after),
22715 &dpyinfo->mouse_face_end_col,
22716 &dpyinfo->mouse_face_end_row,
22717 &dpyinfo->mouse_face_end_x,
22718 &dpyinfo->mouse_face_end_y,
22719 Qnil);
22720 dpyinfo->mouse_face_window = window;
22721 dpyinfo->mouse_face_face_id
22722 = face_at_buffer_position (w, pos, 0, 0,
22723 &ignore, pos + 1,
22724 !dpyinfo->mouse_face_hidden);
22725
22726 /* Display it as active. */
22727 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
22728 cursor = No_Cursor;
22729 }
22730 }
22731 }
22732
22733 check_help_echo:
22734
22735 /* Look for a `help-echo' property. */
22736 if (NILP (help_echo_string)) {
22737 Lisp_Object help, overlay;
22738
22739 /* Check overlays first. */
22740 help = overlay = Qnil;
22741 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
22742 {
22743 overlay = overlay_vec[i];
22744 help = Foverlay_get (overlay, Qhelp_echo);
22745 }
22746
22747 if (!NILP (help))
22748 {
22749 help_echo_string = help;
22750 help_echo_window = window;
22751 help_echo_object = overlay;
22752 help_echo_pos = pos;
22753 }
22754 else
22755 {
22756 Lisp_Object object = glyph->object;
22757 int charpos = glyph->charpos;
22758
22759 /* Try text properties. */
22760 if (STRINGP (object)
22761 && charpos >= 0
22762 && charpos < SCHARS (object))
22763 {
22764 help = Fget_text_property (make_number (charpos),
22765 Qhelp_echo, object);
22766 if (NILP (help))
22767 {
22768 /* If the string itself doesn't specify a help-echo,
22769 see if the buffer text ``under'' it does. */
22770 struct glyph_row *r
22771 = MATRIX_ROW (w->current_matrix, vpos);
22772 int start = MATRIX_ROW_START_CHARPOS (r);
22773 int pos = string_buffer_position (w, object, start);
22774 if (pos > 0)
22775 {
22776 help = Fget_char_property (make_number (pos),
22777 Qhelp_echo, w->buffer);
22778 if (!NILP (help))
22779 {
22780 charpos = pos;
22781 object = w->buffer;
22782 }
22783 }
22784 }
22785 }
22786 else if (BUFFERP (object)
22787 && charpos >= BEGV
22788 && charpos < ZV)
22789 help = Fget_text_property (make_number (charpos), Qhelp_echo,
22790 object);
22791
22792 if (!NILP (help))
22793 {
22794 help_echo_string = help;
22795 help_echo_window = window;
22796 help_echo_object = object;
22797 help_echo_pos = charpos;
22798 }
22799 }
22800 }
22801
22802 /* Look for a `pointer' property. */
22803 if (NILP (pointer))
22804 {
22805 /* Check overlays first. */
22806 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
22807 pointer = Foverlay_get (overlay_vec[i], Qpointer);
22808
22809 if (NILP (pointer))
22810 {
22811 Lisp_Object object = glyph->object;
22812 int charpos = glyph->charpos;
22813
22814 /* Try text properties. */
22815 if (STRINGP (object)
22816 && charpos >= 0
22817 && charpos < SCHARS (object))
22818 {
22819 pointer = Fget_text_property (make_number (charpos),
22820 Qpointer, object);
22821 if (NILP (pointer))
22822 {
22823 /* If the string itself doesn't specify a pointer,
22824 see if the buffer text ``under'' it does. */
22825 struct glyph_row *r
22826 = MATRIX_ROW (w->current_matrix, vpos);
22827 int start = MATRIX_ROW_START_CHARPOS (r);
22828 int pos = string_buffer_position (w, object, start);
22829 if (pos > 0)
22830 pointer = Fget_char_property (make_number (pos),
22831 Qpointer, w->buffer);
22832 }
22833 }
22834 else if (BUFFERP (object)
22835 && charpos >= BEGV
22836 && charpos < ZV)
22837 pointer = Fget_text_property (make_number (charpos),
22838 Qpointer, object);
22839 }
22840 }
22841
22842 BEGV = obegv;
22843 ZV = ozv;
22844 current_buffer = obuf;
22845 }
22846
22847 set_cursor:
22848
22849 define_frame_cursor1 (f, cursor, pointer);
22850 }
22851
22852
22853 /* EXPORT for RIF:
22854 Clear any mouse-face on window W. This function is part of the
22855 redisplay interface, and is called from try_window_id and similar
22856 functions to ensure the mouse-highlight is off. */
22857
22858 void
22859 x_clear_window_mouse_face (w)
22860 struct window *w;
22861 {
22862 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
22863 Lisp_Object window;
22864
22865 BLOCK_INPUT;
22866 XSETWINDOW (window, w);
22867 if (EQ (window, dpyinfo->mouse_face_window))
22868 clear_mouse_face (dpyinfo);
22869 UNBLOCK_INPUT;
22870 }
22871
22872
22873 /* EXPORT:
22874 Just discard the mouse face information for frame F, if any.
22875 This is used when the size of F is changed. */
22876
22877 void
22878 cancel_mouse_face (f)
22879 struct frame *f;
22880 {
22881 Lisp_Object window;
22882 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
22883
22884 window = dpyinfo->mouse_face_window;
22885 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
22886 {
22887 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
22888 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
22889 dpyinfo->mouse_face_window = Qnil;
22890 }
22891 }
22892
22893
22894 #endif /* HAVE_WINDOW_SYSTEM */
22895
22896 \f
22897 /***********************************************************************
22898 Exposure Events
22899 ***********************************************************************/
22900
22901 #ifdef HAVE_WINDOW_SYSTEM
22902
22903 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
22904 which intersects rectangle R. R is in window-relative coordinates. */
22905
22906 static void
22907 expose_area (w, row, r, area)
22908 struct window *w;
22909 struct glyph_row *row;
22910 XRectangle *r;
22911 enum glyph_row_area area;
22912 {
22913 struct glyph *first = row->glyphs[area];
22914 struct glyph *end = row->glyphs[area] + row->used[area];
22915 struct glyph *last;
22916 int first_x, start_x, x;
22917
22918 if (area == TEXT_AREA && row->fill_line_p)
22919 /* If row extends face to end of line write the whole line. */
22920 draw_glyphs (w, 0, row, area,
22921 0, row->used[area],
22922 DRAW_NORMAL_TEXT, 0);
22923 else
22924 {
22925 /* Set START_X to the window-relative start position for drawing glyphs of
22926 AREA. The first glyph of the text area can be partially visible.
22927 The first glyphs of other areas cannot. */
22928 start_x = window_box_left_offset (w, area);
22929 x = start_x;
22930 if (area == TEXT_AREA)
22931 x += row->x;
22932
22933 /* Find the first glyph that must be redrawn. */
22934 while (first < end
22935 && x + first->pixel_width < r->x)
22936 {
22937 x += first->pixel_width;
22938 ++first;
22939 }
22940
22941 /* Find the last one. */
22942 last = first;
22943 first_x = x;
22944 while (last < end
22945 && x < r->x + r->width)
22946 {
22947 x += last->pixel_width;
22948 ++last;
22949 }
22950
22951 /* Repaint. */
22952 if (last > first)
22953 draw_glyphs (w, first_x - start_x, row, area,
22954 first - row->glyphs[area], last - row->glyphs[area],
22955 DRAW_NORMAL_TEXT, 0);
22956 }
22957 }
22958
22959
22960 /* Redraw the parts of the glyph row ROW on window W intersecting
22961 rectangle R. R is in window-relative coordinates. Value is
22962 non-zero if mouse-face was overwritten. */
22963
22964 static int
22965 expose_line (w, row, r)
22966 struct window *w;
22967 struct glyph_row *row;
22968 XRectangle *r;
22969 {
22970 xassert (row->enabled_p);
22971
22972 if (row->mode_line_p || w->pseudo_window_p)
22973 draw_glyphs (w, 0, row, TEXT_AREA,
22974 0, row->used[TEXT_AREA],
22975 DRAW_NORMAL_TEXT, 0);
22976 else
22977 {
22978 if (row->used[LEFT_MARGIN_AREA])
22979 expose_area (w, row, r, LEFT_MARGIN_AREA);
22980 if (row->used[TEXT_AREA])
22981 expose_area (w, row, r, TEXT_AREA);
22982 if (row->used[RIGHT_MARGIN_AREA])
22983 expose_area (w, row, r, RIGHT_MARGIN_AREA);
22984 draw_row_fringe_bitmaps (w, row);
22985 }
22986
22987 return row->mouse_face_p;
22988 }
22989
22990
22991 /* Redraw those parts of glyphs rows during expose event handling that
22992 overlap other rows. Redrawing of an exposed line writes over parts
22993 of lines overlapping that exposed line; this function fixes that.
22994
22995 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
22996 row in W's current matrix that is exposed and overlaps other rows.
22997 LAST_OVERLAPPING_ROW is the last such row. */
22998
22999 static void
23000 expose_overlaps (w, first_overlapping_row, last_overlapping_row)
23001 struct window *w;
23002 struct glyph_row *first_overlapping_row;
23003 struct glyph_row *last_overlapping_row;
23004 {
23005 struct glyph_row *row;
23006
23007 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
23008 if (row->overlapping_p)
23009 {
23010 xassert (row->enabled_p && !row->mode_line_p);
23011
23012 if (row->used[LEFT_MARGIN_AREA])
23013 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
23014
23015 if (row->used[TEXT_AREA])
23016 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
23017
23018 if (row->used[RIGHT_MARGIN_AREA])
23019 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
23020 }
23021 }
23022
23023
23024 /* Return non-zero if W's cursor intersects rectangle R. */
23025
23026 static int
23027 phys_cursor_in_rect_p (w, r)
23028 struct window *w;
23029 XRectangle *r;
23030 {
23031 XRectangle cr, result;
23032 struct glyph *cursor_glyph;
23033
23034 cursor_glyph = get_phys_cursor_glyph (w);
23035 if (cursor_glyph)
23036 {
23037 /* r is relative to W's box, but w->phys_cursor.x is relative
23038 to left edge of W's TEXT area. Adjust it. */
23039 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
23040 cr.y = w->phys_cursor.y;
23041 cr.width = cursor_glyph->pixel_width;
23042 cr.height = w->phys_cursor_height;
23043 /* ++KFS: W32 version used W32-specific IntersectRect here, but
23044 I assume the effect is the same -- and this is portable. */
23045 return x_intersect_rectangles (&cr, r, &result);
23046 }
23047 else
23048 return 0;
23049 }
23050
23051
23052 /* EXPORT:
23053 Draw a vertical window border to the right of window W if W doesn't
23054 have vertical scroll bars. */
23055
23056 void
23057 x_draw_vertical_border (w)
23058 struct window *w;
23059 {
23060 /* We could do better, if we knew what type of scroll-bar the adjacent
23061 windows (on either side) have... But we don't :-(
23062 However, I think this works ok. ++KFS 2003-04-25 */
23063
23064 /* Redraw borders between horizontally adjacent windows. Don't
23065 do it for frames with vertical scroll bars because either the
23066 right scroll bar of a window, or the left scroll bar of its
23067 neighbor will suffice as a border. */
23068 if (FRAME_HAS_VERTICAL_SCROLL_BARS (XFRAME (w->frame)))
23069 return;
23070
23071 if (!WINDOW_RIGHTMOST_P (w)
23072 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
23073 {
23074 int x0, x1, y0, y1;
23075
23076 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
23077 y1 -= 1;
23078
23079 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
23080 x1 -= 1;
23081
23082 rif->draw_vertical_window_border (w, x1, y0, y1);
23083 }
23084 else if (!WINDOW_LEFTMOST_P (w)
23085 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
23086 {
23087 int x0, x1, y0, y1;
23088
23089 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
23090 y1 -= 1;
23091
23092 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
23093 x0 -= 1;
23094
23095 rif->draw_vertical_window_border (w, x0, y0, y1);
23096 }
23097 }
23098
23099
23100 /* Redraw the part of window W intersection rectangle FR. Pixel
23101 coordinates in FR are frame-relative. Call this function with
23102 input blocked. Value is non-zero if the exposure overwrites
23103 mouse-face. */
23104
23105 static int
23106 expose_window (w, fr)
23107 struct window *w;
23108 XRectangle *fr;
23109 {
23110 struct frame *f = XFRAME (w->frame);
23111 XRectangle wr, r;
23112 int mouse_face_overwritten_p = 0;
23113
23114 /* If window is not yet fully initialized, do nothing. This can
23115 happen when toolkit scroll bars are used and a window is split.
23116 Reconfiguring the scroll bar will generate an expose for a newly
23117 created window. */
23118 if (w->current_matrix == NULL)
23119 return 0;
23120
23121 /* When we're currently updating the window, display and current
23122 matrix usually don't agree. Arrange for a thorough display
23123 later. */
23124 if (w == updated_window)
23125 {
23126 SET_FRAME_GARBAGED (f);
23127 return 0;
23128 }
23129
23130 /* Frame-relative pixel rectangle of W. */
23131 wr.x = WINDOW_LEFT_EDGE_X (w);
23132 wr.y = WINDOW_TOP_EDGE_Y (w);
23133 wr.width = WINDOW_TOTAL_WIDTH (w);
23134 wr.height = WINDOW_TOTAL_HEIGHT (w);
23135
23136 if (x_intersect_rectangles (fr, &wr, &r))
23137 {
23138 int yb = window_text_bottom_y (w);
23139 struct glyph_row *row;
23140 int cursor_cleared_p;
23141 struct glyph_row *first_overlapping_row, *last_overlapping_row;
23142
23143 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
23144 r.x, r.y, r.width, r.height));
23145
23146 /* Convert to window coordinates. */
23147 r.x -= WINDOW_LEFT_EDGE_X (w);
23148 r.y -= WINDOW_TOP_EDGE_Y (w);
23149
23150 /* Turn off the cursor. */
23151 if (!w->pseudo_window_p
23152 && phys_cursor_in_rect_p (w, &r))
23153 {
23154 x_clear_cursor (w);
23155 cursor_cleared_p = 1;
23156 }
23157 else
23158 cursor_cleared_p = 0;
23159
23160 /* Update lines intersecting rectangle R. */
23161 first_overlapping_row = last_overlapping_row = NULL;
23162 for (row = w->current_matrix->rows;
23163 row->enabled_p;
23164 ++row)
23165 {
23166 int y0 = row->y;
23167 int y1 = MATRIX_ROW_BOTTOM_Y (row);
23168
23169 if ((y0 >= r.y && y0 < r.y + r.height)
23170 || (y1 > r.y && y1 < r.y + r.height)
23171 || (r.y >= y0 && r.y < y1)
23172 || (r.y + r.height > y0 && r.y + r.height < y1))
23173 {
23174 /* A header line may be overlapping, but there is no need
23175 to fix overlapping areas for them. KFS 2005-02-12 */
23176 if (row->overlapping_p && !row->mode_line_p)
23177 {
23178 if (first_overlapping_row == NULL)
23179 first_overlapping_row = row;
23180 last_overlapping_row = row;
23181 }
23182
23183 if (expose_line (w, row, &r))
23184 mouse_face_overwritten_p = 1;
23185 }
23186
23187 if (y1 >= yb)
23188 break;
23189 }
23190
23191 /* Display the mode line if there is one. */
23192 if (WINDOW_WANTS_MODELINE_P (w)
23193 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
23194 row->enabled_p)
23195 && row->y < r.y + r.height)
23196 {
23197 if (expose_line (w, row, &r))
23198 mouse_face_overwritten_p = 1;
23199 }
23200
23201 if (!w->pseudo_window_p)
23202 {
23203 /* Fix the display of overlapping rows. */
23204 if (first_overlapping_row)
23205 expose_overlaps (w, first_overlapping_row, last_overlapping_row);
23206
23207 /* Draw border between windows. */
23208 x_draw_vertical_border (w);
23209
23210 /* Turn the cursor on again. */
23211 if (cursor_cleared_p)
23212 update_window_cursor (w, 1);
23213 }
23214 }
23215
23216 return mouse_face_overwritten_p;
23217 }
23218
23219
23220
23221 /* Redraw (parts) of all windows in the window tree rooted at W that
23222 intersect R. R contains frame pixel coordinates. Value is
23223 non-zero if the exposure overwrites mouse-face. */
23224
23225 static int
23226 expose_window_tree (w, r)
23227 struct window *w;
23228 XRectangle *r;
23229 {
23230 struct frame *f = XFRAME (w->frame);
23231 int mouse_face_overwritten_p = 0;
23232
23233 while (w && !FRAME_GARBAGED_P (f))
23234 {
23235 if (!NILP (w->hchild))
23236 mouse_face_overwritten_p
23237 |= expose_window_tree (XWINDOW (w->hchild), r);
23238 else if (!NILP (w->vchild))
23239 mouse_face_overwritten_p
23240 |= expose_window_tree (XWINDOW (w->vchild), r);
23241 else
23242 mouse_face_overwritten_p |= expose_window (w, r);
23243
23244 w = NILP (w->next) ? NULL : XWINDOW (w->next);
23245 }
23246
23247 return mouse_face_overwritten_p;
23248 }
23249
23250
23251 /* EXPORT:
23252 Redisplay an exposed area of frame F. X and Y are the upper-left
23253 corner of the exposed rectangle. W and H are width and height of
23254 the exposed area. All are pixel values. W or H zero means redraw
23255 the entire frame. */
23256
23257 void
23258 expose_frame (f, x, y, w, h)
23259 struct frame *f;
23260 int x, y, w, h;
23261 {
23262 XRectangle r;
23263 int mouse_face_overwritten_p = 0;
23264
23265 TRACE ((stderr, "expose_frame "));
23266
23267 /* No need to redraw if frame will be redrawn soon. */
23268 if (FRAME_GARBAGED_P (f))
23269 {
23270 TRACE ((stderr, " garbaged\n"));
23271 return;
23272 }
23273
23274 /* If basic faces haven't been realized yet, there is no point in
23275 trying to redraw anything. This can happen when we get an expose
23276 event while Emacs is starting, e.g. by moving another window. */
23277 if (FRAME_FACE_CACHE (f) == NULL
23278 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
23279 {
23280 TRACE ((stderr, " no faces\n"));
23281 return;
23282 }
23283
23284 if (w == 0 || h == 0)
23285 {
23286 r.x = r.y = 0;
23287 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
23288 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
23289 }
23290 else
23291 {
23292 r.x = x;
23293 r.y = y;
23294 r.width = w;
23295 r.height = h;
23296 }
23297
23298 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
23299 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
23300
23301 if (WINDOWP (f->tool_bar_window))
23302 mouse_face_overwritten_p
23303 |= expose_window (XWINDOW (f->tool_bar_window), &r);
23304
23305 #ifdef HAVE_X_WINDOWS
23306 #ifndef MSDOS
23307 #ifndef USE_X_TOOLKIT
23308 if (WINDOWP (f->menu_bar_window))
23309 mouse_face_overwritten_p
23310 |= expose_window (XWINDOW (f->menu_bar_window), &r);
23311 #endif /* not USE_X_TOOLKIT */
23312 #endif
23313 #endif
23314
23315 /* Some window managers support a focus-follows-mouse style with
23316 delayed raising of frames. Imagine a partially obscured frame,
23317 and moving the mouse into partially obscured mouse-face on that
23318 frame. The visible part of the mouse-face will be highlighted,
23319 then the WM raises the obscured frame. With at least one WM, KDE
23320 2.1, Emacs is not getting any event for the raising of the frame
23321 (even tried with SubstructureRedirectMask), only Expose events.
23322 These expose events will draw text normally, i.e. not
23323 highlighted. Which means we must redo the highlight here.
23324 Subsume it under ``we love X''. --gerd 2001-08-15 */
23325 /* Included in Windows version because Windows most likely does not
23326 do the right thing if any third party tool offers
23327 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
23328 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
23329 {
23330 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
23331 if (f == dpyinfo->mouse_face_mouse_frame)
23332 {
23333 int x = dpyinfo->mouse_face_mouse_x;
23334 int y = dpyinfo->mouse_face_mouse_y;
23335 clear_mouse_face (dpyinfo);
23336 note_mouse_highlight (f, x, y);
23337 }
23338 }
23339 }
23340
23341
23342 /* EXPORT:
23343 Determine the intersection of two rectangles R1 and R2. Return
23344 the intersection in *RESULT. Value is non-zero if RESULT is not
23345 empty. */
23346
23347 int
23348 x_intersect_rectangles (r1, r2, result)
23349 XRectangle *r1, *r2, *result;
23350 {
23351 XRectangle *left, *right;
23352 XRectangle *upper, *lower;
23353 int intersection_p = 0;
23354
23355 /* Rearrange so that R1 is the left-most rectangle. */
23356 if (r1->x < r2->x)
23357 left = r1, right = r2;
23358 else
23359 left = r2, right = r1;
23360
23361 /* X0 of the intersection is right.x0, if this is inside R1,
23362 otherwise there is no intersection. */
23363 if (right->x <= left->x + left->width)
23364 {
23365 result->x = right->x;
23366
23367 /* The right end of the intersection is the minimum of the
23368 the right ends of left and right. */
23369 result->width = (min (left->x + left->width, right->x + right->width)
23370 - result->x);
23371
23372 /* Same game for Y. */
23373 if (r1->y < r2->y)
23374 upper = r1, lower = r2;
23375 else
23376 upper = r2, lower = r1;
23377
23378 /* The upper end of the intersection is lower.y0, if this is inside
23379 of upper. Otherwise, there is no intersection. */
23380 if (lower->y <= upper->y + upper->height)
23381 {
23382 result->y = lower->y;
23383
23384 /* The lower end of the intersection is the minimum of the lower
23385 ends of upper and lower. */
23386 result->height = (min (lower->y + lower->height,
23387 upper->y + upper->height)
23388 - result->y);
23389 intersection_p = 1;
23390 }
23391 }
23392
23393 return intersection_p;
23394 }
23395
23396 #endif /* HAVE_WINDOW_SYSTEM */
23397
23398 \f
23399 /***********************************************************************
23400 Initialization
23401 ***********************************************************************/
23402
23403 void
23404 syms_of_xdisp ()
23405 {
23406 Vwith_echo_area_save_vector = Qnil;
23407 staticpro (&Vwith_echo_area_save_vector);
23408
23409 Vmessage_stack = Qnil;
23410 staticpro (&Vmessage_stack);
23411
23412 Qinhibit_redisplay = intern ("inhibit-redisplay");
23413 staticpro (&Qinhibit_redisplay);
23414
23415 message_dolog_marker1 = Fmake_marker ();
23416 staticpro (&message_dolog_marker1);
23417 message_dolog_marker2 = Fmake_marker ();
23418 staticpro (&message_dolog_marker2);
23419 message_dolog_marker3 = Fmake_marker ();
23420 staticpro (&message_dolog_marker3);
23421
23422 #if GLYPH_DEBUG
23423 defsubr (&Sdump_frame_glyph_matrix);
23424 defsubr (&Sdump_glyph_matrix);
23425 defsubr (&Sdump_glyph_row);
23426 defsubr (&Sdump_tool_bar_row);
23427 defsubr (&Strace_redisplay);
23428 defsubr (&Strace_to_stderr);
23429 #endif
23430 #ifdef HAVE_WINDOW_SYSTEM
23431 defsubr (&Stool_bar_lines_needed);
23432 defsubr (&Slookup_image_map);
23433 #endif
23434 defsubr (&Sformat_mode_line);
23435
23436 staticpro (&Qmenu_bar_update_hook);
23437 Qmenu_bar_update_hook = intern ("menu-bar-update-hook");
23438
23439 staticpro (&Qoverriding_terminal_local_map);
23440 Qoverriding_terminal_local_map = intern ("overriding-terminal-local-map");
23441
23442 staticpro (&Qoverriding_local_map);
23443 Qoverriding_local_map = intern ("overriding-local-map");
23444
23445 staticpro (&Qwindow_scroll_functions);
23446 Qwindow_scroll_functions = intern ("window-scroll-functions");
23447
23448 staticpro (&Qredisplay_end_trigger_functions);
23449 Qredisplay_end_trigger_functions = intern ("redisplay-end-trigger-functions");
23450
23451 staticpro (&Qinhibit_point_motion_hooks);
23452 Qinhibit_point_motion_hooks = intern ("inhibit-point-motion-hooks");
23453
23454 QCdata = intern (":data");
23455 staticpro (&QCdata);
23456 Qdisplay = intern ("display");
23457 staticpro (&Qdisplay);
23458 Qspace_width = intern ("space-width");
23459 staticpro (&Qspace_width);
23460 Qraise = intern ("raise");
23461 staticpro (&Qraise);
23462 Qslice = intern ("slice");
23463 staticpro (&Qslice);
23464 Qspace = intern ("space");
23465 staticpro (&Qspace);
23466 Qmargin = intern ("margin");
23467 staticpro (&Qmargin);
23468 Qpointer = intern ("pointer");
23469 staticpro (&Qpointer);
23470 Qleft_margin = intern ("left-margin");
23471 staticpro (&Qleft_margin);
23472 Qright_margin = intern ("right-margin");
23473 staticpro (&Qright_margin);
23474 Qcenter = intern ("center");
23475 staticpro (&Qcenter);
23476 Qline_height = intern ("line-height");
23477 staticpro (&Qline_height);
23478 QCalign_to = intern (":align-to");
23479 staticpro (&QCalign_to);
23480 QCrelative_width = intern (":relative-width");
23481 staticpro (&QCrelative_width);
23482 QCrelative_height = intern (":relative-height");
23483 staticpro (&QCrelative_height);
23484 QCeval = intern (":eval");
23485 staticpro (&QCeval);
23486 QCpropertize = intern (":propertize");
23487 staticpro (&QCpropertize);
23488 QCfile = intern (":file");
23489 staticpro (&QCfile);
23490 Qfontified = intern ("fontified");
23491 staticpro (&Qfontified);
23492 Qfontification_functions = intern ("fontification-functions");
23493 staticpro (&Qfontification_functions);
23494 Qtrailing_whitespace = intern ("trailing-whitespace");
23495 staticpro (&Qtrailing_whitespace);
23496 Qescape_glyph = intern ("escape-glyph");
23497 staticpro (&Qescape_glyph);
23498 Qnobreak_space = intern ("nobreak-space");
23499 staticpro (&Qnobreak_space);
23500 Qimage = intern ("image");
23501 staticpro (&Qimage);
23502 QCmap = intern (":map");
23503 staticpro (&QCmap);
23504 QCpointer = intern (":pointer");
23505 staticpro (&QCpointer);
23506 Qrect = intern ("rect");
23507 staticpro (&Qrect);
23508 Qcircle = intern ("circle");
23509 staticpro (&Qcircle);
23510 Qpoly = intern ("poly");
23511 staticpro (&Qpoly);
23512 Qmessage_truncate_lines = intern ("message-truncate-lines");
23513 staticpro (&Qmessage_truncate_lines);
23514 Qgrow_only = intern ("grow-only");
23515 staticpro (&Qgrow_only);
23516 Qinhibit_menubar_update = intern ("inhibit-menubar-update");
23517 staticpro (&Qinhibit_menubar_update);
23518 Qinhibit_eval_during_redisplay = intern ("inhibit-eval-during-redisplay");
23519 staticpro (&Qinhibit_eval_during_redisplay);
23520 Qposition = intern ("position");
23521 staticpro (&Qposition);
23522 Qbuffer_position = intern ("buffer-position");
23523 staticpro (&Qbuffer_position);
23524 Qobject = intern ("object");
23525 staticpro (&Qobject);
23526 Qbar = intern ("bar");
23527 staticpro (&Qbar);
23528 Qhbar = intern ("hbar");
23529 staticpro (&Qhbar);
23530 Qbox = intern ("box");
23531 staticpro (&Qbox);
23532 Qhollow = intern ("hollow");
23533 staticpro (&Qhollow);
23534 Qhand = intern ("hand");
23535 staticpro (&Qhand);
23536 Qarrow = intern ("arrow");
23537 staticpro (&Qarrow);
23538 Qtext = intern ("text");
23539 staticpro (&Qtext);
23540 Qrisky_local_variable = intern ("risky-local-variable");
23541 staticpro (&Qrisky_local_variable);
23542 Qinhibit_free_realized_faces = intern ("inhibit-free-realized-faces");
23543 staticpro (&Qinhibit_free_realized_faces);
23544
23545 list_of_error = Fcons (Fcons (intern ("error"),
23546 Fcons (intern ("void-variable"), Qnil)),
23547 Qnil);
23548 staticpro (&list_of_error);
23549
23550 Qlast_arrow_position = intern ("last-arrow-position");
23551 staticpro (&Qlast_arrow_position);
23552 Qlast_arrow_string = intern ("last-arrow-string");
23553 staticpro (&Qlast_arrow_string);
23554
23555 Qoverlay_arrow_string = intern ("overlay-arrow-string");
23556 staticpro (&Qoverlay_arrow_string);
23557 Qoverlay_arrow_bitmap = intern ("overlay-arrow-bitmap");
23558 staticpro (&Qoverlay_arrow_bitmap);
23559
23560 echo_buffer[0] = echo_buffer[1] = Qnil;
23561 staticpro (&echo_buffer[0]);
23562 staticpro (&echo_buffer[1]);
23563
23564 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
23565 staticpro (&echo_area_buffer[0]);
23566 staticpro (&echo_area_buffer[1]);
23567
23568 Vmessages_buffer_name = build_string ("*Messages*");
23569 staticpro (&Vmessages_buffer_name);
23570
23571 mode_line_proptrans_alist = Qnil;
23572 staticpro (&mode_line_proptrans_alist);
23573 mode_line_string_list = Qnil;
23574 staticpro (&mode_line_string_list);
23575 mode_line_string_face = Qnil;
23576 staticpro (&mode_line_string_face);
23577 mode_line_string_face_prop = Qnil;
23578 staticpro (&mode_line_string_face_prop);
23579 Vmode_line_unwind_vector = Qnil;
23580 staticpro (&Vmode_line_unwind_vector);
23581
23582 help_echo_string = Qnil;
23583 staticpro (&help_echo_string);
23584 help_echo_object = Qnil;
23585 staticpro (&help_echo_object);
23586 help_echo_window = Qnil;
23587 staticpro (&help_echo_window);
23588 previous_help_echo_string = Qnil;
23589 staticpro (&previous_help_echo_string);
23590 help_echo_pos = -1;
23591
23592 #ifdef HAVE_WINDOW_SYSTEM
23593 DEFVAR_BOOL ("x-stretch-cursor", &x_stretch_cursor_p,
23594 doc: /* *Non-nil means draw block cursor as wide as the glyph under it.
23595 For example, if a block cursor is over a tab, it will be drawn as
23596 wide as that tab on the display. */);
23597 x_stretch_cursor_p = 0;
23598 #endif
23599
23600 DEFVAR_LISP ("show-trailing-whitespace", &Vshow_trailing_whitespace,
23601 doc: /* *Non-nil means highlight trailing whitespace.
23602 The face used for trailing whitespace is `trailing-whitespace'. */);
23603 Vshow_trailing_whitespace = Qnil;
23604
23605 DEFVAR_LISP ("nobreak-char-display", &Vnobreak_char_display,
23606 doc: /* *Control highlighting of nobreak space and soft hyphen.
23607 A value of t means highlight the character itself (for nobreak space,
23608 use face `nobreak-space').
23609 A value of nil means no highlighting.
23610 Other values mean display the escape glyph followed by an ordinary
23611 space or ordinary hyphen. */);
23612 Vnobreak_char_display = Qt;
23613
23614 DEFVAR_LISP ("void-text-area-pointer", &Vvoid_text_area_pointer,
23615 doc: /* *The pointer shape to show in void text areas.
23616 A value of nil means to show the text pointer. Other options are `arrow',
23617 `text', `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
23618 Vvoid_text_area_pointer = Qarrow;
23619
23620 DEFVAR_LISP ("inhibit-redisplay", &Vinhibit_redisplay,
23621 doc: /* Non-nil means don't actually do any redisplay.
23622 This is used for internal purposes. */);
23623 Vinhibit_redisplay = Qnil;
23624
23625 DEFVAR_LISP ("global-mode-string", &Vglobal_mode_string,
23626 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
23627 Vglobal_mode_string = Qnil;
23628
23629 DEFVAR_LISP ("overlay-arrow-position", &Voverlay_arrow_position,
23630 doc: /* Marker for where to display an arrow on top of the buffer text.
23631 This must be the beginning of a line in order to work.
23632 See also `overlay-arrow-string'. */);
23633 Voverlay_arrow_position = Qnil;
23634
23635 DEFVAR_LISP ("overlay-arrow-string", &Voverlay_arrow_string,
23636 doc: /* String to display as an arrow in non-window frames.
23637 See also `overlay-arrow-position'. */);
23638 Voverlay_arrow_string = build_string ("=>");
23639
23640 DEFVAR_LISP ("overlay-arrow-variable-list", &Voverlay_arrow_variable_list,
23641 doc: /* List of variables (symbols) which hold markers for overlay arrows.
23642 The symbols on this list are examined during redisplay to determine
23643 where to display overlay arrows. */);
23644 Voverlay_arrow_variable_list
23645 = Fcons (intern ("overlay-arrow-position"), Qnil);
23646
23647 DEFVAR_INT ("scroll-step", &scroll_step,
23648 doc: /* *The number of lines to try scrolling a window by when point moves out.
23649 If that fails to bring point back on frame, point is centered instead.
23650 If this is zero, point is always centered after it moves off frame.
23651 If you want scrolling to always be a line at a time, you should set
23652 `scroll-conservatively' to a large value rather than set this to 1. */);
23653
23654 DEFVAR_INT ("scroll-conservatively", &scroll_conservatively,
23655 doc: /* *Scroll up to this many lines, to bring point back on screen.
23656 A value of zero means to scroll the text to center point vertically
23657 in the window. */);
23658 scroll_conservatively = 0;
23659
23660 DEFVAR_INT ("scroll-margin", &scroll_margin,
23661 doc: /* *Number of lines of margin at the top and bottom of a window.
23662 Recenter the window whenever point gets within this many lines
23663 of the top or bottom of the window. */);
23664 scroll_margin = 0;
23665
23666 DEFVAR_LISP ("display-pixels-per-inch", &Vdisplay_pixels_per_inch,
23667 doc: /* Pixels per inch value for non-window system displays.
23668 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
23669 Vdisplay_pixels_per_inch = make_float (72.0);
23670
23671 #if GLYPH_DEBUG
23672 DEFVAR_INT ("debug-end-pos", &debug_end_pos, doc: /* Don't ask. */);
23673 #endif
23674
23675 DEFVAR_BOOL ("truncate-partial-width-windows",
23676 &truncate_partial_width_windows,
23677 doc: /* *Non-nil means truncate lines in all windows less than full frame wide. */);
23678 truncate_partial_width_windows = 1;
23679
23680 DEFVAR_BOOL ("mode-line-inverse-video", &mode_line_inverse_video,
23681 doc: /* nil means display the mode-line/header-line/menu-bar in the default face.
23682 Any other value means to use the appropriate face, `mode-line',
23683 `header-line', or `menu' respectively. */);
23684 mode_line_inverse_video = 1;
23685
23686 DEFVAR_LISP ("line-number-display-limit", &Vline_number_display_limit,
23687 doc: /* *Maximum buffer size for which line number should be displayed.
23688 If the buffer is bigger than this, the line number does not appear
23689 in the mode line. A value of nil means no limit. */);
23690 Vline_number_display_limit = Qnil;
23691
23692 DEFVAR_INT ("line-number-display-limit-width",
23693 &line_number_display_limit_width,
23694 doc: /* *Maximum line width (in characters) for line number display.
23695 If the average length of the lines near point is bigger than this, then the
23696 line number may be omitted from the mode line. */);
23697 line_number_display_limit_width = 200;
23698
23699 DEFVAR_BOOL ("highlight-nonselected-windows", &highlight_nonselected_windows,
23700 doc: /* *Non-nil means highlight region even in nonselected windows. */);
23701 highlight_nonselected_windows = 0;
23702
23703 DEFVAR_BOOL ("multiple-frames", &multiple_frames,
23704 doc: /* Non-nil if more than one frame is visible on this display.
23705 Minibuffer-only frames don't count, but iconified frames do.
23706 This variable is not guaranteed to be accurate except while processing
23707 `frame-title-format' and `icon-title-format'. */);
23708
23709 DEFVAR_LISP ("frame-title-format", &Vframe_title_format,
23710 doc: /* Template for displaying the title bar of visible frames.
23711 \(Assuming the window manager supports this feature.)
23712 This variable has the same structure as `mode-line-format' (which see),
23713 and is used only on frames for which no explicit name has been set
23714 \(see `modify-frame-parameters'). */);
23715
23716 DEFVAR_LISP ("icon-title-format", &Vicon_title_format,
23717 doc: /* Template for displaying the title bar of an iconified frame.
23718 \(Assuming the window manager supports this feature.)
23719 This variable has the same structure as `mode-line-format' (which see),
23720 and is used only on frames for which no explicit name has been set
23721 \(see `modify-frame-parameters'). */);
23722 Vicon_title_format
23723 = Vframe_title_format
23724 = Fcons (intern ("multiple-frames"),
23725 Fcons (build_string ("%b"),
23726 Fcons (Fcons (empty_string,
23727 Fcons (intern ("invocation-name"),
23728 Fcons (build_string ("@"),
23729 Fcons (intern ("system-name"),
23730 Qnil)))),
23731 Qnil)));
23732
23733 DEFVAR_LISP ("message-log-max", &Vmessage_log_max,
23734 doc: /* Maximum number of lines to keep in the message log buffer.
23735 If nil, disable message logging. If t, log messages but don't truncate
23736 the buffer when it becomes large. */);
23737 Vmessage_log_max = make_number (50);
23738
23739 DEFVAR_LISP ("window-size-change-functions", &Vwindow_size_change_functions,
23740 doc: /* Functions called before redisplay, if window sizes have changed.
23741 The value should be a list of functions that take one argument.
23742 Just before redisplay, for each frame, if any of its windows have changed
23743 size since the last redisplay, or have been split or deleted,
23744 all the functions in the list are called, with the frame as argument. */);
23745 Vwindow_size_change_functions = Qnil;
23746
23747 DEFVAR_LISP ("window-scroll-functions", &Vwindow_scroll_functions,
23748 doc: /* List of functions to call before redisplaying a window with scrolling.
23749 Each function is called with two arguments, the window
23750 and its new display-start position. Note that the value of `window-end'
23751 is not valid when these functions are called. */);
23752 Vwindow_scroll_functions = Qnil;
23753
23754 DEFVAR_LISP ("redisplay-end-trigger-functions", &Vredisplay_end_trigger_functions,
23755 doc: /* Functions called when redisplay of a window reaches the end trigger.
23756 Each function is called with two arguments, the window and the end trigger value.
23757 See `set-window-redisplay-end-trigger'. */);
23758 Vredisplay_end_trigger_functions = Qnil;
23759
23760 DEFVAR_BOOL ("mouse-autoselect-window", &mouse_autoselect_window,
23761 doc: /* *Non-nil means autoselect window with mouse pointer. */);
23762 mouse_autoselect_window = 0;
23763
23764 DEFVAR_BOOL ("auto-resize-tool-bars", &auto_resize_tool_bars_p,
23765 doc: /* *Non-nil means automatically resize tool-bars.
23766 This increases a tool-bar's height if not all tool-bar items are visible.
23767 It decreases a tool-bar's height when it would display blank lines
23768 otherwise. */);
23769 auto_resize_tool_bars_p = 1;
23770
23771 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", &auto_raise_tool_bar_buttons_p,
23772 doc: /* *Non-nil means raise tool-bar buttons when the mouse moves over them. */);
23773 auto_raise_tool_bar_buttons_p = 1;
23774
23775 DEFVAR_BOOL ("make-cursor-line-fully-visible", &make_cursor_line_fully_visible_p,
23776 doc: /* *Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
23777 make_cursor_line_fully_visible_p = 1;
23778
23779 DEFVAR_LISP ("tool-bar-border", &Vtool_bar_border,
23780 doc: /* *Border below tool-bar in pixels.
23781 If an integer, use it as the height of the border.
23782 If it is one of `internal-border-width' or `border-width', use the
23783 value of the corresponding frame parameter.
23784 Otherwise, no border is added below the tool-bar. */);
23785 Vtool_bar_border = Qinternal_border_width;
23786
23787 DEFVAR_LISP ("tool-bar-button-margin", &Vtool_bar_button_margin,
23788 doc: /* *Margin around tool-bar buttons in pixels.
23789 If an integer, use that for both horizontal and vertical margins.
23790 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
23791 HORZ specifying the horizontal margin, and VERT specifying the
23792 vertical margin. */);
23793 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
23794
23795 DEFVAR_INT ("tool-bar-button-relief", &tool_bar_button_relief,
23796 doc: /* *Relief thickness of tool-bar buttons. */);
23797 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
23798
23799 DEFVAR_LISP ("fontification-functions", &Vfontification_functions,
23800 doc: /* List of functions to call to fontify regions of text.
23801 Each function is called with one argument POS. Functions must
23802 fontify a region starting at POS in the current buffer, and give
23803 fontified regions the property `fontified'. */);
23804 Vfontification_functions = Qnil;
23805 Fmake_variable_buffer_local (Qfontification_functions);
23806
23807 DEFVAR_BOOL ("unibyte-display-via-language-environment",
23808 &unibyte_display_via_language_environment,
23809 doc: /* *Non-nil means display unibyte text according to language environment.
23810 Specifically this means that unibyte non-ASCII characters
23811 are displayed by converting them to the equivalent multibyte characters
23812 according to the current language environment. As a result, they are
23813 displayed according to the current fontset. */);
23814 unibyte_display_via_language_environment = 0;
23815
23816 DEFVAR_LISP ("max-mini-window-height", &Vmax_mini_window_height,
23817 doc: /* *Maximum height for resizing mini-windows.
23818 If a float, it specifies a fraction of the mini-window frame's height.
23819 If an integer, it specifies a number of lines. */);
23820 Vmax_mini_window_height = make_float (0.25);
23821
23822 DEFVAR_LISP ("resize-mini-windows", &Vresize_mini_windows,
23823 doc: /* *How to resize mini-windows.
23824 A value of nil means don't automatically resize mini-windows.
23825 A value of t means resize them to fit the text displayed in them.
23826 A value of `grow-only', the default, means let mini-windows grow
23827 only, until their display becomes empty, at which point the windows
23828 go back to their normal size. */);
23829 Vresize_mini_windows = Qgrow_only;
23830
23831 DEFVAR_LISP ("blink-cursor-alist", &Vblink_cursor_alist,
23832 doc: /* Alist specifying how to blink the cursor off.
23833 Each element has the form (ON-STATE . OFF-STATE). Whenever the
23834 `cursor-type' frame-parameter or variable equals ON-STATE,
23835 comparing using `equal', Emacs uses OFF-STATE to specify
23836 how to blink it off. ON-STATE and OFF-STATE are values for
23837 the `cursor-type' frame parameter.
23838
23839 If a frame's ON-STATE has no entry in this list,
23840 the frame's other specifications determine how to blink the cursor off. */);
23841 Vblink_cursor_alist = Qnil;
23842
23843 DEFVAR_BOOL ("auto-hscroll-mode", &automatic_hscrolling_p,
23844 doc: /* *Non-nil means scroll the display automatically to make point visible. */);
23845 automatic_hscrolling_p = 1;
23846
23847 DEFVAR_INT ("hscroll-margin", &hscroll_margin,
23848 doc: /* *How many columns away from the window edge point is allowed to get
23849 before automatic hscrolling will horizontally scroll the window. */);
23850 hscroll_margin = 5;
23851
23852 DEFVAR_LISP ("hscroll-step", &Vhscroll_step,
23853 doc: /* *How many columns to scroll the window when point gets too close to the edge.
23854 When point is less than `hscroll-margin' columns from the window
23855 edge, automatic hscrolling will scroll the window by the amount of columns
23856 determined by this variable. If its value is a positive integer, scroll that
23857 many columns. If it's a positive floating-point number, it specifies the
23858 fraction of the window's width to scroll. If it's nil or zero, point will be
23859 centered horizontally after the scroll. Any other value, including negative
23860 numbers, are treated as if the value were zero.
23861
23862 Automatic hscrolling always moves point outside the scroll margin, so if
23863 point was more than scroll step columns inside the margin, the window will
23864 scroll more than the value given by the scroll step.
23865
23866 Note that the lower bound for automatic hscrolling specified by `scroll-left'
23867 and `scroll-right' overrides this variable's effect. */);
23868 Vhscroll_step = make_number (0);
23869
23870 DEFVAR_BOOL ("message-truncate-lines", &message_truncate_lines,
23871 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
23872 Bind this around calls to `message' to let it take effect. */);
23873 message_truncate_lines = 0;
23874
23875 DEFVAR_LISP ("menu-bar-update-hook", &Vmenu_bar_update_hook,
23876 doc: /* Normal hook run to update the menu bar definitions.
23877 Redisplay runs this hook before it redisplays the menu bar.
23878 This is used to update submenus such as Buffers,
23879 whose contents depend on various data. */);
23880 Vmenu_bar_update_hook = Qnil;
23881
23882 DEFVAR_BOOL ("inhibit-menubar-update", &inhibit_menubar_update,
23883 doc: /* Non-nil means don't update menu bars. Internal use only. */);
23884 inhibit_menubar_update = 0;
23885
23886 DEFVAR_BOOL ("inhibit-eval-during-redisplay", &inhibit_eval_during_redisplay,
23887 doc: /* Non-nil means don't eval Lisp during redisplay. */);
23888 inhibit_eval_during_redisplay = 0;
23889
23890 DEFVAR_BOOL ("inhibit-free-realized-faces", &inhibit_free_realized_faces,
23891 doc: /* Non-nil means don't free realized faces. Internal use only. */);
23892 inhibit_free_realized_faces = 0;
23893
23894 #if GLYPH_DEBUG
23895 DEFVAR_BOOL ("inhibit-try-window-id", &inhibit_try_window_id,
23896 doc: /* Inhibit try_window_id display optimization. */);
23897 inhibit_try_window_id = 0;
23898
23899 DEFVAR_BOOL ("inhibit-try-window-reusing", &inhibit_try_window_reusing,
23900 doc: /* Inhibit try_window_reusing display optimization. */);
23901 inhibit_try_window_reusing = 0;
23902
23903 DEFVAR_BOOL ("inhibit-try-cursor-movement", &inhibit_try_cursor_movement,
23904 doc: /* Inhibit try_cursor_movement display optimization. */);
23905 inhibit_try_cursor_movement = 0;
23906 #endif /* GLYPH_DEBUG */
23907 }
23908
23909
23910 /* Initialize this module when Emacs starts. */
23911
23912 void
23913 init_xdisp ()
23914 {
23915 Lisp_Object root_window;
23916 struct window *mini_w;
23917
23918 current_header_line_height = current_mode_line_height = -1;
23919
23920 CHARPOS (this_line_start_pos) = 0;
23921
23922 mini_w = XWINDOW (minibuf_window);
23923 root_window = FRAME_ROOT_WINDOW (XFRAME (WINDOW_FRAME (mini_w)));
23924
23925 if (!noninteractive)
23926 {
23927 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (root_window)));
23928 int i;
23929
23930 XWINDOW (root_window)->top_line = make_number (FRAME_TOP_MARGIN (f));
23931 set_window_height (root_window,
23932 FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f),
23933 0);
23934 mini_w->top_line = make_number (FRAME_LINES (f) - 1);
23935 set_window_height (minibuf_window, 1, 0);
23936
23937 XWINDOW (root_window)->total_cols = make_number (FRAME_COLS (f));
23938 mini_w->total_cols = make_number (FRAME_COLS (f));
23939
23940 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
23941 scratch_glyph_row.glyphs[TEXT_AREA + 1]
23942 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
23943
23944 /* The default ellipsis glyphs `...'. */
23945 for (i = 0; i < 3; ++i)
23946 default_invis_vector[i] = make_number ('.');
23947 }
23948
23949 {
23950 /* Allocate the buffer for frame titles.
23951 Also used for `format-mode-line'. */
23952 int size = 100;
23953 mode_line_noprop_buf = (char *) xmalloc (size);
23954 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
23955 mode_line_noprop_ptr = mode_line_noprop_buf;
23956 mode_line_target = MODE_LINE_DISPLAY;
23957 }
23958
23959 help_echo_showing_p = 0;
23960 }
23961
23962
23963 /* arch-tag: eacc864d-bb6a-4b74-894a-1a4399a1358b
23964 (do not change this comment) */