Overhaul and simplify single_kboard API. Allow calls to `recursive-edit' in process...
[bpt/emacs.git] / 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 Free Software Foundation, Inc.
5
6 This file is part of GNU Emacs.
7
8 GNU Emacs is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
11 any later version.
12
13 GNU Emacs is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GNU Emacs; see the file COPYING. If not, write to
20 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 Boston, MA 02110-1301, USA. */
22
23 /* New redisplay written by Gerd Moellmann <gerd@gnu.org>.
24
25 Redisplay.
26
27 Emacs separates the task of updating the display from code
28 modifying global state, e.g. buffer text. This way functions
29 operating on buffers don't also have to be concerned with updating
30 the display.
31
32 Updating the display is triggered by the Lisp interpreter when it
33 decides it's time to do it. This is done either automatically for
34 you as part of the interpreter's command loop or as the result of
35 calling Lisp functions like `sit-for'. The C function `redisplay'
36 in xdisp.c is the only entry into the inner redisplay code. (Or,
37 let's say almost---see the description of direct update
38 operations, below.)
39
40 The following diagram shows how redisplay code is invoked. As you
41 can see, Lisp calls redisplay and vice versa. Under window systems
42 like X, some portions of the redisplay code are also called
43 asynchronously during mouse movement or expose events. It is very
44 important that these code parts do NOT use the C library (malloc,
45 free) because many C libraries under Unix are not reentrant. They
46 may also NOT call functions of the Lisp interpreter which could
47 change the interpreter's state. If you don't follow these rules,
48 you will encounter bugs which are very hard to explain.
49
50 (Direct functions, see below)
51 direct_output_for_insert,
52 direct_forward_char (dispnew.c)
53 +---------------------------------+
54 | |
55 | V
56 +--------------+ redisplay +----------------+
57 | Lisp machine |---------------->| Redisplay code |<--+
58 +--------------+ (xdisp.c) +----------------+ |
59 ^ | |
60 +----------------------------------+ |
61 Don't use this path when called |
62 asynchronously! |
63 |
64 expose_window (asynchronous) |
65 |
66 X expose events -----+
67
68 What does redisplay do? Obviously, it has to figure out somehow what
69 has been changed since the last time the display has been updated,
70 and to make these changes visible. Preferably it would do that in
71 a moderately intelligent way, i.e. fast.
72
73 Changes in buffer text can be deduced from window and buffer
74 structures, and from some global variables like `beg_unchanged' and
75 `end_unchanged'. The contents of the display are additionally
76 recorded in a `glyph matrix', a two-dimensional matrix of glyph
77 structures. Each row in such a matrix corresponds to a line on the
78 display, and each glyph in a row corresponds to a column displaying
79 a character, an image, or what else. This matrix is called the
80 `current glyph matrix' or `current matrix' in redisplay
81 terminology.
82
83 For buffer parts that have been changed since the last update, a
84 second glyph matrix is constructed, the so called `desired glyph
85 matrix' or short `desired matrix'. Current and desired matrix are
86 then compared to find a cheap way to update the display, e.g. by
87 reusing part of the display by scrolling lines.
88
89
90 Direct operations.
91
92 You will find a lot of redisplay optimizations when you start
93 looking at the innards of redisplay. The overall goal of all these
94 optimizations is to make redisplay fast because it is done
95 frequently.
96
97 Two optimizations are not found in xdisp.c. These are the direct
98 operations mentioned above. As the name suggests they follow a
99 different principle than the rest of redisplay. Instead of
100 building a desired matrix and then comparing it with the current
101 display, they perform their actions directly on the display and on
102 the current matrix.
103
104 One direct operation updates the display after one character has
105 been entered. The other one moves the cursor by one position
106 forward or backward. You find these functions under the names
107 `direct_output_for_insert' and `direct_output_forward_char' in
108 dispnew.c.
109
110
111 Desired matrices.
112
113 Desired matrices are always built per Emacs window. The function
114 `display_line' is the central function to look at if you are
115 interested. It constructs one row in a desired matrix given an
116 iterator structure containing both a buffer position and a
117 description of the environment in which the text is to be
118 displayed. But this is too early, read on.
119
120 Characters and pixmaps displayed for a range of buffer text depend
121 on various settings of buffers and windows, on overlays and text
122 properties, on display tables, on selective display. The good news
123 is that all this hairy stuff is hidden behind a small set of
124 interface functions taking an iterator structure (struct it)
125 argument.
126
127 Iteration over things to be displayed is then simple. It is
128 started by initializing an iterator with a call to init_iterator.
129 Calls to get_next_display_element fill the iterator structure with
130 relevant information about the next thing to display. Calls to
131 set_iterator_to_next move the iterator to the next thing.
132
133 Besides this, an iterator also contains information about the
134 display environment in which glyphs for display elements are to be
135 produced. It has fields for the width and height of the display,
136 the information whether long lines are truncated or continued, a
137 current X and Y position, and lots of other stuff you can better
138 see in dispextern.h.
139
140 Glyphs in a desired matrix are normally constructed in a loop
141 calling get_next_display_element and then produce_glyphs. The call
142 to produce_glyphs will fill the iterator structure with pixel
143 information about the element being displayed and at the same time
144 produce glyphs for it. If the display element fits on the line
145 being displayed, set_iterator_to_next is called next, otherwise the
146 glyphs produced are discarded.
147
148
149 Frame matrices.
150
151 That just couldn't be all, could it? What about terminal types not
152 supporting operations on sub-windows of the screen? To update the
153 display on such a terminal, window-based glyph matrices are not
154 well suited. To be able to reuse part of the display (scrolling
155 lines up and down), we must instead have a view of the whole
156 screen. This is what `frame matrices' are for. They are a trick.
157
158 Frames on terminals like above have a glyph pool. Windows on such
159 a frame sub-allocate their glyph memory from their frame's glyph
160 pool. The frame itself is given its own glyph matrices. By
161 coincidence---or maybe something else---rows in window glyph
162 matrices are slices of corresponding rows in frame matrices. Thus
163 writing to window matrices implicitly updates a frame matrix which
164 provides us with the view of the whole screen that we originally
165 wanted to have without having to move many bytes around. To be
166 honest, there is a little bit more done, but not much more. If you
167 plan to extend that code, take a look at dispnew.c. The function
168 build_frame_matrix is a good starting point. */
169
170 #include <config.h>
171 #include <stdio.h>
172
173 #include "lisp.h"
174 #include "keyboard.h"
175 #include "frame.h"
176 #include "window.h"
177 #include "termchar.h"
178 #include "dispextern.h"
179 #include "buffer.h"
180 #include "charset.h"
181 #include "indent.h"
182 #include "commands.h"
183 #include "keymap.h"
184 #include "macros.h"
185 #include "disptab.h"
186 #include "termhooks.h"
187 #include "intervals.h"
188 #include "coding.h"
189 #include "process.h"
190 #include "region-cache.h"
191 #include "fontset.h"
192 #include "blockinput.h"
193
194 #ifdef HAVE_X_WINDOWS
195 #include "xterm.h"
196 #endif
197 #ifdef WINDOWSNT
198 #include "w32term.h"
199 #endif
200 #ifdef MAC_OS
201 #include "macterm.h"
202 #endif
203
204 #ifndef FRAME_X_OUTPUT
205 #define FRAME_X_OUTPUT(f) ((f)->output_data.x)
206 #endif
207
208 #define INFINITY 10000000
209
210 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
211 || defined (USE_GTK)
212 extern void set_frame_menubar P_ ((struct frame *f, int, int));
213 extern int pending_menu_activation;
214 #endif
215
216 extern int interrupt_input;
217 extern int command_loop_level;
218
219 extern Lisp_Object do_mouse_tracking;
220
221 extern int minibuffer_auto_raise;
222 extern Lisp_Object Vminibuffer_list;
223
224 extern Lisp_Object Qface;
225 extern Lisp_Object Qmode_line, Qmode_line_inactive, Qheader_line;
226
227 extern Lisp_Object Voverriding_local_map;
228 extern Lisp_Object Voverriding_local_map_menu_flag;
229 extern Lisp_Object Qmenu_item;
230 extern Lisp_Object Qwhen;
231 extern Lisp_Object Qhelp_echo;
232
233 Lisp_Object Qoverriding_local_map, Qoverriding_terminal_local_map;
234 Lisp_Object Qwindow_scroll_functions, Vwindow_scroll_functions;
235 Lisp_Object Qredisplay_end_trigger_functions, Vredisplay_end_trigger_functions;
236 Lisp_Object Qinhibit_point_motion_hooks;
237 Lisp_Object QCeval, QCfile, QCdata, QCpropertize;
238 Lisp_Object Qfontified;
239 Lisp_Object Qgrow_only;
240 Lisp_Object Qinhibit_eval_during_redisplay;
241 Lisp_Object Qbuffer_position, Qposition, Qobject;
242
243 /* Cursor shapes */
244 Lisp_Object Qbar, Qhbar, Qbox, Qhollow;
245
246 /* Pointer shapes */
247 Lisp_Object Qarrow, Qhand, Qtext;
248
249 Lisp_Object Qrisky_local_variable;
250
251 /* Holds the list (error). */
252 Lisp_Object list_of_error;
253
254 /* Functions called to fontify regions of text. */
255
256 Lisp_Object Vfontification_functions;
257 Lisp_Object Qfontification_functions;
258
259 /* Non-zero means automatically select any window when the mouse
260 cursor moves into it. */
261 int mouse_autoselect_window;
262
263 /* Non-zero means draw tool bar buttons raised when the mouse moves
264 over them. */
265
266 int auto_raise_tool_bar_buttons_p;
267
268 /* Non-zero means to reposition window if cursor line is only partially visible. */
269
270 int make_cursor_line_fully_visible_p;
271
272 /* Margin around tool bar buttons in pixels. */
273
274 Lisp_Object Vtool_bar_button_margin;
275
276 /* Thickness of shadow to draw around tool bar buttons. */
277
278 EMACS_INT tool_bar_button_relief;
279
280 /* Non-zero means automatically resize tool-bars so that all tool-bar
281 items are visible, and no blank lines remain. */
282
283 int auto_resize_tool_bars_p;
284
285 /* Non-zero means draw block and hollow cursor as wide as the glyph
286 under it. For example, if a block cursor is over a tab, it will be
287 drawn as wide as that tab on the display. */
288
289 int x_stretch_cursor_p;
290
291 /* Non-nil means don't actually do any redisplay. */
292
293 Lisp_Object Vinhibit_redisplay, Qinhibit_redisplay;
294
295 /* Non-zero means Lisp evaluation during redisplay is inhibited. */
296
297 int inhibit_eval_during_redisplay;
298
299 /* Names of text properties relevant for redisplay. */
300
301 Lisp_Object Qdisplay;
302 extern Lisp_Object Qface, Qinvisible, Qwidth;
303
304 /* Symbols used in text property values. */
305
306 Lisp_Object Vdisplay_pixels_per_inch;
307 Lisp_Object Qspace, QCalign_to, QCrelative_width, QCrelative_height;
308 Lisp_Object Qleft_margin, Qright_margin, Qspace_width, Qraise;
309 Lisp_Object Qslice;
310 Lisp_Object Qcenter;
311 Lisp_Object Qmargin, Qpointer;
312 Lisp_Object Qline_height;
313 extern Lisp_Object Qheight;
314 extern Lisp_Object QCwidth, QCheight, QCascent;
315 extern Lisp_Object Qscroll_bar;
316 extern Lisp_Object Qcursor;
317
318 /* Non-nil means highlight trailing whitespace. */
319
320 Lisp_Object Vshow_trailing_whitespace;
321
322 /* Non-nil means escape non-break space and hyphens. */
323
324 Lisp_Object Vnobreak_char_display;
325
326 #ifdef HAVE_WINDOW_SYSTEM
327 extern Lisp_Object Voverflow_newline_into_fringe;
328
329 /* Test if overflow newline into fringe. Called with iterator IT
330 at or past right window margin, and with IT->current_x set. */
331
332 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(it) \
333 (!NILP (Voverflow_newline_into_fringe) \
334 && FRAME_WINDOW_P (it->f) \
335 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) > 0 \
336 && it->current_x == it->last_visible_x)
337
338 #endif /* HAVE_WINDOW_SYSTEM */
339
340 /* Non-nil means show the text cursor in void text areas
341 i.e. in blank areas after eol and eob. This used to be
342 the default in 21.3. */
343
344 Lisp_Object Vvoid_text_area_pointer;
345
346 /* Name of the face used to highlight trailing whitespace. */
347
348 Lisp_Object Qtrailing_whitespace;
349
350 /* Name and number of the face used to highlight escape glyphs. */
351
352 Lisp_Object Qescape_glyph;
353
354 /* Name and number of the face used to highlight non-breaking spaces. */
355
356 Lisp_Object Qnobreak_space;
357
358 /* The symbol `image' which is the car of the lists used to represent
359 images in Lisp. */
360
361 Lisp_Object Qimage;
362
363 /* The image map types. */
364 Lisp_Object QCmap, QCpointer;
365 Lisp_Object Qrect, Qcircle, Qpoly;
366
367 /* Non-zero means print newline to stdout before next mini-buffer
368 message. */
369
370 int noninteractive_need_newline;
371
372 /* Non-zero means print newline to message log before next message. */
373
374 static int message_log_need_newline;
375
376 /* Three markers that message_dolog uses.
377 It could allocate them itself, but that causes trouble
378 in handling memory-full errors. */
379 static Lisp_Object message_dolog_marker1;
380 static Lisp_Object message_dolog_marker2;
381 static Lisp_Object message_dolog_marker3;
382 \f
383 /* The buffer position of the first character appearing entirely or
384 partially on the line of the selected window which contains the
385 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
386 redisplay optimization in redisplay_internal. */
387
388 static struct text_pos this_line_start_pos;
389
390 /* Number of characters past the end of the line above, including the
391 terminating newline. */
392
393 static struct text_pos this_line_end_pos;
394
395 /* The vertical positions and the height of this line. */
396
397 static int this_line_vpos;
398 static int this_line_y;
399 static int this_line_pixel_height;
400
401 /* X position at which this display line starts. Usually zero;
402 negative if first character is partially visible. */
403
404 static int this_line_start_x;
405
406 /* Buffer that this_line_.* variables are referring to. */
407
408 static struct buffer *this_line_buffer;
409
410 /* Nonzero means truncate lines in all windows less wide than the
411 frame. */
412
413 int truncate_partial_width_windows;
414
415 /* A flag to control how to display unibyte 8-bit character. */
416
417 int unibyte_display_via_language_environment;
418
419 /* Nonzero means we have more than one non-mini-buffer-only frame.
420 Not guaranteed to be accurate except while parsing
421 frame-title-format. */
422
423 int multiple_frames;
424
425 Lisp_Object Vglobal_mode_string;
426
427
428 /* List of variables (symbols) which hold markers for overlay arrows.
429 The symbols on this list are examined during redisplay to determine
430 where to display overlay arrows. */
431
432 Lisp_Object Voverlay_arrow_variable_list;
433
434 /* Marker for where to display an arrow on top of the buffer text. */
435
436 Lisp_Object Voverlay_arrow_position;
437
438 /* String to display for the arrow. Only used on terminal frames. */
439
440 Lisp_Object Voverlay_arrow_string;
441
442 /* Values of those variables at last redisplay are stored as
443 properties on `overlay-arrow-position' symbol. However, if
444 Voverlay_arrow_position is a marker, last-arrow-position is its
445 numerical position. */
446
447 Lisp_Object Qlast_arrow_position, Qlast_arrow_string;
448
449 /* Alternative overlay-arrow-string and overlay-arrow-bitmap
450 properties on a symbol in overlay-arrow-variable-list. */
451
452 Lisp_Object Qoverlay_arrow_string, Qoverlay_arrow_bitmap;
453
454 /* Like mode-line-format, but for the title bar on a visible frame. */
455
456 Lisp_Object Vframe_title_format;
457
458 /* Like mode-line-format, but for the title bar on an iconified frame. */
459
460 Lisp_Object Vicon_title_format;
461
462 /* List of functions to call when a window's size changes. These
463 functions get one arg, a frame on which one or more windows' sizes
464 have changed. */
465
466 static Lisp_Object Vwindow_size_change_functions;
467
468 Lisp_Object Qmenu_bar_update_hook, Vmenu_bar_update_hook;
469
470 /* Nonzero if an overlay arrow has been displayed in this window. */
471
472 static int overlay_arrow_seen;
473
474 /* Nonzero means highlight the region even in nonselected windows. */
475
476 int highlight_nonselected_windows;
477
478 /* If cursor motion alone moves point off frame, try scrolling this
479 many lines up or down if that will bring it back. */
480
481 static EMACS_INT scroll_step;
482
483 /* Nonzero means scroll just far enough to bring point back on the
484 screen, when appropriate. */
485
486 static EMACS_INT scroll_conservatively;
487
488 /* Recenter the window whenever point gets within this many lines of
489 the top or bottom of the window. This value is translated into a
490 pixel value by multiplying it with FRAME_LINE_HEIGHT, which means
491 that there is really a fixed pixel height scroll margin. */
492
493 EMACS_INT scroll_margin;
494
495 /* Number of windows showing the buffer of the selected window (or
496 another buffer with the same base buffer). keyboard.c refers to
497 this. */
498
499 int buffer_shared;
500
501 /* Vector containing glyphs for an ellipsis `...'. */
502
503 static Lisp_Object default_invis_vector[3];
504
505 /* Zero means display the mode-line/header-line/menu-bar in the default face
506 (this slightly odd definition is for compatibility with previous versions
507 of emacs), non-zero means display them using their respective faces.
508
509 This variable is deprecated. */
510
511 int mode_line_inverse_video;
512
513 /* Prompt to display in front of the mini-buffer contents. */
514
515 Lisp_Object minibuf_prompt;
516
517 /* Width of current mini-buffer prompt. Only set after display_line
518 of the line that contains the prompt. */
519
520 int minibuf_prompt_width;
521
522 /* This is the window where the echo area message was displayed. It
523 is always a mini-buffer window, but it may not be the same window
524 currently active as a mini-buffer. */
525
526 Lisp_Object echo_area_window;
527
528 /* List of pairs (MESSAGE . MULTIBYTE). The function save_message
529 pushes the current message and the value of
530 message_enable_multibyte on the stack, the function restore_message
531 pops the stack and displays MESSAGE again. */
532
533 Lisp_Object Vmessage_stack;
534
535 /* Nonzero means multibyte characters were enabled when the echo area
536 message was specified. */
537
538 int message_enable_multibyte;
539
540 /* Nonzero if we should redraw the mode lines on the next redisplay. */
541
542 int update_mode_lines;
543
544 /* Nonzero if window sizes or contents have changed since last
545 redisplay that finished. */
546
547 int windows_or_buffers_changed;
548
549 /* Nonzero means a frame's cursor type has been changed. */
550
551 int cursor_type_changed;
552
553 /* Nonzero after display_mode_line if %l was used and it displayed a
554 line number. */
555
556 int line_number_displayed;
557
558 /* Maximum buffer size for which to display line numbers. */
559
560 Lisp_Object Vline_number_display_limit;
561
562 /* Line width to consider when repositioning for line number display. */
563
564 static EMACS_INT line_number_display_limit_width;
565
566 /* Number of lines to keep in the message log buffer. t means
567 infinite. nil means don't log at all. */
568
569 Lisp_Object Vmessage_log_max;
570
571 /* The name of the *Messages* buffer, a string. */
572
573 static Lisp_Object Vmessages_buffer_name;
574
575 /* Current, index 0, and last displayed echo area message. Either
576 buffers from echo_buffers, or nil to indicate no message. */
577
578 Lisp_Object echo_area_buffer[2];
579
580 /* The buffers referenced from echo_area_buffer. */
581
582 static Lisp_Object echo_buffer[2];
583
584 /* A vector saved used in with_area_buffer to reduce consing. */
585
586 static Lisp_Object Vwith_echo_area_save_vector;
587
588 /* Non-zero means display_echo_area should display the last echo area
589 message again. Set by redisplay_preserve_echo_area. */
590
591 static int display_last_displayed_message_p;
592
593 /* Nonzero if echo area is being used by print; zero if being used by
594 message. */
595
596 int message_buf_print;
597
598 /* The symbol `inhibit-menubar-update' and its DEFVAR_BOOL variable. */
599
600 Lisp_Object Qinhibit_menubar_update;
601 int inhibit_menubar_update;
602
603 /* Maximum height for resizing mini-windows. Either a float
604 specifying a fraction of the available height, or an integer
605 specifying a number of lines. */
606
607 Lisp_Object Vmax_mini_window_height;
608
609 /* Non-zero means messages should be displayed with truncated
610 lines instead of being continued. */
611
612 int message_truncate_lines;
613 Lisp_Object Qmessage_truncate_lines;
614
615 /* Set to 1 in clear_message to make redisplay_internal aware
616 of an emptied echo area. */
617
618 static int message_cleared_p;
619
620 /* How to blink the default frame cursor off. */
621 Lisp_Object Vblink_cursor_alist;
622
623 /* A scratch glyph row with contents used for generating truncation
624 glyphs. Also used in direct_output_for_insert. */
625
626 #define MAX_SCRATCH_GLYPHS 100
627 struct glyph_row scratch_glyph_row;
628 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
629
630 /* Ascent and height of the last line processed by move_it_to. */
631
632 static int last_max_ascent, last_height;
633
634 /* Non-zero if there's a help-echo in the echo area. */
635
636 int help_echo_showing_p;
637
638 /* If >= 0, computed, exact values of mode-line and header-line height
639 to use in the macros CURRENT_MODE_LINE_HEIGHT and
640 CURRENT_HEADER_LINE_HEIGHT. */
641
642 int current_mode_line_height, current_header_line_height;
643
644 /* The maximum distance to look ahead for text properties. Values
645 that are too small let us call compute_char_face and similar
646 functions too often which is expensive. Values that are too large
647 let us call compute_char_face and alike too often because we
648 might not be interested in text properties that far away. */
649
650 #define TEXT_PROP_DISTANCE_LIMIT 100
651
652 #if GLYPH_DEBUG
653
654 /* Variables to turn off display optimizations from Lisp. */
655
656 int inhibit_try_window_id, inhibit_try_window_reusing;
657 int inhibit_try_cursor_movement;
658
659 /* Non-zero means print traces of redisplay if compiled with
660 GLYPH_DEBUG != 0. */
661
662 int trace_redisplay_p;
663
664 #endif /* GLYPH_DEBUG */
665
666 #ifdef DEBUG_TRACE_MOVE
667 /* Non-zero means trace with TRACE_MOVE to stderr. */
668 int trace_move;
669
670 #define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
671 #else
672 #define TRACE_MOVE(x) (void) 0
673 #endif
674
675 /* Non-zero means automatically scroll windows horizontally to make
676 point visible. */
677
678 int automatic_hscrolling_p;
679
680 /* How close to the margin can point get before the window is scrolled
681 horizontally. */
682 EMACS_INT hscroll_margin;
683
684 /* How much to scroll horizontally when point is inside the above margin. */
685 Lisp_Object Vhscroll_step;
686
687 /* The variable `resize-mini-windows'. If nil, don't resize
688 mini-windows. If t, always resize them to fit the text they
689 display. If `grow-only', let mini-windows grow only until they
690 become empty. */
691
692 Lisp_Object Vresize_mini_windows;
693
694 /* Buffer being redisplayed -- for redisplay_window_error. */
695
696 struct buffer *displayed_buffer;
697
698 /* Value returned from text property handlers (see below). */
699
700 enum prop_handled
701 {
702 HANDLED_NORMALLY,
703 HANDLED_RECOMPUTE_PROPS,
704 HANDLED_OVERLAY_STRING_CONSUMED,
705 HANDLED_RETURN
706 };
707
708 /* A description of text properties that redisplay is interested
709 in. */
710
711 struct props
712 {
713 /* The name of the property. */
714 Lisp_Object *name;
715
716 /* A unique index for the property. */
717 enum prop_idx idx;
718
719 /* A handler function called to set up iterator IT from the property
720 at IT's current position. Value is used to steer handle_stop. */
721 enum prop_handled (*handler) P_ ((struct it *it));
722 };
723
724 static enum prop_handled handle_face_prop P_ ((struct it *));
725 static enum prop_handled handle_invisible_prop P_ ((struct it *));
726 static enum prop_handled handle_display_prop P_ ((struct it *));
727 static enum prop_handled handle_composition_prop P_ ((struct it *));
728 static enum prop_handled handle_overlay_change P_ ((struct it *));
729 static enum prop_handled handle_fontified_prop P_ ((struct it *));
730
731 /* Properties handled by iterators. */
732
733 static struct props it_props[] =
734 {
735 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
736 /* Handle `face' before `display' because some sub-properties of
737 `display' need to know the face. */
738 {&Qface, FACE_PROP_IDX, handle_face_prop},
739 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
740 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
741 {&Qcomposition, COMPOSITION_PROP_IDX, handle_composition_prop},
742 {NULL, 0, NULL}
743 };
744
745 /* Value is the position described by X. If X is a marker, value is
746 the marker_position of X. Otherwise, value is X. */
747
748 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
749
750 /* Enumeration returned by some move_it_.* functions internally. */
751
752 enum move_it_result
753 {
754 /* Not used. Undefined value. */
755 MOVE_UNDEFINED,
756
757 /* Move ended at the requested buffer position or ZV. */
758 MOVE_POS_MATCH_OR_ZV,
759
760 /* Move ended at the requested X pixel position. */
761 MOVE_X_REACHED,
762
763 /* Move within a line ended at the end of a line that must be
764 continued. */
765 MOVE_LINE_CONTINUED,
766
767 /* Move within a line ended at the end of a line that would
768 be displayed truncated. */
769 MOVE_LINE_TRUNCATED,
770
771 /* Move within a line ended at a line end. */
772 MOVE_NEWLINE_OR_CR
773 };
774
775 /* This counter is used to clear the face cache every once in a while
776 in redisplay_internal. It is incremented for each redisplay.
777 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
778 cleared. */
779
780 #define CLEAR_FACE_CACHE_COUNT 500
781 static int clear_face_cache_count;
782
783 /* Similarly for the image cache. */
784
785 #ifdef HAVE_WINDOW_SYSTEM
786 #define CLEAR_IMAGE_CACHE_COUNT 101
787 static int clear_image_cache_count;
788 #endif
789
790 /* Non-zero while redisplay_internal is in progress. */
791
792 int redisplaying_p;
793
794 /* Non-zero means don't free realized faces. Bound while freeing
795 realized faces is dangerous because glyph matrices might still
796 reference them. */
797
798 int inhibit_free_realized_faces;
799 Lisp_Object Qinhibit_free_realized_faces;
800
801 /* If a string, XTread_socket generates an event to display that string.
802 (The display is done in read_char.) */
803
804 Lisp_Object help_echo_string;
805 Lisp_Object help_echo_window;
806 Lisp_Object help_echo_object;
807 int help_echo_pos;
808
809 /* Temporary variable for XTread_socket. */
810
811 Lisp_Object previous_help_echo_string;
812
813 /* Null glyph slice */
814
815 static struct glyph_slice null_glyph_slice = { 0, 0, 0, 0 };
816
817 \f
818 /* Function prototypes. */
819
820 static void setup_for_ellipsis P_ ((struct it *, int));
821 static void mark_window_display_accurate_1 P_ ((struct window *, int));
822 static int single_display_spec_string_p P_ ((Lisp_Object, Lisp_Object));
823 static int display_prop_string_p P_ ((Lisp_Object, Lisp_Object));
824 static int cursor_row_p P_ ((struct window *, struct glyph_row *));
825 static int redisplay_mode_lines P_ ((Lisp_Object, int));
826 static char *decode_mode_spec_coding P_ ((Lisp_Object, char *, int));
827
828 #if 0
829 static int invisible_text_between_p P_ ((struct it *, int, int));
830 #endif
831
832 static void pint2str P_ ((char *, int, int));
833 static void pint2hrstr P_ ((char *, int, int));
834 static struct text_pos run_window_scroll_functions P_ ((Lisp_Object,
835 struct text_pos));
836 static void reconsider_clip_changes P_ ((struct window *, struct buffer *));
837 static int text_outside_line_unchanged_p P_ ((struct window *, int, int));
838 static void store_mode_line_noprop_char P_ ((char));
839 static int store_mode_line_noprop P_ ((const unsigned char *, int, int));
840 static void x_consider_frame_title P_ ((Lisp_Object));
841 static void handle_stop P_ ((struct it *));
842 static int tool_bar_lines_needed P_ ((struct frame *));
843 static int single_display_spec_intangible_p P_ ((Lisp_Object));
844 static void ensure_echo_area_buffers P_ ((void));
845 static Lisp_Object unwind_with_echo_area_buffer P_ ((Lisp_Object));
846 static Lisp_Object with_echo_area_buffer_unwind_data P_ ((struct window *));
847 static int with_echo_area_buffer P_ ((struct window *, int,
848 int (*) (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT),
849 EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
850 static void clear_garbaged_frames P_ ((void));
851 static int current_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
852 static int truncate_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
853 static int set_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
854 static int display_echo_area P_ ((struct window *));
855 static int display_echo_area_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
856 static int resize_mini_window_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
857 static Lisp_Object unwind_redisplay P_ ((Lisp_Object));
858 static int string_char_and_length P_ ((const unsigned char *, int, int *));
859 static struct text_pos display_prop_end P_ ((struct it *, Lisp_Object,
860 struct text_pos));
861 static int compute_window_start_on_continuation_line P_ ((struct window *));
862 static Lisp_Object safe_eval_handler P_ ((Lisp_Object));
863 static void insert_left_trunc_glyphs P_ ((struct it *));
864 static struct glyph_row *get_overlay_arrow_glyph_row P_ ((struct window *,
865 Lisp_Object));
866 static void extend_face_to_end_of_line P_ ((struct it *));
867 static int append_space_for_newline P_ ((struct it *, int));
868 static int cursor_row_fully_visible_p P_ ((struct window *, int, int));
869 static int try_scrolling P_ ((Lisp_Object, int, EMACS_INT, EMACS_INT, int, int));
870 static int try_cursor_movement P_ ((Lisp_Object, struct text_pos, int *));
871 static int trailing_whitespace_p P_ ((int));
872 static int message_log_check_duplicate P_ ((int, int, int, int));
873 static void push_it P_ ((struct it *));
874 static void pop_it P_ ((struct it *));
875 static void sync_frame_with_window_matrix_rows P_ ((struct window *));
876 static void select_frame_for_redisplay P_ ((Lisp_Object));
877 static void redisplay_internal P_ ((int));
878 static int echo_area_display P_ ((int));
879 static void redisplay_windows P_ ((Lisp_Object));
880 static void redisplay_window P_ ((Lisp_Object, int));
881 static Lisp_Object redisplay_window_error ();
882 static Lisp_Object redisplay_window_0 P_ ((Lisp_Object));
883 static Lisp_Object redisplay_window_1 P_ ((Lisp_Object));
884 static void update_menu_bar P_ ((struct frame *, int));
885 static int try_window_reusing_current_matrix P_ ((struct window *));
886 static int try_window_id P_ ((struct window *));
887 static int display_line P_ ((struct it *));
888 static int display_mode_lines P_ ((struct window *));
889 static int display_mode_line P_ ((struct window *, enum face_id, Lisp_Object));
890 static int display_mode_element P_ ((struct it *, int, int, int, Lisp_Object, Lisp_Object, int));
891 static int store_mode_line_string P_ ((char *, Lisp_Object, int, int, int, Lisp_Object));
892 static char *decode_mode_spec P_ ((struct window *, int, int, int, int *));
893 static void display_menu_bar P_ ((struct window *));
894 static int display_count_lines P_ ((int, int, int, int, int *));
895 static int display_string P_ ((unsigned char *, Lisp_Object, Lisp_Object,
896 int, int, struct it *, int, int, int, int));
897 static void compute_line_metrics P_ ((struct it *));
898 static void run_redisplay_end_trigger_hook P_ ((struct it *));
899 static int get_overlay_strings P_ ((struct it *, int));
900 static void next_overlay_string P_ ((struct it *));
901 static void reseat P_ ((struct it *, struct text_pos, int));
902 static void reseat_1 P_ ((struct it *, struct text_pos, int));
903 static void back_to_previous_visible_line_start P_ ((struct it *));
904 void reseat_at_previous_visible_line_start P_ ((struct it *));
905 static void reseat_at_next_visible_line_start P_ ((struct it *, int));
906 static int next_element_from_ellipsis P_ ((struct it *));
907 static int next_element_from_display_vector P_ ((struct it *));
908 static int next_element_from_string P_ ((struct it *));
909 static int next_element_from_c_string P_ ((struct it *));
910 static int next_element_from_buffer P_ ((struct it *));
911 static int next_element_from_composition P_ ((struct it *));
912 static int next_element_from_image P_ ((struct it *));
913 static int next_element_from_stretch P_ ((struct it *));
914 static void load_overlay_strings P_ ((struct it *, int));
915 static int init_from_display_pos P_ ((struct it *, struct window *,
916 struct display_pos *));
917 static void reseat_to_string P_ ((struct it *, unsigned char *,
918 Lisp_Object, int, int, int, int));
919 static enum move_it_result move_it_in_display_line_to P_ ((struct it *,
920 int, int, int));
921 void move_it_vertically_backward P_ ((struct it *, int));
922 static void init_to_row_start P_ ((struct it *, struct window *,
923 struct glyph_row *));
924 static int init_to_row_end P_ ((struct it *, struct window *,
925 struct glyph_row *));
926 static void back_to_previous_line_start P_ ((struct it *));
927 static int forward_to_next_line_start P_ ((struct it *, int *));
928 static struct text_pos string_pos_nchars_ahead P_ ((struct text_pos,
929 Lisp_Object, int));
930 static struct text_pos string_pos P_ ((int, Lisp_Object));
931 static struct text_pos c_string_pos P_ ((int, unsigned char *, int));
932 static int number_of_chars P_ ((unsigned char *, int));
933 static void compute_stop_pos P_ ((struct it *));
934 static void compute_string_pos P_ ((struct text_pos *, struct text_pos,
935 Lisp_Object));
936 static int face_before_or_after_it_pos P_ ((struct it *, int));
937 static int next_overlay_change P_ ((int));
938 static int handle_single_display_spec P_ ((struct it *, Lisp_Object,
939 Lisp_Object, struct text_pos *,
940 int));
941 static int underlying_face_id P_ ((struct it *));
942 static int in_ellipses_for_invisible_text_p P_ ((struct display_pos *,
943 struct window *));
944
945 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
946 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
947
948 #ifdef HAVE_WINDOW_SYSTEM
949
950 static void update_tool_bar P_ ((struct frame *, int));
951 static void build_desired_tool_bar_string P_ ((struct frame *f));
952 static int redisplay_tool_bar P_ ((struct frame *));
953 static void display_tool_bar_line P_ ((struct it *));
954 static void notice_overwritten_cursor P_ ((struct window *,
955 enum glyph_row_area,
956 int, int, int, int));
957
958
959
960 #endif /* HAVE_WINDOW_SYSTEM */
961
962 \f
963 /***********************************************************************
964 Window display dimensions
965 ***********************************************************************/
966
967 /* Return the bottom boundary y-position for text lines in window W.
968 This is the first y position at which a line cannot start.
969 It is relative to the top of the window.
970
971 This is the height of W minus the height of a mode line, if any. */
972
973 INLINE int
974 window_text_bottom_y (w)
975 struct window *w;
976 {
977 int height = WINDOW_TOTAL_HEIGHT (w);
978
979 if (WINDOW_WANTS_MODELINE_P (w))
980 height -= CURRENT_MODE_LINE_HEIGHT (w);
981 return height;
982 }
983
984 /* Return the pixel width of display area AREA of window W. AREA < 0
985 means return the total width of W, not including fringes to
986 the left and right of the window. */
987
988 INLINE int
989 window_box_width (w, area)
990 struct window *w;
991 int area;
992 {
993 int cols = XFASTINT (w->total_cols);
994 int pixels = 0;
995
996 if (!w->pseudo_window_p)
997 {
998 cols -= WINDOW_SCROLL_BAR_COLS (w);
999
1000 if (area == TEXT_AREA)
1001 {
1002 if (INTEGERP (w->left_margin_cols))
1003 cols -= XFASTINT (w->left_margin_cols);
1004 if (INTEGERP (w->right_margin_cols))
1005 cols -= XFASTINT (w->right_margin_cols);
1006 pixels = -WINDOW_TOTAL_FRINGE_WIDTH (w);
1007 }
1008 else if (area == LEFT_MARGIN_AREA)
1009 {
1010 cols = (INTEGERP (w->left_margin_cols)
1011 ? XFASTINT (w->left_margin_cols) : 0);
1012 pixels = 0;
1013 }
1014 else if (area == RIGHT_MARGIN_AREA)
1015 {
1016 cols = (INTEGERP (w->right_margin_cols)
1017 ? XFASTINT (w->right_margin_cols) : 0);
1018 pixels = 0;
1019 }
1020 }
1021
1022 return cols * WINDOW_FRAME_COLUMN_WIDTH (w) + pixels;
1023 }
1024
1025
1026 /* Return the pixel height of the display area of window W, not
1027 including mode lines of W, if any. */
1028
1029 INLINE int
1030 window_box_height (w)
1031 struct window *w;
1032 {
1033 struct frame *f = XFRAME (w->frame);
1034 int height = WINDOW_TOTAL_HEIGHT (w);
1035
1036 xassert (height >= 0);
1037
1038 /* Note: the code below that determines the mode-line/header-line
1039 height is essentially the same as that contained in the macro
1040 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
1041 the appropriate glyph row has its `mode_line_p' flag set,
1042 and if it doesn't, uses estimate_mode_line_height instead. */
1043
1044 if (WINDOW_WANTS_MODELINE_P (w))
1045 {
1046 struct glyph_row *ml_row
1047 = (w->current_matrix && w->current_matrix->rows
1048 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
1049 : 0);
1050 if (ml_row && ml_row->mode_line_p)
1051 height -= ml_row->height;
1052 else
1053 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
1054 }
1055
1056 if (WINDOW_WANTS_HEADER_LINE_P (w))
1057 {
1058 struct glyph_row *hl_row
1059 = (w->current_matrix && w->current_matrix->rows
1060 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
1061 : 0);
1062 if (hl_row && hl_row->mode_line_p)
1063 height -= hl_row->height;
1064 else
1065 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
1066 }
1067
1068 /* With a very small font and a mode-line that's taller than
1069 default, we might end up with a negative height. */
1070 return max (0, height);
1071 }
1072
1073 /* Return the window-relative coordinate of the left edge of display
1074 area AREA of window W. AREA < 0 means return the left edge of the
1075 whole window, to the right of the left fringe of W. */
1076
1077 INLINE int
1078 window_box_left_offset (w, area)
1079 struct window *w;
1080 int area;
1081 {
1082 int x;
1083
1084 if (w->pseudo_window_p)
1085 return 0;
1086
1087 x = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
1088
1089 if (area == TEXT_AREA)
1090 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1091 + window_box_width (w, LEFT_MARGIN_AREA));
1092 else if (area == RIGHT_MARGIN_AREA)
1093 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1094 + window_box_width (w, LEFT_MARGIN_AREA)
1095 + window_box_width (w, TEXT_AREA)
1096 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1097 ? 0
1098 : WINDOW_RIGHT_FRINGE_WIDTH (w)));
1099 else if (area == LEFT_MARGIN_AREA
1100 && WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1101 x += WINDOW_LEFT_FRINGE_WIDTH (w);
1102
1103 return x;
1104 }
1105
1106
1107 /* Return the window-relative coordinate of the right edge of display
1108 area AREA of window W. AREA < 0 means return the left edge of the
1109 whole window, to the left of the right fringe of W. */
1110
1111 INLINE int
1112 window_box_right_offset (w, area)
1113 struct window *w;
1114 int area;
1115 {
1116 return window_box_left_offset (w, area) + window_box_width (w, area);
1117 }
1118
1119 /* Return the frame-relative coordinate of the left edge of display
1120 area AREA of window W. AREA < 0 means return the left edge of the
1121 whole window, to the right of the left fringe of W. */
1122
1123 INLINE int
1124 window_box_left (w, area)
1125 struct window *w;
1126 int area;
1127 {
1128 struct frame *f = XFRAME (w->frame);
1129 int x;
1130
1131 if (w->pseudo_window_p)
1132 return FRAME_INTERNAL_BORDER_WIDTH (f);
1133
1134 x = (WINDOW_LEFT_EDGE_X (w)
1135 + window_box_left_offset (w, area));
1136
1137 return x;
1138 }
1139
1140
1141 /* Return the frame-relative coordinate of the right edge of display
1142 area AREA of window W. AREA < 0 means return the left edge of the
1143 whole window, to the left of the right fringe of W. */
1144
1145 INLINE int
1146 window_box_right (w, area)
1147 struct window *w;
1148 int area;
1149 {
1150 return window_box_left (w, area) + window_box_width (w, area);
1151 }
1152
1153 /* Get the bounding box of the display area AREA of window W, without
1154 mode lines, in frame-relative coordinates. AREA < 0 means the
1155 whole window, not including the left and right fringes of
1156 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1157 coordinates of the upper-left corner of the box. Return in
1158 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1159
1160 INLINE void
1161 window_box (w, area, box_x, box_y, box_width, box_height)
1162 struct window *w;
1163 int area;
1164 int *box_x, *box_y, *box_width, *box_height;
1165 {
1166 if (box_width)
1167 *box_width = window_box_width (w, area);
1168 if (box_height)
1169 *box_height = window_box_height (w);
1170 if (box_x)
1171 *box_x = window_box_left (w, area);
1172 if (box_y)
1173 {
1174 *box_y = WINDOW_TOP_EDGE_Y (w);
1175 if (WINDOW_WANTS_HEADER_LINE_P (w))
1176 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1177 }
1178 }
1179
1180
1181 /* Get the bounding box of the display area AREA of window W, without
1182 mode lines. AREA < 0 means the whole window, not including the
1183 left and right fringe of the window. Return in *TOP_LEFT_X
1184 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1185 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1186 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1187 box. */
1188
1189 INLINE void
1190 window_box_edges (w, area, top_left_x, top_left_y,
1191 bottom_right_x, bottom_right_y)
1192 struct window *w;
1193 int area;
1194 int *top_left_x, *top_left_y, *bottom_right_x, *bottom_right_y;
1195 {
1196 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
1197 bottom_right_y);
1198 *bottom_right_x += *top_left_x;
1199 *bottom_right_y += *top_left_y;
1200 }
1201
1202
1203 \f
1204 /***********************************************************************
1205 Utilities
1206 ***********************************************************************/
1207
1208 /* Return the bottom y-position of the line the iterator IT is in.
1209 This can modify IT's settings. */
1210
1211 int
1212 line_bottom_y (it)
1213 struct it *it;
1214 {
1215 int line_height = it->max_ascent + it->max_descent;
1216 int line_top_y = it->current_y;
1217
1218 if (line_height == 0)
1219 {
1220 if (last_height)
1221 line_height = last_height;
1222 else if (IT_CHARPOS (*it) < ZV)
1223 {
1224 move_it_by_lines (it, 1, 1);
1225 line_height = (it->max_ascent || it->max_descent
1226 ? it->max_ascent + it->max_descent
1227 : last_height);
1228 }
1229 else
1230 {
1231 struct glyph_row *row = it->glyph_row;
1232
1233 /* Use the default character height. */
1234 it->glyph_row = NULL;
1235 it->what = IT_CHARACTER;
1236 it->c = ' ';
1237 it->len = 1;
1238 PRODUCE_GLYPHS (it);
1239 line_height = it->ascent + it->descent;
1240 it->glyph_row = row;
1241 }
1242 }
1243
1244 return line_top_y + line_height;
1245 }
1246
1247
1248 /* Return 1 if position CHARPOS is visible in window W.
1249 If visible, set *X and *Y to pixel coordinates of top left corner.
1250 Set *RTOP and *RBOT to pixel height of an invisible area of glyph at POS.
1251 EXACT_MODE_LINE_HEIGHTS_P non-zero means compute exact mode-line
1252 and header-lines heights. */
1253
1254 int
1255 pos_visible_p (w, charpos, x, y, rtop, rbot, exact_mode_line_heights_p)
1256 struct window *w;
1257 int charpos, *x, *y, *rtop, *rbot, exact_mode_line_heights_p;
1258 {
1259 struct it it;
1260 struct text_pos top;
1261 int visible_p = 0;
1262 struct buffer *old_buffer = NULL;
1263
1264 if (noninteractive)
1265 return visible_p;
1266
1267 if (XBUFFER (w->buffer) != current_buffer)
1268 {
1269 old_buffer = current_buffer;
1270 set_buffer_internal_1 (XBUFFER (w->buffer));
1271 }
1272
1273 SET_TEXT_POS_FROM_MARKER (top, w->start);
1274
1275 /* Compute exact mode line heights, if requested. */
1276 if (exact_mode_line_heights_p)
1277 {
1278 if (WINDOW_WANTS_MODELINE_P (w))
1279 current_mode_line_height
1280 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1281 current_buffer->mode_line_format);
1282
1283 if (WINDOW_WANTS_HEADER_LINE_P (w))
1284 current_header_line_height
1285 = display_mode_line (w, HEADER_LINE_FACE_ID,
1286 current_buffer->header_line_format);
1287 }
1288
1289 start_display (&it, w, top);
1290 move_it_to (&it, charpos, -1, it.last_visible_y, -1,
1291 MOVE_TO_POS | MOVE_TO_Y);
1292
1293 /* Note that we may overshoot because of invisible text. */
1294 if (IT_CHARPOS (it) >= charpos)
1295 {
1296 int top_x = it.current_x;
1297 int top_y = it.current_y;
1298 int bottom_y = (last_height = 0, line_bottom_y (&it));
1299 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
1300
1301 if (top_y < window_top_y)
1302 visible_p = bottom_y > window_top_y;
1303 else if (top_y < it.last_visible_y)
1304 visible_p = 1;
1305 if (visible_p)
1306 {
1307 *x = top_x;
1308 *y = max (top_y + max (0, it.max_ascent - it.ascent), window_top_y);
1309 *rtop = max (0, window_top_y - top_y);
1310 *rbot = max (0, bottom_y - it.last_visible_y);
1311 }
1312 }
1313 else
1314 {
1315 struct it it2;
1316
1317 it2 = it;
1318 if (IT_CHARPOS (it) < ZV && FETCH_BYTE (IT_BYTEPOS (it)) != '\n')
1319 move_it_by_lines (&it, 1, 0);
1320 if (charpos < IT_CHARPOS (it))
1321 {
1322 visible_p = 1;
1323 move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS);
1324 *x = it2.current_x;
1325 *y = it2.current_y + it2.max_ascent - it2.ascent;
1326 *rtop = max (0, -it2.current_y);
1327 *rbot = max (0, ((it2.current_y + it2.max_ascent + it2.max_descent)
1328 - it.last_visible_y));
1329 }
1330 }
1331
1332 if (old_buffer)
1333 set_buffer_internal_1 (old_buffer);
1334
1335 current_header_line_height = current_mode_line_height = -1;
1336
1337 if (visible_p && XFASTINT (w->hscroll) > 0)
1338 *x -= XFASTINT (w->hscroll) * WINDOW_FRAME_COLUMN_WIDTH (w);
1339
1340 return visible_p;
1341 }
1342
1343
1344 /* Return the next character from STR which is MAXLEN bytes long.
1345 Return in *LEN the length of the character. This is like
1346 STRING_CHAR_AND_LENGTH but never returns an invalid character. If
1347 we find one, we return a `?', but with the length of the invalid
1348 character. */
1349
1350 static INLINE int
1351 string_char_and_length (str, maxlen, len)
1352 const unsigned char *str;
1353 int maxlen, *len;
1354 {
1355 int c;
1356
1357 c = STRING_CHAR_AND_LENGTH (str, maxlen, *len);
1358 if (!CHAR_VALID_P (c, 1))
1359 /* We may not change the length here because other places in Emacs
1360 don't use this function, i.e. they silently accept invalid
1361 characters. */
1362 c = '?';
1363
1364 return c;
1365 }
1366
1367
1368
1369 /* Given a position POS containing a valid character and byte position
1370 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1371
1372 static struct text_pos
1373 string_pos_nchars_ahead (pos, string, nchars)
1374 struct text_pos pos;
1375 Lisp_Object string;
1376 int nchars;
1377 {
1378 xassert (STRINGP (string) && nchars >= 0);
1379
1380 if (STRING_MULTIBYTE (string))
1381 {
1382 int rest = SBYTES (string) - BYTEPOS (pos);
1383 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1384 int len;
1385
1386 while (nchars--)
1387 {
1388 string_char_and_length (p, rest, &len);
1389 p += len, rest -= len;
1390 xassert (rest >= 0);
1391 CHARPOS (pos) += 1;
1392 BYTEPOS (pos) += len;
1393 }
1394 }
1395 else
1396 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1397
1398 return pos;
1399 }
1400
1401
1402 /* Value is the text position, i.e. character and byte position,
1403 for character position CHARPOS in STRING. */
1404
1405 static INLINE struct text_pos
1406 string_pos (charpos, string)
1407 int charpos;
1408 Lisp_Object string;
1409 {
1410 struct text_pos pos;
1411 xassert (STRINGP (string));
1412 xassert (charpos >= 0);
1413 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1414 return pos;
1415 }
1416
1417
1418 /* Value is a text position, i.e. character and byte position, for
1419 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1420 means recognize multibyte characters. */
1421
1422 static struct text_pos
1423 c_string_pos (charpos, s, multibyte_p)
1424 int charpos;
1425 unsigned char *s;
1426 int multibyte_p;
1427 {
1428 struct text_pos pos;
1429
1430 xassert (s != NULL);
1431 xassert (charpos >= 0);
1432
1433 if (multibyte_p)
1434 {
1435 int rest = strlen (s), len;
1436
1437 SET_TEXT_POS (pos, 0, 0);
1438 while (charpos--)
1439 {
1440 string_char_and_length (s, rest, &len);
1441 s += len, rest -= len;
1442 xassert (rest >= 0);
1443 CHARPOS (pos) += 1;
1444 BYTEPOS (pos) += len;
1445 }
1446 }
1447 else
1448 SET_TEXT_POS (pos, charpos, charpos);
1449
1450 return pos;
1451 }
1452
1453
1454 /* Value is the number of characters in C string S. MULTIBYTE_P
1455 non-zero means recognize multibyte characters. */
1456
1457 static int
1458 number_of_chars (s, multibyte_p)
1459 unsigned char *s;
1460 int multibyte_p;
1461 {
1462 int nchars;
1463
1464 if (multibyte_p)
1465 {
1466 int rest = strlen (s), len;
1467 unsigned char *p = (unsigned char *) s;
1468
1469 for (nchars = 0; rest > 0; ++nchars)
1470 {
1471 string_char_and_length (p, rest, &len);
1472 rest -= len, p += len;
1473 }
1474 }
1475 else
1476 nchars = strlen (s);
1477
1478 return nchars;
1479 }
1480
1481
1482 /* Compute byte position NEWPOS->bytepos corresponding to
1483 NEWPOS->charpos. POS is a known position in string STRING.
1484 NEWPOS->charpos must be >= POS.charpos. */
1485
1486 static void
1487 compute_string_pos (newpos, pos, string)
1488 struct text_pos *newpos, pos;
1489 Lisp_Object string;
1490 {
1491 xassert (STRINGP (string));
1492 xassert (CHARPOS (*newpos) >= CHARPOS (pos));
1493
1494 if (STRING_MULTIBYTE (string))
1495 *newpos = string_pos_nchars_ahead (pos, string,
1496 CHARPOS (*newpos) - CHARPOS (pos));
1497 else
1498 BYTEPOS (*newpos) = CHARPOS (*newpos);
1499 }
1500
1501 /* EXPORT:
1502 Return an estimation of the pixel height of mode or top lines on
1503 frame F. FACE_ID specifies what line's height to estimate. */
1504
1505 int
1506 estimate_mode_line_height (f, face_id)
1507 struct frame *f;
1508 enum face_id face_id;
1509 {
1510 #ifdef HAVE_WINDOW_SYSTEM
1511 if (FRAME_WINDOW_P (f))
1512 {
1513 int height = FONT_HEIGHT (FRAME_FONT (f));
1514
1515 /* This function is called so early when Emacs starts that the face
1516 cache and mode line face are not yet initialized. */
1517 if (FRAME_FACE_CACHE (f))
1518 {
1519 struct face *face = FACE_FROM_ID (f, face_id);
1520 if (face)
1521 {
1522 if (face->font)
1523 height = FONT_HEIGHT (face->font);
1524 if (face->box_line_width > 0)
1525 height += 2 * face->box_line_width;
1526 }
1527 }
1528
1529 return height;
1530 }
1531 #endif
1532
1533 return 1;
1534 }
1535
1536 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1537 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1538 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
1539 not force the value into range. */
1540
1541 void
1542 pixel_to_glyph_coords (f, pix_x, pix_y, x, y, bounds, noclip)
1543 FRAME_PTR f;
1544 register int pix_x, pix_y;
1545 int *x, *y;
1546 NativeRectangle *bounds;
1547 int noclip;
1548 {
1549
1550 #ifdef HAVE_WINDOW_SYSTEM
1551 if (FRAME_WINDOW_P (f))
1552 {
1553 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1554 even for negative values. */
1555 if (pix_x < 0)
1556 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1557 if (pix_y < 0)
1558 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1559
1560 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1561 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1562
1563 if (bounds)
1564 STORE_NATIVE_RECT (*bounds,
1565 FRAME_COL_TO_PIXEL_X (f, pix_x),
1566 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1567 FRAME_COLUMN_WIDTH (f) - 1,
1568 FRAME_LINE_HEIGHT (f) - 1);
1569
1570 if (!noclip)
1571 {
1572 if (pix_x < 0)
1573 pix_x = 0;
1574 else if (pix_x > FRAME_TOTAL_COLS (f))
1575 pix_x = FRAME_TOTAL_COLS (f);
1576
1577 if (pix_y < 0)
1578 pix_y = 0;
1579 else if (pix_y > FRAME_LINES (f))
1580 pix_y = FRAME_LINES (f);
1581 }
1582 }
1583 #endif
1584
1585 *x = pix_x;
1586 *y = pix_y;
1587 }
1588
1589
1590 /* Given HPOS/VPOS in the current matrix of W, return corresponding
1591 frame-relative pixel positions in *FRAME_X and *FRAME_Y. If we
1592 can't tell the positions because W's display is not up to date,
1593 return 0. */
1594
1595 int
1596 glyph_to_pixel_coords (w, hpos, vpos, frame_x, frame_y)
1597 struct window *w;
1598 int hpos, vpos;
1599 int *frame_x, *frame_y;
1600 {
1601 #ifdef HAVE_WINDOW_SYSTEM
1602 if (FRAME_WINDOW_P (XFRAME (WINDOW_FRAME (w))))
1603 {
1604 int success_p;
1605
1606 xassert (hpos >= 0 && hpos < w->current_matrix->matrix_w);
1607 xassert (vpos >= 0 && vpos < w->current_matrix->matrix_h);
1608
1609 if (display_completed)
1610 {
1611 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
1612 struct glyph *glyph = row->glyphs[TEXT_AREA];
1613 struct glyph *end = glyph + min (hpos, row->used[TEXT_AREA]);
1614
1615 hpos = row->x;
1616 vpos = row->y;
1617 while (glyph < end)
1618 {
1619 hpos += glyph->pixel_width;
1620 ++glyph;
1621 }
1622
1623 /* If first glyph is partially visible, its first visible position is still 0. */
1624 if (hpos < 0)
1625 hpos = 0;
1626
1627 success_p = 1;
1628 }
1629 else
1630 {
1631 hpos = vpos = 0;
1632 success_p = 0;
1633 }
1634
1635 *frame_x = WINDOW_TO_FRAME_PIXEL_X (w, hpos);
1636 *frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, vpos);
1637 return success_p;
1638 }
1639 #endif
1640
1641 *frame_x = hpos;
1642 *frame_y = vpos;
1643 return 1;
1644 }
1645
1646
1647 #ifdef HAVE_WINDOW_SYSTEM
1648
1649 /* Find the glyph under window-relative coordinates X/Y in window W.
1650 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1651 strings. Return in *HPOS and *VPOS the row and column number of
1652 the glyph found. Return in *AREA the glyph area containing X.
1653 Value is a pointer to the glyph found or null if X/Y is not on
1654 text, or we can't tell because W's current matrix is not up to
1655 date. */
1656
1657 static struct glyph *
1658 x_y_to_hpos_vpos (w, x, y, hpos, vpos, dx, dy, area)
1659 struct window *w;
1660 int x, y;
1661 int *hpos, *vpos, *dx, *dy, *area;
1662 {
1663 struct glyph *glyph, *end;
1664 struct glyph_row *row = NULL;
1665 int x0, i;
1666
1667 /* Find row containing Y. Give up if some row is not enabled. */
1668 for (i = 0; i < w->current_matrix->nrows; ++i)
1669 {
1670 row = MATRIX_ROW (w->current_matrix, i);
1671 if (!row->enabled_p)
1672 return NULL;
1673 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1674 break;
1675 }
1676
1677 *vpos = i;
1678 *hpos = 0;
1679
1680 /* Give up if Y is not in the window. */
1681 if (i == w->current_matrix->nrows)
1682 return NULL;
1683
1684 /* Get the glyph area containing X. */
1685 if (w->pseudo_window_p)
1686 {
1687 *area = TEXT_AREA;
1688 x0 = 0;
1689 }
1690 else
1691 {
1692 if (x < window_box_left_offset (w, TEXT_AREA))
1693 {
1694 *area = LEFT_MARGIN_AREA;
1695 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1696 }
1697 else if (x < window_box_right_offset (w, TEXT_AREA))
1698 {
1699 *area = TEXT_AREA;
1700 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
1701 }
1702 else
1703 {
1704 *area = RIGHT_MARGIN_AREA;
1705 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1706 }
1707 }
1708
1709 /* Find glyph containing X. */
1710 glyph = row->glyphs[*area];
1711 end = glyph + row->used[*area];
1712 x -= x0;
1713 while (glyph < end && x >= glyph->pixel_width)
1714 {
1715 x -= glyph->pixel_width;
1716 ++glyph;
1717 }
1718
1719 if (glyph == end)
1720 return NULL;
1721
1722 if (dx)
1723 {
1724 *dx = x;
1725 *dy = y - (row->y + row->ascent - glyph->ascent);
1726 }
1727
1728 *hpos = glyph - row->glyphs[*area];
1729 return glyph;
1730 }
1731
1732
1733 /* EXPORT:
1734 Convert frame-relative x/y to coordinates relative to window W.
1735 Takes pseudo-windows into account. */
1736
1737 void
1738 frame_to_window_pixel_xy (w, x, y)
1739 struct window *w;
1740 int *x, *y;
1741 {
1742 if (w->pseudo_window_p)
1743 {
1744 /* A pseudo-window is always full-width, and starts at the
1745 left edge of the frame, plus a frame border. */
1746 struct frame *f = XFRAME (w->frame);
1747 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
1748 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1749 }
1750 else
1751 {
1752 *x -= WINDOW_LEFT_EDGE_X (w);
1753 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1754 }
1755 }
1756
1757 /* EXPORT:
1758 Return in RECTS[] at most N clipping rectangles for glyph string S.
1759 Return the number of stored rectangles. */
1760
1761 int
1762 get_glyph_string_clip_rects (s, rects, n)
1763 struct glyph_string *s;
1764 NativeRectangle *rects;
1765 int n;
1766 {
1767 XRectangle r;
1768
1769 if (n <= 0)
1770 return 0;
1771
1772 if (s->row->full_width_p)
1773 {
1774 /* Draw full-width. X coordinates are relative to S->w->left_col. */
1775 r.x = WINDOW_LEFT_EDGE_X (s->w);
1776 r.width = WINDOW_TOTAL_WIDTH (s->w);
1777
1778 /* Unless displaying a mode or menu bar line, which are always
1779 fully visible, clip to the visible part of the row. */
1780 if (s->w->pseudo_window_p)
1781 r.height = s->row->visible_height;
1782 else
1783 r.height = s->height;
1784 }
1785 else
1786 {
1787 /* This is a text line that may be partially visible. */
1788 r.x = window_box_left (s->w, s->area);
1789 r.width = window_box_width (s->w, s->area);
1790 r.height = s->row->visible_height;
1791 }
1792
1793 if (s->clip_head)
1794 if (r.x < s->clip_head->x)
1795 {
1796 if (r.width >= s->clip_head->x - r.x)
1797 r.width -= s->clip_head->x - r.x;
1798 else
1799 r.width = 0;
1800 r.x = s->clip_head->x;
1801 }
1802 if (s->clip_tail)
1803 if (r.x + r.width > s->clip_tail->x + s->clip_tail->background_width)
1804 {
1805 if (s->clip_tail->x + s->clip_tail->background_width >= r.x)
1806 r.width = s->clip_tail->x + s->clip_tail->background_width - r.x;
1807 else
1808 r.width = 0;
1809 }
1810
1811 /* If S draws overlapping rows, it's sufficient to use the top and
1812 bottom of the window for clipping because this glyph string
1813 intentionally draws over other lines. */
1814 if (s->for_overlaps)
1815 {
1816 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1817 r.height = window_text_bottom_y (s->w) - r.y;
1818
1819 /* Alas, the above simple strategy does not work for the
1820 environments with anti-aliased text: if the same text is
1821 drawn onto the same place multiple times, it gets thicker.
1822 If the overlap we are processing is for the erased cursor, we
1823 take the intersection with the rectagle of the cursor. */
1824 if (s->for_overlaps & OVERLAPS_ERASED_CURSOR)
1825 {
1826 XRectangle rc, r_save = r;
1827
1828 rc.x = WINDOW_TEXT_TO_FRAME_PIXEL_X (s->w, s->w->phys_cursor.x);
1829 rc.y = s->w->phys_cursor.y;
1830 rc.width = s->w->phys_cursor_width;
1831 rc.height = s->w->phys_cursor_height;
1832
1833 x_intersect_rectangles (&r_save, &rc, &r);
1834 }
1835 }
1836 else
1837 {
1838 /* Don't use S->y for clipping because it doesn't take partially
1839 visible lines into account. For example, it can be negative for
1840 partially visible lines at the top of a window. */
1841 if (!s->row->full_width_p
1842 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
1843 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1844 else
1845 r.y = max (0, s->row->y);
1846
1847 /* If drawing a tool-bar window, draw it over the internal border
1848 at the top of the window. */
1849 if (WINDOWP (s->f->tool_bar_window)
1850 && s->w == XWINDOW (s->f->tool_bar_window))
1851 r.y -= FRAME_INTERNAL_BORDER_WIDTH (s->f);
1852 }
1853
1854 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
1855
1856 /* If drawing the cursor, don't let glyph draw outside its
1857 advertised boundaries. Cleartype does this under some circumstances. */
1858 if (s->hl == DRAW_CURSOR)
1859 {
1860 struct glyph *glyph = s->first_glyph;
1861 int height, max_y;
1862
1863 if (s->x > r.x)
1864 {
1865 r.width -= s->x - r.x;
1866 r.x = s->x;
1867 }
1868 r.width = min (r.width, glyph->pixel_width);
1869
1870 /* If r.y is below window bottom, ensure that we still see a cursor. */
1871 height = min (glyph->ascent + glyph->descent,
1872 min (FRAME_LINE_HEIGHT (s->f), s->row->visible_height));
1873 max_y = window_text_bottom_y (s->w) - height;
1874 max_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, max_y);
1875 if (s->ybase - glyph->ascent > max_y)
1876 {
1877 r.y = max_y;
1878 r.height = height;
1879 }
1880 else
1881 {
1882 /* Don't draw cursor glyph taller than our actual glyph. */
1883 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
1884 if (height < r.height)
1885 {
1886 max_y = r.y + r.height;
1887 r.y = min (max_y, max (r.y, s->ybase + glyph->descent - height));
1888 r.height = min (max_y - r.y, height);
1889 }
1890 }
1891 }
1892
1893 if ((s->for_overlaps & OVERLAPS_BOTH) == 0
1894 || ((s->for_overlaps & OVERLAPS_BOTH) == OVERLAPS_BOTH && n == 1))
1895 {
1896 #ifdef CONVERT_FROM_XRECT
1897 CONVERT_FROM_XRECT (r, *rects);
1898 #else
1899 *rects = r;
1900 #endif
1901 return 1;
1902 }
1903 else
1904 {
1905 /* If we are processing overlapping and allowed to return
1906 multiple clipping rectangles, we exclude the row of the glyph
1907 string from the clipping rectangle. This is to avoid drawing
1908 the same text on the environment with anti-aliasing. */
1909 #ifdef CONVERT_FROM_XRECT
1910 XRectangle rs[2];
1911 #else
1912 XRectangle *rs = rects;
1913 #endif
1914 int i = 0, row_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, s->row->y);
1915
1916 if (s->for_overlaps & OVERLAPS_PRED)
1917 {
1918 rs[i] = r;
1919 if (r.y + r.height > row_y)
1920 {
1921 if (r.y < row_y)
1922 rs[i].height = row_y - r.y;
1923 else
1924 rs[i].height = 0;
1925 }
1926 i++;
1927 }
1928 if (s->for_overlaps & OVERLAPS_SUCC)
1929 {
1930 rs[i] = r;
1931 if (r.y < row_y + s->row->visible_height)
1932 {
1933 if (r.y + r.height > row_y + s->row->visible_height)
1934 {
1935 rs[i].y = row_y + s->row->visible_height;
1936 rs[i].height = r.y + r.height - rs[i].y;
1937 }
1938 else
1939 rs[i].height = 0;
1940 }
1941 i++;
1942 }
1943
1944 n = i;
1945 #ifdef CONVERT_FROM_XRECT
1946 for (i = 0; i < n; i++)
1947 CONVERT_FROM_XRECT (rs[i], rects[i]);
1948 #endif
1949 return n;
1950 }
1951 }
1952
1953 /* EXPORT:
1954 Return in *NR the clipping rectangle for glyph string S. */
1955
1956 void
1957 get_glyph_string_clip_rect (s, nr)
1958 struct glyph_string *s;
1959 NativeRectangle *nr;
1960 {
1961 get_glyph_string_clip_rects (s, nr, 1);
1962 }
1963
1964
1965 /* EXPORT:
1966 Return the position and height of the phys cursor in window W.
1967 Set w->phys_cursor_width to width of phys cursor.
1968 */
1969
1970 int
1971 get_phys_cursor_geometry (w, row, glyph, heightp)
1972 struct window *w;
1973 struct glyph_row *row;
1974 struct glyph *glyph;
1975 int *heightp;
1976 {
1977 struct frame *f = XFRAME (WINDOW_FRAME (w));
1978 int y, wd, h, h0, y0;
1979
1980 /* Compute the width of the rectangle to draw. If on a stretch
1981 glyph, and `x-stretch-block-cursor' is nil, don't draw a
1982 rectangle as wide as the glyph, but use a canonical character
1983 width instead. */
1984 wd = glyph->pixel_width - 1;
1985 #ifdef HAVE_NTGUI
1986 wd++; /* Why? */
1987 #endif
1988 if (glyph->type == STRETCH_GLYPH
1989 && !x_stretch_cursor_p)
1990 wd = min (FRAME_COLUMN_WIDTH (f), wd);
1991 w->phys_cursor_width = wd;
1992
1993 y = w->phys_cursor.y + row->ascent - glyph->ascent;
1994
1995 /* If y is below window bottom, ensure that we still see a cursor. */
1996 h0 = min (FRAME_LINE_HEIGHT (f), row->visible_height);
1997
1998 h = max (h0, glyph->ascent + glyph->descent);
1999 h0 = min (h0, glyph->ascent + glyph->descent);
2000
2001 y0 = WINDOW_HEADER_LINE_HEIGHT (w);
2002 if (y < y0)
2003 {
2004 h = max (h - (y0 - y) + 1, h0);
2005 y = y0 - 1;
2006 }
2007 else
2008 {
2009 y0 = window_text_bottom_y (w) - h0;
2010 if (y > y0)
2011 {
2012 h += y - y0;
2013 y = y0;
2014 }
2015 }
2016
2017 *heightp = h - 1;
2018 return WINDOW_TO_FRAME_PIXEL_Y (w, y);
2019 }
2020
2021 /*
2022 * Remember which glyph the mouse is over.
2023 */
2024
2025 void
2026 remember_mouse_glyph (f, gx, gy, rect)
2027 struct frame *f;
2028 int gx, gy;
2029 NativeRectangle *rect;
2030 {
2031 Lisp_Object window;
2032 struct window *w;
2033 struct glyph_row *r, *gr, *end_row;
2034 enum window_part part;
2035 enum glyph_row_area area;
2036 int x, y, width, height;
2037
2038 /* Try to determine frame pixel position and size of the glyph under
2039 frame pixel coordinates X/Y on frame F. */
2040
2041 window = window_from_coordinates (f, gx, gy, &part, &x, &y, 0);
2042 if (NILP (window))
2043 {
2044 width = FRAME_SMALLEST_CHAR_WIDTH (f);
2045 height = FRAME_SMALLEST_FONT_HEIGHT (f);
2046 goto virtual_glyph;
2047 }
2048
2049 w = XWINDOW (window);
2050 width = WINDOW_FRAME_COLUMN_WIDTH (w);
2051 height = WINDOW_FRAME_LINE_HEIGHT (w);
2052
2053 r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
2054 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
2055
2056 if (w->pseudo_window_p)
2057 {
2058 area = TEXT_AREA;
2059 part = ON_MODE_LINE; /* Don't adjust margin. */
2060 goto text_glyph;
2061 }
2062
2063 switch (part)
2064 {
2065 case ON_LEFT_MARGIN:
2066 area = LEFT_MARGIN_AREA;
2067 goto text_glyph;
2068
2069 case ON_RIGHT_MARGIN:
2070 area = RIGHT_MARGIN_AREA;
2071 goto text_glyph;
2072
2073 case ON_HEADER_LINE:
2074 case ON_MODE_LINE:
2075 gr = (part == ON_HEADER_LINE
2076 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
2077 : MATRIX_MODE_LINE_ROW (w->current_matrix));
2078 gy = gr->y;
2079 area = TEXT_AREA;
2080 goto text_glyph_row_found;
2081
2082 case ON_TEXT:
2083 area = TEXT_AREA;
2084
2085 text_glyph:
2086 gr = 0; gy = 0;
2087 for (; r <= end_row && r->enabled_p; ++r)
2088 if (r->y + r->height > y)
2089 {
2090 gr = r; gy = r->y;
2091 break;
2092 }
2093
2094 text_glyph_row_found:
2095 if (gr && gy <= y)
2096 {
2097 struct glyph *g = gr->glyphs[area];
2098 struct glyph *end = g + gr->used[area];
2099
2100 height = gr->height;
2101 for (gx = gr->x; g < end; gx += g->pixel_width, ++g)
2102 if (gx + g->pixel_width > x)
2103 break;
2104
2105 if (g < end)
2106 {
2107 if (g->type == IMAGE_GLYPH)
2108 {
2109 /* Don't remember when mouse is over image, as
2110 image may have hot-spots. */
2111 STORE_NATIVE_RECT (*rect, 0, 0, 0, 0);
2112 return;
2113 }
2114 width = g->pixel_width;
2115 }
2116 else
2117 {
2118 /* Use nominal char spacing at end of line. */
2119 x -= gx;
2120 gx += (x / width) * width;
2121 }
2122
2123 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2124 gx += window_box_left_offset (w, area);
2125 }
2126 else
2127 {
2128 /* Use nominal line height at end of window. */
2129 gx = (x / width) * width;
2130 y -= gy;
2131 gy += (y / height) * height;
2132 }
2133 break;
2134
2135 case ON_LEFT_FRINGE:
2136 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2137 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w)
2138 : window_box_right_offset (w, LEFT_MARGIN_AREA));
2139 width = WINDOW_LEFT_FRINGE_WIDTH (w);
2140 goto row_glyph;
2141
2142 case ON_RIGHT_FRINGE:
2143 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2144 ? window_box_right_offset (w, RIGHT_MARGIN_AREA)
2145 : window_box_right_offset (w, TEXT_AREA));
2146 width = WINDOW_RIGHT_FRINGE_WIDTH (w);
2147 goto row_glyph;
2148
2149 case ON_SCROLL_BAR:
2150 gx = (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
2151 ? 0
2152 : (window_box_right_offset (w, RIGHT_MARGIN_AREA)
2153 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2154 ? WINDOW_RIGHT_FRINGE_WIDTH (w)
2155 : 0)));
2156 width = WINDOW_SCROLL_BAR_AREA_WIDTH (w);
2157
2158 row_glyph:
2159 gr = 0, gy = 0;
2160 for (; r <= end_row && r->enabled_p; ++r)
2161 if (r->y + r->height > y)
2162 {
2163 gr = r; gy = r->y;
2164 break;
2165 }
2166
2167 if (gr && gy <= y)
2168 height = gr->height;
2169 else
2170 {
2171 /* Use nominal line height at end of window. */
2172 y -= gy;
2173 gy += (y / height) * height;
2174 }
2175 break;
2176
2177 default:
2178 ;
2179 virtual_glyph:
2180 /* If there is no glyph under the mouse, then we divide the screen
2181 into a grid of the smallest glyph in the frame, and use that
2182 as our "glyph". */
2183
2184 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to
2185 round down even for negative values. */
2186 if (gx < 0)
2187 gx -= width - 1;
2188 if (gy < 0)
2189 gy -= height - 1;
2190
2191 gx = (gx / width) * width;
2192 gy = (gy / height) * height;
2193
2194 goto store_rect;
2195 }
2196
2197 gx += WINDOW_LEFT_EDGE_X (w);
2198 gy += WINDOW_TOP_EDGE_Y (w);
2199
2200 store_rect:
2201 STORE_NATIVE_RECT (*rect, gx, gy, width, height);
2202
2203 /* Visible feedback for debugging. */
2204 #if 0
2205 #if HAVE_X_WINDOWS
2206 XDrawRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2207 f->output_data.x->normal_gc,
2208 gx, gy, width, height);
2209 #endif
2210 #endif
2211 }
2212
2213
2214 #endif /* HAVE_WINDOW_SYSTEM */
2215
2216 \f
2217 /***********************************************************************
2218 Lisp form evaluation
2219 ***********************************************************************/
2220
2221 /* Error handler for safe_eval and safe_call. */
2222
2223 static Lisp_Object
2224 safe_eval_handler (arg)
2225 Lisp_Object arg;
2226 {
2227 add_to_log ("Error during redisplay: %s", arg, Qnil);
2228 return Qnil;
2229 }
2230
2231
2232 /* Evaluate SEXPR and return the result, or nil if something went
2233 wrong. Prevent redisplay during the evaluation. */
2234
2235 Lisp_Object
2236 safe_eval (sexpr)
2237 Lisp_Object sexpr;
2238 {
2239 Lisp_Object val;
2240
2241 if (inhibit_eval_during_redisplay)
2242 val = Qnil;
2243 else
2244 {
2245 int count = SPECPDL_INDEX ();
2246 struct gcpro gcpro1;
2247
2248 GCPRO1 (sexpr);
2249 specbind (Qinhibit_redisplay, Qt);
2250 /* Use Qt to ensure debugger does not run,
2251 so there is no possibility of wanting to redisplay. */
2252 val = internal_condition_case_1 (Feval, sexpr, Qt,
2253 safe_eval_handler);
2254 UNGCPRO;
2255 val = unbind_to (count, val);
2256 }
2257
2258 return val;
2259 }
2260
2261
2262 /* Call function ARGS[0] with arguments ARGS[1] to ARGS[NARGS - 1].
2263 Return the result, or nil if something went wrong. Prevent
2264 redisplay during the evaluation. */
2265
2266 Lisp_Object
2267 safe_call (nargs, args)
2268 int nargs;
2269 Lisp_Object *args;
2270 {
2271 Lisp_Object val;
2272
2273 if (inhibit_eval_during_redisplay)
2274 val = Qnil;
2275 else
2276 {
2277 int count = SPECPDL_INDEX ();
2278 struct gcpro gcpro1;
2279
2280 GCPRO1 (args[0]);
2281 gcpro1.nvars = nargs;
2282 specbind (Qinhibit_redisplay, Qt);
2283 /* Use Qt to ensure debugger does not run,
2284 so there is no possibility of wanting to redisplay. */
2285 val = internal_condition_case_2 (Ffuncall, nargs, args, Qt,
2286 safe_eval_handler);
2287 UNGCPRO;
2288 val = unbind_to (count, val);
2289 }
2290
2291 return val;
2292 }
2293
2294
2295 /* Call function FN with one argument ARG.
2296 Return the result, or nil if something went wrong. */
2297
2298 Lisp_Object
2299 safe_call1 (fn, arg)
2300 Lisp_Object fn, arg;
2301 {
2302 Lisp_Object args[2];
2303 args[0] = fn;
2304 args[1] = arg;
2305 return safe_call (2, args);
2306 }
2307
2308
2309 \f
2310 /***********************************************************************
2311 Debugging
2312 ***********************************************************************/
2313
2314 #if 0
2315
2316 /* Define CHECK_IT to perform sanity checks on iterators.
2317 This is for debugging. It is too slow to do unconditionally. */
2318
2319 static void
2320 check_it (it)
2321 struct it *it;
2322 {
2323 if (it->method == GET_FROM_STRING)
2324 {
2325 xassert (STRINGP (it->string));
2326 xassert (IT_STRING_CHARPOS (*it) >= 0);
2327 }
2328 else
2329 {
2330 xassert (IT_STRING_CHARPOS (*it) < 0);
2331 if (it->method == GET_FROM_BUFFER)
2332 {
2333 /* Check that character and byte positions agree. */
2334 xassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
2335 }
2336 }
2337
2338 if (it->dpvec)
2339 xassert (it->current.dpvec_index >= 0);
2340 else
2341 xassert (it->current.dpvec_index < 0);
2342 }
2343
2344 #define CHECK_IT(IT) check_it ((IT))
2345
2346 #else /* not 0 */
2347
2348 #define CHECK_IT(IT) (void) 0
2349
2350 #endif /* not 0 */
2351
2352
2353 #if GLYPH_DEBUG
2354
2355 /* Check that the window end of window W is what we expect it
2356 to be---the last row in the current matrix displaying text. */
2357
2358 static void
2359 check_window_end (w)
2360 struct window *w;
2361 {
2362 if (!MINI_WINDOW_P (w)
2363 && !NILP (w->window_end_valid))
2364 {
2365 struct glyph_row *row;
2366 xassert ((row = MATRIX_ROW (w->current_matrix,
2367 XFASTINT (w->window_end_vpos)),
2368 !row->enabled_p
2369 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
2370 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
2371 }
2372 }
2373
2374 #define CHECK_WINDOW_END(W) check_window_end ((W))
2375
2376 #else /* not GLYPH_DEBUG */
2377
2378 #define CHECK_WINDOW_END(W) (void) 0
2379
2380 #endif /* not GLYPH_DEBUG */
2381
2382
2383 \f
2384 /***********************************************************************
2385 Iterator initialization
2386 ***********************************************************************/
2387
2388 /* Initialize IT for displaying current_buffer in window W, starting
2389 at character position CHARPOS. CHARPOS < 0 means that no buffer
2390 position is specified which is useful when the iterator is assigned
2391 a position later. BYTEPOS is the byte position corresponding to
2392 CHARPOS. BYTEPOS < 0 means compute it from CHARPOS.
2393
2394 If ROW is not null, calls to produce_glyphs with IT as parameter
2395 will produce glyphs in that row.
2396
2397 BASE_FACE_ID is the id of a base face to use. It must be one of
2398 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2399 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2400 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2401
2402 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2403 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2404 will be initialized to use the corresponding mode line glyph row of
2405 the desired matrix of W. */
2406
2407 void
2408 init_iterator (it, w, charpos, bytepos, row, base_face_id)
2409 struct it *it;
2410 struct window *w;
2411 int charpos, bytepos;
2412 struct glyph_row *row;
2413 enum face_id base_face_id;
2414 {
2415 int highlight_region_p;
2416
2417 /* Some precondition checks. */
2418 xassert (w != NULL && it != NULL);
2419 xassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2420 && charpos <= ZV));
2421
2422 /* If face attributes have been changed since the last redisplay,
2423 free realized faces now because they depend on face definitions
2424 that might have changed. Don't free faces while there might be
2425 desired matrices pending which reference these faces. */
2426 if (face_change_count && !inhibit_free_realized_faces)
2427 {
2428 face_change_count = 0;
2429 free_all_realized_faces (Qnil);
2430 }
2431
2432 /* Use one of the mode line rows of W's desired matrix if
2433 appropriate. */
2434 if (row == NULL)
2435 {
2436 if (base_face_id == MODE_LINE_FACE_ID
2437 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2438 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2439 else if (base_face_id == HEADER_LINE_FACE_ID)
2440 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2441 }
2442
2443 /* Clear IT. */
2444 bzero (it, sizeof *it);
2445 it->current.overlay_string_index = -1;
2446 it->current.dpvec_index = -1;
2447 it->base_face_id = base_face_id;
2448 it->string = Qnil;
2449 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2450
2451 /* The window in which we iterate over current_buffer: */
2452 XSETWINDOW (it->window, w);
2453 it->w = w;
2454 it->f = XFRAME (w->frame);
2455
2456 /* Extra space between lines (on window systems only). */
2457 if (base_face_id == DEFAULT_FACE_ID
2458 && FRAME_WINDOW_P (it->f))
2459 {
2460 if (NATNUMP (current_buffer->extra_line_spacing))
2461 it->extra_line_spacing = XFASTINT (current_buffer->extra_line_spacing);
2462 else if (FLOATP (current_buffer->extra_line_spacing))
2463 it->extra_line_spacing = (XFLOAT_DATA (current_buffer->extra_line_spacing)
2464 * FRAME_LINE_HEIGHT (it->f));
2465 else if (it->f->extra_line_spacing > 0)
2466 it->extra_line_spacing = it->f->extra_line_spacing;
2467 it->max_extra_line_spacing = 0;
2468 }
2469
2470 /* If realized faces have been removed, e.g. because of face
2471 attribute changes of named faces, recompute them. When running
2472 in batch mode, the face cache of the initial frame is null. If
2473 we happen to get called, make a dummy face cache. */
2474 if (FRAME_FACE_CACHE (it->f) == NULL)
2475 init_frame_faces (it->f);
2476 if (FRAME_FACE_CACHE (it->f)->used == 0)
2477 recompute_basic_faces (it->f);
2478
2479 /* Current value of the `slice', `space-width', and 'height' properties. */
2480 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
2481 it->space_width = Qnil;
2482 it->font_height = Qnil;
2483 it->override_ascent = -1;
2484
2485 /* Are control characters displayed as `^C'? */
2486 it->ctl_arrow_p = !NILP (current_buffer->ctl_arrow);
2487
2488 /* -1 means everything between a CR and the following line end
2489 is invisible. >0 means lines indented more than this value are
2490 invisible. */
2491 it->selective = (INTEGERP (current_buffer->selective_display)
2492 ? XFASTINT (current_buffer->selective_display)
2493 : (!NILP (current_buffer->selective_display)
2494 ? -1 : 0));
2495 it->selective_display_ellipsis_p
2496 = !NILP (current_buffer->selective_display_ellipses);
2497
2498 /* Display table to use. */
2499 it->dp = window_display_table (w);
2500
2501 /* Are multibyte characters enabled in current_buffer? */
2502 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
2503
2504 /* Non-zero if we should highlight the region. */
2505 highlight_region_p
2506 = (!NILP (Vtransient_mark_mode)
2507 && !NILP (current_buffer->mark_active)
2508 && XMARKER (current_buffer->mark)->buffer != 0);
2509
2510 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
2511 start and end of a visible region in window IT->w. Set both to
2512 -1 to indicate no region. */
2513 if (highlight_region_p
2514 /* Maybe highlight only in selected window. */
2515 && (/* Either show region everywhere. */
2516 highlight_nonselected_windows
2517 /* Or show region in the selected window. */
2518 || w == XWINDOW (selected_window)
2519 /* Or show the region if we are in the mini-buffer and W is
2520 the window the mini-buffer refers to. */
2521 || (MINI_WINDOW_P (XWINDOW (selected_window))
2522 && WINDOWP (minibuf_selected_window)
2523 && w == XWINDOW (minibuf_selected_window))))
2524 {
2525 int charpos = marker_position (current_buffer->mark);
2526 it->region_beg_charpos = min (PT, charpos);
2527 it->region_end_charpos = max (PT, charpos);
2528 }
2529 else
2530 it->region_beg_charpos = it->region_end_charpos = -1;
2531
2532 /* Get the position at which the redisplay_end_trigger hook should
2533 be run, if it is to be run at all. */
2534 if (MARKERP (w->redisplay_end_trigger)
2535 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2536 it->redisplay_end_trigger_charpos
2537 = marker_position (w->redisplay_end_trigger);
2538 else if (INTEGERP (w->redisplay_end_trigger))
2539 it->redisplay_end_trigger_charpos = XINT (w->redisplay_end_trigger);
2540
2541 /* Correct bogus values of tab_width. */
2542 it->tab_width = XINT (current_buffer->tab_width);
2543 if (it->tab_width <= 0 || it->tab_width > 1000)
2544 it->tab_width = 8;
2545
2546 /* Are lines in the display truncated? */
2547 it->truncate_lines_p
2548 = (base_face_id != DEFAULT_FACE_ID
2549 || XINT (it->w->hscroll)
2550 || (truncate_partial_width_windows
2551 && !WINDOW_FULL_WIDTH_P (it->w))
2552 || !NILP (current_buffer->truncate_lines));
2553
2554 /* Get dimensions of truncation and continuation glyphs. These are
2555 displayed as fringe bitmaps under X, so we don't need them for such
2556 frames. */
2557 if (!FRAME_WINDOW_P (it->f))
2558 {
2559 if (it->truncate_lines_p)
2560 {
2561 /* We will need the truncation glyph. */
2562 xassert (it->glyph_row == NULL);
2563 produce_special_glyphs (it, IT_TRUNCATION);
2564 it->truncation_pixel_width = it->pixel_width;
2565 }
2566 else
2567 {
2568 /* We will need the continuation glyph. */
2569 xassert (it->glyph_row == NULL);
2570 produce_special_glyphs (it, IT_CONTINUATION);
2571 it->continuation_pixel_width = it->pixel_width;
2572 }
2573
2574 /* Reset these values to zero because the produce_special_glyphs
2575 above has changed them. */
2576 it->pixel_width = it->ascent = it->descent = 0;
2577 it->phys_ascent = it->phys_descent = 0;
2578 }
2579
2580 /* Set this after getting the dimensions of truncation and
2581 continuation glyphs, so that we don't produce glyphs when calling
2582 produce_special_glyphs, above. */
2583 it->glyph_row = row;
2584 it->area = TEXT_AREA;
2585
2586 /* Get the dimensions of the display area. The display area
2587 consists of the visible window area plus a horizontally scrolled
2588 part to the left of the window. All x-values are relative to the
2589 start of this total display area. */
2590 if (base_face_id != DEFAULT_FACE_ID)
2591 {
2592 /* Mode lines, menu bar in terminal frames. */
2593 it->first_visible_x = 0;
2594 it->last_visible_x = WINDOW_TOTAL_WIDTH (w);
2595 }
2596 else
2597 {
2598 it->first_visible_x
2599 = XFASTINT (it->w->hscroll) * FRAME_COLUMN_WIDTH (it->f);
2600 it->last_visible_x = (it->first_visible_x
2601 + window_box_width (w, TEXT_AREA));
2602
2603 /* If we truncate lines, leave room for the truncator glyph(s) at
2604 the right margin. Otherwise, leave room for the continuation
2605 glyph(s). Truncation and continuation glyphs are not inserted
2606 for window-based redisplay. */
2607 if (!FRAME_WINDOW_P (it->f))
2608 {
2609 if (it->truncate_lines_p)
2610 it->last_visible_x -= it->truncation_pixel_width;
2611 else
2612 it->last_visible_x -= it->continuation_pixel_width;
2613 }
2614
2615 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2616 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2617 }
2618
2619 /* Leave room for a border glyph. */
2620 if (!FRAME_WINDOW_P (it->f)
2621 && !WINDOW_RIGHTMOST_P (it->w))
2622 it->last_visible_x -= 1;
2623
2624 it->last_visible_y = window_text_bottom_y (w);
2625
2626 /* For mode lines and alike, arrange for the first glyph having a
2627 left box line if the face specifies a box. */
2628 if (base_face_id != DEFAULT_FACE_ID)
2629 {
2630 struct face *face;
2631
2632 it->face_id = base_face_id;
2633
2634 /* If we have a boxed mode line, make the first character appear
2635 with a left box line. */
2636 face = FACE_FROM_ID (it->f, base_face_id);
2637 if (face->box != FACE_NO_BOX)
2638 it->start_of_box_run_p = 1;
2639 }
2640
2641 /* If a buffer position was specified, set the iterator there,
2642 getting overlays and face properties from that position. */
2643 if (charpos >= BUF_BEG (current_buffer))
2644 {
2645 it->end_charpos = ZV;
2646 it->face_id = -1;
2647 IT_CHARPOS (*it) = charpos;
2648
2649 /* Compute byte position if not specified. */
2650 if (bytepos < charpos)
2651 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
2652 else
2653 IT_BYTEPOS (*it) = bytepos;
2654
2655 it->start = it->current;
2656
2657 /* Compute faces etc. */
2658 reseat (it, it->current.pos, 1);
2659 }
2660
2661 CHECK_IT (it);
2662 }
2663
2664
2665 /* Initialize IT for the display of window W with window start POS. */
2666
2667 void
2668 start_display (it, w, pos)
2669 struct it *it;
2670 struct window *w;
2671 struct text_pos pos;
2672 {
2673 struct glyph_row *row;
2674 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
2675
2676 row = w->desired_matrix->rows + first_vpos;
2677 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
2678 it->first_vpos = first_vpos;
2679
2680 /* Don't reseat to previous visible line start if current start
2681 position is in a string or image. */
2682 if (it->method == GET_FROM_BUFFER && !it->truncate_lines_p)
2683 {
2684 int start_at_line_beg_p;
2685 int first_y = it->current_y;
2686
2687 /* If window start is not at a line start, skip forward to POS to
2688 get the correct continuation lines width. */
2689 start_at_line_beg_p = (CHARPOS (pos) == BEGV
2690 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
2691 if (!start_at_line_beg_p)
2692 {
2693 int new_x;
2694
2695 reseat_at_previous_visible_line_start (it);
2696 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
2697
2698 new_x = it->current_x + it->pixel_width;
2699
2700 /* If lines are continued, this line may end in the middle
2701 of a multi-glyph character (e.g. a control character
2702 displayed as \003, or in the middle of an overlay
2703 string). In this case move_it_to above will not have
2704 taken us to the start of the continuation line but to the
2705 end of the continued line. */
2706 if (it->current_x > 0
2707 && !it->truncate_lines_p /* Lines are continued. */
2708 && (/* And glyph doesn't fit on the line. */
2709 new_x > it->last_visible_x
2710 /* Or it fits exactly and we're on a window
2711 system frame. */
2712 || (new_x == it->last_visible_x
2713 && FRAME_WINDOW_P (it->f))))
2714 {
2715 if (it->current.dpvec_index >= 0
2716 || it->current.overlay_string_index >= 0)
2717 {
2718 set_iterator_to_next (it, 1);
2719 move_it_in_display_line_to (it, -1, -1, 0);
2720 }
2721
2722 it->continuation_lines_width += it->current_x;
2723 }
2724
2725 /* We're starting a new display line, not affected by the
2726 height of the continued line, so clear the appropriate
2727 fields in the iterator structure. */
2728 it->max_ascent = it->max_descent = 0;
2729 it->max_phys_ascent = it->max_phys_descent = 0;
2730
2731 it->current_y = first_y;
2732 it->vpos = 0;
2733 it->current_x = it->hpos = 0;
2734 }
2735 }
2736
2737 #if 0 /* Don't assert the following because start_display is sometimes
2738 called intentionally with a window start that is not at a
2739 line start. Please leave this code in as a comment. */
2740
2741 /* Window start should be on a line start, now. */
2742 xassert (it->continuation_lines_width
2743 || IT_CHARPOS (it) == BEGV
2744 || FETCH_BYTE (IT_BYTEPOS (it) - 1) == '\n');
2745 #endif /* 0 */
2746 }
2747
2748
2749 /* Return 1 if POS is a position in ellipses displayed for invisible
2750 text. W is the window we display, for text property lookup. */
2751
2752 static int
2753 in_ellipses_for_invisible_text_p (pos, w)
2754 struct display_pos *pos;
2755 struct window *w;
2756 {
2757 Lisp_Object prop, window;
2758 int ellipses_p = 0;
2759 int charpos = CHARPOS (pos->pos);
2760
2761 /* If POS specifies a position in a display vector, this might
2762 be for an ellipsis displayed for invisible text. We won't
2763 get the iterator set up for delivering that ellipsis unless
2764 we make sure that it gets aware of the invisible text. */
2765 if (pos->dpvec_index >= 0
2766 && pos->overlay_string_index < 0
2767 && CHARPOS (pos->string_pos) < 0
2768 && charpos > BEGV
2769 && (XSETWINDOW (window, w),
2770 prop = Fget_char_property (make_number (charpos),
2771 Qinvisible, window),
2772 !TEXT_PROP_MEANS_INVISIBLE (prop)))
2773 {
2774 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
2775 window);
2776 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
2777 }
2778
2779 return ellipses_p;
2780 }
2781
2782
2783 /* Initialize IT for stepping through current_buffer in window W,
2784 starting at position POS that includes overlay string and display
2785 vector/ control character translation position information. Value
2786 is zero if there are overlay strings with newlines at POS. */
2787
2788 static int
2789 init_from_display_pos (it, w, pos)
2790 struct it *it;
2791 struct window *w;
2792 struct display_pos *pos;
2793 {
2794 int charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
2795 int i, overlay_strings_with_newlines = 0;
2796
2797 /* If POS specifies a position in a display vector, this might
2798 be for an ellipsis displayed for invisible text. We won't
2799 get the iterator set up for delivering that ellipsis unless
2800 we make sure that it gets aware of the invisible text. */
2801 if (in_ellipses_for_invisible_text_p (pos, w))
2802 {
2803 --charpos;
2804 bytepos = 0;
2805 }
2806
2807 /* Keep in mind: the call to reseat in init_iterator skips invisible
2808 text, so we might end up at a position different from POS. This
2809 is only a problem when POS is a row start after a newline and an
2810 overlay starts there with an after-string, and the overlay has an
2811 invisible property. Since we don't skip invisible text in
2812 display_line and elsewhere immediately after consuming the
2813 newline before the row start, such a POS will not be in a string,
2814 but the call to init_iterator below will move us to the
2815 after-string. */
2816 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
2817
2818 /* This only scans the current chunk -- it should scan all chunks.
2819 However, OVERLAY_STRING_CHUNK_SIZE has been increased from 3 in 21.1
2820 to 16 in 22.1 to make this a lesser problem. */
2821 for (i = 0; i < it->n_overlay_strings && i < OVERLAY_STRING_CHUNK_SIZE; ++i)
2822 {
2823 const char *s = SDATA (it->overlay_strings[i]);
2824 const char *e = s + SBYTES (it->overlay_strings[i]);
2825
2826 while (s < e && *s != '\n')
2827 ++s;
2828
2829 if (s < e)
2830 {
2831 overlay_strings_with_newlines = 1;
2832 break;
2833 }
2834 }
2835
2836 /* If position is within an overlay string, set up IT to the right
2837 overlay string. */
2838 if (pos->overlay_string_index >= 0)
2839 {
2840 int relative_index;
2841
2842 /* If the first overlay string happens to have a `display'
2843 property for an image, the iterator will be set up for that
2844 image, and we have to undo that setup first before we can
2845 correct the overlay string index. */
2846 if (it->method == GET_FROM_IMAGE)
2847 pop_it (it);
2848
2849 /* We already have the first chunk of overlay strings in
2850 IT->overlay_strings. Load more until the one for
2851 pos->overlay_string_index is in IT->overlay_strings. */
2852 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
2853 {
2854 int n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
2855 it->current.overlay_string_index = 0;
2856 while (n--)
2857 {
2858 load_overlay_strings (it, 0);
2859 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
2860 }
2861 }
2862
2863 it->current.overlay_string_index = pos->overlay_string_index;
2864 relative_index = (it->current.overlay_string_index
2865 % OVERLAY_STRING_CHUNK_SIZE);
2866 it->string = it->overlay_strings[relative_index];
2867 xassert (STRINGP (it->string));
2868 it->current.string_pos = pos->string_pos;
2869 it->method = GET_FROM_STRING;
2870 }
2871
2872 #if 0 /* This is bogus because POS not having an overlay string
2873 position does not mean it's after the string. Example: A
2874 line starting with a before-string and initialization of IT
2875 to the previous row's end position. */
2876 else if (it->current.overlay_string_index >= 0)
2877 {
2878 /* If POS says we're already after an overlay string ending at
2879 POS, make sure to pop the iterator because it will be in
2880 front of that overlay string. When POS is ZV, we've thereby
2881 also ``processed'' overlay strings at ZV. */
2882 while (it->sp)
2883 pop_it (it);
2884 it->current.overlay_string_index = -1;
2885 it->method = GET_FROM_BUFFER;
2886 if (CHARPOS (pos->pos) == ZV)
2887 it->overlay_strings_at_end_processed_p = 1;
2888 }
2889 #endif /* 0 */
2890
2891 if (CHARPOS (pos->string_pos) >= 0)
2892 {
2893 /* Recorded position is not in an overlay string, but in another
2894 string. This can only be a string from a `display' property.
2895 IT should already be filled with that string. */
2896 it->current.string_pos = pos->string_pos;
2897 xassert (STRINGP (it->string));
2898 }
2899
2900 /* Restore position in display vector translations, control
2901 character translations or ellipses. */
2902 if (pos->dpvec_index >= 0)
2903 {
2904 if (it->dpvec == NULL)
2905 get_next_display_element (it);
2906 xassert (it->dpvec && it->current.dpvec_index == 0);
2907 it->current.dpvec_index = pos->dpvec_index;
2908 }
2909
2910 CHECK_IT (it);
2911 return !overlay_strings_with_newlines;
2912 }
2913
2914
2915 /* Initialize IT for stepping through current_buffer in window W
2916 starting at ROW->start. */
2917
2918 static void
2919 init_to_row_start (it, w, row)
2920 struct it *it;
2921 struct window *w;
2922 struct glyph_row *row;
2923 {
2924 init_from_display_pos (it, w, &row->start);
2925 it->start = row->start;
2926 it->continuation_lines_width = row->continuation_lines_width;
2927 CHECK_IT (it);
2928 }
2929
2930
2931 /* Initialize IT for stepping through current_buffer in window W
2932 starting in the line following ROW, i.e. starting at ROW->end.
2933 Value is zero if there are overlay strings with newlines at ROW's
2934 end position. */
2935
2936 static int
2937 init_to_row_end (it, w, row)
2938 struct it *it;
2939 struct window *w;
2940 struct glyph_row *row;
2941 {
2942 int success = 0;
2943
2944 if (init_from_display_pos (it, w, &row->end))
2945 {
2946 if (row->continued_p)
2947 it->continuation_lines_width
2948 = row->continuation_lines_width + row->pixel_width;
2949 CHECK_IT (it);
2950 success = 1;
2951 }
2952
2953 return success;
2954 }
2955
2956
2957
2958 \f
2959 /***********************************************************************
2960 Text properties
2961 ***********************************************************************/
2962
2963 /* Called when IT reaches IT->stop_charpos. Handle text property and
2964 overlay changes. Set IT->stop_charpos to the next position where
2965 to stop. */
2966
2967 static void
2968 handle_stop (it)
2969 struct it *it;
2970 {
2971 enum prop_handled handled;
2972 int handle_overlay_change_p;
2973 struct props *p;
2974
2975 it->dpvec = NULL;
2976 it->current.dpvec_index = -1;
2977 handle_overlay_change_p = !it->ignore_overlay_strings_at_pos_p;
2978 it->ignore_overlay_strings_at_pos_p = 0;
2979
2980 /* Use face of preceding text for ellipsis (if invisible) */
2981 if (it->selective_display_ellipsis_p)
2982 it->saved_face_id = it->face_id;
2983
2984 do
2985 {
2986 handled = HANDLED_NORMALLY;
2987
2988 /* Call text property handlers. */
2989 for (p = it_props; p->handler; ++p)
2990 {
2991 handled = p->handler (it);
2992
2993 if (handled == HANDLED_RECOMPUTE_PROPS)
2994 break;
2995 else if (handled == HANDLED_RETURN)
2996 return;
2997 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
2998 handle_overlay_change_p = 0;
2999 }
3000
3001 if (handled != HANDLED_RECOMPUTE_PROPS)
3002 {
3003 /* Don't check for overlay strings below when set to deliver
3004 characters from a display vector. */
3005 if (it->method == GET_FROM_DISPLAY_VECTOR)
3006 handle_overlay_change_p = 0;
3007
3008 /* Handle overlay changes. */
3009 if (handle_overlay_change_p)
3010 handled = handle_overlay_change (it);
3011
3012 /* Determine where to stop next. */
3013 if (handled == HANDLED_NORMALLY)
3014 compute_stop_pos (it);
3015 }
3016 }
3017 while (handled == HANDLED_RECOMPUTE_PROPS);
3018 }
3019
3020
3021 /* Compute IT->stop_charpos from text property and overlay change
3022 information for IT's current position. */
3023
3024 static void
3025 compute_stop_pos (it)
3026 struct it *it;
3027 {
3028 register INTERVAL iv, next_iv;
3029 Lisp_Object object, limit, position;
3030
3031 /* If nowhere else, stop at the end. */
3032 it->stop_charpos = it->end_charpos;
3033
3034 if (STRINGP (it->string))
3035 {
3036 /* Strings are usually short, so don't limit the search for
3037 properties. */
3038 object = it->string;
3039 limit = Qnil;
3040 position = make_number (IT_STRING_CHARPOS (*it));
3041 }
3042 else
3043 {
3044 int charpos;
3045
3046 /* If next overlay change is in front of the current stop pos
3047 (which is IT->end_charpos), stop there. Note: value of
3048 next_overlay_change is point-max if no overlay change
3049 follows. */
3050 charpos = next_overlay_change (IT_CHARPOS (*it));
3051 if (charpos < it->stop_charpos)
3052 it->stop_charpos = charpos;
3053
3054 /* If showing the region, we have to stop at the region
3055 start or end because the face might change there. */
3056 if (it->region_beg_charpos > 0)
3057 {
3058 if (IT_CHARPOS (*it) < it->region_beg_charpos)
3059 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
3060 else if (IT_CHARPOS (*it) < it->region_end_charpos)
3061 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
3062 }
3063
3064 /* Set up variables for computing the stop position from text
3065 property changes. */
3066 XSETBUFFER (object, current_buffer);
3067 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
3068 position = make_number (IT_CHARPOS (*it));
3069
3070 }
3071
3072 /* Get the interval containing IT's position. Value is a null
3073 interval if there isn't such an interval. */
3074 iv = validate_interval_range (object, &position, &position, 0);
3075 if (!NULL_INTERVAL_P (iv))
3076 {
3077 Lisp_Object values_here[LAST_PROP_IDX];
3078 struct props *p;
3079
3080 /* Get properties here. */
3081 for (p = it_props; p->handler; ++p)
3082 values_here[p->idx] = textget (iv->plist, *p->name);
3083
3084 /* Look for an interval following iv that has different
3085 properties. */
3086 for (next_iv = next_interval (iv);
3087 (!NULL_INTERVAL_P (next_iv)
3088 && (NILP (limit)
3089 || XFASTINT (limit) > next_iv->position));
3090 next_iv = next_interval (next_iv))
3091 {
3092 for (p = it_props; p->handler; ++p)
3093 {
3094 Lisp_Object new_value;
3095
3096 new_value = textget (next_iv->plist, *p->name);
3097 if (!EQ (values_here[p->idx], new_value))
3098 break;
3099 }
3100
3101 if (p->handler)
3102 break;
3103 }
3104
3105 if (!NULL_INTERVAL_P (next_iv))
3106 {
3107 if (INTEGERP (limit)
3108 && next_iv->position >= XFASTINT (limit))
3109 /* No text property change up to limit. */
3110 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
3111 else
3112 /* Text properties change in next_iv. */
3113 it->stop_charpos = min (it->stop_charpos, next_iv->position);
3114 }
3115 }
3116
3117 xassert (STRINGP (it->string)
3118 || (it->stop_charpos >= BEGV
3119 && it->stop_charpos >= IT_CHARPOS (*it)));
3120 }
3121
3122
3123 /* Return the position of the next overlay change after POS in
3124 current_buffer. Value is point-max if no overlay change
3125 follows. This is like `next-overlay-change' but doesn't use
3126 xmalloc. */
3127
3128 static int
3129 next_overlay_change (pos)
3130 int pos;
3131 {
3132 int noverlays;
3133 int endpos;
3134 Lisp_Object *overlays;
3135 int i;
3136
3137 /* Get all overlays at the given position. */
3138 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, 1);
3139
3140 /* If any of these overlays ends before endpos,
3141 use its ending point instead. */
3142 for (i = 0; i < noverlays; ++i)
3143 {
3144 Lisp_Object oend;
3145 int oendpos;
3146
3147 oend = OVERLAY_END (overlays[i]);
3148 oendpos = OVERLAY_POSITION (oend);
3149 endpos = min (endpos, oendpos);
3150 }
3151
3152 return endpos;
3153 }
3154
3155
3156 \f
3157 /***********************************************************************
3158 Fontification
3159 ***********************************************************************/
3160
3161 /* Handle changes in the `fontified' property of the current buffer by
3162 calling hook functions from Qfontification_functions to fontify
3163 regions of text. */
3164
3165 static enum prop_handled
3166 handle_fontified_prop (it)
3167 struct it *it;
3168 {
3169 Lisp_Object prop, pos;
3170 enum prop_handled handled = HANDLED_NORMALLY;
3171
3172 if (!NILP (Vmemory_full))
3173 return handled;
3174
3175 /* Get the value of the `fontified' property at IT's current buffer
3176 position. (The `fontified' property doesn't have a special
3177 meaning in strings.) If the value is nil, call functions from
3178 Qfontification_functions. */
3179 if (!STRINGP (it->string)
3180 && it->s == NULL
3181 && !NILP (Vfontification_functions)
3182 && !NILP (Vrun_hooks)
3183 && (pos = make_number (IT_CHARPOS (*it)),
3184 prop = Fget_char_property (pos, Qfontified, Qnil),
3185 NILP (prop)))
3186 {
3187 int count = SPECPDL_INDEX ();
3188 Lisp_Object val;
3189
3190 val = Vfontification_functions;
3191 specbind (Qfontification_functions, Qnil);
3192
3193 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
3194 safe_call1 (val, pos);
3195 else
3196 {
3197 Lisp_Object globals, fn;
3198 struct gcpro gcpro1, gcpro2;
3199
3200 globals = Qnil;
3201 GCPRO2 (val, globals);
3202
3203 for (; CONSP (val); val = XCDR (val))
3204 {
3205 fn = XCAR (val);
3206
3207 if (EQ (fn, Qt))
3208 {
3209 /* A value of t indicates this hook has a local
3210 binding; it means to run the global binding too.
3211 In a global value, t should not occur. If it
3212 does, we must ignore it to avoid an endless
3213 loop. */
3214 for (globals = Fdefault_value (Qfontification_functions);
3215 CONSP (globals);
3216 globals = XCDR (globals))
3217 {
3218 fn = XCAR (globals);
3219 if (!EQ (fn, Qt))
3220 safe_call1 (fn, pos);
3221 }
3222 }
3223 else
3224 safe_call1 (fn, pos);
3225 }
3226
3227 UNGCPRO;
3228 }
3229
3230 unbind_to (count, Qnil);
3231
3232 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
3233 something. This avoids an endless loop if they failed to
3234 fontify the text for which reason ever. */
3235 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
3236 handled = HANDLED_RECOMPUTE_PROPS;
3237 }
3238
3239 return handled;
3240 }
3241
3242
3243 \f
3244 /***********************************************************************
3245 Faces
3246 ***********************************************************************/
3247
3248 /* Set up iterator IT from face properties at its current position.
3249 Called from handle_stop. */
3250
3251 static enum prop_handled
3252 handle_face_prop (it)
3253 struct it *it;
3254 {
3255 int new_face_id, next_stop;
3256
3257 if (!STRINGP (it->string))
3258 {
3259 new_face_id
3260 = face_at_buffer_position (it->w,
3261 IT_CHARPOS (*it),
3262 it->region_beg_charpos,
3263 it->region_end_charpos,
3264 &next_stop,
3265 (IT_CHARPOS (*it)
3266 + TEXT_PROP_DISTANCE_LIMIT),
3267 0);
3268
3269 /* Is this a start of a run of characters with box face?
3270 Caveat: this can be called for a freshly initialized
3271 iterator; face_id is -1 in this case. We know that the new
3272 face will not change until limit, i.e. if the new face has a
3273 box, all characters up to limit will have one. But, as
3274 usual, we don't know whether limit is really the end. */
3275 if (new_face_id != it->face_id)
3276 {
3277 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3278
3279 /* If new face has a box but old face has not, this is
3280 the start of a run of characters with box, i.e. it has
3281 a shadow on the left side. The value of face_id of the
3282 iterator will be -1 if this is the initial call that gets
3283 the face. In this case, we have to look in front of IT's
3284 position and see whether there is a face != new_face_id. */
3285 it->start_of_box_run_p
3286 = (new_face->box != FACE_NO_BOX
3287 && (it->face_id >= 0
3288 || IT_CHARPOS (*it) == BEG
3289 || new_face_id != face_before_it_pos (it)));
3290 it->face_box_p = new_face->box != FACE_NO_BOX;
3291 }
3292 }
3293 else
3294 {
3295 int base_face_id, bufpos;
3296
3297 if (it->current.overlay_string_index >= 0)
3298 bufpos = IT_CHARPOS (*it);
3299 else
3300 bufpos = 0;
3301
3302 /* For strings from a buffer, i.e. overlay strings or strings
3303 from a `display' property, use the face at IT's current
3304 buffer position as the base face to merge with, so that
3305 overlay strings appear in the same face as surrounding
3306 text, unless they specify their own faces. */
3307 base_face_id = underlying_face_id (it);
3308
3309 new_face_id = face_at_string_position (it->w,
3310 it->string,
3311 IT_STRING_CHARPOS (*it),
3312 bufpos,
3313 it->region_beg_charpos,
3314 it->region_end_charpos,
3315 &next_stop,
3316 base_face_id, 0);
3317
3318 #if 0 /* This shouldn't be neccessary. Let's check it. */
3319 /* If IT is used to display a mode line we would really like to
3320 use the mode line face instead of the frame's default face. */
3321 if (it->glyph_row == MATRIX_MODE_LINE_ROW (it->w->desired_matrix)
3322 && new_face_id == DEFAULT_FACE_ID)
3323 new_face_id = CURRENT_MODE_LINE_FACE_ID (it->w);
3324 #endif
3325
3326 /* Is this a start of a run of characters with box? Caveat:
3327 this can be called for a freshly allocated iterator; face_id
3328 is -1 is this case. We know that the new face will not
3329 change until the next check pos, i.e. if the new face has a
3330 box, all characters up to that position will have a
3331 box. But, as usual, we don't know whether that position
3332 is really the end. */
3333 if (new_face_id != it->face_id)
3334 {
3335 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3336 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3337
3338 /* If new face has a box but old face hasn't, this is the
3339 start of a run of characters with box, i.e. it has a
3340 shadow on the left side. */
3341 it->start_of_box_run_p
3342 = new_face->box && (old_face == NULL || !old_face->box);
3343 it->face_box_p = new_face->box != FACE_NO_BOX;
3344 }
3345 }
3346
3347 it->face_id = new_face_id;
3348 return HANDLED_NORMALLY;
3349 }
3350
3351
3352 /* Return the ID of the face ``underlying'' IT's current position,
3353 which is in a string. If the iterator is associated with a
3354 buffer, return the face at IT's current buffer position.
3355 Otherwise, use the iterator's base_face_id. */
3356
3357 static int
3358 underlying_face_id (it)
3359 struct it *it;
3360 {
3361 int face_id = it->base_face_id, i;
3362
3363 xassert (STRINGP (it->string));
3364
3365 for (i = it->sp - 1; i >= 0; --i)
3366 if (NILP (it->stack[i].string))
3367 face_id = it->stack[i].face_id;
3368
3369 return face_id;
3370 }
3371
3372
3373 /* Compute the face one character before or after the current position
3374 of IT. BEFORE_P non-zero means get the face in front of IT's
3375 position. Value is the id of the face. */
3376
3377 static int
3378 face_before_or_after_it_pos (it, before_p)
3379 struct it *it;
3380 int before_p;
3381 {
3382 int face_id, limit;
3383 int next_check_charpos;
3384 struct text_pos pos;
3385
3386 xassert (it->s == NULL);
3387
3388 if (STRINGP (it->string))
3389 {
3390 int bufpos, base_face_id;
3391
3392 /* No face change past the end of the string (for the case
3393 we are padding with spaces). No face change before the
3394 string start. */
3395 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
3396 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
3397 return it->face_id;
3398
3399 /* Set pos to the position before or after IT's current position. */
3400 if (before_p)
3401 pos = string_pos (IT_STRING_CHARPOS (*it) - 1, it->string);
3402 else
3403 /* For composition, we must check the character after the
3404 composition. */
3405 pos = (it->what == IT_COMPOSITION
3406 ? string_pos (IT_STRING_CHARPOS (*it) + it->cmp_len, it->string)
3407 : string_pos (IT_STRING_CHARPOS (*it) + 1, it->string));
3408
3409 if (it->current.overlay_string_index >= 0)
3410 bufpos = IT_CHARPOS (*it);
3411 else
3412 bufpos = 0;
3413
3414 base_face_id = underlying_face_id (it);
3415
3416 /* Get the face for ASCII, or unibyte. */
3417 face_id = face_at_string_position (it->w,
3418 it->string,
3419 CHARPOS (pos),
3420 bufpos,
3421 it->region_beg_charpos,
3422 it->region_end_charpos,
3423 &next_check_charpos,
3424 base_face_id, 0);
3425
3426 /* Correct the face for charsets different from ASCII. Do it
3427 for the multibyte case only. The face returned above is
3428 suitable for unibyte text if IT->string is unibyte. */
3429 if (STRING_MULTIBYTE (it->string))
3430 {
3431 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos);
3432 int rest = SBYTES (it->string) - BYTEPOS (pos);
3433 int c, len;
3434 struct face *face = FACE_FROM_ID (it->f, face_id);
3435
3436 c = string_char_and_length (p, rest, &len);
3437 face_id = FACE_FOR_CHAR (it->f, face, c);
3438 }
3439 }
3440 else
3441 {
3442 if ((IT_CHARPOS (*it) >= ZV && !before_p)
3443 || (IT_CHARPOS (*it) <= BEGV && before_p))
3444 return it->face_id;
3445
3446 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
3447 pos = it->current.pos;
3448
3449 if (before_p)
3450 DEC_TEXT_POS (pos, it->multibyte_p);
3451 else
3452 {
3453 if (it->what == IT_COMPOSITION)
3454 /* For composition, we must check the position after the
3455 composition. */
3456 pos.charpos += it->cmp_len, pos.bytepos += it->len;
3457 else
3458 INC_TEXT_POS (pos, it->multibyte_p);
3459 }
3460
3461 /* Determine face for CHARSET_ASCII, or unibyte. */
3462 face_id = face_at_buffer_position (it->w,
3463 CHARPOS (pos),
3464 it->region_beg_charpos,
3465 it->region_end_charpos,
3466 &next_check_charpos,
3467 limit, 0);
3468
3469 /* Correct the face for charsets different from ASCII. Do it
3470 for the multibyte case only. The face returned above is
3471 suitable for unibyte text if current_buffer is unibyte. */
3472 if (it->multibyte_p)
3473 {
3474 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
3475 struct face *face = FACE_FROM_ID (it->f, face_id);
3476 face_id = FACE_FOR_CHAR (it->f, face, c);
3477 }
3478 }
3479
3480 return face_id;
3481 }
3482
3483
3484 \f
3485 /***********************************************************************
3486 Invisible text
3487 ***********************************************************************/
3488
3489 /* Set up iterator IT from invisible properties at its current
3490 position. Called from handle_stop. */
3491
3492 static enum prop_handled
3493 handle_invisible_prop (it)
3494 struct it *it;
3495 {
3496 enum prop_handled handled = HANDLED_NORMALLY;
3497
3498 if (STRINGP (it->string))
3499 {
3500 extern Lisp_Object Qinvisible;
3501 Lisp_Object prop, end_charpos, limit, charpos;
3502
3503 /* Get the value of the invisible text property at the
3504 current position. Value will be nil if there is no such
3505 property. */
3506 charpos = make_number (IT_STRING_CHARPOS (*it));
3507 prop = Fget_text_property (charpos, Qinvisible, it->string);
3508
3509 if (!NILP (prop)
3510 && IT_STRING_CHARPOS (*it) < it->end_charpos)
3511 {
3512 handled = HANDLED_RECOMPUTE_PROPS;
3513
3514 /* Get the position at which the next change of the
3515 invisible text property can be found in IT->string.
3516 Value will be nil if the property value is the same for
3517 all the rest of IT->string. */
3518 XSETINT (limit, SCHARS (it->string));
3519 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
3520 it->string, limit);
3521
3522 /* Text at current position is invisible. The next
3523 change in the property is at position end_charpos.
3524 Move IT's current position to that position. */
3525 if (INTEGERP (end_charpos)
3526 && XFASTINT (end_charpos) < XFASTINT (limit))
3527 {
3528 struct text_pos old;
3529 old = it->current.string_pos;
3530 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
3531 compute_string_pos (&it->current.string_pos, old, it->string);
3532 }
3533 else
3534 {
3535 /* The rest of the string is invisible. If this is an
3536 overlay string, proceed with the next overlay string
3537 or whatever comes and return a character from there. */
3538 if (it->current.overlay_string_index >= 0)
3539 {
3540 next_overlay_string (it);
3541 /* Don't check for overlay strings when we just
3542 finished processing them. */
3543 handled = HANDLED_OVERLAY_STRING_CONSUMED;
3544 }
3545 else
3546 {
3547 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
3548 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
3549 }
3550 }
3551 }
3552 }
3553 else
3554 {
3555 int invis_p, newpos, next_stop, start_charpos;
3556 Lisp_Object pos, prop, overlay;
3557
3558 /* First of all, is there invisible text at this position? */
3559 start_charpos = IT_CHARPOS (*it);
3560 pos = make_number (IT_CHARPOS (*it));
3561 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
3562 &overlay);
3563 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3564
3565 /* If we are on invisible text, skip over it. */
3566 if (invis_p && IT_CHARPOS (*it) < it->end_charpos)
3567 {
3568 /* Record whether we have to display an ellipsis for the
3569 invisible text. */
3570 int display_ellipsis_p = invis_p == 2;
3571
3572 handled = HANDLED_RECOMPUTE_PROPS;
3573
3574 /* Loop skipping over invisible text. The loop is left at
3575 ZV or with IT on the first char being visible again. */
3576 do
3577 {
3578 /* Try to skip some invisible text. Return value is the
3579 position reached which can be equal to IT's position
3580 if there is nothing invisible here. This skips both
3581 over invisible text properties and overlays with
3582 invisible property. */
3583 newpos = skip_invisible (IT_CHARPOS (*it),
3584 &next_stop, ZV, it->window);
3585
3586 /* If we skipped nothing at all we weren't at invisible
3587 text in the first place. If everything to the end of
3588 the buffer was skipped, end the loop. */
3589 if (newpos == IT_CHARPOS (*it) || newpos >= ZV)
3590 invis_p = 0;
3591 else
3592 {
3593 /* We skipped some characters but not necessarily
3594 all there are. Check if we ended up on visible
3595 text. Fget_char_property returns the property of
3596 the char before the given position, i.e. if we
3597 get invis_p = 0, this means that the char at
3598 newpos is visible. */
3599 pos = make_number (newpos);
3600 prop = Fget_char_property (pos, Qinvisible, it->window);
3601 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3602 }
3603
3604 /* If we ended up on invisible text, proceed to
3605 skip starting with next_stop. */
3606 if (invis_p)
3607 IT_CHARPOS (*it) = next_stop;
3608 }
3609 while (invis_p);
3610
3611 /* The position newpos is now either ZV or on visible text. */
3612 IT_CHARPOS (*it) = newpos;
3613 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
3614
3615 /* If there are before-strings at the start of invisible
3616 text, and the text is invisible because of a text
3617 property, arrange to show before-strings because 20.x did
3618 it that way. (If the text is invisible because of an
3619 overlay property instead of a text property, this is
3620 already handled in the overlay code.) */
3621 if (NILP (overlay)
3622 && get_overlay_strings (it, start_charpos))
3623 {
3624 handled = HANDLED_RECOMPUTE_PROPS;
3625 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
3626 }
3627 else if (display_ellipsis_p)
3628 setup_for_ellipsis (it, 0);
3629 }
3630 }
3631
3632 return handled;
3633 }
3634
3635
3636 /* Make iterator IT return `...' next.
3637 Replaces LEN characters from buffer. */
3638
3639 static void
3640 setup_for_ellipsis (it, len)
3641 struct it *it;
3642 int len;
3643 {
3644 /* Use the display table definition for `...'. Invalid glyphs
3645 will be handled by the method returning elements from dpvec. */
3646 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
3647 {
3648 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
3649 it->dpvec = v->contents;
3650 it->dpend = v->contents + v->size;
3651 }
3652 else
3653 {
3654 /* Default `...'. */
3655 it->dpvec = default_invis_vector;
3656 it->dpend = default_invis_vector + 3;
3657 }
3658
3659 it->dpvec_char_len = len;
3660 it->current.dpvec_index = 0;
3661 it->dpvec_face_id = -1;
3662
3663 /* Remember the current face id in case glyphs specify faces.
3664 IT's face is restored in set_iterator_to_next.
3665 saved_face_id was set to preceding char's face in handle_stop. */
3666 if (it->saved_face_id < 0 || it->saved_face_id != it->face_id)
3667 it->saved_face_id = it->face_id = DEFAULT_FACE_ID;
3668
3669 it->method = GET_FROM_DISPLAY_VECTOR;
3670 it->ellipsis_p = 1;
3671 }
3672
3673
3674 \f
3675 /***********************************************************************
3676 'display' property
3677 ***********************************************************************/
3678
3679 /* Set up iterator IT from `display' property at its current position.
3680 Called from handle_stop.
3681 We return HANDLED_RETURN if some part of the display property
3682 overrides the display of the buffer text itself.
3683 Otherwise we return HANDLED_NORMALLY. */
3684
3685 static enum prop_handled
3686 handle_display_prop (it)
3687 struct it *it;
3688 {
3689 Lisp_Object prop, object;
3690 struct text_pos *position;
3691 /* Nonzero if some property replaces the display of the text itself. */
3692 int display_replaced_p = 0;
3693
3694 if (STRINGP (it->string))
3695 {
3696 object = it->string;
3697 position = &it->current.string_pos;
3698 }
3699 else
3700 {
3701 XSETWINDOW (object, it->w);
3702 position = &it->current.pos;
3703 }
3704
3705 /* Reset those iterator values set from display property values. */
3706 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
3707 it->space_width = Qnil;
3708 it->font_height = Qnil;
3709 it->voffset = 0;
3710
3711 /* We don't support recursive `display' properties, i.e. string
3712 values that have a string `display' property, that have a string
3713 `display' property etc. */
3714 if (!it->string_from_display_prop_p)
3715 it->area = TEXT_AREA;
3716
3717 prop = Fget_char_property (make_number (position->charpos),
3718 Qdisplay, object);
3719 if (NILP (prop))
3720 return HANDLED_NORMALLY;
3721
3722 if (!STRINGP (it->string))
3723 object = it->w->buffer;
3724
3725 if (CONSP (prop)
3726 /* Simple properties. */
3727 && !EQ (XCAR (prop), Qimage)
3728 && !EQ (XCAR (prop), Qspace)
3729 && !EQ (XCAR (prop), Qwhen)
3730 && !EQ (XCAR (prop), Qslice)
3731 && !EQ (XCAR (prop), Qspace_width)
3732 && !EQ (XCAR (prop), Qheight)
3733 && !EQ (XCAR (prop), Qraise)
3734 /* Marginal area specifications. */
3735 && !(CONSP (XCAR (prop)) && EQ (XCAR (XCAR (prop)), Qmargin))
3736 && !EQ (XCAR (prop), Qleft_fringe)
3737 && !EQ (XCAR (prop), Qright_fringe)
3738 && !NILP (XCAR (prop)))
3739 {
3740 for (; CONSP (prop); prop = XCDR (prop))
3741 {
3742 if (handle_single_display_spec (it, XCAR (prop), object,
3743 position, display_replaced_p))
3744 display_replaced_p = 1;
3745 }
3746 }
3747 else if (VECTORP (prop))
3748 {
3749 int i;
3750 for (i = 0; i < ASIZE (prop); ++i)
3751 if (handle_single_display_spec (it, AREF (prop, i), object,
3752 position, display_replaced_p))
3753 display_replaced_p = 1;
3754 }
3755 else
3756 {
3757 int ret = handle_single_display_spec (it, prop, object, position, 0);
3758 if (ret < 0) /* Replaced by "", i.e. nothing. */
3759 return HANDLED_RECOMPUTE_PROPS;
3760 if (ret)
3761 display_replaced_p = 1;
3762 }
3763
3764 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
3765 }
3766
3767
3768 /* Value is the position of the end of the `display' property starting
3769 at START_POS in OBJECT. */
3770
3771 static struct text_pos
3772 display_prop_end (it, object, start_pos)
3773 struct it *it;
3774 Lisp_Object object;
3775 struct text_pos start_pos;
3776 {
3777 Lisp_Object end;
3778 struct text_pos end_pos;
3779
3780 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
3781 Qdisplay, object, Qnil);
3782 CHARPOS (end_pos) = XFASTINT (end);
3783 if (STRINGP (object))
3784 compute_string_pos (&end_pos, start_pos, it->string);
3785 else
3786 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
3787
3788 return end_pos;
3789 }
3790
3791
3792 /* Set up IT from a single `display' specification PROP. OBJECT
3793 is the object in which the `display' property was found. *POSITION
3794 is the position at which it was found. DISPLAY_REPLACED_P non-zero
3795 means that we previously saw a display specification which already
3796 replaced text display with something else, for example an image;
3797 we ignore such properties after the first one has been processed.
3798
3799 If PROP is a `space' or `image' specification, and in some other
3800 cases too, set *POSITION to the position where the `display'
3801 property ends.
3802
3803 Value is non-zero if something was found which replaces the display
3804 of buffer or string text. Specifically, the value is -1 if that
3805 "something" is "nothing". */
3806
3807 static int
3808 handle_single_display_spec (it, spec, object, position,
3809 display_replaced_before_p)
3810 struct it *it;
3811 Lisp_Object spec;
3812 Lisp_Object object;
3813 struct text_pos *position;
3814 int display_replaced_before_p;
3815 {
3816 Lisp_Object form;
3817 Lisp_Object location, value;
3818 struct text_pos start_pos;
3819 int valid_p;
3820
3821 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
3822 If the result is non-nil, use VALUE instead of SPEC. */
3823 form = Qt;
3824 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
3825 {
3826 spec = XCDR (spec);
3827 if (!CONSP (spec))
3828 return 0;
3829 form = XCAR (spec);
3830 spec = XCDR (spec);
3831 }
3832
3833 if (!NILP (form) && !EQ (form, Qt))
3834 {
3835 int count = SPECPDL_INDEX ();
3836 struct gcpro gcpro1;
3837
3838 /* Bind `object' to the object having the `display' property, a
3839 buffer or string. Bind `position' to the position in the
3840 object where the property was found, and `buffer-position'
3841 to the current position in the buffer. */
3842 specbind (Qobject, object);
3843 specbind (Qposition, make_number (CHARPOS (*position)));
3844 specbind (Qbuffer_position,
3845 make_number (STRINGP (object)
3846 ? IT_CHARPOS (*it) : CHARPOS (*position)));
3847 GCPRO1 (form);
3848 form = safe_eval (form);
3849 UNGCPRO;
3850 unbind_to (count, Qnil);
3851 }
3852
3853 if (NILP (form))
3854 return 0;
3855
3856 /* Handle `(height HEIGHT)' specifications. */
3857 if (CONSP (spec)
3858 && EQ (XCAR (spec), Qheight)
3859 && CONSP (XCDR (spec)))
3860 {
3861 if (!FRAME_WINDOW_P (it->f))
3862 return 0;
3863
3864 it->font_height = XCAR (XCDR (spec));
3865 if (!NILP (it->font_height))
3866 {
3867 struct face *face = FACE_FROM_ID (it->f, it->face_id);
3868 int new_height = -1;
3869
3870 if (CONSP (it->font_height)
3871 && (EQ (XCAR (it->font_height), Qplus)
3872 || EQ (XCAR (it->font_height), Qminus))
3873 && CONSP (XCDR (it->font_height))
3874 && INTEGERP (XCAR (XCDR (it->font_height))))
3875 {
3876 /* `(+ N)' or `(- N)' where N is an integer. */
3877 int steps = XINT (XCAR (XCDR (it->font_height)));
3878 if (EQ (XCAR (it->font_height), Qplus))
3879 steps = - steps;
3880 it->face_id = smaller_face (it->f, it->face_id, steps);
3881 }
3882 else if (FUNCTIONP (it->font_height))
3883 {
3884 /* Call function with current height as argument.
3885 Value is the new height. */
3886 Lisp_Object height;
3887 height = safe_call1 (it->font_height,
3888 face->lface[LFACE_HEIGHT_INDEX]);
3889 if (NUMBERP (height))
3890 new_height = XFLOATINT (height);
3891 }
3892 else if (NUMBERP (it->font_height))
3893 {
3894 /* Value is a multiple of the canonical char height. */
3895 struct face *face;
3896
3897 face = FACE_FROM_ID (it->f, DEFAULT_FACE_ID);
3898 new_height = (XFLOATINT (it->font_height)
3899 * XINT (face->lface[LFACE_HEIGHT_INDEX]));
3900 }
3901 else
3902 {
3903 /* Evaluate IT->font_height with `height' bound to the
3904 current specified height to get the new height. */
3905 int count = SPECPDL_INDEX ();
3906
3907 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
3908 value = safe_eval (it->font_height);
3909 unbind_to (count, Qnil);
3910
3911 if (NUMBERP (value))
3912 new_height = XFLOATINT (value);
3913 }
3914
3915 if (new_height > 0)
3916 it->face_id = face_with_height (it->f, it->face_id, new_height);
3917 }
3918
3919 return 0;
3920 }
3921
3922 /* Handle `(space_width WIDTH)'. */
3923 if (CONSP (spec)
3924 && EQ (XCAR (spec), Qspace_width)
3925 && CONSP (XCDR (spec)))
3926 {
3927 if (!FRAME_WINDOW_P (it->f))
3928 return 0;
3929
3930 value = XCAR (XCDR (spec));
3931 if (NUMBERP (value) && XFLOATINT (value) > 0)
3932 it->space_width = value;
3933
3934 return 0;
3935 }
3936
3937 /* Handle `(slice X Y WIDTH HEIGHT)'. */
3938 if (CONSP (spec)
3939 && EQ (XCAR (spec), Qslice))
3940 {
3941 Lisp_Object tem;
3942
3943 if (!FRAME_WINDOW_P (it->f))
3944 return 0;
3945
3946 if (tem = XCDR (spec), CONSP (tem))
3947 {
3948 it->slice.x = XCAR (tem);
3949 if (tem = XCDR (tem), CONSP (tem))
3950 {
3951 it->slice.y = XCAR (tem);
3952 if (tem = XCDR (tem), CONSP (tem))
3953 {
3954 it->slice.width = XCAR (tem);
3955 if (tem = XCDR (tem), CONSP (tem))
3956 it->slice.height = XCAR (tem);
3957 }
3958 }
3959 }
3960
3961 return 0;
3962 }
3963
3964 /* Handle `(raise FACTOR)'. */
3965 if (CONSP (spec)
3966 && EQ (XCAR (spec), Qraise)
3967 && CONSP (XCDR (spec)))
3968 {
3969 if (!FRAME_WINDOW_P (it->f))
3970 return 0;
3971
3972 #ifdef HAVE_WINDOW_SYSTEM
3973 value = XCAR (XCDR (spec));
3974 if (NUMBERP (value))
3975 {
3976 struct face *face = FACE_FROM_ID (it->f, it->face_id);
3977 it->voffset = - (XFLOATINT (value)
3978 * (FONT_HEIGHT (face->font)));
3979 }
3980 #endif /* HAVE_WINDOW_SYSTEM */
3981
3982 return 0;
3983 }
3984
3985 /* Don't handle the other kinds of display specifications
3986 inside a string that we got from a `display' property. */
3987 if (it->string_from_display_prop_p)
3988 return 0;
3989
3990 /* Characters having this form of property are not displayed, so
3991 we have to find the end of the property. */
3992 start_pos = *position;
3993 *position = display_prop_end (it, object, start_pos);
3994 value = Qnil;
3995
3996 /* Stop the scan at that end position--we assume that all
3997 text properties change there. */
3998 it->stop_charpos = position->charpos;
3999
4000 /* Handle `(left-fringe BITMAP [FACE])'
4001 and `(right-fringe BITMAP [FACE])'. */
4002 if (CONSP (spec)
4003 && (EQ (XCAR (spec), Qleft_fringe)
4004 || EQ (XCAR (spec), Qright_fringe))
4005 && CONSP (XCDR (spec)))
4006 {
4007 int face_id = DEFAULT_FACE_ID;
4008 int fringe_bitmap;
4009
4010 if (!FRAME_WINDOW_P (it->f))
4011 /* If we return here, POSITION has been advanced
4012 across the text with this property. */
4013 return 0;
4014
4015 #ifdef HAVE_WINDOW_SYSTEM
4016 value = XCAR (XCDR (spec));
4017 if (!SYMBOLP (value)
4018 || !(fringe_bitmap = lookup_fringe_bitmap (value)))
4019 /* If we return here, POSITION has been advanced
4020 across the text with this property. */
4021 return 0;
4022
4023 if (CONSP (XCDR (XCDR (spec))))
4024 {
4025 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
4026 int face_id2 = lookup_derived_face (it->f, face_name,
4027 'A', FRINGE_FACE_ID, 0);
4028 if (face_id2 >= 0)
4029 face_id = face_id2;
4030 }
4031
4032 /* Save current settings of IT so that we can restore them
4033 when we are finished with the glyph property value. */
4034
4035 push_it (it);
4036
4037 it->area = TEXT_AREA;
4038 it->what = IT_IMAGE;
4039 it->image_id = -1; /* no image */
4040 it->position = start_pos;
4041 it->object = NILP (object) ? it->w->buffer : object;
4042 it->method = GET_FROM_IMAGE;
4043 it->face_id = face_id;
4044
4045 /* Say that we haven't consumed the characters with
4046 `display' property yet. The call to pop_it in
4047 set_iterator_to_next will clean this up. */
4048 *position = start_pos;
4049
4050 if (EQ (XCAR (spec), Qleft_fringe))
4051 {
4052 it->left_user_fringe_bitmap = fringe_bitmap;
4053 it->left_user_fringe_face_id = face_id;
4054 }
4055 else
4056 {
4057 it->right_user_fringe_bitmap = fringe_bitmap;
4058 it->right_user_fringe_face_id = face_id;
4059 }
4060 #endif /* HAVE_WINDOW_SYSTEM */
4061 return 1;
4062 }
4063
4064 /* Prepare to handle `((margin left-margin) ...)',
4065 `((margin right-margin) ...)' and `((margin nil) ...)'
4066 prefixes for display specifications. */
4067 location = Qunbound;
4068 if (CONSP (spec) && CONSP (XCAR (spec)))
4069 {
4070 Lisp_Object tem;
4071
4072 value = XCDR (spec);
4073 if (CONSP (value))
4074 value = XCAR (value);
4075
4076 tem = XCAR (spec);
4077 if (EQ (XCAR (tem), Qmargin)
4078 && (tem = XCDR (tem),
4079 tem = CONSP (tem) ? XCAR (tem) : Qnil,
4080 (NILP (tem)
4081 || EQ (tem, Qleft_margin)
4082 || EQ (tem, Qright_margin))))
4083 location = tem;
4084 }
4085
4086 if (EQ (location, Qunbound))
4087 {
4088 location = Qnil;
4089 value = spec;
4090 }
4091
4092 /* After this point, VALUE is the property after any
4093 margin prefix has been stripped. It must be a string,
4094 an image specification, or `(space ...)'.
4095
4096 LOCATION specifies where to display: `left-margin',
4097 `right-margin' or nil. */
4098
4099 valid_p = (STRINGP (value)
4100 #ifdef HAVE_WINDOW_SYSTEM
4101 || (FRAME_WINDOW_P (it->f) && valid_image_p (value))
4102 #endif /* not HAVE_WINDOW_SYSTEM */
4103 || (CONSP (value) && EQ (XCAR (value), Qspace)));
4104
4105 if (valid_p && !display_replaced_before_p)
4106 {
4107 /* Save current settings of IT so that we can restore them
4108 when we are finished with the glyph property value. */
4109 push_it (it);
4110 if (NILP (location))
4111 it->area = TEXT_AREA;
4112 else if (EQ (location, Qleft_margin))
4113 it->area = LEFT_MARGIN_AREA;
4114 else
4115 it->area = RIGHT_MARGIN_AREA;
4116
4117 if (STRINGP (value))
4118 {
4119 if (SCHARS (value) == 0)
4120 {
4121 pop_it (it);
4122 return -1; /* Replaced by "", i.e. nothing. */
4123 }
4124 it->string = value;
4125 it->multibyte_p = STRING_MULTIBYTE (it->string);
4126 it->current.overlay_string_index = -1;
4127 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
4128 it->end_charpos = it->string_nchars = SCHARS (it->string);
4129 it->method = GET_FROM_STRING;
4130 it->stop_charpos = 0;
4131 it->string_from_display_prop_p = 1;
4132 /* Say that we haven't consumed the characters with
4133 `display' property yet. The call to pop_it in
4134 set_iterator_to_next will clean this up. */
4135 *position = start_pos;
4136 }
4137 else if (CONSP (value) && EQ (XCAR (value), Qspace))
4138 {
4139 it->method = GET_FROM_STRETCH;
4140 it->object = value;
4141 it->current.pos = it->position = start_pos;
4142
4143 }
4144 #ifdef HAVE_WINDOW_SYSTEM
4145 else
4146 {
4147 it->what = IT_IMAGE;
4148 it->image_id = lookup_image (it->f, value);
4149 it->position = start_pos;
4150 it->object = NILP (object) ? it->w->buffer : object;
4151 it->method = GET_FROM_IMAGE;
4152
4153 /* Say that we haven't consumed the characters with
4154 `display' property yet. The call to pop_it in
4155 set_iterator_to_next will clean this up. */
4156 *position = start_pos;
4157 }
4158 #endif /* HAVE_WINDOW_SYSTEM */
4159
4160 return 1;
4161 }
4162
4163 /* Invalid property or property not supported. Restore
4164 POSITION to what it was before. */
4165 *position = start_pos;
4166 return 0;
4167 }
4168
4169
4170 /* Check if SPEC is a display sub-property value whose text should be
4171 treated as intangible. */
4172
4173 static int
4174 single_display_spec_intangible_p (prop)
4175 Lisp_Object prop;
4176 {
4177 /* Skip over `when FORM'. */
4178 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
4179 {
4180 prop = XCDR (prop);
4181 if (!CONSP (prop))
4182 return 0;
4183 prop = XCDR (prop);
4184 }
4185
4186 if (STRINGP (prop))
4187 return 1;
4188
4189 if (!CONSP (prop))
4190 return 0;
4191
4192 /* Skip over `margin LOCATION'. If LOCATION is in the margins,
4193 we don't need to treat text as intangible. */
4194 if (EQ (XCAR (prop), Qmargin))
4195 {
4196 prop = XCDR (prop);
4197 if (!CONSP (prop))
4198 return 0;
4199
4200 prop = XCDR (prop);
4201 if (!CONSP (prop)
4202 || EQ (XCAR (prop), Qleft_margin)
4203 || EQ (XCAR (prop), Qright_margin))
4204 return 0;
4205 }
4206
4207 return (CONSP (prop)
4208 && (EQ (XCAR (prop), Qimage)
4209 || EQ (XCAR (prop), Qspace)));
4210 }
4211
4212
4213 /* Check if PROP is a display property value whose text should be
4214 treated as intangible. */
4215
4216 int
4217 display_prop_intangible_p (prop)
4218 Lisp_Object prop;
4219 {
4220 if (CONSP (prop)
4221 && CONSP (XCAR (prop))
4222 && !EQ (Qmargin, XCAR (XCAR (prop))))
4223 {
4224 /* A list of sub-properties. */
4225 while (CONSP (prop))
4226 {
4227 if (single_display_spec_intangible_p (XCAR (prop)))
4228 return 1;
4229 prop = XCDR (prop);
4230 }
4231 }
4232 else if (VECTORP (prop))
4233 {
4234 /* A vector of sub-properties. */
4235 int i;
4236 for (i = 0; i < ASIZE (prop); ++i)
4237 if (single_display_spec_intangible_p (AREF (prop, i)))
4238 return 1;
4239 }
4240 else
4241 return single_display_spec_intangible_p (prop);
4242
4243 return 0;
4244 }
4245
4246
4247 /* Return 1 if PROP is a display sub-property value containing STRING. */
4248
4249 static int
4250 single_display_spec_string_p (prop, string)
4251 Lisp_Object prop, string;
4252 {
4253 if (EQ (string, prop))
4254 return 1;
4255
4256 /* Skip over `when FORM'. */
4257 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
4258 {
4259 prop = XCDR (prop);
4260 if (!CONSP (prop))
4261 return 0;
4262 prop = XCDR (prop);
4263 }
4264
4265 if (CONSP (prop))
4266 /* Skip over `margin LOCATION'. */
4267 if (EQ (XCAR (prop), Qmargin))
4268 {
4269 prop = XCDR (prop);
4270 if (!CONSP (prop))
4271 return 0;
4272
4273 prop = XCDR (prop);
4274 if (!CONSP (prop))
4275 return 0;
4276 }
4277
4278 return CONSP (prop) && EQ (XCAR (prop), string);
4279 }
4280
4281
4282 /* Return 1 if STRING appears in the `display' property PROP. */
4283
4284 static int
4285 display_prop_string_p (prop, string)
4286 Lisp_Object prop, string;
4287 {
4288 if (CONSP (prop)
4289 && CONSP (XCAR (prop))
4290 && !EQ (Qmargin, XCAR (XCAR (prop))))
4291 {
4292 /* A list of sub-properties. */
4293 while (CONSP (prop))
4294 {
4295 if (single_display_spec_string_p (XCAR (prop), string))
4296 return 1;
4297 prop = XCDR (prop);
4298 }
4299 }
4300 else if (VECTORP (prop))
4301 {
4302 /* A vector of sub-properties. */
4303 int i;
4304 for (i = 0; i < ASIZE (prop); ++i)
4305 if (single_display_spec_string_p (AREF (prop, i), string))
4306 return 1;
4307 }
4308 else
4309 return single_display_spec_string_p (prop, string);
4310
4311 return 0;
4312 }
4313
4314
4315 /* Determine from which buffer position in W's buffer STRING comes
4316 from. AROUND_CHARPOS is an approximate position where it could
4317 be from. Value is the buffer position or 0 if it couldn't be
4318 determined.
4319
4320 W's buffer must be current.
4321
4322 This function is necessary because we don't record buffer positions
4323 in glyphs generated from strings (to keep struct glyph small).
4324 This function may only use code that doesn't eval because it is
4325 called asynchronously from note_mouse_highlight. */
4326
4327 int
4328 string_buffer_position (w, string, around_charpos)
4329 struct window *w;
4330 Lisp_Object string;
4331 int around_charpos;
4332 {
4333 Lisp_Object limit, prop, pos;
4334 const int MAX_DISTANCE = 1000;
4335 int found = 0;
4336
4337 pos = make_number (around_charpos);
4338 limit = make_number (min (XINT (pos) + MAX_DISTANCE, ZV));
4339 while (!found && !EQ (pos, limit))
4340 {
4341 prop = Fget_char_property (pos, Qdisplay, Qnil);
4342 if (!NILP (prop) && display_prop_string_p (prop, string))
4343 found = 1;
4344 else
4345 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil, limit);
4346 }
4347
4348 if (!found)
4349 {
4350 pos = make_number (around_charpos);
4351 limit = make_number (max (XINT (pos) - MAX_DISTANCE, BEGV));
4352 while (!found && !EQ (pos, limit))
4353 {
4354 prop = Fget_char_property (pos, Qdisplay, Qnil);
4355 if (!NILP (prop) && display_prop_string_p (prop, string))
4356 found = 1;
4357 else
4358 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
4359 limit);
4360 }
4361 }
4362
4363 return found ? XINT (pos) : 0;
4364 }
4365
4366
4367 \f
4368 /***********************************************************************
4369 `composition' property
4370 ***********************************************************************/
4371
4372 /* Set up iterator IT from `composition' property at its current
4373 position. Called from handle_stop. */
4374
4375 static enum prop_handled
4376 handle_composition_prop (it)
4377 struct it *it;
4378 {
4379 Lisp_Object prop, string;
4380 int pos, pos_byte, end;
4381 enum prop_handled handled = HANDLED_NORMALLY;
4382
4383 if (STRINGP (it->string))
4384 {
4385 pos = IT_STRING_CHARPOS (*it);
4386 pos_byte = IT_STRING_BYTEPOS (*it);
4387 string = it->string;
4388 }
4389 else
4390 {
4391 pos = IT_CHARPOS (*it);
4392 pos_byte = IT_BYTEPOS (*it);
4393 string = Qnil;
4394 }
4395
4396 /* If there's a valid composition and point is not inside of the
4397 composition (in the case that the composition is from the current
4398 buffer), draw a glyph composed from the composition components. */
4399 if (find_composition (pos, -1, &pos, &end, &prop, string)
4400 && COMPOSITION_VALID_P (pos, end, prop)
4401 && (STRINGP (it->string) || (PT <= pos || PT >= end)))
4402 {
4403 int id = get_composition_id (pos, pos_byte, end - pos, prop, string);
4404
4405 if (id >= 0)
4406 {
4407 it->method = GET_FROM_COMPOSITION;
4408 it->cmp_id = id;
4409 it->cmp_len = COMPOSITION_LENGTH (prop);
4410 /* For a terminal, draw only the first character of the
4411 components. */
4412 it->c = COMPOSITION_GLYPH (composition_table[id], 0);
4413 it->len = (STRINGP (it->string)
4414 ? string_char_to_byte (it->string, end)
4415 : CHAR_TO_BYTE (end)) - pos_byte;
4416 it->stop_charpos = end;
4417 handled = HANDLED_RETURN;
4418 }
4419 }
4420
4421 return handled;
4422 }
4423
4424
4425 \f
4426 /***********************************************************************
4427 Overlay strings
4428 ***********************************************************************/
4429
4430 /* The following structure is used to record overlay strings for
4431 later sorting in load_overlay_strings. */
4432
4433 struct overlay_entry
4434 {
4435 Lisp_Object overlay;
4436 Lisp_Object string;
4437 int priority;
4438 int after_string_p;
4439 };
4440
4441
4442 /* Set up iterator IT from overlay strings at its current position.
4443 Called from handle_stop. */
4444
4445 static enum prop_handled
4446 handle_overlay_change (it)
4447 struct it *it;
4448 {
4449 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
4450 return HANDLED_RECOMPUTE_PROPS;
4451 else
4452 return HANDLED_NORMALLY;
4453 }
4454
4455
4456 /* Set up the next overlay string for delivery by IT, if there is an
4457 overlay string to deliver. Called by set_iterator_to_next when the
4458 end of the current overlay string is reached. If there are more
4459 overlay strings to display, IT->string and
4460 IT->current.overlay_string_index are set appropriately here.
4461 Otherwise IT->string is set to nil. */
4462
4463 static void
4464 next_overlay_string (it)
4465 struct it *it;
4466 {
4467 ++it->current.overlay_string_index;
4468 if (it->current.overlay_string_index == it->n_overlay_strings)
4469 {
4470 /* No more overlay strings. Restore IT's settings to what
4471 they were before overlay strings were processed, and
4472 continue to deliver from current_buffer. */
4473 int display_ellipsis_p = it->stack[it->sp - 1].display_ellipsis_p;
4474
4475 pop_it (it);
4476 xassert (it->stop_charpos >= BEGV
4477 && it->stop_charpos <= it->end_charpos);
4478 it->string = Qnil;
4479 it->current.overlay_string_index = -1;
4480 SET_TEXT_POS (it->current.string_pos, -1, -1);
4481 it->n_overlay_strings = 0;
4482 it->method = GET_FROM_BUFFER;
4483
4484 /* If we're at the end of the buffer, record that we have
4485 processed the overlay strings there already, so that
4486 next_element_from_buffer doesn't try it again. */
4487 if (IT_CHARPOS (*it) >= it->end_charpos)
4488 it->overlay_strings_at_end_processed_p = 1;
4489
4490 /* If we have to display `...' for invisible text, set
4491 the iterator up for that. */
4492 if (display_ellipsis_p)
4493 setup_for_ellipsis (it, 0);
4494 }
4495 else
4496 {
4497 /* There are more overlay strings to process. If
4498 IT->current.overlay_string_index has advanced to a position
4499 where we must load IT->overlay_strings with more strings, do
4500 it. */
4501 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
4502
4503 if (it->current.overlay_string_index && i == 0)
4504 load_overlay_strings (it, 0);
4505
4506 /* Initialize IT to deliver display elements from the overlay
4507 string. */
4508 it->string = it->overlay_strings[i];
4509 it->multibyte_p = STRING_MULTIBYTE (it->string);
4510 SET_TEXT_POS (it->current.string_pos, 0, 0);
4511 it->method = GET_FROM_STRING;
4512 it->stop_charpos = 0;
4513 }
4514
4515 CHECK_IT (it);
4516 }
4517
4518
4519 /* Compare two overlay_entry structures E1 and E2. Used as a
4520 comparison function for qsort in load_overlay_strings. Overlay
4521 strings for the same position are sorted so that
4522
4523 1. All after-strings come in front of before-strings, except
4524 when they come from the same overlay.
4525
4526 2. Within after-strings, strings are sorted so that overlay strings
4527 from overlays with higher priorities come first.
4528
4529 2. Within before-strings, strings are sorted so that overlay
4530 strings from overlays with higher priorities come last.
4531
4532 Value is analogous to strcmp. */
4533
4534
4535 static int
4536 compare_overlay_entries (e1, e2)
4537 void *e1, *e2;
4538 {
4539 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
4540 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
4541 int result;
4542
4543 if (entry1->after_string_p != entry2->after_string_p)
4544 {
4545 /* Let after-strings appear in front of before-strings if
4546 they come from different overlays. */
4547 if (EQ (entry1->overlay, entry2->overlay))
4548 result = entry1->after_string_p ? 1 : -1;
4549 else
4550 result = entry1->after_string_p ? -1 : 1;
4551 }
4552 else if (entry1->after_string_p)
4553 /* After-strings sorted in order of decreasing priority. */
4554 result = entry2->priority - entry1->priority;
4555 else
4556 /* Before-strings sorted in order of increasing priority. */
4557 result = entry1->priority - entry2->priority;
4558
4559 return result;
4560 }
4561
4562
4563 /* Load the vector IT->overlay_strings with overlay strings from IT's
4564 current buffer position, or from CHARPOS if that is > 0. Set
4565 IT->n_overlays to the total number of overlay strings found.
4566
4567 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
4568 a time. On entry into load_overlay_strings,
4569 IT->current.overlay_string_index gives the number of overlay
4570 strings that have already been loaded by previous calls to this
4571 function.
4572
4573 IT->add_overlay_start contains an additional overlay start
4574 position to consider for taking overlay strings from, if non-zero.
4575 This position comes into play when the overlay has an `invisible'
4576 property, and both before and after-strings. When we've skipped to
4577 the end of the overlay, because of its `invisible' property, we
4578 nevertheless want its before-string to appear.
4579 IT->add_overlay_start will contain the overlay start position
4580 in this case.
4581
4582 Overlay strings are sorted so that after-string strings come in
4583 front of before-string strings. Within before and after-strings,
4584 strings are sorted by overlay priority. See also function
4585 compare_overlay_entries. */
4586
4587 static void
4588 load_overlay_strings (it, charpos)
4589 struct it *it;
4590 int charpos;
4591 {
4592 extern Lisp_Object Qafter_string, Qbefore_string, Qwindow, Qpriority;
4593 Lisp_Object overlay, window, str, invisible;
4594 struct Lisp_Overlay *ov;
4595 int start, end;
4596 int size = 20;
4597 int n = 0, i, j, invis_p;
4598 struct overlay_entry *entries
4599 = (struct overlay_entry *) alloca (size * sizeof *entries);
4600
4601 if (charpos <= 0)
4602 charpos = IT_CHARPOS (*it);
4603
4604 /* Append the overlay string STRING of overlay OVERLAY to vector
4605 `entries' which has size `size' and currently contains `n'
4606 elements. AFTER_P non-zero means STRING is an after-string of
4607 OVERLAY. */
4608 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
4609 do \
4610 { \
4611 Lisp_Object priority; \
4612 \
4613 if (n == size) \
4614 { \
4615 int new_size = 2 * size; \
4616 struct overlay_entry *old = entries; \
4617 entries = \
4618 (struct overlay_entry *) alloca (new_size \
4619 * sizeof *entries); \
4620 bcopy (old, entries, size * sizeof *entries); \
4621 size = new_size; \
4622 } \
4623 \
4624 entries[n].string = (STRING); \
4625 entries[n].overlay = (OVERLAY); \
4626 priority = Foverlay_get ((OVERLAY), Qpriority); \
4627 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
4628 entries[n].after_string_p = (AFTER_P); \
4629 ++n; \
4630 } \
4631 while (0)
4632
4633 /* Process overlay before the overlay center. */
4634 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
4635 {
4636 XSETMISC (overlay, ov);
4637 xassert (OVERLAYP (overlay));
4638 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4639 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4640
4641 if (end < charpos)
4642 break;
4643
4644 /* Skip this overlay if it doesn't start or end at IT's current
4645 position. */
4646 if (end != charpos && start != charpos)
4647 continue;
4648
4649 /* Skip this overlay if it doesn't apply to IT->w. */
4650 window = Foverlay_get (overlay, Qwindow);
4651 if (WINDOWP (window) && XWINDOW (window) != it->w)
4652 continue;
4653
4654 /* If the text ``under'' the overlay is invisible, both before-
4655 and after-strings from this overlay are visible; start and
4656 end position are indistinguishable. */
4657 invisible = Foverlay_get (overlay, Qinvisible);
4658 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4659
4660 /* If overlay has a non-empty before-string, record it. */
4661 if ((start == charpos || (end == charpos && invis_p))
4662 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
4663 && SCHARS (str))
4664 RECORD_OVERLAY_STRING (overlay, str, 0);
4665
4666 /* If overlay has a non-empty after-string, record it. */
4667 if ((end == charpos || (start == charpos && invis_p))
4668 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
4669 && SCHARS (str))
4670 RECORD_OVERLAY_STRING (overlay, str, 1);
4671 }
4672
4673 /* Process overlays after the overlay center. */
4674 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
4675 {
4676 XSETMISC (overlay, ov);
4677 xassert (OVERLAYP (overlay));
4678 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4679 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4680
4681 if (start > charpos)
4682 break;
4683
4684 /* Skip this overlay if it doesn't start or end at IT's current
4685 position. */
4686 if (end != charpos && start != charpos)
4687 continue;
4688
4689 /* Skip this overlay if it doesn't apply to IT->w. */
4690 window = Foverlay_get (overlay, Qwindow);
4691 if (WINDOWP (window) && XWINDOW (window) != it->w)
4692 continue;
4693
4694 /* If the text ``under'' the overlay is invisible, it has a zero
4695 dimension, and both before- and after-strings apply. */
4696 invisible = Foverlay_get (overlay, Qinvisible);
4697 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4698
4699 /* If overlay has a non-empty before-string, record it. */
4700 if ((start == charpos || (end == charpos && invis_p))
4701 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
4702 && SCHARS (str))
4703 RECORD_OVERLAY_STRING (overlay, str, 0);
4704
4705 /* If overlay has a non-empty after-string, record it. */
4706 if ((end == charpos || (start == charpos && invis_p))
4707 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
4708 && SCHARS (str))
4709 RECORD_OVERLAY_STRING (overlay, str, 1);
4710 }
4711
4712 #undef RECORD_OVERLAY_STRING
4713
4714 /* Sort entries. */
4715 if (n > 1)
4716 qsort (entries, n, sizeof *entries, compare_overlay_entries);
4717
4718 /* Record the total number of strings to process. */
4719 it->n_overlay_strings = n;
4720
4721 /* IT->current.overlay_string_index is the number of overlay strings
4722 that have already been consumed by IT. Copy some of the
4723 remaining overlay strings to IT->overlay_strings. */
4724 i = 0;
4725 j = it->current.overlay_string_index;
4726 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
4727 it->overlay_strings[i++] = entries[j++].string;
4728
4729 CHECK_IT (it);
4730 }
4731
4732
4733 /* Get the first chunk of overlay strings at IT's current buffer
4734 position, or at CHARPOS if that is > 0. Value is non-zero if at
4735 least one overlay string was found. */
4736
4737 static int
4738 get_overlay_strings (it, charpos)
4739 struct it *it;
4740 int charpos;
4741 {
4742 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
4743 process. This fills IT->overlay_strings with strings, and sets
4744 IT->n_overlay_strings to the total number of strings to process.
4745 IT->pos.overlay_string_index has to be set temporarily to zero
4746 because load_overlay_strings needs this; it must be set to -1
4747 when no overlay strings are found because a zero value would
4748 indicate a position in the first overlay string. */
4749 it->current.overlay_string_index = 0;
4750 load_overlay_strings (it, charpos);
4751
4752 /* If we found overlay strings, set up IT to deliver display
4753 elements from the first one. Otherwise set up IT to deliver
4754 from current_buffer. */
4755 if (it->n_overlay_strings)
4756 {
4757 /* Make sure we know settings in current_buffer, so that we can
4758 restore meaningful values when we're done with the overlay
4759 strings. */
4760 compute_stop_pos (it);
4761 xassert (it->face_id >= 0);
4762
4763 /* Save IT's settings. They are restored after all overlay
4764 strings have been processed. */
4765 xassert (it->sp == 0);
4766 push_it (it);
4767
4768 /* Set up IT to deliver display elements from the first overlay
4769 string. */
4770 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
4771 it->string = it->overlay_strings[0];
4772 it->stop_charpos = 0;
4773 xassert (STRINGP (it->string));
4774 it->end_charpos = SCHARS (it->string);
4775 it->multibyte_p = STRING_MULTIBYTE (it->string);
4776 it->method = GET_FROM_STRING;
4777 }
4778 else
4779 {
4780 it->string = Qnil;
4781 it->current.overlay_string_index = -1;
4782 it->method = GET_FROM_BUFFER;
4783 }
4784
4785 CHECK_IT (it);
4786
4787 /* Value is non-zero if we found at least one overlay string. */
4788 return STRINGP (it->string);
4789 }
4790
4791
4792 \f
4793 /***********************************************************************
4794 Saving and restoring state
4795 ***********************************************************************/
4796
4797 /* Save current settings of IT on IT->stack. Called, for example,
4798 before setting up IT for an overlay string, to be able to restore
4799 IT's settings to what they were after the overlay string has been
4800 processed. */
4801
4802 static void
4803 push_it (it)
4804 struct it *it;
4805 {
4806 struct iterator_stack_entry *p;
4807
4808 xassert (it->sp < 2);
4809 p = it->stack + it->sp;
4810
4811 p->stop_charpos = it->stop_charpos;
4812 xassert (it->face_id >= 0);
4813 p->face_id = it->face_id;
4814 p->string = it->string;
4815 p->pos = it->current;
4816 p->end_charpos = it->end_charpos;
4817 p->string_nchars = it->string_nchars;
4818 p->area = it->area;
4819 p->multibyte_p = it->multibyte_p;
4820 p->slice = it->slice;
4821 p->space_width = it->space_width;
4822 p->font_height = it->font_height;
4823 p->voffset = it->voffset;
4824 p->string_from_display_prop_p = it->string_from_display_prop_p;
4825 p->display_ellipsis_p = 0;
4826 ++it->sp;
4827 }
4828
4829
4830 /* Restore IT's settings from IT->stack. Called, for example, when no
4831 more overlay strings must be processed, and we return to delivering
4832 display elements from a buffer, or when the end of a string from a
4833 `display' property is reached and we return to delivering display
4834 elements from an overlay string, or from a buffer. */
4835
4836 static void
4837 pop_it (it)
4838 struct it *it;
4839 {
4840 struct iterator_stack_entry *p;
4841
4842 xassert (it->sp > 0);
4843 --it->sp;
4844 p = it->stack + it->sp;
4845 it->stop_charpos = p->stop_charpos;
4846 it->face_id = p->face_id;
4847 it->string = p->string;
4848 it->current = p->pos;
4849 it->end_charpos = p->end_charpos;
4850 it->string_nchars = p->string_nchars;
4851 it->area = p->area;
4852 it->multibyte_p = p->multibyte_p;
4853 it->slice = p->slice;
4854 it->space_width = p->space_width;
4855 it->font_height = p->font_height;
4856 it->voffset = p->voffset;
4857 it->string_from_display_prop_p = p->string_from_display_prop_p;
4858 }
4859
4860
4861 \f
4862 /***********************************************************************
4863 Moving over lines
4864 ***********************************************************************/
4865
4866 /* Set IT's current position to the previous line start. */
4867
4868 static void
4869 back_to_previous_line_start (it)
4870 struct it *it;
4871 {
4872 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
4873 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
4874 }
4875
4876
4877 /* Move IT to the next line start.
4878
4879 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
4880 we skipped over part of the text (as opposed to moving the iterator
4881 continuously over the text). Otherwise, don't change the value
4882 of *SKIPPED_P.
4883
4884 Newlines may come from buffer text, overlay strings, or strings
4885 displayed via the `display' property. That's the reason we can't
4886 simply use find_next_newline_no_quit.
4887
4888 Note that this function may not skip over invisible text that is so
4889 because of text properties and immediately follows a newline. If
4890 it would, function reseat_at_next_visible_line_start, when called
4891 from set_iterator_to_next, would effectively make invisible
4892 characters following a newline part of the wrong glyph row, which
4893 leads to wrong cursor motion. */
4894
4895 static int
4896 forward_to_next_line_start (it, skipped_p)
4897 struct it *it;
4898 int *skipped_p;
4899 {
4900 int old_selective, newline_found_p, n;
4901 const int MAX_NEWLINE_DISTANCE = 500;
4902
4903 /* If already on a newline, just consume it to avoid unintended
4904 skipping over invisible text below. */
4905 if (it->what == IT_CHARACTER
4906 && it->c == '\n'
4907 && CHARPOS (it->position) == IT_CHARPOS (*it))
4908 {
4909 set_iterator_to_next (it, 0);
4910 it->c = 0;
4911 return 1;
4912 }
4913
4914 /* Don't handle selective display in the following. It's (a)
4915 unnecessary because it's done by the caller, and (b) leads to an
4916 infinite recursion because next_element_from_ellipsis indirectly
4917 calls this function. */
4918 old_selective = it->selective;
4919 it->selective = 0;
4920
4921 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
4922 from buffer text. */
4923 for (n = newline_found_p = 0;
4924 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
4925 n += STRINGP (it->string) ? 0 : 1)
4926 {
4927 if (!get_next_display_element (it))
4928 return 0;
4929 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
4930 set_iterator_to_next (it, 0);
4931 }
4932
4933 /* If we didn't find a newline near enough, see if we can use a
4934 short-cut. */
4935 if (!newline_found_p)
4936 {
4937 int start = IT_CHARPOS (*it);
4938 int limit = find_next_newline_no_quit (start, 1);
4939 Lisp_Object pos;
4940
4941 xassert (!STRINGP (it->string));
4942
4943 /* If there isn't any `display' property in sight, and no
4944 overlays, we can just use the position of the newline in
4945 buffer text. */
4946 if (it->stop_charpos >= limit
4947 || ((pos = Fnext_single_property_change (make_number (start),
4948 Qdisplay,
4949 Qnil, make_number (limit)),
4950 NILP (pos))
4951 && next_overlay_change (start) == ZV))
4952 {
4953 IT_CHARPOS (*it) = limit;
4954 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
4955 *skipped_p = newline_found_p = 1;
4956 }
4957 else
4958 {
4959 while (get_next_display_element (it)
4960 && !newline_found_p)
4961 {
4962 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
4963 set_iterator_to_next (it, 0);
4964 }
4965 }
4966 }
4967
4968 it->selective = old_selective;
4969 return newline_found_p;
4970 }
4971
4972
4973 /* Set IT's current position to the previous visible line start. Skip
4974 invisible text that is so either due to text properties or due to
4975 selective display. Caution: this does not change IT->current_x and
4976 IT->hpos. */
4977
4978 static void
4979 back_to_previous_visible_line_start (it)
4980 struct it *it;
4981 {
4982 while (IT_CHARPOS (*it) > BEGV)
4983 {
4984 back_to_previous_line_start (it);
4985 if (IT_CHARPOS (*it) <= BEGV)
4986 break;
4987
4988 /* If selective > 0, then lines indented more than that values
4989 are invisible. */
4990 if (it->selective > 0
4991 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
4992 (double) it->selective)) /* iftc */
4993 continue;
4994
4995 /* Check the newline before point for invisibility. */
4996 {
4997 Lisp_Object prop;
4998 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
4999 Qinvisible, it->window);
5000 if (TEXT_PROP_MEANS_INVISIBLE (prop))
5001 continue;
5002 }
5003
5004 /* If newline has a display property that replaces the newline with something
5005 else (image or text), find start of overlay or interval and continue search
5006 from that point. */
5007 if (IT_CHARPOS (*it) > BEGV)
5008 {
5009 struct it it2 = *it;
5010 int pos;
5011 int beg, end;
5012 Lisp_Object val, overlay;
5013
5014 pos = --IT_CHARPOS (it2);
5015 --IT_BYTEPOS (it2);
5016 it2.sp = 0;
5017 if (handle_display_prop (&it2) == HANDLED_RETURN
5018 && !NILP (val = get_char_property_and_overlay
5019 (make_number (pos), Qdisplay, Qnil, &overlay))
5020 && (OVERLAYP (overlay)
5021 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
5022 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
5023 {
5024 if (beg < BEGV)
5025 beg = BEGV;
5026 IT_CHARPOS (*it) = beg;
5027 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
5028 continue;
5029 }
5030 }
5031
5032 break;
5033 }
5034
5035 xassert (IT_CHARPOS (*it) >= BEGV);
5036 xassert (IT_CHARPOS (*it) == BEGV
5037 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
5038 CHECK_IT (it);
5039 }
5040
5041
5042 /* Reseat iterator IT at the previous visible line start. Skip
5043 invisible text that is so either due to text properties or due to
5044 selective display. At the end, update IT's overlay information,
5045 face information etc. */
5046
5047 void
5048 reseat_at_previous_visible_line_start (it)
5049 struct it *it;
5050 {
5051 back_to_previous_visible_line_start (it);
5052 reseat (it, it->current.pos, 1);
5053 CHECK_IT (it);
5054 }
5055
5056
5057 /* Reseat iterator IT on the next visible line start in the current
5058 buffer. ON_NEWLINE_P non-zero means position IT on the newline
5059 preceding the line start. Skip over invisible text that is so
5060 because of selective display. Compute faces, overlays etc at the
5061 new position. Note that this function does not skip over text that
5062 is invisible because of text properties. */
5063
5064 static void
5065 reseat_at_next_visible_line_start (it, on_newline_p)
5066 struct it *it;
5067 int on_newline_p;
5068 {
5069 int newline_found_p, skipped_p = 0;
5070
5071 newline_found_p = forward_to_next_line_start (it, &skipped_p);
5072
5073 /* Skip over lines that are invisible because they are indented
5074 more than the value of IT->selective. */
5075 if (it->selective > 0)
5076 while (IT_CHARPOS (*it) < ZV
5077 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
5078 (double) it->selective)) /* iftc */
5079 {
5080 xassert (IT_BYTEPOS (*it) == BEGV
5081 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
5082 newline_found_p = forward_to_next_line_start (it, &skipped_p);
5083 }
5084
5085 /* Position on the newline if that's what's requested. */
5086 if (on_newline_p && newline_found_p)
5087 {
5088 if (STRINGP (it->string))
5089 {
5090 if (IT_STRING_CHARPOS (*it) > 0)
5091 {
5092 --IT_STRING_CHARPOS (*it);
5093 --IT_STRING_BYTEPOS (*it);
5094 }
5095 }
5096 else if (IT_CHARPOS (*it) > BEGV)
5097 {
5098 --IT_CHARPOS (*it);
5099 --IT_BYTEPOS (*it);
5100 reseat (it, it->current.pos, 0);
5101 }
5102 }
5103 else if (skipped_p)
5104 reseat (it, it->current.pos, 0);
5105
5106 CHECK_IT (it);
5107 }
5108
5109
5110 \f
5111 /***********************************************************************
5112 Changing an iterator's position
5113 ***********************************************************************/
5114
5115 /* Change IT's current position to POS in current_buffer. If FORCE_P
5116 is non-zero, always check for text properties at the new position.
5117 Otherwise, text properties are only looked up if POS >=
5118 IT->check_charpos of a property. */
5119
5120 static void
5121 reseat (it, pos, force_p)
5122 struct it *it;
5123 struct text_pos pos;
5124 int force_p;
5125 {
5126 int original_pos = IT_CHARPOS (*it);
5127
5128 reseat_1 (it, pos, 0);
5129
5130 /* Determine where to check text properties. Avoid doing it
5131 where possible because text property lookup is very expensive. */
5132 if (force_p
5133 || CHARPOS (pos) > it->stop_charpos
5134 || CHARPOS (pos) < original_pos)
5135 handle_stop (it);
5136
5137 CHECK_IT (it);
5138 }
5139
5140
5141 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
5142 IT->stop_pos to POS, also. */
5143
5144 static void
5145 reseat_1 (it, pos, set_stop_p)
5146 struct it *it;
5147 struct text_pos pos;
5148 int set_stop_p;
5149 {
5150 /* Don't call this function when scanning a C string. */
5151 xassert (it->s == NULL);
5152
5153 /* POS must be a reasonable value. */
5154 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
5155
5156 it->current.pos = it->position = pos;
5157 XSETBUFFER (it->object, current_buffer);
5158 it->end_charpos = ZV;
5159 it->dpvec = NULL;
5160 it->current.dpvec_index = -1;
5161 it->current.overlay_string_index = -1;
5162 IT_STRING_CHARPOS (*it) = -1;
5163 IT_STRING_BYTEPOS (*it) = -1;
5164 it->string = Qnil;
5165 it->method = GET_FROM_BUFFER;
5166 /* RMS: I added this to fix a bug in move_it_vertically_backward
5167 where it->area continued to relate to the starting point
5168 for the backward motion. Bug report from
5169 Nick Roberts <nick@nick.uklinux.net> on 19 May 2003.
5170 However, I am not sure whether reseat still does the right thing
5171 in general after this change. */
5172 it->area = TEXT_AREA;
5173 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
5174 it->sp = 0;
5175 it->face_before_selective_p = 0;
5176
5177 if (set_stop_p)
5178 it->stop_charpos = CHARPOS (pos);
5179 }
5180
5181
5182 /* Set up IT for displaying a string, starting at CHARPOS in window W.
5183 If S is non-null, it is a C string to iterate over. Otherwise,
5184 STRING gives a Lisp string to iterate over.
5185
5186 If PRECISION > 0, don't return more then PRECISION number of
5187 characters from the string.
5188
5189 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
5190 characters have been returned. FIELD_WIDTH < 0 means an infinite
5191 field width.
5192
5193 MULTIBYTE = 0 means disable processing of multibyte characters,
5194 MULTIBYTE > 0 means enable it,
5195 MULTIBYTE < 0 means use IT->multibyte_p.
5196
5197 IT must be initialized via a prior call to init_iterator before
5198 calling this function. */
5199
5200 static void
5201 reseat_to_string (it, s, string, charpos, precision, field_width, multibyte)
5202 struct it *it;
5203 unsigned char *s;
5204 Lisp_Object string;
5205 int charpos;
5206 int precision, field_width, multibyte;
5207 {
5208 /* No region in strings. */
5209 it->region_beg_charpos = it->region_end_charpos = -1;
5210
5211 /* No text property checks performed by default, but see below. */
5212 it->stop_charpos = -1;
5213
5214 /* Set iterator position and end position. */
5215 bzero (&it->current, sizeof it->current);
5216 it->current.overlay_string_index = -1;
5217 it->current.dpvec_index = -1;
5218 xassert (charpos >= 0);
5219
5220 /* If STRING is specified, use its multibyteness, otherwise use the
5221 setting of MULTIBYTE, if specified. */
5222 if (multibyte >= 0)
5223 it->multibyte_p = multibyte > 0;
5224
5225 if (s == NULL)
5226 {
5227 xassert (STRINGP (string));
5228 it->string = string;
5229 it->s = NULL;
5230 it->end_charpos = it->string_nchars = SCHARS (string);
5231 it->method = GET_FROM_STRING;
5232 it->current.string_pos = string_pos (charpos, string);
5233 }
5234 else
5235 {
5236 it->s = s;
5237 it->string = Qnil;
5238
5239 /* Note that we use IT->current.pos, not it->current.string_pos,
5240 for displaying C strings. */
5241 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
5242 if (it->multibyte_p)
5243 {
5244 it->current.pos = c_string_pos (charpos, s, 1);
5245 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
5246 }
5247 else
5248 {
5249 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
5250 it->end_charpos = it->string_nchars = strlen (s);
5251 }
5252
5253 it->method = GET_FROM_C_STRING;
5254 }
5255
5256 /* PRECISION > 0 means don't return more than PRECISION characters
5257 from the string. */
5258 if (precision > 0 && it->end_charpos - charpos > precision)
5259 it->end_charpos = it->string_nchars = charpos + precision;
5260
5261 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
5262 characters have been returned. FIELD_WIDTH == 0 means don't pad,
5263 FIELD_WIDTH < 0 means infinite field width. This is useful for
5264 padding with `-' at the end of a mode line. */
5265 if (field_width < 0)
5266 field_width = INFINITY;
5267 if (field_width > it->end_charpos - charpos)
5268 it->end_charpos = charpos + field_width;
5269
5270 /* Use the standard display table for displaying strings. */
5271 if (DISP_TABLE_P (Vstandard_display_table))
5272 it->dp = XCHAR_TABLE (Vstandard_display_table);
5273
5274 it->stop_charpos = charpos;
5275 CHECK_IT (it);
5276 }
5277
5278
5279 \f
5280 /***********************************************************************
5281 Iteration
5282 ***********************************************************************/
5283
5284 /* Map enum it_method value to corresponding next_element_from_* function. */
5285
5286 static int (* get_next_element[NUM_IT_METHODS]) P_ ((struct it *it)) =
5287 {
5288 next_element_from_buffer,
5289 next_element_from_display_vector,
5290 next_element_from_composition,
5291 next_element_from_string,
5292 next_element_from_c_string,
5293 next_element_from_image,
5294 next_element_from_stretch
5295 };
5296
5297
5298 /* Load IT's display element fields with information about the next
5299 display element from the current position of IT. Value is zero if
5300 end of buffer (or C string) is reached. */
5301
5302 int
5303 get_next_display_element (it)
5304 struct it *it;
5305 {
5306 /* Non-zero means that we found a display element. Zero means that
5307 we hit the end of what we iterate over. Performance note: the
5308 function pointer `method' used here turns out to be faster than
5309 using a sequence of if-statements. */
5310 int success_p;
5311
5312 get_next:
5313 success_p = (*get_next_element[it->method]) (it);
5314
5315 if (it->what == IT_CHARACTER)
5316 {
5317 /* Map via display table or translate control characters.
5318 IT->c, IT->len etc. have been set to the next character by
5319 the function call above. If we have a display table, and it
5320 contains an entry for IT->c, translate it. Don't do this if
5321 IT->c itself comes from a display table, otherwise we could
5322 end up in an infinite recursion. (An alternative could be to
5323 count the recursion depth of this function and signal an
5324 error when a certain maximum depth is reached.) Is it worth
5325 it? */
5326 if (success_p && it->dpvec == NULL)
5327 {
5328 Lisp_Object dv;
5329
5330 if (it->dp
5331 && (dv = DISP_CHAR_VECTOR (it->dp, it->c),
5332 VECTORP (dv)))
5333 {
5334 struct Lisp_Vector *v = XVECTOR (dv);
5335
5336 /* Return the first character from the display table
5337 entry, if not empty. If empty, don't display the
5338 current character. */
5339 if (v->size)
5340 {
5341 it->dpvec_char_len = it->len;
5342 it->dpvec = v->contents;
5343 it->dpend = v->contents + v->size;
5344 it->current.dpvec_index = 0;
5345 it->dpvec_face_id = -1;
5346 it->saved_face_id = it->face_id;
5347 it->method = GET_FROM_DISPLAY_VECTOR;
5348 it->ellipsis_p = 0;
5349 }
5350 else
5351 {
5352 set_iterator_to_next (it, 0);
5353 }
5354 goto get_next;
5355 }
5356
5357 /* Translate control characters into `\003' or `^C' form.
5358 Control characters coming from a display table entry are
5359 currently not translated because we use IT->dpvec to hold
5360 the translation. This could easily be changed but I
5361 don't believe that it is worth doing.
5362
5363 If it->multibyte_p is nonzero, eight-bit characters and
5364 non-printable multibyte characters are also translated to
5365 octal form.
5366
5367 If it->multibyte_p is zero, eight-bit characters that
5368 don't have corresponding multibyte char code are also
5369 translated to octal form. */
5370 else if ((it->c < ' '
5371 && (it->area != TEXT_AREA
5372 /* In mode line, treat \n like other crl chars. */
5373 || (it->c != '\t'
5374 && it->glyph_row && it->glyph_row->mode_line_p)
5375 || (it->c != '\n' && it->c != '\t')))
5376 || (it->multibyte_p
5377 ? ((it->c >= 127
5378 && it->len == 1)
5379 || !CHAR_PRINTABLE_P (it->c)
5380 || (!NILP (Vnobreak_char_display)
5381 && (it->c == 0x8a0 || it->c == 0x8ad
5382 || it->c == 0x920 || it->c == 0x92d
5383 || it->c == 0xe20 || it->c == 0xe2d
5384 || it->c == 0xf20 || it->c == 0xf2d)))
5385 : (it->c >= 127
5386 && (!unibyte_display_via_language_environment
5387 || it->c == unibyte_char_to_multibyte (it->c)))))
5388 {
5389 /* IT->c is a control character which must be displayed
5390 either as '\003' or as `^C' where the '\\' and '^'
5391 can be defined in the display table. Fill
5392 IT->ctl_chars with glyphs for what we have to
5393 display. Then, set IT->dpvec to these glyphs. */
5394 GLYPH g;
5395 int ctl_len;
5396 int face_id, lface_id = 0 ;
5397 GLYPH escape_glyph;
5398
5399 /* Handle control characters with ^. */
5400
5401 if (it->c < 128 && it->ctl_arrow_p)
5402 {
5403 g = '^'; /* default glyph for Control */
5404 /* Set IT->ctl_chars[0] to the glyph for `^'. */
5405 if (it->dp
5406 && INTEGERP (DISP_CTRL_GLYPH (it->dp))
5407 && GLYPH_CHAR_VALID_P (XINT (DISP_CTRL_GLYPH (it->dp))))
5408 {
5409 g = XINT (DISP_CTRL_GLYPH (it->dp));
5410 lface_id = FAST_GLYPH_FACE (g);
5411 }
5412 if (lface_id)
5413 {
5414 g = FAST_GLYPH_CHAR (g);
5415 face_id = merge_faces (it->f, Qt, lface_id,
5416 it->face_id);
5417 }
5418 else
5419 {
5420 /* Merge the escape-glyph face into the current face. */
5421 face_id = merge_faces (it->f, Qescape_glyph, 0,
5422 it->face_id);
5423 }
5424
5425 XSETINT (it->ctl_chars[0], g);
5426 g = it->c ^ 0100;
5427 XSETINT (it->ctl_chars[1], g);
5428 ctl_len = 2;
5429 goto display_control;
5430 }
5431
5432 /* Handle non-break space in the mode where it only gets
5433 highlighting. */
5434
5435 if (EQ (Vnobreak_char_display, Qt)
5436 && (it->c == 0x8a0 || it->c == 0x920
5437 || it->c == 0xe20 || it->c == 0xf20))
5438 {
5439 /* Merge the no-break-space face into the current face. */
5440 face_id = merge_faces (it->f, Qnobreak_space, 0,
5441 it->face_id);
5442
5443 g = it->c = ' ';
5444 XSETINT (it->ctl_chars[0], g);
5445 ctl_len = 1;
5446 goto display_control;
5447 }
5448
5449 /* Handle sequences that start with the "escape glyph". */
5450
5451 /* the default escape glyph is \. */
5452 escape_glyph = '\\';
5453
5454 if (it->dp
5455 && INTEGERP (DISP_ESCAPE_GLYPH (it->dp))
5456 && GLYPH_CHAR_VALID_P (XFASTINT (DISP_ESCAPE_GLYPH (it->dp))))
5457 {
5458 escape_glyph = XFASTINT (DISP_ESCAPE_GLYPH (it->dp));
5459 lface_id = FAST_GLYPH_FACE (escape_glyph);
5460 }
5461 if (lface_id)
5462 {
5463 /* The display table specified a face.
5464 Merge it into face_id and also into escape_glyph. */
5465 escape_glyph = FAST_GLYPH_CHAR (escape_glyph);
5466 face_id = merge_faces (it->f, Qt, lface_id,
5467 it->face_id);
5468 }
5469 else
5470 {
5471 /* Merge the escape-glyph face into the current face. */
5472 face_id = merge_faces (it->f, Qescape_glyph, 0,
5473 it->face_id);
5474 }
5475
5476 /* Handle soft hyphens in the mode where they only get
5477 highlighting. */
5478
5479 if (EQ (Vnobreak_char_display, Qt)
5480 && (it->c == 0x8ad || it->c == 0x92d
5481 || it->c == 0xe2d || it->c == 0xf2d))
5482 {
5483 g = it->c = '-';
5484 XSETINT (it->ctl_chars[0], g);
5485 ctl_len = 1;
5486 goto display_control;
5487 }
5488
5489 /* Handle non-break space and soft hyphen
5490 with the escape glyph. */
5491
5492 if (it->c == 0x8a0 || it->c == 0x8ad
5493 || it->c == 0x920 || it->c == 0x92d
5494 || it->c == 0xe20 || it->c == 0xe2d
5495 || it->c == 0xf20 || it->c == 0xf2d)
5496 {
5497 XSETINT (it->ctl_chars[0], escape_glyph);
5498 g = it->c = ((it->c & 0xf) == 0 ? ' ' : '-');
5499 XSETINT (it->ctl_chars[1], g);
5500 ctl_len = 2;
5501 goto display_control;
5502 }
5503
5504 {
5505 unsigned char str[MAX_MULTIBYTE_LENGTH];
5506 int len;
5507 int i;
5508
5509 /* Set IT->ctl_chars[0] to the glyph for `\\'. */
5510 if (SINGLE_BYTE_CHAR_P (it->c))
5511 str[0] = it->c, len = 1;
5512 else
5513 {
5514 len = CHAR_STRING_NO_SIGNAL (it->c, str);
5515 if (len < 0)
5516 {
5517 /* It's an invalid character, which shouldn't
5518 happen actually, but due to bugs it may
5519 happen. Let's print the char as is, there's
5520 not much meaningful we can do with it. */
5521 str[0] = it->c;
5522 str[1] = it->c >> 8;
5523 str[2] = it->c >> 16;
5524 str[3] = it->c >> 24;
5525 len = 4;
5526 }
5527 }
5528
5529 for (i = 0; i < len; i++)
5530 {
5531 XSETINT (it->ctl_chars[i * 4], escape_glyph);
5532 /* Insert three more glyphs into IT->ctl_chars for
5533 the octal display of the character. */
5534 g = ((str[i] >> 6) & 7) + '0';
5535 XSETINT (it->ctl_chars[i * 4 + 1], g);
5536 g = ((str[i] >> 3) & 7) + '0';
5537 XSETINT (it->ctl_chars[i * 4 + 2], g);
5538 g = (str[i] & 7) + '0';
5539 XSETINT (it->ctl_chars[i * 4 + 3], g);
5540 }
5541 ctl_len = len * 4;
5542 }
5543
5544 display_control:
5545 /* Set up IT->dpvec and return first character from it. */
5546 it->dpvec_char_len = it->len;
5547 it->dpvec = it->ctl_chars;
5548 it->dpend = it->dpvec + ctl_len;
5549 it->current.dpvec_index = 0;
5550 it->dpvec_face_id = face_id;
5551 it->saved_face_id = it->face_id;
5552 it->method = GET_FROM_DISPLAY_VECTOR;
5553 it->ellipsis_p = 0;
5554 goto get_next;
5555 }
5556 }
5557
5558 /* Adjust face id for a multibyte character. There are no
5559 multibyte character in unibyte text. */
5560 if (it->multibyte_p
5561 && success_p
5562 && FRAME_WINDOW_P (it->f))
5563 {
5564 struct face *face = FACE_FROM_ID (it->f, it->face_id);
5565 it->face_id = FACE_FOR_CHAR (it->f, face, it->c);
5566 }
5567 }
5568
5569 /* Is this character the last one of a run of characters with
5570 box? If yes, set IT->end_of_box_run_p to 1. */
5571 if (it->face_box_p
5572 && it->s == NULL)
5573 {
5574 int face_id;
5575 struct face *face;
5576
5577 it->end_of_box_run_p
5578 = ((face_id = face_after_it_pos (it),
5579 face_id != it->face_id)
5580 && (face = FACE_FROM_ID (it->f, face_id),
5581 face->box == FACE_NO_BOX));
5582 }
5583
5584 /* Value is 0 if end of buffer or string reached. */
5585 return success_p;
5586 }
5587
5588
5589 /* Move IT to the next display element.
5590
5591 RESEAT_P non-zero means if called on a newline in buffer text,
5592 skip to the next visible line start.
5593
5594 Functions get_next_display_element and set_iterator_to_next are
5595 separate because I find this arrangement easier to handle than a
5596 get_next_display_element function that also increments IT's
5597 position. The way it is we can first look at an iterator's current
5598 display element, decide whether it fits on a line, and if it does,
5599 increment the iterator position. The other way around we probably
5600 would either need a flag indicating whether the iterator has to be
5601 incremented the next time, or we would have to implement a
5602 decrement position function which would not be easy to write. */
5603
5604 void
5605 set_iterator_to_next (it, reseat_p)
5606 struct it *it;
5607 int reseat_p;
5608 {
5609 /* Reset flags indicating start and end of a sequence of characters
5610 with box. Reset them at the start of this function because
5611 moving the iterator to a new position might set them. */
5612 it->start_of_box_run_p = it->end_of_box_run_p = 0;
5613
5614 switch (it->method)
5615 {
5616 case GET_FROM_BUFFER:
5617 /* The current display element of IT is a character from
5618 current_buffer. Advance in the buffer, and maybe skip over
5619 invisible lines that are so because of selective display. */
5620 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
5621 reseat_at_next_visible_line_start (it, 0);
5622 else
5623 {
5624 xassert (it->len != 0);
5625 IT_BYTEPOS (*it) += it->len;
5626 IT_CHARPOS (*it) += 1;
5627 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
5628 }
5629 break;
5630
5631 case GET_FROM_COMPOSITION:
5632 xassert (it->cmp_id >= 0 && it->cmp_id < n_compositions);
5633 if (STRINGP (it->string))
5634 {
5635 IT_STRING_BYTEPOS (*it) += it->len;
5636 IT_STRING_CHARPOS (*it) += it->cmp_len;
5637 it->method = GET_FROM_STRING;
5638 goto consider_string_end;
5639 }
5640 else
5641 {
5642 IT_BYTEPOS (*it) += it->len;
5643 IT_CHARPOS (*it) += it->cmp_len;
5644 it->method = GET_FROM_BUFFER;
5645 }
5646 break;
5647
5648 case GET_FROM_C_STRING:
5649 /* Current display element of IT is from a C string. */
5650 IT_BYTEPOS (*it) += it->len;
5651 IT_CHARPOS (*it) += 1;
5652 break;
5653
5654 case GET_FROM_DISPLAY_VECTOR:
5655 /* Current display element of IT is from a display table entry.
5656 Advance in the display table definition. Reset it to null if
5657 end reached, and continue with characters from buffers/
5658 strings. */
5659 ++it->current.dpvec_index;
5660
5661 /* Restore face of the iterator to what they were before the
5662 display vector entry (these entries may contain faces). */
5663 it->face_id = it->saved_face_id;
5664
5665 if (it->dpvec + it->current.dpvec_index == it->dpend)
5666 {
5667 if (it->s)
5668 it->method = GET_FROM_C_STRING;
5669 else if (STRINGP (it->string))
5670 it->method = GET_FROM_STRING;
5671 else
5672 it->method = GET_FROM_BUFFER;
5673
5674 it->dpvec = NULL;
5675 it->current.dpvec_index = -1;
5676
5677 /* Skip over characters which were displayed via IT->dpvec. */
5678 if (it->dpvec_char_len < 0)
5679 reseat_at_next_visible_line_start (it, 1);
5680 else if (it->dpvec_char_len > 0)
5681 {
5682 if (it->method == GET_FROM_STRING
5683 && it->n_overlay_strings > 0)
5684 it->ignore_overlay_strings_at_pos_p = 1;
5685 it->len = it->dpvec_char_len;
5686 set_iterator_to_next (it, reseat_p);
5687 }
5688
5689 /* Recheck faces after display vector */
5690 it->stop_charpos = IT_CHARPOS (*it);
5691 }
5692 break;
5693
5694 case GET_FROM_STRING:
5695 /* Current display element is a character from a Lisp string. */
5696 xassert (it->s == NULL && STRINGP (it->string));
5697 IT_STRING_BYTEPOS (*it) += it->len;
5698 IT_STRING_CHARPOS (*it) += 1;
5699
5700 consider_string_end:
5701
5702 if (it->current.overlay_string_index >= 0)
5703 {
5704 /* IT->string is an overlay string. Advance to the
5705 next, if there is one. */
5706 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
5707 next_overlay_string (it);
5708 }
5709 else
5710 {
5711 /* IT->string is not an overlay string. If we reached
5712 its end, and there is something on IT->stack, proceed
5713 with what is on the stack. This can be either another
5714 string, this time an overlay string, or a buffer. */
5715 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
5716 && it->sp > 0)
5717 {
5718 pop_it (it);
5719 if (STRINGP (it->string))
5720 goto consider_string_end;
5721 it->method = GET_FROM_BUFFER;
5722 }
5723 }
5724 break;
5725
5726 case GET_FROM_IMAGE:
5727 case GET_FROM_STRETCH:
5728 /* The position etc with which we have to proceed are on
5729 the stack. The position may be at the end of a string,
5730 if the `display' property takes up the whole string. */
5731 xassert (it->sp > 0);
5732 pop_it (it);
5733 it->image_id = 0;
5734 if (STRINGP (it->string))
5735 {
5736 it->method = GET_FROM_STRING;
5737 goto consider_string_end;
5738 }
5739 it->method = GET_FROM_BUFFER;
5740 break;
5741
5742 default:
5743 /* There are no other methods defined, so this should be a bug. */
5744 abort ();
5745 }
5746
5747 xassert (it->method != GET_FROM_STRING
5748 || (STRINGP (it->string)
5749 && IT_STRING_CHARPOS (*it) >= 0));
5750 }
5751
5752 /* Load IT's display element fields with information about the next
5753 display element which comes from a display table entry or from the
5754 result of translating a control character to one of the forms `^C'
5755 or `\003'.
5756
5757 IT->dpvec holds the glyphs to return as characters.
5758 IT->saved_face_id holds the face id before the display vector--
5759 it is restored into IT->face_idin set_iterator_to_next. */
5760
5761 static int
5762 next_element_from_display_vector (it)
5763 struct it *it;
5764 {
5765 /* Precondition. */
5766 xassert (it->dpvec && it->current.dpvec_index >= 0);
5767
5768 it->face_id = it->saved_face_id;
5769
5770 if (INTEGERP (*it->dpvec)
5771 && GLYPH_CHAR_VALID_P (XFASTINT (*it->dpvec)))
5772 {
5773 GLYPH g;
5774
5775 g = XFASTINT (it->dpvec[it->current.dpvec_index]);
5776 it->c = FAST_GLYPH_CHAR (g);
5777 it->len = CHAR_BYTES (it->c);
5778
5779 /* The entry may contain a face id to use. Such a face id is
5780 the id of a Lisp face, not a realized face. A face id of
5781 zero means no face is specified. */
5782 if (it->dpvec_face_id >= 0)
5783 it->face_id = it->dpvec_face_id;
5784 else
5785 {
5786 int lface_id = FAST_GLYPH_FACE (g);
5787 if (lface_id > 0)
5788 it->face_id = merge_faces (it->f, Qt, lface_id,
5789 it->saved_face_id);
5790 }
5791 }
5792 else
5793 /* Display table entry is invalid. Return a space. */
5794 it->c = ' ', it->len = 1;
5795
5796 /* Don't change position and object of the iterator here. They are
5797 still the values of the character that had this display table
5798 entry or was translated, and that's what we want. */
5799 it->what = IT_CHARACTER;
5800 return 1;
5801 }
5802
5803
5804 /* Load IT with the next display element from Lisp string IT->string.
5805 IT->current.string_pos is the current position within the string.
5806 If IT->current.overlay_string_index >= 0, the Lisp string is an
5807 overlay string. */
5808
5809 static int
5810 next_element_from_string (it)
5811 struct it *it;
5812 {
5813 struct text_pos position;
5814
5815 xassert (STRINGP (it->string));
5816 xassert (IT_STRING_CHARPOS (*it) >= 0);
5817 position = it->current.string_pos;
5818
5819 /* Time to check for invisible text? */
5820 if (IT_STRING_CHARPOS (*it) < it->end_charpos
5821 && IT_STRING_CHARPOS (*it) == it->stop_charpos)
5822 {
5823 handle_stop (it);
5824
5825 /* Since a handler may have changed IT->method, we must
5826 recurse here. */
5827 return get_next_display_element (it);
5828 }
5829
5830 if (it->current.overlay_string_index >= 0)
5831 {
5832 /* Get the next character from an overlay string. In overlay
5833 strings, There is no field width or padding with spaces to
5834 do. */
5835 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
5836 {
5837 it->what = IT_EOB;
5838 return 0;
5839 }
5840 else if (STRING_MULTIBYTE (it->string))
5841 {
5842 int remaining = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
5843 const unsigned char *s = (SDATA (it->string)
5844 + IT_STRING_BYTEPOS (*it));
5845 it->c = string_char_and_length (s, remaining, &it->len);
5846 }
5847 else
5848 {
5849 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
5850 it->len = 1;
5851 }
5852 }
5853 else
5854 {
5855 /* Get the next character from a Lisp string that is not an
5856 overlay string. Such strings come from the mode line, for
5857 example. We may have to pad with spaces, or truncate the
5858 string. See also next_element_from_c_string. */
5859 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
5860 {
5861 it->what = IT_EOB;
5862 return 0;
5863 }
5864 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
5865 {
5866 /* Pad with spaces. */
5867 it->c = ' ', it->len = 1;
5868 CHARPOS (position) = BYTEPOS (position) = -1;
5869 }
5870 else if (STRING_MULTIBYTE (it->string))
5871 {
5872 int maxlen = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
5873 const unsigned char *s = (SDATA (it->string)
5874 + IT_STRING_BYTEPOS (*it));
5875 it->c = string_char_and_length (s, maxlen, &it->len);
5876 }
5877 else
5878 {
5879 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
5880 it->len = 1;
5881 }
5882 }
5883
5884 /* Record what we have and where it came from. Note that we store a
5885 buffer position in IT->position although it could arguably be a
5886 string position. */
5887 it->what = IT_CHARACTER;
5888 it->object = it->string;
5889 it->position = position;
5890 return 1;
5891 }
5892
5893
5894 /* Load IT with next display element from C string IT->s.
5895 IT->string_nchars is the maximum number of characters to return
5896 from the string. IT->end_charpos may be greater than
5897 IT->string_nchars when this function is called, in which case we
5898 may have to return padding spaces. Value is zero if end of string
5899 reached, including padding spaces. */
5900
5901 static int
5902 next_element_from_c_string (it)
5903 struct it *it;
5904 {
5905 int success_p = 1;
5906
5907 xassert (it->s);
5908 it->what = IT_CHARACTER;
5909 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
5910 it->object = Qnil;
5911
5912 /* IT's position can be greater IT->string_nchars in case a field
5913 width or precision has been specified when the iterator was
5914 initialized. */
5915 if (IT_CHARPOS (*it) >= it->end_charpos)
5916 {
5917 /* End of the game. */
5918 it->what = IT_EOB;
5919 success_p = 0;
5920 }
5921 else if (IT_CHARPOS (*it) >= it->string_nchars)
5922 {
5923 /* Pad with spaces. */
5924 it->c = ' ', it->len = 1;
5925 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
5926 }
5927 else if (it->multibyte_p)
5928 {
5929 /* Implementation note: The calls to strlen apparently aren't a
5930 performance problem because there is no noticeable performance
5931 difference between Emacs running in unibyte or multibyte mode. */
5932 int maxlen = strlen (it->s) - IT_BYTEPOS (*it);
5933 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it),
5934 maxlen, &it->len);
5935 }
5936 else
5937 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
5938
5939 return success_p;
5940 }
5941
5942
5943 /* Set up IT to return characters from an ellipsis, if appropriate.
5944 The definition of the ellipsis glyphs may come from a display table
5945 entry. This function Fills IT with the first glyph from the
5946 ellipsis if an ellipsis is to be displayed. */
5947
5948 static int
5949 next_element_from_ellipsis (it)
5950 struct it *it;
5951 {
5952 if (it->selective_display_ellipsis_p)
5953 setup_for_ellipsis (it, it->len);
5954 else
5955 {
5956 /* The face at the current position may be different from the
5957 face we find after the invisible text. Remember what it
5958 was in IT->saved_face_id, and signal that it's there by
5959 setting face_before_selective_p. */
5960 it->saved_face_id = it->face_id;
5961 it->method = GET_FROM_BUFFER;
5962 reseat_at_next_visible_line_start (it, 1);
5963 it->face_before_selective_p = 1;
5964 }
5965
5966 return get_next_display_element (it);
5967 }
5968
5969
5970 /* Deliver an image display element. The iterator IT is already
5971 filled with image information (done in handle_display_prop). Value
5972 is always 1. */
5973
5974
5975 static int
5976 next_element_from_image (it)
5977 struct it *it;
5978 {
5979 it->what = IT_IMAGE;
5980 return 1;
5981 }
5982
5983
5984 /* Fill iterator IT with next display element from a stretch glyph
5985 property. IT->object is the value of the text property. Value is
5986 always 1. */
5987
5988 static int
5989 next_element_from_stretch (it)
5990 struct it *it;
5991 {
5992 it->what = IT_STRETCH;
5993 return 1;
5994 }
5995
5996
5997 /* Load IT with the next display element from current_buffer. Value
5998 is zero if end of buffer reached. IT->stop_charpos is the next
5999 position at which to stop and check for text properties or buffer
6000 end. */
6001
6002 static int
6003 next_element_from_buffer (it)
6004 struct it *it;
6005 {
6006 int success_p = 1;
6007
6008 /* Check this assumption, otherwise, we would never enter the
6009 if-statement, below. */
6010 xassert (IT_CHARPOS (*it) >= BEGV
6011 && IT_CHARPOS (*it) <= it->stop_charpos);
6012
6013 if (IT_CHARPOS (*it) >= it->stop_charpos)
6014 {
6015 if (IT_CHARPOS (*it) >= it->end_charpos)
6016 {
6017 int overlay_strings_follow_p;
6018
6019 /* End of the game, except when overlay strings follow that
6020 haven't been returned yet. */
6021 if (it->overlay_strings_at_end_processed_p)
6022 overlay_strings_follow_p = 0;
6023 else
6024 {
6025 it->overlay_strings_at_end_processed_p = 1;
6026 overlay_strings_follow_p = get_overlay_strings (it, 0);
6027 }
6028
6029 if (overlay_strings_follow_p)
6030 success_p = get_next_display_element (it);
6031 else
6032 {
6033 it->what = IT_EOB;
6034 it->position = it->current.pos;
6035 success_p = 0;
6036 }
6037 }
6038 else
6039 {
6040 handle_stop (it);
6041 return get_next_display_element (it);
6042 }
6043 }
6044 else
6045 {
6046 /* No face changes, overlays etc. in sight, so just return a
6047 character from current_buffer. */
6048 unsigned char *p;
6049
6050 /* Maybe run the redisplay end trigger hook. Performance note:
6051 This doesn't seem to cost measurable time. */
6052 if (it->redisplay_end_trigger_charpos
6053 && it->glyph_row
6054 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
6055 run_redisplay_end_trigger_hook (it);
6056
6057 /* Get the next character, maybe multibyte. */
6058 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
6059 if (it->multibyte_p && !ASCII_BYTE_P (*p))
6060 {
6061 int maxlen = ((IT_BYTEPOS (*it) >= GPT_BYTE ? ZV_BYTE : GPT_BYTE)
6062 - IT_BYTEPOS (*it));
6063 it->c = string_char_and_length (p, maxlen, &it->len);
6064 }
6065 else
6066 it->c = *p, it->len = 1;
6067
6068 /* Record what we have and where it came from. */
6069 it->what = IT_CHARACTER;;
6070 it->object = it->w->buffer;
6071 it->position = it->current.pos;
6072
6073 /* Normally we return the character found above, except when we
6074 really want to return an ellipsis for selective display. */
6075 if (it->selective)
6076 {
6077 if (it->c == '\n')
6078 {
6079 /* A value of selective > 0 means hide lines indented more
6080 than that number of columns. */
6081 if (it->selective > 0
6082 && IT_CHARPOS (*it) + 1 < ZV
6083 && indented_beyond_p (IT_CHARPOS (*it) + 1,
6084 IT_BYTEPOS (*it) + 1,
6085 (double) it->selective)) /* iftc */
6086 {
6087 success_p = next_element_from_ellipsis (it);
6088 it->dpvec_char_len = -1;
6089 }
6090 }
6091 else if (it->c == '\r' && it->selective == -1)
6092 {
6093 /* A value of selective == -1 means that everything from the
6094 CR to the end of the line is invisible, with maybe an
6095 ellipsis displayed for it. */
6096 success_p = next_element_from_ellipsis (it);
6097 it->dpvec_char_len = -1;
6098 }
6099 }
6100 }
6101
6102 /* Value is zero if end of buffer reached. */
6103 xassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
6104 return success_p;
6105 }
6106
6107
6108 /* Run the redisplay end trigger hook for IT. */
6109
6110 static void
6111 run_redisplay_end_trigger_hook (it)
6112 struct it *it;
6113 {
6114 Lisp_Object args[3];
6115
6116 /* IT->glyph_row should be non-null, i.e. we should be actually
6117 displaying something, or otherwise we should not run the hook. */
6118 xassert (it->glyph_row);
6119
6120 /* Set up hook arguments. */
6121 args[0] = Qredisplay_end_trigger_functions;
6122 args[1] = it->window;
6123 XSETINT (args[2], it->redisplay_end_trigger_charpos);
6124 it->redisplay_end_trigger_charpos = 0;
6125
6126 /* Since we are *trying* to run these functions, don't try to run
6127 them again, even if they get an error. */
6128 it->w->redisplay_end_trigger = Qnil;
6129 Frun_hook_with_args (3, args);
6130
6131 /* Notice if it changed the face of the character we are on. */
6132 handle_face_prop (it);
6133 }
6134
6135
6136 /* Deliver a composition display element. The iterator IT is already
6137 filled with composition information (done in
6138 handle_composition_prop). Value is always 1. */
6139
6140 static int
6141 next_element_from_composition (it)
6142 struct it *it;
6143 {
6144 it->what = IT_COMPOSITION;
6145 it->position = (STRINGP (it->string)
6146 ? it->current.string_pos
6147 : it->current.pos);
6148 return 1;
6149 }
6150
6151
6152 \f
6153 /***********************************************************************
6154 Moving an iterator without producing glyphs
6155 ***********************************************************************/
6156
6157 /* Check if iterator is at a position corresponding to a valid buffer
6158 position after some move_it_ call. */
6159
6160 #define IT_POS_VALID_AFTER_MOVE_P(it) \
6161 ((it)->method == GET_FROM_STRING \
6162 ? IT_STRING_CHARPOS (*it) == 0 \
6163 : 1)
6164
6165
6166 /* Move iterator IT to a specified buffer or X position within one
6167 line on the display without producing glyphs.
6168
6169 OP should be a bit mask including some or all of these bits:
6170 MOVE_TO_X: Stop on reaching x-position TO_X.
6171 MOVE_TO_POS: Stop on reaching buffer or string position TO_CHARPOS.
6172 Regardless of OP's value, stop in reaching the end of the display line.
6173
6174 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
6175 This means, in particular, that TO_X includes window's horizontal
6176 scroll amount.
6177
6178 The return value has several possible values that
6179 say what condition caused the scan to stop:
6180
6181 MOVE_POS_MATCH_OR_ZV
6182 - when TO_POS or ZV was reached.
6183
6184 MOVE_X_REACHED
6185 -when TO_X was reached before TO_POS or ZV were reached.
6186
6187 MOVE_LINE_CONTINUED
6188 - when we reached the end of the display area and the line must
6189 be continued.
6190
6191 MOVE_LINE_TRUNCATED
6192 - when we reached the end of the display area and the line is
6193 truncated.
6194
6195 MOVE_NEWLINE_OR_CR
6196 - when we stopped at a line end, i.e. a newline or a CR and selective
6197 display is on. */
6198
6199 static enum move_it_result
6200 move_it_in_display_line_to (it, to_charpos, to_x, op)
6201 struct it *it;
6202 int to_charpos, to_x, op;
6203 {
6204 enum move_it_result result = MOVE_UNDEFINED;
6205 struct glyph_row *saved_glyph_row;
6206
6207 /* Don't produce glyphs in produce_glyphs. */
6208 saved_glyph_row = it->glyph_row;
6209 it->glyph_row = NULL;
6210
6211 #define BUFFER_POS_REACHED_P() \
6212 ((op & MOVE_TO_POS) != 0 \
6213 && BUFFERP (it->object) \
6214 && IT_CHARPOS (*it) >= to_charpos \
6215 && (it->method == GET_FROM_BUFFER \
6216 || (it->method == GET_FROM_DISPLAY_VECTOR \
6217 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
6218
6219
6220 while (1)
6221 {
6222 int x, i, ascent = 0, descent = 0;
6223
6224 /* Stop if we move beyond TO_CHARPOS (after an image or stretch glyph). */
6225 if ((op & MOVE_TO_POS) != 0
6226 && BUFFERP (it->object)
6227 && it->method == GET_FROM_BUFFER
6228 && IT_CHARPOS (*it) > to_charpos)
6229 {
6230 result = MOVE_POS_MATCH_OR_ZV;
6231 break;
6232 }
6233
6234 /* Stop when ZV reached.
6235 We used to stop here when TO_CHARPOS reached as well, but that is
6236 too soon if this glyph does not fit on this line. So we handle it
6237 explicitly below. */
6238 if (!get_next_display_element (it)
6239 || (it->truncate_lines_p
6240 && BUFFER_POS_REACHED_P ()))
6241 {
6242 result = MOVE_POS_MATCH_OR_ZV;
6243 break;
6244 }
6245
6246 /* The call to produce_glyphs will get the metrics of the
6247 display element IT is loaded with. We record in x the
6248 x-position before this display element in case it does not
6249 fit on the line. */
6250 x = it->current_x;
6251
6252 /* Remember the line height so far in case the next element doesn't
6253 fit on the line. */
6254 if (!it->truncate_lines_p)
6255 {
6256 ascent = it->max_ascent;
6257 descent = it->max_descent;
6258 }
6259
6260 PRODUCE_GLYPHS (it);
6261
6262 if (it->area != TEXT_AREA)
6263 {
6264 set_iterator_to_next (it, 1);
6265 continue;
6266 }
6267
6268 /* The number of glyphs we get back in IT->nglyphs will normally
6269 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
6270 character on a terminal frame, or (iii) a line end. For the
6271 second case, IT->nglyphs - 1 padding glyphs will be present
6272 (on X frames, there is only one glyph produced for a
6273 composite character.
6274
6275 The behavior implemented below means, for continuation lines,
6276 that as many spaces of a TAB as fit on the current line are
6277 displayed there. For terminal frames, as many glyphs of a
6278 multi-glyph character are displayed in the current line, too.
6279 This is what the old redisplay code did, and we keep it that
6280 way. Under X, the whole shape of a complex character must
6281 fit on the line or it will be completely displayed in the
6282 next line.
6283
6284 Note that both for tabs and padding glyphs, all glyphs have
6285 the same width. */
6286 if (it->nglyphs)
6287 {
6288 /* More than one glyph or glyph doesn't fit on line. All
6289 glyphs have the same width. */
6290 int single_glyph_width = it->pixel_width / it->nglyphs;
6291 int new_x;
6292 int x_before_this_char = x;
6293 int hpos_before_this_char = it->hpos;
6294
6295 for (i = 0; i < it->nglyphs; ++i, x = new_x)
6296 {
6297 new_x = x + single_glyph_width;
6298
6299 /* We want to leave anything reaching TO_X to the caller. */
6300 if ((op & MOVE_TO_X) && new_x > to_x)
6301 {
6302 if (BUFFER_POS_REACHED_P ())
6303 goto buffer_pos_reached;
6304 it->current_x = x;
6305 result = MOVE_X_REACHED;
6306 break;
6307 }
6308 else if (/* Lines are continued. */
6309 !it->truncate_lines_p
6310 && (/* And glyph doesn't fit on the line. */
6311 new_x > it->last_visible_x
6312 /* Or it fits exactly and we're on a window
6313 system frame. */
6314 || (new_x == it->last_visible_x
6315 && FRAME_WINDOW_P (it->f))))
6316 {
6317 if (/* IT->hpos == 0 means the very first glyph
6318 doesn't fit on the line, e.g. a wide image. */
6319 it->hpos == 0
6320 || (new_x == it->last_visible_x
6321 && FRAME_WINDOW_P (it->f)))
6322 {
6323 ++it->hpos;
6324 it->current_x = new_x;
6325
6326 /* The character's last glyph just barely fits
6327 in this row. */
6328 if (i == it->nglyphs - 1)
6329 {
6330 /* If this is the destination position,
6331 return a position *before* it in this row,
6332 now that we know it fits in this row. */
6333 if (BUFFER_POS_REACHED_P ())
6334 {
6335 it->hpos = hpos_before_this_char;
6336 it->current_x = x_before_this_char;
6337 result = MOVE_POS_MATCH_OR_ZV;
6338 break;
6339 }
6340
6341 set_iterator_to_next (it, 1);
6342 #ifdef HAVE_WINDOW_SYSTEM
6343 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
6344 {
6345 if (!get_next_display_element (it))
6346 {
6347 result = MOVE_POS_MATCH_OR_ZV;
6348 break;
6349 }
6350 if (BUFFER_POS_REACHED_P ())
6351 {
6352 if (ITERATOR_AT_END_OF_LINE_P (it))
6353 result = MOVE_POS_MATCH_OR_ZV;
6354 else
6355 result = MOVE_LINE_CONTINUED;
6356 break;
6357 }
6358 if (ITERATOR_AT_END_OF_LINE_P (it))
6359 {
6360 result = MOVE_NEWLINE_OR_CR;
6361 break;
6362 }
6363 }
6364 #endif /* HAVE_WINDOW_SYSTEM */
6365 }
6366 }
6367 else
6368 {
6369 it->current_x = x;
6370 it->max_ascent = ascent;
6371 it->max_descent = descent;
6372 }
6373
6374 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
6375 IT_CHARPOS (*it)));
6376 result = MOVE_LINE_CONTINUED;
6377 break;
6378 }
6379 else if (BUFFER_POS_REACHED_P ())
6380 goto buffer_pos_reached;
6381 else if (new_x > it->first_visible_x)
6382 {
6383 /* Glyph is visible. Increment number of glyphs that
6384 would be displayed. */
6385 ++it->hpos;
6386 }
6387 else
6388 {
6389 /* Glyph is completely off the left margin of the display
6390 area. Nothing to do. */
6391 }
6392 }
6393
6394 if (result != MOVE_UNDEFINED)
6395 break;
6396 }
6397 else if (BUFFER_POS_REACHED_P ())
6398 {
6399 buffer_pos_reached:
6400 it->current_x = x;
6401 it->max_ascent = ascent;
6402 it->max_descent = descent;
6403 result = MOVE_POS_MATCH_OR_ZV;
6404 break;
6405 }
6406 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
6407 {
6408 /* Stop when TO_X specified and reached. This check is
6409 necessary here because of lines consisting of a line end,
6410 only. The line end will not produce any glyphs and we
6411 would never get MOVE_X_REACHED. */
6412 xassert (it->nglyphs == 0);
6413 result = MOVE_X_REACHED;
6414 break;
6415 }
6416
6417 /* Is this a line end? If yes, we're done. */
6418 if (ITERATOR_AT_END_OF_LINE_P (it))
6419 {
6420 result = MOVE_NEWLINE_OR_CR;
6421 break;
6422 }
6423
6424 /* The current display element has been consumed. Advance
6425 to the next. */
6426 set_iterator_to_next (it, 1);
6427
6428 /* Stop if lines are truncated and IT's current x-position is
6429 past the right edge of the window now. */
6430 if (it->truncate_lines_p
6431 && it->current_x >= it->last_visible_x)
6432 {
6433 #ifdef HAVE_WINDOW_SYSTEM
6434 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
6435 {
6436 if (!get_next_display_element (it)
6437 || BUFFER_POS_REACHED_P ())
6438 {
6439 result = MOVE_POS_MATCH_OR_ZV;
6440 break;
6441 }
6442 if (ITERATOR_AT_END_OF_LINE_P (it))
6443 {
6444 result = MOVE_NEWLINE_OR_CR;
6445 break;
6446 }
6447 }
6448 #endif /* HAVE_WINDOW_SYSTEM */
6449 result = MOVE_LINE_TRUNCATED;
6450 break;
6451 }
6452 }
6453
6454 #undef BUFFER_POS_REACHED_P
6455
6456 /* Restore the iterator settings altered at the beginning of this
6457 function. */
6458 it->glyph_row = saved_glyph_row;
6459 return result;
6460 }
6461
6462
6463 /* Move IT forward until it satisfies one or more of the criteria in
6464 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
6465
6466 OP is a bit-mask that specifies where to stop, and in particular,
6467 which of those four position arguments makes a difference. See the
6468 description of enum move_operation_enum.
6469
6470 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
6471 screen line, this function will set IT to the next position >
6472 TO_CHARPOS. */
6473
6474 void
6475 move_it_to (it, to_charpos, to_x, to_y, to_vpos, op)
6476 struct it *it;
6477 int to_charpos, to_x, to_y, to_vpos;
6478 int op;
6479 {
6480 enum move_it_result skip, skip2 = MOVE_X_REACHED;
6481 int line_height;
6482 int reached = 0;
6483
6484 for (;;)
6485 {
6486 if (op & MOVE_TO_VPOS)
6487 {
6488 /* If no TO_CHARPOS and no TO_X specified, stop at the
6489 start of the line TO_VPOS. */
6490 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
6491 {
6492 if (it->vpos == to_vpos)
6493 {
6494 reached = 1;
6495 break;
6496 }
6497 else
6498 skip = move_it_in_display_line_to (it, -1, -1, 0);
6499 }
6500 else
6501 {
6502 /* TO_VPOS >= 0 means stop at TO_X in the line at
6503 TO_VPOS, or at TO_POS, whichever comes first. */
6504 if (it->vpos == to_vpos)
6505 {
6506 reached = 2;
6507 break;
6508 }
6509
6510 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
6511
6512 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
6513 {
6514 reached = 3;
6515 break;
6516 }
6517 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
6518 {
6519 /* We have reached TO_X but not in the line we want. */
6520 skip = move_it_in_display_line_to (it, to_charpos,
6521 -1, MOVE_TO_POS);
6522 if (skip == MOVE_POS_MATCH_OR_ZV)
6523 {
6524 reached = 4;
6525 break;
6526 }
6527 }
6528 }
6529 }
6530 else if (op & MOVE_TO_Y)
6531 {
6532 struct it it_backup;
6533
6534 /* TO_Y specified means stop at TO_X in the line containing
6535 TO_Y---or at TO_CHARPOS if this is reached first. The
6536 problem is that we can't really tell whether the line
6537 contains TO_Y before we have completely scanned it, and
6538 this may skip past TO_X. What we do is to first scan to
6539 TO_X.
6540
6541 If TO_X is not specified, use a TO_X of zero. The reason
6542 is to make the outcome of this function more predictable.
6543 If we didn't use TO_X == 0, we would stop at the end of
6544 the line which is probably not what a caller would expect
6545 to happen. */
6546 skip = move_it_in_display_line_to (it, to_charpos,
6547 ((op & MOVE_TO_X)
6548 ? to_x : 0),
6549 (MOVE_TO_X
6550 | (op & MOVE_TO_POS)));
6551
6552 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
6553 if (skip == MOVE_POS_MATCH_OR_ZV)
6554 {
6555 reached = 5;
6556 break;
6557 }
6558
6559 /* If TO_X was reached, we would like to know whether TO_Y
6560 is in the line. This can only be said if we know the
6561 total line height which requires us to scan the rest of
6562 the line. */
6563 if (skip == MOVE_X_REACHED)
6564 {
6565 it_backup = *it;
6566 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
6567 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
6568 op & MOVE_TO_POS);
6569 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
6570 }
6571
6572 /* Now, decide whether TO_Y is in this line. */
6573 line_height = it->max_ascent + it->max_descent;
6574 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
6575
6576 if (to_y >= it->current_y
6577 && to_y < it->current_y + line_height)
6578 {
6579 if (skip == MOVE_X_REACHED)
6580 /* If TO_Y is in this line and TO_X was reached above,
6581 we scanned too far. We have to restore IT's settings
6582 to the ones before skipping. */
6583 *it = it_backup;
6584 reached = 6;
6585 }
6586 else if (skip == MOVE_X_REACHED)
6587 {
6588 skip = skip2;
6589 if (skip == MOVE_POS_MATCH_OR_ZV)
6590 reached = 7;
6591 }
6592
6593 if (reached)
6594 break;
6595 }
6596 else
6597 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
6598
6599 switch (skip)
6600 {
6601 case MOVE_POS_MATCH_OR_ZV:
6602 reached = 8;
6603 goto out;
6604
6605 case MOVE_NEWLINE_OR_CR:
6606 set_iterator_to_next (it, 1);
6607 it->continuation_lines_width = 0;
6608 break;
6609
6610 case MOVE_LINE_TRUNCATED:
6611 it->continuation_lines_width = 0;
6612 reseat_at_next_visible_line_start (it, 0);
6613 if ((op & MOVE_TO_POS) != 0
6614 && IT_CHARPOS (*it) > to_charpos)
6615 {
6616 reached = 9;
6617 goto out;
6618 }
6619 break;
6620
6621 case MOVE_LINE_CONTINUED:
6622 it->continuation_lines_width += it->current_x;
6623 break;
6624
6625 default:
6626 abort ();
6627 }
6628
6629 /* Reset/increment for the next run. */
6630 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
6631 it->current_x = it->hpos = 0;
6632 it->current_y += it->max_ascent + it->max_descent;
6633 ++it->vpos;
6634 last_height = it->max_ascent + it->max_descent;
6635 last_max_ascent = it->max_ascent;
6636 it->max_ascent = it->max_descent = 0;
6637 }
6638
6639 out:
6640
6641 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
6642 }
6643
6644
6645 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
6646
6647 If DY > 0, move IT backward at least that many pixels. DY = 0
6648 means move IT backward to the preceding line start or BEGV. This
6649 function may move over more than DY pixels if IT->current_y - DY
6650 ends up in the middle of a line; in this case IT->current_y will be
6651 set to the top of the line moved to. */
6652
6653 void
6654 move_it_vertically_backward (it, dy)
6655 struct it *it;
6656 int dy;
6657 {
6658 int nlines, h;
6659 struct it it2, it3;
6660 int start_pos;
6661
6662 move_further_back:
6663 xassert (dy >= 0);
6664
6665 start_pos = IT_CHARPOS (*it);
6666
6667 /* Estimate how many newlines we must move back. */
6668 nlines = max (1, dy / FRAME_LINE_HEIGHT (it->f));
6669
6670 /* Set the iterator's position that many lines back. */
6671 while (nlines-- && IT_CHARPOS (*it) > BEGV)
6672 back_to_previous_visible_line_start (it);
6673
6674 /* Reseat the iterator here. When moving backward, we don't want
6675 reseat to skip forward over invisible text, set up the iterator
6676 to deliver from overlay strings at the new position etc. So,
6677 use reseat_1 here. */
6678 reseat_1 (it, it->current.pos, 1);
6679
6680 /* We are now surely at a line start. */
6681 it->current_x = it->hpos = 0;
6682 it->continuation_lines_width = 0;
6683
6684 /* Move forward and see what y-distance we moved. First move to the
6685 start of the next line so that we get its height. We need this
6686 height to be able to tell whether we reached the specified
6687 y-distance. */
6688 it2 = *it;
6689 it2.max_ascent = it2.max_descent = 0;
6690 do
6691 {
6692 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
6693 MOVE_TO_POS | MOVE_TO_VPOS);
6694 }
6695 while (!IT_POS_VALID_AFTER_MOVE_P (&it2));
6696 xassert (IT_CHARPOS (*it) >= BEGV);
6697 it3 = it2;
6698
6699 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
6700 xassert (IT_CHARPOS (*it) >= BEGV);
6701 /* H is the actual vertical distance from the position in *IT
6702 and the starting position. */
6703 h = it2.current_y - it->current_y;
6704 /* NLINES is the distance in number of lines. */
6705 nlines = it2.vpos - it->vpos;
6706
6707 /* Correct IT's y and vpos position
6708 so that they are relative to the starting point. */
6709 it->vpos -= nlines;
6710 it->current_y -= h;
6711
6712 if (dy == 0)
6713 {
6714 /* DY == 0 means move to the start of the screen line. The
6715 value of nlines is > 0 if continuation lines were involved. */
6716 if (nlines > 0)
6717 move_it_by_lines (it, nlines, 1);
6718 #if 0
6719 /* I think this assert is bogus if buffer contains
6720 invisible text or images. KFS. */
6721 xassert (IT_CHARPOS (*it) <= start_pos);
6722 #endif
6723 }
6724 else
6725 {
6726 /* The y-position we try to reach, relative to *IT.
6727 Note that H has been subtracted in front of the if-statement. */
6728 int target_y = it->current_y + h - dy;
6729 int y0 = it3.current_y;
6730 int y1 = line_bottom_y (&it3);
6731 int line_height = y1 - y0;
6732
6733 /* If we did not reach target_y, try to move further backward if
6734 we can. If we moved too far backward, try to move forward. */
6735 if (target_y < it->current_y
6736 /* This is heuristic. In a window that's 3 lines high, with
6737 a line height of 13 pixels each, recentering with point
6738 on the bottom line will try to move -39/2 = 19 pixels
6739 backward. Try to avoid moving into the first line. */
6740 && (it->current_y - target_y
6741 > min (window_box_height (it->w), line_height * 2 / 3))
6742 && IT_CHARPOS (*it) > BEGV)
6743 {
6744 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
6745 target_y - it->current_y));
6746 dy = it->current_y - target_y;
6747 goto move_further_back;
6748 }
6749 else if (target_y >= it->current_y + line_height
6750 && IT_CHARPOS (*it) < ZV)
6751 {
6752 /* Should move forward by at least one line, maybe more.
6753
6754 Note: Calling move_it_by_lines can be expensive on
6755 terminal frames, where compute_motion is used (via
6756 vmotion) to do the job, when there are very long lines
6757 and truncate-lines is nil. That's the reason for
6758 treating terminal frames specially here. */
6759
6760 if (!FRAME_WINDOW_P (it->f))
6761 move_it_vertically (it, target_y - (it->current_y + line_height));
6762 else
6763 {
6764 do
6765 {
6766 move_it_by_lines (it, 1, 1);
6767 }
6768 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
6769 }
6770
6771 #if 0
6772 /* I think this assert is bogus if buffer contains
6773 invisible text or images. KFS. */
6774 xassert (IT_CHARPOS (*it) >= BEGV);
6775 #endif
6776 }
6777 }
6778 }
6779
6780
6781 /* Move IT by a specified amount of pixel lines DY. DY negative means
6782 move backwards. DY = 0 means move to start of screen line. At the
6783 end, IT will be on the start of a screen line. */
6784
6785 void
6786 move_it_vertically (it, dy)
6787 struct it *it;
6788 int dy;
6789 {
6790 if (dy <= 0)
6791 move_it_vertically_backward (it, -dy);
6792 else
6793 {
6794 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
6795 move_it_to (it, ZV, -1, it->current_y + dy, -1,
6796 MOVE_TO_POS | MOVE_TO_Y);
6797 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
6798
6799 /* If buffer ends in ZV without a newline, move to the start of
6800 the line to satisfy the post-condition. */
6801 if (IT_CHARPOS (*it) == ZV
6802 && ZV > BEGV
6803 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
6804 move_it_by_lines (it, 0, 0);
6805 }
6806 }
6807
6808
6809 /* Move iterator IT past the end of the text line it is in. */
6810
6811 void
6812 move_it_past_eol (it)
6813 struct it *it;
6814 {
6815 enum move_it_result rc;
6816
6817 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
6818 if (rc == MOVE_NEWLINE_OR_CR)
6819 set_iterator_to_next (it, 0);
6820 }
6821
6822
6823 #if 0 /* Currently not used. */
6824
6825 /* Return non-zero if some text between buffer positions START_CHARPOS
6826 and END_CHARPOS is invisible. IT->window is the window for text
6827 property lookup. */
6828
6829 static int
6830 invisible_text_between_p (it, start_charpos, end_charpos)
6831 struct it *it;
6832 int start_charpos, end_charpos;
6833 {
6834 Lisp_Object prop, limit;
6835 int invisible_found_p;
6836
6837 xassert (it != NULL && start_charpos <= end_charpos);
6838
6839 /* Is text at START invisible? */
6840 prop = Fget_char_property (make_number (start_charpos), Qinvisible,
6841 it->window);
6842 if (TEXT_PROP_MEANS_INVISIBLE (prop))
6843 invisible_found_p = 1;
6844 else
6845 {
6846 limit = Fnext_single_char_property_change (make_number (start_charpos),
6847 Qinvisible, Qnil,
6848 make_number (end_charpos));
6849 invisible_found_p = XFASTINT (limit) < end_charpos;
6850 }
6851
6852 return invisible_found_p;
6853 }
6854
6855 #endif /* 0 */
6856
6857
6858 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
6859 negative means move up. DVPOS == 0 means move to the start of the
6860 screen line. NEED_Y_P non-zero means calculate IT->current_y. If
6861 NEED_Y_P is zero, IT->current_y will be left unchanged.
6862
6863 Further optimization ideas: If we would know that IT->f doesn't use
6864 a face with proportional font, we could be faster for
6865 truncate-lines nil. */
6866
6867 void
6868 move_it_by_lines (it, dvpos, need_y_p)
6869 struct it *it;
6870 int dvpos, need_y_p;
6871 {
6872 struct position pos;
6873
6874 if (!FRAME_WINDOW_P (it->f))
6875 {
6876 struct text_pos textpos;
6877
6878 /* We can use vmotion on frames without proportional fonts. */
6879 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
6880 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
6881 reseat (it, textpos, 1);
6882 it->vpos += pos.vpos;
6883 it->current_y += pos.vpos;
6884 }
6885 else if (dvpos == 0)
6886 {
6887 /* DVPOS == 0 means move to the start of the screen line. */
6888 move_it_vertically_backward (it, 0);
6889 xassert (it->current_x == 0 && it->hpos == 0);
6890 /* Let next call to line_bottom_y calculate real line height */
6891 last_height = 0;
6892 }
6893 else if (dvpos > 0)
6894 {
6895 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
6896 if (!IT_POS_VALID_AFTER_MOVE_P (it))
6897 move_it_to (it, IT_CHARPOS (*it) + 1, -1, -1, -1, MOVE_TO_POS);
6898 }
6899 else
6900 {
6901 struct it it2;
6902 int start_charpos, i;
6903
6904 /* Start at the beginning of the screen line containing IT's
6905 position. This may actually move vertically backwards,
6906 in case of overlays, so adjust dvpos accordingly. */
6907 dvpos += it->vpos;
6908 move_it_vertically_backward (it, 0);
6909 dvpos -= it->vpos;
6910
6911 /* Go back -DVPOS visible lines and reseat the iterator there. */
6912 start_charpos = IT_CHARPOS (*it);
6913 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > BEGV; --i)
6914 back_to_previous_visible_line_start (it);
6915 reseat (it, it->current.pos, 1);
6916
6917 /* Move further back if we end up in a string or an image. */
6918 while (!IT_POS_VALID_AFTER_MOVE_P (it))
6919 {
6920 /* First try to move to start of display line. */
6921 dvpos += it->vpos;
6922 move_it_vertically_backward (it, 0);
6923 dvpos -= it->vpos;
6924 if (IT_POS_VALID_AFTER_MOVE_P (it))
6925 break;
6926 /* If start of line is still in string or image,
6927 move further back. */
6928 back_to_previous_visible_line_start (it);
6929 reseat (it, it->current.pos, 1);
6930 dvpos--;
6931 }
6932
6933 it->current_x = it->hpos = 0;
6934
6935 /* Above call may have moved too far if continuation lines
6936 are involved. Scan forward and see if it did. */
6937 it2 = *it;
6938 it2.vpos = it2.current_y = 0;
6939 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
6940 it->vpos -= it2.vpos;
6941 it->current_y -= it2.current_y;
6942 it->current_x = it->hpos = 0;
6943
6944 /* If we moved too far back, move IT some lines forward. */
6945 if (it2.vpos > -dvpos)
6946 {
6947 int delta = it2.vpos + dvpos;
6948 it2 = *it;
6949 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
6950 /* Move back again if we got too far ahead. */
6951 if (IT_CHARPOS (*it) >= start_charpos)
6952 *it = it2;
6953 }
6954 }
6955 }
6956
6957 /* Return 1 if IT points into the middle of a display vector. */
6958
6959 int
6960 in_display_vector_p (it)
6961 struct it *it;
6962 {
6963 return (it->method == GET_FROM_DISPLAY_VECTOR
6964 && it->current.dpvec_index > 0
6965 && it->dpvec + it->current.dpvec_index != it->dpend);
6966 }
6967
6968 \f
6969 /***********************************************************************
6970 Messages
6971 ***********************************************************************/
6972
6973
6974 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
6975 to *Messages*. */
6976
6977 void
6978 add_to_log (format, arg1, arg2)
6979 char *format;
6980 Lisp_Object arg1, arg2;
6981 {
6982 Lisp_Object args[3];
6983 Lisp_Object msg, fmt;
6984 char *buffer;
6985 int len;
6986 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
6987 USE_SAFE_ALLOCA;
6988
6989 /* Do nothing if called asynchronously. Inserting text into
6990 a buffer may call after-change-functions and alike and
6991 that would means running Lisp asynchronously. */
6992 if (handling_signal)
6993 return;
6994
6995 fmt = msg = Qnil;
6996 GCPRO4 (fmt, msg, arg1, arg2);
6997
6998 args[0] = fmt = build_string (format);
6999 args[1] = arg1;
7000 args[2] = arg2;
7001 msg = Fformat (3, args);
7002
7003 len = SBYTES (msg) + 1;
7004 SAFE_ALLOCA (buffer, char *, len);
7005 bcopy (SDATA (msg), buffer, len);
7006
7007 message_dolog (buffer, len - 1, 1, 0);
7008 SAFE_FREE ();
7009
7010 UNGCPRO;
7011 }
7012
7013
7014 /* Output a newline in the *Messages* buffer if "needs" one. */
7015
7016 void
7017 message_log_maybe_newline ()
7018 {
7019 if (message_log_need_newline)
7020 message_dolog ("", 0, 1, 0);
7021 }
7022
7023
7024 /* Add a string M of length NBYTES to the message log, optionally
7025 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
7026 nonzero, means interpret the contents of M as multibyte. This
7027 function calls low-level routines in order to bypass text property
7028 hooks, etc. which might not be safe to run.
7029
7030 This may GC (insert may run before/after change hooks),
7031 so the buffer M must NOT point to a Lisp string. */
7032
7033 void
7034 message_dolog (m, nbytes, nlflag, multibyte)
7035 const char *m;
7036 int nbytes, nlflag, multibyte;
7037 {
7038 if (!NILP (Vmemory_full))
7039 return;
7040
7041 if (!NILP (Vmessage_log_max))
7042 {
7043 struct buffer *oldbuf;
7044 Lisp_Object oldpoint, oldbegv, oldzv;
7045 int old_windows_or_buffers_changed = windows_or_buffers_changed;
7046 int point_at_end = 0;
7047 int zv_at_end = 0;
7048 Lisp_Object old_deactivate_mark, tem;
7049 struct gcpro gcpro1;
7050
7051 old_deactivate_mark = Vdeactivate_mark;
7052 oldbuf = current_buffer;
7053 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
7054 current_buffer->undo_list = Qt;
7055
7056 oldpoint = message_dolog_marker1;
7057 set_marker_restricted (oldpoint, make_number (PT), Qnil);
7058 oldbegv = message_dolog_marker2;
7059 set_marker_restricted (oldbegv, make_number (BEGV), Qnil);
7060 oldzv = message_dolog_marker3;
7061 set_marker_restricted (oldzv, make_number (ZV), Qnil);
7062 GCPRO1 (old_deactivate_mark);
7063
7064 if (PT == Z)
7065 point_at_end = 1;
7066 if (ZV == Z)
7067 zv_at_end = 1;
7068
7069 BEGV = BEG;
7070 BEGV_BYTE = BEG_BYTE;
7071 ZV = Z;
7072 ZV_BYTE = Z_BYTE;
7073 TEMP_SET_PT_BOTH (Z, Z_BYTE);
7074
7075 /* Insert the string--maybe converting multibyte to single byte
7076 or vice versa, so that all the text fits the buffer. */
7077 if (multibyte
7078 && NILP (current_buffer->enable_multibyte_characters))
7079 {
7080 int i, c, char_bytes;
7081 unsigned char work[1];
7082
7083 /* Convert a multibyte string to single-byte
7084 for the *Message* buffer. */
7085 for (i = 0; i < nbytes; i += char_bytes)
7086 {
7087 c = string_char_and_length (m + i, nbytes - i, &char_bytes);
7088 work[0] = (SINGLE_BYTE_CHAR_P (c)
7089 ? c
7090 : multibyte_char_to_unibyte (c, Qnil));
7091 insert_1_both (work, 1, 1, 1, 0, 0);
7092 }
7093 }
7094 else if (! multibyte
7095 && ! NILP (current_buffer->enable_multibyte_characters))
7096 {
7097 int i, c, char_bytes;
7098 unsigned char *msg = (unsigned char *) m;
7099 unsigned char str[MAX_MULTIBYTE_LENGTH];
7100 /* Convert a single-byte string to multibyte
7101 for the *Message* buffer. */
7102 for (i = 0; i < nbytes; i++)
7103 {
7104 c = unibyte_char_to_multibyte (msg[i]);
7105 char_bytes = CHAR_STRING (c, str);
7106 insert_1_both (str, 1, char_bytes, 1, 0, 0);
7107 }
7108 }
7109 else if (nbytes)
7110 insert_1 (m, nbytes, 1, 0, 0);
7111
7112 if (nlflag)
7113 {
7114 int this_bol, this_bol_byte, prev_bol, prev_bol_byte, dup;
7115 insert_1 ("\n", 1, 1, 0, 0);
7116
7117 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
7118 this_bol = PT;
7119 this_bol_byte = PT_BYTE;
7120
7121 /* See if this line duplicates the previous one.
7122 If so, combine duplicates. */
7123 if (this_bol > BEG)
7124 {
7125 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
7126 prev_bol = PT;
7127 prev_bol_byte = PT_BYTE;
7128
7129 dup = message_log_check_duplicate (prev_bol, prev_bol_byte,
7130 this_bol, this_bol_byte);
7131 if (dup)
7132 {
7133 del_range_both (prev_bol, prev_bol_byte,
7134 this_bol, this_bol_byte, 0);
7135 if (dup > 1)
7136 {
7137 char dupstr[40];
7138 int duplen;
7139
7140 /* If you change this format, don't forget to also
7141 change message_log_check_duplicate. */
7142 sprintf (dupstr, " [%d times]", dup);
7143 duplen = strlen (dupstr);
7144 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
7145 insert_1 (dupstr, duplen, 1, 0, 1);
7146 }
7147 }
7148 }
7149
7150 /* If we have more than the desired maximum number of lines
7151 in the *Messages* buffer now, delete the oldest ones.
7152 This is safe because we don't have undo in this buffer. */
7153
7154 if (NATNUMP (Vmessage_log_max))
7155 {
7156 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
7157 -XFASTINT (Vmessage_log_max) - 1, 0);
7158 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
7159 }
7160 }
7161 BEGV = XMARKER (oldbegv)->charpos;
7162 BEGV_BYTE = marker_byte_position (oldbegv);
7163
7164 if (zv_at_end)
7165 {
7166 ZV = Z;
7167 ZV_BYTE = Z_BYTE;
7168 }
7169 else
7170 {
7171 ZV = XMARKER (oldzv)->charpos;
7172 ZV_BYTE = marker_byte_position (oldzv);
7173 }
7174
7175 if (point_at_end)
7176 TEMP_SET_PT_BOTH (Z, Z_BYTE);
7177 else
7178 /* We can't do Fgoto_char (oldpoint) because it will run some
7179 Lisp code. */
7180 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
7181 XMARKER (oldpoint)->bytepos);
7182
7183 UNGCPRO;
7184 unchain_marker (XMARKER (oldpoint));
7185 unchain_marker (XMARKER (oldbegv));
7186 unchain_marker (XMARKER (oldzv));
7187
7188 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
7189 set_buffer_internal (oldbuf);
7190 if (NILP (tem))
7191 windows_or_buffers_changed = old_windows_or_buffers_changed;
7192 message_log_need_newline = !nlflag;
7193 Vdeactivate_mark = old_deactivate_mark;
7194 }
7195 }
7196
7197
7198 /* We are at the end of the buffer after just having inserted a newline.
7199 (Note: We depend on the fact we won't be crossing the gap.)
7200 Check to see if the most recent message looks a lot like the previous one.
7201 Return 0 if different, 1 if the new one should just replace it, or a
7202 value N > 1 if we should also append " [N times]". */
7203
7204 static int
7205 message_log_check_duplicate (prev_bol, prev_bol_byte, this_bol, this_bol_byte)
7206 int prev_bol, this_bol;
7207 int prev_bol_byte, this_bol_byte;
7208 {
7209 int i;
7210 int len = Z_BYTE - 1 - this_bol_byte;
7211 int seen_dots = 0;
7212 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
7213 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
7214
7215 for (i = 0; i < len; i++)
7216 {
7217 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
7218 seen_dots = 1;
7219 if (p1[i] != p2[i])
7220 return seen_dots;
7221 }
7222 p1 += len;
7223 if (*p1 == '\n')
7224 return 2;
7225 if (*p1++ == ' ' && *p1++ == '[')
7226 {
7227 int n = 0;
7228 while (*p1 >= '0' && *p1 <= '9')
7229 n = n * 10 + *p1++ - '0';
7230 if (strncmp (p1, " times]\n", 8) == 0)
7231 return n+1;
7232 }
7233 return 0;
7234 }
7235 \f
7236
7237 /* Display an echo area message M with a specified length of NBYTES
7238 bytes. The string may include null characters. If M is 0, clear
7239 out any existing message, and let the mini-buffer text show
7240 through.
7241
7242 This may GC, so the buffer M must NOT point to a Lisp string. */
7243
7244 void
7245 message2 (m, nbytes, multibyte)
7246 const char *m;
7247 int nbytes;
7248 int multibyte;
7249 {
7250 /* First flush out any partial line written with print. */
7251 message_log_maybe_newline ();
7252 if (m)
7253 message_dolog (m, nbytes, 1, multibyte);
7254 message2_nolog (m, nbytes, multibyte);
7255 }
7256
7257
7258 /* The non-logging counterpart of message2. */
7259
7260 void
7261 message2_nolog (m, nbytes, multibyte)
7262 const char *m;
7263 int nbytes, multibyte;
7264 {
7265 struct frame *sf = SELECTED_FRAME ();
7266 message_enable_multibyte = multibyte;
7267
7268 if (noninteractive)
7269 {
7270 if (noninteractive_need_newline)
7271 putc ('\n', stderr);
7272 noninteractive_need_newline = 0;
7273 if (m)
7274 fwrite (m, nbytes, 1, stderr);
7275 if (cursor_in_echo_area == 0)
7276 fprintf (stderr, "\n");
7277 fflush (stderr);
7278 }
7279 /* A null message buffer means that the frame hasn't really been
7280 initialized yet. Error messages get reported properly by
7281 cmd_error, so this must be just an informative message; toss it. */
7282 else if (INTERACTIVE
7283 && sf->glyphs_initialized_p
7284 && FRAME_MESSAGE_BUF (sf))
7285 {
7286 Lisp_Object mini_window;
7287 struct frame *f;
7288
7289 /* Get the frame containing the mini-buffer
7290 that the selected frame is using. */
7291 mini_window = FRAME_MINIBUF_WINDOW (sf);
7292 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
7293
7294 FRAME_SAMPLE_VISIBILITY (f);
7295 if (FRAME_VISIBLE_P (sf)
7296 && ! FRAME_VISIBLE_P (f))
7297 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
7298
7299 if (m)
7300 {
7301 set_message (m, Qnil, nbytes, multibyte);
7302 if (minibuffer_auto_raise)
7303 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
7304 }
7305 else
7306 clear_message (1, 1);
7307
7308 do_pending_window_change (0);
7309 echo_area_display (1);
7310 do_pending_window_change (0);
7311 if (FRAME_TERMINAL (f)->frame_up_to_date_hook != 0 && ! gc_in_progress)
7312 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
7313 }
7314 }
7315
7316
7317 /* Display an echo area message M with a specified length of NBYTES
7318 bytes. The string may include null characters. If M is not a
7319 string, clear out any existing message, and let the mini-buffer
7320 text show through.
7321
7322 This function cancels echoing. */
7323
7324 void
7325 message3 (m, nbytes, multibyte)
7326 Lisp_Object m;
7327 int nbytes;
7328 int multibyte;
7329 {
7330 struct gcpro gcpro1;
7331
7332 GCPRO1 (m);
7333 clear_message (1,1);
7334 cancel_echoing ();
7335
7336 /* First flush out any partial line written with print. */
7337 message_log_maybe_newline ();
7338 if (STRINGP (m))
7339 {
7340 char *buffer;
7341 USE_SAFE_ALLOCA;
7342
7343 SAFE_ALLOCA (buffer, char *, nbytes);
7344 bcopy (SDATA (m), buffer, nbytes);
7345 message_dolog (buffer, nbytes, 1, multibyte);
7346 SAFE_FREE ();
7347 }
7348 message3_nolog (m, nbytes, multibyte);
7349
7350 UNGCPRO;
7351 }
7352
7353
7354 /* The non-logging version of message3.
7355 This does not cancel echoing, because it is used for echoing.
7356 Perhaps we need to make a separate function for echoing
7357 and make this cancel echoing. */
7358
7359 void
7360 message3_nolog (m, nbytes, multibyte)
7361 Lisp_Object m;
7362 int nbytes, multibyte;
7363 {
7364 struct frame *sf = SELECTED_FRAME ();
7365 message_enable_multibyte = multibyte;
7366
7367 if (noninteractive)
7368 {
7369 if (noninteractive_need_newline)
7370 putc ('\n', stderr);
7371 noninteractive_need_newline = 0;
7372 if (STRINGP (m))
7373 fwrite (SDATA (m), nbytes, 1, stderr);
7374 if (cursor_in_echo_area == 0)
7375 fprintf (stderr, "\n");
7376 fflush (stderr);
7377 }
7378 /* A null message buffer means that the frame hasn't really been
7379 initialized yet. Error messages get reported properly by
7380 cmd_error, so this must be just an informative message; toss it. */
7381 else if (INTERACTIVE
7382 && sf->glyphs_initialized_p
7383 && FRAME_MESSAGE_BUF (sf))
7384 {
7385 Lisp_Object mini_window;
7386 Lisp_Object frame;
7387 struct frame *f;
7388
7389 /* Get the frame containing the mini-buffer
7390 that the selected frame is using. */
7391 mini_window = FRAME_MINIBUF_WINDOW (sf);
7392 frame = XWINDOW (mini_window)->frame;
7393 f = XFRAME (frame);
7394
7395 FRAME_SAMPLE_VISIBILITY (f);
7396 if (FRAME_VISIBLE_P (sf)
7397 && !FRAME_VISIBLE_P (f))
7398 Fmake_frame_visible (frame);
7399
7400 if (STRINGP (m) && SCHARS (m) > 0)
7401 {
7402 set_message (NULL, m, nbytes, multibyte);
7403 if (minibuffer_auto_raise)
7404 Fraise_frame (frame);
7405 /* Assume we are not echoing.
7406 (If we are, echo_now will override this.) */
7407 echo_message_buffer = Qnil;
7408 }
7409 else
7410 clear_message (1, 1);
7411
7412 do_pending_window_change (0);
7413 echo_area_display (1);
7414 do_pending_window_change (0);
7415 if (FRAME_TERMINAL (f)->frame_up_to_date_hook != 0 && ! gc_in_progress)
7416 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
7417 }
7418 }
7419
7420
7421 /* Display a null-terminated echo area message M. If M is 0, clear
7422 out any existing message, and let the mini-buffer text show through.
7423
7424 The buffer M must continue to exist until after the echo area gets
7425 cleared or some other message gets displayed there. Do not pass
7426 text that is stored in a Lisp string. Do not pass text in a buffer
7427 that was alloca'd. */
7428
7429 void
7430 message1 (m)
7431 char *m;
7432 {
7433 message2 (m, (m ? strlen (m) : 0), 0);
7434 }
7435
7436
7437 /* The non-logging counterpart of message1. */
7438
7439 void
7440 message1_nolog (m)
7441 char *m;
7442 {
7443 message2_nolog (m, (m ? strlen (m) : 0), 0);
7444 }
7445
7446 /* Display a message M which contains a single %s
7447 which gets replaced with STRING. */
7448
7449 void
7450 message_with_string (m, string, log)
7451 char *m;
7452 Lisp_Object string;
7453 int log;
7454 {
7455 CHECK_STRING (string);
7456
7457 if (noninteractive)
7458 {
7459 if (m)
7460 {
7461 if (noninteractive_need_newline)
7462 putc ('\n', stderr);
7463 noninteractive_need_newline = 0;
7464 fprintf (stderr, m, SDATA (string));
7465 if (cursor_in_echo_area == 0)
7466 fprintf (stderr, "\n");
7467 fflush (stderr);
7468 }
7469 }
7470 else if (INTERACTIVE)
7471 {
7472 /* The frame whose minibuffer we're going to display the message on.
7473 It may be larger than the selected frame, so we need
7474 to use its buffer, not the selected frame's buffer. */
7475 Lisp_Object mini_window;
7476 struct frame *f, *sf = SELECTED_FRAME ();
7477
7478 /* Get the frame containing the minibuffer
7479 that the selected frame is using. */
7480 mini_window = FRAME_MINIBUF_WINDOW (sf);
7481 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
7482
7483 /* A null message buffer means that the frame hasn't really been
7484 initialized yet. Error messages get reported properly by
7485 cmd_error, so this must be just an informative message; toss it. */
7486 if (FRAME_MESSAGE_BUF (f))
7487 {
7488 Lisp_Object args[2], message;
7489 struct gcpro gcpro1, gcpro2;
7490
7491 args[0] = build_string (m);
7492 args[1] = message = string;
7493 GCPRO2 (args[0], message);
7494 gcpro1.nvars = 2;
7495
7496 message = Fformat (2, args);
7497
7498 if (log)
7499 message3 (message, SBYTES (message), STRING_MULTIBYTE (message));
7500 else
7501 message3_nolog (message, SBYTES (message), STRING_MULTIBYTE (message));
7502
7503 UNGCPRO;
7504
7505 /* Print should start at the beginning of the message
7506 buffer next time. */
7507 message_buf_print = 0;
7508 }
7509 }
7510 }
7511
7512
7513 /* Dump an informative message to the minibuf. If M is 0, clear out
7514 any existing message, and let the mini-buffer text show through. */
7515
7516 /* VARARGS 1 */
7517 void
7518 message (m, a1, a2, a3)
7519 char *m;
7520 EMACS_INT a1, a2, a3;
7521 {
7522 if (noninteractive)
7523 {
7524 if (m)
7525 {
7526 if (noninteractive_need_newline)
7527 putc ('\n', stderr);
7528 noninteractive_need_newline = 0;
7529 fprintf (stderr, m, a1, a2, a3);
7530 if (cursor_in_echo_area == 0)
7531 fprintf (stderr, "\n");
7532 fflush (stderr);
7533 }
7534 }
7535 else if (INTERACTIVE)
7536 {
7537 /* The frame whose mini-buffer we're going to display the message
7538 on. It may be larger than the selected frame, so we need to
7539 use its buffer, not the selected frame's buffer. */
7540 Lisp_Object mini_window;
7541 struct frame *f, *sf = SELECTED_FRAME ();
7542
7543 /* Get the frame containing the mini-buffer
7544 that the selected frame is using. */
7545 mini_window = FRAME_MINIBUF_WINDOW (sf);
7546 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
7547
7548 /* A null message buffer means that the frame hasn't really been
7549 initialized yet. Error messages get reported properly by
7550 cmd_error, so this must be just an informative message; toss
7551 it. */
7552 if (FRAME_MESSAGE_BUF (f))
7553 {
7554 if (m)
7555 {
7556 int len;
7557 #ifdef NO_ARG_ARRAY
7558 char *a[3];
7559 a[0] = (char *) a1;
7560 a[1] = (char *) a2;
7561 a[2] = (char *) a3;
7562
7563 len = doprnt (FRAME_MESSAGE_BUF (f),
7564 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
7565 #else
7566 len = doprnt (FRAME_MESSAGE_BUF (f),
7567 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3,
7568 (char **) &a1);
7569 #endif /* NO_ARG_ARRAY */
7570
7571 message2 (FRAME_MESSAGE_BUF (f), len, 0);
7572 }
7573 else
7574 message1 (0);
7575
7576 /* Print should start at the beginning of the message
7577 buffer next time. */
7578 message_buf_print = 0;
7579 }
7580 }
7581 }
7582
7583
7584 /* The non-logging version of message. */
7585
7586 void
7587 message_nolog (m, a1, a2, a3)
7588 char *m;
7589 EMACS_INT a1, a2, a3;
7590 {
7591 Lisp_Object old_log_max;
7592 old_log_max = Vmessage_log_max;
7593 Vmessage_log_max = Qnil;
7594 message (m, a1, a2, a3);
7595 Vmessage_log_max = old_log_max;
7596 }
7597
7598
7599 /* Display the current message in the current mini-buffer. This is
7600 only called from error handlers in process.c, and is not time
7601 critical. */
7602
7603 void
7604 update_echo_area ()
7605 {
7606 if (!NILP (echo_area_buffer[0]))
7607 {
7608 Lisp_Object string;
7609 string = Fcurrent_message ();
7610 message3 (string, SBYTES (string),
7611 !NILP (current_buffer->enable_multibyte_characters));
7612 }
7613 }
7614
7615
7616 /* Make sure echo area buffers in `echo_buffers' are live.
7617 If they aren't, make new ones. */
7618
7619 static void
7620 ensure_echo_area_buffers ()
7621 {
7622 int i;
7623
7624 for (i = 0; i < 2; ++i)
7625 if (!BUFFERP (echo_buffer[i])
7626 || NILP (XBUFFER (echo_buffer[i])->name))
7627 {
7628 char name[30];
7629 Lisp_Object old_buffer;
7630 int j;
7631
7632 old_buffer = echo_buffer[i];
7633 sprintf (name, " *Echo Area %d*", i);
7634 echo_buffer[i] = Fget_buffer_create (build_string (name));
7635 XBUFFER (echo_buffer[i])->truncate_lines = Qnil;
7636
7637 for (j = 0; j < 2; ++j)
7638 if (EQ (old_buffer, echo_area_buffer[j]))
7639 echo_area_buffer[j] = echo_buffer[i];
7640 }
7641 }
7642
7643
7644 /* Call FN with args A1..A4 with either the current or last displayed
7645 echo_area_buffer as current buffer.
7646
7647 WHICH zero means use the current message buffer
7648 echo_area_buffer[0]. If that is nil, choose a suitable buffer
7649 from echo_buffer[] and clear it.
7650
7651 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
7652 suitable buffer from echo_buffer[] and clear it.
7653
7654 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
7655 that the current message becomes the last displayed one, make
7656 choose a suitable buffer for echo_area_buffer[0], and clear it.
7657
7658 Value is what FN returns. */
7659
7660 static int
7661 with_echo_area_buffer (w, which, fn, a1, a2, a3, a4)
7662 struct window *w;
7663 int which;
7664 int (*fn) P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
7665 EMACS_INT a1;
7666 Lisp_Object a2;
7667 EMACS_INT a3, a4;
7668 {
7669 Lisp_Object buffer;
7670 int this_one, the_other, clear_buffer_p, rc;
7671 int count = SPECPDL_INDEX ();
7672
7673 /* If buffers aren't live, make new ones. */
7674 ensure_echo_area_buffers ();
7675
7676 clear_buffer_p = 0;
7677
7678 if (which == 0)
7679 this_one = 0, the_other = 1;
7680 else if (which > 0)
7681 this_one = 1, the_other = 0;
7682 else
7683 {
7684 this_one = 0, the_other = 1;
7685 clear_buffer_p = 1;
7686
7687 /* We need a fresh one in case the current echo buffer equals
7688 the one containing the last displayed echo area message. */
7689 if (!NILP (echo_area_buffer[this_one])
7690 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
7691 echo_area_buffer[this_one] = Qnil;
7692 }
7693
7694 /* Choose a suitable buffer from echo_buffer[] is we don't
7695 have one. */
7696 if (NILP (echo_area_buffer[this_one]))
7697 {
7698 echo_area_buffer[this_one]
7699 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
7700 ? echo_buffer[the_other]
7701 : echo_buffer[this_one]);
7702 clear_buffer_p = 1;
7703 }
7704
7705 buffer = echo_area_buffer[this_one];
7706
7707 /* Don't get confused by reusing the buffer used for echoing
7708 for a different purpose. */
7709 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
7710 cancel_echoing ();
7711
7712 record_unwind_protect (unwind_with_echo_area_buffer,
7713 with_echo_area_buffer_unwind_data (w));
7714
7715 /* Make the echo area buffer current. Note that for display
7716 purposes, it is not necessary that the displayed window's buffer
7717 == current_buffer, except for text property lookup. So, let's
7718 only set that buffer temporarily here without doing a full
7719 Fset_window_buffer. We must also change w->pointm, though,
7720 because otherwise an assertions in unshow_buffer fails, and Emacs
7721 aborts. */
7722 set_buffer_internal_1 (XBUFFER (buffer));
7723 if (w)
7724 {
7725 w->buffer = buffer;
7726 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
7727 }
7728
7729 current_buffer->undo_list = Qt;
7730 current_buffer->read_only = Qnil;
7731 specbind (Qinhibit_read_only, Qt);
7732 specbind (Qinhibit_modification_hooks, Qt);
7733
7734 if (clear_buffer_p && Z > BEG)
7735 del_range (BEG, Z);
7736
7737 xassert (BEGV >= BEG);
7738 xassert (ZV <= Z && ZV >= BEGV);
7739
7740 rc = fn (a1, a2, a3, a4);
7741
7742 xassert (BEGV >= BEG);
7743 xassert (ZV <= Z && ZV >= BEGV);
7744
7745 unbind_to (count, Qnil);
7746 return rc;
7747 }
7748
7749
7750 /* Save state that should be preserved around the call to the function
7751 FN called in with_echo_area_buffer. */
7752
7753 static Lisp_Object
7754 with_echo_area_buffer_unwind_data (w)
7755 struct window *w;
7756 {
7757 int i = 0;
7758 Lisp_Object vector;
7759
7760 /* Reduce consing by keeping one vector in
7761 Vwith_echo_area_save_vector. */
7762 vector = Vwith_echo_area_save_vector;
7763 Vwith_echo_area_save_vector = Qnil;
7764
7765 if (NILP (vector))
7766 vector = Fmake_vector (make_number (7), Qnil);
7767
7768 XSETBUFFER (AREF (vector, i), current_buffer); ++i;
7769 AREF (vector, i) = Vdeactivate_mark, ++i;
7770 AREF (vector, i) = make_number (windows_or_buffers_changed), ++i;
7771
7772 if (w)
7773 {
7774 XSETWINDOW (AREF (vector, i), w); ++i;
7775 AREF (vector, i) = w->buffer; ++i;
7776 AREF (vector, i) = make_number (XMARKER (w->pointm)->charpos); ++i;
7777 AREF (vector, i) = make_number (XMARKER (w->pointm)->bytepos); ++i;
7778 }
7779 else
7780 {
7781 int end = i + 4;
7782 for (; i < end; ++i)
7783 AREF (vector, i) = Qnil;
7784 }
7785
7786 xassert (i == ASIZE (vector));
7787 return vector;
7788 }
7789
7790
7791 /* Restore global state from VECTOR which was created by
7792 with_echo_area_buffer_unwind_data. */
7793
7794 static Lisp_Object
7795 unwind_with_echo_area_buffer (vector)
7796 Lisp_Object vector;
7797 {
7798 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
7799 Vdeactivate_mark = AREF (vector, 1);
7800 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
7801
7802 if (WINDOWP (AREF (vector, 3)))
7803 {
7804 struct window *w;
7805 Lisp_Object buffer, charpos, bytepos;
7806
7807 w = XWINDOW (AREF (vector, 3));
7808 buffer = AREF (vector, 4);
7809 charpos = AREF (vector, 5);
7810 bytepos = AREF (vector, 6);
7811
7812 w->buffer = buffer;
7813 set_marker_both (w->pointm, buffer,
7814 XFASTINT (charpos), XFASTINT (bytepos));
7815 }
7816
7817 Vwith_echo_area_save_vector = vector;
7818 return Qnil;
7819 }
7820
7821
7822 /* Set up the echo area for use by print functions. MULTIBYTE_P
7823 non-zero means we will print multibyte. */
7824
7825 void
7826 setup_echo_area_for_printing (multibyte_p)
7827 int multibyte_p;
7828 {
7829 /* If we can't find an echo area any more, exit. */
7830 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
7831 Fkill_emacs (Qnil);
7832
7833 ensure_echo_area_buffers ();
7834
7835 if (!message_buf_print)
7836 {
7837 /* A message has been output since the last time we printed.
7838 Choose a fresh echo area buffer. */
7839 if (EQ (echo_area_buffer[1], echo_buffer[0]))
7840 echo_area_buffer[0] = echo_buffer[1];
7841 else
7842 echo_area_buffer[0] = echo_buffer[0];
7843
7844 /* Switch to that buffer and clear it. */
7845 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
7846 current_buffer->truncate_lines = Qnil;
7847
7848 if (Z > BEG)
7849 {
7850 int count = SPECPDL_INDEX ();
7851 specbind (Qinhibit_read_only, Qt);
7852 /* Note that undo recording is always disabled. */
7853 del_range (BEG, Z);
7854 unbind_to (count, Qnil);
7855 }
7856 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
7857
7858 /* Set up the buffer for the multibyteness we need. */
7859 if (multibyte_p
7860 != !NILP (current_buffer->enable_multibyte_characters))
7861 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
7862
7863 /* Raise the frame containing the echo area. */
7864 if (minibuffer_auto_raise)
7865 {
7866 struct frame *sf = SELECTED_FRAME ();
7867 Lisp_Object mini_window;
7868 mini_window = FRAME_MINIBUF_WINDOW (sf);
7869 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
7870 }
7871
7872 message_log_maybe_newline ();
7873 message_buf_print = 1;
7874 }
7875 else
7876 {
7877 if (NILP (echo_area_buffer[0]))
7878 {
7879 if (EQ (echo_area_buffer[1], echo_buffer[0]))
7880 echo_area_buffer[0] = echo_buffer[1];
7881 else
7882 echo_area_buffer[0] = echo_buffer[0];
7883 }
7884
7885 if (current_buffer != XBUFFER (echo_area_buffer[0]))
7886 {
7887 /* Someone switched buffers between print requests. */
7888 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
7889 current_buffer->truncate_lines = Qnil;
7890 }
7891 }
7892 }
7893
7894
7895 /* Display an echo area message in window W. Value is non-zero if W's
7896 height is changed. If display_last_displayed_message_p is
7897 non-zero, display the message that was last displayed, otherwise
7898 display the current message. */
7899
7900 static int
7901 display_echo_area (w)
7902 struct window *w;
7903 {
7904 int i, no_message_p, window_height_changed_p, count;
7905
7906 /* Temporarily disable garbage collections while displaying the echo
7907 area. This is done because a GC can print a message itself.
7908 That message would modify the echo area buffer's contents while a
7909 redisplay of the buffer is going on, and seriously confuse
7910 redisplay. */
7911 count = inhibit_garbage_collection ();
7912
7913 /* If there is no message, we must call display_echo_area_1
7914 nevertheless because it resizes the window. But we will have to
7915 reset the echo_area_buffer in question to nil at the end because
7916 with_echo_area_buffer will sets it to an empty buffer. */
7917 i = display_last_displayed_message_p ? 1 : 0;
7918 no_message_p = NILP (echo_area_buffer[i]);
7919
7920 window_height_changed_p
7921 = with_echo_area_buffer (w, display_last_displayed_message_p,
7922 display_echo_area_1,
7923 (EMACS_INT) w, Qnil, 0, 0);
7924
7925 if (no_message_p)
7926 echo_area_buffer[i] = Qnil;
7927
7928 unbind_to (count, Qnil);
7929 return window_height_changed_p;
7930 }
7931
7932
7933 /* Helper for display_echo_area. Display the current buffer which
7934 contains the current echo area message in window W, a mini-window,
7935 a pointer to which is passed in A1. A2..A4 are currently not used.
7936 Change the height of W so that all of the message is displayed.
7937 Value is non-zero if height of W was changed. */
7938
7939 static int
7940 display_echo_area_1 (a1, a2, a3, a4)
7941 EMACS_INT a1;
7942 Lisp_Object a2;
7943 EMACS_INT a3, a4;
7944 {
7945 struct window *w = (struct window *) a1;
7946 Lisp_Object window;
7947 struct text_pos start;
7948 int window_height_changed_p = 0;
7949
7950 /* Do this before displaying, so that we have a large enough glyph
7951 matrix for the display. If we can't get enough space for the
7952 whole text, display the last N lines. That works by setting w->start. */
7953 window_height_changed_p = resize_mini_window (w, 0);
7954
7955 /* Use the starting position chosen by resize_mini_window. */
7956 SET_TEXT_POS_FROM_MARKER (start, w->start);
7957
7958 /* Display. */
7959 clear_glyph_matrix (w->desired_matrix);
7960 XSETWINDOW (window, w);
7961 try_window (window, start, 0);
7962
7963 return window_height_changed_p;
7964 }
7965
7966
7967 /* Resize the echo area window to exactly the size needed for the
7968 currently displayed message, if there is one. If a mini-buffer
7969 is active, don't shrink it. */
7970
7971 void
7972 resize_echo_area_exactly ()
7973 {
7974 if (BUFFERP (echo_area_buffer[0])
7975 && WINDOWP (echo_area_window))
7976 {
7977 struct window *w = XWINDOW (echo_area_window);
7978 int resized_p;
7979 Lisp_Object resize_exactly;
7980
7981 if (minibuf_level == 0)
7982 resize_exactly = Qt;
7983 else
7984 resize_exactly = Qnil;
7985
7986 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
7987 (EMACS_INT) w, resize_exactly, 0, 0);
7988 if (resized_p)
7989 {
7990 ++windows_or_buffers_changed;
7991 ++update_mode_lines;
7992 redisplay_internal (0);
7993 }
7994 }
7995 }
7996
7997
7998 /* Callback function for with_echo_area_buffer, when used from
7999 resize_echo_area_exactly. A1 contains a pointer to the window to
8000 resize, EXACTLY non-nil means resize the mini-window exactly to the
8001 size of the text displayed. A3 and A4 are not used. Value is what
8002 resize_mini_window returns. */
8003
8004 static int
8005 resize_mini_window_1 (a1, exactly, a3, a4)
8006 EMACS_INT a1;
8007 Lisp_Object exactly;
8008 EMACS_INT a3, a4;
8009 {
8010 return resize_mini_window ((struct window *) a1, !NILP (exactly));
8011 }
8012
8013
8014 /* Resize mini-window W to fit the size of its contents. EXACT:P
8015 means size the window exactly to the size needed. Otherwise, it's
8016 only enlarged until W's buffer is empty.
8017
8018 Set W->start to the right place to begin display. If the whole
8019 contents fit, start at the beginning. Otherwise, start so as
8020 to make the end of the contents appear. This is particularly
8021 important for y-or-n-p, but seems desirable generally.
8022
8023 Value is non-zero if the window height has been changed. */
8024
8025 int
8026 resize_mini_window (w, exact_p)
8027 struct window *w;
8028 int exact_p;
8029 {
8030 struct frame *f = XFRAME (w->frame);
8031 int window_height_changed_p = 0;
8032
8033 xassert (MINI_WINDOW_P (w));
8034
8035 /* By default, start display at the beginning. */
8036 set_marker_both (w->start, w->buffer,
8037 BUF_BEGV (XBUFFER (w->buffer)),
8038 BUF_BEGV_BYTE (XBUFFER (w->buffer)));
8039
8040 /* Don't resize windows while redisplaying a window; it would
8041 confuse redisplay functions when the size of the window they are
8042 displaying changes from under them. Such a resizing can happen,
8043 for instance, when which-func prints a long message while
8044 we are running fontification-functions. We're running these
8045 functions with safe_call which binds inhibit-redisplay to t. */
8046 if (!NILP (Vinhibit_redisplay))
8047 return 0;
8048
8049 /* Nil means don't try to resize. */
8050 if (NILP (Vresize_mini_windows)
8051 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
8052 return 0;
8053
8054 if (!FRAME_MINIBUF_ONLY_P (f))
8055 {
8056 struct it it;
8057 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
8058 int total_height = WINDOW_TOTAL_LINES (root) + WINDOW_TOTAL_LINES (w);
8059 int height, max_height;
8060 int unit = FRAME_LINE_HEIGHT (f);
8061 struct text_pos start;
8062 struct buffer *old_current_buffer = NULL;
8063
8064 if (current_buffer != XBUFFER (w->buffer))
8065 {
8066 old_current_buffer = current_buffer;
8067 set_buffer_internal (XBUFFER (w->buffer));
8068 }
8069
8070 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
8071
8072 /* Compute the max. number of lines specified by the user. */
8073 if (FLOATP (Vmax_mini_window_height))
8074 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_LINES (f);
8075 else if (INTEGERP (Vmax_mini_window_height))
8076 max_height = XINT (Vmax_mini_window_height);
8077 else
8078 max_height = total_height / 4;
8079
8080 /* Correct that max. height if it's bogus. */
8081 max_height = max (1, max_height);
8082 max_height = min (total_height, max_height);
8083
8084 /* Find out the height of the text in the window. */
8085 if (it.truncate_lines_p)
8086 height = 1;
8087 else
8088 {
8089 last_height = 0;
8090 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
8091 if (it.max_ascent == 0 && it.max_descent == 0)
8092 height = it.current_y + last_height;
8093 else
8094 height = it.current_y + it.max_ascent + it.max_descent;
8095 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
8096 height = (height + unit - 1) / unit;
8097 }
8098
8099 /* Compute a suitable window start. */
8100 if (height > max_height)
8101 {
8102 height = max_height;
8103 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
8104 move_it_vertically_backward (&it, (height - 1) * unit);
8105 start = it.current.pos;
8106 }
8107 else
8108 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
8109 SET_MARKER_FROM_TEXT_POS (w->start, start);
8110
8111 if (EQ (Vresize_mini_windows, Qgrow_only))
8112 {
8113 /* Let it grow only, until we display an empty message, in which
8114 case the window shrinks again. */
8115 if (height > WINDOW_TOTAL_LINES (w))
8116 {
8117 int old_height = WINDOW_TOTAL_LINES (w);
8118 freeze_window_starts (f, 1);
8119 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
8120 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8121 }
8122 else if (height < WINDOW_TOTAL_LINES (w)
8123 && (exact_p || BEGV == ZV))
8124 {
8125 int old_height = WINDOW_TOTAL_LINES (w);
8126 freeze_window_starts (f, 0);
8127 shrink_mini_window (w);
8128 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8129 }
8130 }
8131 else
8132 {
8133 /* Always resize to exact size needed. */
8134 if (height > WINDOW_TOTAL_LINES (w))
8135 {
8136 int old_height = WINDOW_TOTAL_LINES (w);
8137 freeze_window_starts (f, 1);
8138 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
8139 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8140 }
8141 else if (height < WINDOW_TOTAL_LINES (w))
8142 {
8143 int old_height = WINDOW_TOTAL_LINES (w);
8144 freeze_window_starts (f, 0);
8145 shrink_mini_window (w);
8146
8147 if (height)
8148 {
8149 freeze_window_starts (f, 1);
8150 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
8151 }
8152
8153 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8154 }
8155 }
8156
8157 if (old_current_buffer)
8158 set_buffer_internal (old_current_buffer);
8159 }
8160
8161 return window_height_changed_p;
8162 }
8163
8164
8165 /* Value is the current message, a string, or nil if there is no
8166 current message. */
8167
8168 Lisp_Object
8169 current_message ()
8170 {
8171 Lisp_Object msg;
8172
8173 if (NILP (echo_area_buffer[0]))
8174 msg = Qnil;
8175 else
8176 {
8177 with_echo_area_buffer (0, 0, current_message_1,
8178 (EMACS_INT) &msg, Qnil, 0, 0);
8179 if (NILP (msg))
8180 echo_area_buffer[0] = Qnil;
8181 }
8182
8183 return msg;
8184 }
8185
8186
8187 static int
8188 current_message_1 (a1, a2, a3, a4)
8189 EMACS_INT a1;
8190 Lisp_Object a2;
8191 EMACS_INT a3, a4;
8192 {
8193 Lisp_Object *msg = (Lisp_Object *) a1;
8194
8195 if (Z > BEG)
8196 *msg = make_buffer_string (BEG, Z, 1);
8197 else
8198 *msg = Qnil;
8199 return 0;
8200 }
8201
8202
8203 /* Push the current message on Vmessage_stack for later restauration
8204 by restore_message. Value is non-zero if the current message isn't
8205 empty. This is a relatively infrequent operation, so it's not
8206 worth optimizing. */
8207
8208 int
8209 push_message ()
8210 {
8211 Lisp_Object msg;
8212 msg = current_message ();
8213 Vmessage_stack = Fcons (msg, Vmessage_stack);
8214 return STRINGP (msg);
8215 }
8216
8217
8218 /* Restore message display from the top of Vmessage_stack. */
8219
8220 void
8221 restore_message ()
8222 {
8223 Lisp_Object msg;
8224
8225 xassert (CONSP (Vmessage_stack));
8226 msg = XCAR (Vmessage_stack);
8227 if (STRINGP (msg))
8228 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
8229 else
8230 message3_nolog (msg, 0, 0);
8231 }
8232
8233
8234 /* Handler for record_unwind_protect calling pop_message. */
8235
8236 Lisp_Object
8237 pop_message_unwind (dummy)
8238 Lisp_Object dummy;
8239 {
8240 pop_message ();
8241 return Qnil;
8242 }
8243
8244 /* Pop the top-most entry off Vmessage_stack. */
8245
8246 void
8247 pop_message ()
8248 {
8249 xassert (CONSP (Vmessage_stack));
8250 Vmessage_stack = XCDR (Vmessage_stack);
8251 }
8252
8253
8254 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
8255 exits. If the stack is not empty, we have a missing pop_message
8256 somewhere. */
8257
8258 void
8259 check_message_stack ()
8260 {
8261 if (!NILP (Vmessage_stack))
8262 abort ();
8263 }
8264
8265
8266 /* Truncate to NCHARS what will be displayed in the echo area the next
8267 time we display it---but don't redisplay it now. */
8268
8269 void
8270 truncate_echo_area (nchars)
8271 int nchars;
8272 {
8273 if (nchars == 0)
8274 echo_area_buffer[0] = Qnil;
8275 /* A null message buffer means that the frame hasn't really been
8276 initialized yet. Error messages get reported properly by
8277 cmd_error, so this must be just an informative message; toss it. */
8278 else if (!noninteractive
8279 && INTERACTIVE
8280 && !NILP (echo_area_buffer[0]))
8281 {
8282 struct frame *sf = SELECTED_FRAME ();
8283 if (FRAME_MESSAGE_BUF (sf))
8284 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
8285 }
8286 }
8287
8288
8289 /* Helper function for truncate_echo_area. Truncate the current
8290 message to at most NCHARS characters. */
8291
8292 static int
8293 truncate_message_1 (nchars, a2, a3, a4)
8294 EMACS_INT nchars;
8295 Lisp_Object a2;
8296 EMACS_INT a3, a4;
8297 {
8298 if (BEG + nchars < Z)
8299 del_range (BEG + nchars, Z);
8300 if (Z == BEG)
8301 echo_area_buffer[0] = Qnil;
8302 return 0;
8303 }
8304
8305
8306 /* Set the current message to a substring of S or STRING.
8307
8308 If STRING is a Lisp string, set the message to the first NBYTES
8309 bytes from STRING. NBYTES zero means use the whole string. If
8310 STRING is multibyte, the message will be displayed multibyte.
8311
8312 If S is not null, set the message to the first LEN bytes of S. LEN
8313 zero means use the whole string. MULTIBYTE_P non-zero means S is
8314 multibyte. Display the message multibyte in that case.
8315
8316 Doesn't GC, as with_echo_area_buffer binds Qinhibit_modification_hooks
8317 to t before calling set_message_1 (which calls insert).
8318 */
8319
8320 void
8321 set_message (s, string, nbytes, multibyte_p)
8322 const char *s;
8323 Lisp_Object string;
8324 int nbytes, multibyte_p;
8325 {
8326 message_enable_multibyte
8327 = ((s && multibyte_p)
8328 || (STRINGP (string) && STRING_MULTIBYTE (string)));
8329
8330 with_echo_area_buffer (0, -1, set_message_1,
8331 (EMACS_INT) s, string, nbytes, multibyte_p);
8332 message_buf_print = 0;
8333 help_echo_showing_p = 0;
8334 }
8335
8336
8337 /* Helper function for set_message. Arguments have the same meaning
8338 as there, with A1 corresponding to S and A2 corresponding to STRING
8339 This function is called with the echo area buffer being
8340 current. */
8341
8342 static int
8343 set_message_1 (a1, a2, nbytes, multibyte_p)
8344 EMACS_INT a1;
8345 Lisp_Object a2;
8346 EMACS_INT nbytes, multibyte_p;
8347 {
8348 const char *s = (const char *) a1;
8349 Lisp_Object string = a2;
8350
8351 /* Change multibyteness of the echo buffer appropriately. */
8352 if (message_enable_multibyte
8353 != !NILP (current_buffer->enable_multibyte_characters))
8354 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
8355
8356 current_buffer->truncate_lines = message_truncate_lines ? Qt : Qnil;
8357
8358 /* Insert new message at BEG. */
8359 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
8360
8361 if (STRINGP (string))
8362 {
8363 int nchars;
8364
8365 if (nbytes == 0)
8366 nbytes = SBYTES (string);
8367 nchars = string_byte_to_char (string, nbytes);
8368
8369 /* This function takes care of single/multibyte conversion. We
8370 just have to ensure that the echo area buffer has the right
8371 setting of enable_multibyte_characters. */
8372 insert_from_string (string, 0, 0, nchars, nbytes, 1);
8373 }
8374 else if (s)
8375 {
8376 if (nbytes == 0)
8377 nbytes = strlen (s);
8378
8379 if (multibyte_p && NILP (current_buffer->enable_multibyte_characters))
8380 {
8381 /* Convert from multi-byte to single-byte. */
8382 int i, c, n;
8383 unsigned char work[1];
8384
8385 /* Convert a multibyte string to single-byte. */
8386 for (i = 0; i < nbytes; i += n)
8387 {
8388 c = string_char_and_length (s + i, nbytes - i, &n);
8389 work[0] = (SINGLE_BYTE_CHAR_P (c)
8390 ? c
8391 : multibyte_char_to_unibyte (c, Qnil));
8392 insert_1_both (work, 1, 1, 1, 0, 0);
8393 }
8394 }
8395 else if (!multibyte_p
8396 && !NILP (current_buffer->enable_multibyte_characters))
8397 {
8398 /* Convert from single-byte to multi-byte. */
8399 int i, c, n;
8400 const unsigned char *msg = (const unsigned char *) s;
8401 unsigned char str[MAX_MULTIBYTE_LENGTH];
8402
8403 /* Convert a single-byte string to multibyte. */
8404 for (i = 0; i < nbytes; i++)
8405 {
8406 c = unibyte_char_to_multibyte (msg[i]);
8407 n = CHAR_STRING (c, str);
8408 insert_1_both (str, 1, n, 1, 0, 0);
8409 }
8410 }
8411 else
8412 insert_1 (s, nbytes, 1, 0, 0);
8413 }
8414
8415 return 0;
8416 }
8417
8418
8419 /* Clear messages. CURRENT_P non-zero means clear the current
8420 message. LAST_DISPLAYED_P non-zero means clear the message
8421 last displayed. */
8422
8423 void
8424 clear_message (current_p, last_displayed_p)
8425 int current_p, last_displayed_p;
8426 {
8427 if (current_p)
8428 {
8429 echo_area_buffer[0] = Qnil;
8430 message_cleared_p = 1;
8431 }
8432
8433 if (last_displayed_p)
8434 echo_area_buffer[1] = Qnil;
8435
8436 message_buf_print = 0;
8437 }
8438
8439 /* Clear garbaged frames.
8440
8441 This function is used where the old redisplay called
8442 redraw_garbaged_frames which in turn called redraw_frame which in
8443 turn called clear_frame. The call to clear_frame was a source of
8444 flickering. I believe a clear_frame is not necessary. It should
8445 suffice in the new redisplay to invalidate all current matrices,
8446 and ensure a complete redisplay of all windows. */
8447
8448 static void
8449 clear_garbaged_frames ()
8450 {
8451 if (frame_garbaged)
8452 {
8453 Lisp_Object tail, frame;
8454 int changed_count = 0;
8455
8456 FOR_EACH_FRAME (tail, frame)
8457 {
8458 struct frame *f = XFRAME (frame);
8459
8460 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
8461 {
8462 if (f->resized_p)
8463 {
8464 Fredraw_frame (frame);
8465 f->force_flush_display_p = 1;
8466 }
8467 clear_current_matrices (f);
8468 changed_count++;
8469 f->garbaged = 0;
8470 f->resized_p = 0;
8471 }
8472 }
8473
8474 frame_garbaged = 0;
8475 if (changed_count)
8476 ++windows_or_buffers_changed;
8477 }
8478 }
8479
8480
8481 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
8482 is non-zero update selected_frame. Value is non-zero if the
8483 mini-windows height has been changed. */
8484
8485 static int
8486 echo_area_display (update_frame_p)
8487 int update_frame_p;
8488 {
8489 Lisp_Object mini_window;
8490 struct window *w;
8491 struct frame *f;
8492 int window_height_changed_p = 0;
8493 struct frame *sf = SELECTED_FRAME ();
8494
8495 mini_window = FRAME_MINIBUF_WINDOW (sf);
8496 w = XWINDOW (mini_window);
8497 f = XFRAME (WINDOW_FRAME (w));
8498
8499 /* Don't display if frame is invisible or not yet initialized. */
8500 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
8501 return 0;
8502
8503 /* The terminal frame is used as the first Emacs frame on the Mac OS. */
8504 #ifndef MAC_OS8
8505 #ifdef HAVE_WINDOW_SYSTEM
8506 /* When Emacs starts, selected_frame may be the initial terminal
8507 frame. If we let this through, a message would be displayed on
8508 the terminal. */
8509 if (FRAME_INITIAL_P (XFRAME (selected_frame)))
8510 return 0;
8511 #endif /* HAVE_WINDOW_SYSTEM */
8512 #endif
8513
8514 /* Redraw garbaged frames. */
8515 if (frame_garbaged)
8516 clear_garbaged_frames ();
8517
8518 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
8519 {
8520 echo_area_window = mini_window;
8521 window_height_changed_p = display_echo_area (w);
8522 w->must_be_updated_p = 1;
8523
8524 /* Update the display, unless called from redisplay_internal.
8525 Also don't update the screen during redisplay itself. The
8526 update will happen at the end of redisplay, and an update
8527 here could cause confusion. */
8528 if (update_frame_p && !redisplaying_p)
8529 {
8530 int n = 0;
8531
8532 /* If the display update has been interrupted by pending
8533 input, update mode lines in the frame. Due to the
8534 pending input, it might have been that redisplay hasn't
8535 been called, so that mode lines above the echo area are
8536 garbaged. This looks odd, so we prevent it here. */
8537 if (!display_completed)
8538 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
8539
8540 if (window_height_changed_p
8541 /* Don't do this if Emacs is shutting down. Redisplay
8542 needs to run hooks. */
8543 && !NILP (Vrun_hooks))
8544 {
8545 /* Must update other windows. Likewise as in other
8546 cases, don't let this update be interrupted by
8547 pending input. */
8548 int count = SPECPDL_INDEX ();
8549 specbind (Qredisplay_dont_pause, Qt);
8550 windows_or_buffers_changed = 1;
8551 redisplay_internal (0);
8552 unbind_to (count, Qnil);
8553 }
8554 else if (FRAME_WINDOW_P (f) && n == 0)
8555 {
8556 /* Window configuration is the same as before.
8557 Can do with a display update of the echo area,
8558 unless we displayed some mode lines. */
8559 update_single_window (w, 1);
8560 FRAME_RIF (f)->flush_display (f);
8561 }
8562 else
8563 update_frame (f, 1, 1);
8564
8565 /* If cursor is in the echo area, make sure that the next
8566 redisplay displays the minibuffer, so that the cursor will
8567 be replaced with what the minibuffer wants. */
8568 if (cursor_in_echo_area)
8569 ++windows_or_buffers_changed;
8570 }
8571 }
8572 else if (!EQ (mini_window, selected_window))
8573 windows_or_buffers_changed++;
8574
8575 /* Last displayed message is now the current message. */
8576 echo_area_buffer[1] = echo_area_buffer[0];
8577 /* Inform read_char that we're not echoing. */
8578 echo_message_buffer = Qnil;
8579
8580 /* Prevent redisplay optimization in redisplay_internal by resetting
8581 this_line_start_pos. This is done because the mini-buffer now
8582 displays the message instead of its buffer text. */
8583 if (EQ (mini_window, selected_window))
8584 CHARPOS (this_line_start_pos) = 0;
8585
8586 return window_height_changed_p;
8587 }
8588
8589
8590 \f
8591 /***********************************************************************
8592 Mode Lines and Frame Titles
8593 ***********************************************************************/
8594
8595 /* A buffer for constructing non-propertized mode-line strings and
8596 frame titles in it; allocated from the heap in init_xdisp and
8597 resized as needed in store_mode_line_noprop_char. */
8598
8599 static char *mode_line_noprop_buf;
8600
8601 /* The buffer's end, and a current output position in it. */
8602
8603 static char *mode_line_noprop_buf_end;
8604 static char *mode_line_noprop_ptr;
8605
8606 #define MODE_LINE_NOPROP_LEN(start) \
8607 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
8608
8609 static enum {
8610 MODE_LINE_DISPLAY = 0,
8611 MODE_LINE_TITLE,
8612 MODE_LINE_NOPROP,
8613 MODE_LINE_STRING
8614 } mode_line_target;
8615
8616 /* Alist that caches the results of :propertize.
8617 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
8618 static Lisp_Object mode_line_proptrans_alist;
8619
8620 /* List of strings making up the mode-line. */
8621 static Lisp_Object mode_line_string_list;
8622
8623 /* Base face property when building propertized mode line string. */
8624 static Lisp_Object mode_line_string_face;
8625 static Lisp_Object mode_line_string_face_prop;
8626
8627
8628 /* Unwind data for mode line strings */
8629
8630 static Lisp_Object Vmode_line_unwind_vector;
8631
8632 static Lisp_Object
8633 format_mode_line_unwind_data (obuf, save_proptrans)
8634 struct buffer *obuf;
8635 {
8636 Lisp_Object vector;
8637
8638 /* Reduce consing by keeping one vector in
8639 Vwith_echo_area_save_vector. */
8640 vector = Vmode_line_unwind_vector;
8641 Vmode_line_unwind_vector = Qnil;
8642
8643 if (NILP (vector))
8644 vector = Fmake_vector (make_number (7), Qnil);
8645
8646 AREF (vector, 0) = make_number (mode_line_target);
8647 AREF (vector, 1) = make_number (MODE_LINE_NOPROP_LEN (0));
8648 AREF (vector, 2) = mode_line_string_list;
8649 AREF (vector, 3) = (save_proptrans ? mode_line_proptrans_alist : Qt);
8650 AREF (vector, 4) = mode_line_string_face;
8651 AREF (vector, 5) = mode_line_string_face_prop;
8652
8653 if (obuf)
8654 XSETBUFFER (AREF (vector, 6), obuf);
8655 else
8656 AREF (vector, 6) = Qnil;
8657
8658 return vector;
8659 }
8660
8661 static Lisp_Object
8662 unwind_format_mode_line (vector)
8663 Lisp_Object vector;
8664 {
8665 mode_line_target = XINT (AREF (vector, 0));
8666 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
8667 mode_line_string_list = AREF (vector, 2);
8668 if (! EQ (AREF (vector, 3), Qt))
8669 mode_line_proptrans_alist = AREF (vector, 3);
8670 mode_line_string_face = AREF (vector, 4);
8671 mode_line_string_face_prop = AREF (vector, 5);
8672
8673 if (!NILP (AREF (vector, 6)))
8674 {
8675 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
8676 AREF (vector, 6) = Qnil;
8677 }
8678
8679 Vmode_line_unwind_vector = vector;
8680 return Qnil;
8681 }
8682
8683
8684 /* Store a single character C for the frame title in mode_line_noprop_buf.
8685 Re-allocate mode_line_noprop_buf if necessary. */
8686
8687 static void
8688 #ifdef PROTOTYPES
8689 store_mode_line_noprop_char (char c)
8690 #else
8691 store_mode_line_noprop_char (c)
8692 char c;
8693 #endif
8694 {
8695 /* If output position has reached the end of the allocated buffer,
8696 double the buffer's size. */
8697 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
8698 {
8699 int len = MODE_LINE_NOPROP_LEN (0);
8700 int new_size = 2 * len * sizeof *mode_line_noprop_buf;
8701 mode_line_noprop_buf = (char *) xrealloc (mode_line_noprop_buf, new_size);
8702 mode_line_noprop_buf_end = mode_line_noprop_buf + new_size;
8703 mode_line_noprop_ptr = mode_line_noprop_buf + len;
8704 }
8705
8706 *mode_line_noprop_ptr++ = c;
8707 }
8708
8709
8710 /* Store part of a frame title in mode_line_noprop_buf, beginning at
8711 mode_line_noprop_ptr. STR is the string to store. Do not copy
8712 characters that yield more columns than PRECISION; PRECISION <= 0
8713 means copy the whole string. Pad with spaces until FIELD_WIDTH
8714 number of characters have been copied; FIELD_WIDTH <= 0 means don't
8715 pad. Called from display_mode_element when it is used to build a
8716 frame title. */
8717
8718 static int
8719 store_mode_line_noprop (str, field_width, precision)
8720 const unsigned char *str;
8721 int field_width, precision;
8722 {
8723 int n = 0;
8724 int dummy, nbytes;
8725
8726 /* Copy at most PRECISION chars from STR. */
8727 nbytes = strlen (str);
8728 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
8729 while (nbytes--)
8730 store_mode_line_noprop_char (*str++);
8731
8732 /* Fill up with spaces until FIELD_WIDTH reached. */
8733 while (field_width > 0
8734 && n < field_width)
8735 {
8736 store_mode_line_noprop_char (' ');
8737 ++n;
8738 }
8739
8740 return n;
8741 }
8742
8743 /***********************************************************************
8744 Frame Titles
8745 ***********************************************************************/
8746
8747 #ifdef HAVE_WINDOW_SYSTEM
8748
8749 /* Set the title of FRAME, if it has changed. The title format is
8750 Vicon_title_format if FRAME is iconified, otherwise it is
8751 frame_title_format. */
8752
8753 static void
8754 x_consider_frame_title (frame)
8755 Lisp_Object frame;
8756 {
8757 struct frame *f = XFRAME (frame);
8758
8759 if (FRAME_WINDOW_P (f)
8760 || FRAME_MINIBUF_ONLY_P (f)
8761 || f->explicit_name)
8762 {
8763 /* Do we have more than one visible frame on this X display? */
8764 Lisp_Object tail;
8765 Lisp_Object fmt;
8766 int title_start;
8767 char *title;
8768 int len;
8769 struct it it;
8770 int count = SPECPDL_INDEX ();
8771
8772 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
8773 {
8774 Lisp_Object other_frame = XCAR (tail);
8775 struct frame *tf = XFRAME (other_frame);
8776
8777 if (tf != f
8778 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
8779 && !FRAME_MINIBUF_ONLY_P (tf)
8780 && !EQ (other_frame, tip_frame)
8781 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
8782 break;
8783 }
8784
8785 /* Set global variable indicating that multiple frames exist. */
8786 multiple_frames = CONSP (tail);
8787
8788 /* Switch to the buffer of selected window of the frame. Set up
8789 mode_line_target so that display_mode_element will output into
8790 mode_line_noprop_buf; then display the title. */
8791 record_unwind_protect (unwind_format_mode_line,
8792 format_mode_line_unwind_data (current_buffer, 0));
8793
8794 set_buffer_internal_1 (XBUFFER (XWINDOW (f->selected_window)->buffer));
8795 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
8796
8797 mode_line_target = MODE_LINE_TITLE;
8798 title_start = MODE_LINE_NOPROP_LEN (0);
8799 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
8800 NULL, DEFAULT_FACE_ID);
8801 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
8802 len = MODE_LINE_NOPROP_LEN (title_start);
8803 title = mode_line_noprop_buf + title_start;
8804 unbind_to (count, Qnil);
8805
8806 /* Set the title only if it's changed. This avoids consing in
8807 the common case where it hasn't. (If it turns out that we've
8808 already wasted too much time by walking through the list with
8809 display_mode_element, then we might need to optimize at a
8810 higher level than this.) */
8811 if (! STRINGP (f->name)
8812 || SBYTES (f->name) != len
8813 || bcmp (title, SDATA (f->name), len) != 0)
8814 x_implicitly_set_name (f, make_string (title, len), Qnil);
8815 }
8816 }
8817
8818 #endif /* not HAVE_WINDOW_SYSTEM */
8819
8820
8821
8822 \f
8823 /***********************************************************************
8824 Menu Bars
8825 ***********************************************************************/
8826
8827
8828 /* Prepare for redisplay by updating menu-bar item lists when
8829 appropriate. This can call eval. */
8830
8831 void
8832 prepare_menu_bars ()
8833 {
8834 int all_windows;
8835 struct gcpro gcpro1, gcpro2;
8836 struct frame *f;
8837 Lisp_Object tooltip_frame;
8838
8839 #ifdef HAVE_WINDOW_SYSTEM
8840 tooltip_frame = tip_frame;
8841 #else
8842 tooltip_frame = Qnil;
8843 #endif
8844
8845 /* Update all frame titles based on their buffer names, etc. We do
8846 this before the menu bars so that the buffer-menu will show the
8847 up-to-date frame titles. */
8848 #ifdef HAVE_WINDOW_SYSTEM
8849 if (windows_or_buffers_changed || update_mode_lines)
8850 {
8851 Lisp_Object tail, frame;
8852
8853 FOR_EACH_FRAME (tail, frame)
8854 {
8855 f = XFRAME (frame);
8856 if (!EQ (frame, tooltip_frame)
8857 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
8858 x_consider_frame_title (frame);
8859 }
8860 }
8861 #endif /* HAVE_WINDOW_SYSTEM */
8862
8863 /* Update the menu bar item lists, if appropriate. This has to be
8864 done before any actual redisplay or generation of display lines. */
8865 all_windows = (update_mode_lines
8866 || buffer_shared > 1
8867 || windows_or_buffers_changed);
8868 if (all_windows)
8869 {
8870 Lisp_Object tail, frame;
8871 int count = SPECPDL_INDEX ();
8872
8873 record_unwind_save_match_data ();
8874
8875 FOR_EACH_FRAME (tail, frame)
8876 {
8877 f = XFRAME (frame);
8878
8879 /* Ignore tooltip frame. */
8880 if (EQ (frame, tooltip_frame))
8881 continue;
8882
8883 /* If a window on this frame changed size, report that to
8884 the user and clear the size-change flag. */
8885 if (FRAME_WINDOW_SIZES_CHANGED (f))
8886 {
8887 Lisp_Object functions;
8888
8889 /* Clear flag first in case we get an error below. */
8890 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
8891 functions = Vwindow_size_change_functions;
8892 GCPRO2 (tail, functions);
8893
8894 while (CONSP (functions))
8895 {
8896 call1 (XCAR (functions), frame);
8897 functions = XCDR (functions);
8898 }
8899 UNGCPRO;
8900 }
8901
8902 GCPRO1 (tail);
8903 update_menu_bar (f, 0);
8904 #ifdef HAVE_WINDOW_SYSTEM
8905 update_tool_bar (f, 0);
8906 #endif
8907 UNGCPRO;
8908 }
8909
8910 unbind_to (count, Qnil);
8911 }
8912 else
8913 {
8914 struct frame *sf = SELECTED_FRAME ();
8915 update_menu_bar (sf, 1);
8916 #ifdef HAVE_WINDOW_SYSTEM
8917 update_tool_bar (sf, 1);
8918 #endif
8919 }
8920
8921 /* Motif needs this. See comment in xmenu.c. Turn it off when
8922 pending_menu_activation is not defined. */
8923 #ifdef USE_X_TOOLKIT
8924 pending_menu_activation = 0;
8925 #endif
8926 }
8927
8928
8929 /* Update the menu bar item list for frame F. This has to be done
8930 before we start to fill in any display lines, because it can call
8931 eval.
8932
8933 If SAVE_MATCH_DATA is non-zero, we must save and restore it here. */
8934
8935 static void
8936 update_menu_bar (f, save_match_data)
8937 struct frame *f;
8938 int save_match_data;
8939 {
8940 Lisp_Object window;
8941 register struct window *w;
8942
8943 /* If called recursively during a menu update, do nothing. This can
8944 happen when, for instance, an activate-menubar-hook causes a
8945 redisplay. */
8946 if (inhibit_menubar_update)
8947 return;
8948
8949 window = FRAME_SELECTED_WINDOW (f);
8950 w = XWINDOW (window);
8951
8952 #if 0 /* The if statement below this if statement used to include the
8953 condition !NILP (w->update_mode_line), rather than using
8954 update_mode_lines directly, and this if statement may have
8955 been added to make that condition work. Now the if
8956 statement below matches its comment, this isn't needed. */
8957 if (update_mode_lines)
8958 w->update_mode_line = Qt;
8959 #endif
8960
8961 if (FRAME_WINDOW_P (f)
8962 ?
8963 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
8964 || defined (USE_GTK)
8965 FRAME_EXTERNAL_MENU_BAR (f)
8966 #else
8967 FRAME_MENU_BAR_LINES (f) > 0
8968 #endif
8969 : FRAME_MENU_BAR_LINES (f) > 0)
8970 {
8971 /* If the user has switched buffers or windows, we need to
8972 recompute to reflect the new bindings. But we'll
8973 recompute when update_mode_lines is set too; that means
8974 that people can use force-mode-line-update to request
8975 that the menu bar be recomputed. The adverse effect on
8976 the rest of the redisplay algorithm is about the same as
8977 windows_or_buffers_changed anyway. */
8978 if (windows_or_buffers_changed
8979 /* This used to test w->update_mode_line, but we believe
8980 there is no need to recompute the menu in that case. */
8981 || update_mode_lines
8982 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
8983 < BUF_MODIFF (XBUFFER (w->buffer)))
8984 != !NILP (w->last_had_star))
8985 || ((!NILP (Vtransient_mark_mode)
8986 && !NILP (XBUFFER (w->buffer)->mark_active))
8987 != !NILP (w->region_showing)))
8988 {
8989 struct buffer *prev = current_buffer;
8990 int count = SPECPDL_INDEX ();
8991
8992 specbind (Qinhibit_menubar_update, Qt);
8993
8994 set_buffer_internal_1 (XBUFFER (w->buffer));
8995 if (save_match_data)
8996 record_unwind_save_match_data ();
8997 if (NILP (Voverriding_local_map_menu_flag))
8998 {
8999 specbind (Qoverriding_terminal_local_map, Qnil);
9000 specbind (Qoverriding_local_map, Qnil);
9001 }
9002
9003 /* Run the Lucid hook. */
9004 safe_run_hooks (Qactivate_menubar_hook);
9005
9006 /* If it has changed current-menubar from previous value,
9007 really recompute the menu-bar from the value. */
9008 if (! NILP (Vlucid_menu_bar_dirty_flag))
9009 call0 (Qrecompute_lucid_menubar);
9010
9011 safe_run_hooks (Qmenu_bar_update_hook);
9012 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
9013
9014 /* Redisplay the menu bar in case we changed it. */
9015 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
9016 || defined (USE_GTK)
9017 if (FRAME_WINDOW_P (f)
9018 #if defined (MAC_OS)
9019 /* All frames on Mac OS share the same menubar. So only the
9020 selected frame should be allowed to set it. */
9021 && f == SELECTED_FRAME ()
9022 #endif
9023 )
9024 set_frame_menubar (f, 0, 0);
9025 else
9026 /* On a terminal screen, the menu bar is an ordinary screen
9027 line, and this makes it get updated. */
9028 w->update_mode_line = Qt;
9029 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || MAC_OS || USE_GTK) */
9030 /* In the non-toolkit version, the menu bar is an ordinary screen
9031 line, and this makes it get updated. */
9032 w->update_mode_line = Qt;
9033 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || MAC_OS || USE_GTK) */
9034
9035 unbind_to (count, Qnil);
9036 set_buffer_internal_1 (prev);
9037 }
9038 }
9039 }
9040
9041
9042 \f
9043 /***********************************************************************
9044 Output Cursor
9045 ***********************************************************************/
9046
9047 #ifdef HAVE_WINDOW_SYSTEM
9048
9049 /* EXPORT:
9050 Nominal cursor position -- where to draw output.
9051 HPOS and VPOS are window relative glyph matrix coordinates.
9052 X and Y are window relative pixel coordinates. */
9053
9054 struct cursor_pos output_cursor;
9055
9056
9057 /* EXPORT:
9058 Set the global variable output_cursor to CURSOR. All cursor
9059 positions are relative to updated_window. */
9060
9061 void
9062 set_output_cursor (cursor)
9063 struct cursor_pos *cursor;
9064 {
9065 output_cursor.hpos = cursor->hpos;
9066 output_cursor.vpos = cursor->vpos;
9067 output_cursor.x = cursor->x;
9068 output_cursor.y = cursor->y;
9069 }
9070
9071
9072 /* EXPORT for RIF:
9073 Set a nominal cursor position.
9074
9075 HPOS and VPOS are column/row positions in a window glyph matrix. X
9076 and Y are window text area relative pixel positions.
9077
9078 If this is done during an update, updated_window will contain the
9079 window that is being updated and the position is the future output
9080 cursor position for that window. If updated_window is null, use
9081 selected_window and display the cursor at the given position. */
9082
9083 void
9084 x_cursor_to (vpos, hpos, y, x)
9085 int vpos, hpos, y, x;
9086 {
9087 struct window *w;
9088
9089 /* If updated_window is not set, work on selected_window. */
9090 if (updated_window)
9091 w = updated_window;
9092 else
9093 w = XWINDOW (selected_window);
9094
9095 /* Set the output cursor. */
9096 output_cursor.hpos = hpos;
9097 output_cursor.vpos = vpos;
9098 output_cursor.x = x;
9099 output_cursor.y = y;
9100
9101 /* If not called as part of an update, really display the cursor.
9102 This will also set the cursor position of W. */
9103 if (updated_window == NULL)
9104 {
9105 BLOCK_INPUT;
9106 display_and_set_cursor (w, 1, hpos, vpos, x, y);
9107 if (FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
9108 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (SELECTED_FRAME ());
9109 UNBLOCK_INPUT;
9110 }
9111 }
9112
9113 #endif /* HAVE_WINDOW_SYSTEM */
9114
9115 \f
9116 /***********************************************************************
9117 Tool-bars
9118 ***********************************************************************/
9119
9120 #ifdef HAVE_WINDOW_SYSTEM
9121
9122 /* Where the mouse was last time we reported a mouse event. */
9123
9124 FRAME_PTR last_mouse_frame;
9125
9126 /* Tool-bar item index of the item on which a mouse button was pressed
9127 or -1. */
9128
9129 int last_tool_bar_item;
9130
9131
9132 /* Update the tool-bar item list for frame F. This has to be done
9133 before we start to fill in any display lines. Called from
9134 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
9135 and restore it here. */
9136
9137 static void
9138 update_tool_bar (f, save_match_data)
9139 struct frame *f;
9140 int save_match_data;
9141 {
9142 #ifdef USE_GTK
9143 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
9144 #else
9145 int do_update = WINDOWP (f->tool_bar_window)
9146 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0;
9147 #endif
9148
9149 if (do_update)
9150 {
9151 Lisp_Object window;
9152 struct window *w;
9153
9154 window = FRAME_SELECTED_WINDOW (f);
9155 w = XWINDOW (window);
9156
9157 /* If the user has switched buffers or windows, we need to
9158 recompute to reflect the new bindings. But we'll
9159 recompute when update_mode_lines is set too; that means
9160 that people can use force-mode-line-update to request
9161 that the menu bar be recomputed. The adverse effect on
9162 the rest of the redisplay algorithm is about the same as
9163 windows_or_buffers_changed anyway. */
9164 if (windows_or_buffers_changed
9165 || !NILP (w->update_mode_line)
9166 || update_mode_lines
9167 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
9168 < BUF_MODIFF (XBUFFER (w->buffer)))
9169 != !NILP (w->last_had_star))
9170 || ((!NILP (Vtransient_mark_mode)
9171 && !NILP (XBUFFER (w->buffer)->mark_active))
9172 != !NILP (w->region_showing)))
9173 {
9174 struct buffer *prev = current_buffer;
9175 int count = SPECPDL_INDEX ();
9176 Lisp_Object new_tool_bar;
9177 int new_n_tool_bar;
9178 struct gcpro gcpro1;
9179
9180 /* Set current_buffer to the buffer of the selected
9181 window of the frame, so that we get the right local
9182 keymaps. */
9183 set_buffer_internal_1 (XBUFFER (w->buffer));
9184
9185 /* Save match data, if we must. */
9186 if (save_match_data)
9187 record_unwind_save_match_data ();
9188
9189 /* Make sure that we don't accidentally use bogus keymaps. */
9190 if (NILP (Voverriding_local_map_menu_flag))
9191 {
9192 specbind (Qoverriding_terminal_local_map, Qnil);
9193 specbind (Qoverriding_local_map, Qnil);
9194 }
9195
9196 GCPRO1 (new_tool_bar);
9197
9198 /* Build desired tool-bar items from keymaps. */
9199 new_tool_bar = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
9200 &new_n_tool_bar);
9201
9202 /* Redisplay the tool-bar if we changed it. */
9203 if (NILP (Fequal (new_tool_bar, f->tool_bar_items)))
9204 {
9205 /* Redisplay that happens asynchronously due to an expose event
9206 may access f->tool_bar_items. Make sure we update both
9207 variables within BLOCK_INPUT so no such event interrupts. */
9208 BLOCK_INPUT;
9209 f->tool_bar_items = new_tool_bar;
9210 f->n_tool_bar_items = new_n_tool_bar;
9211 w->update_mode_line = Qt;
9212 UNBLOCK_INPUT;
9213 }
9214
9215 UNGCPRO;
9216
9217 unbind_to (count, Qnil);
9218 set_buffer_internal_1 (prev);
9219 }
9220 }
9221 }
9222
9223
9224 /* Set F->desired_tool_bar_string to a Lisp string representing frame
9225 F's desired tool-bar contents. F->tool_bar_items must have
9226 been set up previously by calling prepare_menu_bars. */
9227
9228 static void
9229 build_desired_tool_bar_string (f)
9230 struct frame *f;
9231 {
9232 int i, size, size_needed;
9233 struct gcpro gcpro1, gcpro2, gcpro3;
9234 Lisp_Object image, plist, props;
9235
9236 image = plist = props = Qnil;
9237 GCPRO3 (image, plist, props);
9238
9239 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
9240 Otherwise, make a new string. */
9241
9242 /* The size of the string we might be able to reuse. */
9243 size = (STRINGP (f->desired_tool_bar_string)
9244 ? SCHARS (f->desired_tool_bar_string)
9245 : 0);
9246
9247 /* We need one space in the string for each image. */
9248 size_needed = f->n_tool_bar_items;
9249
9250 /* Reuse f->desired_tool_bar_string, if possible. */
9251 if (size < size_needed || NILP (f->desired_tool_bar_string))
9252 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
9253 make_number (' '));
9254 else
9255 {
9256 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
9257 Fremove_text_properties (make_number (0), make_number (size),
9258 props, f->desired_tool_bar_string);
9259 }
9260
9261 /* Put a `display' property on the string for the images to display,
9262 put a `menu_item' property on tool-bar items with a value that
9263 is the index of the item in F's tool-bar item vector. */
9264 for (i = 0; i < f->n_tool_bar_items; ++i)
9265 {
9266 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
9267
9268 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
9269 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
9270 int hmargin, vmargin, relief, idx, end;
9271 extern Lisp_Object QCrelief, QCmargin, QCconversion;
9272
9273 /* If image is a vector, choose the image according to the
9274 button state. */
9275 image = PROP (TOOL_BAR_ITEM_IMAGES);
9276 if (VECTORP (image))
9277 {
9278 if (enabled_p)
9279 idx = (selected_p
9280 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
9281 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
9282 else
9283 idx = (selected_p
9284 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
9285 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
9286
9287 xassert (ASIZE (image) >= idx);
9288 image = AREF (image, idx);
9289 }
9290 else
9291 idx = -1;
9292
9293 /* Ignore invalid image specifications. */
9294 if (!valid_image_p (image))
9295 continue;
9296
9297 /* Display the tool-bar button pressed, or depressed. */
9298 plist = Fcopy_sequence (XCDR (image));
9299
9300 /* Compute margin and relief to draw. */
9301 relief = (tool_bar_button_relief >= 0
9302 ? tool_bar_button_relief
9303 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
9304 hmargin = vmargin = relief;
9305
9306 if (INTEGERP (Vtool_bar_button_margin)
9307 && XINT (Vtool_bar_button_margin) > 0)
9308 {
9309 hmargin += XFASTINT (Vtool_bar_button_margin);
9310 vmargin += XFASTINT (Vtool_bar_button_margin);
9311 }
9312 else if (CONSP (Vtool_bar_button_margin))
9313 {
9314 if (INTEGERP (XCAR (Vtool_bar_button_margin))
9315 && XINT (XCAR (Vtool_bar_button_margin)) > 0)
9316 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
9317
9318 if (INTEGERP (XCDR (Vtool_bar_button_margin))
9319 && XINT (XCDR (Vtool_bar_button_margin)) > 0)
9320 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
9321 }
9322
9323 if (auto_raise_tool_bar_buttons_p)
9324 {
9325 /* Add a `:relief' property to the image spec if the item is
9326 selected. */
9327 if (selected_p)
9328 {
9329 plist = Fplist_put (plist, QCrelief, make_number (-relief));
9330 hmargin -= relief;
9331 vmargin -= relief;
9332 }
9333 }
9334 else
9335 {
9336 /* If image is selected, display it pressed, i.e. with a
9337 negative relief. If it's not selected, display it with a
9338 raised relief. */
9339 plist = Fplist_put (plist, QCrelief,
9340 (selected_p
9341 ? make_number (-relief)
9342 : make_number (relief)));
9343 hmargin -= relief;
9344 vmargin -= relief;
9345 }
9346
9347 /* Put a margin around the image. */
9348 if (hmargin || vmargin)
9349 {
9350 if (hmargin == vmargin)
9351 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
9352 else
9353 plist = Fplist_put (plist, QCmargin,
9354 Fcons (make_number (hmargin),
9355 make_number (vmargin)));
9356 }
9357
9358 /* If button is not enabled, and we don't have special images
9359 for the disabled state, make the image appear disabled by
9360 applying an appropriate algorithm to it. */
9361 if (!enabled_p && idx < 0)
9362 plist = Fplist_put (plist, QCconversion, Qdisabled);
9363
9364 /* Put a `display' text property on the string for the image to
9365 display. Put a `menu-item' property on the string that gives
9366 the start of this item's properties in the tool-bar items
9367 vector. */
9368 image = Fcons (Qimage, plist);
9369 props = list4 (Qdisplay, image,
9370 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
9371
9372 /* Let the last image hide all remaining spaces in the tool bar
9373 string. The string can be longer than needed when we reuse a
9374 previous string. */
9375 if (i + 1 == f->n_tool_bar_items)
9376 end = SCHARS (f->desired_tool_bar_string);
9377 else
9378 end = i + 1;
9379 Fadd_text_properties (make_number (i), make_number (end),
9380 props, f->desired_tool_bar_string);
9381 #undef PROP
9382 }
9383
9384 UNGCPRO;
9385 }
9386
9387
9388 /* Display one line of the tool-bar of frame IT->f. */
9389
9390 static void
9391 display_tool_bar_line (it)
9392 struct it *it;
9393 {
9394 struct glyph_row *row = it->glyph_row;
9395 int max_x = it->last_visible_x;
9396 struct glyph *last;
9397
9398 prepare_desired_row (row);
9399 row->y = it->current_y;
9400
9401 /* Note that this isn't made use of if the face hasn't a box,
9402 so there's no need to check the face here. */
9403 it->start_of_box_run_p = 1;
9404
9405 while (it->current_x < max_x)
9406 {
9407 int x_before, x, n_glyphs_before, i, nglyphs;
9408
9409 /* Get the next display element. */
9410 if (!get_next_display_element (it))
9411 break;
9412
9413 /* Produce glyphs. */
9414 x_before = it->current_x;
9415 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
9416 PRODUCE_GLYPHS (it);
9417
9418 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
9419 i = 0;
9420 x = x_before;
9421 while (i < nglyphs)
9422 {
9423 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
9424
9425 if (x + glyph->pixel_width > max_x)
9426 {
9427 /* Glyph doesn't fit on line. */
9428 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
9429 it->current_x = x;
9430 goto out;
9431 }
9432
9433 ++it->hpos;
9434 x += glyph->pixel_width;
9435 ++i;
9436 }
9437
9438 /* Stop at line ends. */
9439 if (ITERATOR_AT_END_OF_LINE_P (it))
9440 break;
9441
9442 set_iterator_to_next (it, 1);
9443 }
9444
9445 out:;
9446
9447 row->displays_text_p = row->used[TEXT_AREA] != 0;
9448 extend_face_to_end_of_line (it);
9449 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
9450 last->right_box_line_p = 1;
9451 if (last == row->glyphs[TEXT_AREA])
9452 last->left_box_line_p = 1;
9453 compute_line_metrics (it);
9454
9455 /* If line is empty, make it occupy the rest of the tool-bar. */
9456 if (!row->displays_text_p)
9457 {
9458 row->height = row->phys_height = it->last_visible_y - row->y;
9459 row->ascent = row->phys_ascent = 0;
9460 row->extra_line_spacing = 0;
9461 }
9462
9463 row->full_width_p = 1;
9464 row->continued_p = 0;
9465 row->truncated_on_left_p = 0;
9466 row->truncated_on_right_p = 0;
9467
9468 it->current_x = it->hpos = 0;
9469 it->current_y += row->height;
9470 ++it->vpos;
9471 ++it->glyph_row;
9472 }
9473
9474
9475 /* Value is the number of screen lines needed to make all tool-bar
9476 items of frame F visible. */
9477
9478 static int
9479 tool_bar_lines_needed (f)
9480 struct frame *f;
9481 {
9482 struct window *w = XWINDOW (f->tool_bar_window);
9483 struct it it;
9484
9485 /* Initialize an iterator for iteration over
9486 F->desired_tool_bar_string in the tool-bar window of frame F. */
9487 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
9488 it.first_visible_x = 0;
9489 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
9490 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
9491
9492 while (!ITERATOR_AT_END_P (&it))
9493 {
9494 it.glyph_row = w->desired_matrix->rows;
9495 clear_glyph_row (it.glyph_row);
9496 display_tool_bar_line (&it);
9497 }
9498
9499 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
9500 }
9501
9502
9503 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
9504 0, 1, 0,
9505 doc: /* Return the number of lines occupied by the tool bar of FRAME. */)
9506 (frame)
9507 Lisp_Object frame;
9508 {
9509 struct frame *f;
9510 struct window *w;
9511 int nlines = 0;
9512
9513 if (NILP (frame))
9514 frame = selected_frame;
9515 else
9516 CHECK_FRAME (frame);
9517 f = XFRAME (frame);
9518
9519 if (WINDOWP (f->tool_bar_window)
9520 || (w = XWINDOW (f->tool_bar_window),
9521 WINDOW_TOTAL_LINES (w) > 0))
9522 {
9523 update_tool_bar (f, 1);
9524 if (f->n_tool_bar_items)
9525 {
9526 build_desired_tool_bar_string (f);
9527 nlines = tool_bar_lines_needed (f);
9528 }
9529 }
9530
9531 return make_number (nlines);
9532 }
9533
9534
9535 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
9536 height should be changed. */
9537
9538 static int
9539 redisplay_tool_bar (f)
9540 struct frame *f;
9541 {
9542 struct window *w;
9543 struct it it;
9544 struct glyph_row *row;
9545 int change_height_p = 0;
9546
9547 #ifdef USE_GTK
9548 if (FRAME_EXTERNAL_TOOL_BAR (f))
9549 update_frame_tool_bar (f);
9550 return 0;
9551 #endif
9552
9553 /* If frame hasn't a tool-bar window or if it is zero-height, don't
9554 do anything. This means you must start with tool-bar-lines
9555 non-zero to get the auto-sizing effect. Or in other words, you
9556 can turn off tool-bars by specifying tool-bar-lines zero. */
9557 if (!WINDOWP (f->tool_bar_window)
9558 || (w = XWINDOW (f->tool_bar_window),
9559 WINDOW_TOTAL_LINES (w) == 0))
9560 return 0;
9561
9562 /* Set up an iterator for the tool-bar window. */
9563 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
9564 it.first_visible_x = 0;
9565 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
9566 row = it.glyph_row;
9567
9568 /* Build a string that represents the contents of the tool-bar. */
9569 build_desired_tool_bar_string (f);
9570 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
9571
9572 /* Display as many lines as needed to display all tool-bar items. */
9573 while (it.current_y < it.last_visible_y)
9574 display_tool_bar_line (&it);
9575
9576 /* It doesn't make much sense to try scrolling in the tool-bar
9577 window, so don't do it. */
9578 w->desired_matrix->no_scrolling_p = 1;
9579 w->must_be_updated_p = 1;
9580
9581 if (auto_resize_tool_bars_p)
9582 {
9583 int nlines;
9584
9585 /* If we couldn't display everything, change the tool-bar's
9586 height. */
9587 if (IT_STRING_CHARPOS (it) < it.end_charpos)
9588 change_height_p = 1;
9589
9590 /* If there are blank lines at the end, except for a partially
9591 visible blank line at the end that is smaller than
9592 FRAME_LINE_HEIGHT, change the tool-bar's height. */
9593 row = it.glyph_row - 1;
9594 if (!row->displays_text_p
9595 && row->height >= FRAME_LINE_HEIGHT (f))
9596 change_height_p = 1;
9597
9598 /* If row displays tool-bar items, but is partially visible,
9599 change the tool-bar's height. */
9600 if (row->displays_text_p
9601 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y)
9602 change_height_p = 1;
9603
9604 /* Resize windows as needed by changing the `tool-bar-lines'
9605 frame parameter. */
9606 if (change_height_p
9607 && (nlines = tool_bar_lines_needed (f),
9608 nlines != WINDOW_TOTAL_LINES (w)))
9609 {
9610 extern Lisp_Object Qtool_bar_lines;
9611 Lisp_Object frame;
9612 int old_height = WINDOW_TOTAL_LINES (w);
9613
9614 XSETFRAME (frame, f);
9615 clear_glyph_matrix (w->desired_matrix);
9616 Fmodify_frame_parameters (frame,
9617 Fcons (Fcons (Qtool_bar_lines,
9618 make_number (nlines)),
9619 Qnil));
9620 if (WINDOW_TOTAL_LINES (w) != old_height)
9621 fonts_changed_p = 1;
9622 }
9623 }
9624
9625 return change_height_p;
9626 }
9627
9628
9629 /* Get information about the tool-bar item which is displayed in GLYPH
9630 on frame F. Return in *PROP_IDX the index where tool-bar item
9631 properties start in F->tool_bar_items. Value is zero if
9632 GLYPH doesn't display a tool-bar item. */
9633
9634 static int
9635 tool_bar_item_info (f, glyph, prop_idx)
9636 struct frame *f;
9637 struct glyph *glyph;
9638 int *prop_idx;
9639 {
9640 Lisp_Object prop;
9641 int success_p;
9642 int charpos;
9643
9644 /* This function can be called asynchronously, which means we must
9645 exclude any possibility that Fget_text_property signals an
9646 error. */
9647 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
9648 charpos = max (0, charpos);
9649
9650 /* Get the text property `menu-item' at pos. The value of that
9651 property is the start index of this item's properties in
9652 F->tool_bar_items. */
9653 prop = Fget_text_property (make_number (charpos),
9654 Qmenu_item, f->current_tool_bar_string);
9655 if (INTEGERP (prop))
9656 {
9657 *prop_idx = XINT (prop);
9658 success_p = 1;
9659 }
9660 else
9661 success_p = 0;
9662
9663 return success_p;
9664 }
9665
9666 \f
9667 /* Get information about the tool-bar item at position X/Y on frame F.
9668 Return in *GLYPH a pointer to the glyph of the tool-bar item in
9669 the current matrix of the tool-bar window of F, or NULL if not
9670 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
9671 item in F->tool_bar_items. Value is
9672
9673 -1 if X/Y is not on a tool-bar item
9674 0 if X/Y is on the same item that was highlighted before.
9675 1 otherwise. */
9676
9677 static int
9678 get_tool_bar_item (f, x, y, glyph, hpos, vpos, prop_idx)
9679 struct frame *f;
9680 int x, y;
9681 struct glyph **glyph;
9682 int *hpos, *vpos, *prop_idx;
9683 {
9684 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9685 struct window *w = XWINDOW (f->tool_bar_window);
9686 int area;
9687
9688 /* Find the glyph under X/Y. */
9689 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
9690 if (*glyph == NULL)
9691 return -1;
9692
9693 /* Get the start of this tool-bar item's properties in
9694 f->tool_bar_items. */
9695 if (!tool_bar_item_info (f, *glyph, prop_idx))
9696 return -1;
9697
9698 /* Is mouse on the highlighted item? */
9699 if (EQ (f->tool_bar_window, dpyinfo->mouse_face_window)
9700 && *vpos >= dpyinfo->mouse_face_beg_row
9701 && *vpos <= dpyinfo->mouse_face_end_row
9702 && (*vpos > dpyinfo->mouse_face_beg_row
9703 || *hpos >= dpyinfo->mouse_face_beg_col)
9704 && (*vpos < dpyinfo->mouse_face_end_row
9705 || *hpos < dpyinfo->mouse_face_end_col
9706 || dpyinfo->mouse_face_past_end))
9707 return 0;
9708
9709 return 1;
9710 }
9711
9712
9713 /* EXPORT:
9714 Handle mouse button event on the tool-bar of frame F, at
9715 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
9716 0 for button release. MODIFIERS is event modifiers for button
9717 release. */
9718
9719 void
9720 handle_tool_bar_click (f, x, y, down_p, modifiers)
9721 struct frame *f;
9722 int x, y, down_p;
9723 unsigned int modifiers;
9724 {
9725 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9726 struct window *w = XWINDOW (f->tool_bar_window);
9727 int hpos, vpos, prop_idx;
9728 struct glyph *glyph;
9729 Lisp_Object enabled_p;
9730
9731 /* If not on the highlighted tool-bar item, return. */
9732 frame_to_window_pixel_xy (w, &x, &y);
9733 if (get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
9734 return;
9735
9736 /* If item is disabled, do nothing. */
9737 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
9738 if (NILP (enabled_p))
9739 return;
9740
9741 if (down_p)
9742 {
9743 /* Show item in pressed state. */
9744 show_mouse_face (dpyinfo, DRAW_IMAGE_SUNKEN);
9745 dpyinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
9746 last_tool_bar_item = prop_idx;
9747 }
9748 else
9749 {
9750 Lisp_Object key, frame;
9751 struct input_event event;
9752 EVENT_INIT (event);
9753
9754 /* Show item in released state. */
9755 show_mouse_face (dpyinfo, DRAW_IMAGE_RAISED);
9756 dpyinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
9757
9758 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
9759
9760 XSETFRAME (frame, f);
9761 event.kind = TOOL_BAR_EVENT;
9762 event.frame_or_window = frame;
9763 event.arg = frame;
9764 kbd_buffer_store_event (&event);
9765
9766 event.kind = TOOL_BAR_EVENT;
9767 event.frame_or_window = frame;
9768 event.arg = key;
9769 event.modifiers = modifiers;
9770 kbd_buffer_store_event (&event);
9771 last_tool_bar_item = -1;
9772 }
9773 }
9774
9775
9776 /* Possibly highlight a tool-bar item on frame F when mouse moves to
9777 tool-bar window-relative coordinates X/Y. Called from
9778 note_mouse_highlight. */
9779
9780 static void
9781 note_tool_bar_highlight (f, x, y)
9782 struct frame *f;
9783 int x, y;
9784 {
9785 Lisp_Object window = f->tool_bar_window;
9786 struct window *w = XWINDOW (window);
9787 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9788 int hpos, vpos;
9789 struct glyph *glyph;
9790 struct glyph_row *row;
9791 int i;
9792 Lisp_Object enabled_p;
9793 int prop_idx;
9794 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
9795 int mouse_down_p, rc;
9796
9797 /* Function note_mouse_highlight is called with negative x(y
9798 values when mouse moves outside of the frame. */
9799 if (x <= 0 || y <= 0)
9800 {
9801 clear_mouse_face (dpyinfo);
9802 return;
9803 }
9804
9805 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
9806 if (rc < 0)
9807 {
9808 /* Not on tool-bar item. */
9809 clear_mouse_face (dpyinfo);
9810 return;
9811 }
9812 else if (rc == 0)
9813 /* On same tool-bar item as before. */
9814 goto set_help_echo;
9815
9816 clear_mouse_face (dpyinfo);
9817
9818 /* Mouse is down, but on different tool-bar item? */
9819 mouse_down_p = (dpyinfo->grabbed
9820 && f == last_mouse_frame
9821 && FRAME_LIVE_P (f));
9822 if (mouse_down_p
9823 && last_tool_bar_item != prop_idx)
9824 return;
9825
9826 dpyinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
9827 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
9828
9829 /* If tool-bar item is not enabled, don't highlight it. */
9830 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
9831 if (!NILP (enabled_p))
9832 {
9833 /* Compute the x-position of the glyph. In front and past the
9834 image is a space. We include this in the highlighted area. */
9835 row = MATRIX_ROW (w->current_matrix, vpos);
9836 for (i = x = 0; i < hpos; ++i)
9837 x += row->glyphs[TEXT_AREA][i].pixel_width;
9838
9839 /* Record this as the current active region. */
9840 dpyinfo->mouse_face_beg_col = hpos;
9841 dpyinfo->mouse_face_beg_row = vpos;
9842 dpyinfo->mouse_face_beg_x = x;
9843 dpyinfo->mouse_face_beg_y = row->y;
9844 dpyinfo->mouse_face_past_end = 0;
9845
9846 dpyinfo->mouse_face_end_col = hpos + 1;
9847 dpyinfo->mouse_face_end_row = vpos;
9848 dpyinfo->mouse_face_end_x = x + glyph->pixel_width;
9849 dpyinfo->mouse_face_end_y = row->y;
9850 dpyinfo->mouse_face_window = window;
9851 dpyinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
9852
9853 /* Display it as active. */
9854 show_mouse_face (dpyinfo, draw);
9855 dpyinfo->mouse_face_image_state = draw;
9856 }
9857
9858 set_help_echo:
9859
9860 /* Set help_echo_string to a help string to display for this tool-bar item.
9861 XTread_socket does the rest. */
9862 help_echo_object = help_echo_window = Qnil;
9863 help_echo_pos = -1;
9864 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
9865 if (NILP (help_echo_string))
9866 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
9867 }
9868
9869 #endif /* HAVE_WINDOW_SYSTEM */
9870
9871
9872 \f
9873 /************************************************************************
9874 Horizontal scrolling
9875 ************************************************************************/
9876
9877 static int hscroll_window_tree P_ ((Lisp_Object));
9878 static int hscroll_windows P_ ((Lisp_Object));
9879
9880 /* For all leaf windows in the window tree rooted at WINDOW, set their
9881 hscroll value so that PT is (i) visible in the window, and (ii) so
9882 that it is not within a certain margin at the window's left and
9883 right border. Value is non-zero if any window's hscroll has been
9884 changed. */
9885
9886 static int
9887 hscroll_window_tree (window)
9888 Lisp_Object window;
9889 {
9890 int hscrolled_p = 0;
9891 int hscroll_relative_p = FLOATP (Vhscroll_step);
9892 int hscroll_step_abs = 0;
9893 double hscroll_step_rel = 0;
9894
9895 if (hscroll_relative_p)
9896 {
9897 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
9898 if (hscroll_step_rel < 0)
9899 {
9900 hscroll_relative_p = 0;
9901 hscroll_step_abs = 0;
9902 }
9903 }
9904 else if (INTEGERP (Vhscroll_step))
9905 {
9906 hscroll_step_abs = XINT (Vhscroll_step);
9907 if (hscroll_step_abs < 0)
9908 hscroll_step_abs = 0;
9909 }
9910 else
9911 hscroll_step_abs = 0;
9912
9913 while (WINDOWP (window))
9914 {
9915 struct window *w = XWINDOW (window);
9916
9917 if (WINDOWP (w->hchild))
9918 hscrolled_p |= hscroll_window_tree (w->hchild);
9919 else if (WINDOWP (w->vchild))
9920 hscrolled_p |= hscroll_window_tree (w->vchild);
9921 else if (w->cursor.vpos >= 0)
9922 {
9923 int h_margin;
9924 int text_area_width;
9925 struct glyph_row *current_cursor_row
9926 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
9927 struct glyph_row *desired_cursor_row
9928 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
9929 struct glyph_row *cursor_row
9930 = (desired_cursor_row->enabled_p
9931 ? desired_cursor_row
9932 : current_cursor_row);
9933
9934 text_area_width = window_box_width (w, TEXT_AREA);
9935
9936 /* Scroll when cursor is inside this scroll margin. */
9937 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
9938
9939 if ((XFASTINT (w->hscroll)
9940 && w->cursor.x <= h_margin)
9941 || (cursor_row->enabled_p
9942 && cursor_row->truncated_on_right_p
9943 && (w->cursor.x >= text_area_width - h_margin)))
9944 {
9945 struct it it;
9946 int hscroll;
9947 struct buffer *saved_current_buffer;
9948 int pt;
9949 int wanted_x;
9950
9951 /* Find point in a display of infinite width. */
9952 saved_current_buffer = current_buffer;
9953 current_buffer = XBUFFER (w->buffer);
9954
9955 if (w == XWINDOW (selected_window))
9956 pt = BUF_PT (current_buffer);
9957 else
9958 {
9959 pt = marker_position (w->pointm);
9960 pt = max (BEGV, pt);
9961 pt = min (ZV, pt);
9962 }
9963
9964 /* Move iterator to pt starting at cursor_row->start in
9965 a line with infinite width. */
9966 init_to_row_start (&it, w, cursor_row);
9967 it.last_visible_x = INFINITY;
9968 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
9969 current_buffer = saved_current_buffer;
9970
9971 /* Position cursor in window. */
9972 if (!hscroll_relative_p && hscroll_step_abs == 0)
9973 hscroll = max (0, (it.current_x
9974 - (ITERATOR_AT_END_OF_LINE_P (&it)
9975 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
9976 : (text_area_width / 2))))
9977 / FRAME_COLUMN_WIDTH (it.f);
9978 else if (w->cursor.x >= text_area_width - h_margin)
9979 {
9980 if (hscroll_relative_p)
9981 wanted_x = text_area_width * (1 - hscroll_step_rel)
9982 - h_margin;
9983 else
9984 wanted_x = text_area_width
9985 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
9986 - h_margin;
9987 hscroll
9988 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
9989 }
9990 else
9991 {
9992 if (hscroll_relative_p)
9993 wanted_x = text_area_width * hscroll_step_rel
9994 + h_margin;
9995 else
9996 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
9997 + h_margin;
9998 hscroll
9999 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
10000 }
10001 hscroll = max (hscroll, XFASTINT (w->min_hscroll));
10002
10003 /* Don't call Fset_window_hscroll if value hasn't
10004 changed because it will prevent redisplay
10005 optimizations. */
10006 if (XFASTINT (w->hscroll) != hscroll)
10007 {
10008 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
10009 w->hscroll = make_number (hscroll);
10010 hscrolled_p = 1;
10011 }
10012 }
10013 }
10014
10015 window = w->next;
10016 }
10017
10018 /* Value is non-zero if hscroll of any leaf window has been changed. */
10019 return hscrolled_p;
10020 }
10021
10022
10023 /* Set hscroll so that cursor is visible and not inside horizontal
10024 scroll margins for all windows in the tree rooted at WINDOW. See
10025 also hscroll_window_tree above. Value is non-zero if any window's
10026 hscroll has been changed. If it has, desired matrices on the frame
10027 of WINDOW are cleared. */
10028
10029 static int
10030 hscroll_windows (window)
10031 Lisp_Object window;
10032 {
10033 int hscrolled_p;
10034
10035 if (automatic_hscrolling_p)
10036 {
10037 hscrolled_p = hscroll_window_tree (window);
10038 if (hscrolled_p)
10039 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
10040 }
10041 else
10042 hscrolled_p = 0;
10043 return hscrolled_p;
10044 }
10045
10046
10047 \f
10048 /************************************************************************
10049 Redisplay
10050 ************************************************************************/
10051
10052 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
10053 to a non-zero value. This is sometimes handy to have in a debugger
10054 session. */
10055
10056 #if GLYPH_DEBUG
10057
10058 /* First and last unchanged row for try_window_id. */
10059
10060 int debug_first_unchanged_at_end_vpos;
10061 int debug_last_unchanged_at_beg_vpos;
10062
10063 /* Delta vpos and y. */
10064
10065 int debug_dvpos, debug_dy;
10066
10067 /* Delta in characters and bytes for try_window_id. */
10068
10069 int debug_delta, debug_delta_bytes;
10070
10071 /* Values of window_end_pos and window_end_vpos at the end of
10072 try_window_id. */
10073
10074 EMACS_INT debug_end_pos, debug_end_vpos;
10075
10076 /* Append a string to W->desired_matrix->method. FMT is a printf
10077 format string. A1...A9 are a supplement for a variable-length
10078 argument list. If trace_redisplay_p is non-zero also printf the
10079 resulting string to stderr. */
10080
10081 static void
10082 debug_method_add (w, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9)
10083 struct window *w;
10084 char *fmt;
10085 int a1, a2, a3, a4, a5, a6, a7, a8, a9;
10086 {
10087 char buffer[512];
10088 char *method = w->desired_matrix->method;
10089 int len = strlen (method);
10090 int size = sizeof w->desired_matrix->method;
10091 int remaining = size - len - 1;
10092
10093 sprintf (buffer, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9);
10094 if (len && remaining)
10095 {
10096 method[len] = '|';
10097 --remaining, ++len;
10098 }
10099
10100 strncpy (method + len, buffer, remaining);
10101
10102 if (trace_redisplay_p)
10103 fprintf (stderr, "%p (%s): %s\n",
10104 w,
10105 ((BUFFERP (w->buffer)
10106 && STRINGP (XBUFFER (w->buffer)->name))
10107 ? (char *) SDATA (XBUFFER (w->buffer)->name)
10108 : "no buffer"),
10109 buffer);
10110 }
10111
10112 #endif /* GLYPH_DEBUG */
10113
10114
10115 /* Value is non-zero if all changes in window W, which displays
10116 current_buffer, are in the text between START and END. START is a
10117 buffer position, END is given as a distance from Z. Used in
10118 redisplay_internal for display optimization. */
10119
10120 static INLINE int
10121 text_outside_line_unchanged_p (w, start, end)
10122 struct window *w;
10123 int start, end;
10124 {
10125 int unchanged_p = 1;
10126
10127 /* If text or overlays have changed, see where. */
10128 if (XFASTINT (w->last_modified) < MODIFF
10129 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
10130 {
10131 /* Gap in the line? */
10132 if (GPT < start || Z - GPT < end)
10133 unchanged_p = 0;
10134
10135 /* Changes start in front of the line, or end after it? */
10136 if (unchanged_p
10137 && (BEG_UNCHANGED < start - 1
10138 || END_UNCHANGED < end))
10139 unchanged_p = 0;
10140
10141 /* If selective display, can't optimize if changes start at the
10142 beginning of the line. */
10143 if (unchanged_p
10144 && INTEGERP (current_buffer->selective_display)
10145 && XINT (current_buffer->selective_display) > 0
10146 && (BEG_UNCHANGED < start || GPT <= start))
10147 unchanged_p = 0;
10148
10149 /* If there are overlays at the start or end of the line, these
10150 may have overlay strings with newlines in them. A change at
10151 START, for instance, may actually concern the display of such
10152 overlay strings as well, and they are displayed on different
10153 lines. So, quickly rule out this case. (For the future, it
10154 might be desirable to implement something more telling than
10155 just BEG/END_UNCHANGED.) */
10156 if (unchanged_p)
10157 {
10158 if (BEG + BEG_UNCHANGED == start
10159 && overlay_touches_p (start))
10160 unchanged_p = 0;
10161 if (END_UNCHANGED == end
10162 && overlay_touches_p (Z - end))
10163 unchanged_p = 0;
10164 }
10165 }
10166
10167 return unchanged_p;
10168 }
10169
10170
10171 /* Do a frame update, taking possible shortcuts into account. This is
10172 the main external entry point for redisplay.
10173
10174 If the last redisplay displayed an echo area message and that message
10175 is no longer requested, we clear the echo area or bring back the
10176 mini-buffer if that is in use. */
10177
10178 void
10179 redisplay ()
10180 {
10181 redisplay_internal (0);
10182 }
10183
10184
10185 static Lisp_Object
10186 overlay_arrow_string_or_property (var)
10187 Lisp_Object var;
10188 {
10189 Lisp_Object val;
10190
10191 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
10192 return val;
10193
10194 return Voverlay_arrow_string;
10195 }
10196
10197 /* Return 1 if there are any overlay-arrows in current_buffer. */
10198 static int
10199 overlay_arrow_in_current_buffer_p ()
10200 {
10201 Lisp_Object vlist;
10202
10203 for (vlist = Voverlay_arrow_variable_list;
10204 CONSP (vlist);
10205 vlist = XCDR (vlist))
10206 {
10207 Lisp_Object var = XCAR (vlist);
10208 Lisp_Object val;
10209
10210 if (!SYMBOLP (var))
10211 continue;
10212 val = find_symbol_value (var);
10213 if (MARKERP (val)
10214 && current_buffer == XMARKER (val)->buffer)
10215 return 1;
10216 }
10217 return 0;
10218 }
10219
10220
10221 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
10222 has changed. */
10223
10224 static int
10225 overlay_arrows_changed_p ()
10226 {
10227 Lisp_Object vlist;
10228
10229 for (vlist = Voverlay_arrow_variable_list;
10230 CONSP (vlist);
10231 vlist = XCDR (vlist))
10232 {
10233 Lisp_Object var = XCAR (vlist);
10234 Lisp_Object val, pstr;
10235
10236 if (!SYMBOLP (var))
10237 continue;
10238 val = find_symbol_value (var);
10239 if (!MARKERP (val))
10240 continue;
10241 if (! EQ (COERCE_MARKER (val),
10242 Fget (var, Qlast_arrow_position))
10243 || ! (pstr = overlay_arrow_string_or_property (var),
10244 EQ (pstr, Fget (var, Qlast_arrow_string))))
10245 return 1;
10246 }
10247 return 0;
10248 }
10249
10250 /* Mark overlay arrows to be updated on next redisplay. */
10251
10252 static void
10253 update_overlay_arrows (up_to_date)
10254 int up_to_date;
10255 {
10256 Lisp_Object vlist;
10257
10258 for (vlist = Voverlay_arrow_variable_list;
10259 CONSP (vlist);
10260 vlist = XCDR (vlist))
10261 {
10262 Lisp_Object var = XCAR (vlist);
10263
10264 if (!SYMBOLP (var))
10265 continue;
10266
10267 if (up_to_date > 0)
10268 {
10269 Lisp_Object val = find_symbol_value (var);
10270 Fput (var, Qlast_arrow_position,
10271 COERCE_MARKER (val));
10272 Fput (var, Qlast_arrow_string,
10273 overlay_arrow_string_or_property (var));
10274 }
10275 else if (up_to_date < 0
10276 || !NILP (Fget (var, Qlast_arrow_position)))
10277 {
10278 Fput (var, Qlast_arrow_position, Qt);
10279 Fput (var, Qlast_arrow_string, Qt);
10280 }
10281 }
10282 }
10283
10284
10285 /* Return overlay arrow string to display at row.
10286 Return integer (bitmap number) for arrow bitmap in left fringe.
10287 Return nil if no overlay arrow. */
10288
10289 static Lisp_Object
10290 overlay_arrow_at_row (it, row)
10291 struct it *it;
10292 struct glyph_row *row;
10293 {
10294 Lisp_Object vlist;
10295
10296 for (vlist = Voverlay_arrow_variable_list;
10297 CONSP (vlist);
10298 vlist = XCDR (vlist))
10299 {
10300 Lisp_Object var = XCAR (vlist);
10301 Lisp_Object val;
10302
10303 if (!SYMBOLP (var))
10304 continue;
10305
10306 val = find_symbol_value (var);
10307
10308 if (MARKERP (val)
10309 && current_buffer == XMARKER (val)->buffer
10310 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
10311 {
10312 if (FRAME_WINDOW_P (it->f)
10313 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
10314 {
10315 #ifdef HAVE_WINDOW_SYSTEM
10316 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
10317 {
10318 int fringe_bitmap;
10319 if ((fringe_bitmap = lookup_fringe_bitmap (val)) != 0)
10320 return make_number (fringe_bitmap);
10321 }
10322 #endif
10323 return make_number (-1); /* Use default arrow bitmap */
10324 }
10325 return overlay_arrow_string_or_property (var);
10326 }
10327 }
10328
10329 return Qnil;
10330 }
10331
10332 /* Return 1 if point moved out of or into a composition. Otherwise
10333 return 0. PREV_BUF and PREV_PT are the last point buffer and
10334 position. BUF and PT are the current point buffer and position. */
10335
10336 int
10337 check_point_in_composition (prev_buf, prev_pt, buf, pt)
10338 struct buffer *prev_buf, *buf;
10339 int prev_pt, pt;
10340 {
10341 int start, end;
10342 Lisp_Object prop;
10343 Lisp_Object buffer;
10344
10345 XSETBUFFER (buffer, buf);
10346 /* Check a composition at the last point if point moved within the
10347 same buffer. */
10348 if (prev_buf == buf)
10349 {
10350 if (prev_pt == pt)
10351 /* Point didn't move. */
10352 return 0;
10353
10354 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
10355 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
10356 && COMPOSITION_VALID_P (start, end, prop)
10357 && start < prev_pt && end > prev_pt)
10358 /* The last point was within the composition. Return 1 iff
10359 point moved out of the composition. */
10360 return (pt <= start || pt >= end);
10361 }
10362
10363 /* Check a composition at the current point. */
10364 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
10365 && find_composition (pt, -1, &start, &end, &prop, buffer)
10366 && COMPOSITION_VALID_P (start, end, prop)
10367 && start < pt && end > pt);
10368 }
10369
10370
10371 /* Reconsider the setting of B->clip_changed which is displayed
10372 in window W. */
10373
10374 static INLINE void
10375 reconsider_clip_changes (w, b)
10376 struct window *w;
10377 struct buffer *b;
10378 {
10379 if (b->clip_changed
10380 && !NILP (w->window_end_valid)
10381 && w->current_matrix->buffer == b
10382 && w->current_matrix->zv == BUF_ZV (b)
10383 && w->current_matrix->begv == BUF_BEGV (b))
10384 b->clip_changed = 0;
10385
10386 /* If display wasn't paused, and W is not a tool bar window, see if
10387 point has been moved into or out of a composition. In that case,
10388 we set b->clip_changed to 1 to force updating the screen. If
10389 b->clip_changed has already been set to 1, we can skip this
10390 check. */
10391 if (!b->clip_changed
10392 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
10393 {
10394 int pt;
10395
10396 if (w == XWINDOW (selected_window))
10397 pt = BUF_PT (current_buffer);
10398 else
10399 pt = marker_position (w->pointm);
10400
10401 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
10402 || pt != XINT (w->last_point))
10403 && check_point_in_composition (w->current_matrix->buffer,
10404 XINT (w->last_point),
10405 XBUFFER (w->buffer), pt))
10406 b->clip_changed = 1;
10407 }
10408 }
10409 \f
10410
10411 /* Select FRAME to forward the values of frame-local variables into C
10412 variables so that the redisplay routines can access those values
10413 directly. */
10414
10415 static void
10416 select_frame_for_redisplay (frame)
10417 Lisp_Object frame;
10418 {
10419 Lisp_Object tail, sym, val;
10420 Lisp_Object old = selected_frame;
10421
10422 xassert (FRAMEP (frame) && FRAME_LIVE_P (XFRAME (frame)));
10423
10424 selected_frame = frame;
10425
10426 for (tail = XFRAME (frame)->param_alist; CONSP (tail); tail = XCDR (tail))
10427 if (CONSP (XCAR (tail))
10428 && (sym = XCAR (XCAR (tail)),
10429 SYMBOLP (sym))
10430 && (sym = indirect_variable (sym),
10431 val = SYMBOL_VALUE (sym),
10432 (BUFFER_LOCAL_VALUEP (val)
10433 || SOME_BUFFER_LOCAL_VALUEP (val)))
10434 && XBUFFER_LOCAL_VALUE (val)->check_frame)
10435 /* Use find_symbol_value rather than Fsymbol_value
10436 to avoid an error if it is void. */
10437 find_symbol_value (sym);
10438
10439 for (tail = XFRAME (old)->param_alist; CONSP (tail); tail = XCDR (tail))
10440 if (CONSP (XCAR (tail))
10441 && (sym = XCAR (XCAR (tail)),
10442 SYMBOLP (sym))
10443 && (sym = indirect_variable (sym),
10444 val = SYMBOL_VALUE (sym),
10445 (BUFFER_LOCAL_VALUEP (val)
10446 || SOME_BUFFER_LOCAL_VALUEP (val)))
10447 && XBUFFER_LOCAL_VALUE (val)->check_frame)
10448 find_symbol_value (sym);
10449 }
10450
10451
10452 #define STOP_POLLING \
10453 do { if (! polling_stopped_here) stop_polling (); \
10454 polling_stopped_here = 1; } while (0)
10455
10456 #define RESUME_POLLING \
10457 do { if (polling_stopped_here) start_polling (); \
10458 polling_stopped_here = 0; } while (0)
10459
10460
10461 /* If PRESERVE_ECHO_AREA is nonzero, it means this redisplay is not in
10462 response to any user action; therefore, we should preserve the echo
10463 area. (Actually, our caller does that job.) Perhaps in the future
10464 avoid recentering windows if it is not necessary; currently that
10465 causes some problems. */
10466
10467 static void
10468 redisplay_internal (preserve_echo_area)
10469 int preserve_echo_area;
10470 {
10471 struct window *w = XWINDOW (selected_window);
10472 struct frame *f = XFRAME (w->frame);
10473 int pause;
10474 int must_finish = 0;
10475 struct text_pos tlbufpos, tlendpos;
10476 int number_of_visible_frames;
10477 int count;
10478 struct frame *sf = SELECTED_FRAME ();
10479 int polling_stopped_here = 0;
10480
10481 /* Non-zero means redisplay has to consider all windows on all
10482 frames. Zero means, only selected_window is considered. */
10483 int consider_all_windows_p;
10484
10485 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
10486
10487 /* No redisplay if running in batch mode or frame is not yet fully
10488 initialized, or redisplay is explicitly turned off by setting
10489 Vinhibit_redisplay. */
10490 if (noninteractive
10491 || !NILP (Vinhibit_redisplay)
10492 || !f->glyphs_initialized_p)
10493 return;
10494
10495 /* The flag redisplay_performed_directly_p is set by
10496 direct_output_for_insert when it already did the whole screen
10497 update necessary. */
10498 if (redisplay_performed_directly_p)
10499 {
10500 redisplay_performed_directly_p = 0;
10501 if (!hscroll_windows (selected_window))
10502 return;
10503 }
10504
10505 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
10506 if (popup_activated ())
10507 return;
10508 #endif
10509
10510 /* I don't think this happens but let's be paranoid. */
10511 if (redisplaying_p)
10512 return;
10513
10514 /* Record a function that resets redisplaying_p to its old value
10515 when we leave this function. */
10516 count = SPECPDL_INDEX ();
10517 record_unwind_protect (unwind_redisplay,
10518 Fcons (make_number (redisplaying_p), selected_frame));
10519 ++redisplaying_p;
10520 specbind (Qinhibit_free_realized_faces, Qnil);
10521
10522 {
10523 Lisp_Object tail, frame;
10524
10525 FOR_EACH_FRAME (tail, frame)
10526 {
10527 struct frame *f = XFRAME (frame);
10528 f->already_hscrolled_p = 0;
10529 }
10530 }
10531
10532 retry:
10533 pause = 0;
10534 reconsider_clip_changes (w, current_buffer);
10535
10536 /* If new fonts have been loaded that make a glyph matrix adjustment
10537 necessary, do it. */
10538 if (fonts_changed_p)
10539 {
10540 adjust_glyphs (NULL);
10541 ++windows_or_buffers_changed;
10542 fonts_changed_p = 0;
10543 }
10544
10545 /* If face_change_count is non-zero, init_iterator will free all
10546 realized faces, which includes the faces referenced from current
10547 matrices. So, we can't reuse current matrices in this case. */
10548 if (face_change_count)
10549 ++windows_or_buffers_changed;
10550
10551 if (FRAME_TERMCAP_P (sf)
10552 && FRAME_TTY (sf)->previous_frame != sf)
10553 {
10554 /* Since frames on a single ASCII terminal share the same
10555 display area, displaying a different frame means redisplay
10556 the whole thing. */
10557 windows_or_buffers_changed++;
10558 SET_FRAME_GARBAGED (sf);
10559 FRAME_TTY (sf)->previous_frame = sf;
10560 }
10561
10562 /* Set the visible flags for all frames. Do this before checking
10563 for resized or garbaged frames; they want to know if their frames
10564 are visible. See the comment in frame.h for
10565 FRAME_SAMPLE_VISIBILITY. */
10566 {
10567 Lisp_Object tail, frame;
10568
10569 number_of_visible_frames = 0;
10570
10571 FOR_EACH_FRAME (tail, frame)
10572 {
10573 struct frame *f = XFRAME (frame);
10574
10575 FRAME_SAMPLE_VISIBILITY (f);
10576 if (FRAME_VISIBLE_P (f))
10577 ++number_of_visible_frames;
10578 clear_desired_matrices (f);
10579 }
10580 }
10581
10582
10583 /* Notice any pending interrupt request to change frame size. */
10584 do_pending_window_change (1);
10585
10586 /* Clear frames marked as garbaged. */
10587 if (frame_garbaged)
10588 clear_garbaged_frames ();
10589
10590 /* Build menubar and tool-bar items. */
10591 if (NILP (Vmemory_full))
10592 prepare_menu_bars ();
10593
10594 if (windows_or_buffers_changed)
10595 update_mode_lines++;
10596
10597 /* Detect case that we need to write or remove a star in the mode line. */
10598 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
10599 {
10600 w->update_mode_line = Qt;
10601 if (buffer_shared > 1)
10602 update_mode_lines++;
10603 }
10604
10605 /* If %c is in the mode line, update it if needed. */
10606 if (!NILP (w->column_number_displayed)
10607 /* This alternative quickly identifies a common case
10608 where no change is needed. */
10609 && !(PT == XFASTINT (w->last_point)
10610 && XFASTINT (w->last_modified) >= MODIFF
10611 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
10612 && (XFASTINT (w->column_number_displayed)
10613 != (int) current_column ())) /* iftc */
10614 w->update_mode_line = Qt;
10615
10616 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
10617
10618 /* The variable buffer_shared is set in redisplay_window and
10619 indicates that we redisplay a buffer in different windows. See
10620 there. */
10621 consider_all_windows_p = (update_mode_lines || buffer_shared > 1
10622 || cursor_type_changed);
10623
10624 /* If specs for an arrow have changed, do thorough redisplay
10625 to ensure we remove any arrow that should no longer exist. */
10626 if (overlay_arrows_changed_p ())
10627 consider_all_windows_p = windows_or_buffers_changed = 1;
10628
10629 /* Normally the message* functions will have already displayed and
10630 updated the echo area, but the frame may have been trashed, or
10631 the update may have been preempted, so display the echo area
10632 again here. Checking message_cleared_p captures the case that
10633 the echo area should be cleared. */
10634 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
10635 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
10636 || (message_cleared_p
10637 && minibuf_level == 0
10638 /* If the mini-window is currently selected, this means the
10639 echo-area doesn't show through. */
10640 && !MINI_WINDOW_P (XWINDOW (selected_window))))
10641 {
10642 int window_height_changed_p = echo_area_display (0);
10643 must_finish = 1;
10644
10645 /* If we don't display the current message, don't clear the
10646 message_cleared_p flag, because, if we did, we wouldn't clear
10647 the echo area in the next redisplay which doesn't preserve
10648 the echo area. */
10649 if (!display_last_displayed_message_p)
10650 message_cleared_p = 0;
10651
10652 if (fonts_changed_p)
10653 goto retry;
10654 else if (window_height_changed_p)
10655 {
10656 consider_all_windows_p = 1;
10657 ++update_mode_lines;
10658 ++windows_or_buffers_changed;
10659
10660 /* If window configuration was changed, frames may have been
10661 marked garbaged. Clear them or we will experience
10662 surprises wrt scrolling. */
10663 if (frame_garbaged)
10664 clear_garbaged_frames ();
10665 }
10666 }
10667 else if (EQ (selected_window, minibuf_window)
10668 && (current_buffer->clip_changed
10669 || XFASTINT (w->last_modified) < MODIFF
10670 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
10671 && resize_mini_window (w, 0))
10672 {
10673 /* Resized active mini-window to fit the size of what it is
10674 showing if its contents might have changed. */
10675 must_finish = 1;
10676 consider_all_windows_p = 1;
10677 ++windows_or_buffers_changed;
10678 ++update_mode_lines;
10679
10680 /* If window configuration was changed, frames may have been
10681 marked garbaged. Clear them or we will experience
10682 surprises wrt scrolling. */
10683 if (frame_garbaged)
10684 clear_garbaged_frames ();
10685 }
10686
10687
10688 /* If showing the region, and mark has changed, we must redisplay
10689 the whole window. The assignment to this_line_start_pos prevents
10690 the optimization directly below this if-statement. */
10691 if (((!NILP (Vtransient_mark_mode)
10692 && !NILP (XBUFFER (w->buffer)->mark_active))
10693 != !NILP (w->region_showing))
10694 || (!NILP (w->region_showing)
10695 && !EQ (w->region_showing,
10696 Fmarker_position (XBUFFER (w->buffer)->mark))))
10697 CHARPOS (this_line_start_pos) = 0;
10698
10699 /* Optimize the case that only the line containing the cursor in the
10700 selected window has changed. Variables starting with this_ are
10701 set in display_line and record information about the line
10702 containing the cursor. */
10703 tlbufpos = this_line_start_pos;
10704 tlendpos = this_line_end_pos;
10705 if (!consider_all_windows_p
10706 && CHARPOS (tlbufpos) > 0
10707 && NILP (w->update_mode_line)
10708 && !current_buffer->clip_changed
10709 && !current_buffer->prevent_redisplay_optimizations_p
10710 && FRAME_VISIBLE_P (XFRAME (w->frame))
10711 && !FRAME_OBSCURED_P (XFRAME (w->frame))
10712 /* Make sure recorded data applies to current buffer, etc. */
10713 && this_line_buffer == current_buffer
10714 && current_buffer == XBUFFER (w->buffer)
10715 && NILP (w->force_start)
10716 && NILP (w->optional_new_start)
10717 /* Point must be on the line that we have info recorded about. */
10718 && PT >= CHARPOS (tlbufpos)
10719 && PT <= Z - CHARPOS (tlendpos)
10720 /* All text outside that line, including its final newline,
10721 must be unchanged */
10722 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
10723 CHARPOS (tlendpos)))
10724 {
10725 if (CHARPOS (tlbufpos) > BEGV
10726 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
10727 && (CHARPOS (tlbufpos) == ZV
10728 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
10729 /* Former continuation line has disappeared by becoming empty */
10730 goto cancel;
10731 else if (XFASTINT (w->last_modified) < MODIFF
10732 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
10733 || MINI_WINDOW_P (w))
10734 {
10735 /* We have to handle the case of continuation around a
10736 wide-column character (See the comment in indent.c around
10737 line 885).
10738
10739 For instance, in the following case:
10740
10741 -------- Insert --------
10742 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
10743 J_I_ ==> J_I_ `^^' are cursors.
10744 ^^ ^^
10745 -------- --------
10746
10747 As we have to redraw the line above, we should goto cancel. */
10748
10749 struct it it;
10750 int line_height_before = this_line_pixel_height;
10751
10752 /* Note that start_display will handle the case that the
10753 line starting at tlbufpos is a continuation lines. */
10754 start_display (&it, w, tlbufpos);
10755
10756 /* Implementation note: It this still necessary? */
10757 if (it.current_x != this_line_start_x)
10758 goto cancel;
10759
10760 TRACE ((stderr, "trying display optimization 1\n"));
10761 w->cursor.vpos = -1;
10762 overlay_arrow_seen = 0;
10763 it.vpos = this_line_vpos;
10764 it.current_y = this_line_y;
10765 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
10766 display_line (&it);
10767
10768 /* If line contains point, is not continued,
10769 and ends at same distance from eob as before, we win */
10770 if (w->cursor.vpos >= 0
10771 /* Line is not continued, otherwise this_line_start_pos
10772 would have been set to 0 in display_line. */
10773 && CHARPOS (this_line_start_pos)
10774 /* Line ends as before. */
10775 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
10776 /* Line has same height as before. Otherwise other lines
10777 would have to be shifted up or down. */
10778 && this_line_pixel_height == line_height_before)
10779 {
10780 /* If this is not the window's last line, we must adjust
10781 the charstarts of the lines below. */
10782 if (it.current_y < it.last_visible_y)
10783 {
10784 struct glyph_row *row
10785 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
10786 int delta, delta_bytes;
10787
10788 if (Z - CHARPOS (tlendpos) == ZV)
10789 {
10790 /* This line ends at end of (accessible part of)
10791 buffer. There is no newline to count. */
10792 delta = (Z
10793 - CHARPOS (tlendpos)
10794 - MATRIX_ROW_START_CHARPOS (row));
10795 delta_bytes = (Z_BYTE
10796 - BYTEPOS (tlendpos)
10797 - MATRIX_ROW_START_BYTEPOS (row));
10798 }
10799 else
10800 {
10801 /* This line ends in a newline. Must take
10802 account of the newline and the rest of the
10803 text that follows. */
10804 delta = (Z
10805 - CHARPOS (tlendpos)
10806 - MATRIX_ROW_START_CHARPOS (row));
10807 delta_bytes = (Z_BYTE
10808 - BYTEPOS (tlendpos)
10809 - MATRIX_ROW_START_BYTEPOS (row));
10810 }
10811
10812 increment_matrix_positions (w->current_matrix,
10813 this_line_vpos + 1,
10814 w->current_matrix->nrows,
10815 delta, delta_bytes);
10816 }
10817
10818 /* If this row displays text now but previously didn't,
10819 or vice versa, w->window_end_vpos may have to be
10820 adjusted. */
10821 if ((it.glyph_row - 1)->displays_text_p)
10822 {
10823 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
10824 XSETINT (w->window_end_vpos, this_line_vpos);
10825 }
10826 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
10827 && this_line_vpos > 0)
10828 XSETINT (w->window_end_vpos, this_line_vpos - 1);
10829 w->window_end_valid = Qnil;
10830
10831 /* Update hint: No need to try to scroll in update_window. */
10832 w->desired_matrix->no_scrolling_p = 1;
10833
10834 #if GLYPH_DEBUG
10835 *w->desired_matrix->method = 0;
10836 debug_method_add (w, "optimization 1");
10837 #endif
10838 #ifdef HAVE_WINDOW_SYSTEM
10839 update_window_fringes (w, 0);
10840 #endif
10841 goto update;
10842 }
10843 else
10844 goto cancel;
10845 }
10846 else if (/* Cursor position hasn't changed. */
10847 PT == XFASTINT (w->last_point)
10848 /* Make sure the cursor was last displayed
10849 in this window. Otherwise we have to reposition it. */
10850 && 0 <= w->cursor.vpos
10851 && WINDOW_TOTAL_LINES (w) > w->cursor.vpos)
10852 {
10853 if (!must_finish)
10854 {
10855 do_pending_window_change (1);
10856
10857 /* We used to always goto end_of_redisplay here, but this
10858 isn't enough if we have a blinking cursor. */
10859 if (w->cursor_off_p == w->last_cursor_off_p)
10860 goto end_of_redisplay;
10861 }
10862 goto update;
10863 }
10864 /* If highlighting the region, or if the cursor is in the echo area,
10865 then we can't just move the cursor. */
10866 else if (! (!NILP (Vtransient_mark_mode)
10867 && !NILP (current_buffer->mark_active))
10868 && (EQ (selected_window, current_buffer->last_selected_window)
10869 || highlight_nonselected_windows)
10870 && NILP (w->region_showing)
10871 && NILP (Vshow_trailing_whitespace)
10872 && !cursor_in_echo_area)
10873 {
10874 struct it it;
10875 struct glyph_row *row;
10876
10877 /* Skip from tlbufpos to PT and see where it is. Note that
10878 PT may be in invisible text. If so, we will end at the
10879 next visible position. */
10880 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
10881 NULL, DEFAULT_FACE_ID);
10882 it.current_x = this_line_start_x;
10883 it.current_y = this_line_y;
10884 it.vpos = this_line_vpos;
10885
10886 /* The call to move_it_to stops in front of PT, but
10887 moves over before-strings. */
10888 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
10889
10890 if (it.vpos == this_line_vpos
10891 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
10892 row->enabled_p))
10893 {
10894 xassert (this_line_vpos == it.vpos);
10895 xassert (this_line_y == it.current_y);
10896 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
10897 #if GLYPH_DEBUG
10898 *w->desired_matrix->method = 0;
10899 debug_method_add (w, "optimization 3");
10900 #endif
10901 goto update;
10902 }
10903 else
10904 goto cancel;
10905 }
10906
10907 cancel:
10908 /* Text changed drastically or point moved off of line. */
10909 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
10910 }
10911
10912 CHARPOS (this_line_start_pos) = 0;
10913 consider_all_windows_p |= buffer_shared > 1;
10914 ++clear_face_cache_count;
10915 #ifdef HAVE_WINDOW_SYSTEM
10916 ++clear_image_cache_count;
10917 #endif
10918
10919 /* Build desired matrices, and update the display. If
10920 consider_all_windows_p is non-zero, do it for all windows on all
10921 frames. Otherwise do it for selected_window, only. */
10922
10923 if (consider_all_windows_p)
10924 {
10925 Lisp_Object tail, frame;
10926
10927 FOR_EACH_FRAME (tail, frame)
10928 XFRAME (frame)->updated_p = 0;
10929
10930 /* Recompute # windows showing selected buffer. This will be
10931 incremented each time such a window is displayed. */
10932 buffer_shared = 0;
10933
10934 FOR_EACH_FRAME (tail, frame)
10935 {
10936 struct frame *f = XFRAME (frame);
10937
10938 if (FRAME_WINDOW_P (f) || FRAME_TERMCAP_P (f) || f == sf)
10939 {
10940 if (! EQ (frame, selected_frame))
10941 /* Select the frame, for the sake of frame-local
10942 variables. */
10943 select_frame_for_redisplay (frame);
10944
10945 /* Mark all the scroll bars to be removed; we'll redeem
10946 the ones we want when we redisplay their windows. */
10947 if (FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
10948 FRAME_TERMINAL (f)->condemn_scroll_bars_hook (f);
10949
10950 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
10951 redisplay_windows (FRAME_ROOT_WINDOW (f));
10952
10953 /* Any scroll bars which redisplay_windows should have
10954 nuked should now go away. */
10955 if (FRAME_TERMINAL (f)->judge_scroll_bars_hook)
10956 FRAME_TERMINAL (f)->judge_scroll_bars_hook (f);
10957
10958 /* If fonts changed, display again. */
10959 /* ??? rms: I suspect it is a mistake to jump all the way
10960 back to retry here. It should just retry this frame. */
10961 if (fonts_changed_p)
10962 goto retry;
10963
10964 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
10965 {
10966 /* See if we have to hscroll. */
10967 if (!f->already_hscrolled_p)
10968 {
10969 f->already_hscrolled_p = 1;
10970 if (hscroll_windows (f->root_window))
10971 goto retry;
10972 }
10973
10974 /* Prevent various kinds of signals during display
10975 update. stdio is not robust about handling
10976 signals, which can cause an apparent I/O
10977 error. */
10978 if (interrupt_input)
10979 unrequest_sigio ();
10980 STOP_POLLING;
10981
10982 /* Update the display. */
10983 set_window_update_flags (XWINDOW (f->root_window), 1);
10984 pause |= update_frame (f, 0, 0);
10985 #if 0 /* Exiting the loop can leave the wrong value for buffer_shared. */
10986 if (pause)
10987 break;
10988 #endif
10989
10990 f->updated_p = 1;
10991 }
10992 }
10993 }
10994
10995 if (!pause)
10996 {
10997 /* Do the mark_window_display_accurate after all windows have
10998 been redisplayed because this call resets flags in buffers
10999 which are needed for proper redisplay. */
11000 FOR_EACH_FRAME (tail, frame)
11001 {
11002 struct frame *f = XFRAME (frame);
11003 if (f->updated_p)
11004 {
11005 mark_window_display_accurate (f->root_window, 1);
11006 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
11007 FRAME_TERMINAL (f)->frame_up_to_date_hook (f);
11008 }
11009 }
11010 }
11011 }
11012 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
11013 {
11014 Lisp_Object mini_window;
11015 struct frame *mini_frame;
11016
11017 displayed_buffer = XBUFFER (XWINDOW (selected_window)->buffer);
11018 /* Use list_of_error, not Qerror, so that
11019 we catch only errors and don't run the debugger. */
11020 internal_condition_case_1 (redisplay_window_1, selected_window,
11021 list_of_error,
11022 redisplay_window_error);
11023
11024 /* Compare desired and current matrices, perform output. */
11025
11026 update:
11027 /* If fonts changed, display again. */
11028 if (fonts_changed_p)
11029 goto retry;
11030
11031 /* Prevent various kinds of signals during display update.
11032 stdio is not robust about handling signals,
11033 which can cause an apparent I/O error. */
11034 if (interrupt_input)
11035 unrequest_sigio ();
11036 STOP_POLLING;
11037
11038 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
11039 {
11040 if (hscroll_windows (selected_window))
11041 goto retry;
11042
11043 XWINDOW (selected_window)->must_be_updated_p = 1;
11044 pause = update_frame (sf, 0, 0);
11045 }
11046
11047 /* We may have called echo_area_display at the top of this
11048 function. If the echo area is on another frame, that may
11049 have put text on a frame other than the selected one, so the
11050 above call to update_frame would not have caught it. Catch
11051 it here. */
11052 mini_window = FRAME_MINIBUF_WINDOW (sf);
11053 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
11054
11055 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
11056 {
11057 XWINDOW (mini_window)->must_be_updated_p = 1;
11058 pause |= update_frame (mini_frame, 0, 0);
11059 if (!pause && hscroll_windows (mini_window))
11060 goto retry;
11061 }
11062 }
11063
11064 /* If display was paused because of pending input, make sure we do a
11065 thorough update the next time. */
11066 if (pause)
11067 {
11068 /* Prevent the optimization at the beginning of
11069 redisplay_internal that tries a single-line update of the
11070 line containing the cursor in the selected window. */
11071 CHARPOS (this_line_start_pos) = 0;
11072
11073 /* Let the overlay arrow be updated the next time. */
11074 update_overlay_arrows (0);
11075
11076 /* If we pause after scrolling, some rows in the current
11077 matrices of some windows are not valid. */
11078 if (!WINDOW_FULL_WIDTH_P (w)
11079 && !FRAME_WINDOW_P (XFRAME (w->frame)))
11080 update_mode_lines = 1;
11081 }
11082 else
11083 {
11084 if (!consider_all_windows_p)
11085 {
11086 /* This has already been done above if
11087 consider_all_windows_p is set. */
11088 mark_window_display_accurate_1 (w, 1);
11089
11090 /* Say overlay arrows are up to date. */
11091 update_overlay_arrows (1);
11092
11093 if (FRAME_TERMINAL (sf)->frame_up_to_date_hook != 0)
11094 FRAME_TERMINAL (sf)->frame_up_to_date_hook (sf);
11095 }
11096
11097 update_mode_lines = 0;
11098 windows_or_buffers_changed = 0;
11099 cursor_type_changed = 0;
11100 }
11101
11102 /* Start SIGIO interrupts coming again. Having them off during the
11103 code above makes it less likely one will discard output, but not
11104 impossible, since there might be stuff in the system buffer here.
11105 But it is much hairier to try to do anything about that. */
11106 if (interrupt_input)
11107 request_sigio ();
11108 RESUME_POLLING;
11109
11110 /* If a frame has become visible which was not before, redisplay
11111 again, so that we display it. Expose events for such a frame
11112 (which it gets when becoming visible) don't call the parts of
11113 redisplay constructing glyphs, so simply exposing a frame won't
11114 display anything in this case. So, we have to display these
11115 frames here explicitly. */
11116 if (!pause)
11117 {
11118 Lisp_Object tail, frame;
11119 int new_count = 0;
11120
11121 FOR_EACH_FRAME (tail, frame)
11122 {
11123 int this_is_visible = 0;
11124
11125 if (XFRAME (frame)->visible)
11126 this_is_visible = 1;
11127 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
11128 if (XFRAME (frame)->visible)
11129 this_is_visible = 1;
11130
11131 if (this_is_visible)
11132 new_count++;
11133 }
11134
11135 if (new_count != number_of_visible_frames)
11136 windows_or_buffers_changed++;
11137 }
11138
11139 /* Change frame size now if a change is pending. */
11140 do_pending_window_change (1);
11141
11142 /* If we just did a pending size change, or have additional
11143 visible frames, redisplay again. */
11144 if (windows_or_buffers_changed && !pause)
11145 goto retry;
11146
11147 /* Clear the face cache eventually. */
11148 if (consider_all_windows_p)
11149 {
11150 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
11151 {
11152 clear_face_cache (0);
11153 clear_face_cache_count = 0;
11154 }
11155 #ifdef HAVE_WINDOW_SYSTEM
11156 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
11157 {
11158 Lisp_Object tail, frame;
11159 FOR_EACH_FRAME (tail, frame)
11160 {
11161 struct frame *f = XFRAME (frame);
11162 if (FRAME_WINDOW_P (f))
11163 clear_image_cache (f, 0);
11164 }
11165 clear_image_cache_count = 0;
11166 }
11167 #endif /* HAVE_WINDOW_SYSTEM */
11168 }
11169
11170 end_of_redisplay:
11171 unbind_to (count, Qnil);
11172 RESUME_POLLING;
11173 }
11174
11175
11176 /* Redisplay, but leave alone any recent echo area message unless
11177 another message has been requested in its place.
11178
11179 This is useful in situations where you need to redisplay but no
11180 user action has occurred, making it inappropriate for the message
11181 area to be cleared. See tracking_off and
11182 wait_reading_process_output for examples of these situations.
11183
11184 FROM_WHERE is an integer saying from where this function was
11185 called. This is useful for debugging. */
11186
11187 void
11188 redisplay_preserve_echo_area (from_where)
11189 int from_where;
11190 {
11191 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
11192
11193 if (!NILP (echo_area_buffer[1]))
11194 {
11195 /* We have a previously displayed message, but no current
11196 message. Redisplay the previous message. */
11197 display_last_displayed_message_p = 1;
11198 redisplay_internal (1);
11199 display_last_displayed_message_p = 0;
11200 }
11201 else
11202 redisplay_internal (1);
11203
11204 if (FRAME_RIF (SELECTED_FRAME ()) != NULL
11205 && FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
11206 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (NULL);
11207 }
11208
11209
11210 /* Function registered with record_unwind_protect in
11211 redisplay_internal. Reset redisplaying_p to the value it had
11212 before redisplay_internal was called, and clear
11213 prevent_freeing_realized_faces_p. It also selects the previously
11214 selected frame, unless it has been deleted (by an X connection
11215 failure during redisplay, for example). */
11216
11217 static Lisp_Object
11218 unwind_redisplay (val)
11219 Lisp_Object val;
11220 {
11221 Lisp_Object old_redisplaying_p, old_frame;
11222
11223 old_redisplaying_p = XCAR (val);
11224 redisplaying_p = XFASTINT (old_redisplaying_p);
11225 old_frame = XCDR (val);
11226 if (! EQ (old_frame, selected_frame)
11227 && FRAME_LIVE_P (XFRAME (old_frame)))
11228 select_frame_for_redisplay (old_frame);
11229 return Qnil;
11230 }
11231
11232
11233 /* Mark the display of window W as accurate or inaccurate. If
11234 ACCURATE_P is non-zero mark display of W as accurate. If
11235 ACCURATE_P is zero, arrange for W to be redisplayed the next time
11236 redisplay_internal is called. */
11237
11238 static void
11239 mark_window_display_accurate_1 (w, accurate_p)
11240 struct window *w;
11241 int accurate_p;
11242 {
11243 if (BUFFERP (w->buffer))
11244 {
11245 struct buffer *b = XBUFFER (w->buffer);
11246
11247 w->last_modified
11248 = make_number (accurate_p ? BUF_MODIFF (b) : 0);
11249 w->last_overlay_modified
11250 = make_number (accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
11251 w->last_had_star
11252 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b) ? Qt : Qnil;
11253
11254 if (accurate_p)
11255 {
11256 b->clip_changed = 0;
11257 b->prevent_redisplay_optimizations_p = 0;
11258
11259 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
11260 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
11261 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
11262 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
11263
11264 w->current_matrix->buffer = b;
11265 w->current_matrix->begv = BUF_BEGV (b);
11266 w->current_matrix->zv = BUF_ZV (b);
11267
11268 w->last_cursor = w->cursor;
11269 w->last_cursor_off_p = w->cursor_off_p;
11270
11271 if (w == XWINDOW (selected_window))
11272 w->last_point = make_number (BUF_PT (b));
11273 else
11274 w->last_point = make_number (XMARKER (w->pointm)->charpos);
11275 }
11276 }
11277
11278 if (accurate_p)
11279 {
11280 w->window_end_valid = w->buffer;
11281 #if 0 /* This is incorrect with variable-height lines. */
11282 xassert (XINT (w->window_end_vpos)
11283 < (WINDOW_TOTAL_LINES (w)
11284 - (WINDOW_WANTS_MODELINE_P (w) ? 1 : 0)));
11285 #endif
11286 w->update_mode_line = Qnil;
11287 }
11288 }
11289
11290
11291 /* Mark the display of windows in the window tree rooted at WINDOW as
11292 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
11293 windows as accurate. If ACCURATE_P is zero, arrange for windows to
11294 be redisplayed the next time redisplay_internal is called. */
11295
11296 void
11297 mark_window_display_accurate (window, accurate_p)
11298 Lisp_Object window;
11299 int accurate_p;
11300 {
11301 struct window *w;
11302
11303 for (; !NILP (window); window = w->next)
11304 {
11305 w = XWINDOW (window);
11306 mark_window_display_accurate_1 (w, accurate_p);
11307
11308 if (!NILP (w->vchild))
11309 mark_window_display_accurate (w->vchild, accurate_p);
11310 if (!NILP (w->hchild))
11311 mark_window_display_accurate (w->hchild, accurate_p);
11312 }
11313
11314 if (accurate_p)
11315 {
11316 update_overlay_arrows (1);
11317 }
11318 else
11319 {
11320 /* Force a thorough redisplay the next time by setting
11321 last_arrow_position and last_arrow_string to t, which is
11322 unequal to any useful value of Voverlay_arrow_... */
11323 update_overlay_arrows (-1);
11324 }
11325 }
11326
11327
11328 /* Return value in display table DP (Lisp_Char_Table *) for character
11329 C. Since a display table doesn't have any parent, we don't have to
11330 follow parent. Do not call this function directly but use the
11331 macro DISP_CHAR_VECTOR. */
11332
11333 Lisp_Object
11334 disp_char_vector (dp, c)
11335 struct Lisp_Char_Table *dp;
11336 int c;
11337 {
11338 int code[4], i;
11339 Lisp_Object val;
11340
11341 if (SINGLE_BYTE_CHAR_P (c))
11342 return (dp->contents[c]);
11343
11344 SPLIT_CHAR (c, code[0], code[1], code[2]);
11345 if (code[1] < 32)
11346 code[1] = -1;
11347 else if (code[2] < 32)
11348 code[2] = -1;
11349
11350 /* Here, the possible range of code[0] (== charset ID) is
11351 128..max_charset. Since the top level char table contains data
11352 for multibyte characters after 256th element, we must increment
11353 code[0] by 128 to get a correct index. */
11354 code[0] += 128;
11355 code[3] = -1; /* anchor */
11356
11357 for (i = 0; code[i] >= 0; i++, dp = XCHAR_TABLE (val))
11358 {
11359 val = dp->contents[code[i]];
11360 if (!SUB_CHAR_TABLE_P (val))
11361 return (NILP (val) ? dp->defalt : val);
11362 }
11363
11364 /* Here, val is a sub char table. We return the default value of
11365 it. */
11366 return (dp->defalt);
11367 }
11368
11369
11370 \f
11371 /***********************************************************************
11372 Window Redisplay
11373 ***********************************************************************/
11374
11375 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
11376
11377 static void
11378 redisplay_windows (window)
11379 Lisp_Object window;
11380 {
11381 while (!NILP (window))
11382 {
11383 struct window *w = XWINDOW (window);
11384
11385 if (!NILP (w->hchild))
11386 redisplay_windows (w->hchild);
11387 else if (!NILP (w->vchild))
11388 redisplay_windows (w->vchild);
11389 else
11390 {
11391 displayed_buffer = XBUFFER (w->buffer);
11392 /* Use list_of_error, not Qerror, so that
11393 we catch only errors and don't run the debugger. */
11394 internal_condition_case_1 (redisplay_window_0, window,
11395 list_of_error,
11396 redisplay_window_error);
11397 }
11398
11399 window = w->next;
11400 }
11401 }
11402
11403 static Lisp_Object
11404 redisplay_window_error ()
11405 {
11406 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
11407 return Qnil;
11408 }
11409
11410 static Lisp_Object
11411 redisplay_window_0 (window)
11412 Lisp_Object window;
11413 {
11414 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
11415 redisplay_window (window, 0);
11416 return Qnil;
11417 }
11418
11419 static Lisp_Object
11420 redisplay_window_1 (window)
11421 Lisp_Object window;
11422 {
11423 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
11424 redisplay_window (window, 1);
11425 return Qnil;
11426 }
11427 \f
11428
11429 /* Increment GLYPH until it reaches END or CONDITION fails while
11430 adding (GLYPH)->pixel_width to X. */
11431
11432 #define SKIP_GLYPHS(glyph, end, x, condition) \
11433 do \
11434 { \
11435 (x) += (glyph)->pixel_width; \
11436 ++(glyph); \
11437 } \
11438 while ((glyph) < (end) && (condition))
11439
11440
11441 /* Set cursor position of W. PT is assumed to be displayed in ROW.
11442 DELTA is the number of bytes by which positions recorded in ROW
11443 differ from current buffer positions. */
11444
11445 void
11446 set_cursor_from_row (w, row, matrix, delta, delta_bytes, dy, dvpos)
11447 struct window *w;
11448 struct glyph_row *row;
11449 struct glyph_matrix *matrix;
11450 int delta, delta_bytes, dy, dvpos;
11451 {
11452 struct glyph *glyph = row->glyphs[TEXT_AREA];
11453 struct glyph *end = glyph + row->used[TEXT_AREA];
11454 struct glyph *cursor = NULL;
11455 /* The first glyph that starts a sequence of glyphs from string. */
11456 struct glyph *string_start;
11457 /* The X coordinate of string_start. */
11458 int string_start_x;
11459 /* The last known character position. */
11460 int last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
11461 /* The last known character position before string_start. */
11462 int string_before_pos;
11463 int x = row->x;
11464 int cursor_x = x;
11465 int cursor_from_overlay_pos = 0;
11466 int pt_old = PT - delta;
11467
11468 /* Skip over glyphs not having an object at the start of the row.
11469 These are special glyphs like truncation marks on terminal
11470 frames. */
11471 if (row->displays_text_p)
11472 while (glyph < end
11473 && INTEGERP (glyph->object)
11474 && glyph->charpos < 0)
11475 {
11476 x += glyph->pixel_width;
11477 ++glyph;
11478 }
11479
11480 string_start = NULL;
11481 while (glyph < end
11482 && !INTEGERP (glyph->object)
11483 && (!BUFFERP (glyph->object)
11484 || (last_pos = glyph->charpos) < pt_old))
11485 {
11486 if (! STRINGP (glyph->object))
11487 {
11488 string_start = NULL;
11489 x += glyph->pixel_width;
11490 ++glyph;
11491 if (cursor_from_overlay_pos
11492 && last_pos > cursor_from_overlay_pos)
11493 {
11494 cursor_from_overlay_pos = 0;
11495 cursor = 0;
11496 }
11497 }
11498 else
11499 {
11500 string_before_pos = last_pos;
11501 string_start = glyph;
11502 string_start_x = x;
11503 /* Skip all glyphs from string. */
11504 do
11505 {
11506 int pos;
11507 if ((cursor == NULL || glyph > cursor)
11508 && !NILP (Fget_char_property (make_number ((glyph)->charpos),
11509 Qcursor, (glyph)->object))
11510 && (pos = string_buffer_position (w, glyph->object,
11511 string_before_pos),
11512 (pos == 0 /* From overlay */
11513 || pos == pt_old)))
11514 {
11515 /* Estimate overlay buffer position from the buffer
11516 positions of the glyphs before and after the overlay.
11517 Add 1 to last_pos so that if point corresponds to the
11518 glyph right after the overlay, we still use a 'cursor'
11519 property found in that overlay. */
11520 cursor_from_overlay_pos = pos == 0 ? last_pos+1 : 0;
11521 cursor = glyph;
11522 cursor_x = x;
11523 }
11524 x += glyph->pixel_width;
11525 ++glyph;
11526 }
11527 while (glyph < end && STRINGP (glyph->object));
11528 }
11529 }
11530
11531 if (cursor != NULL)
11532 {
11533 glyph = cursor;
11534 x = cursor_x;
11535 }
11536 else if (row->ends_in_ellipsis_p && glyph == end)
11537 {
11538 /* Scan back over the ellipsis glyphs, decrementing positions. */
11539 while (glyph > row->glyphs[TEXT_AREA]
11540 && (glyph - 1)->charpos == last_pos)
11541 glyph--, x -= glyph->pixel_width;
11542 /* That loop always goes one position too far,
11543 including the glyph before the ellipsis.
11544 So scan forward over that one. */
11545 x += glyph->pixel_width;
11546 glyph++;
11547 }
11548 else if (string_start
11549 && (glyph == end || !BUFFERP (glyph->object) || last_pos > pt_old))
11550 {
11551 /* We may have skipped over point because the previous glyphs
11552 are from string. As there's no easy way to know the
11553 character position of the current glyph, find the correct
11554 glyph on point by scanning from string_start again. */
11555 Lisp_Object limit;
11556 Lisp_Object string;
11557 int pos;
11558
11559 limit = make_number (pt_old + 1);
11560 end = glyph;
11561 glyph = string_start;
11562 x = string_start_x;
11563 string = glyph->object;
11564 pos = string_buffer_position (w, string, string_before_pos);
11565 /* If STRING is from overlay, LAST_POS == 0. We skip such glyphs
11566 because we always put cursor after overlay strings. */
11567 while (pos == 0 && glyph < end)
11568 {
11569 string = glyph->object;
11570 SKIP_GLYPHS (glyph, end, x, EQ (glyph->object, string));
11571 if (glyph < end)
11572 pos = string_buffer_position (w, glyph->object, string_before_pos);
11573 }
11574
11575 while (glyph < end)
11576 {
11577 pos = XINT (Fnext_single_char_property_change
11578 (make_number (pos), Qdisplay, Qnil, limit));
11579 if (pos > pt_old)
11580 break;
11581 /* Skip glyphs from the same string. */
11582 string = glyph->object;
11583 SKIP_GLYPHS (glyph, end, x, EQ (glyph->object, string));
11584 /* Skip glyphs from an overlay. */
11585 while (glyph < end
11586 && ! string_buffer_position (w, glyph->object, pos))
11587 {
11588 string = glyph->object;
11589 SKIP_GLYPHS (glyph, end, x, EQ (glyph->object, string));
11590 }
11591 }
11592 }
11593
11594 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
11595 w->cursor.x = x;
11596 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
11597 w->cursor.y = row->y + dy;
11598
11599 if (w == XWINDOW (selected_window))
11600 {
11601 if (!row->continued_p
11602 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
11603 && row->x == 0)
11604 {
11605 this_line_buffer = XBUFFER (w->buffer);
11606
11607 CHARPOS (this_line_start_pos)
11608 = MATRIX_ROW_START_CHARPOS (row) + delta;
11609 BYTEPOS (this_line_start_pos)
11610 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
11611
11612 CHARPOS (this_line_end_pos)
11613 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
11614 BYTEPOS (this_line_end_pos)
11615 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
11616
11617 this_line_y = w->cursor.y;
11618 this_line_pixel_height = row->height;
11619 this_line_vpos = w->cursor.vpos;
11620 this_line_start_x = row->x;
11621 }
11622 else
11623 CHARPOS (this_line_start_pos) = 0;
11624 }
11625 }
11626
11627
11628 /* Run window scroll functions, if any, for WINDOW with new window
11629 start STARTP. Sets the window start of WINDOW to that position.
11630
11631 We assume that the window's buffer is really current. */
11632
11633 static INLINE struct text_pos
11634 run_window_scroll_functions (window, startp)
11635 Lisp_Object window;
11636 struct text_pos startp;
11637 {
11638 struct window *w = XWINDOW (window);
11639 SET_MARKER_FROM_TEXT_POS (w->start, startp);
11640
11641 if (current_buffer != XBUFFER (w->buffer))
11642 abort ();
11643
11644 if (!NILP (Vwindow_scroll_functions))
11645 {
11646 run_hook_with_args_2 (Qwindow_scroll_functions, window,
11647 make_number (CHARPOS (startp)));
11648 SET_TEXT_POS_FROM_MARKER (startp, w->start);
11649 /* In case the hook functions switch buffers. */
11650 if (current_buffer != XBUFFER (w->buffer))
11651 set_buffer_internal_1 (XBUFFER (w->buffer));
11652 }
11653
11654 return startp;
11655 }
11656
11657
11658 /* Make sure the line containing the cursor is fully visible.
11659 A value of 1 means there is nothing to be done.
11660 (Either the line is fully visible, or it cannot be made so,
11661 or we cannot tell.)
11662
11663 If FORCE_P is non-zero, return 0 even if partial visible cursor row
11664 is higher than window.
11665
11666 A value of 0 means the caller should do scrolling
11667 as if point had gone off the screen. */
11668
11669 static int
11670 cursor_row_fully_visible_p (w, force_p, current_matrix_p)
11671 struct window *w;
11672 int force_p;
11673 int current_matrix_p;
11674 {
11675 struct glyph_matrix *matrix;
11676 struct glyph_row *row;
11677 int window_height;
11678
11679 if (!make_cursor_line_fully_visible_p)
11680 return 1;
11681
11682 /* It's not always possible to find the cursor, e.g, when a window
11683 is full of overlay strings. Don't do anything in that case. */
11684 if (w->cursor.vpos < 0)
11685 return 1;
11686
11687 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
11688 row = MATRIX_ROW (matrix, w->cursor.vpos);
11689
11690 /* If the cursor row is not partially visible, there's nothing to do. */
11691 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
11692 return 1;
11693
11694 /* If the row the cursor is in is taller than the window's height,
11695 it's not clear what to do, so do nothing. */
11696 window_height = window_box_height (w);
11697 if (row->height >= window_height)
11698 {
11699 if (!force_p || MINI_WINDOW_P (w) || w->vscroll)
11700 return 1;
11701 }
11702 return 0;
11703
11704 #if 0
11705 /* This code used to try to scroll the window just enough to make
11706 the line visible. It returned 0 to say that the caller should
11707 allocate larger glyph matrices. */
11708
11709 if (MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (w, row))
11710 {
11711 int dy = row->height - row->visible_height;
11712 w->vscroll = 0;
11713 w->cursor.y += dy;
11714 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
11715 }
11716 else /* MATRIX_ROW_PARTIALLY_VISIBLE_AT_BOTTOM_P (w, row)) */
11717 {
11718 int dy = - (row->height - row->visible_height);
11719 w->vscroll = dy;
11720 w->cursor.y += dy;
11721 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
11722 }
11723
11724 /* When we change the cursor y-position of the selected window,
11725 change this_line_y as well so that the display optimization for
11726 the cursor line of the selected window in redisplay_internal uses
11727 the correct y-position. */
11728 if (w == XWINDOW (selected_window))
11729 this_line_y = w->cursor.y;
11730
11731 /* If vscrolling requires a larger glyph matrix, arrange for a fresh
11732 redisplay with larger matrices. */
11733 if (matrix->nrows < required_matrix_height (w))
11734 {
11735 fonts_changed_p = 1;
11736 return 0;
11737 }
11738
11739 return 1;
11740 #endif /* 0 */
11741 }
11742
11743
11744 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
11745 non-zero means only WINDOW is redisplayed in redisplay_internal.
11746 TEMP_SCROLL_STEP has the same meaning as scroll_step, and is used
11747 in redisplay_window to bring a partially visible line into view in
11748 the case that only the cursor has moved.
11749
11750 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
11751 last screen line's vertical height extends past the end of the screen.
11752
11753 Value is
11754
11755 1 if scrolling succeeded
11756
11757 0 if scrolling didn't find point.
11758
11759 -1 if new fonts have been loaded so that we must interrupt
11760 redisplay, adjust glyph matrices, and try again. */
11761
11762 enum
11763 {
11764 SCROLLING_SUCCESS,
11765 SCROLLING_FAILED,
11766 SCROLLING_NEED_LARGER_MATRICES
11767 };
11768
11769 static int
11770 try_scrolling (window, just_this_one_p, scroll_conservatively,
11771 scroll_step, temp_scroll_step, last_line_misfit)
11772 Lisp_Object window;
11773 int just_this_one_p;
11774 EMACS_INT scroll_conservatively, scroll_step;
11775 int temp_scroll_step;
11776 int last_line_misfit;
11777 {
11778 struct window *w = XWINDOW (window);
11779 struct frame *f = XFRAME (w->frame);
11780 struct text_pos scroll_margin_pos;
11781 struct text_pos pos;
11782 struct text_pos startp;
11783 struct it it;
11784 Lisp_Object window_end;
11785 int this_scroll_margin;
11786 int dy = 0;
11787 int scroll_max;
11788 int rc;
11789 int amount_to_scroll = 0;
11790 Lisp_Object aggressive;
11791 int height;
11792 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
11793
11794 #if GLYPH_DEBUG
11795 debug_method_add (w, "try_scrolling");
11796 #endif
11797
11798 SET_TEXT_POS_FROM_MARKER (startp, w->start);
11799
11800 /* Compute scroll margin height in pixels. We scroll when point is
11801 within this distance from the top or bottom of the window. */
11802 if (scroll_margin > 0)
11803 {
11804 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
11805 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
11806 }
11807 else
11808 this_scroll_margin = 0;
11809
11810 /* Force scroll_conservatively to have a reasonable value so it doesn't
11811 cause an overflow while computing how much to scroll. */
11812 if (scroll_conservatively)
11813 scroll_conservatively = min (scroll_conservatively,
11814 MOST_POSITIVE_FIXNUM / FRAME_LINE_HEIGHT (f));
11815
11816 /* Compute how much we should try to scroll maximally to bring point
11817 into view. */
11818 if (scroll_step || scroll_conservatively || temp_scroll_step)
11819 scroll_max = max (scroll_step,
11820 max (scroll_conservatively, temp_scroll_step));
11821 else if (NUMBERP (current_buffer->scroll_down_aggressively)
11822 || NUMBERP (current_buffer->scroll_up_aggressively))
11823 /* We're trying to scroll because of aggressive scrolling
11824 but no scroll_step is set. Choose an arbitrary one. Maybe
11825 there should be a variable for this. */
11826 scroll_max = 10;
11827 else
11828 scroll_max = 0;
11829 scroll_max *= FRAME_LINE_HEIGHT (f);
11830
11831 /* Decide whether we have to scroll down. Start at the window end
11832 and move this_scroll_margin up to find the position of the scroll
11833 margin. */
11834 window_end = Fwindow_end (window, Qt);
11835
11836 too_near_end:
11837
11838 CHARPOS (scroll_margin_pos) = XINT (window_end);
11839 BYTEPOS (scroll_margin_pos) = CHAR_TO_BYTE (CHARPOS (scroll_margin_pos));
11840
11841 if (this_scroll_margin || extra_scroll_margin_lines)
11842 {
11843 start_display (&it, w, scroll_margin_pos);
11844 if (this_scroll_margin)
11845 move_it_vertically_backward (&it, this_scroll_margin);
11846 if (extra_scroll_margin_lines)
11847 move_it_by_lines (&it, - extra_scroll_margin_lines, 0);
11848 scroll_margin_pos = it.current.pos;
11849 }
11850
11851 if (PT >= CHARPOS (scroll_margin_pos))
11852 {
11853 int y0;
11854
11855 /* Point is in the scroll margin at the bottom of the window, or
11856 below. Compute a new window start that makes point visible. */
11857
11858 /* Compute the distance from the scroll margin to PT.
11859 Give up if the distance is greater than scroll_max. */
11860 start_display (&it, w, scroll_margin_pos);
11861 y0 = it.current_y;
11862 move_it_to (&it, PT, 0, it.last_visible_y, -1,
11863 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
11864
11865 /* To make point visible, we have to move the window start
11866 down so that the line the cursor is in is visible, which
11867 means we have to add in the height of the cursor line. */
11868 dy = line_bottom_y (&it) - y0;
11869
11870 if (dy > scroll_max)
11871 return SCROLLING_FAILED;
11872
11873 /* Move the window start down. If scrolling conservatively,
11874 move it just enough down to make point visible. If
11875 scroll_step is set, move it down by scroll_step. */
11876 start_display (&it, w, startp);
11877
11878 if (scroll_conservatively)
11879 /* Set AMOUNT_TO_SCROLL to at least one line,
11880 and at most scroll_conservatively lines. */
11881 amount_to_scroll
11882 = min (max (dy, FRAME_LINE_HEIGHT (f)),
11883 FRAME_LINE_HEIGHT (f) * scroll_conservatively);
11884 else if (scroll_step || temp_scroll_step)
11885 amount_to_scroll = scroll_max;
11886 else
11887 {
11888 aggressive = current_buffer->scroll_up_aggressively;
11889 height = WINDOW_BOX_TEXT_HEIGHT (w);
11890 if (NUMBERP (aggressive))
11891 {
11892 double float_amount = XFLOATINT (aggressive) * height;
11893 amount_to_scroll = float_amount;
11894 if (amount_to_scroll == 0 && float_amount > 0)
11895 amount_to_scroll = 1;
11896 }
11897 }
11898
11899 if (amount_to_scroll <= 0)
11900 return SCROLLING_FAILED;
11901
11902 /* If moving by amount_to_scroll leaves STARTP unchanged,
11903 move it down one screen line. */
11904
11905 move_it_vertically (&it, amount_to_scroll);
11906 if (CHARPOS (it.current.pos) == CHARPOS (startp))
11907 move_it_by_lines (&it, 1, 1);
11908 startp = it.current.pos;
11909 }
11910 else
11911 {
11912 /* See if point is inside the scroll margin at the top of the
11913 window. */
11914 scroll_margin_pos = startp;
11915 if (this_scroll_margin)
11916 {
11917 start_display (&it, w, startp);
11918 move_it_vertically (&it, this_scroll_margin);
11919 scroll_margin_pos = it.current.pos;
11920 }
11921
11922 if (PT < CHARPOS (scroll_margin_pos))
11923 {
11924 /* Point is in the scroll margin at the top of the window or
11925 above what is displayed in the window. */
11926 int y0;
11927
11928 /* Compute the vertical distance from PT to the scroll
11929 margin position. Give up if distance is greater than
11930 scroll_max. */
11931 SET_TEXT_POS (pos, PT, PT_BYTE);
11932 start_display (&it, w, pos);
11933 y0 = it.current_y;
11934 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
11935 it.last_visible_y, -1,
11936 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
11937 dy = it.current_y - y0;
11938 if (dy > scroll_max)
11939 return SCROLLING_FAILED;
11940
11941 /* Compute new window start. */
11942 start_display (&it, w, startp);
11943
11944 if (scroll_conservatively)
11945 amount_to_scroll
11946 = max (dy, FRAME_LINE_HEIGHT (f) * max (scroll_step, temp_scroll_step));
11947 else if (scroll_step || temp_scroll_step)
11948 amount_to_scroll = scroll_max;
11949 else
11950 {
11951 aggressive = current_buffer->scroll_down_aggressively;
11952 height = WINDOW_BOX_TEXT_HEIGHT (w);
11953 if (NUMBERP (aggressive))
11954 {
11955 double float_amount = XFLOATINT (aggressive) * height;
11956 amount_to_scroll = float_amount;
11957 if (amount_to_scroll == 0 && float_amount > 0)
11958 amount_to_scroll = 1;
11959 }
11960 }
11961
11962 if (amount_to_scroll <= 0)
11963 return SCROLLING_FAILED;
11964
11965 move_it_vertically_backward (&it, amount_to_scroll);
11966 startp = it.current.pos;
11967 }
11968 }
11969
11970 /* Run window scroll functions. */
11971 startp = run_window_scroll_functions (window, startp);
11972
11973 /* Display the window. Give up if new fonts are loaded, or if point
11974 doesn't appear. */
11975 if (!try_window (window, startp, 0))
11976 rc = SCROLLING_NEED_LARGER_MATRICES;
11977 else if (w->cursor.vpos < 0)
11978 {
11979 clear_glyph_matrix (w->desired_matrix);
11980 rc = SCROLLING_FAILED;
11981 }
11982 else
11983 {
11984 /* Maybe forget recorded base line for line number display. */
11985 if (!just_this_one_p
11986 || current_buffer->clip_changed
11987 || BEG_UNCHANGED < CHARPOS (startp))
11988 w->base_line_number = Qnil;
11989
11990 /* If cursor ends up on a partially visible line,
11991 treat that as being off the bottom of the screen. */
11992 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1, 0))
11993 {
11994 clear_glyph_matrix (w->desired_matrix);
11995 ++extra_scroll_margin_lines;
11996 goto too_near_end;
11997 }
11998 rc = SCROLLING_SUCCESS;
11999 }
12000
12001 return rc;
12002 }
12003
12004
12005 /* Compute a suitable window start for window W if display of W starts
12006 on a continuation line. Value is non-zero if a new window start
12007 was computed.
12008
12009 The new window start will be computed, based on W's width, starting
12010 from the start of the continued line. It is the start of the
12011 screen line with the minimum distance from the old start W->start. */
12012
12013 static int
12014 compute_window_start_on_continuation_line (w)
12015 struct window *w;
12016 {
12017 struct text_pos pos, start_pos;
12018 int window_start_changed_p = 0;
12019
12020 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
12021
12022 /* If window start is on a continuation line... Window start may be
12023 < BEGV in case there's invisible text at the start of the
12024 buffer (M-x rmail, for example). */
12025 if (CHARPOS (start_pos) > BEGV
12026 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
12027 {
12028 struct it it;
12029 struct glyph_row *row;
12030
12031 /* Handle the case that the window start is out of range. */
12032 if (CHARPOS (start_pos) < BEGV)
12033 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
12034 else if (CHARPOS (start_pos) > ZV)
12035 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
12036
12037 /* Find the start of the continued line. This should be fast
12038 because scan_buffer is fast (newline cache). */
12039 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
12040 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
12041 row, DEFAULT_FACE_ID);
12042 reseat_at_previous_visible_line_start (&it);
12043
12044 /* If the line start is "too far" away from the window start,
12045 say it takes too much time to compute a new window start. */
12046 if (CHARPOS (start_pos) - IT_CHARPOS (it)
12047 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
12048 {
12049 int min_distance, distance;
12050
12051 /* Move forward by display lines to find the new window
12052 start. If window width was enlarged, the new start can
12053 be expected to be > the old start. If window width was
12054 decreased, the new window start will be < the old start.
12055 So, we're looking for the display line start with the
12056 minimum distance from the old window start. */
12057 pos = it.current.pos;
12058 min_distance = INFINITY;
12059 while ((distance = abs (CHARPOS (start_pos) - IT_CHARPOS (it))),
12060 distance < min_distance)
12061 {
12062 min_distance = distance;
12063 pos = it.current.pos;
12064 move_it_by_lines (&it, 1, 0);
12065 }
12066
12067 /* Set the window start there. */
12068 SET_MARKER_FROM_TEXT_POS (w->start, pos);
12069 window_start_changed_p = 1;
12070 }
12071 }
12072
12073 return window_start_changed_p;
12074 }
12075
12076
12077 /* Try cursor movement in case text has not changed in window WINDOW,
12078 with window start STARTP. Value is
12079
12080 CURSOR_MOVEMENT_SUCCESS if successful
12081
12082 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
12083
12084 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
12085 display. *SCROLL_STEP is set to 1, under certain circumstances, if
12086 we want to scroll as if scroll-step were set to 1. See the code.
12087
12088 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
12089 which case we have to abort this redisplay, and adjust matrices
12090 first. */
12091
12092 enum
12093 {
12094 CURSOR_MOVEMENT_SUCCESS,
12095 CURSOR_MOVEMENT_CANNOT_BE_USED,
12096 CURSOR_MOVEMENT_MUST_SCROLL,
12097 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
12098 };
12099
12100 static int
12101 try_cursor_movement (window, startp, scroll_step)
12102 Lisp_Object window;
12103 struct text_pos startp;
12104 int *scroll_step;
12105 {
12106 struct window *w = XWINDOW (window);
12107 struct frame *f = XFRAME (w->frame);
12108 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
12109
12110 #if GLYPH_DEBUG
12111 if (inhibit_try_cursor_movement)
12112 return rc;
12113 #endif
12114
12115 /* Handle case where text has not changed, only point, and it has
12116 not moved off the frame. */
12117 if (/* Point may be in this window. */
12118 PT >= CHARPOS (startp)
12119 /* Selective display hasn't changed. */
12120 && !current_buffer->clip_changed
12121 /* Function force-mode-line-update is used to force a thorough
12122 redisplay. It sets either windows_or_buffers_changed or
12123 update_mode_lines. So don't take a shortcut here for these
12124 cases. */
12125 && !update_mode_lines
12126 && !windows_or_buffers_changed
12127 && !cursor_type_changed
12128 /* Can't use this case if highlighting a region. When a
12129 region exists, cursor movement has to do more than just
12130 set the cursor. */
12131 && !(!NILP (Vtransient_mark_mode)
12132 && !NILP (current_buffer->mark_active))
12133 && NILP (w->region_showing)
12134 && NILP (Vshow_trailing_whitespace)
12135 /* Right after splitting windows, last_point may be nil. */
12136 && INTEGERP (w->last_point)
12137 /* This code is not used for mini-buffer for the sake of the case
12138 of redisplaying to replace an echo area message; since in
12139 that case the mini-buffer contents per se are usually
12140 unchanged. This code is of no real use in the mini-buffer
12141 since the handling of this_line_start_pos, etc., in redisplay
12142 handles the same cases. */
12143 && !EQ (window, minibuf_window)
12144 /* When splitting windows or for new windows, it happens that
12145 redisplay is called with a nil window_end_vpos or one being
12146 larger than the window. This should really be fixed in
12147 window.c. I don't have this on my list, now, so we do
12148 approximately the same as the old redisplay code. --gerd. */
12149 && INTEGERP (w->window_end_vpos)
12150 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
12151 && (FRAME_WINDOW_P (f)
12152 || !overlay_arrow_in_current_buffer_p ()))
12153 {
12154 int this_scroll_margin, top_scroll_margin;
12155 struct glyph_row *row = NULL;
12156
12157 #if GLYPH_DEBUG
12158 debug_method_add (w, "cursor movement");
12159 #endif
12160
12161 /* Scroll if point within this distance from the top or bottom
12162 of the window. This is a pixel value. */
12163 this_scroll_margin = max (0, scroll_margin);
12164 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
12165 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
12166
12167 top_scroll_margin = this_scroll_margin;
12168 if (WINDOW_WANTS_HEADER_LINE_P (w))
12169 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
12170
12171 /* Start with the row the cursor was displayed during the last
12172 not paused redisplay. Give up if that row is not valid. */
12173 if (w->last_cursor.vpos < 0
12174 || w->last_cursor.vpos >= w->current_matrix->nrows)
12175 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12176 else
12177 {
12178 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
12179 if (row->mode_line_p)
12180 ++row;
12181 if (!row->enabled_p)
12182 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12183 }
12184
12185 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
12186 {
12187 int scroll_p = 0;
12188 int last_y = window_text_bottom_y (w) - this_scroll_margin;
12189
12190 if (PT > XFASTINT (w->last_point))
12191 {
12192 /* Point has moved forward. */
12193 while (MATRIX_ROW_END_CHARPOS (row) < PT
12194 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
12195 {
12196 xassert (row->enabled_p);
12197 ++row;
12198 }
12199
12200 /* The end position of a row equals the start position
12201 of the next row. If PT is there, we would rather
12202 display it in the next line. */
12203 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
12204 && MATRIX_ROW_END_CHARPOS (row) == PT
12205 && !cursor_row_p (w, row))
12206 ++row;
12207
12208 /* If within the scroll margin, scroll. Note that
12209 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
12210 the next line would be drawn, and that
12211 this_scroll_margin can be zero. */
12212 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
12213 || PT > MATRIX_ROW_END_CHARPOS (row)
12214 /* Line is completely visible last line in window
12215 and PT is to be set in the next line. */
12216 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
12217 && PT == MATRIX_ROW_END_CHARPOS (row)
12218 && !row->ends_at_zv_p
12219 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
12220 scroll_p = 1;
12221 }
12222 else if (PT < XFASTINT (w->last_point))
12223 {
12224 /* Cursor has to be moved backward. Note that PT >=
12225 CHARPOS (startp) because of the outer if-statement. */
12226 while (!row->mode_line_p
12227 && (MATRIX_ROW_START_CHARPOS (row) > PT
12228 || (MATRIX_ROW_START_CHARPOS (row) == PT
12229 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
12230 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
12231 row > w->current_matrix->rows
12232 && (row-1)->ends_in_newline_from_string_p))))
12233 && (row->y > top_scroll_margin
12234 || CHARPOS (startp) == BEGV))
12235 {
12236 xassert (row->enabled_p);
12237 --row;
12238 }
12239
12240 /* Consider the following case: Window starts at BEGV,
12241 there is invisible, intangible text at BEGV, so that
12242 display starts at some point START > BEGV. It can
12243 happen that we are called with PT somewhere between
12244 BEGV and START. Try to handle that case. */
12245 if (row < w->current_matrix->rows
12246 || row->mode_line_p)
12247 {
12248 row = w->current_matrix->rows;
12249 if (row->mode_line_p)
12250 ++row;
12251 }
12252
12253 /* Due to newlines in overlay strings, we may have to
12254 skip forward over overlay strings. */
12255 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
12256 && MATRIX_ROW_END_CHARPOS (row) == PT
12257 && !cursor_row_p (w, row))
12258 ++row;
12259
12260 /* If within the scroll margin, scroll. */
12261 if (row->y < top_scroll_margin
12262 && CHARPOS (startp) != BEGV)
12263 scroll_p = 1;
12264 }
12265 else
12266 {
12267 /* Cursor did not move. So don't scroll even if cursor line
12268 is partially visible, as it was so before. */
12269 rc = CURSOR_MOVEMENT_SUCCESS;
12270 }
12271
12272 if (PT < MATRIX_ROW_START_CHARPOS (row)
12273 || PT > MATRIX_ROW_END_CHARPOS (row))
12274 {
12275 /* if PT is not in the glyph row, give up. */
12276 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12277 }
12278 else if (rc != CURSOR_MOVEMENT_SUCCESS
12279 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
12280 && make_cursor_line_fully_visible_p)
12281 {
12282 if (PT == MATRIX_ROW_END_CHARPOS (row)
12283 && !row->ends_at_zv_p
12284 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
12285 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12286 else if (row->height > window_box_height (w))
12287 {
12288 /* If we end up in a partially visible line, let's
12289 make it fully visible, except when it's taller
12290 than the window, in which case we can't do much
12291 about it. */
12292 *scroll_step = 1;
12293 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12294 }
12295 else
12296 {
12297 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
12298 if (!cursor_row_fully_visible_p (w, 0, 1))
12299 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12300 else
12301 rc = CURSOR_MOVEMENT_SUCCESS;
12302 }
12303 }
12304 else if (scroll_p)
12305 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12306 else
12307 {
12308 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
12309 rc = CURSOR_MOVEMENT_SUCCESS;
12310 }
12311 }
12312 }
12313
12314 return rc;
12315 }
12316
12317 void
12318 set_vertical_scroll_bar (w)
12319 struct window *w;
12320 {
12321 int start, end, whole;
12322
12323 /* Calculate the start and end positions for the current window.
12324 At some point, it would be nice to choose between scrollbars
12325 which reflect the whole buffer size, with special markers
12326 indicating narrowing, and scrollbars which reflect only the
12327 visible region.
12328
12329 Note that mini-buffers sometimes aren't displaying any text. */
12330 if (!MINI_WINDOW_P (w)
12331 || (w == XWINDOW (minibuf_window)
12332 && NILP (echo_area_buffer[0])))
12333 {
12334 struct buffer *buf = XBUFFER (w->buffer);
12335 whole = BUF_ZV (buf) - BUF_BEGV (buf);
12336 start = marker_position (w->start) - BUF_BEGV (buf);
12337 /* I don't think this is guaranteed to be right. For the
12338 moment, we'll pretend it is. */
12339 end = BUF_Z (buf) - XFASTINT (w->window_end_pos) - BUF_BEGV (buf);
12340
12341 if (end < start)
12342 end = start;
12343 if (whole < (end - start))
12344 whole = end - start;
12345 }
12346 else
12347 start = end = whole = 0;
12348
12349 /* Indicate what this scroll bar ought to be displaying now. */
12350 if (FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
12351 (*FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
12352 (w, end - start, whole, start);
12353 }
12354
12355
12356 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
12357 selected_window is redisplayed.
12358
12359 We can return without actually redisplaying the window if
12360 fonts_changed_p is nonzero. In that case, redisplay_internal will
12361 retry. */
12362
12363 static void
12364 redisplay_window (window, just_this_one_p)
12365 Lisp_Object window;
12366 int just_this_one_p;
12367 {
12368 struct window *w = XWINDOW (window);
12369 struct frame *f = XFRAME (w->frame);
12370 struct buffer *buffer = XBUFFER (w->buffer);
12371 struct buffer *old = current_buffer;
12372 struct text_pos lpoint, opoint, startp;
12373 int update_mode_line;
12374 int tem;
12375 struct it it;
12376 /* Record it now because it's overwritten. */
12377 int current_matrix_up_to_date_p = 0;
12378 int used_current_matrix_p = 0;
12379 /* This is less strict than current_matrix_up_to_date_p.
12380 It indictes that the buffer contents and narrowing are unchanged. */
12381 int buffer_unchanged_p = 0;
12382 int temp_scroll_step = 0;
12383 int count = SPECPDL_INDEX ();
12384 int rc;
12385 int centering_position = -1;
12386 int last_line_misfit = 0;
12387
12388 SET_TEXT_POS (lpoint, PT, PT_BYTE);
12389 opoint = lpoint;
12390
12391 /* W must be a leaf window here. */
12392 xassert (!NILP (w->buffer));
12393 #if GLYPH_DEBUG
12394 *w->desired_matrix->method = 0;
12395 #endif
12396
12397 specbind (Qinhibit_point_motion_hooks, Qt);
12398
12399 reconsider_clip_changes (w, buffer);
12400
12401 /* Has the mode line to be updated? */
12402 update_mode_line = (!NILP (w->update_mode_line)
12403 || update_mode_lines
12404 || buffer->clip_changed
12405 || buffer->prevent_redisplay_optimizations_p);
12406
12407 if (MINI_WINDOW_P (w))
12408 {
12409 if (w == XWINDOW (echo_area_window)
12410 && !NILP (echo_area_buffer[0]))
12411 {
12412 if (update_mode_line)
12413 /* We may have to update a tty frame's menu bar or a
12414 tool-bar. Example `M-x C-h C-h C-g'. */
12415 goto finish_menu_bars;
12416 else
12417 /* We've already displayed the echo area glyphs in this window. */
12418 goto finish_scroll_bars;
12419 }
12420 else if ((w != XWINDOW (minibuf_window)
12421 || minibuf_level == 0)
12422 /* When buffer is nonempty, redisplay window normally. */
12423 && BUF_Z (XBUFFER (w->buffer)) == BUF_BEG (XBUFFER (w->buffer))
12424 /* Quail displays non-mini buffers in minibuffer window.
12425 In that case, redisplay the window normally. */
12426 && !NILP (Fmemq (w->buffer, Vminibuffer_list)))
12427 {
12428 /* W is a mini-buffer window, but it's not active, so clear
12429 it. */
12430 int yb = window_text_bottom_y (w);
12431 struct glyph_row *row;
12432 int y;
12433
12434 for (y = 0, row = w->desired_matrix->rows;
12435 y < yb;
12436 y += row->height, ++row)
12437 blank_row (w, row, y);
12438 goto finish_scroll_bars;
12439 }
12440
12441 clear_glyph_matrix (w->desired_matrix);
12442 }
12443
12444 /* Otherwise set up data on this window; select its buffer and point
12445 value. */
12446 /* Really select the buffer, for the sake of buffer-local
12447 variables. */
12448 set_buffer_internal_1 (XBUFFER (w->buffer));
12449 SET_TEXT_POS (opoint, PT, PT_BYTE);
12450
12451 current_matrix_up_to_date_p
12452 = (!NILP (w->window_end_valid)
12453 && !current_buffer->clip_changed
12454 && !current_buffer->prevent_redisplay_optimizations_p
12455 && XFASTINT (w->last_modified) >= MODIFF
12456 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
12457
12458 buffer_unchanged_p
12459 = (!NILP (w->window_end_valid)
12460 && !current_buffer->clip_changed
12461 && XFASTINT (w->last_modified) >= MODIFF
12462 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
12463
12464 /* When windows_or_buffers_changed is non-zero, we can't rely on
12465 the window end being valid, so set it to nil there. */
12466 if (windows_or_buffers_changed)
12467 {
12468 /* If window starts on a continuation line, maybe adjust the
12469 window start in case the window's width changed. */
12470 if (XMARKER (w->start)->buffer == current_buffer)
12471 compute_window_start_on_continuation_line (w);
12472
12473 w->window_end_valid = Qnil;
12474 }
12475
12476 /* Some sanity checks. */
12477 CHECK_WINDOW_END (w);
12478 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
12479 abort ();
12480 if (BYTEPOS (opoint) < CHARPOS (opoint))
12481 abort ();
12482
12483 /* If %c is in mode line, update it if needed. */
12484 if (!NILP (w->column_number_displayed)
12485 /* This alternative quickly identifies a common case
12486 where no change is needed. */
12487 && !(PT == XFASTINT (w->last_point)
12488 && XFASTINT (w->last_modified) >= MODIFF
12489 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
12490 && (XFASTINT (w->column_number_displayed)
12491 != (int) current_column ())) /* iftc */
12492 update_mode_line = 1;
12493
12494 /* Count number of windows showing the selected buffer. An indirect
12495 buffer counts as its base buffer. */
12496 if (!just_this_one_p)
12497 {
12498 struct buffer *current_base, *window_base;
12499 current_base = current_buffer;
12500 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
12501 if (current_base->base_buffer)
12502 current_base = current_base->base_buffer;
12503 if (window_base->base_buffer)
12504 window_base = window_base->base_buffer;
12505 if (current_base == window_base)
12506 buffer_shared++;
12507 }
12508
12509 /* Point refers normally to the selected window. For any other
12510 window, set up appropriate value. */
12511 if (!EQ (window, selected_window))
12512 {
12513 int new_pt = XMARKER (w->pointm)->charpos;
12514 int new_pt_byte = marker_byte_position (w->pointm);
12515 if (new_pt < BEGV)
12516 {
12517 new_pt = BEGV;
12518 new_pt_byte = BEGV_BYTE;
12519 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
12520 }
12521 else if (new_pt > (ZV - 1))
12522 {
12523 new_pt = ZV;
12524 new_pt_byte = ZV_BYTE;
12525 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
12526 }
12527
12528 /* We don't use SET_PT so that the point-motion hooks don't run. */
12529 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
12530 }
12531
12532 /* If any of the character widths specified in the display table
12533 have changed, invalidate the width run cache. It's true that
12534 this may be a bit late to catch such changes, but the rest of
12535 redisplay goes (non-fatally) haywire when the display table is
12536 changed, so why should we worry about doing any better? */
12537 if (current_buffer->width_run_cache)
12538 {
12539 struct Lisp_Char_Table *disptab = buffer_display_table ();
12540
12541 if (! disptab_matches_widthtab (disptab,
12542 XVECTOR (current_buffer->width_table)))
12543 {
12544 invalidate_region_cache (current_buffer,
12545 current_buffer->width_run_cache,
12546 BEG, Z);
12547 recompute_width_table (current_buffer, disptab);
12548 }
12549 }
12550
12551 /* If window-start is screwed up, choose a new one. */
12552 if (XMARKER (w->start)->buffer != current_buffer)
12553 goto recenter;
12554
12555 SET_TEXT_POS_FROM_MARKER (startp, w->start);
12556
12557 /* If someone specified a new starting point but did not insist,
12558 check whether it can be used. */
12559 if (!NILP (w->optional_new_start)
12560 && CHARPOS (startp) >= BEGV
12561 && CHARPOS (startp) <= ZV)
12562 {
12563 w->optional_new_start = Qnil;
12564 start_display (&it, w, startp);
12565 move_it_to (&it, PT, 0, it.last_visible_y, -1,
12566 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
12567 if (IT_CHARPOS (it) == PT)
12568 w->force_start = Qt;
12569 /* IT may overshoot PT if text at PT is invisible. */
12570 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
12571 w->force_start = Qt;
12572
12573
12574 }
12575
12576 /* Handle case where place to start displaying has been specified,
12577 unless the specified location is outside the accessible range. */
12578 if (!NILP (w->force_start)
12579 || w->frozen_window_start_p)
12580 {
12581 /* We set this later on if we have to adjust point. */
12582 int new_vpos = -1;
12583 int val;
12584
12585 w->force_start = Qnil;
12586 w->vscroll = 0;
12587 w->window_end_valid = Qnil;
12588
12589 /* Forget any recorded base line for line number display. */
12590 if (!buffer_unchanged_p)
12591 w->base_line_number = Qnil;
12592
12593 /* Redisplay the mode line. Select the buffer properly for that.
12594 Also, run the hook window-scroll-functions
12595 because we have scrolled. */
12596 /* Note, we do this after clearing force_start because
12597 if there's an error, it is better to forget about force_start
12598 than to get into an infinite loop calling the hook functions
12599 and having them get more errors. */
12600 if (!update_mode_line
12601 || ! NILP (Vwindow_scroll_functions))
12602 {
12603 update_mode_line = 1;
12604 w->update_mode_line = Qt;
12605 startp = run_window_scroll_functions (window, startp);
12606 }
12607
12608 w->last_modified = make_number (0);
12609 w->last_overlay_modified = make_number (0);
12610 if (CHARPOS (startp) < BEGV)
12611 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
12612 else if (CHARPOS (startp) > ZV)
12613 SET_TEXT_POS (startp, ZV, ZV_BYTE);
12614
12615 /* Redisplay, then check if cursor has been set during the
12616 redisplay. Give up if new fonts were loaded. */
12617 val = try_window (window, startp, 1);
12618 if (!val)
12619 {
12620 w->force_start = Qt;
12621 clear_glyph_matrix (w->desired_matrix);
12622 goto need_larger_matrices;
12623 }
12624 /* Point was outside the scroll margins. */
12625 if (val < 0)
12626 new_vpos = window_box_height (w) / 2;
12627
12628 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
12629 {
12630 /* If point does not appear, try to move point so it does
12631 appear. The desired matrix has been built above, so we
12632 can use it here. */
12633 new_vpos = window_box_height (w) / 2;
12634 }
12635
12636 if (!cursor_row_fully_visible_p (w, 0, 0))
12637 {
12638 /* Point does appear, but on a line partly visible at end of window.
12639 Move it back to a fully-visible line. */
12640 new_vpos = window_box_height (w);
12641 }
12642
12643 /* If we need to move point for either of the above reasons,
12644 now actually do it. */
12645 if (new_vpos >= 0)
12646 {
12647 struct glyph_row *row;
12648
12649 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
12650 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
12651 ++row;
12652
12653 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
12654 MATRIX_ROW_START_BYTEPOS (row));
12655
12656 if (w != XWINDOW (selected_window))
12657 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
12658 else if (current_buffer == old)
12659 SET_TEXT_POS (lpoint, PT, PT_BYTE);
12660
12661 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
12662
12663 /* If we are highlighting the region, then we just changed
12664 the region, so redisplay to show it. */
12665 if (!NILP (Vtransient_mark_mode)
12666 && !NILP (current_buffer->mark_active))
12667 {
12668 clear_glyph_matrix (w->desired_matrix);
12669 if (!try_window (window, startp, 0))
12670 goto need_larger_matrices;
12671 }
12672 }
12673
12674 #if GLYPH_DEBUG
12675 debug_method_add (w, "forced window start");
12676 #endif
12677 goto done;
12678 }
12679
12680 /* Handle case where text has not changed, only point, and it has
12681 not moved off the frame, and we are not retrying after hscroll.
12682 (current_matrix_up_to_date_p is nonzero when retrying.) */
12683 if (current_matrix_up_to_date_p
12684 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
12685 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
12686 {
12687 switch (rc)
12688 {
12689 case CURSOR_MOVEMENT_SUCCESS:
12690 used_current_matrix_p = 1;
12691 goto done;
12692
12693 #if 0 /* try_cursor_movement never returns this value. */
12694 case CURSOR_MOVEMENT_NEED_LARGER_MATRICES:
12695 goto need_larger_matrices;
12696 #endif
12697
12698 case CURSOR_MOVEMENT_MUST_SCROLL:
12699 goto try_to_scroll;
12700
12701 default:
12702 abort ();
12703 }
12704 }
12705 /* If current starting point was originally the beginning of a line
12706 but no longer is, find a new starting point. */
12707 else if (!NILP (w->start_at_line_beg)
12708 && !(CHARPOS (startp) <= BEGV
12709 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
12710 {
12711 #if GLYPH_DEBUG
12712 debug_method_add (w, "recenter 1");
12713 #endif
12714 goto recenter;
12715 }
12716
12717 /* Try scrolling with try_window_id. Value is > 0 if update has
12718 been done, it is -1 if we know that the same window start will
12719 not work. It is 0 if unsuccessful for some other reason. */
12720 else if ((tem = try_window_id (w)) != 0)
12721 {
12722 #if GLYPH_DEBUG
12723 debug_method_add (w, "try_window_id %d", tem);
12724 #endif
12725
12726 if (fonts_changed_p)
12727 goto need_larger_matrices;
12728 if (tem > 0)
12729 goto done;
12730
12731 /* Otherwise try_window_id has returned -1 which means that we
12732 don't want the alternative below this comment to execute. */
12733 }
12734 else if (CHARPOS (startp) >= BEGV
12735 && CHARPOS (startp) <= ZV
12736 && PT >= CHARPOS (startp)
12737 && (CHARPOS (startp) < ZV
12738 /* Avoid starting at end of buffer. */
12739 || CHARPOS (startp) == BEGV
12740 || (XFASTINT (w->last_modified) >= MODIFF
12741 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
12742 {
12743 #if GLYPH_DEBUG
12744 debug_method_add (w, "same window start");
12745 #endif
12746
12747 /* Try to redisplay starting at same place as before.
12748 If point has not moved off frame, accept the results. */
12749 if (!current_matrix_up_to_date_p
12750 /* Don't use try_window_reusing_current_matrix in this case
12751 because a window scroll function can have changed the
12752 buffer. */
12753 || !NILP (Vwindow_scroll_functions)
12754 || MINI_WINDOW_P (w)
12755 || !(used_current_matrix_p
12756 = try_window_reusing_current_matrix (w)))
12757 {
12758 IF_DEBUG (debug_method_add (w, "1"));
12759 if (try_window (window, startp, 1) < 0)
12760 /* -1 means we need to scroll.
12761 0 means we need new matrices, but fonts_changed_p
12762 is set in that case, so we will detect it below. */
12763 goto try_to_scroll;
12764 }
12765
12766 if (fonts_changed_p)
12767 goto need_larger_matrices;
12768
12769 if (w->cursor.vpos >= 0)
12770 {
12771 if (!just_this_one_p
12772 || current_buffer->clip_changed
12773 || BEG_UNCHANGED < CHARPOS (startp))
12774 /* Forget any recorded base line for line number display. */
12775 w->base_line_number = Qnil;
12776
12777 if (!cursor_row_fully_visible_p (w, 1, 0))
12778 {
12779 clear_glyph_matrix (w->desired_matrix);
12780 last_line_misfit = 1;
12781 }
12782 /* Drop through and scroll. */
12783 else
12784 goto done;
12785 }
12786 else
12787 clear_glyph_matrix (w->desired_matrix);
12788 }
12789
12790 try_to_scroll:
12791
12792 w->last_modified = make_number (0);
12793 w->last_overlay_modified = make_number (0);
12794
12795 /* Redisplay the mode line. Select the buffer properly for that. */
12796 if (!update_mode_line)
12797 {
12798 update_mode_line = 1;
12799 w->update_mode_line = Qt;
12800 }
12801
12802 /* Try to scroll by specified few lines. */
12803 if ((scroll_conservatively
12804 || scroll_step
12805 || temp_scroll_step
12806 || NUMBERP (current_buffer->scroll_up_aggressively)
12807 || NUMBERP (current_buffer->scroll_down_aggressively))
12808 && !current_buffer->clip_changed
12809 && CHARPOS (startp) >= BEGV
12810 && CHARPOS (startp) <= ZV)
12811 {
12812 /* The function returns -1 if new fonts were loaded, 1 if
12813 successful, 0 if not successful. */
12814 int rc = try_scrolling (window, just_this_one_p,
12815 scroll_conservatively,
12816 scroll_step,
12817 temp_scroll_step, last_line_misfit);
12818 switch (rc)
12819 {
12820 case SCROLLING_SUCCESS:
12821 goto done;
12822
12823 case SCROLLING_NEED_LARGER_MATRICES:
12824 goto need_larger_matrices;
12825
12826 case SCROLLING_FAILED:
12827 break;
12828
12829 default:
12830 abort ();
12831 }
12832 }
12833
12834 /* Finally, just choose place to start which centers point */
12835
12836 recenter:
12837 if (centering_position < 0)
12838 centering_position = window_box_height (w) / 2;
12839
12840 #if GLYPH_DEBUG
12841 debug_method_add (w, "recenter");
12842 #endif
12843
12844 /* w->vscroll = 0; */
12845
12846 /* Forget any previously recorded base line for line number display. */
12847 if (!buffer_unchanged_p)
12848 w->base_line_number = Qnil;
12849
12850 /* Move backward half the height of the window. */
12851 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
12852 it.current_y = it.last_visible_y;
12853 move_it_vertically_backward (&it, centering_position);
12854 xassert (IT_CHARPOS (it) >= BEGV);
12855
12856 /* The function move_it_vertically_backward may move over more
12857 than the specified y-distance. If it->w is small, e.g. a
12858 mini-buffer window, we may end up in front of the window's
12859 display area. Start displaying at the start of the line
12860 containing PT in this case. */
12861 if (it.current_y <= 0)
12862 {
12863 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
12864 move_it_vertically_backward (&it, 0);
12865 #if 0
12866 /* I think this assert is bogus if buffer contains
12867 invisible text or images. KFS. */
12868 xassert (IT_CHARPOS (it) <= PT);
12869 #endif
12870 it.current_y = 0;
12871 }
12872
12873 it.current_x = it.hpos = 0;
12874
12875 /* Set startp here explicitly in case that helps avoid an infinite loop
12876 in case the window-scroll-functions functions get errors. */
12877 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
12878
12879 /* Run scroll hooks. */
12880 startp = run_window_scroll_functions (window, it.current.pos);
12881
12882 /* Redisplay the window. */
12883 if (!current_matrix_up_to_date_p
12884 || windows_or_buffers_changed
12885 || cursor_type_changed
12886 /* Don't use try_window_reusing_current_matrix in this case
12887 because it can have changed the buffer. */
12888 || !NILP (Vwindow_scroll_functions)
12889 || !just_this_one_p
12890 || MINI_WINDOW_P (w)
12891 || !(used_current_matrix_p
12892 = try_window_reusing_current_matrix (w)))
12893 try_window (window, startp, 0);
12894
12895 /* If new fonts have been loaded (due to fontsets), give up. We
12896 have to start a new redisplay since we need to re-adjust glyph
12897 matrices. */
12898 if (fonts_changed_p)
12899 goto need_larger_matrices;
12900
12901 /* If cursor did not appear assume that the middle of the window is
12902 in the first line of the window. Do it again with the next line.
12903 (Imagine a window of height 100, displaying two lines of height
12904 60. Moving back 50 from it->last_visible_y will end in the first
12905 line.) */
12906 if (w->cursor.vpos < 0)
12907 {
12908 if (!NILP (w->window_end_valid)
12909 && PT >= Z - XFASTINT (w->window_end_pos))
12910 {
12911 clear_glyph_matrix (w->desired_matrix);
12912 move_it_by_lines (&it, 1, 0);
12913 try_window (window, it.current.pos, 0);
12914 }
12915 else if (PT < IT_CHARPOS (it))
12916 {
12917 clear_glyph_matrix (w->desired_matrix);
12918 move_it_by_lines (&it, -1, 0);
12919 try_window (window, it.current.pos, 0);
12920 }
12921 else
12922 {
12923 /* Not much we can do about it. */
12924 }
12925 }
12926
12927 /* Consider the following case: Window starts at BEGV, there is
12928 invisible, intangible text at BEGV, so that display starts at
12929 some point START > BEGV. It can happen that we are called with
12930 PT somewhere between BEGV and START. Try to handle that case. */
12931 if (w->cursor.vpos < 0)
12932 {
12933 struct glyph_row *row = w->current_matrix->rows;
12934 if (row->mode_line_p)
12935 ++row;
12936 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
12937 }
12938
12939 if (!cursor_row_fully_visible_p (w, 0, 0))
12940 {
12941 /* If vscroll is enabled, disable it and try again. */
12942 if (w->vscroll)
12943 {
12944 w->vscroll = 0;
12945 clear_glyph_matrix (w->desired_matrix);
12946 goto recenter;
12947 }
12948
12949 /* If centering point failed to make the whole line visible,
12950 put point at the top instead. That has to make the whole line
12951 visible, if it can be done. */
12952 if (centering_position == 0)
12953 goto done;
12954
12955 clear_glyph_matrix (w->desired_matrix);
12956 centering_position = 0;
12957 goto recenter;
12958 }
12959
12960 done:
12961
12962 SET_TEXT_POS_FROM_MARKER (startp, w->start);
12963 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
12964 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
12965 ? Qt : Qnil);
12966
12967 /* Display the mode line, if we must. */
12968 if ((update_mode_line
12969 /* If window not full width, must redo its mode line
12970 if (a) the window to its side is being redone and
12971 (b) we do a frame-based redisplay. This is a consequence
12972 of how inverted lines are drawn in frame-based redisplay. */
12973 || (!just_this_one_p
12974 && !FRAME_WINDOW_P (f)
12975 && !WINDOW_FULL_WIDTH_P (w))
12976 /* Line number to display. */
12977 || INTEGERP (w->base_line_pos)
12978 /* Column number is displayed and different from the one displayed. */
12979 || (!NILP (w->column_number_displayed)
12980 && (XFASTINT (w->column_number_displayed)
12981 != (int) current_column ()))) /* iftc */
12982 /* This means that the window has a mode line. */
12983 && (WINDOW_WANTS_MODELINE_P (w)
12984 || WINDOW_WANTS_HEADER_LINE_P (w)))
12985 {
12986 display_mode_lines (w);
12987
12988 /* If mode line height has changed, arrange for a thorough
12989 immediate redisplay using the correct mode line height. */
12990 if (WINDOW_WANTS_MODELINE_P (w)
12991 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
12992 {
12993 fonts_changed_p = 1;
12994 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
12995 = DESIRED_MODE_LINE_HEIGHT (w);
12996 }
12997
12998 /* If top line height has changed, arrange for a thorough
12999 immediate redisplay using the correct mode line height. */
13000 if (WINDOW_WANTS_HEADER_LINE_P (w)
13001 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
13002 {
13003 fonts_changed_p = 1;
13004 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
13005 = DESIRED_HEADER_LINE_HEIGHT (w);
13006 }
13007
13008 if (fonts_changed_p)
13009 goto need_larger_matrices;
13010 }
13011
13012 if (!line_number_displayed
13013 && !BUFFERP (w->base_line_pos))
13014 {
13015 w->base_line_pos = Qnil;
13016 w->base_line_number = Qnil;
13017 }
13018
13019 finish_menu_bars:
13020
13021 /* When we reach a frame's selected window, redo the frame's menu bar. */
13022 if (update_mode_line
13023 && EQ (FRAME_SELECTED_WINDOW (f), window))
13024 {
13025 int redisplay_menu_p = 0;
13026 int redisplay_tool_bar_p = 0;
13027
13028 if (FRAME_WINDOW_P (f))
13029 {
13030 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
13031 || defined (USE_GTK)
13032 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
13033 #else
13034 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
13035 #endif
13036 }
13037 else
13038 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
13039
13040 if (redisplay_menu_p)
13041 display_menu_bar (w);
13042
13043 #ifdef HAVE_WINDOW_SYSTEM
13044 if (FRAME_WINDOW_P (f))
13045 {
13046 #ifdef USE_GTK
13047 redisplay_tool_bar_p = FRAME_EXTERNAL_TOOL_BAR (f);
13048 #else
13049 redisplay_tool_bar_p = WINDOWP (f->tool_bar_window)
13050 && (FRAME_TOOL_BAR_LINES (f) > 0
13051 || auto_resize_tool_bars_p);
13052 #endif
13053
13054 if (redisplay_tool_bar_p)
13055 redisplay_tool_bar (f);
13056 }
13057 #endif
13058 }
13059
13060 #ifdef HAVE_WINDOW_SYSTEM
13061 if (FRAME_WINDOW_P (f)
13062 && update_window_fringes (w, (just_this_one_p
13063 || (!used_current_matrix_p && !overlay_arrow_seen)
13064 || w->pseudo_window_p)))
13065 {
13066 update_begin (f);
13067 BLOCK_INPUT;
13068 if (draw_window_fringes (w, 1))
13069 x_draw_vertical_border (w);
13070 UNBLOCK_INPUT;
13071 update_end (f);
13072 }
13073 #endif /* HAVE_WINDOW_SYSTEM */
13074
13075 /* We go to this label, with fonts_changed_p nonzero,
13076 if it is necessary to try again using larger glyph matrices.
13077 We have to redeem the scroll bar even in this case,
13078 because the loop in redisplay_internal expects that. */
13079 need_larger_matrices:
13080 ;
13081 finish_scroll_bars:
13082
13083 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
13084 {
13085 /* Set the thumb's position and size. */
13086 set_vertical_scroll_bar (w);
13087
13088 /* Note that we actually used the scroll bar attached to this
13089 window, so it shouldn't be deleted at the end of redisplay. */
13090 if (FRAME_TERMINAL (f)->redeem_scroll_bar_hook)
13091 (*FRAME_TERMINAL (f)->redeem_scroll_bar_hook) (w);
13092 }
13093
13094 /* Restore current_buffer and value of point in it. */
13095 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
13096 set_buffer_internal_1 (old);
13097 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
13098
13099 unbind_to (count, Qnil);
13100 }
13101
13102
13103 /* Build the complete desired matrix of WINDOW with a window start
13104 buffer position POS.
13105
13106 Value is 1 if successful. It is zero if fonts were loaded during
13107 redisplay which makes re-adjusting glyph matrices necessary, and -1
13108 if point would appear in the scroll margins.
13109 (We check that only if CHECK_MARGINS is nonzero. */
13110
13111 int
13112 try_window (window, pos, check_margins)
13113 Lisp_Object window;
13114 struct text_pos pos;
13115 int check_margins;
13116 {
13117 struct window *w = XWINDOW (window);
13118 struct it it;
13119 struct glyph_row *last_text_row = NULL;
13120
13121 /* Make POS the new window start. */
13122 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
13123
13124 /* Mark cursor position as unknown. No overlay arrow seen. */
13125 w->cursor.vpos = -1;
13126 overlay_arrow_seen = 0;
13127
13128 /* Initialize iterator and info to start at POS. */
13129 start_display (&it, w, pos);
13130
13131 /* Display all lines of W. */
13132 while (it.current_y < it.last_visible_y)
13133 {
13134 if (display_line (&it))
13135 last_text_row = it.glyph_row - 1;
13136 if (fonts_changed_p)
13137 return 0;
13138 }
13139
13140 /* Don't let the cursor end in the scroll margins. */
13141 if (check_margins
13142 && !MINI_WINDOW_P (w))
13143 {
13144 int this_scroll_margin;
13145
13146 this_scroll_margin = max (0, scroll_margin);
13147 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
13148 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
13149
13150 if ((w->cursor.y < this_scroll_margin
13151 && CHARPOS (pos) > BEGV
13152 && IT_CHARPOS (it) < ZV)
13153 /* rms: considering make_cursor_line_fully_visible_p here
13154 seems to give wrong results. We don't want to recenter
13155 when the last line is partly visible, we want to allow
13156 that case to be handled in the usual way. */
13157 || (w->cursor.y + 1) > it.last_visible_y)
13158 {
13159 w->cursor.vpos = -1;
13160 clear_glyph_matrix (w->desired_matrix);
13161 return -1;
13162 }
13163 }
13164
13165 /* If bottom moved off end of frame, change mode line percentage. */
13166 if (XFASTINT (w->window_end_pos) <= 0
13167 && Z != IT_CHARPOS (it))
13168 w->update_mode_line = Qt;
13169
13170 /* Set window_end_pos to the offset of the last character displayed
13171 on the window from the end of current_buffer. Set
13172 window_end_vpos to its row number. */
13173 if (last_text_row)
13174 {
13175 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
13176 w->window_end_bytepos
13177 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
13178 w->window_end_pos
13179 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
13180 w->window_end_vpos
13181 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
13182 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
13183 ->displays_text_p);
13184 }
13185 else
13186 {
13187 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
13188 w->window_end_pos = make_number (Z - ZV);
13189 w->window_end_vpos = make_number (0);
13190 }
13191
13192 /* But that is not valid info until redisplay finishes. */
13193 w->window_end_valid = Qnil;
13194 return 1;
13195 }
13196
13197
13198 \f
13199 /************************************************************************
13200 Window redisplay reusing current matrix when buffer has not changed
13201 ************************************************************************/
13202
13203 /* Try redisplay of window W showing an unchanged buffer with a
13204 different window start than the last time it was displayed by
13205 reusing its current matrix. Value is non-zero if successful.
13206 W->start is the new window start. */
13207
13208 static int
13209 try_window_reusing_current_matrix (w)
13210 struct window *w;
13211 {
13212 struct frame *f = XFRAME (w->frame);
13213 struct glyph_row *row, *bottom_row;
13214 struct it it;
13215 struct run run;
13216 struct text_pos start, new_start;
13217 int nrows_scrolled, i;
13218 struct glyph_row *last_text_row;
13219 struct glyph_row *last_reused_text_row;
13220 struct glyph_row *start_row;
13221 int start_vpos, min_y, max_y;
13222
13223 #if GLYPH_DEBUG
13224 if (inhibit_try_window_reusing)
13225 return 0;
13226 #endif
13227
13228 if (/* This function doesn't handle terminal frames. */
13229 !FRAME_WINDOW_P (f)
13230 /* Don't try to reuse the display if windows have been split
13231 or such. */
13232 || windows_or_buffers_changed
13233 || cursor_type_changed)
13234 return 0;
13235
13236 /* Can't do this if region may have changed. */
13237 if ((!NILP (Vtransient_mark_mode)
13238 && !NILP (current_buffer->mark_active))
13239 || !NILP (w->region_showing)
13240 || !NILP (Vshow_trailing_whitespace))
13241 return 0;
13242
13243 /* If top-line visibility has changed, give up. */
13244 if (WINDOW_WANTS_HEADER_LINE_P (w)
13245 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
13246 return 0;
13247
13248 /* Give up if old or new display is scrolled vertically. We could
13249 make this function handle this, but right now it doesn't. */
13250 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
13251 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
13252 return 0;
13253
13254 /* The variable new_start now holds the new window start. The old
13255 start `start' can be determined from the current matrix. */
13256 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
13257 start = start_row->start.pos;
13258 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
13259
13260 /* Clear the desired matrix for the display below. */
13261 clear_glyph_matrix (w->desired_matrix);
13262
13263 if (CHARPOS (new_start) <= CHARPOS (start))
13264 {
13265 int first_row_y;
13266
13267 /* Don't use this method if the display starts with an ellipsis
13268 displayed for invisible text. It's not easy to handle that case
13269 below, and it's certainly not worth the effort since this is
13270 not a frequent case. */
13271 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
13272 return 0;
13273
13274 IF_DEBUG (debug_method_add (w, "twu1"));
13275
13276 /* Display up to a row that can be reused. The variable
13277 last_text_row is set to the last row displayed that displays
13278 text. Note that it.vpos == 0 if or if not there is a
13279 header-line; it's not the same as the MATRIX_ROW_VPOS! */
13280 start_display (&it, w, new_start);
13281 first_row_y = it.current_y;
13282 w->cursor.vpos = -1;
13283 last_text_row = last_reused_text_row = NULL;
13284
13285 while (it.current_y < it.last_visible_y
13286 && !fonts_changed_p)
13287 {
13288 /* If we have reached into the characters in the START row,
13289 that means the line boundaries have changed. So we
13290 can't start copying with the row START. Maybe it will
13291 work to start copying with the following row. */
13292 while (IT_CHARPOS (it) > CHARPOS (start))
13293 {
13294 /* Advance to the next row as the "start". */
13295 start_row++;
13296 start = start_row->start.pos;
13297 /* If there are no more rows to try, or just one, give up. */
13298 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
13299 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
13300 || CHARPOS (start) == ZV)
13301 {
13302 clear_glyph_matrix (w->desired_matrix);
13303 return 0;
13304 }
13305
13306 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
13307 }
13308 /* If we have reached alignment,
13309 we can copy the rest of the rows. */
13310 if (IT_CHARPOS (it) == CHARPOS (start))
13311 break;
13312
13313 if (display_line (&it))
13314 last_text_row = it.glyph_row - 1;
13315 }
13316
13317 /* A value of current_y < last_visible_y means that we stopped
13318 at the previous window start, which in turn means that we
13319 have at least one reusable row. */
13320 if (it.current_y < it.last_visible_y)
13321 {
13322 /* IT.vpos always starts from 0; it counts text lines. */
13323 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
13324
13325 /* Find PT if not already found in the lines displayed. */
13326 if (w->cursor.vpos < 0)
13327 {
13328 int dy = it.current_y - start_row->y;
13329
13330 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
13331 row = row_containing_pos (w, PT, row, NULL, dy);
13332 if (row)
13333 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
13334 dy, nrows_scrolled);
13335 else
13336 {
13337 clear_glyph_matrix (w->desired_matrix);
13338 return 0;
13339 }
13340 }
13341
13342 /* Scroll the display. Do it before the current matrix is
13343 changed. The problem here is that update has not yet
13344 run, i.e. part of the current matrix is not up to date.
13345 scroll_run_hook will clear the cursor, and use the
13346 current matrix to get the height of the row the cursor is
13347 in. */
13348 run.current_y = start_row->y;
13349 run.desired_y = it.current_y;
13350 run.height = it.last_visible_y - it.current_y;
13351
13352 if (run.height > 0 && run.current_y != run.desired_y)
13353 {
13354 update_begin (f);
13355 FRAME_RIF (f)->update_window_begin_hook (w);
13356 FRAME_RIF (f)->clear_window_mouse_face (w);
13357 FRAME_RIF (f)->scroll_run_hook (w, &run);
13358 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
13359 update_end (f);
13360 }
13361
13362 /* Shift current matrix down by nrows_scrolled lines. */
13363 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
13364 rotate_matrix (w->current_matrix,
13365 start_vpos,
13366 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
13367 nrows_scrolled);
13368
13369 /* Disable lines that must be updated. */
13370 for (i = 0; i < it.vpos; ++i)
13371 (start_row + i)->enabled_p = 0;
13372
13373 /* Re-compute Y positions. */
13374 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
13375 max_y = it.last_visible_y;
13376 for (row = start_row + nrows_scrolled;
13377 row < bottom_row;
13378 ++row)
13379 {
13380 row->y = it.current_y;
13381 row->visible_height = row->height;
13382
13383 if (row->y < min_y)
13384 row->visible_height -= min_y - row->y;
13385 if (row->y + row->height > max_y)
13386 row->visible_height -= row->y + row->height - max_y;
13387 row->redraw_fringe_bitmaps_p = 1;
13388
13389 it.current_y += row->height;
13390
13391 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
13392 last_reused_text_row = row;
13393 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
13394 break;
13395 }
13396
13397 /* Disable lines in the current matrix which are now
13398 below the window. */
13399 for (++row; row < bottom_row; ++row)
13400 row->enabled_p = row->mode_line_p = 0;
13401 }
13402
13403 /* Update window_end_pos etc.; last_reused_text_row is the last
13404 reused row from the current matrix containing text, if any.
13405 The value of last_text_row is the last displayed line
13406 containing text. */
13407 if (last_reused_text_row)
13408 {
13409 w->window_end_bytepos
13410 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
13411 w->window_end_pos
13412 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
13413 w->window_end_vpos
13414 = make_number (MATRIX_ROW_VPOS (last_reused_text_row,
13415 w->current_matrix));
13416 }
13417 else if (last_text_row)
13418 {
13419 w->window_end_bytepos
13420 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
13421 w->window_end_pos
13422 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
13423 w->window_end_vpos
13424 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
13425 }
13426 else
13427 {
13428 /* This window must be completely empty. */
13429 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
13430 w->window_end_pos = make_number (Z - ZV);
13431 w->window_end_vpos = make_number (0);
13432 }
13433 w->window_end_valid = Qnil;
13434
13435 /* Update hint: don't try scrolling again in update_window. */
13436 w->desired_matrix->no_scrolling_p = 1;
13437
13438 #if GLYPH_DEBUG
13439 debug_method_add (w, "try_window_reusing_current_matrix 1");
13440 #endif
13441 return 1;
13442 }
13443 else if (CHARPOS (new_start) > CHARPOS (start))
13444 {
13445 struct glyph_row *pt_row, *row;
13446 struct glyph_row *first_reusable_row;
13447 struct glyph_row *first_row_to_display;
13448 int dy;
13449 int yb = window_text_bottom_y (w);
13450
13451 /* Find the row starting at new_start, if there is one. Don't
13452 reuse a partially visible line at the end. */
13453 first_reusable_row = start_row;
13454 while (first_reusable_row->enabled_p
13455 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
13456 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
13457 < CHARPOS (new_start)))
13458 ++first_reusable_row;
13459
13460 /* Give up if there is no row to reuse. */
13461 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
13462 || !first_reusable_row->enabled_p
13463 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
13464 != CHARPOS (new_start)))
13465 return 0;
13466
13467 /* We can reuse fully visible rows beginning with
13468 first_reusable_row to the end of the window. Set
13469 first_row_to_display to the first row that cannot be reused.
13470 Set pt_row to the row containing point, if there is any. */
13471 pt_row = NULL;
13472 for (first_row_to_display = first_reusable_row;
13473 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
13474 ++first_row_to_display)
13475 {
13476 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
13477 && PT < MATRIX_ROW_END_CHARPOS (first_row_to_display))
13478 pt_row = first_row_to_display;
13479 }
13480
13481 /* Start displaying at the start of first_row_to_display. */
13482 xassert (first_row_to_display->y < yb);
13483 init_to_row_start (&it, w, first_row_to_display);
13484
13485 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
13486 - start_vpos);
13487 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
13488 - nrows_scrolled);
13489 it.current_y = (first_row_to_display->y - first_reusable_row->y
13490 + WINDOW_HEADER_LINE_HEIGHT (w));
13491
13492 /* Display lines beginning with first_row_to_display in the
13493 desired matrix. Set last_text_row to the last row displayed
13494 that displays text. */
13495 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
13496 if (pt_row == NULL)
13497 w->cursor.vpos = -1;
13498 last_text_row = NULL;
13499 while (it.current_y < it.last_visible_y && !fonts_changed_p)
13500 if (display_line (&it))
13501 last_text_row = it.glyph_row - 1;
13502
13503 /* Give up If point isn't in a row displayed or reused. */
13504 if (w->cursor.vpos < 0)
13505 {
13506 clear_glyph_matrix (w->desired_matrix);
13507 return 0;
13508 }
13509
13510 /* If point is in a reused row, adjust y and vpos of the cursor
13511 position. */
13512 if (pt_row)
13513 {
13514 w->cursor.vpos -= nrows_scrolled;
13515 w->cursor.y -= first_reusable_row->y - start_row->y;
13516 }
13517
13518 /* Scroll the display. */
13519 run.current_y = first_reusable_row->y;
13520 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
13521 run.height = it.last_visible_y - run.current_y;
13522 dy = run.current_y - run.desired_y;
13523
13524 if (run.height)
13525 {
13526 update_begin (f);
13527 FRAME_RIF (f)->update_window_begin_hook (w);
13528 FRAME_RIF (f)->clear_window_mouse_face (w);
13529 FRAME_RIF (f)->scroll_run_hook (w, &run);
13530 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
13531 update_end (f);
13532 }
13533
13534 /* Adjust Y positions of reused rows. */
13535 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
13536 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
13537 max_y = it.last_visible_y;
13538 for (row = first_reusable_row; row < first_row_to_display; ++row)
13539 {
13540 row->y -= dy;
13541 row->visible_height = row->height;
13542 if (row->y < min_y)
13543 row->visible_height -= min_y - row->y;
13544 if (row->y + row->height > max_y)
13545 row->visible_height -= row->y + row->height - max_y;
13546 row->redraw_fringe_bitmaps_p = 1;
13547 }
13548
13549 /* Scroll the current matrix. */
13550 xassert (nrows_scrolled > 0);
13551 rotate_matrix (w->current_matrix,
13552 start_vpos,
13553 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
13554 -nrows_scrolled);
13555
13556 /* Disable rows not reused. */
13557 for (row -= nrows_scrolled; row < bottom_row; ++row)
13558 row->enabled_p = 0;
13559
13560 /* Point may have moved to a different line, so we cannot assume that
13561 the previous cursor position is valid; locate the correct row. */
13562 if (pt_row)
13563 {
13564 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
13565 row < bottom_row && PT >= MATRIX_ROW_END_CHARPOS (row);
13566 row++)
13567 {
13568 w->cursor.vpos++;
13569 w->cursor.y = row->y;
13570 }
13571 if (row < bottom_row)
13572 {
13573 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
13574 while (glyph->charpos < PT)
13575 {
13576 w->cursor.hpos++;
13577 w->cursor.x += glyph->pixel_width;
13578 glyph++;
13579 }
13580 }
13581 }
13582
13583 /* Adjust window end. A null value of last_text_row means that
13584 the window end is in reused rows which in turn means that
13585 only its vpos can have changed. */
13586 if (last_text_row)
13587 {
13588 w->window_end_bytepos
13589 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
13590 w->window_end_pos
13591 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
13592 w->window_end_vpos
13593 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
13594 }
13595 else
13596 {
13597 w->window_end_vpos
13598 = make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled);
13599 }
13600
13601 w->window_end_valid = Qnil;
13602 w->desired_matrix->no_scrolling_p = 1;
13603
13604 #if GLYPH_DEBUG
13605 debug_method_add (w, "try_window_reusing_current_matrix 2");
13606 #endif
13607 return 1;
13608 }
13609
13610 return 0;
13611 }
13612
13613
13614 \f
13615 /************************************************************************
13616 Window redisplay reusing current matrix when buffer has changed
13617 ************************************************************************/
13618
13619 static struct glyph_row *find_last_unchanged_at_beg_row P_ ((struct window *));
13620 static struct glyph_row *find_first_unchanged_at_end_row P_ ((struct window *,
13621 int *, int *));
13622 static struct glyph_row *
13623 find_last_row_displaying_text P_ ((struct glyph_matrix *, struct it *,
13624 struct glyph_row *));
13625
13626
13627 /* Return the last row in MATRIX displaying text. If row START is
13628 non-null, start searching with that row. IT gives the dimensions
13629 of the display. Value is null if matrix is empty; otherwise it is
13630 a pointer to the row found. */
13631
13632 static struct glyph_row *
13633 find_last_row_displaying_text (matrix, it, start)
13634 struct glyph_matrix *matrix;
13635 struct it *it;
13636 struct glyph_row *start;
13637 {
13638 struct glyph_row *row, *row_found;
13639
13640 /* Set row_found to the last row in IT->w's current matrix
13641 displaying text. The loop looks funny but think of partially
13642 visible lines. */
13643 row_found = NULL;
13644 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
13645 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
13646 {
13647 xassert (row->enabled_p);
13648 row_found = row;
13649 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
13650 break;
13651 ++row;
13652 }
13653
13654 return row_found;
13655 }
13656
13657
13658 /* Return the last row in the current matrix of W that is not affected
13659 by changes at the start of current_buffer that occurred since W's
13660 current matrix was built. Value is null if no such row exists.
13661
13662 BEG_UNCHANGED us the number of characters unchanged at the start of
13663 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
13664 first changed character in current_buffer. Characters at positions <
13665 BEG + BEG_UNCHANGED are at the same buffer positions as they were
13666 when the current matrix was built. */
13667
13668 static struct glyph_row *
13669 find_last_unchanged_at_beg_row (w)
13670 struct window *w;
13671 {
13672 int first_changed_pos = BEG + BEG_UNCHANGED;
13673 struct glyph_row *row;
13674 struct glyph_row *row_found = NULL;
13675 int yb = window_text_bottom_y (w);
13676
13677 /* Find the last row displaying unchanged text. */
13678 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
13679 while (MATRIX_ROW_DISPLAYS_TEXT_P (row)
13680 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos)
13681 {
13682 if (/* If row ends before first_changed_pos, it is unchanged,
13683 except in some case. */
13684 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
13685 /* When row ends in ZV and we write at ZV it is not
13686 unchanged. */
13687 && !row->ends_at_zv_p
13688 /* When first_changed_pos is the end of a continued line,
13689 row is not unchanged because it may be no longer
13690 continued. */
13691 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
13692 && (row->continued_p
13693 || row->exact_window_width_line_p)))
13694 row_found = row;
13695
13696 /* Stop if last visible row. */
13697 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
13698 break;
13699
13700 ++row;
13701 }
13702
13703 return row_found;
13704 }
13705
13706
13707 /* Find the first glyph row in the current matrix of W that is not
13708 affected by changes at the end of current_buffer since the
13709 time W's current matrix was built.
13710
13711 Return in *DELTA the number of chars by which buffer positions in
13712 unchanged text at the end of current_buffer must be adjusted.
13713
13714 Return in *DELTA_BYTES the corresponding number of bytes.
13715
13716 Value is null if no such row exists, i.e. all rows are affected by
13717 changes. */
13718
13719 static struct glyph_row *
13720 find_first_unchanged_at_end_row (w, delta, delta_bytes)
13721 struct window *w;
13722 int *delta, *delta_bytes;
13723 {
13724 struct glyph_row *row;
13725 struct glyph_row *row_found = NULL;
13726
13727 *delta = *delta_bytes = 0;
13728
13729 /* Display must not have been paused, otherwise the current matrix
13730 is not up to date. */
13731 if (NILP (w->window_end_valid))
13732 abort ();
13733
13734 /* A value of window_end_pos >= END_UNCHANGED means that the window
13735 end is in the range of changed text. If so, there is no
13736 unchanged row at the end of W's current matrix. */
13737 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
13738 return NULL;
13739
13740 /* Set row to the last row in W's current matrix displaying text. */
13741 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
13742
13743 /* If matrix is entirely empty, no unchanged row exists. */
13744 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
13745 {
13746 /* The value of row is the last glyph row in the matrix having a
13747 meaningful buffer position in it. The end position of row
13748 corresponds to window_end_pos. This allows us to translate
13749 buffer positions in the current matrix to current buffer
13750 positions for characters not in changed text. */
13751 int Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
13752 int Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
13753 int last_unchanged_pos, last_unchanged_pos_old;
13754 struct glyph_row *first_text_row
13755 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
13756
13757 *delta = Z - Z_old;
13758 *delta_bytes = Z_BYTE - Z_BYTE_old;
13759
13760 /* Set last_unchanged_pos to the buffer position of the last
13761 character in the buffer that has not been changed. Z is the
13762 index + 1 of the last character in current_buffer, i.e. by
13763 subtracting END_UNCHANGED we get the index of the last
13764 unchanged character, and we have to add BEG to get its buffer
13765 position. */
13766 last_unchanged_pos = Z - END_UNCHANGED + BEG;
13767 last_unchanged_pos_old = last_unchanged_pos - *delta;
13768
13769 /* Search backward from ROW for a row displaying a line that
13770 starts at a minimum position >= last_unchanged_pos_old. */
13771 for (; row > first_text_row; --row)
13772 {
13773 /* This used to abort, but it can happen.
13774 It is ok to just stop the search instead here. KFS. */
13775 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
13776 break;
13777
13778 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
13779 row_found = row;
13780 }
13781 }
13782
13783 if (row_found && !MATRIX_ROW_DISPLAYS_TEXT_P (row_found))
13784 abort ();
13785
13786 return row_found;
13787 }
13788
13789
13790 /* Make sure that glyph rows in the current matrix of window W
13791 reference the same glyph memory as corresponding rows in the
13792 frame's frame matrix. This function is called after scrolling W's
13793 current matrix on a terminal frame in try_window_id and
13794 try_window_reusing_current_matrix. */
13795
13796 static void
13797 sync_frame_with_window_matrix_rows (w)
13798 struct window *w;
13799 {
13800 struct frame *f = XFRAME (w->frame);
13801 struct glyph_row *window_row, *window_row_end, *frame_row;
13802
13803 /* Preconditions: W must be a leaf window and full-width. Its frame
13804 must have a frame matrix. */
13805 xassert (NILP (w->hchild) && NILP (w->vchild));
13806 xassert (WINDOW_FULL_WIDTH_P (w));
13807 xassert (!FRAME_WINDOW_P (f));
13808
13809 /* If W is a full-width window, glyph pointers in W's current matrix
13810 have, by definition, to be the same as glyph pointers in the
13811 corresponding frame matrix. Note that frame matrices have no
13812 marginal areas (see build_frame_matrix). */
13813 window_row = w->current_matrix->rows;
13814 window_row_end = window_row + w->current_matrix->nrows;
13815 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
13816 while (window_row < window_row_end)
13817 {
13818 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
13819 struct glyph *end = window_row->glyphs[LAST_AREA];
13820
13821 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
13822 frame_row->glyphs[TEXT_AREA] = start;
13823 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
13824 frame_row->glyphs[LAST_AREA] = end;
13825
13826 /* Disable frame rows whose corresponding window rows have
13827 been disabled in try_window_id. */
13828 if (!window_row->enabled_p)
13829 frame_row->enabled_p = 0;
13830
13831 ++window_row, ++frame_row;
13832 }
13833 }
13834
13835
13836 /* Find the glyph row in window W containing CHARPOS. Consider all
13837 rows between START and END (not inclusive). END null means search
13838 all rows to the end of the display area of W. Value is the row
13839 containing CHARPOS or null. */
13840
13841 struct glyph_row *
13842 row_containing_pos (w, charpos, start, end, dy)
13843 struct window *w;
13844 int charpos;
13845 struct glyph_row *start, *end;
13846 int dy;
13847 {
13848 struct glyph_row *row = start;
13849 int last_y;
13850
13851 /* If we happen to start on a header-line, skip that. */
13852 if (row->mode_line_p)
13853 ++row;
13854
13855 if ((end && row >= end) || !row->enabled_p)
13856 return NULL;
13857
13858 last_y = window_text_bottom_y (w) - dy;
13859
13860 while (1)
13861 {
13862 /* Give up if we have gone too far. */
13863 if (end && row >= end)
13864 return NULL;
13865 /* This formerly returned if they were equal.
13866 I think that both quantities are of a "last plus one" type;
13867 if so, when they are equal, the row is within the screen. -- rms. */
13868 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
13869 return NULL;
13870
13871 /* If it is in this row, return this row. */
13872 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
13873 || (MATRIX_ROW_END_CHARPOS (row) == charpos
13874 /* The end position of a row equals the start
13875 position of the next row. If CHARPOS is there, we
13876 would rather display it in the next line, except
13877 when this line ends in ZV. */
13878 && !row->ends_at_zv_p
13879 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
13880 && charpos >= MATRIX_ROW_START_CHARPOS (row))
13881 return row;
13882 ++row;
13883 }
13884 }
13885
13886
13887 /* Try to redisplay window W by reusing its existing display. W's
13888 current matrix must be up to date when this function is called,
13889 i.e. window_end_valid must not be nil.
13890
13891 Value is
13892
13893 1 if display has been updated
13894 0 if otherwise unsuccessful
13895 -1 if redisplay with same window start is known not to succeed
13896
13897 The following steps are performed:
13898
13899 1. Find the last row in the current matrix of W that is not
13900 affected by changes at the start of current_buffer. If no such row
13901 is found, give up.
13902
13903 2. Find the first row in W's current matrix that is not affected by
13904 changes at the end of current_buffer. Maybe there is no such row.
13905
13906 3. Display lines beginning with the row + 1 found in step 1 to the
13907 row found in step 2 or, if step 2 didn't find a row, to the end of
13908 the window.
13909
13910 4. If cursor is not known to appear on the window, give up.
13911
13912 5. If display stopped at the row found in step 2, scroll the
13913 display and current matrix as needed.
13914
13915 6. Maybe display some lines at the end of W, if we must. This can
13916 happen under various circumstances, like a partially visible line
13917 becoming fully visible, or because newly displayed lines are displayed
13918 in smaller font sizes.
13919
13920 7. Update W's window end information. */
13921
13922 static int
13923 try_window_id (w)
13924 struct window *w;
13925 {
13926 struct frame *f = XFRAME (w->frame);
13927 struct glyph_matrix *current_matrix = w->current_matrix;
13928 struct glyph_matrix *desired_matrix = w->desired_matrix;
13929 struct glyph_row *last_unchanged_at_beg_row;
13930 struct glyph_row *first_unchanged_at_end_row;
13931 struct glyph_row *row;
13932 struct glyph_row *bottom_row;
13933 int bottom_vpos;
13934 struct it it;
13935 int delta = 0, delta_bytes = 0, stop_pos, dvpos, dy;
13936 struct text_pos start_pos;
13937 struct run run;
13938 int first_unchanged_at_end_vpos = 0;
13939 struct glyph_row *last_text_row, *last_text_row_at_end;
13940 struct text_pos start;
13941 int first_changed_charpos, last_changed_charpos;
13942
13943 #if GLYPH_DEBUG
13944 if (inhibit_try_window_id)
13945 return 0;
13946 #endif
13947
13948 /* This is handy for debugging. */
13949 #if 0
13950 #define GIVE_UP(X) \
13951 do { \
13952 fprintf (stderr, "try_window_id give up %d\n", (X)); \
13953 return 0; \
13954 } while (0)
13955 #else
13956 #define GIVE_UP(X) return 0
13957 #endif
13958
13959 SET_TEXT_POS_FROM_MARKER (start, w->start);
13960
13961 /* Don't use this for mini-windows because these can show
13962 messages and mini-buffers, and we don't handle that here. */
13963 if (MINI_WINDOW_P (w))
13964 GIVE_UP (1);
13965
13966 /* This flag is used to prevent redisplay optimizations. */
13967 if (windows_or_buffers_changed || cursor_type_changed)
13968 GIVE_UP (2);
13969
13970 /* Verify that narrowing has not changed.
13971 Also verify that we were not told to prevent redisplay optimizations.
13972 It would be nice to further
13973 reduce the number of cases where this prevents try_window_id. */
13974 if (current_buffer->clip_changed
13975 || current_buffer->prevent_redisplay_optimizations_p)
13976 GIVE_UP (3);
13977
13978 /* Window must either use window-based redisplay or be full width. */
13979 if (!FRAME_WINDOW_P (f)
13980 && (!FRAME_LINE_INS_DEL_OK (f)
13981 || !WINDOW_FULL_WIDTH_P (w)))
13982 GIVE_UP (4);
13983
13984 /* Give up if point is not known NOT to appear in W. */
13985 if (PT < CHARPOS (start))
13986 GIVE_UP (5);
13987
13988 /* Another way to prevent redisplay optimizations. */
13989 if (XFASTINT (w->last_modified) == 0)
13990 GIVE_UP (6);
13991
13992 /* Verify that window is not hscrolled. */
13993 if (XFASTINT (w->hscroll) != 0)
13994 GIVE_UP (7);
13995
13996 /* Verify that display wasn't paused. */
13997 if (NILP (w->window_end_valid))
13998 GIVE_UP (8);
13999
14000 /* Can't use this if highlighting a region because a cursor movement
14001 will do more than just set the cursor. */
14002 if (!NILP (Vtransient_mark_mode)
14003 && !NILP (current_buffer->mark_active))
14004 GIVE_UP (9);
14005
14006 /* Likewise if highlighting trailing whitespace. */
14007 if (!NILP (Vshow_trailing_whitespace))
14008 GIVE_UP (11);
14009
14010 /* Likewise if showing a region. */
14011 if (!NILP (w->region_showing))
14012 GIVE_UP (10);
14013
14014 /* Can use this if overlay arrow position and or string have changed. */
14015 if (overlay_arrows_changed_p ())
14016 GIVE_UP (12);
14017
14018
14019 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
14020 only if buffer has really changed. The reason is that the gap is
14021 initially at Z for freshly visited files. The code below would
14022 set end_unchanged to 0 in that case. */
14023 if (MODIFF > SAVE_MODIFF
14024 /* This seems to happen sometimes after saving a buffer. */
14025 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
14026 {
14027 if (GPT - BEG < BEG_UNCHANGED)
14028 BEG_UNCHANGED = GPT - BEG;
14029 if (Z - GPT < END_UNCHANGED)
14030 END_UNCHANGED = Z - GPT;
14031 }
14032
14033 /* The position of the first and last character that has been changed. */
14034 first_changed_charpos = BEG + BEG_UNCHANGED;
14035 last_changed_charpos = Z - END_UNCHANGED;
14036
14037 /* If window starts after a line end, and the last change is in
14038 front of that newline, then changes don't affect the display.
14039 This case happens with stealth-fontification. Note that although
14040 the display is unchanged, glyph positions in the matrix have to
14041 be adjusted, of course. */
14042 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
14043 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
14044 && ((last_changed_charpos < CHARPOS (start)
14045 && CHARPOS (start) == BEGV)
14046 || (last_changed_charpos < CHARPOS (start) - 1
14047 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
14048 {
14049 int Z_old, delta, Z_BYTE_old, delta_bytes;
14050 struct glyph_row *r0;
14051
14052 /* Compute how many chars/bytes have been added to or removed
14053 from the buffer. */
14054 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
14055 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
14056 delta = Z - Z_old;
14057 delta_bytes = Z_BYTE - Z_BYTE_old;
14058
14059 /* Give up if PT is not in the window. Note that it already has
14060 been checked at the start of try_window_id that PT is not in
14061 front of the window start. */
14062 if (PT >= MATRIX_ROW_END_CHARPOS (row) + delta)
14063 GIVE_UP (13);
14064
14065 /* If window start is unchanged, we can reuse the whole matrix
14066 as is, after adjusting glyph positions. No need to compute
14067 the window end again, since its offset from Z hasn't changed. */
14068 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
14069 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + delta
14070 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + delta_bytes
14071 /* PT must not be in a partially visible line. */
14072 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + delta
14073 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
14074 {
14075 /* Adjust positions in the glyph matrix. */
14076 if (delta || delta_bytes)
14077 {
14078 struct glyph_row *r1
14079 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
14080 increment_matrix_positions (w->current_matrix,
14081 MATRIX_ROW_VPOS (r0, current_matrix),
14082 MATRIX_ROW_VPOS (r1, current_matrix),
14083 delta, delta_bytes);
14084 }
14085
14086 /* Set the cursor. */
14087 row = row_containing_pos (w, PT, r0, NULL, 0);
14088 if (row)
14089 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
14090 else
14091 abort ();
14092 return 1;
14093 }
14094 }
14095
14096 /* Handle the case that changes are all below what is displayed in
14097 the window, and that PT is in the window. This shortcut cannot
14098 be taken if ZV is visible in the window, and text has been added
14099 there that is visible in the window. */
14100 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
14101 /* ZV is not visible in the window, or there are no
14102 changes at ZV, actually. */
14103 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
14104 || first_changed_charpos == last_changed_charpos))
14105 {
14106 struct glyph_row *r0;
14107
14108 /* Give up if PT is not in the window. Note that it already has
14109 been checked at the start of try_window_id that PT is not in
14110 front of the window start. */
14111 if (PT >= MATRIX_ROW_END_CHARPOS (row))
14112 GIVE_UP (14);
14113
14114 /* If window start is unchanged, we can reuse the whole matrix
14115 as is, without changing glyph positions since no text has
14116 been added/removed in front of the window end. */
14117 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
14118 if (TEXT_POS_EQUAL_P (start, r0->start.pos)
14119 /* PT must not be in a partially visible line. */
14120 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
14121 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
14122 {
14123 /* We have to compute the window end anew since text
14124 can have been added/removed after it. */
14125 w->window_end_pos
14126 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
14127 w->window_end_bytepos
14128 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
14129
14130 /* Set the cursor. */
14131 row = row_containing_pos (w, PT, r0, NULL, 0);
14132 if (row)
14133 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
14134 else
14135 abort ();
14136 return 2;
14137 }
14138 }
14139
14140 /* Give up if window start is in the changed area.
14141
14142 The condition used to read
14143
14144 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
14145
14146 but why that was tested escapes me at the moment. */
14147 if (CHARPOS (start) >= first_changed_charpos
14148 && CHARPOS (start) <= last_changed_charpos)
14149 GIVE_UP (15);
14150
14151 /* Check that window start agrees with the start of the first glyph
14152 row in its current matrix. Check this after we know the window
14153 start is not in changed text, otherwise positions would not be
14154 comparable. */
14155 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
14156 if (!TEXT_POS_EQUAL_P (start, row->start.pos))
14157 GIVE_UP (16);
14158
14159 /* Give up if the window ends in strings. Overlay strings
14160 at the end are difficult to handle, so don't try. */
14161 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
14162 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
14163 GIVE_UP (20);
14164
14165 /* Compute the position at which we have to start displaying new
14166 lines. Some of the lines at the top of the window might be
14167 reusable because they are not displaying changed text. Find the
14168 last row in W's current matrix not affected by changes at the
14169 start of current_buffer. Value is null if changes start in the
14170 first line of window. */
14171 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
14172 if (last_unchanged_at_beg_row)
14173 {
14174 /* Avoid starting to display in the moddle of a character, a TAB
14175 for instance. This is easier than to set up the iterator
14176 exactly, and it's not a frequent case, so the additional
14177 effort wouldn't really pay off. */
14178 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
14179 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
14180 && last_unchanged_at_beg_row > w->current_matrix->rows)
14181 --last_unchanged_at_beg_row;
14182
14183 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
14184 GIVE_UP (17);
14185
14186 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
14187 GIVE_UP (18);
14188 start_pos = it.current.pos;
14189
14190 /* Start displaying new lines in the desired matrix at the same
14191 vpos we would use in the current matrix, i.e. below
14192 last_unchanged_at_beg_row. */
14193 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
14194 current_matrix);
14195 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
14196 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
14197
14198 xassert (it.hpos == 0 && it.current_x == 0);
14199 }
14200 else
14201 {
14202 /* There are no reusable lines at the start of the window.
14203 Start displaying in the first text line. */
14204 start_display (&it, w, start);
14205 it.vpos = it.first_vpos;
14206 start_pos = it.current.pos;
14207 }
14208
14209 /* Find the first row that is not affected by changes at the end of
14210 the buffer. Value will be null if there is no unchanged row, in
14211 which case we must redisplay to the end of the window. delta
14212 will be set to the value by which buffer positions beginning with
14213 first_unchanged_at_end_row have to be adjusted due to text
14214 changes. */
14215 first_unchanged_at_end_row
14216 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
14217 IF_DEBUG (debug_delta = delta);
14218 IF_DEBUG (debug_delta_bytes = delta_bytes);
14219
14220 /* Set stop_pos to the buffer position up to which we will have to
14221 display new lines. If first_unchanged_at_end_row != NULL, this
14222 is the buffer position of the start of the line displayed in that
14223 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
14224 that we don't stop at a buffer position. */
14225 stop_pos = 0;
14226 if (first_unchanged_at_end_row)
14227 {
14228 xassert (last_unchanged_at_beg_row == NULL
14229 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
14230
14231 /* If this is a continuation line, move forward to the next one
14232 that isn't. Changes in lines above affect this line.
14233 Caution: this may move first_unchanged_at_end_row to a row
14234 not displaying text. */
14235 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
14236 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
14237 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
14238 < it.last_visible_y))
14239 ++first_unchanged_at_end_row;
14240
14241 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
14242 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
14243 >= it.last_visible_y))
14244 first_unchanged_at_end_row = NULL;
14245 else
14246 {
14247 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
14248 + delta);
14249 first_unchanged_at_end_vpos
14250 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
14251 xassert (stop_pos >= Z - END_UNCHANGED);
14252 }
14253 }
14254 else if (last_unchanged_at_beg_row == NULL)
14255 GIVE_UP (19);
14256
14257
14258 #if GLYPH_DEBUG
14259
14260 /* Either there is no unchanged row at the end, or the one we have
14261 now displays text. This is a necessary condition for the window
14262 end pos calculation at the end of this function. */
14263 xassert (first_unchanged_at_end_row == NULL
14264 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
14265
14266 debug_last_unchanged_at_beg_vpos
14267 = (last_unchanged_at_beg_row
14268 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
14269 : -1);
14270 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
14271
14272 #endif /* GLYPH_DEBUG != 0 */
14273
14274
14275 /* Display new lines. Set last_text_row to the last new line
14276 displayed which has text on it, i.e. might end up as being the
14277 line where the window_end_vpos is. */
14278 w->cursor.vpos = -1;
14279 last_text_row = NULL;
14280 overlay_arrow_seen = 0;
14281 while (it.current_y < it.last_visible_y
14282 && !fonts_changed_p
14283 && (first_unchanged_at_end_row == NULL
14284 || IT_CHARPOS (it) < stop_pos))
14285 {
14286 if (display_line (&it))
14287 last_text_row = it.glyph_row - 1;
14288 }
14289
14290 if (fonts_changed_p)
14291 return -1;
14292
14293
14294 /* Compute differences in buffer positions, y-positions etc. for
14295 lines reused at the bottom of the window. Compute what we can
14296 scroll. */
14297 if (first_unchanged_at_end_row
14298 /* No lines reused because we displayed everything up to the
14299 bottom of the window. */
14300 && it.current_y < it.last_visible_y)
14301 {
14302 dvpos = (it.vpos
14303 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
14304 current_matrix));
14305 dy = it.current_y - first_unchanged_at_end_row->y;
14306 run.current_y = first_unchanged_at_end_row->y;
14307 run.desired_y = run.current_y + dy;
14308 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
14309 }
14310 else
14311 {
14312 delta = dvpos = dy = run.current_y = run.desired_y = run.height = 0;
14313 first_unchanged_at_end_row = NULL;
14314 }
14315 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
14316
14317
14318 /* Find the cursor if not already found. We have to decide whether
14319 PT will appear on this window (it sometimes doesn't, but this is
14320 not a very frequent case.) This decision has to be made before
14321 the current matrix is altered. A value of cursor.vpos < 0 means
14322 that PT is either in one of the lines beginning at
14323 first_unchanged_at_end_row or below the window. Don't care for
14324 lines that might be displayed later at the window end; as
14325 mentioned, this is not a frequent case. */
14326 if (w->cursor.vpos < 0)
14327 {
14328 /* Cursor in unchanged rows at the top? */
14329 if (PT < CHARPOS (start_pos)
14330 && last_unchanged_at_beg_row)
14331 {
14332 row = row_containing_pos (w, PT,
14333 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
14334 last_unchanged_at_beg_row + 1, 0);
14335 if (row)
14336 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
14337 }
14338
14339 /* Start from first_unchanged_at_end_row looking for PT. */
14340 else if (first_unchanged_at_end_row)
14341 {
14342 row = row_containing_pos (w, PT - delta,
14343 first_unchanged_at_end_row, NULL, 0);
14344 if (row)
14345 set_cursor_from_row (w, row, w->current_matrix, delta,
14346 delta_bytes, dy, dvpos);
14347 }
14348
14349 /* Give up if cursor was not found. */
14350 if (w->cursor.vpos < 0)
14351 {
14352 clear_glyph_matrix (w->desired_matrix);
14353 return -1;
14354 }
14355 }
14356
14357 /* Don't let the cursor end in the scroll margins. */
14358 {
14359 int this_scroll_margin, cursor_height;
14360
14361 this_scroll_margin = max (0, scroll_margin);
14362 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
14363 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
14364 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
14365
14366 if ((w->cursor.y < this_scroll_margin
14367 && CHARPOS (start) > BEGV)
14368 /* Old redisplay didn't take scroll margin into account at the bottom,
14369 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
14370 || (w->cursor.y + (make_cursor_line_fully_visible_p
14371 ? cursor_height + this_scroll_margin
14372 : 1)) > it.last_visible_y)
14373 {
14374 w->cursor.vpos = -1;
14375 clear_glyph_matrix (w->desired_matrix);
14376 return -1;
14377 }
14378 }
14379
14380 /* Scroll the display. Do it before changing the current matrix so
14381 that xterm.c doesn't get confused about where the cursor glyph is
14382 found. */
14383 if (dy && run.height)
14384 {
14385 update_begin (f);
14386
14387 if (FRAME_WINDOW_P (f))
14388 {
14389 FRAME_RIF (f)->update_window_begin_hook (w);
14390 FRAME_RIF (f)->clear_window_mouse_face (w);
14391 FRAME_RIF (f)->scroll_run_hook (w, &run);
14392 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
14393 }
14394 else
14395 {
14396 /* Terminal frame. In this case, dvpos gives the number of
14397 lines to scroll by; dvpos < 0 means scroll up. */
14398 int first_unchanged_at_end_vpos
14399 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
14400 int from = WINDOW_TOP_EDGE_LINE (w) + first_unchanged_at_end_vpos;
14401 int end = (WINDOW_TOP_EDGE_LINE (w)
14402 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
14403 + window_internal_height (w));
14404
14405 /* Perform the operation on the screen. */
14406 if (dvpos > 0)
14407 {
14408 /* Scroll last_unchanged_at_beg_row to the end of the
14409 window down dvpos lines. */
14410 set_terminal_window (f, end);
14411
14412 /* On dumb terminals delete dvpos lines at the end
14413 before inserting dvpos empty lines. */
14414 if (!FRAME_SCROLL_REGION_OK (f))
14415 ins_del_lines (f, end - dvpos, -dvpos);
14416
14417 /* Insert dvpos empty lines in front of
14418 last_unchanged_at_beg_row. */
14419 ins_del_lines (f, from, dvpos);
14420 }
14421 else if (dvpos < 0)
14422 {
14423 /* Scroll up last_unchanged_at_beg_vpos to the end of
14424 the window to last_unchanged_at_beg_vpos - |dvpos|. */
14425 set_terminal_window (f, end);
14426
14427 /* Delete dvpos lines in front of
14428 last_unchanged_at_beg_vpos. ins_del_lines will set
14429 the cursor to the given vpos and emit |dvpos| delete
14430 line sequences. */
14431 ins_del_lines (f, from + dvpos, dvpos);
14432
14433 /* On a dumb terminal insert dvpos empty lines at the
14434 end. */
14435 if (!FRAME_SCROLL_REGION_OK (f))
14436 ins_del_lines (f, end + dvpos, -dvpos);
14437 }
14438
14439 set_terminal_window (f, 0);
14440 }
14441
14442 update_end (f);
14443 }
14444
14445 /* Shift reused rows of the current matrix to the right position.
14446 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
14447 text. */
14448 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
14449 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
14450 if (dvpos < 0)
14451 {
14452 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
14453 bottom_vpos, dvpos);
14454 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
14455 bottom_vpos, 0);
14456 }
14457 else if (dvpos > 0)
14458 {
14459 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
14460 bottom_vpos, dvpos);
14461 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
14462 first_unchanged_at_end_vpos + dvpos, 0);
14463 }
14464
14465 /* For frame-based redisplay, make sure that current frame and window
14466 matrix are in sync with respect to glyph memory. */
14467 if (!FRAME_WINDOW_P (f))
14468 sync_frame_with_window_matrix_rows (w);
14469
14470 /* Adjust buffer positions in reused rows. */
14471 if (delta)
14472 increment_matrix_positions (current_matrix,
14473 first_unchanged_at_end_vpos + dvpos,
14474 bottom_vpos, delta, delta_bytes);
14475
14476 /* Adjust Y positions. */
14477 if (dy)
14478 shift_glyph_matrix (w, current_matrix,
14479 first_unchanged_at_end_vpos + dvpos,
14480 bottom_vpos, dy);
14481
14482 if (first_unchanged_at_end_row)
14483 {
14484 first_unchanged_at_end_row += dvpos;
14485 if (first_unchanged_at_end_row->y >= it.last_visible_y
14486 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
14487 first_unchanged_at_end_row = NULL;
14488 }
14489
14490 /* If scrolling up, there may be some lines to display at the end of
14491 the window. */
14492 last_text_row_at_end = NULL;
14493 if (dy < 0)
14494 {
14495 /* Scrolling up can leave for example a partially visible line
14496 at the end of the window to be redisplayed. */
14497 /* Set last_row to the glyph row in the current matrix where the
14498 window end line is found. It has been moved up or down in
14499 the matrix by dvpos. */
14500 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
14501 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
14502
14503 /* If last_row is the window end line, it should display text. */
14504 xassert (last_row->displays_text_p);
14505
14506 /* If window end line was partially visible before, begin
14507 displaying at that line. Otherwise begin displaying with the
14508 line following it. */
14509 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
14510 {
14511 init_to_row_start (&it, w, last_row);
14512 it.vpos = last_vpos;
14513 it.current_y = last_row->y;
14514 }
14515 else
14516 {
14517 init_to_row_end (&it, w, last_row);
14518 it.vpos = 1 + last_vpos;
14519 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
14520 ++last_row;
14521 }
14522
14523 /* We may start in a continuation line. If so, we have to
14524 get the right continuation_lines_width and current_x. */
14525 it.continuation_lines_width = last_row->continuation_lines_width;
14526 it.hpos = it.current_x = 0;
14527
14528 /* Display the rest of the lines at the window end. */
14529 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
14530 while (it.current_y < it.last_visible_y
14531 && !fonts_changed_p)
14532 {
14533 /* Is it always sure that the display agrees with lines in
14534 the current matrix? I don't think so, so we mark rows
14535 displayed invalid in the current matrix by setting their
14536 enabled_p flag to zero. */
14537 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
14538 if (display_line (&it))
14539 last_text_row_at_end = it.glyph_row - 1;
14540 }
14541 }
14542
14543 /* Update window_end_pos and window_end_vpos. */
14544 if (first_unchanged_at_end_row
14545 && !last_text_row_at_end)
14546 {
14547 /* Window end line if one of the preserved rows from the current
14548 matrix. Set row to the last row displaying text in current
14549 matrix starting at first_unchanged_at_end_row, after
14550 scrolling. */
14551 xassert (first_unchanged_at_end_row->displays_text_p);
14552 row = find_last_row_displaying_text (w->current_matrix, &it,
14553 first_unchanged_at_end_row);
14554 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
14555
14556 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
14557 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
14558 w->window_end_vpos
14559 = make_number (MATRIX_ROW_VPOS (row, w->current_matrix));
14560 xassert (w->window_end_bytepos >= 0);
14561 IF_DEBUG (debug_method_add (w, "A"));
14562 }
14563 else if (last_text_row_at_end)
14564 {
14565 w->window_end_pos
14566 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
14567 w->window_end_bytepos
14568 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
14569 w->window_end_vpos
14570 = make_number (MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
14571 xassert (w->window_end_bytepos >= 0);
14572 IF_DEBUG (debug_method_add (w, "B"));
14573 }
14574 else if (last_text_row)
14575 {
14576 /* We have displayed either to the end of the window or at the
14577 end of the window, i.e. the last row with text is to be found
14578 in the desired matrix. */
14579 w->window_end_pos
14580 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
14581 w->window_end_bytepos
14582 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
14583 w->window_end_vpos
14584 = make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix));
14585 xassert (w->window_end_bytepos >= 0);
14586 }
14587 else if (first_unchanged_at_end_row == NULL
14588 && last_text_row == NULL
14589 && last_text_row_at_end == NULL)
14590 {
14591 /* Displayed to end of window, but no line containing text was
14592 displayed. Lines were deleted at the end of the window. */
14593 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
14594 int vpos = XFASTINT (w->window_end_vpos);
14595 struct glyph_row *current_row = current_matrix->rows + vpos;
14596 struct glyph_row *desired_row = desired_matrix->rows + vpos;
14597
14598 for (row = NULL;
14599 row == NULL && vpos >= first_vpos;
14600 --vpos, --current_row, --desired_row)
14601 {
14602 if (desired_row->enabled_p)
14603 {
14604 if (desired_row->displays_text_p)
14605 row = desired_row;
14606 }
14607 else if (current_row->displays_text_p)
14608 row = current_row;
14609 }
14610
14611 xassert (row != NULL);
14612 w->window_end_vpos = make_number (vpos + 1);
14613 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
14614 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
14615 xassert (w->window_end_bytepos >= 0);
14616 IF_DEBUG (debug_method_add (w, "C"));
14617 }
14618 else
14619 abort ();
14620
14621 #if 0 /* This leads to problems, for instance when the cursor is
14622 at ZV, and the cursor line displays no text. */
14623 /* Disable rows below what's displayed in the window. This makes
14624 debugging easier. */
14625 enable_glyph_matrix_rows (current_matrix,
14626 XFASTINT (w->window_end_vpos) + 1,
14627 bottom_vpos, 0);
14628 #endif
14629
14630 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
14631 debug_end_vpos = XFASTINT (w->window_end_vpos));
14632
14633 /* Record that display has not been completed. */
14634 w->window_end_valid = Qnil;
14635 w->desired_matrix->no_scrolling_p = 1;
14636 return 3;
14637
14638 #undef GIVE_UP
14639 }
14640
14641
14642 \f
14643 /***********************************************************************
14644 More debugging support
14645 ***********************************************************************/
14646
14647 #if GLYPH_DEBUG
14648
14649 void dump_glyph_row P_ ((struct glyph_row *, int, int));
14650 void dump_glyph_matrix P_ ((struct glyph_matrix *, int));
14651 void dump_glyph P_ ((struct glyph_row *, struct glyph *, int));
14652
14653
14654 /* Dump the contents of glyph matrix MATRIX on stderr.
14655
14656 GLYPHS 0 means don't show glyph contents.
14657 GLYPHS 1 means show glyphs in short form
14658 GLYPHS > 1 means show glyphs in long form. */
14659
14660 void
14661 dump_glyph_matrix (matrix, glyphs)
14662 struct glyph_matrix *matrix;
14663 int glyphs;
14664 {
14665 int i;
14666 for (i = 0; i < matrix->nrows; ++i)
14667 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
14668 }
14669
14670
14671 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
14672 the glyph row and area where the glyph comes from. */
14673
14674 void
14675 dump_glyph (row, glyph, area)
14676 struct glyph_row *row;
14677 struct glyph *glyph;
14678 int area;
14679 {
14680 if (glyph->type == CHAR_GLYPH)
14681 {
14682 fprintf (stderr,
14683 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
14684 glyph - row->glyphs[TEXT_AREA],
14685 'C',
14686 glyph->charpos,
14687 (BUFFERP (glyph->object)
14688 ? 'B'
14689 : (STRINGP (glyph->object)
14690 ? 'S'
14691 : '-')),
14692 glyph->pixel_width,
14693 glyph->u.ch,
14694 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
14695 ? glyph->u.ch
14696 : '.'),
14697 glyph->face_id,
14698 glyph->left_box_line_p,
14699 glyph->right_box_line_p);
14700 }
14701 else if (glyph->type == STRETCH_GLYPH)
14702 {
14703 fprintf (stderr,
14704 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
14705 glyph - row->glyphs[TEXT_AREA],
14706 'S',
14707 glyph->charpos,
14708 (BUFFERP (glyph->object)
14709 ? 'B'
14710 : (STRINGP (glyph->object)
14711 ? 'S'
14712 : '-')),
14713 glyph->pixel_width,
14714 0,
14715 '.',
14716 glyph->face_id,
14717 glyph->left_box_line_p,
14718 glyph->right_box_line_p);
14719 }
14720 else if (glyph->type == IMAGE_GLYPH)
14721 {
14722 fprintf (stderr,
14723 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
14724 glyph - row->glyphs[TEXT_AREA],
14725 'I',
14726 glyph->charpos,
14727 (BUFFERP (glyph->object)
14728 ? 'B'
14729 : (STRINGP (glyph->object)
14730 ? 'S'
14731 : '-')),
14732 glyph->pixel_width,
14733 glyph->u.img_id,
14734 '.',
14735 glyph->face_id,
14736 glyph->left_box_line_p,
14737 glyph->right_box_line_p);
14738 }
14739 }
14740
14741
14742 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
14743 GLYPHS 0 means don't show glyph contents.
14744 GLYPHS 1 means show glyphs in short form
14745 GLYPHS > 1 means show glyphs in long form. */
14746
14747 void
14748 dump_glyph_row (row, vpos, glyphs)
14749 struct glyph_row *row;
14750 int vpos, glyphs;
14751 {
14752 if (glyphs != 1)
14753 {
14754 fprintf (stderr, "Row Start End Used oEI><\\CTZFesm X Y W H V A P\n");
14755 fprintf (stderr, "======================================================================\n");
14756
14757 fprintf (stderr, "%3d %5d %5d %4d %1.1d%1.1d%1.1d%1.1d\
14758 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
14759 vpos,
14760 MATRIX_ROW_START_CHARPOS (row),
14761 MATRIX_ROW_END_CHARPOS (row),
14762 row->used[TEXT_AREA],
14763 row->contains_overlapping_glyphs_p,
14764 row->enabled_p,
14765 row->truncated_on_left_p,
14766 row->truncated_on_right_p,
14767 row->continued_p,
14768 MATRIX_ROW_CONTINUATION_LINE_P (row),
14769 row->displays_text_p,
14770 row->ends_at_zv_p,
14771 row->fill_line_p,
14772 row->ends_in_middle_of_char_p,
14773 row->starts_in_middle_of_char_p,
14774 row->mouse_face_p,
14775 row->x,
14776 row->y,
14777 row->pixel_width,
14778 row->height,
14779 row->visible_height,
14780 row->ascent,
14781 row->phys_ascent);
14782 fprintf (stderr, "%9d %5d\t%5d\n", row->start.overlay_string_index,
14783 row->end.overlay_string_index,
14784 row->continuation_lines_width);
14785 fprintf (stderr, "%9d %5d\n",
14786 CHARPOS (row->start.string_pos),
14787 CHARPOS (row->end.string_pos));
14788 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
14789 row->end.dpvec_index);
14790 }
14791
14792 if (glyphs > 1)
14793 {
14794 int area;
14795
14796 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
14797 {
14798 struct glyph *glyph = row->glyphs[area];
14799 struct glyph *glyph_end = glyph + row->used[area];
14800
14801 /* Glyph for a line end in text. */
14802 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
14803 ++glyph_end;
14804
14805 if (glyph < glyph_end)
14806 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
14807
14808 for (; glyph < glyph_end; ++glyph)
14809 dump_glyph (row, glyph, area);
14810 }
14811 }
14812 else if (glyphs == 1)
14813 {
14814 int area;
14815
14816 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
14817 {
14818 char *s = (char *) alloca (row->used[area] + 1);
14819 int i;
14820
14821 for (i = 0; i < row->used[area]; ++i)
14822 {
14823 struct glyph *glyph = row->glyphs[area] + i;
14824 if (glyph->type == CHAR_GLYPH
14825 && glyph->u.ch < 0x80
14826 && glyph->u.ch >= ' ')
14827 s[i] = glyph->u.ch;
14828 else
14829 s[i] = '.';
14830 }
14831
14832 s[i] = '\0';
14833 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
14834 }
14835 }
14836 }
14837
14838
14839 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
14840 Sdump_glyph_matrix, 0, 1, "p",
14841 doc: /* Dump the current matrix of the selected window to stderr.
14842 Shows contents of glyph row structures. With non-nil
14843 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
14844 glyphs in short form, otherwise show glyphs in long form. */)
14845 (glyphs)
14846 Lisp_Object glyphs;
14847 {
14848 struct window *w = XWINDOW (selected_window);
14849 struct buffer *buffer = XBUFFER (w->buffer);
14850
14851 fprintf (stderr, "PT = %d, BEGV = %d. ZV = %d\n",
14852 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
14853 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
14854 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
14855 fprintf (stderr, "=============================================\n");
14856 dump_glyph_matrix (w->current_matrix,
14857 NILP (glyphs) ? 0 : XINT (glyphs));
14858 return Qnil;
14859 }
14860
14861
14862 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
14863 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
14864 ()
14865 {
14866 struct frame *f = XFRAME (selected_frame);
14867 dump_glyph_matrix (f->current_matrix, 1);
14868 return Qnil;
14869 }
14870
14871
14872 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
14873 doc: /* Dump glyph row ROW to stderr.
14874 GLYPH 0 means don't dump glyphs.
14875 GLYPH 1 means dump glyphs in short form.
14876 GLYPH > 1 or omitted means dump glyphs in long form. */)
14877 (row, glyphs)
14878 Lisp_Object row, glyphs;
14879 {
14880 struct glyph_matrix *matrix;
14881 int vpos;
14882
14883 CHECK_NUMBER (row);
14884 matrix = XWINDOW (selected_window)->current_matrix;
14885 vpos = XINT (row);
14886 if (vpos >= 0 && vpos < matrix->nrows)
14887 dump_glyph_row (MATRIX_ROW (matrix, vpos),
14888 vpos,
14889 INTEGERP (glyphs) ? XINT (glyphs) : 2);
14890 return Qnil;
14891 }
14892
14893
14894 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
14895 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
14896 GLYPH 0 means don't dump glyphs.
14897 GLYPH 1 means dump glyphs in short form.
14898 GLYPH > 1 or omitted means dump glyphs in long form. */)
14899 (row, glyphs)
14900 Lisp_Object row, glyphs;
14901 {
14902 struct frame *sf = SELECTED_FRAME ();
14903 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
14904 int vpos;
14905
14906 CHECK_NUMBER (row);
14907 vpos = XINT (row);
14908 if (vpos >= 0 && vpos < m->nrows)
14909 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
14910 INTEGERP (glyphs) ? XINT (glyphs) : 2);
14911 return Qnil;
14912 }
14913
14914
14915 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
14916 doc: /* Toggle tracing of redisplay.
14917 With ARG, turn tracing on if and only if ARG is positive. */)
14918 (arg)
14919 Lisp_Object arg;
14920 {
14921 if (NILP (arg))
14922 trace_redisplay_p = !trace_redisplay_p;
14923 else
14924 {
14925 arg = Fprefix_numeric_value (arg);
14926 trace_redisplay_p = XINT (arg) > 0;
14927 }
14928
14929 return Qnil;
14930 }
14931
14932
14933 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
14934 doc: /* Like `format', but print result to stderr.
14935 usage: (trace-to-stderr STRING &rest OBJECTS) */)
14936 (nargs, args)
14937 int nargs;
14938 Lisp_Object *args;
14939 {
14940 Lisp_Object s = Fformat (nargs, args);
14941 fprintf (stderr, "%s", SDATA (s));
14942 return Qnil;
14943 }
14944
14945 #endif /* GLYPH_DEBUG */
14946
14947
14948 \f
14949 /***********************************************************************
14950 Building Desired Matrix Rows
14951 ***********************************************************************/
14952
14953 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
14954 Used for non-window-redisplay windows, and for windows w/o left fringe. */
14955
14956 static struct glyph_row *
14957 get_overlay_arrow_glyph_row (w, overlay_arrow_string)
14958 struct window *w;
14959 Lisp_Object overlay_arrow_string;
14960 {
14961 struct frame *f = XFRAME (WINDOW_FRAME (w));
14962 struct buffer *buffer = XBUFFER (w->buffer);
14963 struct buffer *old = current_buffer;
14964 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
14965 int arrow_len = SCHARS (overlay_arrow_string);
14966 const unsigned char *arrow_end = arrow_string + arrow_len;
14967 const unsigned char *p;
14968 struct it it;
14969 int multibyte_p;
14970 int n_glyphs_before;
14971
14972 set_buffer_temp (buffer);
14973 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
14974 it.glyph_row->used[TEXT_AREA] = 0;
14975 SET_TEXT_POS (it.position, 0, 0);
14976
14977 multibyte_p = !NILP (buffer->enable_multibyte_characters);
14978 p = arrow_string;
14979 while (p < arrow_end)
14980 {
14981 Lisp_Object face, ilisp;
14982
14983 /* Get the next character. */
14984 if (multibyte_p)
14985 it.c = string_char_and_length (p, arrow_len, &it.len);
14986 else
14987 it.c = *p, it.len = 1;
14988 p += it.len;
14989
14990 /* Get its face. */
14991 ilisp = make_number (p - arrow_string);
14992 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
14993 it.face_id = compute_char_face (f, it.c, face);
14994
14995 /* Compute its width, get its glyphs. */
14996 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
14997 SET_TEXT_POS (it.position, -1, -1);
14998 PRODUCE_GLYPHS (&it);
14999
15000 /* If this character doesn't fit any more in the line, we have
15001 to remove some glyphs. */
15002 if (it.current_x > it.last_visible_x)
15003 {
15004 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
15005 break;
15006 }
15007 }
15008
15009 set_buffer_temp (old);
15010 return it.glyph_row;
15011 }
15012
15013
15014 /* Insert truncation glyphs at the start of IT->glyph_row. Truncation
15015 glyphs are only inserted for terminal frames since we can't really
15016 win with truncation glyphs when partially visible glyphs are
15017 involved. Which glyphs to insert is determined by
15018 produce_special_glyphs. */
15019
15020 static void
15021 insert_left_trunc_glyphs (it)
15022 struct it *it;
15023 {
15024 struct it truncate_it;
15025 struct glyph *from, *end, *to, *toend;
15026
15027 xassert (!FRAME_WINDOW_P (it->f));
15028
15029 /* Get the truncation glyphs. */
15030 truncate_it = *it;
15031 truncate_it.current_x = 0;
15032 truncate_it.face_id = DEFAULT_FACE_ID;
15033 truncate_it.glyph_row = &scratch_glyph_row;
15034 truncate_it.glyph_row->used[TEXT_AREA] = 0;
15035 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
15036 truncate_it.object = make_number (0);
15037 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
15038
15039 /* Overwrite glyphs from IT with truncation glyphs. */
15040 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
15041 end = from + truncate_it.glyph_row->used[TEXT_AREA];
15042 to = it->glyph_row->glyphs[TEXT_AREA];
15043 toend = to + it->glyph_row->used[TEXT_AREA];
15044
15045 while (from < end)
15046 *to++ = *from++;
15047
15048 /* There may be padding glyphs left over. Overwrite them too. */
15049 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
15050 {
15051 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
15052 while (from < end)
15053 *to++ = *from++;
15054 }
15055
15056 if (to > toend)
15057 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
15058 }
15059
15060
15061 /* Compute the pixel height and width of IT->glyph_row.
15062
15063 Most of the time, ascent and height of a display line will be equal
15064 to the max_ascent and max_height values of the display iterator
15065 structure. This is not the case if
15066
15067 1. We hit ZV without displaying anything. In this case, max_ascent
15068 and max_height will be zero.
15069
15070 2. We have some glyphs that don't contribute to the line height.
15071 (The glyph row flag contributes_to_line_height_p is for future
15072 pixmap extensions).
15073
15074 The first case is easily covered by using default values because in
15075 these cases, the line height does not really matter, except that it
15076 must not be zero. */
15077
15078 static void
15079 compute_line_metrics (it)
15080 struct it *it;
15081 {
15082 struct glyph_row *row = it->glyph_row;
15083 int area, i;
15084
15085 if (FRAME_WINDOW_P (it->f))
15086 {
15087 int i, min_y, max_y;
15088
15089 /* The line may consist of one space only, that was added to
15090 place the cursor on it. If so, the row's height hasn't been
15091 computed yet. */
15092 if (row->height == 0)
15093 {
15094 if (it->max_ascent + it->max_descent == 0)
15095 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
15096 row->ascent = it->max_ascent;
15097 row->height = it->max_ascent + it->max_descent;
15098 row->phys_ascent = it->max_phys_ascent;
15099 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
15100 row->extra_line_spacing = it->max_extra_line_spacing;
15101 }
15102
15103 /* Compute the width of this line. */
15104 row->pixel_width = row->x;
15105 for (i = 0; i < row->used[TEXT_AREA]; ++i)
15106 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
15107
15108 xassert (row->pixel_width >= 0);
15109 xassert (row->ascent >= 0 && row->height > 0);
15110
15111 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
15112 || MATRIX_ROW_OVERLAPS_PRED_P (row));
15113
15114 /* If first line's physical ascent is larger than its logical
15115 ascent, use the physical ascent, and make the row taller.
15116 This makes accented characters fully visible. */
15117 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
15118 && row->phys_ascent > row->ascent)
15119 {
15120 row->height += row->phys_ascent - row->ascent;
15121 row->ascent = row->phys_ascent;
15122 }
15123
15124 /* Compute how much of the line is visible. */
15125 row->visible_height = row->height;
15126
15127 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
15128 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
15129
15130 if (row->y < min_y)
15131 row->visible_height -= min_y - row->y;
15132 if (row->y + row->height > max_y)
15133 row->visible_height -= row->y + row->height - max_y;
15134 }
15135 else
15136 {
15137 row->pixel_width = row->used[TEXT_AREA];
15138 if (row->continued_p)
15139 row->pixel_width -= it->continuation_pixel_width;
15140 else if (row->truncated_on_right_p)
15141 row->pixel_width -= it->truncation_pixel_width;
15142 row->ascent = row->phys_ascent = 0;
15143 row->height = row->phys_height = row->visible_height = 1;
15144 row->extra_line_spacing = 0;
15145 }
15146
15147 /* Compute a hash code for this row. */
15148 row->hash = 0;
15149 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
15150 for (i = 0; i < row->used[area]; ++i)
15151 row->hash = ((((row->hash << 4) + (row->hash >> 24)) & 0x0fffffff)
15152 + row->glyphs[area][i].u.val
15153 + row->glyphs[area][i].face_id
15154 + row->glyphs[area][i].padding_p
15155 + (row->glyphs[area][i].type << 2));
15156
15157 it->max_ascent = it->max_descent = 0;
15158 it->max_phys_ascent = it->max_phys_descent = 0;
15159 }
15160
15161
15162 /* Append one space to the glyph row of iterator IT if doing a
15163 window-based redisplay. The space has the same face as
15164 IT->face_id. Value is non-zero if a space was added.
15165
15166 This function is called to make sure that there is always one glyph
15167 at the end of a glyph row that the cursor can be set on under
15168 window-systems. (If there weren't such a glyph we would not know
15169 how wide and tall a box cursor should be displayed).
15170
15171 At the same time this space let's a nicely handle clearing to the
15172 end of the line if the row ends in italic text. */
15173
15174 static int
15175 append_space_for_newline (it, default_face_p)
15176 struct it *it;
15177 int default_face_p;
15178 {
15179 if (FRAME_WINDOW_P (it->f))
15180 {
15181 int n = it->glyph_row->used[TEXT_AREA];
15182
15183 if (it->glyph_row->glyphs[TEXT_AREA] + n
15184 < it->glyph_row->glyphs[1 + TEXT_AREA])
15185 {
15186 /* Save some values that must not be changed.
15187 Must save IT->c and IT->len because otherwise
15188 ITERATOR_AT_END_P wouldn't work anymore after
15189 append_space_for_newline has been called. */
15190 enum display_element_type saved_what = it->what;
15191 int saved_c = it->c, saved_len = it->len;
15192 int saved_x = it->current_x;
15193 int saved_face_id = it->face_id;
15194 struct text_pos saved_pos;
15195 Lisp_Object saved_object;
15196 struct face *face;
15197
15198 saved_object = it->object;
15199 saved_pos = it->position;
15200
15201 it->what = IT_CHARACTER;
15202 bzero (&it->position, sizeof it->position);
15203 it->object = make_number (0);
15204 it->c = ' ';
15205 it->len = 1;
15206
15207 if (default_face_p)
15208 it->face_id = DEFAULT_FACE_ID;
15209 else if (it->face_before_selective_p)
15210 it->face_id = it->saved_face_id;
15211 face = FACE_FROM_ID (it->f, it->face_id);
15212 it->face_id = FACE_FOR_CHAR (it->f, face, 0);
15213
15214 PRODUCE_GLYPHS (it);
15215
15216 it->override_ascent = -1;
15217 it->constrain_row_ascent_descent_p = 0;
15218 it->current_x = saved_x;
15219 it->object = saved_object;
15220 it->position = saved_pos;
15221 it->what = saved_what;
15222 it->face_id = saved_face_id;
15223 it->len = saved_len;
15224 it->c = saved_c;
15225 return 1;
15226 }
15227 }
15228
15229 return 0;
15230 }
15231
15232
15233 /* Extend the face of the last glyph in the text area of IT->glyph_row
15234 to the end of the display line. Called from display_line.
15235 If the glyph row is empty, add a space glyph to it so that we
15236 know the face to draw. Set the glyph row flag fill_line_p. */
15237
15238 static void
15239 extend_face_to_end_of_line (it)
15240 struct it *it;
15241 {
15242 struct face *face;
15243 struct frame *f = it->f;
15244
15245 /* If line is already filled, do nothing. */
15246 if (it->current_x >= it->last_visible_x)
15247 return;
15248
15249 /* Face extension extends the background and box of IT->face_id
15250 to the end of the line. If the background equals the background
15251 of the frame, we don't have to do anything. */
15252 if (it->face_before_selective_p)
15253 face = FACE_FROM_ID (it->f, it->saved_face_id);
15254 else
15255 face = FACE_FROM_ID (f, it->face_id);
15256
15257 if (FRAME_WINDOW_P (f)
15258 && face->box == FACE_NO_BOX
15259 && face->background == FRAME_BACKGROUND_PIXEL (f)
15260 && !face->stipple)
15261 return;
15262
15263 /* Set the glyph row flag indicating that the face of the last glyph
15264 in the text area has to be drawn to the end of the text area. */
15265 it->glyph_row->fill_line_p = 1;
15266
15267 /* If current character of IT is not ASCII, make sure we have the
15268 ASCII face. This will be automatically undone the next time
15269 get_next_display_element returns a multibyte character. Note
15270 that the character will always be single byte in unibyte text. */
15271 if (!SINGLE_BYTE_CHAR_P (it->c))
15272 {
15273 it->face_id = FACE_FOR_CHAR (f, face, 0);
15274 }
15275
15276 if (FRAME_WINDOW_P (f))
15277 {
15278 /* If the row is empty, add a space with the current face of IT,
15279 so that we know which face to draw. */
15280 if (it->glyph_row->used[TEXT_AREA] == 0)
15281 {
15282 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
15283 it->glyph_row->glyphs[TEXT_AREA][0].face_id = it->face_id;
15284 it->glyph_row->used[TEXT_AREA] = 1;
15285 }
15286 }
15287 else
15288 {
15289 /* Save some values that must not be changed. */
15290 int saved_x = it->current_x;
15291 struct text_pos saved_pos;
15292 Lisp_Object saved_object;
15293 enum display_element_type saved_what = it->what;
15294 int saved_face_id = it->face_id;
15295
15296 saved_object = it->object;
15297 saved_pos = it->position;
15298
15299 it->what = IT_CHARACTER;
15300 bzero (&it->position, sizeof it->position);
15301 it->object = make_number (0);
15302 it->c = ' ';
15303 it->len = 1;
15304 it->face_id = face->id;
15305
15306 PRODUCE_GLYPHS (it);
15307
15308 while (it->current_x <= it->last_visible_x)
15309 PRODUCE_GLYPHS (it);
15310
15311 /* Don't count these blanks really. It would let us insert a left
15312 truncation glyph below and make us set the cursor on them, maybe. */
15313 it->current_x = saved_x;
15314 it->object = saved_object;
15315 it->position = saved_pos;
15316 it->what = saved_what;
15317 it->face_id = saved_face_id;
15318 }
15319 }
15320
15321
15322 /* Value is non-zero if text starting at CHARPOS in current_buffer is
15323 trailing whitespace. */
15324
15325 static int
15326 trailing_whitespace_p (charpos)
15327 int charpos;
15328 {
15329 int bytepos = CHAR_TO_BYTE (charpos);
15330 int c = 0;
15331
15332 while (bytepos < ZV_BYTE
15333 && (c = FETCH_CHAR (bytepos),
15334 c == ' ' || c == '\t'))
15335 ++bytepos;
15336
15337 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
15338 {
15339 if (bytepos != PT_BYTE)
15340 return 1;
15341 }
15342 return 0;
15343 }
15344
15345
15346 /* Highlight trailing whitespace, if any, in ROW. */
15347
15348 void
15349 highlight_trailing_whitespace (f, row)
15350 struct frame *f;
15351 struct glyph_row *row;
15352 {
15353 int used = row->used[TEXT_AREA];
15354
15355 if (used)
15356 {
15357 struct glyph *start = row->glyphs[TEXT_AREA];
15358 struct glyph *glyph = start + used - 1;
15359
15360 /* Skip over glyphs inserted to display the cursor at the
15361 end of a line, for extending the face of the last glyph
15362 to the end of the line on terminals, and for truncation
15363 and continuation glyphs. */
15364 while (glyph >= start
15365 && glyph->type == CHAR_GLYPH
15366 && INTEGERP (glyph->object))
15367 --glyph;
15368
15369 /* If last glyph is a space or stretch, and it's trailing
15370 whitespace, set the face of all trailing whitespace glyphs in
15371 IT->glyph_row to `trailing-whitespace'. */
15372 if (glyph >= start
15373 && BUFFERP (glyph->object)
15374 && (glyph->type == STRETCH_GLYPH
15375 || (glyph->type == CHAR_GLYPH
15376 && glyph->u.ch == ' '))
15377 && trailing_whitespace_p (glyph->charpos))
15378 {
15379 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0, 0);
15380 if (face_id < 0)
15381 return;
15382
15383 while (glyph >= start
15384 && BUFFERP (glyph->object)
15385 && (glyph->type == STRETCH_GLYPH
15386 || (glyph->type == CHAR_GLYPH
15387 && glyph->u.ch == ' ')))
15388 (glyph--)->face_id = face_id;
15389 }
15390 }
15391 }
15392
15393
15394 /* Value is non-zero if glyph row ROW in window W should be
15395 used to hold the cursor. */
15396
15397 static int
15398 cursor_row_p (w, row)
15399 struct window *w;
15400 struct glyph_row *row;
15401 {
15402 int cursor_row_p = 1;
15403
15404 if (PT == MATRIX_ROW_END_CHARPOS (row))
15405 {
15406 /* If the row ends with a newline from a string, we don't want
15407 the cursor there, but we still want it at the start of the
15408 string if the string starts in this row.
15409 If the row is continued it doesn't end in a newline. */
15410 if (CHARPOS (row->end.string_pos) >= 0)
15411 cursor_row_p = (row->continued_p
15412 || PT >= MATRIX_ROW_START_CHARPOS (row));
15413 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
15414 {
15415 /* If the row ends in middle of a real character,
15416 and the line is continued, we want the cursor here.
15417 That's because MATRIX_ROW_END_CHARPOS would equal
15418 PT if PT is before the character. */
15419 if (!row->ends_in_ellipsis_p)
15420 cursor_row_p = row->continued_p;
15421 else
15422 /* If the row ends in an ellipsis, then
15423 MATRIX_ROW_END_CHARPOS will equal point after the invisible text.
15424 We want that position to be displayed after the ellipsis. */
15425 cursor_row_p = 0;
15426 }
15427 /* If the row ends at ZV, display the cursor at the end of that
15428 row instead of at the start of the row below. */
15429 else if (row->ends_at_zv_p)
15430 cursor_row_p = 1;
15431 else
15432 cursor_row_p = 0;
15433 }
15434
15435 return cursor_row_p;
15436 }
15437
15438
15439 /* Construct the glyph row IT->glyph_row in the desired matrix of
15440 IT->w from text at the current position of IT. See dispextern.h
15441 for an overview of struct it. Value is non-zero if
15442 IT->glyph_row displays text, as opposed to a line displaying ZV
15443 only. */
15444
15445 static int
15446 display_line (it)
15447 struct it *it;
15448 {
15449 struct glyph_row *row = it->glyph_row;
15450 Lisp_Object overlay_arrow_string;
15451
15452 /* We always start displaying at hpos zero even if hscrolled. */
15453 xassert (it->hpos == 0 && it->current_x == 0);
15454
15455 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
15456 >= it->w->desired_matrix->nrows)
15457 {
15458 it->w->nrows_scale_factor++;
15459 fonts_changed_p = 1;
15460 return 0;
15461 }
15462
15463 /* Is IT->w showing the region? */
15464 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
15465
15466 /* Clear the result glyph row and enable it. */
15467 prepare_desired_row (row);
15468
15469 row->y = it->current_y;
15470 row->start = it->start;
15471 row->continuation_lines_width = it->continuation_lines_width;
15472 row->displays_text_p = 1;
15473 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
15474 it->starts_in_middle_of_char_p = 0;
15475
15476 /* Arrange the overlays nicely for our purposes. Usually, we call
15477 display_line on only one line at a time, in which case this
15478 can't really hurt too much, or we call it on lines which appear
15479 one after another in the buffer, in which case all calls to
15480 recenter_overlay_lists but the first will be pretty cheap. */
15481 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
15482
15483 /* Move over display elements that are not visible because we are
15484 hscrolled. This may stop at an x-position < IT->first_visible_x
15485 if the first glyph is partially visible or if we hit a line end. */
15486 if (it->current_x < it->first_visible_x)
15487 {
15488 move_it_in_display_line_to (it, ZV, it->first_visible_x,
15489 MOVE_TO_POS | MOVE_TO_X);
15490 }
15491
15492 /* Get the initial row height. This is either the height of the
15493 text hscrolled, if there is any, or zero. */
15494 row->ascent = it->max_ascent;
15495 row->height = it->max_ascent + it->max_descent;
15496 row->phys_ascent = it->max_phys_ascent;
15497 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
15498 row->extra_line_spacing = it->max_extra_line_spacing;
15499
15500 /* Loop generating characters. The loop is left with IT on the next
15501 character to display. */
15502 while (1)
15503 {
15504 int n_glyphs_before, hpos_before, x_before;
15505 int x, i, nglyphs;
15506 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
15507
15508 /* Retrieve the next thing to display. Value is zero if end of
15509 buffer reached. */
15510 if (!get_next_display_element (it))
15511 {
15512 /* Maybe add a space at the end of this line that is used to
15513 display the cursor there under X. Set the charpos of the
15514 first glyph of blank lines not corresponding to any text
15515 to -1. */
15516 #ifdef HAVE_WINDOW_SYSTEM
15517 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
15518 row->exact_window_width_line_p = 1;
15519 else
15520 #endif /* HAVE_WINDOW_SYSTEM */
15521 if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
15522 || row->used[TEXT_AREA] == 0)
15523 {
15524 row->glyphs[TEXT_AREA]->charpos = -1;
15525 row->displays_text_p = 0;
15526
15527 if (!NILP (XBUFFER (it->w->buffer)->indicate_empty_lines)
15528 && (!MINI_WINDOW_P (it->w)
15529 || (minibuf_level && EQ (it->window, minibuf_window))))
15530 row->indicate_empty_line_p = 1;
15531 }
15532
15533 it->continuation_lines_width = 0;
15534 row->ends_at_zv_p = 1;
15535 break;
15536 }
15537
15538 /* Now, get the metrics of what we want to display. This also
15539 generates glyphs in `row' (which is IT->glyph_row). */
15540 n_glyphs_before = row->used[TEXT_AREA];
15541 x = it->current_x;
15542
15543 /* Remember the line height so far in case the next element doesn't
15544 fit on the line. */
15545 if (!it->truncate_lines_p)
15546 {
15547 ascent = it->max_ascent;
15548 descent = it->max_descent;
15549 phys_ascent = it->max_phys_ascent;
15550 phys_descent = it->max_phys_descent;
15551 }
15552
15553 PRODUCE_GLYPHS (it);
15554
15555 /* If this display element was in marginal areas, continue with
15556 the next one. */
15557 if (it->area != TEXT_AREA)
15558 {
15559 row->ascent = max (row->ascent, it->max_ascent);
15560 row->height = max (row->height, it->max_ascent + it->max_descent);
15561 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
15562 row->phys_height = max (row->phys_height,
15563 it->max_phys_ascent + it->max_phys_descent);
15564 row->extra_line_spacing = max (row->extra_line_spacing,
15565 it->max_extra_line_spacing);
15566 set_iterator_to_next (it, 1);
15567 continue;
15568 }
15569
15570 /* Does the display element fit on the line? If we truncate
15571 lines, we should draw past the right edge of the window. If
15572 we don't truncate, we want to stop so that we can display the
15573 continuation glyph before the right margin. If lines are
15574 continued, there are two possible strategies for characters
15575 resulting in more than 1 glyph (e.g. tabs): Display as many
15576 glyphs as possible in this line and leave the rest for the
15577 continuation line, or display the whole element in the next
15578 line. Original redisplay did the former, so we do it also. */
15579 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
15580 hpos_before = it->hpos;
15581 x_before = x;
15582
15583 if (/* Not a newline. */
15584 nglyphs > 0
15585 /* Glyphs produced fit entirely in the line. */
15586 && it->current_x < it->last_visible_x)
15587 {
15588 it->hpos += nglyphs;
15589 row->ascent = max (row->ascent, it->max_ascent);
15590 row->height = max (row->height, it->max_ascent + it->max_descent);
15591 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
15592 row->phys_height = max (row->phys_height,
15593 it->max_phys_ascent + it->max_phys_descent);
15594 row->extra_line_spacing = max (row->extra_line_spacing,
15595 it->max_extra_line_spacing);
15596 if (it->current_x - it->pixel_width < it->first_visible_x)
15597 row->x = x - it->first_visible_x;
15598 }
15599 else
15600 {
15601 int new_x;
15602 struct glyph *glyph;
15603
15604 for (i = 0; i < nglyphs; ++i, x = new_x)
15605 {
15606 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
15607 new_x = x + glyph->pixel_width;
15608
15609 if (/* Lines are continued. */
15610 !it->truncate_lines_p
15611 && (/* Glyph doesn't fit on the line. */
15612 new_x > it->last_visible_x
15613 /* Or it fits exactly on a window system frame. */
15614 || (new_x == it->last_visible_x
15615 && FRAME_WINDOW_P (it->f))))
15616 {
15617 /* End of a continued line. */
15618
15619 if (it->hpos == 0
15620 || (new_x == it->last_visible_x
15621 && FRAME_WINDOW_P (it->f)))
15622 {
15623 /* Current glyph is the only one on the line or
15624 fits exactly on the line. We must continue
15625 the line because we can't draw the cursor
15626 after the glyph. */
15627 row->continued_p = 1;
15628 it->current_x = new_x;
15629 it->continuation_lines_width += new_x;
15630 ++it->hpos;
15631 if (i == nglyphs - 1)
15632 {
15633 set_iterator_to_next (it, 1);
15634 #ifdef HAVE_WINDOW_SYSTEM
15635 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
15636 {
15637 if (!get_next_display_element (it))
15638 {
15639 row->exact_window_width_line_p = 1;
15640 it->continuation_lines_width = 0;
15641 row->continued_p = 0;
15642 row->ends_at_zv_p = 1;
15643 }
15644 else if (ITERATOR_AT_END_OF_LINE_P (it))
15645 {
15646 row->continued_p = 0;
15647 row->exact_window_width_line_p = 1;
15648 }
15649 }
15650 #endif /* HAVE_WINDOW_SYSTEM */
15651 }
15652 }
15653 else if (CHAR_GLYPH_PADDING_P (*glyph)
15654 && !FRAME_WINDOW_P (it->f))
15655 {
15656 /* A padding glyph that doesn't fit on this line.
15657 This means the whole character doesn't fit
15658 on the line. */
15659 row->used[TEXT_AREA] = n_glyphs_before;
15660
15661 /* Fill the rest of the row with continuation
15662 glyphs like in 20.x. */
15663 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
15664 < row->glyphs[1 + TEXT_AREA])
15665 produce_special_glyphs (it, IT_CONTINUATION);
15666
15667 row->continued_p = 1;
15668 it->current_x = x_before;
15669 it->continuation_lines_width += x_before;
15670
15671 /* Restore the height to what it was before the
15672 element not fitting on the line. */
15673 it->max_ascent = ascent;
15674 it->max_descent = descent;
15675 it->max_phys_ascent = phys_ascent;
15676 it->max_phys_descent = phys_descent;
15677 }
15678 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
15679 {
15680 /* A TAB that extends past the right edge of the
15681 window. This produces a single glyph on
15682 window system frames. We leave the glyph in
15683 this row and let it fill the row, but don't
15684 consume the TAB. */
15685 it->continuation_lines_width += it->last_visible_x;
15686 row->ends_in_middle_of_char_p = 1;
15687 row->continued_p = 1;
15688 glyph->pixel_width = it->last_visible_x - x;
15689 it->starts_in_middle_of_char_p = 1;
15690 }
15691 else
15692 {
15693 /* Something other than a TAB that draws past
15694 the right edge of the window. Restore
15695 positions to values before the element. */
15696 row->used[TEXT_AREA] = n_glyphs_before + i;
15697
15698 /* Display continuation glyphs. */
15699 if (!FRAME_WINDOW_P (it->f))
15700 produce_special_glyphs (it, IT_CONTINUATION);
15701 row->continued_p = 1;
15702
15703 it->current_x = x_before;
15704 it->continuation_lines_width += x;
15705 extend_face_to_end_of_line (it);
15706
15707 if (nglyphs > 1 && i > 0)
15708 {
15709 row->ends_in_middle_of_char_p = 1;
15710 it->starts_in_middle_of_char_p = 1;
15711 }
15712
15713 /* Restore the height to what it was before the
15714 element not fitting on the line. */
15715 it->max_ascent = ascent;
15716 it->max_descent = descent;
15717 it->max_phys_ascent = phys_ascent;
15718 it->max_phys_descent = phys_descent;
15719 }
15720
15721 break;
15722 }
15723 else if (new_x > it->first_visible_x)
15724 {
15725 /* Increment number of glyphs actually displayed. */
15726 ++it->hpos;
15727
15728 if (x < it->first_visible_x)
15729 /* Glyph is partially visible, i.e. row starts at
15730 negative X position. */
15731 row->x = x - it->first_visible_x;
15732 }
15733 else
15734 {
15735 /* Glyph is completely off the left margin of the
15736 window. This should not happen because of the
15737 move_it_in_display_line at the start of this
15738 function, unless the text display area of the
15739 window is empty. */
15740 xassert (it->first_visible_x <= it->last_visible_x);
15741 }
15742 }
15743
15744 row->ascent = max (row->ascent, it->max_ascent);
15745 row->height = max (row->height, it->max_ascent + it->max_descent);
15746 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
15747 row->phys_height = max (row->phys_height,
15748 it->max_phys_ascent + it->max_phys_descent);
15749 row->extra_line_spacing = max (row->extra_line_spacing,
15750 it->max_extra_line_spacing);
15751
15752 /* End of this display line if row is continued. */
15753 if (row->continued_p || row->ends_at_zv_p)
15754 break;
15755 }
15756
15757 at_end_of_line:
15758 /* Is this a line end? If yes, we're also done, after making
15759 sure that a non-default face is extended up to the right
15760 margin of the window. */
15761 if (ITERATOR_AT_END_OF_LINE_P (it))
15762 {
15763 int used_before = row->used[TEXT_AREA];
15764
15765 row->ends_in_newline_from_string_p = STRINGP (it->object);
15766
15767 #ifdef HAVE_WINDOW_SYSTEM
15768 /* Add a space at the end of the line that is used to
15769 display the cursor there. */
15770 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
15771 append_space_for_newline (it, 0);
15772 #endif /* HAVE_WINDOW_SYSTEM */
15773
15774 /* Extend the face to the end of the line. */
15775 extend_face_to_end_of_line (it);
15776
15777 /* Make sure we have the position. */
15778 if (used_before == 0)
15779 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
15780
15781 /* Consume the line end. This skips over invisible lines. */
15782 set_iterator_to_next (it, 1);
15783 it->continuation_lines_width = 0;
15784 break;
15785 }
15786
15787 /* Proceed with next display element. Note that this skips
15788 over lines invisible because of selective display. */
15789 set_iterator_to_next (it, 1);
15790
15791 /* If we truncate lines, we are done when the last displayed
15792 glyphs reach past the right margin of the window. */
15793 if (it->truncate_lines_p
15794 && (FRAME_WINDOW_P (it->f)
15795 ? (it->current_x >= it->last_visible_x)
15796 : (it->current_x > it->last_visible_x)))
15797 {
15798 /* Maybe add truncation glyphs. */
15799 if (!FRAME_WINDOW_P (it->f))
15800 {
15801 int i, n;
15802
15803 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
15804 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
15805 break;
15806
15807 for (n = row->used[TEXT_AREA]; i < n; ++i)
15808 {
15809 row->used[TEXT_AREA] = i;
15810 produce_special_glyphs (it, IT_TRUNCATION);
15811 }
15812 }
15813 #ifdef HAVE_WINDOW_SYSTEM
15814 else
15815 {
15816 /* Don't truncate if we can overflow newline into fringe. */
15817 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
15818 {
15819 if (!get_next_display_element (it))
15820 {
15821 it->continuation_lines_width = 0;
15822 row->ends_at_zv_p = 1;
15823 row->exact_window_width_line_p = 1;
15824 break;
15825 }
15826 if (ITERATOR_AT_END_OF_LINE_P (it))
15827 {
15828 row->exact_window_width_line_p = 1;
15829 goto at_end_of_line;
15830 }
15831 }
15832 }
15833 #endif /* HAVE_WINDOW_SYSTEM */
15834
15835 row->truncated_on_right_p = 1;
15836 it->continuation_lines_width = 0;
15837 reseat_at_next_visible_line_start (it, 0);
15838 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
15839 it->hpos = hpos_before;
15840 it->current_x = x_before;
15841 break;
15842 }
15843 }
15844
15845 /* If line is not empty and hscrolled, maybe insert truncation glyphs
15846 at the left window margin. */
15847 if (it->first_visible_x
15848 && IT_CHARPOS (*it) != MATRIX_ROW_START_CHARPOS (row))
15849 {
15850 if (!FRAME_WINDOW_P (it->f))
15851 insert_left_trunc_glyphs (it);
15852 row->truncated_on_left_p = 1;
15853 }
15854
15855 /* If the start of this line is the overlay arrow-position, then
15856 mark this glyph row as the one containing the overlay arrow.
15857 This is clearly a mess with variable size fonts. It would be
15858 better to let it be displayed like cursors under X. */
15859 if ((row->displays_text_p || !overlay_arrow_seen)
15860 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
15861 !NILP (overlay_arrow_string)))
15862 {
15863 /* Overlay arrow in window redisplay is a fringe bitmap. */
15864 if (STRINGP (overlay_arrow_string))
15865 {
15866 struct glyph_row *arrow_row
15867 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
15868 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
15869 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
15870 struct glyph *p = row->glyphs[TEXT_AREA];
15871 struct glyph *p2, *end;
15872
15873 /* Copy the arrow glyphs. */
15874 while (glyph < arrow_end)
15875 *p++ = *glyph++;
15876
15877 /* Throw away padding glyphs. */
15878 p2 = p;
15879 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
15880 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
15881 ++p2;
15882 if (p2 > p)
15883 {
15884 while (p2 < end)
15885 *p++ = *p2++;
15886 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
15887 }
15888 }
15889 else
15890 {
15891 xassert (INTEGERP (overlay_arrow_string));
15892 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
15893 }
15894 overlay_arrow_seen = 1;
15895 }
15896
15897 /* Compute pixel dimensions of this line. */
15898 compute_line_metrics (it);
15899
15900 /* Remember the position at which this line ends. */
15901 row->end = it->current;
15902
15903 /* Record whether this row ends inside an ellipsis. */
15904 row->ends_in_ellipsis_p
15905 = (it->method == GET_FROM_DISPLAY_VECTOR
15906 && it->ellipsis_p);
15907
15908 /* Save fringe bitmaps in this row. */
15909 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
15910 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
15911 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
15912 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
15913
15914 it->left_user_fringe_bitmap = 0;
15915 it->left_user_fringe_face_id = 0;
15916 it->right_user_fringe_bitmap = 0;
15917 it->right_user_fringe_face_id = 0;
15918
15919 /* Maybe set the cursor. */
15920 if (it->w->cursor.vpos < 0
15921 && PT >= MATRIX_ROW_START_CHARPOS (row)
15922 && PT <= MATRIX_ROW_END_CHARPOS (row)
15923 && cursor_row_p (it->w, row))
15924 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
15925
15926 /* Highlight trailing whitespace. */
15927 if (!NILP (Vshow_trailing_whitespace))
15928 highlight_trailing_whitespace (it->f, it->glyph_row);
15929
15930 /* Prepare for the next line. This line starts horizontally at (X
15931 HPOS) = (0 0). Vertical positions are incremented. As a
15932 convenience for the caller, IT->glyph_row is set to the next
15933 row to be used. */
15934 it->current_x = it->hpos = 0;
15935 it->current_y += row->height;
15936 ++it->vpos;
15937 ++it->glyph_row;
15938 it->start = it->current;
15939 return row->displays_text_p;
15940 }
15941
15942
15943 \f
15944 /***********************************************************************
15945 Menu Bar
15946 ***********************************************************************/
15947
15948 /* Redisplay the menu bar in the frame for window W.
15949
15950 The menu bar of X frames that don't have X toolkit support is
15951 displayed in a special window W->frame->menu_bar_window.
15952
15953 The menu bar of terminal frames is treated specially as far as
15954 glyph matrices are concerned. Menu bar lines are not part of
15955 windows, so the update is done directly on the frame matrix rows
15956 for the menu bar. */
15957
15958 static void
15959 display_menu_bar (w)
15960 struct window *w;
15961 {
15962 struct frame *f = XFRAME (WINDOW_FRAME (w));
15963 struct it it;
15964 Lisp_Object items;
15965 int i;
15966
15967 /* Don't do all this for graphical frames. */
15968 #ifdef HAVE_NTGUI
15969 if (!NILP (Vwindow_system))
15970 return;
15971 #endif
15972 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
15973 if (FRAME_X_P (f))
15974 return;
15975 #endif
15976 #ifdef MAC_OS
15977 if (FRAME_MAC_P (f))
15978 return;
15979 #endif
15980
15981 #ifdef USE_X_TOOLKIT
15982 xassert (!FRAME_WINDOW_P (f));
15983 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
15984 it.first_visible_x = 0;
15985 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
15986 #else /* not USE_X_TOOLKIT */
15987 if (FRAME_WINDOW_P (f))
15988 {
15989 /* Menu bar lines are displayed in the desired matrix of the
15990 dummy window menu_bar_window. */
15991 struct window *menu_w;
15992 xassert (WINDOWP (f->menu_bar_window));
15993 menu_w = XWINDOW (f->menu_bar_window);
15994 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
15995 MENU_FACE_ID);
15996 it.first_visible_x = 0;
15997 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
15998 }
15999 else
16000 {
16001 /* This is a TTY frame, i.e. character hpos/vpos are used as
16002 pixel x/y. */
16003 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
16004 MENU_FACE_ID);
16005 it.first_visible_x = 0;
16006 it.last_visible_x = FRAME_COLS (f);
16007 }
16008 #endif /* not USE_X_TOOLKIT */
16009
16010 if (! mode_line_inverse_video)
16011 /* Force the menu-bar to be displayed in the default face. */
16012 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
16013
16014 /* Clear all rows of the menu bar. */
16015 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
16016 {
16017 struct glyph_row *row = it.glyph_row + i;
16018 clear_glyph_row (row);
16019 row->enabled_p = 1;
16020 row->full_width_p = 1;
16021 }
16022
16023 /* Display all items of the menu bar. */
16024 items = FRAME_MENU_BAR_ITEMS (it.f);
16025 for (i = 0; i < XVECTOR (items)->size; i += 4)
16026 {
16027 Lisp_Object string;
16028
16029 /* Stop at nil string. */
16030 string = AREF (items, i + 1);
16031 if (NILP (string))
16032 break;
16033
16034 /* Remember where item was displayed. */
16035 AREF (items, i + 3) = make_number (it.hpos);
16036
16037 /* Display the item, pad with one space. */
16038 if (it.current_x < it.last_visible_x)
16039 display_string (NULL, string, Qnil, 0, 0, &it,
16040 SCHARS (string) + 1, 0, 0, -1);
16041 }
16042
16043 /* Fill out the line with spaces. */
16044 if (it.current_x < it.last_visible_x)
16045 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
16046
16047 /* Compute the total height of the lines. */
16048 compute_line_metrics (&it);
16049 }
16050
16051
16052 \f
16053 /***********************************************************************
16054 Mode Line
16055 ***********************************************************************/
16056
16057 /* Redisplay mode lines in the window tree whose root is WINDOW. If
16058 FORCE is non-zero, redisplay mode lines unconditionally.
16059 Otherwise, redisplay only mode lines that are garbaged. Value is
16060 the number of windows whose mode lines were redisplayed. */
16061
16062 static int
16063 redisplay_mode_lines (window, force)
16064 Lisp_Object window;
16065 int force;
16066 {
16067 int nwindows = 0;
16068
16069 while (!NILP (window))
16070 {
16071 struct window *w = XWINDOW (window);
16072
16073 if (WINDOWP (w->hchild))
16074 nwindows += redisplay_mode_lines (w->hchild, force);
16075 else if (WINDOWP (w->vchild))
16076 nwindows += redisplay_mode_lines (w->vchild, force);
16077 else if (force
16078 || FRAME_GARBAGED_P (XFRAME (w->frame))
16079 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
16080 {
16081 struct text_pos lpoint;
16082 struct buffer *old = current_buffer;
16083
16084 /* Set the window's buffer for the mode line display. */
16085 SET_TEXT_POS (lpoint, PT, PT_BYTE);
16086 set_buffer_internal_1 (XBUFFER (w->buffer));
16087
16088 /* Point refers normally to the selected window. For any
16089 other window, set up appropriate value. */
16090 if (!EQ (window, selected_window))
16091 {
16092 struct text_pos pt;
16093
16094 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
16095 if (CHARPOS (pt) < BEGV)
16096 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
16097 else if (CHARPOS (pt) > (ZV - 1))
16098 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
16099 else
16100 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
16101 }
16102
16103 /* Display mode lines. */
16104 clear_glyph_matrix (w->desired_matrix);
16105 if (display_mode_lines (w))
16106 {
16107 ++nwindows;
16108 w->must_be_updated_p = 1;
16109 }
16110
16111 /* Restore old settings. */
16112 set_buffer_internal_1 (old);
16113 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
16114 }
16115
16116 window = w->next;
16117 }
16118
16119 return nwindows;
16120 }
16121
16122
16123 /* Display the mode and/or top line of window W. Value is the number
16124 of mode lines displayed. */
16125
16126 static int
16127 display_mode_lines (w)
16128 struct window *w;
16129 {
16130 Lisp_Object old_selected_window, old_selected_frame;
16131 int n = 0;
16132
16133 old_selected_frame = selected_frame;
16134 selected_frame = w->frame;
16135 old_selected_window = selected_window;
16136 XSETWINDOW (selected_window, w);
16137
16138 /* These will be set while the mode line specs are processed. */
16139 line_number_displayed = 0;
16140 w->column_number_displayed = Qnil;
16141
16142 if (WINDOW_WANTS_MODELINE_P (w))
16143 {
16144 struct window *sel_w = XWINDOW (old_selected_window);
16145
16146 /* Select mode line face based on the real selected window. */
16147 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
16148 current_buffer->mode_line_format);
16149 ++n;
16150 }
16151
16152 if (WINDOW_WANTS_HEADER_LINE_P (w))
16153 {
16154 display_mode_line (w, HEADER_LINE_FACE_ID,
16155 current_buffer->header_line_format);
16156 ++n;
16157 }
16158
16159 selected_frame = old_selected_frame;
16160 selected_window = old_selected_window;
16161 return n;
16162 }
16163
16164
16165 /* Display mode or top line of window W. FACE_ID specifies which line
16166 to display; it is either MODE_LINE_FACE_ID or HEADER_LINE_FACE_ID.
16167 FORMAT is the mode line format to display. Value is the pixel
16168 height of the mode line displayed. */
16169
16170 static int
16171 display_mode_line (w, face_id, format)
16172 struct window *w;
16173 enum face_id face_id;
16174 Lisp_Object format;
16175 {
16176 struct it it;
16177 struct face *face;
16178 int count = SPECPDL_INDEX ();
16179
16180 init_iterator (&it, w, -1, -1, NULL, face_id);
16181 prepare_desired_row (it.glyph_row);
16182
16183 it.glyph_row->mode_line_p = 1;
16184
16185 if (! mode_line_inverse_video)
16186 /* Force the mode-line to be displayed in the default face. */
16187 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
16188
16189 record_unwind_protect (unwind_format_mode_line,
16190 format_mode_line_unwind_data (NULL, 0));
16191
16192 mode_line_target = MODE_LINE_DISPLAY;
16193
16194 /* Temporarily make frame's keyboard the current kboard so that
16195 kboard-local variables in the mode_line_format will get the right
16196 values. */
16197 push_kboard (FRAME_KBOARD (it.f));
16198 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
16199 pop_kboard ();
16200
16201 unbind_to (count, Qnil);
16202
16203 /* Fill up with spaces. */
16204 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
16205
16206 compute_line_metrics (&it);
16207 it.glyph_row->full_width_p = 1;
16208 it.glyph_row->continued_p = 0;
16209 it.glyph_row->truncated_on_left_p = 0;
16210 it.glyph_row->truncated_on_right_p = 0;
16211
16212 /* Make a 3D mode-line have a shadow at its right end. */
16213 face = FACE_FROM_ID (it.f, face_id);
16214 extend_face_to_end_of_line (&it);
16215 if (face->box != FACE_NO_BOX)
16216 {
16217 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
16218 + it.glyph_row->used[TEXT_AREA] - 1);
16219 last->right_box_line_p = 1;
16220 }
16221
16222 return it.glyph_row->height;
16223 }
16224
16225 /* Move element ELT in LIST to the front of LIST.
16226 Return the updated list. */
16227
16228 static Lisp_Object
16229 move_elt_to_front (elt, list)
16230 Lisp_Object elt, list;
16231 {
16232 register Lisp_Object tail, prev;
16233 register Lisp_Object tem;
16234
16235 tail = list;
16236 prev = Qnil;
16237 while (CONSP (tail))
16238 {
16239 tem = XCAR (tail);
16240
16241 if (EQ (elt, tem))
16242 {
16243 /* Splice out the link TAIL. */
16244 if (NILP (prev))
16245 list = XCDR (tail);
16246 else
16247 Fsetcdr (prev, XCDR (tail));
16248
16249 /* Now make it the first. */
16250 Fsetcdr (tail, list);
16251 return tail;
16252 }
16253 else
16254 prev = tail;
16255 tail = XCDR (tail);
16256 QUIT;
16257 }
16258
16259 /* Not found--return unchanged LIST. */
16260 return list;
16261 }
16262
16263 /* Contribute ELT to the mode line for window IT->w. How it
16264 translates into text depends on its data type.
16265
16266 IT describes the display environment in which we display, as usual.
16267
16268 DEPTH is the depth in recursion. It is used to prevent
16269 infinite recursion here.
16270
16271 FIELD_WIDTH is the number of characters the display of ELT should
16272 occupy in the mode line, and PRECISION is the maximum number of
16273 characters to display from ELT's representation. See
16274 display_string for details.
16275
16276 Returns the hpos of the end of the text generated by ELT.
16277
16278 PROPS is a property list to add to any string we encounter.
16279
16280 If RISKY is nonzero, remove (disregard) any properties in any string
16281 we encounter, and ignore :eval and :propertize.
16282
16283 The global variable `mode_line_target' determines whether the
16284 output is passed to `store_mode_line_noprop',
16285 `store_mode_line_string', or `display_string'. */
16286
16287 static int
16288 display_mode_element (it, depth, field_width, precision, elt, props, risky)
16289 struct it *it;
16290 int depth;
16291 int field_width, precision;
16292 Lisp_Object elt, props;
16293 int risky;
16294 {
16295 int n = 0, field, prec;
16296 int literal = 0;
16297
16298 tail_recurse:
16299 if (depth > 100)
16300 elt = build_string ("*too-deep*");
16301
16302 depth++;
16303
16304 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
16305 {
16306 case Lisp_String:
16307 {
16308 /* A string: output it and check for %-constructs within it. */
16309 unsigned char c;
16310 int offset = 0;
16311
16312 if (SCHARS (elt) > 0
16313 && (!NILP (props) || risky))
16314 {
16315 Lisp_Object oprops, aelt;
16316 oprops = Ftext_properties_at (make_number (0), elt);
16317
16318 /* If the starting string's properties are not what
16319 we want, translate the string. Also, if the string
16320 is risky, do that anyway. */
16321
16322 if (NILP (Fequal (props, oprops)) || risky)
16323 {
16324 /* If the starting string has properties,
16325 merge the specified ones onto the existing ones. */
16326 if (! NILP (oprops) && !risky)
16327 {
16328 Lisp_Object tem;
16329
16330 oprops = Fcopy_sequence (oprops);
16331 tem = props;
16332 while (CONSP (tem))
16333 {
16334 oprops = Fplist_put (oprops, XCAR (tem),
16335 XCAR (XCDR (tem)));
16336 tem = XCDR (XCDR (tem));
16337 }
16338 props = oprops;
16339 }
16340
16341 aelt = Fassoc (elt, mode_line_proptrans_alist);
16342 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
16343 {
16344 /* AELT is what we want. Move it to the front
16345 without consing. */
16346 elt = XCAR (aelt);
16347 mode_line_proptrans_alist
16348 = move_elt_to_front (aelt, mode_line_proptrans_alist);
16349 }
16350 else
16351 {
16352 Lisp_Object tem;
16353
16354 /* If AELT has the wrong props, it is useless.
16355 so get rid of it. */
16356 if (! NILP (aelt))
16357 mode_line_proptrans_alist
16358 = Fdelq (aelt, mode_line_proptrans_alist);
16359
16360 elt = Fcopy_sequence (elt);
16361 Fset_text_properties (make_number (0), Flength (elt),
16362 props, elt);
16363 /* Add this item to mode_line_proptrans_alist. */
16364 mode_line_proptrans_alist
16365 = Fcons (Fcons (elt, props),
16366 mode_line_proptrans_alist);
16367 /* Truncate mode_line_proptrans_alist
16368 to at most 50 elements. */
16369 tem = Fnthcdr (make_number (50),
16370 mode_line_proptrans_alist);
16371 if (! NILP (tem))
16372 XSETCDR (tem, Qnil);
16373 }
16374 }
16375 }
16376
16377 offset = 0;
16378
16379 if (literal)
16380 {
16381 prec = precision - n;
16382 switch (mode_line_target)
16383 {
16384 case MODE_LINE_NOPROP:
16385 case MODE_LINE_TITLE:
16386 n += store_mode_line_noprop (SDATA (elt), -1, prec);
16387 break;
16388 case MODE_LINE_STRING:
16389 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
16390 break;
16391 case MODE_LINE_DISPLAY:
16392 n += display_string (NULL, elt, Qnil, 0, 0, it,
16393 0, prec, 0, STRING_MULTIBYTE (elt));
16394 break;
16395 }
16396
16397 break;
16398 }
16399
16400 /* Handle the non-literal case. */
16401
16402 while ((precision <= 0 || n < precision)
16403 && SREF (elt, offset) != 0
16404 && (mode_line_target != MODE_LINE_DISPLAY
16405 || it->current_x < it->last_visible_x))
16406 {
16407 int last_offset = offset;
16408
16409 /* Advance to end of string or next format specifier. */
16410 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
16411 ;
16412
16413 if (offset - 1 != last_offset)
16414 {
16415 int nchars, nbytes;
16416
16417 /* Output to end of string or up to '%'. Field width
16418 is length of string. Don't output more than
16419 PRECISION allows us. */
16420 offset--;
16421
16422 prec = c_string_width (SDATA (elt) + last_offset,
16423 offset - last_offset, precision - n,
16424 &nchars, &nbytes);
16425
16426 switch (mode_line_target)
16427 {
16428 case MODE_LINE_NOPROP:
16429 case MODE_LINE_TITLE:
16430 n += store_mode_line_noprop (SDATA (elt) + last_offset, 0, prec);
16431 break;
16432 case MODE_LINE_STRING:
16433 {
16434 int bytepos = last_offset;
16435 int charpos = string_byte_to_char (elt, bytepos);
16436 int endpos = (precision <= 0
16437 ? string_byte_to_char (elt, offset)
16438 : charpos + nchars);
16439
16440 n += store_mode_line_string (NULL,
16441 Fsubstring (elt, make_number (charpos),
16442 make_number (endpos)),
16443 0, 0, 0, Qnil);
16444 }
16445 break;
16446 case MODE_LINE_DISPLAY:
16447 {
16448 int bytepos = last_offset;
16449 int charpos = string_byte_to_char (elt, bytepos);
16450 n += display_string (NULL, elt, Qnil, 0, charpos,
16451 it, 0, prec, 0,
16452 STRING_MULTIBYTE (elt));
16453 }
16454 break;
16455 }
16456 }
16457 else /* c == '%' */
16458 {
16459 int percent_position = offset;
16460
16461 /* Get the specified minimum width. Zero means
16462 don't pad. */
16463 field = 0;
16464 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
16465 field = field * 10 + c - '0';
16466
16467 /* Don't pad beyond the total padding allowed. */
16468 if (field_width - n > 0 && field > field_width - n)
16469 field = field_width - n;
16470
16471 /* Note that either PRECISION <= 0 or N < PRECISION. */
16472 prec = precision - n;
16473
16474 if (c == 'M')
16475 n += display_mode_element (it, depth, field, prec,
16476 Vglobal_mode_string, props,
16477 risky);
16478 else if (c != 0)
16479 {
16480 int multibyte;
16481 int bytepos, charpos;
16482 unsigned char *spec;
16483
16484 bytepos = percent_position;
16485 charpos = (STRING_MULTIBYTE (elt)
16486 ? string_byte_to_char (elt, bytepos)
16487 : bytepos);
16488
16489 spec
16490 = decode_mode_spec (it->w, c, field, prec, &multibyte);
16491
16492 switch (mode_line_target)
16493 {
16494 case MODE_LINE_NOPROP:
16495 case MODE_LINE_TITLE:
16496 n += store_mode_line_noprop (spec, field, prec);
16497 break;
16498 case MODE_LINE_STRING:
16499 {
16500 int len = strlen (spec);
16501 Lisp_Object tem = make_string (spec, len);
16502 props = Ftext_properties_at (make_number (charpos), elt);
16503 /* Should only keep face property in props */
16504 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
16505 }
16506 break;
16507 case MODE_LINE_DISPLAY:
16508 {
16509 int nglyphs_before, nwritten;
16510
16511 nglyphs_before = it->glyph_row->used[TEXT_AREA];
16512 nwritten = display_string (spec, Qnil, elt,
16513 charpos, 0, it,
16514 field, prec, 0,
16515 multibyte);
16516
16517 /* Assign to the glyphs written above the
16518 string where the `%x' came from, position
16519 of the `%'. */
16520 if (nwritten > 0)
16521 {
16522 struct glyph *glyph
16523 = (it->glyph_row->glyphs[TEXT_AREA]
16524 + nglyphs_before);
16525 int i;
16526
16527 for (i = 0; i < nwritten; ++i)
16528 {
16529 glyph[i].object = elt;
16530 glyph[i].charpos = charpos;
16531 }
16532
16533 n += nwritten;
16534 }
16535 }
16536 break;
16537 }
16538 }
16539 else /* c == 0 */
16540 break;
16541 }
16542 }
16543 }
16544 break;
16545
16546 case Lisp_Symbol:
16547 /* A symbol: process the value of the symbol recursively
16548 as if it appeared here directly. Avoid error if symbol void.
16549 Special case: if value of symbol is a string, output the string
16550 literally. */
16551 {
16552 register Lisp_Object tem;
16553
16554 /* If the variable is not marked as risky to set
16555 then its contents are risky to use. */
16556 if (NILP (Fget (elt, Qrisky_local_variable)))
16557 risky = 1;
16558
16559 tem = Fboundp (elt);
16560 if (!NILP (tem))
16561 {
16562 tem = Fsymbol_value (elt);
16563 /* If value is a string, output that string literally:
16564 don't check for % within it. */
16565 if (STRINGP (tem))
16566 literal = 1;
16567
16568 if (!EQ (tem, elt))
16569 {
16570 /* Give up right away for nil or t. */
16571 elt = tem;
16572 goto tail_recurse;
16573 }
16574 }
16575 }
16576 break;
16577
16578 case Lisp_Cons:
16579 {
16580 register Lisp_Object car, tem;
16581
16582 /* A cons cell: five distinct cases.
16583 If first element is :eval or :propertize, do something special.
16584 If first element is a string or a cons, process all the elements
16585 and effectively concatenate them.
16586 If first element is a negative number, truncate displaying cdr to
16587 at most that many characters. If positive, pad (with spaces)
16588 to at least that many characters.
16589 If first element is a symbol, process the cadr or caddr recursively
16590 according to whether the symbol's value is non-nil or nil. */
16591 car = XCAR (elt);
16592 if (EQ (car, QCeval))
16593 {
16594 /* An element of the form (:eval FORM) means evaluate FORM
16595 and use the result as mode line elements. */
16596
16597 if (risky)
16598 break;
16599
16600 if (CONSP (XCDR (elt)))
16601 {
16602 Lisp_Object spec;
16603 spec = safe_eval (XCAR (XCDR (elt)));
16604 n += display_mode_element (it, depth, field_width - n,
16605 precision - n, spec, props,
16606 risky);
16607 }
16608 }
16609 else if (EQ (car, QCpropertize))
16610 {
16611 /* An element of the form (:propertize ELT PROPS...)
16612 means display ELT but applying properties PROPS. */
16613
16614 if (risky)
16615 break;
16616
16617 if (CONSP (XCDR (elt)))
16618 n += display_mode_element (it, depth, field_width - n,
16619 precision - n, XCAR (XCDR (elt)),
16620 XCDR (XCDR (elt)), risky);
16621 }
16622 else if (SYMBOLP (car))
16623 {
16624 tem = Fboundp (car);
16625 elt = XCDR (elt);
16626 if (!CONSP (elt))
16627 goto invalid;
16628 /* elt is now the cdr, and we know it is a cons cell.
16629 Use its car if CAR has a non-nil value. */
16630 if (!NILP (tem))
16631 {
16632 tem = Fsymbol_value (car);
16633 if (!NILP (tem))
16634 {
16635 elt = XCAR (elt);
16636 goto tail_recurse;
16637 }
16638 }
16639 /* Symbol's value is nil (or symbol is unbound)
16640 Get the cddr of the original list
16641 and if possible find the caddr and use that. */
16642 elt = XCDR (elt);
16643 if (NILP (elt))
16644 break;
16645 else if (!CONSP (elt))
16646 goto invalid;
16647 elt = XCAR (elt);
16648 goto tail_recurse;
16649 }
16650 else if (INTEGERP (car))
16651 {
16652 register int lim = XINT (car);
16653 elt = XCDR (elt);
16654 if (lim < 0)
16655 {
16656 /* Negative int means reduce maximum width. */
16657 if (precision <= 0)
16658 precision = -lim;
16659 else
16660 precision = min (precision, -lim);
16661 }
16662 else if (lim > 0)
16663 {
16664 /* Padding specified. Don't let it be more than
16665 current maximum. */
16666 if (precision > 0)
16667 lim = min (precision, lim);
16668
16669 /* If that's more padding than already wanted, queue it.
16670 But don't reduce padding already specified even if
16671 that is beyond the current truncation point. */
16672 field_width = max (lim, field_width);
16673 }
16674 goto tail_recurse;
16675 }
16676 else if (STRINGP (car) || CONSP (car))
16677 {
16678 register int limit = 50;
16679 /* Limit is to protect against circular lists. */
16680 while (CONSP (elt)
16681 && --limit > 0
16682 && (precision <= 0 || n < precision))
16683 {
16684 n += display_mode_element (it, depth,
16685 /* Do padding only after the last
16686 element in the list. */
16687 (! CONSP (XCDR (elt))
16688 ? field_width - n
16689 : 0),
16690 precision - n, XCAR (elt),
16691 props, risky);
16692 elt = XCDR (elt);
16693 }
16694 }
16695 }
16696 break;
16697
16698 default:
16699 invalid:
16700 elt = build_string ("*invalid*");
16701 goto tail_recurse;
16702 }
16703
16704 /* Pad to FIELD_WIDTH. */
16705 if (field_width > 0 && n < field_width)
16706 {
16707 switch (mode_line_target)
16708 {
16709 case MODE_LINE_NOPROP:
16710 case MODE_LINE_TITLE:
16711 n += store_mode_line_noprop ("", field_width - n, 0);
16712 break;
16713 case MODE_LINE_STRING:
16714 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
16715 break;
16716 case MODE_LINE_DISPLAY:
16717 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
16718 0, 0, 0);
16719 break;
16720 }
16721 }
16722
16723 return n;
16724 }
16725
16726 /* Store a mode-line string element in mode_line_string_list.
16727
16728 If STRING is non-null, display that C string. Otherwise, the Lisp
16729 string LISP_STRING is displayed.
16730
16731 FIELD_WIDTH is the minimum number of output glyphs to produce.
16732 If STRING has fewer characters than FIELD_WIDTH, pad to the right
16733 with spaces. FIELD_WIDTH <= 0 means don't pad.
16734
16735 PRECISION is the maximum number of characters to output from
16736 STRING. PRECISION <= 0 means don't truncate the string.
16737
16738 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
16739 properties to the string.
16740
16741 PROPS are the properties to add to the string.
16742 The mode_line_string_face face property is always added to the string.
16743 */
16744
16745 static int
16746 store_mode_line_string (string, lisp_string, copy_string, field_width, precision, props)
16747 char *string;
16748 Lisp_Object lisp_string;
16749 int copy_string;
16750 int field_width;
16751 int precision;
16752 Lisp_Object props;
16753 {
16754 int len;
16755 int n = 0;
16756
16757 if (string != NULL)
16758 {
16759 len = strlen (string);
16760 if (precision > 0 && len > precision)
16761 len = precision;
16762 lisp_string = make_string (string, len);
16763 if (NILP (props))
16764 props = mode_line_string_face_prop;
16765 else if (!NILP (mode_line_string_face))
16766 {
16767 Lisp_Object face = Fplist_get (props, Qface);
16768 props = Fcopy_sequence (props);
16769 if (NILP (face))
16770 face = mode_line_string_face;
16771 else
16772 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
16773 props = Fplist_put (props, Qface, face);
16774 }
16775 Fadd_text_properties (make_number (0), make_number (len),
16776 props, lisp_string);
16777 }
16778 else
16779 {
16780 len = XFASTINT (Flength (lisp_string));
16781 if (precision > 0 && len > precision)
16782 {
16783 len = precision;
16784 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
16785 precision = -1;
16786 }
16787 if (!NILP (mode_line_string_face))
16788 {
16789 Lisp_Object face;
16790 if (NILP (props))
16791 props = Ftext_properties_at (make_number (0), lisp_string);
16792 face = Fplist_get (props, Qface);
16793 if (NILP (face))
16794 face = mode_line_string_face;
16795 else
16796 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
16797 props = Fcons (Qface, Fcons (face, Qnil));
16798 if (copy_string)
16799 lisp_string = Fcopy_sequence (lisp_string);
16800 }
16801 if (!NILP (props))
16802 Fadd_text_properties (make_number (0), make_number (len),
16803 props, lisp_string);
16804 }
16805
16806 if (len > 0)
16807 {
16808 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
16809 n += len;
16810 }
16811
16812 if (field_width > len)
16813 {
16814 field_width -= len;
16815 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
16816 if (!NILP (props))
16817 Fadd_text_properties (make_number (0), make_number (field_width),
16818 props, lisp_string);
16819 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
16820 n += field_width;
16821 }
16822
16823 return n;
16824 }
16825
16826
16827 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
16828 1, 4, 0,
16829 doc: /* Format a string out of a mode line format specification.
16830 First arg FORMAT specifies the mode line format (see `mode-line-format'
16831 for details) to use.
16832
16833 Optional second arg FACE specifies the face property to put
16834 on all characters for which no face is specified.
16835 t means whatever face the window's mode line currently uses
16836 \(either `mode-line' or `mode-line-inactive', depending).
16837 nil means the default is no face property.
16838 If FACE is an integer, the value string has no text properties.
16839
16840 Optional third and fourth args WINDOW and BUFFER specify the window
16841 and buffer to use as the context for the formatting (defaults
16842 are the selected window and the window's buffer). */)
16843 (format, face, window, buffer)
16844 Lisp_Object format, face, window, buffer;
16845 {
16846 struct it it;
16847 int len;
16848 struct window *w;
16849 struct buffer *old_buffer = NULL;
16850 int face_id = -1;
16851 int no_props = INTEGERP (face);
16852 int count = SPECPDL_INDEX ();
16853 Lisp_Object str;
16854 int string_start = 0;
16855
16856 if (NILP (window))
16857 window = selected_window;
16858 CHECK_WINDOW (window);
16859 w = XWINDOW (window);
16860
16861 if (NILP (buffer))
16862 buffer = w->buffer;
16863 CHECK_BUFFER (buffer);
16864
16865 if (NILP (format))
16866 return build_string ("");
16867
16868 if (no_props)
16869 face = Qnil;
16870
16871 if (!NILP (face))
16872 {
16873 if (EQ (face, Qt))
16874 face = (EQ (window, selected_window) ? Qmode_line : Qmode_line_inactive);
16875 face_id = lookup_named_face (XFRAME (WINDOW_FRAME (w)), face, 0, 0);
16876 }
16877
16878 if (face_id < 0)
16879 face_id = DEFAULT_FACE_ID;
16880
16881 if (XBUFFER (buffer) != current_buffer)
16882 old_buffer = current_buffer;
16883
16884 /* Save things including mode_line_proptrans_alist,
16885 and set that to nil so that we don't alter the outer value. */
16886 record_unwind_protect (unwind_format_mode_line,
16887 format_mode_line_unwind_data (old_buffer, 1));
16888 mode_line_proptrans_alist = Qnil;
16889
16890 if (old_buffer)
16891 set_buffer_internal_1 (XBUFFER (buffer));
16892
16893 init_iterator (&it, w, -1, -1, NULL, face_id);
16894
16895 if (no_props)
16896 {
16897 mode_line_target = MODE_LINE_NOPROP;
16898 mode_line_string_face_prop = Qnil;
16899 mode_line_string_list = Qnil;
16900 string_start = MODE_LINE_NOPROP_LEN (0);
16901 }
16902 else
16903 {
16904 mode_line_target = MODE_LINE_STRING;
16905 mode_line_string_list = Qnil;
16906 mode_line_string_face = face;
16907 mode_line_string_face_prop
16908 = (NILP (face) ? Qnil : Fcons (Qface, Fcons (face, Qnil)));
16909 }
16910
16911 push_kboard (FRAME_KBOARD (it.f));
16912 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
16913 pop_kboard ();
16914
16915 if (no_props)
16916 {
16917 len = MODE_LINE_NOPROP_LEN (string_start);
16918 str = make_string (mode_line_noprop_buf + string_start, len);
16919 }
16920 else
16921 {
16922 mode_line_string_list = Fnreverse (mode_line_string_list);
16923 str = Fmapconcat (intern ("identity"), mode_line_string_list,
16924 make_string ("", 0));
16925 }
16926
16927 unbind_to (count, Qnil);
16928 return str;
16929 }
16930
16931 /* Write a null-terminated, right justified decimal representation of
16932 the positive integer D to BUF using a minimal field width WIDTH. */
16933
16934 static void
16935 pint2str (buf, width, d)
16936 register char *buf;
16937 register int width;
16938 register int d;
16939 {
16940 register char *p = buf;
16941
16942 if (d <= 0)
16943 *p++ = '0';
16944 else
16945 {
16946 while (d > 0)
16947 {
16948 *p++ = d % 10 + '0';
16949 d /= 10;
16950 }
16951 }
16952
16953 for (width -= (int) (p - buf); width > 0; --width)
16954 *p++ = ' ';
16955 *p-- = '\0';
16956 while (p > buf)
16957 {
16958 d = *buf;
16959 *buf++ = *p;
16960 *p-- = d;
16961 }
16962 }
16963
16964 /* Write a null-terminated, right justified decimal and "human
16965 readable" representation of the nonnegative integer D to BUF using
16966 a minimal field width WIDTH. D should be smaller than 999.5e24. */
16967
16968 static const char power_letter[] =
16969 {
16970 0, /* not used */
16971 'k', /* kilo */
16972 'M', /* mega */
16973 'G', /* giga */
16974 'T', /* tera */
16975 'P', /* peta */
16976 'E', /* exa */
16977 'Z', /* zetta */
16978 'Y' /* yotta */
16979 };
16980
16981 static void
16982 pint2hrstr (buf, width, d)
16983 char *buf;
16984 int width;
16985 int d;
16986 {
16987 /* We aim to represent the nonnegative integer D as
16988 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
16989 int quotient = d;
16990 int remainder = 0;
16991 /* -1 means: do not use TENTHS. */
16992 int tenths = -1;
16993 int exponent = 0;
16994
16995 /* Length of QUOTIENT.TENTHS as a string. */
16996 int length;
16997
16998 char * psuffix;
16999 char * p;
17000
17001 if (1000 <= quotient)
17002 {
17003 /* Scale to the appropriate EXPONENT. */
17004 do
17005 {
17006 remainder = quotient % 1000;
17007 quotient /= 1000;
17008 exponent++;
17009 }
17010 while (1000 <= quotient);
17011
17012 /* Round to nearest and decide whether to use TENTHS or not. */
17013 if (quotient <= 9)
17014 {
17015 tenths = remainder / 100;
17016 if (50 <= remainder % 100)
17017 {
17018 if (tenths < 9)
17019 tenths++;
17020 else
17021 {
17022 quotient++;
17023 if (quotient == 10)
17024 tenths = -1;
17025 else
17026 tenths = 0;
17027 }
17028 }
17029 }
17030 else
17031 if (500 <= remainder)
17032 {
17033 if (quotient < 999)
17034 quotient++;
17035 else
17036 {
17037 quotient = 1;
17038 exponent++;
17039 tenths = 0;
17040 }
17041 }
17042 }
17043
17044 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
17045 if (tenths == -1 && quotient <= 99)
17046 if (quotient <= 9)
17047 length = 1;
17048 else
17049 length = 2;
17050 else
17051 length = 3;
17052 p = psuffix = buf + max (width, length);
17053
17054 /* Print EXPONENT. */
17055 if (exponent)
17056 *psuffix++ = power_letter[exponent];
17057 *psuffix = '\0';
17058
17059 /* Print TENTHS. */
17060 if (tenths >= 0)
17061 {
17062 *--p = '0' + tenths;
17063 *--p = '.';
17064 }
17065
17066 /* Print QUOTIENT. */
17067 do
17068 {
17069 int digit = quotient % 10;
17070 *--p = '0' + digit;
17071 }
17072 while ((quotient /= 10) != 0);
17073
17074 /* Print leading spaces. */
17075 while (buf < p)
17076 *--p = ' ';
17077 }
17078
17079 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
17080 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
17081 type of CODING_SYSTEM. Return updated pointer into BUF. */
17082
17083 static unsigned char invalid_eol_type[] = "(*invalid*)";
17084
17085 static char *
17086 decode_mode_spec_coding (coding_system, buf, eol_flag)
17087 Lisp_Object coding_system;
17088 register char *buf;
17089 int eol_flag;
17090 {
17091 Lisp_Object val;
17092 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
17093 const unsigned char *eol_str;
17094 int eol_str_len;
17095 /* The EOL conversion we are using. */
17096 Lisp_Object eoltype;
17097
17098 val = Fget (coding_system, Qcoding_system);
17099 eoltype = Qnil;
17100
17101 if (!VECTORP (val)) /* Not yet decided. */
17102 {
17103 if (multibyte)
17104 *buf++ = '-';
17105 if (eol_flag)
17106 eoltype = eol_mnemonic_undecided;
17107 /* Don't mention EOL conversion if it isn't decided. */
17108 }
17109 else
17110 {
17111 Lisp_Object eolvalue;
17112
17113 eolvalue = Fget (coding_system, Qeol_type);
17114
17115 if (multibyte)
17116 *buf++ = XFASTINT (AREF (val, 1));
17117
17118 if (eol_flag)
17119 {
17120 /* The EOL conversion that is normal on this system. */
17121
17122 if (NILP (eolvalue)) /* Not yet decided. */
17123 eoltype = eol_mnemonic_undecided;
17124 else if (VECTORP (eolvalue)) /* Not yet decided. */
17125 eoltype = eol_mnemonic_undecided;
17126 else /* INTEGERP (eolvalue) -- 0:LF, 1:CRLF, 2:CR */
17127 eoltype = (XFASTINT (eolvalue) == 0
17128 ? eol_mnemonic_unix
17129 : (XFASTINT (eolvalue) == 1
17130 ? eol_mnemonic_dos : eol_mnemonic_mac));
17131 }
17132 }
17133
17134 if (eol_flag)
17135 {
17136 /* Mention the EOL conversion if it is not the usual one. */
17137 if (STRINGP (eoltype))
17138 {
17139 eol_str = SDATA (eoltype);
17140 eol_str_len = SBYTES (eoltype);
17141 }
17142 else if (INTEGERP (eoltype)
17143 && CHAR_VALID_P (XINT (eoltype), 0))
17144 {
17145 unsigned char *tmp = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
17146 eol_str_len = CHAR_STRING (XINT (eoltype), tmp);
17147 eol_str = tmp;
17148 }
17149 else
17150 {
17151 eol_str = invalid_eol_type;
17152 eol_str_len = sizeof (invalid_eol_type) - 1;
17153 }
17154 bcopy (eol_str, buf, eol_str_len);
17155 buf += eol_str_len;
17156 }
17157
17158 return buf;
17159 }
17160
17161 /* Return a string for the output of a mode line %-spec for window W,
17162 generated by character C. PRECISION >= 0 means don't return a
17163 string longer than that value. FIELD_WIDTH > 0 means pad the
17164 string returned with spaces to that value. Return 1 in *MULTIBYTE
17165 if the result is multibyte text.
17166
17167 Note we operate on the current buffer for most purposes,
17168 the exception being w->base_line_pos. */
17169
17170 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
17171
17172 static char *
17173 decode_mode_spec (w, c, field_width, precision, multibyte)
17174 struct window *w;
17175 register int c;
17176 int field_width, precision;
17177 int *multibyte;
17178 {
17179 Lisp_Object obj;
17180 struct frame *f = XFRAME (WINDOW_FRAME (w));
17181 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
17182 struct buffer *b = current_buffer;
17183
17184 obj = Qnil;
17185 *multibyte = 0;
17186
17187 switch (c)
17188 {
17189 case '*':
17190 if (!NILP (b->read_only))
17191 return "%";
17192 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
17193 return "*";
17194 return "-";
17195
17196 case '+':
17197 /* This differs from %* only for a modified read-only buffer. */
17198 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
17199 return "*";
17200 if (!NILP (b->read_only))
17201 return "%";
17202 return "-";
17203
17204 case '&':
17205 /* This differs from %* in ignoring read-only-ness. */
17206 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
17207 return "*";
17208 return "-";
17209
17210 case '%':
17211 return "%";
17212
17213 case '[':
17214 {
17215 int i;
17216 char *p;
17217
17218 if (command_loop_level > 5)
17219 return "[[[... ";
17220 p = decode_mode_spec_buf;
17221 for (i = 0; i < command_loop_level; i++)
17222 *p++ = '[';
17223 *p = 0;
17224 return decode_mode_spec_buf;
17225 }
17226
17227 case ']':
17228 {
17229 int i;
17230 char *p;
17231
17232 if (command_loop_level > 5)
17233 return " ...]]]";
17234 p = decode_mode_spec_buf;
17235 for (i = 0; i < command_loop_level; i++)
17236 *p++ = ']';
17237 *p = 0;
17238 return decode_mode_spec_buf;
17239 }
17240
17241 case '-':
17242 {
17243 register int i;
17244
17245 /* Let lots_of_dashes be a string of infinite length. */
17246 if (mode_line_target == MODE_LINE_NOPROP ||
17247 mode_line_target == MODE_LINE_STRING)
17248 return "--";
17249 if (field_width <= 0
17250 || field_width > sizeof (lots_of_dashes))
17251 {
17252 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
17253 decode_mode_spec_buf[i] = '-';
17254 decode_mode_spec_buf[i] = '\0';
17255 return decode_mode_spec_buf;
17256 }
17257 else
17258 return lots_of_dashes;
17259 }
17260
17261 case 'b':
17262 obj = b->name;
17263 break;
17264
17265 case 'c':
17266 {
17267 int col = (int) current_column (); /* iftc */
17268 w->column_number_displayed = make_number (col);
17269 pint2str (decode_mode_spec_buf, field_width, col);
17270 return decode_mode_spec_buf;
17271 }
17272
17273 case 'e':
17274 #ifndef SYSTEM_MALLOC
17275 {
17276 if (NILP (Vmemory_full))
17277 return "";
17278 else
17279 return "!MEM FULL! ";
17280 }
17281 #else
17282 return "";
17283 #endif
17284
17285 case 'F':
17286 /* %F displays the frame name. */
17287 if (!NILP (f->title))
17288 return (char *) SDATA (f->title);
17289 if (f->explicit_name || ! FRAME_WINDOW_P (f))
17290 return (char *) SDATA (f->name);
17291 return "Emacs";
17292
17293 case 'f':
17294 obj = b->filename;
17295 break;
17296
17297 case 'i':
17298 {
17299 int size = ZV - BEGV;
17300 pint2str (decode_mode_spec_buf, field_width, size);
17301 return decode_mode_spec_buf;
17302 }
17303
17304 case 'I':
17305 {
17306 int size = ZV - BEGV;
17307 pint2hrstr (decode_mode_spec_buf, field_width, size);
17308 return decode_mode_spec_buf;
17309 }
17310
17311 case 'l':
17312 {
17313 int startpos = XMARKER (w->start)->charpos;
17314 int startpos_byte = marker_byte_position (w->start);
17315 int line, linepos, linepos_byte, topline;
17316 int nlines, junk;
17317 int height = WINDOW_TOTAL_LINES (w);
17318
17319 /* If we decided that this buffer isn't suitable for line numbers,
17320 don't forget that too fast. */
17321 if (EQ (w->base_line_pos, w->buffer))
17322 goto no_value;
17323 /* But do forget it, if the window shows a different buffer now. */
17324 else if (BUFFERP (w->base_line_pos))
17325 w->base_line_pos = Qnil;
17326
17327 /* If the buffer is very big, don't waste time. */
17328 if (INTEGERP (Vline_number_display_limit)
17329 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
17330 {
17331 w->base_line_pos = Qnil;
17332 w->base_line_number = Qnil;
17333 goto no_value;
17334 }
17335
17336 if (!NILP (w->base_line_number)
17337 && !NILP (w->base_line_pos)
17338 && XFASTINT (w->base_line_pos) <= startpos)
17339 {
17340 line = XFASTINT (w->base_line_number);
17341 linepos = XFASTINT (w->base_line_pos);
17342 linepos_byte = buf_charpos_to_bytepos (b, linepos);
17343 }
17344 else
17345 {
17346 line = 1;
17347 linepos = BUF_BEGV (b);
17348 linepos_byte = BUF_BEGV_BYTE (b);
17349 }
17350
17351 /* Count lines from base line to window start position. */
17352 nlines = display_count_lines (linepos, linepos_byte,
17353 startpos_byte,
17354 startpos, &junk);
17355
17356 topline = nlines + line;
17357
17358 /* Determine a new base line, if the old one is too close
17359 or too far away, or if we did not have one.
17360 "Too close" means it's plausible a scroll-down would
17361 go back past it. */
17362 if (startpos == BUF_BEGV (b))
17363 {
17364 w->base_line_number = make_number (topline);
17365 w->base_line_pos = make_number (BUF_BEGV (b));
17366 }
17367 else if (nlines < height + 25 || nlines > height * 3 + 50
17368 || linepos == BUF_BEGV (b))
17369 {
17370 int limit = BUF_BEGV (b);
17371 int limit_byte = BUF_BEGV_BYTE (b);
17372 int position;
17373 int distance = (height * 2 + 30) * line_number_display_limit_width;
17374
17375 if (startpos - distance > limit)
17376 {
17377 limit = startpos - distance;
17378 limit_byte = CHAR_TO_BYTE (limit);
17379 }
17380
17381 nlines = display_count_lines (startpos, startpos_byte,
17382 limit_byte,
17383 - (height * 2 + 30),
17384 &position);
17385 /* If we couldn't find the lines we wanted within
17386 line_number_display_limit_width chars per line,
17387 give up on line numbers for this window. */
17388 if (position == limit_byte && limit == startpos - distance)
17389 {
17390 w->base_line_pos = w->buffer;
17391 w->base_line_number = Qnil;
17392 goto no_value;
17393 }
17394
17395 w->base_line_number = make_number (topline - nlines);
17396 w->base_line_pos = make_number (BYTE_TO_CHAR (position));
17397 }
17398
17399 /* Now count lines from the start pos to point. */
17400 nlines = display_count_lines (startpos, startpos_byte,
17401 PT_BYTE, PT, &junk);
17402
17403 /* Record that we did display the line number. */
17404 line_number_displayed = 1;
17405
17406 /* Make the string to show. */
17407 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
17408 return decode_mode_spec_buf;
17409 no_value:
17410 {
17411 char* p = decode_mode_spec_buf;
17412 int pad = field_width - 2;
17413 while (pad-- > 0)
17414 *p++ = ' ';
17415 *p++ = '?';
17416 *p++ = '?';
17417 *p = '\0';
17418 return decode_mode_spec_buf;
17419 }
17420 }
17421 break;
17422
17423 case 'm':
17424 obj = b->mode_name;
17425 break;
17426
17427 case 'n':
17428 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
17429 return " Narrow";
17430 break;
17431
17432 case 'p':
17433 {
17434 int pos = marker_position (w->start);
17435 int total = BUF_ZV (b) - BUF_BEGV (b);
17436
17437 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
17438 {
17439 if (pos <= BUF_BEGV (b))
17440 return "All";
17441 else
17442 return "Bottom";
17443 }
17444 else if (pos <= BUF_BEGV (b))
17445 return "Top";
17446 else
17447 {
17448 if (total > 1000000)
17449 /* Do it differently for a large value, to avoid overflow. */
17450 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
17451 else
17452 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
17453 /* We can't normally display a 3-digit number,
17454 so get us a 2-digit number that is close. */
17455 if (total == 100)
17456 total = 99;
17457 sprintf (decode_mode_spec_buf, "%2d%%", total);
17458 return decode_mode_spec_buf;
17459 }
17460 }
17461
17462 /* Display percentage of size above the bottom of the screen. */
17463 case 'P':
17464 {
17465 int toppos = marker_position (w->start);
17466 int botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
17467 int total = BUF_ZV (b) - BUF_BEGV (b);
17468
17469 if (botpos >= BUF_ZV (b))
17470 {
17471 if (toppos <= BUF_BEGV (b))
17472 return "All";
17473 else
17474 return "Bottom";
17475 }
17476 else
17477 {
17478 if (total > 1000000)
17479 /* Do it differently for a large value, to avoid overflow. */
17480 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
17481 else
17482 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
17483 /* We can't normally display a 3-digit number,
17484 so get us a 2-digit number that is close. */
17485 if (total == 100)
17486 total = 99;
17487 if (toppos <= BUF_BEGV (b))
17488 sprintf (decode_mode_spec_buf, "Top%2d%%", total);
17489 else
17490 sprintf (decode_mode_spec_buf, "%2d%%", total);
17491 return decode_mode_spec_buf;
17492 }
17493 }
17494
17495 case 's':
17496 /* status of process */
17497 obj = Fget_buffer_process (Fcurrent_buffer ());
17498 if (NILP (obj))
17499 return "no process";
17500 #ifdef subprocesses
17501 obj = Fsymbol_name (Fprocess_status (obj));
17502 #endif
17503 break;
17504
17505 case 't': /* indicate TEXT or BINARY */
17506 #ifdef MODE_LINE_BINARY_TEXT
17507 return MODE_LINE_BINARY_TEXT (b);
17508 #else
17509 return "T";
17510 #endif
17511
17512 case 'z':
17513 /* coding-system (not including end-of-line format) */
17514 case 'Z':
17515 /* coding-system (including end-of-line type) */
17516 {
17517 int eol_flag = (c == 'Z');
17518 char *p = decode_mode_spec_buf;
17519
17520 if (! FRAME_WINDOW_P (f))
17521 {
17522 /* No need to mention EOL here--the terminal never needs
17523 to do EOL conversion. */
17524 p = decode_mode_spec_coding (FRAME_KEYBOARD_CODING (f)->symbol, p, 0);
17525 p = decode_mode_spec_coding (FRAME_TERMINAL_CODING (f)->symbol, p, 0);
17526 }
17527 p = decode_mode_spec_coding (b->buffer_file_coding_system,
17528 p, eol_flag);
17529
17530 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
17531 #ifdef subprocesses
17532 obj = Fget_buffer_process (Fcurrent_buffer ());
17533 if (PROCESSP (obj))
17534 {
17535 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
17536 p, eol_flag);
17537 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
17538 p, eol_flag);
17539 }
17540 #endif /* subprocesses */
17541 #endif /* 0 */
17542 *p = 0;
17543 return decode_mode_spec_buf;
17544 }
17545 }
17546
17547 if (STRINGP (obj))
17548 {
17549 *multibyte = STRING_MULTIBYTE (obj);
17550 return (char *) SDATA (obj);
17551 }
17552 else
17553 return "";
17554 }
17555
17556
17557 /* Count up to COUNT lines starting from START / START_BYTE.
17558 But don't go beyond LIMIT_BYTE.
17559 Return the number of lines thus found (always nonnegative).
17560
17561 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
17562
17563 static int
17564 display_count_lines (start, start_byte, limit_byte, count, byte_pos_ptr)
17565 int start, start_byte, limit_byte, count;
17566 int *byte_pos_ptr;
17567 {
17568 register unsigned char *cursor;
17569 unsigned char *base;
17570
17571 register int ceiling;
17572 register unsigned char *ceiling_addr;
17573 int orig_count = count;
17574
17575 /* If we are not in selective display mode,
17576 check only for newlines. */
17577 int selective_display = (!NILP (current_buffer->selective_display)
17578 && !INTEGERP (current_buffer->selective_display));
17579
17580 if (count > 0)
17581 {
17582 while (start_byte < limit_byte)
17583 {
17584 ceiling = BUFFER_CEILING_OF (start_byte);
17585 ceiling = min (limit_byte - 1, ceiling);
17586 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
17587 base = (cursor = BYTE_POS_ADDR (start_byte));
17588 while (1)
17589 {
17590 if (selective_display)
17591 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
17592 ;
17593 else
17594 while (*cursor != '\n' && ++cursor != ceiling_addr)
17595 ;
17596
17597 if (cursor != ceiling_addr)
17598 {
17599 if (--count == 0)
17600 {
17601 start_byte += cursor - base + 1;
17602 *byte_pos_ptr = start_byte;
17603 return orig_count;
17604 }
17605 else
17606 if (++cursor == ceiling_addr)
17607 break;
17608 }
17609 else
17610 break;
17611 }
17612 start_byte += cursor - base;
17613 }
17614 }
17615 else
17616 {
17617 while (start_byte > limit_byte)
17618 {
17619 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
17620 ceiling = max (limit_byte, ceiling);
17621 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
17622 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
17623 while (1)
17624 {
17625 if (selective_display)
17626 while (--cursor != ceiling_addr
17627 && *cursor != '\n' && *cursor != 015)
17628 ;
17629 else
17630 while (--cursor != ceiling_addr && *cursor != '\n')
17631 ;
17632
17633 if (cursor != ceiling_addr)
17634 {
17635 if (++count == 0)
17636 {
17637 start_byte += cursor - base + 1;
17638 *byte_pos_ptr = start_byte;
17639 /* When scanning backwards, we should
17640 not count the newline posterior to which we stop. */
17641 return - orig_count - 1;
17642 }
17643 }
17644 else
17645 break;
17646 }
17647 /* Here we add 1 to compensate for the last decrement
17648 of CURSOR, which took it past the valid range. */
17649 start_byte += cursor - base + 1;
17650 }
17651 }
17652
17653 *byte_pos_ptr = limit_byte;
17654
17655 if (count < 0)
17656 return - orig_count + count;
17657 return orig_count - count;
17658
17659 }
17660
17661
17662 \f
17663 /***********************************************************************
17664 Displaying strings
17665 ***********************************************************************/
17666
17667 /* Display a NUL-terminated string, starting with index START.
17668
17669 If STRING is non-null, display that C string. Otherwise, the Lisp
17670 string LISP_STRING is displayed.
17671
17672 If FACE_STRING is not nil, FACE_STRING_POS is a position in
17673 FACE_STRING. Display STRING or LISP_STRING with the face at
17674 FACE_STRING_POS in FACE_STRING:
17675
17676 Display the string in the environment given by IT, but use the
17677 standard display table, temporarily.
17678
17679 FIELD_WIDTH is the minimum number of output glyphs to produce.
17680 If STRING has fewer characters than FIELD_WIDTH, pad to the right
17681 with spaces. If STRING has more characters, more than FIELD_WIDTH
17682 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
17683
17684 PRECISION is the maximum number of characters to output from
17685 STRING. PRECISION < 0 means don't truncate the string.
17686
17687 This is roughly equivalent to printf format specifiers:
17688
17689 FIELD_WIDTH PRECISION PRINTF
17690 ----------------------------------------
17691 -1 -1 %s
17692 -1 10 %.10s
17693 10 -1 %10s
17694 20 10 %20.10s
17695
17696 MULTIBYTE zero means do not display multibyte chars, > 0 means do
17697 display them, and < 0 means obey the current buffer's value of
17698 enable_multibyte_characters.
17699
17700 Value is the number of glyphs produced. */
17701
17702 static int
17703 display_string (string, lisp_string, face_string, face_string_pos,
17704 start, it, field_width, precision, max_x, multibyte)
17705 unsigned char *string;
17706 Lisp_Object lisp_string;
17707 Lisp_Object face_string;
17708 int face_string_pos;
17709 int start;
17710 struct it *it;
17711 int field_width, precision, max_x;
17712 int multibyte;
17713 {
17714 int hpos_at_start = it->hpos;
17715 int saved_face_id = it->face_id;
17716 struct glyph_row *row = it->glyph_row;
17717
17718 /* Initialize the iterator IT for iteration over STRING beginning
17719 with index START. */
17720 reseat_to_string (it, string, lisp_string, start,
17721 precision, field_width, multibyte);
17722
17723 /* If displaying STRING, set up the face of the iterator
17724 from LISP_STRING, if that's given. */
17725 if (STRINGP (face_string))
17726 {
17727 int endptr;
17728 struct face *face;
17729
17730 it->face_id
17731 = face_at_string_position (it->w, face_string, face_string_pos,
17732 0, it->region_beg_charpos,
17733 it->region_end_charpos,
17734 &endptr, it->base_face_id, 0);
17735 face = FACE_FROM_ID (it->f, it->face_id);
17736 it->face_box_p = face->box != FACE_NO_BOX;
17737 }
17738
17739 /* Set max_x to the maximum allowed X position. Don't let it go
17740 beyond the right edge of the window. */
17741 if (max_x <= 0)
17742 max_x = it->last_visible_x;
17743 else
17744 max_x = min (max_x, it->last_visible_x);
17745
17746 /* Skip over display elements that are not visible. because IT->w is
17747 hscrolled. */
17748 if (it->current_x < it->first_visible_x)
17749 move_it_in_display_line_to (it, 100000, it->first_visible_x,
17750 MOVE_TO_POS | MOVE_TO_X);
17751
17752 row->ascent = it->max_ascent;
17753 row->height = it->max_ascent + it->max_descent;
17754 row->phys_ascent = it->max_phys_ascent;
17755 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
17756 row->extra_line_spacing = it->max_extra_line_spacing;
17757
17758 /* This condition is for the case that we are called with current_x
17759 past last_visible_x. */
17760 while (it->current_x < max_x)
17761 {
17762 int x_before, x, n_glyphs_before, i, nglyphs;
17763
17764 /* Get the next display element. */
17765 if (!get_next_display_element (it))
17766 break;
17767
17768 /* Produce glyphs. */
17769 x_before = it->current_x;
17770 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
17771 PRODUCE_GLYPHS (it);
17772
17773 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
17774 i = 0;
17775 x = x_before;
17776 while (i < nglyphs)
17777 {
17778 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
17779
17780 if (!it->truncate_lines_p
17781 && x + glyph->pixel_width > max_x)
17782 {
17783 /* End of continued line or max_x reached. */
17784 if (CHAR_GLYPH_PADDING_P (*glyph))
17785 {
17786 /* A wide character is unbreakable. */
17787 it->glyph_row->used[TEXT_AREA] = n_glyphs_before;
17788 it->current_x = x_before;
17789 }
17790 else
17791 {
17792 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
17793 it->current_x = x;
17794 }
17795 break;
17796 }
17797 else if (x + glyph->pixel_width > it->first_visible_x)
17798 {
17799 /* Glyph is at least partially visible. */
17800 ++it->hpos;
17801 if (x < it->first_visible_x)
17802 it->glyph_row->x = x - it->first_visible_x;
17803 }
17804 else
17805 {
17806 /* Glyph is off the left margin of the display area.
17807 Should not happen. */
17808 abort ();
17809 }
17810
17811 row->ascent = max (row->ascent, it->max_ascent);
17812 row->height = max (row->height, it->max_ascent + it->max_descent);
17813 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
17814 row->phys_height = max (row->phys_height,
17815 it->max_phys_ascent + it->max_phys_descent);
17816 row->extra_line_spacing = max (row->extra_line_spacing,
17817 it->max_extra_line_spacing);
17818 x += glyph->pixel_width;
17819 ++i;
17820 }
17821
17822 /* Stop if max_x reached. */
17823 if (i < nglyphs)
17824 break;
17825
17826 /* Stop at line ends. */
17827 if (ITERATOR_AT_END_OF_LINE_P (it))
17828 {
17829 it->continuation_lines_width = 0;
17830 break;
17831 }
17832
17833 set_iterator_to_next (it, 1);
17834
17835 /* Stop if truncating at the right edge. */
17836 if (it->truncate_lines_p
17837 && it->current_x >= it->last_visible_x)
17838 {
17839 /* Add truncation mark, but don't do it if the line is
17840 truncated at a padding space. */
17841 if (IT_CHARPOS (*it) < it->string_nchars)
17842 {
17843 if (!FRAME_WINDOW_P (it->f))
17844 {
17845 int i, n;
17846
17847 if (it->current_x > it->last_visible_x)
17848 {
17849 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
17850 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
17851 break;
17852 for (n = row->used[TEXT_AREA]; i < n; ++i)
17853 {
17854 row->used[TEXT_AREA] = i;
17855 produce_special_glyphs (it, IT_TRUNCATION);
17856 }
17857 }
17858 produce_special_glyphs (it, IT_TRUNCATION);
17859 }
17860 it->glyph_row->truncated_on_right_p = 1;
17861 }
17862 break;
17863 }
17864 }
17865
17866 /* Maybe insert a truncation at the left. */
17867 if (it->first_visible_x
17868 && IT_CHARPOS (*it) > 0)
17869 {
17870 if (!FRAME_WINDOW_P (it->f))
17871 insert_left_trunc_glyphs (it);
17872 it->glyph_row->truncated_on_left_p = 1;
17873 }
17874
17875 it->face_id = saved_face_id;
17876
17877 /* Value is number of columns displayed. */
17878 return it->hpos - hpos_at_start;
17879 }
17880
17881
17882 \f
17883 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
17884 appears as an element of LIST or as the car of an element of LIST.
17885 If PROPVAL is a list, compare each element against LIST in that
17886 way, and return 1/2 if any element of PROPVAL is found in LIST.
17887 Otherwise return 0. This function cannot quit.
17888 The return value is 2 if the text is invisible but with an ellipsis
17889 and 1 if it's invisible and without an ellipsis. */
17890
17891 int
17892 invisible_p (propval, list)
17893 register Lisp_Object propval;
17894 Lisp_Object list;
17895 {
17896 register Lisp_Object tail, proptail;
17897
17898 for (tail = list; CONSP (tail); tail = XCDR (tail))
17899 {
17900 register Lisp_Object tem;
17901 tem = XCAR (tail);
17902 if (EQ (propval, tem))
17903 return 1;
17904 if (CONSP (tem) && EQ (propval, XCAR (tem)))
17905 return NILP (XCDR (tem)) ? 1 : 2;
17906 }
17907
17908 if (CONSP (propval))
17909 {
17910 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
17911 {
17912 Lisp_Object propelt;
17913 propelt = XCAR (proptail);
17914 for (tail = list; CONSP (tail); tail = XCDR (tail))
17915 {
17916 register Lisp_Object tem;
17917 tem = XCAR (tail);
17918 if (EQ (propelt, tem))
17919 return 1;
17920 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
17921 return NILP (XCDR (tem)) ? 1 : 2;
17922 }
17923 }
17924 }
17925
17926 return 0;
17927 }
17928
17929 /* Calculate a width or height in pixels from a specification using
17930 the following elements:
17931
17932 SPEC ::=
17933 NUM - a (fractional) multiple of the default font width/height
17934 (NUM) - specifies exactly NUM pixels
17935 UNIT - a fixed number of pixels, see below.
17936 ELEMENT - size of a display element in pixels, see below.
17937 (NUM . SPEC) - equals NUM * SPEC
17938 (+ SPEC SPEC ...) - add pixel values
17939 (- SPEC SPEC ...) - subtract pixel values
17940 (- SPEC) - negate pixel value
17941
17942 NUM ::=
17943 INT or FLOAT - a number constant
17944 SYMBOL - use symbol's (buffer local) variable binding.
17945
17946 UNIT ::=
17947 in - pixels per inch *)
17948 mm - pixels per 1/1000 meter *)
17949 cm - pixels per 1/100 meter *)
17950 width - width of current font in pixels.
17951 height - height of current font in pixels.
17952
17953 *) using the ratio(s) defined in display-pixels-per-inch.
17954
17955 ELEMENT ::=
17956
17957 left-fringe - left fringe width in pixels
17958 right-fringe - right fringe width in pixels
17959
17960 left-margin - left margin width in pixels
17961 right-margin - right margin width in pixels
17962
17963 scroll-bar - scroll-bar area width in pixels
17964
17965 Examples:
17966
17967 Pixels corresponding to 5 inches:
17968 (5 . in)
17969
17970 Total width of non-text areas on left side of window (if scroll-bar is on left):
17971 '(space :width (+ left-fringe left-margin scroll-bar))
17972
17973 Align to first text column (in header line):
17974 '(space :align-to 0)
17975
17976 Align to middle of text area minus half the width of variable `my-image'
17977 containing a loaded image:
17978 '(space :align-to (0.5 . (- text my-image)))
17979
17980 Width of left margin minus width of 1 character in the default font:
17981 '(space :width (- left-margin 1))
17982
17983 Width of left margin minus width of 2 characters in the current font:
17984 '(space :width (- left-margin (2 . width)))
17985
17986 Center 1 character over left-margin (in header line):
17987 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
17988
17989 Different ways to express width of left fringe plus left margin minus one pixel:
17990 '(space :width (- (+ left-fringe left-margin) (1)))
17991 '(space :width (+ left-fringe left-margin (- (1))))
17992 '(space :width (+ left-fringe left-margin (-1)))
17993
17994 */
17995
17996 #define NUMVAL(X) \
17997 ((INTEGERP (X) || FLOATP (X)) \
17998 ? XFLOATINT (X) \
17999 : - 1)
18000
18001 int
18002 calc_pixel_width_or_height (res, it, prop, font, width_p, align_to)
18003 double *res;
18004 struct it *it;
18005 Lisp_Object prop;
18006 void *font;
18007 int width_p, *align_to;
18008 {
18009 double pixels;
18010
18011 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
18012 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
18013
18014 if (NILP (prop))
18015 return OK_PIXELS (0);
18016
18017 xassert (FRAME_LIVE_P (it->f));
18018
18019 if (SYMBOLP (prop))
18020 {
18021 if (SCHARS (SYMBOL_NAME (prop)) == 2)
18022 {
18023 char *unit = SDATA (SYMBOL_NAME (prop));
18024
18025 if (unit[0] == 'i' && unit[1] == 'n')
18026 pixels = 1.0;
18027 else if (unit[0] == 'm' && unit[1] == 'm')
18028 pixels = 25.4;
18029 else if (unit[0] == 'c' && unit[1] == 'm')
18030 pixels = 2.54;
18031 else
18032 pixels = 0;
18033 if (pixels > 0)
18034 {
18035 double ppi;
18036 #ifdef HAVE_WINDOW_SYSTEM
18037 if (FRAME_WINDOW_P (it->f)
18038 && (ppi = (width_p
18039 ? FRAME_X_DISPLAY_INFO (it->f)->resx
18040 : FRAME_X_DISPLAY_INFO (it->f)->resy),
18041 ppi > 0))
18042 return OK_PIXELS (ppi / pixels);
18043 #endif
18044
18045 if ((ppi = NUMVAL (Vdisplay_pixels_per_inch), ppi > 0)
18046 || (CONSP (Vdisplay_pixels_per_inch)
18047 && (ppi = (width_p
18048 ? NUMVAL (XCAR (Vdisplay_pixels_per_inch))
18049 : NUMVAL (XCDR (Vdisplay_pixels_per_inch))),
18050 ppi > 0)))
18051 return OK_PIXELS (ppi / pixels);
18052
18053 return 0;
18054 }
18055 }
18056
18057 #ifdef HAVE_WINDOW_SYSTEM
18058 if (EQ (prop, Qheight))
18059 return OK_PIXELS (font ? FONT_HEIGHT ((XFontStruct *)font) : FRAME_LINE_HEIGHT (it->f));
18060 if (EQ (prop, Qwidth))
18061 return OK_PIXELS (font ? FONT_WIDTH ((XFontStruct *)font) : FRAME_COLUMN_WIDTH (it->f));
18062 #else
18063 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
18064 return OK_PIXELS (1);
18065 #endif
18066
18067 if (EQ (prop, Qtext))
18068 return OK_PIXELS (width_p
18069 ? window_box_width (it->w, TEXT_AREA)
18070 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
18071
18072 if (align_to && *align_to < 0)
18073 {
18074 *res = 0;
18075 if (EQ (prop, Qleft))
18076 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
18077 if (EQ (prop, Qright))
18078 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
18079 if (EQ (prop, Qcenter))
18080 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
18081 + window_box_width (it->w, TEXT_AREA) / 2);
18082 if (EQ (prop, Qleft_fringe))
18083 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
18084 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
18085 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
18086 if (EQ (prop, Qright_fringe))
18087 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
18088 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
18089 : window_box_right_offset (it->w, TEXT_AREA));
18090 if (EQ (prop, Qleft_margin))
18091 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
18092 if (EQ (prop, Qright_margin))
18093 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
18094 if (EQ (prop, Qscroll_bar))
18095 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
18096 ? 0
18097 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
18098 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
18099 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
18100 : 0)));
18101 }
18102 else
18103 {
18104 if (EQ (prop, Qleft_fringe))
18105 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
18106 if (EQ (prop, Qright_fringe))
18107 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
18108 if (EQ (prop, Qleft_margin))
18109 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
18110 if (EQ (prop, Qright_margin))
18111 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
18112 if (EQ (prop, Qscroll_bar))
18113 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
18114 }
18115
18116 prop = Fbuffer_local_value (prop, it->w->buffer);
18117 }
18118
18119 if (INTEGERP (prop) || FLOATP (prop))
18120 {
18121 int base_unit = (width_p
18122 ? FRAME_COLUMN_WIDTH (it->f)
18123 : FRAME_LINE_HEIGHT (it->f));
18124 return OK_PIXELS (XFLOATINT (prop) * base_unit);
18125 }
18126
18127 if (CONSP (prop))
18128 {
18129 Lisp_Object car = XCAR (prop);
18130 Lisp_Object cdr = XCDR (prop);
18131
18132 if (SYMBOLP (car))
18133 {
18134 #ifdef HAVE_WINDOW_SYSTEM
18135 if (FRAME_WINDOW_P (it->f)
18136 && valid_image_p (prop))
18137 {
18138 int id = lookup_image (it->f, prop);
18139 struct image *img = IMAGE_FROM_ID (it->f, id);
18140
18141 return OK_PIXELS (width_p ? img->width : img->height);
18142 }
18143 #endif
18144 if (EQ (car, Qplus) || EQ (car, Qminus))
18145 {
18146 int first = 1;
18147 double px;
18148
18149 pixels = 0;
18150 while (CONSP (cdr))
18151 {
18152 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
18153 font, width_p, align_to))
18154 return 0;
18155 if (first)
18156 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
18157 else
18158 pixels += px;
18159 cdr = XCDR (cdr);
18160 }
18161 if (EQ (car, Qminus))
18162 pixels = -pixels;
18163 return OK_PIXELS (pixels);
18164 }
18165
18166 car = Fbuffer_local_value (car, it->w->buffer);
18167 }
18168
18169 if (INTEGERP (car) || FLOATP (car))
18170 {
18171 double fact;
18172 pixels = XFLOATINT (car);
18173 if (NILP (cdr))
18174 return OK_PIXELS (pixels);
18175 if (calc_pixel_width_or_height (&fact, it, cdr,
18176 font, width_p, align_to))
18177 return OK_PIXELS (pixels * fact);
18178 return 0;
18179 }
18180
18181 return 0;
18182 }
18183
18184 return 0;
18185 }
18186
18187 \f
18188 /***********************************************************************
18189 Glyph Display
18190 ***********************************************************************/
18191
18192 #ifdef HAVE_WINDOW_SYSTEM
18193
18194 #if GLYPH_DEBUG
18195
18196 void
18197 dump_glyph_string (s)
18198 struct glyph_string *s;
18199 {
18200 fprintf (stderr, "glyph string\n");
18201 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
18202 s->x, s->y, s->width, s->height);
18203 fprintf (stderr, " ybase = %d\n", s->ybase);
18204 fprintf (stderr, " hl = %d\n", s->hl);
18205 fprintf (stderr, " left overhang = %d, right = %d\n",
18206 s->left_overhang, s->right_overhang);
18207 fprintf (stderr, " nchars = %d\n", s->nchars);
18208 fprintf (stderr, " extends to end of line = %d\n",
18209 s->extends_to_end_of_line_p);
18210 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
18211 fprintf (stderr, " bg width = %d\n", s->background_width);
18212 }
18213
18214 #endif /* GLYPH_DEBUG */
18215
18216 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
18217 of XChar2b structures for S; it can't be allocated in
18218 init_glyph_string because it must be allocated via `alloca'. W
18219 is the window on which S is drawn. ROW and AREA are the glyph row
18220 and area within the row from which S is constructed. START is the
18221 index of the first glyph structure covered by S. HL is a
18222 face-override for drawing S. */
18223
18224 #ifdef HAVE_NTGUI
18225 #define OPTIONAL_HDC(hdc) hdc,
18226 #define DECLARE_HDC(hdc) HDC hdc;
18227 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
18228 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
18229 #endif
18230
18231 #ifndef OPTIONAL_HDC
18232 #define OPTIONAL_HDC(hdc)
18233 #define DECLARE_HDC(hdc)
18234 #define ALLOCATE_HDC(hdc, f)
18235 #define RELEASE_HDC(hdc, f)
18236 #endif
18237
18238 static void
18239 init_glyph_string (s, OPTIONAL_HDC (hdc) char2b, w, row, area, start, hl)
18240 struct glyph_string *s;
18241 DECLARE_HDC (hdc)
18242 XChar2b *char2b;
18243 struct window *w;
18244 struct glyph_row *row;
18245 enum glyph_row_area area;
18246 int start;
18247 enum draw_glyphs_face hl;
18248 {
18249 bzero (s, sizeof *s);
18250 s->w = w;
18251 s->f = XFRAME (w->frame);
18252 #ifdef HAVE_NTGUI
18253 s->hdc = hdc;
18254 #endif
18255 s->display = FRAME_X_DISPLAY (s->f);
18256 s->window = FRAME_X_WINDOW (s->f);
18257 s->char2b = char2b;
18258 s->hl = hl;
18259 s->row = row;
18260 s->area = area;
18261 s->first_glyph = row->glyphs[area] + start;
18262 s->height = row->height;
18263 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
18264
18265 /* Display the internal border below the tool-bar window. */
18266 if (WINDOWP (s->f->tool_bar_window)
18267 && s->w == XWINDOW (s->f->tool_bar_window))
18268 s->y -= FRAME_INTERNAL_BORDER_WIDTH (s->f);
18269
18270 s->ybase = s->y + row->ascent;
18271 }
18272
18273
18274 /* Append the list of glyph strings with head H and tail T to the list
18275 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
18276
18277 static INLINE void
18278 append_glyph_string_lists (head, tail, h, t)
18279 struct glyph_string **head, **tail;
18280 struct glyph_string *h, *t;
18281 {
18282 if (h)
18283 {
18284 if (*head)
18285 (*tail)->next = h;
18286 else
18287 *head = h;
18288 h->prev = *tail;
18289 *tail = t;
18290 }
18291 }
18292
18293
18294 /* Prepend the list of glyph strings with head H and tail T to the
18295 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
18296 result. */
18297
18298 static INLINE void
18299 prepend_glyph_string_lists (head, tail, h, t)
18300 struct glyph_string **head, **tail;
18301 struct glyph_string *h, *t;
18302 {
18303 if (h)
18304 {
18305 if (*head)
18306 (*head)->prev = t;
18307 else
18308 *tail = t;
18309 t->next = *head;
18310 *head = h;
18311 }
18312 }
18313
18314
18315 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
18316 Set *HEAD and *TAIL to the resulting list. */
18317
18318 static INLINE void
18319 append_glyph_string (head, tail, s)
18320 struct glyph_string **head, **tail;
18321 struct glyph_string *s;
18322 {
18323 s->next = s->prev = NULL;
18324 append_glyph_string_lists (head, tail, s, s);
18325 }
18326
18327
18328 /* Get face and two-byte form of character glyph GLYPH on frame F.
18329 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
18330 a pointer to a realized face that is ready for display. */
18331
18332 static INLINE struct face *
18333 get_glyph_face_and_encoding (f, glyph, char2b, two_byte_p)
18334 struct frame *f;
18335 struct glyph *glyph;
18336 XChar2b *char2b;
18337 int *two_byte_p;
18338 {
18339 struct face *face;
18340
18341 xassert (glyph->type == CHAR_GLYPH);
18342 face = FACE_FROM_ID (f, glyph->face_id);
18343
18344 if (two_byte_p)
18345 *two_byte_p = 0;
18346
18347 if (!glyph->multibyte_p)
18348 {
18349 /* Unibyte case. We don't have to encode, but we have to make
18350 sure to use a face suitable for unibyte. */
18351 STORE_XCHAR2B (char2b, 0, glyph->u.ch);
18352 }
18353 else if (glyph->u.ch < 128
18354 && glyph->face_id < BASIC_FACE_ID_SENTINEL)
18355 {
18356 /* Case of ASCII in a face known to fit ASCII. */
18357 STORE_XCHAR2B (char2b, 0, glyph->u.ch);
18358 }
18359 else
18360 {
18361 int c1, c2, charset;
18362
18363 /* Split characters into bytes. If c2 is -1 afterwards, C is
18364 really a one-byte character so that byte1 is zero. */
18365 SPLIT_CHAR (glyph->u.ch, charset, c1, c2);
18366 if (c2 > 0)
18367 STORE_XCHAR2B (char2b, c1, c2);
18368 else
18369 STORE_XCHAR2B (char2b, 0, c1);
18370
18371 /* Maybe encode the character in *CHAR2B. */
18372 if (charset != CHARSET_ASCII)
18373 {
18374 struct font_info *font_info
18375 = FONT_INFO_FROM_ID (f, face->font_info_id);
18376 if (font_info)
18377 glyph->font_type
18378 = FRAME_RIF (f)->encode_char (glyph->u.ch, char2b, font_info, two_byte_p);
18379 }
18380 }
18381
18382 /* Make sure X resources of the face are allocated. */
18383 xassert (face != NULL);
18384 PREPARE_FACE_FOR_DISPLAY (f, face);
18385 return face;
18386 }
18387
18388
18389 /* Fill glyph string S with composition components specified by S->cmp.
18390
18391 FACES is an array of faces for all components of this composition.
18392 S->gidx is the index of the first component for S.
18393
18394 OVERLAPS non-zero means S should draw the foreground only, and use
18395 its physical height for clipping. See also draw_glyphs.
18396
18397 Value is the index of a component not in S. */
18398
18399 static int
18400 fill_composite_glyph_string (s, faces, overlaps)
18401 struct glyph_string *s;
18402 struct face **faces;
18403 int overlaps;
18404 {
18405 int i;
18406
18407 xassert (s);
18408
18409 s->for_overlaps = overlaps;
18410
18411 s->face = faces[s->gidx];
18412 s->font = s->face->font;
18413 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
18414
18415 /* For all glyphs of this composition, starting at the offset
18416 S->gidx, until we reach the end of the definition or encounter a
18417 glyph that requires the different face, add it to S. */
18418 ++s->nchars;
18419 for (i = s->gidx + 1; i < s->cmp->glyph_len && faces[i] == s->face; ++i)
18420 ++s->nchars;
18421
18422 /* All glyph strings for the same composition has the same width,
18423 i.e. the width set for the first component of the composition. */
18424
18425 s->width = s->first_glyph->pixel_width;
18426
18427 /* If the specified font could not be loaded, use the frame's
18428 default font, but record the fact that we couldn't load it in
18429 the glyph string so that we can draw rectangles for the
18430 characters of the glyph string. */
18431 if (s->font == NULL)
18432 {
18433 s->font_not_found_p = 1;
18434 s->font = FRAME_FONT (s->f);
18435 }
18436
18437 /* Adjust base line for subscript/superscript text. */
18438 s->ybase += s->first_glyph->voffset;
18439
18440 xassert (s->face && s->face->gc);
18441
18442 /* This glyph string must always be drawn with 16-bit functions. */
18443 s->two_byte_p = 1;
18444
18445 return s->gidx + s->nchars;
18446 }
18447
18448
18449 /* Fill glyph string S from a sequence of character glyphs.
18450
18451 FACE_ID is the face id of the string. START is the index of the
18452 first glyph to consider, END is the index of the last + 1.
18453 OVERLAPS non-zero means S should draw the foreground only, and use
18454 its physical height for clipping. See also draw_glyphs.
18455
18456 Value is the index of the first glyph not in S. */
18457
18458 static int
18459 fill_glyph_string (s, face_id, start, end, overlaps)
18460 struct glyph_string *s;
18461 int face_id;
18462 int start, end, overlaps;
18463 {
18464 struct glyph *glyph, *last;
18465 int voffset;
18466 int glyph_not_available_p;
18467
18468 xassert (s->f == XFRAME (s->w->frame));
18469 xassert (s->nchars == 0);
18470 xassert (start >= 0 && end > start);
18471
18472 s->for_overlaps = overlaps,
18473 glyph = s->row->glyphs[s->area] + start;
18474 last = s->row->glyphs[s->area] + end;
18475 voffset = glyph->voffset;
18476
18477 glyph_not_available_p = glyph->glyph_not_available_p;
18478
18479 while (glyph < last
18480 && glyph->type == CHAR_GLYPH
18481 && glyph->voffset == voffset
18482 /* Same face id implies same font, nowadays. */
18483 && glyph->face_id == face_id
18484 && glyph->glyph_not_available_p == glyph_not_available_p)
18485 {
18486 int two_byte_p;
18487
18488 s->face = get_glyph_face_and_encoding (s->f, glyph,
18489 s->char2b + s->nchars,
18490 &two_byte_p);
18491 s->two_byte_p = two_byte_p;
18492 ++s->nchars;
18493 xassert (s->nchars <= end - start);
18494 s->width += glyph->pixel_width;
18495 ++glyph;
18496 }
18497
18498 s->font = s->face->font;
18499 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
18500
18501 /* If the specified font could not be loaded, use the frame's font,
18502 but record the fact that we couldn't load it in
18503 S->font_not_found_p so that we can draw rectangles for the
18504 characters of the glyph string. */
18505 if (s->font == NULL || glyph_not_available_p)
18506 {
18507 s->font_not_found_p = 1;
18508 s->font = FRAME_FONT (s->f);
18509 }
18510
18511 /* Adjust base line for subscript/superscript text. */
18512 s->ybase += voffset;
18513
18514 xassert (s->face && s->face->gc);
18515 return glyph - s->row->glyphs[s->area];
18516 }
18517
18518
18519 /* Fill glyph string S from image glyph S->first_glyph. */
18520
18521 static void
18522 fill_image_glyph_string (s)
18523 struct glyph_string *s;
18524 {
18525 xassert (s->first_glyph->type == IMAGE_GLYPH);
18526 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
18527 xassert (s->img);
18528 s->slice = s->first_glyph->slice;
18529 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
18530 s->font = s->face->font;
18531 s->width = s->first_glyph->pixel_width;
18532
18533 /* Adjust base line for subscript/superscript text. */
18534 s->ybase += s->first_glyph->voffset;
18535 }
18536
18537
18538 /* Fill glyph string S from a sequence of stretch glyphs.
18539
18540 ROW is the glyph row in which the glyphs are found, AREA is the
18541 area within the row. START is the index of the first glyph to
18542 consider, END is the index of the last + 1.
18543
18544 Value is the index of the first glyph not in S. */
18545
18546 static int
18547 fill_stretch_glyph_string (s, row, area, start, end)
18548 struct glyph_string *s;
18549 struct glyph_row *row;
18550 enum glyph_row_area area;
18551 int start, end;
18552 {
18553 struct glyph *glyph, *last;
18554 int voffset, face_id;
18555
18556 xassert (s->first_glyph->type == STRETCH_GLYPH);
18557
18558 glyph = s->row->glyphs[s->area] + start;
18559 last = s->row->glyphs[s->area] + end;
18560 face_id = glyph->face_id;
18561 s->face = FACE_FROM_ID (s->f, face_id);
18562 s->font = s->face->font;
18563 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
18564 s->width = glyph->pixel_width;
18565 voffset = glyph->voffset;
18566
18567 for (++glyph;
18568 (glyph < last
18569 && glyph->type == STRETCH_GLYPH
18570 && glyph->voffset == voffset
18571 && glyph->face_id == face_id);
18572 ++glyph)
18573 s->width += glyph->pixel_width;
18574
18575 /* Adjust base line for subscript/superscript text. */
18576 s->ybase += voffset;
18577
18578 /* The case that face->gc == 0 is handled when drawing the glyph
18579 string by calling PREPARE_FACE_FOR_DISPLAY. */
18580 xassert (s->face);
18581 return glyph - s->row->glyphs[s->area];
18582 }
18583
18584
18585 /* EXPORT for RIF:
18586 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
18587 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
18588 assumed to be zero. */
18589
18590 void
18591 x_get_glyph_overhangs (glyph, f, left, right)
18592 struct glyph *glyph;
18593 struct frame *f;
18594 int *left, *right;
18595 {
18596 *left = *right = 0;
18597
18598 if (glyph->type == CHAR_GLYPH)
18599 {
18600 XFontStruct *font;
18601 struct face *face;
18602 struct font_info *font_info;
18603 XChar2b char2b;
18604 XCharStruct *pcm;
18605
18606 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
18607 font = face->font;
18608 font_info = FONT_INFO_FROM_ID (f, face->font_info_id);
18609 if (font /* ++KFS: Should this be font_info ? */
18610 && (pcm = FRAME_RIF (f)->per_char_metric (font, &char2b, glyph->font_type)))
18611 {
18612 if (pcm->rbearing > pcm->width)
18613 *right = pcm->rbearing - pcm->width;
18614 if (pcm->lbearing < 0)
18615 *left = -pcm->lbearing;
18616 }
18617 }
18618 }
18619
18620
18621 /* Return the index of the first glyph preceding glyph string S that
18622 is overwritten by S because of S's left overhang. Value is -1
18623 if no glyphs are overwritten. */
18624
18625 static int
18626 left_overwritten (s)
18627 struct glyph_string *s;
18628 {
18629 int k;
18630
18631 if (s->left_overhang)
18632 {
18633 int x = 0, i;
18634 struct glyph *glyphs = s->row->glyphs[s->area];
18635 int first = s->first_glyph - glyphs;
18636
18637 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
18638 x -= glyphs[i].pixel_width;
18639
18640 k = i + 1;
18641 }
18642 else
18643 k = -1;
18644
18645 return k;
18646 }
18647
18648
18649 /* Return the index of the first glyph preceding glyph string S that
18650 is overwriting S because of its right overhang. Value is -1 if no
18651 glyph in front of S overwrites S. */
18652
18653 static int
18654 left_overwriting (s)
18655 struct glyph_string *s;
18656 {
18657 int i, k, x;
18658 struct glyph *glyphs = s->row->glyphs[s->area];
18659 int first = s->first_glyph - glyphs;
18660
18661 k = -1;
18662 x = 0;
18663 for (i = first - 1; i >= 0; --i)
18664 {
18665 int left, right;
18666 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
18667 if (x + right > 0)
18668 k = i;
18669 x -= glyphs[i].pixel_width;
18670 }
18671
18672 return k;
18673 }
18674
18675
18676 /* Return the index of the last glyph following glyph string S that is
18677 not overwritten by S because of S's right overhang. Value is -1 if
18678 no such glyph is found. */
18679
18680 static int
18681 right_overwritten (s)
18682 struct glyph_string *s;
18683 {
18684 int k = -1;
18685
18686 if (s->right_overhang)
18687 {
18688 int x = 0, i;
18689 struct glyph *glyphs = s->row->glyphs[s->area];
18690 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
18691 int end = s->row->used[s->area];
18692
18693 for (i = first; i < end && s->right_overhang > x; ++i)
18694 x += glyphs[i].pixel_width;
18695
18696 k = i;
18697 }
18698
18699 return k;
18700 }
18701
18702
18703 /* Return the index of the last glyph following glyph string S that
18704 overwrites S because of its left overhang. Value is negative
18705 if no such glyph is found. */
18706
18707 static int
18708 right_overwriting (s)
18709 struct glyph_string *s;
18710 {
18711 int i, k, x;
18712 int end = s->row->used[s->area];
18713 struct glyph *glyphs = s->row->glyphs[s->area];
18714 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
18715
18716 k = -1;
18717 x = 0;
18718 for (i = first; i < end; ++i)
18719 {
18720 int left, right;
18721 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
18722 if (x - left < 0)
18723 k = i;
18724 x += glyphs[i].pixel_width;
18725 }
18726
18727 return k;
18728 }
18729
18730
18731 /* Get face and two-byte form of character C in face FACE_ID on frame
18732 F. The encoding of C is returned in *CHAR2B. MULTIBYTE_P non-zero
18733 means we want to display multibyte text. DISPLAY_P non-zero means
18734 make sure that X resources for the face returned are allocated.
18735 Value is a pointer to a realized face that is ready for display if
18736 DISPLAY_P is non-zero. */
18737
18738 static INLINE struct face *
18739 get_char_face_and_encoding (f, c, face_id, char2b, multibyte_p, display_p)
18740 struct frame *f;
18741 int c, face_id;
18742 XChar2b *char2b;
18743 int multibyte_p, display_p;
18744 {
18745 struct face *face = FACE_FROM_ID (f, face_id);
18746
18747 if (!multibyte_p)
18748 {
18749 /* Unibyte case. We don't have to encode, but we have to make
18750 sure to use a face suitable for unibyte. */
18751 STORE_XCHAR2B (char2b, 0, c);
18752 face_id = FACE_FOR_CHAR (f, face, c);
18753 face = FACE_FROM_ID (f, face_id);
18754 }
18755 else if (c < 128 && face_id < BASIC_FACE_ID_SENTINEL)
18756 {
18757 /* Case of ASCII in a face known to fit ASCII. */
18758 STORE_XCHAR2B (char2b, 0, c);
18759 }
18760 else
18761 {
18762 int c1, c2, charset;
18763
18764 /* Split characters into bytes. If c2 is -1 afterwards, C is
18765 really a one-byte character so that byte1 is zero. */
18766 SPLIT_CHAR (c, charset, c1, c2);
18767 if (c2 > 0)
18768 STORE_XCHAR2B (char2b, c1, c2);
18769 else
18770 STORE_XCHAR2B (char2b, 0, c1);
18771
18772 /* Maybe encode the character in *CHAR2B. */
18773 if (face->font != NULL)
18774 {
18775 struct font_info *font_info
18776 = FONT_INFO_FROM_ID (f, face->font_info_id);
18777 if (font_info)
18778 FRAME_RIF (f)->encode_char (c, char2b, font_info, 0);
18779 }
18780 }
18781
18782 /* Make sure X resources of the face are allocated. */
18783 #ifdef HAVE_X_WINDOWS
18784 if (display_p)
18785 #endif
18786 {
18787 xassert (face != NULL);
18788 PREPARE_FACE_FOR_DISPLAY (f, face);
18789 }
18790
18791 return face;
18792 }
18793
18794
18795 /* Set background width of glyph string S. START is the index of the
18796 first glyph following S. LAST_X is the right-most x-position + 1
18797 in the drawing area. */
18798
18799 static INLINE void
18800 set_glyph_string_background_width (s, start, last_x)
18801 struct glyph_string *s;
18802 int start;
18803 int last_x;
18804 {
18805 /* If the face of this glyph string has to be drawn to the end of
18806 the drawing area, set S->extends_to_end_of_line_p. */
18807
18808 if (start == s->row->used[s->area]
18809 && s->area == TEXT_AREA
18810 && ((s->row->fill_line_p
18811 && (s->hl == DRAW_NORMAL_TEXT
18812 || s->hl == DRAW_IMAGE_RAISED
18813 || s->hl == DRAW_IMAGE_SUNKEN))
18814 || s->hl == DRAW_MOUSE_FACE))
18815 s->extends_to_end_of_line_p = 1;
18816
18817 /* If S extends its face to the end of the line, set its
18818 background_width to the distance to the right edge of the drawing
18819 area. */
18820 if (s->extends_to_end_of_line_p)
18821 s->background_width = last_x - s->x + 1;
18822 else
18823 s->background_width = s->width;
18824 }
18825
18826
18827 /* Compute overhangs and x-positions for glyph string S and its
18828 predecessors, or successors. X is the starting x-position for S.
18829 BACKWARD_P non-zero means process predecessors. */
18830
18831 static void
18832 compute_overhangs_and_x (s, x, backward_p)
18833 struct glyph_string *s;
18834 int x;
18835 int backward_p;
18836 {
18837 if (backward_p)
18838 {
18839 while (s)
18840 {
18841 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
18842 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
18843 x -= s->width;
18844 s->x = x;
18845 s = s->prev;
18846 }
18847 }
18848 else
18849 {
18850 while (s)
18851 {
18852 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
18853 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
18854 s->x = x;
18855 x += s->width;
18856 s = s->next;
18857 }
18858 }
18859 }
18860
18861
18862
18863 /* The following macros are only called from draw_glyphs below.
18864 They reference the following parameters of that function directly:
18865 `w', `row', `area', and `overlap_p'
18866 as well as the following local variables:
18867 `s', `f', and `hdc' (in W32) */
18868
18869 #ifdef HAVE_NTGUI
18870 /* On W32, silently add local `hdc' variable to argument list of
18871 init_glyph_string. */
18872 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
18873 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
18874 #else
18875 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
18876 init_glyph_string (s, char2b, w, row, area, start, hl)
18877 #endif
18878
18879 /* Add a glyph string for a stretch glyph to the list of strings
18880 between HEAD and TAIL. START is the index of the stretch glyph in
18881 row area AREA of glyph row ROW. END is the index of the last glyph
18882 in that glyph row area. X is the current output position assigned
18883 to the new glyph string constructed. HL overrides that face of the
18884 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
18885 is the right-most x-position of the drawing area. */
18886
18887 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
18888 and below -- keep them on one line. */
18889 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
18890 do \
18891 { \
18892 s = (struct glyph_string *) alloca (sizeof *s); \
18893 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
18894 START = fill_stretch_glyph_string (s, row, area, START, END); \
18895 append_glyph_string (&HEAD, &TAIL, s); \
18896 s->x = (X); \
18897 } \
18898 while (0)
18899
18900
18901 /* Add a glyph string for an image glyph to the list of strings
18902 between HEAD and TAIL. START is the index of the image glyph in
18903 row area AREA of glyph row ROW. END is the index of the last glyph
18904 in that glyph row area. X is the current output position assigned
18905 to the new glyph string constructed. HL overrides that face of the
18906 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
18907 is the right-most x-position of the drawing area. */
18908
18909 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
18910 do \
18911 { \
18912 s = (struct glyph_string *) alloca (sizeof *s); \
18913 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
18914 fill_image_glyph_string (s); \
18915 append_glyph_string (&HEAD, &TAIL, s); \
18916 ++START; \
18917 s->x = (X); \
18918 } \
18919 while (0)
18920
18921
18922 /* Add a glyph string for a sequence of character glyphs to the list
18923 of strings between HEAD and TAIL. START is the index of the first
18924 glyph in row area AREA of glyph row ROW that is part of the new
18925 glyph string. END is the index of the last glyph in that glyph row
18926 area. X is the current output position assigned to the new glyph
18927 string constructed. HL overrides that face of the glyph; e.g. it
18928 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
18929 right-most x-position of the drawing area. */
18930
18931 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
18932 do \
18933 { \
18934 int c, face_id; \
18935 XChar2b *char2b; \
18936 \
18937 c = (row)->glyphs[area][START].u.ch; \
18938 face_id = (row)->glyphs[area][START].face_id; \
18939 \
18940 s = (struct glyph_string *) alloca (sizeof *s); \
18941 char2b = (XChar2b *) alloca ((END - START) * sizeof *char2b); \
18942 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
18943 append_glyph_string (&HEAD, &TAIL, s); \
18944 s->x = (X); \
18945 START = fill_glyph_string (s, face_id, START, END, overlaps); \
18946 } \
18947 while (0)
18948
18949
18950 /* Add a glyph string for a composite sequence to the list of strings
18951 between HEAD and TAIL. START is the index of the first glyph in
18952 row area AREA of glyph row ROW that is part of the new glyph
18953 string. END is the index of the last glyph in that glyph row area.
18954 X is the current output position assigned to the new glyph string
18955 constructed. HL overrides that face of the glyph; e.g. it is
18956 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
18957 x-position of the drawing area. */
18958
18959 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
18960 do { \
18961 int cmp_id = (row)->glyphs[area][START].u.cmp_id; \
18962 int face_id = (row)->glyphs[area][START].face_id; \
18963 struct face *base_face = FACE_FROM_ID (f, face_id); \
18964 struct composition *cmp = composition_table[cmp_id]; \
18965 int glyph_len = cmp->glyph_len; \
18966 XChar2b *char2b; \
18967 struct face **faces; \
18968 struct glyph_string *first_s = NULL; \
18969 int n; \
18970 \
18971 base_face = base_face->ascii_face; \
18972 char2b = (XChar2b *) alloca ((sizeof *char2b) * glyph_len); \
18973 faces = (struct face **) alloca ((sizeof *faces) * glyph_len); \
18974 /* At first, fill in `char2b' and `faces'. */ \
18975 for (n = 0; n < glyph_len; n++) \
18976 { \
18977 int c = COMPOSITION_GLYPH (cmp, n); \
18978 int this_face_id = FACE_FOR_CHAR (f, base_face, c); \
18979 faces[n] = FACE_FROM_ID (f, this_face_id); \
18980 get_char_face_and_encoding (f, c, this_face_id, \
18981 char2b + n, 1, 1); \
18982 } \
18983 \
18984 /* Make glyph_strings for each glyph sequence that is drawable by \
18985 the same face, and append them to HEAD/TAIL. */ \
18986 for (n = 0; n < cmp->glyph_len;) \
18987 { \
18988 s = (struct glyph_string *) alloca (sizeof *s); \
18989 INIT_GLYPH_STRING (s, char2b + n, w, row, area, START, HL); \
18990 append_glyph_string (&(HEAD), &(TAIL), s); \
18991 s->cmp = cmp; \
18992 s->gidx = n; \
18993 s->x = (X); \
18994 \
18995 if (n == 0) \
18996 first_s = s; \
18997 \
18998 n = fill_composite_glyph_string (s, faces, overlaps); \
18999 } \
19000 \
19001 ++START; \
19002 s = first_s; \
19003 } while (0)
19004
19005
19006 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
19007 of AREA of glyph row ROW on window W between indices START and END.
19008 HL overrides the face for drawing glyph strings, e.g. it is
19009 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
19010 x-positions of the drawing area.
19011
19012 This is an ugly monster macro construct because we must use alloca
19013 to allocate glyph strings (because draw_glyphs can be called
19014 asynchronously). */
19015
19016 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
19017 do \
19018 { \
19019 HEAD = TAIL = NULL; \
19020 while (START < END) \
19021 { \
19022 struct glyph *first_glyph = (row)->glyphs[area] + START; \
19023 switch (first_glyph->type) \
19024 { \
19025 case CHAR_GLYPH: \
19026 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
19027 HL, X, LAST_X); \
19028 break; \
19029 \
19030 case COMPOSITE_GLYPH: \
19031 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
19032 HL, X, LAST_X); \
19033 break; \
19034 \
19035 case STRETCH_GLYPH: \
19036 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
19037 HL, X, LAST_X); \
19038 break; \
19039 \
19040 case IMAGE_GLYPH: \
19041 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
19042 HL, X, LAST_X); \
19043 break; \
19044 \
19045 default: \
19046 abort (); \
19047 } \
19048 \
19049 set_glyph_string_background_width (s, START, LAST_X); \
19050 (X) += s->width; \
19051 } \
19052 } \
19053 while (0)
19054
19055
19056 /* Draw glyphs between START and END in AREA of ROW on window W,
19057 starting at x-position X. X is relative to AREA in W. HL is a
19058 face-override with the following meaning:
19059
19060 DRAW_NORMAL_TEXT draw normally
19061 DRAW_CURSOR draw in cursor face
19062 DRAW_MOUSE_FACE draw in mouse face.
19063 DRAW_INVERSE_VIDEO draw in mode line face
19064 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
19065 DRAW_IMAGE_RAISED draw an image with a raised relief around it
19066
19067 If OVERLAPS is non-zero, draw only the foreground of characters and
19068 clip to the physical height of ROW. Non-zero value also defines
19069 the overlapping part to be drawn:
19070
19071 OVERLAPS_PRED overlap with preceding rows
19072 OVERLAPS_SUCC overlap with succeeding rows
19073 OVERLAPS_BOTH overlap with both preceding/succeeding rows
19074 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
19075
19076 Value is the x-position reached, relative to AREA of W. */
19077
19078 static int
19079 draw_glyphs (w, x, row, area, start, end, hl, overlaps)
19080 struct window *w;
19081 int x;
19082 struct glyph_row *row;
19083 enum glyph_row_area area;
19084 int start, end;
19085 enum draw_glyphs_face hl;
19086 int overlaps;
19087 {
19088 struct glyph_string *head, *tail;
19089 struct glyph_string *s;
19090 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
19091 int last_x, area_width;
19092 int x_reached;
19093 int i, j;
19094 struct frame *f = XFRAME (WINDOW_FRAME (w));
19095 DECLARE_HDC (hdc);
19096
19097 ALLOCATE_HDC (hdc, f);
19098
19099 /* Let's rather be paranoid than getting a SEGV. */
19100 end = min (end, row->used[area]);
19101 start = max (0, start);
19102 start = min (end, start);
19103
19104 /* Translate X to frame coordinates. Set last_x to the right
19105 end of the drawing area. */
19106 if (row->full_width_p)
19107 {
19108 /* X is relative to the left edge of W, without scroll bars
19109 or fringes. */
19110 x += WINDOW_LEFT_EDGE_X (w);
19111 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
19112 }
19113 else
19114 {
19115 int area_left = window_box_left (w, area);
19116 x += area_left;
19117 area_width = window_box_width (w, area);
19118 last_x = area_left + area_width;
19119 }
19120
19121 /* Build a doubly-linked list of glyph_string structures between
19122 head and tail from what we have to draw. Note that the macro
19123 BUILD_GLYPH_STRINGS will modify its start parameter. That's
19124 the reason we use a separate variable `i'. */
19125 i = start;
19126 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
19127 if (tail)
19128 x_reached = tail->x + tail->background_width;
19129 else
19130 x_reached = x;
19131
19132 /* If there are any glyphs with lbearing < 0 or rbearing > width in
19133 the row, redraw some glyphs in front or following the glyph
19134 strings built above. */
19135 if (head && !overlaps && row->contains_overlapping_glyphs_p)
19136 {
19137 int dummy_x = 0;
19138 struct glyph_string *h, *t;
19139
19140 /* Compute overhangs for all glyph strings. */
19141 if (FRAME_RIF (f)->compute_glyph_string_overhangs)
19142 for (s = head; s; s = s->next)
19143 FRAME_RIF (f)->compute_glyph_string_overhangs (s);
19144
19145 /* Prepend glyph strings for glyphs in front of the first glyph
19146 string that are overwritten because of the first glyph
19147 string's left overhang. The background of all strings
19148 prepended must be drawn because the first glyph string
19149 draws over it. */
19150 i = left_overwritten (head);
19151 if (i >= 0)
19152 {
19153 j = i;
19154 BUILD_GLYPH_STRINGS (j, start, h, t,
19155 DRAW_NORMAL_TEXT, dummy_x, last_x);
19156 start = i;
19157 compute_overhangs_and_x (t, head->x, 1);
19158 prepend_glyph_string_lists (&head, &tail, h, t);
19159 clip_head = head;
19160 }
19161
19162 /* Prepend glyph strings for glyphs in front of the first glyph
19163 string that overwrite that glyph string because of their
19164 right overhang. For these strings, only the foreground must
19165 be drawn, because it draws over the glyph string at `head'.
19166 The background must not be drawn because this would overwrite
19167 right overhangs of preceding glyphs for which no glyph
19168 strings exist. */
19169 i = left_overwriting (head);
19170 if (i >= 0)
19171 {
19172 clip_head = head;
19173 BUILD_GLYPH_STRINGS (i, start, h, t,
19174 DRAW_NORMAL_TEXT, dummy_x, last_x);
19175 for (s = h; s; s = s->next)
19176 s->background_filled_p = 1;
19177 compute_overhangs_and_x (t, head->x, 1);
19178 prepend_glyph_string_lists (&head, &tail, h, t);
19179 }
19180
19181 /* Append glyphs strings for glyphs following the last glyph
19182 string tail that are overwritten by tail. The background of
19183 these strings has to be drawn because tail's foreground draws
19184 over it. */
19185 i = right_overwritten (tail);
19186 if (i >= 0)
19187 {
19188 BUILD_GLYPH_STRINGS (end, i, h, t,
19189 DRAW_NORMAL_TEXT, x, last_x);
19190 compute_overhangs_and_x (h, tail->x + tail->width, 0);
19191 append_glyph_string_lists (&head, &tail, h, t);
19192 clip_tail = tail;
19193 }
19194
19195 /* Append glyph strings for glyphs following the last glyph
19196 string tail that overwrite tail. The foreground of such
19197 glyphs has to be drawn because it writes into the background
19198 of tail. The background must not be drawn because it could
19199 paint over the foreground of following glyphs. */
19200 i = right_overwriting (tail);
19201 if (i >= 0)
19202 {
19203 clip_tail = tail;
19204 BUILD_GLYPH_STRINGS (end, i, h, t,
19205 DRAW_NORMAL_TEXT, x, last_x);
19206 for (s = h; s; s = s->next)
19207 s->background_filled_p = 1;
19208 compute_overhangs_and_x (h, tail->x + tail->width, 0);
19209 append_glyph_string_lists (&head, &tail, h, t);
19210 }
19211 if (clip_head || clip_tail)
19212 for (s = head; s; s = s->next)
19213 {
19214 s->clip_head = clip_head;
19215 s->clip_tail = clip_tail;
19216 }
19217 }
19218
19219 /* Draw all strings. */
19220 for (s = head; s; s = s->next)
19221 FRAME_RIF (f)->draw_glyph_string (s);
19222
19223 if (area == TEXT_AREA
19224 && !row->full_width_p
19225 /* When drawing overlapping rows, only the glyph strings'
19226 foreground is drawn, which doesn't erase a cursor
19227 completely. */
19228 && !overlaps)
19229 {
19230 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
19231 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
19232 : (tail ? tail->x + tail->background_width : x));
19233
19234 int text_left = window_box_left (w, TEXT_AREA);
19235 x0 -= text_left;
19236 x1 -= text_left;
19237
19238 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
19239 row->y, MATRIX_ROW_BOTTOM_Y (row));
19240 }
19241
19242 /* Value is the x-position up to which drawn, relative to AREA of W.
19243 This doesn't include parts drawn because of overhangs. */
19244 if (row->full_width_p)
19245 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
19246 else
19247 x_reached -= window_box_left (w, area);
19248
19249 RELEASE_HDC (hdc, f);
19250
19251 return x_reached;
19252 }
19253
19254 /* Expand row matrix if too narrow. Don't expand if area
19255 is not present. */
19256
19257 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
19258 { \
19259 if (!fonts_changed_p \
19260 && (it->glyph_row->glyphs[area] \
19261 < it->glyph_row->glyphs[area + 1])) \
19262 { \
19263 it->w->ncols_scale_factor++; \
19264 fonts_changed_p = 1; \
19265 } \
19266 }
19267
19268 /* Store one glyph for IT->char_to_display in IT->glyph_row.
19269 Called from x_produce_glyphs when IT->glyph_row is non-null. */
19270
19271 static INLINE void
19272 append_glyph (it)
19273 struct it *it;
19274 {
19275 struct glyph *glyph;
19276 enum glyph_row_area area = it->area;
19277
19278 xassert (it->glyph_row);
19279 xassert (it->char_to_display != '\n' && it->char_to_display != '\t');
19280
19281 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
19282 if (glyph < it->glyph_row->glyphs[area + 1])
19283 {
19284 glyph->charpos = CHARPOS (it->position);
19285 glyph->object = it->object;
19286 glyph->pixel_width = it->pixel_width;
19287 glyph->ascent = it->ascent;
19288 glyph->descent = it->descent;
19289 glyph->voffset = it->voffset;
19290 glyph->type = CHAR_GLYPH;
19291 glyph->multibyte_p = it->multibyte_p;
19292 glyph->left_box_line_p = it->start_of_box_run_p;
19293 glyph->right_box_line_p = it->end_of_box_run_p;
19294 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
19295 || it->phys_descent > it->descent);
19296 glyph->padding_p = 0;
19297 glyph->glyph_not_available_p = it->glyph_not_available_p;
19298 glyph->face_id = it->face_id;
19299 glyph->u.ch = it->char_to_display;
19300 glyph->slice = null_glyph_slice;
19301 glyph->font_type = FONT_TYPE_UNKNOWN;
19302 ++it->glyph_row->used[area];
19303 }
19304 else
19305 IT_EXPAND_MATRIX_WIDTH (it, area);
19306 }
19307
19308 /* Store one glyph for the composition IT->cmp_id in IT->glyph_row.
19309 Called from x_produce_glyphs when IT->glyph_row is non-null. */
19310
19311 static INLINE void
19312 append_composite_glyph (it)
19313 struct it *it;
19314 {
19315 struct glyph *glyph;
19316 enum glyph_row_area area = it->area;
19317
19318 xassert (it->glyph_row);
19319
19320 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
19321 if (glyph < it->glyph_row->glyphs[area + 1])
19322 {
19323 glyph->charpos = CHARPOS (it->position);
19324 glyph->object = it->object;
19325 glyph->pixel_width = it->pixel_width;
19326 glyph->ascent = it->ascent;
19327 glyph->descent = it->descent;
19328 glyph->voffset = it->voffset;
19329 glyph->type = COMPOSITE_GLYPH;
19330 glyph->multibyte_p = it->multibyte_p;
19331 glyph->left_box_line_p = it->start_of_box_run_p;
19332 glyph->right_box_line_p = it->end_of_box_run_p;
19333 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
19334 || it->phys_descent > it->descent);
19335 glyph->padding_p = 0;
19336 glyph->glyph_not_available_p = 0;
19337 glyph->face_id = it->face_id;
19338 glyph->u.cmp_id = it->cmp_id;
19339 glyph->slice = null_glyph_slice;
19340 glyph->font_type = FONT_TYPE_UNKNOWN;
19341 ++it->glyph_row->used[area];
19342 }
19343 else
19344 IT_EXPAND_MATRIX_WIDTH (it, area);
19345 }
19346
19347
19348 /* Change IT->ascent and IT->height according to the setting of
19349 IT->voffset. */
19350
19351 static INLINE void
19352 take_vertical_position_into_account (it)
19353 struct it *it;
19354 {
19355 if (it->voffset)
19356 {
19357 if (it->voffset < 0)
19358 /* Increase the ascent so that we can display the text higher
19359 in the line. */
19360 it->ascent -= it->voffset;
19361 else
19362 /* Increase the descent so that we can display the text lower
19363 in the line. */
19364 it->descent += it->voffset;
19365 }
19366 }
19367
19368
19369 /* Produce glyphs/get display metrics for the image IT is loaded with.
19370 See the description of struct display_iterator in dispextern.h for
19371 an overview of struct display_iterator. */
19372
19373 static void
19374 produce_image_glyph (it)
19375 struct it *it;
19376 {
19377 struct image *img;
19378 struct face *face;
19379 int glyph_ascent;
19380 struct glyph_slice slice;
19381
19382 xassert (it->what == IT_IMAGE);
19383
19384 face = FACE_FROM_ID (it->f, it->face_id);
19385 xassert (face);
19386 /* Make sure X resources of the face is loaded. */
19387 PREPARE_FACE_FOR_DISPLAY (it->f, face);
19388
19389 if (it->image_id < 0)
19390 {
19391 /* Fringe bitmap. */
19392 it->ascent = it->phys_ascent = 0;
19393 it->descent = it->phys_descent = 0;
19394 it->pixel_width = 0;
19395 it->nglyphs = 0;
19396 return;
19397 }
19398
19399 img = IMAGE_FROM_ID (it->f, it->image_id);
19400 xassert (img);
19401 /* Make sure X resources of the image is loaded. */
19402 prepare_image_for_display (it->f, img);
19403
19404 slice.x = slice.y = 0;
19405 slice.width = img->width;
19406 slice.height = img->height;
19407
19408 if (INTEGERP (it->slice.x))
19409 slice.x = XINT (it->slice.x);
19410 else if (FLOATP (it->slice.x))
19411 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
19412
19413 if (INTEGERP (it->slice.y))
19414 slice.y = XINT (it->slice.y);
19415 else if (FLOATP (it->slice.y))
19416 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
19417
19418 if (INTEGERP (it->slice.width))
19419 slice.width = XINT (it->slice.width);
19420 else if (FLOATP (it->slice.width))
19421 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
19422
19423 if (INTEGERP (it->slice.height))
19424 slice.height = XINT (it->slice.height);
19425 else if (FLOATP (it->slice.height))
19426 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
19427
19428 if (slice.x >= img->width)
19429 slice.x = img->width;
19430 if (slice.y >= img->height)
19431 slice.y = img->height;
19432 if (slice.x + slice.width >= img->width)
19433 slice.width = img->width - slice.x;
19434 if (slice.y + slice.height > img->height)
19435 slice.height = img->height - slice.y;
19436
19437 if (slice.width == 0 || slice.height == 0)
19438 return;
19439
19440 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
19441
19442 it->descent = slice.height - glyph_ascent;
19443 if (slice.y == 0)
19444 it->descent += img->vmargin;
19445 if (slice.y + slice.height == img->height)
19446 it->descent += img->vmargin;
19447 it->phys_descent = it->descent;
19448
19449 it->pixel_width = slice.width;
19450 if (slice.x == 0)
19451 it->pixel_width += img->hmargin;
19452 if (slice.x + slice.width == img->width)
19453 it->pixel_width += img->hmargin;
19454
19455 /* It's quite possible for images to have an ascent greater than
19456 their height, so don't get confused in that case. */
19457 if (it->descent < 0)
19458 it->descent = 0;
19459
19460 #if 0 /* this breaks image tiling */
19461 /* If this glyph is alone on the last line, adjust it.ascent to minimum row ascent. */
19462 int face_ascent = face->font ? FONT_BASE (face->font) : FRAME_BASELINE_OFFSET (it->f);
19463 if (face_ascent > it->ascent)
19464 it->ascent = it->phys_ascent = face_ascent;
19465 #endif
19466
19467 it->nglyphs = 1;
19468
19469 if (face->box != FACE_NO_BOX)
19470 {
19471 if (face->box_line_width > 0)
19472 {
19473 if (slice.y == 0)
19474 it->ascent += face->box_line_width;
19475 if (slice.y + slice.height == img->height)
19476 it->descent += face->box_line_width;
19477 }
19478
19479 if (it->start_of_box_run_p && slice.x == 0)
19480 it->pixel_width += abs (face->box_line_width);
19481 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
19482 it->pixel_width += abs (face->box_line_width);
19483 }
19484
19485 take_vertical_position_into_account (it);
19486
19487 if (it->glyph_row)
19488 {
19489 struct glyph *glyph;
19490 enum glyph_row_area area = it->area;
19491
19492 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
19493 if (glyph < it->glyph_row->glyphs[area + 1])
19494 {
19495 glyph->charpos = CHARPOS (it->position);
19496 glyph->object = it->object;
19497 glyph->pixel_width = it->pixel_width;
19498 glyph->ascent = glyph_ascent;
19499 glyph->descent = it->descent;
19500 glyph->voffset = it->voffset;
19501 glyph->type = IMAGE_GLYPH;
19502 glyph->multibyte_p = it->multibyte_p;
19503 glyph->left_box_line_p = it->start_of_box_run_p;
19504 glyph->right_box_line_p = it->end_of_box_run_p;
19505 glyph->overlaps_vertically_p = 0;
19506 glyph->padding_p = 0;
19507 glyph->glyph_not_available_p = 0;
19508 glyph->face_id = it->face_id;
19509 glyph->u.img_id = img->id;
19510 glyph->slice = slice;
19511 glyph->font_type = FONT_TYPE_UNKNOWN;
19512 ++it->glyph_row->used[area];
19513 }
19514 else
19515 IT_EXPAND_MATRIX_WIDTH (it, area);
19516 }
19517 }
19518
19519
19520 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
19521 of the glyph, WIDTH and HEIGHT are the width and height of the
19522 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
19523
19524 static void
19525 append_stretch_glyph (it, object, width, height, ascent)
19526 struct it *it;
19527 Lisp_Object object;
19528 int width, height;
19529 int ascent;
19530 {
19531 struct glyph *glyph;
19532 enum glyph_row_area area = it->area;
19533
19534 xassert (ascent >= 0 && ascent <= height);
19535
19536 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
19537 if (glyph < it->glyph_row->glyphs[area + 1])
19538 {
19539 glyph->charpos = CHARPOS (it->position);
19540 glyph->object = object;
19541 glyph->pixel_width = width;
19542 glyph->ascent = ascent;
19543 glyph->descent = height - ascent;
19544 glyph->voffset = it->voffset;
19545 glyph->type = STRETCH_GLYPH;
19546 glyph->multibyte_p = it->multibyte_p;
19547 glyph->left_box_line_p = it->start_of_box_run_p;
19548 glyph->right_box_line_p = it->end_of_box_run_p;
19549 glyph->overlaps_vertically_p = 0;
19550 glyph->padding_p = 0;
19551 glyph->glyph_not_available_p = 0;
19552 glyph->face_id = it->face_id;
19553 glyph->u.stretch.ascent = ascent;
19554 glyph->u.stretch.height = height;
19555 glyph->slice = null_glyph_slice;
19556 glyph->font_type = FONT_TYPE_UNKNOWN;
19557 ++it->glyph_row->used[area];
19558 }
19559 else
19560 IT_EXPAND_MATRIX_WIDTH (it, area);
19561 }
19562
19563
19564 /* Produce a stretch glyph for iterator IT. IT->object is the value
19565 of the glyph property displayed. The value must be a list
19566 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
19567 being recognized:
19568
19569 1. `:width WIDTH' specifies that the space should be WIDTH *
19570 canonical char width wide. WIDTH may be an integer or floating
19571 point number.
19572
19573 2. `:relative-width FACTOR' specifies that the width of the stretch
19574 should be computed from the width of the first character having the
19575 `glyph' property, and should be FACTOR times that width.
19576
19577 3. `:align-to HPOS' specifies that the space should be wide enough
19578 to reach HPOS, a value in canonical character units.
19579
19580 Exactly one of the above pairs must be present.
19581
19582 4. `:height HEIGHT' specifies that the height of the stretch produced
19583 should be HEIGHT, measured in canonical character units.
19584
19585 5. `:relative-height FACTOR' specifies that the height of the
19586 stretch should be FACTOR times the height of the characters having
19587 the glyph property.
19588
19589 Either none or exactly one of 4 or 5 must be present.
19590
19591 6. `:ascent ASCENT' specifies that ASCENT percent of the height
19592 of the stretch should be used for the ascent of the stretch.
19593 ASCENT must be in the range 0 <= ASCENT <= 100. */
19594
19595 static void
19596 produce_stretch_glyph (it)
19597 struct it *it;
19598 {
19599 /* (space :width WIDTH :height HEIGHT ...) */
19600 Lisp_Object prop, plist;
19601 int width = 0, height = 0, align_to = -1;
19602 int zero_width_ok_p = 0, zero_height_ok_p = 0;
19603 int ascent = 0;
19604 double tem;
19605 struct face *face = FACE_FROM_ID (it->f, it->face_id);
19606 XFontStruct *font = face->font ? face->font : FRAME_FONT (it->f);
19607
19608 PREPARE_FACE_FOR_DISPLAY (it->f, face);
19609
19610 /* List should start with `space'. */
19611 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
19612 plist = XCDR (it->object);
19613
19614 /* Compute the width of the stretch. */
19615 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
19616 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
19617 {
19618 /* Absolute width `:width WIDTH' specified and valid. */
19619 zero_width_ok_p = 1;
19620 width = (int)tem;
19621 }
19622 else if (prop = Fplist_get (plist, QCrelative_width),
19623 NUMVAL (prop) > 0)
19624 {
19625 /* Relative width `:relative-width FACTOR' specified and valid.
19626 Compute the width of the characters having the `glyph'
19627 property. */
19628 struct it it2;
19629 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
19630
19631 it2 = *it;
19632 if (it->multibyte_p)
19633 {
19634 int maxlen = ((IT_BYTEPOS (*it) >= GPT ? ZV : GPT)
19635 - IT_BYTEPOS (*it));
19636 it2.c = STRING_CHAR_AND_LENGTH (p, maxlen, it2.len);
19637 }
19638 else
19639 it2.c = *p, it2.len = 1;
19640
19641 it2.glyph_row = NULL;
19642 it2.what = IT_CHARACTER;
19643 x_produce_glyphs (&it2);
19644 width = NUMVAL (prop) * it2.pixel_width;
19645 }
19646 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
19647 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
19648 {
19649 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
19650 align_to = (align_to < 0
19651 ? 0
19652 : align_to - window_box_left_offset (it->w, TEXT_AREA));
19653 else if (align_to < 0)
19654 align_to = window_box_left_offset (it->w, TEXT_AREA);
19655 width = max (0, (int)tem + align_to - it->current_x);
19656 zero_width_ok_p = 1;
19657 }
19658 else
19659 /* Nothing specified -> width defaults to canonical char width. */
19660 width = FRAME_COLUMN_WIDTH (it->f);
19661
19662 if (width <= 0 && (width < 0 || !zero_width_ok_p))
19663 width = 1;
19664
19665 /* Compute height. */
19666 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
19667 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
19668 {
19669 height = (int)tem;
19670 zero_height_ok_p = 1;
19671 }
19672 else if (prop = Fplist_get (plist, QCrelative_height),
19673 NUMVAL (prop) > 0)
19674 height = FONT_HEIGHT (font) * NUMVAL (prop);
19675 else
19676 height = FONT_HEIGHT (font);
19677
19678 if (height <= 0 && (height < 0 || !zero_height_ok_p))
19679 height = 1;
19680
19681 /* Compute percentage of height used for ascent. If
19682 `:ascent ASCENT' is present and valid, use that. Otherwise,
19683 derive the ascent from the font in use. */
19684 if (prop = Fplist_get (plist, QCascent),
19685 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
19686 ascent = height * NUMVAL (prop) / 100.0;
19687 else if (!NILP (prop)
19688 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
19689 ascent = min (max (0, (int)tem), height);
19690 else
19691 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
19692
19693 if (width > 0 && height > 0 && it->glyph_row)
19694 {
19695 Lisp_Object object = it->stack[it->sp - 1].string;
19696 if (!STRINGP (object))
19697 object = it->w->buffer;
19698 append_stretch_glyph (it, object, width, height, ascent);
19699 }
19700
19701 it->pixel_width = width;
19702 it->ascent = it->phys_ascent = ascent;
19703 it->descent = it->phys_descent = height - it->ascent;
19704 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
19705
19706 if (width > 0 && height > 0 && face->box != FACE_NO_BOX)
19707 {
19708 if (face->box_line_width > 0)
19709 {
19710 it->ascent += face->box_line_width;
19711 it->descent += face->box_line_width;
19712 }
19713
19714 if (it->start_of_box_run_p)
19715 it->pixel_width += abs (face->box_line_width);
19716 if (it->end_of_box_run_p)
19717 it->pixel_width += abs (face->box_line_width);
19718 }
19719
19720 take_vertical_position_into_account (it);
19721 }
19722
19723 /* Get line-height and line-spacing property at point.
19724 If line-height has format (HEIGHT TOTAL), return TOTAL
19725 in TOTAL_HEIGHT. */
19726
19727 static Lisp_Object
19728 get_line_height_property (it, prop)
19729 struct it *it;
19730 Lisp_Object prop;
19731 {
19732 Lisp_Object position;
19733
19734 if (STRINGP (it->object))
19735 position = make_number (IT_STRING_CHARPOS (*it));
19736 else if (BUFFERP (it->object))
19737 position = make_number (IT_CHARPOS (*it));
19738 else
19739 return Qnil;
19740
19741 return Fget_char_property (position, prop, it->object);
19742 }
19743
19744 /* Calculate line-height and line-spacing properties.
19745 An integer value specifies explicit pixel value.
19746 A float value specifies relative value to current face height.
19747 A cons (float . face-name) specifies relative value to
19748 height of specified face font.
19749
19750 Returns height in pixels, or nil. */
19751
19752
19753 static Lisp_Object
19754 calc_line_height_property (it, val, font, boff, override)
19755 struct it *it;
19756 Lisp_Object val;
19757 XFontStruct *font;
19758 int boff, override;
19759 {
19760 Lisp_Object face_name = Qnil;
19761 int ascent, descent, height;
19762
19763 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
19764 return val;
19765
19766 if (CONSP (val))
19767 {
19768 face_name = XCAR (val);
19769 val = XCDR (val);
19770 if (!NUMBERP (val))
19771 val = make_number (1);
19772 if (NILP (face_name))
19773 {
19774 height = it->ascent + it->descent;
19775 goto scale;
19776 }
19777 }
19778
19779 if (NILP (face_name))
19780 {
19781 font = FRAME_FONT (it->f);
19782 boff = FRAME_BASELINE_OFFSET (it->f);
19783 }
19784 else if (EQ (face_name, Qt))
19785 {
19786 override = 0;
19787 }
19788 else
19789 {
19790 int face_id;
19791 struct face *face;
19792 struct font_info *font_info;
19793
19794 face_id = lookup_named_face (it->f, face_name, ' ', 0);
19795 if (face_id < 0)
19796 return make_number (-1);
19797
19798 face = FACE_FROM_ID (it->f, face_id);
19799 font = face->font;
19800 if (font == NULL)
19801 return make_number (-1);
19802
19803 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
19804 boff = font_info->baseline_offset;
19805 if (font_info->vertical_centering)
19806 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
19807 }
19808
19809 ascent = FONT_BASE (font) + boff;
19810 descent = FONT_DESCENT (font) - boff;
19811
19812 if (override)
19813 {
19814 it->override_ascent = ascent;
19815 it->override_descent = descent;
19816 it->override_boff = boff;
19817 }
19818
19819 height = ascent + descent;
19820
19821 scale:
19822 if (FLOATP (val))
19823 height = (int)(XFLOAT_DATA (val) * height);
19824 else if (INTEGERP (val))
19825 height *= XINT (val);
19826
19827 return make_number (height);
19828 }
19829
19830
19831 /* RIF:
19832 Produce glyphs/get display metrics for the display element IT is
19833 loaded with. See the description of struct display_iterator in
19834 dispextern.h for an overview of struct display_iterator. */
19835
19836 void
19837 x_produce_glyphs (it)
19838 struct it *it;
19839 {
19840 int extra_line_spacing = it->extra_line_spacing;
19841
19842 it->glyph_not_available_p = 0;
19843
19844 if (it->what == IT_CHARACTER)
19845 {
19846 XChar2b char2b;
19847 XFontStruct *font;
19848 struct face *face = FACE_FROM_ID (it->f, it->face_id);
19849 XCharStruct *pcm;
19850 int font_not_found_p;
19851 struct font_info *font_info;
19852 int boff; /* baseline offset */
19853 /* We may change it->multibyte_p upon unibyte<->multibyte
19854 conversion. So, save the current value now and restore it
19855 later.
19856
19857 Note: It seems that we don't have to record multibyte_p in
19858 struct glyph because the character code itself tells if or
19859 not the character is multibyte. Thus, in the future, we must
19860 consider eliminating the field `multibyte_p' in the struct
19861 glyph. */
19862 int saved_multibyte_p = it->multibyte_p;
19863
19864 /* Maybe translate single-byte characters to multibyte, or the
19865 other way. */
19866 it->char_to_display = it->c;
19867 if (!ASCII_BYTE_P (it->c))
19868 {
19869 if (unibyte_display_via_language_environment
19870 && SINGLE_BYTE_CHAR_P (it->c)
19871 && (it->c >= 0240
19872 || !NILP (Vnonascii_translation_table)))
19873 {
19874 it->char_to_display = unibyte_char_to_multibyte (it->c);
19875 it->multibyte_p = 1;
19876 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
19877 face = FACE_FROM_ID (it->f, it->face_id);
19878 }
19879 else if (!SINGLE_BYTE_CHAR_P (it->c)
19880 && !it->multibyte_p)
19881 {
19882 it->multibyte_p = 1;
19883 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
19884 face = FACE_FROM_ID (it->f, it->face_id);
19885 }
19886 }
19887
19888 /* Get font to use. Encode IT->char_to_display. */
19889 get_char_face_and_encoding (it->f, it->char_to_display, it->face_id,
19890 &char2b, it->multibyte_p, 0);
19891 font = face->font;
19892
19893 /* When no suitable font found, use the default font. */
19894 font_not_found_p = font == NULL;
19895 if (font_not_found_p)
19896 {
19897 font = FRAME_FONT (it->f);
19898 boff = FRAME_BASELINE_OFFSET (it->f);
19899 font_info = NULL;
19900 }
19901 else
19902 {
19903 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
19904 boff = font_info->baseline_offset;
19905 if (font_info->vertical_centering)
19906 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
19907 }
19908
19909 if (it->char_to_display >= ' '
19910 && (!it->multibyte_p || it->char_to_display < 128))
19911 {
19912 /* Either unibyte or ASCII. */
19913 int stretched_p;
19914
19915 it->nglyphs = 1;
19916
19917 pcm = FRAME_RIF (it->f)->per_char_metric
19918 (font, &char2b, FONT_TYPE_FOR_UNIBYTE (font, it->char_to_display));
19919
19920 if (it->override_ascent >= 0)
19921 {
19922 it->ascent = it->override_ascent;
19923 it->descent = it->override_descent;
19924 boff = it->override_boff;
19925 }
19926 else
19927 {
19928 it->ascent = FONT_BASE (font) + boff;
19929 it->descent = FONT_DESCENT (font) - boff;
19930 }
19931
19932 if (pcm)
19933 {
19934 it->phys_ascent = pcm->ascent + boff;
19935 it->phys_descent = pcm->descent - boff;
19936 it->pixel_width = pcm->width;
19937 }
19938 else
19939 {
19940 it->glyph_not_available_p = 1;
19941 it->phys_ascent = it->ascent;
19942 it->phys_descent = it->descent;
19943 it->pixel_width = FONT_WIDTH (font);
19944 }
19945
19946 if (it->constrain_row_ascent_descent_p)
19947 {
19948 if (it->descent > it->max_descent)
19949 {
19950 it->ascent += it->descent - it->max_descent;
19951 it->descent = it->max_descent;
19952 }
19953 if (it->ascent > it->max_ascent)
19954 {
19955 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
19956 it->ascent = it->max_ascent;
19957 }
19958 it->phys_ascent = min (it->phys_ascent, it->ascent);
19959 it->phys_descent = min (it->phys_descent, it->descent);
19960 extra_line_spacing = 0;
19961 }
19962
19963 /* If this is a space inside a region of text with
19964 `space-width' property, change its width. */
19965 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
19966 if (stretched_p)
19967 it->pixel_width *= XFLOATINT (it->space_width);
19968
19969 /* If face has a box, add the box thickness to the character
19970 height. If character has a box line to the left and/or
19971 right, add the box line width to the character's width. */
19972 if (face->box != FACE_NO_BOX)
19973 {
19974 int thick = face->box_line_width;
19975
19976 if (thick > 0)
19977 {
19978 it->ascent += thick;
19979 it->descent += thick;
19980 }
19981 else
19982 thick = -thick;
19983
19984 if (it->start_of_box_run_p)
19985 it->pixel_width += thick;
19986 if (it->end_of_box_run_p)
19987 it->pixel_width += thick;
19988 }
19989
19990 /* If face has an overline, add the height of the overline
19991 (1 pixel) and a 1 pixel margin to the character height. */
19992 if (face->overline_p)
19993 it->ascent += 2;
19994
19995 if (it->constrain_row_ascent_descent_p)
19996 {
19997 if (it->ascent > it->max_ascent)
19998 it->ascent = it->max_ascent;
19999 if (it->descent > it->max_descent)
20000 it->descent = it->max_descent;
20001 }
20002
20003 take_vertical_position_into_account (it);
20004
20005 /* If we have to actually produce glyphs, do it. */
20006 if (it->glyph_row)
20007 {
20008 if (stretched_p)
20009 {
20010 /* Translate a space with a `space-width' property
20011 into a stretch glyph. */
20012 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
20013 / FONT_HEIGHT (font));
20014 append_stretch_glyph (it, it->object, it->pixel_width,
20015 it->ascent + it->descent, ascent);
20016 }
20017 else
20018 append_glyph (it);
20019
20020 /* If characters with lbearing or rbearing are displayed
20021 in this line, record that fact in a flag of the
20022 glyph row. This is used to optimize X output code. */
20023 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
20024 it->glyph_row->contains_overlapping_glyphs_p = 1;
20025 }
20026 }
20027 else if (it->char_to_display == '\n')
20028 {
20029 /* A newline has no width but we need the height of the line.
20030 But if previous part of the line set a height, don't
20031 increase that height */
20032
20033 Lisp_Object height;
20034 Lisp_Object total_height = Qnil;
20035
20036 it->override_ascent = -1;
20037 it->pixel_width = 0;
20038 it->nglyphs = 0;
20039
20040 height = get_line_height_property(it, Qline_height);
20041 /* Split (line-height total-height) list */
20042 if (CONSP (height)
20043 && CONSP (XCDR (height))
20044 && NILP (XCDR (XCDR (height))))
20045 {
20046 total_height = XCAR (XCDR (height));
20047 height = XCAR (height);
20048 }
20049 height = calc_line_height_property(it, height, font, boff, 1);
20050
20051 if (it->override_ascent >= 0)
20052 {
20053 it->ascent = it->override_ascent;
20054 it->descent = it->override_descent;
20055 boff = it->override_boff;
20056 }
20057 else
20058 {
20059 it->ascent = FONT_BASE (font) + boff;
20060 it->descent = FONT_DESCENT (font) - boff;
20061 }
20062
20063 if (EQ (height, Qt))
20064 {
20065 if (it->descent > it->max_descent)
20066 {
20067 it->ascent += it->descent - it->max_descent;
20068 it->descent = it->max_descent;
20069 }
20070 if (it->ascent > it->max_ascent)
20071 {
20072 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
20073 it->ascent = it->max_ascent;
20074 }
20075 it->phys_ascent = min (it->phys_ascent, it->ascent);
20076 it->phys_descent = min (it->phys_descent, it->descent);
20077 it->constrain_row_ascent_descent_p = 1;
20078 extra_line_spacing = 0;
20079 }
20080 else
20081 {
20082 Lisp_Object spacing;
20083
20084 it->phys_ascent = it->ascent;
20085 it->phys_descent = it->descent;
20086
20087 if ((it->max_ascent > 0 || it->max_descent > 0)
20088 && face->box != FACE_NO_BOX
20089 && face->box_line_width > 0)
20090 {
20091 it->ascent += face->box_line_width;
20092 it->descent += face->box_line_width;
20093 }
20094 if (!NILP (height)
20095 && XINT (height) > it->ascent + it->descent)
20096 it->ascent = XINT (height) - it->descent;
20097
20098 if (!NILP (total_height))
20099 spacing = calc_line_height_property(it, total_height, font, boff, 0);
20100 else
20101 {
20102 spacing = get_line_height_property(it, Qline_spacing);
20103 spacing = calc_line_height_property(it, spacing, font, boff, 0);
20104 }
20105 if (INTEGERP (spacing))
20106 {
20107 extra_line_spacing = XINT (spacing);
20108 if (!NILP (total_height))
20109 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
20110 }
20111 }
20112 }
20113 else if (it->char_to_display == '\t')
20114 {
20115 int tab_width = it->tab_width * FRAME_SPACE_WIDTH (it->f);
20116 int x = it->current_x + it->continuation_lines_width;
20117 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
20118
20119 /* If the distance from the current position to the next tab
20120 stop is less than a space character width, use the
20121 tab stop after that. */
20122 if (next_tab_x - x < FRAME_SPACE_WIDTH (it->f))
20123 next_tab_x += tab_width;
20124
20125 it->pixel_width = next_tab_x - x;
20126 it->nglyphs = 1;
20127 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
20128 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
20129
20130 if (it->glyph_row)
20131 {
20132 append_stretch_glyph (it, it->object, it->pixel_width,
20133 it->ascent + it->descent, it->ascent);
20134 }
20135 }
20136 else
20137 {
20138 /* A multi-byte character. Assume that the display width of the
20139 character is the width of the character multiplied by the
20140 width of the font. */
20141
20142 /* If we found a font, this font should give us the right
20143 metrics. If we didn't find a font, use the frame's
20144 default font and calculate the width of the character
20145 from the charset width; this is what old redisplay code
20146 did. */
20147
20148 pcm = FRAME_RIF (it->f)->per_char_metric (font, &char2b,
20149 FONT_TYPE_FOR_MULTIBYTE (font, it->c));
20150
20151 if (font_not_found_p || !pcm)
20152 {
20153 int charset = CHAR_CHARSET (it->char_to_display);
20154
20155 it->glyph_not_available_p = 1;
20156 it->pixel_width = (FRAME_COLUMN_WIDTH (it->f)
20157 * CHARSET_WIDTH (charset));
20158 it->phys_ascent = FONT_BASE (font) + boff;
20159 it->phys_descent = FONT_DESCENT (font) - boff;
20160 }
20161 else
20162 {
20163 it->pixel_width = pcm->width;
20164 it->phys_ascent = pcm->ascent + boff;
20165 it->phys_descent = pcm->descent - boff;
20166 if (it->glyph_row
20167 && (pcm->lbearing < 0
20168 || pcm->rbearing > pcm->width))
20169 it->glyph_row->contains_overlapping_glyphs_p = 1;
20170 }
20171 it->nglyphs = 1;
20172 it->ascent = FONT_BASE (font) + boff;
20173 it->descent = FONT_DESCENT (font) - boff;
20174 if (face->box != FACE_NO_BOX)
20175 {
20176 int thick = face->box_line_width;
20177
20178 if (thick > 0)
20179 {
20180 it->ascent += thick;
20181 it->descent += thick;
20182 }
20183 else
20184 thick = - thick;
20185
20186 if (it->start_of_box_run_p)
20187 it->pixel_width += thick;
20188 if (it->end_of_box_run_p)
20189 it->pixel_width += thick;
20190 }
20191
20192 /* If face has an overline, add the height of the overline
20193 (1 pixel) and a 1 pixel margin to the character height. */
20194 if (face->overline_p)
20195 it->ascent += 2;
20196
20197 take_vertical_position_into_account (it);
20198
20199 if (it->glyph_row)
20200 append_glyph (it);
20201 }
20202 it->multibyte_p = saved_multibyte_p;
20203 }
20204 else if (it->what == IT_COMPOSITION)
20205 {
20206 /* Note: A composition is represented as one glyph in the
20207 glyph matrix. There are no padding glyphs. */
20208 XChar2b char2b;
20209 XFontStruct *font;
20210 struct face *face = FACE_FROM_ID (it->f, it->face_id);
20211 XCharStruct *pcm;
20212 int font_not_found_p;
20213 struct font_info *font_info;
20214 int boff; /* baseline offset */
20215 struct composition *cmp = composition_table[it->cmp_id];
20216
20217 /* Maybe translate single-byte characters to multibyte. */
20218 it->char_to_display = it->c;
20219 if (unibyte_display_via_language_environment
20220 && SINGLE_BYTE_CHAR_P (it->c)
20221 && (it->c >= 0240
20222 || (it->c >= 0200
20223 && !NILP (Vnonascii_translation_table))))
20224 {
20225 it->char_to_display = unibyte_char_to_multibyte (it->c);
20226 }
20227
20228 /* Get face and font to use. Encode IT->char_to_display. */
20229 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
20230 face = FACE_FROM_ID (it->f, it->face_id);
20231 get_char_face_and_encoding (it->f, it->char_to_display, it->face_id,
20232 &char2b, it->multibyte_p, 0);
20233 font = face->font;
20234
20235 /* When no suitable font found, use the default font. */
20236 font_not_found_p = font == NULL;
20237 if (font_not_found_p)
20238 {
20239 font = FRAME_FONT (it->f);
20240 boff = FRAME_BASELINE_OFFSET (it->f);
20241 font_info = NULL;
20242 }
20243 else
20244 {
20245 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
20246 boff = font_info->baseline_offset;
20247 if (font_info->vertical_centering)
20248 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
20249 }
20250
20251 /* There are no padding glyphs, so there is only one glyph to
20252 produce for the composition. Important is that pixel_width,
20253 ascent and descent are the values of what is drawn by
20254 draw_glyphs (i.e. the values of the overall glyphs composed). */
20255 it->nglyphs = 1;
20256
20257 /* If we have not yet calculated pixel size data of glyphs of
20258 the composition for the current face font, calculate them
20259 now. Theoretically, we have to check all fonts for the
20260 glyphs, but that requires much time and memory space. So,
20261 here we check only the font of the first glyph. This leads
20262 to incorrect display very rarely, and C-l (recenter) can
20263 correct the display anyway. */
20264 if (cmp->font != (void *) font)
20265 {
20266 /* Ascent and descent of the font of the first character of
20267 this composition (adjusted by baseline offset). Ascent
20268 and descent of overall glyphs should not be less than
20269 them respectively. */
20270 int font_ascent = FONT_BASE (font) + boff;
20271 int font_descent = FONT_DESCENT (font) - boff;
20272 /* Bounding box of the overall glyphs. */
20273 int leftmost, rightmost, lowest, highest;
20274 int i, width, ascent, descent;
20275
20276 cmp->font = (void *) font;
20277
20278 /* Initialize the bounding box. */
20279 if (font_info
20280 && (pcm = FRAME_RIF (it->f)->per_char_metric (font, &char2b,
20281 FONT_TYPE_FOR_MULTIBYTE (font, it->c))))
20282 {
20283 width = pcm->width;
20284 ascent = pcm->ascent;
20285 descent = pcm->descent;
20286 }
20287 else
20288 {
20289 width = FONT_WIDTH (font);
20290 ascent = FONT_BASE (font);
20291 descent = FONT_DESCENT (font);
20292 }
20293
20294 rightmost = width;
20295 lowest = - descent + boff;
20296 highest = ascent + boff;
20297 leftmost = 0;
20298
20299 if (font_info
20300 && font_info->default_ascent
20301 && CHAR_TABLE_P (Vuse_default_ascent)
20302 && !NILP (Faref (Vuse_default_ascent,
20303 make_number (it->char_to_display))))
20304 highest = font_info->default_ascent + boff;
20305
20306 /* Draw the first glyph at the normal position. It may be
20307 shifted to right later if some other glyphs are drawn at
20308 the left. */
20309 cmp->offsets[0] = 0;
20310 cmp->offsets[1] = boff;
20311
20312 /* Set cmp->offsets for the remaining glyphs. */
20313 for (i = 1; i < cmp->glyph_len; i++)
20314 {
20315 int left, right, btm, top;
20316 int ch = COMPOSITION_GLYPH (cmp, i);
20317 int face_id = FACE_FOR_CHAR (it->f, face, ch);
20318
20319 face = FACE_FROM_ID (it->f, face_id);
20320 get_char_face_and_encoding (it->f, ch, face->id,
20321 &char2b, it->multibyte_p, 0);
20322 font = face->font;
20323 if (font == NULL)
20324 {
20325 font = FRAME_FONT (it->f);
20326 boff = FRAME_BASELINE_OFFSET (it->f);
20327 font_info = NULL;
20328 }
20329 else
20330 {
20331 font_info
20332 = FONT_INFO_FROM_ID (it->f, face->font_info_id);
20333 boff = font_info->baseline_offset;
20334 if (font_info->vertical_centering)
20335 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
20336 }
20337
20338 if (font_info
20339 && (pcm = FRAME_RIF (it->f)->per_char_metric (font, &char2b,
20340 FONT_TYPE_FOR_MULTIBYTE (font, ch))))
20341 {
20342 width = pcm->width;
20343 ascent = pcm->ascent;
20344 descent = pcm->descent;
20345 }
20346 else
20347 {
20348 width = FONT_WIDTH (font);
20349 ascent = 1;
20350 descent = 0;
20351 }
20352
20353 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
20354 {
20355 /* Relative composition with or without
20356 alternate chars. */
20357 left = (leftmost + rightmost - width) / 2;
20358 btm = - descent + boff;
20359 if (font_info && font_info->relative_compose
20360 && (! CHAR_TABLE_P (Vignore_relative_composition)
20361 || NILP (Faref (Vignore_relative_composition,
20362 make_number (ch)))))
20363 {
20364
20365 if (- descent >= font_info->relative_compose)
20366 /* One extra pixel between two glyphs. */
20367 btm = highest + 1;
20368 else if (ascent <= 0)
20369 /* One extra pixel between two glyphs. */
20370 btm = lowest - 1 - ascent - descent;
20371 }
20372 }
20373 else
20374 {
20375 /* A composition rule is specified by an integer
20376 value that encodes global and new reference
20377 points (GREF and NREF). GREF and NREF are
20378 specified by numbers as below:
20379
20380 0---1---2 -- ascent
20381 | |
20382 | |
20383 | |
20384 9--10--11 -- center
20385 | |
20386 ---3---4---5--- baseline
20387 | |
20388 6---7---8 -- descent
20389 */
20390 int rule = COMPOSITION_RULE (cmp, i);
20391 int gref, nref, grefx, grefy, nrefx, nrefy;
20392
20393 COMPOSITION_DECODE_RULE (rule, gref, nref);
20394 grefx = gref % 3, nrefx = nref % 3;
20395 grefy = gref / 3, nrefy = nref / 3;
20396
20397 left = (leftmost
20398 + grefx * (rightmost - leftmost) / 2
20399 - nrefx * width / 2);
20400 btm = ((grefy == 0 ? highest
20401 : grefy == 1 ? 0
20402 : grefy == 2 ? lowest
20403 : (highest + lowest) / 2)
20404 - (nrefy == 0 ? ascent + descent
20405 : nrefy == 1 ? descent - boff
20406 : nrefy == 2 ? 0
20407 : (ascent + descent) / 2));
20408 }
20409
20410 cmp->offsets[i * 2] = left;
20411 cmp->offsets[i * 2 + 1] = btm + descent;
20412
20413 /* Update the bounding box of the overall glyphs. */
20414 right = left + width;
20415 top = btm + descent + ascent;
20416 if (left < leftmost)
20417 leftmost = left;
20418 if (right > rightmost)
20419 rightmost = right;
20420 if (top > highest)
20421 highest = top;
20422 if (btm < lowest)
20423 lowest = btm;
20424 }
20425
20426 /* If there are glyphs whose x-offsets are negative,
20427 shift all glyphs to the right and make all x-offsets
20428 non-negative. */
20429 if (leftmost < 0)
20430 {
20431 for (i = 0; i < cmp->glyph_len; i++)
20432 cmp->offsets[i * 2] -= leftmost;
20433 rightmost -= leftmost;
20434 }
20435
20436 cmp->pixel_width = rightmost;
20437 cmp->ascent = highest;
20438 cmp->descent = - lowest;
20439 if (cmp->ascent < font_ascent)
20440 cmp->ascent = font_ascent;
20441 if (cmp->descent < font_descent)
20442 cmp->descent = font_descent;
20443 }
20444
20445 it->pixel_width = cmp->pixel_width;
20446 it->ascent = it->phys_ascent = cmp->ascent;
20447 it->descent = it->phys_descent = cmp->descent;
20448
20449 if (face->box != FACE_NO_BOX)
20450 {
20451 int thick = face->box_line_width;
20452
20453 if (thick > 0)
20454 {
20455 it->ascent += thick;
20456 it->descent += thick;
20457 }
20458 else
20459 thick = - thick;
20460
20461 if (it->start_of_box_run_p)
20462 it->pixel_width += thick;
20463 if (it->end_of_box_run_p)
20464 it->pixel_width += thick;
20465 }
20466
20467 /* If face has an overline, add the height of the overline
20468 (1 pixel) and a 1 pixel margin to the character height. */
20469 if (face->overline_p)
20470 it->ascent += 2;
20471
20472 take_vertical_position_into_account (it);
20473
20474 if (it->glyph_row)
20475 append_composite_glyph (it);
20476 }
20477 else if (it->what == IT_IMAGE)
20478 produce_image_glyph (it);
20479 else if (it->what == IT_STRETCH)
20480 produce_stretch_glyph (it);
20481
20482 /* Accumulate dimensions. Note: can't assume that it->descent > 0
20483 because this isn't true for images with `:ascent 100'. */
20484 xassert (it->ascent >= 0 && it->descent >= 0);
20485 if (it->area == TEXT_AREA)
20486 it->current_x += it->pixel_width;
20487
20488 if (extra_line_spacing > 0)
20489 {
20490 it->descent += extra_line_spacing;
20491 if (extra_line_spacing > it->max_extra_line_spacing)
20492 it->max_extra_line_spacing = extra_line_spacing;
20493 }
20494
20495 it->max_ascent = max (it->max_ascent, it->ascent);
20496 it->max_descent = max (it->max_descent, it->descent);
20497 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
20498 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
20499 }
20500
20501 /* EXPORT for RIF:
20502 Output LEN glyphs starting at START at the nominal cursor position.
20503 Advance the nominal cursor over the text. The global variable
20504 updated_window contains the window being updated, updated_row is
20505 the glyph row being updated, and updated_area is the area of that
20506 row being updated. */
20507
20508 void
20509 x_write_glyphs (start, len)
20510 struct glyph *start;
20511 int len;
20512 {
20513 int x, hpos;
20514
20515 xassert (updated_window && updated_row);
20516 BLOCK_INPUT;
20517
20518 /* Write glyphs. */
20519
20520 hpos = start - updated_row->glyphs[updated_area];
20521 x = draw_glyphs (updated_window, output_cursor.x,
20522 updated_row, updated_area,
20523 hpos, hpos + len,
20524 DRAW_NORMAL_TEXT, 0);
20525
20526 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
20527 if (updated_area == TEXT_AREA
20528 && updated_window->phys_cursor_on_p
20529 && updated_window->phys_cursor.vpos == output_cursor.vpos
20530 && updated_window->phys_cursor.hpos >= hpos
20531 && updated_window->phys_cursor.hpos < hpos + len)
20532 updated_window->phys_cursor_on_p = 0;
20533
20534 UNBLOCK_INPUT;
20535
20536 /* Advance the output cursor. */
20537 output_cursor.hpos += len;
20538 output_cursor.x = x;
20539 }
20540
20541
20542 /* EXPORT for RIF:
20543 Insert LEN glyphs from START at the nominal cursor position. */
20544
20545 void
20546 x_insert_glyphs (start, len)
20547 struct glyph *start;
20548 int len;
20549 {
20550 struct frame *f;
20551 struct window *w;
20552 int line_height, shift_by_width, shifted_region_width;
20553 struct glyph_row *row;
20554 struct glyph *glyph;
20555 int frame_x, frame_y, hpos;
20556
20557 xassert (updated_window && updated_row);
20558 BLOCK_INPUT;
20559 w = updated_window;
20560 f = XFRAME (WINDOW_FRAME (w));
20561
20562 /* Get the height of the line we are in. */
20563 row = updated_row;
20564 line_height = row->height;
20565
20566 /* Get the width of the glyphs to insert. */
20567 shift_by_width = 0;
20568 for (glyph = start; glyph < start + len; ++glyph)
20569 shift_by_width += glyph->pixel_width;
20570
20571 /* Get the width of the region to shift right. */
20572 shifted_region_width = (window_box_width (w, updated_area)
20573 - output_cursor.x
20574 - shift_by_width);
20575
20576 /* Shift right. */
20577 frame_x = window_box_left (w, updated_area) + output_cursor.x;
20578 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
20579
20580 FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
20581 line_height, shift_by_width);
20582
20583 /* Write the glyphs. */
20584 hpos = start - row->glyphs[updated_area];
20585 draw_glyphs (w, output_cursor.x, row, updated_area,
20586 hpos, hpos + len,
20587 DRAW_NORMAL_TEXT, 0);
20588
20589 /* Advance the output cursor. */
20590 output_cursor.hpos += len;
20591 output_cursor.x += shift_by_width;
20592 UNBLOCK_INPUT;
20593 }
20594
20595
20596 /* EXPORT for RIF:
20597 Erase the current text line from the nominal cursor position
20598 (inclusive) to pixel column TO_X (exclusive). The idea is that
20599 everything from TO_X onward is already erased.
20600
20601 TO_X is a pixel position relative to updated_area of
20602 updated_window. TO_X == -1 means clear to the end of this area. */
20603
20604 void
20605 x_clear_end_of_line (to_x)
20606 int to_x;
20607 {
20608 struct frame *f;
20609 struct window *w = updated_window;
20610 int max_x, min_y, max_y;
20611 int from_x, from_y, to_y;
20612
20613 xassert (updated_window && updated_row);
20614 f = XFRAME (w->frame);
20615
20616 if (updated_row->full_width_p)
20617 max_x = WINDOW_TOTAL_WIDTH (w);
20618 else
20619 max_x = window_box_width (w, updated_area);
20620 max_y = window_text_bottom_y (w);
20621
20622 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
20623 of window. For TO_X > 0, truncate to end of drawing area. */
20624 if (to_x == 0)
20625 return;
20626 else if (to_x < 0)
20627 to_x = max_x;
20628 else
20629 to_x = min (to_x, max_x);
20630
20631 to_y = min (max_y, output_cursor.y + updated_row->height);
20632
20633 /* Notice if the cursor will be cleared by this operation. */
20634 if (!updated_row->full_width_p)
20635 notice_overwritten_cursor (w, updated_area,
20636 output_cursor.x, -1,
20637 updated_row->y,
20638 MATRIX_ROW_BOTTOM_Y (updated_row));
20639
20640 from_x = output_cursor.x;
20641
20642 /* Translate to frame coordinates. */
20643 if (updated_row->full_width_p)
20644 {
20645 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
20646 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
20647 }
20648 else
20649 {
20650 int area_left = window_box_left (w, updated_area);
20651 from_x += area_left;
20652 to_x += area_left;
20653 }
20654
20655 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
20656 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
20657 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
20658
20659 /* Prevent inadvertently clearing to end of the X window. */
20660 if (to_x > from_x && to_y > from_y)
20661 {
20662 BLOCK_INPUT;
20663 FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
20664 to_x - from_x, to_y - from_y);
20665 UNBLOCK_INPUT;
20666 }
20667 }
20668
20669 #endif /* HAVE_WINDOW_SYSTEM */
20670
20671
20672 \f
20673 /***********************************************************************
20674 Cursor types
20675 ***********************************************************************/
20676
20677 /* Value is the internal representation of the specified cursor type
20678 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
20679 of the bar cursor. */
20680
20681 static enum text_cursor_kinds
20682 get_specified_cursor_type (arg, width)
20683 Lisp_Object arg;
20684 int *width;
20685 {
20686 enum text_cursor_kinds type;
20687
20688 if (NILP (arg))
20689 return NO_CURSOR;
20690
20691 if (EQ (arg, Qbox))
20692 return FILLED_BOX_CURSOR;
20693
20694 if (EQ (arg, Qhollow))
20695 return HOLLOW_BOX_CURSOR;
20696
20697 if (EQ (arg, Qbar))
20698 {
20699 *width = 2;
20700 return BAR_CURSOR;
20701 }
20702
20703 if (CONSP (arg)
20704 && EQ (XCAR (arg), Qbar)
20705 && INTEGERP (XCDR (arg))
20706 && XINT (XCDR (arg)) >= 0)
20707 {
20708 *width = XINT (XCDR (arg));
20709 return BAR_CURSOR;
20710 }
20711
20712 if (EQ (arg, Qhbar))
20713 {
20714 *width = 2;
20715 return HBAR_CURSOR;
20716 }
20717
20718 if (CONSP (arg)
20719 && EQ (XCAR (arg), Qhbar)
20720 && INTEGERP (XCDR (arg))
20721 && XINT (XCDR (arg)) >= 0)
20722 {
20723 *width = XINT (XCDR (arg));
20724 return HBAR_CURSOR;
20725 }
20726
20727 /* Treat anything unknown as "hollow box cursor".
20728 It was bad to signal an error; people have trouble fixing
20729 .Xdefaults with Emacs, when it has something bad in it. */
20730 type = HOLLOW_BOX_CURSOR;
20731
20732 return type;
20733 }
20734
20735 /* Set the default cursor types for specified frame. */
20736 void
20737 set_frame_cursor_types (f, arg)
20738 struct frame *f;
20739 Lisp_Object arg;
20740 {
20741 int width;
20742 Lisp_Object tem;
20743
20744 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
20745 FRAME_CURSOR_WIDTH (f) = width;
20746
20747 /* By default, set up the blink-off state depending on the on-state. */
20748
20749 tem = Fassoc (arg, Vblink_cursor_alist);
20750 if (!NILP (tem))
20751 {
20752 FRAME_BLINK_OFF_CURSOR (f)
20753 = get_specified_cursor_type (XCDR (tem), &width);
20754 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
20755 }
20756 else
20757 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
20758 }
20759
20760
20761 /* Return the cursor we want to be displayed in window W. Return
20762 width of bar/hbar cursor through WIDTH arg. Return with
20763 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
20764 (i.e. if the `system caret' should track this cursor).
20765
20766 In a mini-buffer window, we want the cursor only to appear if we
20767 are reading input from this window. For the selected window, we
20768 want the cursor type given by the frame parameter or buffer local
20769 setting of cursor-type. If explicitly marked off, draw no cursor.
20770 In all other cases, we want a hollow box cursor. */
20771
20772 static enum text_cursor_kinds
20773 get_window_cursor_type (w, glyph, width, active_cursor)
20774 struct window *w;
20775 struct glyph *glyph;
20776 int *width;
20777 int *active_cursor;
20778 {
20779 struct frame *f = XFRAME (w->frame);
20780 struct buffer *b = XBUFFER (w->buffer);
20781 int cursor_type = DEFAULT_CURSOR;
20782 Lisp_Object alt_cursor;
20783 int non_selected = 0;
20784
20785 *active_cursor = 1;
20786
20787 /* Echo area */
20788 if (cursor_in_echo_area
20789 && FRAME_HAS_MINIBUF_P (f)
20790 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
20791 {
20792 if (w == XWINDOW (echo_area_window))
20793 {
20794 *width = FRAME_CURSOR_WIDTH (f);
20795 return FRAME_DESIRED_CURSOR (f);
20796 }
20797
20798 *active_cursor = 0;
20799 non_selected = 1;
20800 }
20801
20802 /* Nonselected window or nonselected frame. */
20803 else if (w != XWINDOW (f->selected_window)
20804 #ifdef HAVE_WINDOW_SYSTEM
20805 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame
20806 #endif
20807 )
20808 {
20809 *active_cursor = 0;
20810
20811 if (MINI_WINDOW_P (w) && minibuf_level == 0)
20812 return NO_CURSOR;
20813
20814 non_selected = 1;
20815 }
20816
20817 /* Never display a cursor in a window in which cursor-type is nil. */
20818 if (NILP (b->cursor_type))
20819 return NO_CURSOR;
20820
20821 /* Use cursor-in-non-selected-windows for non-selected window or frame. */
20822 if (non_selected)
20823 {
20824 alt_cursor = b->cursor_in_non_selected_windows;
20825 return get_specified_cursor_type (alt_cursor, width);
20826 }
20827
20828 /* Get the normal cursor type for this window. */
20829 if (EQ (b->cursor_type, Qt))
20830 {
20831 cursor_type = FRAME_DESIRED_CURSOR (f);
20832 *width = FRAME_CURSOR_WIDTH (f);
20833 }
20834 else
20835 cursor_type = get_specified_cursor_type (b->cursor_type, width);
20836
20837 /* Use normal cursor if not blinked off. */
20838 if (!w->cursor_off_p)
20839 {
20840 if (glyph != NULL && glyph->type == IMAGE_GLYPH) {
20841 if (cursor_type == FILLED_BOX_CURSOR)
20842 cursor_type = HOLLOW_BOX_CURSOR;
20843 }
20844 return cursor_type;
20845 }
20846
20847 /* Cursor is blinked off, so determine how to "toggle" it. */
20848
20849 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
20850 if ((alt_cursor = Fassoc (b->cursor_type, Vblink_cursor_alist), !NILP (alt_cursor)))
20851 return get_specified_cursor_type (XCDR (alt_cursor), width);
20852
20853 /* Then see if frame has specified a specific blink off cursor type. */
20854 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
20855 {
20856 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
20857 return FRAME_BLINK_OFF_CURSOR (f);
20858 }
20859
20860 #if 0
20861 /* Some people liked having a permanently visible blinking cursor,
20862 while others had very strong opinions against it. So it was
20863 decided to remove it. KFS 2003-09-03 */
20864
20865 /* Finally perform built-in cursor blinking:
20866 filled box <-> hollow box
20867 wide [h]bar <-> narrow [h]bar
20868 narrow [h]bar <-> no cursor
20869 other type <-> no cursor */
20870
20871 if (cursor_type == FILLED_BOX_CURSOR)
20872 return HOLLOW_BOX_CURSOR;
20873
20874 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
20875 {
20876 *width = 1;
20877 return cursor_type;
20878 }
20879 #endif
20880
20881 return NO_CURSOR;
20882 }
20883
20884
20885 #ifdef HAVE_WINDOW_SYSTEM
20886
20887 /* Notice when the text cursor of window W has been completely
20888 overwritten by a drawing operation that outputs glyphs in AREA
20889 starting at X0 and ending at X1 in the line starting at Y0 and
20890 ending at Y1. X coordinates are area-relative. X1 < 0 means all
20891 the rest of the line after X0 has been written. Y coordinates
20892 are window-relative. */
20893
20894 static void
20895 notice_overwritten_cursor (w, area, x0, x1, y0, y1)
20896 struct window *w;
20897 enum glyph_row_area area;
20898 int x0, y0, x1, y1;
20899 {
20900 int cx0, cx1, cy0, cy1;
20901 struct glyph_row *row;
20902
20903 if (!w->phys_cursor_on_p)
20904 return;
20905 if (area != TEXT_AREA)
20906 return;
20907
20908 if (w->phys_cursor.vpos < 0
20909 || w->phys_cursor.vpos >= w->current_matrix->nrows
20910 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
20911 !(row->enabled_p && row->displays_text_p)))
20912 return;
20913
20914 if (row->cursor_in_fringe_p)
20915 {
20916 row->cursor_in_fringe_p = 0;
20917 draw_fringe_bitmap (w, row, 0);
20918 w->phys_cursor_on_p = 0;
20919 return;
20920 }
20921
20922 cx0 = w->phys_cursor.x;
20923 cx1 = cx0 + w->phys_cursor_width;
20924 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
20925 return;
20926
20927 /* The cursor image will be completely removed from the
20928 screen if the output area intersects the cursor area in
20929 y-direction. When we draw in [y0 y1[, and some part of
20930 the cursor is at y < y0, that part must have been drawn
20931 before. When scrolling, the cursor is erased before
20932 actually scrolling, so we don't come here. When not
20933 scrolling, the rows above the old cursor row must have
20934 changed, and in this case these rows must have written
20935 over the cursor image.
20936
20937 Likewise if part of the cursor is below y1, with the
20938 exception of the cursor being in the first blank row at
20939 the buffer and window end because update_text_area
20940 doesn't draw that row. (Except when it does, but
20941 that's handled in update_text_area.) */
20942
20943 cy0 = w->phys_cursor.y;
20944 cy1 = cy0 + w->phys_cursor_height;
20945 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
20946 return;
20947
20948 w->phys_cursor_on_p = 0;
20949 }
20950
20951 #endif /* HAVE_WINDOW_SYSTEM */
20952
20953 \f
20954 /************************************************************************
20955 Mouse Face
20956 ************************************************************************/
20957
20958 #ifdef HAVE_WINDOW_SYSTEM
20959
20960 /* EXPORT for RIF:
20961 Fix the display of area AREA of overlapping row ROW in window W
20962 with respect to the overlapping part OVERLAPS. */
20963
20964 void
20965 x_fix_overlapping_area (w, row, area, overlaps)
20966 struct window *w;
20967 struct glyph_row *row;
20968 enum glyph_row_area area;
20969 int overlaps;
20970 {
20971 int i, x;
20972
20973 BLOCK_INPUT;
20974
20975 x = 0;
20976 for (i = 0; i < row->used[area];)
20977 {
20978 if (row->glyphs[area][i].overlaps_vertically_p)
20979 {
20980 int start = i, start_x = x;
20981
20982 do
20983 {
20984 x += row->glyphs[area][i].pixel_width;
20985 ++i;
20986 }
20987 while (i < row->used[area]
20988 && row->glyphs[area][i].overlaps_vertically_p);
20989
20990 draw_glyphs (w, start_x, row, area,
20991 start, i,
20992 DRAW_NORMAL_TEXT, overlaps);
20993 }
20994 else
20995 {
20996 x += row->glyphs[area][i].pixel_width;
20997 ++i;
20998 }
20999 }
21000
21001 UNBLOCK_INPUT;
21002 }
21003
21004
21005 /* EXPORT:
21006 Draw the cursor glyph of window W in glyph row ROW. See the
21007 comment of draw_glyphs for the meaning of HL. */
21008
21009 void
21010 draw_phys_cursor_glyph (w, row, hl)
21011 struct window *w;
21012 struct glyph_row *row;
21013 enum draw_glyphs_face hl;
21014 {
21015 /* If cursor hpos is out of bounds, don't draw garbage. This can
21016 happen in mini-buffer windows when switching between echo area
21017 glyphs and mini-buffer. */
21018 if (w->phys_cursor.hpos < row->used[TEXT_AREA])
21019 {
21020 int on_p = w->phys_cursor_on_p;
21021 int x1;
21022 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA,
21023 w->phys_cursor.hpos, w->phys_cursor.hpos + 1,
21024 hl, 0);
21025 w->phys_cursor_on_p = on_p;
21026
21027 if (hl == DRAW_CURSOR)
21028 w->phys_cursor_width = x1 - w->phys_cursor.x;
21029 /* When we erase the cursor, and ROW is overlapped by other
21030 rows, make sure that these overlapping parts of other rows
21031 are redrawn. */
21032 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
21033 {
21034 w->phys_cursor_width = x1 - w->phys_cursor.x;
21035
21036 if (row > w->current_matrix->rows
21037 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
21038 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
21039 OVERLAPS_ERASED_CURSOR);
21040
21041 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
21042 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
21043 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
21044 OVERLAPS_ERASED_CURSOR);
21045 }
21046 }
21047 }
21048
21049
21050 /* EXPORT:
21051 Erase the image of a cursor of window W from the screen. */
21052
21053 void
21054 erase_phys_cursor (w)
21055 struct window *w;
21056 {
21057 struct frame *f = XFRAME (w->frame);
21058 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
21059 int hpos = w->phys_cursor.hpos;
21060 int vpos = w->phys_cursor.vpos;
21061 int mouse_face_here_p = 0;
21062 struct glyph_matrix *active_glyphs = w->current_matrix;
21063 struct glyph_row *cursor_row;
21064 struct glyph *cursor_glyph;
21065 enum draw_glyphs_face hl;
21066
21067 /* No cursor displayed or row invalidated => nothing to do on the
21068 screen. */
21069 if (w->phys_cursor_type == NO_CURSOR)
21070 goto mark_cursor_off;
21071
21072 /* VPOS >= active_glyphs->nrows means that window has been resized.
21073 Don't bother to erase the cursor. */
21074 if (vpos >= active_glyphs->nrows)
21075 goto mark_cursor_off;
21076
21077 /* If row containing cursor is marked invalid, there is nothing we
21078 can do. */
21079 cursor_row = MATRIX_ROW (active_glyphs, vpos);
21080 if (!cursor_row->enabled_p)
21081 goto mark_cursor_off;
21082
21083 /* If line spacing is > 0, old cursor may only be partially visible in
21084 window after split-window. So adjust visible height. */
21085 cursor_row->visible_height = min (cursor_row->visible_height,
21086 window_text_bottom_y (w) - cursor_row->y);
21087
21088 /* If row is completely invisible, don't attempt to delete a cursor which
21089 isn't there. This can happen if cursor is at top of a window, and
21090 we switch to a buffer with a header line in that window. */
21091 if (cursor_row->visible_height <= 0)
21092 goto mark_cursor_off;
21093
21094 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
21095 if (cursor_row->cursor_in_fringe_p)
21096 {
21097 cursor_row->cursor_in_fringe_p = 0;
21098 draw_fringe_bitmap (w, cursor_row, 0);
21099 goto mark_cursor_off;
21100 }
21101
21102 /* This can happen when the new row is shorter than the old one.
21103 In this case, either draw_glyphs or clear_end_of_line
21104 should have cleared the cursor. Note that we wouldn't be
21105 able to erase the cursor in this case because we don't have a
21106 cursor glyph at hand. */
21107 if (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])
21108 goto mark_cursor_off;
21109
21110 /* If the cursor is in the mouse face area, redisplay that when
21111 we clear the cursor. */
21112 if (! NILP (dpyinfo->mouse_face_window)
21113 && w == XWINDOW (dpyinfo->mouse_face_window)
21114 && (vpos > dpyinfo->mouse_face_beg_row
21115 || (vpos == dpyinfo->mouse_face_beg_row
21116 && hpos >= dpyinfo->mouse_face_beg_col))
21117 && (vpos < dpyinfo->mouse_face_end_row
21118 || (vpos == dpyinfo->mouse_face_end_row
21119 && hpos < dpyinfo->mouse_face_end_col))
21120 /* Don't redraw the cursor's spot in mouse face if it is at the
21121 end of a line (on a newline). The cursor appears there, but
21122 mouse highlighting does not. */
21123 && cursor_row->used[TEXT_AREA] > hpos)
21124 mouse_face_here_p = 1;
21125
21126 /* Maybe clear the display under the cursor. */
21127 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
21128 {
21129 int x, y;
21130 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
21131 int width;
21132
21133 cursor_glyph = get_phys_cursor_glyph (w);
21134 if (cursor_glyph == NULL)
21135 goto mark_cursor_off;
21136
21137 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
21138 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
21139 width = min (cursor_glyph->pixel_width,
21140 window_box_width (w, TEXT_AREA) - w->phys_cursor.x);
21141
21142 FRAME_RIF (f)->clear_frame_area (f, x, y, width, cursor_row->visible_height);
21143 }
21144
21145 /* Erase the cursor by redrawing the character underneath it. */
21146 if (mouse_face_here_p)
21147 hl = DRAW_MOUSE_FACE;
21148 else
21149 hl = DRAW_NORMAL_TEXT;
21150 draw_phys_cursor_glyph (w, cursor_row, hl);
21151
21152 mark_cursor_off:
21153 w->phys_cursor_on_p = 0;
21154 w->phys_cursor_type = NO_CURSOR;
21155 }
21156
21157
21158 /* EXPORT:
21159 Display or clear cursor of window W. If ON is zero, clear the
21160 cursor. If it is non-zero, display the cursor. If ON is nonzero,
21161 where to put the cursor is specified by HPOS, VPOS, X and Y. */
21162
21163 void
21164 display_and_set_cursor (w, on, hpos, vpos, x, y)
21165 struct window *w;
21166 int on, hpos, vpos, x, y;
21167 {
21168 struct frame *f = XFRAME (w->frame);
21169 int new_cursor_type;
21170 int new_cursor_width;
21171 int active_cursor;
21172 struct glyph_row *glyph_row;
21173 struct glyph *glyph;
21174
21175 /* This is pointless on invisible frames, and dangerous on garbaged
21176 windows and frames; in the latter case, the frame or window may
21177 be in the midst of changing its size, and x and y may be off the
21178 window. */
21179 if (! FRAME_VISIBLE_P (f)
21180 || FRAME_GARBAGED_P (f)
21181 || vpos >= w->current_matrix->nrows
21182 || hpos >= w->current_matrix->matrix_w)
21183 return;
21184
21185 /* If cursor is off and we want it off, return quickly. */
21186 if (!on && !w->phys_cursor_on_p)
21187 return;
21188
21189 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
21190 /* If cursor row is not enabled, we don't really know where to
21191 display the cursor. */
21192 if (!glyph_row->enabled_p)
21193 {
21194 w->phys_cursor_on_p = 0;
21195 return;
21196 }
21197
21198 glyph = NULL;
21199 if (!glyph_row->exact_window_width_line_p
21200 || hpos < glyph_row->used[TEXT_AREA])
21201 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
21202
21203 xassert (interrupt_input_blocked);
21204
21205 /* Set new_cursor_type to the cursor we want to be displayed. */
21206 new_cursor_type = get_window_cursor_type (w, glyph,
21207 &new_cursor_width, &active_cursor);
21208
21209 /* If cursor is currently being shown and we don't want it to be or
21210 it is in the wrong place, or the cursor type is not what we want,
21211 erase it. */
21212 if (w->phys_cursor_on_p
21213 && (!on
21214 || w->phys_cursor.x != x
21215 || w->phys_cursor.y != y
21216 || new_cursor_type != w->phys_cursor_type
21217 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
21218 && new_cursor_width != w->phys_cursor_width)))
21219 erase_phys_cursor (w);
21220
21221 /* Don't check phys_cursor_on_p here because that flag is only set
21222 to zero in some cases where we know that the cursor has been
21223 completely erased, to avoid the extra work of erasing the cursor
21224 twice. In other words, phys_cursor_on_p can be 1 and the cursor
21225 still not be visible, or it has only been partly erased. */
21226 if (on)
21227 {
21228 w->phys_cursor_ascent = glyph_row->ascent;
21229 w->phys_cursor_height = glyph_row->height;
21230
21231 /* Set phys_cursor_.* before x_draw_.* is called because some
21232 of them may need the information. */
21233 w->phys_cursor.x = x;
21234 w->phys_cursor.y = glyph_row->y;
21235 w->phys_cursor.hpos = hpos;
21236 w->phys_cursor.vpos = vpos;
21237 }
21238
21239 FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
21240 new_cursor_type, new_cursor_width,
21241 on, active_cursor);
21242 }
21243
21244
21245 /* Switch the display of W's cursor on or off, according to the value
21246 of ON. */
21247
21248 static void
21249 update_window_cursor (w, on)
21250 struct window *w;
21251 int on;
21252 {
21253 /* Don't update cursor in windows whose frame is in the process
21254 of being deleted. */
21255 if (w->current_matrix)
21256 {
21257 BLOCK_INPUT;
21258 display_and_set_cursor (w, on, w->phys_cursor.hpos, w->phys_cursor.vpos,
21259 w->phys_cursor.x, w->phys_cursor.y);
21260 UNBLOCK_INPUT;
21261 }
21262 }
21263
21264
21265 /* Call update_window_cursor with parameter ON_P on all leaf windows
21266 in the window tree rooted at W. */
21267
21268 static void
21269 update_cursor_in_window_tree (w, on_p)
21270 struct window *w;
21271 int on_p;
21272 {
21273 while (w)
21274 {
21275 if (!NILP (w->hchild))
21276 update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
21277 else if (!NILP (w->vchild))
21278 update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
21279 else
21280 update_window_cursor (w, on_p);
21281
21282 w = NILP (w->next) ? 0 : XWINDOW (w->next);
21283 }
21284 }
21285
21286
21287 /* EXPORT:
21288 Display the cursor on window W, or clear it, according to ON_P.
21289 Don't change the cursor's position. */
21290
21291 void
21292 x_update_cursor (f, on_p)
21293 struct frame *f;
21294 int on_p;
21295 {
21296 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
21297 }
21298
21299
21300 /* EXPORT:
21301 Clear the cursor of window W to background color, and mark the
21302 cursor as not shown. This is used when the text where the cursor
21303 is is about to be rewritten. */
21304
21305 void
21306 x_clear_cursor (w)
21307 struct window *w;
21308 {
21309 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
21310 update_window_cursor (w, 0);
21311 }
21312
21313
21314 /* EXPORT:
21315 Display the active region described by mouse_face_* according to DRAW. */
21316
21317 void
21318 show_mouse_face (dpyinfo, draw)
21319 Display_Info *dpyinfo;
21320 enum draw_glyphs_face draw;
21321 {
21322 struct window *w = XWINDOW (dpyinfo->mouse_face_window);
21323 struct frame *f = XFRAME (WINDOW_FRAME (w));
21324
21325 if (/* If window is in the process of being destroyed, don't bother
21326 to do anything. */
21327 w->current_matrix != NULL
21328 /* Don't update mouse highlight if hidden */
21329 && (draw != DRAW_MOUSE_FACE || !dpyinfo->mouse_face_hidden)
21330 /* Recognize when we are called to operate on rows that don't exist
21331 anymore. This can happen when a window is split. */
21332 && dpyinfo->mouse_face_end_row < w->current_matrix->nrows)
21333 {
21334 int phys_cursor_on_p = w->phys_cursor_on_p;
21335 struct glyph_row *row, *first, *last;
21336
21337 first = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_beg_row);
21338 last = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_end_row);
21339
21340 for (row = first; row <= last && row->enabled_p; ++row)
21341 {
21342 int start_hpos, end_hpos, start_x;
21343
21344 /* For all but the first row, the highlight starts at column 0. */
21345 if (row == first)
21346 {
21347 start_hpos = dpyinfo->mouse_face_beg_col;
21348 start_x = dpyinfo->mouse_face_beg_x;
21349 }
21350 else
21351 {
21352 start_hpos = 0;
21353 start_x = 0;
21354 }
21355
21356 if (row == last)
21357 end_hpos = dpyinfo->mouse_face_end_col;
21358 else
21359 {
21360 end_hpos = row->used[TEXT_AREA];
21361 if (draw == DRAW_NORMAL_TEXT)
21362 row->fill_line_p = 1; /* Clear to end of line */
21363 }
21364
21365 if (end_hpos > start_hpos)
21366 {
21367 draw_glyphs (w, start_x, row, TEXT_AREA,
21368 start_hpos, end_hpos,
21369 draw, 0);
21370
21371 row->mouse_face_p
21372 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
21373 }
21374 }
21375
21376 /* When we've written over the cursor, arrange for it to
21377 be displayed again. */
21378 if (phys_cursor_on_p && !w->phys_cursor_on_p)
21379 {
21380 BLOCK_INPUT;
21381 display_and_set_cursor (w, 1,
21382 w->phys_cursor.hpos, w->phys_cursor.vpos,
21383 w->phys_cursor.x, w->phys_cursor.y);
21384 UNBLOCK_INPUT;
21385 }
21386 }
21387
21388 /* Change the mouse cursor. */
21389 if (draw == DRAW_NORMAL_TEXT)
21390 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
21391 else if (draw == DRAW_MOUSE_FACE)
21392 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
21393 else
21394 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
21395 }
21396
21397 /* EXPORT:
21398 Clear out the mouse-highlighted active region.
21399 Redraw it un-highlighted first. Value is non-zero if mouse
21400 face was actually drawn unhighlighted. */
21401
21402 int
21403 clear_mouse_face (dpyinfo)
21404 Display_Info *dpyinfo;
21405 {
21406 int cleared = 0;
21407
21408 if (!dpyinfo->mouse_face_hidden && !NILP (dpyinfo->mouse_face_window))
21409 {
21410 show_mouse_face (dpyinfo, DRAW_NORMAL_TEXT);
21411 cleared = 1;
21412 }
21413
21414 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
21415 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
21416 dpyinfo->mouse_face_window = Qnil;
21417 dpyinfo->mouse_face_overlay = Qnil;
21418 return cleared;
21419 }
21420
21421
21422 /* EXPORT:
21423 Non-zero if physical cursor of window W is within mouse face. */
21424
21425 int
21426 cursor_in_mouse_face_p (w)
21427 struct window *w;
21428 {
21429 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
21430 int in_mouse_face = 0;
21431
21432 if (WINDOWP (dpyinfo->mouse_face_window)
21433 && XWINDOW (dpyinfo->mouse_face_window) == w)
21434 {
21435 int hpos = w->phys_cursor.hpos;
21436 int vpos = w->phys_cursor.vpos;
21437
21438 if (vpos >= dpyinfo->mouse_face_beg_row
21439 && vpos <= dpyinfo->mouse_face_end_row
21440 && (vpos > dpyinfo->mouse_face_beg_row
21441 || hpos >= dpyinfo->mouse_face_beg_col)
21442 && (vpos < dpyinfo->mouse_face_end_row
21443 || hpos < dpyinfo->mouse_face_end_col
21444 || dpyinfo->mouse_face_past_end))
21445 in_mouse_face = 1;
21446 }
21447
21448 return in_mouse_face;
21449 }
21450
21451
21452
21453 \f
21454 /* Find the glyph matrix position of buffer position CHARPOS in window
21455 *W. HPOS, *VPOS, *X, and *Y are set to the positions found. W's
21456 current glyphs must be up to date. If CHARPOS is above window
21457 start return (0, 0, 0, 0). If CHARPOS is after end of W, return end
21458 of last line in W. In the row containing CHARPOS, stop before glyphs
21459 having STOP as object. */
21460
21461 #if 1 /* This is a version of fast_find_position that's more correct
21462 in the presence of hscrolling, for example. I didn't install
21463 it right away because the problem fixed is minor, it failed
21464 in 20.x as well, and I think it's too risky to install
21465 so near the release of 21.1. 2001-09-25 gerd. */
21466
21467 static int
21468 fast_find_position (w, charpos, hpos, vpos, x, y, stop)
21469 struct window *w;
21470 int charpos;
21471 int *hpos, *vpos, *x, *y;
21472 Lisp_Object stop;
21473 {
21474 struct glyph_row *row, *first;
21475 struct glyph *glyph, *end;
21476 int past_end = 0;
21477
21478 first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
21479 if (charpos < MATRIX_ROW_START_CHARPOS (first))
21480 {
21481 *x = first->x;
21482 *y = first->y;
21483 *hpos = 0;
21484 *vpos = MATRIX_ROW_VPOS (first, w->current_matrix);
21485 return 1;
21486 }
21487
21488 row = row_containing_pos (w, charpos, first, NULL, 0);
21489 if (row == NULL)
21490 {
21491 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
21492 past_end = 1;
21493 }
21494
21495 /* If whole rows or last part of a row came from a display overlay,
21496 row_containing_pos will skip over such rows because their end pos
21497 equals the start pos of the overlay or interval.
21498
21499 Move back if we have a STOP object and previous row's
21500 end glyph came from STOP. */
21501 if (!NILP (stop))
21502 {
21503 struct glyph_row *prev;
21504 while ((prev = row - 1, prev >= first)
21505 && MATRIX_ROW_END_CHARPOS (prev) == charpos
21506 && prev->used[TEXT_AREA] > 0)
21507 {
21508 struct glyph *beg = prev->glyphs[TEXT_AREA];
21509 glyph = beg + prev->used[TEXT_AREA];
21510 while (--glyph >= beg
21511 && INTEGERP (glyph->object));
21512 if (glyph < beg
21513 || !EQ (stop, glyph->object))
21514 break;
21515 row = prev;
21516 }
21517 }
21518
21519 *x = row->x;
21520 *y = row->y;
21521 *vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
21522
21523 glyph = row->glyphs[TEXT_AREA];
21524 end = glyph + row->used[TEXT_AREA];
21525
21526 /* Skip over glyphs not having an object at the start of the row.
21527 These are special glyphs like truncation marks on terminal
21528 frames. */
21529 if (row->displays_text_p)
21530 while (glyph < end
21531 && INTEGERP (glyph->object)
21532 && !EQ (stop, glyph->object)
21533 && glyph->charpos < 0)
21534 {
21535 *x += glyph->pixel_width;
21536 ++glyph;
21537 }
21538
21539 while (glyph < end
21540 && !INTEGERP (glyph->object)
21541 && !EQ (stop, glyph->object)
21542 && (!BUFFERP (glyph->object)
21543 || glyph->charpos < charpos))
21544 {
21545 *x += glyph->pixel_width;
21546 ++glyph;
21547 }
21548
21549 *hpos = glyph - row->glyphs[TEXT_AREA];
21550 return !past_end;
21551 }
21552
21553 #else /* not 1 */
21554
21555 static int
21556 fast_find_position (w, pos, hpos, vpos, x, y, stop)
21557 struct window *w;
21558 int pos;
21559 int *hpos, *vpos, *x, *y;
21560 Lisp_Object stop;
21561 {
21562 int i;
21563 int lastcol;
21564 int maybe_next_line_p = 0;
21565 int line_start_position;
21566 int yb = window_text_bottom_y (w);
21567 struct glyph_row *row, *best_row;
21568 int row_vpos, best_row_vpos;
21569 int current_x;
21570
21571 row = best_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
21572 row_vpos = best_row_vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
21573
21574 while (row->y < yb)
21575 {
21576 if (row->used[TEXT_AREA])
21577 line_start_position = row->glyphs[TEXT_AREA]->charpos;
21578 else
21579 line_start_position = 0;
21580
21581 if (line_start_position > pos)
21582 break;
21583 /* If the position sought is the end of the buffer,
21584 don't include the blank lines at the bottom of the window. */
21585 else if (line_start_position == pos
21586 && pos == BUF_ZV (XBUFFER (w->buffer)))
21587 {
21588 maybe_next_line_p = 1;
21589 break;
21590 }
21591 else if (line_start_position > 0)
21592 {
21593 best_row = row;
21594 best_row_vpos = row_vpos;
21595 }
21596
21597 if (row->y + row->height >= yb)
21598 break;
21599
21600 ++row;
21601 ++row_vpos;
21602 }
21603
21604 /* Find the right column within BEST_ROW. */
21605 lastcol = 0;
21606 current_x = best_row->x;
21607 for (i = 0; i < best_row->used[TEXT_AREA]; i++)
21608 {
21609 struct glyph *glyph = best_row->glyphs[TEXT_AREA] + i;
21610 int charpos = glyph->charpos;
21611
21612 if (BUFFERP (glyph->object))
21613 {
21614 if (charpos == pos)
21615 {
21616 *hpos = i;
21617 *vpos = best_row_vpos;
21618 *x = current_x;
21619 *y = best_row->y;
21620 return 1;
21621 }
21622 else if (charpos > pos)
21623 break;
21624 }
21625 else if (EQ (glyph->object, stop))
21626 break;
21627
21628 if (charpos > 0)
21629 lastcol = i;
21630 current_x += glyph->pixel_width;
21631 }
21632
21633 /* If we're looking for the end of the buffer,
21634 and we didn't find it in the line we scanned,
21635 use the start of the following line. */
21636 if (maybe_next_line_p)
21637 {
21638 ++best_row;
21639 ++best_row_vpos;
21640 lastcol = 0;
21641 current_x = best_row->x;
21642 }
21643
21644 *vpos = best_row_vpos;
21645 *hpos = lastcol + 1;
21646 *x = current_x;
21647 *y = best_row->y;
21648 return 0;
21649 }
21650
21651 #endif /* not 1 */
21652
21653
21654 /* Find the position of the glyph for position POS in OBJECT in
21655 window W's current matrix, and return in *X, *Y the pixel
21656 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
21657
21658 RIGHT_P non-zero means return the position of the right edge of the
21659 glyph, RIGHT_P zero means return the left edge position.
21660
21661 If no glyph for POS exists in the matrix, return the position of
21662 the glyph with the next smaller position that is in the matrix, if
21663 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
21664 exists in the matrix, return the position of the glyph with the
21665 next larger position in OBJECT.
21666
21667 Value is non-zero if a glyph was found. */
21668
21669 static int
21670 fast_find_string_pos (w, pos, object, hpos, vpos, x, y, right_p)
21671 struct window *w;
21672 int pos;
21673 Lisp_Object object;
21674 int *hpos, *vpos, *x, *y;
21675 int right_p;
21676 {
21677 int yb = window_text_bottom_y (w);
21678 struct glyph_row *r;
21679 struct glyph *best_glyph = NULL;
21680 struct glyph_row *best_row = NULL;
21681 int best_x = 0;
21682
21683 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
21684 r->enabled_p && r->y < yb;
21685 ++r)
21686 {
21687 struct glyph *g = r->glyphs[TEXT_AREA];
21688 struct glyph *e = g + r->used[TEXT_AREA];
21689 int gx;
21690
21691 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
21692 if (EQ (g->object, object))
21693 {
21694 if (g->charpos == pos)
21695 {
21696 best_glyph = g;
21697 best_x = gx;
21698 best_row = r;
21699 goto found;
21700 }
21701 else if (best_glyph == NULL
21702 || ((abs (g->charpos - pos)
21703 < abs (best_glyph->charpos - pos))
21704 && (right_p
21705 ? g->charpos < pos
21706 : g->charpos > pos)))
21707 {
21708 best_glyph = g;
21709 best_x = gx;
21710 best_row = r;
21711 }
21712 }
21713 }
21714
21715 found:
21716
21717 if (best_glyph)
21718 {
21719 *x = best_x;
21720 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
21721
21722 if (right_p)
21723 {
21724 *x += best_glyph->pixel_width;
21725 ++*hpos;
21726 }
21727
21728 *y = best_row->y;
21729 *vpos = best_row - w->current_matrix->rows;
21730 }
21731
21732 return best_glyph != NULL;
21733 }
21734
21735
21736 /* See if position X, Y is within a hot-spot of an image. */
21737
21738 static int
21739 on_hot_spot_p (hot_spot, x, y)
21740 Lisp_Object hot_spot;
21741 int x, y;
21742 {
21743 if (!CONSP (hot_spot))
21744 return 0;
21745
21746 if (EQ (XCAR (hot_spot), Qrect))
21747 {
21748 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
21749 Lisp_Object rect = XCDR (hot_spot);
21750 Lisp_Object tem;
21751 if (!CONSP (rect))
21752 return 0;
21753 if (!CONSP (XCAR (rect)))
21754 return 0;
21755 if (!CONSP (XCDR (rect)))
21756 return 0;
21757 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
21758 return 0;
21759 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
21760 return 0;
21761 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
21762 return 0;
21763 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
21764 return 0;
21765 return 1;
21766 }
21767 else if (EQ (XCAR (hot_spot), Qcircle))
21768 {
21769 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
21770 Lisp_Object circ = XCDR (hot_spot);
21771 Lisp_Object lr, lx0, ly0;
21772 if (CONSP (circ)
21773 && CONSP (XCAR (circ))
21774 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
21775 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
21776 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
21777 {
21778 double r = XFLOATINT (lr);
21779 double dx = XINT (lx0) - x;
21780 double dy = XINT (ly0) - y;
21781 return (dx * dx + dy * dy <= r * r);
21782 }
21783 }
21784 else if (EQ (XCAR (hot_spot), Qpoly))
21785 {
21786 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
21787 if (VECTORP (XCDR (hot_spot)))
21788 {
21789 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
21790 Lisp_Object *poly = v->contents;
21791 int n = v->size;
21792 int i;
21793 int inside = 0;
21794 Lisp_Object lx, ly;
21795 int x0, y0;
21796
21797 /* Need an even number of coordinates, and at least 3 edges. */
21798 if (n < 6 || n & 1)
21799 return 0;
21800
21801 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
21802 If count is odd, we are inside polygon. Pixels on edges
21803 may or may not be included depending on actual geometry of the
21804 polygon. */
21805 if ((lx = poly[n-2], !INTEGERP (lx))
21806 || (ly = poly[n-1], !INTEGERP (lx)))
21807 return 0;
21808 x0 = XINT (lx), y0 = XINT (ly);
21809 for (i = 0; i < n; i += 2)
21810 {
21811 int x1 = x0, y1 = y0;
21812 if ((lx = poly[i], !INTEGERP (lx))
21813 || (ly = poly[i+1], !INTEGERP (ly)))
21814 return 0;
21815 x0 = XINT (lx), y0 = XINT (ly);
21816
21817 /* Does this segment cross the X line? */
21818 if (x0 >= x)
21819 {
21820 if (x1 >= x)
21821 continue;
21822 }
21823 else if (x1 < x)
21824 continue;
21825 if (y > y0 && y > y1)
21826 continue;
21827 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
21828 inside = !inside;
21829 }
21830 return inside;
21831 }
21832 }
21833 return 0;
21834 }
21835
21836 Lisp_Object
21837 find_hot_spot (map, x, y)
21838 Lisp_Object map;
21839 int x, y;
21840 {
21841 while (CONSP (map))
21842 {
21843 if (CONSP (XCAR (map))
21844 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
21845 return XCAR (map);
21846 map = XCDR (map);
21847 }
21848
21849 return Qnil;
21850 }
21851
21852 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
21853 3, 3, 0,
21854 doc: /* Lookup in image map MAP coordinates X and Y.
21855 An image map is an alist where each element has the format (AREA ID PLIST).
21856 An AREA is specified as either a rectangle, a circle, or a polygon:
21857 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
21858 pixel coordinates of the upper left and bottom right corners.
21859 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
21860 and the radius of the circle; r may be a float or integer.
21861 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
21862 vector describes one corner in the polygon.
21863 Returns the alist element for the first matching AREA in MAP. */)
21864 (map, x, y)
21865 Lisp_Object map;
21866 Lisp_Object x, y;
21867 {
21868 if (NILP (map))
21869 return Qnil;
21870
21871 CHECK_NUMBER (x);
21872 CHECK_NUMBER (y);
21873
21874 return find_hot_spot (map, XINT (x), XINT (y));
21875 }
21876
21877
21878 /* Display frame CURSOR, optionally using shape defined by POINTER. */
21879 static void
21880 define_frame_cursor1 (f, cursor, pointer)
21881 struct frame *f;
21882 Cursor cursor;
21883 Lisp_Object pointer;
21884 {
21885 /* Do not change cursor shape while dragging mouse. */
21886 if (!NILP (do_mouse_tracking))
21887 return;
21888
21889 if (!NILP (pointer))
21890 {
21891 if (EQ (pointer, Qarrow))
21892 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
21893 else if (EQ (pointer, Qhand))
21894 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
21895 else if (EQ (pointer, Qtext))
21896 cursor = FRAME_X_OUTPUT (f)->text_cursor;
21897 else if (EQ (pointer, intern ("hdrag")))
21898 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
21899 #ifdef HAVE_X_WINDOWS
21900 else if (EQ (pointer, intern ("vdrag")))
21901 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
21902 #endif
21903 else if (EQ (pointer, intern ("hourglass")))
21904 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
21905 else if (EQ (pointer, Qmodeline))
21906 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
21907 else
21908 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
21909 }
21910
21911 if (cursor != No_Cursor)
21912 FRAME_RIF (f)->define_frame_cursor (f, cursor);
21913 }
21914
21915 /* Take proper action when mouse has moved to the mode or header line
21916 or marginal area AREA of window W, x-position X and y-position Y.
21917 X is relative to the start of the text display area of W, so the
21918 width of bitmap areas and scroll bars must be subtracted to get a
21919 position relative to the start of the mode line. */
21920
21921 static void
21922 note_mode_line_or_margin_highlight (window, x, y, area)
21923 Lisp_Object window;
21924 int x, y;
21925 enum window_part area;
21926 {
21927 struct window *w = XWINDOW (window);
21928 struct frame *f = XFRAME (w->frame);
21929 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
21930 Cursor cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
21931 Lisp_Object pointer = Qnil;
21932 int charpos, dx, dy, width, height;
21933 Lisp_Object string, object = Qnil;
21934 Lisp_Object pos, help;
21935
21936 Lisp_Object mouse_face;
21937 int original_x_pixel = x;
21938 struct glyph * glyph = NULL;
21939 struct glyph_row *row;
21940
21941 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
21942 {
21943 int x0;
21944 struct glyph *end;
21945
21946 string = mode_line_string (w, area, &x, &y, &charpos,
21947 &object, &dx, &dy, &width, &height);
21948
21949 row = (area == ON_MODE_LINE
21950 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
21951 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
21952
21953 /* Find glyph */
21954 if (row->mode_line_p && row->enabled_p)
21955 {
21956 glyph = row->glyphs[TEXT_AREA];
21957 end = glyph + row->used[TEXT_AREA];
21958
21959 for (x0 = original_x_pixel;
21960 glyph < end && x0 >= glyph->pixel_width;
21961 ++glyph)
21962 x0 -= glyph->pixel_width;
21963
21964 if (glyph >= end)
21965 glyph = NULL;
21966 }
21967 }
21968 else
21969 {
21970 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
21971 string = marginal_area_string (w, area, &x, &y, &charpos,
21972 &object, &dx, &dy, &width, &height);
21973 }
21974
21975 help = Qnil;
21976
21977 if (IMAGEP (object))
21978 {
21979 Lisp_Object image_map, hotspot;
21980 if ((image_map = Fplist_get (XCDR (object), QCmap),
21981 !NILP (image_map))
21982 && (hotspot = find_hot_spot (image_map, dx, dy),
21983 CONSP (hotspot))
21984 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
21985 {
21986 Lisp_Object area_id, plist;
21987
21988 area_id = XCAR (hotspot);
21989 /* Could check AREA_ID to see if we enter/leave this hot-spot.
21990 If so, we could look for mouse-enter, mouse-leave
21991 properties in PLIST (and do something...). */
21992 hotspot = XCDR (hotspot);
21993 if (CONSP (hotspot)
21994 && (plist = XCAR (hotspot), CONSP (plist)))
21995 {
21996 pointer = Fplist_get (plist, Qpointer);
21997 if (NILP (pointer))
21998 pointer = Qhand;
21999 help = Fplist_get (plist, Qhelp_echo);
22000 if (!NILP (help))
22001 {
22002 help_echo_string = help;
22003 /* Is this correct? ++kfs */
22004 XSETWINDOW (help_echo_window, w);
22005 help_echo_object = w->buffer;
22006 help_echo_pos = charpos;
22007 }
22008 }
22009 }
22010 if (NILP (pointer))
22011 pointer = Fplist_get (XCDR (object), QCpointer);
22012 }
22013
22014 if (STRINGP (string))
22015 {
22016 pos = make_number (charpos);
22017 /* If we're on a string with `help-echo' text property, arrange
22018 for the help to be displayed. This is done by setting the
22019 global variable help_echo_string to the help string. */
22020 if (NILP (help))
22021 {
22022 help = Fget_text_property (pos, Qhelp_echo, string);
22023 if (!NILP (help))
22024 {
22025 help_echo_string = help;
22026 XSETWINDOW (help_echo_window, w);
22027 help_echo_object = string;
22028 help_echo_pos = charpos;
22029 }
22030 }
22031
22032 if (NILP (pointer))
22033 pointer = Fget_text_property (pos, Qpointer, string);
22034
22035 /* Change the mouse pointer according to what is under X/Y. */
22036 if (NILP (pointer) && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
22037 {
22038 Lisp_Object map;
22039 map = Fget_text_property (pos, Qlocal_map, string);
22040 if (!KEYMAPP (map))
22041 map = Fget_text_property (pos, Qkeymap, string);
22042 if (!KEYMAPP (map))
22043 cursor = dpyinfo->vertical_scroll_bar_cursor;
22044 }
22045
22046 /* Change the mouse face according to what is under X/Y. */
22047 mouse_face = Fget_text_property (pos, Qmouse_face, string);
22048 if (!NILP (mouse_face)
22049 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
22050 && glyph)
22051 {
22052 Lisp_Object b, e;
22053
22054 struct glyph * tmp_glyph;
22055
22056 int gpos;
22057 int gseq_length;
22058 int total_pixel_width;
22059 int ignore;
22060
22061 int vpos, hpos;
22062
22063 b = Fprevious_single_property_change (make_number (charpos + 1),
22064 Qmouse_face, string, Qnil);
22065 if (NILP (b))
22066 b = make_number (0);
22067
22068 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
22069 if (NILP (e))
22070 e = make_number (SCHARS (string));
22071
22072 /* Calculate the position(glyph position: GPOS) of GLYPH in
22073 displayed string. GPOS is different from CHARPOS.
22074
22075 CHARPOS is the position of glyph in internal string
22076 object. A mode line string format has structures which
22077 is converted to a flatten by emacs lisp interpreter.
22078 The internal string is an element of the structures.
22079 The displayed string is the flatten string. */
22080 for (tmp_glyph = glyph - 1, gpos = 0;
22081 tmp_glyph->charpos >= XINT (b);
22082 tmp_glyph--, gpos++)
22083 {
22084 if (!EQ (tmp_glyph->object, glyph->object))
22085 break;
22086 }
22087
22088 /* Calculate the lenght(glyph sequence length: GSEQ_LENGTH) of
22089 displayed string holding GLYPH.
22090
22091 GSEQ_LENGTH is different from SCHARS (STRING).
22092 SCHARS (STRING) returns the length of the internal string. */
22093 for (tmp_glyph = glyph, gseq_length = gpos;
22094 tmp_glyph->charpos < XINT (e);
22095 tmp_glyph++, gseq_length++)
22096 {
22097 if (!EQ (tmp_glyph->object, glyph->object))
22098 break;
22099 }
22100
22101 total_pixel_width = 0;
22102 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
22103 total_pixel_width += tmp_glyph->pixel_width;
22104
22105 /* Pre calculation of re-rendering position */
22106 vpos = (x - gpos);
22107 hpos = (area == ON_MODE_LINE
22108 ? (w->current_matrix)->nrows - 1
22109 : 0);
22110
22111 /* If the re-rendering position is included in the last
22112 re-rendering area, we should do nothing. */
22113 if ( EQ (window, dpyinfo->mouse_face_window)
22114 && dpyinfo->mouse_face_beg_col <= vpos
22115 && vpos < dpyinfo->mouse_face_end_col
22116 && dpyinfo->mouse_face_beg_row == hpos )
22117 return;
22118
22119 if (clear_mouse_face (dpyinfo))
22120 cursor = No_Cursor;
22121
22122 dpyinfo->mouse_face_beg_col = vpos;
22123 dpyinfo->mouse_face_beg_row = hpos;
22124
22125 dpyinfo->mouse_face_beg_x = original_x_pixel - (total_pixel_width + dx);
22126 dpyinfo->mouse_face_beg_y = 0;
22127
22128 dpyinfo->mouse_face_end_col = vpos + gseq_length;
22129 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_beg_row;
22130
22131 dpyinfo->mouse_face_end_x = 0;
22132 dpyinfo->mouse_face_end_y = 0;
22133
22134 dpyinfo->mouse_face_past_end = 0;
22135 dpyinfo->mouse_face_window = window;
22136
22137 dpyinfo->mouse_face_face_id = face_at_string_position (w, string,
22138 charpos,
22139 0, 0, 0, &ignore,
22140 glyph->face_id, 1);
22141 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
22142
22143 if (NILP (pointer))
22144 pointer = Qhand;
22145 }
22146 else if ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
22147 clear_mouse_face (dpyinfo);
22148 }
22149 define_frame_cursor1 (f, cursor, pointer);
22150 }
22151
22152
22153 /* EXPORT:
22154 Take proper action when the mouse has moved to position X, Y on
22155 frame F as regards highlighting characters that have mouse-face
22156 properties. Also de-highlighting chars where the mouse was before.
22157 X and Y can be negative or out of range. */
22158
22159 void
22160 note_mouse_highlight (f, x, y)
22161 struct frame *f;
22162 int x, y;
22163 {
22164 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
22165 enum window_part part;
22166 Lisp_Object window;
22167 struct window *w;
22168 Cursor cursor = No_Cursor;
22169 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
22170 struct buffer *b;
22171
22172 /* When a menu is active, don't highlight because this looks odd. */
22173 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NTGUI)
22174 if (popup_activated ())
22175 return;
22176 #endif
22177
22178 if (NILP (Vmouse_highlight)
22179 || !f->glyphs_initialized_p)
22180 return;
22181
22182 dpyinfo->mouse_face_mouse_x = x;
22183 dpyinfo->mouse_face_mouse_y = y;
22184 dpyinfo->mouse_face_mouse_frame = f;
22185
22186 if (dpyinfo->mouse_face_defer)
22187 return;
22188
22189 if (gc_in_progress)
22190 {
22191 dpyinfo->mouse_face_deferred_gc = 1;
22192 return;
22193 }
22194
22195 /* Which window is that in? */
22196 window = window_from_coordinates (f, x, y, &part, 0, 0, 1);
22197
22198 /* If we were displaying active text in another window, clear that.
22199 Also clear if we move out of text area in same window. */
22200 if (! EQ (window, dpyinfo->mouse_face_window)
22201 || (part != ON_TEXT && part != ON_MODE_LINE && part != ON_HEADER_LINE
22202 && !NILP (dpyinfo->mouse_face_window)))
22203 clear_mouse_face (dpyinfo);
22204
22205 /* Not on a window -> return. */
22206 if (!WINDOWP (window))
22207 return;
22208
22209 /* Reset help_echo_string. It will get recomputed below. */
22210 help_echo_string = Qnil;
22211
22212 /* Convert to window-relative pixel coordinates. */
22213 w = XWINDOW (window);
22214 frame_to_window_pixel_xy (w, &x, &y);
22215
22216 /* Handle tool-bar window differently since it doesn't display a
22217 buffer. */
22218 if (EQ (window, f->tool_bar_window))
22219 {
22220 note_tool_bar_highlight (f, x, y);
22221 return;
22222 }
22223
22224 /* Mouse is on the mode, header line or margin? */
22225 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
22226 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
22227 {
22228 note_mode_line_or_margin_highlight (window, x, y, part);
22229 return;
22230 }
22231
22232 if (part == ON_VERTICAL_BORDER)
22233 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
22234 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
22235 || part == ON_SCROLL_BAR)
22236 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
22237 else
22238 cursor = FRAME_X_OUTPUT (f)->text_cursor;
22239
22240 /* Are we in a window whose display is up to date?
22241 And verify the buffer's text has not changed. */
22242 b = XBUFFER (w->buffer);
22243 if (part == ON_TEXT
22244 && EQ (w->window_end_valid, w->buffer)
22245 && XFASTINT (w->last_modified) == BUF_MODIFF (b)
22246 && XFASTINT (w->last_overlay_modified) == BUF_OVERLAY_MODIFF (b))
22247 {
22248 int hpos, vpos, pos, i, dx, dy, area;
22249 struct glyph *glyph;
22250 Lisp_Object object;
22251 Lisp_Object mouse_face = Qnil, overlay = Qnil, position;
22252 Lisp_Object *overlay_vec = NULL;
22253 int noverlays;
22254 struct buffer *obuf;
22255 int obegv, ozv, same_region;
22256
22257 /* Find the glyph under X/Y. */
22258 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
22259
22260 /* Look for :pointer property on image. */
22261 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
22262 {
22263 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
22264 if (img != NULL && IMAGEP (img->spec))
22265 {
22266 Lisp_Object image_map, hotspot;
22267 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
22268 !NILP (image_map))
22269 && (hotspot = find_hot_spot (image_map,
22270 glyph->slice.x + dx,
22271 glyph->slice.y + dy),
22272 CONSP (hotspot))
22273 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
22274 {
22275 Lisp_Object area_id, plist;
22276
22277 area_id = XCAR (hotspot);
22278 /* Could check AREA_ID to see if we enter/leave this hot-spot.
22279 If so, we could look for mouse-enter, mouse-leave
22280 properties in PLIST (and do something...). */
22281 hotspot = XCDR (hotspot);
22282 if (CONSP (hotspot)
22283 && (plist = XCAR (hotspot), CONSP (plist)))
22284 {
22285 pointer = Fplist_get (plist, Qpointer);
22286 if (NILP (pointer))
22287 pointer = Qhand;
22288 help_echo_string = Fplist_get (plist, Qhelp_echo);
22289 if (!NILP (help_echo_string))
22290 {
22291 help_echo_window = window;
22292 help_echo_object = glyph->object;
22293 help_echo_pos = glyph->charpos;
22294 }
22295 }
22296 }
22297 if (NILP (pointer))
22298 pointer = Fplist_get (XCDR (img->spec), QCpointer);
22299 }
22300 }
22301
22302 /* Clear mouse face if X/Y not over text. */
22303 if (glyph == NULL
22304 || area != TEXT_AREA
22305 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p)
22306 {
22307 if (clear_mouse_face (dpyinfo))
22308 cursor = No_Cursor;
22309 if (NILP (pointer))
22310 {
22311 if (area != TEXT_AREA)
22312 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
22313 else
22314 pointer = Vvoid_text_area_pointer;
22315 }
22316 goto set_cursor;
22317 }
22318
22319 pos = glyph->charpos;
22320 object = glyph->object;
22321 if (!STRINGP (object) && !BUFFERP (object))
22322 goto set_cursor;
22323
22324 /* If we get an out-of-range value, return now; avoid an error. */
22325 if (BUFFERP (object) && pos > BUF_Z (b))
22326 goto set_cursor;
22327
22328 /* Make the window's buffer temporarily current for
22329 overlays_at and compute_char_face. */
22330 obuf = current_buffer;
22331 current_buffer = b;
22332 obegv = BEGV;
22333 ozv = ZV;
22334 BEGV = BEG;
22335 ZV = Z;
22336
22337 /* Is this char mouse-active or does it have help-echo? */
22338 position = make_number (pos);
22339
22340 if (BUFFERP (object))
22341 {
22342 /* Put all the overlays we want in a vector in overlay_vec. */
22343 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
22344 /* Sort overlays into increasing priority order. */
22345 noverlays = sort_overlays (overlay_vec, noverlays, w);
22346 }
22347 else
22348 noverlays = 0;
22349
22350 same_region = (EQ (window, dpyinfo->mouse_face_window)
22351 && vpos >= dpyinfo->mouse_face_beg_row
22352 && vpos <= dpyinfo->mouse_face_end_row
22353 && (vpos > dpyinfo->mouse_face_beg_row
22354 || hpos >= dpyinfo->mouse_face_beg_col)
22355 && (vpos < dpyinfo->mouse_face_end_row
22356 || hpos < dpyinfo->mouse_face_end_col
22357 || dpyinfo->mouse_face_past_end));
22358
22359 if (same_region)
22360 cursor = No_Cursor;
22361
22362 /* Check mouse-face highlighting. */
22363 if (! same_region
22364 /* If there exists an overlay with mouse-face overlapping
22365 the one we are currently highlighting, we have to
22366 check if we enter the overlapping overlay, and then
22367 highlight only that. */
22368 || (OVERLAYP (dpyinfo->mouse_face_overlay)
22369 && mouse_face_overlay_overlaps (dpyinfo->mouse_face_overlay)))
22370 {
22371 /* Find the highest priority overlay that has a mouse-face
22372 property. */
22373 overlay = Qnil;
22374 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
22375 {
22376 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
22377 if (!NILP (mouse_face))
22378 overlay = overlay_vec[i];
22379 }
22380
22381 /* If we're actually highlighting the same overlay as
22382 before, there's no need to do that again. */
22383 if (!NILP (overlay)
22384 && EQ (overlay, dpyinfo->mouse_face_overlay))
22385 goto check_help_echo;
22386
22387 dpyinfo->mouse_face_overlay = overlay;
22388
22389 /* Clear the display of the old active region, if any. */
22390 if (clear_mouse_face (dpyinfo))
22391 cursor = No_Cursor;
22392
22393 /* If no overlay applies, get a text property. */
22394 if (NILP (overlay))
22395 mouse_face = Fget_text_property (position, Qmouse_face, object);
22396
22397 /* Handle the overlay case. */
22398 if (!NILP (overlay))
22399 {
22400 /* Find the range of text around this char that
22401 should be active. */
22402 Lisp_Object before, after;
22403 int ignore;
22404
22405 before = Foverlay_start (overlay);
22406 after = Foverlay_end (overlay);
22407 /* Record this as the current active region. */
22408 fast_find_position (w, XFASTINT (before),
22409 &dpyinfo->mouse_face_beg_col,
22410 &dpyinfo->mouse_face_beg_row,
22411 &dpyinfo->mouse_face_beg_x,
22412 &dpyinfo->mouse_face_beg_y, Qnil);
22413
22414 dpyinfo->mouse_face_past_end
22415 = !fast_find_position (w, XFASTINT (after),
22416 &dpyinfo->mouse_face_end_col,
22417 &dpyinfo->mouse_face_end_row,
22418 &dpyinfo->mouse_face_end_x,
22419 &dpyinfo->mouse_face_end_y, Qnil);
22420 dpyinfo->mouse_face_window = window;
22421
22422 dpyinfo->mouse_face_face_id
22423 = face_at_buffer_position (w, pos, 0, 0,
22424 &ignore, pos + 1,
22425 !dpyinfo->mouse_face_hidden);
22426
22427 /* Display it as active. */
22428 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
22429 cursor = No_Cursor;
22430 }
22431 /* Handle the text property case. */
22432 else if (!NILP (mouse_face) && BUFFERP (object))
22433 {
22434 /* Find the range of text around this char that
22435 should be active. */
22436 Lisp_Object before, after, beginning, end;
22437 int ignore;
22438
22439 beginning = Fmarker_position (w->start);
22440 end = make_number (BUF_Z (XBUFFER (object))
22441 - XFASTINT (w->window_end_pos));
22442 before
22443 = Fprevious_single_property_change (make_number (pos + 1),
22444 Qmouse_face,
22445 object, beginning);
22446 after
22447 = Fnext_single_property_change (position, Qmouse_face,
22448 object, end);
22449
22450 /* Record this as the current active region. */
22451 fast_find_position (w, XFASTINT (before),
22452 &dpyinfo->mouse_face_beg_col,
22453 &dpyinfo->mouse_face_beg_row,
22454 &dpyinfo->mouse_face_beg_x,
22455 &dpyinfo->mouse_face_beg_y, Qnil);
22456 dpyinfo->mouse_face_past_end
22457 = !fast_find_position (w, XFASTINT (after),
22458 &dpyinfo->mouse_face_end_col,
22459 &dpyinfo->mouse_face_end_row,
22460 &dpyinfo->mouse_face_end_x,
22461 &dpyinfo->mouse_face_end_y, Qnil);
22462 dpyinfo->mouse_face_window = window;
22463
22464 if (BUFFERP (object))
22465 dpyinfo->mouse_face_face_id
22466 = face_at_buffer_position (w, pos, 0, 0,
22467 &ignore, pos + 1,
22468 !dpyinfo->mouse_face_hidden);
22469
22470 /* Display it as active. */
22471 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
22472 cursor = No_Cursor;
22473 }
22474 else if (!NILP (mouse_face) && STRINGP (object))
22475 {
22476 Lisp_Object b, e;
22477 int ignore;
22478
22479 b = Fprevious_single_property_change (make_number (pos + 1),
22480 Qmouse_face,
22481 object, Qnil);
22482 e = Fnext_single_property_change (position, Qmouse_face,
22483 object, Qnil);
22484 if (NILP (b))
22485 b = make_number (0);
22486 if (NILP (e))
22487 e = make_number (SCHARS (object) - 1);
22488
22489 fast_find_string_pos (w, XINT (b), object,
22490 &dpyinfo->mouse_face_beg_col,
22491 &dpyinfo->mouse_face_beg_row,
22492 &dpyinfo->mouse_face_beg_x,
22493 &dpyinfo->mouse_face_beg_y, 0);
22494 fast_find_string_pos (w, XINT (e), object,
22495 &dpyinfo->mouse_face_end_col,
22496 &dpyinfo->mouse_face_end_row,
22497 &dpyinfo->mouse_face_end_x,
22498 &dpyinfo->mouse_face_end_y, 1);
22499 dpyinfo->mouse_face_past_end = 0;
22500 dpyinfo->mouse_face_window = window;
22501 dpyinfo->mouse_face_face_id
22502 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
22503 glyph->face_id, 1);
22504 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
22505 cursor = No_Cursor;
22506 }
22507 else if (STRINGP (object) && NILP (mouse_face))
22508 {
22509 /* A string which doesn't have mouse-face, but
22510 the text ``under'' it might have. */
22511 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
22512 int start = MATRIX_ROW_START_CHARPOS (r);
22513
22514 pos = string_buffer_position (w, object, start);
22515 if (pos > 0)
22516 mouse_face = get_char_property_and_overlay (make_number (pos),
22517 Qmouse_face,
22518 w->buffer,
22519 &overlay);
22520 if (!NILP (mouse_face) && !NILP (overlay))
22521 {
22522 Lisp_Object before = Foverlay_start (overlay);
22523 Lisp_Object after = Foverlay_end (overlay);
22524 int ignore;
22525
22526 /* Note that we might not be able to find position
22527 BEFORE in the glyph matrix if the overlay is
22528 entirely covered by a `display' property. In
22529 this case, we overshoot. So let's stop in
22530 the glyph matrix before glyphs for OBJECT. */
22531 fast_find_position (w, XFASTINT (before),
22532 &dpyinfo->mouse_face_beg_col,
22533 &dpyinfo->mouse_face_beg_row,
22534 &dpyinfo->mouse_face_beg_x,
22535 &dpyinfo->mouse_face_beg_y,
22536 object);
22537
22538 dpyinfo->mouse_face_past_end
22539 = !fast_find_position (w, XFASTINT (after),
22540 &dpyinfo->mouse_face_end_col,
22541 &dpyinfo->mouse_face_end_row,
22542 &dpyinfo->mouse_face_end_x,
22543 &dpyinfo->mouse_face_end_y,
22544 Qnil);
22545 dpyinfo->mouse_face_window = window;
22546 dpyinfo->mouse_face_face_id
22547 = face_at_buffer_position (w, pos, 0, 0,
22548 &ignore, pos + 1,
22549 !dpyinfo->mouse_face_hidden);
22550
22551 /* Display it as active. */
22552 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
22553 cursor = No_Cursor;
22554 }
22555 }
22556 }
22557
22558 check_help_echo:
22559
22560 /* Look for a `help-echo' property. */
22561 if (NILP (help_echo_string)) {
22562 Lisp_Object help, overlay;
22563
22564 /* Check overlays first. */
22565 help = overlay = Qnil;
22566 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
22567 {
22568 overlay = overlay_vec[i];
22569 help = Foverlay_get (overlay, Qhelp_echo);
22570 }
22571
22572 if (!NILP (help))
22573 {
22574 help_echo_string = help;
22575 help_echo_window = window;
22576 help_echo_object = overlay;
22577 help_echo_pos = pos;
22578 }
22579 else
22580 {
22581 Lisp_Object object = glyph->object;
22582 int charpos = glyph->charpos;
22583
22584 /* Try text properties. */
22585 if (STRINGP (object)
22586 && charpos >= 0
22587 && charpos < SCHARS (object))
22588 {
22589 help = Fget_text_property (make_number (charpos),
22590 Qhelp_echo, object);
22591 if (NILP (help))
22592 {
22593 /* If the string itself doesn't specify a help-echo,
22594 see if the buffer text ``under'' it does. */
22595 struct glyph_row *r
22596 = MATRIX_ROW (w->current_matrix, vpos);
22597 int start = MATRIX_ROW_START_CHARPOS (r);
22598 int pos = string_buffer_position (w, object, start);
22599 if (pos > 0)
22600 {
22601 help = Fget_char_property (make_number (pos),
22602 Qhelp_echo, w->buffer);
22603 if (!NILP (help))
22604 {
22605 charpos = pos;
22606 object = w->buffer;
22607 }
22608 }
22609 }
22610 }
22611 else if (BUFFERP (object)
22612 && charpos >= BEGV
22613 && charpos < ZV)
22614 help = Fget_text_property (make_number (charpos), Qhelp_echo,
22615 object);
22616
22617 if (!NILP (help))
22618 {
22619 help_echo_string = help;
22620 help_echo_window = window;
22621 help_echo_object = object;
22622 help_echo_pos = charpos;
22623 }
22624 }
22625 }
22626
22627 /* Look for a `pointer' property. */
22628 if (NILP (pointer))
22629 {
22630 /* Check overlays first. */
22631 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
22632 pointer = Foverlay_get (overlay_vec[i], Qpointer);
22633
22634 if (NILP (pointer))
22635 {
22636 Lisp_Object object = glyph->object;
22637 int charpos = glyph->charpos;
22638
22639 /* Try text properties. */
22640 if (STRINGP (object)
22641 && charpos >= 0
22642 && charpos < SCHARS (object))
22643 {
22644 pointer = Fget_text_property (make_number (charpos),
22645 Qpointer, object);
22646 if (NILP (pointer))
22647 {
22648 /* If the string itself doesn't specify a pointer,
22649 see if the buffer text ``under'' it does. */
22650 struct glyph_row *r
22651 = MATRIX_ROW (w->current_matrix, vpos);
22652 int start = MATRIX_ROW_START_CHARPOS (r);
22653 int pos = string_buffer_position (w, object, start);
22654 if (pos > 0)
22655 pointer = Fget_char_property (make_number (pos),
22656 Qpointer, w->buffer);
22657 }
22658 }
22659 else if (BUFFERP (object)
22660 && charpos >= BEGV
22661 && charpos < ZV)
22662 pointer = Fget_text_property (make_number (charpos),
22663 Qpointer, object);
22664 }
22665 }
22666
22667 BEGV = obegv;
22668 ZV = ozv;
22669 current_buffer = obuf;
22670 }
22671
22672 set_cursor:
22673
22674 define_frame_cursor1 (f, cursor, pointer);
22675 }
22676
22677
22678 /* EXPORT for RIF:
22679 Clear any mouse-face on window W. This function is part of the
22680 redisplay interface, and is called from try_window_id and similar
22681 functions to ensure the mouse-highlight is off. */
22682
22683 void
22684 x_clear_window_mouse_face (w)
22685 struct window *w;
22686 {
22687 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
22688 Lisp_Object window;
22689
22690 BLOCK_INPUT;
22691 XSETWINDOW (window, w);
22692 if (EQ (window, dpyinfo->mouse_face_window))
22693 clear_mouse_face (dpyinfo);
22694 UNBLOCK_INPUT;
22695 }
22696
22697
22698 /* EXPORT:
22699 Just discard the mouse face information for frame F, if any.
22700 This is used when the size of F is changed. */
22701
22702 void
22703 cancel_mouse_face (f)
22704 struct frame *f;
22705 {
22706 Lisp_Object window;
22707 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
22708
22709 window = dpyinfo->mouse_face_window;
22710 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
22711 {
22712 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
22713 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
22714 dpyinfo->mouse_face_window = Qnil;
22715 }
22716 }
22717
22718
22719 #endif /* HAVE_WINDOW_SYSTEM */
22720
22721 \f
22722 /***********************************************************************
22723 Exposure Events
22724 ***********************************************************************/
22725
22726 #ifdef HAVE_WINDOW_SYSTEM
22727
22728 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
22729 which intersects rectangle R. R is in window-relative coordinates. */
22730
22731 static void
22732 expose_area (w, row, r, area)
22733 struct window *w;
22734 struct glyph_row *row;
22735 XRectangle *r;
22736 enum glyph_row_area area;
22737 {
22738 struct glyph *first = row->glyphs[area];
22739 struct glyph *end = row->glyphs[area] + row->used[area];
22740 struct glyph *last;
22741 int first_x, start_x, x;
22742
22743 if (area == TEXT_AREA && row->fill_line_p)
22744 /* If row extends face to end of line write the whole line. */
22745 draw_glyphs (w, 0, row, area,
22746 0, row->used[area],
22747 DRAW_NORMAL_TEXT, 0);
22748 else
22749 {
22750 /* Set START_X to the window-relative start position for drawing glyphs of
22751 AREA. The first glyph of the text area can be partially visible.
22752 The first glyphs of other areas cannot. */
22753 start_x = window_box_left_offset (w, area);
22754 x = start_x;
22755 if (area == TEXT_AREA)
22756 x += row->x;
22757
22758 /* Find the first glyph that must be redrawn. */
22759 while (first < end
22760 && x + first->pixel_width < r->x)
22761 {
22762 x += first->pixel_width;
22763 ++first;
22764 }
22765
22766 /* Find the last one. */
22767 last = first;
22768 first_x = x;
22769 while (last < end
22770 && x < r->x + r->width)
22771 {
22772 x += last->pixel_width;
22773 ++last;
22774 }
22775
22776 /* Repaint. */
22777 if (last > first)
22778 draw_glyphs (w, first_x - start_x, row, area,
22779 first - row->glyphs[area], last - row->glyphs[area],
22780 DRAW_NORMAL_TEXT, 0);
22781 }
22782 }
22783
22784
22785 /* Redraw the parts of the glyph row ROW on window W intersecting
22786 rectangle R. R is in window-relative coordinates. Value is
22787 non-zero if mouse-face was overwritten. */
22788
22789 static int
22790 expose_line (w, row, r)
22791 struct window *w;
22792 struct glyph_row *row;
22793 XRectangle *r;
22794 {
22795 xassert (row->enabled_p);
22796
22797 if (row->mode_line_p || w->pseudo_window_p)
22798 draw_glyphs (w, 0, row, TEXT_AREA,
22799 0, row->used[TEXT_AREA],
22800 DRAW_NORMAL_TEXT, 0);
22801 else
22802 {
22803 if (row->used[LEFT_MARGIN_AREA])
22804 expose_area (w, row, r, LEFT_MARGIN_AREA);
22805 if (row->used[TEXT_AREA])
22806 expose_area (w, row, r, TEXT_AREA);
22807 if (row->used[RIGHT_MARGIN_AREA])
22808 expose_area (w, row, r, RIGHT_MARGIN_AREA);
22809 draw_row_fringe_bitmaps (w, row);
22810 }
22811
22812 return row->mouse_face_p;
22813 }
22814
22815
22816 /* Redraw those parts of glyphs rows during expose event handling that
22817 overlap other rows. Redrawing of an exposed line writes over parts
22818 of lines overlapping that exposed line; this function fixes that.
22819
22820 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
22821 row in W's current matrix that is exposed and overlaps other rows.
22822 LAST_OVERLAPPING_ROW is the last such row. */
22823
22824 static void
22825 expose_overlaps (w, first_overlapping_row, last_overlapping_row)
22826 struct window *w;
22827 struct glyph_row *first_overlapping_row;
22828 struct glyph_row *last_overlapping_row;
22829 {
22830 struct glyph_row *row;
22831
22832 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
22833 if (row->overlapping_p)
22834 {
22835 xassert (row->enabled_p && !row->mode_line_p);
22836
22837 if (row->used[LEFT_MARGIN_AREA])
22838 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
22839
22840 if (row->used[TEXT_AREA])
22841 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
22842
22843 if (row->used[RIGHT_MARGIN_AREA])
22844 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
22845 }
22846 }
22847
22848
22849 /* Return non-zero if W's cursor intersects rectangle R. */
22850
22851 static int
22852 phys_cursor_in_rect_p (w, r)
22853 struct window *w;
22854 XRectangle *r;
22855 {
22856 XRectangle cr, result;
22857 struct glyph *cursor_glyph;
22858
22859 cursor_glyph = get_phys_cursor_glyph (w);
22860 if (cursor_glyph)
22861 {
22862 /* r is relative to W's box, but w->phys_cursor.x is relative
22863 to left edge of W's TEXT area. Adjust it. */
22864 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
22865 cr.y = w->phys_cursor.y;
22866 cr.width = cursor_glyph->pixel_width;
22867 cr.height = w->phys_cursor_height;
22868 /* ++KFS: W32 version used W32-specific IntersectRect here, but
22869 I assume the effect is the same -- and this is portable. */
22870 return x_intersect_rectangles (&cr, r, &result);
22871 }
22872 /* If we don't understand the format, pretend we're not in the hot-spot. */
22873 return 0;
22874 }
22875
22876
22877 /* EXPORT:
22878 Draw a vertical window border to the right of window W if W doesn't
22879 have vertical scroll bars. */
22880
22881 void
22882 x_draw_vertical_border (w)
22883 struct window *w;
22884 {
22885 struct frame *f = XFRAME (WINDOW_FRAME (w));
22886
22887 /* We could do better, if we knew what type of scroll-bar the adjacent
22888 windows (on either side) have... But we don't :-(
22889 However, I think this works ok. ++KFS 2003-04-25 */
22890
22891 /* Redraw borders between horizontally adjacent windows. Don't
22892 do it for frames with vertical scroll bars because either the
22893 right scroll bar of a window, or the left scroll bar of its
22894 neighbor will suffice as a border. */
22895 if (FRAME_HAS_VERTICAL_SCROLL_BARS (XFRAME (w->frame)))
22896 return;
22897
22898 if (!WINDOW_RIGHTMOST_P (w)
22899 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
22900 {
22901 int x0, x1, y0, y1;
22902
22903 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
22904 y1 -= 1;
22905
22906 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
22907 x1 -= 1;
22908
22909 FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
22910 }
22911 else if (!WINDOW_LEFTMOST_P (w)
22912 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
22913 {
22914 int x0, x1, y0, y1;
22915
22916 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
22917 y1 -= 1;
22918
22919 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
22920 x0 -= 1;
22921
22922 FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
22923 }
22924 }
22925
22926
22927 /* Redraw the part of window W intersection rectangle FR. Pixel
22928 coordinates in FR are frame-relative. Call this function with
22929 input blocked. Value is non-zero if the exposure overwrites
22930 mouse-face. */
22931
22932 static int
22933 expose_window (w, fr)
22934 struct window *w;
22935 XRectangle *fr;
22936 {
22937 struct frame *f = XFRAME (w->frame);
22938 XRectangle wr, r;
22939 int mouse_face_overwritten_p = 0;
22940
22941 /* If window is not yet fully initialized, do nothing. This can
22942 happen when toolkit scroll bars are used and a window is split.
22943 Reconfiguring the scroll bar will generate an expose for a newly
22944 created window. */
22945 if (w->current_matrix == NULL)
22946 return 0;
22947
22948 /* When we're currently updating the window, display and current
22949 matrix usually don't agree. Arrange for a thorough display
22950 later. */
22951 if (w == updated_window)
22952 {
22953 SET_FRAME_GARBAGED (f);
22954 return 0;
22955 }
22956
22957 /* Frame-relative pixel rectangle of W. */
22958 wr.x = WINDOW_LEFT_EDGE_X (w);
22959 wr.y = WINDOW_TOP_EDGE_Y (w);
22960 wr.width = WINDOW_TOTAL_WIDTH (w);
22961 wr.height = WINDOW_TOTAL_HEIGHT (w);
22962
22963 if (x_intersect_rectangles (fr, &wr, &r))
22964 {
22965 int yb = window_text_bottom_y (w);
22966 struct glyph_row *row;
22967 int cursor_cleared_p;
22968 struct glyph_row *first_overlapping_row, *last_overlapping_row;
22969
22970 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
22971 r.x, r.y, r.width, r.height));
22972
22973 /* Convert to window coordinates. */
22974 r.x -= WINDOW_LEFT_EDGE_X (w);
22975 r.y -= WINDOW_TOP_EDGE_Y (w);
22976
22977 /* Turn off the cursor. */
22978 if (!w->pseudo_window_p
22979 && phys_cursor_in_rect_p (w, &r))
22980 {
22981 x_clear_cursor (w);
22982 cursor_cleared_p = 1;
22983 }
22984 else
22985 cursor_cleared_p = 0;
22986
22987 /* Update lines intersecting rectangle R. */
22988 first_overlapping_row = last_overlapping_row = NULL;
22989 for (row = w->current_matrix->rows;
22990 row->enabled_p;
22991 ++row)
22992 {
22993 int y0 = row->y;
22994 int y1 = MATRIX_ROW_BOTTOM_Y (row);
22995
22996 if ((y0 >= r.y && y0 < r.y + r.height)
22997 || (y1 > r.y && y1 < r.y + r.height)
22998 || (r.y >= y0 && r.y < y1)
22999 || (r.y + r.height > y0 && r.y + r.height < y1))
23000 {
23001 /* A header line may be overlapping, but there is no need
23002 to fix overlapping areas for them. KFS 2005-02-12 */
23003 if (row->overlapping_p && !row->mode_line_p)
23004 {
23005 if (first_overlapping_row == NULL)
23006 first_overlapping_row = row;
23007 last_overlapping_row = row;
23008 }
23009
23010 if (expose_line (w, row, &r))
23011 mouse_face_overwritten_p = 1;
23012 }
23013
23014 if (y1 >= yb)
23015 break;
23016 }
23017
23018 /* Display the mode line if there is one. */
23019 if (WINDOW_WANTS_MODELINE_P (w)
23020 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
23021 row->enabled_p)
23022 && row->y < r.y + r.height)
23023 {
23024 if (expose_line (w, row, &r))
23025 mouse_face_overwritten_p = 1;
23026 }
23027
23028 if (!w->pseudo_window_p)
23029 {
23030 /* Fix the display of overlapping rows. */
23031 if (first_overlapping_row)
23032 expose_overlaps (w, first_overlapping_row, last_overlapping_row);
23033
23034 /* Draw border between windows. */
23035 x_draw_vertical_border (w);
23036
23037 /* Turn the cursor on again. */
23038 if (cursor_cleared_p)
23039 update_window_cursor (w, 1);
23040 }
23041 }
23042
23043 return mouse_face_overwritten_p;
23044 }
23045
23046
23047
23048 /* Redraw (parts) of all windows in the window tree rooted at W that
23049 intersect R. R contains frame pixel coordinates. Value is
23050 non-zero if the exposure overwrites mouse-face. */
23051
23052 static int
23053 expose_window_tree (w, r)
23054 struct window *w;
23055 XRectangle *r;
23056 {
23057 struct frame *f = XFRAME (w->frame);
23058 int mouse_face_overwritten_p = 0;
23059
23060 while (w && !FRAME_GARBAGED_P (f))
23061 {
23062 if (!NILP (w->hchild))
23063 mouse_face_overwritten_p
23064 |= expose_window_tree (XWINDOW (w->hchild), r);
23065 else if (!NILP (w->vchild))
23066 mouse_face_overwritten_p
23067 |= expose_window_tree (XWINDOW (w->vchild), r);
23068 else
23069 mouse_face_overwritten_p |= expose_window (w, r);
23070
23071 w = NILP (w->next) ? NULL : XWINDOW (w->next);
23072 }
23073
23074 return mouse_face_overwritten_p;
23075 }
23076
23077
23078 /* EXPORT:
23079 Redisplay an exposed area of frame F. X and Y are the upper-left
23080 corner of the exposed rectangle. W and H are width and height of
23081 the exposed area. All are pixel values. W or H zero means redraw
23082 the entire frame. */
23083
23084 void
23085 expose_frame (f, x, y, w, h)
23086 struct frame *f;
23087 int x, y, w, h;
23088 {
23089 XRectangle r;
23090 int mouse_face_overwritten_p = 0;
23091
23092 TRACE ((stderr, "expose_frame "));
23093
23094 /* No need to redraw if frame will be redrawn soon. */
23095 if (FRAME_GARBAGED_P (f))
23096 {
23097 TRACE ((stderr, " garbaged\n"));
23098 return;
23099 }
23100
23101 /* If basic faces haven't been realized yet, there is no point in
23102 trying to redraw anything. This can happen when we get an expose
23103 event while Emacs is starting, e.g. by moving another window. */
23104 if (FRAME_FACE_CACHE (f) == NULL
23105 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
23106 {
23107 TRACE ((stderr, " no faces\n"));
23108 return;
23109 }
23110
23111 if (w == 0 || h == 0)
23112 {
23113 r.x = r.y = 0;
23114 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
23115 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
23116 }
23117 else
23118 {
23119 r.x = x;
23120 r.y = y;
23121 r.width = w;
23122 r.height = h;
23123 }
23124
23125 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
23126 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
23127
23128 if (WINDOWP (f->tool_bar_window))
23129 mouse_face_overwritten_p
23130 |= expose_window (XWINDOW (f->tool_bar_window), &r);
23131
23132 #ifdef HAVE_X_WINDOWS
23133 #ifndef MSDOS
23134 #ifndef USE_X_TOOLKIT
23135 if (WINDOWP (f->menu_bar_window))
23136 mouse_face_overwritten_p
23137 |= expose_window (XWINDOW (f->menu_bar_window), &r);
23138 #endif /* not USE_X_TOOLKIT */
23139 #endif
23140 #endif
23141
23142 /* Some window managers support a focus-follows-mouse style with
23143 delayed raising of frames. Imagine a partially obscured frame,
23144 and moving the mouse into partially obscured mouse-face on that
23145 frame. The visible part of the mouse-face will be highlighted,
23146 then the WM raises the obscured frame. With at least one WM, KDE
23147 2.1, Emacs is not getting any event for the raising of the frame
23148 (even tried with SubstructureRedirectMask), only Expose events.
23149 These expose events will draw text normally, i.e. not
23150 highlighted. Which means we must redo the highlight here.
23151 Subsume it under ``we love X''. --gerd 2001-08-15 */
23152 /* Included in Windows version because Windows most likely does not
23153 do the right thing if any third party tool offers
23154 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
23155 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
23156 {
23157 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
23158 if (f == dpyinfo->mouse_face_mouse_frame)
23159 {
23160 int x = dpyinfo->mouse_face_mouse_x;
23161 int y = dpyinfo->mouse_face_mouse_y;
23162 clear_mouse_face (dpyinfo);
23163 note_mouse_highlight (f, x, y);
23164 }
23165 }
23166 }
23167
23168
23169 /* EXPORT:
23170 Determine the intersection of two rectangles R1 and R2. Return
23171 the intersection in *RESULT. Value is non-zero if RESULT is not
23172 empty. */
23173
23174 int
23175 x_intersect_rectangles (r1, r2, result)
23176 XRectangle *r1, *r2, *result;
23177 {
23178 XRectangle *left, *right;
23179 XRectangle *upper, *lower;
23180 int intersection_p = 0;
23181
23182 /* Rearrange so that R1 is the left-most rectangle. */
23183 if (r1->x < r2->x)
23184 left = r1, right = r2;
23185 else
23186 left = r2, right = r1;
23187
23188 /* X0 of the intersection is right.x0, if this is inside R1,
23189 otherwise there is no intersection. */
23190 if (right->x <= left->x + left->width)
23191 {
23192 result->x = right->x;
23193
23194 /* The right end of the intersection is the minimum of the
23195 the right ends of left and right. */
23196 result->width = (min (left->x + left->width, right->x + right->width)
23197 - result->x);
23198
23199 /* Same game for Y. */
23200 if (r1->y < r2->y)
23201 upper = r1, lower = r2;
23202 else
23203 upper = r2, lower = r1;
23204
23205 /* The upper end of the intersection is lower.y0, if this is inside
23206 of upper. Otherwise, there is no intersection. */
23207 if (lower->y <= upper->y + upper->height)
23208 {
23209 result->y = lower->y;
23210
23211 /* The lower end of the intersection is the minimum of the lower
23212 ends of upper and lower. */
23213 result->height = (min (lower->y + lower->height,
23214 upper->y + upper->height)
23215 - result->y);
23216 intersection_p = 1;
23217 }
23218 }
23219
23220 return intersection_p;
23221 }
23222
23223 #endif /* HAVE_WINDOW_SYSTEM */
23224
23225 \f
23226 /***********************************************************************
23227 Initialization
23228 ***********************************************************************/
23229
23230 void
23231 syms_of_xdisp ()
23232 {
23233 Vwith_echo_area_save_vector = Qnil;
23234 staticpro (&Vwith_echo_area_save_vector);
23235
23236 Vmessage_stack = Qnil;
23237 staticpro (&Vmessage_stack);
23238
23239 Qinhibit_redisplay = intern ("inhibit-redisplay");
23240 staticpro (&Qinhibit_redisplay);
23241
23242 message_dolog_marker1 = Fmake_marker ();
23243 staticpro (&message_dolog_marker1);
23244 message_dolog_marker2 = Fmake_marker ();
23245 staticpro (&message_dolog_marker2);
23246 message_dolog_marker3 = Fmake_marker ();
23247 staticpro (&message_dolog_marker3);
23248
23249 #if GLYPH_DEBUG
23250 defsubr (&Sdump_frame_glyph_matrix);
23251 defsubr (&Sdump_glyph_matrix);
23252 defsubr (&Sdump_glyph_row);
23253 defsubr (&Sdump_tool_bar_row);
23254 defsubr (&Strace_redisplay);
23255 defsubr (&Strace_to_stderr);
23256 #endif
23257 #ifdef HAVE_WINDOW_SYSTEM
23258 defsubr (&Stool_bar_lines_needed);
23259 defsubr (&Slookup_image_map);
23260 #endif
23261 defsubr (&Sformat_mode_line);
23262
23263 staticpro (&Qmenu_bar_update_hook);
23264 Qmenu_bar_update_hook = intern ("menu-bar-update-hook");
23265
23266 staticpro (&Qoverriding_terminal_local_map);
23267 Qoverriding_terminal_local_map = intern ("overriding-terminal-local-map");
23268
23269 staticpro (&Qoverriding_local_map);
23270 Qoverriding_local_map = intern ("overriding-local-map");
23271
23272 staticpro (&Qwindow_scroll_functions);
23273 Qwindow_scroll_functions = intern ("window-scroll-functions");
23274
23275 staticpro (&Qredisplay_end_trigger_functions);
23276 Qredisplay_end_trigger_functions = intern ("redisplay-end-trigger-functions");
23277
23278 staticpro (&Qinhibit_point_motion_hooks);
23279 Qinhibit_point_motion_hooks = intern ("inhibit-point-motion-hooks");
23280
23281 QCdata = intern (":data");
23282 staticpro (&QCdata);
23283 Qdisplay = intern ("display");
23284 staticpro (&Qdisplay);
23285 Qspace_width = intern ("space-width");
23286 staticpro (&Qspace_width);
23287 Qraise = intern ("raise");
23288 staticpro (&Qraise);
23289 Qslice = intern ("slice");
23290 staticpro (&Qslice);
23291 Qspace = intern ("space");
23292 staticpro (&Qspace);
23293 Qmargin = intern ("margin");
23294 staticpro (&Qmargin);
23295 Qpointer = intern ("pointer");
23296 staticpro (&Qpointer);
23297 Qleft_margin = intern ("left-margin");
23298 staticpro (&Qleft_margin);
23299 Qright_margin = intern ("right-margin");
23300 staticpro (&Qright_margin);
23301 Qcenter = intern ("center");
23302 staticpro (&Qcenter);
23303 Qline_height = intern ("line-height");
23304 staticpro (&Qline_height);
23305 QCalign_to = intern (":align-to");
23306 staticpro (&QCalign_to);
23307 QCrelative_width = intern (":relative-width");
23308 staticpro (&QCrelative_width);
23309 QCrelative_height = intern (":relative-height");
23310 staticpro (&QCrelative_height);
23311 QCeval = intern (":eval");
23312 staticpro (&QCeval);
23313 QCpropertize = intern (":propertize");
23314 staticpro (&QCpropertize);
23315 QCfile = intern (":file");
23316 staticpro (&QCfile);
23317 Qfontified = intern ("fontified");
23318 staticpro (&Qfontified);
23319 Qfontification_functions = intern ("fontification-functions");
23320 staticpro (&Qfontification_functions);
23321 Qtrailing_whitespace = intern ("trailing-whitespace");
23322 staticpro (&Qtrailing_whitespace);
23323 Qescape_glyph = intern ("escape-glyph");
23324 staticpro (&Qescape_glyph);
23325 Qnobreak_space = intern ("nobreak-space");
23326 staticpro (&Qnobreak_space);
23327 Qimage = intern ("image");
23328 staticpro (&Qimage);
23329 QCmap = intern (":map");
23330 staticpro (&QCmap);
23331 QCpointer = intern (":pointer");
23332 staticpro (&QCpointer);
23333 Qrect = intern ("rect");
23334 staticpro (&Qrect);
23335 Qcircle = intern ("circle");
23336 staticpro (&Qcircle);
23337 Qpoly = intern ("poly");
23338 staticpro (&Qpoly);
23339 Qmessage_truncate_lines = intern ("message-truncate-lines");
23340 staticpro (&Qmessage_truncate_lines);
23341 Qgrow_only = intern ("grow-only");
23342 staticpro (&Qgrow_only);
23343 Qinhibit_menubar_update = intern ("inhibit-menubar-update");
23344 staticpro (&Qinhibit_menubar_update);
23345 Qinhibit_eval_during_redisplay = intern ("inhibit-eval-during-redisplay");
23346 staticpro (&Qinhibit_eval_during_redisplay);
23347 Qposition = intern ("position");
23348 staticpro (&Qposition);
23349 Qbuffer_position = intern ("buffer-position");
23350 staticpro (&Qbuffer_position);
23351 Qobject = intern ("object");
23352 staticpro (&Qobject);
23353 Qbar = intern ("bar");
23354 staticpro (&Qbar);
23355 Qhbar = intern ("hbar");
23356 staticpro (&Qhbar);
23357 Qbox = intern ("box");
23358 staticpro (&Qbox);
23359 Qhollow = intern ("hollow");
23360 staticpro (&Qhollow);
23361 Qhand = intern ("hand");
23362 staticpro (&Qhand);
23363 Qarrow = intern ("arrow");
23364 staticpro (&Qarrow);
23365 Qtext = intern ("text");
23366 staticpro (&Qtext);
23367 Qrisky_local_variable = intern ("risky-local-variable");
23368 staticpro (&Qrisky_local_variable);
23369 Qinhibit_free_realized_faces = intern ("inhibit-free-realized-faces");
23370 staticpro (&Qinhibit_free_realized_faces);
23371
23372 list_of_error = Fcons (Fcons (intern ("error"),
23373 Fcons (intern ("void-variable"), Qnil)),
23374 Qnil);
23375 staticpro (&list_of_error);
23376
23377 Qlast_arrow_position = intern ("last-arrow-position");
23378 staticpro (&Qlast_arrow_position);
23379 Qlast_arrow_string = intern ("last-arrow-string");
23380 staticpro (&Qlast_arrow_string);
23381
23382 Qoverlay_arrow_string = intern ("overlay-arrow-string");
23383 staticpro (&Qoverlay_arrow_string);
23384 Qoverlay_arrow_bitmap = intern ("overlay-arrow-bitmap");
23385 staticpro (&Qoverlay_arrow_bitmap);
23386
23387 echo_buffer[0] = echo_buffer[1] = Qnil;
23388 staticpro (&echo_buffer[0]);
23389 staticpro (&echo_buffer[1]);
23390
23391 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
23392 staticpro (&echo_area_buffer[0]);
23393 staticpro (&echo_area_buffer[1]);
23394
23395 Vmessages_buffer_name = build_string ("*Messages*");
23396 staticpro (&Vmessages_buffer_name);
23397
23398 mode_line_proptrans_alist = Qnil;
23399 staticpro (&mode_line_proptrans_alist);
23400 mode_line_string_list = Qnil;
23401 staticpro (&mode_line_string_list);
23402 mode_line_string_face = Qnil;
23403 staticpro (&mode_line_string_face);
23404 mode_line_string_face_prop = Qnil;
23405 staticpro (&mode_line_string_face_prop);
23406 Vmode_line_unwind_vector = Qnil;
23407 staticpro (&Vmode_line_unwind_vector);
23408
23409 help_echo_string = Qnil;
23410 staticpro (&help_echo_string);
23411 help_echo_object = Qnil;
23412 staticpro (&help_echo_object);
23413 help_echo_window = Qnil;
23414 staticpro (&help_echo_window);
23415 previous_help_echo_string = Qnil;
23416 staticpro (&previous_help_echo_string);
23417 help_echo_pos = -1;
23418
23419 #ifdef HAVE_WINDOW_SYSTEM
23420 DEFVAR_BOOL ("x-stretch-cursor", &x_stretch_cursor_p,
23421 doc: /* *Non-nil means draw block cursor as wide as the glyph under it.
23422 For example, if a block cursor is over a tab, it will be drawn as
23423 wide as that tab on the display. */);
23424 x_stretch_cursor_p = 0;
23425 #endif
23426
23427 DEFVAR_LISP ("show-trailing-whitespace", &Vshow_trailing_whitespace,
23428 doc: /* *Non-nil means highlight trailing whitespace.
23429 The face used for trailing whitespace is `trailing-whitespace'. */);
23430 Vshow_trailing_whitespace = Qnil;
23431
23432 DEFVAR_LISP ("nobreak-char-display", &Vnobreak_char_display,
23433 doc: /* *Control highlighting of nobreak space and soft hyphen.
23434 A value of t means highlight the character itself (for nobreak space,
23435 use face `nobreak-space').
23436 A value of nil means no highlighting.
23437 Other values mean display the escape glyph followed by an ordinary
23438 space or ordinary hyphen. */);
23439 Vnobreak_char_display = Qt;
23440
23441 DEFVAR_LISP ("void-text-area-pointer", &Vvoid_text_area_pointer,
23442 doc: /* *The pointer shape to show in void text areas.
23443 A value of nil means to show the text pointer. Other options are `arrow',
23444 `text', `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
23445 Vvoid_text_area_pointer = Qarrow;
23446
23447 DEFVAR_LISP ("inhibit-redisplay", &Vinhibit_redisplay,
23448 doc: /* Non-nil means don't actually do any redisplay.
23449 This is used for internal purposes. */);
23450 Vinhibit_redisplay = Qnil;
23451
23452 DEFVAR_LISP ("global-mode-string", &Vglobal_mode_string,
23453 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
23454 Vglobal_mode_string = Qnil;
23455
23456 DEFVAR_LISP ("overlay-arrow-position", &Voverlay_arrow_position,
23457 doc: /* Marker for where to display an arrow on top of the buffer text.
23458 This must be the beginning of a line in order to work.
23459 See also `overlay-arrow-string'. */);
23460 Voverlay_arrow_position = Qnil;
23461
23462 DEFVAR_LISP ("overlay-arrow-string", &Voverlay_arrow_string,
23463 doc: /* String to display as an arrow in non-window frames.
23464 See also `overlay-arrow-position'. */);
23465 Voverlay_arrow_string = build_string ("=>");
23466
23467 DEFVAR_LISP ("overlay-arrow-variable-list", &Voverlay_arrow_variable_list,
23468 doc: /* List of variables (symbols) which hold markers for overlay arrows.
23469 The symbols on this list are examined during redisplay to determine
23470 where to display overlay arrows. */);
23471 Voverlay_arrow_variable_list
23472 = Fcons (intern ("overlay-arrow-position"), Qnil);
23473
23474 DEFVAR_INT ("scroll-step", &scroll_step,
23475 doc: /* *The number of lines to try scrolling a window by when point moves out.
23476 If that fails to bring point back on frame, point is centered instead.
23477 If this is zero, point is always centered after it moves off frame.
23478 If you want scrolling to always be a line at a time, you should set
23479 `scroll-conservatively' to a large value rather than set this to 1. */);
23480
23481 DEFVAR_INT ("scroll-conservatively", &scroll_conservatively,
23482 doc: /* *Scroll up to this many lines, to bring point back on screen.
23483 A value of zero means to scroll the text to center point vertically
23484 in the window. */);
23485 scroll_conservatively = 0;
23486
23487 DEFVAR_INT ("scroll-margin", &scroll_margin,
23488 doc: /* *Number of lines of margin at the top and bottom of a window.
23489 Recenter the window whenever point gets within this many lines
23490 of the top or bottom of the window. */);
23491 scroll_margin = 0;
23492
23493 DEFVAR_LISP ("display-pixels-per-inch", &Vdisplay_pixels_per_inch,
23494 doc: /* Pixels per inch value for non-window system displays.
23495 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
23496 Vdisplay_pixels_per_inch = make_float (72.0);
23497
23498 #if GLYPH_DEBUG
23499 DEFVAR_INT ("debug-end-pos", &debug_end_pos, doc: /* Don't ask. */);
23500 #endif
23501
23502 DEFVAR_BOOL ("truncate-partial-width-windows",
23503 &truncate_partial_width_windows,
23504 doc: /* *Non-nil means truncate lines in all windows less than full frame wide. */);
23505 truncate_partial_width_windows = 1;
23506
23507 DEFVAR_BOOL ("mode-line-inverse-video", &mode_line_inverse_video,
23508 doc: /* nil means display the mode-line/header-line/menu-bar in the default face.
23509 Any other value means to use the appropriate face, `mode-line',
23510 `header-line', or `menu' respectively. */);
23511 mode_line_inverse_video = 1;
23512
23513 DEFVAR_LISP ("line-number-display-limit", &Vline_number_display_limit,
23514 doc: /* *Maximum buffer size for which line number should be displayed.
23515 If the buffer is bigger than this, the line number does not appear
23516 in the mode line. A value of nil means no limit. */);
23517 Vline_number_display_limit = Qnil;
23518
23519 DEFVAR_INT ("line-number-display-limit-width",
23520 &line_number_display_limit_width,
23521 doc: /* *Maximum line width (in characters) for line number display.
23522 If the average length of the lines near point is bigger than this, then the
23523 line number may be omitted from the mode line. */);
23524 line_number_display_limit_width = 200;
23525
23526 DEFVAR_BOOL ("highlight-nonselected-windows", &highlight_nonselected_windows,
23527 doc: /* *Non-nil means highlight region even in nonselected windows. */);
23528 highlight_nonselected_windows = 0;
23529
23530 DEFVAR_BOOL ("multiple-frames", &multiple_frames,
23531 doc: /* Non-nil if more than one frame is visible on this display.
23532 Minibuffer-only frames don't count, but iconified frames do.
23533 This variable is not guaranteed to be accurate except while processing
23534 `frame-title-format' and `icon-title-format'. */);
23535
23536 DEFVAR_LISP ("frame-title-format", &Vframe_title_format,
23537 doc: /* Template for displaying the title bar of visible frames.
23538 \(Assuming the window manager supports this feature.)
23539 This variable has the same structure as `mode-line-format' (which see),
23540 and is used only on frames for which no explicit name has been set
23541 \(see `modify-frame-parameters'). */);
23542
23543 DEFVAR_LISP ("icon-title-format", &Vicon_title_format,
23544 doc: /* Template for displaying the title bar of an iconified frame.
23545 \(Assuming the window manager supports this feature.)
23546 This variable has the same structure as `mode-line-format' (which see),
23547 and is used only on frames for which no explicit name has been set
23548 \(see `modify-frame-parameters'). */);
23549 Vicon_title_format
23550 = Vframe_title_format
23551 = Fcons (intern ("multiple-frames"),
23552 Fcons (build_string ("%b"),
23553 Fcons (Fcons (empty_string,
23554 Fcons (intern ("invocation-name"),
23555 Fcons (build_string ("@"),
23556 Fcons (intern ("system-name"),
23557 Qnil)))),
23558 Qnil)));
23559
23560 DEFVAR_LISP ("message-log-max", &Vmessage_log_max,
23561 doc: /* Maximum number of lines to keep in the message log buffer.
23562 If nil, disable message logging. If t, log messages but don't truncate
23563 the buffer when it becomes large. */);
23564 Vmessage_log_max = make_number (50);
23565
23566 DEFVAR_LISP ("window-size-change-functions", &Vwindow_size_change_functions,
23567 doc: /* Functions called before redisplay, if window sizes have changed.
23568 The value should be a list of functions that take one argument.
23569 Just before redisplay, for each frame, if any of its windows have changed
23570 size since the last redisplay, or have been split or deleted,
23571 all the functions in the list are called, with the frame as argument. */);
23572 Vwindow_size_change_functions = Qnil;
23573
23574 DEFVAR_LISP ("window-scroll-functions", &Vwindow_scroll_functions,
23575 doc: /* List of functions to call before redisplaying a window with scrolling.
23576 Each function is called with two arguments, the window
23577 and its new display-start position. Note that the value of `window-end'
23578 is not valid when these functions are called. */);
23579 Vwindow_scroll_functions = Qnil;
23580
23581 DEFVAR_LISP ("redisplay-end-trigger-functions", &Vredisplay_end_trigger_functions,
23582 doc: /* Functions called when redisplay of a window reaches the end trigger.
23583 Each function is called with two arguments, the window and the end trigger value.
23584 See `set-window-redisplay-end-trigger'. */);
23585 Vredisplay_end_trigger_functions = Qnil;
23586
23587 DEFVAR_BOOL ("mouse-autoselect-window", &mouse_autoselect_window,
23588 doc: /* *Non-nil means autoselect window with mouse pointer. */);
23589 mouse_autoselect_window = 0;
23590
23591 DEFVAR_BOOL ("auto-resize-tool-bars", &auto_resize_tool_bars_p,
23592 doc: /* *Non-nil means automatically resize tool-bars.
23593 This increases a tool-bar's height if not all tool-bar items are visible.
23594 It decreases a tool-bar's height when it would display blank lines
23595 otherwise. */);
23596 auto_resize_tool_bars_p = 1;
23597
23598 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", &auto_raise_tool_bar_buttons_p,
23599 doc: /* *Non-nil means raise tool-bar buttons when the mouse moves over them. */);
23600 auto_raise_tool_bar_buttons_p = 1;
23601
23602 DEFVAR_BOOL ("make-cursor-line-fully-visible", &make_cursor_line_fully_visible_p,
23603 doc: /* *Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
23604 make_cursor_line_fully_visible_p = 1;
23605
23606 DEFVAR_LISP ("tool-bar-button-margin", &Vtool_bar_button_margin,
23607 doc: /* *Margin around tool-bar buttons in pixels.
23608 If an integer, use that for both horizontal and vertical margins.
23609 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
23610 HORZ specifying the horizontal margin, and VERT specifying the
23611 vertical margin. */);
23612 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
23613
23614 DEFVAR_INT ("tool-bar-button-relief", &tool_bar_button_relief,
23615 doc: /* *Relief thickness of tool-bar buttons. */);
23616 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
23617
23618 DEFVAR_LISP ("fontification-functions", &Vfontification_functions,
23619 doc: /* List of functions to call to fontify regions of text.
23620 Each function is called with one argument POS. Functions must
23621 fontify a region starting at POS in the current buffer, and give
23622 fontified regions the property `fontified'. */);
23623 Vfontification_functions = Qnil;
23624 Fmake_variable_buffer_local (Qfontification_functions);
23625
23626 DEFVAR_BOOL ("unibyte-display-via-language-environment",
23627 &unibyte_display_via_language_environment,
23628 doc: /* *Non-nil means display unibyte text according to language environment.
23629 Specifically this means that unibyte non-ASCII characters
23630 are displayed by converting them to the equivalent multibyte characters
23631 according to the current language environment. As a result, they are
23632 displayed according to the current fontset. */);
23633 unibyte_display_via_language_environment = 0;
23634
23635 DEFVAR_LISP ("max-mini-window-height", &Vmax_mini_window_height,
23636 doc: /* *Maximum height for resizing mini-windows.
23637 If a float, it specifies a fraction of the mini-window frame's height.
23638 If an integer, it specifies a number of lines. */);
23639 Vmax_mini_window_height = make_float (0.25);
23640
23641 DEFVAR_LISP ("resize-mini-windows", &Vresize_mini_windows,
23642 doc: /* *How to resize mini-windows.
23643 A value of nil means don't automatically resize mini-windows.
23644 A value of t means resize them to fit the text displayed in them.
23645 A value of `grow-only', the default, means let mini-windows grow
23646 only, until their display becomes empty, at which point the windows
23647 go back to their normal size. */);
23648 Vresize_mini_windows = Qgrow_only;
23649
23650 DEFVAR_LISP ("blink-cursor-alist", &Vblink_cursor_alist,
23651 doc: /* Alist specifying how to blink the cursor off.
23652 Each element has the form (ON-STATE . OFF-STATE). Whenever the
23653 `cursor-type' frame-parameter or variable equals ON-STATE,
23654 comparing using `equal', Emacs uses OFF-STATE to specify
23655 how to blink it off. ON-STATE and OFF-STATE are values for
23656 the `cursor-type' frame parameter.
23657
23658 If a frame's ON-STATE has no entry in this list,
23659 the frame's other specifications determine how to blink the cursor off. */);
23660 Vblink_cursor_alist = Qnil;
23661
23662 DEFVAR_BOOL ("auto-hscroll-mode", &automatic_hscrolling_p,
23663 doc: /* *Non-nil means scroll the display automatically to make point visible. */);
23664 automatic_hscrolling_p = 1;
23665
23666 DEFVAR_INT ("hscroll-margin", &hscroll_margin,
23667 doc: /* *How many columns away from the window edge point is allowed to get
23668 before automatic hscrolling will horizontally scroll the window. */);
23669 hscroll_margin = 5;
23670
23671 DEFVAR_LISP ("hscroll-step", &Vhscroll_step,
23672 doc: /* *How many columns to scroll the window when point gets too close to the edge.
23673 When point is less than `automatic-hscroll-margin' columns from the window
23674 edge, automatic hscrolling will scroll the window by the amount of columns
23675 determined by this variable. If its value is a positive integer, scroll that
23676 many columns. If it's a positive floating-point number, it specifies the
23677 fraction of the window's width to scroll. If it's nil or zero, point will be
23678 centered horizontally after the scroll. Any other value, including negative
23679 numbers, are treated as if the value were zero.
23680
23681 Automatic hscrolling always moves point outside the scroll margin, so if
23682 point was more than scroll step columns inside the margin, the window will
23683 scroll more than the value given by the scroll step.
23684
23685 Note that the lower bound for automatic hscrolling specified by `scroll-left'
23686 and `scroll-right' overrides this variable's effect. */);
23687 Vhscroll_step = make_number (0);
23688
23689 DEFVAR_BOOL ("message-truncate-lines", &message_truncate_lines,
23690 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
23691 Bind this around calls to `message' to let it take effect. */);
23692 message_truncate_lines = 0;
23693
23694 DEFVAR_LISP ("menu-bar-update-hook", &Vmenu_bar_update_hook,
23695 doc: /* Normal hook run to update the menu bar definitions.
23696 Redisplay runs this hook before it redisplays the menu bar.
23697 This is used to update submenus such as Buffers,
23698 whose contents depend on various data. */);
23699 Vmenu_bar_update_hook = Qnil;
23700
23701 DEFVAR_BOOL ("inhibit-menubar-update", &inhibit_menubar_update,
23702 doc: /* Non-nil means don't update menu bars. Internal use only. */);
23703 inhibit_menubar_update = 0;
23704
23705 DEFVAR_BOOL ("inhibit-eval-during-redisplay", &inhibit_eval_during_redisplay,
23706 doc: /* Non-nil means don't eval Lisp during redisplay. */);
23707 inhibit_eval_during_redisplay = 0;
23708
23709 DEFVAR_BOOL ("inhibit-free-realized-faces", &inhibit_free_realized_faces,
23710 doc: /* Non-nil means don't free realized faces. Internal use only. */);
23711 inhibit_free_realized_faces = 0;
23712
23713 #if GLYPH_DEBUG
23714 DEFVAR_BOOL ("inhibit-try-window-id", &inhibit_try_window_id,
23715 doc: /* Inhibit try_window_id display optimization. */);
23716 inhibit_try_window_id = 0;
23717
23718 DEFVAR_BOOL ("inhibit-try-window-reusing", &inhibit_try_window_reusing,
23719 doc: /* Inhibit try_window_reusing display optimization. */);
23720 inhibit_try_window_reusing = 0;
23721
23722 DEFVAR_BOOL ("inhibit-try-cursor-movement", &inhibit_try_cursor_movement,
23723 doc: /* Inhibit try_cursor_movement display optimization. */);
23724 inhibit_try_cursor_movement = 0;
23725 #endif /* GLYPH_DEBUG */
23726 }
23727
23728
23729 /* Initialize this module when Emacs starts. */
23730
23731 void
23732 init_xdisp ()
23733 {
23734 Lisp_Object root_window;
23735 struct window *mini_w;
23736
23737 current_header_line_height = current_mode_line_height = -1;
23738
23739 CHARPOS (this_line_start_pos) = 0;
23740
23741 mini_w = XWINDOW (minibuf_window);
23742 root_window = FRAME_ROOT_WINDOW (XFRAME (WINDOW_FRAME (mini_w)));
23743
23744 if (!noninteractive)
23745 {
23746 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (root_window)));
23747 int i;
23748
23749 XWINDOW (root_window)->top_line = make_number (FRAME_TOP_MARGIN (f));
23750 set_window_height (root_window,
23751 FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f),
23752 0);
23753 mini_w->top_line = make_number (FRAME_LINES (f) - 1);
23754 set_window_height (minibuf_window, 1, 0);
23755
23756 XWINDOW (root_window)->total_cols = make_number (FRAME_COLS (f));
23757 mini_w->total_cols = make_number (FRAME_COLS (f));
23758
23759 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
23760 scratch_glyph_row.glyphs[TEXT_AREA + 1]
23761 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
23762
23763 /* The default ellipsis glyphs `...'. */
23764 for (i = 0; i < 3; ++i)
23765 default_invis_vector[i] = make_number ('.');
23766 }
23767
23768 {
23769 /* Allocate the buffer for frame titles.
23770 Also used for `format-mode-line'. */
23771 int size = 100;
23772 mode_line_noprop_buf = (char *) xmalloc (size);
23773 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
23774 mode_line_noprop_ptr = mode_line_noprop_buf;
23775 mode_line_target = MODE_LINE_DISPLAY;
23776 }
23777
23778 help_echo_showing_p = 0;
23779 }
23780
23781
23782 /* arch-tag: eacc864d-bb6a-4b74-894a-1a4399a1358b
23783 (do not change this comment) */