* xdisp.c (pos_visible_p): Revert 2008-01-25 change (Bug#5730).
[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, 2006, 2007, 2008, 2009, 2010
5 Free Software Foundation, Inc.
6
7 This file is part of GNU Emacs.
8
9 GNU Emacs is free software: you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation, either version 3 of the License, or
12 (at your option) any later version.
13
14 GNU Emacs is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
21
22 /* New redisplay written by Gerd Moellmann <gerd@gnu.org>.
23
24 Redisplay.
25
26 Emacs separates the task of updating the display from code
27 modifying global state, e.g. buffer text. This way functions
28 operating on buffers don't also have to be concerned with updating
29 the display.
30
31 Updating the display is triggered by the Lisp interpreter when it
32 decides it's time to do it. This is done either automatically for
33 you as part of the interpreter's command loop or as the result of
34 calling Lisp functions like `sit-for'. The C function `redisplay'
35 in xdisp.c is the only entry into the inner redisplay code. (Or,
36 let's say almost---see the description of direct update
37 operations, below.)
38
39 The following diagram shows how redisplay code is invoked. As you
40 can see, Lisp calls redisplay and vice versa. Under window systems
41 like X, some portions of the redisplay code are also called
42 asynchronously during mouse movement or expose events. It is very
43 important that these code parts do NOT use the C library (malloc,
44 free) because many C libraries under Unix are not reentrant. They
45 may also NOT call functions of the Lisp interpreter which could
46 change the interpreter's state. If you don't follow these rules,
47 you will encounter bugs which are very hard to explain.
48
49 (Direct functions, see below)
50 direct_output_for_insert,
51 direct_forward_char (dispnew.c)
52 +---------------------------------+
53 | |
54 | V
55 +--------------+ redisplay +----------------+
56 | Lisp machine |---------------->| Redisplay code |<--+
57 +--------------+ (xdisp.c) +----------------+ |
58 ^ | |
59 +----------------------------------+ |
60 Don't use this path when called |
61 asynchronously! |
62 |
63 expose_window (asynchronous) |
64 |
65 X expose events -----+
66
67 What does redisplay do? Obviously, it has to figure out somehow what
68 has been changed since the last time the display has been updated,
69 and to make these changes visible. Preferably it would do that in
70 a moderately intelligent way, i.e. fast.
71
72 Changes in buffer text can be deduced from window and buffer
73 structures, and from some global variables like `beg_unchanged' and
74 `end_unchanged'. The contents of the display are additionally
75 recorded in a `glyph matrix', a two-dimensional matrix of glyph
76 structures. Each row in such a matrix corresponds to a line on the
77 display, and each glyph in a row corresponds to a column displaying
78 a character, an image, or what else. This matrix is called the
79 `current glyph matrix' or `current matrix' in redisplay
80 terminology.
81
82 For buffer parts that have been changed since the last update, a
83 second glyph matrix is constructed, the so called `desired glyph
84 matrix' or short `desired matrix'. Current and desired matrix are
85 then compared to find a cheap way to update the display, e.g. by
86 reusing part of the display by scrolling lines.
87
88
89 Direct operations.
90
91 You will find a lot of redisplay optimizations when you start
92 looking at the innards of redisplay. The overall goal of all these
93 optimizations is to make redisplay fast because it is done
94 frequently.
95
96 Two optimizations are not found in xdisp.c. These are the direct
97 operations mentioned above. As the name suggests they follow a
98 different principle than the rest of redisplay. Instead of
99 building a desired matrix and then comparing it with the current
100 display, they perform their actions directly on the display and on
101 the current matrix.
102
103 One direct operation updates the display after one character has
104 been entered. The other one moves the cursor by one position
105 forward or backward. You find these functions under the names
106 `direct_output_for_insert' and `direct_output_forward_char' in
107 dispnew.c.
108
109
110 Desired matrices.
111
112 Desired matrices are always built per Emacs window. The function
113 `display_line' is the central function to look at if you are
114 interested. It constructs one row in a desired matrix given an
115 iterator structure containing both a buffer position and a
116 description of the environment in which the text is to be
117 displayed. But this is too early, read on.
118
119 Characters and pixmaps displayed for a range of buffer text depend
120 on various settings of buffers and windows, on overlays and text
121 properties, on display tables, on selective display. The good news
122 is that all this hairy stuff is hidden behind a small set of
123 interface functions taking an iterator structure (struct it)
124 argument.
125
126 Iteration over things to be displayed is then simple. It is
127 started by initializing an iterator with a call to init_iterator.
128 Calls to get_next_display_element fill the iterator structure with
129 relevant information about the next thing to display. Calls to
130 set_iterator_to_next move the iterator to the next thing.
131
132 Besides this, an iterator also contains information about the
133 display environment in which glyphs for display elements are to be
134 produced. It has fields for the width and height of the display,
135 the information whether long lines are truncated or continued, a
136 current X and Y position, and lots of other stuff you can better
137 see in dispextern.h.
138
139 Glyphs in a desired matrix are normally constructed in a loop
140 calling get_next_display_element and then produce_glyphs. The call
141 to produce_glyphs will fill the iterator structure with pixel
142 information about the element being displayed and at the same time
143 produce glyphs for it. If the display element fits on the line
144 being displayed, set_iterator_to_next is called next, otherwise the
145 glyphs produced are discarded.
146
147
148 Frame matrices.
149
150 That just couldn't be all, could it? What about terminal types not
151 supporting operations on sub-windows of the screen? To update the
152 display on such a terminal, window-based glyph matrices are not
153 well suited. To be able to reuse part of the display (scrolling
154 lines up and down), we must instead have a view of the whole
155 screen. This is what `frame matrices' are for. They are a trick.
156
157 Frames on terminals like above have a glyph pool. Windows on such
158 a frame sub-allocate their glyph memory from their frame's glyph
159 pool. The frame itself is given its own glyph matrices. By
160 coincidence---or maybe something else---rows in window glyph
161 matrices are slices of corresponding rows in frame matrices. Thus
162 writing to window matrices implicitly updates a frame matrix which
163 provides us with the view of the whole screen that we originally
164 wanted to have without having to move many bytes around. To be
165 honest, there is a little bit more done, but not much more. If you
166 plan to extend that code, take a look at dispnew.c. The function
167 build_frame_matrix is a good starting point. */
168
169 #include <config.h>
170 #include <stdio.h>
171 #include <limits.h>
172 #include <setjmp.h>
173
174 #include "lisp.h"
175 #include "keyboard.h"
176 #include "frame.h"
177 #include "window.h"
178 #include "termchar.h"
179 #include "dispextern.h"
180 #include "buffer.h"
181 #include "character.h"
182 #include "charset.h"
183 #include "indent.h"
184 #include "commands.h"
185 #include "keymap.h"
186 #include "macros.h"
187 #include "disptab.h"
188 #include "termhooks.h"
189 #include "intervals.h"
190 #include "coding.h"
191 #include "process.h"
192 #include "region-cache.h"
193 #include "font.h"
194 #include "fontset.h"
195 #include "blockinput.h"
196
197 #ifdef HAVE_X_WINDOWS
198 #include "xterm.h"
199 #endif
200 #ifdef WINDOWSNT
201 #include "w32term.h"
202 #endif
203 #ifdef HAVE_NS
204 #include "nsterm.h"
205 #endif
206 #ifdef USE_GTK
207 #include "gtkutil.h"
208 #endif
209
210 #include "font.h"
211
212 #ifndef FRAME_X_OUTPUT
213 #define FRAME_X_OUTPUT(f) ((f)->output_data.x)
214 #endif
215
216 #define INFINITY 10000000
217
218 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
219 || defined(HAVE_NS) || defined (USE_GTK)
220 extern void set_frame_menubar P_ ((struct frame *f, int, int));
221 extern int pending_menu_activation;
222 #endif
223
224 extern int interrupt_input;
225 extern int command_loop_level;
226
227 extern Lisp_Object do_mouse_tracking;
228
229 extern int minibuffer_auto_raise;
230 extern Lisp_Object Vminibuffer_list;
231
232 extern Lisp_Object Qface;
233 extern Lisp_Object Qmode_line, Qmode_line_inactive, Qheader_line;
234
235 extern Lisp_Object Voverriding_local_map;
236 extern Lisp_Object Voverriding_local_map_menu_flag;
237 extern Lisp_Object Qmenu_item;
238 extern Lisp_Object Qwhen;
239 extern Lisp_Object Qhelp_echo;
240 extern Lisp_Object Qbefore_string, Qafter_string;
241
242 Lisp_Object Qoverriding_local_map, Qoverriding_terminal_local_map;
243 Lisp_Object Qwindow_scroll_functions, Vwindow_scroll_functions;
244 Lisp_Object Qwindow_text_change_functions, Vwindow_text_change_functions;
245 Lisp_Object Qredisplay_end_trigger_functions, Vredisplay_end_trigger_functions;
246 Lisp_Object Qinhibit_point_motion_hooks;
247 Lisp_Object QCeval, QCfile, QCdata, QCpropertize;
248 Lisp_Object Qfontified;
249 Lisp_Object Qgrow_only;
250 Lisp_Object Qinhibit_eval_during_redisplay;
251 Lisp_Object Qbuffer_position, Qposition, Qobject;
252
253 /* Cursor shapes */
254 Lisp_Object Qbar, Qhbar, Qbox, Qhollow;
255
256 /* Pointer shapes */
257 Lisp_Object Qarrow, Qhand, Qtext;
258
259 Lisp_Object Qrisky_local_variable;
260
261 /* Holds the list (error). */
262 Lisp_Object list_of_error;
263
264 /* Functions called to fontify regions of text. */
265
266 Lisp_Object Vfontification_functions;
267 Lisp_Object Qfontification_functions;
268
269 /* Non-nil means automatically select any window when the mouse
270 cursor moves into it. */
271 Lisp_Object Vmouse_autoselect_window;
272
273 Lisp_Object Vwrap_prefix, Qwrap_prefix;
274 Lisp_Object Vline_prefix, Qline_prefix;
275
276 /* Non-zero means draw tool bar buttons raised when the mouse moves
277 over them. */
278
279 int auto_raise_tool_bar_buttons_p;
280
281 /* Non-zero means to reposition window if cursor line is only partially visible. */
282
283 int make_cursor_line_fully_visible_p;
284
285 /* Margin below tool bar in pixels. 0 or nil means no margin.
286 If value is `internal-border-width' or `border-width',
287 the corresponding frame parameter is used. */
288
289 Lisp_Object Vtool_bar_border;
290
291 /* Margin around tool bar buttons in pixels. */
292
293 Lisp_Object Vtool_bar_button_margin;
294
295 /* Thickness of shadow to draw around tool bar buttons. */
296
297 EMACS_INT tool_bar_button_relief;
298
299 /* Non-nil means automatically resize tool-bars so that all tool-bar
300 items are visible, and no blank lines remain.
301
302 If value is `grow-only', only make tool-bar bigger. */
303
304 Lisp_Object Vauto_resize_tool_bars;
305
306 /* Non-zero means draw block and hollow cursor as wide as the glyph
307 under it. For example, if a block cursor is over a tab, it will be
308 drawn as wide as that tab on the display. */
309
310 int x_stretch_cursor_p;
311
312 /* Non-nil means don't actually do any redisplay. */
313
314 Lisp_Object Vinhibit_redisplay, Qinhibit_redisplay;
315
316 /* Non-zero means Lisp evaluation during redisplay is inhibited. */
317
318 int inhibit_eval_during_redisplay;
319
320 /* Names of text properties relevant for redisplay. */
321
322 Lisp_Object Qdisplay;
323 extern Lisp_Object Qface, Qinvisible, Qwidth;
324
325 /* Symbols used in text property values. */
326
327 Lisp_Object Vdisplay_pixels_per_inch;
328 Lisp_Object Qspace, QCalign_to, QCrelative_width, QCrelative_height;
329 Lisp_Object Qleft_margin, Qright_margin, Qspace_width, Qraise;
330 Lisp_Object Qslice;
331 Lisp_Object Qcenter;
332 Lisp_Object Qmargin, Qpointer;
333 Lisp_Object Qline_height;
334 extern Lisp_Object Qheight;
335 extern Lisp_Object QCwidth, QCheight, QCascent;
336 extern Lisp_Object Qscroll_bar;
337 extern Lisp_Object Qcursor;
338
339 /* Non-nil means highlight trailing whitespace. */
340
341 Lisp_Object Vshow_trailing_whitespace;
342
343 /* Non-nil means escape non-break space and hyphens. */
344
345 Lisp_Object Vnobreak_char_display;
346
347 #ifdef HAVE_WINDOW_SYSTEM
348 extern Lisp_Object Voverflow_newline_into_fringe;
349
350 /* Test if overflow newline into fringe. Called with iterator IT
351 at or past right window margin, and with IT->current_x set. */
352
353 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(it) \
354 (!NILP (Voverflow_newline_into_fringe) \
355 && FRAME_WINDOW_P (it->f) \
356 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) > 0 \
357 && it->current_x == it->last_visible_x \
358 && it->line_wrap != WORD_WRAP)
359
360 #else /* !HAVE_WINDOW_SYSTEM */
361 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(it) 0
362 #endif /* HAVE_WINDOW_SYSTEM */
363
364 /* Test if the display element loaded in IT is a space or tab
365 character. This is used to determine word wrapping. */
366
367 #define IT_DISPLAYING_WHITESPACE(it) \
368 (it->what == IT_CHARACTER && (it->c == ' ' || it->c == '\t'))
369
370 /* Non-nil means show the text cursor in void text areas
371 i.e. in blank areas after eol and eob. This used to be
372 the default in 21.3. */
373
374 Lisp_Object Vvoid_text_area_pointer;
375
376 /* Name of the face used to highlight trailing whitespace. */
377
378 Lisp_Object Qtrailing_whitespace;
379
380 /* Name and number of the face used to highlight escape glyphs. */
381
382 Lisp_Object Qescape_glyph;
383
384 /* Name and number of the face used to highlight non-breaking spaces. */
385
386 Lisp_Object Qnobreak_space;
387
388 /* The symbol `image' which is the car of the lists used to represent
389 images in Lisp. */
390
391 Lisp_Object Qimage;
392
393 /* The image map types. */
394 Lisp_Object QCmap, QCpointer;
395 Lisp_Object Qrect, Qcircle, Qpoly;
396
397 /* Non-zero means print newline to stdout before next mini-buffer
398 message. */
399
400 int noninteractive_need_newline;
401
402 /* Non-zero means print newline to message log before next message. */
403
404 static int message_log_need_newline;
405
406 /* Three markers that message_dolog uses.
407 It could allocate them itself, but that causes trouble
408 in handling memory-full errors. */
409 static Lisp_Object message_dolog_marker1;
410 static Lisp_Object message_dolog_marker2;
411 static Lisp_Object message_dolog_marker3;
412 \f
413 /* The buffer position of the first character appearing entirely or
414 partially on the line of the selected window which contains the
415 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
416 redisplay optimization in redisplay_internal. */
417
418 static struct text_pos this_line_start_pos;
419
420 /* Number of characters past the end of the line above, including the
421 terminating newline. */
422
423 static struct text_pos this_line_end_pos;
424
425 /* The vertical positions and the height of this line. */
426
427 static int this_line_vpos;
428 static int this_line_y;
429 static int this_line_pixel_height;
430
431 /* X position at which this display line starts. Usually zero;
432 negative if first character is partially visible. */
433
434 static int this_line_start_x;
435
436 /* Buffer that this_line_.* variables are referring to. */
437
438 static struct buffer *this_line_buffer;
439
440 /* Nonzero means truncate lines in all windows less wide than the
441 frame. */
442
443 Lisp_Object Vtruncate_partial_width_windows;
444
445 /* A flag to control how to display unibyte 8-bit character. */
446
447 int unibyte_display_via_language_environment;
448
449 /* Nonzero means we have more than one non-mini-buffer-only frame.
450 Not guaranteed to be accurate except while parsing
451 frame-title-format. */
452
453 int multiple_frames;
454
455 Lisp_Object Vglobal_mode_string;
456
457
458 /* List of variables (symbols) which hold markers for overlay arrows.
459 The symbols on this list are examined during redisplay to determine
460 where to display overlay arrows. */
461
462 Lisp_Object Voverlay_arrow_variable_list;
463
464 /* Marker for where to display an arrow on top of the buffer text. */
465
466 Lisp_Object Voverlay_arrow_position;
467
468 /* String to display for the arrow. Only used on terminal frames. */
469
470 Lisp_Object Voverlay_arrow_string;
471
472 /* Values of those variables at last redisplay are stored as
473 properties on `overlay-arrow-position' symbol. However, if
474 Voverlay_arrow_position is a marker, last-arrow-position is its
475 numerical position. */
476
477 Lisp_Object Qlast_arrow_position, Qlast_arrow_string;
478
479 /* Alternative overlay-arrow-string and overlay-arrow-bitmap
480 properties on a symbol in overlay-arrow-variable-list. */
481
482 Lisp_Object Qoverlay_arrow_string, Qoverlay_arrow_bitmap;
483
484 /* Like mode-line-format, but for the title bar on a visible frame. */
485
486 Lisp_Object Vframe_title_format;
487
488 /* Like mode-line-format, but for the title bar on an iconified frame. */
489
490 Lisp_Object Vicon_title_format;
491
492 /* List of functions to call when a window's size changes. These
493 functions get one arg, a frame on which one or more windows' sizes
494 have changed. */
495
496 static Lisp_Object Vwindow_size_change_functions;
497
498 Lisp_Object Qmenu_bar_update_hook, Vmenu_bar_update_hook;
499
500 /* Nonzero if an overlay arrow has been displayed in this window. */
501
502 static int overlay_arrow_seen;
503
504 /* Nonzero means highlight the region even in nonselected windows. */
505
506 int highlight_nonselected_windows;
507
508 /* If cursor motion alone moves point off frame, try scrolling this
509 many lines up or down if that will bring it back. */
510
511 static EMACS_INT scroll_step;
512
513 /* Nonzero means scroll just far enough to bring point back on the
514 screen, when appropriate. */
515
516 static EMACS_INT scroll_conservatively;
517
518 /* Recenter the window whenever point gets within this many lines of
519 the top or bottom of the window. This value is translated into a
520 pixel value by multiplying it with FRAME_LINE_HEIGHT, which means
521 that there is really a fixed pixel height scroll margin. */
522
523 EMACS_INT scroll_margin;
524
525 /* Number of windows showing the buffer of the selected window (or
526 another buffer with the same base buffer). keyboard.c refers to
527 this. */
528
529 int buffer_shared;
530
531 /* Vector containing glyphs for an ellipsis `...'. */
532
533 static Lisp_Object default_invis_vector[3];
534
535 /* Zero means display the mode-line/header-line/menu-bar in the default face
536 (this slightly odd definition is for compatibility with previous versions
537 of emacs), non-zero means display them using their respective faces.
538
539 This variable is deprecated. */
540
541 int mode_line_inverse_video;
542
543 /* Prompt to display in front of the mini-buffer contents. */
544
545 Lisp_Object minibuf_prompt;
546
547 /* Width of current mini-buffer prompt. Only set after display_line
548 of the line that contains the prompt. */
549
550 int minibuf_prompt_width;
551
552 /* This is the window where the echo area message was displayed. It
553 is always a mini-buffer window, but it may not be the same window
554 currently active as a mini-buffer. */
555
556 Lisp_Object echo_area_window;
557
558 /* List of pairs (MESSAGE . MULTIBYTE). The function save_message
559 pushes the current message and the value of
560 message_enable_multibyte on the stack, the function restore_message
561 pops the stack and displays MESSAGE again. */
562
563 Lisp_Object Vmessage_stack;
564
565 /* Nonzero means multibyte characters were enabled when the echo area
566 message was specified. */
567
568 int message_enable_multibyte;
569
570 /* Nonzero if we should redraw the mode lines on the next redisplay. */
571
572 int update_mode_lines;
573
574 /* Nonzero if window sizes or contents have changed since last
575 redisplay that finished. */
576
577 int windows_or_buffers_changed;
578
579 /* Nonzero means a frame's cursor type has been changed. */
580
581 int cursor_type_changed;
582
583 /* Nonzero after display_mode_line if %l was used and it displayed a
584 line number. */
585
586 int line_number_displayed;
587
588 /* Maximum buffer size for which to display line numbers. */
589
590 Lisp_Object Vline_number_display_limit;
591
592 /* Line width to consider when repositioning for line number display. */
593
594 static EMACS_INT line_number_display_limit_width;
595
596 /* Number of lines to keep in the message log buffer. t means
597 infinite. nil means don't log at all. */
598
599 Lisp_Object Vmessage_log_max;
600
601 /* The name of the *Messages* buffer, a string. */
602
603 static Lisp_Object Vmessages_buffer_name;
604
605 /* Current, index 0, and last displayed echo area message. Either
606 buffers from echo_buffers, or nil to indicate no message. */
607
608 Lisp_Object echo_area_buffer[2];
609
610 /* The buffers referenced from echo_area_buffer. */
611
612 static Lisp_Object echo_buffer[2];
613
614 /* A vector saved used in with_area_buffer to reduce consing. */
615
616 static Lisp_Object Vwith_echo_area_save_vector;
617
618 /* Non-zero means display_echo_area should display the last echo area
619 message again. Set by redisplay_preserve_echo_area. */
620
621 static int display_last_displayed_message_p;
622
623 /* Nonzero if echo area is being used by print; zero if being used by
624 message. */
625
626 int message_buf_print;
627
628 /* The symbol `inhibit-menubar-update' and its DEFVAR_BOOL variable. */
629
630 Lisp_Object Qinhibit_menubar_update;
631 int inhibit_menubar_update;
632
633 /* When evaluating expressions from menu bar items (enable conditions,
634 for instance), this is the frame they are being processed for. */
635
636 Lisp_Object Vmenu_updating_frame;
637
638 /* Maximum height for resizing mini-windows. Either a float
639 specifying a fraction of the available height, or an integer
640 specifying a number of lines. */
641
642 Lisp_Object Vmax_mini_window_height;
643
644 /* Non-zero means messages should be displayed with truncated
645 lines instead of being continued. */
646
647 int message_truncate_lines;
648 Lisp_Object Qmessage_truncate_lines;
649
650 /* Set to 1 in clear_message to make redisplay_internal aware
651 of an emptied echo area. */
652
653 static int message_cleared_p;
654
655 /* How to blink the default frame cursor off. */
656 Lisp_Object Vblink_cursor_alist;
657
658 /* A scratch glyph row with contents used for generating truncation
659 glyphs. Also used in direct_output_for_insert. */
660
661 #define MAX_SCRATCH_GLYPHS 100
662 struct glyph_row scratch_glyph_row;
663 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
664
665 /* Ascent and height of the last line processed by move_it_to. */
666
667 static int last_max_ascent, last_height;
668
669 /* Non-zero if there's a help-echo in the echo area. */
670
671 int help_echo_showing_p;
672
673 /* If >= 0, computed, exact values of mode-line and header-line height
674 to use in the macros CURRENT_MODE_LINE_HEIGHT and
675 CURRENT_HEADER_LINE_HEIGHT. */
676
677 int current_mode_line_height, current_header_line_height;
678
679 /* The maximum distance to look ahead for text properties. Values
680 that are too small let us call compute_char_face and similar
681 functions too often which is expensive. Values that are too large
682 let us call compute_char_face and alike too often because we
683 might not be interested in text properties that far away. */
684
685 #define TEXT_PROP_DISTANCE_LIMIT 100
686
687 #if GLYPH_DEBUG
688
689 /* Variables to turn off display optimizations from Lisp. */
690
691 int inhibit_try_window_id, inhibit_try_window_reusing;
692 int inhibit_try_cursor_movement;
693
694 /* Non-zero means print traces of redisplay if compiled with
695 GLYPH_DEBUG != 0. */
696
697 int trace_redisplay_p;
698
699 #endif /* GLYPH_DEBUG */
700
701 #ifdef DEBUG_TRACE_MOVE
702 /* Non-zero means trace with TRACE_MOVE to stderr. */
703 int trace_move;
704
705 #define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
706 #else
707 #define TRACE_MOVE(x) (void) 0
708 #endif
709
710 /* Non-zero means automatically scroll windows horizontally to make
711 point visible. */
712
713 int automatic_hscrolling_p;
714 Lisp_Object Qauto_hscroll_mode;
715
716 /* How close to the margin can point get before the window is scrolled
717 horizontally. */
718 EMACS_INT hscroll_margin;
719
720 /* How much to scroll horizontally when point is inside the above margin. */
721 Lisp_Object Vhscroll_step;
722
723 /* The variable `resize-mini-windows'. If nil, don't resize
724 mini-windows. If t, always resize them to fit the text they
725 display. If `grow-only', let mini-windows grow only until they
726 become empty. */
727
728 Lisp_Object Vresize_mini_windows;
729
730 /* Buffer being redisplayed -- for redisplay_window_error. */
731
732 struct buffer *displayed_buffer;
733
734 /* Space between overline and text. */
735
736 EMACS_INT overline_margin;
737
738 /* Require underline to be at least this many screen pixels below baseline
739 This to avoid underline "merging" with the base of letters at small
740 font sizes, particularly when x_use_underline_position_properties is on. */
741
742 EMACS_INT underline_minimum_offset;
743
744 /* Value returned from text property handlers (see below). */
745
746 enum prop_handled
747 {
748 HANDLED_NORMALLY,
749 HANDLED_RECOMPUTE_PROPS,
750 HANDLED_OVERLAY_STRING_CONSUMED,
751 HANDLED_RETURN
752 };
753
754 /* A description of text properties that redisplay is interested
755 in. */
756
757 struct props
758 {
759 /* The name of the property. */
760 Lisp_Object *name;
761
762 /* A unique index for the property. */
763 enum prop_idx idx;
764
765 /* A handler function called to set up iterator IT from the property
766 at IT's current position. Value is used to steer handle_stop. */
767 enum prop_handled (*handler) P_ ((struct it *it));
768 };
769
770 static enum prop_handled handle_face_prop P_ ((struct it *));
771 static enum prop_handled handle_invisible_prop P_ ((struct it *));
772 static enum prop_handled handle_display_prop P_ ((struct it *));
773 static enum prop_handled handle_composition_prop P_ ((struct it *));
774 static enum prop_handled handle_overlay_change P_ ((struct it *));
775 static enum prop_handled handle_fontified_prop P_ ((struct it *));
776
777 /* Properties handled by iterators. */
778
779 static struct props it_props[] =
780 {
781 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
782 /* Handle `face' before `display' because some sub-properties of
783 `display' need to know the face. */
784 {&Qface, FACE_PROP_IDX, handle_face_prop},
785 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
786 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
787 {&Qcomposition, COMPOSITION_PROP_IDX, handle_composition_prop},
788 {NULL, 0, NULL}
789 };
790
791 /* Value is the position described by X. If X is a marker, value is
792 the marker_position of X. Otherwise, value is X. */
793
794 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
795
796 /* Enumeration returned by some move_it_.* functions internally. */
797
798 enum move_it_result
799 {
800 /* Not used. Undefined value. */
801 MOVE_UNDEFINED,
802
803 /* Move ended at the requested buffer position or ZV. */
804 MOVE_POS_MATCH_OR_ZV,
805
806 /* Move ended at the requested X pixel position. */
807 MOVE_X_REACHED,
808
809 /* Move within a line ended at the end of a line that must be
810 continued. */
811 MOVE_LINE_CONTINUED,
812
813 /* Move within a line ended at the end of a line that would
814 be displayed truncated. */
815 MOVE_LINE_TRUNCATED,
816
817 /* Move within a line ended at a line end. */
818 MOVE_NEWLINE_OR_CR
819 };
820
821 /* This counter is used to clear the face cache every once in a while
822 in redisplay_internal. It is incremented for each redisplay.
823 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
824 cleared. */
825
826 #define CLEAR_FACE_CACHE_COUNT 500
827 static int clear_face_cache_count;
828
829 /* Similarly for the image cache. */
830
831 #ifdef HAVE_WINDOW_SYSTEM
832 #define CLEAR_IMAGE_CACHE_COUNT 101
833 static int clear_image_cache_count;
834 #endif
835
836 /* Non-zero while redisplay_internal is in progress. */
837
838 int redisplaying_p;
839
840 /* Non-zero means don't free realized faces. Bound while freeing
841 realized faces is dangerous because glyph matrices might still
842 reference them. */
843
844 int inhibit_free_realized_faces;
845 Lisp_Object Qinhibit_free_realized_faces;
846
847 /* If a string, XTread_socket generates an event to display that string.
848 (The display is done in read_char.) */
849
850 Lisp_Object help_echo_string;
851 Lisp_Object help_echo_window;
852 Lisp_Object help_echo_object;
853 int help_echo_pos;
854
855 /* Temporary variable for XTread_socket. */
856
857 Lisp_Object previous_help_echo_string;
858
859 /* Null glyph slice */
860
861 static struct glyph_slice null_glyph_slice = { 0, 0, 0, 0 };
862
863 /* Platform-independent portion of hourglass implementation. */
864
865 /* Non-zero means we're allowed to display a hourglass pointer. */
866 int display_hourglass_p;
867
868 /* Non-zero means an hourglass cursor is currently shown. */
869 int hourglass_shown_p;
870
871 /* If non-null, an asynchronous timer that, when it expires, displays
872 an hourglass cursor on all frames. */
873 struct atimer *hourglass_atimer;
874
875 /* Number of seconds to wait before displaying an hourglass cursor. */
876 Lisp_Object Vhourglass_delay;
877
878 /* Default number of seconds to wait before displaying an hourglass
879 cursor. */
880 #define DEFAULT_HOURGLASS_DELAY 1
881
882 \f
883 /* Function prototypes. */
884
885 static void setup_for_ellipsis P_ ((struct it *, int));
886 static void mark_window_display_accurate_1 P_ ((struct window *, int));
887 static int single_display_spec_string_p P_ ((Lisp_Object, Lisp_Object));
888 static int display_prop_string_p P_ ((Lisp_Object, Lisp_Object));
889 static int cursor_row_p P_ ((struct window *, struct glyph_row *));
890 static int redisplay_mode_lines P_ ((Lisp_Object, int));
891 static char *decode_mode_spec_coding P_ ((Lisp_Object, char *, int));
892
893 static Lisp_Object get_it_property P_ ((struct it *it, Lisp_Object prop));
894
895 static void handle_line_prefix P_ ((struct it *));
896
897 static void pint2str P_ ((char *, int, int));
898 static void pint2hrstr P_ ((char *, int, int));
899 static struct text_pos run_window_scroll_functions P_ ((Lisp_Object,
900 struct text_pos));
901 static void reconsider_clip_changes P_ ((struct window *, struct buffer *));
902 static int text_outside_line_unchanged_p P_ ((struct window *, int, int));
903 static void store_mode_line_noprop_char P_ ((char));
904 static int store_mode_line_noprop P_ ((const unsigned char *, int, int));
905 static void x_consider_frame_title P_ ((Lisp_Object));
906 static void handle_stop P_ ((struct it *));
907 static int tool_bar_lines_needed P_ ((struct frame *, int *));
908 static int single_display_spec_intangible_p P_ ((Lisp_Object));
909 static void ensure_echo_area_buffers P_ ((void));
910 static Lisp_Object unwind_with_echo_area_buffer P_ ((Lisp_Object));
911 static Lisp_Object with_echo_area_buffer_unwind_data P_ ((struct window *));
912 static int with_echo_area_buffer P_ ((struct window *, int,
913 int (*) (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT),
914 EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
915 static void clear_garbaged_frames P_ ((void));
916 static int current_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
917 static int truncate_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
918 static int set_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
919 static int display_echo_area P_ ((struct window *));
920 static int display_echo_area_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
921 static int resize_mini_window_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
922 static Lisp_Object unwind_redisplay P_ ((Lisp_Object));
923 static int string_char_and_length P_ ((const unsigned char *, int *));
924 static struct text_pos display_prop_end P_ ((struct it *, Lisp_Object,
925 struct text_pos));
926 static int compute_window_start_on_continuation_line P_ ((struct window *));
927 static Lisp_Object safe_eval_handler P_ ((Lisp_Object));
928 static void insert_left_trunc_glyphs P_ ((struct it *));
929 static struct glyph_row *get_overlay_arrow_glyph_row P_ ((struct window *,
930 Lisp_Object));
931 static void extend_face_to_end_of_line P_ ((struct it *));
932 static int append_space_for_newline P_ ((struct it *, int));
933 static int cursor_row_fully_visible_p P_ ((struct window *, int, int));
934 static int try_scrolling P_ ((Lisp_Object, int, EMACS_INT, EMACS_INT, int, int));
935 static int try_cursor_movement P_ ((Lisp_Object, struct text_pos, int *));
936 static int trailing_whitespace_p P_ ((int));
937 static int message_log_check_duplicate P_ ((int, int, int, int));
938 static void push_it P_ ((struct it *));
939 static void pop_it P_ ((struct it *));
940 static void sync_frame_with_window_matrix_rows P_ ((struct window *));
941 static void select_frame_for_redisplay P_ ((Lisp_Object));
942 static void redisplay_internal P_ ((int));
943 static int echo_area_display P_ ((int));
944 static void redisplay_windows P_ ((Lisp_Object));
945 static void redisplay_window P_ ((Lisp_Object, int));
946 static Lisp_Object redisplay_window_error ();
947 static Lisp_Object redisplay_window_0 P_ ((Lisp_Object));
948 static Lisp_Object redisplay_window_1 P_ ((Lisp_Object));
949 static int update_menu_bar P_ ((struct frame *, int, int));
950 static int try_window_reusing_current_matrix P_ ((struct window *));
951 static int try_window_id P_ ((struct window *));
952 static int display_line P_ ((struct it *));
953 static int display_mode_lines P_ ((struct window *));
954 static int display_mode_line P_ ((struct window *, enum face_id, Lisp_Object));
955 static int display_mode_element P_ ((struct it *, int, int, int, Lisp_Object, Lisp_Object, int));
956 static int store_mode_line_string P_ ((char *, Lisp_Object, int, int, int, Lisp_Object));
957 static char *decode_mode_spec P_ ((struct window *, int, int, int,
958 Lisp_Object *));
959 static void display_menu_bar P_ ((struct window *));
960 static int display_count_lines P_ ((int, int, int, int, int *));
961 static int display_string P_ ((unsigned char *, Lisp_Object, Lisp_Object,
962 EMACS_INT, EMACS_INT, struct it *, int, int, int, int));
963 static void compute_line_metrics P_ ((struct it *));
964 static void run_redisplay_end_trigger_hook P_ ((struct it *));
965 static int get_overlay_strings P_ ((struct it *, int));
966 static int get_overlay_strings_1 P_ ((struct it *, int, int));
967 static void next_overlay_string P_ ((struct it *));
968 static void reseat P_ ((struct it *, struct text_pos, int));
969 static void reseat_1 P_ ((struct it *, struct text_pos, int));
970 static void back_to_previous_visible_line_start P_ ((struct it *));
971 void reseat_at_previous_visible_line_start P_ ((struct it *));
972 static void reseat_at_next_visible_line_start P_ ((struct it *, int));
973 static int next_element_from_ellipsis P_ ((struct it *));
974 static int next_element_from_display_vector P_ ((struct it *));
975 static int next_element_from_string P_ ((struct it *));
976 static int next_element_from_c_string P_ ((struct it *));
977 static int next_element_from_buffer P_ ((struct it *));
978 static int next_element_from_composition P_ ((struct it *));
979 static int next_element_from_image P_ ((struct it *));
980 static int next_element_from_stretch P_ ((struct it *));
981 static void load_overlay_strings P_ ((struct it *, int));
982 static int init_from_display_pos P_ ((struct it *, struct window *,
983 struct display_pos *));
984 static void reseat_to_string P_ ((struct it *, unsigned char *,
985 Lisp_Object, int, int, int, int));
986 static enum move_it_result
987 move_it_in_display_line_to (struct it *, EMACS_INT, int,
988 enum move_operation_enum);
989 void move_it_vertically_backward P_ ((struct it *, int));
990 static void init_to_row_start P_ ((struct it *, struct window *,
991 struct glyph_row *));
992 static int init_to_row_end P_ ((struct it *, struct window *,
993 struct glyph_row *));
994 static void back_to_previous_line_start P_ ((struct it *));
995 static int forward_to_next_line_start P_ ((struct it *, int *));
996 static struct text_pos string_pos_nchars_ahead P_ ((struct text_pos,
997 Lisp_Object, int));
998 static struct text_pos string_pos P_ ((int, Lisp_Object));
999 static struct text_pos c_string_pos P_ ((int, unsigned char *, int));
1000 static int number_of_chars P_ ((unsigned char *, int));
1001 static void compute_stop_pos P_ ((struct it *));
1002 static void compute_string_pos P_ ((struct text_pos *, struct text_pos,
1003 Lisp_Object));
1004 static int face_before_or_after_it_pos P_ ((struct it *, int));
1005 static EMACS_INT next_overlay_change P_ ((EMACS_INT));
1006 static int handle_single_display_spec P_ ((struct it *, Lisp_Object,
1007 Lisp_Object, Lisp_Object,
1008 struct text_pos *, int));
1009 static int underlying_face_id P_ ((struct it *));
1010 static int in_ellipses_for_invisible_text_p P_ ((struct display_pos *,
1011 struct window *));
1012
1013 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
1014 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
1015
1016 #ifdef HAVE_WINDOW_SYSTEM
1017
1018 static void update_tool_bar P_ ((struct frame *, int));
1019 static void build_desired_tool_bar_string P_ ((struct frame *f));
1020 static int redisplay_tool_bar P_ ((struct frame *));
1021 static void display_tool_bar_line P_ ((struct it *, int));
1022 static void notice_overwritten_cursor P_ ((struct window *,
1023 enum glyph_row_area,
1024 int, int, int, int));
1025
1026
1027
1028 #endif /* HAVE_WINDOW_SYSTEM */
1029
1030 \f
1031 /***********************************************************************
1032 Window display dimensions
1033 ***********************************************************************/
1034
1035 /* Return the bottom boundary y-position for text lines in window W.
1036 This is the first y position at which a line cannot start.
1037 It is relative to the top of the window.
1038
1039 This is the height of W minus the height of a mode line, if any. */
1040
1041 INLINE int
1042 window_text_bottom_y (w)
1043 struct window *w;
1044 {
1045 int height = WINDOW_TOTAL_HEIGHT (w);
1046
1047 if (WINDOW_WANTS_MODELINE_P (w))
1048 height -= CURRENT_MODE_LINE_HEIGHT (w);
1049 return height;
1050 }
1051
1052 /* Return the pixel width of display area AREA of window W. AREA < 0
1053 means return the total width of W, not including fringes to
1054 the left and right of the window. */
1055
1056 INLINE int
1057 window_box_width (w, area)
1058 struct window *w;
1059 int area;
1060 {
1061 int cols = XFASTINT (w->total_cols);
1062 int pixels = 0;
1063
1064 if (!w->pseudo_window_p)
1065 {
1066 cols -= WINDOW_SCROLL_BAR_COLS (w);
1067
1068 if (area == TEXT_AREA)
1069 {
1070 if (INTEGERP (w->left_margin_cols))
1071 cols -= XFASTINT (w->left_margin_cols);
1072 if (INTEGERP (w->right_margin_cols))
1073 cols -= XFASTINT (w->right_margin_cols);
1074 pixels = -WINDOW_TOTAL_FRINGE_WIDTH (w);
1075 }
1076 else if (area == LEFT_MARGIN_AREA)
1077 {
1078 cols = (INTEGERP (w->left_margin_cols)
1079 ? XFASTINT (w->left_margin_cols) : 0);
1080 pixels = 0;
1081 }
1082 else if (area == RIGHT_MARGIN_AREA)
1083 {
1084 cols = (INTEGERP (w->right_margin_cols)
1085 ? XFASTINT (w->right_margin_cols) : 0);
1086 pixels = 0;
1087 }
1088 }
1089
1090 return cols * WINDOW_FRAME_COLUMN_WIDTH (w) + pixels;
1091 }
1092
1093
1094 /* Return the pixel height of the display area of window W, not
1095 including mode lines of W, if any. */
1096
1097 INLINE int
1098 window_box_height (w)
1099 struct window *w;
1100 {
1101 struct frame *f = XFRAME (w->frame);
1102 int height = WINDOW_TOTAL_HEIGHT (w);
1103
1104 xassert (height >= 0);
1105
1106 /* Note: the code below that determines the mode-line/header-line
1107 height is essentially the same as that contained in the macro
1108 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
1109 the appropriate glyph row has its `mode_line_p' flag set,
1110 and if it doesn't, uses estimate_mode_line_height instead. */
1111
1112 if (WINDOW_WANTS_MODELINE_P (w))
1113 {
1114 struct glyph_row *ml_row
1115 = (w->current_matrix && w->current_matrix->rows
1116 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
1117 : 0);
1118 if (ml_row && ml_row->mode_line_p)
1119 height -= ml_row->height;
1120 else
1121 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
1122 }
1123
1124 if (WINDOW_WANTS_HEADER_LINE_P (w))
1125 {
1126 struct glyph_row *hl_row
1127 = (w->current_matrix && w->current_matrix->rows
1128 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
1129 : 0);
1130 if (hl_row && hl_row->mode_line_p)
1131 height -= hl_row->height;
1132 else
1133 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
1134 }
1135
1136 /* With a very small font and a mode-line that's taller than
1137 default, we might end up with a negative height. */
1138 return max (0, height);
1139 }
1140
1141 /* Return the window-relative coordinate of the left edge of display
1142 area AREA of window W. AREA < 0 means return the left edge of the
1143 whole window, to the right of the left fringe of W. */
1144
1145 INLINE int
1146 window_box_left_offset (w, area)
1147 struct window *w;
1148 int area;
1149 {
1150 int x;
1151
1152 if (w->pseudo_window_p)
1153 return 0;
1154
1155 x = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
1156
1157 if (area == TEXT_AREA)
1158 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1159 + window_box_width (w, LEFT_MARGIN_AREA));
1160 else if (area == RIGHT_MARGIN_AREA)
1161 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1162 + window_box_width (w, LEFT_MARGIN_AREA)
1163 + window_box_width (w, TEXT_AREA)
1164 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1165 ? 0
1166 : WINDOW_RIGHT_FRINGE_WIDTH (w)));
1167 else if (area == LEFT_MARGIN_AREA
1168 && WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1169 x += WINDOW_LEFT_FRINGE_WIDTH (w);
1170
1171 return x;
1172 }
1173
1174
1175 /* Return the window-relative coordinate of the right edge of display
1176 area AREA of window W. AREA < 0 means return the left edge of the
1177 whole window, to the left of the right fringe of W. */
1178
1179 INLINE int
1180 window_box_right_offset (w, area)
1181 struct window *w;
1182 int area;
1183 {
1184 return window_box_left_offset (w, area) + window_box_width (w, area);
1185 }
1186
1187 /* Return the frame-relative coordinate of the left edge of display
1188 area AREA of window W. AREA < 0 means return the left edge of the
1189 whole window, to the right of the left fringe of W. */
1190
1191 INLINE int
1192 window_box_left (w, area)
1193 struct window *w;
1194 int area;
1195 {
1196 struct frame *f = XFRAME (w->frame);
1197 int x;
1198
1199 if (w->pseudo_window_p)
1200 return FRAME_INTERNAL_BORDER_WIDTH (f);
1201
1202 x = (WINDOW_LEFT_EDGE_X (w)
1203 + window_box_left_offset (w, area));
1204
1205 return x;
1206 }
1207
1208
1209 /* Return the frame-relative coordinate of the right edge of display
1210 area AREA of window W. AREA < 0 means return the left edge of the
1211 whole window, to the left of the right fringe of W. */
1212
1213 INLINE int
1214 window_box_right (w, area)
1215 struct window *w;
1216 int area;
1217 {
1218 return window_box_left (w, area) + window_box_width (w, area);
1219 }
1220
1221 /* Get the bounding box of the display area AREA of window W, without
1222 mode lines, in frame-relative coordinates. AREA < 0 means the
1223 whole window, not including the left and right fringes of
1224 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1225 coordinates of the upper-left corner of the box. Return in
1226 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1227
1228 INLINE void
1229 window_box (w, area, box_x, box_y, box_width, box_height)
1230 struct window *w;
1231 int area;
1232 int *box_x, *box_y, *box_width, *box_height;
1233 {
1234 if (box_width)
1235 *box_width = window_box_width (w, area);
1236 if (box_height)
1237 *box_height = window_box_height (w);
1238 if (box_x)
1239 *box_x = window_box_left (w, area);
1240 if (box_y)
1241 {
1242 *box_y = WINDOW_TOP_EDGE_Y (w);
1243 if (WINDOW_WANTS_HEADER_LINE_P (w))
1244 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1245 }
1246 }
1247
1248
1249 /* Get the bounding box of the display area AREA of window W, without
1250 mode lines. AREA < 0 means the whole window, not including the
1251 left and right fringe of the window. Return in *TOP_LEFT_X
1252 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1253 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1254 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1255 box. */
1256
1257 INLINE void
1258 window_box_edges (w, area, top_left_x, top_left_y,
1259 bottom_right_x, bottom_right_y)
1260 struct window *w;
1261 int area;
1262 int *top_left_x, *top_left_y, *bottom_right_x, *bottom_right_y;
1263 {
1264 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
1265 bottom_right_y);
1266 *bottom_right_x += *top_left_x;
1267 *bottom_right_y += *top_left_y;
1268 }
1269
1270
1271 \f
1272 /***********************************************************************
1273 Utilities
1274 ***********************************************************************/
1275
1276 /* Return the bottom y-position of the line the iterator IT is in.
1277 This can modify IT's settings. */
1278
1279 int
1280 line_bottom_y (it)
1281 struct it *it;
1282 {
1283 int line_height = it->max_ascent + it->max_descent;
1284 int line_top_y = it->current_y;
1285
1286 if (line_height == 0)
1287 {
1288 if (last_height)
1289 line_height = last_height;
1290 else if (IT_CHARPOS (*it) < ZV)
1291 {
1292 move_it_by_lines (it, 1, 1);
1293 line_height = (it->max_ascent || it->max_descent
1294 ? it->max_ascent + it->max_descent
1295 : last_height);
1296 }
1297 else
1298 {
1299 struct glyph_row *row = it->glyph_row;
1300
1301 /* Use the default character height. */
1302 it->glyph_row = NULL;
1303 it->what = IT_CHARACTER;
1304 it->c = ' ';
1305 it->len = 1;
1306 PRODUCE_GLYPHS (it);
1307 line_height = it->ascent + it->descent;
1308 it->glyph_row = row;
1309 }
1310 }
1311
1312 return line_top_y + line_height;
1313 }
1314
1315
1316 /* Return 1 if position CHARPOS is visible in window W.
1317 CHARPOS < 0 means return info about WINDOW_END position.
1318 If visible, set *X and *Y to pixel coordinates of top left corner.
1319 Set *RTOP and *RBOT to pixel height of an invisible area of glyph at POS.
1320 Set *ROWH and *VPOS to row's visible height and VPOS (row number). */
1321
1322 int
1323 pos_visible_p (w, charpos, x, y, rtop, rbot, rowh, vpos)
1324 struct window *w;
1325 int charpos, *x, *y, *rtop, *rbot, *rowh, *vpos;
1326 {
1327 struct it it;
1328 struct text_pos top;
1329 int visible_p = 0;
1330 struct buffer *old_buffer = NULL;
1331
1332 if (FRAME_INITIAL_P (XFRAME (WINDOW_FRAME (w))))
1333 return visible_p;
1334
1335 if (XBUFFER (w->buffer) != current_buffer)
1336 {
1337 old_buffer = current_buffer;
1338 set_buffer_internal_1 (XBUFFER (w->buffer));
1339 }
1340
1341 SET_TEXT_POS_FROM_MARKER (top, w->start);
1342
1343 /* Compute exact mode line heights. */
1344 if (WINDOW_WANTS_MODELINE_P (w))
1345 current_mode_line_height
1346 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1347 current_buffer->mode_line_format);
1348
1349 if (WINDOW_WANTS_HEADER_LINE_P (w))
1350 current_header_line_height
1351 = display_mode_line (w, HEADER_LINE_FACE_ID,
1352 current_buffer->header_line_format);
1353
1354 start_display (&it, w, top);
1355 move_it_to (&it, charpos, -1, it.last_visible_y-1, -1,
1356 (charpos >= 0 ? MOVE_TO_POS : 0) | MOVE_TO_Y);
1357
1358 if (charpos >= 0 && IT_CHARPOS (it) >= charpos)
1359 {
1360 /* We have reached CHARPOS, or passed it. How the call to
1361 move_it_to can overshoot: (i) If CHARPOS is on invisible
1362 text, move_it_to stops at the end of the invisible text,
1363 after CHARPOS. (ii) If CHARPOS is in a display vector,
1364 move_it_to stops on its last glyph. */
1365 int top_x = it.current_x;
1366 int top_y = it.current_y;
1367 enum it_method it_method = it.method;
1368 /* Calling line_bottom_y may change it.method, it.position, etc. */
1369 int bottom_y = (last_height = 0, line_bottom_y (&it));
1370 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
1371
1372 if (top_y < window_top_y)
1373 visible_p = bottom_y > window_top_y;
1374 else if (top_y < it.last_visible_y)
1375 visible_p = 1;
1376 if (visible_p)
1377 {
1378 if (it_method == GET_FROM_DISPLAY_VECTOR)
1379 {
1380 /* We stopped on the last glyph of a display vector.
1381 Try and recompute. Hack alert! */
1382 if (charpos < 2 || top.charpos >= charpos)
1383 top_x = it.glyph_row->x;
1384 else
1385 {
1386 struct it it2;
1387 start_display (&it2, w, top);
1388 move_it_to (&it2, charpos - 1, -1, -1, -1, MOVE_TO_POS);
1389 get_next_display_element (&it2);
1390 PRODUCE_GLYPHS (&it2);
1391 if (ITERATOR_AT_END_OF_LINE_P (&it2)
1392 || it2.current_x > it2.last_visible_x)
1393 top_x = it.glyph_row->x;
1394 else
1395 {
1396 top_x = it2.current_x;
1397 top_y = it2.current_y;
1398 }
1399 }
1400 }
1401
1402 *x = top_x;
1403 *y = max (top_y + max (0, it.max_ascent - it.ascent), window_top_y);
1404 *rtop = max (0, window_top_y - top_y);
1405 *rbot = max (0, bottom_y - it.last_visible_y);
1406 *rowh = max (0, (min (bottom_y, it.last_visible_y)
1407 - max (top_y, window_top_y)));
1408 *vpos = it.vpos;
1409 }
1410 }
1411 else
1412 {
1413 struct it it2;
1414
1415 it2 = it;
1416 if (IT_CHARPOS (it) < ZV && FETCH_BYTE (IT_BYTEPOS (it)) != '\n')
1417 move_it_by_lines (&it, 1, 0);
1418 if (charpos < IT_CHARPOS (it)
1419 || (it.what == IT_EOB && charpos == IT_CHARPOS (it)))
1420 {
1421 visible_p = 1;
1422 move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS);
1423 *x = it2.current_x;
1424 *y = it2.current_y + it2.max_ascent - it2.ascent;
1425 *rtop = max (0, -it2.current_y);
1426 *rbot = max (0, ((it2.current_y + it2.max_ascent + it2.max_descent)
1427 - it.last_visible_y));
1428 *rowh = max (0, (min (it2.current_y + it2.max_ascent + it2.max_descent,
1429 it.last_visible_y)
1430 - max (it2.current_y,
1431 WINDOW_HEADER_LINE_HEIGHT (w))));
1432 *vpos = it2.vpos;
1433 }
1434 }
1435
1436 if (old_buffer)
1437 set_buffer_internal_1 (old_buffer);
1438
1439 current_header_line_height = current_mode_line_height = -1;
1440
1441 if (visible_p && XFASTINT (w->hscroll) > 0)
1442 *x -= XFASTINT (w->hscroll) * WINDOW_FRAME_COLUMN_WIDTH (w);
1443
1444 #if 0
1445 /* Debugging code. */
1446 if (visible_p)
1447 fprintf (stderr, "+pv pt=%d vs=%d --> x=%d y=%d rt=%d rb=%d rh=%d vp=%d\n",
1448 charpos, w->vscroll, *x, *y, *rtop, *rbot, *rowh, *vpos);
1449 else
1450 fprintf (stderr, "-pv pt=%d vs=%d\n", charpos, w->vscroll);
1451 #endif
1452
1453 return visible_p;
1454 }
1455
1456
1457 /* Return the next character from STR which is MAXLEN bytes long.
1458 Return in *LEN the length of the character. This is like
1459 STRING_CHAR_AND_LENGTH but never returns an invalid character. If
1460 we find one, we return a `?', but with the length of the invalid
1461 character. */
1462
1463 static INLINE int
1464 string_char_and_length (str, len)
1465 const unsigned char *str;
1466 int *len;
1467 {
1468 int c;
1469
1470 c = STRING_CHAR_AND_LENGTH (str, *len);
1471 if (!CHAR_VALID_P (c, 1))
1472 /* We may not change the length here because other places in Emacs
1473 don't use this function, i.e. they silently accept invalid
1474 characters. */
1475 c = '?';
1476
1477 return c;
1478 }
1479
1480
1481
1482 /* Given a position POS containing a valid character and byte position
1483 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1484
1485 static struct text_pos
1486 string_pos_nchars_ahead (pos, string, nchars)
1487 struct text_pos pos;
1488 Lisp_Object string;
1489 int nchars;
1490 {
1491 xassert (STRINGP (string) && nchars >= 0);
1492
1493 if (STRING_MULTIBYTE (string))
1494 {
1495 int rest = SBYTES (string) - BYTEPOS (pos);
1496 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1497 int len;
1498
1499 while (nchars--)
1500 {
1501 string_char_and_length (p, &len);
1502 p += len, rest -= len;
1503 xassert (rest >= 0);
1504 CHARPOS (pos) += 1;
1505 BYTEPOS (pos) += len;
1506 }
1507 }
1508 else
1509 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1510
1511 return pos;
1512 }
1513
1514
1515 /* Value is the text position, i.e. character and byte position,
1516 for character position CHARPOS in STRING. */
1517
1518 static INLINE struct text_pos
1519 string_pos (charpos, string)
1520 int charpos;
1521 Lisp_Object string;
1522 {
1523 struct text_pos pos;
1524 xassert (STRINGP (string));
1525 xassert (charpos >= 0);
1526 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1527 return pos;
1528 }
1529
1530
1531 /* Value is a text position, i.e. character and byte position, for
1532 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1533 means recognize multibyte characters. */
1534
1535 static struct text_pos
1536 c_string_pos (charpos, s, multibyte_p)
1537 int charpos;
1538 unsigned char *s;
1539 int multibyte_p;
1540 {
1541 struct text_pos pos;
1542
1543 xassert (s != NULL);
1544 xassert (charpos >= 0);
1545
1546 if (multibyte_p)
1547 {
1548 int rest = strlen (s), len;
1549
1550 SET_TEXT_POS (pos, 0, 0);
1551 while (charpos--)
1552 {
1553 string_char_and_length (s, &len);
1554 s += len, rest -= len;
1555 xassert (rest >= 0);
1556 CHARPOS (pos) += 1;
1557 BYTEPOS (pos) += len;
1558 }
1559 }
1560 else
1561 SET_TEXT_POS (pos, charpos, charpos);
1562
1563 return pos;
1564 }
1565
1566
1567 /* Value is the number of characters in C string S. MULTIBYTE_P
1568 non-zero means recognize multibyte characters. */
1569
1570 static int
1571 number_of_chars (s, multibyte_p)
1572 unsigned char *s;
1573 int multibyte_p;
1574 {
1575 int nchars;
1576
1577 if (multibyte_p)
1578 {
1579 int rest = strlen (s), len;
1580 unsigned char *p = (unsigned char *) s;
1581
1582 for (nchars = 0; rest > 0; ++nchars)
1583 {
1584 string_char_and_length (p, &len);
1585 rest -= len, p += len;
1586 }
1587 }
1588 else
1589 nchars = strlen (s);
1590
1591 return nchars;
1592 }
1593
1594
1595 /* Compute byte position NEWPOS->bytepos corresponding to
1596 NEWPOS->charpos. POS is a known position in string STRING.
1597 NEWPOS->charpos must be >= POS.charpos. */
1598
1599 static void
1600 compute_string_pos (newpos, pos, string)
1601 struct text_pos *newpos, pos;
1602 Lisp_Object string;
1603 {
1604 xassert (STRINGP (string));
1605 xassert (CHARPOS (*newpos) >= CHARPOS (pos));
1606
1607 if (STRING_MULTIBYTE (string))
1608 *newpos = string_pos_nchars_ahead (pos, string,
1609 CHARPOS (*newpos) - CHARPOS (pos));
1610 else
1611 BYTEPOS (*newpos) = CHARPOS (*newpos);
1612 }
1613
1614 /* EXPORT:
1615 Return an estimation of the pixel height of mode or header lines on
1616 frame F. FACE_ID specifies what line's height to estimate. */
1617
1618 int
1619 estimate_mode_line_height (f, face_id)
1620 struct frame *f;
1621 enum face_id face_id;
1622 {
1623 #ifdef HAVE_WINDOW_SYSTEM
1624 if (FRAME_WINDOW_P (f))
1625 {
1626 int height = FONT_HEIGHT (FRAME_FONT (f));
1627
1628 /* This function is called so early when Emacs starts that the face
1629 cache and mode line face are not yet initialized. */
1630 if (FRAME_FACE_CACHE (f))
1631 {
1632 struct face *face = FACE_FROM_ID (f, face_id);
1633 if (face)
1634 {
1635 if (face->font)
1636 height = FONT_HEIGHT (face->font);
1637 if (face->box_line_width > 0)
1638 height += 2 * face->box_line_width;
1639 }
1640 }
1641
1642 return height;
1643 }
1644 #endif
1645
1646 return 1;
1647 }
1648
1649 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1650 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1651 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
1652 not force the value into range. */
1653
1654 void
1655 pixel_to_glyph_coords (f, pix_x, pix_y, x, y, bounds, noclip)
1656 FRAME_PTR f;
1657 register int pix_x, pix_y;
1658 int *x, *y;
1659 NativeRectangle *bounds;
1660 int noclip;
1661 {
1662
1663 #ifdef HAVE_WINDOW_SYSTEM
1664 if (FRAME_WINDOW_P (f))
1665 {
1666 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1667 even for negative values. */
1668 if (pix_x < 0)
1669 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1670 if (pix_y < 0)
1671 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1672
1673 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1674 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1675
1676 if (bounds)
1677 STORE_NATIVE_RECT (*bounds,
1678 FRAME_COL_TO_PIXEL_X (f, pix_x),
1679 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1680 FRAME_COLUMN_WIDTH (f) - 1,
1681 FRAME_LINE_HEIGHT (f) - 1);
1682
1683 if (!noclip)
1684 {
1685 if (pix_x < 0)
1686 pix_x = 0;
1687 else if (pix_x > FRAME_TOTAL_COLS (f))
1688 pix_x = FRAME_TOTAL_COLS (f);
1689
1690 if (pix_y < 0)
1691 pix_y = 0;
1692 else if (pix_y > FRAME_LINES (f))
1693 pix_y = FRAME_LINES (f);
1694 }
1695 }
1696 #endif
1697
1698 *x = pix_x;
1699 *y = pix_y;
1700 }
1701
1702
1703 /* Given HPOS/VPOS in the current matrix of W, return corresponding
1704 frame-relative pixel positions in *FRAME_X and *FRAME_Y. If we
1705 can't tell the positions because W's display is not up to date,
1706 return 0. */
1707
1708 int
1709 glyph_to_pixel_coords (w, hpos, vpos, frame_x, frame_y)
1710 struct window *w;
1711 int hpos, vpos;
1712 int *frame_x, *frame_y;
1713 {
1714 #ifdef HAVE_WINDOW_SYSTEM
1715 if (FRAME_WINDOW_P (XFRAME (WINDOW_FRAME (w))))
1716 {
1717 int success_p;
1718
1719 xassert (hpos >= 0 && hpos < w->current_matrix->matrix_w);
1720 xassert (vpos >= 0 && vpos < w->current_matrix->matrix_h);
1721
1722 if (display_completed)
1723 {
1724 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
1725 struct glyph *glyph = row->glyphs[TEXT_AREA];
1726 struct glyph *end = glyph + min (hpos, row->used[TEXT_AREA]);
1727
1728 hpos = row->x;
1729 vpos = row->y;
1730 while (glyph < end)
1731 {
1732 hpos += glyph->pixel_width;
1733 ++glyph;
1734 }
1735
1736 /* If first glyph is partially visible, its first visible position is still 0. */
1737 if (hpos < 0)
1738 hpos = 0;
1739
1740 success_p = 1;
1741 }
1742 else
1743 {
1744 hpos = vpos = 0;
1745 success_p = 0;
1746 }
1747
1748 *frame_x = WINDOW_TO_FRAME_PIXEL_X (w, hpos);
1749 *frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, vpos);
1750 return success_p;
1751 }
1752 #endif
1753
1754 *frame_x = hpos;
1755 *frame_y = vpos;
1756 return 1;
1757 }
1758
1759
1760 #ifdef HAVE_WINDOW_SYSTEM
1761
1762 /* Find the glyph under window-relative coordinates X/Y in window W.
1763 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1764 strings. Return in *HPOS and *VPOS the row and column number of
1765 the glyph found. Return in *AREA the glyph area containing X.
1766 Value is a pointer to the glyph found or null if X/Y is not on
1767 text, or we can't tell because W's current matrix is not up to
1768 date. */
1769
1770 static
1771 struct glyph *
1772 x_y_to_hpos_vpos (w, x, y, hpos, vpos, dx, dy, area)
1773 struct window *w;
1774 int x, y;
1775 int *hpos, *vpos, *dx, *dy, *area;
1776 {
1777 struct glyph *glyph, *end;
1778 struct glyph_row *row = NULL;
1779 int x0, i;
1780
1781 /* Find row containing Y. Give up if some row is not enabled. */
1782 for (i = 0; i < w->current_matrix->nrows; ++i)
1783 {
1784 row = MATRIX_ROW (w->current_matrix, i);
1785 if (!row->enabled_p)
1786 return NULL;
1787 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1788 break;
1789 }
1790
1791 *vpos = i;
1792 *hpos = 0;
1793
1794 /* Give up if Y is not in the window. */
1795 if (i == w->current_matrix->nrows)
1796 return NULL;
1797
1798 /* Get the glyph area containing X. */
1799 if (w->pseudo_window_p)
1800 {
1801 *area = TEXT_AREA;
1802 x0 = 0;
1803 }
1804 else
1805 {
1806 if (x < window_box_left_offset (w, TEXT_AREA))
1807 {
1808 *area = LEFT_MARGIN_AREA;
1809 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1810 }
1811 else if (x < window_box_right_offset (w, TEXT_AREA))
1812 {
1813 *area = TEXT_AREA;
1814 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
1815 }
1816 else
1817 {
1818 *area = RIGHT_MARGIN_AREA;
1819 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1820 }
1821 }
1822
1823 /* Find glyph containing X. */
1824 glyph = row->glyphs[*area];
1825 end = glyph + row->used[*area];
1826 x -= x0;
1827 while (glyph < end && x >= glyph->pixel_width)
1828 {
1829 x -= glyph->pixel_width;
1830 ++glyph;
1831 }
1832
1833 if (glyph == end)
1834 return NULL;
1835
1836 if (dx)
1837 {
1838 *dx = x;
1839 *dy = y - (row->y + row->ascent - glyph->ascent);
1840 }
1841
1842 *hpos = glyph - row->glyphs[*area];
1843 return glyph;
1844 }
1845
1846
1847 /* EXPORT:
1848 Convert frame-relative x/y to coordinates relative to window W.
1849 Takes pseudo-windows into account. */
1850
1851 void
1852 frame_to_window_pixel_xy (w, x, y)
1853 struct window *w;
1854 int *x, *y;
1855 {
1856 if (w->pseudo_window_p)
1857 {
1858 /* A pseudo-window is always full-width, and starts at the
1859 left edge of the frame, plus a frame border. */
1860 struct frame *f = XFRAME (w->frame);
1861 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
1862 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1863 }
1864 else
1865 {
1866 *x -= WINDOW_LEFT_EDGE_X (w);
1867 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1868 }
1869 }
1870
1871 /* EXPORT:
1872 Return in RECTS[] at most N clipping rectangles for glyph string S.
1873 Return the number of stored rectangles. */
1874
1875 int
1876 get_glyph_string_clip_rects (s, rects, n)
1877 struct glyph_string *s;
1878 NativeRectangle *rects;
1879 int n;
1880 {
1881 XRectangle r;
1882
1883 if (n <= 0)
1884 return 0;
1885
1886 if (s->row->full_width_p)
1887 {
1888 /* Draw full-width. X coordinates are relative to S->w->left_col. */
1889 r.x = WINDOW_LEFT_EDGE_X (s->w);
1890 r.width = WINDOW_TOTAL_WIDTH (s->w);
1891
1892 /* Unless displaying a mode or menu bar line, which are always
1893 fully visible, clip to the visible part of the row. */
1894 if (s->w->pseudo_window_p)
1895 r.height = s->row->visible_height;
1896 else
1897 r.height = s->height;
1898 }
1899 else
1900 {
1901 /* This is a text line that may be partially visible. */
1902 r.x = window_box_left (s->w, s->area);
1903 r.width = window_box_width (s->w, s->area);
1904 r.height = s->row->visible_height;
1905 }
1906
1907 if (s->clip_head)
1908 if (r.x < s->clip_head->x)
1909 {
1910 if (r.width >= s->clip_head->x - r.x)
1911 r.width -= s->clip_head->x - r.x;
1912 else
1913 r.width = 0;
1914 r.x = s->clip_head->x;
1915 }
1916 if (s->clip_tail)
1917 if (r.x + r.width > s->clip_tail->x + s->clip_tail->background_width)
1918 {
1919 if (s->clip_tail->x + s->clip_tail->background_width >= r.x)
1920 r.width = s->clip_tail->x + s->clip_tail->background_width - r.x;
1921 else
1922 r.width = 0;
1923 }
1924
1925 /* If S draws overlapping rows, it's sufficient to use the top and
1926 bottom of the window for clipping because this glyph string
1927 intentionally draws over other lines. */
1928 if (s->for_overlaps)
1929 {
1930 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1931 r.height = window_text_bottom_y (s->w) - r.y;
1932
1933 /* Alas, the above simple strategy does not work for the
1934 environments with anti-aliased text: if the same text is
1935 drawn onto the same place multiple times, it gets thicker.
1936 If the overlap we are processing is for the erased cursor, we
1937 take the intersection with the rectagle of the cursor. */
1938 if (s->for_overlaps & OVERLAPS_ERASED_CURSOR)
1939 {
1940 XRectangle rc, r_save = r;
1941
1942 rc.x = WINDOW_TEXT_TO_FRAME_PIXEL_X (s->w, s->w->phys_cursor.x);
1943 rc.y = s->w->phys_cursor.y;
1944 rc.width = s->w->phys_cursor_width;
1945 rc.height = s->w->phys_cursor_height;
1946
1947 x_intersect_rectangles (&r_save, &rc, &r);
1948 }
1949 }
1950 else
1951 {
1952 /* Don't use S->y for clipping because it doesn't take partially
1953 visible lines into account. For example, it can be negative for
1954 partially visible lines at the top of a window. */
1955 if (!s->row->full_width_p
1956 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
1957 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1958 else
1959 r.y = max (0, s->row->y);
1960 }
1961
1962 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
1963
1964 /* If drawing the cursor, don't let glyph draw outside its
1965 advertised boundaries. Cleartype does this under some circumstances. */
1966 if (s->hl == DRAW_CURSOR)
1967 {
1968 struct glyph *glyph = s->first_glyph;
1969 int height, max_y;
1970
1971 if (s->x > r.x)
1972 {
1973 r.width -= s->x - r.x;
1974 r.x = s->x;
1975 }
1976 r.width = min (r.width, glyph->pixel_width);
1977
1978 /* If r.y is below window bottom, ensure that we still see a cursor. */
1979 height = min (glyph->ascent + glyph->descent,
1980 min (FRAME_LINE_HEIGHT (s->f), s->row->visible_height));
1981 max_y = window_text_bottom_y (s->w) - height;
1982 max_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, max_y);
1983 if (s->ybase - glyph->ascent > max_y)
1984 {
1985 r.y = max_y;
1986 r.height = height;
1987 }
1988 else
1989 {
1990 /* Don't draw cursor glyph taller than our actual glyph. */
1991 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
1992 if (height < r.height)
1993 {
1994 max_y = r.y + r.height;
1995 r.y = min (max_y, max (r.y, s->ybase + glyph->descent - height));
1996 r.height = min (max_y - r.y, height);
1997 }
1998 }
1999 }
2000
2001 if (s->row->clip)
2002 {
2003 XRectangle r_save = r;
2004
2005 if (! x_intersect_rectangles (&r_save, s->row->clip, &r))
2006 r.width = 0;
2007 }
2008
2009 if ((s->for_overlaps & OVERLAPS_BOTH) == 0
2010 || ((s->for_overlaps & OVERLAPS_BOTH) == OVERLAPS_BOTH && n == 1))
2011 {
2012 #ifdef CONVERT_FROM_XRECT
2013 CONVERT_FROM_XRECT (r, *rects);
2014 #else
2015 *rects = r;
2016 #endif
2017 return 1;
2018 }
2019 else
2020 {
2021 /* If we are processing overlapping and allowed to return
2022 multiple clipping rectangles, we exclude the row of the glyph
2023 string from the clipping rectangle. This is to avoid drawing
2024 the same text on the environment with anti-aliasing. */
2025 #ifdef CONVERT_FROM_XRECT
2026 XRectangle rs[2];
2027 #else
2028 XRectangle *rs = rects;
2029 #endif
2030 int i = 0, row_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, s->row->y);
2031
2032 if (s->for_overlaps & OVERLAPS_PRED)
2033 {
2034 rs[i] = r;
2035 if (r.y + r.height > row_y)
2036 {
2037 if (r.y < row_y)
2038 rs[i].height = row_y - r.y;
2039 else
2040 rs[i].height = 0;
2041 }
2042 i++;
2043 }
2044 if (s->for_overlaps & OVERLAPS_SUCC)
2045 {
2046 rs[i] = r;
2047 if (r.y < row_y + s->row->visible_height)
2048 {
2049 if (r.y + r.height > row_y + s->row->visible_height)
2050 {
2051 rs[i].y = row_y + s->row->visible_height;
2052 rs[i].height = r.y + r.height - rs[i].y;
2053 }
2054 else
2055 rs[i].height = 0;
2056 }
2057 i++;
2058 }
2059
2060 n = i;
2061 #ifdef CONVERT_FROM_XRECT
2062 for (i = 0; i < n; i++)
2063 CONVERT_FROM_XRECT (rs[i], rects[i]);
2064 #endif
2065 return n;
2066 }
2067 }
2068
2069 /* EXPORT:
2070 Return in *NR the clipping rectangle for glyph string S. */
2071
2072 void
2073 get_glyph_string_clip_rect (s, nr)
2074 struct glyph_string *s;
2075 NativeRectangle *nr;
2076 {
2077 get_glyph_string_clip_rects (s, nr, 1);
2078 }
2079
2080
2081 /* EXPORT:
2082 Return the position and height of the phys cursor in window W.
2083 Set w->phys_cursor_width to width of phys cursor.
2084 */
2085
2086 void
2087 get_phys_cursor_geometry (w, row, glyph, xp, yp, heightp)
2088 struct window *w;
2089 struct glyph_row *row;
2090 struct glyph *glyph;
2091 int *xp, *yp, *heightp;
2092 {
2093 struct frame *f = XFRAME (WINDOW_FRAME (w));
2094 int x, y, wd, h, h0, y0;
2095
2096 /* Compute the width of the rectangle to draw. If on a stretch
2097 glyph, and `x-stretch-block-cursor' is nil, don't draw a
2098 rectangle as wide as the glyph, but use a canonical character
2099 width instead. */
2100 wd = glyph->pixel_width - 1;
2101 #if defined(HAVE_NTGUI) || defined(HAVE_NS)
2102 wd++; /* Why? */
2103 #endif
2104
2105 x = w->phys_cursor.x;
2106 if (x < 0)
2107 {
2108 wd += x;
2109 x = 0;
2110 }
2111
2112 if (glyph->type == STRETCH_GLYPH
2113 && !x_stretch_cursor_p)
2114 wd = min (FRAME_COLUMN_WIDTH (f), wd);
2115 w->phys_cursor_width = wd;
2116
2117 y = w->phys_cursor.y + row->ascent - glyph->ascent;
2118
2119 /* If y is below window bottom, ensure that we still see a cursor. */
2120 h0 = min (FRAME_LINE_HEIGHT (f), row->visible_height);
2121
2122 h = max (h0, glyph->ascent + glyph->descent);
2123 h0 = min (h0, glyph->ascent + glyph->descent);
2124
2125 y0 = WINDOW_HEADER_LINE_HEIGHT (w);
2126 if (y < y0)
2127 {
2128 h = max (h - (y0 - y) + 1, h0);
2129 y = y0 - 1;
2130 }
2131 else
2132 {
2133 y0 = window_text_bottom_y (w) - h0;
2134 if (y > y0)
2135 {
2136 h += y - y0;
2137 y = y0;
2138 }
2139 }
2140
2141 *xp = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
2142 *yp = WINDOW_TO_FRAME_PIXEL_Y (w, y);
2143 *heightp = h;
2144 }
2145
2146 /*
2147 * Remember which glyph the mouse is over.
2148 */
2149
2150 void
2151 remember_mouse_glyph (f, gx, gy, rect)
2152 struct frame *f;
2153 int gx, gy;
2154 NativeRectangle *rect;
2155 {
2156 Lisp_Object window;
2157 struct window *w;
2158 struct glyph_row *r, *gr, *end_row;
2159 enum window_part part;
2160 enum glyph_row_area area;
2161 int x, y, width, height;
2162
2163 /* Try to determine frame pixel position and size of the glyph under
2164 frame pixel coordinates X/Y on frame F. */
2165
2166 if (!f->glyphs_initialized_p
2167 || (window = window_from_coordinates (f, gx, gy, &part, &x, &y, 0),
2168 NILP (window)))
2169 {
2170 width = FRAME_SMALLEST_CHAR_WIDTH (f);
2171 height = FRAME_SMALLEST_FONT_HEIGHT (f);
2172 goto virtual_glyph;
2173 }
2174
2175 w = XWINDOW (window);
2176 width = WINDOW_FRAME_COLUMN_WIDTH (w);
2177 height = WINDOW_FRAME_LINE_HEIGHT (w);
2178
2179 r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
2180 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
2181
2182 if (w->pseudo_window_p)
2183 {
2184 area = TEXT_AREA;
2185 part = ON_MODE_LINE; /* Don't adjust margin. */
2186 goto text_glyph;
2187 }
2188
2189 switch (part)
2190 {
2191 case ON_LEFT_MARGIN:
2192 area = LEFT_MARGIN_AREA;
2193 goto text_glyph;
2194
2195 case ON_RIGHT_MARGIN:
2196 area = RIGHT_MARGIN_AREA;
2197 goto text_glyph;
2198
2199 case ON_HEADER_LINE:
2200 case ON_MODE_LINE:
2201 gr = (part == ON_HEADER_LINE
2202 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
2203 : MATRIX_MODE_LINE_ROW (w->current_matrix));
2204 gy = gr->y;
2205 area = TEXT_AREA;
2206 goto text_glyph_row_found;
2207
2208 case ON_TEXT:
2209 area = TEXT_AREA;
2210
2211 text_glyph:
2212 gr = 0; gy = 0;
2213 for (; r <= end_row && r->enabled_p; ++r)
2214 if (r->y + r->height > y)
2215 {
2216 gr = r; gy = r->y;
2217 break;
2218 }
2219
2220 text_glyph_row_found:
2221 if (gr && gy <= y)
2222 {
2223 struct glyph *g = gr->glyphs[area];
2224 struct glyph *end = g + gr->used[area];
2225
2226 height = gr->height;
2227 for (gx = gr->x; g < end; gx += g->pixel_width, ++g)
2228 if (gx + g->pixel_width > x)
2229 break;
2230
2231 if (g < end)
2232 {
2233 if (g->type == IMAGE_GLYPH)
2234 {
2235 /* Don't remember when mouse is over image, as
2236 image may have hot-spots. */
2237 STORE_NATIVE_RECT (*rect, 0, 0, 0, 0);
2238 return;
2239 }
2240 width = g->pixel_width;
2241 }
2242 else
2243 {
2244 /* Use nominal char spacing at end of line. */
2245 x -= gx;
2246 gx += (x / width) * width;
2247 }
2248
2249 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2250 gx += window_box_left_offset (w, area);
2251 }
2252 else
2253 {
2254 /* Use nominal line height at end of window. */
2255 gx = (x / width) * width;
2256 y -= gy;
2257 gy += (y / height) * height;
2258 }
2259 break;
2260
2261 case ON_LEFT_FRINGE:
2262 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2263 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w)
2264 : window_box_right_offset (w, LEFT_MARGIN_AREA));
2265 width = WINDOW_LEFT_FRINGE_WIDTH (w);
2266 goto row_glyph;
2267
2268 case ON_RIGHT_FRINGE:
2269 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2270 ? window_box_right_offset (w, RIGHT_MARGIN_AREA)
2271 : window_box_right_offset (w, TEXT_AREA));
2272 width = WINDOW_RIGHT_FRINGE_WIDTH (w);
2273 goto row_glyph;
2274
2275 case ON_SCROLL_BAR:
2276 gx = (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
2277 ? 0
2278 : (window_box_right_offset (w, RIGHT_MARGIN_AREA)
2279 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2280 ? WINDOW_RIGHT_FRINGE_WIDTH (w)
2281 : 0)));
2282 width = WINDOW_SCROLL_BAR_AREA_WIDTH (w);
2283
2284 row_glyph:
2285 gr = 0, gy = 0;
2286 for (; r <= end_row && r->enabled_p; ++r)
2287 if (r->y + r->height > y)
2288 {
2289 gr = r; gy = r->y;
2290 break;
2291 }
2292
2293 if (gr && gy <= y)
2294 height = gr->height;
2295 else
2296 {
2297 /* Use nominal line height at end of window. */
2298 y -= gy;
2299 gy += (y / height) * height;
2300 }
2301 break;
2302
2303 default:
2304 ;
2305 virtual_glyph:
2306 /* If there is no glyph under the mouse, then we divide the screen
2307 into a grid of the smallest glyph in the frame, and use that
2308 as our "glyph". */
2309
2310 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to
2311 round down even for negative values. */
2312 if (gx < 0)
2313 gx -= width - 1;
2314 if (gy < 0)
2315 gy -= height - 1;
2316
2317 gx = (gx / width) * width;
2318 gy = (gy / height) * height;
2319
2320 goto store_rect;
2321 }
2322
2323 gx += WINDOW_LEFT_EDGE_X (w);
2324 gy += WINDOW_TOP_EDGE_Y (w);
2325
2326 store_rect:
2327 STORE_NATIVE_RECT (*rect, gx, gy, width, height);
2328
2329 /* Visible feedback for debugging. */
2330 #if 0
2331 #if HAVE_X_WINDOWS
2332 XDrawRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2333 f->output_data.x->normal_gc,
2334 gx, gy, width, height);
2335 #endif
2336 #endif
2337 }
2338
2339
2340 #endif /* HAVE_WINDOW_SYSTEM */
2341
2342 \f
2343 /***********************************************************************
2344 Lisp form evaluation
2345 ***********************************************************************/
2346
2347 /* Error handler for safe_eval and safe_call. */
2348
2349 static Lisp_Object
2350 safe_eval_handler (arg)
2351 Lisp_Object arg;
2352 {
2353 add_to_log ("Error during redisplay: %s", arg, Qnil);
2354 return Qnil;
2355 }
2356
2357
2358 /* Evaluate SEXPR and return the result, or nil if something went
2359 wrong. Prevent redisplay during the evaluation. */
2360
2361 /* Call function ARGS[0] with arguments ARGS[1] to ARGS[NARGS - 1].
2362 Return the result, or nil if something went wrong. Prevent
2363 redisplay during the evaluation. */
2364
2365 Lisp_Object
2366 safe_call (nargs, args)
2367 int nargs;
2368 Lisp_Object *args;
2369 {
2370 Lisp_Object val;
2371
2372 if (inhibit_eval_during_redisplay)
2373 val = Qnil;
2374 else
2375 {
2376 int count = SPECPDL_INDEX ();
2377 struct gcpro gcpro1;
2378
2379 GCPRO1 (args[0]);
2380 gcpro1.nvars = nargs;
2381 specbind (Qinhibit_redisplay, Qt);
2382 /* Use Qt to ensure debugger does not run,
2383 so there is no possibility of wanting to redisplay. */
2384 val = internal_condition_case_2 (Ffuncall, nargs, args, Qt,
2385 safe_eval_handler);
2386 UNGCPRO;
2387 val = unbind_to (count, val);
2388 }
2389
2390 return val;
2391 }
2392
2393
2394 /* Call function FN with one argument ARG.
2395 Return the result, or nil if something went wrong. */
2396
2397 Lisp_Object
2398 safe_call1 (fn, arg)
2399 Lisp_Object fn, arg;
2400 {
2401 Lisp_Object args[2];
2402 args[0] = fn;
2403 args[1] = arg;
2404 return safe_call (2, args);
2405 }
2406
2407 static Lisp_Object Qeval;
2408
2409 Lisp_Object
2410 safe_eval (Lisp_Object sexpr)
2411 {
2412 return safe_call1 (Qeval, sexpr);
2413 }
2414
2415 /* Call function FN with one argument ARG.
2416 Return the result, or nil if something went wrong. */
2417
2418 Lisp_Object
2419 safe_call2 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2)
2420 {
2421 Lisp_Object args[3];
2422 args[0] = fn;
2423 args[1] = arg1;
2424 args[2] = arg2;
2425 return safe_call (3, args);
2426 }
2427
2428
2429 \f
2430 /***********************************************************************
2431 Debugging
2432 ***********************************************************************/
2433
2434 #if 0
2435
2436 /* Define CHECK_IT to perform sanity checks on iterators.
2437 This is for debugging. It is too slow to do unconditionally. */
2438
2439 static void
2440 check_it (it)
2441 struct it *it;
2442 {
2443 if (it->method == GET_FROM_STRING)
2444 {
2445 xassert (STRINGP (it->string));
2446 xassert (IT_STRING_CHARPOS (*it) >= 0);
2447 }
2448 else
2449 {
2450 xassert (IT_STRING_CHARPOS (*it) < 0);
2451 if (it->method == GET_FROM_BUFFER)
2452 {
2453 /* Check that character and byte positions agree. */
2454 xassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
2455 }
2456 }
2457
2458 if (it->dpvec)
2459 xassert (it->current.dpvec_index >= 0);
2460 else
2461 xassert (it->current.dpvec_index < 0);
2462 }
2463
2464 #define CHECK_IT(IT) check_it ((IT))
2465
2466 #else /* not 0 */
2467
2468 #define CHECK_IT(IT) (void) 0
2469
2470 #endif /* not 0 */
2471
2472
2473 #if GLYPH_DEBUG
2474
2475 /* Check that the window end of window W is what we expect it
2476 to be---the last row in the current matrix displaying text. */
2477
2478 static void
2479 check_window_end (w)
2480 struct window *w;
2481 {
2482 if (!MINI_WINDOW_P (w)
2483 && !NILP (w->window_end_valid))
2484 {
2485 struct glyph_row *row;
2486 xassert ((row = MATRIX_ROW (w->current_matrix,
2487 XFASTINT (w->window_end_vpos)),
2488 !row->enabled_p
2489 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
2490 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
2491 }
2492 }
2493
2494 #define CHECK_WINDOW_END(W) check_window_end ((W))
2495
2496 #else /* not GLYPH_DEBUG */
2497
2498 #define CHECK_WINDOW_END(W) (void) 0
2499
2500 #endif /* not GLYPH_DEBUG */
2501
2502
2503 \f
2504 /***********************************************************************
2505 Iterator initialization
2506 ***********************************************************************/
2507
2508 /* Initialize IT for displaying current_buffer in window W, starting
2509 at character position CHARPOS. CHARPOS < 0 means that no buffer
2510 position is specified which is useful when the iterator is assigned
2511 a position later. BYTEPOS is the byte position corresponding to
2512 CHARPOS. BYTEPOS < 0 means compute it from CHARPOS.
2513
2514 If ROW is not null, calls to produce_glyphs with IT as parameter
2515 will produce glyphs in that row.
2516
2517 BASE_FACE_ID is the id of a base face to use. It must be one of
2518 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2519 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2520 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2521
2522 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2523 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2524 will be initialized to use the corresponding mode line glyph row of
2525 the desired matrix of W. */
2526
2527 void
2528 init_iterator (it, w, charpos, bytepos, row, base_face_id)
2529 struct it *it;
2530 struct window *w;
2531 int charpos, bytepos;
2532 struct glyph_row *row;
2533 enum face_id base_face_id;
2534 {
2535 int highlight_region_p;
2536 enum face_id remapped_base_face_id = base_face_id;
2537
2538 /* Some precondition checks. */
2539 xassert (w != NULL && it != NULL);
2540 xassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2541 && charpos <= ZV));
2542
2543 /* If face attributes have been changed since the last redisplay,
2544 free realized faces now because they depend on face definitions
2545 that might have changed. Don't free faces while there might be
2546 desired matrices pending which reference these faces. */
2547 if (face_change_count && !inhibit_free_realized_faces)
2548 {
2549 face_change_count = 0;
2550 free_all_realized_faces (Qnil);
2551 }
2552
2553 /* Perhaps remap BASE_FACE_ID to a user-specified alternative. */
2554 if (! NILP (Vface_remapping_alist))
2555 remapped_base_face_id = lookup_basic_face (XFRAME (w->frame), base_face_id);
2556
2557 /* Use one of the mode line rows of W's desired matrix if
2558 appropriate. */
2559 if (row == NULL)
2560 {
2561 if (base_face_id == MODE_LINE_FACE_ID
2562 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2563 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2564 else if (base_face_id == HEADER_LINE_FACE_ID)
2565 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2566 }
2567
2568 /* Clear IT. */
2569 bzero (it, sizeof *it);
2570 it->current.overlay_string_index = -1;
2571 it->current.dpvec_index = -1;
2572 it->base_face_id = remapped_base_face_id;
2573 it->string = Qnil;
2574 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2575
2576 /* The window in which we iterate over current_buffer: */
2577 XSETWINDOW (it->window, w);
2578 it->w = w;
2579 it->f = XFRAME (w->frame);
2580
2581 it->cmp_it.id = -1;
2582
2583 /* Extra space between lines (on window systems only). */
2584 if (base_face_id == DEFAULT_FACE_ID
2585 && FRAME_WINDOW_P (it->f))
2586 {
2587 if (NATNUMP (current_buffer->extra_line_spacing))
2588 it->extra_line_spacing = XFASTINT (current_buffer->extra_line_spacing);
2589 else if (FLOATP (current_buffer->extra_line_spacing))
2590 it->extra_line_spacing = (XFLOAT_DATA (current_buffer->extra_line_spacing)
2591 * FRAME_LINE_HEIGHT (it->f));
2592 else if (it->f->extra_line_spacing > 0)
2593 it->extra_line_spacing = it->f->extra_line_spacing;
2594 it->max_extra_line_spacing = 0;
2595 }
2596
2597 /* If realized faces have been removed, e.g. because of face
2598 attribute changes of named faces, recompute them. When running
2599 in batch mode, the face cache of the initial frame is null. If
2600 we happen to get called, make a dummy face cache. */
2601 if (FRAME_FACE_CACHE (it->f) == NULL)
2602 init_frame_faces (it->f);
2603 if (FRAME_FACE_CACHE (it->f)->used == 0)
2604 recompute_basic_faces (it->f);
2605
2606 /* Current value of the `slice', `space-width', and 'height' properties. */
2607 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
2608 it->space_width = Qnil;
2609 it->font_height = Qnil;
2610 it->override_ascent = -1;
2611
2612 /* Are control characters displayed as `^C'? */
2613 it->ctl_arrow_p = !NILP (current_buffer->ctl_arrow);
2614
2615 /* -1 means everything between a CR and the following line end
2616 is invisible. >0 means lines indented more than this value are
2617 invisible. */
2618 it->selective = (INTEGERP (current_buffer->selective_display)
2619 ? XFASTINT (current_buffer->selective_display)
2620 : (!NILP (current_buffer->selective_display)
2621 ? -1 : 0));
2622 it->selective_display_ellipsis_p
2623 = !NILP (current_buffer->selective_display_ellipses);
2624
2625 /* Display table to use. */
2626 it->dp = window_display_table (w);
2627
2628 /* Are multibyte characters enabled in current_buffer? */
2629 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
2630
2631 /* Non-zero if we should highlight the region. */
2632 highlight_region_p
2633 = (!NILP (Vtransient_mark_mode)
2634 && !NILP (current_buffer->mark_active)
2635 && XMARKER (current_buffer->mark)->buffer != 0);
2636
2637 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
2638 start and end of a visible region in window IT->w. Set both to
2639 -1 to indicate no region. */
2640 if (highlight_region_p
2641 /* Maybe highlight only in selected window. */
2642 && (/* Either show region everywhere. */
2643 highlight_nonselected_windows
2644 /* Or show region in the selected window. */
2645 || w == XWINDOW (selected_window)
2646 /* Or show the region if we are in the mini-buffer and W is
2647 the window the mini-buffer refers to. */
2648 || (MINI_WINDOW_P (XWINDOW (selected_window))
2649 && WINDOWP (minibuf_selected_window)
2650 && w == XWINDOW (minibuf_selected_window))))
2651 {
2652 int charpos = marker_position (current_buffer->mark);
2653 it->region_beg_charpos = min (PT, charpos);
2654 it->region_end_charpos = max (PT, charpos);
2655 }
2656 else
2657 it->region_beg_charpos = it->region_end_charpos = -1;
2658
2659 /* Get the position at which the redisplay_end_trigger hook should
2660 be run, if it is to be run at all. */
2661 if (MARKERP (w->redisplay_end_trigger)
2662 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2663 it->redisplay_end_trigger_charpos
2664 = marker_position (w->redisplay_end_trigger);
2665 else if (INTEGERP (w->redisplay_end_trigger))
2666 it->redisplay_end_trigger_charpos = XINT (w->redisplay_end_trigger);
2667
2668 /* Correct bogus values of tab_width. */
2669 it->tab_width = XINT (current_buffer->tab_width);
2670 if (it->tab_width <= 0 || it->tab_width > 1000)
2671 it->tab_width = 8;
2672
2673 /* Are lines in the display truncated? */
2674 if (base_face_id != DEFAULT_FACE_ID
2675 || XINT (it->w->hscroll)
2676 || (! WINDOW_FULL_WIDTH_P (it->w)
2677 && ((!NILP (Vtruncate_partial_width_windows)
2678 && !INTEGERP (Vtruncate_partial_width_windows))
2679 || (INTEGERP (Vtruncate_partial_width_windows)
2680 && (WINDOW_TOTAL_COLS (it->w)
2681 < XINT (Vtruncate_partial_width_windows))))))
2682 it->line_wrap = TRUNCATE;
2683 else if (NILP (current_buffer->truncate_lines))
2684 it->line_wrap = NILP (current_buffer->word_wrap)
2685 ? WINDOW_WRAP : WORD_WRAP;
2686 else
2687 it->line_wrap = TRUNCATE;
2688
2689 /* Get dimensions of truncation and continuation glyphs. These are
2690 displayed as fringe bitmaps under X, so we don't need them for such
2691 frames. */
2692 if (!FRAME_WINDOW_P (it->f))
2693 {
2694 if (it->line_wrap == TRUNCATE)
2695 {
2696 /* We will need the truncation glyph. */
2697 xassert (it->glyph_row == NULL);
2698 produce_special_glyphs (it, IT_TRUNCATION);
2699 it->truncation_pixel_width = it->pixel_width;
2700 }
2701 else
2702 {
2703 /* We will need the continuation glyph. */
2704 xassert (it->glyph_row == NULL);
2705 produce_special_glyphs (it, IT_CONTINUATION);
2706 it->continuation_pixel_width = it->pixel_width;
2707 }
2708
2709 /* Reset these values to zero because the produce_special_glyphs
2710 above has changed them. */
2711 it->pixel_width = it->ascent = it->descent = 0;
2712 it->phys_ascent = it->phys_descent = 0;
2713 }
2714
2715 /* Set this after getting the dimensions of truncation and
2716 continuation glyphs, so that we don't produce glyphs when calling
2717 produce_special_glyphs, above. */
2718 it->glyph_row = row;
2719 it->area = TEXT_AREA;
2720
2721 /* Get the dimensions of the display area. The display area
2722 consists of the visible window area plus a horizontally scrolled
2723 part to the left of the window. All x-values are relative to the
2724 start of this total display area. */
2725 if (base_face_id != DEFAULT_FACE_ID)
2726 {
2727 /* Mode lines, menu bar in terminal frames. */
2728 it->first_visible_x = 0;
2729 it->last_visible_x = WINDOW_TOTAL_WIDTH (w);
2730 }
2731 else
2732 {
2733 it->first_visible_x
2734 = XFASTINT (it->w->hscroll) * FRAME_COLUMN_WIDTH (it->f);
2735 it->last_visible_x = (it->first_visible_x
2736 + window_box_width (w, TEXT_AREA));
2737
2738 /* If we truncate lines, leave room for the truncator glyph(s) at
2739 the right margin. Otherwise, leave room for the continuation
2740 glyph(s). Truncation and continuation glyphs are not inserted
2741 for window-based redisplay. */
2742 if (!FRAME_WINDOW_P (it->f))
2743 {
2744 if (it->line_wrap == TRUNCATE)
2745 it->last_visible_x -= it->truncation_pixel_width;
2746 else
2747 it->last_visible_x -= it->continuation_pixel_width;
2748 }
2749
2750 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2751 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2752 }
2753
2754 /* Leave room for a border glyph. */
2755 if (!FRAME_WINDOW_P (it->f)
2756 && !WINDOW_RIGHTMOST_P (it->w))
2757 it->last_visible_x -= 1;
2758
2759 it->last_visible_y = window_text_bottom_y (w);
2760
2761 /* For mode lines and alike, arrange for the first glyph having a
2762 left box line if the face specifies a box. */
2763 if (base_face_id != DEFAULT_FACE_ID)
2764 {
2765 struct face *face;
2766
2767 it->face_id = remapped_base_face_id;
2768
2769 /* If we have a boxed mode line, make the first character appear
2770 with a left box line. */
2771 face = FACE_FROM_ID (it->f, remapped_base_face_id);
2772 if (face->box != FACE_NO_BOX)
2773 it->start_of_box_run_p = 1;
2774 }
2775
2776 /* If a buffer position was specified, set the iterator there,
2777 getting overlays and face properties from that position. */
2778 if (charpos >= BUF_BEG (current_buffer))
2779 {
2780 it->end_charpos = ZV;
2781 it->face_id = -1;
2782 IT_CHARPOS (*it) = charpos;
2783
2784 /* Compute byte position if not specified. */
2785 if (bytepos < charpos)
2786 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
2787 else
2788 IT_BYTEPOS (*it) = bytepos;
2789
2790 it->start = it->current;
2791
2792 /* Compute faces etc. */
2793 reseat (it, it->current.pos, 1);
2794 }
2795
2796 CHECK_IT (it);
2797 }
2798
2799
2800 /* Initialize IT for the display of window W with window start POS. */
2801
2802 void
2803 start_display (it, w, pos)
2804 struct it *it;
2805 struct window *w;
2806 struct text_pos pos;
2807 {
2808 struct glyph_row *row;
2809 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
2810
2811 row = w->desired_matrix->rows + first_vpos;
2812 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
2813 it->first_vpos = first_vpos;
2814
2815 /* Don't reseat to previous visible line start if current start
2816 position is in a string or image. */
2817 if (it->method == GET_FROM_BUFFER && it->line_wrap != TRUNCATE)
2818 {
2819 int start_at_line_beg_p;
2820 int first_y = it->current_y;
2821
2822 /* If window start is not at a line start, skip forward to POS to
2823 get the correct continuation lines width. */
2824 start_at_line_beg_p = (CHARPOS (pos) == BEGV
2825 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
2826 if (!start_at_line_beg_p)
2827 {
2828 int new_x;
2829
2830 reseat_at_previous_visible_line_start (it);
2831 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
2832
2833 new_x = it->current_x + it->pixel_width;
2834
2835 /* If lines are continued, this line may end in the middle
2836 of a multi-glyph character (e.g. a control character
2837 displayed as \003, or in the middle of an overlay
2838 string). In this case move_it_to above will not have
2839 taken us to the start of the continuation line but to the
2840 end of the continued line. */
2841 if (it->current_x > 0
2842 && it->line_wrap != TRUNCATE /* Lines are continued. */
2843 && (/* And glyph doesn't fit on the line. */
2844 new_x > it->last_visible_x
2845 /* Or it fits exactly and we're on a window
2846 system frame. */
2847 || (new_x == it->last_visible_x
2848 && FRAME_WINDOW_P (it->f))))
2849 {
2850 if (it->current.dpvec_index >= 0
2851 || it->current.overlay_string_index >= 0)
2852 {
2853 set_iterator_to_next (it, 1);
2854 move_it_in_display_line_to (it, -1, -1, 0);
2855 }
2856
2857 it->continuation_lines_width += it->current_x;
2858 }
2859
2860 /* We're starting a new display line, not affected by the
2861 height of the continued line, so clear the appropriate
2862 fields in the iterator structure. */
2863 it->max_ascent = it->max_descent = 0;
2864 it->max_phys_ascent = it->max_phys_descent = 0;
2865
2866 it->current_y = first_y;
2867 it->vpos = 0;
2868 it->current_x = it->hpos = 0;
2869 }
2870 }
2871 }
2872
2873
2874 /* Return 1 if POS is a position in ellipses displayed for invisible
2875 text. W is the window we display, for text property lookup. */
2876
2877 static int
2878 in_ellipses_for_invisible_text_p (pos, w)
2879 struct display_pos *pos;
2880 struct window *w;
2881 {
2882 Lisp_Object prop, window;
2883 int ellipses_p = 0;
2884 int charpos = CHARPOS (pos->pos);
2885
2886 /* If POS specifies a position in a display vector, this might
2887 be for an ellipsis displayed for invisible text. We won't
2888 get the iterator set up for delivering that ellipsis unless
2889 we make sure that it gets aware of the invisible text. */
2890 if (pos->dpvec_index >= 0
2891 && pos->overlay_string_index < 0
2892 && CHARPOS (pos->string_pos) < 0
2893 && charpos > BEGV
2894 && (XSETWINDOW (window, w),
2895 prop = Fget_char_property (make_number (charpos),
2896 Qinvisible, window),
2897 !TEXT_PROP_MEANS_INVISIBLE (prop)))
2898 {
2899 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
2900 window);
2901 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
2902 }
2903
2904 return ellipses_p;
2905 }
2906
2907
2908 /* Initialize IT for stepping through current_buffer in window W,
2909 starting at position POS that includes overlay string and display
2910 vector/ control character translation position information. Value
2911 is zero if there are overlay strings with newlines at POS. */
2912
2913 static int
2914 init_from_display_pos (it, w, pos)
2915 struct it *it;
2916 struct window *w;
2917 struct display_pos *pos;
2918 {
2919 int charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
2920 int i, overlay_strings_with_newlines = 0;
2921
2922 /* If POS specifies a position in a display vector, this might
2923 be for an ellipsis displayed for invisible text. We won't
2924 get the iterator set up for delivering that ellipsis unless
2925 we make sure that it gets aware of the invisible text. */
2926 if (in_ellipses_for_invisible_text_p (pos, w))
2927 {
2928 --charpos;
2929 bytepos = 0;
2930 }
2931
2932 /* Keep in mind: the call to reseat in init_iterator skips invisible
2933 text, so we might end up at a position different from POS. This
2934 is only a problem when POS is a row start after a newline and an
2935 overlay starts there with an after-string, and the overlay has an
2936 invisible property. Since we don't skip invisible text in
2937 display_line and elsewhere immediately after consuming the
2938 newline before the row start, such a POS will not be in a string,
2939 but the call to init_iterator below will move us to the
2940 after-string. */
2941 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
2942
2943 /* This only scans the current chunk -- it should scan all chunks.
2944 However, OVERLAY_STRING_CHUNK_SIZE has been increased from 3 in 21.1
2945 to 16 in 22.1 to make this a lesser problem. */
2946 for (i = 0; i < it->n_overlay_strings && i < OVERLAY_STRING_CHUNK_SIZE; ++i)
2947 {
2948 const char *s = SDATA (it->overlay_strings[i]);
2949 const char *e = s + SBYTES (it->overlay_strings[i]);
2950
2951 while (s < e && *s != '\n')
2952 ++s;
2953
2954 if (s < e)
2955 {
2956 overlay_strings_with_newlines = 1;
2957 break;
2958 }
2959 }
2960
2961 /* If position is within an overlay string, set up IT to the right
2962 overlay string. */
2963 if (pos->overlay_string_index >= 0)
2964 {
2965 int relative_index;
2966
2967 /* If the first overlay string happens to have a `display'
2968 property for an image, the iterator will be set up for that
2969 image, and we have to undo that setup first before we can
2970 correct the overlay string index. */
2971 if (it->method == GET_FROM_IMAGE)
2972 pop_it (it);
2973
2974 /* We already have the first chunk of overlay strings in
2975 IT->overlay_strings. Load more until the one for
2976 pos->overlay_string_index is in IT->overlay_strings. */
2977 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
2978 {
2979 int n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
2980 it->current.overlay_string_index = 0;
2981 while (n--)
2982 {
2983 load_overlay_strings (it, 0);
2984 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
2985 }
2986 }
2987
2988 it->current.overlay_string_index = pos->overlay_string_index;
2989 relative_index = (it->current.overlay_string_index
2990 % OVERLAY_STRING_CHUNK_SIZE);
2991 it->string = it->overlay_strings[relative_index];
2992 xassert (STRINGP (it->string));
2993 it->current.string_pos = pos->string_pos;
2994 it->method = GET_FROM_STRING;
2995 }
2996
2997 if (CHARPOS (pos->string_pos) >= 0)
2998 {
2999 /* Recorded position is not in an overlay string, but in another
3000 string. This can only be a string from a `display' property.
3001 IT should already be filled with that string. */
3002 it->current.string_pos = pos->string_pos;
3003 xassert (STRINGP (it->string));
3004 }
3005
3006 /* Restore position in display vector translations, control
3007 character translations or ellipses. */
3008 if (pos->dpvec_index >= 0)
3009 {
3010 if (it->dpvec == NULL)
3011 get_next_display_element (it);
3012 xassert (it->dpvec && it->current.dpvec_index == 0);
3013 it->current.dpvec_index = pos->dpvec_index;
3014 }
3015
3016 CHECK_IT (it);
3017 return !overlay_strings_with_newlines;
3018 }
3019
3020
3021 /* Initialize IT for stepping through current_buffer in window W
3022 starting at ROW->start. */
3023
3024 static void
3025 init_to_row_start (it, w, row)
3026 struct it *it;
3027 struct window *w;
3028 struct glyph_row *row;
3029 {
3030 init_from_display_pos (it, w, &row->start);
3031 it->start = row->start;
3032 it->continuation_lines_width = row->continuation_lines_width;
3033 CHECK_IT (it);
3034 }
3035
3036
3037 /* Initialize IT for stepping through current_buffer in window W
3038 starting in the line following ROW, i.e. starting at ROW->end.
3039 Value is zero if there are overlay strings with newlines at ROW's
3040 end position. */
3041
3042 static int
3043 init_to_row_end (it, w, row)
3044 struct it *it;
3045 struct window *w;
3046 struct glyph_row *row;
3047 {
3048 int success = 0;
3049
3050 if (init_from_display_pos (it, w, &row->end))
3051 {
3052 if (row->continued_p)
3053 it->continuation_lines_width
3054 = row->continuation_lines_width + row->pixel_width;
3055 CHECK_IT (it);
3056 success = 1;
3057 }
3058
3059 return success;
3060 }
3061
3062
3063
3064 \f
3065 /***********************************************************************
3066 Text properties
3067 ***********************************************************************/
3068
3069 /* Called when IT reaches IT->stop_charpos. Handle text property and
3070 overlay changes. Set IT->stop_charpos to the next position where
3071 to stop. */
3072
3073 static void
3074 handle_stop (it)
3075 struct it *it;
3076 {
3077 enum prop_handled handled;
3078 int handle_overlay_change_p;
3079 struct props *p;
3080
3081 it->dpvec = NULL;
3082 it->current.dpvec_index = -1;
3083 handle_overlay_change_p = !it->ignore_overlay_strings_at_pos_p;
3084 it->ignore_overlay_strings_at_pos_p = 0;
3085 it->ellipsis_p = 0;
3086
3087 /* Use face of preceding text for ellipsis (if invisible) */
3088 if (it->selective_display_ellipsis_p)
3089 it->saved_face_id = it->face_id;
3090
3091 do
3092 {
3093 handled = HANDLED_NORMALLY;
3094
3095 /* Call text property handlers. */
3096 for (p = it_props; p->handler; ++p)
3097 {
3098 handled = p->handler (it);
3099
3100 if (handled == HANDLED_RECOMPUTE_PROPS)
3101 break;
3102 else if (handled == HANDLED_RETURN)
3103 {
3104 /* We still want to show before and after strings from
3105 overlays even if the actual buffer text is replaced. */
3106 if (!handle_overlay_change_p
3107 || it->sp > 1
3108 || !get_overlay_strings_1 (it, 0, 0))
3109 {
3110 if (it->ellipsis_p)
3111 setup_for_ellipsis (it, 0);
3112 /* When handling a display spec, we might load an
3113 empty string. In that case, discard it here. We
3114 used to discard it in handle_single_display_spec,
3115 but that causes get_overlay_strings_1, above, to
3116 ignore overlay strings that we must check. */
3117 if (STRINGP (it->string) && !SCHARS (it->string))
3118 pop_it (it);
3119 return;
3120 }
3121 else if (STRINGP (it->string) && !SCHARS (it->string))
3122 pop_it (it);
3123 else
3124 {
3125 it->ignore_overlay_strings_at_pos_p = 1;
3126 it->string_from_display_prop_p = 0;
3127 handle_overlay_change_p = 0;
3128 }
3129 handled = HANDLED_RECOMPUTE_PROPS;
3130 break;
3131 }
3132 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
3133 handle_overlay_change_p = 0;
3134 }
3135
3136 if (handled != HANDLED_RECOMPUTE_PROPS)
3137 {
3138 /* Don't check for overlay strings below when set to deliver
3139 characters from a display vector. */
3140 if (it->method == GET_FROM_DISPLAY_VECTOR)
3141 handle_overlay_change_p = 0;
3142
3143 /* Handle overlay changes.
3144 This sets HANDLED to HANDLED_RECOMPUTE_PROPS
3145 if it finds overlays. */
3146 if (handle_overlay_change_p)
3147 handled = handle_overlay_change (it);
3148 }
3149
3150 if (it->ellipsis_p)
3151 {
3152 setup_for_ellipsis (it, 0);
3153 break;
3154 }
3155 }
3156 while (handled == HANDLED_RECOMPUTE_PROPS);
3157
3158 /* Determine where to stop next. */
3159 if (handled == HANDLED_NORMALLY)
3160 compute_stop_pos (it);
3161 }
3162
3163
3164 /* Compute IT->stop_charpos from text property and overlay change
3165 information for IT's current position. */
3166
3167 static void
3168 compute_stop_pos (it)
3169 struct it *it;
3170 {
3171 register INTERVAL iv, next_iv;
3172 Lisp_Object object, limit, position;
3173 EMACS_INT charpos, bytepos;
3174
3175 /* If nowhere else, stop at the end. */
3176 it->stop_charpos = it->end_charpos;
3177
3178 if (STRINGP (it->string))
3179 {
3180 /* Strings are usually short, so don't limit the search for
3181 properties. */
3182 object = it->string;
3183 limit = Qnil;
3184 charpos = IT_STRING_CHARPOS (*it);
3185 bytepos = IT_STRING_BYTEPOS (*it);
3186 }
3187 else
3188 {
3189 EMACS_INT pos;
3190
3191 /* If next overlay change is in front of the current stop pos
3192 (which is IT->end_charpos), stop there. Note: value of
3193 next_overlay_change is point-max if no overlay change
3194 follows. */
3195 charpos = IT_CHARPOS (*it);
3196 bytepos = IT_BYTEPOS (*it);
3197 pos = next_overlay_change (charpos);
3198 if (pos < it->stop_charpos)
3199 it->stop_charpos = pos;
3200
3201 /* If showing the region, we have to stop at the region
3202 start or end because the face might change there. */
3203 if (it->region_beg_charpos > 0)
3204 {
3205 if (IT_CHARPOS (*it) < it->region_beg_charpos)
3206 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
3207 else if (IT_CHARPOS (*it) < it->region_end_charpos)
3208 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
3209 }
3210
3211 /* Set up variables for computing the stop position from text
3212 property changes. */
3213 XSETBUFFER (object, current_buffer);
3214 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
3215 }
3216
3217 /* Get the interval containing IT's position. Value is a null
3218 interval if there isn't such an interval. */
3219 position = make_number (charpos);
3220 iv = validate_interval_range (object, &position, &position, 0);
3221 if (!NULL_INTERVAL_P (iv))
3222 {
3223 Lisp_Object values_here[LAST_PROP_IDX];
3224 struct props *p;
3225
3226 /* Get properties here. */
3227 for (p = it_props; p->handler; ++p)
3228 values_here[p->idx] = textget (iv->plist, *p->name);
3229
3230 /* Look for an interval following iv that has different
3231 properties. */
3232 for (next_iv = next_interval (iv);
3233 (!NULL_INTERVAL_P (next_iv)
3234 && (NILP (limit)
3235 || XFASTINT (limit) > next_iv->position));
3236 next_iv = next_interval (next_iv))
3237 {
3238 for (p = it_props; p->handler; ++p)
3239 {
3240 Lisp_Object new_value;
3241
3242 new_value = textget (next_iv->plist, *p->name);
3243 if (!EQ (values_here[p->idx], new_value))
3244 break;
3245 }
3246
3247 if (p->handler)
3248 break;
3249 }
3250
3251 if (!NULL_INTERVAL_P (next_iv))
3252 {
3253 if (INTEGERP (limit)
3254 && next_iv->position >= XFASTINT (limit))
3255 /* No text property change up to limit. */
3256 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
3257 else
3258 /* Text properties change in next_iv. */
3259 it->stop_charpos = min (it->stop_charpos, next_iv->position);
3260 }
3261 }
3262
3263 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos,
3264 it->stop_charpos, it->string);
3265
3266 xassert (STRINGP (it->string)
3267 || (it->stop_charpos >= BEGV
3268 && it->stop_charpos >= IT_CHARPOS (*it)));
3269 }
3270
3271
3272 /* Return the position of the next overlay change after POS in
3273 current_buffer. Value is point-max if no overlay change
3274 follows. This is like `next-overlay-change' but doesn't use
3275 xmalloc. */
3276
3277 static EMACS_INT
3278 next_overlay_change (pos)
3279 EMACS_INT pos;
3280 {
3281 int noverlays;
3282 EMACS_INT endpos;
3283 Lisp_Object *overlays;
3284 int i;
3285
3286 /* Get all overlays at the given position. */
3287 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, 1);
3288
3289 /* If any of these overlays ends before endpos,
3290 use its ending point instead. */
3291 for (i = 0; i < noverlays; ++i)
3292 {
3293 Lisp_Object oend;
3294 EMACS_INT oendpos;
3295
3296 oend = OVERLAY_END (overlays[i]);
3297 oendpos = OVERLAY_POSITION (oend);
3298 endpos = min (endpos, oendpos);
3299 }
3300
3301 return endpos;
3302 }
3303
3304
3305 \f
3306 /***********************************************************************
3307 Fontification
3308 ***********************************************************************/
3309
3310 /* Handle changes in the `fontified' property of the current buffer by
3311 calling hook functions from Qfontification_functions to fontify
3312 regions of text. */
3313
3314 static enum prop_handled
3315 handle_fontified_prop (it)
3316 struct it *it;
3317 {
3318 Lisp_Object prop, pos;
3319 enum prop_handled handled = HANDLED_NORMALLY;
3320
3321 if (!NILP (Vmemory_full))
3322 return handled;
3323
3324 /* Get the value of the `fontified' property at IT's current buffer
3325 position. (The `fontified' property doesn't have a special
3326 meaning in strings.) If the value is nil, call functions from
3327 Qfontification_functions. */
3328 if (!STRINGP (it->string)
3329 && it->s == NULL
3330 && !NILP (Vfontification_functions)
3331 && !NILP (Vrun_hooks)
3332 && (pos = make_number (IT_CHARPOS (*it)),
3333 prop = Fget_char_property (pos, Qfontified, Qnil),
3334 /* Ignore the special cased nil value always present at EOB since
3335 no amount of fontifying will be able to change it. */
3336 NILP (prop) && IT_CHARPOS (*it) < Z))
3337 {
3338 int count = SPECPDL_INDEX ();
3339 Lisp_Object val;
3340
3341 val = Vfontification_functions;
3342 specbind (Qfontification_functions, Qnil);
3343
3344 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
3345 safe_call1 (val, pos);
3346 else
3347 {
3348 Lisp_Object globals, fn;
3349 struct gcpro gcpro1, gcpro2;
3350
3351 globals = Qnil;
3352 GCPRO2 (val, globals);
3353
3354 for (; CONSP (val); val = XCDR (val))
3355 {
3356 fn = XCAR (val);
3357
3358 if (EQ (fn, Qt))
3359 {
3360 /* A value of t indicates this hook has a local
3361 binding; it means to run the global binding too.
3362 In a global value, t should not occur. If it
3363 does, we must ignore it to avoid an endless
3364 loop. */
3365 for (globals = Fdefault_value (Qfontification_functions);
3366 CONSP (globals);
3367 globals = XCDR (globals))
3368 {
3369 fn = XCAR (globals);
3370 if (!EQ (fn, Qt))
3371 safe_call1 (fn, pos);
3372 }
3373 }
3374 else
3375 safe_call1 (fn, pos);
3376 }
3377
3378 UNGCPRO;
3379 }
3380
3381 unbind_to (count, Qnil);
3382
3383 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
3384 something. This avoids an endless loop if they failed to
3385 fontify the text for which reason ever. */
3386 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
3387 handled = HANDLED_RECOMPUTE_PROPS;
3388 }
3389
3390 return handled;
3391 }
3392
3393
3394 \f
3395 /***********************************************************************
3396 Faces
3397 ***********************************************************************/
3398
3399 /* Set up iterator IT from face properties at its current position.
3400 Called from handle_stop. */
3401
3402 static enum prop_handled
3403 handle_face_prop (it)
3404 struct it *it;
3405 {
3406 int new_face_id;
3407 EMACS_INT next_stop;
3408
3409 if (!STRINGP (it->string))
3410 {
3411 new_face_id
3412 = face_at_buffer_position (it->w,
3413 IT_CHARPOS (*it),
3414 it->region_beg_charpos,
3415 it->region_end_charpos,
3416 &next_stop,
3417 (IT_CHARPOS (*it)
3418 + TEXT_PROP_DISTANCE_LIMIT),
3419 0, it->base_face_id);
3420
3421 /* Is this a start of a run of characters with box face?
3422 Caveat: this can be called for a freshly initialized
3423 iterator; face_id is -1 in this case. We know that the new
3424 face will not change until limit, i.e. if the new face has a
3425 box, all characters up to limit will have one. But, as
3426 usual, we don't know whether limit is really the end. */
3427 if (new_face_id != it->face_id)
3428 {
3429 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3430
3431 /* If new face has a box but old face has not, this is
3432 the start of a run of characters with box, i.e. it has
3433 a shadow on the left side. The value of face_id of the
3434 iterator will be -1 if this is the initial call that gets
3435 the face. In this case, we have to look in front of IT's
3436 position and see whether there is a face != new_face_id. */
3437 it->start_of_box_run_p
3438 = (new_face->box != FACE_NO_BOX
3439 && (it->face_id >= 0
3440 || IT_CHARPOS (*it) == BEG
3441 || new_face_id != face_before_it_pos (it)));
3442 it->face_box_p = new_face->box != FACE_NO_BOX;
3443 }
3444 }
3445 else
3446 {
3447 int base_face_id, bufpos;
3448 int i;
3449 Lisp_Object from_overlay
3450 = (it->current.overlay_string_index >= 0
3451 ? it->string_overlays[it->current.overlay_string_index]
3452 : Qnil);
3453
3454 /* See if we got to this string directly or indirectly from
3455 an overlay property. That includes the before-string or
3456 after-string of an overlay, strings in display properties
3457 provided by an overlay, their text properties, etc.
3458
3459 FROM_OVERLAY is the overlay that brought us here, or nil if none. */
3460 if (! NILP (from_overlay))
3461 for (i = it->sp - 1; i >= 0; i--)
3462 {
3463 if (it->stack[i].current.overlay_string_index >= 0)
3464 from_overlay
3465 = it->string_overlays[it->stack[i].current.overlay_string_index];
3466 else if (! NILP (it->stack[i].from_overlay))
3467 from_overlay = it->stack[i].from_overlay;
3468
3469 if (!NILP (from_overlay))
3470 break;
3471 }
3472
3473 if (! NILP (from_overlay))
3474 {
3475 bufpos = IT_CHARPOS (*it);
3476 /* For a string from an overlay, the base face depends
3477 only on text properties and ignores overlays. */
3478 base_face_id
3479 = face_for_overlay_string (it->w,
3480 IT_CHARPOS (*it),
3481 it->region_beg_charpos,
3482 it->region_end_charpos,
3483 &next_stop,
3484 (IT_CHARPOS (*it)
3485 + TEXT_PROP_DISTANCE_LIMIT),
3486 0,
3487 from_overlay);
3488 }
3489 else
3490 {
3491 bufpos = 0;
3492
3493 /* For strings from a `display' property, use the face at
3494 IT's current buffer position as the base face to merge
3495 with, so that overlay strings appear in the same face as
3496 surrounding text, unless they specify their own
3497 faces. */
3498 base_face_id = underlying_face_id (it);
3499 }
3500
3501 new_face_id = face_at_string_position (it->w,
3502 it->string,
3503 IT_STRING_CHARPOS (*it),
3504 bufpos,
3505 it->region_beg_charpos,
3506 it->region_end_charpos,
3507 &next_stop,
3508 base_face_id, 0);
3509
3510 /* Is this a start of a run of characters with box? Caveat:
3511 this can be called for a freshly allocated iterator; face_id
3512 is -1 is this case. We know that the new face will not
3513 change until the next check pos, i.e. if the new face has a
3514 box, all characters up to that position will have a
3515 box. But, as usual, we don't know whether that position
3516 is really the end. */
3517 if (new_face_id != it->face_id)
3518 {
3519 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3520 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3521
3522 /* If new face has a box but old face hasn't, this is the
3523 start of a run of characters with box, i.e. it has a
3524 shadow on the left side. */
3525 it->start_of_box_run_p
3526 = new_face->box && (old_face == NULL || !old_face->box);
3527 it->face_box_p = new_face->box != FACE_NO_BOX;
3528 }
3529 }
3530
3531 it->face_id = new_face_id;
3532 return HANDLED_NORMALLY;
3533 }
3534
3535
3536 /* Return the ID of the face ``underlying'' IT's current position,
3537 which is in a string. If the iterator is associated with a
3538 buffer, return the face at IT's current buffer position.
3539 Otherwise, use the iterator's base_face_id. */
3540
3541 static int
3542 underlying_face_id (it)
3543 struct it *it;
3544 {
3545 int face_id = it->base_face_id, i;
3546
3547 xassert (STRINGP (it->string));
3548
3549 for (i = it->sp - 1; i >= 0; --i)
3550 if (NILP (it->stack[i].string))
3551 face_id = it->stack[i].face_id;
3552
3553 return face_id;
3554 }
3555
3556
3557 /* Compute the face one character before or after the current position
3558 of IT. BEFORE_P non-zero means get the face in front of IT's
3559 position. Value is the id of the face. */
3560
3561 static int
3562 face_before_or_after_it_pos (it, before_p)
3563 struct it *it;
3564 int before_p;
3565 {
3566 int face_id, limit;
3567 EMACS_INT next_check_charpos;
3568 struct text_pos pos;
3569
3570 xassert (it->s == NULL);
3571
3572 if (STRINGP (it->string))
3573 {
3574 int bufpos, base_face_id;
3575
3576 /* No face change past the end of the string (for the case
3577 we are padding with spaces). No face change before the
3578 string start. */
3579 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
3580 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
3581 return it->face_id;
3582
3583 /* Set pos to the position before or after IT's current position. */
3584 if (before_p)
3585 pos = string_pos (IT_STRING_CHARPOS (*it) - 1, it->string);
3586 else
3587 /* For composition, we must check the character after the
3588 composition. */
3589 pos = (it->what == IT_COMPOSITION
3590 ? string_pos (IT_STRING_CHARPOS (*it)
3591 + it->cmp_it.nchars, it->string)
3592 : string_pos (IT_STRING_CHARPOS (*it) + 1, it->string));
3593
3594 if (it->current.overlay_string_index >= 0)
3595 bufpos = IT_CHARPOS (*it);
3596 else
3597 bufpos = 0;
3598
3599 base_face_id = underlying_face_id (it);
3600
3601 /* Get the face for ASCII, or unibyte. */
3602 face_id = face_at_string_position (it->w,
3603 it->string,
3604 CHARPOS (pos),
3605 bufpos,
3606 it->region_beg_charpos,
3607 it->region_end_charpos,
3608 &next_check_charpos,
3609 base_face_id, 0);
3610
3611 /* Correct the face for charsets different from ASCII. Do it
3612 for the multibyte case only. The face returned above is
3613 suitable for unibyte text if IT->string is unibyte. */
3614 if (STRING_MULTIBYTE (it->string))
3615 {
3616 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos);
3617 int rest = SBYTES (it->string) - BYTEPOS (pos);
3618 int c, len;
3619 struct face *face = FACE_FROM_ID (it->f, face_id);
3620
3621 c = string_char_and_length (p, &len);
3622 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), it->string);
3623 }
3624 }
3625 else
3626 {
3627 if ((IT_CHARPOS (*it) >= ZV && !before_p)
3628 || (IT_CHARPOS (*it) <= BEGV && before_p))
3629 return it->face_id;
3630
3631 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
3632 pos = it->current.pos;
3633
3634 if (before_p)
3635 DEC_TEXT_POS (pos, it->multibyte_p);
3636 else
3637 {
3638 if (it->what == IT_COMPOSITION)
3639 /* For composition, we must check the position after the
3640 composition. */
3641 pos.charpos += it->cmp_it.nchars, pos.bytepos += it->len;
3642 else
3643 INC_TEXT_POS (pos, it->multibyte_p);
3644 }
3645
3646 /* Determine face for CHARSET_ASCII, or unibyte. */
3647 face_id = face_at_buffer_position (it->w,
3648 CHARPOS (pos),
3649 it->region_beg_charpos,
3650 it->region_end_charpos,
3651 &next_check_charpos,
3652 limit, 0, -1);
3653
3654 /* Correct the face for charsets different from ASCII. Do it
3655 for the multibyte case only. The face returned above is
3656 suitable for unibyte text if current_buffer is unibyte. */
3657 if (it->multibyte_p)
3658 {
3659 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
3660 struct face *face = FACE_FROM_ID (it->f, face_id);
3661 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), Qnil);
3662 }
3663 }
3664
3665 return face_id;
3666 }
3667
3668
3669 \f
3670 /***********************************************************************
3671 Invisible text
3672 ***********************************************************************/
3673
3674 /* Set up iterator IT from invisible properties at its current
3675 position. Called from handle_stop. */
3676
3677 static enum prop_handled
3678 handle_invisible_prop (it)
3679 struct it *it;
3680 {
3681 enum prop_handled handled = HANDLED_NORMALLY;
3682
3683 if (STRINGP (it->string))
3684 {
3685 extern Lisp_Object Qinvisible;
3686 Lisp_Object prop, end_charpos, limit, charpos;
3687
3688 /* Get the value of the invisible text property at the
3689 current position. Value will be nil if there is no such
3690 property. */
3691 charpos = make_number (IT_STRING_CHARPOS (*it));
3692 prop = Fget_text_property (charpos, Qinvisible, it->string);
3693
3694 if (!NILP (prop)
3695 && IT_STRING_CHARPOS (*it) < it->end_charpos)
3696 {
3697 handled = HANDLED_RECOMPUTE_PROPS;
3698
3699 /* Get the position at which the next change of the
3700 invisible text property can be found in IT->string.
3701 Value will be nil if the property value is the same for
3702 all the rest of IT->string. */
3703 XSETINT (limit, SCHARS (it->string));
3704 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
3705 it->string, limit);
3706
3707 /* Text at current position is invisible. The next
3708 change in the property is at position end_charpos.
3709 Move IT's current position to that position. */
3710 if (INTEGERP (end_charpos)
3711 && XFASTINT (end_charpos) < XFASTINT (limit))
3712 {
3713 struct text_pos old;
3714 old = it->current.string_pos;
3715 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
3716 compute_string_pos (&it->current.string_pos, old, it->string);
3717 }
3718 else
3719 {
3720 /* The rest of the string is invisible. If this is an
3721 overlay string, proceed with the next overlay string
3722 or whatever comes and return a character from there. */
3723 if (it->current.overlay_string_index >= 0)
3724 {
3725 next_overlay_string (it);
3726 /* Don't check for overlay strings when we just
3727 finished processing them. */
3728 handled = HANDLED_OVERLAY_STRING_CONSUMED;
3729 }
3730 else
3731 {
3732 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
3733 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
3734 }
3735 }
3736 }
3737 }
3738 else
3739 {
3740 int invis_p;
3741 EMACS_INT newpos, next_stop, start_charpos;
3742 Lisp_Object pos, prop, overlay;
3743
3744 /* First of all, is there invisible text at this position? */
3745 start_charpos = IT_CHARPOS (*it);
3746 pos = make_number (IT_CHARPOS (*it));
3747 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
3748 &overlay);
3749 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3750
3751 /* If we are on invisible text, skip over it. */
3752 if (invis_p && IT_CHARPOS (*it) < it->end_charpos)
3753 {
3754 /* Record whether we have to display an ellipsis for the
3755 invisible text. */
3756 int display_ellipsis_p = invis_p == 2;
3757
3758 handled = HANDLED_RECOMPUTE_PROPS;
3759
3760 /* Loop skipping over invisible text. The loop is left at
3761 ZV or with IT on the first char being visible again. */
3762 do
3763 {
3764 /* Try to skip some invisible text. Return value is the
3765 position reached which can be equal to IT's position
3766 if there is nothing invisible here. This skips both
3767 over invisible text properties and overlays with
3768 invisible property. */
3769 newpos = skip_invisible (IT_CHARPOS (*it),
3770 &next_stop, ZV, it->window);
3771
3772 /* If we skipped nothing at all we weren't at invisible
3773 text in the first place. If everything to the end of
3774 the buffer was skipped, end the loop. */
3775 if (newpos == IT_CHARPOS (*it) || newpos >= ZV)
3776 invis_p = 0;
3777 else
3778 {
3779 /* We skipped some characters but not necessarily
3780 all there are. Check if we ended up on visible
3781 text. Fget_char_property returns the property of
3782 the char before the given position, i.e. if we
3783 get invis_p = 0, this means that the char at
3784 newpos is visible. */
3785 pos = make_number (newpos);
3786 prop = Fget_char_property (pos, Qinvisible, it->window);
3787 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3788 }
3789
3790 /* If we ended up on invisible text, proceed to
3791 skip starting with next_stop. */
3792 if (invis_p)
3793 IT_CHARPOS (*it) = next_stop;
3794
3795 /* If there are adjacent invisible texts, don't lose the
3796 second one's ellipsis. */
3797 if (invis_p == 2)
3798 display_ellipsis_p = 1;
3799 }
3800 while (invis_p);
3801
3802 /* The position newpos is now either ZV or on visible text. */
3803 IT_CHARPOS (*it) = newpos;
3804 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
3805
3806 /* If there are before-strings at the start of invisible
3807 text, and the text is invisible because of a text
3808 property, arrange to show before-strings because 20.x did
3809 it that way. (If the text is invisible because of an
3810 overlay property instead of a text property, this is
3811 already handled in the overlay code.) */
3812 if (NILP (overlay)
3813 && get_overlay_strings (it, start_charpos))
3814 {
3815 handled = HANDLED_RECOMPUTE_PROPS;
3816 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
3817 }
3818 else if (display_ellipsis_p)
3819 {
3820 /* Make sure that the glyphs of the ellipsis will get
3821 correct `charpos' values. If we would not update
3822 it->position here, the glyphs would belong to the
3823 last visible character _before_ the invisible
3824 text, which confuses `set_cursor_from_row'.
3825
3826 We use the last invisible position instead of the
3827 first because this way the cursor is always drawn on
3828 the first "." of the ellipsis, whenever PT is inside
3829 the invisible text. Otherwise the cursor would be
3830 placed _after_ the ellipsis when the point is after the
3831 first invisible character. */
3832 if (!STRINGP (it->object))
3833 {
3834 it->position.charpos = IT_CHARPOS (*it) - 1;
3835 it->position.bytepos = CHAR_TO_BYTE (it->position.charpos);
3836 }
3837 it->ellipsis_p = 1;
3838 /* Let the ellipsis display before
3839 considering any properties of the following char.
3840 Fixes jasonr@gnu.org 01 Oct 07 bug. */
3841 handled = HANDLED_RETURN;
3842 }
3843 }
3844 }
3845
3846 return handled;
3847 }
3848
3849
3850 /* Make iterator IT return `...' next.
3851 Replaces LEN characters from buffer. */
3852
3853 static void
3854 setup_for_ellipsis (it, len)
3855 struct it *it;
3856 int len;
3857 {
3858 /* Use the display table definition for `...'. Invalid glyphs
3859 will be handled by the method returning elements from dpvec. */
3860 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
3861 {
3862 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
3863 it->dpvec = v->contents;
3864 it->dpend = v->contents + v->size;
3865 }
3866 else
3867 {
3868 /* Default `...'. */
3869 it->dpvec = default_invis_vector;
3870 it->dpend = default_invis_vector + 3;
3871 }
3872
3873 it->dpvec_char_len = len;
3874 it->current.dpvec_index = 0;
3875 it->dpvec_face_id = -1;
3876
3877 /* Remember the current face id in case glyphs specify faces.
3878 IT's face is restored in set_iterator_to_next.
3879 saved_face_id was set to preceding char's face in handle_stop. */
3880 if (it->saved_face_id < 0 || it->saved_face_id != it->face_id)
3881 it->saved_face_id = it->face_id = DEFAULT_FACE_ID;
3882
3883 it->method = GET_FROM_DISPLAY_VECTOR;
3884 it->ellipsis_p = 1;
3885 }
3886
3887
3888 \f
3889 /***********************************************************************
3890 'display' property
3891 ***********************************************************************/
3892
3893 /* Set up iterator IT from `display' property at its current position.
3894 Called from handle_stop.
3895 We return HANDLED_RETURN if some part of the display property
3896 overrides the display of the buffer text itself.
3897 Otherwise we return HANDLED_NORMALLY. */
3898
3899 static enum prop_handled
3900 handle_display_prop (it)
3901 struct it *it;
3902 {
3903 Lisp_Object prop, object, overlay;
3904 struct text_pos *position;
3905 /* Nonzero if some property replaces the display of the text itself. */
3906 int display_replaced_p = 0;
3907
3908 if (STRINGP (it->string))
3909 {
3910 object = it->string;
3911 position = &it->current.string_pos;
3912 }
3913 else
3914 {
3915 XSETWINDOW (object, it->w);
3916 position = &it->current.pos;
3917 }
3918
3919 /* Reset those iterator values set from display property values. */
3920 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
3921 it->space_width = Qnil;
3922 it->font_height = Qnil;
3923 it->voffset = 0;
3924
3925 /* We don't support recursive `display' properties, i.e. string
3926 values that have a string `display' property, that have a string
3927 `display' property etc. */
3928 if (!it->string_from_display_prop_p)
3929 it->area = TEXT_AREA;
3930
3931 prop = get_char_property_and_overlay (make_number (position->charpos),
3932 Qdisplay, object, &overlay);
3933 if (NILP (prop))
3934 return HANDLED_NORMALLY;
3935 /* Now OVERLAY is the overlay that gave us this property, or nil
3936 if it was a text property. */
3937
3938 if (!STRINGP (it->string))
3939 object = it->w->buffer;
3940
3941 if (CONSP (prop)
3942 /* Simple properties. */
3943 && !EQ (XCAR (prop), Qimage)
3944 && !EQ (XCAR (prop), Qspace)
3945 && !EQ (XCAR (prop), Qwhen)
3946 && !EQ (XCAR (prop), Qslice)
3947 && !EQ (XCAR (prop), Qspace_width)
3948 && !EQ (XCAR (prop), Qheight)
3949 && !EQ (XCAR (prop), Qraise)
3950 /* Marginal area specifications. */
3951 && !(CONSP (XCAR (prop)) && EQ (XCAR (XCAR (prop)), Qmargin))
3952 && !EQ (XCAR (prop), Qleft_fringe)
3953 && !EQ (XCAR (prop), Qright_fringe)
3954 && !NILP (XCAR (prop)))
3955 {
3956 for (; CONSP (prop); prop = XCDR (prop))
3957 {
3958 if (handle_single_display_spec (it, XCAR (prop), object, overlay,
3959 position, display_replaced_p))
3960 {
3961 display_replaced_p = 1;
3962 /* If some text in a string is replaced, `position' no
3963 longer points to the position of `object'. */
3964 if (STRINGP (object))
3965 break;
3966 }
3967 }
3968 }
3969 else if (VECTORP (prop))
3970 {
3971 int i;
3972 for (i = 0; i < ASIZE (prop); ++i)
3973 if (handle_single_display_spec (it, AREF (prop, i), object, overlay,
3974 position, display_replaced_p))
3975 {
3976 display_replaced_p = 1;
3977 /* If some text in a string is replaced, `position' no
3978 longer points to the position of `object'. */
3979 if (STRINGP (object))
3980 break;
3981 }
3982 }
3983 else
3984 {
3985 if (handle_single_display_spec (it, prop, object, overlay,
3986 position, 0))
3987 display_replaced_p = 1;
3988 }
3989
3990 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
3991 }
3992
3993
3994 /* Value is the position of the end of the `display' property starting
3995 at START_POS in OBJECT. */
3996
3997 static struct text_pos
3998 display_prop_end (it, object, start_pos)
3999 struct it *it;
4000 Lisp_Object object;
4001 struct text_pos start_pos;
4002 {
4003 Lisp_Object end;
4004 struct text_pos end_pos;
4005
4006 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
4007 Qdisplay, object, Qnil);
4008 CHARPOS (end_pos) = XFASTINT (end);
4009 if (STRINGP (object))
4010 compute_string_pos (&end_pos, start_pos, it->string);
4011 else
4012 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
4013
4014 return end_pos;
4015 }
4016
4017
4018 /* Set up IT from a single `display' specification PROP. OBJECT
4019 is the object in which the `display' property was found. *POSITION
4020 is the position at which it was found. DISPLAY_REPLACED_P non-zero
4021 means that we previously saw a display specification which already
4022 replaced text display with something else, for example an image;
4023 we ignore such properties after the first one has been processed.
4024
4025 OVERLAY is the overlay this `display' property came from,
4026 or nil if it was a text property.
4027
4028 If PROP is a `space' or `image' specification, and in some other
4029 cases too, set *POSITION to the position where the `display'
4030 property ends.
4031
4032 Value is non-zero if something was found which replaces the display
4033 of buffer or string text. */
4034
4035 static int
4036 handle_single_display_spec (it, spec, object, overlay, position,
4037 display_replaced_before_p)
4038 struct it *it;
4039 Lisp_Object spec;
4040 Lisp_Object object;
4041 Lisp_Object overlay;
4042 struct text_pos *position;
4043 int display_replaced_before_p;
4044 {
4045 Lisp_Object form;
4046 Lisp_Object location, value;
4047 struct text_pos start_pos, save_pos;
4048 int valid_p;
4049
4050 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
4051 If the result is non-nil, use VALUE instead of SPEC. */
4052 form = Qt;
4053 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
4054 {
4055 spec = XCDR (spec);
4056 if (!CONSP (spec))
4057 return 0;
4058 form = XCAR (spec);
4059 spec = XCDR (spec);
4060 }
4061
4062 if (!NILP (form) && !EQ (form, Qt))
4063 {
4064 int count = SPECPDL_INDEX ();
4065 struct gcpro gcpro1;
4066
4067 /* Bind `object' to the object having the `display' property, a
4068 buffer or string. Bind `position' to the position in the
4069 object where the property was found, and `buffer-position'
4070 to the current position in the buffer. */
4071 specbind (Qobject, object);
4072 specbind (Qposition, make_number (CHARPOS (*position)));
4073 specbind (Qbuffer_position,
4074 make_number (STRINGP (object)
4075 ? IT_CHARPOS (*it) : CHARPOS (*position)));
4076 GCPRO1 (form);
4077 form = safe_eval (form);
4078 UNGCPRO;
4079 unbind_to (count, Qnil);
4080 }
4081
4082 if (NILP (form))
4083 return 0;
4084
4085 /* Handle `(height HEIGHT)' specifications. */
4086 if (CONSP (spec)
4087 && EQ (XCAR (spec), Qheight)
4088 && CONSP (XCDR (spec)))
4089 {
4090 if (!FRAME_WINDOW_P (it->f))
4091 return 0;
4092
4093 it->font_height = XCAR (XCDR (spec));
4094 if (!NILP (it->font_height))
4095 {
4096 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4097 int new_height = -1;
4098
4099 if (CONSP (it->font_height)
4100 && (EQ (XCAR (it->font_height), Qplus)
4101 || EQ (XCAR (it->font_height), Qminus))
4102 && CONSP (XCDR (it->font_height))
4103 && INTEGERP (XCAR (XCDR (it->font_height))))
4104 {
4105 /* `(+ N)' or `(- N)' where N is an integer. */
4106 int steps = XINT (XCAR (XCDR (it->font_height)));
4107 if (EQ (XCAR (it->font_height), Qplus))
4108 steps = - steps;
4109 it->face_id = smaller_face (it->f, it->face_id, steps);
4110 }
4111 else if (FUNCTIONP (it->font_height))
4112 {
4113 /* Call function with current height as argument.
4114 Value is the new height. */
4115 Lisp_Object height;
4116 height = safe_call1 (it->font_height,
4117 face->lface[LFACE_HEIGHT_INDEX]);
4118 if (NUMBERP (height))
4119 new_height = XFLOATINT (height);
4120 }
4121 else if (NUMBERP (it->font_height))
4122 {
4123 /* Value is a multiple of the canonical char height. */
4124 struct face *face;
4125
4126 face = FACE_FROM_ID (it->f,
4127 lookup_basic_face (it->f, DEFAULT_FACE_ID));
4128 new_height = (XFLOATINT (it->font_height)
4129 * XINT (face->lface[LFACE_HEIGHT_INDEX]));
4130 }
4131 else
4132 {
4133 /* Evaluate IT->font_height with `height' bound to the
4134 current specified height to get the new height. */
4135 int count = SPECPDL_INDEX ();
4136
4137 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
4138 value = safe_eval (it->font_height);
4139 unbind_to (count, Qnil);
4140
4141 if (NUMBERP (value))
4142 new_height = XFLOATINT (value);
4143 }
4144
4145 if (new_height > 0)
4146 it->face_id = face_with_height (it->f, it->face_id, new_height);
4147 }
4148
4149 return 0;
4150 }
4151
4152 /* Handle `(space-width WIDTH)'. */
4153 if (CONSP (spec)
4154 && EQ (XCAR (spec), Qspace_width)
4155 && CONSP (XCDR (spec)))
4156 {
4157 if (!FRAME_WINDOW_P (it->f))
4158 return 0;
4159
4160 value = XCAR (XCDR (spec));
4161 if (NUMBERP (value) && XFLOATINT (value) > 0)
4162 it->space_width = value;
4163
4164 return 0;
4165 }
4166
4167 /* Handle `(slice X Y WIDTH HEIGHT)'. */
4168 if (CONSP (spec)
4169 && EQ (XCAR (spec), Qslice))
4170 {
4171 Lisp_Object tem;
4172
4173 if (!FRAME_WINDOW_P (it->f))
4174 return 0;
4175
4176 if (tem = XCDR (spec), CONSP (tem))
4177 {
4178 it->slice.x = XCAR (tem);
4179 if (tem = XCDR (tem), CONSP (tem))
4180 {
4181 it->slice.y = XCAR (tem);
4182 if (tem = XCDR (tem), CONSP (tem))
4183 {
4184 it->slice.width = XCAR (tem);
4185 if (tem = XCDR (tem), CONSP (tem))
4186 it->slice.height = XCAR (tem);
4187 }
4188 }
4189 }
4190
4191 return 0;
4192 }
4193
4194 /* Handle `(raise FACTOR)'. */
4195 if (CONSP (spec)
4196 && EQ (XCAR (spec), Qraise)
4197 && CONSP (XCDR (spec)))
4198 {
4199 if (!FRAME_WINDOW_P (it->f))
4200 return 0;
4201
4202 #ifdef HAVE_WINDOW_SYSTEM
4203 value = XCAR (XCDR (spec));
4204 if (NUMBERP (value))
4205 {
4206 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4207 it->voffset = - (XFLOATINT (value)
4208 * (FONT_HEIGHT (face->font)));
4209 }
4210 #endif /* HAVE_WINDOW_SYSTEM */
4211
4212 return 0;
4213 }
4214
4215 /* Don't handle the other kinds of display specifications
4216 inside a string that we got from a `display' property. */
4217 if (it->string_from_display_prop_p)
4218 return 0;
4219
4220 /* Characters having this form of property are not displayed, so
4221 we have to find the end of the property. */
4222 start_pos = *position;
4223 *position = display_prop_end (it, object, start_pos);
4224 value = Qnil;
4225
4226 /* Stop the scan at that end position--we assume that all
4227 text properties change there. */
4228 it->stop_charpos = position->charpos;
4229
4230 /* Handle `(left-fringe BITMAP [FACE])'
4231 and `(right-fringe BITMAP [FACE])'. */
4232 if (CONSP (spec)
4233 && (EQ (XCAR (spec), Qleft_fringe)
4234 || EQ (XCAR (spec), Qright_fringe))
4235 && CONSP (XCDR (spec)))
4236 {
4237 int face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);
4238 int fringe_bitmap;
4239
4240 if (!FRAME_WINDOW_P (it->f))
4241 /* If we return here, POSITION has been advanced
4242 across the text with this property. */
4243 return 0;
4244
4245 #ifdef HAVE_WINDOW_SYSTEM
4246 value = XCAR (XCDR (spec));
4247 if (!SYMBOLP (value)
4248 || !(fringe_bitmap = lookup_fringe_bitmap (value)))
4249 /* If we return here, POSITION has been advanced
4250 across the text with this property. */
4251 return 0;
4252
4253 if (CONSP (XCDR (XCDR (spec))))
4254 {
4255 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
4256 int face_id2 = lookup_derived_face (it->f, face_name,
4257 FRINGE_FACE_ID, 0);
4258 if (face_id2 >= 0)
4259 face_id = face_id2;
4260 }
4261
4262 /* Save current settings of IT so that we can restore them
4263 when we are finished with the glyph property value. */
4264
4265 save_pos = it->position;
4266 it->position = *position;
4267 push_it (it);
4268 it->position = save_pos;
4269
4270 it->area = TEXT_AREA;
4271 it->what = IT_IMAGE;
4272 it->image_id = -1; /* no image */
4273 it->position = start_pos;
4274 it->object = NILP (object) ? it->w->buffer : object;
4275 it->method = GET_FROM_IMAGE;
4276 it->from_overlay = Qnil;
4277 it->face_id = face_id;
4278
4279 /* Say that we haven't consumed the characters with
4280 `display' property yet. The call to pop_it in
4281 set_iterator_to_next will clean this up. */
4282 *position = start_pos;
4283
4284 if (EQ (XCAR (spec), Qleft_fringe))
4285 {
4286 it->left_user_fringe_bitmap = fringe_bitmap;
4287 it->left_user_fringe_face_id = face_id;
4288 }
4289 else
4290 {
4291 it->right_user_fringe_bitmap = fringe_bitmap;
4292 it->right_user_fringe_face_id = face_id;
4293 }
4294 #endif /* HAVE_WINDOW_SYSTEM */
4295 return 1;
4296 }
4297
4298 /* Prepare to handle `((margin left-margin) ...)',
4299 `((margin right-margin) ...)' and `((margin nil) ...)'
4300 prefixes for display specifications. */
4301 location = Qunbound;
4302 if (CONSP (spec) && CONSP (XCAR (spec)))
4303 {
4304 Lisp_Object tem;
4305
4306 value = XCDR (spec);
4307 if (CONSP (value))
4308 value = XCAR (value);
4309
4310 tem = XCAR (spec);
4311 if (EQ (XCAR (tem), Qmargin)
4312 && (tem = XCDR (tem),
4313 tem = CONSP (tem) ? XCAR (tem) : Qnil,
4314 (NILP (tem)
4315 || EQ (tem, Qleft_margin)
4316 || EQ (tem, Qright_margin))))
4317 location = tem;
4318 }
4319
4320 if (EQ (location, Qunbound))
4321 {
4322 location = Qnil;
4323 value = spec;
4324 }
4325
4326 /* After this point, VALUE is the property after any
4327 margin prefix has been stripped. It must be a string,
4328 an image specification, or `(space ...)'.
4329
4330 LOCATION specifies where to display: `left-margin',
4331 `right-margin' or nil. */
4332
4333 valid_p = (STRINGP (value)
4334 #ifdef HAVE_WINDOW_SYSTEM
4335 || (FRAME_WINDOW_P (it->f) && valid_image_p (value))
4336 #endif /* not HAVE_WINDOW_SYSTEM */
4337 || (CONSP (value) && EQ (XCAR (value), Qspace)));
4338
4339 if (valid_p && !display_replaced_before_p)
4340 {
4341 /* Save current settings of IT so that we can restore them
4342 when we are finished with the glyph property value. */
4343 save_pos = it->position;
4344 it->position = *position;
4345 push_it (it);
4346 it->position = save_pos;
4347 it->from_overlay = overlay;
4348
4349 if (NILP (location))
4350 it->area = TEXT_AREA;
4351 else if (EQ (location, Qleft_margin))
4352 it->area = LEFT_MARGIN_AREA;
4353 else
4354 it->area = RIGHT_MARGIN_AREA;
4355
4356 if (STRINGP (value))
4357 {
4358 it->string = value;
4359 it->multibyte_p = STRING_MULTIBYTE (it->string);
4360 it->current.overlay_string_index = -1;
4361 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
4362 it->end_charpos = it->string_nchars = SCHARS (it->string);
4363 it->method = GET_FROM_STRING;
4364 it->stop_charpos = 0;
4365 it->string_from_display_prop_p = 1;
4366 /* Say that we haven't consumed the characters with
4367 `display' property yet. The call to pop_it in
4368 set_iterator_to_next will clean this up. */
4369 if (BUFFERP (object))
4370 *position = start_pos;
4371 }
4372 else if (CONSP (value) && EQ (XCAR (value), Qspace))
4373 {
4374 it->method = GET_FROM_STRETCH;
4375 it->object = value;
4376 *position = it->position = start_pos;
4377 }
4378 #ifdef HAVE_WINDOW_SYSTEM
4379 else
4380 {
4381 it->what = IT_IMAGE;
4382 it->image_id = lookup_image (it->f, value);
4383 it->position = start_pos;
4384 it->object = NILP (object) ? it->w->buffer : object;
4385 it->method = GET_FROM_IMAGE;
4386
4387 /* Say that we haven't consumed the characters with
4388 `display' property yet. The call to pop_it in
4389 set_iterator_to_next will clean this up. */
4390 *position = start_pos;
4391 }
4392 #endif /* HAVE_WINDOW_SYSTEM */
4393
4394 return 1;
4395 }
4396
4397 /* Invalid property or property not supported. Restore
4398 POSITION to what it was before. */
4399 *position = start_pos;
4400 return 0;
4401 }
4402
4403
4404 /* Check if SPEC is a display sub-property value whose text should be
4405 treated as intangible. */
4406
4407 static int
4408 single_display_spec_intangible_p (prop)
4409 Lisp_Object prop;
4410 {
4411 /* Skip over `when FORM'. */
4412 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
4413 {
4414 prop = XCDR (prop);
4415 if (!CONSP (prop))
4416 return 0;
4417 prop = XCDR (prop);
4418 }
4419
4420 if (STRINGP (prop))
4421 return 1;
4422
4423 if (!CONSP (prop))
4424 return 0;
4425
4426 /* Skip over `margin LOCATION'. If LOCATION is in the margins,
4427 we don't need to treat text as intangible. */
4428 if (EQ (XCAR (prop), Qmargin))
4429 {
4430 prop = XCDR (prop);
4431 if (!CONSP (prop))
4432 return 0;
4433
4434 prop = XCDR (prop);
4435 if (!CONSP (prop)
4436 || EQ (XCAR (prop), Qleft_margin)
4437 || EQ (XCAR (prop), Qright_margin))
4438 return 0;
4439 }
4440
4441 return (CONSP (prop)
4442 && (EQ (XCAR (prop), Qimage)
4443 || EQ (XCAR (prop), Qspace)));
4444 }
4445
4446
4447 /* Check if PROP is a display property value whose text should be
4448 treated as intangible. */
4449
4450 int
4451 display_prop_intangible_p (prop)
4452 Lisp_Object prop;
4453 {
4454 if (CONSP (prop)
4455 && CONSP (XCAR (prop))
4456 && !EQ (Qmargin, XCAR (XCAR (prop))))
4457 {
4458 /* A list of sub-properties. */
4459 while (CONSP (prop))
4460 {
4461 if (single_display_spec_intangible_p (XCAR (prop)))
4462 return 1;
4463 prop = XCDR (prop);
4464 }
4465 }
4466 else if (VECTORP (prop))
4467 {
4468 /* A vector of sub-properties. */
4469 int i;
4470 for (i = 0; i < ASIZE (prop); ++i)
4471 if (single_display_spec_intangible_p (AREF (prop, i)))
4472 return 1;
4473 }
4474 else
4475 return single_display_spec_intangible_p (prop);
4476
4477 return 0;
4478 }
4479
4480
4481 /* Return 1 if PROP is a display sub-property value containing STRING. */
4482
4483 static int
4484 single_display_spec_string_p (prop, string)
4485 Lisp_Object prop, string;
4486 {
4487 if (EQ (string, prop))
4488 return 1;
4489
4490 /* Skip over `when FORM'. */
4491 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
4492 {
4493 prop = XCDR (prop);
4494 if (!CONSP (prop))
4495 return 0;
4496 prop = XCDR (prop);
4497 }
4498
4499 if (CONSP (prop))
4500 /* Skip over `margin LOCATION'. */
4501 if (EQ (XCAR (prop), Qmargin))
4502 {
4503 prop = XCDR (prop);
4504 if (!CONSP (prop))
4505 return 0;
4506
4507 prop = XCDR (prop);
4508 if (!CONSP (prop))
4509 return 0;
4510 }
4511
4512 return CONSP (prop) && EQ (XCAR (prop), string);
4513 }
4514
4515
4516 /* Return 1 if STRING appears in the `display' property PROP. */
4517
4518 static int
4519 display_prop_string_p (prop, string)
4520 Lisp_Object prop, string;
4521 {
4522 if (CONSP (prop)
4523 && CONSP (XCAR (prop))
4524 && !EQ (Qmargin, XCAR (XCAR (prop))))
4525 {
4526 /* A list of sub-properties. */
4527 while (CONSP (prop))
4528 {
4529 if (single_display_spec_string_p (XCAR (prop), string))
4530 return 1;
4531 prop = XCDR (prop);
4532 }
4533 }
4534 else if (VECTORP (prop))
4535 {
4536 /* A vector of sub-properties. */
4537 int i;
4538 for (i = 0; i < ASIZE (prop); ++i)
4539 if (single_display_spec_string_p (AREF (prop, i), string))
4540 return 1;
4541 }
4542 else
4543 return single_display_spec_string_p (prop, string);
4544
4545 return 0;
4546 }
4547
4548
4549 /* Determine which buffer position in W's buffer STRING comes from.
4550 AROUND_CHARPOS is an approximate position where it could come from.
4551 Value is the buffer position or 0 if it couldn't be determined.
4552
4553 W's buffer must be current.
4554
4555 This function is necessary because we don't record buffer positions
4556 in glyphs generated from strings (to keep struct glyph small).
4557 This function may only use code that doesn't eval because it is
4558 called asynchronously from note_mouse_highlight. */
4559
4560 int
4561 string_buffer_position (w, string, around_charpos)
4562 struct window *w;
4563 Lisp_Object string;
4564 int around_charpos;
4565 {
4566 Lisp_Object limit, prop, pos;
4567 const int MAX_DISTANCE = 1000;
4568 int found = 0;
4569
4570 pos = make_number (around_charpos);
4571 limit = make_number (min (XINT (pos) + MAX_DISTANCE, ZV));
4572 while (!found && !EQ (pos, limit))
4573 {
4574 prop = Fget_char_property (pos, Qdisplay, Qnil);
4575 if (!NILP (prop) && display_prop_string_p (prop, string))
4576 found = 1;
4577 else
4578 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil, limit);
4579 }
4580
4581 if (!found)
4582 {
4583 pos = make_number (around_charpos);
4584 limit = make_number (max (XINT (pos) - MAX_DISTANCE, BEGV));
4585 while (!found && !EQ (pos, limit))
4586 {
4587 prop = Fget_char_property (pos, Qdisplay, Qnil);
4588 if (!NILP (prop) && display_prop_string_p (prop, string))
4589 found = 1;
4590 else
4591 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
4592 limit);
4593 }
4594 }
4595
4596 return found ? XINT (pos) : 0;
4597 }
4598
4599
4600 \f
4601 /***********************************************************************
4602 `composition' property
4603 ***********************************************************************/
4604
4605 /* Set up iterator IT from `composition' property at its current
4606 position. Called from handle_stop. */
4607
4608 static enum prop_handled
4609 handle_composition_prop (it)
4610 struct it *it;
4611 {
4612 Lisp_Object prop, string;
4613 EMACS_INT pos, pos_byte, start, end;
4614
4615 if (STRINGP (it->string))
4616 {
4617 unsigned char *s;
4618
4619 pos = IT_STRING_CHARPOS (*it);
4620 pos_byte = IT_STRING_BYTEPOS (*it);
4621 string = it->string;
4622 s = SDATA (string) + pos_byte;
4623 it->c = STRING_CHAR (s);
4624 }
4625 else
4626 {
4627 pos = IT_CHARPOS (*it);
4628 pos_byte = IT_BYTEPOS (*it);
4629 string = Qnil;
4630 it->c = FETCH_CHAR (pos_byte);
4631 }
4632
4633 /* If there's a valid composition and point is not inside of the
4634 composition (in the case that the composition is from the current
4635 buffer), draw a glyph composed from the composition components. */
4636 if (find_composition (pos, -1, &start, &end, &prop, string)
4637 && COMPOSITION_VALID_P (start, end, prop)
4638 && (STRINGP (it->string) || (PT <= start || PT >= end)))
4639 {
4640 if (start != pos)
4641 {
4642 if (STRINGP (it->string))
4643 pos_byte = string_char_to_byte (it->string, start);
4644 else
4645 pos_byte = CHAR_TO_BYTE (start);
4646 }
4647 it->cmp_it.id = get_composition_id (start, pos_byte, end - start,
4648 prop, string);
4649
4650 if (it->cmp_it.id >= 0)
4651 {
4652 it->cmp_it.ch = -1;
4653 it->cmp_it.nchars = COMPOSITION_LENGTH (prop);
4654 it->cmp_it.nglyphs = -1;
4655 }
4656 }
4657
4658 return HANDLED_NORMALLY;
4659 }
4660
4661
4662 \f
4663 /***********************************************************************
4664 Overlay strings
4665 ***********************************************************************/
4666
4667 /* The following structure is used to record overlay strings for
4668 later sorting in load_overlay_strings. */
4669
4670 struct overlay_entry
4671 {
4672 Lisp_Object overlay;
4673 Lisp_Object string;
4674 int priority;
4675 int after_string_p;
4676 };
4677
4678
4679 /* Set up iterator IT from overlay strings at its current position.
4680 Called from handle_stop. */
4681
4682 static enum prop_handled
4683 handle_overlay_change (it)
4684 struct it *it;
4685 {
4686 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
4687 return HANDLED_RECOMPUTE_PROPS;
4688 else
4689 return HANDLED_NORMALLY;
4690 }
4691
4692
4693 /* Set up the next overlay string for delivery by IT, if there is an
4694 overlay string to deliver. Called by set_iterator_to_next when the
4695 end of the current overlay string is reached. If there are more
4696 overlay strings to display, IT->string and
4697 IT->current.overlay_string_index are set appropriately here.
4698 Otherwise IT->string is set to nil. */
4699
4700 static void
4701 next_overlay_string (it)
4702 struct it *it;
4703 {
4704 ++it->current.overlay_string_index;
4705 if (it->current.overlay_string_index == it->n_overlay_strings)
4706 {
4707 /* No more overlay strings. Restore IT's settings to what
4708 they were before overlay strings were processed, and
4709 continue to deliver from current_buffer. */
4710
4711 it->ellipsis_p = (it->stack[it->sp - 1].display_ellipsis_p != 0);
4712 pop_it (it);
4713 xassert (it->sp > 0
4714 || (NILP (it->string)
4715 && it->method == GET_FROM_BUFFER
4716 && it->stop_charpos >= BEGV
4717 && it->stop_charpos <= it->end_charpos));
4718 it->current.overlay_string_index = -1;
4719 it->n_overlay_strings = 0;
4720
4721 /* If we're at the end of the buffer, record that we have
4722 processed the overlay strings there already, so that
4723 next_element_from_buffer doesn't try it again. */
4724 if (NILP (it->string) && IT_CHARPOS (*it) >= it->end_charpos)
4725 it->overlay_strings_at_end_processed_p = 1;
4726 }
4727 else
4728 {
4729 /* There are more overlay strings to process. If
4730 IT->current.overlay_string_index has advanced to a position
4731 where we must load IT->overlay_strings with more strings, do
4732 it. */
4733 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
4734
4735 if (it->current.overlay_string_index && i == 0)
4736 load_overlay_strings (it, 0);
4737
4738 /* Initialize IT to deliver display elements from the overlay
4739 string. */
4740 it->string = it->overlay_strings[i];
4741 it->multibyte_p = STRING_MULTIBYTE (it->string);
4742 SET_TEXT_POS (it->current.string_pos, 0, 0);
4743 it->method = GET_FROM_STRING;
4744 it->stop_charpos = 0;
4745 if (it->cmp_it.stop_pos >= 0)
4746 it->cmp_it.stop_pos = 0;
4747 }
4748
4749 CHECK_IT (it);
4750 }
4751
4752
4753 /* Compare two overlay_entry structures E1 and E2. Used as a
4754 comparison function for qsort in load_overlay_strings. Overlay
4755 strings for the same position are sorted so that
4756
4757 1. All after-strings come in front of before-strings, except
4758 when they come from the same overlay.
4759
4760 2. Within after-strings, strings are sorted so that overlay strings
4761 from overlays with higher priorities come first.
4762
4763 2. Within before-strings, strings are sorted so that overlay
4764 strings from overlays with higher priorities come last.
4765
4766 Value is analogous to strcmp. */
4767
4768
4769 static int
4770 compare_overlay_entries (e1, e2)
4771 void *e1, *e2;
4772 {
4773 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
4774 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
4775 int result;
4776
4777 if (entry1->after_string_p != entry2->after_string_p)
4778 {
4779 /* Let after-strings appear in front of before-strings if
4780 they come from different overlays. */
4781 if (EQ (entry1->overlay, entry2->overlay))
4782 result = entry1->after_string_p ? 1 : -1;
4783 else
4784 result = entry1->after_string_p ? -1 : 1;
4785 }
4786 else if (entry1->after_string_p)
4787 /* After-strings sorted in order of decreasing priority. */
4788 result = entry2->priority - entry1->priority;
4789 else
4790 /* Before-strings sorted in order of increasing priority. */
4791 result = entry1->priority - entry2->priority;
4792
4793 return result;
4794 }
4795
4796
4797 /* Load the vector IT->overlay_strings with overlay strings from IT's
4798 current buffer position, or from CHARPOS if that is > 0. Set
4799 IT->n_overlays to the total number of overlay strings found.
4800
4801 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
4802 a time. On entry into load_overlay_strings,
4803 IT->current.overlay_string_index gives the number of overlay
4804 strings that have already been loaded by previous calls to this
4805 function.
4806
4807 IT->add_overlay_start contains an additional overlay start
4808 position to consider for taking overlay strings from, if non-zero.
4809 This position comes into play when the overlay has an `invisible'
4810 property, and both before and after-strings. When we've skipped to
4811 the end of the overlay, because of its `invisible' property, we
4812 nevertheless want its before-string to appear.
4813 IT->add_overlay_start will contain the overlay start position
4814 in this case.
4815
4816 Overlay strings are sorted so that after-string strings come in
4817 front of before-string strings. Within before and after-strings,
4818 strings are sorted by overlay priority. See also function
4819 compare_overlay_entries. */
4820
4821 static void
4822 load_overlay_strings (it, charpos)
4823 struct it *it;
4824 int charpos;
4825 {
4826 extern Lisp_Object Qwindow, Qpriority;
4827 Lisp_Object overlay, window, str, invisible;
4828 struct Lisp_Overlay *ov;
4829 int start, end;
4830 int size = 20;
4831 int n = 0, i, j, invis_p;
4832 struct overlay_entry *entries
4833 = (struct overlay_entry *) alloca (size * sizeof *entries);
4834
4835 if (charpos <= 0)
4836 charpos = IT_CHARPOS (*it);
4837
4838 /* Append the overlay string STRING of overlay OVERLAY to vector
4839 `entries' which has size `size' and currently contains `n'
4840 elements. AFTER_P non-zero means STRING is an after-string of
4841 OVERLAY. */
4842 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
4843 do \
4844 { \
4845 Lisp_Object priority; \
4846 \
4847 if (n == size) \
4848 { \
4849 int new_size = 2 * size; \
4850 struct overlay_entry *old = entries; \
4851 entries = \
4852 (struct overlay_entry *) alloca (new_size \
4853 * sizeof *entries); \
4854 bcopy (old, entries, size * sizeof *entries); \
4855 size = new_size; \
4856 } \
4857 \
4858 entries[n].string = (STRING); \
4859 entries[n].overlay = (OVERLAY); \
4860 priority = Foverlay_get ((OVERLAY), Qpriority); \
4861 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
4862 entries[n].after_string_p = (AFTER_P); \
4863 ++n; \
4864 } \
4865 while (0)
4866
4867 /* Process overlay before the overlay center. */
4868 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
4869 {
4870 XSETMISC (overlay, ov);
4871 xassert (OVERLAYP (overlay));
4872 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4873 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4874
4875 if (end < charpos)
4876 break;
4877
4878 /* Skip this overlay if it doesn't start or end at IT's current
4879 position. */
4880 if (end != charpos && start != charpos)
4881 continue;
4882
4883 /* Skip this overlay if it doesn't apply to IT->w. */
4884 window = Foverlay_get (overlay, Qwindow);
4885 if (WINDOWP (window) && XWINDOW (window) != it->w)
4886 continue;
4887
4888 /* If the text ``under'' the overlay is invisible, both before-
4889 and after-strings from this overlay are visible; start and
4890 end position are indistinguishable. */
4891 invisible = Foverlay_get (overlay, Qinvisible);
4892 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4893
4894 /* If overlay has a non-empty before-string, record it. */
4895 if ((start == charpos || (end == charpos && invis_p))
4896 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
4897 && SCHARS (str))
4898 RECORD_OVERLAY_STRING (overlay, str, 0);
4899
4900 /* If overlay has a non-empty after-string, record it. */
4901 if ((end == charpos || (start == charpos && invis_p))
4902 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
4903 && SCHARS (str))
4904 RECORD_OVERLAY_STRING (overlay, str, 1);
4905 }
4906
4907 /* Process overlays after the overlay center. */
4908 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
4909 {
4910 XSETMISC (overlay, ov);
4911 xassert (OVERLAYP (overlay));
4912 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4913 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4914
4915 if (start > charpos)
4916 break;
4917
4918 /* Skip this overlay if it doesn't start or end at IT's current
4919 position. */
4920 if (end != charpos && start != charpos)
4921 continue;
4922
4923 /* Skip this overlay if it doesn't apply to IT->w. */
4924 window = Foverlay_get (overlay, Qwindow);
4925 if (WINDOWP (window) && XWINDOW (window) != it->w)
4926 continue;
4927
4928 /* If the text ``under'' the overlay is invisible, it has a zero
4929 dimension, and both before- and after-strings apply. */
4930 invisible = Foverlay_get (overlay, Qinvisible);
4931 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4932
4933 /* If overlay has a non-empty before-string, record it. */
4934 if ((start == charpos || (end == charpos && invis_p))
4935 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
4936 && SCHARS (str))
4937 RECORD_OVERLAY_STRING (overlay, str, 0);
4938
4939 /* If overlay has a non-empty after-string, record it. */
4940 if ((end == charpos || (start == charpos && invis_p))
4941 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
4942 && SCHARS (str))
4943 RECORD_OVERLAY_STRING (overlay, str, 1);
4944 }
4945
4946 #undef RECORD_OVERLAY_STRING
4947
4948 /* Sort entries. */
4949 if (n > 1)
4950 qsort (entries, n, sizeof *entries, compare_overlay_entries);
4951
4952 /* Record the total number of strings to process. */
4953 it->n_overlay_strings = n;
4954
4955 /* IT->current.overlay_string_index is the number of overlay strings
4956 that have already been consumed by IT. Copy some of the
4957 remaining overlay strings to IT->overlay_strings. */
4958 i = 0;
4959 j = it->current.overlay_string_index;
4960 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
4961 {
4962 it->overlay_strings[i] = entries[j].string;
4963 it->string_overlays[i++] = entries[j++].overlay;
4964 }
4965
4966 CHECK_IT (it);
4967 }
4968
4969
4970 /* Get the first chunk of overlay strings at IT's current buffer
4971 position, or at CHARPOS if that is > 0. Value is non-zero if at
4972 least one overlay string was found. */
4973
4974 static int
4975 get_overlay_strings_1 (it, charpos, compute_stop_p)
4976 struct it *it;
4977 int charpos;
4978 int compute_stop_p;
4979 {
4980 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
4981 process. This fills IT->overlay_strings with strings, and sets
4982 IT->n_overlay_strings to the total number of strings to process.
4983 IT->pos.overlay_string_index has to be set temporarily to zero
4984 because load_overlay_strings needs this; it must be set to -1
4985 when no overlay strings are found because a zero value would
4986 indicate a position in the first overlay string. */
4987 it->current.overlay_string_index = 0;
4988 load_overlay_strings (it, charpos);
4989
4990 /* If we found overlay strings, set up IT to deliver display
4991 elements from the first one. Otherwise set up IT to deliver
4992 from current_buffer. */
4993 if (it->n_overlay_strings)
4994 {
4995 /* Make sure we know settings in current_buffer, so that we can
4996 restore meaningful values when we're done with the overlay
4997 strings. */
4998 if (compute_stop_p)
4999 compute_stop_pos (it);
5000 xassert (it->face_id >= 0);
5001
5002 /* Save IT's settings. They are restored after all overlay
5003 strings have been processed. */
5004 xassert (!compute_stop_p || it->sp == 0);
5005
5006 /* When called from handle_stop, there might be an empty display
5007 string loaded. In that case, don't bother saving it. */
5008 if (!STRINGP (it->string) || SCHARS (it->string))
5009 push_it (it);
5010
5011 /* Set up IT to deliver display elements from the first overlay
5012 string. */
5013 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5014 it->string = it->overlay_strings[0];
5015 it->from_overlay = Qnil;
5016 it->stop_charpos = 0;
5017 xassert (STRINGP (it->string));
5018 it->end_charpos = SCHARS (it->string);
5019 it->multibyte_p = STRING_MULTIBYTE (it->string);
5020 it->method = GET_FROM_STRING;
5021 return 1;
5022 }
5023
5024 it->current.overlay_string_index = -1;
5025 return 0;
5026 }
5027
5028 static int
5029 get_overlay_strings (it, charpos)
5030 struct it *it;
5031 int charpos;
5032 {
5033 it->string = Qnil;
5034 it->method = GET_FROM_BUFFER;
5035
5036 (void) get_overlay_strings_1 (it, charpos, 1);
5037
5038 CHECK_IT (it);
5039
5040 /* Value is non-zero if we found at least one overlay string. */
5041 return STRINGP (it->string);
5042 }
5043
5044
5045 \f
5046 /***********************************************************************
5047 Saving and restoring state
5048 ***********************************************************************/
5049
5050 /* Save current settings of IT on IT->stack. Called, for example,
5051 before setting up IT for an overlay string, to be able to restore
5052 IT's settings to what they were after the overlay string has been
5053 processed. */
5054
5055 static void
5056 push_it (it)
5057 struct it *it;
5058 {
5059 struct iterator_stack_entry *p;
5060
5061 xassert (it->sp < IT_STACK_SIZE);
5062 p = it->stack + it->sp;
5063
5064 p->stop_charpos = it->stop_charpos;
5065 p->cmp_it = it->cmp_it;
5066 xassert (it->face_id >= 0);
5067 p->face_id = it->face_id;
5068 p->string = it->string;
5069 p->method = it->method;
5070 p->from_overlay = it->from_overlay;
5071 switch (p->method)
5072 {
5073 case GET_FROM_IMAGE:
5074 p->u.image.object = it->object;
5075 p->u.image.image_id = it->image_id;
5076 p->u.image.slice = it->slice;
5077 break;
5078 case GET_FROM_STRETCH:
5079 p->u.stretch.object = it->object;
5080 break;
5081 }
5082 p->position = it->position;
5083 p->current = it->current;
5084 p->end_charpos = it->end_charpos;
5085 p->string_nchars = it->string_nchars;
5086 p->area = it->area;
5087 p->multibyte_p = it->multibyte_p;
5088 p->avoid_cursor_p = it->avoid_cursor_p;
5089 p->space_width = it->space_width;
5090 p->font_height = it->font_height;
5091 p->voffset = it->voffset;
5092 p->string_from_display_prop_p = it->string_from_display_prop_p;
5093 p->display_ellipsis_p = 0;
5094 p->line_wrap = it->line_wrap;
5095 ++it->sp;
5096 }
5097
5098
5099 /* Restore IT's settings from IT->stack. Called, for example, when no
5100 more overlay strings must be processed, and we return to delivering
5101 display elements from a buffer, or when the end of a string from a
5102 `display' property is reached and we return to delivering display
5103 elements from an overlay string, or from a buffer. */
5104
5105 static void
5106 pop_it (it)
5107 struct it *it;
5108 {
5109 struct iterator_stack_entry *p;
5110
5111 xassert (it->sp > 0);
5112 --it->sp;
5113 p = it->stack + it->sp;
5114 it->stop_charpos = p->stop_charpos;
5115 it->cmp_it = p->cmp_it;
5116 it->face_id = p->face_id;
5117 it->current = p->current;
5118 it->position = p->position;
5119 it->string = p->string;
5120 it->from_overlay = p->from_overlay;
5121 if (NILP (it->string))
5122 SET_TEXT_POS (it->current.string_pos, -1, -1);
5123 it->method = p->method;
5124 switch (it->method)
5125 {
5126 case GET_FROM_IMAGE:
5127 it->image_id = p->u.image.image_id;
5128 it->object = p->u.image.object;
5129 it->slice = p->u.image.slice;
5130 break;
5131 case GET_FROM_STRETCH:
5132 it->object = p->u.comp.object;
5133 break;
5134 case GET_FROM_BUFFER:
5135 it->object = it->w->buffer;
5136 break;
5137 case GET_FROM_STRING:
5138 it->object = it->string;
5139 break;
5140 case GET_FROM_DISPLAY_VECTOR:
5141 if (it->s)
5142 it->method = GET_FROM_C_STRING;
5143 else if (STRINGP (it->string))
5144 it->method = GET_FROM_STRING;
5145 else
5146 {
5147 it->method = GET_FROM_BUFFER;
5148 it->object = it->w->buffer;
5149 }
5150 }
5151 it->end_charpos = p->end_charpos;
5152 it->string_nchars = p->string_nchars;
5153 it->area = p->area;
5154 it->multibyte_p = p->multibyte_p;
5155 it->avoid_cursor_p = p->avoid_cursor_p;
5156 it->space_width = p->space_width;
5157 it->font_height = p->font_height;
5158 it->voffset = p->voffset;
5159 it->string_from_display_prop_p = p->string_from_display_prop_p;
5160 it->line_wrap = p->line_wrap;
5161 }
5162
5163
5164 \f
5165 /***********************************************************************
5166 Moving over lines
5167 ***********************************************************************/
5168
5169 /* Set IT's current position to the previous line start. */
5170
5171 static void
5172 back_to_previous_line_start (it)
5173 struct it *it;
5174 {
5175 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
5176 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
5177 }
5178
5179
5180 /* Move IT to the next line start.
5181
5182 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
5183 we skipped over part of the text (as opposed to moving the iterator
5184 continuously over the text). Otherwise, don't change the value
5185 of *SKIPPED_P.
5186
5187 Newlines may come from buffer text, overlay strings, or strings
5188 displayed via the `display' property. That's the reason we can't
5189 simply use find_next_newline_no_quit.
5190
5191 Note that this function may not skip over invisible text that is so
5192 because of text properties and immediately follows a newline. If
5193 it would, function reseat_at_next_visible_line_start, when called
5194 from set_iterator_to_next, would effectively make invisible
5195 characters following a newline part of the wrong glyph row, which
5196 leads to wrong cursor motion. */
5197
5198 static int
5199 forward_to_next_line_start (it, skipped_p)
5200 struct it *it;
5201 int *skipped_p;
5202 {
5203 int old_selective, newline_found_p, n;
5204 const int MAX_NEWLINE_DISTANCE = 500;
5205
5206 /* If already on a newline, just consume it to avoid unintended
5207 skipping over invisible text below. */
5208 if (it->what == IT_CHARACTER
5209 && it->c == '\n'
5210 && CHARPOS (it->position) == IT_CHARPOS (*it))
5211 {
5212 set_iterator_to_next (it, 0);
5213 it->c = 0;
5214 return 1;
5215 }
5216
5217 /* Don't handle selective display in the following. It's (a)
5218 unnecessary because it's done by the caller, and (b) leads to an
5219 infinite recursion because next_element_from_ellipsis indirectly
5220 calls this function. */
5221 old_selective = it->selective;
5222 it->selective = 0;
5223
5224 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
5225 from buffer text. */
5226 for (n = newline_found_p = 0;
5227 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
5228 n += STRINGP (it->string) ? 0 : 1)
5229 {
5230 if (!get_next_display_element (it))
5231 return 0;
5232 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
5233 set_iterator_to_next (it, 0);
5234 }
5235
5236 /* If we didn't find a newline near enough, see if we can use a
5237 short-cut. */
5238 if (!newline_found_p)
5239 {
5240 int start = IT_CHARPOS (*it);
5241 int limit = find_next_newline_no_quit (start, 1);
5242 Lisp_Object pos;
5243
5244 xassert (!STRINGP (it->string));
5245
5246 /* If there isn't any `display' property in sight, and no
5247 overlays, we can just use the position of the newline in
5248 buffer text. */
5249 if (it->stop_charpos >= limit
5250 || ((pos = Fnext_single_property_change (make_number (start),
5251 Qdisplay,
5252 Qnil, make_number (limit)),
5253 NILP (pos))
5254 && next_overlay_change (start) == ZV))
5255 {
5256 IT_CHARPOS (*it) = limit;
5257 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
5258 *skipped_p = newline_found_p = 1;
5259 }
5260 else
5261 {
5262 while (get_next_display_element (it)
5263 && !newline_found_p)
5264 {
5265 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
5266 set_iterator_to_next (it, 0);
5267 }
5268 }
5269 }
5270
5271 it->selective = old_selective;
5272 return newline_found_p;
5273 }
5274
5275
5276 /* Set IT's current position to the previous visible line start. Skip
5277 invisible text that is so either due to text properties or due to
5278 selective display. Caution: this does not change IT->current_x and
5279 IT->hpos. */
5280
5281 static void
5282 back_to_previous_visible_line_start (it)
5283 struct it *it;
5284 {
5285 while (IT_CHARPOS (*it) > BEGV)
5286 {
5287 back_to_previous_line_start (it);
5288
5289 if (IT_CHARPOS (*it) <= BEGV)
5290 break;
5291
5292 /* If selective > 0, then lines indented more than that values
5293 are invisible. */
5294 if (it->selective > 0
5295 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
5296 (double) it->selective)) /* iftc */
5297 continue;
5298
5299 /* Check the newline before point for invisibility. */
5300 {
5301 Lisp_Object prop;
5302 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
5303 Qinvisible, it->window);
5304 if (TEXT_PROP_MEANS_INVISIBLE (prop))
5305 continue;
5306 }
5307
5308 if (IT_CHARPOS (*it) <= BEGV)
5309 break;
5310
5311 {
5312 struct it it2;
5313 int pos;
5314 EMACS_INT beg, end;
5315 Lisp_Object val, overlay;
5316
5317 /* If newline is part of a composition, continue from start of composition */
5318 if (find_composition (IT_CHARPOS (*it), -1, &beg, &end, &val, Qnil)
5319 && beg < IT_CHARPOS (*it))
5320 goto replaced;
5321
5322 /* If newline is replaced by a display property, find start of overlay
5323 or interval and continue search from that point. */
5324 it2 = *it;
5325 pos = --IT_CHARPOS (it2);
5326 --IT_BYTEPOS (it2);
5327 it2.sp = 0;
5328 it2.string_from_display_prop_p = 0;
5329 if (handle_display_prop (&it2) == HANDLED_RETURN
5330 && !NILP (val = get_char_property_and_overlay
5331 (make_number (pos), Qdisplay, Qnil, &overlay))
5332 && (OVERLAYP (overlay)
5333 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
5334 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
5335 goto replaced;
5336
5337 /* Newline is not replaced by anything -- so we are done. */
5338 break;
5339
5340 replaced:
5341 if (beg < BEGV)
5342 beg = BEGV;
5343 IT_CHARPOS (*it) = beg;
5344 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
5345 }
5346 }
5347
5348 it->continuation_lines_width = 0;
5349
5350 xassert (IT_CHARPOS (*it) >= BEGV);
5351 xassert (IT_CHARPOS (*it) == BEGV
5352 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
5353 CHECK_IT (it);
5354 }
5355
5356
5357 /* Reseat iterator IT at the previous visible line start. Skip
5358 invisible text that is so either due to text properties or due to
5359 selective display. At the end, update IT's overlay information,
5360 face information etc. */
5361
5362 void
5363 reseat_at_previous_visible_line_start (it)
5364 struct it *it;
5365 {
5366 back_to_previous_visible_line_start (it);
5367 reseat (it, it->current.pos, 1);
5368 CHECK_IT (it);
5369 }
5370
5371
5372 /* Reseat iterator IT on the next visible line start in the current
5373 buffer. ON_NEWLINE_P non-zero means position IT on the newline
5374 preceding the line start. Skip over invisible text that is so
5375 because of selective display. Compute faces, overlays etc at the
5376 new position. Note that this function does not skip over text that
5377 is invisible because of text properties. */
5378
5379 static void
5380 reseat_at_next_visible_line_start (it, on_newline_p)
5381 struct it *it;
5382 int on_newline_p;
5383 {
5384 int newline_found_p, skipped_p = 0;
5385
5386 newline_found_p = forward_to_next_line_start (it, &skipped_p);
5387
5388 /* Skip over lines that are invisible because they are indented
5389 more than the value of IT->selective. */
5390 if (it->selective > 0)
5391 while (IT_CHARPOS (*it) < ZV
5392 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
5393 (double) it->selective)) /* iftc */
5394 {
5395 xassert (IT_BYTEPOS (*it) == BEGV
5396 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
5397 newline_found_p = forward_to_next_line_start (it, &skipped_p);
5398 }
5399
5400 /* Position on the newline if that's what's requested. */
5401 if (on_newline_p && newline_found_p)
5402 {
5403 if (STRINGP (it->string))
5404 {
5405 if (IT_STRING_CHARPOS (*it) > 0)
5406 {
5407 --IT_STRING_CHARPOS (*it);
5408 --IT_STRING_BYTEPOS (*it);
5409 }
5410 }
5411 else if (IT_CHARPOS (*it) > BEGV)
5412 {
5413 --IT_CHARPOS (*it);
5414 --IT_BYTEPOS (*it);
5415 reseat (it, it->current.pos, 0);
5416 }
5417 }
5418 else if (skipped_p)
5419 reseat (it, it->current.pos, 0);
5420
5421 CHECK_IT (it);
5422 }
5423
5424
5425 \f
5426 /***********************************************************************
5427 Changing an iterator's position
5428 ***********************************************************************/
5429
5430 /* Change IT's current position to POS in current_buffer. If FORCE_P
5431 is non-zero, always check for text properties at the new position.
5432 Otherwise, text properties are only looked up if POS >=
5433 IT->check_charpos of a property. */
5434
5435 static void
5436 reseat (it, pos, force_p)
5437 struct it *it;
5438 struct text_pos pos;
5439 int force_p;
5440 {
5441 int original_pos = IT_CHARPOS (*it);
5442
5443 reseat_1 (it, pos, 0);
5444
5445 /* Determine where to check text properties. Avoid doing it
5446 where possible because text property lookup is very expensive. */
5447 if (force_p
5448 || CHARPOS (pos) > it->stop_charpos
5449 || CHARPOS (pos) < original_pos)
5450 handle_stop (it);
5451
5452 CHECK_IT (it);
5453 }
5454
5455
5456 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
5457 IT->stop_pos to POS, also. */
5458
5459 static void
5460 reseat_1 (it, pos, set_stop_p)
5461 struct it *it;
5462 struct text_pos pos;
5463 int set_stop_p;
5464 {
5465 /* Don't call this function when scanning a C string. */
5466 xassert (it->s == NULL);
5467
5468 /* POS must be a reasonable value. */
5469 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
5470
5471 it->current.pos = it->position = pos;
5472 it->end_charpos = ZV;
5473 it->dpvec = NULL;
5474 it->current.dpvec_index = -1;
5475 it->current.overlay_string_index = -1;
5476 IT_STRING_CHARPOS (*it) = -1;
5477 IT_STRING_BYTEPOS (*it) = -1;
5478 it->string = Qnil;
5479 it->string_from_display_prop_p = 0;
5480 it->method = GET_FROM_BUFFER;
5481 it->object = it->w->buffer;
5482 it->area = TEXT_AREA;
5483 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
5484 it->sp = 0;
5485 it->string_from_display_prop_p = 0;
5486 it->face_before_selective_p = 0;
5487
5488 if (set_stop_p)
5489 it->stop_charpos = CHARPOS (pos);
5490 }
5491
5492
5493 /* Set up IT for displaying a string, starting at CHARPOS in window W.
5494 If S is non-null, it is a C string to iterate over. Otherwise,
5495 STRING gives a Lisp string to iterate over.
5496
5497 If PRECISION > 0, don't return more then PRECISION number of
5498 characters from the string.
5499
5500 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
5501 characters have been returned. FIELD_WIDTH < 0 means an infinite
5502 field width.
5503
5504 MULTIBYTE = 0 means disable processing of multibyte characters,
5505 MULTIBYTE > 0 means enable it,
5506 MULTIBYTE < 0 means use IT->multibyte_p.
5507
5508 IT must be initialized via a prior call to init_iterator before
5509 calling this function. */
5510
5511 static void
5512 reseat_to_string (it, s, string, charpos, precision, field_width, multibyte)
5513 struct it *it;
5514 unsigned char *s;
5515 Lisp_Object string;
5516 int charpos;
5517 int precision, field_width, multibyte;
5518 {
5519 /* No region in strings. */
5520 it->region_beg_charpos = it->region_end_charpos = -1;
5521
5522 /* No text property checks performed by default, but see below. */
5523 it->stop_charpos = -1;
5524
5525 /* Set iterator position and end position. */
5526 bzero (&it->current, sizeof it->current);
5527 it->current.overlay_string_index = -1;
5528 it->current.dpvec_index = -1;
5529 xassert (charpos >= 0);
5530
5531 /* If STRING is specified, use its multibyteness, otherwise use the
5532 setting of MULTIBYTE, if specified. */
5533 if (multibyte >= 0)
5534 it->multibyte_p = multibyte > 0;
5535
5536 if (s == NULL)
5537 {
5538 xassert (STRINGP (string));
5539 it->string = string;
5540 it->s = NULL;
5541 it->end_charpos = it->string_nchars = SCHARS (string);
5542 it->method = GET_FROM_STRING;
5543 it->current.string_pos = string_pos (charpos, string);
5544 }
5545 else
5546 {
5547 it->s = s;
5548 it->string = Qnil;
5549
5550 /* Note that we use IT->current.pos, not it->current.string_pos,
5551 for displaying C strings. */
5552 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
5553 if (it->multibyte_p)
5554 {
5555 it->current.pos = c_string_pos (charpos, s, 1);
5556 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
5557 }
5558 else
5559 {
5560 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
5561 it->end_charpos = it->string_nchars = strlen (s);
5562 }
5563
5564 it->method = GET_FROM_C_STRING;
5565 }
5566
5567 /* PRECISION > 0 means don't return more than PRECISION characters
5568 from the string. */
5569 if (precision > 0 && it->end_charpos - charpos > precision)
5570 it->end_charpos = it->string_nchars = charpos + precision;
5571
5572 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
5573 characters have been returned. FIELD_WIDTH == 0 means don't pad,
5574 FIELD_WIDTH < 0 means infinite field width. This is useful for
5575 padding with `-' at the end of a mode line. */
5576 if (field_width < 0)
5577 field_width = INFINITY;
5578 if (field_width > it->end_charpos - charpos)
5579 it->end_charpos = charpos + field_width;
5580
5581 /* Use the standard display table for displaying strings. */
5582 if (DISP_TABLE_P (Vstandard_display_table))
5583 it->dp = XCHAR_TABLE (Vstandard_display_table);
5584
5585 it->stop_charpos = charpos;
5586 if (s == NULL && it->multibyte_p)
5587 {
5588 EMACS_INT endpos = SCHARS (it->string);
5589 if (endpos > it->end_charpos)
5590 endpos = it->end_charpos;
5591 composition_compute_stop_pos (&it->cmp_it, charpos, -1, endpos,
5592 it->string);
5593 }
5594 CHECK_IT (it);
5595 }
5596
5597
5598 \f
5599 /***********************************************************************
5600 Iteration
5601 ***********************************************************************/
5602
5603 /* Map enum it_method value to corresponding next_element_from_* function. */
5604
5605 static int (* get_next_element[NUM_IT_METHODS]) P_ ((struct it *it)) =
5606 {
5607 next_element_from_buffer,
5608 next_element_from_display_vector,
5609 next_element_from_string,
5610 next_element_from_c_string,
5611 next_element_from_image,
5612 next_element_from_stretch
5613 };
5614
5615 #define GET_NEXT_DISPLAY_ELEMENT(it) (*get_next_element[(it)->method]) (it)
5616
5617
5618 /* Return 1 iff a character at CHARPOS (and BYTEPOS) is composed
5619 (possibly with the following characters). */
5620
5621 #define CHAR_COMPOSED_P(IT,CHARPOS,BYTEPOS,END_CHARPOS) \
5622 ((IT)->cmp_it.id >= 0 \
5623 || ((IT)->cmp_it.stop_pos == (CHARPOS) \
5624 && composition_reseat_it (&(IT)->cmp_it, CHARPOS, BYTEPOS, \
5625 END_CHARPOS, (IT)->w, \
5626 FACE_FROM_ID ((IT)->f, (IT)->face_id), \
5627 (IT)->string)))
5628
5629
5630 /* Load IT's display element fields with information about the next
5631 display element from the current position of IT. Value is zero if
5632 end of buffer (or C string) is reached. */
5633
5634 static struct frame *last_escape_glyph_frame = NULL;
5635 static unsigned last_escape_glyph_face_id = (1 << FACE_ID_BITS);
5636 static int last_escape_glyph_merged_face_id = 0;
5637
5638 int
5639 get_next_display_element (it)
5640 struct it *it;
5641 {
5642 /* Non-zero means that we found a display element. Zero means that
5643 we hit the end of what we iterate over. Performance note: the
5644 function pointer `method' used here turns out to be faster than
5645 using a sequence of if-statements. */
5646 int success_p;
5647
5648 get_next:
5649 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
5650
5651 if (it->what == IT_CHARACTER)
5652 {
5653 /* Map via display table or translate control characters.
5654 IT->c, IT->len etc. have been set to the next character by
5655 the function call above. If we have a display table, and it
5656 contains an entry for IT->c, translate it. Don't do this if
5657 IT->c itself comes from a display table, otherwise we could
5658 end up in an infinite recursion. (An alternative could be to
5659 count the recursion depth of this function and signal an
5660 error when a certain maximum depth is reached.) Is it worth
5661 it? */
5662 if (success_p && it->dpvec == NULL)
5663 {
5664 Lisp_Object dv;
5665 struct charset *unibyte = CHARSET_FROM_ID (charset_unibyte);
5666 enum { char_is_other = 0, char_is_nbsp, char_is_soft_hyphen }
5667 nbsp_or_shy = char_is_other;
5668 int decoded = it->c;
5669
5670 if (it->dp
5671 && (dv = DISP_CHAR_VECTOR (it->dp, it->c),
5672 VECTORP (dv)))
5673 {
5674 struct Lisp_Vector *v = XVECTOR (dv);
5675
5676 /* Return the first character from the display table
5677 entry, if not empty. If empty, don't display the
5678 current character. */
5679 if (v->size)
5680 {
5681 it->dpvec_char_len = it->len;
5682 it->dpvec = v->contents;
5683 it->dpend = v->contents + v->size;
5684 it->current.dpvec_index = 0;
5685 it->dpvec_face_id = -1;
5686 it->saved_face_id = it->face_id;
5687 it->method = GET_FROM_DISPLAY_VECTOR;
5688 it->ellipsis_p = 0;
5689 }
5690 else
5691 {
5692 set_iterator_to_next (it, 0);
5693 }
5694 goto get_next;
5695 }
5696
5697 if (unibyte_display_via_language_environment
5698 && !ASCII_CHAR_P (it->c))
5699 decoded = DECODE_CHAR (unibyte, it->c);
5700
5701 if (it->c >= 0x80 && ! NILP (Vnobreak_char_display))
5702 {
5703 if (it->multibyte_p)
5704 nbsp_or_shy = (it->c == 0xA0 ? char_is_nbsp
5705 : it->c == 0xAD ? char_is_soft_hyphen
5706 : char_is_other);
5707 else if (unibyte_display_via_language_environment)
5708 nbsp_or_shy = (decoded == 0xA0 ? char_is_nbsp
5709 : decoded == 0xAD ? char_is_soft_hyphen
5710 : char_is_other);
5711 }
5712
5713 /* Translate control characters into `\003' or `^C' form.
5714 Control characters coming from a display table entry are
5715 currently not translated because we use IT->dpvec to hold
5716 the translation. This could easily be changed but I
5717 don't believe that it is worth doing.
5718
5719 If it->multibyte_p is nonzero, non-printable non-ASCII
5720 characters are also translated to octal form.
5721
5722 If it->multibyte_p is zero, eight-bit characters that
5723 don't have corresponding multibyte char code are also
5724 translated to octal form. */
5725 if ((it->c < ' '
5726 ? (it->area != TEXT_AREA
5727 /* In mode line, treat \n, \t like other crl chars. */
5728 || (it->c != '\t'
5729 && it->glyph_row
5730 && (it->glyph_row->mode_line_p || it->avoid_cursor_p))
5731 || (it->c != '\n' && it->c != '\t'))
5732 : (nbsp_or_shy
5733 || (it->multibyte_p
5734 ? ! CHAR_PRINTABLE_P (it->c)
5735 : (! unibyte_display_via_language_environment
5736 ? it->c >= 0x80
5737 : (decoded >= 0x80 && decoded < 0xA0))))))
5738 {
5739 /* IT->c is a control character which must be displayed
5740 either as '\003' or as `^C' where the '\\' and '^'
5741 can be defined in the display table. Fill
5742 IT->ctl_chars with glyphs for what we have to
5743 display. Then, set IT->dpvec to these glyphs. */
5744 Lisp_Object gc;
5745 int ctl_len;
5746 int face_id, lface_id = 0 ;
5747 int escape_glyph;
5748
5749 /* Handle control characters with ^. */
5750
5751 if (it->c < 128 && it->ctl_arrow_p)
5752 {
5753 int g;
5754
5755 g = '^'; /* default glyph for Control */
5756 /* Set IT->ctl_chars[0] to the glyph for `^'. */
5757 if (it->dp
5758 && (gc = DISP_CTRL_GLYPH (it->dp), GLYPH_CODE_P (gc))
5759 && GLYPH_CODE_CHAR_VALID_P (gc))
5760 {
5761 g = GLYPH_CODE_CHAR (gc);
5762 lface_id = GLYPH_CODE_FACE (gc);
5763 }
5764 if (lface_id)
5765 {
5766 face_id = merge_faces (it->f, Qt, lface_id, it->face_id);
5767 }
5768 else if (it->f == last_escape_glyph_frame
5769 && it->face_id == last_escape_glyph_face_id)
5770 {
5771 face_id = last_escape_glyph_merged_face_id;
5772 }
5773 else
5774 {
5775 /* Merge the escape-glyph face into the current face. */
5776 face_id = merge_faces (it->f, Qescape_glyph, 0,
5777 it->face_id);
5778 last_escape_glyph_frame = it->f;
5779 last_escape_glyph_face_id = it->face_id;
5780 last_escape_glyph_merged_face_id = face_id;
5781 }
5782
5783 XSETINT (it->ctl_chars[0], g);
5784 XSETINT (it->ctl_chars[1], it->c ^ 0100);
5785 ctl_len = 2;
5786 goto display_control;
5787 }
5788
5789 /* Handle non-break space in the mode where it only gets
5790 highlighting. */
5791
5792 if (EQ (Vnobreak_char_display, Qt)
5793 && nbsp_or_shy == char_is_nbsp)
5794 {
5795 /* Merge the no-break-space face into the current face. */
5796 face_id = merge_faces (it->f, Qnobreak_space, 0,
5797 it->face_id);
5798
5799 it->c = ' ';
5800 XSETINT (it->ctl_chars[0], ' ');
5801 ctl_len = 1;
5802 goto display_control;
5803 }
5804
5805 /* Handle sequences that start with the "escape glyph". */
5806
5807 /* the default escape glyph is \. */
5808 escape_glyph = '\\';
5809
5810 if (it->dp
5811 && (gc = DISP_ESCAPE_GLYPH (it->dp), GLYPH_CODE_P (gc))
5812 && GLYPH_CODE_CHAR_VALID_P (gc))
5813 {
5814 escape_glyph = GLYPH_CODE_CHAR (gc);
5815 lface_id = GLYPH_CODE_FACE (gc);
5816 }
5817 if (lface_id)
5818 {
5819 /* The display table specified a face.
5820 Merge it into face_id and also into escape_glyph. */
5821 face_id = merge_faces (it->f, Qt, lface_id,
5822 it->face_id);
5823 }
5824 else if (it->f == last_escape_glyph_frame
5825 && it->face_id == last_escape_glyph_face_id)
5826 {
5827 face_id = last_escape_glyph_merged_face_id;
5828 }
5829 else
5830 {
5831 /* Merge the escape-glyph face into the current face. */
5832 face_id = merge_faces (it->f, Qescape_glyph, 0,
5833 it->face_id);
5834 last_escape_glyph_frame = it->f;
5835 last_escape_glyph_face_id = it->face_id;
5836 last_escape_glyph_merged_face_id = face_id;
5837 }
5838
5839 /* Handle soft hyphens in the mode where they only get
5840 highlighting. */
5841
5842 if (EQ (Vnobreak_char_display, Qt)
5843 && nbsp_or_shy == char_is_soft_hyphen)
5844 {
5845 it->c = '-';
5846 XSETINT (it->ctl_chars[0], '-');
5847 ctl_len = 1;
5848 goto display_control;
5849 }
5850
5851 /* Handle non-break space and soft hyphen
5852 with the escape glyph. */
5853
5854 if (nbsp_or_shy)
5855 {
5856 XSETINT (it->ctl_chars[0], escape_glyph);
5857 it->c = (nbsp_or_shy == char_is_nbsp ? ' ' : '-');
5858 XSETINT (it->ctl_chars[1], it->c);
5859 ctl_len = 2;
5860 goto display_control;
5861 }
5862
5863 {
5864 unsigned char str[MAX_MULTIBYTE_LENGTH];
5865 int len;
5866 int i;
5867
5868 /* Set IT->ctl_chars[0] to the glyph for `\\'. */
5869 if (CHAR_BYTE8_P (it->c))
5870 {
5871 str[0] = CHAR_TO_BYTE8 (it->c);
5872 len = 1;
5873 }
5874 else if (it->c < 256)
5875 {
5876 str[0] = it->c;
5877 len = 1;
5878 }
5879 else
5880 {
5881 /* It's an invalid character, which shouldn't
5882 happen actually, but due to bugs it may
5883 happen. Let's print the char as is, there's
5884 not much meaningful we can do with it. */
5885 str[0] = it->c;
5886 str[1] = it->c >> 8;
5887 str[2] = it->c >> 16;
5888 str[3] = it->c >> 24;
5889 len = 4;
5890 }
5891
5892 for (i = 0; i < len; i++)
5893 {
5894 int g;
5895 XSETINT (it->ctl_chars[i * 4], escape_glyph);
5896 /* Insert three more glyphs into IT->ctl_chars for
5897 the octal display of the character. */
5898 g = ((str[i] >> 6) & 7) + '0';
5899 XSETINT (it->ctl_chars[i * 4 + 1], g);
5900 g = ((str[i] >> 3) & 7) + '0';
5901 XSETINT (it->ctl_chars[i * 4 + 2], g);
5902 g = (str[i] & 7) + '0';
5903 XSETINT (it->ctl_chars[i * 4 + 3], g);
5904 }
5905 ctl_len = len * 4;
5906 }
5907
5908 display_control:
5909 /* Set up IT->dpvec and return first character from it. */
5910 it->dpvec_char_len = it->len;
5911 it->dpvec = it->ctl_chars;
5912 it->dpend = it->dpvec + ctl_len;
5913 it->current.dpvec_index = 0;
5914 it->dpvec_face_id = face_id;
5915 it->saved_face_id = it->face_id;
5916 it->method = GET_FROM_DISPLAY_VECTOR;
5917 it->ellipsis_p = 0;
5918 goto get_next;
5919 }
5920 }
5921 }
5922
5923 #ifdef HAVE_WINDOW_SYSTEM
5924 /* Adjust face id for a multibyte character. There are no multibyte
5925 character in unibyte text. */
5926 if ((it->what == IT_CHARACTER || it->what == IT_COMPOSITION)
5927 && it->multibyte_p
5928 && success_p
5929 && FRAME_WINDOW_P (it->f))
5930 {
5931 struct face *face = FACE_FROM_ID (it->f, it->face_id);
5932
5933 if (it->what == IT_COMPOSITION && it->cmp_it.ch >= 0)
5934 {
5935 /* Automatic composition with glyph-string. */
5936 Lisp_Object gstring = composition_gstring_from_id (it->cmp_it.id);
5937
5938 it->face_id = face_for_font (it->f, LGSTRING_FONT (gstring), face);
5939 }
5940 else
5941 {
5942 int pos = (it->s ? -1
5943 : STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
5944 : IT_CHARPOS (*it));
5945
5946 it->face_id = FACE_FOR_CHAR (it->f, face, it->c, pos, it->string);
5947 }
5948 }
5949 #endif
5950
5951 /* Is this character the last one of a run of characters with
5952 box? If yes, set IT->end_of_box_run_p to 1. */
5953 if (it->face_box_p
5954 && it->s == NULL)
5955 {
5956 if (it->method == GET_FROM_STRING && it->sp)
5957 {
5958 int face_id = underlying_face_id (it);
5959 struct face *face = FACE_FROM_ID (it->f, face_id);
5960
5961 if (face)
5962 {
5963 if (face->box == FACE_NO_BOX)
5964 {
5965 /* If the box comes from face properties in a
5966 display string, check faces in that string. */
5967 int string_face_id = face_after_it_pos (it);
5968 it->end_of_box_run_p
5969 = (FACE_FROM_ID (it->f, string_face_id)->box
5970 == FACE_NO_BOX);
5971 }
5972 /* Otherwise, the box comes from the underlying face.
5973 If this is the last string character displayed, check
5974 the next buffer location. */
5975 else if ((IT_STRING_CHARPOS (*it) >= SCHARS (it->string) - 1)
5976 && (it->current.overlay_string_index
5977 == it->n_overlay_strings - 1))
5978 {
5979 EMACS_INT ignore;
5980 int next_face_id;
5981 struct text_pos pos = it->current.pos;
5982 INC_TEXT_POS (pos, it->multibyte_p);
5983
5984 next_face_id = face_at_buffer_position
5985 (it->w, CHARPOS (pos), it->region_beg_charpos,
5986 it->region_end_charpos, &ignore,
5987 (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT), 0,
5988 -1);
5989 it->end_of_box_run_p
5990 = (FACE_FROM_ID (it->f, next_face_id)->box
5991 == FACE_NO_BOX);
5992 }
5993 }
5994 }
5995 else
5996 {
5997 int face_id = face_after_it_pos (it);
5998 it->end_of_box_run_p
5999 = (face_id != it->face_id
6000 && FACE_FROM_ID (it->f, face_id)->box == FACE_NO_BOX);
6001 }
6002 }
6003
6004 /* Value is 0 if end of buffer or string reached. */
6005 return success_p;
6006 }
6007
6008
6009 /* Move IT to the next display element.
6010
6011 RESEAT_P non-zero means if called on a newline in buffer text,
6012 skip to the next visible line start.
6013
6014 Functions get_next_display_element and set_iterator_to_next are
6015 separate because I find this arrangement easier to handle than a
6016 get_next_display_element function that also increments IT's
6017 position. The way it is we can first look at an iterator's current
6018 display element, decide whether it fits on a line, and if it does,
6019 increment the iterator position. The other way around we probably
6020 would either need a flag indicating whether the iterator has to be
6021 incremented the next time, or we would have to implement a
6022 decrement position function which would not be easy to write. */
6023
6024 void
6025 set_iterator_to_next (it, reseat_p)
6026 struct it *it;
6027 int reseat_p;
6028 {
6029 /* Reset flags indicating start and end of a sequence of characters
6030 with box. Reset them at the start of this function because
6031 moving the iterator to a new position might set them. */
6032 it->start_of_box_run_p = it->end_of_box_run_p = 0;
6033
6034 switch (it->method)
6035 {
6036 case GET_FROM_BUFFER:
6037 /* The current display element of IT is a character from
6038 current_buffer. Advance in the buffer, and maybe skip over
6039 invisible lines that are so because of selective display. */
6040 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
6041 reseat_at_next_visible_line_start (it, 0);
6042 else if (it->cmp_it.id >= 0)
6043 {
6044 IT_CHARPOS (*it) += it->cmp_it.nchars;
6045 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
6046 if (it->cmp_it.to < it->cmp_it.nglyphs)
6047 it->cmp_it.from = it->cmp_it.to;
6048 else
6049 {
6050 it->cmp_it.id = -1;
6051 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
6052 IT_BYTEPOS (*it), it->stop_charpos,
6053 Qnil);
6054 }
6055 }
6056 else
6057 {
6058 xassert (it->len != 0);
6059 IT_BYTEPOS (*it) += it->len;
6060 IT_CHARPOS (*it) += 1;
6061 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
6062 }
6063 break;
6064
6065 case GET_FROM_C_STRING:
6066 /* Current display element of IT is from a C string. */
6067 IT_BYTEPOS (*it) += it->len;
6068 IT_CHARPOS (*it) += 1;
6069 break;
6070
6071 case GET_FROM_DISPLAY_VECTOR:
6072 /* Current display element of IT is from a display table entry.
6073 Advance in the display table definition. Reset it to null if
6074 end reached, and continue with characters from buffers/
6075 strings. */
6076 ++it->current.dpvec_index;
6077
6078 /* Restore face of the iterator to what they were before the
6079 display vector entry (these entries may contain faces). */
6080 it->face_id = it->saved_face_id;
6081
6082 if (it->dpvec + it->current.dpvec_index == it->dpend)
6083 {
6084 int recheck_faces = it->ellipsis_p;
6085
6086 if (it->s)
6087 it->method = GET_FROM_C_STRING;
6088 else if (STRINGP (it->string))
6089 it->method = GET_FROM_STRING;
6090 else
6091 {
6092 it->method = GET_FROM_BUFFER;
6093 it->object = it->w->buffer;
6094 }
6095
6096 it->dpvec = NULL;
6097 it->current.dpvec_index = -1;
6098
6099 /* Skip over characters which were displayed via IT->dpvec. */
6100 if (it->dpvec_char_len < 0)
6101 reseat_at_next_visible_line_start (it, 1);
6102 else if (it->dpvec_char_len > 0)
6103 {
6104 if (it->method == GET_FROM_STRING
6105 && it->n_overlay_strings > 0)
6106 it->ignore_overlay_strings_at_pos_p = 1;
6107 it->len = it->dpvec_char_len;
6108 set_iterator_to_next (it, reseat_p);
6109 }
6110
6111 /* Maybe recheck faces after display vector */
6112 if (recheck_faces)
6113 it->stop_charpos = IT_CHARPOS (*it);
6114 }
6115 break;
6116
6117 case GET_FROM_STRING:
6118 /* Current display element is a character from a Lisp string. */
6119 xassert (it->s == NULL && STRINGP (it->string));
6120 if (it->cmp_it.id >= 0)
6121 {
6122 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
6123 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
6124 if (it->cmp_it.to < it->cmp_it.nglyphs)
6125 it->cmp_it.from = it->cmp_it.to;
6126 else
6127 {
6128 it->cmp_it.id = -1;
6129 composition_compute_stop_pos (&it->cmp_it,
6130 IT_STRING_CHARPOS (*it),
6131 IT_STRING_BYTEPOS (*it),
6132 it->stop_charpos, it->string);
6133 }
6134 }
6135 else
6136 {
6137 IT_STRING_BYTEPOS (*it) += it->len;
6138 IT_STRING_CHARPOS (*it) += 1;
6139 }
6140
6141 consider_string_end:
6142
6143 if (it->current.overlay_string_index >= 0)
6144 {
6145 /* IT->string is an overlay string. Advance to the
6146 next, if there is one. */
6147 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
6148 {
6149 it->ellipsis_p = 0;
6150 next_overlay_string (it);
6151 if (it->ellipsis_p)
6152 setup_for_ellipsis (it, 0);
6153 }
6154 }
6155 else
6156 {
6157 /* IT->string is not an overlay string. If we reached
6158 its end, and there is something on IT->stack, proceed
6159 with what is on the stack. This can be either another
6160 string, this time an overlay string, or a buffer. */
6161 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
6162 && it->sp > 0)
6163 {
6164 pop_it (it);
6165 if (it->method == GET_FROM_STRING)
6166 goto consider_string_end;
6167 }
6168 }
6169 break;
6170
6171 case GET_FROM_IMAGE:
6172 case GET_FROM_STRETCH:
6173 /* The position etc with which we have to proceed are on
6174 the stack. The position may be at the end of a string,
6175 if the `display' property takes up the whole string. */
6176 xassert (it->sp > 0);
6177 pop_it (it);
6178 if (it->method == GET_FROM_STRING)
6179 goto consider_string_end;
6180 break;
6181
6182 default:
6183 /* There are no other methods defined, so this should be a bug. */
6184 abort ();
6185 }
6186
6187 xassert (it->method != GET_FROM_STRING
6188 || (STRINGP (it->string)
6189 && IT_STRING_CHARPOS (*it) >= 0));
6190 }
6191
6192 /* Load IT's display element fields with information about the next
6193 display element which comes from a display table entry or from the
6194 result of translating a control character to one of the forms `^C'
6195 or `\003'.
6196
6197 IT->dpvec holds the glyphs to return as characters.
6198 IT->saved_face_id holds the face id before the display vector--it
6199 is restored into IT->face_id in set_iterator_to_next. */
6200
6201 static int
6202 next_element_from_display_vector (it)
6203 struct it *it;
6204 {
6205 Lisp_Object gc;
6206
6207 /* Precondition. */
6208 xassert (it->dpvec && it->current.dpvec_index >= 0);
6209
6210 it->face_id = it->saved_face_id;
6211
6212 /* KFS: This code used to check ip->dpvec[0] instead of the current element.
6213 That seemed totally bogus - so I changed it... */
6214 gc = it->dpvec[it->current.dpvec_index];
6215
6216 if (GLYPH_CODE_P (gc) && GLYPH_CODE_CHAR_VALID_P (gc))
6217 {
6218 it->c = GLYPH_CODE_CHAR (gc);
6219 it->len = CHAR_BYTES (it->c);
6220
6221 /* The entry may contain a face id to use. Such a face id is
6222 the id of a Lisp face, not a realized face. A face id of
6223 zero means no face is specified. */
6224 if (it->dpvec_face_id >= 0)
6225 it->face_id = it->dpvec_face_id;
6226 else
6227 {
6228 int lface_id = GLYPH_CODE_FACE (gc);
6229 if (lface_id > 0)
6230 it->face_id = merge_faces (it->f, Qt, lface_id,
6231 it->saved_face_id);
6232 }
6233 }
6234 else
6235 /* Display table entry is invalid. Return a space. */
6236 it->c = ' ', it->len = 1;
6237
6238 /* Don't change position and object of the iterator here. They are
6239 still the values of the character that had this display table
6240 entry or was translated, and that's what we want. */
6241 it->what = IT_CHARACTER;
6242 return 1;
6243 }
6244
6245
6246 /* Load IT with the next display element from Lisp string IT->string.
6247 IT->current.string_pos is the current position within the string.
6248 If IT->current.overlay_string_index >= 0, the Lisp string is an
6249 overlay string. */
6250
6251 static int
6252 next_element_from_string (it)
6253 struct it *it;
6254 {
6255 struct text_pos position;
6256
6257 xassert (STRINGP (it->string));
6258 xassert (IT_STRING_CHARPOS (*it) >= 0);
6259 position = it->current.string_pos;
6260
6261 /* Time to check for invisible text? */
6262 if (IT_STRING_CHARPOS (*it) < it->end_charpos
6263 && IT_STRING_CHARPOS (*it) == it->stop_charpos)
6264 {
6265 handle_stop (it);
6266
6267 /* Since a handler may have changed IT->method, we must
6268 recurse here. */
6269 return GET_NEXT_DISPLAY_ELEMENT (it);
6270 }
6271
6272 if (it->current.overlay_string_index >= 0)
6273 {
6274 /* Get the next character from an overlay string. In overlay
6275 strings, There is no field width or padding with spaces to
6276 do. */
6277 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
6278 {
6279 it->what = IT_EOB;
6280 return 0;
6281 }
6282 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
6283 IT_STRING_BYTEPOS (*it), SCHARS (it->string))
6284 && next_element_from_composition (it))
6285 {
6286 return 1;
6287 }
6288 else if (STRING_MULTIBYTE (it->string))
6289 {
6290 int remaining = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
6291 const unsigned char *s = (SDATA (it->string)
6292 + IT_STRING_BYTEPOS (*it));
6293 it->c = string_char_and_length (s, &it->len);
6294 }
6295 else
6296 {
6297 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
6298 it->len = 1;
6299 }
6300 }
6301 else
6302 {
6303 /* Get the next character from a Lisp string that is not an
6304 overlay string. Such strings come from the mode line, for
6305 example. We may have to pad with spaces, or truncate the
6306 string. See also next_element_from_c_string. */
6307 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
6308 {
6309 it->what = IT_EOB;
6310 return 0;
6311 }
6312 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
6313 {
6314 /* Pad with spaces. */
6315 it->c = ' ', it->len = 1;
6316 CHARPOS (position) = BYTEPOS (position) = -1;
6317 }
6318 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
6319 IT_STRING_BYTEPOS (*it), it->string_nchars)
6320 && next_element_from_composition (it))
6321 {
6322 return 1;
6323 }
6324 else if (STRING_MULTIBYTE (it->string))
6325 {
6326 int maxlen = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
6327 const unsigned char *s = (SDATA (it->string)
6328 + IT_STRING_BYTEPOS (*it));
6329 it->c = string_char_and_length (s, &it->len);
6330 }
6331 else
6332 {
6333 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
6334 it->len = 1;
6335 }
6336 }
6337
6338 /* Record what we have and where it came from. */
6339 it->what = IT_CHARACTER;
6340 it->object = it->string;
6341 it->position = position;
6342 return 1;
6343 }
6344
6345
6346 /* Load IT with next display element from C string IT->s.
6347 IT->string_nchars is the maximum number of characters to return
6348 from the string. IT->end_charpos may be greater than
6349 IT->string_nchars when this function is called, in which case we
6350 may have to return padding spaces. Value is zero if end of string
6351 reached, including padding spaces. */
6352
6353 static int
6354 next_element_from_c_string (it)
6355 struct it *it;
6356 {
6357 int success_p = 1;
6358
6359 xassert (it->s);
6360 it->what = IT_CHARACTER;
6361 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
6362 it->object = Qnil;
6363
6364 /* IT's position can be greater IT->string_nchars in case a field
6365 width or precision has been specified when the iterator was
6366 initialized. */
6367 if (IT_CHARPOS (*it) >= it->end_charpos)
6368 {
6369 /* End of the game. */
6370 it->what = IT_EOB;
6371 success_p = 0;
6372 }
6373 else if (IT_CHARPOS (*it) >= it->string_nchars)
6374 {
6375 /* Pad with spaces. */
6376 it->c = ' ', it->len = 1;
6377 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
6378 }
6379 else if (it->multibyte_p)
6380 {
6381 /* Implementation note: The calls to strlen apparently aren't a
6382 performance problem because there is no noticeable performance
6383 difference between Emacs running in unibyte or multibyte mode. */
6384 int maxlen = strlen (it->s) - IT_BYTEPOS (*it);
6385 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it), &it->len);
6386 }
6387 else
6388 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
6389
6390 return success_p;
6391 }
6392
6393
6394 /* Set up IT to return characters from an ellipsis, if appropriate.
6395 The definition of the ellipsis glyphs may come from a display table
6396 entry. This function fills IT with the first glyph from the
6397 ellipsis if an ellipsis is to be displayed. */
6398
6399 static int
6400 next_element_from_ellipsis (it)
6401 struct it *it;
6402 {
6403 if (it->selective_display_ellipsis_p)
6404 setup_for_ellipsis (it, it->len);
6405 else
6406 {
6407 /* The face at the current position may be different from the
6408 face we find after the invisible text. Remember what it
6409 was in IT->saved_face_id, and signal that it's there by
6410 setting face_before_selective_p. */
6411 it->saved_face_id = it->face_id;
6412 it->method = GET_FROM_BUFFER;
6413 it->object = it->w->buffer;
6414 reseat_at_next_visible_line_start (it, 1);
6415 it->face_before_selective_p = 1;
6416 }
6417
6418 return GET_NEXT_DISPLAY_ELEMENT (it);
6419 }
6420
6421
6422 /* Deliver an image display element. The iterator IT is already
6423 filled with image information (done in handle_display_prop). Value
6424 is always 1. */
6425
6426
6427 static int
6428 next_element_from_image (it)
6429 struct it *it;
6430 {
6431 it->what = IT_IMAGE;
6432 return 1;
6433 }
6434
6435
6436 /* Fill iterator IT with next display element from a stretch glyph
6437 property. IT->object is the value of the text property. Value is
6438 always 1. */
6439
6440 static int
6441 next_element_from_stretch (it)
6442 struct it *it;
6443 {
6444 it->what = IT_STRETCH;
6445 return 1;
6446 }
6447
6448
6449 /* Load IT with the next display element from current_buffer. Value
6450 is zero if end of buffer reached. IT->stop_charpos is the next
6451 position at which to stop and check for text properties or buffer
6452 end. */
6453
6454 static int
6455 next_element_from_buffer (it)
6456 struct it *it;
6457 {
6458 int success_p = 1;
6459
6460 xassert (IT_CHARPOS (*it) >= BEGV);
6461
6462 if (IT_CHARPOS (*it) >= it->stop_charpos)
6463 {
6464 if (IT_CHARPOS (*it) >= it->end_charpos)
6465 {
6466 int overlay_strings_follow_p;
6467
6468 /* End of the game, except when overlay strings follow that
6469 haven't been returned yet. */
6470 if (it->overlay_strings_at_end_processed_p)
6471 overlay_strings_follow_p = 0;
6472 else
6473 {
6474 it->overlay_strings_at_end_processed_p = 1;
6475 overlay_strings_follow_p = get_overlay_strings (it, 0);
6476 }
6477
6478 if (overlay_strings_follow_p)
6479 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
6480 else
6481 {
6482 it->what = IT_EOB;
6483 it->position = it->current.pos;
6484 success_p = 0;
6485 }
6486 }
6487 else
6488 {
6489 handle_stop (it);
6490 return GET_NEXT_DISPLAY_ELEMENT (it);
6491 }
6492 }
6493 else
6494 {
6495 /* No face changes, overlays etc. in sight, so just return a
6496 character from current_buffer. */
6497 unsigned char *p;
6498
6499 /* Maybe run the redisplay end trigger hook. Performance note:
6500 This doesn't seem to cost measurable time. */
6501 if (it->redisplay_end_trigger_charpos
6502 && it->glyph_row
6503 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
6504 run_redisplay_end_trigger_hook (it);
6505
6506 if (CHAR_COMPOSED_P (it, IT_CHARPOS (*it), IT_BYTEPOS (*it),
6507 it->end_charpos)
6508 && next_element_from_composition (it))
6509 {
6510 return 1;
6511 }
6512
6513 /* Get the next character, maybe multibyte. */
6514 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
6515 if (it->multibyte_p && !ASCII_BYTE_P (*p))
6516 it->c = STRING_CHAR_AND_LENGTH (p, it->len);
6517 else
6518 it->c = *p, it->len = 1;
6519
6520 /* Record what we have and where it came from. */
6521 it->what = IT_CHARACTER;
6522 it->object = it->w->buffer;
6523 it->position = it->current.pos;
6524
6525 /* Normally we return the character found above, except when we
6526 really want to return an ellipsis for selective display. */
6527 if (it->selective)
6528 {
6529 if (it->c == '\n')
6530 {
6531 /* A value of selective > 0 means hide lines indented more
6532 than that number of columns. */
6533 if (it->selective > 0
6534 && IT_CHARPOS (*it) + 1 < ZV
6535 && indented_beyond_p (IT_CHARPOS (*it) + 1,
6536 IT_BYTEPOS (*it) + 1,
6537 (double) it->selective)) /* iftc */
6538 {
6539 success_p = next_element_from_ellipsis (it);
6540 it->dpvec_char_len = -1;
6541 }
6542 }
6543 else if (it->c == '\r' && it->selective == -1)
6544 {
6545 /* A value of selective == -1 means that everything from the
6546 CR to the end of the line is invisible, with maybe an
6547 ellipsis displayed for it. */
6548 success_p = next_element_from_ellipsis (it);
6549 it->dpvec_char_len = -1;
6550 }
6551 }
6552 }
6553
6554 /* Value is zero if end of buffer reached. */
6555 xassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
6556 return success_p;
6557 }
6558
6559
6560 /* Run the redisplay end trigger hook for IT. */
6561
6562 static void
6563 run_redisplay_end_trigger_hook (it)
6564 struct it *it;
6565 {
6566 Lisp_Object args[3];
6567
6568 /* IT->glyph_row should be non-null, i.e. we should be actually
6569 displaying something, or otherwise we should not run the hook. */
6570 xassert (it->glyph_row);
6571
6572 /* Set up hook arguments. */
6573 args[0] = Qredisplay_end_trigger_functions;
6574 args[1] = it->window;
6575 XSETINT (args[2], it->redisplay_end_trigger_charpos);
6576 it->redisplay_end_trigger_charpos = 0;
6577
6578 /* Since we are *trying* to run these functions, don't try to run
6579 them again, even if they get an error. */
6580 it->w->redisplay_end_trigger = Qnil;
6581 Frun_hook_with_args (3, args);
6582
6583 /* Notice if it changed the face of the character we are on. */
6584 handle_face_prop (it);
6585 }
6586
6587
6588 /* Deliver a composition display element. Unlike the other
6589 next_element_from_XXX, this function is not registered in the array
6590 get_next_element[]. It is called from next_element_from_buffer and
6591 next_element_from_string when necessary. */
6592
6593 static int
6594 next_element_from_composition (it)
6595 struct it *it;
6596 {
6597 it->what = IT_COMPOSITION;
6598 it->len = it->cmp_it.nbytes;
6599 if (STRINGP (it->string))
6600 {
6601 if (it->c < 0)
6602 {
6603 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
6604 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
6605 return 0;
6606 }
6607 it->position = it->current.string_pos;
6608 it->object = it->string;
6609 it->c = composition_update_it (&it->cmp_it, IT_STRING_CHARPOS (*it),
6610 IT_STRING_BYTEPOS (*it), it->string);
6611 }
6612 else
6613 {
6614 if (it->c < 0)
6615 {
6616 IT_CHARPOS (*it) += it->cmp_it.nchars;
6617 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
6618 return 0;
6619 }
6620 it->position = it->current.pos;
6621 it->object = it->w->buffer;
6622 it->c = composition_update_it (&it->cmp_it, IT_CHARPOS (*it),
6623 IT_BYTEPOS (*it), Qnil);
6624 }
6625 return 1;
6626 }
6627
6628
6629 \f
6630 /***********************************************************************
6631 Moving an iterator without producing glyphs
6632 ***********************************************************************/
6633
6634 /* Check if iterator is at a position corresponding to a valid buffer
6635 position after some move_it_ call. */
6636
6637 #define IT_POS_VALID_AFTER_MOVE_P(it) \
6638 ((it)->method == GET_FROM_STRING \
6639 ? IT_STRING_CHARPOS (*it) == 0 \
6640 : 1)
6641
6642
6643 /* Move iterator IT to a specified buffer or X position within one
6644 line on the display without producing glyphs.
6645
6646 OP should be a bit mask including some or all of these bits:
6647 MOVE_TO_X: Stop on reaching x-position TO_X.
6648 MOVE_TO_POS: Stop on reaching buffer or string position TO_CHARPOS.
6649 Regardless of OP's value, stop in reaching the end of the display line.
6650
6651 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
6652 This means, in particular, that TO_X includes window's horizontal
6653 scroll amount.
6654
6655 The return value has several possible values that
6656 say what condition caused the scan to stop:
6657
6658 MOVE_POS_MATCH_OR_ZV
6659 - when TO_POS or ZV was reached.
6660
6661 MOVE_X_REACHED
6662 -when TO_X was reached before TO_POS or ZV were reached.
6663
6664 MOVE_LINE_CONTINUED
6665 - when we reached the end of the display area and the line must
6666 be continued.
6667
6668 MOVE_LINE_TRUNCATED
6669 - when we reached the end of the display area and the line is
6670 truncated.
6671
6672 MOVE_NEWLINE_OR_CR
6673 - when we stopped at a line end, i.e. a newline or a CR and selective
6674 display is on. */
6675
6676 static enum move_it_result
6677 move_it_in_display_line_to (struct it *it,
6678 EMACS_INT to_charpos, int to_x,
6679 enum move_operation_enum op)
6680 {
6681 enum move_it_result result = MOVE_UNDEFINED;
6682 struct glyph_row *saved_glyph_row;
6683 struct it wrap_it, atpos_it, atx_it;
6684 int may_wrap = 0;
6685
6686 /* Don't produce glyphs in produce_glyphs. */
6687 saved_glyph_row = it->glyph_row;
6688 it->glyph_row = NULL;
6689
6690 /* Use wrap_it to save a copy of IT wherever a word wrap could
6691 occur. Use atpos_it to save a copy of IT at the desired buffer
6692 position, if found, so that we can scan ahead and check if the
6693 word later overshoots the window edge. Use atx_it similarly, for
6694 pixel positions. */
6695 wrap_it.sp = -1;
6696 atpos_it.sp = -1;
6697 atx_it.sp = -1;
6698
6699 #define BUFFER_POS_REACHED_P() \
6700 ((op & MOVE_TO_POS) != 0 \
6701 && BUFFERP (it->object) \
6702 && IT_CHARPOS (*it) >= to_charpos \
6703 && (it->method == GET_FROM_BUFFER \
6704 || (it->method == GET_FROM_DISPLAY_VECTOR \
6705 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
6706
6707 /* If there's a line-/wrap-prefix, handle it. */
6708 if (it->hpos == 0 && it->method == GET_FROM_BUFFER
6709 && it->current_y < it->last_visible_y)
6710 handle_line_prefix (it);
6711
6712 while (1)
6713 {
6714 int x, i, ascent = 0, descent = 0;
6715
6716 /* Utility macro to reset an iterator with x, ascent, and descent. */
6717 #define IT_RESET_X_ASCENT_DESCENT(IT) \
6718 ((IT)->current_x = x, (IT)->max_ascent = ascent, \
6719 (IT)->max_descent = descent)
6720
6721 /* Stop if we move beyond TO_CHARPOS (after an image or stretch
6722 glyph). */
6723 if ((op & MOVE_TO_POS) != 0
6724 && BUFFERP (it->object)
6725 && it->method == GET_FROM_BUFFER
6726 && IT_CHARPOS (*it) > to_charpos)
6727 {
6728 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
6729 {
6730 result = MOVE_POS_MATCH_OR_ZV;
6731 break;
6732 }
6733 else if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
6734 /* If wrap_it is valid, the current position might be in a
6735 word that is wrapped. So, save the iterator in
6736 atpos_it and continue to see if wrapping happens. */
6737 atpos_it = *it;
6738 }
6739
6740 /* Stop when ZV reached.
6741 We used to stop here when TO_CHARPOS reached as well, but that is
6742 too soon if this glyph does not fit on this line. So we handle it
6743 explicitly below. */
6744 if (!get_next_display_element (it))
6745 {
6746 result = MOVE_POS_MATCH_OR_ZV;
6747 break;
6748 }
6749
6750 if (it->line_wrap == TRUNCATE)
6751 {
6752 if (BUFFER_POS_REACHED_P ())
6753 {
6754 result = MOVE_POS_MATCH_OR_ZV;
6755 break;
6756 }
6757 }
6758 else
6759 {
6760 if (it->line_wrap == WORD_WRAP)
6761 {
6762 if (IT_DISPLAYING_WHITESPACE (it))
6763 may_wrap = 1;
6764 else if (may_wrap)
6765 {
6766 /* We have reached a glyph that follows one or more
6767 whitespace characters. If the position is
6768 already found, we are done. */
6769 if (atpos_it.sp >= 0)
6770 {
6771 *it = atpos_it;
6772 result = MOVE_POS_MATCH_OR_ZV;
6773 goto done;
6774 }
6775 if (atx_it.sp >= 0)
6776 {
6777 *it = atx_it;
6778 result = MOVE_X_REACHED;
6779 goto done;
6780 }
6781 /* Otherwise, we can wrap here. */
6782 wrap_it = *it;
6783 may_wrap = 0;
6784 }
6785 }
6786 }
6787
6788 /* Remember the line height for the current line, in case
6789 the next element doesn't fit on the line. */
6790 ascent = it->max_ascent;
6791 descent = it->max_descent;
6792
6793 /* The call to produce_glyphs will get the metrics of the
6794 display element IT is loaded with. Record the x-position
6795 before this display element, in case it doesn't fit on the
6796 line. */
6797 x = it->current_x;
6798
6799 PRODUCE_GLYPHS (it);
6800
6801 if (it->area != TEXT_AREA)
6802 {
6803 set_iterator_to_next (it, 1);
6804 continue;
6805 }
6806
6807 /* The number of glyphs we get back in IT->nglyphs will normally
6808 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
6809 character on a terminal frame, or (iii) a line end. For the
6810 second case, IT->nglyphs - 1 padding glyphs will be present.
6811 (On X frames, there is only one glyph produced for a
6812 composite character.)
6813
6814 The behavior implemented below means, for continuation lines,
6815 that as many spaces of a TAB as fit on the current line are
6816 displayed there. For terminal frames, as many glyphs of a
6817 multi-glyph character are displayed in the current line, too.
6818 This is what the old redisplay code did, and we keep it that
6819 way. Under X, the whole shape of a complex character must
6820 fit on the line or it will be completely displayed in the
6821 next line.
6822
6823 Note that both for tabs and padding glyphs, all glyphs have
6824 the same width. */
6825 if (it->nglyphs)
6826 {
6827 /* More than one glyph or glyph doesn't fit on line. All
6828 glyphs have the same width. */
6829 int single_glyph_width = it->pixel_width / it->nglyphs;
6830 int new_x;
6831 int x_before_this_char = x;
6832 int hpos_before_this_char = it->hpos;
6833
6834 for (i = 0; i < it->nglyphs; ++i, x = new_x)
6835 {
6836 new_x = x + single_glyph_width;
6837
6838 /* We want to leave anything reaching TO_X to the caller. */
6839 if ((op & MOVE_TO_X) && new_x > to_x)
6840 {
6841 if (BUFFER_POS_REACHED_P ())
6842 {
6843 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
6844 goto buffer_pos_reached;
6845 if (atpos_it.sp < 0)
6846 {
6847 atpos_it = *it;
6848 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
6849 }
6850 }
6851 else
6852 {
6853 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
6854 {
6855 it->current_x = x;
6856 result = MOVE_X_REACHED;
6857 break;
6858 }
6859 if (atx_it.sp < 0)
6860 {
6861 atx_it = *it;
6862 IT_RESET_X_ASCENT_DESCENT (&atx_it);
6863 }
6864 }
6865 }
6866
6867 if (/* Lines are continued. */
6868 it->line_wrap != TRUNCATE
6869 && (/* And glyph doesn't fit on the line. */
6870 new_x > it->last_visible_x
6871 /* Or it fits exactly and we're on a window
6872 system frame. */
6873 || (new_x == it->last_visible_x
6874 && FRAME_WINDOW_P (it->f))))
6875 {
6876 if (/* IT->hpos == 0 means the very first glyph
6877 doesn't fit on the line, e.g. a wide image. */
6878 it->hpos == 0
6879 || (new_x == it->last_visible_x
6880 && FRAME_WINDOW_P (it->f)))
6881 {
6882 ++it->hpos;
6883 it->current_x = new_x;
6884
6885 /* The character's last glyph just barely fits
6886 in this row. */
6887 if (i == it->nglyphs - 1)
6888 {
6889 /* If this is the destination position,
6890 return a position *before* it in this row,
6891 now that we know it fits in this row. */
6892 if (BUFFER_POS_REACHED_P ())
6893 {
6894 if (it->line_wrap != WORD_WRAP
6895 || wrap_it.sp < 0)
6896 {
6897 it->hpos = hpos_before_this_char;
6898 it->current_x = x_before_this_char;
6899 result = MOVE_POS_MATCH_OR_ZV;
6900 break;
6901 }
6902 if (it->line_wrap == WORD_WRAP
6903 && atpos_it.sp < 0)
6904 {
6905 atpos_it = *it;
6906 atpos_it.current_x = x_before_this_char;
6907 atpos_it.hpos = hpos_before_this_char;
6908 }
6909 }
6910
6911 set_iterator_to_next (it, 1);
6912 /* On graphical terminals, newlines may
6913 "overflow" into the fringe if
6914 overflow-newline-into-fringe is non-nil.
6915 On text-only terminals, newlines may
6916 overflow into the last glyph on the
6917 display line.*/
6918 if (!FRAME_WINDOW_P (it->f)
6919 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
6920 {
6921 if (!get_next_display_element (it))
6922 {
6923 result = MOVE_POS_MATCH_OR_ZV;
6924 break;
6925 }
6926 if (BUFFER_POS_REACHED_P ())
6927 {
6928 if (ITERATOR_AT_END_OF_LINE_P (it))
6929 result = MOVE_POS_MATCH_OR_ZV;
6930 else
6931 result = MOVE_LINE_CONTINUED;
6932 break;
6933 }
6934 if (ITERATOR_AT_END_OF_LINE_P (it))
6935 {
6936 result = MOVE_NEWLINE_OR_CR;
6937 break;
6938 }
6939 }
6940 }
6941 }
6942 else
6943 IT_RESET_X_ASCENT_DESCENT (it);
6944
6945 if (wrap_it.sp >= 0)
6946 {
6947 *it = wrap_it;
6948 atpos_it.sp = -1;
6949 atx_it.sp = -1;
6950 }
6951
6952 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
6953 IT_CHARPOS (*it)));
6954 result = MOVE_LINE_CONTINUED;
6955 break;
6956 }
6957
6958 if (BUFFER_POS_REACHED_P ())
6959 {
6960 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
6961 goto buffer_pos_reached;
6962 if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
6963 {
6964 atpos_it = *it;
6965 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
6966 }
6967 }
6968
6969 if (new_x > it->first_visible_x)
6970 {
6971 /* Glyph is visible. Increment number of glyphs that
6972 would be displayed. */
6973 ++it->hpos;
6974 }
6975 }
6976
6977 if (result != MOVE_UNDEFINED)
6978 break;
6979 }
6980 else if (BUFFER_POS_REACHED_P ())
6981 {
6982 buffer_pos_reached:
6983 IT_RESET_X_ASCENT_DESCENT (it);
6984 result = MOVE_POS_MATCH_OR_ZV;
6985 break;
6986 }
6987 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
6988 {
6989 /* Stop when TO_X specified and reached. This check is
6990 necessary here because of lines consisting of a line end,
6991 only. The line end will not produce any glyphs and we
6992 would never get MOVE_X_REACHED. */
6993 xassert (it->nglyphs == 0);
6994 result = MOVE_X_REACHED;
6995 break;
6996 }
6997
6998 /* Is this a line end? If yes, we're done. */
6999 if (ITERATOR_AT_END_OF_LINE_P (it))
7000 {
7001 result = MOVE_NEWLINE_OR_CR;
7002 break;
7003 }
7004
7005 /* The current display element has been consumed. Advance
7006 to the next. */
7007 set_iterator_to_next (it, 1);
7008
7009 /* Stop if lines are truncated and IT's current x-position is
7010 past the right edge of the window now. */
7011 if (it->line_wrap == TRUNCATE
7012 && it->current_x >= it->last_visible_x)
7013 {
7014 if (!FRAME_WINDOW_P (it->f)
7015 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
7016 {
7017 if (!get_next_display_element (it)
7018 || BUFFER_POS_REACHED_P ())
7019 {
7020 result = MOVE_POS_MATCH_OR_ZV;
7021 break;
7022 }
7023 if (ITERATOR_AT_END_OF_LINE_P (it))
7024 {
7025 result = MOVE_NEWLINE_OR_CR;
7026 break;
7027 }
7028 }
7029 result = MOVE_LINE_TRUNCATED;
7030 break;
7031 }
7032 #undef IT_RESET_X_ASCENT_DESCENT
7033 }
7034
7035 #undef BUFFER_POS_REACHED_P
7036
7037 /* If we scanned beyond to_pos and didn't find a point to wrap at,
7038 restore the saved iterator. */
7039 if (atpos_it.sp >= 0)
7040 *it = atpos_it;
7041 else if (atx_it.sp >= 0)
7042 *it = atx_it;
7043
7044 done:
7045
7046 /* Restore the iterator settings altered at the beginning of this
7047 function. */
7048 it->glyph_row = saved_glyph_row;
7049 return result;
7050 }
7051
7052 /* For external use. */
7053 void
7054 move_it_in_display_line (struct it *it,
7055 EMACS_INT to_charpos, int to_x,
7056 enum move_operation_enum op)
7057 {
7058 if (it->line_wrap == WORD_WRAP
7059 && (op & MOVE_TO_X))
7060 {
7061 struct it save_it = *it;
7062 int skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
7063 /* When word-wrap is on, TO_X may lie past the end
7064 of a wrapped line. Then it->current is the
7065 character on the next line, so backtrack to the
7066 space before the wrap point. */
7067 if (skip == MOVE_LINE_CONTINUED)
7068 {
7069 int prev_x = max (it->current_x - 1, 0);
7070 *it = save_it;
7071 move_it_in_display_line_to
7072 (it, -1, prev_x, MOVE_TO_X);
7073 }
7074 }
7075 else
7076 move_it_in_display_line_to (it, to_charpos, to_x, op);
7077 }
7078
7079
7080 /* Move IT forward until it satisfies one or more of the criteria in
7081 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
7082
7083 OP is a bit-mask that specifies where to stop, and in particular,
7084 which of those four position arguments makes a difference. See the
7085 description of enum move_operation_enum.
7086
7087 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
7088 screen line, this function will set IT to the next position >
7089 TO_CHARPOS. */
7090
7091 void
7092 move_it_to (it, to_charpos, to_x, to_y, to_vpos, op)
7093 struct it *it;
7094 int to_charpos, to_x, to_y, to_vpos;
7095 int op;
7096 {
7097 enum move_it_result skip, skip2 = MOVE_X_REACHED;
7098 int line_height, line_start_x = 0, reached = 0;
7099
7100 for (;;)
7101 {
7102 if (op & MOVE_TO_VPOS)
7103 {
7104 /* If no TO_CHARPOS and no TO_X specified, stop at the
7105 start of the line TO_VPOS. */
7106 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
7107 {
7108 if (it->vpos == to_vpos)
7109 {
7110 reached = 1;
7111 break;
7112 }
7113 else
7114 skip = move_it_in_display_line_to (it, -1, -1, 0);
7115 }
7116 else
7117 {
7118 /* TO_VPOS >= 0 means stop at TO_X in the line at
7119 TO_VPOS, or at TO_POS, whichever comes first. */
7120 if (it->vpos == to_vpos)
7121 {
7122 reached = 2;
7123 break;
7124 }
7125
7126 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
7127
7128 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
7129 {
7130 reached = 3;
7131 break;
7132 }
7133 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
7134 {
7135 /* We have reached TO_X but not in the line we want. */
7136 skip = move_it_in_display_line_to (it, to_charpos,
7137 -1, MOVE_TO_POS);
7138 if (skip == MOVE_POS_MATCH_OR_ZV)
7139 {
7140 reached = 4;
7141 break;
7142 }
7143 }
7144 }
7145 }
7146 else if (op & MOVE_TO_Y)
7147 {
7148 struct it it_backup;
7149
7150 if (it->line_wrap == WORD_WRAP)
7151 it_backup = *it;
7152
7153 /* TO_Y specified means stop at TO_X in the line containing
7154 TO_Y---or at TO_CHARPOS if this is reached first. The
7155 problem is that we can't really tell whether the line
7156 contains TO_Y before we have completely scanned it, and
7157 this may skip past TO_X. What we do is to first scan to
7158 TO_X.
7159
7160 If TO_X is not specified, use a TO_X of zero. The reason
7161 is to make the outcome of this function more predictable.
7162 If we didn't use TO_X == 0, we would stop at the end of
7163 the line which is probably not what a caller would expect
7164 to happen. */
7165 skip = move_it_in_display_line_to
7166 (it, to_charpos, ((op & MOVE_TO_X) ? to_x : 0),
7167 (MOVE_TO_X | (op & MOVE_TO_POS)));
7168
7169 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
7170 if (skip == MOVE_POS_MATCH_OR_ZV)
7171 reached = 5;
7172 else if (skip == MOVE_X_REACHED)
7173 {
7174 /* If TO_X was reached, we want to know whether TO_Y is
7175 in the line. We know this is the case if the already
7176 scanned glyphs make the line tall enough. Otherwise,
7177 we must check by scanning the rest of the line. */
7178 line_height = it->max_ascent + it->max_descent;
7179 if (to_y >= it->current_y
7180 && to_y < it->current_y + line_height)
7181 {
7182 reached = 6;
7183 break;
7184 }
7185 it_backup = *it;
7186 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
7187 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
7188 op & MOVE_TO_POS);
7189 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
7190 line_height = it->max_ascent + it->max_descent;
7191 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
7192
7193 if (to_y >= it->current_y
7194 && to_y < it->current_y + line_height)
7195 {
7196 /* If TO_Y is in this line and TO_X was reached
7197 above, we scanned too far. We have to restore
7198 IT's settings to the ones before skipping. */
7199 *it = it_backup;
7200 reached = 6;
7201 }
7202 else
7203 {
7204 skip = skip2;
7205 if (skip == MOVE_POS_MATCH_OR_ZV)
7206 reached = 7;
7207 }
7208 }
7209 else
7210 {
7211 /* Check whether TO_Y is in this line. */
7212 line_height = it->max_ascent + it->max_descent;
7213 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
7214
7215 if (to_y >= it->current_y
7216 && to_y < it->current_y + line_height)
7217 {
7218 /* When word-wrap is on, TO_X may lie past the end
7219 of a wrapped line. Then it->current is the
7220 character on the next line, so backtrack to the
7221 space before the wrap point. */
7222 if (skip == MOVE_LINE_CONTINUED
7223 && it->line_wrap == WORD_WRAP)
7224 {
7225 int prev_x = max (it->current_x - 1, 0);
7226 *it = it_backup;
7227 skip = move_it_in_display_line_to
7228 (it, -1, prev_x, MOVE_TO_X);
7229 }
7230 reached = 6;
7231 }
7232 }
7233
7234 if (reached)
7235 break;
7236 }
7237 else if (BUFFERP (it->object)
7238 && (it->method == GET_FROM_BUFFER
7239 || it->method == GET_FROM_STRETCH)
7240 && IT_CHARPOS (*it) >= to_charpos)
7241 skip = MOVE_POS_MATCH_OR_ZV;
7242 else
7243 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
7244
7245 switch (skip)
7246 {
7247 case MOVE_POS_MATCH_OR_ZV:
7248 reached = 8;
7249 goto out;
7250
7251 case MOVE_NEWLINE_OR_CR:
7252 set_iterator_to_next (it, 1);
7253 it->continuation_lines_width = 0;
7254 break;
7255
7256 case MOVE_LINE_TRUNCATED:
7257 it->continuation_lines_width = 0;
7258 reseat_at_next_visible_line_start (it, 0);
7259 if ((op & MOVE_TO_POS) != 0
7260 && IT_CHARPOS (*it) > to_charpos)
7261 {
7262 reached = 9;
7263 goto out;
7264 }
7265 break;
7266
7267 case MOVE_LINE_CONTINUED:
7268 /* For continued lines ending in a tab, some of the glyphs
7269 associated with the tab are displayed on the current
7270 line. Since it->current_x does not include these glyphs,
7271 we use it->last_visible_x instead. */
7272 if (it->c == '\t')
7273 {
7274 it->continuation_lines_width += it->last_visible_x;
7275 /* When moving by vpos, ensure that the iterator really
7276 advances to the next line (bug#847, bug#969). Fixme:
7277 do we need to do this in other circumstances? */
7278 if (it->current_x != it->last_visible_x
7279 && (op & MOVE_TO_VPOS)
7280 && !(op & (MOVE_TO_X | MOVE_TO_POS)))
7281 {
7282 line_start_x = it->current_x + it->pixel_width
7283 - it->last_visible_x;
7284 set_iterator_to_next (it, 0);
7285 }
7286 }
7287 else
7288 it->continuation_lines_width += it->current_x;
7289 break;
7290
7291 default:
7292 abort ();
7293 }
7294
7295 /* Reset/increment for the next run. */
7296 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
7297 it->current_x = line_start_x;
7298 line_start_x = 0;
7299 it->hpos = 0;
7300 it->current_y += it->max_ascent + it->max_descent;
7301 ++it->vpos;
7302 last_height = it->max_ascent + it->max_descent;
7303 last_max_ascent = it->max_ascent;
7304 it->max_ascent = it->max_descent = 0;
7305 }
7306
7307 out:
7308
7309 /* On text terminals, we may stop at the end of a line in the middle
7310 of a multi-character glyph. If the glyph itself is continued,
7311 i.e. it is actually displayed on the next line, don't treat this
7312 stopping point as valid; move to the next line instead (unless
7313 that brings us offscreen). */
7314 if (!FRAME_WINDOW_P (it->f)
7315 && op & MOVE_TO_POS
7316 && IT_CHARPOS (*it) == to_charpos
7317 && it->what == IT_CHARACTER
7318 && it->nglyphs > 1
7319 && it->line_wrap == WINDOW_WRAP
7320 && it->current_x == it->last_visible_x - 1
7321 && it->c != '\n'
7322 && it->c != '\t'
7323 && it->vpos < XFASTINT (it->w->window_end_vpos))
7324 {
7325 it->continuation_lines_width += it->current_x;
7326 it->current_x = it->hpos = it->max_ascent = it->max_descent = 0;
7327 it->current_y += it->max_ascent + it->max_descent;
7328 ++it->vpos;
7329 last_height = it->max_ascent + it->max_descent;
7330 last_max_ascent = it->max_ascent;
7331 }
7332
7333 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
7334 }
7335
7336
7337 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
7338
7339 If DY > 0, move IT backward at least that many pixels. DY = 0
7340 means move IT backward to the preceding line start or BEGV. This
7341 function may move over more than DY pixels if IT->current_y - DY
7342 ends up in the middle of a line; in this case IT->current_y will be
7343 set to the top of the line moved to. */
7344
7345 void
7346 move_it_vertically_backward (it, dy)
7347 struct it *it;
7348 int dy;
7349 {
7350 int nlines, h;
7351 struct it it2, it3;
7352 int start_pos;
7353
7354 move_further_back:
7355 xassert (dy >= 0);
7356
7357 start_pos = IT_CHARPOS (*it);
7358
7359 /* Estimate how many newlines we must move back. */
7360 nlines = max (1, dy / FRAME_LINE_HEIGHT (it->f));
7361
7362 /* Set the iterator's position that many lines back. */
7363 while (nlines-- && IT_CHARPOS (*it) > BEGV)
7364 back_to_previous_visible_line_start (it);
7365
7366 /* Reseat the iterator here. When moving backward, we don't want
7367 reseat to skip forward over invisible text, set up the iterator
7368 to deliver from overlay strings at the new position etc. So,
7369 use reseat_1 here. */
7370 reseat_1 (it, it->current.pos, 1);
7371
7372 /* We are now surely at a line start. */
7373 it->current_x = it->hpos = 0;
7374 it->continuation_lines_width = 0;
7375
7376 /* Move forward and see what y-distance we moved. First move to the
7377 start of the next line so that we get its height. We need this
7378 height to be able to tell whether we reached the specified
7379 y-distance. */
7380 it2 = *it;
7381 it2.max_ascent = it2.max_descent = 0;
7382 do
7383 {
7384 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
7385 MOVE_TO_POS | MOVE_TO_VPOS);
7386 }
7387 while (!IT_POS_VALID_AFTER_MOVE_P (&it2));
7388 xassert (IT_CHARPOS (*it) >= BEGV);
7389 it3 = it2;
7390
7391 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
7392 xassert (IT_CHARPOS (*it) >= BEGV);
7393 /* H is the actual vertical distance from the position in *IT
7394 and the starting position. */
7395 h = it2.current_y - it->current_y;
7396 /* NLINES is the distance in number of lines. */
7397 nlines = it2.vpos - it->vpos;
7398
7399 /* Correct IT's y and vpos position
7400 so that they are relative to the starting point. */
7401 it->vpos -= nlines;
7402 it->current_y -= h;
7403
7404 if (dy == 0)
7405 {
7406 /* DY == 0 means move to the start of the screen line. The
7407 value of nlines is > 0 if continuation lines were involved. */
7408 if (nlines > 0)
7409 move_it_by_lines (it, nlines, 1);
7410 }
7411 else
7412 {
7413 /* The y-position we try to reach, relative to *IT.
7414 Note that H has been subtracted in front of the if-statement. */
7415 int target_y = it->current_y + h - dy;
7416 int y0 = it3.current_y;
7417 int y1 = line_bottom_y (&it3);
7418 int line_height = y1 - y0;
7419
7420 /* If we did not reach target_y, try to move further backward if
7421 we can. If we moved too far backward, try to move forward. */
7422 if (target_y < it->current_y
7423 /* This is heuristic. In a window that's 3 lines high, with
7424 a line height of 13 pixels each, recentering with point
7425 on the bottom line will try to move -39/2 = 19 pixels
7426 backward. Try to avoid moving into the first line. */
7427 && (it->current_y - target_y
7428 > min (window_box_height (it->w), line_height * 2 / 3))
7429 && IT_CHARPOS (*it) > BEGV)
7430 {
7431 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
7432 target_y - it->current_y));
7433 dy = it->current_y - target_y;
7434 goto move_further_back;
7435 }
7436 else if (target_y >= it->current_y + line_height
7437 && IT_CHARPOS (*it) < ZV)
7438 {
7439 /* Should move forward by at least one line, maybe more.
7440
7441 Note: Calling move_it_by_lines can be expensive on
7442 terminal frames, where compute_motion is used (via
7443 vmotion) to do the job, when there are very long lines
7444 and truncate-lines is nil. That's the reason for
7445 treating terminal frames specially here. */
7446
7447 if (!FRAME_WINDOW_P (it->f))
7448 move_it_vertically (it, target_y - (it->current_y + line_height));
7449 else
7450 {
7451 do
7452 {
7453 move_it_by_lines (it, 1, 1);
7454 }
7455 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
7456 }
7457 }
7458 }
7459 }
7460
7461
7462 /* Move IT by a specified amount of pixel lines DY. DY negative means
7463 move backwards. DY = 0 means move to start of screen line. At the
7464 end, IT will be on the start of a screen line. */
7465
7466 void
7467 move_it_vertically (it, dy)
7468 struct it *it;
7469 int dy;
7470 {
7471 if (dy <= 0)
7472 move_it_vertically_backward (it, -dy);
7473 else
7474 {
7475 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
7476 move_it_to (it, ZV, -1, it->current_y + dy, -1,
7477 MOVE_TO_POS | MOVE_TO_Y);
7478 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
7479
7480 /* If buffer ends in ZV without a newline, move to the start of
7481 the line to satisfy the post-condition. */
7482 if (IT_CHARPOS (*it) == ZV
7483 && ZV > BEGV
7484 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
7485 move_it_by_lines (it, 0, 0);
7486 }
7487 }
7488
7489
7490 /* Move iterator IT past the end of the text line it is in. */
7491
7492 void
7493 move_it_past_eol (it)
7494 struct it *it;
7495 {
7496 enum move_it_result rc;
7497
7498 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
7499 if (rc == MOVE_NEWLINE_OR_CR)
7500 set_iterator_to_next (it, 0);
7501 }
7502
7503
7504 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
7505 negative means move up. DVPOS == 0 means move to the start of the
7506 screen line. NEED_Y_P non-zero means calculate IT->current_y. If
7507 NEED_Y_P is zero, IT->current_y will be left unchanged.
7508
7509 Further optimization ideas: If we would know that IT->f doesn't use
7510 a face with proportional font, we could be faster for
7511 truncate-lines nil. */
7512
7513 void
7514 move_it_by_lines (it, dvpos, need_y_p)
7515 struct it *it;
7516 int dvpos, need_y_p;
7517 {
7518 struct position pos;
7519
7520 /* The commented-out optimization uses vmotion on terminals. This
7521 gives bad results, because elements like it->what, on which
7522 callers such as pos_visible_p rely, aren't updated. */
7523 /* if (!FRAME_WINDOW_P (it->f))
7524 {
7525 struct text_pos textpos;
7526
7527 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
7528 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
7529 reseat (it, textpos, 1);
7530 it->vpos += pos.vpos;
7531 it->current_y += pos.vpos;
7532 }
7533 else */
7534
7535 if (dvpos == 0)
7536 {
7537 /* DVPOS == 0 means move to the start of the screen line. */
7538 move_it_vertically_backward (it, 0);
7539 xassert (it->current_x == 0 && it->hpos == 0);
7540 /* Let next call to line_bottom_y calculate real line height */
7541 last_height = 0;
7542 }
7543 else if (dvpos > 0)
7544 {
7545 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
7546 if (!IT_POS_VALID_AFTER_MOVE_P (it))
7547 move_it_to (it, IT_CHARPOS (*it) + 1, -1, -1, -1, MOVE_TO_POS);
7548 }
7549 else
7550 {
7551 struct it it2;
7552 int start_charpos, i;
7553
7554 /* Start at the beginning of the screen line containing IT's
7555 position. This may actually move vertically backwards,
7556 in case of overlays, so adjust dvpos accordingly. */
7557 dvpos += it->vpos;
7558 move_it_vertically_backward (it, 0);
7559 dvpos -= it->vpos;
7560
7561 /* Go back -DVPOS visible lines and reseat the iterator there. */
7562 start_charpos = IT_CHARPOS (*it);
7563 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > BEGV; --i)
7564 back_to_previous_visible_line_start (it);
7565 reseat (it, it->current.pos, 1);
7566
7567 /* Move further back if we end up in a string or an image. */
7568 while (!IT_POS_VALID_AFTER_MOVE_P (it))
7569 {
7570 /* First try to move to start of display line. */
7571 dvpos += it->vpos;
7572 move_it_vertically_backward (it, 0);
7573 dvpos -= it->vpos;
7574 if (IT_POS_VALID_AFTER_MOVE_P (it))
7575 break;
7576 /* If start of line is still in string or image,
7577 move further back. */
7578 back_to_previous_visible_line_start (it);
7579 reseat (it, it->current.pos, 1);
7580 dvpos--;
7581 }
7582
7583 it->current_x = it->hpos = 0;
7584
7585 /* Above call may have moved too far if continuation lines
7586 are involved. Scan forward and see if it did. */
7587 it2 = *it;
7588 it2.vpos = it2.current_y = 0;
7589 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
7590 it->vpos -= it2.vpos;
7591 it->current_y -= it2.current_y;
7592 it->current_x = it->hpos = 0;
7593
7594 /* If we moved too far back, move IT some lines forward. */
7595 if (it2.vpos > -dvpos)
7596 {
7597 int delta = it2.vpos + dvpos;
7598 it2 = *it;
7599 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
7600 /* Move back again if we got too far ahead. */
7601 if (IT_CHARPOS (*it) >= start_charpos)
7602 *it = it2;
7603 }
7604 }
7605 }
7606
7607 /* Return 1 if IT points into the middle of a display vector. */
7608
7609 int
7610 in_display_vector_p (it)
7611 struct it *it;
7612 {
7613 return (it->method == GET_FROM_DISPLAY_VECTOR
7614 && it->current.dpvec_index > 0
7615 && it->dpvec + it->current.dpvec_index != it->dpend);
7616 }
7617
7618 \f
7619 /***********************************************************************
7620 Messages
7621 ***********************************************************************/
7622
7623
7624 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
7625 to *Messages*. */
7626
7627 void
7628 add_to_log (format, arg1, arg2)
7629 char *format;
7630 Lisp_Object arg1, arg2;
7631 {
7632 Lisp_Object args[3];
7633 Lisp_Object msg, fmt;
7634 char *buffer;
7635 int len;
7636 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
7637 USE_SAFE_ALLOCA;
7638
7639 /* Do nothing if called asynchronously. Inserting text into
7640 a buffer may call after-change-functions and alike and
7641 that would means running Lisp asynchronously. */
7642 if (handling_signal)
7643 return;
7644
7645 fmt = msg = Qnil;
7646 GCPRO4 (fmt, msg, arg1, arg2);
7647
7648 args[0] = fmt = build_string (format);
7649 args[1] = arg1;
7650 args[2] = arg2;
7651 msg = Fformat (3, args);
7652
7653 len = SBYTES (msg) + 1;
7654 SAFE_ALLOCA (buffer, char *, len);
7655 bcopy (SDATA (msg), buffer, len);
7656
7657 message_dolog (buffer, len - 1, 1, 0);
7658 SAFE_FREE ();
7659
7660 UNGCPRO;
7661 }
7662
7663
7664 /* Output a newline in the *Messages* buffer if "needs" one. */
7665
7666 void
7667 message_log_maybe_newline ()
7668 {
7669 if (message_log_need_newline)
7670 message_dolog ("", 0, 1, 0);
7671 }
7672
7673
7674 /* Add a string M of length NBYTES to the message log, optionally
7675 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
7676 nonzero, means interpret the contents of M as multibyte. This
7677 function calls low-level routines in order to bypass text property
7678 hooks, etc. which might not be safe to run.
7679
7680 This may GC (insert may run before/after change hooks),
7681 so the buffer M must NOT point to a Lisp string. */
7682
7683 void
7684 message_dolog (m, nbytes, nlflag, multibyte)
7685 const char *m;
7686 int nbytes, nlflag, multibyte;
7687 {
7688 if (!NILP (Vmemory_full))
7689 return;
7690
7691 if (!NILP (Vmessage_log_max))
7692 {
7693 struct buffer *oldbuf;
7694 Lisp_Object oldpoint, oldbegv, oldzv;
7695 int old_windows_or_buffers_changed = windows_or_buffers_changed;
7696 int point_at_end = 0;
7697 int zv_at_end = 0;
7698 Lisp_Object old_deactivate_mark, tem;
7699 struct gcpro gcpro1;
7700
7701 old_deactivate_mark = Vdeactivate_mark;
7702 oldbuf = current_buffer;
7703 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
7704 current_buffer->undo_list = Qt;
7705
7706 oldpoint = message_dolog_marker1;
7707 set_marker_restricted (oldpoint, make_number (PT), Qnil);
7708 oldbegv = message_dolog_marker2;
7709 set_marker_restricted (oldbegv, make_number (BEGV), Qnil);
7710 oldzv = message_dolog_marker3;
7711 set_marker_restricted (oldzv, make_number (ZV), Qnil);
7712 GCPRO1 (old_deactivate_mark);
7713
7714 if (PT == Z)
7715 point_at_end = 1;
7716 if (ZV == Z)
7717 zv_at_end = 1;
7718
7719 BEGV = BEG;
7720 BEGV_BYTE = BEG_BYTE;
7721 ZV = Z;
7722 ZV_BYTE = Z_BYTE;
7723 TEMP_SET_PT_BOTH (Z, Z_BYTE);
7724
7725 /* Insert the string--maybe converting multibyte to single byte
7726 or vice versa, so that all the text fits the buffer. */
7727 if (multibyte
7728 && NILP (current_buffer->enable_multibyte_characters))
7729 {
7730 int i, c, char_bytes;
7731 unsigned char work[1];
7732
7733 /* Convert a multibyte string to single-byte
7734 for the *Message* buffer. */
7735 for (i = 0; i < nbytes; i += char_bytes)
7736 {
7737 c = string_char_and_length (m + i, &char_bytes);
7738 work[0] = (ASCII_CHAR_P (c)
7739 ? c
7740 : multibyte_char_to_unibyte (c, Qnil));
7741 insert_1_both (work, 1, 1, 1, 0, 0);
7742 }
7743 }
7744 else if (! multibyte
7745 && ! NILP (current_buffer->enable_multibyte_characters))
7746 {
7747 int i, c, char_bytes;
7748 unsigned char *msg = (unsigned char *) m;
7749 unsigned char str[MAX_MULTIBYTE_LENGTH];
7750 /* Convert a single-byte string to multibyte
7751 for the *Message* buffer. */
7752 for (i = 0; i < nbytes; i++)
7753 {
7754 c = msg[i];
7755 MAKE_CHAR_MULTIBYTE (c);
7756 char_bytes = CHAR_STRING (c, str);
7757 insert_1_both (str, 1, char_bytes, 1, 0, 0);
7758 }
7759 }
7760 else if (nbytes)
7761 insert_1 (m, nbytes, 1, 0, 0);
7762
7763 if (nlflag)
7764 {
7765 int this_bol, this_bol_byte, prev_bol, prev_bol_byte, dup;
7766 insert_1 ("\n", 1, 1, 0, 0);
7767
7768 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
7769 this_bol = PT;
7770 this_bol_byte = PT_BYTE;
7771
7772 /* See if this line duplicates the previous one.
7773 If so, combine duplicates. */
7774 if (this_bol > BEG)
7775 {
7776 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
7777 prev_bol = PT;
7778 prev_bol_byte = PT_BYTE;
7779
7780 dup = message_log_check_duplicate (prev_bol, prev_bol_byte,
7781 this_bol, this_bol_byte);
7782 if (dup)
7783 {
7784 del_range_both (prev_bol, prev_bol_byte,
7785 this_bol, this_bol_byte, 0);
7786 if (dup > 1)
7787 {
7788 char dupstr[40];
7789 int duplen;
7790
7791 /* If you change this format, don't forget to also
7792 change message_log_check_duplicate. */
7793 sprintf (dupstr, " [%d times]", dup);
7794 duplen = strlen (dupstr);
7795 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
7796 insert_1 (dupstr, duplen, 1, 0, 1);
7797 }
7798 }
7799 }
7800
7801 /* If we have more than the desired maximum number of lines
7802 in the *Messages* buffer now, delete the oldest ones.
7803 This is safe because we don't have undo in this buffer. */
7804
7805 if (NATNUMP (Vmessage_log_max))
7806 {
7807 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
7808 -XFASTINT (Vmessage_log_max) - 1, 0);
7809 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
7810 }
7811 }
7812 BEGV = XMARKER (oldbegv)->charpos;
7813 BEGV_BYTE = marker_byte_position (oldbegv);
7814
7815 if (zv_at_end)
7816 {
7817 ZV = Z;
7818 ZV_BYTE = Z_BYTE;
7819 }
7820 else
7821 {
7822 ZV = XMARKER (oldzv)->charpos;
7823 ZV_BYTE = marker_byte_position (oldzv);
7824 }
7825
7826 if (point_at_end)
7827 TEMP_SET_PT_BOTH (Z, Z_BYTE);
7828 else
7829 /* We can't do Fgoto_char (oldpoint) because it will run some
7830 Lisp code. */
7831 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
7832 XMARKER (oldpoint)->bytepos);
7833
7834 UNGCPRO;
7835 unchain_marker (XMARKER (oldpoint));
7836 unchain_marker (XMARKER (oldbegv));
7837 unchain_marker (XMARKER (oldzv));
7838
7839 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
7840 set_buffer_internal (oldbuf);
7841 if (NILP (tem))
7842 windows_or_buffers_changed = old_windows_or_buffers_changed;
7843 message_log_need_newline = !nlflag;
7844 Vdeactivate_mark = old_deactivate_mark;
7845 }
7846 }
7847
7848
7849 /* We are at the end of the buffer after just having inserted a newline.
7850 (Note: We depend on the fact we won't be crossing the gap.)
7851 Check to see if the most recent message looks a lot like the previous one.
7852 Return 0 if different, 1 if the new one should just replace it, or a
7853 value N > 1 if we should also append " [N times]". */
7854
7855 static int
7856 message_log_check_duplicate (prev_bol, prev_bol_byte, this_bol, this_bol_byte)
7857 int prev_bol, this_bol;
7858 int prev_bol_byte, this_bol_byte;
7859 {
7860 int i;
7861 int len = Z_BYTE - 1 - this_bol_byte;
7862 int seen_dots = 0;
7863 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
7864 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
7865
7866 for (i = 0; i < len; i++)
7867 {
7868 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
7869 seen_dots = 1;
7870 if (p1[i] != p2[i])
7871 return seen_dots;
7872 }
7873 p1 += len;
7874 if (*p1 == '\n')
7875 return 2;
7876 if (*p1++ == ' ' && *p1++ == '[')
7877 {
7878 int n = 0;
7879 while (*p1 >= '0' && *p1 <= '9')
7880 n = n * 10 + *p1++ - '0';
7881 if (strncmp (p1, " times]\n", 8) == 0)
7882 return n+1;
7883 }
7884 return 0;
7885 }
7886 \f
7887
7888 /* Display an echo area message M with a specified length of NBYTES
7889 bytes. The string may include null characters. If M is 0, clear
7890 out any existing message, and let the mini-buffer text show
7891 through.
7892
7893 This may GC, so the buffer M must NOT point to a Lisp string. */
7894
7895 void
7896 message2 (m, nbytes, multibyte)
7897 const char *m;
7898 int nbytes;
7899 int multibyte;
7900 {
7901 /* First flush out any partial line written with print. */
7902 message_log_maybe_newline ();
7903 if (m)
7904 message_dolog (m, nbytes, 1, multibyte);
7905 message2_nolog (m, nbytes, multibyte);
7906 }
7907
7908
7909 /* The non-logging counterpart of message2. */
7910
7911 void
7912 message2_nolog (m, nbytes, multibyte)
7913 const char *m;
7914 int nbytes, multibyte;
7915 {
7916 struct frame *sf = SELECTED_FRAME ();
7917 message_enable_multibyte = multibyte;
7918
7919 if (FRAME_INITIAL_P (sf))
7920 {
7921 if (noninteractive_need_newline)
7922 putc ('\n', stderr);
7923 noninteractive_need_newline = 0;
7924 if (m)
7925 fwrite (m, nbytes, 1, stderr);
7926 if (cursor_in_echo_area == 0)
7927 fprintf (stderr, "\n");
7928 fflush (stderr);
7929 }
7930 /* A null message buffer means that the frame hasn't really been
7931 initialized yet. Error messages get reported properly by
7932 cmd_error, so this must be just an informative message; toss it. */
7933 else if (INTERACTIVE
7934 && sf->glyphs_initialized_p
7935 && FRAME_MESSAGE_BUF (sf))
7936 {
7937 Lisp_Object mini_window;
7938 struct frame *f;
7939
7940 /* Get the frame containing the mini-buffer
7941 that the selected frame is using. */
7942 mini_window = FRAME_MINIBUF_WINDOW (sf);
7943 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
7944
7945 FRAME_SAMPLE_VISIBILITY (f);
7946 if (FRAME_VISIBLE_P (sf)
7947 && ! FRAME_VISIBLE_P (f))
7948 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
7949
7950 if (m)
7951 {
7952 set_message (m, Qnil, nbytes, multibyte);
7953 if (minibuffer_auto_raise)
7954 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
7955 }
7956 else
7957 clear_message (1, 1);
7958
7959 do_pending_window_change (0);
7960 echo_area_display (1);
7961 do_pending_window_change (0);
7962 if (FRAME_TERMINAL (f)->frame_up_to_date_hook != 0 && ! gc_in_progress)
7963 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
7964 }
7965 }
7966
7967
7968 /* Display an echo area message M with a specified length of NBYTES
7969 bytes. The string may include null characters. If M is not a
7970 string, clear out any existing message, and let the mini-buffer
7971 text show through.
7972
7973 This function cancels echoing. */
7974
7975 void
7976 message3 (m, nbytes, multibyte)
7977 Lisp_Object m;
7978 int nbytes;
7979 int multibyte;
7980 {
7981 struct gcpro gcpro1;
7982
7983 GCPRO1 (m);
7984 clear_message (1,1);
7985 cancel_echoing ();
7986
7987 /* First flush out any partial line written with print. */
7988 message_log_maybe_newline ();
7989 if (STRINGP (m))
7990 {
7991 char *buffer;
7992 USE_SAFE_ALLOCA;
7993
7994 SAFE_ALLOCA (buffer, char *, nbytes);
7995 bcopy (SDATA (m), buffer, nbytes);
7996 message_dolog (buffer, nbytes, 1, multibyte);
7997 SAFE_FREE ();
7998 }
7999 message3_nolog (m, nbytes, multibyte);
8000
8001 UNGCPRO;
8002 }
8003
8004
8005 /* The non-logging version of message3.
8006 This does not cancel echoing, because it is used for echoing.
8007 Perhaps we need to make a separate function for echoing
8008 and make this cancel echoing. */
8009
8010 void
8011 message3_nolog (m, nbytes, multibyte)
8012 Lisp_Object m;
8013 int nbytes, multibyte;
8014 {
8015 struct frame *sf = SELECTED_FRAME ();
8016 message_enable_multibyte = multibyte;
8017
8018 if (FRAME_INITIAL_P (sf))
8019 {
8020 if (noninteractive_need_newline)
8021 putc ('\n', stderr);
8022 noninteractive_need_newline = 0;
8023 if (STRINGP (m))
8024 fwrite (SDATA (m), nbytes, 1, stderr);
8025 if (cursor_in_echo_area == 0)
8026 fprintf (stderr, "\n");
8027 fflush (stderr);
8028 }
8029 /* A null message buffer means that the frame hasn't really been
8030 initialized yet. Error messages get reported properly by
8031 cmd_error, so this must be just an informative message; toss it. */
8032 else if (INTERACTIVE
8033 && sf->glyphs_initialized_p
8034 && FRAME_MESSAGE_BUF (sf))
8035 {
8036 Lisp_Object mini_window;
8037 Lisp_Object frame;
8038 struct frame *f;
8039
8040 /* Get the frame containing the mini-buffer
8041 that the selected frame is using. */
8042 mini_window = FRAME_MINIBUF_WINDOW (sf);
8043 frame = XWINDOW (mini_window)->frame;
8044 f = XFRAME (frame);
8045
8046 FRAME_SAMPLE_VISIBILITY (f);
8047 if (FRAME_VISIBLE_P (sf)
8048 && !FRAME_VISIBLE_P (f))
8049 Fmake_frame_visible (frame);
8050
8051 if (STRINGP (m) && SCHARS (m) > 0)
8052 {
8053 set_message (NULL, m, nbytes, multibyte);
8054 if (minibuffer_auto_raise)
8055 Fraise_frame (frame);
8056 /* Assume we are not echoing.
8057 (If we are, echo_now will override this.) */
8058 echo_message_buffer = Qnil;
8059 }
8060 else
8061 clear_message (1, 1);
8062
8063 do_pending_window_change (0);
8064 echo_area_display (1);
8065 do_pending_window_change (0);
8066 if (FRAME_TERMINAL (f)->frame_up_to_date_hook != 0 && ! gc_in_progress)
8067 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
8068 }
8069 }
8070
8071
8072 /* Display a null-terminated echo area message M. If M is 0, clear
8073 out any existing message, and let the mini-buffer text show through.
8074
8075 The buffer M must continue to exist until after the echo area gets
8076 cleared or some other message gets displayed there. Do not pass
8077 text that is stored in a Lisp string. Do not pass text in a buffer
8078 that was alloca'd. */
8079
8080 void
8081 message1 (m)
8082 char *m;
8083 {
8084 message2 (m, (m ? strlen (m) : 0), 0);
8085 }
8086
8087
8088 /* The non-logging counterpart of message1. */
8089
8090 void
8091 message1_nolog (m)
8092 char *m;
8093 {
8094 message2_nolog (m, (m ? strlen (m) : 0), 0);
8095 }
8096
8097 /* Display a message M which contains a single %s
8098 which gets replaced with STRING. */
8099
8100 void
8101 message_with_string (m, string, log)
8102 char *m;
8103 Lisp_Object string;
8104 int log;
8105 {
8106 CHECK_STRING (string);
8107
8108 if (noninteractive)
8109 {
8110 if (m)
8111 {
8112 if (noninteractive_need_newline)
8113 putc ('\n', stderr);
8114 noninteractive_need_newline = 0;
8115 fprintf (stderr, m, SDATA (string));
8116 if (!cursor_in_echo_area)
8117 fprintf (stderr, "\n");
8118 fflush (stderr);
8119 }
8120 }
8121 else if (INTERACTIVE)
8122 {
8123 /* The frame whose minibuffer we're going to display the message on.
8124 It may be larger than the selected frame, so we need
8125 to use its buffer, not the selected frame's buffer. */
8126 Lisp_Object mini_window;
8127 struct frame *f, *sf = SELECTED_FRAME ();
8128
8129 /* Get the frame containing the minibuffer
8130 that the selected frame is using. */
8131 mini_window = FRAME_MINIBUF_WINDOW (sf);
8132 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
8133
8134 /* A null message buffer means that the frame hasn't really been
8135 initialized yet. Error messages get reported properly by
8136 cmd_error, so this must be just an informative message; toss it. */
8137 if (FRAME_MESSAGE_BUF (f))
8138 {
8139 Lisp_Object args[2], message;
8140 struct gcpro gcpro1, gcpro2;
8141
8142 args[0] = build_string (m);
8143 args[1] = message = string;
8144 GCPRO2 (args[0], message);
8145 gcpro1.nvars = 2;
8146
8147 message = Fformat (2, args);
8148
8149 if (log)
8150 message3 (message, SBYTES (message), STRING_MULTIBYTE (message));
8151 else
8152 message3_nolog (message, SBYTES (message), STRING_MULTIBYTE (message));
8153
8154 UNGCPRO;
8155
8156 /* Print should start at the beginning of the message
8157 buffer next time. */
8158 message_buf_print = 0;
8159 }
8160 }
8161 }
8162
8163
8164 /* Dump an informative message to the minibuf. If M is 0, clear out
8165 any existing message, and let the mini-buffer text show through. */
8166
8167 /* VARARGS 1 */
8168 void
8169 message (m, a1, a2, a3)
8170 char *m;
8171 EMACS_INT a1, a2, a3;
8172 {
8173 if (noninteractive)
8174 {
8175 if (m)
8176 {
8177 if (noninteractive_need_newline)
8178 putc ('\n', stderr);
8179 noninteractive_need_newline = 0;
8180 fprintf (stderr, m, a1, a2, a3);
8181 if (cursor_in_echo_area == 0)
8182 fprintf (stderr, "\n");
8183 fflush (stderr);
8184 }
8185 }
8186 else if (INTERACTIVE)
8187 {
8188 /* The frame whose mini-buffer we're going to display the message
8189 on. It may be larger than the selected frame, so we need to
8190 use its buffer, not the selected frame's buffer. */
8191 Lisp_Object mini_window;
8192 struct frame *f, *sf = SELECTED_FRAME ();
8193
8194 /* Get the frame containing the mini-buffer
8195 that the selected frame is using. */
8196 mini_window = FRAME_MINIBUF_WINDOW (sf);
8197 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
8198
8199 /* A null message buffer means that the frame hasn't really been
8200 initialized yet. Error messages get reported properly by
8201 cmd_error, so this must be just an informative message; toss
8202 it. */
8203 if (FRAME_MESSAGE_BUF (f))
8204 {
8205 if (m)
8206 {
8207 int len;
8208 #ifdef NO_ARG_ARRAY
8209 char *a[3];
8210 a[0] = (char *) a1;
8211 a[1] = (char *) a2;
8212 a[2] = (char *) a3;
8213
8214 len = doprnt (FRAME_MESSAGE_BUF (f),
8215 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
8216 #else
8217 len = doprnt (FRAME_MESSAGE_BUF (f),
8218 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3,
8219 (char **) &a1);
8220 #endif /* NO_ARG_ARRAY */
8221
8222 message2 (FRAME_MESSAGE_BUF (f), len, 0);
8223 }
8224 else
8225 message1 (0);
8226
8227 /* Print should start at the beginning of the message
8228 buffer next time. */
8229 message_buf_print = 0;
8230 }
8231 }
8232 }
8233
8234
8235 /* The non-logging version of message. */
8236
8237 void
8238 message_nolog (m, a1, a2, a3)
8239 char *m;
8240 EMACS_INT a1, a2, a3;
8241 {
8242 Lisp_Object old_log_max;
8243 old_log_max = Vmessage_log_max;
8244 Vmessage_log_max = Qnil;
8245 message (m, a1, a2, a3);
8246 Vmessage_log_max = old_log_max;
8247 }
8248
8249
8250 /* Display the current message in the current mini-buffer. This is
8251 only called from error handlers in process.c, and is not time
8252 critical. */
8253
8254 void
8255 update_echo_area ()
8256 {
8257 if (!NILP (echo_area_buffer[0]))
8258 {
8259 Lisp_Object string;
8260 string = Fcurrent_message ();
8261 message3 (string, SBYTES (string),
8262 !NILP (current_buffer->enable_multibyte_characters));
8263 }
8264 }
8265
8266
8267 /* Make sure echo area buffers in `echo_buffers' are live.
8268 If they aren't, make new ones. */
8269
8270 static void
8271 ensure_echo_area_buffers ()
8272 {
8273 int i;
8274
8275 for (i = 0; i < 2; ++i)
8276 if (!BUFFERP (echo_buffer[i])
8277 || NILP (XBUFFER (echo_buffer[i])->name))
8278 {
8279 char name[30];
8280 Lisp_Object old_buffer;
8281 int j;
8282
8283 old_buffer = echo_buffer[i];
8284 sprintf (name, " *Echo Area %d*", i);
8285 echo_buffer[i] = Fget_buffer_create (build_string (name));
8286 XBUFFER (echo_buffer[i])->truncate_lines = Qnil;
8287 /* to force word wrap in echo area -
8288 it was decided to postpone this*/
8289 /* XBUFFER (echo_buffer[i])->word_wrap = Qt; */
8290
8291 for (j = 0; j < 2; ++j)
8292 if (EQ (old_buffer, echo_area_buffer[j]))
8293 echo_area_buffer[j] = echo_buffer[i];
8294 }
8295 }
8296
8297
8298 /* Call FN with args A1..A4 with either the current or last displayed
8299 echo_area_buffer as current buffer.
8300
8301 WHICH zero means use the current message buffer
8302 echo_area_buffer[0]. If that is nil, choose a suitable buffer
8303 from echo_buffer[] and clear it.
8304
8305 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
8306 suitable buffer from echo_buffer[] and clear it.
8307
8308 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
8309 that the current message becomes the last displayed one, make
8310 choose a suitable buffer for echo_area_buffer[0], and clear it.
8311
8312 Value is what FN returns. */
8313
8314 static int
8315 with_echo_area_buffer (w, which, fn, a1, a2, a3, a4)
8316 struct window *w;
8317 int which;
8318 int (*fn) P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
8319 EMACS_INT a1;
8320 Lisp_Object a2;
8321 EMACS_INT a3, a4;
8322 {
8323 Lisp_Object buffer;
8324 int this_one, the_other, clear_buffer_p, rc;
8325 int count = SPECPDL_INDEX ();
8326
8327 /* If buffers aren't live, make new ones. */
8328 ensure_echo_area_buffers ();
8329
8330 clear_buffer_p = 0;
8331
8332 if (which == 0)
8333 this_one = 0, the_other = 1;
8334 else if (which > 0)
8335 this_one = 1, the_other = 0;
8336 else
8337 {
8338 this_one = 0, the_other = 1;
8339 clear_buffer_p = 1;
8340
8341 /* We need a fresh one in case the current echo buffer equals
8342 the one containing the last displayed echo area message. */
8343 if (!NILP (echo_area_buffer[this_one])
8344 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
8345 echo_area_buffer[this_one] = Qnil;
8346 }
8347
8348 /* Choose a suitable buffer from echo_buffer[] is we don't
8349 have one. */
8350 if (NILP (echo_area_buffer[this_one]))
8351 {
8352 echo_area_buffer[this_one]
8353 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
8354 ? echo_buffer[the_other]
8355 : echo_buffer[this_one]);
8356 clear_buffer_p = 1;
8357 }
8358
8359 buffer = echo_area_buffer[this_one];
8360
8361 /* Don't get confused by reusing the buffer used for echoing
8362 for a different purpose. */
8363 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
8364 cancel_echoing ();
8365
8366 record_unwind_protect (unwind_with_echo_area_buffer,
8367 with_echo_area_buffer_unwind_data (w));
8368
8369 /* Make the echo area buffer current. Note that for display
8370 purposes, it is not necessary that the displayed window's buffer
8371 == current_buffer, except for text property lookup. So, let's
8372 only set that buffer temporarily here without doing a full
8373 Fset_window_buffer. We must also change w->pointm, though,
8374 because otherwise an assertions in unshow_buffer fails, and Emacs
8375 aborts. */
8376 set_buffer_internal_1 (XBUFFER (buffer));
8377 if (w)
8378 {
8379 w->buffer = buffer;
8380 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
8381 }
8382
8383 current_buffer->undo_list = Qt;
8384 current_buffer->read_only = Qnil;
8385 specbind (Qinhibit_read_only, Qt);
8386 specbind (Qinhibit_modification_hooks, Qt);
8387
8388 if (clear_buffer_p && Z > BEG)
8389 del_range (BEG, Z);
8390
8391 xassert (BEGV >= BEG);
8392 xassert (ZV <= Z && ZV >= BEGV);
8393
8394 rc = fn (a1, a2, a3, a4);
8395
8396 xassert (BEGV >= BEG);
8397 xassert (ZV <= Z && ZV >= BEGV);
8398
8399 unbind_to (count, Qnil);
8400 return rc;
8401 }
8402
8403
8404 /* Save state that should be preserved around the call to the function
8405 FN called in with_echo_area_buffer. */
8406
8407 static Lisp_Object
8408 with_echo_area_buffer_unwind_data (w)
8409 struct window *w;
8410 {
8411 int i = 0;
8412 Lisp_Object vector, tmp;
8413
8414 /* Reduce consing by keeping one vector in
8415 Vwith_echo_area_save_vector. */
8416 vector = Vwith_echo_area_save_vector;
8417 Vwith_echo_area_save_vector = Qnil;
8418
8419 if (NILP (vector))
8420 vector = Fmake_vector (make_number (7), Qnil);
8421
8422 XSETBUFFER (tmp, current_buffer); ASET (vector, i, tmp); ++i;
8423 ASET (vector, i, Vdeactivate_mark); ++i;
8424 ASET (vector, i, make_number (windows_or_buffers_changed)); ++i;
8425
8426 if (w)
8427 {
8428 XSETWINDOW (tmp, w); ASET (vector, i, tmp); ++i;
8429 ASET (vector, i, w->buffer); ++i;
8430 ASET (vector, i, make_number (XMARKER (w->pointm)->charpos)); ++i;
8431 ASET (vector, i, make_number (XMARKER (w->pointm)->bytepos)); ++i;
8432 }
8433 else
8434 {
8435 int end = i + 4;
8436 for (; i < end; ++i)
8437 ASET (vector, i, Qnil);
8438 }
8439
8440 xassert (i == ASIZE (vector));
8441 return vector;
8442 }
8443
8444
8445 /* Restore global state from VECTOR which was created by
8446 with_echo_area_buffer_unwind_data. */
8447
8448 static Lisp_Object
8449 unwind_with_echo_area_buffer (vector)
8450 Lisp_Object vector;
8451 {
8452 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
8453 Vdeactivate_mark = AREF (vector, 1);
8454 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
8455
8456 if (WINDOWP (AREF (vector, 3)))
8457 {
8458 struct window *w;
8459 Lisp_Object buffer, charpos, bytepos;
8460
8461 w = XWINDOW (AREF (vector, 3));
8462 buffer = AREF (vector, 4);
8463 charpos = AREF (vector, 5);
8464 bytepos = AREF (vector, 6);
8465
8466 w->buffer = buffer;
8467 set_marker_both (w->pointm, buffer,
8468 XFASTINT (charpos), XFASTINT (bytepos));
8469 }
8470
8471 Vwith_echo_area_save_vector = vector;
8472 return Qnil;
8473 }
8474
8475
8476 /* Set up the echo area for use by print functions. MULTIBYTE_P
8477 non-zero means we will print multibyte. */
8478
8479 void
8480 setup_echo_area_for_printing (multibyte_p)
8481 int multibyte_p;
8482 {
8483 /* If we can't find an echo area any more, exit. */
8484 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
8485 Fkill_emacs (Qnil);
8486
8487 ensure_echo_area_buffers ();
8488
8489 if (!message_buf_print)
8490 {
8491 /* A message has been output since the last time we printed.
8492 Choose a fresh echo area buffer. */
8493 if (EQ (echo_area_buffer[1], echo_buffer[0]))
8494 echo_area_buffer[0] = echo_buffer[1];
8495 else
8496 echo_area_buffer[0] = echo_buffer[0];
8497
8498 /* Switch to that buffer and clear it. */
8499 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
8500 current_buffer->truncate_lines = Qnil;
8501
8502 if (Z > BEG)
8503 {
8504 int count = SPECPDL_INDEX ();
8505 specbind (Qinhibit_read_only, Qt);
8506 /* Note that undo recording is always disabled. */
8507 del_range (BEG, Z);
8508 unbind_to (count, Qnil);
8509 }
8510 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
8511
8512 /* Set up the buffer for the multibyteness we need. */
8513 if (multibyte_p
8514 != !NILP (current_buffer->enable_multibyte_characters))
8515 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
8516
8517 /* Raise the frame containing the echo area. */
8518 if (minibuffer_auto_raise)
8519 {
8520 struct frame *sf = SELECTED_FRAME ();
8521 Lisp_Object mini_window;
8522 mini_window = FRAME_MINIBUF_WINDOW (sf);
8523 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
8524 }
8525
8526 message_log_maybe_newline ();
8527 message_buf_print = 1;
8528 }
8529 else
8530 {
8531 if (NILP (echo_area_buffer[0]))
8532 {
8533 if (EQ (echo_area_buffer[1], echo_buffer[0]))
8534 echo_area_buffer[0] = echo_buffer[1];
8535 else
8536 echo_area_buffer[0] = echo_buffer[0];
8537 }
8538
8539 if (current_buffer != XBUFFER (echo_area_buffer[0]))
8540 {
8541 /* Someone switched buffers between print requests. */
8542 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
8543 current_buffer->truncate_lines = Qnil;
8544 }
8545 }
8546 }
8547
8548
8549 /* Display an echo area message in window W. Value is non-zero if W's
8550 height is changed. If display_last_displayed_message_p is
8551 non-zero, display the message that was last displayed, otherwise
8552 display the current message. */
8553
8554 static int
8555 display_echo_area (w)
8556 struct window *w;
8557 {
8558 int i, no_message_p, window_height_changed_p, count;
8559
8560 /* Temporarily disable garbage collections while displaying the echo
8561 area. This is done because a GC can print a message itself.
8562 That message would modify the echo area buffer's contents while a
8563 redisplay of the buffer is going on, and seriously confuse
8564 redisplay. */
8565 count = inhibit_garbage_collection ();
8566
8567 /* If there is no message, we must call display_echo_area_1
8568 nevertheless because it resizes the window. But we will have to
8569 reset the echo_area_buffer in question to nil at the end because
8570 with_echo_area_buffer will sets it to an empty buffer. */
8571 i = display_last_displayed_message_p ? 1 : 0;
8572 no_message_p = NILP (echo_area_buffer[i]);
8573
8574 window_height_changed_p
8575 = with_echo_area_buffer (w, display_last_displayed_message_p,
8576 display_echo_area_1,
8577 (EMACS_INT) w, Qnil, 0, 0);
8578
8579 if (no_message_p)
8580 echo_area_buffer[i] = Qnil;
8581
8582 unbind_to (count, Qnil);
8583 return window_height_changed_p;
8584 }
8585
8586
8587 /* Helper for display_echo_area. Display the current buffer which
8588 contains the current echo area message in window W, a mini-window,
8589 a pointer to which is passed in A1. A2..A4 are currently not used.
8590 Change the height of W so that all of the message is displayed.
8591 Value is non-zero if height of W was changed. */
8592
8593 static int
8594 display_echo_area_1 (a1, a2, a3, a4)
8595 EMACS_INT a1;
8596 Lisp_Object a2;
8597 EMACS_INT a3, a4;
8598 {
8599 struct window *w = (struct window *) a1;
8600 Lisp_Object window;
8601 struct text_pos start;
8602 int window_height_changed_p = 0;
8603
8604 /* Do this before displaying, so that we have a large enough glyph
8605 matrix for the display. If we can't get enough space for the
8606 whole text, display the last N lines. That works by setting w->start. */
8607 window_height_changed_p = resize_mini_window (w, 0);
8608
8609 /* Use the starting position chosen by resize_mini_window. */
8610 SET_TEXT_POS_FROM_MARKER (start, w->start);
8611
8612 /* Display. */
8613 clear_glyph_matrix (w->desired_matrix);
8614 XSETWINDOW (window, w);
8615 try_window (window, start, 0);
8616
8617 return window_height_changed_p;
8618 }
8619
8620
8621 /* Resize the echo area window to exactly the size needed for the
8622 currently displayed message, if there is one. If a mini-buffer
8623 is active, don't shrink it. */
8624
8625 void
8626 resize_echo_area_exactly ()
8627 {
8628 if (BUFFERP (echo_area_buffer[0])
8629 && WINDOWP (echo_area_window))
8630 {
8631 struct window *w = XWINDOW (echo_area_window);
8632 int resized_p;
8633 Lisp_Object resize_exactly;
8634
8635 if (minibuf_level == 0)
8636 resize_exactly = Qt;
8637 else
8638 resize_exactly = Qnil;
8639
8640 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
8641 (EMACS_INT) w, resize_exactly, 0, 0);
8642 if (resized_p)
8643 {
8644 ++windows_or_buffers_changed;
8645 ++update_mode_lines;
8646 redisplay_internal (0);
8647 }
8648 }
8649 }
8650
8651
8652 /* Callback function for with_echo_area_buffer, when used from
8653 resize_echo_area_exactly. A1 contains a pointer to the window to
8654 resize, EXACTLY non-nil means resize the mini-window exactly to the
8655 size of the text displayed. A3 and A4 are not used. Value is what
8656 resize_mini_window returns. */
8657
8658 static int
8659 resize_mini_window_1 (a1, exactly, a3, a4)
8660 EMACS_INT a1;
8661 Lisp_Object exactly;
8662 EMACS_INT a3, a4;
8663 {
8664 return resize_mini_window ((struct window *) a1, !NILP (exactly));
8665 }
8666
8667
8668 /* Resize mini-window W to fit the size of its contents. EXACT_P
8669 means size the window exactly to the size needed. Otherwise, it's
8670 only enlarged until W's buffer is empty.
8671
8672 Set W->start to the right place to begin display. If the whole
8673 contents fit, start at the beginning. Otherwise, start so as
8674 to make the end of the contents appear. This is particularly
8675 important for y-or-n-p, but seems desirable generally.
8676
8677 Value is non-zero if the window height has been changed. */
8678
8679 int
8680 resize_mini_window (w, exact_p)
8681 struct window *w;
8682 int exact_p;
8683 {
8684 struct frame *f = XFRAME (w->frame);
8685 int window_height_changed_p = 0;
8686
8687 xassert (MINI_WINDOW_P (w));
8688
8689 /* By default, start display at the beginning. */
8690 set_marker_both (w->start, w->buffer,
8691 BUF_BEGV (XBUFFER (w->buffer)),
8692 BUF_BEGV_BYTE (XBUFFER (w->buffer)));
8693
8694 /* Don't resize windows while redisplaying a window; it would
8695 confuse redisplay functions when the size of the window they are
8696 displaying changes from under them. Such a resizing can happen,
8697 for instance, when which-func prints a long message while
8698 we are running fontification-functions. We're running these
8699 functions with safe_call which binds inhibit-redisplay to t. */
8700 if (!NILP (Vinhibit_redisplay))
8701 return 0;
8702
8703 /* Nil means don't try to resize. */
8704 if (NILP (Vresize_mini_windows)
8705 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
8706 return 0;
8707
8708 if (!FRAME_MINIBUF_ONLY_P (f))
8709 {
8710 struct it it;
8711 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
8712 int total_height = WINDOW_TOTAL_LINES (root) + WINDOW_TOTAL_LINES (w);
8713 int height, max_height;
8714 int unit = FRAME_LINE_HEIGHT (f);
8715 struct text_pos start;
8716 struct buffer *old_current_buffer = NULL;
8717
8718 if (current_buffer != XBUFFER (w->buffer))
8719 {
8720 old_current_buffer = current_buffer;
8721 set_buffer_internal (XBUFFER (w->buffer));
8722 }
8723
8724 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
8725
8726 /* Compute the max. number of lines specified by the user. */
8727 if (FLOATP (Vmax_mini_window_height))
8728 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_LINES (f);
8729 else if (INTEGERP (Vmax_mini_window_height))
8730 max_height = XINT (Vmax_mini_window_height);
8731 else
8732 max_height = total_height / 4;
8733
8734 /* Correct that max. height if it's bogus. */
8735 max_height = max (1, max_height);
8736 max_height = min (total_height, max_height);
8737
8738 /* Find out the height of the text in the window. */
8739 if (it.line_wrap == TRUNCATE)
8740 height = 1;
8741 else
8742 {
8743 last_height = 0;
8744 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
8745 if (it.max_ascent == 0 && it.max_descent == 0)
8746 height = it.current_y + last_height;
8747 else
8748 height = it.current_y + it.max_ascent + it.max_descent;
8749 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
8750 height = (height + unit - 1) / unit;
8751 }
8752
8753 /* Compute a suitable window start. */
8754 if (height > max_height)
8755 {
8756 height = max_height;
8757 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
8758 move_it_vertically_backward (&it, (height - 1) * unit);
8759 start = it.current.pos;
8760 }
8761 else
8762 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
8763 SET_MARKER_FROM_TEXT_POS (w->start, start);
8764
8765 if (EQ (Vresize_mini_windows, Qgrow_only))
8766 {
8767 /* Let it grow only, until we display an empty message, in which
8768 case the window shrinks again. */
8769 if (height > WINDOW_TOTAL_LINES (w))
8770 {
8771 int old_height = WINDOW_TOTAL_LINES (w);
8772 freeze_window_starts (f, 1);
8773 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
8774 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8775 }
8776 else if (height < WINDOW_TOTAL_LINES (w)
8777 && (exact_p || BEGV == ZV))
8778 {
8779 int old_height = WINDOW_TOTAL_LINES (w);
8780 freeze_window_starts (f, 0);
8781 shrink_mini_window (w);
8782 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8783 }
8784 }
8785 else
8786 {
8787 /* Always resize to exact size needed. */
8788 if (height > WINDOW_TOTAL_LINES (w))
8789 {
8790 int old_height = WINDOW_TOTAL_LINES (w);
8791 freeze_window_starts (f, 1);
8792 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
8793 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8794 }
8795 else if (height < WINDOW_TOTAL_LINES (w))
8796 {
8797 int old_height = WINDOW_TOTAL_LINES (w);
8798 freeze_window_starts (f, 0);
8799 shrink_mini_window (w);
8800
8801 if (height)
8802 {
8803 freeze_window_starts (f, 1);
8804 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
8805 }
8806
8807 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8808 }
8809 }
8810
8811 if (old_current_buffer)
8812 set_buffer_internal (old_current_buffer);
8813 }
8814
8815 return window_height_changed_p;
8816 }
8817
8818
8819 /* Value is the current message, a string, or nil if there is no
8820 current message. */
8821
8822 Lisp_Object
8823 current_message ()
8824 {
8825 Lisp_Object msg;
8826
8827 if (!BUFFERP (echo_area_buffer[0]))
8828 msg = Qnil;
8829 else
8830 {
8831 with_echo_area_buffer (0, 0, current_message_1,
8832 (EMACS_INT) &msg, Qnil, 0, 0);
8833 if (NILP (msg))
8834 echo_area_buffer[0] = Qnil;
8835 }
8836
8837 return msg;
8838 }
8839
8840
8841 static int
8842 current_message_1 (a1, a2, a3, a4)
8843 EMACS_INT a1;
8844 Lisp_Object a2;
8845 EMACS_INT a3, a4;
8846 {
8847 Lisp_Object *msg = (Lisp_Object *) a1;
8848
8849 if (Z > BEG)
8850 *msg = make_buffer_string (BEG, Z, 1);
8851 else
8852 *msg = Qnil;
8853 return 0;
8854 }
8855
8856
8857 /* Push the current message on Vmessage_stack for later restauration
8858 by restore_message. Value is non-zero if the current message isn't
8859 empty. This is a relatively infrequent operation, so it's not
8860 worth optimizing. */
8861
8862 int
8863 push_message ()
8864 {
8865 Lisp_Object msg;
8866 msg = current_message ();
8867 Vmessage_stack = Fcons (msg, Vmessage_stack);
8868 return STRINGP (msg);
8869 }
8870
8871
8872 /* Restore message display from the top of Vmessage_stack. */
8873
8874 void
8875 restore_message ()
8876 {
8877 Lisp_Object msg;
8878
8879 xassert (CONSP (Vmessage_stack));
8880 msg = XCAR (Vmessage_stack);
8881 if (STRINGP (msg))
8882 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
8883 else
8884 message3_nolog (msg, 0, 0);
8885 }
8886
8887
8888 /* Handler for record_unwind_protect calling pop_message. */
8889
8890 Lisp_Object
8891 pop_message_unwind (dummy)
8892 Lisp_Object dummy;
8893 {
8894 pop_message ();
8895 return Qnil;
8896 }
8897
8898 /* Pop the top-most entry off Vmessage_stack. */
8899
8900 void
8901 pop_message ()
8902 {
8903 xassert (CONSP (Vmessage_stack));
8904 Vmessage_stack = XCDR (Vmessage_stack);
8905 }
8906
8907
8908 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
8909 exits. If the stack is not empty, we have a missing pop_message
8910 somewhere. */
8911
8912 void
8913 check_message_stack ()
8914 {
8915 if (!NILP (Vmessage_stack))
8916 abort ();
8917 }
8918
8919
8920 /* Truncate to NCHARS what will be displayed in the echo area the next
8921 time we display it---but don't redisplay it now. */
8922
8923 void
8924 truncate_echo_area (nchars)
8925 int nchars;
8926 {
8927 if (nchars == 0)
8928 echo_area_buffer[0] = Qnil;
8929 /* A null message buffer means that the frame hasn't really been
8930 initialized yet. Error messages get reported properly by
8931 cmd_error, so this must be just an informative message; toss it. */
8932 else if (!noninteractive
8933 && INTERACTIVE
8934 && !NILP (echo_area_buffer[0]))
8935 {
8936 struct frame *sf = SELECTED_FRAME ();
8937 if (FRAME_MESSAGE_BUF (sf))
8938 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
8939 }
8940 }
8941
8942
8943 /* Helper function for truncate_echo_area. Truncate the current
8944 message to at most NCHARS characters. */
8945
8946 static int
8947 truncate_message_1 (nchars, a2, a3, a4)
8948 EMACS_INT nchars;
8949 Lisp_Object a2;
8950 EMACS_INT a3, a4;
8951 {
8952 if (BEG + nchars < Z)
8953 del_range (BEG + nchars, Z);
8954 if (Z == BEG)
8955 echo_area_buffer[0] = Qnil;
8956 return 0;
8957 }
8958
8959
8960 /* Set the current message to a substring of S or STRING.
8961
8962 If STRING is a Lisp string, set the message to the first NBYTES
8963 bytes from STRING. NBYTES zero means use the whole string. If
8964 STRING is multibyte, the message will be displayed multibyte.
8965
8966 If S is not null, set the message to the first LEN bytes of S. LEN
8967 zero means use the whole string. MULTIBYTE_P non-zero means S is
8968 multibyte. Display the message multibyte in that case.
8969
8970 Doesn't GC, as with_echo_area_buffer binds Qinhibit_modification_hooks
8971 to t before calling set_message_1 (which calls insert).
8972 */
8973
8974 void
8975 set_message (s, string, nbytes, multibyte_p)
8976 const char *s;
8977 Lisp_Object string;
8978 int nbytes, multibyte_p;
8979 {
8980 message_enable_multibyte
8981 = ((s && multibyte_p)
8982 || (STRINGP (string) && STRING_MULTIBYTE (string)));
8983
8984 with_echo_area_buffer (0, -1, set_message_1,
8985 (EMACS_INT) s, string, nbytes, multibyte_p);
8986 message_buf_print = 0;
8987 help_echo_showing_p = 0;
8988 }
8989
8990
8991 /* Helper function for set_message. Arguments have the same meaning
8992 as there, with A1 corresponding to S and A2 corresponding to STRING
8993 This function is called with the echo area buffer being
8994 current. */
8995
8996 static int
8997 set_message_1 (a1, a2, nbytes, multibyte_p)
8998 EMACS_INT a1;
8999 Lisp_Object a2;
9000 EMACS_INT nbytes, multibyte_p;
9001 {
9002 const char *s = (const char *) a1;
9003 Lisp_Object string = a2;
9004
9005 /* Change multibyteness of the echo buffer appropriately. */
9006 if (message_enable_multibyte
9007 != !NILP (current_buffer->enable_multibyte_characters))
9008 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
9009
9010 current_buffer->truncate_lines = message_truncate_lines ? Qt : Qnil;
9011
9012 /* Insert new message at BEG. */
9013 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
9014
9015 if (STRINGP (string))
9016 {
9017 int nchars;
9018
9019 if (nbytes == 0)
9020 nbytes = SBYTES (string);
9021 nchars = string_byte_to_char (string, nbytes);
9022
9023 /* This function takes care of single/multibyte conversion. We
9024 just have to ensure that the echo area buffer has the right
9025 setting of enable_multibyte_characters. */
9026 insert_from_string (string, 0, 0, nchars, nbytes, 1);
9027 }
9028 else if (s)
9029 {
9030 if (nbytes == 0)
9031 nbytes = strlen (s);
9032
9033 if (multibyte_p && NILP (current_buffer->enable_multibyte_characters))
9034 {
9035 /* Convert from multi-byte to single-byte. */
9036 int i, c, n;
9037 unsigned char work[1];
9038
9039 /* Convert a multibyte string to single-byte. */
9040 for (i = 0; i < nbytes; i += n)
9041 {
9042 c = string_char_and_length (s + i, &n);
9043 work[0] = (ASCII_CHAR_P (c)
9044 ? c
9045 : multibyte_char_to_unibyte (c, Qnil));
9046 insert_1_both (work, 1, 1, 1, 0, 0);
9047 }
9048 }
9049 else if (!multibyte_p
9050 && !NILP (current_buffer->enable_multibyte_characters))
9051 {
9052 /* Convert from single-byte to multi-byte. */
9053 int i, c, n;
9054 const unsigned char *msg = (const unsigned char *) s;
9055 unsigned char str[MAX_MULTIBYTE_LENGTH];
9056
9057 /* Convert a single-byte string to multibyte. */
9058 for (i = 0; i < nbytes; i++)
9059 {
9060 c = msg[i];
9061 MAKE_CHAR_MULTIBYTE (c);
9062 n = CHAR_STRING (c, str);
9063 insert_1_both (str, 1, n, 1, 0, 0);
9064 }
9065 }
9066 else
9067 insert_1 (s, nbytes, 1, 0, 0);
9068 }
9069
9070 return 0;
9071 }
9072
9073
9074 /* Clear messages. CURRENT_P non-zero means clear the current
9075 message. LAST_DISPLAYED_P non-zero means clear the message
9076 last displayed. */
9077
9078 void
9079 clear_message (current_p, last_displayed_p)
9080 int current_p, last_displayed_p;
9081 {
9082 if (current_p)
9083 {
9084 echo_area_buffer[0] = Qnil;
9085 message_cleared_p = 1;
9086 }
9087
9088 if (last_displayed_p)
9089 echo_area_buffer[1] = Qnil;
9090
9091 message_buf_print = 0;
9092 }
9093
9094 /* Clear garbaged frames.
9095
9096 This function is used where the old redisplay called
9097 redraw_garbaged_frames which in turn called redraw_frame which in
9098 turn called clear_frame. The call to clear_frame was a source of
9099 flickering. I believe a clear_frame is not necessary. It should
9100 suffice in the new redisplay to invalidate all current matrices,
9101 and ensure a complete redisplay of all windows. */
9102
9103 static void
9104 clear_garbaged_frames ()
9105 {
9106 if (frame_garbaged)
9107 {
9108 Lisp_Object tail, frame;
9109 int changed_count = 0;
9110
9111 FOR_EACH_FRAME (tail, frame)
9112 {
9113 struct frame *f = XFRAME (frame);
9114
9115 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
9116 {
9117 if (f->resized_p)
9118 {
9119 Fredraw_frame (frame);
9120 f->force_flush_display_p = 1;
9121 }
9122 clear_current_matrices (f);
9123 changed_count++;
9124 f->garbaged = 0;
9125 f->resized_p = 0;
9126 }
9127 }
9128
9129 frame_garbaged = 0;
9130 if (changed_count)
9131 ++windows_or_buffers_changed;
9132 }
9133 }
9134
9135
9136 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
9137 is non-zero update selected_frame. Value is non-zero if the
9138 mini-windows height has been changed. */
9139
9140 static int
9141 echo_area_display (update_frame_p)
9142 int update_frame_p;
9143 {
9144 Lisp_Object mini_window;
9145 struct window *w;
9146 struct frame *f;
9147 int window_height_changed_p = 0;
9148 struct frame *sf = SELECTED_FRAME ();
9149
9150 mini_window = FRAME_MINIBUF_WINDOW (sf);
9151 w = XWINDOW (mini_window);
9152 f = XFRAME (WINDOW_FRAME (w));
9153
9154 /* Don't display if frame is invisible or not yet initialized. */
9155 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
9156 return 0;
9157
9158 #ifdef HAVE_WINDOW_SYSTEM
9159 /* When Emacs starts, selected_frame may be the initial terminal
9160 frame. If we let this through, a message would be displayed on
9161 the terminal. */
9162 if (FRAME_INITIAL_P (XFRAME (selected_frame)))
9163 return 0;
9164 #endif /* HAVE_WINDOW_SYSTEM */
9165
9166 /* Redraw garbaged frames. */
9167 if (frame_garbaged)
9168 clear_garbaged_frames ();
9169
9170 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
9171 {
9172 echo_area_window = mini_window;
9173 window_height_changed_p = display_echo_area (w);
9174 w->must_be_updated_p = 1;
9175
9176 /* Update the display, unless called from redisplay_internal.
9177 Also don't update the screen during redisplay itself. The
9178 update will happen at the end of redisplay, and an update
9179 here could cause confusion. */
9180 if (update_frame_p && !redisplaying_p)
9181 {
9182 int n = 0;
9183
9184 /* If the display update has been interrupted by pending
9185 input, update mode lines in the frame. Due to the
9186 pending input, it might have been that redisplay hasn't
9187 been called, so that mode lines above the echo area are
9188 garbaged. This looks odd, so we prevent it here. */
9189 if (!display_completed)
9190 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
9191
9192 if (window_height_changed_p
9193 /* Don't do this if Emacs is shutting down. Redisplay
9194 needs to run hooks. */
9195 && !NILP (Vrun_hooks))
9196 {
9197 /* Must update other windows. Likewise as in other
9198 cases, don't let this update be interrupted by
9199 pending input. */
9200 int count = SPECPDL_INDEX ();
9201 specbind (Qredisplay_dont_pause, Qt);
9202 windows_or_buffers_changed = 1;
9203 redisplay_internal (0);
9204 unbind_to (count, Qnil);
9205 }
9206 else if (FRAME_WINDOW_P (f) && n == 0)
9207 {
9208 /* Window configuration is the same as before.
9209 Can do with a display update of the echo area,
9210 unless we displayed some mode lines. */
9211 update_single_window (w, 1);
9212 FRAME_RIF (f)->flush_display (f);
9213 }
9214 else
9215 update_frame (f, 1, 1);
9216
9217 /* If cursor is in the echo area, make sure that the next
9218 redisplay displays the minibuffer, so that the cursor will
9219 be replaced with what the minibuffer wants. */
9220 if (cursor_in_echo_area)
9221 ++windows_or_buffers_changed;
9222 }
9223 }
9224 else if (!EQ (mini_window, selected_window))
9225 windows_or_buffers_changed++;
9226
9227 /* Last displayed message is now the current message. */
9228 echo_area_buffer[1] = echo_area_buffer[0];
9229 /* Inform read_char that we're not echoing. */
9230 echo_message_buffer = Qnil;
9231
9232 /* Prevent redisplay optimization in redisplay_internal by resetting
9233 this_line_start_pos. This is done because the mini-buffer now
9234 displays the message instead of its buffer text. */
9235 if (EQ (mini_window, selected_window))
9236 CHARPOS (this_line_start_pos) = 0;
9237
9238 return window_height_changed_p;
9239 }
9240
9241
9242 \f
9243 /***********************************************************************
9244 Mode Lines and Frame Titles
9245 ***********************************************************************/
9246
9247 /* A buffer for constructing non-propertized mode-line strings and
9248 frame titles in it; allocated from the heap in init_xdisp and
9249 resized as needed in store_mode_line_noprop_char. */
9250
9251 static char *mode_line_noprop_buf;
9252
9253 /* The buffer's end, and a current output position in it. */
9254
9255 static char *mode_line_noprop_buf_end;
9256 static char *mode_line_noprop_ptr;
9257
9258 #define MODE_LINE_NOPROP_LEN(start) \
9259 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
9260
9261 static enum {
9262 MODE_LINE_DISPLAY = 0,
9263 MODE_LINE_TITLE,
9264 MODE_LINE_NOPROP,
9265 MODE_LINE_STRING
9266 } mode_line_target;
9267
9268 /* Alist that caches the results of :propertize.
9269 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
9270 static Lisp_Object mode_line_proptrans_alist;
9271
9272 /* List of strings making up the mode-line. */
9273 static Lisp_Object mode_line_string_list;
9274
9275 /* Base face property when building propertized mode line string. */
9276 static Lisp_Object mode_line_string_face;
9277 static Lisp_Object mode_line_string_face_prop;
9278
9279
9280 /* Unwind data for mode line strings */
9281
9282 static Lisp_Object Vmode_line_unwind_vector;
9283
9284 static Lisp_Object
9285 format_mode_line_unwind_data (struct buffer *obuf,
9286 Lisp_Object owin,
9287 int save_proptrans)
9288 {
9289 Lisp_Object vector, tmp;
9290
9291 /* Reduce consing by keeping one vector in
9292 Vwith_echo_area_save_vector. */
9293 vector = Vmode_line_unwind_vector;
9294 Vmode_line_unwind_vector = Qnil;
9295
9296 if (NILP (vector))
9297 vector = Fmake_vector (make_number (8), Qnil);
9298
9299 ASET (vector, 0, make_number (mode_line_target));
9300 ASET (vector, 1, make_number (MODE_LINE_NOPROP_LEN (0)));
9301 ASET (vector, 2, mode_line_string_list);
9302 ASET (vector, 3, save_proptrans ? mode_line_proptrans_alist : Qt);
9303 ASET (vector, 4, mode_line_string_face);
9304 ASET (vector, 5, mode_line_string_face_prop);
9305
9306 if (obuf)
9307 XSETBUFFER (tmp, obuf);
9308 else
9309 tmp = Qnil;
9310 ASET (vector, 6, tmp);
9311 ASET (vector, 7, owin);
9312
9313 return vector;
9314 }
9315
9316 static Lisp_Object
9317 unwind_format_mode_line (vector)
9318 Lisp_Object vector;
9319 {
9320 mode_line_target = XINT (AREF (vector, 0));
9321 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
9322 mode_line_string_list = AREF (vector, 2);
9323 if (! EQ (AREF (vector, 3), Qt))
9324 mode_line_proptrans_alist = AREF (vector, 3);
9325 mode_line_string_face = AREF (vector, 4);
9326 mode_line_string_face_prop = AREF (vector, 5);
9327
9328 if (!NILP (AREF (vector, 7)))
9329 /* Select window before buffer, since it may change the buffer. */
9330 Fselect_window (AREF (vector, 7), Qt);
9331
9332 if (!NILP (AREF (vector, 6)))
9333 {
9334 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
9335 ASET (vector, 6, Qnil);
9336 }
9337
9338 Vmode_line_unwind_vector = vector;
9339 return Qnil;
9340 }
9341
9342
9343 /* Store a single character C for the frame title in mode_line_noprop_buf.
9344 Re-allocate mode_line_noprop_buf if necessary. */
9345
9346 static void
9347 #ifdef PROTOTYPES
9348 store_mode_line_noprop_char (char c)
9349 #else
9350 store_mode_line_noprop_char (c)
9351 char c;
9352 #endif
9353 {
9354 /* If output position has reached the end of the allocated buffer,
9355 double the buffer's size. */
9356 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
9357 {
9358 int len = MODE_LINE_NOPROP_LEN (0);
9359 int new_size = 2 * len * sizeof *mode_line_noprop_buf;
9360 mode_line_noprop_buf = (char *) xrealloc (mode_line_noprop_buf, new_size);
9361 mode_line_noprop_buf_end = mode_line_noprop_buf + new_size;
9362 mode_line_noprop_ptr = mode_line_noprop_buf + len;
9363 }
9364
9365 *mode_line_noprop_ptr++ = c;
9366 }
9367
9368
9369 /* Store part of a frame title in mode_line_noprop_buf, beginning at
9370 mode_line_noprop_ptr. STR is the string to store. Do not copy
9371 characters that yield more columns than PRECISION; PRECISION <= 0
9372 means copy the whole string. Pad with spaces until FIELD_WIDTH
9373 number of characters have been copied; FIELD_WIDTH <= 0 means don't
9374 pad. Called from display_mode_element when it is used to build a
9375 frame title. */
9376
9377 static int
9378 store_mode_line_noprop (str, field_width, precision)
9379 const unsigned char *str;
9380 int field_width, precision;
9381 {
9382 int n = 0;
9383 int dummy, nbytes;
9384
9385 /* Copy at most PRECISION chars from STR. */
9386 nbytes = strlen (str);
9387 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
9388 while (nbytes--)
9389 store_mode_line_noprop_char (*str++);
9390
9391 /* Fill up with spaces until FIELD_WIDTH reached. */
9392 while (field_width > 0
9393 && n < field_width)
9394 {
9395 store_mode_line_noprop_char (' ');
9396 ++n;
9397 }
9398
9399 return n;
9400 }
9401
9402 /***********************************************************************
9403 Frame Titles
9404 ***********************************************************************/
9405
9406 #ifdef HAVE_WINDOW_SYSTEM
9407
9408 /* Set the title of FRAME, if it has changed. The title format is
9409 Vicon_title_format if FRAME is iconified, otherwise it is
9410 frame_title_format. */
9411
9412 static void
9413 x_consider_frame_title (frame)
9414 Lisp_Object frame;
9415 {
9416 struct frame *f = XFRAME (frame);
9417
9418 if (FRAME_WINDOW_P (f)
9419 || FRAME_MINIBUF_ONLY_P (f)
9420 || f->explicit_name)
9421 {
9422 /* Do we have more than one visible frame on this X display? */
9423 Lisp_Object tail;
9424 Lisp_Object fmt;
9425 int title_start;
9426 char *title;
9427 int len;
9428 struct it it;
9429 int count = SPECPDL_INDEX ();
9430
9431 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
9432 {
9433 Lisp_Object other_frame = XCAR (tail);
9434 struct frame *tf = XFRAME (other_frame);
9435
9436 if (tf != f
9437 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
9438 && !FRAME_MINIBUF_ONLY_P (tf)
9439 && !EQ (other_frame, tip_frame)
9440 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
9441 break;
9442 }
9443
9444 /* Set global variable indicating that multiple frames exist. */
9445 multiple_frames = CONSP (tail);
9446
9447 /* Switch to the buffer of selected window of the frame. Set up
9448 mode_line_target so that display_mode_element will output into
9449 mode_line_noprop_buf; then display the title. */
9450 record_unwind_protect (unwind_format_mode_line,
9451 format_mode_line_unwind_data
9452 (current_buffer, selected_window, 0));
9453
9454 Fselect_window (f->selected_window, Qt);
9455 set_buffer_internal_1 (XBUFFER (XWINDOW (f->selected_window)->buffer));
9456 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
9457
9458 mode_line_target = MODE_LINE_TITLE;
9459 title_start = MODE_LINE_NOPROP_LEN (0);
9460 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
9461 NULL, DEFAULT_FACE_ID);
9462 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
9463 len = MODE_LINE_NOPROP_LEN (title_start);
9464 title = mode_line_noprop_buf + title_start;
9465 unbind_to (count, Qnil);
9466
9467 /* Set the title only if it's changed. This avoids consing in
9468 the common case where it hasn't. (If it turns out that we've
9469 already wasted too much time by walking through the list with
9470 display_mode_element, then we might need to optimize at a
9471 higher level than this.) */
9472 if (! STRINGP (f->name)
9473 || SBYTES (f->name) != len
9474 || bcmp (title, SDATA (f->name), len) != 0)
9475 {
9476 #ifdef HAVE_NS
9477 if (FRAME_NS_P (f))
9478 {
9479 if (!MINI_WINDOW_P(XWINDOW(f->selected_window)))
9480 {
9481 if (EQ (fmt, Qt))
9482 ns_set_name_as_filename (f);
9483 else
9484 x_implicitly_set_name (f, make_string(title, len),
9485 Qnil);
9486 }
9487 }
9488 else
9489 #endif
9490 x_implicitly_set_name (f, make_string (title, len), Qnil);
9491 }
9492 #ifdef HAVE_NS
9493 if (FRAME_NS_P (f))
9494 {
9495 /* do this also for frames with explicit names */
9496 ns_implicitly_set_icon_type(f);
9497 ns_set_doc_edited(f, Fbuffer_modified_p
9498 (XWINDOW (f->selected_window)->buffer), Qnil);
9499 }
9500 #endif
9501 }
9502 }
9503
9504 #endif /* not HAVE_WINDOW_SYSTEM */
9505
9506
9507
9508 \f
9509 /***********************************************************************
9510 Menu Bars
9511 ***********************************************************************/
9512
9513
9514 /* Prepare for redisplay by updating menu-bar item lists when
9515 appropriate. This can call eval. */
9516
9517 void
9518 prepare_menu_bars ()
9519 {
9520 int all_windows;
9521 struct gcpro gcpro1, gcpro2;
9522 struct frame *f;
9523 Lisp_Object tooltip_frame;
9524
9525 #ifdef HAVE_WINDOW_SYSTEM
9526 tooltip_frame = tip_frame;
9527 #else
9528 tooltip_frame = Qnil;
9529 #endif
9530
9531 /* Update all frame titles based on their buffer names, etc. We do
9532 this before the menu bars so that the buffer-menu will show the
9533 up-to-date frame titles. */
9534 #ifdef HAVE_WINDOW_SYSTEM
9535 if (windows_or_buffers_changed || update_mode_lines)
9536 {
9537 Lisp_Object tail, frame;
9538
9539 FOR_EACH_FRAME (tail, frame)
9540 {
9541 f = XFRAME (frame);
9542 if (!EQ (frame, tooltip_frame)
9543 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
9544 x_consider_frame_title (frame);
9545 }
9546 }
9547 #endif /* HAVE_WINDOW_SYSTEM */
9548
9549 /* Update the menu bar item lists, if appropriate. This has to be
9550 done before any actual redisplay or generation of display lines. */
9551 all_windows = (update_mode_lines
9552 || buffer_shared > 1
9553 || windows_or_buffers_changed);
9554 if (all_windows)
9555 {
9556 Lisp_Object tail, frame;
9557 int count = SPECPDL_INDEX ();
9558 /* 1 means that update_menu_bar has run its hooks
9559 so any further calls to update_menu_bar shouldn't do so again. */
9560 int menu_bar_hooks_run = 0;
9561
9562 record_unwind_save_match_data ();
9563
9564 FOR_EACH_FRAME (tail, frame)
9565 {
9566 f = XFRAME (frame);
9567
9568 /* Ignore tooltip frame. */
9569 if (EQ (frame, tooltip_frame))
9570 continue;
9571
9572 /* If a window on this frame changed size, report that to
9573 the user and clear the size-change flag. */
9574 if (FRAME_WINDOW_SIZES_CHANGED (f))
9575 {
9576 Lisp_Object functions;
9577
9578 /* Clear flag first in case we get an error below. */
9579 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
9580 functions = Vwindow_size_change_functions;
9581 GCPRO2 (tail, functions);
9582
9583 while (CONSP (functions))
9584 {
9585 if (!EQ (XCAR (functions), Qt))
9586 call1 (XCAR (functions), frame);
9587 functions = XCDR (functions);
9588 }
9589 UNGCPRO;
9590 }
9591
9592 GCPRO1 (tail);
9593 menu_bar_hooks_run = update_menu_bar (f, 0, menu_bar_hooks_run);
9594 #ifdef HAVE_WINDOW_SYSTEM
9595 update_tool_bar (f, 0);
9596 #endif
9597 UNGCPRO;
9598 }
9599
9600 unbind_to (count, Qnil);
9601 }
9602 else
9603 {
9604 struct frame *sf = SELECTED_FRAME ();
9605 update_menu_bar (sf, 1, 0);
9606 #ifdef HAVE_WINDOW_SYSTEM
9607 update_tool_bar (sf, 1);
9608 #endif
9609 }
9610
9611 /* Motif needs this. See comment in xmenu.c. Turn it off when
9612 pending_menu_activation is not defined. */
9613 #ifdef USE_X_TOOLKIT
9614 pending_menu_activation = 0;
9615 #endif
9616 }
9617
9618
9619 /* Update the menu bar item list for frame F. This has to be done
9620 before we start to fill in any display lines, because it can call
9621 eval.
9622
9623 If SAVE_MATCH_DATA is non-zero, we must save and restore it here.
9624
9625 If HOOKS_RUN is 1, that means a previous call to update_menu_bar
9626 already ran the menu bar hooks for this redisplay, so there
9627 is no need to run them again. The return value is the
9628 updated value of this flag, to pass to the next call. */
9629
9630 static int
9631 update_menu_bar (f, save_match_data, hooks_run)
9632 struct frame *f;
9633 int save_match_data;
9634 int hooks_run;
9635 {
9636 Lisp_Object window;
9637 register struct window *w;
9638
9639 /* If called recursively during a menu update, do nothing. This can
9640 happen when, for instance, an activate-menubar-hook causes a
9641 redisplay. */
9642 if (inhibit_menubar_update)
9643 return hooks_run;
9644
9645 window = FRAME_SELECTED_WINDOW (f);
9646 w = XWINDOW (window);
9647
9648 if (FRAME_WINDOW_P (f)
9649 ?
9650 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
9651 || defined (HAVE_NS) || defined (USE_GTK)
9652 FRAME_EXTERNAL_MENU_BAR (f)
9653 #else
9654 FRAME_MENU_BAR_LINES (f) > 0
9655 #endif
9656 : FRAME_MENU_BAR_LINES (f) > 0)
9657 {
9658 /* If the user has switched buffers or windows, we need to
9659 recompute to reflect the new bindings. But we'll
9660 recompute when update_mode_lines is set too; that means
9661 that people can use force-mode-line-update to request
9662 that the menu bar be recomputed. The adverse effect on
9663 the rest of the redisplay algorithm is about the same as
9664 windows_or_buffers_changed anyway. */
9665 if (windows_or_buffers_changed
9666 /* This used to test w->update_mode_line, but we believe
9667 there is no need to recompute the menu in that case. */
9668 || update_mode_lines
9669 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
9670 < BUF_MODIFF (XBUFFER (w->buffer)))
9671 != !NILP (w->last_had_star))
9672 || ((!NILP (Vtransient_mark_mode)
9673 && !NILP (XBUFFER (w->buffer)->mark_active))
9674 != !NILP (w->region_showing)))
9675 {
9676 struct buffer *prev = current_buffer;
9677 int count = SPECPDL_INDEX ();
9678
9679 specbind (Qinhibit_menubar_update, Qt);
9680
9681 set_buffer_internal_1 (XBUFFER (w->buffer));
9682 if (save_match_data)
9683 record_unwind_save_match_data ();
9684 if (NILP (Voverriding_local_map_menu_flag))
9685 {
9686 specbind (Qoverriding_terminal_local_map, Qnil);
9687 specbind (Qoverriding_local_map, Qnil);
9688 }
9689
9690 if (!hooks_run)
9691 {
9692 /* Run the Lucid hook. */
9693 safe_run_hooks (Qactivate_menubar_hook);
9694
9695 /* If it has changed current-menubar from previous value,
9696 really recompute the menu-bar from the value. */
9697 if (! NILP (Vlucid_menu_bar_dirty_flag))
9698 call0 (Qrecompute_lucid_menubar);
9699
9700 safe_run_hooks (Qmenu_bar_update_hook);
9701
9702 hooks_run = 1;
9703 }
9704
9705 XSETFRAME (Vmenu_updating_frame, f);
9706 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
9707
9708 /* Redisplay the menu bar in case we changed it. */
9709 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
9710 || defined (HAVE_NS) || defined (USE_GTK)
9711 if (FRAME_WINDOW_P (f))
9712 {
9713 #if defined (HAVE_NS)
9714 /* All frames on Mac OS share the same menubar. So only
9715 the selected frame should be allowed to set it. */
9716 if (f == SELECTED_FRAME ())
9717 #endif
9718 set_frame_menubar (f, 0, 0);
9719 }
9720 else
9721 /* On a terminal screen, the menu bar is an ordinary screen
9722 line, and this makes it get updated. */
9723 w->update_mode_line = Qt;
9724 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
9725 /* In the non-toolkit version, the menu bar is an ordinary screen
9726 line, and this makes it get updated. */
9727 w->update_mode_line = Qt;
9728 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
9729
9730 unbind_to (count, Qnil);
9731 set_buffer_internal_1 (prev);
9732 }
9733 }
9734
9735 return hooks_run;
9736 }
9737
9738
9739 \f
9740 /***********************************************************************
9741 Output Cursor
9742 ***********************************************************************/
9743
9744 #ifdef HAVE_WINDOW_SYSTEM
9745
9746 /* EXPORT:
9747 Nominal cursor position -- where to draw output.
9748 HPOS and VPOS are window relative glyph matrix coordinates.
9749 X and Y are window relative pixel coordinates. */
9750
9751 struct cursor_pos output_cursor;
9752
9753
9754 /* EXPORT:
9755 Set the global variable output_cursor to CURSOR. All cursor
9756 positions are relative to updated_window. */
9757
9758 void
9759 set_output_cursor (cursor)
9760 struct cursor_pos *cursor;
9761 {
9762 output_cursor.hpos = cursor->hpos;
9763 output_cursor.vpos = cursor->vpos;
9764 output_cursor.x = cursor->x;
9765 output_cursor.y = cursor->y;
9766 }
9767
9768
9769 /* EXPORT for RIF:
9770 Set a nominal cursor position.
9771
9772 HPOS and VPOS are column/row positions in a window glyph matrix. X
9773 and Y are window text area relative pixel positions.
9774
9775 If this is done during an update, updated_window will contain the
9776 window that is being updated and the position is the future output
9777 cursor position for that window. If updated_window is null, use
9778 selected_window and display the cursor at the given position. */
9779
9780 void
9781 x_cursor_to (vpos, hpos, y, x)
9782 int vpos, hpos, y, x;
9783 {
9784 struct window *w;
9785
9786 /* If updated_window is not set, work on selected_window. */
9787 if (updated_window)
9788 w = updated_window;
9789 else
9790 w = XWINDOW (selected_window);
9791
9792 /* Set the output cursor. */
9793 output_cursor.hpos = hpos;
9794 output_cursor.vpos = vpos;
9795 output_cursor.x = x;
9796 output_cursor.y = y;
9797
9798 /* If not called as part of an update, really display the cursor.
9799 This will also set the cursor position of W. */
9800 if (updated_window == NULL)
9801 {
9802 BLOCK_INPUT;
9803 display_and_set_cursor (w, 1, hpos, vpos, x, y);
9804 if (FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
9805 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (SELECTED_FRAME ());
9806 UNBLOCK_INPUT;
9807 }
9808 }
9809
9810 #endif /* HAVE_WINDOW_SYSTEM */
9811
9812 \f
9813 /***********************************************************************
9814 Tool-bars
9815 ***********************************************************************/
9816
9817 #ifdef HAVE_WINDOW_SYSTEM
9818
9819 /* Where the mouse was last time we reported a mouse event. */
9820
9821 FRAME_PTR last_mouse_frame;
9822
9823 /* Tool-bar item index of the item on which a mouse button was pressed
9824 or -1. */
9825
9826 int last_tool_bar_item;
9827
9828
9829 static Lisp_Object
9830 update_tool_bar_unwind (frame)
9831 Lisp_Object frame;
9832 {
9833 selected_frame = frame;
9834 return Qnil;
9835 }
9836
9837 /* Update the tool-bar item list for frame F. This has to be done
9838 before we start to fill in any display lines. Called from
9839 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
9840 and restore it here. */
9841
9842 static void
9843 update_tool_bar (f, save_match_data)
9844 struct frame *f;
9845 int save_match_data;
9846 {
9847 #if defined (USE_GTK) || defined (HAVE_NS)
9848 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
9849 #else
9850 int do_update = WINDOWP (f->tool_bar_window)
9851 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0;
9852 #endif
9853
9854 if (do_update)
9855 {
9856 Lisp_Object window;
9857 struct window *w;
9858
9859 window = FRAME_SELECTED_WINDOW (f);
9860 w = XWINDOW (window);
9861
9862 /* If the user has switched buffers or windows, we need to
9863 recompute to reflect the new bindings. But we'll
9864 recompute when update_mode_lines is set too; that means
9865 that people can use force-mode-line-update to request
9866 that the menu bar be recomputed. The adverse effect on
9867 the rest of the redisplay algorithm is about the same as
9868 windows_or_buffers_changed anyway. */
9869 if (windows_or_buffers_changed
9870 || !NILP (w->update_mode_line)
9871 || update_mode_lines
9872 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
9873 < BUF_MODIFF (XBUFFER (w->buffer)))
9874 != !NILP (w->last_had_star))
9875 || ((!NILP (Vtransient_mark_mode)
9876 && !NILP (XBUFFER (w->buffer)->mark_active))
9877 != !NILP (w->region_showing)))
9878 {
9879 struct buffer *prev = current_buffer;
9880 int count = SPECPDL_INDEX ();
9881 Lisp_Object frame, new_tool_bar;
9882 int new_n_tool_bar;
9883 struct gcpro gcpro1;
9884
9885 /* Set current_buffer to the buffer of the selected
9886 window of the frame, so that we get the right local
9887 keymaps. */
9888 set_buffer_internal_1 (XBUFFER (w->buffer));
9889
9890 /* Save match data, if we must. */
9891 if (save_match_data)
9892 record_unwind_save_match_data ();
9893
9894 /* Make sure that we don't accidentally use bogus keymaps. */
9895 if (NILP (Voverriding_local_map_menu_flag))
9896 {
9897 specbind (Qoverriding_terminal_local_map, Qnil);
9898 specbind (Qoverriding_local_map, Qnil);
9899 }
9900
9901 GCPRO1 (new_tool_bar);
9902
9903 /* We must temporarily set the selected frame to this frame
9904 before calling tool_bar_items, because the calculation of
9905 the tool-bar keymap uses the selected frame (see
9906 `tool-bar-make-keymap' in tool-bar.el). */
9907 record_unwind_protect (update_tool_bar_unwind, selected_frame);
9908 XSETFRAME (frame, f);
9909 selected_frame = frame;
9910
9911 /* Build desired tool-bar items from keymaps. */
9912 new_tool_bar = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
9913 &new_n_tool_bar);
9914
9915 /* Redisplay the tool-bar if we changed it. */
9916 if (new_n_tool_bar != f->n_tool_bar_items
9917 || NILP (Fequal (new_tool_bar, f->tool_bar_items)))
9918 {
9919 /* Redisplay that happens asynchronously due to an expose event
9920 may access f->tool_bar_items. Make sure we update both
9921 variables within BLOCK_INPUT so no such event interrupts. */
9922 BLOCK_INPUT;
9923 f->tool_bar_items = new_tool_bar;
9924 f->n_tool_bar_items = new_n_tool_bar;
9925 w->update_mode_line = Qt;
9926 UNBLOCK_INPUT;
9927 }
9928
9929 UNGCPRO;
9930
9931 unbind_to (count, Qnil);
9932 set_buffer_internal_1 (prev);
9933 }
9934 }
9935 }
9936
9937
9938 /* Set F->desired_tool_bar_string to a Lisp string representing frame
9939 F's desired tool-bar contents. F->tool_bar_items must have
9940 been set up previously by calling prepare_menu_bars. */
9941
9942 static void
9943 build_desired_tool_bar_string (f)
9944 struct frame *f;
9945 {
9946 int i, size, size_needed;
9947 struct gcpro gcpro1, gcpro2, gcpro3;
9948 Lisp_Object image, plist, props;
9949
9950 image = plist = props = Qnil;
9951 GCPRO3 (image, plist, props);
9952
9953 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
9954 Otherwise, make a new string. */
9955
9956 /* The size of the string we might be able to reuse. */
9957 size = (STRINGP (f->desired_tool_bar_string)
9958 ? SCHARS (f->desired_tool_bar_string)
9959 : 0);
9960
9961 /* We need one space in the string for each image. */
9962 size_needed = f->n_tool_bar_items;
9963
9964 /* Reuse f->desired_tool_bar_string, if possible. */
9965 if (size < size_needed || NILP (f->desired_tool_bar_string))
9966 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
9967 make_number (' '));
9968 else
9969 {
9970 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
9971 Fremove_text_properties (make_number (0), make_number (size),
9972 props, f->desired_tool_bar_string);
9973 }
9974
9975 /* Put a `display' property on the string for the images to display,
9976 put a `menu_item' property on tool-bar items with a value that
9977 is the index of the item in F's tool-bar item vector. */
9978 for (i = 0; i < f->n_tool_bar_items; ++i)
9979 {
9980 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
9981
9982 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
9983 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
9984 int hmargin, vmargin, relief, idx, end;
9985 extern Lisp_Object QCrelief, QCmargin, QCconversion;
9986
9987 /* If image is a vector, choose the image according to the
9988 button state. */
9989 image = PROP (TOOL_BAR_ITEM_IMAGES);
9990 if (VECTORP (image))
9991 {
9992 if (enabled_p)
9993 idx = (selected_p
9994 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
9995 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
9996 else
9997 idx = (selected_p
9998 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
9999 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
10000
10001 xassert (ASIZE (image) >= idx);
10002 image = AREF (image, idx);
10003 }
10004 else
10005 idx = -1;
10006
10007 /* Ignore invalid image specifications. */
10008 if (!valid_image_p (image))
10009 continue;
10010
10011 /* Display the tool-bar button pressed, or depressed. */
10012 plist = Fcopy_sequence (XCDR (image));
10013
10014 /* Compute margin and relief to draw. */
10015 relief = (tool_bar_button_relief >= 0
10016 ? tool_bar_button_relief
10017 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
10018 hmargin = vmargin = relief;
10019
10020 if (INTEGERP (Vtool_bar_button_margin)
10021 && XINT (Vtool_bar_button_margin) > 0)
10022 {
10023 hmargin += XFASTINT (Vtool_bar_button_margin);
10024 vmargin += XFASTINT (Vtool_bar_button_margin);
10025 }
10026 else if (CONSP (Vtool_bar_button_margin))
10027 {
10028 if (INTEGERP (XCAR (Vtool_bar_button_margin))
10029 && XINT (XCAR (Vtool_bar_button_margin)) > 0)
10030 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
10031
10032 if (INTEGERP (XCDR (Vtool_bar_button_margin))
10033 && XINT (XCDR (Vtool_bar_button_margin)) > 0)
10034 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
10035 }
10036
10037 if (auto_raise_tool_bar_buttons_p)
10038 {
10039 /* Add a `:relief' property to the image spec if the item is
10040 selected. */
10041 if (selected_p)
10042 {
10043 plist = Fplist_put (plist, QCrelief, make_number (-relief));
10044 hmargin -= relief;
10045 vmargin -= relief;
10046 }
10047 }
10048 else
10049 {
10050 /* If image is selected, display it pressed, i.e. with a
10051 negative relief. If it's not selected, display it with a
10052 raised relief. */
10053 plist = Fplist_put (plist, QCrelief,
10054 (selected_p
10055 ? make_number (-relief)
10056 : make_number (relief)));
10057 hmargin -= relief;
10058 vmargin -= relief;
10059 }
10060
10061 /* Put a margin around the image. */
10062 if (hmargin || vmargin)
10063 {
10064 if (hmargin == vmargin)
10065 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
10066 else
10067 plist = Fplist_put (plist, QCmargin,
10068 Fcons (make_number (hmargin),
10069 make_number (vmargin)));
10070 }
10071
10072 /* If button is not enabled, and we don't have special images
10073 for the disabled state, make the image appear disabled by
10074 applying an appropriate algorithm to it. */
10075 if (!enabled_p && idx < 0)
10076 plist = Fplist_put (plist, QCconversion, Qdisabled);
10077
10078 /* Put a `display' text property on the string for the image to
10079 display. Put a `menu-item' property on the string that gives
10080 the start of this item's properties in the tool-bar items
10081 vector. */
10082 image = Fcons (Qimage, plist);
10083 props = list4 (Qdisplay, image,
10084 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
10085
10086 /* Let the last image hide all remaining spaces in the tool bar
10087 string. The string can be longer than needed when we reuse a
10088 previous string. */
10089 if (i + 1 == f->n_tool_bar_items)
10090 end = SCHARS (f->desired_tool_bar_string);
10091 else
10092 end = i + 1;
10093 Fadd_text_properties (make_number (i), make_number (end),
10094 props, f->desired_tool_bar_string);
10095 #undef PROP
10096 }
10097
10098 UNGCPRO;
10099 }
10100
10101
10102 /* Display one line of the tool-bar of frame IT->f.
10103
10104 HEIGHT specifies the desired height of the tool-bar line.
10105 If the actual height of the glyph row is less than HEIGHT, the
10106 row's height is increased to HEIGHT, and the icons are centered
10107 vertically in the new height.
10108
10109 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
10110 count a final empty row in case the tool-bar width exactly matches
10111 the window width.
10112 */
10113
10114 static void
10115 display_tool_bar_line (it, height)
10116 struct it *it;
10117 int height;
10118 {
10119 struct glyph_row *row = it->glyph_row;
10120 int max_x = it->last_visible_x;
10121 struct glyph *last;
10122
10123 prepare_desired_row (row);
10124 row->y = it->current_y;
10125
10126 /* Note that this isn't made use of if the face hasn't a box,
10127 so there's no need to check the face here. */
10128 it->start_of_box_run_p = 1;
10129
10130 while (it->current_x < max_x)
10131 {
10132 int x, n_glyphs_before, i, nglyphs;
10133 struct it it_before;
10134
10135 /* Get the next display element. */
10136 if (!get_next_display_element (it))
10137 {
10138 /* Don't count empty row if we are counting needed tool-bar lines. */
10139 if (height < 0 && !it->hpos)
10140 return;
10141 break;
10142 }
10143
10144 /* Produce glyphs. */
10145 n_glyphs_before = row->used[TEXT_AREA];
10146 it_before = *it;
10147
10148 PRODUCE_GLYPHS (it);
10149
10150 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
10151 i = 0;
10152 x = it_before.current_x;
10153 while (i < nglyphs)
10154 {
10155 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
10156
10157 if (x + glyph->pixel_width > max_x)
10158 {
10159 /* Glyph doesn't fit on line. Backtrack. */
10160 row->used[TEXT_AREA] = n_glyphs_before;
10161 *it = it_before;
10162 /* If this is the only glyph on this line, it will never fit on the
10163 toolbar, so skip it. But ensure there is at least one glyph,
10164 so we don't accidentally disable the tool-bar. */
10165 if (n_glyphs_before == 0
10166 && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
10167 break;
10168 goto out;
10169 }
10170
10171 ++it->hpos;
10172 x += glyph->pixel_width;
10173 ++i;
10174 }
10175
10176 /* Stop at line ends. */
10177 if (ITERATOR_AT_END_OF_LINE_P (it))
10178 break;
10179
10180 set_iterator_to_next (it, 1);
10181 }
10182
10183 out:;
10184
10185 row->displays_text_p = row->used[TEXT_AREA] != 0;
10186
10187 /* Use default face for the border below the tool bar.
10188
10189 FIXME: When auto-resize-tool-bars is grow-only, there is
10190 no additional border below the possibly empty tool-bar lines.
10191 So to make the extra empty lines look "normal", we have to
10192 use the tool-bar face for the border too. */
10193 if (!row->displays_text_p && !EQ (Vauto_resize_tool_bars, Qgrow_only))
10194 it->face_id = DEFAULT_FACE_ID;
10195
10196 extend_face_to_end_of_line (it);
10197 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
10198 last->right_box_line_p = 1;
10199 if (last == row->glyphs[TEXT_AREA])
10200 last->left_box_line_p = 1;
10201
10202 /* Make line the desired height and center it vertically. */
10203 if ((height -= it->max_ascent + it->max_descent) > 0)
10204 {
10205 /* Don't add more than one line height. */
10206 height %= FRAME_LINE_HEIGHT (it->f);
10207 it->max_ascent += height / 2;
10208 it->max_descent += (height + 1) / 2;
10209 }
10210
10211 compute_line_metrics (it);
10212
10213 /* If line is empty, make it occupy the rest of the tool-bar. */
10214 if (!row->displays_text_p)
10215 {
10216 row->height = row->phys_height = it->last_visible_y - row->y;
10217 row->visible_height = row->height;
10218 row->ascent = row->phys_ascent = 0;
10219 row->extra_line_spacing = 0;
10220 }
10221
10222 row->full_width_p = 1;
10223 row->continued_p = 0;
10224 row->truncated_on_left_p = 0;
10225 row->truncated_on_right_p = 0;
10226
10227 it->current_x = it->hpos = 0;
10228 it->current_y += row->height;
10229 ++it->vpos;
10230 ++it->glyph_row;
10231 }
10232
10233
10234 /* Max tool-bar height. */
10235
10236 #define MAX_FRAME_TOOL_BAR_HEIGHT(f) \
10237 ((FRAME_LINE_HEIGHT (f) * FRAME_LINES (f)))
10238
10239 /* Value is the number of screen lines needed to make all tool-bar
10240 items of frame F visible. The number of actual rows needed is
10241 returned in *N_ROWS if non-NULL. */
10242
10243 static int
10244 tool_bar_lines_needed (f, n_rows)
10245 struct frame *f;
10246 int *n_rows;
10247 {
10248 struct window *w = XWINDOW (f->tool_bar_window);
10249 struct it it;
10250 /* tool_bar_lines_needed is called from redisplay_tool_bar after building
10251 the desired matrix, so use (unused) mode-line row as temporary row to
10252 avoid destroying the first tool-bar row. */
10253 struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
10254
10255 /* Initialize an iterator for iteration over
10256 F->desired_tool_bar_string in the tool-bar window of frame F. */
10257 init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
10258 it.first_visible_x = 0;
10259 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
10260 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
10261
10262 while (!ITERATOR_AT_END_P (&it))
10263 {
10264 clear_glyph_row (temp_row);
10265 it.glyph_row = temp_row;
10266 display_tool_bar_line (&it, -1);
10267 }
10268 clear_glyph_row (temp_row);
10269
10270 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
10271 if (n_rows)
10272 *n_rows = it.vpos > 0 ? it.vpos : -1;
10273
10274 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
10275 }
10276
10277
10278 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
10279 0, 1, 0,
10280 doc: /* Return the number of lines occupied by the tool bar of FRAME. */)
10281 (frame)
10282 Lisp_Object frame;
10283 {
10284 struct frame *f;
10285 struct window *w;
10286 int nlines = 0;
10287
10288 if (NILP (frame))
10289 frame = selected_frame;
10290 else
10291 CHECK_FRAME (frame);
10292 f = XFRAME (frame);
10293
10294 if (WINDOWP (f->tool_bar_window)
10295 || (w = XWINDOW (f->tool_bar_window),
10296 WINDOW_TOTAL_LINES (w) > 0))
10297 {
10298 update_tool_bar (f, 1);
10299 if (f->n_tool_bar_items)
10300 {
10301 build_desired_tool_bar_string (f);
10302 nlines = tool_bar_lines_needed (f, NULL);
10303 }
10304 }
10305
10306 return make_number (nlines);
10307 }
10308
10309
10310 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
10311 height should be changed. */
10312
10313 static int
10314 redisplay_tool_bar (f)
10315 struct frame *f;
10316 {
10317 struct window *w;
10318 struct it it;
10319 struct glyph_row *row;
10320
10321 #if defined (USE_GTK) || defined (HAVE_NS)
10322 if (FRAME_EXTERNAL_TOOL_BAR (f))
10323 update_frame_tool_bar (f);
10324 return 0;
10325 #endif
10326
10327 /* If frame hasn't a tool-bar window or if it is zero-height, don't
10328 do anything. This means you must start with tool-bar-lines
10329 non-zero to get the auto-sizing effect. Or in other words, you
10330 can turn off tool-bars by specifying tool-bar-lines zero. */
10331 if (!WINDOWP (f->tool_bar_window)
10332 || (w = XWINDOW (f->tool_bar_window),
10333 WINDOW_TOTAL_LINES (w) == 0))
10334 return 0;
10335
10336 /* Set up an iterator for the tool-bar window. */
10337 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
10338 it.first_visible_x = 0;
10339 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
10340 row = it.glyph_row;
10341
10342 /* Build a string that represents the contents of the tool-bar. */
10343 build_desired_tool_bar_string (f);
10344 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
10345
10346 if (f->n_tool_bar_rows == 0)
10347 {
10348 int nlines;
10349
10350 if ((nlines = tool_bar_lines_needed (f, &f->n_tool_bar_rows),
10351 nlines != WINDOW_TOTAL_LINES (w)))
10352 {
10353 extern Lisp_Object Qtool_bar_lines;
10354 Lisp_Object frame;
10355 int old_height = WINDOW_TOTAL_LINES (w);
10356
10357 XSETFRAME (frame, f);
10358 Fmodify_frame_parameters (frame,
10359 Fcons (Fcons (Qtool_bar_lines,
10360 make_number (nlines)),
10361 Qnil));
10362 if (WINDOW_TOTAL_LINES (w) != old_height)
10363 {
10364 clear_glyph_matrix (w->desired_matrix);
10365 fonts_changed_p = 1;
10366 return 1;
10367 }
10368 }
10369 }
10370
10371 /* Display as many lines as needed to display all tool-bar items. */
10372
10373 if (f->n_tool_bar_rows > 0)
10374 {
10375 int border, rows, height, extra;
10376
10377 if (INTEGERP (Vtool_bar_border))
10378 border = XINT (Vtool_bar_border);
10379 else if (EQ (Vtool_bar_border, Qinternal_border_width))
10380 border = FRAME_INTERNAL_BORDER_WIDTH (f);
10381 else if (EQ (Vtool_bar_border, Qborder_width))
10382 border = f->border_width;
10383 else
10384 border = 0;
10385 if (border < 0)
10386 border = 0;
10387
10388 rows = f->n_tool_bar_rows;
10389 height = max (1, (it.last_visible_y - border) / rows);
10390 extra = it.last_visible_y - border - height * rows;
10391
10392 while (it.current_y < it.last_visible_y)
10393 {
10394 int h = 0;
10395 if (extra > 0 && rows-- > 0)
10396 {
10397 h = (extra + rows - 1) / rows;
10398 extra -= h;
10399 }
10400 display_tool_bar_line (&it, height + h);
10401 }
10402 }
10403 else
10404 {
10405 while (it.current_y < it.last_visible_y)
10406 display_tool_bar_line (&it, 0);
10407 }
10408
10409 /* It doesn't make much sense to try scrolling in the tool-bar
10410 window, so don't do it. */
10411 w->desired_matrix->no_scrolling_p = 1;
10412 w->must_be_updated_p = 1;
10413
10414 if (!NILP (Vauto_resize_tool_bars))
10415 {
10416 int max_tool_bar_height = MAX_FRAME_TOOL_BAR_HEIGHT (f);
10417 int change_height_p = 0;
10418
10419 /* If we couldn't display everything, change the tool-bar's
10420 height if there is room for more. */
10421 if (IT_STRING_CHARPOS (it) < it.end_charpos
10422 && it.current_y < max_tool_bar_height)
10423 change_height_p = 1;
10424
10425 row = it.glyph_row - 1;
10426
10427 /* If there are blank lines at the end, except for a partially
10428 visible blank line at the end that is smaller than
10429 FRAME_LINE_HEIGHT, change the tool-bar's height. */
10430 if (!row->displays_text_p
10431 && row->height >= FRAME_LINE_HEIGHT (f))
10432 change_height_p = 1;
10433
10434 /* If row displays tool-bar items, but is partially visible,
10435 change the tool-bar's height. */
10436 if (row->displays_text_p
10437 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y
10438 && MATRIX_ROW_BOTTOM_Y (row) < max_tool_bar_height)
10439 change_height_p = 1;
10440
10441 /* Resize windows as needed by changing the `tool-bar-lines'
10442 frame parameter. */
10443 if (change_height_p)
10444 {
10445 extern Lisp_Object Qtool_bar_lines;
10446 Lisp_Object frame;
10447 int old_height = WINDOW_TOTAL_LINES (w);
10448 int nrows;
10449 int nlines = tool_bar_lines_needed (f, &nrows);
10450
10451 change_height_p = ((EQ (Vauto_resize_tool_bars, Qgrow_only)
10452 && !f->minimize_tool_bar_window_p)
10453 ? (nlines > old_height)
10454 : (nlines != old_height));
10455 f->minimize_tool_bar_window_p = 0;
10456
10457 if (change_height_p)
10458 {
10459 XSETFRAME (frame, f);
10460 Fmodify_frame_parameters (frame,
10461 Fcons (Fcons (Qtool_bar_lines,
10462 make_number (nlines)),
10463 Qnil));
10464 if (WINDOW_TOTAL_LINES (w) != old_height)
10465 {
10466 clear_glyph_matrix (w->desired_matrix);
10467 f->n_tool_bar_rows = nrows;
10468 fonts_changed_p = 1;
10469 return 1;
10470 }
10471 }
10472 }
10473 }
10474
10475 f->minimize_tool_bar_window_p = 0;
10476 return 0;
10477 }
10478
10479
10480 /* Get information about the tool-bar item which is displayed in GLYPH
10481 on frame F. Return in *PROP_IDX the index where tool-bar item
10482 properties start in F->tool_bar_items. Value is zero if
10483 GLYPH doesn't display a tool-bar item. */
10484
10485 static int
10486 tool_bar_item_info (f, glyph, prop_idx)
10487 struct frame *f;
10488 struct glyph *glyph;
10489 int *prop_idx;
10490 {
10491 Lisp_Object prop;
10492 int success_p;
10493 int charpos;
10494
10495 /* This function can be called asynchronously, which means we must
10496 exclude any possibility that Fget_text_property signals an
10497 error. */
10498 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
10499 charpos = max (0, charpos);
10500
10501 /* Get the text property `menu-item' at pos. The value of that
10502 property is the start index of this item's properties in
10503 F->tool_bar_items. */
10504 prop = Fget_text_property (make_number (charpos),
10505 Qmenu_item, f->current_tool_bar_string);
10506 if (INTEGERP (prop))
10507 {
10508 *prop_idx = XINT (prop);
10509 success_p = 1;
10510 }
10511 else
10512 success_p = 0;
10513
10514 return success_p;
10515 }
10516
10517 \f
10518 /* Get information about the tool-bar item at position X/Y on frame F.
10519 Return in *GLYPH a pointer to the glyph of the tool-bar item in
10520 the current matrix of the tool-bar window of F, or NULL if not
10521 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
10522 item in F->tool_bar_items. Value is
10523
10524 -1 if X/Y is not on a tool-bar item
10525 0 if X/Y is on the same item that was highlighted before.
10526 1 otherwise. */
10527
10528 static int
10529 get_tool_bar_item (f, x, y, glyph, hpos, vpos, prop_idx)
10530 struct frame *f;
10531 int x, y;
10532 struct glyph **glyph;
10533 int *hpos, *vpos, *prop_idx;
10534 {
10535 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10536 struct window *w = XWINDOW (f->tool_bar_window);
10537 int area;
10538
10539 /* Find the glyph under X/Y. */
10540 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
10541 if (*glyph == NULL)
10542 return -1;
10543
10544 /* Get the start of this tool-bar item's properties in
10545 f->tool_bar_items. */
10546 if (!tool_bar_item_info (f, *glyph, prop_idx))
10547 return -1;
10548
10549 /* Is mouse on the highlighted item? */
10550 if (EQ (f->tool_bar_window, dpyinfo->mouse_face_window)
10551 && *vpos >= dpyinfo->mouse_face_beg_row
10552 && *vpos <= dpyinfo->mouse_face_end_row
10553 && (*vpos > dpyinfo->mouse_face_beg_row
10554 || *hpos >= dpyinfo->mouse_face_beg_col)
10555 && (*vpos < dpyinfo->mouse_face_end_row
10556 || *hpos < dpyinfo->mouse_face_end_col
10557 || dpyinfo->mouse_face_past_end))
10558 return 0;
10559
10560 return 1;
10561 }
10562
10563
10564 /* EXPORT:
10565 Handle mouse button event on the tool-bar of frame F, at
10566 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
10567 0 for button release. MODIFIERS is event modifiers for button
10568 release. */
10569
10570 void
10571 handle_tool_bar_click (f, x, y, down_p, modifiers)
10572 struct frame *f;
10573 int x, y, down_p;
10574 unsigned int modifiers;
10575 {
10576 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10577 struct window *w = XWINDOW (f->tool_bar_window);
10578 int hpos, vpos, prop_idx;
10579 struct glyph *glyph;
10580 Lisp_Object enabled_p;
10581
10582 /* If not on the highlighted tool-bar item, return. */
10583 frame_to_window_pixel_xy (w, &x, &y);
10584 if (get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
10585 return;
10586
10587 /* If item is disabled, do nothing. */
10588 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
10589 if (NILP (enabled_p))
10590 return;
10591
10592 if (down_p)
10593 {
10594 /* Show item in pressed state. */
10595 show_mouse_face (dpyinfo, DRAW_IMAGE_SUNKEN);
10596 dpyinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
10597 last_tool_bar_item = prop_idx;
10598 }
10599 else
10600 {
10601 Lisp_Object key, frame;
10602 struct input_event event;
10603 EVENT_INIT (event);
10604
10605 /* Show item in released state. */
10606 show_mouse_face (dpyinfo, DRAW_IMAGE_RAISED);
10607 dpyinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
10608
10609 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
10610
10611 XSETFRAME (frame, f);
10612 event.kind = TOOL_BAR_EVENT;
10613 event.frame_or_window = frame;
10614 event.arg = frame;
10615 kbd_buffer_store_event (&event);
10616
10617 event.kind = TOOL_BAR_EVENT;
10618 event.frame_or_window = frame;
10619 event.arg = key;
10620 event.modifiers = modifiers;
10621 kbd_buffer_store_event (&event);
10622 last_tool_bar_item = -1;
10623 }
10624 }
10625
10626
10627 /* Possibly highlight a tool-bar item on frame F when mouse moves to
10628 tool-bar window-relative coordinates X/Y. Called from
10629 note_mouse_highlight. */
10630
10631 static void
10632 note_tool_bar_highlight (f, x, y)
10633 struct frame *f;
10634 int x, y;
10635 {
10636 Lisp_Object window = f->tool_bar_window;
10637 struct window *w = XWINDOW (window);
10638 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10639 int hpos, vpos;
10640 struct glyph *glyph;
10641 struct glyph_row *row;
10642 int i;
10643 Lisp_Object enabled_p;
10644 int prop_idx;
10645 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
10646 int mouse_down_p, rc;
10647
10648 /* Function note_mouse_highlight is called with negative x(y
10649 values when mouse moves outside of the frame. */
10650 if (x <= 0 || y <= 0)
10651 {
10652 clear_mouse_face (dpyinfo);
10653 return;
10654 }
10655
10656 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
10657 if (rc < 0)
10658 {
10659 /* Not on tool-bar item. */
10660 clear_mouse_face (dpyinfo);
10661 return;
10662 }
10663 else if (rc == 0)
10664 /* On same tool-bar item as before. */
10665 goto set_help_echo;
10666
10667 clear_mouse_face (dpyinfo);
10668
10669 /* Mouse is down, but on different tool-bar item? */
10670 mouse_down_p = (dpyinfo->grabbed
10671 && f == last_mouse_frame
10672 && FRAME_LIVE_P (f));
10673 if (mouse_down_p
10674 && last_tool_bar_item != prop_idx)
10675 return;
10676
10677 dpyinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
10678 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
10679
10680 /* If tool-bar item is not enabled, don't highlight it. */
10681 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
10682 if (!NILP (enabled_p))
10683 {
10684 /* Compute the x-position of the glyph. In front and past the
10685 image is a space. We include this in the highlighted area. */
10686 row = MATRIX_ROW (w->current_matrix, vpos);
10687 for (i = x = 0; i < hpos; ++i)
10688 x += row->glyphs[TEXT_AREA][i].pixel_width;
10689
10690 /* Record this as the current active region. */
10691 dpyinfo->mouse_face_beg_col = hpos;
10692 dpyinfo->mouse_face_beg_row = vpos;
10693 dpyinfo->mouse_face_beg_x = x;
10694 dpyinfo->mouse_face_beg_y = row->y;
10695 dpyinfo->mouse_face_past_end = 0;
10696
10697 dpyinfo->mouse_face_end_col = hpos + 1;
10698 dpyinfo->mouse_face_end_row = vpos;
10699 dpyinfo->mouse_face_end_x = x + glyph->pixel_width;
10700 dpyinfo->mouse_face_end_y = row->y;
10701 dpyinfo->mouse_face_window = window;
10702 dpyinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
10703
10704 /* Display it as active. */
10705 show_mouse_face (dpyinfo, draw);
10706 dpyinfo->mouse_face_image_state = draw;
10707 }
10708
10709 set_help_echo:
10710
10711 /* Set help_echo_string to a help string to display for this tool-bar item.
10712 XTread_socket does the rest. */
10713 help_echo_object = help_echo_window = Qnil;
10714 help_echo_pos = -1;
10715 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
10716 if (NILP (help_echo_string))
10717 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
10718 }
10719
10720 #endif /* HAVE_WINDOW_SYSTEM */
10721
10722
10723 \f
10724 /************************************************************************
10725 Horizontal scrolling
10726 ************************************************************************/
10727
10728 static int hscroll_window_tree P_ ((Lisp_Object));
10729 static int hscroll_windows P_ ((Lisp_Object));
10730
10731 /* For all leaf windows in the window tree rooted at WINDOW, set their
10732 hscroll value so that PT is (i) visible in the window, and (ii) so
10733 that it is not within a certain margin at the window's left and
10734 right border. Value is non-zero if any window's hscroll has been
10735 changed. */
10736
10737 static int
10738 hscroll_window_tree (window)
10739 Lisp_Object window;
10740 {
10741 int hscrolled_p = 0;
10742 int hscroll_relative_p = FLOATP (Vhscroll_step);
10743 int hscroll_step_abs = 0;
10744 double hscroll_step_rel = 0;
10745
10746 if (hscroll_relative_p)
10747 {
10748 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
10749 if (hscroll_step_rel < 0)
10750 {
10751 hscroll_relative_p = 0;
10752 hscroll_step_abs = 0;
10753 }
10754 }
10755 else if (INTEGERP (Vhscroll_step))
10756 {
10757 hscroll_step_abs = XINT (Vhscroll_step);
10758 if (hscroll_step_abs < 0)
10759 hscroll_step_abs = 0;
10760 }
10761 else
10762 hscroll_step_abs = 0;
10763
10764 while (WINDOWP (window))
10765 {
10766 struct window *w = XWINDOW (window);
10767
10768 if (WINDOWP (w->hchild))
10769 hscrolled_p |= hscroll_window_tree (w->hchild);
10770 else if (WINDOWP (w->vchild))
10771 hscrolled_p |= hscroll_window_tree (w->vchild);
10772 else if (w->cursor.vpos >= 0)
10773 {
10774 int h_margin;
10775 int text_area_width;
10776 struct glyph_row *current_cursor_row
10777 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
10778 struct glyph_row *desired_cursor_row
10779 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
10780 struct glyph_row *cursor_row
10781 = (desired_cursor_row->enabled_p
10782 ? desired_cursor_row
10783 : current_cursor_row);
10784
10785 text_area_width = window_box_width (w, TEXT_AREA);
10786
10787 /* Scroll when cursor is inside this scroll margin. */
10788 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
10789
10790 if (!NILP (Fbuffer_local_value (Qauto_hscroll_mode, w->buffer))
10791 && ((XFASTINT (w->hscroll)
10792 && w->cursor.x <= h_margin)
10793 || (cursor_row->enabled_p
10794 && cursor_row->truncated_on_right_p
10795 && (w->cursor.x >= text_area_width - h_margin))))
10796 {
10797 struct it it;
10798 int hscroll;
10799 struct buffer *saved_current_buffer;
10800 int pt;
10801 int wanted_x;
10802
10803 /* Find point in a display of infinite width. */
10804 saved_current_buffer = current_buffer;
10805 current_buffer = XBUFFER (w->buffer);
10806
10807 if (w == XWINDOW (selected_window))
10808 pt = BUF_PT (current_buffer);
10809 else
10810 {
10811 pt = marker_position (w->pointm);
10812 pt = max (BEGV, pt);
10813 pt = min (ZV, pt);
10814 }
10815
10816 /* Move iterator to pt starting at cursor_row->start in
10817 a line with infinite width. */
10818 init_to_row_start (&it, w, cursor_row);
10819 it.last_visible_x = INFINITY;
10820 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
10821 current_buffer = saved_current_buffer;
10822
10823 /* Position cursor in window. */
10824 if (!hscroll_relative_p && hscroll_step_abs == 0)
10825 hscroll = max (0, (it.current_x
10826 - (ITERATOR_AT_END_OF_LINE_P (&it)
10827 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
10828 : (text_area_width / 2))))
10829 / FRAME_COLUMN_WIDTH (it.f);
10830 else if (w->cursor.x >= text_area_width - h_margin)
10831 {
10832 if (hscroll_relative_p)
10833 wanted_x = text_area_width * (1 - hscroll_step_rel)
10834 - h_margin;
10835 else
10836 wanted_x = text_area_width
10837 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
10838 - h_margin;
10839 hscroll
10840 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
10841 }
10842 else
10843 {
10844 if (hscroll_relative_p)
10845 wanted_x = text_area_width * hscroll_step_rel
10846 + h_margin;
10847 else
10848 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
10849 + h_margin;
10850 hscroll
10851 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
10852 }
10853 hscroll = max (hscroll, XFASTINT (w->min_hscroll));
10854
10855 /* Don't call Fset_window_hscroll if value hasn't
10856 changed because it will prevent redisplay
10857 optimizations. */
10858 if (XFASTINT (w->hscroll) != hscroll)
10859 {
10860 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
10861 w->hscroll = make_number (hscroll);
10862 hscrolled_p = 1;
10863 }
10864 }
10865 }
10866
10867 window = w->next;
10868 }
10869
10870 /* Value is non-zero if hscroll of any leaf window has been changed. */
10871 return hscrolled_p;
10872 }
10873
10874
10875 /* Set hscroll so that cursor is visible and not inside horizontal
10876 scroll margins for all windows in the tree rooted at WINDOW. See
10877 also hscroll_window_tree above. Value is non-zero if any window's
10878 hscroll has been changed. If it has, desired matrices on the frame
10879 of WINDOW are cleared. */
10880
10881 static int
10882 hscroll_windows (window)
10883 Lisp_Object window;
10884 {
10885 int hscrolled_p = hscroll_window_tree (window);
10886 if (hscrolled_p)
10887 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
10888 return hscrolled_p;
10889 }
10890
10891
10892 \f
10893 /************************************************************************
10894 Redisplay
10895 ************************************************************************/
10896
10897 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
10898 to a non-zero value. This is sometimes handy to have in a debugger
10899 session. */
10900
10901 #if GLYPH_DEBUG
10902
10903 /* First and last unchanged row for try_window_id. */
10904
10905 int debug_first_unchanged_at_end_vpos;
10906 int debug_last_unchanged_at_beg_vpos;
10907
10908 /* Delta vpos and y. */
10909
10910 int debug_dvpos, debug_dy;
10911
10912 /* Delta in characters and bytes for try_window_id. */
10913
10914 int debug_delta, debug_delta_bytes;
10915
10916 /* Values of window_end_pos and window_end_vpos at the end of
10917 try_window_id. */
10918
10919 EMACS_INT debug_end_pos, debug_end_vpos;
10920
10921 /* Append a string to W->desired_matrix->method. FMT is a printf
10922 format string. A1...A9 are a supplement for a variable-length
10923 argument list. If trace_redisplay_p is non-zero also printf the
10924 resulting string to stderr. */
10925
10926 static void
10927 debug_method_add (w, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9)
10928 struct window *w;
10929 char *fmt;
10930 int a1, a2, a3, a4, a5, a6, a7, a8, a9;
10931 {
10932 char buffer[512];
10933 char *method = w->desired_matrix->method;
10934 int len = strlen (method);
10935 int size = sizeof w->desired_matrix->method;
10936 int remaining = size - len - 1;
10937
10938 sprintf (buffer, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9);
10939 if (len && remaining)
10940 {
10941 method[len] = '|';
10942 --remaining, ++len;
10943 }
10944
10945 strncpy (method + len, buffer, remaining);
10946
10947 if (trace_redisplay_p)
10948 fprintf (stderr, "%p (%s): %s\n",
10949 w,
10950 ((BUFFERP (w->buffer)
10951 && STRINGP (XBUFFER (w->buffer)->name))
10952 ? (char *) SDATA (XBUFFER (w->buffer)->name)
10953 : "no buffer"),
10954 buffer);
10955 }
10956
10957 #endif /* GLYPH_DEBUG */
10958
10959
10960 /* Value is non-zero if all changes in window W, which displays
10961 current_buffer, are in the text between START and END. START is a
10962 buffer position, END is given as a distance from Z. Used in
10963 redisplay_internal for display optimization. */
10964
10965 static INLINE int
10966 text_outside_line_unchanged_p (w, start, end)
10967 struct window *w;
10968 int start, end;
10969 {
10970 int unchanged_p = 1;
10971
10972 /* If text or overlays have changed, see where. */
10973 if (XFASTINT (w->last_modified) < MODIFF
10974 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
10975 {
10976 /* Gap in the line? */
10977 if (GPT < start || Z - GPT < end)
10978 unchanged_p = 0;
10979
10980 /* Changes start in front of the line, or end after it? */
10981 if (unchanged_p
10982 && (BEG_UNCHANGED < start - 1
10983 || END_UNCHANGED < end))
10984 unchanged_p = 0;
10985
10986 /* If selective display, can't optimize if changes start at the
10987 beginning of the line. */
10988 if (unchanged_p
10989 && INTEGERP (current_buffer->selective_display)
10990 && XINT (current_buffer->selective_display) > 0
10991 && (BEG_UNCHANGED < start || GPT <= start))
10992 unchanged_p = 0;
10993
10994 /* If there are overlays at the start or end of the line, these
10995 may have overlay strings with newlines in them. A change at
10996 START, for instance, may actually concern the display of such
10997 overlay strings as well, and they are displayed on different
10998 lines. So, quickly rule out this case. (For the future, it
10999 might be desirable to implement something more telling than
11000 just BEG/END_UNCHANGED.) */
11001 if (unchanged_p)
11002 {
11003 if (BEG + BEG_UNCHANGED == start
11004 && overlay_touches_p (start))
11005 unchanged_p = 0;
11006 if (END_UNCHANGED == end
11007 && overlay_touches_p (Z - end))
11008 unchanged_p = 0;
11009 }
11010 }
11011
11012 return unchanged_p;
11013 }
11014
11015
11016 /* Do a frame update, taking possible shortcuts into account. This is
11017 the main external entry point for redisplay.
11018
11019 If the last redisplay displayed an echo area message and that message
11020 is no longer requested, we clear the echo area or bring back the
11021 mini-buffer if that is in use. */
11022
11023 void
11024 redisplay ()
11025 {
11026 redisplay_internal (0);
11027 }
11028
11029
11030 static Lisp_Object
11031 overlay_arrow_string_or_property (var)
11032 Lisp_Object var;
11033 {
11034 Lisp_Object val;
11035
11036 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
11037 return val;
11038
11039 return Voverlay_arrow_string;
11040 }
11041
11042 /* Return 1 if there are any overlay-arrows in current_buffer. */
11043 static int
11044 overlay_arrow_in_current_buffer_p ()
11045 {
11046 Lisp_Object vlist;
11047
11048 for (vlist = Voverlay_arrow_variable_list;
11049 CONSP (vlist);
11050 vlist = XCDR (vlist))
11051 {
11052 Lisp_Object var = XCAR (vlist);
11053 Lisp_Object val;
11054
11055 if (!SYMBOLP (var))
11056 continue;
11057 val = find_symbol_value (var);
11058 if (MARKERP (val)
11059 && current_buffer == XMARKER (val)->buffer)
11060 return 1;
11061 }
11062 return 0;
11063 }
11064
11065
11066 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
11067 has changed. */
11068
11069 static int
11070 overlay_arrows_changed_p ()
11071 {
11072 Lisp_Object vlist;
11073
11074 for (vlist = Voverlay_arrow_variable_list;
11075 CONSP (vlist);
11076 vlist = XCDR (vlist))
11077 {
11078 Lisp_Object var = XCAR (vlist);
11079 Lisp_Object val, pstr;
11080
11081 if (!SYMBOLP (var))
11082 continue;
11083 val = find_symbol_value (var);
11084 if (!MARKERP (val))
11085 continue;
11086 if (! EQ (COERCE_MARKER (val),
11087 Fget (var, Qlast_arrow_position))
11088 || ! (pstr = overlay_arrow_string_or_property (var),
11089 EQ (pstr, Fget (var, Qlast_arrow_string))))
11090 return 1;
11091 }
11092 return 0;
11093 }
11094
11095 /* Mark overlay arrows to be updated on next redisplay. */
11096
11097 static void
11098 update_overlay_arrows (up_to_date)
11099 int up_to_date;
11100 {
11101 Lisp_Object vlist;
11102
11103 for (vlist = Voverlay_arrow_variable_list;
11104 CONSP (vlist);
11105 vlist = XCDR (vlist))
11106 {
11107 Lisp_Object var = XCAR (vlist);
11108
11109 if (!SYMBOLP (var))
11110 continue;
11111
11112 if (up_to_date > 0)
11113 {
11114 Lisp_Object val = find_symbol_value (var);
11115 Fput (var, Qlast_arrow_position,
11116 COERCE_MARKER (val));
11117 Fput (var, Qlast_arrow_string,
11118 overlay_arrow_string_or_property (var));
11119 }
11120 else if (up_to_date < 0
11121 || !NILP (Fget (var, Qlast_arrow_position)))
11122 {
11123 Fput (var, Qlast_arrow_position, Qt);
11124 Fput (var, Qlast_arrow_string, Qt);
11125 }
11126 }
11127 }
11128
11129
11130 /* Return overlay arrow string to display at row.
11131 Return integer (bitmap number) for arrow bitmap in left fringe.
11132 Return nil if no overlay arrow. */
11133
11134 static Lisp_Object
11135 overlay_arrow_at_row (it, row)
11136 struct it *it;
11137 struct glyph_row *row;
11138 {
11139 Lisp_Object vlist;
11140
11141 for (vlist = Voverlay_arrow_variable_list;
11142 CONSP (vlist);
11143 vlist = XCDR (vlist))
11144 {
11145 Lisp_Object var = XCAR (vlist);
11146 Lisp_Object val;
11147
11148 if (!SYMBOLP (var))
11149 continue;
11150
11151 val = find_symbol_value (var);
11152
11153 if (MARKERP (val)
11154 && current_buffer == XMARKER (val)->buffer
11155 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
11156 {
11157 if (FRAME_WINDOW_P (it->f)
11158 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
11159 {
11160 #ifdef HAVE_WINDOW_SYSTEM
11161 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
11162 {
11163 int fringe_bitmap;
11164 if ((fringe_bitmap = lookup_fringe_bitmap (val)) != 0)
11165 return make_number (fringe_bitmap);
11166 }
11167 #endif
11168 return make_number (-1); /* Use default arrow bitmap */
11169 }
11170 return overlay_arrow_string_or_property (var);
11171 }
11172 }
11173
11174 return Qnil;
11175 }
11176
11177 /* Return 1 if point moved out of or into a composition. Otherwise
11178 return 0. PREV_BUF and PREV_PT are the last point buffer and
11179 position. BUF and PT are the current point buffer and position. */
11180
11181 int
11182 check_point_in_composition (prev_buf, prev_pt, buf, pt)
11183 struct buffer *prev_buf, *buf;
11184 int prev_pt, pt;
11185 {
11186 EMACS_INT start, end;
11187 Lisp_Object prop;
11188 Lisp_Object buffer;
11189
11190 XSETBUFFER (buffer, buf);
11191 /* Check a composition at the last point if point moved within the
11192 same buffer. */
11193 if (prev_buf == buf)
11194 {
11195 if (prev_pt == pt)
11196 /* Point didn't move. */
11197 return 0;
11198
11199 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
11200 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
11201 && COMPOSITION_VALID_P (start, end, prop)
11202 && start < prev_pt && end > prev_pt)
11203 /* The last point was within the composition. Return 1 iff
11204 point moved out of the composition. */
11205 return (pt <= start || pt >= end);
11206 }
11207
11208 /* Check a composition at the current point. */
11209 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
11210 && find_composition (pt, -1, &start, &end, &prop, buffer)
11211 && COMPOSITION_VALID_P (start, end, prop)
11212 && start < pt && end > pt);
11213 }
11214
11215
11216 /* Reconsider the setting of B->clip_changed which is displayed
11217 in window W. */
11218
11219 static INLINE void
11220 reconsider_clip_changes (w, b)
11221 struct window *w;
11222 struct buffer *b;
11223 {
11224 if (b->clip_changed
11225 && !NILP (w->window_end_valid)
11226 && w->current_matrix->buffer == b
11227 && w->current_matrix->zv == BUF_ZV (b)
11228 && w->current_matrix->begv == BUF_BEGV (b))
11229 b->clip_changed = 0;
11230
11231 /* If display wasn't paused, and W is not a tool bar window, see if
11232 point has been moved into or out of a composition. In that case,
11233 we set b->clip_changed to 1 to force updating the screen. If
11234 b->clip_changed has already been set to 1, we can skip this
11235 check. */
11236 if (!b->clip_changed
11237 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
11238 {
11239 int pt;
11240
11241 if (w == XWINDOW (selected_window))
11242 pt = BUF_PT (current_buffer);
11243 else
11244 pt = marker_position (w->pointm);
11245
11246 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
11247 || pt != XINT (w->last_point))
11248 && check_point_in_composition (w->current_matrix->buffer,
11249 XINT (w->last_point),
11250 XBUFFER (w->buffer), pt))
11251 b->clip_changed = 1;
11252 }
11253 }
11254 \f
11255
11256 /* Select FRAME to forward the values of frame-local variables into C
11257 variables so that the redisplay routines can access those values
11258 directly. */
11259
11260 static void
11261 select_frame_for_redisplay (frame)
11262 Lisp_Object frame;
11263 {
11264 Lisp_Object tail, symbol, val;
11265 Lisp_Object old = selected_frame;
11266 struct Lisp_Symbol *sym;
11267
11268 xassert (FRAMEP (frame) && FRAME_LIVE_P (XFRAME (frame)));
11269
11270 selected_frame = frame;
11271
11272 do
11273 {
11274 for (tail = XFRAME (frame)->param_alist; CONSP (tail); tail = XCDR (tail))
11275 if (CONSP (XCAR (tail))
11276 && (symbol = XCAR (XCAR (tail)),
11277 SYMBOLP (symbol))
11278 && (sym = indirect_variable (XSYMBOL (symbol)),
11279 val = sym->value,
11280 (BUFFER_LOCAL_VALUEP (val)))
11281 && XBUFFER_LOCAL_VALUE (val)->check_frame)
11282 /* Use find_symbol_value rather than Fsymbol_value
11283 to avoid an error if it is void. */
11284 find_symbol_value (symbol);
11285 } while (!EQ (frame, old) && (frame = old, 1));
11286 }
11287
11288
11289 #define STOP_POLLING \
11290 do { if (! polling_stopped_here) stop_polling (); \
11291 polling_stopped_here = 1; } while (0)
11292
11293 #define RESUME_POLLING \
11294 do { if (polling_stopped_here) start_polling (); \
11295 polling_stopped_here = 0; } while (0)
11296
11297
11298 /* If PRESERVE_ECHO_AREA is nonzero, it means this redisplay is not in
11299 response to any user action; therefore, we should preserve the echo
11300 area. (Actually, our caller does that job.) Perhaps in the future
11301 avoid recentering windows if it is not necessary; currently that
11302 causes some problems. */
11303
11304 static void
11305 redisplay_internal (preserve_echo_area)
11306 int preserve_echo_area;
11307 {
11308 struct window *w = XWINDOW (selected_window);
11309 struct frame *f;
11310 int pause;
11311 int must_finish = 0;
11312 struct text_pos tlbufpos, tlendpos;
11313 int number_of_visible_frames;
11314 int count, count1;
11315 struct frame *sf;
11316 int polling_stopped_here = 0;
11317 Lisp_Object old_frame = selected_frame;
11318
11319 /* Non-zero means redisplay has to consider all windows on all
11320 frames. Zero means, only selected_window is considered. */
11321 int consider_all_windows_p;
11322
11323 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
11324
11325 /* No redisplay if running in batch mode or frame is not yet fully
11326 initialized, or redisplay is explicitly turned off by setting
11327 Vinhibit_redisplay. */
11328 if (FRAME_INITIAL_P (SELECTED_FRAME ())
11329 || !NILP (Vinhibit_redisplay))
11330 return;
11331
11332 /* Don't examine these until after testing Vinhibit_redisplay.
11333 When Emacs is shutting down, perhaps because its connection to
11334 X has dropped, we should not look at them at all. */
11335 f = XFRAME (w->frame);
11336 sf = SELECTED_FRAME ();
11337
11338 if (!f->glyphs_initialized_p)
11339 return;
11340
11341 /* The flag redisplay_performed_directly_p is set by
11342 direct_output_for_insert when it already did the whole screen
11343 update necessary. */
11344 if (redisplay_performed_directly_p)
11345 {
11346 redisplay_performed_directly_p = 0;
11347 if (!hscroll_windows (selected_window))
11348 return;
11349 }
11350
11351 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS)
11352 if (popup_activated ())
11353 return;
11354 #endif
11355
11356 /* I don't think this happens but let's be paranoid. */
11357 if (redisplaying_p)
11358 return;
11359
11360 /* Record a function that resets redisplaying_p to its old value
11361 when we leave this function. */
11362 count = SPECPDL_INDEX ();
11363 record_unwind_protect (unwind_redisplay,
11364 Fcons (make_number (redisplaying_p), selected_frame));
11365 ++redisplaying_p;
11366 specbind (Qinhibit_free_realized_faces, Qnil);
11367
11368 {
11369 Lisp_Object tail, frame;
11370
11371 FOR_EACH_FRAME (tail, frame)
11372 {
11373 struct frame *f = XFRAME (frame);
11374 f->already_hscrolled_p = 0;
11375 }
11376 }
11377
11378 retry:
11379 if (!EQ (old_frame, selected_frame)
11380 && FRAME_LIVE_P (XFRAME (old_frame)))
11381 /* When running redisplay, we play a bit fast-and-loose and allow e.g.
11382 selected_frame and selected_window to be temporarily out-of-sync so
11383 when we come back here via `goto retry', we need to resync because we
11384 may need to run Elisp code (via prepare_menu_bars). */
11385 select_frame_for_redisplay (old_frame);
11386
11387 pause = 0;
11388 reconsider_clip_changes (w, current_buffer);
11389 last_escape_glyph_frame = NULL;
11390 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
11391
11392 /* If new fonts have been loaded that make a glyph matrix adjustment
11393 necessary, do it. */
11394 if (fonts_changed_p)
11395 {
11396 adjust_glyphs (NULL);
11397 ++windows_or_buffers_changed;
11398 fonts_changed_p = 0;
11399 }
11400
11401 /* If face_change_count is non-zero, init_iterator will free all
11402 realized faces, which includes the faces referenced from current
11403 matrices. So, we can't reuse current matrices in this case. */
11404 if (face_change_count)
11405 ++windows_or_buffers_changed;
11406
11407 if ((FRAME_TERMCAP_P (sf) || FRAME_MSDOS_P (sf))
11408 && FRAME_TTY (sf)->previous_frame != sf)
11409 {
11410 /* Since frames on a single ASCII terminal share the same
11411 display area, displaying a different frame means redisplay
11412 the whole thing. */
11413 windows_or_buffers_changed++;
11414 SET_FRAME_GARBAGED (sf);
11415 #ifndef DOS_NT
11416 set_tty_color_mode (FRAME_TTY (sf), sf);
11417 #endif
11418 FRAME_TTY (sf)->previous_frame = sf;
11419 }
11420
11421 /* Set the visible flags for all frames. Do this before checking
11422 for resized or garbaged frames; they want to know if their frames
11423 are visible. See the comment in frame.h for
11424 FRAME_SAMPLE_VISIBILITY. */
11425 {
11426 Lisp_Object tail, frame;
11427
11428 number_of_visible_frames = 0;
11429
11430 FOR_EACH_FRAME (tail, frame)
11431 {
11432 struct frame *f = XFRAME (frame);
11433
11434 FRAME_SAMPLE_VISIBILITY (f);
11435 if (FRAME_VISIBLE_P (f))
11436 ++number_of_visible_frames;
11437 clear_desired_matrices (f);
11438 }
11439 }
11440
11441 /* Notice any pending interrupt request to change frame size. */
11442 do_pending_window_change (1);
11443
11444 /* Clear frames marked as garbaged. */
11445 if (frame_garbaged)
11446 clear_garbaged_frames ();
11447
11448 /* Build menubar and tool-bar items. */
11449 if (NILP (Vmemory_full))
11450 prepare_menu_bars ();
11451
11452 if (windows_or_buffers_changed)
11453 update_mode_lines++;
11454
11455 /* Detect case that we need to write or remove a star in the mode line. */
11456 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
11457 {
11458 w->update_mode_line = Qt;
11459 if (buffer_shared > 1)
11460 update_mode_lines++;
11461 }
11462
11463 /* Avoid invocation of point motion hooks by `current_column' below. */
11464 count1 = SPECPDL_INDEX ();
11465 specbind (Qinhibit_point_motion_hooks, Qt);
11466
11467 /* If %c is in the mode line, update it if needed. */
11468 if (!NILP (w->column_number_displayed)
11469 /* This alternative quickly identifies a common case
11470 where no change is needed. */
11471 && !(PT == XFASTINT (w->last_point)
11472 && XFASTINT (w->last_modified) >= MODIFF
11473 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
11474 && (XFASTINT (w->column_number_displayed)
11475 != (int) current_column ())) /* iftc */
11476 w->update_mode_line = Qt;
11477
11478 unbind_to (count1, Qnil);
11479
11480 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
11481
11482 /* The variable buffer_shared is set in redisplay_window and
11483 indicates that we redisplay a buffer in different windows. See
11484 there. */
11485 consider_all_windows_p = (update_mode_lines || buffer_shared > 1
11486 || cursor_type_changed);
11487
11488 /* If specs for an arrow have changed, do thorough redisplay
11489 to ensure we remove any arrow that should no longer exist. */
11490 if (overlay_arrows_changed_p ())
11491 consider_all_windows_p = windows_or_buffers_changed = 1;
11492
11493 /* Normally the message* functions will have already displayed and
11494 updated the echo area, but the frame may have been trashed, or
11495 the update may have been preempted, so display the echo area
11496 again here. Checking message_cleared_p captures the case that
11497 the echo area should be cleared. */
11498 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
11499 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
11500 || (message_cleared_p
11501 && minibuf_level == 0
11502 /* If the mini-window is currently selected, this means the
11503 echo-area doesn't show through. */
11504 && !MINI_WINDOW_P (XWINDOW (selected_window))))
11505 {
11506 int window_height_changed_p = echo_area_display (0);
11507 must_finish = 1;
11508
11509 /* If we don't display the current message, don't clear the
11510 message_cleared_p flag, because, if we did, we wouldn't clear
11511 the echo area in the next redisplay which doesn't preserve
11512 the echo area. */
11513 if (!display_last_displayed_message_p)
11514 message_cleared_p = 0;
11515
11516 if (fonts_changed_p)
11517 goto retry;
11518 else if (window_height_changed_p)
11519 {
11520 consider_all_windows_p = 1;
11521 ++update_mode_lines;
11522 ++windows_or_buffers_changed;
11523
11524 /* If window configuration was changed, frames may have been
11525 marked garbaged. Clear them or we will experience
11526 surprises wrt scrolling. */
11527 if (frame_garbaged)
11528 clear_garbaged_frames ();
11529 }
11530 }
11531 else if (EQ (selected_window, minibuf_window)
11532 && (current_buffer->clip_changed
11533 || XFASTINT (w->last_modified) < MODIFF
11534 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
11535 && resize_mini_window (w, 0))
11536 {
11537 /* Resized active mini-window to fit the size of what it is
11538 showing if its contents might have changed. */
11539 must_finish = 1;
11540 /* FIXME: this causes all frames to be updated, which seems unnecessary
11541 since only the current frame needs to be considered. This function needs
11542 to be rewritten with two variables, consider_all_windows and
11543 consider_all_frames. */
11544 consider_all_windows_p = 1;
11545 ++windows_or_buffers_changed;
11546 ++update_mode_lines;
11547
11548 /* If window configuration was changed, frames may have been
11549 marked garbaged. Clear them or we will experience
11550 surprises wrt scrolling. */
11551 if (frame_garbaged)
11552 clear_garbaged_frames ();
11553 }
11554
11555
11556 /* If showing the region, and mark has changed, we must redisplay
11557 the whole window. The assignment to this_line_start_pos prevents
11558 the optimization directly below this if-statement. */
11559 if (((!NILP (Vtransient_mark_mode)
11560 && !NILP (XBUFFER (w->buffer)->mark_active))
11561 != !NILP (w->region_showing))
11562 || (!NILP (w->region_showing)
11563 && !EQ (w->region_showing,
11564 Fmarker_position (XBUFFER (w->buffer)->mark))))
11565 CHARPOS (this_line_start_pos) = 0;
11566
11567 /* Optimize the case that only the line containing the cursor in the
11568 selected window has changed. Variables starting with this_ are
11569 set in display_line and record information about the line
11570 containing the cursor. */
11571 tlbufpos = this_line_start_pos;
11572 tlendpos = this_line_end_pos;
11573 if (!consider_all_windows_p
11574 && CHARPOS (tlbufpos) > 0
11575 && NILP (w->update_mode_line)
11576 && !current_buffer->clip_changed
11577 && !current_buffer->prevent_redisplay_optimizations_p
11578 && FRAME_VISIBLE_P (XFRAME (w->frame))
11579 && !FRAME_OBSCURED_P (XFRAME (w->frame))
11580 /* Make sure recorded data applies to current buffer, etc. */
11581 && this_line_buffer == current_buffer
11582 && current_buffer == XBUFFER (w->buffer)
11583 && NILP (w->force_start)
11584 && NILP (w->optional_new_start)
11585 /* Point must be on the line that we have info recorded about. */
11586 && PT >= CHARPOS (tlbufpos)
11587 && PT <= Z - CHARPOS (tlendpos)
11588 /* All text outside that line, including its final newline,
11589 must be unchanged. */
11590 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
11591 CHARPOS (tlendpos)))
11592 {
11593 if (CHARPOS (tlbufpos) > BEGV
11594 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
11595 && (CHARPOS (tlbufpos) == ZV
11596 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
11597 /* Former continuation line has disappeared by becoming empty. */
11598 goto cancel;
11599 else if (XFASTINT (w->last_modified) < MODIFF
11600 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
11601 || MINI_WINDOW_P (w))
11602 {
11603 /* We have to handle the case of continuation around a
11604 wide-column character (see the comment in indent.c around
11605 line 1340).
11606
11607 For instance, in the following case:
11608
11609 -------- Insert --------
11610 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
11611 J_I_ ==> J_I_ `^^' are cursors.
11612 ^^ ^^
11613 -------- --------
11614
11615 As we have to redraw the line above, we cannot use this
11616 optimization. */
11617
11618 struct it it;
11619 int line_height_before = this_line_pixel_height;
11620
11621 /* Note that start_display will handle the case that the
11622 line starting at tlbufpos is a continuation line. */
11623 start_display (&it, w, tlbufpos);
11624
11625 /* Implementation note: It this still necessary? */
11626 if (it.current_x != this_line_start_x)
11627 goto cancel;
11628
11629 TRACE ((stderr, "trying display optimization 1\n"));
11630 w->cursor.vpos = -1;
11631 overlay_arrow_seen = 0;
11632 it.vpos = this_line_vpos;
11633 it.current_y = this_line_y;
11634 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
11635 display_line (&it);
11636
11637 /* If line contains point, is not continued,
11638 and ends at same distance from eob as before, we win. */
11639 if (w->cursor.vpos >= 0
11640 /* Line is not continued, otherwise this_line_start_pos
11641 would have been set to 0 in display_line. */
11642 && CHARPOS (this_line_start_pos)
11643 /* Line ends as before. */
11644 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
11645 /* Line has same height as before. Otherwise other lines
11646 would have to be shifted up or down. */
11647 && this_line_pixel_height == line_height_before)
11648 {
11649 /* If this is not the window's last line, we must adjust
11650 the charstarts of the lines below. */
11651 if (it.current_y < it.last_visible_y)
11652 {
11653 struct glyph_row *row
11654 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
11655 int delta, delta_bytes;
11656
11657 /* We used to distinguish between two cases here,
11658 conditioned by Z - CHARPOS (tlendpos) == ZV, for
11659 when the line ends in a newline or the end of the
11660 buffer's accessible portion. But both cases did
11661 the same, so they were collapsed. */
11662 delta = (Z
11663 - CHARPOS (tlendpos)
11664 - MATRIX_ROW_START_CHARPOS (row));
11665 delta_bytes = (Z_BYTE
11666 - BYTEPOS (tlendpos)
11667 - MATRIX_ROW_START_BYTEPOS (row));
11668
11669 increment_matrix_positions (w->current_matrix,
11670 this_line_vpos + 1,
11671 w->current_matrix->nrows,
11672 delta, delta_bytes);
11673 }
11674
11675 /* If this row displays text now but previously didn't,
11676 or vice versa, w->window_end_vpos may have to be
11677 adjusted. */
11678 if ((it.glyph_row - 1)->displays_text_p)
11679 {
11680 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
11681 XSETINT (w->window_end_vpos, this_line_vpos);
11682 }
11683 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
11684 && this_line_vpos > 0)
11685 XSETINT (w->window_end_vpos, this_line_vpos - 1);
11686 w->window_end_valid = Qnil;
11687
11688 /* Update hint: No need to try to scroll in update_window. */
11689 w->desired_matrix->no_scrolling_p = 1;
11690
11691 #if GLYPH_DEBUG
11692 *w->desired_matrix->method = 0;
11693 debug_method_add (w, "optimization 1");
11694 #endif
11695 #ifdef HAVE_WINDOW_SYSTEM
11696 update_window_fringes (w, 0);
11697 #endif
11698 goto update;
11699 }
11700 else
11701 goto cancel;
11702 }
11703 else if (/* Cursor position hasn't changed. */
11704 PT == XFASTINT (w->last_point)
11705 /* Make sure the cursor was last displayed
11706 in this window. Otherwise we have to reposition it. */
11707 && 0 <= w->cursor.vpos
11708 && WINDOW_TOTAL_LINES (w) > w->cursor.vpos)
11709 {
11710 if (!must_finish)
11711 {
11712 do_pending_window_change (1);
11713
11714 /* We used to always goto end_of_redisplay here, but this
11715 isn't enough if we have a blinking cursor. */
11716 if (w->cursor_off_p == w->last_cursor_off_p)
11717 goto end_of_redisplay;
11718 }
11719 goto update;
11720 }
11721 /* If highlighting the region, or if the cursor is in the echo area,
11722 then we can't just move the cursor. */
11723 else if (! (!NILP (Vtransient_mark_mode)
11724 && !NILP (current_buffer->mark_active))
11725 && (EQ (selected_window, current_buffer->last_selected_window)
11726 || highlight_nonselected_windows)
11727 && NILP (w->region_showing)
11728 && NILP (Vshow_trailing_whitespace)
11729 && !cursor_in_echo_area)
11730 {
11731 struct it it;
11732 struct glyph_row *row;
11733
11734 /* Skip from tlbufpos to PT and see where it is. Note that
11735 PT may be in invisible text. If so, we will end at the
11736 next visible position. */
11737 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
11738 NULL, DEFAULT_FACE_ID);
11739 it.current_x = this_line_start_x;
11740 it.current_y = this_line_y;
11741 it.vpos = this_line_vpos;
11742
11743 /* The call to move_it_to stops in front of PT, but
11744 moves over before-strings. */
11745 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
11746
11747 if (it.vpos == this_line_vpos
11748 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
11749 row->enabled_p))
11750 {
11751 xassert (this_line_vpos == it.vpos);
11752 xassert (this_line_y == it.current_y);
11753 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
11754 #if GLYPH_DEBUG
11755 *w->desired_matrix->method = 0;
11756 debug_method_add (w, "optimization 3");
11757 #endif
11758 goto update;
11759 }
11760 else
11761 goto cancel;
11762 }
11763
11764 cancel:
11765 /* Text changed drastically or point moved off of line. */
11766 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
11767 }
11768
11769 CHARPOS (this_line_start_pos) = 0;
11770 consider_all_windows_p |= buffer_shared > 1;
11771 ++clear_face_cache_count;
11772 #ifdef HAVE_WINDOW_SYSTEM
11773 ++clear_image_cache_count;
11774 #endif
11775
11776 /* Build desired matrices, and update the display. If
11777 consider_all_windows_p is non-zero, do it for all windows on all
11778 frames. Otherwise do it for selected_window, only. */
11779
11780 if (consider_all_windows_p)
11781 {
11782 Lisp_Object tail, frame;
11783
11784 FOR_EACH_FRAME (tail, frame)
11785 XFRAME (frame)->updated_p = 0;
11786
11787 /* Recompute # windows showing selected buffer. This will be
11788 incremented each time such a window is displayed. */
11789 buffer_shared = 0;
11790
11791 FOR_EACH_FRAME (tail, frame)
11792 {
11793 struct frame *f = XFRAME (frame);
11794
11795 if (FRAME_WINDOW_P (f) || FRAME_TERMCAP_P (f) || f == sf)
11796 {
11797 if (! EQ (frame, selected_frame))
11798 /* Select the frame, for the sake of frame-local
11799 variables. */
11800 select_frame_for_redisplay (frame);
11801
11802 /* Mark all the scroll bars to be removed; we'll redeem
11803 the ones we want when we redisplay their windows. */
11804 if (FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
11805 FRAME_TERMINAL (f)->condemn_scroll_bars_hook (f);
11806
11807 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
11808 redisplay_windows (FRAME_ROOT_WINDOW (f));
11809
11810 /* The X error handler may have deleted that frame. */
11811 if (!FRAME_LIVE_P (f))
11812 continue;
11813
11814 /* Any scroll bars which redisplay_windows should have
11815 nuked should now go away. */
11816 if (FRAME_TERMINAL (f)->judge_scroll_bars_hook)
11817 FRAME_TERMINAL (f)->judge_scroll_bars_hook (f);
11818
11819 /* If fonts changed, display again. */
11820 /* ??? rms: I suspect it is a mistake to jump all the way
11821 back to retry here. It should just retry this frame. */
11822 if (fonts_changed_p)
11823 goto retry;
11824
11825 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
11826 {
11827 /* See if we have to hscroll. */
11828 if (!f->already_hscrolled_p)
11829 {
11830 f->already_hscrolled_p = 1;
11831 if (hscroll_windows (f->root_window))
11832 goto retry;
11833 }
11834
11835 /* Prevent various kinds of signals during display
11836 update. stdio is not robust about handling
11837 signals, which can cause an apparent I/O
11838 error. */
11839 if (interrupt_input)
11840 unrequest_sigio ();
11841 STOP_POLLING;
11842
11843 /* Update the display. */
11844 set_window_update_flags (XWINDOW (f->root_window), 1);
11845 pause |= update_frame (f, 0, 0);
11846 f->updated_p = 1;
11847 }
11848 }
11849 }
11850
11851 if (!EQ (old_frame, selected_frame)
11852 && FRAME_LIVE_P (XFRAME (old_frame)))
11853 /* We played a bit fast-and-loose above and allowed selected_frame
11854 and selected_window to be temporarily out-of-sync but let's make
11855 sure this stays contained. */
11856 select_frame_for_redisplay (old_frame);
11857 eassert (EQ (XFRAME (selected_frame)->selected_window, selected_window));
11858
11859 if (!pause)
11860 {
11861 /* Do the mark_window_display_accurate after all windows have
11862 been redisplayed because this call resets flags in buffers
11863 which are needed for proper redisplay. */
11864 FOR_EACH_FRAME (tail, frame)
11865 {
11866 struct frame *f = XFRAME (frame);
11867 if (f->updated_p)
11868 {
11869 mark_window_display_accurate (f->root_window, 1);
11870 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
11871 FRAME_TERMINAL (f)->frame_up_to_date_hook (f);
11872 }
11873 }
11874 }
11875 }
11876 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
11877 {
11878 Lisp_Object mini_window;
11879 struct frame *mini_frame;
11880
11881 displayed_buffer = XBUFFER (XWINDOW (selected_window)->buffer);
11882 /* Use list_of_error, not Qerror, so that
11883 we catch only errors and don't run the debugger. */
11884 internal_condition_case_1 (redisplay_window_1, selected_window,
11885 list_of_error,
11886 redisplay_window_error);
11887
11888 /* Compare desired and current matrices, perform output. */
11889
11890 update:
11891 /* If fonts changed, display again. */
11892 if (fonts_changed_p)
11893 goto retry;
11894
11895 /* Prevent various kinds of signals during display update.
11896 stdio is not robust about handling signals,
11897 which can cause an apparent I/O error. */
11898 if (interrupt_input)
11899 unrequest_sigio ();
11900 STOP_POLLING;
11901
11902 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
11903 {
11904 if (hscroll_windows (selected_window))
11905 goto retry;
11906
11907 XWINDOW (selected_window)->must_be_updated_p = 1;
11908 pause = update_frame (sf, 0, 0);
11909 }
11910
11911 /* We may have called echo_area_display at the top of this
11912 function. If the echo area is on another frame, that may
11913 have put text on a frame other than the selected one, so the
11914 above call to update_frame would not have caught it. Catch
11915 it here. */
11916 mini_window = FRAME_MINIBUF_WINDOW (sf);
11917 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
11918
11919 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
11920 {
11921 XWINDOW (mini_window)->must_be_updated_p = 1;
11922 pause |= update_frame (mini_frame, 0, 0);
11923 if (!pause && hscroll_windows (mini_window))
11924 goto retry;
11925 }
11926 }
11927
11928 /* If display was paused because of pending input, make sure we do a
11929 thorough update the next time. */
11930 if (pause)
11931 {
11932 /* Prevent the optimization at the beginning of
11933 redisplay_internal that tries a single-line update of the
11934 line containing the cursor in the selected window. */
11935 CHARPOS (this_line_start_pos) = 0;
11936
11937 /* Let the overlay arrow be updated the next time. */
11938 update_overlay_arrows (0);
11939
11940 /* If we pause after scrolling, some rows in the current
11941 matrices of some windows are not valid. */
11942 if (!WINDOW_FULL_WIDTH_P (w)
11943 && !FRAME_WINDOW_P (XFRAME (w->frame)))
11944 update_mode_lines = 1;
11945 }
11946 else
11947 {
11948 if (!consider_all_windows_p)
11949 {
11950 /* This has already been done above if
11951 consider_all_windows_p is set. */
11952 mark_window_display_accurate_1 (w, 1);
11953
11954 /* Say overlay arrows are up to date. */
11955 update_overlay_arrows (1);
11956
11957 if (FRAME_TERMINAL (sf)->frame_up_to_date_hook != 0)
11958 FRAME_TERMINAL (sf)->frame_up_to_date_hook (sf);
11959 }
11960
11961 update_mode_lines = 0;
11962 windows_or_buffers_changed = 0;
11963 cursor_type_changed = 0;
11964 }
11965
11966 /* Start SIGIO interrupts coming again. Having them off during the
11967 code above makes it less likely one will discard output, but not
11968 impossible, since there might be stuff in the system buffer here.
11969 But it is much hairier to try to do anything about that. */
11970 if (interrupt_input)
11971 request_sigio ();
11972 RESUME_POLLING;
11973
11974 /* If a frame has become visible which was not before, redisplay
11975 again, so that we display it. Expose events for such a frame
11976 (which it gets when becoming visible) don't call the parts of
11977 redisplay constructing glyphs, so simply exposing a frame won't
11978 display anything in this case. So, we have to display these
11979 frames here explicitly. */
11980 if (!pause)
11981 {
11982 Lisp_Object tail, frame;
11983 int new_count = 0;
11984
11985 FOR_EACH_FRAME (tail, frame)
11986 {
11987 int this_is_visible = 0;
11988
11989 if (XFRAME (frame)->visible)
11990 this_is_visible = 1;
11991 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
11992 if (XFRAME (frame)->visible)
11993 this_is_visible = 1;
11994
11995 if (this_is_visible)
11996 new_count++;
11997 }
11998
11999 if (new_count != number_of_visible_frames)
12000 windows_or_buffers_changed++;
12001 }
12002
12003 /* Change frame size now if a change is pending. */
12004 do_pending_window_change (1);
12005
12006 /* If we just did a pending size change, or have additional
12007 visible frames, redisplay again. */
12008 if (windows_or_buffers_changed && !pause)
12009 goto retry;
12010
12011 /* Clear the face cache eventually. */
12012 if (consider_all_windows_p)
12013 {
12014 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
12015 {
12016 clear_face_cache (0);
12017 clear_face_cache_count = 0;
12018 }
12019 #ifdef HAVE_WINDOW_SYSTEM
12020 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
12021 {
12022 clear_image_caches (Qnil);
12023 clear_image_cache_count = 0;
12024 }
12025 #endif /* HAVE_WINDOW_SYSTEM */
12026 }
12027
12028 end_of_redisplay:
12029 unbind_to (count, Qnil);
12030 RESUME_POLLING;
12031 }
12032
12033
12034 /* Redisplay, but leave alone any recent echo area message unless
12035 another message has been requested in its place.
12036
12037 This is useful in situations where you need to redisplay but no
12038 user action has occurred, making it inappropriate for the message
12039 area to be cleared. See tracking_off and
12040 wait_reading_process_output for examples of these situations.
12041
12042 FROM_WHERE is an integer saying from where this function was
12043 called. This is useful for debugging. */
12044
12045 void
12046 redisplay_preserve_echo_area (from_where)
12047 int from_where;
12048 {
12049 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
12050
12051 if (!NILP (echo_area_buffer[1]))
12052 {
12053 /* We have a previously displayed message, but no current
12054 message. Redisplay the previous message. */
12055 display_last_displayed_message_p = 1;
12056 redisplay_internal (1);
12057 display_last_displayed_message_p = 0;
12058 }
12059 else
12060 redisplay_internal (1);
12061
12062 if (FRAME_RIF (SELECTED_FRAME ()) != NULL
12063 && FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
12064 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (NULL);
12065 }
12066
12067
12068 /* Function registered with record_unwind_protect in
12069 redisplay_internal. Reset redisplaying_p to the value it had
12070 before redisplay_internal was called, and clear
12071 prevent_freeing_realized_faces_p. It also selects the previously
12072 selected frame, unless it has been deleted (by an X connection
12073 failure during redisplay, for example). */
12074
12075 static Lisp_Object
12076 unwind_redisplay (val)
12077 Lisp_Object val;
12078 {
12079 Lisp_Object old_redisplaying_p, old_frame;
12080
12081 old_redisplaying_p = XCAR (val);
12082 redisplaying_p = XFASTINT (old_redisplaying_p);
12083 old_frame = XCDR (val);
12084 if (! EQ (old_frame, selected_frame)
12085 && FRAME_LIVE_P (XFRAME (old_frame)))
12086 select_frame_for_redisplay (old_frame);
12087 return Qnil;
12088 }
12089
12090
12091 /* Mark the display of window W as accurate or inaccurate. If
12092 ACCURATE_P is non-zero mark display of W as accurate. If
12093 ACCURATE_P is zero, arrange for W to be redisplayed the next time
12094 redisplay_internal is called. */
12095
12096 static void
12097 mark_window_display_accurate_1 (w, accurate_p)
12098 struct window *w;
12099 int accurate_p;
12100 {
12101 if (BUFFERP (w->buffer))
12102 {
12103 struct buffer *b = XBUFFER (w->buffer);
12104
12105 w->last_modified
12106 = make_number (accurate_p ? BUF_MODIFF (b) : 0);
12107 w->last_overlay_modified
12108 = make_number (accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
12109 w->last_had_star
12110 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b) ? Qt : Qnil;
12111
12112 if (accurate_p)
12113 {
12114 b->clip_changed = 0;
12115 b->prevent_redisplay_optimizations_p = 0;
12116
12117 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
12118 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
12119 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
12120 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
12121
12122 w->current_matrix->buffer = b;
12123 w->current_matrix->begv = BUF_BEGV (b);
12124 w->current_matrix->zv = BUF_ZV (b);
12125
12126 w->last_cursor = w->cursor;
12127 w->last_cursor_off_p = w->cursor_off_p;
12128
12129 if (w == XWINDOW (selected_window))
12130 w->last_point = make_number (BUF_PT (b));
12131 else
12132 w->last_point = make_number (XMARKER (w->pointm)->charpos);
12133 }
12134 }
12135
12136 if (accurate_p)
12137 {
12138 w->window_end_valid = w->buffer;
12139 w->update_mode_line = Qnil;
12140 }
12141 }
12142
12143
12144 /* Mark the display of windows in the window tree rooted at WINDOW as
12145 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
12146 windows as accurate. If ACCURATE_P is zero, arrange for windows to
12147 be redisplayed the next time redisplay_internal is called. */
12148
12149 void
12150 mark_window_display_accurate (window, accurate_p)
12151 Lisp_Object window;
12152 int accurate_p;
12153 {
12154 struct window *w;
12155
12156 for (; !NILP (window); window = w->next)
12157 {
12158 w = XWINDOW (window);
12159 mark_window_display_accurate_1 (w, accurate_p);
12160
12161 if (!NILP (w->vchild))
12162 mark_window_display_accurate (w->vchild, accurate_p);
12163 if (!NILP (w->hchild))
12164 mark_window_display_accurate (w->hchild, accurate_p);
12165 }
12166
12167 if (accurate_p)
12168 {
12169 update_overlay_arrows (1);
12170 }
12171 else
12172 {
12173 /* Force a thorough redisplay the next time by setting
12174 last_arrow_position and last_arrow_string to t, which is
12175 unequal to any useful value of Voverlay_arrow_... */
12176 update_overlay_arrows (-1);
12177 }
12178 }
12179
12180
12181 /* Return value in display table DP (Lisp_Char_Table *) for character
12182 C. Since a display table doesn't have any parent, we don't have to
12183 follow parent. Do not call this function directly but use the
12184 macro DISP_CHAR_VECTOR. */
12185
12186 Lisp_Object
12187 disp_char_vector (dp, c)
12188 struct Lisp_Char_Table *dp;
12189 int c;
12190 {
12191 Lisp_Object val;
12192
12193 if (ASCII_CHAR_P (c))
12194 {
12195 val = dp->ascii;
12196 if (SUB_CHAR_TABLE_P (val))
12197 val = XSUB_CHAR_TABLE (val)->contents[c];
12198 }
12199 else
12200 {
12201 Lisp_Object table;
12202
12203 XSETCHAR_TABLE (table, dp);
12204 val = char_table_ref (table, c);
12205 }
12206 if (NILP (val))
12207 val = dp->defalt;
12208 return val;
12209 }
12210
12211
12212 \f
12213 /***********************************************************************
12214 Window Redisplay
12215 ***********************************************************************/
12216
12217 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
12218
12219 static void
12220 redisplay_windows (window)
12221 Lisp_Object window;
12222 {
12223 while (!NILP (window))
12224 {
12225 struct window *w = XWINDOW (window);
12226
12227 if (!NILP (w->hchild))
12228 redisplay_windows (w->hchild);
12229 else if (!NILP (w->vchild))
12230 redisplay_windows (w->vchild);
12231 else if (!NILP (w->buffer))
12232 {
12233 displayed_buffer = XBUFFER (w->buffer);
12234 /* Use list_of_error, not Qerror, so that
12235 we catch only errors and don't run the debugger. */
12236 internal_condition_case_1 (redisplay_window_0, window,
12237 list_of_error,
12238 redisplay_window_error);
12239 }
12240
12241 window = w->next;
12242 }
12243 }
12244
12245 static Lisp_Object
12246 redisplay_window_error ()
12247 {
12248 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
12249 return Qnil;
12250 }
12251
12252 static Lisp_Object
12253 redisplay_window_0 (window)
12254 Lisp_Object window;
12255 {
12256 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
12257 redisplay_window (window, 0);
12258 return Qnil;
12259 }
12260
12261 static Lisp_Object
12262 redisplay_window_1 (window)
12263 Lisp_Object window;
12264 {
12265 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
12266 redisplay_window (window, 1);
12267 return Qnil;
12268 }
12269 \f
12270
12271 /* Increment GLYPH until it reaches END or CONDITION fails while
12272 adding (GLYPH)->pixel_width to X. */
12273
12274 #define SKIP_GLYPHS(glyph, end, x, condition) \
12275 do \
12276 { \
12277 (x) += (glyph)->pixel_width; \
12278 ++(glyph); \
12279 } \
12280 while ((glyph) < (end) && (condition))
12281
12282
12283 /* Set cursor position of W. PT is assumed to be displayed in ROW.
12284 DELTA and DELTA_BYTES are the numbers of characters and bytes by
12285 which positions recorded in ROW differ from current buffer
12286 positions.
12287
12288 Return 0 if cursor is not on this row, 1 otherwise. */
12289
12290 int
12291 set_cursor_from_row (w, row, matrix, delta, delta_bytes, dy, dvpos)
12292 struct window *w;
12293 struct glyph_row *row;
12294 struct glyph_matrix *matrix;
12295 int delta, delta_bytes, dy, dvpos;
12296 {
12297 struct glyph *glyph = row->glyphs[TEXT_AREA];
12298 struct glyph *end = glyph + row->used[TEXT_AREA];
12299 struct glyph *cursor = NULL;
12300 /* The first glyph that starts a sequence of glyphs from a string
12301 that is a value of a display property. */
12302 struct glyph *string_start;
12303 /* The X coordinate of string_start. */
12304 int string_start_x;
12305 /* The last known character position in row. */
12306 int last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
12307 /* The last known character position before string_start. */
12308 int string_before_pos;
12309 int x = row->x;
12310 int cursor_x = x;
12311 /* Last buffer position covered by an overlay. */
12312 int cursor_from_overlay_pos = 0;
12313 int pt_old = PT - delta;
12314
12315 /* Skip over glyphs not having an object at the start of the row.
12316 These are special glyphs like truncation marks on terminal
12317 frames. */
12318 if (row->displays_text_p)
12319 while (glyph < end
12320 && INTEGERP (glyph->object)
12321 && glyph->charpos < 0)
12322 {
12323 x += glyph->pixel_width;
12324 ++glyph;
12325 }
12326
12327 string_start = NULL;
12328 while (glyph < end
12329 && !INTEGERP (glyph->object)
12330 && (!BUFFERP (glyph->object)
12331 || (last_pos = glyph->charpos) < pt_old
12332 || glyph->avoid_cursor_p))
12333 {
12334 if (! STRINGP (glyph->object))
12335 {
12336 string_start = NULL;
12337 x += glyph->pixel_width;
12338 ++glyph;
12339 /* If we are beyond the cursor position computed from the
12340 last overlay seen, that overlay is not in effect for
12341 current cursor position. Reset the cursor information
12342 computed from that overlay. */
12343 if (cursor_from_overlay_pos
12344 && last_pos >= cursor_from_overlay_pos)
12345 {
12346 cursor_from_overlay_pos = 0;
12347 cursor = NULL;
12348 }
12349 }
12350 else
12351 {
12352 if (string_start == NULL)
12353 {
12354 string_before_pos = last_pos;
12355 string_start = glyph;
12356 string_start_x = x;
12357 }
12358 /* Skip all glyphs from a string. */
12359 do
12360 {
12361 Lisp_Object cprop;
12362 int pos;
12363 if ((cursor == NULL || glyph > cursor)
12364 && (cprop = Fget_char_property (make_number ((glyph)->charpos),
12365 Qcursor, (glyph)->object),
12366 !NILP (cprop))
12367 && (pos = string_buffer_position (w, glyph->object,
12368 string_before_pos),
12369 (pos == 0 /* from overlay */
12370 || pos == pt_old)))
12371 {
12372 /* Compute the first buffer position after the overlay.
12373 If the `cursor' property tells us how many positions
12374 are associated with the overlay, use that. Otherwise,
12375 estimate from the buffer positions of the glyphs
12376 before and after the overlay. */
12377 cursor_from_overlay_pos = (pos ? 0 : last_pos
12378 + (INTEGERP (cprop) ? XINT (cprop) : 0));
12379 cursor = glyph;
12380 cursor_x = x;
12381 }
12382 x += glyph->pixel_width;
12383 ++glyph;
12384 }
12385 while (glyph < end && EQ (glyph->object, string_start->object));
12386 }
12387 }
12388
12389 if (cursor != NULL)
12390 {
12391 glyph = cursor;
12392 x = cursor_x;
12393 }
12394 else if (row->ends_in_ellipsis_p && glyph == end)
12395 {
12396 /* Scan back over the ellipsis glyphs, decrementing positions. */
12397 while (glyph > row->glyphs[TEXT_AREA]
12398 && (glyph - 1)->charpos == last_pos)
12399 glyph--, x -= glyph->pixel_width;
12400 /* That loop always goes one position too far, including the
12401 glyph before the ellipsis. So scan forward over that one. */
12402 x += glyph->pixel_width;
12403 glyph++;
12404 }
12405 else if (string_start
12406 && (glyph == end || !BUFFERP (glyph->object) || last_pos > pt_old))
12407 {
12408 /* We may have skipped over point because the previous glyphs
12409 are from string. As there's no easy way to know the
12410 character position of the current glyph, find the correct
12411 glyph on point by scanning from string_start again. */
12412 Lisp_Object limit;
12413 Lisp_Object string;
12414 struct glyph *stop = glyph;
12415 int pos;
12416
12417 limit = make_number (pt_old + 1);
12418 glyph = string_start;
12419 x = string_start_x;
12420 string = glyph->object;
12421 pos = string_buffer_position (w, string, string_before_pos);
12422 /* If POS == 0, STRING is from overlay. We skip such glyphs
12423 because we always put the cursor after overlay strings. */
12424 while (pos == 0 && glyph < stop)
12425 {
12426 string = glyph->object;
12427 SKIP_GLYPHS (glyph, stop, x, EQ (glyph->object, string));
12428 if (glyph < stop)
12429 pos = string_buffer_position (w, glyph->object, string_before_pos);
12430 }
12431
12432 while (glyph < stop)
12433 {
12434 pos = XINT (Fnext_single_char_property_change
12435 (make_number (pos), Qdisplay, Qnil, limit));
12436 if (pos > pt_old)
12437 break;
12438 /* Skip glyphs from the same string. */
12439 string = glyph->object;
12440 SKIP_GLYPHS (glyph, stop, x, EQ (glyph->object, string));
12441 /* Skip glyphs from an overlay. */
12442 while (glyph < stop
12443 && ! string_buffer_position (w, glyph->object, pos))
12444 {
12445 string = glyph->object;
12446 SKIP_GLYPHS (glyph, stop, x, EQ (glyph->object, string));
12447 }
12448 }
12449
12450 /* If we reached the end of the line, and END was from a string,
12451 the cursor is not on this line. */
12452 if (glyph == end && row->continued_p)
12453 return 0;
12454 }
12455
12456 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
12457 w->cursor.x = x;
12458 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
12459 w->cursor.y = row->y + dy;
12460
12461 if (w == XWINDOW (selected_window))
12462 {
12463 if (!row->continued_p
12464 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
12465 && row->x == 0)
12466 {
12467 this_line_buffer = XBUFFER (w->buffer);
12468
12469 CHARPOS (this_line_start_pos)
12470 = MATRIX_ROW_START_CHARPOS (row) + delta;
12471 BYTEPOS (this_line_start_pos)
12472 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
12473
12474 CHARPOS (this_line_end_pos)
12475 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
12476 BYTEPOS (this_line_end_pos)
12477 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
12478
12479 this_line_y = w->cursor.y;
12480 this_line_pixel_height = row->height;
12481 this_line_vpos = w->cursor.vpos;
12482 this_line_start_x = row->x;
12483 }
12484 else
12485 CHARPOS (this_line_start_pos) = 0;
12486 }
12487
12488 return 1;
12489 }
12490
12491
12492 /* Run window scroll functions, if any, for WINDOW with new window
12493 start STARTP. Sets the window start of WINDOW to that position.
12494
12495 We assume that the window's buffer is really current. */
12496
12497 static INLINE struct text_pos
12498 run_window_scroll_functions (window, startp)
12499 Lisp_Object window;
12500 struct text_pos startp;
12501 {
12502 struct window *w = XWINDOW (window);
12503 SET_MARKER_FROM_TEXT_POS (w->start, startp);
12504
12505 if (current_buffer != XBUFFER (w->buffer))
12506 abort ();
12507
12508 if (!NILP (Vwindow_scroll_functions))
12509 {
12510 run_hook_with_args_2 (Qwindow_scroll_functions, window,
12511 make_number (CHARPOS (startp)));
12512 SET_TEXT_POS_FROM_MARKER (startp, w->start);
12513 /* In case the hook functions switch buffers. */
12514 if (current_buffer != XBUFFER (w->buffer))
12515 set_buffer_internal_1 (XBUFFER (w->buffer));
12516 }
12517
12518 return startp;
12519 }
12520
12521
12522 /* Make sure the line containing the cursor is fully visible.
12523 A value of 1 means there is nothing to be done.
12524 (Either the line is fully visible, or it cannot be made so,
12525 or we cannot tell.)
12526
12527 If FORCE_P is non-zero, return 0 even if partial visible cursor row
12528 is higher than window.
12529
12530 A value of 0 means the caller should do scrolling
12531 as if point had gone off the screen. */
12532
12533 static int
12534 cursor_row_fully_visible_p (w, force_p, current_matrix_p)
12535 struct window *w;
12536 int force_p;
12537 int current_matrix_p;
12538 {
12539 struct glyph_matrix *matrix;
12540 struct glyph_row *row;
12541 int window_height;
12542
12543 if (!make_cursor_line_fully_visible_p)
12544 return 1;
12545
12546 /* It's not always possible to find the cursor, e.g, when a window
12547 is full of overlay strings. Don't do anything in that case. */
12548 if (w->cursor.vpos < 0)
12549 return 1;
12550
12551 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
12552 row = MATRIX_ROW (matrix, w->cursor.vpos);
12553
12554 /* If the cursor row is not partially visible, there's nothing to do. */
12555 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
12556 return 1;
12557
12558 /* If the row the cursor is in is taller than the window's height,
12559 it's not clear what to do, so do nothing. */
12560 window_height = window_box_height (w);
12561 if (row->height >= window_height)
12562 {
12563 if (!force_p || MINI_WINDOW_P (w)
12564 || w->vscroll || w->cursor.vpos == 0)
12565 return 1;
12566 }
12567 return 0;
12568 }
12569
12570
12571 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
12572 non-zero means only WINDOW is redisplayed in redisplay_internal.
12573 TEMP_SCROLL_STEP has the same meaning as scroll_step, and is used
12574 in redisplay_window to bring a partially visible line into view in
12575 the case that only the cursor has moved.
12576
12577 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
12578 last screen line's vertical height extends past the end of the screen.
12579
12580 Value is
12581
12582 1 if scrolling succeeded
12583
12584 0 if scrolling didn't find point.
12585
12586 -1 if new fonts have been loaded so that we must interrupt
12587 redisplay, adjust glyph matrices, and try again. */
12588
12589 enum
12590 {
12591 SCROLLING_SUCCESS,
12592 SCROLLING_FAILED,
12593 SCROLLING_NEED_LARGER_MATRICES
12594 };
12595
12596 static int
12597 try_scrolling (window, just_this_one_p, scroll_conservatively,
12598 scroll_step, temp_scroll_step, last_line_misfit)
12599 Lisp_Object window;
12600 int just_this_one_p;
12601 EMACS_INT scroll_conservatively, scroll_step;
12602 int temp_scroll_step;
12603 int last_line_misfit;
12604 {
12605 struct window *w = XWINDOW (window);
12606 struct frame *f = XFRAME (w->frame);
12607 struct text_pos pos, startp;
12608 struct it it;
12609 int this_scroll_margin, scroll_max, rc, height;
12610 int dy = 0, amount_to_scroll = 0, scroll_down_p = 0;
12611 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
12612 Lisp_Object aggressive;
12613 int scroll_limit = INT_MAX / FRAME_LINE_HEIGHT (f);
12614
12615 #if GLYPH_DEBUG
12616 debug_method_add (w, "try_scrolling");
12617 #endif
12618
12619 SET_TEXT_POS_FROM_MARKER (startp, w->start);
12620
12621 /* Compute scroll margin height in pixels. We scroll when point is
12622 within this distance from the top or bottom of the window. */
12623 if (scroll_margin > 0)
12624 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4)
12625 * FRAME_LINE_HEIGHT (f);
12626 else
12627 this_scroll_margin = 0;
12628
12629 /* Force scroll_conservatively to have a reasonable value, to avoid
12630 overflow while computing how much to scroll. Note that the user
12631 can supply scroll-conservatively equal to `most-positive-fixnum',
12632 which can be larger than INT_MAX. */
12633 if (scroll_conservatively > scroll_limit)
12634 {
12635 scroll_conservatively = scroll_limit;
12636 scroll_max = INT_MAX;
12637 }
12638 else if (scroll_step || scroll_conservatively || temp_scroll_step)
12639 /* Compute how much we should try to scroll maximally to bring
12640 point into view. */
12641 scroll_max = (max (scroll_step,
12642 max (scroll_conservatively, temp_scroll_step))
12643 * FRAME_LINE_HEIGHT (f));
12644 else if (NUMBERP (current_buffer->scroll_down_aggressively)
12645 || NUMBERP (current_buffer->scroll_up_aggressively))
12646 /* We're trying to scroll because of aggressive scrolling but no
12647 scroll_step is set. Choose an arbitrary one. */
12648 scroll_max = 10 * FRAME_LINE_HEIGHT (f);
12649 else
12650 scroll_max = 0;
12651
12652 too_near_end:
12653
12654 /* Decide whether to scroll down. */
12655 if (PT > CHARPOS (startp))
12656 {
12657 int scroll_margin_y;
12658
12659 /* Compute the pixel ypos of the scroll margin, then move it to
12660 either that ypos or PT, whichever comes first. */
12661 start_display (&it, w, startp);
12662 scroll_margin_y = it.last_visible_y - this_scroll_margin
12663 - FRAME_LINE_HEIGHT (f) * extra_scroll_margin_lines;
12664 move_it_to (&it, PT, -1, scroll_margin_y - 1, -1,
12665 (MOVE_TO_POS | MOVE_TO_Y));
12666
12667 if (PT > CHARPOS (it.current.pos))
12668 {
12669 int y0 = line_bottom_y (&it);
12670
12671 /* Compute the distance from the scroll margin to PT
12672 (including the height of the cursor line). Moving the
12673 iterator unconditionally to PT can be slow if PT is far
12674 away, so stop 10 lines past the window bottom (is there a
12675 way to do the right thing quickly?). */
12676 move_it_to (&it, PT, -1,
12677 it.last_visible_y + 10 * FRAME_LINE_HEIGHT (f),
12678 -1, MOVE_TO_POS | MOVE_TO_Y);
12679 dy = line_bottom_y (&it) - y0;
12680
12681 if (dy > scroll_max)
12682 return SCROLLING_FAILED;
12683
12684 scroll_down_p = 1;
12685 }
12686 }
12687
12688 if (scroll_down_p)
12689 {
12690 /* Point is in or below the bottom scroll margin, so move the
12691 window start down. If scrolling conservatively, move it just
12692 enough down to make point visible. If scroll_step is set,
12693 move it down by scroll_step. */
12694 if (scroll_conservatively)
12695 amount_to_scroll
12696 = min (max (dy, FRAME_LINE_HEIGHT (f)),
12697 FRAME_LINE_HEIGHT (f) * scroll_conservatively);
12698 else if (scroll_step || temp_scroll_step)
12699 amount_to_scroll = scroll_max;
12700 else
12701 {
12702 aggressive = current_buffer->scroll_up_aggressively;
12703 height = WINDOW_BOX_TEXT_HEIGHT (w);
12704 if (NUMBERP (aggressive))
12705 {
12706 double float_amount = XFLOATINT (aggressive) * height;
12707 amount_to_scroll = float_amount;
12708 if (amount_to_scroll == 0 && float_amount > 0)
12709 amount_to_scroll = 1;
12710 }
12711 }
12712
12713 if (amount_to_scroll <= 0)
12714 return SCROLLING_FAILED;
12715
12716 start_display (&it, w, startp);
12717 move_it_vertically (&it, amount_to_scroll);
12718
12719 /* If STARTP is unchanged, move it down another screen line. */
12720 if (CHARPOS (it.current.pos) == CHARPOS (startp))
12721 move_it_by_lines (&it, 1, 1);
12722 startp = it.current.pos;
12723 }
12724 else
12725 {
12726 struct text_pos scroll_margin_pos = startp;
12727
12728 /* See if point is inside the scroll margin at the top of the
12729 window. */
12730 if (this_scroll_margin)
12731 {
12732 start_display (&it, w, startp);
12733 move_it_vertically (&it, this_scroll_margin);
12734 scroll_margin_pos = it.current.pos;
12735 }
12736
12737 if (PT < CHARPOS (scroll_margin_pos))
12738 {
12739 /* Point is in the scroll margin at the top of the window or
12740 above what is displayed in the window. */
12741 int y0;
12742
12743 /* Compute the vertical distance from PT to the scroll
12744 margin position. Give up if distance is greater than
12745 scroll_max. */
12746 SET_TEXT_POS (pos, PT, PT_BYTE);
12747 start_display (&it, w, pos);
12748 y0 = it.current_y;
12749 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
12750 it.last_visible_y, -1,
12751 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
12752 dy = it.current_y - y0;
12753 if (dy > scroll_max)
12754 return SCROLLING_FAILED;
12755
12756 /* Compute new window start. */
12757 start_display (&it, w, startp);
12758
12759 if (scroll_conservatively)
12760 amount_to_scroll
12761 = max (dy, FRAME_LINE_HEIGHT (f) * max (scroll_step, temp_scroll_step));
12762 else if (scroll_step || temp_scroll_step)
12763 amount_to_scroll = scroll_max;
12764 else
12765 {
12766 aggressive = current_buffer->scroll_down_aggressively;
12767 height = WINDOW_BOX_TEXT_HEIGHT (w);
12768 if (NUMBERP (aggressive))
12769 {
12770 double float_amount = XFLOATINT (aggressive) * height;
12771 amount_to_scroll = float_amount;
12772 if (amount_to_scroll == 0 && float_amount > 0)
12773 amount_to_scroll = 1;
12774 }
12775 }
12776
12777 if (amount_to_scroll <= 0)
12778 return SCROLLING_FAILED;
12779
12780 move_it_vertically_backward (&it, amount_to_scroll);
12781 startp = it.current.pos;
12782 }
12783 }
12784
12785 /* Run window scroll functions. */
12786 startp = run_window_scroll_functions (window, startp);
12787
12788 /* Display the window. Give up if new fonts are loaded, or if point
12789 doesn't appear. */
12790 if (!try_window (window, startp, 0))
12791 rc = SCROLLING_NEED_LARGER_MATRICES;
12792 else if (w->cursor.vpos < 0)
12793 {
12794 clear_glyph_matrix (w->desired_matrix);
12795 rc = SCROLLING_FAILED;
12796 }
12797 else
12798 {
12799 /* Maybe forget recorded base line for line number display. */
12800 if (!just_this_one_p
12801 || current_buffer->clip_changed
12802 || BEG_UNCHANGED < CHARPOS (startp))
12803 w->base_line_number = Qnil;
12804
12805 /* If cursor ends up on a partially visible line,
12806 treat that as being off the bottom of the screen. */
12807 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1, 0))
12808 {
12809 clear_glyph_matrix (w->desired_matrix);
12810 ++extra_scroll_margin_lines;
12811 goto too_near_end;
12812 }
12813 rc = SCROLLING_SUCCESS;
12814 }
12815
12816 return rc;
12817 }
12818
12819
12820 /* Compute a suitable window start for window W if display of W starts
12821 on a continuation line. Value is non-zero if a new window start
12822 was computed.
12823
12824 The new window start will be computed, based on W's width, starting
12825 from the start of the continued line. It is the start of the
12826 screen line with the minimum distance from the old start W->start. */
12827
12828 static int
12829 compute_window_start_on_continuation_line (w)
12830 struct window *w;
12831 {
12832 struct text_pos pos, start_pos;
12833 int window_start_changed_p = 0;
12834
12835 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
12836
12837 /* If window start is on a continuation line... Window start may be
12838 < BEGV in case there's invisible text at the start of the
12839 buffer (M-x rmail, for example). */
12840 if (CHARPOS (start_pos) > BEGV
12841 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
12842 {
12843 struct it it;
12844 struct glyph_row *row;
12845
12846 /* Handle the case that the window start is out of range. */
12847 if (CHARPOS (start_pos) < BEGV)
12848 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
12849 else if (CHARPOS (start_pos) > ZV)
12850 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
12851
12852 /* Find the start of the continued line. This should be fast
12853 because scan_buffer is fast (newline cache). */
12854 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
12855 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
12856 row, DEFAULT_FACE_ID);
12857 reseat_at_previous_visible_line_start (&it);
12858
12859 /* If the line start is "too far" away from the window start,
12860 say it takes too much time to compute a new window start. */
12861 if (CHARPOS (start_pos) - IT_CHARPOS (it)
12862 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
12863 {
12864 int min_distance, distance;
12865
12866 /* Move forward by display lines to find the new window
12867 start. If window width was enlarged, the new start can
12868 be expected to be > the old start. If window width was
12869 decreased, the new window start will be < the old start.
12870 So, we're looking for the display line start with the
12871 minimum distance from the old window start. */
12872 pos = it.current.pos;
12873 min_distance = INFINITY;
12874 while ((distance = eabs (CHARPOS (start_pos) - IT_CHARPOS (it))),
12875 distance < min_distance)
12876 {
12877 min_distance = distance;
12878 pos = it.current.pos;
12879 move_it_by_lines (&it, 1, 0);
12880 }
12881
12882 /* Set the window start there. */
12883 SET_MARKER_FROM_TEXT_POS (w->start, pos);
12884 window_start_changed_p = 1;
12885 }
12886 }
12887
12888 return window_start_changed_p;
12889 }
12890
12891
12892 /* Try cursor movement in case text has not changed in window WINDOW,
12893 with window start STARTP. Value is
12894
12895 CURSOR_MOVEMENT_SUCCESS if successful
12896
12897 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
12898
12899 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
12900 display. *SCROLL_STEP is set to 1, under certain circumstances, if
12901 we want to scroll as if scroll-step were set to 1. See the code.
12902
12903 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
12904 which case we have to abort this redisplay, and adjust matrices
12905 first. */
12906
12907 enum
12908 {
12909 CURSOR_MOVEMENT_SUCCESS,
12910 CURSOR_MOVEMENT_CANNOT_BE_USED,
12911 CURSOR_MOVEMENT_MUST_SCROLL,
12912 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
12913 };
12914
12915 static int
12916 try_cursor_movement (window, startp, scroll_step)
12917 Lisp_Object window;
12918 struct text_pos startp;
12919 int *scroll_step;
12920 {
12921 struct window *w = XWINDOW (window);
12922 struct frame *f = XFRAME (w->frame);
12923 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
12924
12925 #if GLYPH_DEBUG
12926 if (inhibit_try_cursor_movement)
12927 return rc;
12928 #endif
12929
12930 /* Handle case where text has not changed, only point, and it has
12931 not moved off the frame. */
12932 if (/* Point may be in this window. */
12933 PT >= CHARPOS (startp)
12934 /* Selective display hasn't changed. */
12935 && !current_buffer->clip_changed
12936 /* Function force-mode-line-update is used to force a thorough
12937 redisplay. It sets either windows_or_buffers_changed or
12938 update_mode_lines. So don't take a shortcut here for these
12939 cases. */
12940 && !update_mode_lines
12941 && !windows_or_buffers_changed
12942 && !cursor_type_changed
12943 /* Can't use this case if highlighting a region. When a
12944 region exists, cursor movement has to do more than just
12945 set the cursor. */
12946 && !(!NILP (Vtransient_mark_mode)
12947 && !NILP (current_buffer->mark_active))
12948 && NILP (w->region_showing)
12949 && NILP (Vshow_trailing_whitespace)
12950 /* Right after splitting windows, last_point may be nil. */
12951 && INTEGERP (w->last_point)
12952 /* This code is not used for mini-buffer for the sake of the case
12953 of redisplaying to replace an echo area message; since in
12954 that case the mini-buffer contents per se are usually
12955 unchanged. This code is of no real use in the mini-buffer
12956 since the handling of this_line_start_pos, etc., in redisplay
12957 handles the same cases. */
12958 && !EQ (window, minibuf_window)
12959 /* When splitting windows or for new windows, it happens that
12960 redisplay is called with a nil window_end_vpos or one being
12961 larger than the window. This should really be fixed in
12962 window.c. I don't have this on my list, now, so we do
12963 approximately the same as the old redisplay code. --gerd. */
12964 && INTEGERP (w->window_end_vpos)
12965 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
12966 && (FRAME_WINDOW_P (f)
12967 || !overlay_arrow_in_current_buffer_p ()))
12968 {
12969 int this_scroll_margin, top_scroll_margin;
12970 struct glyph_row *row = NULL;
12971
12972 #if GLYPH_DEBUG
12973 debug_method_add (w, "cursor movement");
12974 #endif
12975
12976 /* Scroll if point within this distance from the top or bottom
12977 of the window. This is a pixel value. */
12978 if (scroll_margin > 0)
12979 {
12980 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
12981 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
12982 }
12983 else
12984 this_scroll_margin = 0;
12985
12986 top_scroll_margin = this_scroll_margin;
12987 if (WINDOW_WANTS_HEADER_LINE_P (w))
12988 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
12989
12990 /* Start with the row the cursor was displayed during the last
12991 not paused redisplay. Give up if that row is not valid. */
12992 if (w->last_cursor.vpos < 0
12993 || w->last_cursor.vpos >= w->current_matrix->nrows)
12994 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12995 else
12996 {
12997 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
12998 if (row->mode_line_p)
12999 ++row;
13000 if (!row->enabled_p)
13001 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13002 }
13003
13004 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
13005 {
13006 int scroll_p = 0;
13007 int last_y = window_text_bottom_y (w) - this_scroll_margin;
13008
13009 if (PT > XFASTINT (w->last_point))
13010 {
13011 /* Point has moved forward. */
13012 while (MATRIX_ROW_END_CHARPOS (row) < PT
13013 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
13014 {
13015 xassert (row->enabled_p);
13016 ++row;
13017 }
13018
13019 /* The end position of a row equals the start position
13020 of the next row. If PT is there, we would rather
13021 display it in the next line. */
13022 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
13023 && MATRIX_ROW_END_CHARPOS (row) == PT
13024 && !cursor_row_p (w, row))
13025 ++row;
13026
13027 /* If within the scroll margin, scroll. Note that
13028 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
13029 the next line would be drawn, and that
13030 this_scroll_margin can be zero. */
13031 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
13032 || PT > MATRIX_ROW_END_CHARPOS (row)
13033 /* Line is completely visible last line in window
13034 and PT is to be set in the next line. */
13035 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
13036 && PT == MATRIX_ROW_END_CHARPOS (row)
13037 && !row->ends_at_zv_p
13038 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
13039 scroll_p = 1;
13040 }
13041 else if (PT < XFASTINT (w->last_point))
13042 {
13043 /* Cursor has to be moved backward. Note that PT >=
13044 CHARPOS (startp) because of the outer if-statement. */
13045 while (!row->mode_line_p
13046 && (MATRIX_ROW_START_CHARPOS (row) > PT
13047 || (MATRIX_ROW_START_CHARPOS (row) == PT
13048 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
13049 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
13050 row > w->current_matrix->rows
13051 && (row-1)->ends_in_newline_from_string_p))))
13052 && (row->y > top_scroll_margin
13053 || CHARPOS (startp) == BEGV))
13054 {
13055 xassert (row->enabled_p);
13056 --row;
13057 }
13058
13059 /* Consider the following case: Window starts at BEGV,
13060 there is invisible, intangible text at BEGV, so that
13061 display starts at some point START > BEGV. It can
13062 happen that we are called with PT somewhere between
13063 BEGV and START. Try to handle that case. */
13064 if (row < w->current_matrix->rows
13065 || row->mode_line_p)
13066 {
13067 row = w->current_matrix->rows;
13068 if (row->mode_line_p)
13069 ++row;
13070 }
13071
13072 /* Due to newlines in overlay strings, we may have to
13073 skip forward over overlay strings. */
13074 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
13075 && MATRIX_ROW_END_CHARPOS (row) == PT
13076 && !cursor_row_p (w, row))
13077 ++row;
13078
13079 /* If within the scroll margin, scroll. */
13080 if (row->y < top_scroll_margin
13081 && CHARPOS (startp) != BEGV)
13082 scroll_p = 1;
13083 }
13084 else
13085 {
13086 /* Cursor did not move. So don't scroll even if cursor line
13087 is partially visible, as it was so before. */
13088 rc = CURSOR_MOVEMENT_SUCCESS;
13089 }
13090
13091 if (PT < MATRIX_ROW_START_CHARPOS (row)
13092 || PT > MATRIX_ROW_END_CHARPOS (row))
13093 {
13094 /* if PT is not in the glyph row, give up. */
13095 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13096 }
13097 else if (rc != CURSOR_MOVEMENT_SUCCESS
13098 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
13099 && make_cursor_line_fully_visible_p)
13100 {
13101 if (PT == MATRIX_ROW_END_CHARPOS (row)
13102 && !row->ends_at_zv_p
13103 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
13104 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13105 else if (row->height > window_box_height (w))
13106 {
13107 /* If we end up in a partially visible line, let's
13108 make it fully visible, except when it's taller
13109 than the window, in which case we can't do much
13110 about it. */
13111 *scroll_step = 1;
13112 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13113 }
13114 else
13115 {
13116 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13117 if (!cursor_row_fully_visible_p (w, 0, 1))
13118 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13119 else
13120 rc = CURSOR_MOVEMENT_SUCCESS;
13121 }
13122 }
13123 else if (scroll_p)
13124 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13125 else
13126 {
13127 do
13128 {
13129 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
13130 {
13131 rc = CURSOR_MOVEMENT_SUCCESS;
13132 break;
13133 }
13134 ++row;
13135 }
13136 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
13137 && MATRIX_ROW_START_CHARPOS (row) == PT
13138 && cursor_row_p (w, row));
13139 }
13140 }
13141 }
13142
13143 return rc;
13144 }
13145
13146 void
13147 set_vertical_scroll_bar (w)
13148 struct window *w;
13149 {
13150 int start, end, whole;
13151
13152 /* Calculate the start and end positions for the current window.
13153 At some point, it would be nice to choose between scrollbars
13154 which reflect the whole buffer size, with special markers
13155 indicating narrowing, and scrollbars which reflect only the
13156 visible region.
13157
13158 Note that mini-buffers sometimes aren't displaying any text. */
13159 if (!MINI_WINDOW_P (w)
13160 || (w == XWINDOW (minibuf_window)
13161 && NILP (echo_area_buffer[0])))
13162 {
13163 struct buffer *buf = XBUFFER (w->buffer);
13164 whole = BUF_ZV (buf) - BUF_BEGV (buf);
13165 start = marker_position (w->start) - BUF_BEGV (buf);
13166 /* I don't think this is guaranteed to be right. For the
13167 moment, we'll pretend it is. */
13168 end = BUF_Z (buf) - XFASTINT (w->window_end_pos) - BUF_BEGV (buf);
13169
13170 if (end < start)
13171 end = start;
13172 if (whole < (end - start))
13173 whole = end - start;
13174 }
13175 else
13176 start = end = whole = 0;
13177
13178 /* Indicate what this scroll bar ought to be displaying now. */
13179 if (FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
13180 (*FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
13181 (w, end - start, whole, start);
13182 }
13183
13184
13185 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
13186 selected_window is redisplayed.
13187
13188 We can return without actually redisplaying the window if
13189 fonts_changed_p is nonzero. In that case, redisplay_internal will
13190 retry. */
13191
13192 static void
13193 redisplay_window (window, just_this_one_p)
13194 Lisp_Object window;
13195 int just_this_one_p;
13196 {
13197 struct window *w = XWINDOW (window);
13198 struct frame *f = XFRAME (w->frame);
13199 struct buffer *buffer = XBUFFER (w->buffer);
13200 struct buffer *old = current_buffer;
13201 struct text_pos lpoint, opoint, startp;
13202 int update_mode_line;
13203 int tem;
13204 struct it it;
13205 /* Record it now because it's overwritten. */
13206 int current_matrix_up_to_date_p = 0;
13207 int used_current_matrix_p = 0;
13208 /* This is less strict than current_matrix_up_to_date_p.
13209 It indictes that the buffer contents and narrowing are unchanged. */
13210 int buffer_unchanged_p = 0;
13211 int temp_scroll_step = 0;
13212 int count = SPECPDL_INDEX ();
13213 int rc;
13214 int centering_position = -1;
13215 int last_line_misfit = 0;
13216 int beg_unchanged, end_unchanged;
13217
13218 SET_TEXT_POS (lpoint, PT, PT_BYTE);
13219 opoint = lpoint;
13220
13221 /* W must be a leaf window here. */
13222 xassert (!NILP (w->buffer));
13223 #if GLYPH_DEBUG
13224 *w->desired_matrix->method = 0;
13225 #endif
13226
13227 restart:
13228 reconsider_clip_changes (w, buffer);
13229
13230 /* Has the mode line to be updated? */
13231 update_mode_line = (!NILP (w->update_mode_line)
13232 || update_mode_lines
13233 || buffer->clip_changed
13234 || buffer->prevent_redisplay_optimizations_p);
13235
13236 if (MINI_WINDOW_P (w))
13237 {
13238 if (w == XWINDOW (echo_area_window)
13239 && !NILP (echo_area_buffer[0]))
13240 {
13241 if (update_mode_line)
13242 /* We may have to update a tty frame's menu bar or a
13243 tool-bar. Example `M-x C-h C-h C-g'. */
13244 goto finish_menu_bars;
13245 else
13246 /* We've already displayed the echo area glyphs in this window. */
13247 goto finish_scroll_bars;
13248 }
13249 else if ((w != XWINDOW (minibuf_window)
13250 || minibuf_level == 0)
13251 /* When buffer is nonempty, redisplay window normally. */
13252 && BUF_Z (XBUFFER (w->buffer)) == BUF_BEG (XBUFFER (w->buffer))
13253 /* Quail displays non-mini buffers in minibuffer window.
13254 In that case, redisplay the window normally. */
13255 && !NILP (Fmemq (w->buffer, Vminibuffer_list)))
13256 {
13257 /* W is a mini-buffer window, but it's not active, so clear
13258 it. */
13259 int yb = window_text_bottom_y (w);
13260 struct glyph_row *row;
13261 int y;
13262
13263 for (y = 0, row = w->desired_matrix->rows;
13264 y < yb;
13265 y += row->height, ++row)
13266 blank_row (w, row, y);
13267 goto finish_scroll_bars;
13268 }
13269
13270 clear_glyph_matrix (w->desired_matrix);
13271 }
13272
13273 /* Otherwise set up data on this window; select its buffer and point
13274 value. */
13275 /* Really select the buffer, for the sake of buffer-local
13276 variables. */
13277 set_buffer_internal_1 (XBUFFER (w->buffer));
13278
13279 current_matrix_up_to_date_p
13280 = (!NILP (w->window_end_valid)
13281 && !current_buffer->clip_changed
13282 && !current_buffer->prevent_redisplay_optimizations_p
13283 && XFASTINT (w->last_modified) >= MODIFF
13284 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
13285
13286 /* Run the window-bottom-change-functions
13287 if it is possible that the text on the screen has changed
13288 (either due to modification of the text, or any other reason). */
13289 if (!current_matrix_up_to_date_p
13290 && !NILP (Vwindow_text_change_functions))
13291 {
13292 safe_run_hooks (Qwindow_text_change_functions);
13293 goto restart;
13294 }
13295
13296 beg_unchanged = BEG_UNCHANGED;
13297 end_unchanged = END_UNCHANGED;
13298
13299 SET_TEXT_POS (opoint, PT, PT_BYTE);
13300
13301 specbind (Qinhibit_point_motion_hooks, Qt);
13302
13303 buffer_unchanged_p
13304 = (!NILP (w->window_end_valid)
13305 && !current_buffer->clip_changed
13306 && XFASTINT (w->last_modified) >= MODIFF
13307 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
13308
13309 /* When windows_or_buffers_changed is non-zero, we can't rely on
13310 the window end being valid, so set it to nil there. */
13311 if (windows_or_buffers_changed)
13312 {
13313 /* If window starts on a continuation line, maybe adjust the
13314 window start in case the window's width changed. */
13315 if (XMARKER (w->start)->buffer == current_buffer)
13316 compute_window_start_on_continuation_line (w);
13317
13318 w->window_end_valid = Qnil;
13319 }
13320
13321 /* Some sanity checks. */
13322 CHECK_WINDOW_END (w);
13323 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
13324 abort ();
13325 if (BYTEPOS (opoint) < CHARPOS (opoint))
13326 abort ();
13327
13328 /* If %c is in mode line, update it if needed. */
13329 if (!NILP (w->column_number_displayed)
13330 /* This alternative quickly identifies a common case
13331 where no change is needed. */
13332 && !(PT == XFASTINT (w->last_point)
13333 && XFASTINT (w->last_modified) >= MODIFF
13334 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
13335 && (XFASTINT (w->column_number_displayed)
13336 != (int) current_column ())) /* iftc */
13337 update_mode_line = 1;
13338
13339 /* Count number of windows showing the selected buffer. An indirect
13340 buffer counts as its base buffer. */
13341 if (!just_this_one_p)
13342 {
13343 struct buffer *current_base, *window_base;
13344 current_base = current_buffer;
13345 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
13346 if (current_base->base_buffer)
13347 current_base = current_base->base_buffer;
13348 if (window_base->base_buffer)
13349 window_base = window_base->base_buffer;
13350 if (current_base == window_base)
13351 buffer_shared++;
13352 }
13353
13354 /* Point refers normally to the selected window. For any other
13355 window, set up appropriate value. */
13356 if (!EQ (window, selected_window))
13357 {
13358 int new_pt = XMARKER (w->pointm)->charpos;
13359 int new_pt_byte = marker_byte_position (w->pointm);
13360 if (new_pt < BEGV)
13361 {
13362 new_pt = BEGV;
13363 new_pt_byte = BEGV_BYTE;
13364 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
13365 }
13366 else if (new_pt > (ZV - 1))
13367 {
13368 new_pt = ZV;
13369 new_pt_byte = ZV_BYTE;
13370 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
13371 }
13372
13373 /* We don't use SET_PT so that the point-motion hooks don't run. */
13374 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
13375 }
13376
13377 /* If any of the character widths specified in the display table
13378 have changed, invalidate the width run cache. It's true that
13379 this may be a bit late to catch such changes, but the rest of
13380 redisplay goes (non-fatally) haywire when the display table is
13381 changed, so why should we worry about doing any better? */
13382 if (current_buffer->width_run_cache)
13383 {
13384 struct Lisp_Char_Table *disptab = buffer_display_table ();
13385
13386 if (! disptab_matches_widthtab (disptab,
13387 XVECTOR (current_buffer->width_table)))
13388 {
13389 invalidate_region_cache (current_buffer,
13390 current_buffer->width_run_cache,
13391 BEG, Z);
13392 recompute_width_table (current_buffer, disptab);
13393 }
13394 }
13395
13396 /* If window-start is screwed up, choose a new one. */
13397 if (XMARKER (w->start)->buffer != current_buffer)
13398 goto recenter;
13399
13400 SET_TEXT_POS_FROM_MARKER (startp, w->start);
13401
13402 /* If someone specified a new starting point but did not insist,
13403 check whether it can be used. */
13404 if (!NILP (w->optional_new_start)
13405 && CHARPOS (startp) >= BEGV
13406 && CHARPOS (startp) <= ZV)
13407 {
13408 w->optional_new_start = Qnil;
13409 start_display (&it, w, startp);
13410 move_it_to (&it, PT, 0, it.last_visible_y, -1,
13411 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
13412 if (IT_CHARPOS (it) == PT)
13413 w->force_start = Qt;
13414 /* IT may overshoot PT if text at PT is invisible. */
13415 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
13416 w->force_start = Qt;
13417 }
13418
13419 force_start:
13420
13421 /* Handle case where place to start displaying has been specified,
13422 unless the specified location is outside the accessible range. */
13423 if (!NILP (w->force_start)
13424 || w->frozen_window_start_p)
13425 {
13426 /* We set this later on if we have to adjust point. */
13427 int new_vpos = -1;
13428
13429 w->force_start = Qnil;
13430 w->vscroll = 0;
13431 w->window_end_valid = Qnil;
13432
13433 /* Forget any recorded base line for line number display. */
13434 if (!buffer_unchanged_p)
13435 w->base_line_number = Qnil;
13436
13437 /* Redisplay the mode line. Select the buffer properly for that.
13438 Also, run the hook window-scroll-functions
13439 because we have scrolled. */
13440 /* Note, we do this after clearing force_start because
13441 if there's an error, it is better to forget about force_start
13442 than to get into an infinite loop calling the hook functions
13443 and having them get more errors. */
13444 if (!update_mode_line
13445 || ! NILP (Vwindow_scroll_functions))
13446 {
13447 update_mode_line = 1;
13448 w->update_mode_line = Qt;
13449 startp = run_window_scroll_functions (window, startp);
13450 }
13451
13452 w->last_modified = make_number (0);
13453 w->last_overlay_modified = make_number (0);
13454 if (CHARPOS (startp) < BEGV)
13455 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
13456 else if (CHARPOS (startp) > ZV)
13457 SET_TEXT_POS (startp, ZV, ZV_BYTE);
13458
13459 /* Redisplay, then check if cursor has been set during the
13460 redisplay. Give up if new fonts were loaded. */
13461 /* We used to issue a CHECK_MARGINS argument to try_window here,
13462 but this causes scrolling to fail when point begins inside
13463 the scroll margin (bug#148) -- cyd */
13464 if (!try_window (window, startp, 0))
13465 {
13466 w->force_start = Qt;
13467 clear_glyph_matrix (w->desired_matrix);
13468 goto need_larger_matrices;
13469 }
13470
13471 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
13472 {
13473 /* If point does not appear, try to move point so it does
13474 appear. The desired matrix has been built above, so we
13475 can use it here. */
13476 new_vpos = window_box_height (w) / 2;
13477 }
13478
13479 if (!cursor_row_fully_visible_p (w, 0, 0))
13480 {
13481 /* Point does appear, but on a line partly visible at end of window.
13482 Move it back to a fully-visible line. */
13483 new_vpos = window_box_height (w);
13484 }
13485
13486 /* If we need to move point for either of the above reasons,
13487 now actually do it. */
13488 if (new_vpos >= 0)
13489 {
13490 struct glyph_row *row;
13491
13492 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
13493 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
13494 ++row;
13495
13496 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
13497 MATRIX_ROW_START_BYTEPOS (row));
13498
13499 if (w != XWINDOW (selected_window))
13500 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
13501 else if (current_buffer == old)
13502 SET_TEXT_POS (lpoint, PT, PT_BYTE);
13503
13504 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
13505
13506 /* If we are highlighting the region, then we just changed
13507 the region, so redisplay to show it. */
13508 if (!NILP (Vtransient_mark_mode)
13509 && !NILP (current_buffer->mark_active))
13510 {
13511 clear_glyph_matrix (w->desired_matrix);
13512 if (!try_window (window, startp, 0))
13513 goto need_larger_matrices;
13514 }
13515 }
13516
13517 #if GLYPH_DEBUG
13518 debug_method_add (w, "forced window start");
13519 #endif
13520 goto done;
13521 }
13522
13523 /* Handle case where text has not changed, only point, and it has
13524 not moved off the frame, and we are not retrying after hscroll.
13525 (current_matrix_up_to_date_p is nonzero when retrying.) */
13526 if (current_matrix_up_to_date_p
13527 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
13528 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
13529 {
13530 switch (rc)
13531 {
13532 case CURSOR_MOVEMENT_SUCCESS:
13533 used_current_matrix_p = 1;
13534 goto done;
13535
13536 case CURSOR_MOVEMENT_MUST_SCROLL:
13537 goto try_to_scroll;
13538
13539 default:
13540 abort ();
13541 }
13542 }
13543 /* If current starting point was originally the beginning of a line
13544 but no longer is, find a new starting point. */
13545 else if (!NILP (w->start_at_line_beg)
13546 && !(CHARPOS (startp) <= BEGV
13547 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
13548 {
13549 #if GLYPH_DEBUG
13550 debug_method_add (w, "recenter 1");
13551 #endif
13552 goto recenter;
13553 }
13554
13555 /* Try scrolling with try_window_id. Value is > 0 if update has
13556 been done, it is -1 if we know that the same window start will
13557 not work. It is 0 if unsuccessful for some other reason. */
13558 else if ((tem = try_window_id (w)) != 0)
13559 {
13560 #if GLYPH_DEBUG
13561 debug_method_add (w, "try_window_id %d", tem);
13562 #endif
13563
13564 if (fonts_changed_p)
13565 goto need_larger_matrices;
13566 if (tem > 0)
13567 goto done;
13568
13569 /* Otherwise try_window_id has returned -1 which means that we
13570 don't want the alternative below this comment to execute. */
13571 }
13572 else if (CHARPOS (startp) >= BEGV
13573 && CHARPOS (startp) <= ZV
13574 && PT >= CHARPOS (startp)
13575 && (CHARPOS (startp) < ZV
13576 /* Avoid starting at end of buffer. */
13577 || CHARPOS (startp) == BEGV
13578 || (XFASTINT (w->last_modified) >= MODIFF
13579 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
13580 {
13581
13582 /* If first window line is a continuation line, and window start
13583 is inside the modified region, but the first change is before
13584 current window start, we must select a new window start.
13585
13586 However, if this is the result of a down-mouse event (e.g. by
13587 extending the mouse-drag-overlay), we don't want to select a
13588 new window start, since that would change the position under
13589 the mouse, resulting in an unwanted mouse-movement rather
13590 than a simple mouse-click. */
13591 if (NILP (w->start_at_line_beg)
13592 && NILP (do_mouse_tracking)
13593 && CHARPOS (startp) > BEGV
13594 && CHARPOS (startp) > BEG + beg_unchanged
13595 && CHARPOS (startp) <= Z - end_unchanged
13596 /* Even if w->start_at_line_beg is nil, a new window may
13597 start at a line_beg, since that's how set_buffer_window
13598 sets it. So, we need to check the return value of
13599 compute_window_start_on_continuation_line. (See also
13600 bug#197). */
13601 && XMARKER (w->start)->buffer == current_buffer
13602 && compute_window_start_on_continuation_line (w))
13603 {
13604 w->force_start = Qt;
13605 SET_TEXT_POS_FROM_MARKER (startp, w->start);
13606 goto force_start;
13607 }
13608
13609 #if GLYPH_DEBUG
13610 debug_method_add (w, "same window start");
13611 #endif
13612
13613 /* Try to redisplay starting at same place as before.
13614 If point has not moved off frame, accept the results. */
13615 if (!current_matrix_up_to_date_p
13616 /* Don't use try_window_reusing_current_matrix in this case
13617 because a window scroll function can have changed the
13618 buffer. */
13619 || !NILP (Vwindow_scroll_functions)
13620 || MINI_WINDOW_P (w)
13621 || !(used_current_matrix_p
13622 = try_window_reusing_current_matrix (w)))
13623 {
13624 IF_DEBUG (debug_method_add (w, "1"));
13625 if (try_window (window, startp, 1) < 0)
13626 /* -1 means we need to scroll.
13627 0 means we need new matrices, but fonts_changed_p
13628 is set in that case, so we will detect it below. */
13629 goto try_to_scroll;
13630 }
13631
13632 if (fonts_changed_p)
13633 goto need_larger_matrices;
13634
13635 if (w->cursor.vpos >= 0)
13636 {
13637 if (!just_this_one_p
13638 || current_buffer->clip_changed
13639 || BEG_UNCHANGED < CHARPOS (startp))
13640 /* Forget any recorded base line for line number display. */
13641 w->base_line_number = Qnil;
13642
13643 if (!cursor_row_fully_visible_p (w, 1, 0))
13644 {
13645 clear_glyph_matrix (w->desired_matrix);
13646 last_line_misfit = 1;
13647 }
13648 /* Drop through and scroll. */
13649 else
13650 goto done;
13651 }
13652 else
13653 clear_glyph_matrix (w->desired_matrix);
13654 }
13655
13656 try_to_scroll:
13657
13658 w->last_modified = make_number (0);
13659 w->last_overlay_modified = make_number (0);
13660
13661 /* Redisplay the mode line. Select the buffer properly for that. */
13662 if (!update_mode_line)
13663 {
13664 update_mode_line = 1;
13665 w->update_mode_line = Qt;
13666 }
13667
13668 /* Try to scroll by specified few lines. */
13669 if ((scroll_conservatively
13670 || scroll_step
13671 || temp_scroll_step
13672 || NUMBERP (current_buffer->scroll_up_aggressively)
13673 || NUMBERP (current_buffer->scroll_down_aggressively))
13674 && !current_buffer->clip_changed
13675 && CHARPOS (startp) >= BEGV
13676 && CHARPOS (startp) <= ZV)
13677 {
13678 /* The function returns -1 if new fonts were loaded, 1 if
13679 successful, 0 if not successful. */
13680 int rc = try_scrolling (window, just_this_one_p,
13681 scroll_conservatively,
13682 scroll_step,
13683 temp_scroll_step, last_line_misfit);
13684 switch (rc)
13685 {
13686 case SCROLLING_SUCCESS:
13687 goto done;
13688
13689 case SCROLLING_NEED_LARGER_MATRICES:
13690 goto need_larger_matrices;
13691
13692 case SCROLLING_FAILED:
13693 break;
13694
13695 default:
13696 abort ();
13697 }
13698 }
13699
13700 /* Finally, just choose place to start which centers point */
13701
13702 recenter:
13703 if (centering_position < 0)
13704 centering_position = window_box_height (w) / 2;
13705
13706 #if GLYPH_DEBUG
13707 debug_method_add (w, "recenter");
13708 #endif
13709
13710 /* w->vscroll = 0; */
13711
13712 /* Forget any previously recorded base line for line number display. */
13713 if (!buffer_unchanged_p)
13714 w->base_line_number = Qnil;
13715
13716 /* Move backward half the height of the window. */
13717 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
13718 it.current_y = it.last_visible_y;
13719 move_it_vertically_backward (&it, centering_position);
13720 xassert (IT_CHARPOS (it) >= BEGV);
13721
13722 /* The function move_it_vertically_backward may move over more
13723 than the specified y-distance. If it->w is small, e.g. a
13724 mini-buffer window, we may end up in front of the window's
13725 display area. Start displaying at the start of the line
13726 containing PT in this case. */
13727 if (it.current_y <= 0)
13728 {
13729 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
13730 move_it_vertically_backward (&it, 0);
13731 it.current_y = 0;
13732 }
13733
13734 it.current_x = it.hpos = 0;
13735
13736 /* Set startp here explicitly in case that helps avoid an infinite loop
13737 in case the window-scroll-functions functions get errors. */
13738 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
13739
13740 /* Run scroll hooks. */
13741 startp = run_window_scroll_functions (window, it.current.pos);
13742
13743 /* Redisplay the window. */
13744 if (!current_matrix_up_to_date_p
13745 || windows_or_buffers_changed
13746 || cursor_type_changed
13747 /* Don't use try_window_reusing_current_matrix in this case
13748 because it can have changed the buffer. */
13749 || !NILP (Vwindow_scroll_functions)
13750 || !just_this_one_p
13751 || MINI_WINDOW_P (w)
13752 || !(used_current_matrix_p
13753 = try_window_reusing_current_matrix (w)))
13754 try_window (window, startp, 0);
13755
13756 /* If new fonts have been loaded (due to fontsets), give up. We
13757 have to start a new redisplay since we need to re-adjust glyph
13758 matrices. */
13759 if (fonts_changed_p)
13760 goto need_larger_matrices;
13761
13762 /* If cursor did not appear assume that the middle of the window is
13763 in the first line of the window. Do it again with the next line.
13764 (Imagine a window of height 100, displaying two lines of height
13765 60. Moving back 50 from it->last_visible_y will end in the first
13766 line.) */
13767 if (w->cursor.vpos < 0)
13768 {
13769 if (!NILP (w->window_end_valid)
13770 && PT >= Z - XFASTINT (w->window_end_pos))
13771 {
13772 clear_glyph_matrix (w->desired_matrix);
13773 move_it_by_lines (&it, 1, 0);
13774 try_window (window, it.current.pos, 0);
13775 }
13776 else if (PT < IT_CHARPOS (it))
13777 {
13778 clear_glyph_matrix (w->desired_matrix);
13779 move_it_by_lines (&it, -1, 0);
13780 try_window (window, it.current.pos, 0);
13781 }
13782 else
13783 {
13784 /* Not much we can do about it. */
13785 }
13786 }
13787
13788 /* Consider the following case: Window starts at BEGV, there is
13789 invisible, intangible text at BEGV, so that display starts at
13790 some point START > BEGV. It can happen that we are called with
13791 PT somewhere between BEGV and START. Try to handle that case. */
13792 if (w->cursor.vpos < 0)
13793 {
13794 struct glyph_row *row = w->current_matrix->rows;
13795 if (row->mode_line_p)
13796 ++row;
13797 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13798 }
13799
13800 if (!cursor_row_fully_visible_p (w, 0, 0))
13801 {
13802 /* If vscroll is enabled, disable it and try again. */
13803 if (w->vscroll)
13804 {
13805 w->vscroll = 0;
13806 clear_glyph_matrix (w->desired_matrix);
13807 goto recenter;
13808 }
13809
13810 /* If centering point failed to make the whole line visible,
13811 put point at the top instead. That has to make the whole line
13812 visible, if it can be done. */
13813 if (centering_position == 0)
13814 goto done;
13815
13816 clear_glyph_matrix (w->desired_matrix);
13817 centering_position = 0;
13818 goto recenter;
13819 }
13820
13821 done:
13822
13823 SET_TEXT_POS_FROM_MARKER (startp, w->start);
13824 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
13825 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
13826 ? Qt : Qnil);
13827
13828 /* Display the mode line, if we must. */
13829 if ((update_mode_line
13830 /* If window not full width, must redo its mode line
13831 if (a) the window to its side is being redone and
13832 (b) we do a frame-based redisplay. This is a consequence
13833 of how inverted lines are drawn in frame-based redisplay. */
13834 || (!just_this_one_p
13835 && !FRAME_WINDOW_P (f)
13836 && !WINDOW_FULL_WIDTH_P (w))
13837 /* Line number to display. */
13838 || INTEGERP (w->base_line_pos)
13839 /* Column number is displayed and different from the one displayed. */
13840 || (!NILP (w->column_number_displayed)
13841 && (XFASTINT (w->column_number_displayed)
13842 != (int) current_column ()))) /* iftc */
13843 /* This means that the window has a mode line. */
13844 && (WINDOW_WANTS_MODELINE_P (w)
13845 || WINDOW_WANTS_HEADER_LINE_P (w)))
13846 {
13847 display_mode_lines (w);
13848
13849 /* If mode line height has changed, arrange for a thorough
13850 immediate redisplay using the correct mode line height. */
13851 if (WINDOW_WANTS_MODELINE_P (w)
13852 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
13853 {
13854 fonts_changed_p = 1;
13855 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
13856 = DESIRED_MODE_LINE_HEIGHT (w);
13857 }
13858
13859 /* If header line height has changed, arrange for a thorough
13860 immediate redisplay using the correct header line height. */
13861 if (WINDOW_WANTS_HEADER_LINE_P (w)
13862 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
13863 {
13864 fonts_changed_p = 1;
13865 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
13866 = DESIRED_HEADER_LINE_HEIGHT (w);
13867 }
13868
13869 if (fonts_changed_p)
13870 goto need_larger_matrices;
13871 }
13872
13873 if (!line_number_displayed
13874 && !BUFFERP (w->base_line_pos))
13875 {
13876 w->base_line_pos = Qnil;
13877 w->base_line_number = Qnil;
13878 }
13879
13880 finish_menu_bars:
13881
13882 /* When we reach a frame's selected window, redo the frame's menu bar. */
13883 if (update_mode_line
13884 && EQ (FRAME_SELECTED_WINDOW (f), window))
13885 {
13886 int redisplay_menu_p = 0;
13887 int redisplay_tool_bar_p = 0;
13888
13889 if (FRAME_WINDOW_P (f))
13890 {
13891 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
13892 || defined (HAVE_NS) || defined (USE_GTK)
13893 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
13894 #else
13895 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
13896 #endif
13897 }
13898 else
13899 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
13900
13901 if (redisplay_menu_p)
13902 display_menu_bar (w);
13903
13904 #ifdef HAVE_WINDOW_SYSTEM
13905 if (FRAME_WINDOW_P (f))
13906 {
13907 #if defined (USE_GTK) || defined (HAVE_NS)
13908 redisplay_tool_bar_p = FRAME_EXTERNAL_TOOL_BAR (f);
13909 #else
13910 redisplay_tool_bar_p = WINDOWP (f->tool_bar_window)
13911 && (FRAME_TOOL_BAR_LINES (f) > 0
13912 || !NILP (Vauto_resize_tool_bars));
13913 #endif
13914
13915 if (redisplay_tool_bar_p && redisplay_tool_bar (f))
13916 {
13917 extern int ignore_mouse_drag_p;
13918 ignore_mouse_drag_p = 1;
13919 }
13920 }
13921 #endif
13922 }
13923
13924 #ifdef HAVE_WINDOW_SYSTEM
13925 if (FRAME_WINDOW_P (f)
13926 && update_window_fringes (w, (just_this_one_p
13927 || (!used_current_matrix_p && !overlay_arrow_seen)
13928 || w->pseudo_window_p)))
13929 {
13930 update_begin (f);
13931 BLOCK_INPUT;
13932 if (draw_window_fringes (w, 1))
13933 x_draw_vertical_border (w);
13934 UNBLOCK_INPUT;
13935 update_end (f);
13936 }
13937 #endif /* HAVE_WINDOW_SYSTEM */
13938
13939 /* We go to this label, with fonts_changed_p nonzero,
13940 if it is necessary to try again using larger glyph matrices.
13941 We have to redeem the scroll bar even in this case,
13942 because the loop in redisplay_internal expects that. */
13943 need_larger_matrices:
13944 ;
13945 finish_scroll_bars:
13946
13947 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
13948 {
13949 /* Set the thumb's position and size. */
13950 set_vertical_scroll_bar (w);
13951
13952 /* Note that we actually used the scroll bar attached to this
13953 window, so it shouldn't be deleted at the end of redisplay. */
13954 if (FRAME_TERMINAL (f)->redeem_scroll_bar_hook)
13955 (*FRAME_TERMINAL (f)->redeem_scroll_bar_hook) (w);
13956 }
13957
13958 /* Restore current_buffer and value of point in it. */
13959 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
13960 set_buffer_internal_1 (old);
13961 /* Avoid an abort in TEMP_SET_PT_BOTH if the buffer has become
13962 shorter. This can be caused by log truncation in *Messages*. */
13963 if (CHARPOS (lpoint) <= ZV)
13964 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
13965
13966 unbind_to (count, Qnil);
13967 }
13968
13969
13970 /* Build the complete desired matrix of WINDOW with a window start
13971 buffer position POS.
13972
13973 Value is 1 if successful. It is zero if fonts were loaded during
13974 redisplay which makes re-adjusting glyph matrices necessary, and -1
13975 if point would appear in the scroll margins.
13976 (We check that only if CHECK_MARGINS is nonzero. */
13977
13978 int
13979 try_window (window, pos, check_margins)
13980 Lisp_Object window;
13981 struct text_pos pos;
13982 int check_margins;
13983 {
13984 struct window *w = XWINDOW (window);
13985 struct it it;
13986 struct glyph_row *last_text_row = NULL;
13987 struct frame *f = XFRAME (w->frame);
13988
13989 /* Make POS the new window start. */
13990 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
13991
13992 /* Mark cursor position as unknown. No overlay arrow seen. */
13993 w->cursor.vpos = -1;
13994 overlay_arrow_seen = 0;
13995
13996 /* Initialize iterator and info to start at POS. */
13997 start_display (&it, w, pos);
13998
13999 /* Display all lines of W. */
14000 while (it.current_y < it.last_visible_y)
14001 {
14002 if (display_line (&it))
14003 last_text_row = it.glyph_row - 1;
14004 if (fonts_changed_p)
14005 return 0;
14006 }
14007
14008 /* Don't let the cursor end in the scroll margins. */
14009 if (check_margins
14010 && !MINI_WINDOW_P (w))
14011 {
14012 int this_scroll_margin;
14013
14014 if (scroll_margin > 0)
14015 {
14016 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
14017 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
14018 }
14019 else
14020 this_scroll_margin = 0;
14021
14022 if ((w->cursor.y >= 0 /* not vscrolled */
14023 && w->cursor.y < this_scroll_margin
14024 && CHARPOS (pos) > BEGV
14025 && IT_CHARPOS (it) < ZV)
14026 /* rms: considering make_cursor_line_fully_visible_p here
14027 seems to give wrong results. We don't want to recenter
14028 when the last line is partly visible, we want to allow
14029 that case to be handled in the usual way. */
14030 || w->cursor.y > it.last_visible_y - this_scroll_margin - 1)
14031 {
14032 w->cursor.vpos = -1;
14033 clear_glyph_matrix (w->desired_matrix);
14034 return -1;
14035 }
14036 }
14037
14038 /* If bottom moved off end of frame, change mode line percentage. */
14039 if (XFASTINT (w->window_end_pos) <= 0
14040 && Z != IT_CHARPOS (it))
14041 w->update_mode_line = Qt;
14042
14043 /* Set window_end_pos to the offset of the last character displayed
14044 on the window from the end of current_buffer. Set
14045 window_end_vpos to its row number. */
14046 if (last_text_row)
14047 {
14048 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
14049 w->window_end_bytepos
14050 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
14051 w->window_end_pos
14052 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
14053 w->window_end_vpos
14054 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
14055 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
14056 ->displays_text_p);
14057 }
14058 else
14059 {
14060 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
14061 w->window_end_pos = make_number (Z - ZV);
14062 w->window_end_vpos = make_number (0);
14063 }
14064
14065 /* But that is not valid info until redisplay finishes. */
14066 w->window_end_valid = Qnil;
14067 return 1;
14068 }
14069
14070
14071 \f
14072 /************************************************************************
14073 Window redisplay reusing current matrix when buffer has not changed
14074 ************************************************************************/
14075
14076 /* Try redisplay of window W showing an unchanged buffer with a
14077 different window start than the last time it was displayed by
14078 reusing its current matrix. Value is non-zero if successful.
14079 W->start is the new window start. */
14080
14081 static int
14082 try_window_reusing_current_matrix (w)
14083 struct window *w;
14084 {
14085 struct frame *f = XFRAME (w->frame);
14086 struct glyph_row *row, *bottom_row;
14087 struct it it;
14088 struct run run;
14089 struct text_pos start, new_start;
14090 int nrows_scrolled, i;
14091 struct glyph_row *last_text_row;
14092 struct glyph_row *last_reused_text_row;
14093 struct glyph_row *start_row;
14094 int start_vpos, min_y, max_y;
14095
14096 #if GLYPH_DEBUG
14097 if (inhibit_try_window_reusing)
14098 return 0;
14099 #endif
14100
14101 if (/* This function doesn't handle terminal frames. */
14102 !FRAME_WINDOW_P (f)
14103 /* Don't try to reuse the display if windows have been split
14104 or such. */
14105 || windows_or_buffers_changed
14106 || cursor_type_changed)
14107 return 0;
14108
14109 /* Can't do this if region may have changed. */
14110 if ((!NILP (Vtransient_mark_mode)
14111 && !NILP (current_buffer->mark_active))
14112 || !NILP (w->region_showing)
14113 || !NILP (Vshow_trailing_whitespace))
14114 return 0;
14115
14116 /* If top-line visibility has changed, give up. */
14117 if (WINDOW_WANTS_HEADER_LINE_P (w)
14118 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
14119 return 0;
14120
14121 /* Give up if old or new display is scrolled vertically. We could
14122 make this function handle this, but right now it doesn't. */
14123 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
14124 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
14125 return 0;
14126
14127 /* The variable new_start now holds the new window start. The old
14128 start `start' can be determined from the current matrix. */
14129 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
14130 start = start_row->start.pos;
14131 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
14132
14133 /* Clear the desired matrix for the display below. */
14134 clear_glyph_matrix (w->desired_matrix);
14135
14136 if (CHARPOS (new_start) <= CHARPOS (start))
14137 {
14138 int first_row_y;
14139
14140 /* Don't use this method if the display starts with an ellipsis
14141 displayed for invisible text. It's not easy to handle that case
14142 below, and it's certainly not worth the effort since this is
14143 not a frequent case. */
14144 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
14145 return 0;
14146
14147 IF_DEBUG (debug_method_add (w, "twu1"));
14148
14149 /* Display up to a row that can be reused. The variable
14150 last_text_row is set to the last row displayed that displays
14151 text. Note that it.vpos == 0 if or if not there is a
14152 header-line; it's not the same as the MATRIX_ROW_VPOS! */
14153 start_display (&it, w, new_start);
14154 first_row_y = it.current_y;
14155 w->cursor.vpos = -1;
14156 last_text_row = last_reused_text_row = NULL;
14157
14158 while (it.current_y < it.last_visible_y
14159 && !fonts_changed_p)
14160 {
14161 /* If we have reached into the characters in the START row,
14162 that means the line boundaries have changed. So we
14163 can't start copying with the row START. Maybe it will
14164 work to start copying with the following row. */
14165 while (IT_CHARPOS (it) > CHARPOS (start))
14166 {
14167 /* Advance to the next row as the "start". */
14168 start_row++;
14169 start = start_row->start.pos;
14170 /* If there are no more rows to try, or just one, give up. */
14171 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
14172 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
14173 || CHARPOS (start) == ZV)
14174 {
14175 clear_glyph_matrix (w->desired_matrix);
14176 return 0;
14177 }
14178
14179 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
14180 }
14181 /* If we have reached alignment,
14182 we can copy the rest of the rows. */
14183 if (IT_CHARPOS (it) == CHARPOS (start))
14184 break;
14185
14186 if (display_line (&it))
14187 last_text_row = it.glyph_row - 1;
14188 }
14189
14190 /* A value of current_y < last_visible_y means that we stopped
14191 at the previous window start, which in turn means that we
14192 have at least one reusable row. */
14193 if (it.current_y < it.last_visible_y)
14194 {
14195 /* IT.vpos always starts from 0; it counts text lines. */
14196 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
14197
14198 /* Find PT if not already found in the lines displayed. */
14199 if (w->cursor.vpos < 0)
14200 {
14201 int dy = it.current_y - start_row->y;
14202
14203 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
14204 row = row_containing_pos (w, PT, row, NULL, dy);
14205 if (row)
14206 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
14207 dy, nrows_scrolled);
14208 else
14209 {
14210 clear_glyph_matrix (w->desired_matrix);
14211 return 0;
14212 }
14213 }
14214
14215 /* Scroll the display. Do it before the current matrix is
14216 changed. The problem here is that update has not yet
14217 run, i.e. part of the current matrix is not up to date.
14218 scroll_run_hook will clear the cursor, and use the
14219 current matrix to get the height of the row the cursor is
14220 in. */
14221 run.current_y = start_row->y;
14222 run.desired_y = it.current_y;
14223 run.height = it.last_visible_y - it.current_y;
14224
14225 if (run.height > 0 && run.current_y != run.desired_y)
14226 {
14227 update_begin (f);
14228 FRAME_RIF (f)->update_window_begin_hook (w);
14229 FRAME_RIF (f)->clear_window_mouse_face (w);
14230 FRAME_RIF (f)->scroll_run_hook (w, &run);
14231 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
14232 update_end (f);
14233 }
14234
14235 /* Shift current matrix down by nrows_scrolled lines. */
14236 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
14237 rotate_matrix (w->current_matrix,
14238 start_vpos,
14239 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
14240 nrows_scrolled);
14241
14242 /* Disable lines that must be updated. */
14243 for (i = 0; i < nrows_scrolled; ++i)
14244 (start_row + i)->enabled_p = 0;
14245
14246 /* Re-compute Y positions. */
14247 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
14248 max_y = it.last_visible_y;
14249 for (row = start_row + nrows_scrolled;
14250 row < bottom_row;
14251 ++row)
14252 {
14253 row->y = it.current_y;
14254 row->visible_height = row->height;
14255
14256 if (row->y < min_y)
14257 row->visible_height -= min_y - row->y;
14258 if (row->y + row->height > max_y)
14259 row->visible_height -= row->y + row->height - max_y;
14260 row->redraw_fringe_bitmaps_p = 1;
14261
14262 it.current_y += row->height;
14263
14264 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
14265 last_reused_text_row = row;
14266 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
14267 break;
14268 }
14269
14270 /* Disable lines in the current matrix which are now
14271 below the window. */
14272 for (++row; row < bottom_row; ++row)
14273 row->enabled_p = row->mode_line_p = 0;
14274 }
14275
14276 /* Update window_end_pos etc.; last_reused_text_row is the last
14277 reused row from the current matrix containing text, if any.
14278 The value of last_text_row is the last displayed line
14279 containing text. */
14280 if (last_reused_text_row)
14281 {
14282 w->window_end_bytepos
14283 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
14284 w->window_end_pos
14285 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
14286 w->window_end_vpos
14287 = make_number (MATRIX_ROW_VPOS (last_reused_text_row,
14288 w->current_matrix));
14289 }
14290 else if (last_text_row)
14291 {
14292 w->window_end_bytepos
14293 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
14294 w->window_end_pos
14295 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
14296 w->window_end_vpos
14297 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
14298 }
14299 else
14300 {
14301 /* This window must be completely empty. */
14302 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
14303 w->window_end_pos = make_number (Z - ZV);
14304 w->window_end_vpos = make_number (0);
14305 }
14306 w->window_end_valid = Qnil;
14307
14308 /* Update hint: don't try scrolling again in update_window. */
14309 w->desired_matrix->no_scrolling_p = 1;
14310
14311 #if GLYPH_DEBUG
14312 debug_method_add (w, "try_window_reusing_current_matrix 1");
14313 #endif
14314 return 1;
14315 }
14316 else if (CHARPOS (new_start) > CHARPOS (start))
14317 {
14318 struct glyph_row *pt_row, *row;
14319 struct glyph_row *first_reusable_row;
14320 struct glyph_row *first_row_to_display;
14321 int dy;
14322 int yb = window_text_bottom_y (w);
14323
14324 /* Find the row starting at new_start, if there is one. Don't
14325 reuse a partially visible line at the end. */
14326 first_reusable_row = start_row;
14327 while (first_reusable_row->enabled_p
14328 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
14329 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
14330 < CHARPOS (new_start)))
14331 ++first_reusable_row;
14332
14333 /* Give up if there is no row to reuse. */
14334 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
14335 || !first_reusable_row->enabled_p
14336 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
14337 != CHARPOS (new_start)))
14338 return 0;
14339
14340 /* We can reuse fully visible rows beginning with
14341 first_reusable_row to the end of the window. Set
14342 first_row_to_display to the first row that cannot be reused.
14343 Set pt_row to the row containing point, if there is any. */
14344 pt_row = NULL;
14345 for (first_row_to_display = first_reusable_row;
14346 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
14347 ++first_row_to_display)
14348 {
14349 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
14350 && PT < MATRIX_ROW_END_CHARPOS (first_row_to_display))
14351 pt_row = first_row_to_display;
14352 }
14353
14354 /* Start displaying at the start of first_row_to_display. */
14355 xassert (first_row_to_display->y < yb);
14356 init_to_row_start (&it, w, first_row_to_display);
14357
14358 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
14359 - start_vpos);
14360 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
14361 - nrows_scrolled);
14362 it.current_y = (first_row_to_display->y - first_reusable_row->y
14363 + WINDOW_HEADER_LINE_HEIGHT (w));
14364
14365 /* Display lines beginning with first_row_to_display in the
14366 desired matrix. Set last_text_row to the last row displayed
14367 that displays text. */
14368 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
14369 if (pt_row == NULL)
14370 w->cursor.vpos = -1;
14371 last_text_row = NULL;
14372 while (it.current_y < it.last_visible_y && !fonts_changed_p)
14373 if (display_line (&it))
14374 last_text_row = it.glyph_row - 1;
14375
14376 /* If point is in a reused row, adjust y and vpos of the cursor
14377 position. */
14378 if (pt_row)
14379 {
14380 w->cursor.vpos -= nrows_scrolled;
14381 w->cursor.y -= first_reusable_row->y - start_row->y;
14382 }
14383
14384 /* Give up if point isn't in a row displayed or reused. (This
14385 also handles the case where w->cursor.vpos < nrows_scrolled
14386 after the calls to display_line, which can happen with scroll
14387 margins. See bug#1295.) */
14388 if (w->cursor.vpos < 0)
14389 {
14390 clear_glyph_matrix (w->desired_matrix);
14391 return 0;
14392 }
14393
14394 /* Scroll the display. */
14395 run.current_y = first_reusable_row->y;
14396 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
14397 run.height = it.last_visible_y - run.current_y;
14398 dy = run.current_y - run.desired_y;
14399
14400 if (run.height)
14401 {
14402 update_begin (f);
14403 FRAME_RIF (f)->update_window_begin_hook (w);
14404 FRAME_RIF (f)->clear_window_mouse_face (w);
14405 FRAME_RIF (f)->scroll_run_hook (w, &run);
14406 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
14407 update_end (f);
14408 }
14409
14410 /* Adjust Y positions of reused rows. */
14411 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
14412 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
14413 max_y = it.last_visible_y;
14414 for (row = first_reusable_row; row < first_row_to_display; ++row)
14415 {
14416 row->y -= dy;
14417 row->visible_height = row->height;
14418 if (row->y < min_y)
14419 row->visible_height -= min_y - row->y;
14420 if (row->y + row->height > max_y)
14421 row->visible_height -= row->y + row->height - max_y;
14422 row->redraw_fringe_bitmaps_p = 1;
14423 }
14424
14425 /* Scroll the current matrix. */
14426 xassert (nrows_scrolled > 0);
14427 rotate_matrix (w->current_matrix,
14428 start_vpos,
14429 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
14430 -nrows_scrolled);
14431
14432 /* Disable rows not reused. */
14433 for (row -= nrows_scrolled; row < bottom_row; ++row)
14434 row->enabled_p = 0;
14435
14436 /* Point may have moved to a different line, so we cannot assume that
14437 the previous cursor position is valid; locate the correct row. */
14438 if (pt_row)
14439 {
14440 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
14441 row < bottom_row && PT >= MATRIX_ROW_END_CHARPOS (row);
14442 row++)
14443 {
14444 w->cursor.vpos++;
14445 w->cursor.y = row->y;
14446 }
14447 if (row < bottom_row)
14448 {
14449 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
14450 struct glyph *end = glyph + row->used[TEXT_AREA];
14451
14452 for (; glyph < end
14453 && (!BUFFERP (glyph->object)
14454 || glyph->charpos < PT);
14455 glyph++)
14456 {
14457 w->cursor.hpos++;
14458 w->cursor.x += glyph->pixel_width;
14459 }
14460 }
14461 }
14462
14463 /* Adjust window end. A null value of last_text_row means that
14464 the window end is in reused rows which in turn means that
14465 only its vpos can have changed. */
14466 if (last_text_row)
14467 {
14468 w->window_end_bytepos
14469 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
14470 w->window_end_pos
14471 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
14472 w->window_end_vpos
14473 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
14474 }
14475 else
14476 {
14477 w->window_end_vpos
14478 = make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled);
14479 }
14480
14481 w->window_end_valid = Qnil;
14482 w->desired_matrix->no_scrolling_p = 1;
14483
14484 #if GLYPH_DEBUG
14485 debug_method_add (w, "try_window_reusing_current_matrix 2");
14486 #endif
14487 return 1;
14488 }
14489
14490 return 0;
14491 }
14492
14493
14494 \f
14495 /************************************************************************
14496 Window redisplay reusing current matrix when buffer has changed
14497 ************************************************************************/
14498
14499 static struct glyph_row *find_last_unchanged_at_beg_row P_ ((struct window *));
14500 static struct glyph_row *find_first_unchanged_at_end_row P_ ((struct window *,
14501 int *, int *));
14502 static struct glyph_row *
14503 find_last_row_displaying_text P_ ((struct glyph_matrix *, struct it *,
14504 struct glyph_row *));
14505
14506
14507 /* Return the last row in MATRIX displaying text. If row START is
14508 non-null, start searching with that row. IT gives the dimensions
14509 of the display. Value is null if matrix is empty; otherwise it is
14510 a pointer to the row found. */
14511
14512 static struct glyph_row *
14513 find_last_row_displaying_text (matrix, it, start)
14514 struct glyph_matrix *matrix;
14515 struct it *it;
14516 struct glyph_row *start;
14517 {
14518 struct glyph_row *row, *row_found;
14519
14520 /* Set row_found to the last row in IT->w's current matrix
14521 displaying text. The loop looks funny but think of partially
14522 visible lines. */
14523 row_found = NULL;
14524 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
14525 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
14526 {
14527 xassert (row->enabled_p);
14528 row_found = row;
14529 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
14530 break;
14531 ++row;
14532 }
14533
14534 return row_found;
14535 }
14536
14537
14538 /* Return the last row in the current matrix of W that is not affected
14539 by changes at the start of current_buffer that occurred since W's
14540 current matrix was built. Value is null if no such row exists.
14541
14542 BEG_UNCHANGED us the number of characters unchanged at the start of
14543 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
14544 first changed character in current_buffer. Characters at positions <
14545 BEG + BEG_UNCHANGED are at the same buffer positions as they were
14546 when the current matrix was built. */
14547
14548 static struct glyph_row *
14549 find_last_unchanged_at_beg_row (w)
14550 struct window *w;
14551 {
14552 int first_changed_pos = BEG + BEG_UNCHANGED;
14553 struct glyph_row *row;
14554 struct glyph_row *row_found = NULL;
14555 int yb = window_text_bottom_y (w);
14556
14557 /* Find the last row displaying unchanged text. */
14558 for (row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
14559 MATRIX_ROW_DISPLAYS_TEXT_P (row)
14560 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos;
14561 ++row)
14562 {
14563 if (/* If row ends before first_changed_pos, it is unchanged,
14564 except in some case. */
14565 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
14566 /* When row ends in ZV and we write at ZV it is not
14567 unchanged. */
14568 && !row->ends_at_zv_p
14569 /* When first_changed_pos is the end of a continued line,
14570 row is not unchanged because it may be no longer
14571 continued. */
14572 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
14573 && (row->continued_p
14574 || row->exact_window_width_line_p)))
14575 row_found = row;
14576
14577 /* Stop if last visible row. */
14578 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
14579 break;
14580 }
14581
14582 return row_found;
14583 }
14584
14585
14586 /* Find the first glyph row in the current matrix of W that is not
14587 affected by changes at the end of current_buffer since the
14588 time W's current matrix was built.
14589
14590 Return in *DELTA the number of chars by which buffer positions in
14591 unchanged text at the end of current_buffer must be adjusted.
14592
14593 Return in *DELTA_BYTES the corresponding number of bytes.
14594
14595 Value is null if no such row exists, i.e. all rows are affected by
14596 changes. */
14597
14598 static struct glyph_row *
14599 find_first_unchanged_at_end_row (w, delta, delta_bytes)
14600 struct window *w;
14601 int *delta, *delta_bytes;
14602 {
14603 struct glyph_row *row;
14604 struct glyph_row *row_found = NULL;
14605
14606 *delta = *delta_bytes = 0;
14607
14608 /* Display must not have been paused, otherwise the current matrix
14609 is not up to date. */
14610 eassert (!NILP (w->window_end_valid));
14611
14612 /* A value of window_end_pos >= END_UNCHANGED means that the window
14613 end is in the range of changed text. If so, there is no
14614 unchanged row at the end of W's current matrix. */
14615 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
14616 return NULL;
14617
14618 /* Set row to the last row in W's current matrix displaying text. */
14619 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
14620
14621 /* If matrix is entirely empty, no unchanged row exists. */
14622 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
14623 {
14624 /* The value of row is the last glyph row in the matrix having a
14625 meaningful buffer position in it. The end position of row
14626 corresponds to window_end_pos. This allows us to translate
14627 buffer positions in the current matrix to current buffer
14628 positions for characters not in changed text. */
14629 int Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
14630 int Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
14631 int last_unchanged_pos, last_unchanged_pos_old;
14632 struct glyph_row *first_text_row
14633 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
14634
14635 *delta = Z - Z_old;
14636 *delta_bytes = Z_BYTE - Z_BYTE_old;
14637
14638 /* Set last_unchanged_pos to the buffer position of the last
14639 character in the buffer that has not been changed. Z is the
14640 index + 1 of the last character in current_buffer, i.e. by
14641 subtracting END_UNCHANGED we get the index of the last
14642 unchanged character, and we have to add BEG to get its buffer
14643 position. */
14644 last_unchanged_pos = Z - END_UNCHANGED + BEG;
14645 last_unchanged_pos_old = last_unchanged_pos - *delta;
14646
14647 /* Search backward from ROW for a row displaying a line that
14648 starts at a minimum position >= last_unchanged_pos_old. */
14649 for (; row > first_text_row; --row)
14650 {
14651 /* This used to abort, but it can happen.
14652 It is ok to just stop the search instead here. KFS. */
14653 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
14654 break;
14655
14656 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
14657 row_found = row;
14658 }
14659 }
14660
14661 eassert (!row_found || MATRIX_ROW_DISPLAYS_TEXT_P (row_found));
14662
14663 return row_found;
14664 }
14665
14666
14667 /* Make sure that glyph rows in the current matrix of window W
14668 reference the same glyph memory as corresponding rows in the
14669 frame's frame matrix. This function is called after scrolling W's
14670 current matrix on a terminal frame in try_window_id and
14671 try_window_reusing_current_matrix. */
14672
14673 static void
14674 sync_frame_with_window_matrix_rows (w)
14675 struct window *w;
14676 {
14677 struct frame *f = XFRAME (w->frame);
14678 struct glyph_row *window_row, *window_row_end, *frame_row;
14679
14680 /* Preconditions: W must be a leaf window and full-width. Its frame
14681 must have a frame matrix. */
14682 xassert (NILP (w->hchild) && NILP (w->vchild));
14683 xassert (WINDOW_FULL_WIDTH_P (w));
14684 xassert (!FRAME_WINDOW_P (f));
14685
14686 /* If W is a full-width window, glyph pointers in W's current matrix
14687 have, by definition, to be the same as glyph pointers in the
14688 corresponding frame matrix. Note that frame matrices have no
14689 marginal areas (see build_frame_matrix). */
14690 window_row = w->current_matrix->rows;
14691 window_row_end = window_row + w->current_matrix->nrows;
14692 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
14693 while (window_row < window_row_end)
14694 {
14695 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
14696 struct glyph *end = window_row->glyphs[LAST_AREA];
14697
14698 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
14699 frame_row->glyphs[TEXT_AREA] = start;
14700 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
14701 frame_row->glyphs[LAST_AREA] = end;
14702
14703 /* Disable frame rows whose corresponding window rows have
14704 been disabled in try_window_id. */
14705 if (!window_row->enabled_p)
14706 frame_row->enabled_p = 0;
14707
14708 ++window_row, ++frame_row;
14709 }
14710 }
14711
14712
14713 /* Find the glyph row in window W containing CHARPOS. Consider all
14714 rows between START and END (not inclusive). END null means search
14715 all rows to the end of the display area of W. Value is the row
14716 containing CHARPOS or null. */
14717
14718 struct glyph_row *
14719 row_containing_pos (w, charpos, start, end, dy)
14720 struct window *w;
14721 int charpos;
14722 struct glyph_row *start, *end;
14723 int dy;
14724 {
14725 struct glyph_row *row = start;
14726 int last_y;
14727
14728 /* If we happen to start on a header-line, skip that. */
14729 if (row->mode_line_p)
14730 ++row;
14731
14732 if ((end && row >= end) || !row->enabled_p)
14733 return NULL;
14734
14735 last_y = window_text_bottom_y (w) - dy;
14736
14737 while (1)
14738 {
14739 /* Give up if we have gone too far. */
14740 if (end && row >= end)
14741 return NULL;
14742 /* This formerly returned if they were equal.
14743 I think that both quantities are of a "last plus one" type;
14744 if so, when they are equal, the row is within the screen. -- rms. */
14745 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
14746 return NULL;
14747
14748 /* If it is in this row, return this row. */
14749 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
14750 || (MATRIX_ROW_END_CHARPOS (row) == charpos
14751 /* The end position of a row equals the start
14752 position of the next row. If CHARPOS is there, we
14753 would rather display it in the next line, except
14754 when this line ends in ZV. */
14755 && !row->ends_at_zv_p
14756 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
14757 && charpos >= MATRIX_ROW_START_CHARPOS (row))
14758 return row;
14759 ++row;
14760 }
14761 }
14762
14763
14764 /* Try to redisplay window W by reusing its existing display. W's
14765 current matrix must be up to date when this function is called,
14766 i.e. window_end_valid must not be nil.
14767
14768 Value is
14769
14770 1 if display has been updated
14771 0 if otherwise unsuccessful
14772 -1 if redisplay with same window start is known not to succeed
14773
14774 The following steps are performed:
14775
14776 1. Find the last row in the current matrix of W that is not
14777 affected by changes at the start of current_buffer. If no such row
14778 is found, give up.
14779
14780 2. Find the first row in W's current matrix that is not affected by
14781 changes at the end of current_buffer. Maybe there is no such row.
14782
14783 3. Display lines beginning with the row + 1 found in step 1 to the
14784 row found in step 2 or, if step 2 didn't find a row, to the end of
14785 the window.
14786
14787 4. If cursor is not known to appear on the window, give up.
14788
14789 5. If display stopped at the row found in step 2, scroll the
14790 display and current matrix as needed.
14791
14792 6. Maybe display some lines at the end of W, if we must. This can
14793 happen under various circumstances, like a partially visible line
14794 becoming fully visible, or because newly displayed lines are displayed
14795 in smaller font sizes.
14796
14797 7. Update W's window end information. */
14798
14799 static int
14800 try_window_id (w)
14801 struct window *w;
14802 {
14803 struct frame *f = XFRAME (w->frame);
14804 struct glyph_matrix *current_matrix = w->current_matrix;
14805 struct glyph_matrix *desired_matrix = w->desired_matrix;
14806 struct glyph_row *last_unchanged_at_beg_row;
14807 struct glyph_row *first_unchanged_at_end_row;
14808 struct glyph_row *row;
14809 struct glyph_row *bottom_row;
14810 int bottom_vpos;
14811 struct it it;
14812 int delta = 0, delta_bytes = 0, stop_pos, dvpos, dy;
14813 struct text_pos start_pos;
14814 struct run run;
14815 int first_unchanged_at_end_vpos = 0;
14816 struct glyph_row *last_text_row, *last_text_row_at_end;
14817 struct text_pos start;
14818 int first_changed_charpos, last_changed_charpos;
14819
14820 #if GLYPH_DEBUG
14821 if (inhibit_try_window_id)
14822 return 0;
14823 #endif
14824
14825 /* This is handy for debugging. */
14826 #if 0
14827 #define GIVE_UP(X) \
14828 do { \
14829 fprintf (stderr, "try_window_id give up %d\n", (X)); \
14830 return 0; \
14831 } while (0)
14832 #else
14833 #define GIVE_UP(X) return 0
14834 #endif
14835
14836 SET_TEXT_POS_FROM_MARKER (start, w->start);
14837
14838 /* Don't use this for mini-windows because these can show
14839 messages and mini-buffers, and we don't handle that here. */
14840 if (MINI_WINDOW_P (w))
14841 GIVE_UP (1);
14842
14843 /* This flag is used to prevent redisplay optimizations. */
14844 if (windows_or_buffers_changed || cursor_type_changed)
14845 GIVE_UP (2);
14846
14847 /* Verify that narrowing has not changed.
14848 Also verify that we were not told to prevent redisplay optimizations.
14849 It would be nice to further
14850 reduce the number of cases where this prevents try_window_id. */
14851 if (current_buffer->clip_changed
14852 || current_buffer->prevent_redisplay_optimizations_p)
14853 GIVE_UP (3);
14854
14855 /* Window must either use window-based redisplay or be full width. */
14856 if (!FRAME_WINDOW_P (f)
14857 && (!FRAME_LINE_INS_DEL_OK (f)
14858 || !WINDOW_FULL_WIDTH_P (w)))
14859 GIVE_UP (4);
14860
14861 /* Give up if point is known NOT to appear in W. */
14862 if (PT < CHARPOS (start))
14863 GIVE_UP (5);
14864
14865 /* Another way to prevent redisplay optimizations. */
14866 if (XFASTINT (w->last_modified) == 0)
14867 GIVE_UP (6);
14868
14869 /* Verify that window is not hscrolled. */
14870 if (XFASTINT (w->hscroll) != 0)
14871 GIVE_UP (7);
14872
14873 /* Verify that display wasn't paused. */
14874 if (NILP (w->window_end_valid))
14875 GIVE_UP (8);
14876
14877 /* Can't use this if highlighting a region because a cursor movement
14878 will do more than just set the cursor. */
14879 if (!NILP (Vtransient_mark_mode)
14880 && !NILP (current_buffer->mark_active))
14881 GIVE_UP (9);
14882
14883 /* Likewise if highlighting trailing whitespace. */
14884 if (!NILP (Vshow_trailing_whitespace))
14885 GIVE_UP (11);
14886
14887 /* Likewise if showing a region. */
14888 if (!NILP (w->region_showing))
14889 GIVE_UP (10);
14890
14891 /* Can't use this if overlay arrow position and/or string have
14892 changed. */
14893 if (overlay_arrows_changed_p ())
14894 GIVE_UP (12);
14895
14896 /* When word-wrap is on, adding a space to the first word of a
14897 wrapped line can change the wrap position, altering the line
14898 above it. It might be worthwhile to handle this more
14899 intelligently, but for now just redisplay from scratch. */
14900 if (!NILP (XBUFFER (w->buffer)->word_wrap))
14901 GIVE_UP (21);
14902
14903 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
14904 only if buffer has really changed. The reason is that the gap is
14905 initially at Z for freshly visited files. The code below would
14906 set end_unchanged to 0 in that case. */
14907 if (MODIFF > SAVE_MODIFF
14908 /* This seems to happen sometimes after saving a buffer. */
14909 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
14910 {
14911 if (GPT - BEG < BEG_UNCHANGED)
14912 BEG_UNCHANGED = GPT - BEG;
14913 if (Z - GPT < END_UNCHANGED)
14914 END_UNCHANGED = Z - GPT;
14915 }
14916
14917 /* The position of the first and last character that has been changed. */
14918 first_changed_charpos = BEG + BEG_UNCHANGED;
14919 last_changed_charpos = Z - END_UNCHANGED;
14920
14921 /* If window starts after a line end, and the last change is in
14922 front of that newline, then changes don't affect the display.
14923 This case happens with stealth-fontification. Note that although
14924 the display is unchanged, glyph positions in the matrix have to
14925 be adjusted, of course. */
14926 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
14927 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
14928 && ((last_changed_charpos < CHARPOS (start)
14929 && CHARPOS (start) == BEGV)
14930 || (last_changed_charpos < CHARPOS (start) - 1
14931 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
14932 {
14933 int Z_old, delta, Z_BYTE_old, delta_bytes;
14934 struct glyph_row *r0;
14935
14936 /* Compute how many chars/bytes have been added to or removed
14937 from the buffer. */
14938 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
14939 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
14940 delta = Z - Z_old;
14941 delta_bytes = Z_BYTE - Z_BYTE_old;
14942
14943 /* Give up if PT is not in the window. Note that it already has
14944 been checked at the start of try_window_id that PT is not in
14945 front of the window start. */
14946 if (PT >= MATRIX_ROW_END_CHARPOS (row) + delta)
14947 GIVE_UP (13);
14948
14949 /* If window start is unchanged, we can reuse the whole matrix
14950 as is, after adjusting glyph positions. No need to compute
14951 the window end again, since its offset from Z hasn't changed. */
14952 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
14953 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + delta
14954 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + delta_bytes
14955 /* PT must not be in a partially visible line. */
14956 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + delta
14957 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
14958 {
14959 /* Adjust positions in the glyph matrix. */
14960 if (delta || delta_bytes)
14961 {
14962 struct glyph_row *r1
14963 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
14964 increment_matrix_positions (w->current_matrix,
14965 MATRIX_ROW_VPOS (r0, current_matrix),
14966 MATRIX_ROW_VPOS (r1, current_matrix),
14967 delta, delta_bytes);
14968 }
14969
14970 /* Set the cursor. */
14971 row = row_containing_pos (w, PT, r0, NULL, 0);
14972 if (row)
14973 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
14974 else
14975 abort ();
14976 return 1;
14977 }
14978 }
14979
14980 /* Handle the case that changes are all below what is displayed in
14981 the window, and that PT is in the window. This shortcut cannot
14982 be taken if ZV is visible in the window, and text has been added
14983 there that is visible in the window. */
14984 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
14985 /* ZV is not visible in the window, or there are no
14986 changes at ZV, actually. */
14987 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
14988 || first_changed_charpos == last_changed_charpos))
14989 {
14990 struct glyph_row *r0;
14991
14992 /* Give up if PT is not in the window. Note that it already has
14993 been checked at the start of try_window_id that PT is not in
14994 front of the window start. */
14995 if (PT >= MATRIX_ROW_END_CHARPOS (row))
14996 GIVE_UP (14);
14997
14998 /* If window start is unchanged, we can reuse the whole matrix
14999 as is, without changing glyph positions since no text has
15000 been added/removed in front of the window end. */
15001 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
15002 if (TEXT_POS_EQUAL_P (start, r0->start.pos)
15003 /* PT must not be in a partially visible line. */
15004 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
15005 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
15006 {
15007 /* We have to compute the window end anew since text
15008 can have been added/removed after it. */
15009 w->window_end_pos
15010 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
15011 w->window_end_bytepos
15012 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
15013
15014 /* Set the cursor. */
15015 row = row_containing_pos (w, PT, r0, NULL, 0);
15016 if (row)
15017 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
15018 else
15019 abort ();
15020 return 2;
15021 }
15022 }
15023
15024 /* Give up if window start is in the changed area.
15025
15026 The condition used to read
15027
15028 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
15029
15030 but why that was tested escapes me at the moment. */
15031 if (CHARPOS (start) >= first_changed_charpos
15032 && CHARPOS (start) <= last_changed_charpos)
15033 GIVE_UP (15);
15034
15035 /* Check that window start agrees with the start of the first glyph
15036 row in its current matrix. Check this after we know the window
15037 start is not in changed text, otherwise positions would not be
15038 comparable. */
15039 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
15040 if (!TEXT_POS_EQUAL_P (start, row->start.pos))
15041 GIVE_UP (16);
15042
15043 /* Give up if the window ends in strings. Overlay strings
15044 at the end are difficult to handle, so don't try. */
15045 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
15046 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
15047 GIVE_UP (20);
15048
15049 /* Compute the position at which we have to start displaying new
15050 lines. Some of the lines at the top of the window might be
15051 reusable because they are not displaying changed text. Find the
15052 last row in W's current matrix not affected by changes at the
15053 start of current_buffer. Value is null if changes start in the
15054 first line of window. */
15055 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
15056 if (last_unchanged_at_beg_row)
15057 {
15058 /* Avoid starting to display in the moddle of a character, a TAB
15059 for instance. This is easier than to set up the iterator
15060 exactly, and it's not a frequent case, so the additional
15061 effort wouldn't really pay off. */
15062 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
15063 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
15064 && last_unchanged_at_beg_row > w->current_matrix->rows)
15065 --last_unchanged_at_beg_row;
15066
15067 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
15068 GIVE_UP (17);
15069
15070 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
15071 GIVE_UP (18);
15072 start_pos = it.current.pos;
15073
15074 /* Start displaying new lines in the desired matrix at the same
15075 vpos we would use in the current matrix, i.e. below
15076 last_unchanged_at_beg_row. */
15077 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
15078 current_matrix);
15079 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
15080 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
15081
15082 xassert (it.hpos == 0 && it.current_x == 0);
15083 }
15084 else
15085 {
15086 /* There are no reusable lines at the start of the window.
15087 Start displaying in the first text line. */
15088 start_display (&it, w, start);
15089 it.vpos = it.first_vpos;
15090 start_pos = it.current.pos;
15091 }
15092
15093 /* Find the first row that is not affected by changes at the end of
15094 the buffer. Value will be null if there is no unchanged row, in
15095 which case we must redisplay to the end of the window. delta
15096 will be set to the value by which buffer positions beginning with
15097 first_unchanged_at_end_row have to be adjusted due to text
15098 changes. */
15099 first_unchanged_at_end_row
15100 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
15101 IF_DEBUG (debug_delta = delta);
15102 IF_DEBUG (debug_delta_bytes = delta_bytes);
15103
15104 /* Set stop_pos to the buffer position up to which we will have to
15105 display new lines. If first_unchanged_at_end_row != NULL, this
15106 is the buffer position of the start of the line displayed in that
15107 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
15108 that we don't stop at a buffer position. */
15109 stop_pos = 0;
15110 if (first_unchanged_at_end_row)
15111 {
15112 xassert (last_unchanged_at_beg_row == NULL
15113 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
15114
15115 /* If this is a continuation line, move forward to the next one
15116 that isn't. Changes in lines above affect this line.
15117 Caution: this may move first_unchanged_at_end_row to a row
15118 not displaying text. */
15119 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
15120 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
15121 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
15122 < it.last_visible_y))
15123 ++first_unchanged_at_end_row;
15124
15125 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
15126 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
15127 >= it.last_visible_y))
15128 first_unchanged_at_end_row = NULL;
15129 else
15130 {
15131 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
15132 + delta);
15133 first_unchanged_at_end_vpos
15134 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
15135 xassert (stop_pos >= Z - END_UNCHANGED);
15136 }
15137 }
15138 else if (last_unchanged_at_beg_row == NULL)
15139 GIVE_UP (19);
15140
15141
15142 #if GLYPH_DEBUG
15143
15144 /* Either there is no unchanged row at the end, or the one we have
15145 now displays text. This is a necessary condition for the window
15146 end pos calculation at the end of this function. */
15147 xassert (first_unchanged_at_end_row == NULL
15148 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
15149
15150 debug_last_unchanged_at_beg_vpos
15151 = (last_unchanged_at_beg_row
15152 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
15153 : -1);
15154 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
15155
15156 #endif /* GLYPH_DEBUG != 0 */
15157
15158
15159 /* Display new lines. Set last_text_row to the last new line
15160 displayed which has text on it, i.e. might end up as being the
15161 line where the window_end_vpos is. */
15162 w->cursor.vpos = -1;
15163 last_text_row = NULL;
15164 overlay_arrow_seen = 0;
15165 while (it.current_y < it.last_visible_y
15166 && !fonts_changed_p
15167 && (first_unchanged_at_end_row == NULL
15168 || IT_CHARPOS (it) < stop_pos))
15169 {
15170 if (display_line (&it))
15171 last_text_row = it.glyph_row - 1;
15172 }
15173
15174 if (fonts_changed_p)
15175 return -1;
15176
15177
15178 /* Compute differences in buffer positions, y-positions etc. for
15179 lines reused at the bottom of the window. Compute what we can
15180 scroll. */
15181 if (first_unchanged_at_end_row
15182 /* No lines reused because we displayed everything up to the
15183 bottom of the window. */
15184 && it.current_y < it.last_visible_y)
15185 {
15186 dvpos = (it.vpos
15187 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
15188 current_matrix));
15189 dy = it.current_y - first_unchanged_at_end_row->y;
15190 run.current_y = first_unchanged_at_end_row->y;
15191 run.desired_y = run.current_y + dy;
15192 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
15193 }
15194 else
15195 {
15196 delta = delta_bytes = dvpos = dy
15197 = run.current_y = run.desired_y = run.height = 0;
15198 first_unchanged_at_end_row = NULL;
15199 }
15200 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
15201
15202
15203 /* Find the cursor if not already found. We have to decide whether
15204 PT will appear on this window (it sometimes doesn't, but this is
15205 not a very frequent case.) This decision has to be made before
15206 the current matrix is altered. A value of cursor.vpos < 0 means
15207 that PT is either in one of the lines beginning at
15208 first_unchanged_at_end_row or below the window. Don't care for
15209 lines that might be displayed later at the window end; as
15210 mentioned, this is not a frequent case. */
15211 if (w->cursor.vpos < 0)
15212 {
15213 /* Cursor in unchanged rows at the top? */
15214 if (PT < CHARPOS (start_pos)
15215 && last_unchanged_at_beg_row)
15216 {
15217 row = row_containing_pos (w, PT,
15218 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
15219 last_unchanged_at_beg_row + 1, 0);
15220 if (row)
15221 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
15222 }
15223
15224 /* Start from first_unchanged_at_end_row looking for PT. */
15225 else if (first_unchanged_at_end_row)
15226 {
15227 row = row_containing_pos (w, PT - delta,
15228 first_unchanged_at_end_row, NULL, 0);
15229 if (row)
15230 set_cursor_from_row (w, row, w->current_matrix, delta,
15231 delta_bytes, dy, dvpos);
15232 }
15233
15234 /* Give up if cursor was not found. */
15235 if (w->cursor.vpos < 0)
15236 {
15237 clear_glyph_matrix (w->desired_matrix);
15238 return -1;
15239 }
15240 }
15241
15242 /* Don't let the cursor end in the scroll margins. */
15243 {
15244 int this_scroll_margin, cursor_height;
15245
15246 this_scroll_margin = max (0, scroll_margin);
15247 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
15248 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
15249 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
15250
15251 if ((w->cursor.y < this_scroll_margin
15252 && CHARPOS (start) > BEGV)
15253 /* Old redisplay didn't take scroll margin into account at the bottom,
15254 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
15255 || (w->cursor.y + (make_cursor_line_fully_visible_p
15256 ? cursor_height + this_scroll_margin
15257 : 1)) > it.last_visible_y)
15258 {
15259 w->cursor.vpos = -1;
15260 clear_glyph_matrix (w->desired_matrix);
15261 return -1;
15262 }
15263 }
15264
15265 /* Scroll the display. Do it before changing the current matrix so
15266 that xterm.c doesn't get confused about where the cursor glyph is
15267 found. */
15268 if (dy && run.height)
15269 {
15270 update_begin (f);
15271
15272 if (FRAME_WINDOW_P (f))
15273 {
15274 FRAME_RIF (f)->update_window_begin_hook (w);
15275 FRAME_RIF (f)->clear_window_mouse_face (w);
15276 FRAME_RIF (f)->scroll_run_hook (w, &run);
15277 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
15278 }
15279 else
15280 {
15281 /* Terminal frame. In this case, dvpos gives the number of
15282 lines to scroll by; dvpos < 0 means scroll up. */
15283 int first_unchanged_at_end_vpos
15284 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
15285 int from = WINDOW_TOP_EDGE_LINE (w) + first_unchanged_at_end_vpos;
15286 int end = (WINDOW_TOP_EDGE_LINE (w)
15287 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
15288 + window_internal_height (w));
15289
15290 /* Perform the operation on the screen. */
15291 if (dvpos > 0)
15292 {
15293 /* Scroll last_unchanged_at_beg_row to the end of the
15294 window down dvpos lines. */
15295 set_terminal_window (f, end);
15296
15297 /* On dumb terminals delete dvpos lines at the end
15298 before inserting dvpos empty lines. */
15299 if (!FRAME_SCROLL_REGION_OK (f))
15300 ins_del_lines (f, end - dvpos, -dvpos);
15301
15302 /* Insert dvpos empty lines in front of
15303 last_unchanged_at_beg_row. */
15304 ins_del_lines (f, from, dvpos);
15305 }
15306 else if (dvpos < 0)
15307 {
15308 /* Scroll up last_unchanged_at_beg_vpos to the end of
15309 the window to last_unchanged_at_beg_vpos - |dvpos|. */
15310 set_terminal_window (f, end);
15311
15312 /* Delete dvpos lines in front of
15313 last_unchanged_at_beg_vpos. ins_del_lines will set
15314 the cursor to the given vpos and emit |dvpos| delete
15315 line sequences. */
15316 ins_del_lines (f, from + dvpos, dvpos);
15317
15318 /* On a dumb terminal insert dvpos empty lines at the
15319 end. */
15320 if (!FRAME_SCROLL_REGION_OK (f))
15321 ins_del_lines (f, end + dvpos, -dvpos);
15322 }
15323
15324 set_terminal_window (f, 0);
15325 }
15326
15327 update_end (f);
15328 }
15329
15330 /* Shift reused rows of the current matrix to the right position.
15331 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
15332 text. */
15333 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
15334 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
15335 if (dvpos < 0)
15336 {
15337 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
15338 bottom_vpos, dvpos);
15339 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
15340 bottom_vpos, 0);
15341 }
15342 else if (dvpos > 0)
15343 {
15344 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
15345 bottom_vpos, dvpos);
15346 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
15347 first_unchanged_at_end_vpos + dvpos, 0);
15348 }
15349
15350 /* For frame-based redisplay, make sure that current frame and window
15351 matrix are in sync with respect to glyph memory. */
15352 if (!FRAME_WINDOW_P (f))
15353 sync_frame_with_window_matrix_rows (w);
15354
15355 /* Adjust buffer positions in reused rows. */
15356 if (delta || delta_bytes)
15357 increment_matrix_positions (current_matrix,
15358 first_unchanged_at_end_vpos + dvpos,
15359 bottom_vpos, delta, delta_bytes);
15360
15361 /* Adjust Y positions. */
15362 if (dy)
15363 shift_glyph_matrix (w, current_matrix,
15364 first_unchanged_at_end_vpos + dvpos,
15365 bottom_vpos, dy);
15366
15367 if (first_unchanged_at_end_row)
15368 {
15369 first_unchanged_at_end_row += dvpos;
15370 if (first_unchanged_at_end_row->y >= it.last_visible_y
15371 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
15372 first_unchanged_at_end_row = NULL;
15373 }
15374
15375 /* If scrolling up, there may be some lines to display at the end of
15376 the window. */
15377 last_text_row_at_end = NULL;
15378 if (dy < 0)
15379 {
15380 /* Scrolling up can leave for example a partially visible line
15381 at the end of the window to be redisplayed. */
15382 /* Set last_row to the glyph row in the current matrix where the
15383 window end line is found. It has been moved up or down in
15384 the matrix by dvpos. */
15385 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
15386 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
15387
15388 /* If last_row is the window end line, it should display text. */
15389 xassert (last_row->displays_text_p);
15390
15391 /* If window end line was partially visible before, begin
15392 displaying at that line. Otherwise begin displaying with the
15393 line following it. */
15394 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
15395 {
15396 init_to_row_start (&it, w, last_row);
15397 it.vpos = last_vpos;
15398 it.current_y = last_row->y;
15399 }
15400 else
15401 {
15402 init_to_row_end (&it, w, last_row);
15403 it.vpos = 1 + last_vpos;
15404 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
15405 ++last_row;
15406 }
15407
15408 /* We may start in a continuation line. If so, we have to
15409 get the right continuation_lines_width and current_x. */
15410 it.continuation_lines_width = last_row->continuation_lines_width;
15411 it.hpos = it.current_x = 0;
15412
15413 /* Display the rest of the lines at the window end. */
15414 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
15415 while (it.current_y < it.last_visible_y
15416 && !fonts_changed_p)
15417 {
15418 /* Is it always sure that the display agrees with lines in
15419 the current matrix? I don't think so, so we mark rows
15420 displayed invalid in the current matrix by setting their
15421 enabled_p flag to zero. */
15422 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
15423 if (display_line (&it))
15424 last_text_row_at_end = it.glyph_row - 1;
15425 }
15426 }
15427
15428 /* Update window_end_pos and window_end_vpos. */
15429 if (first_unchanged_at_end_row
15430 && !last_text_row_at_end)
15431 {
15432 /* Window end line if one of the preserved rows from the current
15433 matrix. Set row to the last row displaying text in current
15434 matrix starting at first_unchanged_at_end_row, after
15435 scrolling. */
15436 xassert (first_unchanged_at_end_row->displays_text_p);
15437 row = find_last_row_displaying_text (w->current_matrix, &it,
15438 first_unchanged_at_end_row);
15439 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
15440
15441 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
15442 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
15443 w->window_end_vpos
15444 = make_number (MATRIX_ROW_VPOS (row, w->current_matrix));
15445 xassert (w->window_end_bytepos >= 0);
15446 IF_DEBUG (debug_method_add (w, "A"));
15447 }
15448 else if (last_text_row_at_end)
15449 {
15450 w->window_end_pos
15451 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
15452 w->window_end_bytepos
15453 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
15454 w->window_end_vpos
15455 = make_number (MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
15456 xassert (w->window_end_bytepos >= 0);
15457 IF_DEBUG (debug_method_add (w, "B"));
15458 }
15459 else if (last_text_row)
15460 {
15461 /* We have displayed either to the end of the window or at the
15462 end of the window, i.e. the last row with text is to be found
15463 in the desired matrix. */
15464 w->window_end_pos
15465 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
15466 w->window_end_bytepos
15467 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
15468 w->window_end_vpos
15469 = make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix));
15470 xassert (w->window_end_bytepos >= 0);
15471 }
15472 else if (first_unchanged_at_end_row == NULL
15473 && last_text_row == NULL
15474 && last_text_row_at_end == NULL)
15475 {
15476 /* Displayed to end of window, but no line containing text was
15477 displayed. Lines were deleted at the end of the window. */
15478 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
15479 int vpos = XFASTINT (w->window_end_vpos);
15480 struct glyph_row *current_row = current_matrix->rows + vpos;
15481 struct glyph_row *desired_row = desired_matrix->rows + vpos;
15482
15483 for (row = NULL;
15484 row == NULL && vpos >= first_vpos;
15485 --vpos, --current_row, --desired_row)
15486 {
15487 if (desired_row->enabled_p)
15488 {
15489 if (desired_row->displays_text_p)
15490 row = desired_row;
15491 }
15492 else if (current_row->displays_text_p)
15493 row = current_row;
15494 }
15495
15496 xassert (row != NULL);
15497 w->window_end_vpos = make_number (vpos + 1);
15498 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
15499 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
15500 xassert (w->window_end_bytepos >= 0);
15501 IF_DEBUG (debug_method_add (w, "C"));
15502 }
15503 else
15504 abort ();
15505
15506 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
15507 debug_end_vpos = XFASTINT (w->window_end_vpos));
15508
15509 /* Record that display has not been completed. */
15510 w->window_end_valid = Qnil;
15511 w->desired_matrix->no_scrolling_p = 1;
15512 return 3;
15513
15514 #undef GIVE_UP
15515 }
15516
15517
15518 \f
15519 /***********************************************************************
15520 More debugging support
15521 ***********************************************************************/
15522
15523 #if GLYPH_DEBUG
15524
15525 void dump_glyph_row P_ ((struct glyph_row *, int, int));
15526 void dump_glyph_matrix P_ ((struct glyph_matrix *, int));
15527 void dump_glyph P_ ((struct glyph_row *, struct glyph *, int));
15528
15529
15530 /* Dump the contents of glyph matrix MATRIX on stderr.
15531
15532 GLYPHS 0 means don't show glyph contents.
15533 GLYPHS 1 means show glyphs in short form
15534 GLYPHS > 1 means show glyphs in long form. */
15535
15536 void
15537 dump_glyph_matrix (matrix, glyphs)
15538 struct glyph_matrix *matrix;
15539 int glyphs;
15540 {
15541 int i;
15542 for (i = 0; i < matrix->nrows; ++i)
15543 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
15544 }
15545
15546
15547 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
15548 the glyph row and area where the glyph comes from. */
15549
15550 void
15551 dump_glyph (row, glyph, area)
15552 struct glyph_row *row;
15553 struct glyph *glyph;
15554 int area;
15555 {
15556 if (glyph->type == CHAR_GLYPH)
15557 {
15558 fprintf (stderr,
15559 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
15560 glyph - row->glyphs[TEXT_AREA],
15561 'C',
15562 glyph->charpos,
15563 (BUFFERP (glyph->object)
15564 ? 'B'
15565 : (STRINGP (glyph->object)
15566 ? 'S'
15567 : '-')),
15568 glyph->pixel_width,
15569 glyph->u.ch,
15570 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
15571 ? glyph->u.ch
15572 : '.'),
15573 glyph->face_id,
15574 glyph->left_box_line_p,
15575 glyph->right_box_line_p);
15576 }
15577 else if (glyph->type == STRETCH_GLYPH)
15578 {
15579 fprintf (stderr,
15580 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
15581 glyph - row->glyphs[TEXT_AREA],
15582 'S',
15583 glyph->charpos,
15584 (BUFFERP (glyph->object)
15585 ? 'B'
15586 : (STRINGP (glyph->object)
15587 ? 'S'
15588 : '-')),
15589 glyph->pixel_width,
15590 0,
15591 '.',
15592 glyph->face_id,
15593 glyph->left_box_line_p,
15594 glyph->right_box_line_p);
15595 }
15596 else if (glyph->type == IMAGE_GLYPH)
15597 {
15598 fprintf (stderr,
15599 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
15600 glyph - row->glyphs[TEXT_AREA],
15601 'I',
15602 glyph->charpos,
15603 (BUFFERP (glyph->object)
15604 ? 'B'
15605 : (STRINGP (glyph->object)
15606 ? 'S'
15607 : '-')),
15608 glyph->pixel_width,
15609 glyph->u.img_id,
15610 '.',
15611 glyph->face_id,
15612 glyph->left_box_line_p,
15613 glyph->right_box_line_p);
15614 }
15615 else if (glyph->type == COMPOSITE_GLYPH)
15616 {
15617 fprintf (stderr,
15618 " %5d %4c %6d %c %3d 0x%05x",
15619 glyph - row->glyphs[TEXT_AREA],
15620 '+',
15621 glyph->charpos,
15622 (BUFFERP (glyph->object)
15623 ? 'B'
15624 : (STRINGP (glyph->object)
15625 ? 'S'
15626 : '-')),
15627 glyph->pixel_width,
15628 glyph->u.cmp.id);
15629 if (glyph->u.cmp.automatic)
15630 fprintf (stderr,
15631 "[%d-%d]",
15632 glyph->u.cmp.from, glyph->u.cmp.to);
15633 fprintf (stderr, " . %4d %1.1d%1.1d\n",
15634 glyph->face_id,
15635 glyph->left_box_line_p,
15636 glyph->right_box_line_p);
15637 }
15638 }
15639
15640
15641 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
15642 GLYPHS 0 means don't show glyph contents.
15643 GLYPHS 1 means show glyphs in short form
15644 GLYPHS > 1 means show glyphs in long form. */
15645
15646 void
15647 dump_glyph_row (row, vpos, glyphs)
15648 struct glyph_row *row;
15649 int vpos, glyphs;
15650 {
15651 if (glyphs != 1)
15652 {
15653 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
15654 fprintf (stderr, "======================================================================\n");
15655
15656 fprintf (stderr, "%3d %5d %5d %4d %1.1d%1.1d%1.1d%1.1d\
15657 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
15658 vpos,
15659 MATRIX_ROW_START_CHARPOS (row),
15660 MATRIX_ROW_END_CHARPOS (row),
15661 row->used[TEXT_AREA],
15662 row->contains_overlapping_glyphs_p,
15663 row->enabled_p,
15664 row->truncated_on_left_p,
15665 row->truncated_on_right_p,
15666 row->continued_p,
15667 MATRIX_ROW_CONTINUATION_LINE_P (row),
15668 row->displays_text_p,
15669 row->ends_at_zv_p,
15670 row->fill_line_p,
15671 row->ends_in_middle_of_char_p,
15672 row->starts_in_middle_of_char_p,
15673 row->mouse_face_p,
15674 row->x,
15675 row->y,
15676 row->pixel_width,
15677 row->height,
15678 row->visible_height,
15679 row->ascent,
15680 row->phys_ascent);
15681 fprintf (stderr, "%9d %5d\t%5d\n", row->start.overlay_string_index,
15682 row->end.overlay_string_index,
15683 row->continuation_lines_width);
15684 fprintf (stderr, "%9d %5d\n",
15685 CHARPOS (row->start.string_pos),
15686 CHARPOS (row->end.string_pos));
15687 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
15688 row->end.dpvec_index);
15689 }
15690
15691 if (glyphs > 1)
15692 {
15693 int area;
15694
15695 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
15696 {
15697 struct glyph *glyph = row->glyphs[area];
15698 struct glyph *glyph_end = glyph + row->used[area];
15699
15700 /* Glyph for a line end in text. */
15701 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
15702 ++glyph_end;
15703
15704 if (glyph < glyph_end)
15705 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
15706
15707 for (; glyph < glyph_end; ++glyph)
15708 dump_glyph (row, glyph, area);
15709 }
15710 }
15711 else if (glyphs == 1)
15712 {
15713 int area;
15714
15715 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
15716 {
15717 char *s = (char *) alloca (row->used[area] + 1);
15718 int i;
15719
15720 for (i = 0; i < row->used[area]; ++i)
15721 {
15722 struct glyph *glyph = row->glyphs[area] + i;
15723 if (glyph->type == CHAR_GLYPH
15724 && glyph->u.ch < 0x80
15725 && glyph->u.ch >= ' ')
15726 s[i] = glyph->u.ch;
15727 else
15728 s[i] = '.';
15729 }
15730
15731 s[i] = '\0';
15732 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
15733 }
15734 }
15735 }
15736
15737
15738 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
15739 Sdump_glyph_matrix, 0, 1, "p",
15740 doc: /* Dump the current matrix of the selected window to stderr.
15741 Shows contents of glyph row structures. With non-nil
15742 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
15743 glyphs in short form, otherwise show glyphs in long form. */)
15744 (glyphs)
15745 Lisp_Object glyphs;
15746 {
15747 struct window *w = XWINDOW (selected_window);
15748 struct buffer *buffer = XBUFFER (w->buffer);
15749
15750 fprintf (stderr, "PT = %d, BEGV = %d. ZV = %d\n",
15751 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
15752 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
15753 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
15754 fprintf (stderr, "=============================================\n");
15755 dump_glyph_matrix (w->current_matrix,
15756 NILP (glyphs) ? 0 : XINT (glyphs));
15757 return Qnil;
15758 }
15759
15760
15761 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
15762 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
15763 ()
15764 {
15765 struct frame *f = XFRAME (selected_frame);
15766 dump_glyph_matrix (f->current_matrix, 1);
15767 return Qnil;
15768 }
15769
15770
15771 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
15772 doc: /* Dump glyph row ROW to stderr.
15773 GLYPH 0 means don't dump glyphs.
15774 GLYPH 1 means dump glyphs in short form.
15775 GLYPH > 1 or omitted means dump glyphs in long form. */)
15776 (row, glyphs)
15777 Lisp_Object row, glyphs;
15778 {
15779 struct glyph_matrix *matrix;
15780 int vpos;
15781
15782 CHECK_NUMBER (row);
15783 matrix = XWINDOW (selected_window)->current_matrix;
15784 vpos = XINT (row);
15785 if (vpos >= 0 && vpos < matrix->nrows)
15786 dump_glyph_row (MATRIX_ROW (matrix, vpos),
15787 vpos,
15788 INTEGERP (glyphs) ? XINT (glyphs) : 2);
15789 return Qnil;
15790 }
15791
15792
15793 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
15794 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
15795 GLYPH 0 means don't dump glyphs.
15796 GLYPH 1 means dump glyphs in short form.
15797 GLYPH > 1 or omitted means dump glyphs in long form. */)
15798 (row, glyphs)
15799 Lisp_Object row, glyphs;
15800 {
15801 struct frame *sf = SELECTED_FRAME ();
15802 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
15803 int vpos;
15804
15805 CHECK_NUMBER (row);
15806 vpos = XINT (row);
15807 if (vpos >= 0 && vpos < m->nrows)
15808 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
15809 INTEGERP (glyphs) ? XINT (glyphs) : 2);
15810 return Qnil;
15811 }
15812
15813
15814 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
15815 doc: /* Toggle tracing of redisplay.
15816 With ARG, turn tracing on if and only if ARG is positive. */)
15817 (arg)
15818 Lisp_Object arg;
15819 {
15820 if (NILP (arg))
15821 trace_redisplay_p = !trace_redisplay_p;
15822 else
15823 {
15824 arg = Fprefix_numeric_value (arg);
15825 trace_redisplay_p = XINT (arg) > 0;
15826 }
15827
15828 return Qnil;
15829 }
15830
15831
15832 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
15833 doc: /* Like `format', but print result to stderr.
15834 usage: (trace-to-stderr STRING &rest OBJECTS) */)
15835 (nargs, args)
15836 int nargs;
15837 Lisp_Object *args;
15838 {
15839 Lisp_Object s = Fformat (nargs, args);
15840 fprintf (stderr, "%s", SDATA (s));
15841 return Qnil;
15842 }
15843
15844 #endif /* GLYPH_DEBUG */
15845
15846
15847 \f
15848 /***********************************************************************
15849 Building Desired Matrix Rows
15850 ***********************************************************************/
15851
15852 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
15853 Used for non-window-redisplay windows, and for windows w/o left fringe. */
15854
15855 static struct glyph_row *
15856 get_overlay_arrow_glyph_row (w, overlay_arrow_string)
15857 struct window *w;
15858 Lisp_Object overlay_arrow_string;
15859 {
15860 struct frame *f = XFRAME (WINDOW_FRAME (w));
15861 struct buffer *buffer = XBUFFER (w->buffer);
15862 struct buffer *old = current_buffer;
15863 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
15864 int arrow_len = SCHARS (overlay_arrow_string);
15865 const unsigned char *arrow_end = arrow_string + arrow_len;
15866 const unsigned char *p;
15867 struct it it;
15868 int multibyte_p;
15869 int n_glyphs_before;
15870
15871 set_buffer_temp (buffer);
15872 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
15873 it.glyph_row->used[TEXT_AREA] = 0;
15874 SET_TEXT_POS (it.position, 0, 0);
15875
15876 multibyte_p = !NILP (buffer->enable_multibyte_characters);
15877 p = arrow_string;
15878 while (p < arrow_end)
15879 {
15880 Lisp_Object face, ilisp;
15881
15882 /* Get the next character. */
15883 if (multibyte_p)
15884 it.c = string_char_and_length (p, &it.len);
15885 else
15886 it.c = *p, it.len = 1;
15887 p += it.len;
15888
15889 /* Get its face. */
15890 ilisp = make_number (p - arrow_string);
15891 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
15892 it.face_id = compute_char_face (f, it.c, face);
15893
15894 /* Compute its width, get its glyphs. */
15895 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
15896 SET_TEXT_POS (it.position, -1, -1);
15897 PRODUCE_GLYPHS (&it);
15898
15899 /* If this character doesn't fit any more in the line, we have
15900 to remove some glyphs. */
15901 if (it.current_x > it.last_visible_x)
15902 {
15903 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
15904 break;
15905 }
15906 }
15907
15908 set_buffer_temp (old);
15909 return it.glyph_row;
15910 }
15911
15912
15913 /* Insert truncation glyphs at the start of IT->glyph_row. Truncation
15914 glyphs are only inserted for terminal frames since we can't really
15915 win with truncation glyphs when partially visible glyphs are
15916 involved. Which glyphs to insert is determined by
15917 produce_special_glyphs. */
15918
15919 static void
15920 insert_left_trunc_glyphs (it)
15921 struct it *it;
15922 {
15923 struct it truncate_it;
15924 struct glyph *from, *end, *to, *toend;
15925
15926 xassert (!FRAME_WINDOW_P (it->f));
15927
15928 /* Get the truncation glyphs. */
15929 truncate_it = *it;
15930 truncate_it.current_x = 0;
15931 truncate_it.face_id = DEFAULT_FACE_ID;
15932 truncate_it.glyph_row = &scratch_glyph_row;
15933 truncate_it.glyph_row->used[TEXT_AREA] = 0;
15934 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
15935 truncate_it.object = make_number (0);
15936 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
15937
15938 /* Overwrite glyphs from IT with truncation glyphs. */
15939 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
15940 end = from + truncate_it.glyph_row->used[TEXT_AREA];
15941 to = it->glyph_row->glyphs[TEXT_AREA];
15942 toend = to + it->glyph_row->used[TEXT_AREA];
15943
15944 while (from < end)
15945 *to++ = *from++;
15946
15947 /* There may be padding glyphs left over. Overwrite them too. */
15948 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
15949 {
15950 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
15951 while (from < end)
15952 *to++ = *from++;
15953 }
15954
15955 if (to > toend)
15956 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
15957 }
15958
15959
15960 /* Compute the pixel height and width of IT->glyph_row.
15961
15962 Most of the time, ascent and height of a display line will be equal
15963 to the max_ascent and max_height values of the display iterator
15964 structure. This is not the case if
15965
15966 1. We hit ZV without displaying anything. In this case, max_ascent
15967 and max_height will be zero.
15968
15969 2. We have some glyphs that don't contribute to the line height.
15970 (The glyph row flag contributes_to_line_height_p is for future
15971 pixmap extensions).
15972
15973 The first case is easily covered by using default values because in
15974 these cases, the line height does not really matter, except that it
15975 must not be zero. */
15976
15977 static void
15978 compute_line_metrics (it)
15979 struct it *it;
15980 {
15981 struct glyph_row *row = it->glyph_row;
15982 int area, i;
15983
15984 if (FRAME_WINDOW_P (it->f))
15985 {
15986 int i, min_y, max_y;
15987
15988 /* The line may consist of one space only, that was added to
15989 place the cursor on it. If so, the row's height hasn't been
15990 computed yet. */
15991 if (row->height == 0)
15992 {
15993 if (it->max_ascent + it->max_descent == 0)
15994 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
15995 row->ascent = it->max_ascent;
15996 row->height = it->max_ascent + it->max_descent;
15997 row->phys_ascent = it->max_phys_ascent;
15998 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
15999 row->extra_line_spacing = it->max_extra_line_spacing;
16000 }
16001
16002 /* Compute the width of this line. */
16003 row->pixel_width = row->x;
16004 for (i = 0; i < row->used[TEXT_AREA]; ++i)
16005 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
16006
16007 xassert (row->pixel_width >= 0);
16008 xassert (row->ascent >= 0 && row->height > 0);
16009
16010 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
16011 || MATRIX_ROW_OVERLAPS_PRED_P (row));
16012
16013 /* If first line's physical ascent is larger than its logical
16014 ascent, use the physical ascent, and make the row taller.
16015 This makes accented characters fully visible. */
16016 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
16017 && row->phys_ascent > row->ascent)
16018 {
16019 row->height += row->phys_ascent - row->ascent;
16020 row->ascent = row->phys_ascent;
16021 }
16022
16023 /* Compute how much of the line is visible. */
16024 row->visible_height = row->height;
16025
16026 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
16027 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
16028
16029 if (row->y < min_y)
16030 row->visible_height -= min_y - row->y;
16031 if (row->y + row->height > max_y)
16032 row->visible_height -= row->y + row->height - max_y;
16033 }
16034 else
16035 {
16036 row->pixel_width = row->used[TEXT_AREA];
16037 if (row->continued_p)
16038 row->pixel_width -= it->continuation_pixel_width;
16039 else if (row->truncated_on_right_p)
16040 row->pixel_width -= it->truncation_pixel_width;
16041 row->ascent = row->phys_ascent = 0;
16042 row->height = row->phys_height = row->visible_height = 1;
16043 row->extra_line_spacing = 0;
16044 }
16045
16046 /* Compute a hash code for this row. */
16047 row->hash = 0;
16048 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
16049 for (i = 0; i < row->used[area]; ++i)
16050 row->hash = ((((row->hash << 4) + (row->hash >> 24)) & 0x0fffffff)
16051 + row->glyphs[area][i].u.val
16052 + row->glyphs[area][i].face_id
16053 + row->glyphs[area][i].padding_p
16054 + (row->glyphs[area][i].type << 2));
16055
16056 it->max_ascent = it->max_descent = 0;
16057 it->max_phys_ascent = it->max_phys_descent = 0;
16058 }
16059
16060
16061 /* Append one space to the glyph row of iterator IT if doing a
16062 window-based redisplay. The space has the same face as
16063 IT->face_id. Value is non-zero if a space was added.
16064
16065 This function is called to make sure that there is always one glyph
16066 at the end of a glyph row that the cursor can be set on under
16067 window-systems. (If there weren't such a glyph we would not know
16068 how wide and tall a box cursor should be displayed).
16069
16070 At the same time this space let's a nicely handle clearing to the
16071 end of the line if the row ends in italic text. */
16072
16073 static int
16074 append_space_for_newline (it, default_face_p)
16075 struct it *it;
16076 int default_face_p;
16077 {
16078 if (FRAME_WINDOW_P (it->f))
16079 {
16080 int n = it->glyph_row->used[TEXT_AREA];
16081
16082 if (it->glyph_row->glyphs[TEXT_AREA] + n
16083 < it->glyph_row->glyphs[1 + TEXT_AREA])
16084 {
16085 /* Save some values that must not be changed.
16086 Must save IT->c and IT->len because otherwise
16087 ITERATOR_AT_END_P wouldn't work anymore after
16088 append_space_for_newline has been called. */
16089 enum display_element_type saved_what = it->what;
16090 int saved_c = it->c, saved_len = it->len;
16091 int saved_x = it->current_x;
16092 int saved_face_id = it->face_id;
16093 struct text_pos saved_pos;
16094 Lisp_Object saved_object;
16095 struct face *face;
16096
16097 saved_object = it->object;
16098 saved_pos = it->position;
16099
16100 it->what = IT_CHARACTER;
16101 bzero (&it->position, sizeof it->position);
16102 it->object = make_number (0);
16103 it->c = ' ';
16104 it->len = 1;
16105
16106 if (default_face_p)
16107 it->face_id = DEFAULT_FACE_ID;
16108 else if (it->face_before_selective_p)
16109 it->face_id = it->saved_face_id;
16110 face = FACE_FROM_ID (it->f, it->face_id);
16111 it->face_id = FACE_FOR_CHAR (it->f, face, 0, -1, Qnil);
16112
16113 PRODUCE_GLYPHS (it);
16114
16115 it->override_ascent = -1;
16116 it->constrain_row_ascent_descent_p = 0;
16117 it->current_x = saved_x;
16118 it->object = saved_object;
16119 it->position = saved_pos;
16120 it->what = saved_what;
16121 it->face_id = saved_face_id;
16122 it->len = saved_len;
16123 it->c = saved_c;
16124 return 1;
16125 }
16126 }
16127
16128 return 0;
16129 }
16130
16131
16132 /* Extend the face of the last glyph in the text area of IT->glyph_row
16133 to the end of the display line. Called from display_line.
16134 If the glyph row is empty, add a space glyph to it so that we
16135 know the face to draw. Set the glyph row flag fill_line_p. */
16136
16137 static void
16138 extend_face_to_end_of_line (it)
16139 struct it *it;
16140 {
16141 struct face *face;
16142 struct frame *f = it->f;
16143
16144 /* If line is already filled, do nothing. */
16145 if (it->current_x >= it->last_visible_x)
16146 return;
16147
16148 /* Face extension extends the background and box of IT->face_id
16149 to the end of the line. If the background equals the background
16150 of the frame, we don't have to do anything. */
16151 if (it->face_before_selective_p)
16152 face = FACE_FROM_ID (it->f, it->saved_face_id);
16153 else
16154 face = FACE_FROM_ID (f, it->face_id);
16155
16156 if (FRAME_WINDOW_P (f)
16157 && it->glyph_row->displays_text_p
16158 && face->box == FACE_NO_BOX
16159 && face->background == FRAME_BACKGROUND_PIXEL (f)
16160 && !face->stipple)
16161 return;
16162
16163 /* Set the glyph row flag indicating that the face of the last glyph
16164 in the text area has to be drawn to the end of the text area. */
16165 it->glyph_row->fill_line_p = 1;
16166
16167 /* If current character of IT is not ASCII, make sure we have the
16168 ASCII face. This will be automatically undone the next time
16169 get_next_display_element returns a multibyte character. Note
16170 that the character will always be single byte in unibyte
16171 text. */
16172 if (!ASCII_CHAR_P (it->c))
16173 {
16174 it->face_id = FACE_FOR_CHAR (f, face, 0, -1, Qnil);
16175 }
16176
16177 if (FRAME_WINDOW_P (f))
16178 {
16179 /* If the row is empty, add a space with the current face of IT,
16180 so that we know which face to draw. */
16181 if (it->glyph_row->used[TEXT_AREA] == 0)
16182 {
16183 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
16184 it->glyph_row->glyphs[TEXT_AREA][0].face_id = it->face_id;
16185 it->glyph_row->used[TEXT_AREA] = 1;
16186 }
16187 }
16188 else
16189 {
16190 /* Save some values that must not be changed. */
16191 int saved_x = it->current_x;
16192 struct text_pos saved_pos;
16193 Lisp_Object saved_object;
16194 enum display_element_type saved_what = it->what;
16195 int saved_face_id = it->face_id;
16196
16197 saved_object = it->object;
16198 saved_pos = it->position;
16199
16200 it->what = IT_CHARACTER;
16201 bzero (&it->position, sizeof it->position);
16202 it->object = make_number (0);
16203 it->c = ' ';
16204 it->len = 1;
16205 it->face_id = face->id;
16206
16207 PRODUCE_GLYPHS (it);
16208
16209 while (it->current_x <= it->last_visible_x)
16210 PRODUCE_GLYPHS (it);
16211
16212 /* Don't count these blanks really. It would let us insert a left
16213 truncation glyph below and make us set the cursor on them, maybe. */
16214 it->current_x = saved_x;
16215 it->object = saved_object;
16216 it->position = saved_pos;
16217 it->what = saved_what;
16218 it->face_id = saved_face_id;
16219 }
16220 }
16221
16222
16223 /* Value is non-zero if text starting at CHARPOS in current_buffer is
16224 trailing whitespace. */
16225
16226 static int
16227 trailing_whitespace_p (charpos)
16228 int charpos;
16229 {
16230 int bytepos = CHAR_TO_BYTE (charpos);
16231 int c = 0;
16232
16233 while (bytepos < ZV_BYTE
16234 && (c = FETCH_CHAR (bytepos),
16235 c == ' ' || c == '\t'))
16236 ++bytepos;
16237
16238 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
16239 {
16240 if (bytepos != PT_BYTE)
16241 return 1;
16242 }
16243 return 0;
16244 }
16245
16246
16247 /* Highlight trailing whitespace, if any, in ROW. */
16248
16249 void
16250 highlight_trailing_whitespace (f, row)
16251 struct frame *f;
16252 struct glyph_row *row;
16253 {
16254 int used = row->used[TEXT_AREA];
16255
16256 if (used)
16257 {
16258 struct glyph *start = row->glyphs[TEXT_AREA];
16259 struct glyph *glyph = start + used - 1;
16260
16261 /* Skip over glyphs inserted to display the cursor at the
16262 end of a line, for extending the face of the last glyph
16263 to the end of the line on terminals, and for truncation
16264 and continuation glyphs. */
16265 while (glyph >= start
16266 && glyph->type == CHAR_GLYPH
16267 && INTEGERP (glyph->object))
16268 --glyph;
16269
16270 /* If last glyph is a space or stretch, and it's trailing
16271 whitespace, set the face of all trailing whitespace glyphs in
16272 IT->glyph_row to `trailing-whitespace'. */
16273 if (glyph >= start
16274 && BUFFERP (glyph->object)
16275 && (glyph->type == STRETCH_GLYPH
16276 || (glyph->type == CHAR_GLYPH
16277 && glyph->u.ch == ' '))
16278 && trailing_whitespace_p (glyph->charpos))
16279 {
16280 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
16281 if (face_id < 0)
16282 return;
16283
16284 while (glyph >= start
16285 && BUFFERP (glyph->object)
16286 && (glyph->type == STRETCH_GLYPH
16287 || (glyph->type == CHAR_GLYPH
16288 && glyph->u.ch == ' ')))
16289 (glyph--)->face_id = face_id;
16290 }
16291 }
16292 }
16293
16294
16295 /* Value is non-zero if glyph row ROW in window W should be
16296 used to hold the cursor. */
16297
16298 static int
16299 cursor_row_p (w, row)
16300 struct window *w;
16301 struct glyph_row *row;
16302 {
16303 int cursor_row_p = 1;
16304
16305 if (PT == MATRIX_ROW_END_CHARPOS (row))
16306 {
16307 /* Suppose the row ends on a string.
16308 Unless the row is continued, that means it ends on a newline
16309 in the string. If it's anything other than a display string
16310 (e.g. a before-string from an overlay), we don't want the
16311 cursor there. (This heuristic seems to give the optimal
16312 behavior for the various types of multi-line strings.) */
16313 if (CHARPOS (row->end.string_pos) >= 0)
16314 {
16315 if (row->continued_p)
16316 cursor_row_p = 1;
16317 else
16318 {
16319 /* Check for `display' property. */
16320 struct glyph *beg = row->glyphs[TEXT_AREA];
16321 struct glyph *end = beg + row->used[TEXT_AREA] - 1;
16322 struct glyph *glyph;
16323
16324 cursor_row_p = 0;
16325 for (glyph = end; glyph >= beg; --glyph)
16326 if (STRINGP (glyph->object))
16327 {
16328 Lisp_Object prop
16329 = Fget_char_property (make_number (PT),
16330 Qdisplay, Qnil);
16331 cursor_row_p =
16332 (!NILP (prop)
16333 && display_prop_string_p (prop, glyph->object));
16334 break;
16335 }
16336 }
16337 }
16338 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
16339 {
16340 /* If the row ends in middle of a real character,
16341 and the line is continued, we want the cursor here.
16342 That's because MATRIX_ROW_END_CHARPOS would equal
16343 PT if PT is before the character. */
16344 if (!row->ends_in_ellipsis_p)
16345 cursor_row_p = row->continued_p;
16346 else
16347 /* If the row ends in an ellipsis, then
16348 MATRIX_ROW_END_CHARPOS will equal point after the invisible text.
16349 We want that position to be displayed after the ellipsis. */
16350 cursor_row_p = 0;
16351 }
16352 /* If the row ends at ZV, display the cursor at the end of that
16353 row instead of at the start of the row below. */
16354 else if (row->ends_at_zv_p)
16355 cursor_row_p = 1;
16356 else
16357 cursor_row_p = 0;
16358 }
16359
16360 return cursor_row_p;
16361 }
16362
16363 \f
16364
16365 /* Push the display property PROP so that it will be rendered at the
16366 current position in IT. Return 1 if PROP was successfully pushed,
16367 0 otherwise. */
16368
16369 static int
16370 push_display_prop (struct it *it, Lisp_Object prop)
16371 {
16372 push_it (it);
16373
16374 if (STRINGP (prop))
16375 {
16376 if (SCHARS (prop) == 0)
16377 {
16378 pop_it (it);
16379 return 0;
16380 }
16381
16382 it->string = prop;
16383 it->multibyte_p = STRING_MULTIBYTE (it->string);
16384 it->current.overlay_string_index = -1;
16385 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
16386 it->end_charpos = it->string_nchars = SCHARS (it->string);
16387 it->method = GET_FROM_STRING;
16388 it->stop_charpos = 0;
16389 }
16390 else if (CONSP (prop) && EQ (XCAR (prop), Qspace))
16391 {
16392 it->method = GET_FROM_STRETCH;
16393 it->object = prop;
16394 }
16395 #ifdef HAVE_WINDOW_SYSTEM
16396 else if (IMAGEP (prop))
16397 {
16398 it->what = IT_IMAGE;
16399 it->image_id = lookup_image (it->f, prop);
16400 it->method = GET_FROM_IMAGE;
16401 }
16402 #endif /* HAVE_WINDOW_SYSTEM */
16403 else
16404 {
16405 pop_it (it); /* bogus display property, give up */
16406 return 0;
16407 }
16408
16409 return 1;
16410 }
16411
16412 /* Return the character-property PROP at the current position in IT. */
16413
16414 static Lisp_Object
16415 get_it_property (it, prop)
16416 struct it *it;
16417 Lisp_Object prop;
16418 {
16419 Lisp_Object position;
16420
16421 if (STRINGP (it->object))
16422 position = make_number (IT_STRING_CHARPOS (*it));
16423 else if (BUFFERP (it->object))
16424 position = make_number (IT_CHARPOS (*it));
16425 else
16426 return Qnil;
16427
16428 return Fget_char_property (position, prop, it->object);
16429 }
16430
16431 /* See if there's a line- or wrap-prefix, and if so, push it on IT. */
16432
16433 static void
16434 handle_line_prefix (struct it *it)
16435 {
16436 Lisp_Object prefix;
16437 if (it->continuation_lines_width > 0)
16438 {
16439 prefix = get_it_property (it, Qwrap_prefix);
16440 if (NILP (prefix))
16441 prefix = Vwrap_prefix;
16442 }
16443 else
16444 {
16445 prefix = get_it_property (it, Qline_prefix);
16446 if (NILP (prefix))
16447 prefix = Vline_prefix;
16448 }
16449 if (! NILP (prefix) && push_display_prop (it, prefix))
16450 {
16451 /* If the prefix is wider than the window, and we try to wrap
16452 it, it would acquire its own wrap prefix, and so on till the
16453 iterator stack overflows. So, don't wrap the prefix. */
16454 it->line_wrap = TRUNCATE;
16455 it->avoid_cursor_p = 1;
16456 }
16457 }
16458
16459 \f
16460
16461 /* Construct the glyph row IT->glyph_row in the desired matrix of
16462 IT->w from text at the current position of IT. See dispextern.h
16463 for an overview of struct it. Value is non-zero if
16464 IT->glyph_row displays text, as opposed to a line displaying ZV
16465 only. */
16466
16467 static int
16468 display_line (it)
16469 struct it *it;
16470 {
16471 struct glyph_row *row = it->glyph_row;
16472 Lisp_Object overlay_arrow_string;
16473 struct it wrap_it;
16474 int may_wrap = 0, wrap_x;
16475 int wrap_row_used = -1, wrap_row_ascent, wrap_row_height;
16476 int wrap_row_phys_ascent, wrap_row_phys_height;
16477 int wrap_row_extra_line_spacing;
16478
16479 /* We always start displaying at hpos zero even if hscrolled. */
16480 xassert (it->hpos == 0 && it->current_x == 0);
16481
16482 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
16483 >= it->w->desired_matrix->nrows)
16484 {
16485 it->w->nrows_scale_factor++;
16486 fonts_changed_p = 1;
16487 return 0;
16488 }
16489
16490 /* Is IT->w showing the region? */
16491 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
16492
16493 /* Clear the result glyph row and enable it. */
16494 prepare_desired_row (row);
16495
16496 row->y = it->current_y;
16497 row->start = it->start;
16498 row->continuation_lines_width = it->continuation_lines_width;
16499 row->displays_text_p = 1;
16500 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
16501 it->starts_in_middle_of_char_p = 0;
16502
16503 /* Arrange the overlays nicely for our purposes. Usually, we call
16504 display_line on only one line at a time, in which case this
16505 can't really hurt too much, or we call it on lines which appear
16506 one after another in the buffer, in which case all calls to
16507 recenter_overlay_lists but the first will be pretty cheap. */
16508 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
16509
16510 /* Move over display elements that are not visible because we are
16511 hscrolled. This may stop at an x-position < IT->first_visible_x
16512 if the first glyph is partially visible or if we hit a line end. */
16513 if (it->current_x < it->first_visible_x)
16514 {
16515 move_it_in_display_line_to (it, ZV, it->first_visible_x,
16516 MOVE_TO_POS | MOVE_TO_X);
16517 }
16518 else
16519 {
16520 /* We only do this when not calling `move_it_in_display_line_to'
16521 above, because move_it_in_display_line_to calls
16522 handle_line_prefix itself. */
16523 handle_line_prefix (it);
16524 }
16525
16526 /* Get the initial row height. This is either the height of the
16527 text hscrolled, if there is any, or zero. */
16528 row->ascent = it->max_ascent;
16529 row->height = it->max_ascent + it->max_descent;
16530 row->phys_ascent = it->max_phys_ascent;
16531 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
16532 row->extra_line_spacing = it->max_extra_line_spacing;
16533
16534 /* Loop generating characters. The loop is left with IT on the next
16535 character to display. */
16536 while (1)
16537 {
16538 int n_glyphs_before, hpos_before, x_before;
16539 int x, i, nglyphs;
16540 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
16541
16542 /* Retrieve the next thing to display. Value is zero if end of
16543 buffer reached. */
16544 if (!get_next_display_element (it))
16545 {
16546 /* Maybe add a space at the end of this line that is used to
16547 display the cursor there under X. Set the charpos of the
16548 first glyph of blank lines not corresponding to any text
16549 to -1. */
16550 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
16551 row->exact_window_width_line_p = 1;
16552 else if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
16553 || row->used[TEXT_AREA] == 0)
16554 {
16555 row->glyphs[TEXT_AREA]->charpos = -1;
16556 row->displays_text_p = 0;
16557
16558 if (!NILP (XBUFFER (it->w->buffer)->indicate_empty_lines)
16559 && (!MINI_WINDOW_P (it->w)
16560 || (minibuf_level && EQ (it->window, minibuf_window))))
16561 row->indicate_empty_line_p = 1;
16562 }
16563
16564 it->continuation_lines_width = 0;
16565 row->ends_at_zv_p = 1;
16566 break;
16567 }
16568
16569 /* Now, get the metrics of what we want to display. This also
16570 generates glyphs in `row' (which is IT->glyph_row). */
16571 n_glyphs_before = row->used[TEXT_AREA];
16572 x = it->current_x;
16573
16574 /* Remember the line height so far in case the next element doesn't
16575 fit on the line. */
16576 if (it->line_wrap != TRUNCATE)
16577 {
16578 ascent = it->max_ascent;
16579 descent = it->max_descent;
16580 phys_ascent = it->max_phys_ascent;
16581 phys_descent = it->max_phys_descent;
16582
16583 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
16584 {
16585 if (IT_DISPLAYING_WHITESPACE (it))
16586 may_wrap = 1;
16587 else if (may_wrap)
16588 {
16589 wrap_it = *it;
16590 wrap_x = x;
16591 wrap_row_used = row->used[TEXT_AREA];
16592 wrap_row_ascent = row->ascent;
16593 wrap_row_height = row->height;
16594 wrap_row_phys_ascent = row->phys_ascent;
16595 wrap_row_phys_height = row->phys_height;
16596 wrap_row_extra_line_spacing = row->extra_line_spacing;
16597 may_wrap = 0;
16598 }
16599 }
16600 }
16601
16602 PRODUCE_GLYPHS (it);
16603
16604 /* If this display element was in marginal areas, continue with
16605 the next one. */
16606 if (it->area != TEXT_AREA)
16607 {
16608 row->ascent = max (row->ascent, it->max_ascent);
16609 row->height = max (row->height, it->max_ascent + it->max_descent);
16610 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
16611 row->phys_height = max (row->phys_height,
16612 it->max_phys_ascent + it->max_phys_descent);
16613 row->extra_line_spacing = max (row->extra_line_spacing,
16614 it->max_extra_line_spacing);
16615 set_iterator_to_next (it, 1);
16616 continue;
16617 }
16618
16619 /* Does the display element fit on the line? If we truncate
16620 lines, we should draw past the right edge of the window. If
16621 we don't truncate, we want to stop so that we can display the
16622 continuation glyph before the right margin. If lines are
16623 continued, there are two possible strategies for characters
16624 resulting in more than 1 glyph (e.g. tabs): Display as many
16625 glyphs as possible in this line and leave the rest for the
16626 continuation line, or display the whole element in the next
16627 line. Original redisplay did the former, so we do it also. */
16628 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
16629 hpos_before = it->hpos;
16630 x_before = x;
16631
16632 if (/* Not a newline. */
16633 nglyphs > 0
16634 /* Glyphs produced fit entirely in the line. */
16635 && it->current_x < it->last_visible_x)
16636 {
16637 it->hpos += nglyphs;
16638 row->ascent = max (row->ascent, it->max_ascent);
16639 row->height = max (row->height, it->max_ascent + it->max_descent);
16640 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
16641 row->phys_height = max (row->phys_height,
16642 it->max_phys_ascent + it->max_phys_descent);
16643 row->extra_line_spacing = max (row->extra_line_spacing,
16644 it->max_extra_line_spacing);
16645 if (it->current_x - it->pixel_width < it->first_visible_x)
16646 row->x = x - it->first_visible_x;
16647 }
16648 else
16649 {
16650 int new_x;
16651 struct glyph *glyph;
16652
16653 for (i = 0; i < nglyphs; ++i, x = new_x)
16654 {
16655 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
16656 new_x = x + glyph->pixel_width;
16657
16658 if (/* Lines are continued. */
16659 it->line_wrap != TRUNCATE
16660 && (/* Glyph doesn't fit on the line. */
16661 new_x > it->last_visible_x
16662 /* Or it fits exactly on a window system frame. */
16663 || (new_x == it->last_visible_x
16664 && FRAME_WINDOW_P (it->f))))
16665 {
16666 /* End of a continued line. */
16667
16668 if (it->hpos == 0
16669 || (new_x == it->last_visible_x
16670 && FRAME_WINDOW_P (it->f)))
16671 {
16672 /* Current glyph is the only one on the line or
16673 fits exactly on the line. We must continue
16674 the line because we can't draw the cursor
16675 after the glyph. */
16676 row->continued_p = 1;
16677 it->current_x = new_x;
16678 it->continuation_lines_width += new_x;
16679 ++it->hpos;
16680 if (i == nglyphs - 1)
16681 {
16682 /* If line-wrap is on, check if a previous
16683 wrap point was found. */
16684 if (wrap_row_used > 0
16685 /* Even if there is a previous wrap
16686 point, continue the line here as
16687 usual, if (i) the previous character
16688 was a space or tab AND (ii) the
16689 current character is not. */
16690 && (!may_wrap
16691 || IT_DISPLAYING_WHITESPACE (it)))
16692 goto back_to_wrap;
16693
16694 set_iterator_to_next (it, 1);
16695 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
16696 {
16697 if (!get_next_display_element (it))
16698 {
16699 row->exact_window_width_line_p = 1;
16700 it->continuation_lines_width = 0;
16701 row->continued_p = 0;
16702 row->ends_at_zv_p = 1;
16703 }
16704 else if (ITERATOR_AT_END_OF_LINE_P (it))
16705 {
16706 row->continued_p = 0;
16707 row->exact_window_width_line_p = 1;
16708 }
16709 }
16710 }
16711 }
16712 else if (CHAR_GLYPH_PADDING_P (*glyph)
16713 && !FRAME_WINDOW_P (it->f))
16714 {
16715 /* A padding glyph that doesn't fit on this line.
16716 This means the whole character doesn't fit
16717 on the line. */
16718 row->used[TEXT_AREA] = n_glyphs_before;
16719
16720 /* Fill the rest of the row with continuation
16721 glyphs like in 20.x. */
16722 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
16723 < row->glyphs[1 + TEXT_AREA])
16724 produce_special_glyphs (it, IT_CONTINUATION);
16725
16726 row->continued_p = 1;
16727 it->current_x = x_before;
16728 it->continuation_lines_width += x_before;
16729
16730 /* Restore the height to what it was before the
16731 element not fitting on the line. */
16732 it->max_ascent = ascent;
16733 it->max_descent = descent;
16734 it->max_phys_ascent = phys_ascent;
16735 it->max_phys_descent = phys_descent;
16736 }
16737 else if (wrap_row_used > 0)
16738 {
16739 back_to_wrap:
16740 *it = wrap_it;
16741 it->continuation_lines_width += wrap_x;
16742 row->used[TEXT_AREA] = wrap_row_used;
16743 row->ascent = wrap_row_ascent;
16744 row->height = wrap_row_height;
16745 row->phys_ascent = wrap_row_phys_ascent;
16746 row->phys_height = wrap_row_phys_height;
16747 row->extra_line_spacing = wrap_row_extra_line_spacing;
16748 row->continued_p = 1;
16749 row->ends_at_zv_p = 0;
16750 row->exact_window_width_line_p = 0;
16751 it->continuation_lines_width += x;
16752
16753 /* Make sure that a non-default face is extended
16754 up to the right margin of the window. */
16755 extend_face_to_end_of_line (it);
16756 }
16757 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
16758 {
16759 /* A TAB that extends past the right edge of the
16760 window. This produces a single glyph on
16761 window system frames. We leave the glyph in
16762 this row and let it fill the row, but don't
16763 consume the TAB. */
16764 it->continuation_lines_width += it->last_visible_x;
16765 row->ends_in_middle_of_char_p = 1;
16766 row->continued_p = 1;
16767 glyph->pixel_width = it->last_visible_x - x;
16768 it->starts_in_middle_of_char_p = 1;
16769 }
16770 else
16771 {
16772 /* Something other than a TAB that draws past
16773 the right edge of the window. Restore
16774 positions to values before the element. */
16775 row->used[TEXT_AREA] = n_glyphs_before + i;
16776
16777 /* Display continuation glyphs. */
16778 if (!FRAME_WINDOW_P (it->f))
16779 produce_special_glyphs (it, IT_CONTINUATION);
16780 row->continued_p = 1;
16781
16782 it->current_x = x_before;
16783 it->continuation_lines_width += x;
16784 extend_face_to_end_of_line (it);
16785
16786 if (nglyphs > 1 && i > 0)
16787 {
16788 row->ends_in_middle_of_char_p = 1;
16789 it->starts_in_middle_of_char_p = 1;
16790 }
16791
16792 /* Restore the height to what it was before the
16793 element not fitting on the line. */
16794 it->max_ascent = ascent;
16795 it->max_descent = descent;
16796 it->max_phys_ascent = phys_ascent;
16797 it->max_phys_descent = phys_descent;
16798 }
16799
16800 break;
16801 }
16802 else if (new_x > it->first_visible_x)
16803 {
16804 /* Increment number of glyphs actually displayed. */
16805 ++it->hpos;
16806
16807 if (x < it->first_visible_x)
16808 /* Glyph is partially visible, i.e. row starts at
16809 negative X position. */
16810 row->x = x - it->first_visible_x;
16811 }
16812 else
16813 {
16814 /* Glyph is completely off the left margin of the
16815 window. This should not happen because of the
16816 move_it_in_display_line at the start of this
16817 function, unless the text display area of the
16818 window is empty. */
16819 xassert (it->first_visible_x <= it->last_visible_x);
16820 }
16821 }
16822
16823 row->ascent = max (row->ascent, it->max_ascent);
16824 row->height = max (row->height, it->max_ascent + it->max_descent);
16825 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
16826 row->phys_height = max (row->phys_height,
16827 it->max_phys_ascent + it->max_phys_descent);
16828 row->extra_line_spacing = max (row->extra_line_spacing,
16829 it->max_extra_line_spacing);
16830
16831 /* End of this display line if row is continued. */
16832 if (row->continued_p || row->ends_at_zv_p)
16833 break;
16834 }
16835
16836 at_end_of_line:
16837 /* Is this a line end? If yes, we're also done, after making
16838 sure that a non-default face is extended up to the right
16839 margin of the window. */
16840 if (ITERATOR_AT_END_OF_LINE_P (it))
16841 {
16842 int used_before = row->used[TEXT_AREA];
16843
16844 row->ends_in_newline_from_string_p = STRINGP (it->object);
16845
16846 /* Add a space at the end of the line that is used to
16847 display the cursor there. */
16848 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
16849 append_space_for_newline (it, 0);
16850
16851 /* Extend the face to the end of the line. */
16852 extend_face_to_end_of_line (it);
16853
16854 /* Make sure we have the position. */
16855 if (used_before == 0)
16856 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
16857
16858 /* Consume the line end. This skips over invisible lines. */
16859 set_iterator_to_next (it, 1);
16860 it->continuation_lines_width = 0;
16861 break;
16862 }
16863
16864 /* Proceed with next display element. Note that this skips
16865 over lines invisible because of selective display. */
16866 set_iterator_to_next (it, 1);
16867
16868 /* If we truncate lines, we are done when the last displayed
16869 glyphs reach past the right margin of the window. */
16870 if (it->line_wrap == TRUNCATE
16871 && (FRAME_WINDOW_P (it->f)
16872 ? (it->current_x >= it->last_visible_x)
16873 : (it->current_x > it->last_visible_x)))
16874 {
16875 /* Maybe add truncation glyphs. */
16876 if (!FRAME_WINDOW_P (it->f))
16877 {
16878 int i, n;
16879
16880 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
16881 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
16882 break;
16883
16884 for (n = row->used[TEXT_AREA]; i < n; ++i)
16885 {
16886 row->used[TEXT_AREA] = i;
16887 produce_special_glyphs (it, IT_TRUNCATION);
16888 }
16889 }
16890 else if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
16891 {
16892 /* Don't truncate if we can overflow newline into fringe. */
16893 if (!get_next_display_element (it))
16894 {
16895 it->continuation_lines_width = 0;
16896 row->ends_at_zv_p = 1;
16897 row->exact_window_width_line_p = 1;
16898 break;
16899 }
16900 if (ITERATOR_AT_END_OF_LINE_P (it))
16901 {
16902 row->exact_window_width_line_p = 1;
16903 goto at_end_of_line;
16904 }
16905 }
16906
16907 row->truncated_on_right_p = 1;
16908 it->continuation_lines_width = 0;
16909 reseat_at_next_visible_line_start (it, 0);
16910 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
16911 it->hpos = hpos_before;
16912 it->current_x = x_before;
16913 break;
16914 }
16915 }
16916
16917 /* If line is not empty and hscrolled, maybe insert truncation glyphs
16918 at the left window margin. */
16919 if (it->first_visible_x
16920 && IT_CHARPOS (*it) != MATRIX_ROW_START_CHARPOS (row))
16921 {
16922 if (!FRAME_WINDOW_P (it->f))
16923 insert_left_trunc_glyphs (it);
16924 row->truncated_on_left_p = 1;
16925 }
16926
16927 /* If the start of this line is the overlay arrow-position, then
16928 mark this glyph row as the one containing the overlay arrow.
16929 This is clearly a mess with variable size fonts. It would be
16930 better to let it be displayed like cursors under X. */
16931 if ((row->displays_text_p || !overlay_arrow_seen)
16932 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
16933 !NILP (overlay_arrow_string)))
16934 {
16935 /* Overlay arrow in window redisplay is a fringe bitmap. */
16936 if (STRINGP (overlay_arrow_string))
16937 {
16938 struct glyph_row *arrow_row
16939 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
16940 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
16941 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
16942 struct glyph *p = row->glyphs[TEXT_AREA];
16943 struct glyph *p2, *end;
16944
16945 /* Copy the arrow glyphs. */
16946 while (glyph < arrow_end)
16947 *p++ = *glyph++;
16948
16949 /* Throw away padding glyphs. */
16950 p2 = p;
16951 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
16952 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
16953 ++p2;
16954 if (p2 > p)
16955 {
16956 while (p2 < end)
16957 *p++ = *p2++;
16958 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
16959 }
16960 }
16961 else
16962 {
16963 xassert (INTEGERP (overlay_arrow_string));
16964 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
16965 }
16966 overlay_arrow_seen = 1;
16967 }
16968
16969 /* Compute pixel dimensions of this line. */
16970 compute_line_metrics (it);
16971
16972 /* Remember the position at which this line ends. */
16973 row->end = it->current;
16974
16975 /* Record whether this row ends inside an ellipsis. */
16976 row->ends_in_ellipsis_p
16977 = (it->method == GET_FROM_DISPLAY_VECTOR
16978 && it->ellipsis_p);
16979
16980 /* Save fringe bitmaps in this row. */
16981 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
16982 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
16983 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
16984 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
16985
16986 it->left_user_fringe_bitmap = 0;
16987 it->left_user_fringe_face_id = 0;
16988 it->right_user_fringe_bitmap = 0;
16989 it->right_user_fringe_face_id = 0;
16990
16991 /* Maybe set the cursor. */
16992 if (it->w->cursor.vpos < 0
16993 && PT >= MATRIX_ROW_START_CHARPOS (row)
16994 && PT <= MATRIX_ROW_END_CHARPOS (row)
16995 && cursor_row_p (it->w, row))
16996 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
16997
16998 /* Highlight trailing whitespace. */
16999 if (!NILP (Vshow_trailing_whitespace))
17000 highlight_trailing_whitespace (it->f, it->glyph_row);
17001
17002 /* Prepare for the next line. This line starts horizontally at (X
17003 HPOS) = (0 0). Vertical positions are incremented. As a
17004 convenience for the caller, IT->glyph_row is set to the next
17005 row to be used. */
17006 it->current_x = it->hpos = 0;
17007 it->current_y += row->height;
17008 ++it->vpos;
17009 ++it->glyph_row;
17010 it->start = it->current;
17011 return row->displays_text_p;
17012 }
17013
17014
17015 \f
17016 /***********************************************************************
17017 Menu Bar
17018 ***********************************************************************/
17019
17020 /* Redisplay the menu bar in the frame for window W.
17021
17022 The menu bar of X frames that don't have X toolkit support is
17023 displayed in a special window W->frame->menu_bar_window.
17024
17025 The menu bar of terminal frames is treated specially as far as
17026 glyph matrices are concerned. Menu bar lines are not part of
17027 windows, so the update is done directly on the frame matrix rows
17028 for the menu bar. */
17029
17030 static void
17031 display_menu_bar (w)
17032 struct window *w;
17033 {
17034 struct frame *f = XFRAME (WINDOW_FRAME (w));
17035 struct it it;
17036 Lisp_Object items;
17037 int i;
17038
17039 /* Don't do all this for graphical frames. */
17040 #ifdef HAVE_NTGUI
17041 if (FRAME_W32_P (f))
17042 return;
17043 #endif
17044 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
17045 if (FRAME_X_P (f))
17046 return;
17047 #endif
17048
17049 #ifdef HAVE_NS
17050 if (FRAME_NS_P (f))
17051 return;
17052 #endif /* HAVE_NS */
17053
17054 #ifdef USE_X_TOOLKIT
17055 xassert (!FRAME_WINDOW_P (f));
17056 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
17057 it.first_visible_x = 0;
17058 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
17059 #else /* not USE_X_TOOLKIT */
17060 if (FRAME_WINDOW_P (f))
17061 {
17062 /* Menu bar lines are displayed in the desired matrix of the
17063 dummy window menu_bar_window. */
17064 struct window *menu_w;
17065 xassert (WINDOWP (f->menu_bar_window));
17066 menu_w = XWINDOW (f->menu_bar_window);
17067 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
17068 MENU_FACE_ID);
17069 it.first_visible_x = 0;
17070 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
17071 }
17072 else
17073 {
17074 /* This is a TTY frame, i.e. character hpos/vpos are used as
17075 pixel x/y. */
17076 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
17077 MENU_FACE_ID);
17078 it.first_visible_x = 0;
17079 it.last_visible_x = FRAME_COLS (f);
17080 }
17081 #endif /* not USE_X_TOOLKIT */
17082
17083 if (! mode_line_inverse_video)
17084 /* Force the menu-bar to be displayed in the default face. */
17085 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
17086
17087 /* Clear all rows of the menu bar. */
17088 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
17089 {
17090 struct glyph_row *row = it.glyph_row + i;
17091 clear_glyph_row (row);
17092 row->enabled_p = 1;
17093 row->full_width_p = 1;
17094 }
17095
17096 /* Display all items of the menu bar. */
17097 items = FRAME_MENU_BAR_ITEMS (it.f);
17098 for (i = 0; i < XVECTOR (items)->size; i += 4)
17099 {
17100 Lisp_Object string;
17101
17102 /* Stop at nil string. */
17103 string = AREF (items, i + 1);
17104 if (NILP (string))
17105 break;
17106
17107 /* Remember where item was displayed. */
17108 ASET (items, i + 3, make_number (it.hpos));
17109
17110 /* Display the item, pad with one space. */
17111 if (it.current_x < it.last_visible_x)
17112 display_string (NULL, string, Qnil, 0, 0, &it,
17113 SCHARS (string) + 1, 0, 0, -1);
17114 }
17115
17116 /* Fill out the line with spaces. */
17117 if (it.current_x < it.last_visible_x)
17118 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
17119
17120 /* Compute the total height of the lines. */
17121 compute_line_metrics (&it);
17122 }
17123
17124
17125 \f
17126 /***********************************************************************
17127 Mode Line
17128 ***********************************************************************/
17129
17130 /* Redisplay mode lines in the window tree whose root is WINDOW. If
17131 FORCE is non-zero, redisplay mode lines unconditionally.
17132 Otherwise, redisplay only mode lines that are garbaged. Value is
17133 the number of windows whose mode lines were redisplayed. */
17134
17135 static int
17136 redisplay_mode_lines (window, force)
17137 Lisp_Object window;
17138 int force;
17139 {
17140 int nwindows = 0;
17141
17142 while (!NILP (window))
17143 {
17144 struct window *w = XWINDOW (window);
17145
17146 if (WINDOWP (w->hchild))
17147 nwindows += redisplay_mode_lines (w->hchild, force);
17148 else if (WINDOWP (w->vchild))
17149 nwindows += redisplay_mode_lines (w->vchild, force);
17150 else if (force
17151 || FRAME_GARBAGED_P (XFRAME (w->frame))
17152 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
17153 {
17154 struct text_pos lpoint;
17155 struct buffer *old = current_buffer;
17156
17157 /* Set the window's buffer for the mode line display. */
17158 SET_TEXT_POS (lpoint, PT, PT_BYTE);
17159 set_buffer_internal_1 (XBUFFER (w->buffer));
17160
17161 /* Point refers normally to the selected window. For any
17162 other window, set up appropriate value. */
17163 if (!EQ (window, selected_window))
17164 {
17165 struct text_pos pt;
17166
17167 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
17168 if (CHARPOS (pt) < BEGV)
17169 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
17170 else if (CHARPOS (pt) > (ZV - 1))
17171 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
17172 else
17173 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
17174 }
17175
17176 /* Display mode lines. */
17177 clear_glyph_matrix (w->desired_matrix);
17178 if (display_mode_lines (w))
17179 {
17180 ++nwindows;
17181 w->must_be_updated_p = 1;
17182 }
17183
17184 /* Restore old settings. */
17185 set_buffer_internal_1 (old);
17186 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
17187 }
17188
17189 window = w->next;
17190 }
17191
17192 return nwindows;
17193 }
17194
17195
17196 /* Display the mode and/or header line of window W. Value is the
17197 sum number of mode lines and header lines displayed. */
17198
17199 static int
17200 display_mode_lines (w)
17201 struct window *w;
17202 {
17203 Lisp_Object old_selected_window, old_selected_frame;
17204 int n = 0;
17205
17206 old_selected_frame = selected_frame;
17207 selected_frame = w->frame;
17208 old_selected_window = selected_window;
17209 XSETWINDOW (selected_window, w);
17210
17211 /* These will be set while the mode line specs are processed. */
17212 line_number_displayed = 0;
17213 w->column_number_displayed = Qnil;
17214
17215 if (WINDOW_WANTS_MODELINE_P (w))
17216 {
17217 struct window *sel_w = XWINDOW (old_selected_window);
17218
17219 /* Select mode line face based on the real selected window. */
17220 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
17221 current_buffer->mode_line_format);
17222 ++n;
17223 }
17224
17225 if (WINDOW_WANTS_HEADER_LINE_P (w))
17226 {
17227 display_mode_line (w, HEADER_LINE_FACE_ID,
17228 current_buffer->header_line_format);
17229 ++n;
17230 }
17231
17232 selected_frame = old_selected_frame;
17233 selected_window = old_selected_window;
17234 return n;
17235 }
17236
17237
17238 /* Display mode or header line of window W. FACE_ID specifies which
17239 line to display; it is either MODE_LINE_FACE_ID or
17240 HEADER_LINE_FACE_ID. FORMAT is the mode/header line format to
17241 display. Value is the pixel height of the mode/header line
17242 displayed. */
17243
17244 static int
17245 display_mode_line (w, face_id, format)
17246 struct window *w;
17247 enum face_id face_id;
17248 Lisp_Object format;
17249 {
17250 struct it it;
17251 struct face *face;
17252 int count = SPECPDL_INDEX ();
17253
17254 init_iterator (&it, w, -1, -1, NULL, face_id);
17255 /* Don't extend on a previously drawn mode-line.
17256 This may happen if called from pos_visible_p. */
17257 it.glyph_row->enabled_p = 0;
17258 prepare_desired_row (it.glyph_row);
17259
17260 it.glyph_row->mode_line_p = 1;
17261
17262 if (! mode_line_inverse_video)
17263 /* Force the mode-line to be displayed in the default face. */
17264 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
17265
17266 record_unwind_protect (unwind_format_mode_line,
17267 format_mode_line_unwind_data (NULL, Qnil, 0));
17268
17269 mode_line_target = MODE_LINE_DISPLAY;
17270
17271 /* Temporarily make frame's keyboard the current kboard so that
17272 kboard-local variables in the mode_line_format will get the right
17273 values. */
17274 push_kboard (FRAME_KBOARD (it.f));
17275 record_unwind_save_match_data ();
17276 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
17277 pop_kboard ();
17278
17279 unbind_to (count, Qnil);
17280
17281 /* Fill up with spaces. */
17282 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
17283
17284 compute_line_metrics (&it);
17285 it.glyph_row->full_width_p = 1;
17286 it.glyph_row->continued_p = 0;
17287 it.glyph_row->truncated_on_left_p = 0;
17288 it.glyph_row->truncated_on_right_p = 0;
17289
17290 /* Make a 3D mode-line have a shadow at its right end. */
17291 face = FACE_FROM_ID (it.f, face_id);
17292 extend_face_to_end_of_line (&it);
17293 if (face->box != FACE_NO_BOX)
17294 {
17295 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
17296 + it.glyph_row->used[TEXT_AREA] - 1);
17297 last->right_box_line_p = 1;
17298 }
17299
17300 return it.glyph_row->height;
17301 }
17302
17303 /* Move element ELT in LIST to the front of LIST.
17304 Return the updated list. */
17305
17306 static Lisp_Object
17307 move_elt_to_front (elt, list)
17308 Lisp_Object elt, list;
17309 {
17310 register Lisp_Object tail, prev;
17311 register Lisp_Object tem;
17312
17313 tail = list;
17314 prev = Qnil;
17315 while (CONSP (tail))
17316 {
17317 tem = XCAR (tail);
17318
17319 if (EQ (elt, tem))
17320 {
17321 /* Splice out the link TAIL. */
17322 if (NILP (prev))
17323 list = XCDR (tail);
17324 else
17325 Fsetcdr (prev, XCDR (tail));
17326
17327 /* Now make it the first. */
17328 Fsetcdr (tail, list);
17329 return tail;
17330 }
17331 else
17332 prev = tail;
17333 tail = XCDR (tail);
17334 QUIT;
17335 }
17336
17337 /* Not found--return unchanged LIST. */
17338 return list;
17339 }
17340
17341 /* Contribute ELT to the mode line for window IT->w. How it
17342 translates into text depends on its data type.
17343
17344 IT describes the display environment in which we display, as usual.
17345
17346 DEPTH is the depth in recursion. It is used to prevent
17347 infinite recursion here.
17348
17349 FIELD_WIDTH is the number of characters the display of ELT should
17350 occupy in the mode line, and PRECISION is the maximum number of
17351 characters to display from ELT's representation. See
17352 display_string for details.
17353
17354 Returns the hpos of the end of the text generated by ELT.
17355
17356 PROPS is a property list to add to any string we encounter.
17357
17358 If RISKY is nonzero, remove (disregard) any properties in any string
17359 we encounter, and ignore :eval and :propertize.
17360
17361 The global variable `mode_line_target' determines whether the
17362 output is passed to `store_mode_line_noprop',
17363 `store_mode_line_string', or `display_string'. */
17364
17365 static int
17366 display_mode_element (it, depth, field_width, precision, elt, props, risky)
17367 struct it *it;
17368 int depth;
17369 int field_width, precision;
17370 Lisp_Object elt, props;
17371 int risky;
17372 {
17373 int n = 0, field, prec;
17374 int literal = 0;
17375
17376 tail_recurse:
17377 if (depth > 100)
17378 elt = build_string ("*too-deep*");
17379
17380 depth++;
17381
17382 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
17383 {
17384 case Lisp_String:
17385 {
17386 /* A string: output it and check for %-constructs within it. */
17387 unsigned char c;
17388 int offset = 0;
17389
17390 if (SCHARS (elt) > 0
17391 && (!NILP (props) || risky))
17392 {
17393 Lisp_Object oprops, aelt;
17394 oprops = Ftext_properties_at (make_number (0), elt);
17395
17396 /* If the starting string's properties are not what
17397 we want, translate the string. Also, if the string
17398 is risky, do that anyway. */
17399
17400 if (NILP (Fequal (props, oprops)) || risky)
17401 {
17402 /* If the starting string has properties,
17403 merge the specified ones onto the existing ones. */
17404 if (! NILP (oprops) && !risky)
17405 {
17406 Lisp_Object tem;
17407
17408 oprops = Fcopy_sequence (oprops);
17409 tem = props;
17410 while (CONSP (tem))
17411 {
17412 oprops = Fplist_put (oprops, XCAR (tem),
17413 XCAR (XCDR (tem)));
17414 tem = XCDR (XCDR (tem));
17415 }
17416 props = oprops;
17417 }
17418
17419 aelt = Fassoc (elt, mode_line_proptrans_alist);
17420 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
17421 {
17422 /* AELT is what we want. Move it to the front
17423 without consing. */
17424 elt = XCAR (aelt);
17425 mode_line_proptrans_alist
17426 = move_elt_to_front (aelt, mode_line_proptrans_alist);
17427 }
17428 else
17429 {
17430 Lisp_Object tem;
17431
17432 /* If AELT has the wrong props, it is useless.
17433 so get rid of it. */
17434 if (! NILP (aelt))
17435 mode_line_proptrans_alist
17436 = Fdelq (aelt, mode_line_proptrans_alist);
17437
17438 elt = Fcopy_sequence (elt);
17439 Fset_text_properties (make_number (0), Flength (elt),
17440 props, elt);
17441 /* Add this item to mode_line_proptrans_alist. */
17442 mode_line_proptrans_alist
17443 = Fcons (Fcons (elt, props),
17444 mode_line_proptrans_alist);
17445 /* Truncate mode_line_proptrans_alist
17446 to at most 50 elements. */
17447 tem = Fnthcdr (make_number (50),
17448 mode_line_proptrans_alist);
17449 if (! NILP (tem))
17450 XSETCDR (tem, Qnil);
17451 }
17452 }
17453 }
17454
17455 offset = 0;
17456
17457 if (literal)
17458 {
17459 prec = precision - n;
17460 switch (mode_line_target)
17461 {
17462 case MODE_LINE_NOPROP:
17463 case MODE_LINE_TITLE:
17464 n += store_mode_line_noprop (SDATA (elt), -1, prec);
17465 break;
17466 case MODE_LINE_STRING:
17467 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
17468 break;
17469 case MODE_LINE_DISPLAY:
17470 n += display_string (NULL, elt, Qnil, 0, 0, it,
17471 0, prec, 0, STRING_MULTIBYTE (elt));
17472 break;
17473 }
17474
17475 break;
17476 }
17477
17478 /* Handle the non-literal case. */
17479
17480 while ((precision <= 0 || n < precision)
17481 && SREF (elt, offset) != 0
17482 && (mode_line_target != MODE_LINE_DISPLAY
17483 || it->current_x < it->last_visible_x))
17484 {
17485 int last_offset = offset;
17486
17487 /* Advance to end of string or next format specifier. */
17488 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
17489 ;
17490
17491 if (offset - 1 != last_offset)
17492 {
17493 int nchars, nbytes;
17494
17495 /* Output to end of string or up to '%'. Field width
17496 is length of string. Don't output more than
17497 PRECISION allows us. */
17498 offset--;
17499
17500 prec = c_string_width (SDATA (elt) + last_offset,
17501 offset - last_offset, precision - n,
17502 &nchars, &nbytes);
17503
17504 switch (mode_line_target)
17505 {
17506 case MODE_LINE_NOPROP:
17507 case MODE_LINE_TITLE:
17508 n += store_mode_line_noprop (SDATA (elt) + last_offset, 0, prec);
17509 break;
17510 case MODE_LINE_STRING:
17511 {
17512 int bytepos = last_offset;
17513 int charpos = string_byte_to_char (elt, bytepos);
17514 int endpos = (precision <= 0
17515 ? string_byte_to_char (elt, offset)
17516 : charpos + nchars);
17517
17518 n += store_mode_line_string (NULL,
17519 Fsubstring (elt, make_number (charpos),
17520 make_number (endpos)),
17521 0, 0, 0, Qnil);
17522 }
17523 break;
17524 case MODE_LINE_DISPLAY:
17525 {
17526 int bytepos = last_offset;
17527 int charpos = string_byte_to_char (elt, bytepos);
17528
17529 if (precision <= 0)
17530 nchars = string_byte_to_char (elt, offset) - charpos;
17531 n += display_string (NULL, elt, Qnil, 0, charpos,
17532 it, 0, nchars, 0,
17533 STRING_MULTIBYTE (elt));
17534 }
17535 break;
17536 }
17537 }
17538 else /* c == '%' */
17539 {
17540 int percent_position = offset;
17541
17542 /* Get the specified minimum width. Zero means
17543 don't pad. */
17544 field = 0;
17545 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
17546 field = field * 10 + c - '0';
17547
17548 /* Don't pad beyond the total padding allowed. */
17549 if (field_width - n > 0 && field > field_width - n)
17550 field = field_width - n;
17551
17552 /* Note that either PRECISION <= 0 or N < PRECISION. */
17553 prec = precision - n;
17554
17555 if (c == 'M')
17556 n += display_mode_element (it, depth, field, prec,
17557 Vglobal_mode_string, props,
17558 risky);
17559 else if (c != 0)
17560 {
17561 int multibyte;
17562 int bytepos, charpos;
17563 unsigned char *spec;
17564 Lisp_Object string;
17565
17566 bytepos = percent_position;
17567 charpos = (STRING_MULTIBYTE (elt)
17568 ? string_byte_to_char (elt, bytepos)
17569 : bytepos);
17570 spec = decode_mode_spec (it->w, c, field, prec, &string);
17571 multibyte = STRINGP (string) && STRING_MULTIBYTE (string);
17572
17573 switch (mode_line_target)
17574 {
17575 case MODE_LINE_NOPROP:
17576 case MODE_LINE_TITLE:
17577 n += store_mode_line_noprop (spec, field, prec);
17578 break;
17579 case MODE_LINE_STRING:
17580 {
17581 int len = strlen (spec);
17582 Lisp_Object tem = make_string (spec, len);
17583 props = Ftext_properties_at (make_number (charpos), elt);
17584 /* Should only keep face property in props */
17585 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
17586 }
17587 break;
17588 case MODE_LINE_DISPLAY:
17589 {
17590 int nglyphs_before, nwritten;
17591
17592 nglyphs_before = it->glyph_row->used[TEXT_AREA];
17593 nwritten = display_string (spec, string, elt,
17594 charpos, 0, it,
17595 field, prec, 0,
17596 multibyte);
17597
17598 /* Assign to the glyphs written above the
17599 string where the `%x' came from, position
17600 of the `%'. */
17601 if (nwritten > 0)
17602 {
17603 struct glyph *glyph
17604 = (it->glyph_row->glyphs[TEXT_AREA]
17605 + nglyphs_before);
17606 int i;
17607
17608 for (i = 0; i < nwritten; ++i)
17609 {
17610 glyph[i].object = elt;
17611 glyph[i].charpos = charpos;
17612 }
17613
17614 n += nwritten;
17615 }
17616 }
17617 break;
17618 }
17619 }
17620 else /* c == 0 */
17621 break;
17622 }
17623 }
17624 }
17625 break;
17626
17627 case Lisp_Symbol:
17628 /* A symbol: process the value of the symbol recursively
17629 as if it appeared here directly. Avoid error if symbol void.
17630 Special case: if value of symbol is a string, output the string
17631 literally. */
17632 {
17633 register Lisp_Object tem;
17634
17635 /* If the variable is not marked as risky to set
17636 then its contents are risky to use. */
17637 if (NILP (Fget (elt, Qrisky_local_variable)))
17638 risky = 1;
17639
17640 tem = Fboundp (elt);
17641 if (!NILP (tem))
17642 {
17643 tem = Fsymbol_value (elt);
17644 /* If value is a string, output that string literally:
17645 don't check for % within it. */
17646 if (STRINGP (tem))
17647 literal = 1;
17648
17649 if (!EQ (tem, elt))
17650 {
17651 /* Give up right away for nil or t. */
17652 elt = tem;
17653 goto tail_recurse;
17654 }
17655 }
17656 }
17657 break;
17658
17659 case Lisp_Cons:
17660 {
17661 register Lisp_Object car, tem;
17662
17663 /* A cons cell: five distinct cases.
17664 If first element is :eval or :propertize, do something special.
17665 If first element is a string or a cons, process all the elements
17666 and effectively concatenate them.
17667 If first element is a negative number, truncate displaying cdr to
17668 at most that many characters. If positive, pad (with spaces)
17669 to at least that many characters.
17670 If first element is a symbol, process the cadr or caddr recursively
17671 according to whether the symbol's value is non-nil or nil. */
17672 car = XCAR (elt);
17673 if (EQ (car, QCeval))
17674 {
17675 /* An element of the form (:eval FORM) means evaluate FORM
17676 and use the result as mode line elements. */
17677
17678 if (risky)
17679 break;
17680
17681 if (CONSP (XCDR (elt)))
17682 {
17683 Lisp_Object spec;
17684 spec = safe_eval (XCAR (XCDR (elt)));
17685 n += display_mode_element (it, depth, field_width - n,
17686 precision - n, spec, props,
17687 risky);
17688 }
17689 }
17690 else if (EQ (car, QCpropertize))
17691 {
17692 /* An element of the form (:propertize ELT PROPS...)
17693 means display ELT but applying properties PROPS. */
17694
17695 if (risky)
17696 break;
17697
17698 if (CONSP (XCDR (elt)))
17699 n += display_mode_element (it, depth, field_width - n,
17700 precision - n, XCAR (XCDR (elt)),
17701 XCDR (XCDR (elt)), risky);
17702 }
17703 else if (SYMBOLP (car))
17704 {
17705 tem = Fboundp (car);
17706 elt = XCDR (elt);
17707 if (!CONSP (elt))
17708 goto invalid;
17709 /* elt is now the cdr, and we know it is a cons cell.
17710 Use its car if CAR has a non-nil value. */
17711 if (!NILP (tem))
17712 {
17713 tem = Fsymbol_value (car);
17714 if (!NILP (tem))
17715 {
17716 elt = XCAR (elt);
17717 goto tail_recurse;
17718 }
17719 }
17720 /* Symbol's value is nil (or symbol is unbound)
17721 Get the cddr of the original list
17722 and if possible find the caddr and use that. */
17723 elt = XCDR (elt);
17724 if (NILP (elt))
17725 break;
17726 else if (!CONSP (elt))
17727 goto invalid;
17728 elt = XCAR (elt);
17729 goto tail_recurse;
17730 }
17731 else if (INTEGERP (car))
17732 {
17733 register int lim = XINT (car);
17734 elt = XCDR (elt);
17735 if (lim < 0)
17736 {
17737 /* Negative int means reduce maximum width. */
17738 if (precision <= 0)
17739 precision = -lim;
17740 else
17741 precision = min (precision, -lim);
17742 }
17743 else if (lim > 0)
17744 {
17745 /* Padding specified. Don't let it be more than
17746 current maximum. */
17747 if (precision > 0)
17748 lim = min (precision, lim);
17749
17750 /* If that's more padding than already wanted, queue it.
17751 But don't reduce padding already specified even if
17752 that is beyond the current truncation point. */
17753 field_width = max (lim, field_width);
17754 }
17755 goto tail_recurse;
17756 }
17757 else if (STRINGP (car) || CONSP (car))
17758 {
17759 Lisp_Object halftail = elt;
17760 int len = 0;
17761
17762 while (CONSP (elt)
17763 && (precision <= 0 || n < precision))
17764 {
17765 n += display_mode_element (it, depth,
17766 /* Do padding only after the last
17767 element in the list. */
17768 (! CONSP (XCDR (elt))
17769 ? field_width - n
17770 : 0),
17771 precision - n, XCAR (elt),
17772 props, risky);
17773 elt = XCDR (elt);
17774 len++;
17775 if ((len & 1) == 0)
17776 halftail = XCDR (halftail);
17777 /* Check for cycle. */
17778 if (EQ (halftail, elt))
17779 break;
17780 }
17781 }
17782 }
17783 break;
17784
17785 default:
17786 invalid:
17787 elt = build_string ("*invalid*");
17788 goto tail_recurse;
17789 }
17790
17791 /* Pad to FIELD_WIDTH. */
17792 if (field_width > 0 && n < field_width)
17793 {
17794 switch (mode_line_target)
17795 {
17796 case MODE_LINE_NOPROP:
17797 case MODE_LINE_TITLE:
17798 n += store_mode_line_noprop ("", field_width - n, 0);
17799 break;
17800 case MODE_LINE_STRING:
17801 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
17802 break;
17803 case MODE_LINE_DISPLAY:
17804 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
17805 0, 0, 0);
17806 break;
17807 }
17808 }
17809
17810 return n;
17811 }
17812
17813 /* Store a mode-line string element in mode_line_string_list.
17814
17815 If STRING is non-null, display that C string. Otherwise, the Lisp
17816 string LISP_STRING is displayed.
17817
17818 FIELD_WIDTH is the minimum number of output glyphs to produce.
17819 If STRING has fewer characters than FIELD_WIDTH, pad to the right
17820 with spaces. FIELD_WIDTH <= 0 means don't pad.
17821
17822 PRECISION is the maximum number of characters to output from
17823 STRING. PRECISION <= 0 means don't truncate the string.
17824
17825 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
17826 properties to the string.
17827
17828 PROPS are the properties to add to the string.
17829 The mode_line_string_face face property is always added to the string.
17830 */
17831
17832 static int
17833 store_mode_line_string (string, lisp_string, copy_string, field_width, precision, props)
17834 char *string;
17835 Lisp_Object lisp_string;
17836 int copy_string;
17837 int field_width;
17838 int precision;
17839 Lisp_Object props;
17840 {
17841 int len;
17842 int n = 0;
17843
17844 if (string != NULL)
17845 {
17846 len = strlen (string);
17847 if (precision > 0 && len > precision)
17848 len = precision;
17849 lisp_string = make_string (string, len);
17850 if (NILP (props))
17851 props = mode_line_string_face_prop;
17852 else if (!NILP (mode_line_string_face))
17853 {
17854 Lisp_Object face = Fplist_get (props, Qface);
17855 props = Fcopy_sequence (props);
17856 if (NILP (face))
17857 face = mode_line_string_face;
17858 else
17859 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
17860 props = Fplist_put (props, Qface, face);
17861 }
17862 Fadd_text_properties (make_number (0), make_number (len),
17863 props, lisp_string);
17864 }
17865 else
17866 {
17867 len = XFASTINT (Flength (lisp_string));
17868 if (precision > 0 && len > precision)
17869 {
17870 len = precision;
17871 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
17872 precision = -1;
17873 }
17874 if (!NILP (mode_line_string_face))
17875 {
17876 Lisp_Object face;
17877 if (NILP (props))
17878 props = Ftext_properties_at (make_number (0), lisp_string);
17879 face = Fplist_get (props, Qface);
17880 if (NILP (face))
17881 face = mode_line_string_face;
17882 else
17883 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
17884 props = Fcons (Qface, Fcons (face, Qnil));
17885 if (copy_string)
17886 lisp_string = Fcopy_sequence (lisp_string);
17887 }
17888 if (!NILP (props))
17889 Fadd_text_properties (make_number (0), make_number (len),
17890 props, lisp_string);
17891 }
17892
17893 if (len > 0)
17894 {
17895 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
17896 n += len;
17897 }
17898
17899 if (field_width > len)
17900 {
17901 field_width -= len;
17902 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
17903 if (!NILP (props))
17904 Fadd_text_properties (make_number (0), make_number (field_width),
17905 props, lisp_string);
17906 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
17907 n += field_width;
17908 }
17909
17910 return n;
17911 }
17912
17913
17914 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
17915 1, 4, 0,
17916 doc: /* Format a string out of a mode line format specification.
17917 First arg FORMAT specifies the mode line format (see `mode-line-format'
17918 for details) to use.
17919
17920 Optional second arg FACE specifies the face property to put
17921 on all characters for which no face is specified.
17922 The value t means whatever face the window's mode line currently uses
17923 \(either `mode-line' or `mode-line-inactive', depending).
17924 A value of nil means the default is no face property.
17925 If FACE is an integer, the value string has no text properties.
17926
17927 Optional third and fourth args WINDOW and BUFFER specify the window
17928 and buffer to use as the context for the formatting (defaults
17929 are the selected window and the window's buffer). */)
17930 (format, face, window, buffer)
17931 Lisp_Object format, face, window, buffer;
17932 {
17933 struct it it;
17934 int len;
17935 struct window *w;
17936 struct buffer *old_buffer = NULL;
17937 int face_id = -1;
17938 int no_props = INTEGERP (face);
17939 int count = SPECPDL_INDEX ();
17940 Lisp_Object str;
17941 int string_start = 0;
17942
17943 if (NILP (window))
17944 window = selected_window;
17945 CHECK_WINDOW (window);
17946 w = XWINDOW (window);
17947
17948 if (NILP (buffer))
17949 buffer = w->buffer;
17950 CHECK_BUFFER (buffer);
17951
17952 /* Make formatting the modeline a non-op when noninteractive, otherwise
17953 there will be problems later caused by a partially initialized frame. */
17954 if (NILP (format) || noninteractive)
17955 return empty_unibyte_string;
17956
17957 if (no_props)
17958 face = Qnil;
17959
17960 if (!NILP (face))
17961 {
17962 if (EQ (face, Qt))
17963 face = (EQ (window, selected_window) ? Qmode_line : Qmode_line_inactive);
17964 face_id = lookup_named_face (XFRAME (WINDOW_FRAME (w)), face, 0);
17965 }
17966
17967 if (face_id < 0)
17968 face_id = DEFAULT_FACE_ID;
17969
17970 if (XBUFFER (buffer) != current_buffer)
17971 old_buffer = current_buffer;
17972
17973 /* Save things including mode_line_proptrans_alist,
17974 and set that to nil so that we don't alter the outer value. */
17975 record_unwind_protect (unwind_format_mode_line,
17976 format_mode_line_unwind_data
17977 (old_buffer, selected_window, 1));
17978 mode_line_proptrans_alist = Qnil;
17979
17980 Fselect_window (window, Qt);
17981 if (old_buffer)
17982 set_buffer_internal_1 (XBUFFER (buffer));
17983
17984 init_iterator (&it, w, -1, -1, NULL, face_id);
17985
17986 if (no_props)
17987 {
17988 mode_line_target = MODE_LINE_NOPROP;
17989 mode_line_string_face_prop = Qnil;
17990 mode_line_string_list = Qnil;
17991 string_start = MODE_LINE_NOPROP_LEN (0);
17992 }
17993 else
17994 {
17995 mode_line_target = MODE_LINE_STRING;
17996 mode_line_string_list = Qnil;
17997 mode_line_string_face = face;
17998 mode_line_string_face_prop
17999 = (NILP (face) ? Qnil : Fcons (Qface, Fcons (face, Qnil)));
18000 }
18001
18002 push_kboard (FRAME_KBOARD (it.f));
18003 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
18004 pop_kboard ();
18005
18006 if (no_props)
18007 {
18008 len = MODE_LINE_NOPROP_LEN (string_start);
18009 str = make_string (mode_line_noprop_buf + string_start, len);
18010 }
18011 else
18012 {
18013 mode_line_string_list = Fnreverse (mode_line_string_list);
18014 str = Fmapconcat (intern ("identity"), mode_line_string_list,
18015 empty_unibyte_string);
18016 }
18017
18018 unbind_to (count, Qnil);
18019 return str;
18020 }
18021
18022 /* Write a null-terminated, right justified decimal representation of
18023 the positive integer D to BUF using a minimal field width WIDTH. */
18024
18025 static void
18026 pint2str (buf, width, d)
18027 register char *buf;
18028 register int width;
18029 register int d;
18030 {
18031 register char *p = buf;
18032
18033 if (d <= 0)
18034 *p++ = '0';
18035 else
18036 {
18037 while (d > 0)
18038 {
18039 *p++ = d % 10 + '0';
18040 d /= 10;
18041 }
18042 }
18043
18044 for (width -= (int) (p - buf); width > 0; --width)
18045 *p++ = ' ';
18046 *p-- = '\0';
18047 while (p > buf)
18048 {
18049 d = *buf;
18050 *buf++ = *p;
18051 *p-- = d;
18052 }
18053 }
18054
18055 /* Write a null-terminated, right justified decimal and "human
18056 readable" representation of the nonnegative integer D to BUF using
18057 a minimal field width WIDTH. D should be smaller than 999.5e24. */
18058
18059 static const char power_letter[] =
18060 {
18061 0, /* not used */
18062 'k', /* kilo */
18063 'M', /* mega */
18064 'G', /* giga */
18065 'T', /* tera */
18066 'P', /* peta */
18067 'E', /* exa */
18068 'Z', /* zetta */
18069 'Y' /* yotta */
18070 };
18071
18072 static void
18073 pint2hrstr (buf, width, d)
18074 char *buf;
18075 int width;
18076 int d;
18077 {
18078 /* We aim to represent the nonnegative integer D as
18079 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
18080 int quotient = d;
18081 int remainder = 0;
18082 /* -1 means: do not use TENTHS. */
18083 int tenths = -1;
18084 int exponent = 0;
18085
18086 /* Length of QUOTIENT.TENTHS as a string. */
18087 int length;
18088
18089 char * psuffix;
18090 char * p;
18091
18092 if (1000 <= quotient)
18093 {
18094 /* Scale to the appropriate EXPONENT. */
18095 do
18096 {
18097 remainder = quotient % 1000;
18098 quotient /= 1000;
18099 exponent++;
18100 }
18101 while (1000 <= quotient);
18102
18103 /* Round to nearest and decide whether to use TENTHS or not. */
18104 if (quotient <= 9)
18105 {
18106 tenths = remainder / 100;
18107 if (50 <= remainder % 100)
18108 {
18109 if (tenths < 9)
18110 tenths++;
18111 else
18112 {
18113 quotient++;
18114 if (quotient == 10)
18115 tenths = -1;
18116 else
18117 tenths = 0;
18118 }
18119 }
18120 }
18121 else
18122 if (500 <= remainder)
18123 {
18124 if (quotient < 999)
18125 quotient++;
18126 else
18127 {
18128 quotient = 1;
18129 exponent++;
18130 tenths = 0;
18131 }
18132 }
18133 }
18134
18135 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
18136 if (tenths == -1 && quotient <= 99)
18137 if (quotient <= 9)
18138 length = 1;
18139 else
18140 length = 2;
18141 else
18142 length = 3;
18143 p = psuffix = buf + max (width, length);
18144
18145 /* Print EXPONENT. */
18146 if (exponent)
18147 *psuffix++ = power_letter[exponent];
18148 *psuffix = '\0';
18149
18150 /* Print TENTHS. */
18151 if (tenths >= 0)
18152 {
18153 *--p = '0' + tenths;
18154 *--p = '.';
18155 }
18156
18157 /* Print QUOTIENT. */
18158 do
18159 {
18160 int digit = quotient % 10;
18161 *--p = '0' + digit;
18162 }
18163 while ((quotient /= 10) != 0);
18164
18165 /* Print leading spaces. */
18166 while (buf < p)
18167 *--p = ' ';
18168 }
18169
18170 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
18171 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
18172 type of CODING_SYSTEM. Return updated pointer into BUF. */
18173
18174 static unsigned char invalid_eol_type[] = "(*invalid*)";
18175
18176 static char *
18177 decode_mode_spec_coding (coding_system, buf, eol_flag)
18178 Lisp_Object coding_system;
18179 register char *buf;
18180 int eol_flag;
18181 {
18182 Lisp_Object val;
18183 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
18184 const unsigned char *eol_str;
18185 int eol_str_len;
18186 /* The EOL conversion we are using. */
18187 Lisp_Object eoltype;
18188
18189 val = CODING_SYSTEM_SPEC (coding_system);
18190 eoltype = Qnil;
18191
18192 if (!VECTORP (val)) /* Not yet decided. */
18193 {
18194 if (multibyte)
18195 *buf++ = '-';
18196 if (eol_flag)
18197 eoltype = eol_mnemonic_undecided;
18198 /* Don't mention EOL conversion if it isn't decided. */
18199 }
18200 else
18201 {
18202 Lisp_Object attrs;
18203 Lisp_Object eolvalue;
18204
18205 attrs = AREF (val, 0);
18206 eolvalue = AREF (val, 2);
18207
18208 if (multibyte)
18209 *buf++ = XFASTINT (CODING_ATTR_MNEMONIC (attrs));
18210
18211 if (eol_flag)
18212 {
18213 /* The EOL conversion that is normal on this system. */
18214
18215 if (NILP (eolvalue)) /* Not yet decided. */
18216 eoltype = eol_mnemonic_undecided;
18217 else if (VECTORP (eolvalue)) /* Not yet decided. */
18218 eoltype = eol_mnemonic_undecided;
18219 else /* eolvalue is Qunix, Qdos, or Qmac. */
18220 eoltype = (EQ (eolvalue, Qunix)
18221 ? eol_mnemonic_unix
18222 : (EQ (eolvalue, Qdos) == 1
18223 ? eol_mnemonic_dos : eol_mnemonic_mac));
18224 }
18225 }
18226
18227 if (eol_flag)
18228 {
18229 /* Mention the EOL conversion if it is not the usual one. */
18230 if (STRINGP (eoltype))
18231 {
18232 eol_str = SDATA (eoltype);
18233 eol_str_len = SBYTES (eoltype);
18234 }
18235 else if (CHARACTERP (eoltype))
18236 {
18237 unsigned char *tmp = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
18238 eol_str_len = CHAR_STRING (XINT (eoltype), tmp);
18239 eol_str = tmp;
18240 }
18241 else
18242 {
18243 eol_str = invalid_eol_type;
18244 eol_str_len = sizeof (invalid_eol_type) - 1;
18245 }
18246 bcopy (eol_str, buf, eol_str_len);
18247 buf += eol_str_len;
18248 }
18249
18250 return buf;
18251 }
18252
18253 /* Return a string for the output of a mode line %-spec for window W,
18254 generated by character C. PRECISION >= 0 means don't return a
18255 string longer than that value. FIELD_WIDTH > 0 means pad the
18256 string returned with spaces to that value. Return a Lisp string in
18257 *STRING if the resulting string is taken from that Lisp string.
18258
18259 Note we operate on the current buffer for most purposes,
18260 the exception being w->base_line_pos. */
18261
18262 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
18263
18264 static char *
18265 decode_mode_spec (w, c, field_width, precision, string)
18266 struct window *w;
18267 register int c;
18268 int field_width, precision;
18269 Lisp_Object *string;
18270 {
18271 Lisp_Object obj;
18272 struct frame *f = XFRAME (WINDOW_FRAME (w));
18273 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
18274 struct buffer *b = current_buffer;
18275
18276 obj = Qnil;
18277 *string = Qnil;
18278
18279 switch (c)
18280 {
18281 case '*':
18282 if (!NILP (b->read_only))
18283 return "%";
18284 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
18285 return "*";
18286 return "-";
18287
18288 case '+':
18289 /* This differs from %* only for a modified read-only buffer. */
18290 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
18291 return "*";
18292 if (!NILP (b->read_only))
18293 return "%";
18294 return "-";
18295
18296 case '&':
18297 /* This differs from %* in ignoring read-only-ness. */
18298 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
18299 return "*";
18300 return "-";
18301
18302 case '%':
18303 return "%";
18304
18305 case '[':
18306 {
18307 int i;
18308 char *p;
18309
18310 if (command_loop_level > 5)
18311 return "[[[... ";
18312 p = decode_mode_spec_buf;
18313 for (i = 0; i < command_loop_level; i++)
18314 *p++ = '[';
18315 *p = 0;
18316 return decode_mode_spec_buf;
18317 }
18318
18319 case ']':
18320 {
18321 int i;
18322 char *p;
18323
18324 if (command_loop_level > 5)
18325 return " ...]]]";
18326 p = decode_mode_spec_buf;
18327 for (i = 0; i < command_loop_level; i++)
18328 *p++ = ']';
18329 *p = 0;
18330 return decode_mode_spec_buf;
18331 }
18332
18333 case '-':
18334 {
18335 register int i;
18336
18337 /* Let lots_of_dashes be a string of infinite length. */
18338 if (mode_line_target == MODE_LINE_NOPROP ||
18339 mode_line_target == MODE_LINE_STRING)
18340 return "--";
18341 if (field_width <= 0
18342 || field_width > sizeof (lots_of_dashes))
18343 {
18344 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
18345 decode_mode_spec_buf[i] = '-';
18346 decode_mode_spec_buf[i] = '\0';
18347 return decode_mode_spec_buf;
18348 }
18349 else
18350 return lots_of_dashes;
18351 }
18352
18353 case 'b':
18354 obj = b->name;
18355 break;
18356
18357 case 'c':
18358 /* %c and %l are ignored in `frame-title-format'.
18359 (In redisplay_internal, the frame title is drawn _before_ the
18360 windows are updated, so the stuff which depends on actual
18361 window contents (such as %l) may fail to render properly, or
18362 even crash emacs.) */
18363 if (mode_line_target == MODE_LINE_TITLE)
18364 return "";
18365 else
18366 {
18367 int col = (int) current_column (); /* iftc */
18368 w->column_number_displayed = make_number (col);
18369 pint2str (decode_mode_spec_buf, field_width, col);
18370 return decode_mode_spec_buf;
18371 }
18372
18373 case 'e':
18374 #ifndef SYSTEM_MALLOC
18375 {
18376 if (NILP (Vmemory_full))
18377 return "";
18378 else
18379 return "!MEM FULL! ";
18380 }
18381 #else
18382 return "";
18383 #endif
18384
18385 case 'F':
18386 /* %F displays the frame name. */
18387 if (!NILP (f->title))
18388 return (char *) SDATA (f->title);
18389 if (f->explicit_name || ! FRAME_WINDOW_P (f))
18390 return (char *) SDATA (f->name);
18391 return "Emacs";
18392
18393 case 'f':
18394 obj = b->filename;
18395 break;
18396
18397 case 'i':
18398 {
18399 int size = ZV - BEGV;
18400 pint2str (decode_mode_spec_buf, field_width, size);
18401 return decode_mode_spec_buf;
18402 }
18403
18404 case 'I':
18405 {
18406 int size = ZV - BEGV;
18407 pint2hrstr (decode_mode_spec_buf, field_width, size);
18408 return decode_mode_spec_buf;
18409 }
18410
18411 case 'l':
18412 {
18413 int startpos, startpos_byte, line, linepos, linepos_byte;
18414 int topline, nlines, junk, height;
18415
18416 /* %c and %l are ignored in `frame-title-format'. */
18417 if (mode_line_target == MODE_LINE_TITLE)
18418 return "";
18419
18420 startpos = XMARKER (w->start)->charpos;
18421 startpos_byte = marker_byte_position (w->start);
18422 height = WINDOW_TOTAL_LINES (w);
18423
18424 /* If we decided that this buffer isn't suitable for line numbers,
18425 don't forget that too fast. */
18426 if (EQ (w->base_line_pos, w->buffer))
18427 goto no_value;
18428 /* But do forget it, if the window shows a different buffer now. */
18429 else if (BUFFERP (w->base_line_pos))
18430 w->base_line_pos = Qnil;
18431
18432 /* If the buffer is very big, don't waste time. */
18433 if (INTEGERP (Vline_number_display_limit)
18434 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
18435 {
18436 w->base_line_pos = Qnil;
18437 w->base_line_number = Qnil;
18438 goto no_value;
18439 }
18440
18441 if (INTEGERP (w->base_line_number)
18442 && INTEGERP (w->base_line_pos)
18443 && XFASTINT (w->base_line_pos) <= startpos)
18444 {
18445 line = XFASTINT (w->base_line_number);
18446 linepos = XFASTINT (w->base_line_pos);
18447 linepos_byte = buf_charpos_to_bytepos (b, linepos);
18448 }
18449 else
18450 {
18451 line = 1;
18452 linepos = BUF_BEGV (b);
18453 linepos_byte = BUF_BEGV_BYTE (b);
18454 }
18455
18456 /* Count lines from base line to window start position. */
18457 nlines = display_count_lines (linepos, linepos_byte,
18458 startpos_byte,
18459 startpos, &junk);
18460
18461 topline = nlines + line;
18462
18463 /* Determine a new base line, if the old one is too close
18464 or too far away, or if we did not have one.
18465 "Too close" means it's plausible a scroll-down would
18466 go back past it. */
18467 if (startpos == BUF_BEGV (b))
18468 {
18469 w->base_line_number = make_number (topline);
18470 w->base_line_pos = make_number (BUF_BEGV (b));
18471 }
18472 else if (nlines < height + 25 || nlines > height * 3 + 50
18473 || linepos == BUF_BEGV (b))
18474 {
18475 int limit = BUF_BEGV (b);
18476 int limit_byte = BUF_BEGV_BYTE (b);
18477 int position;
18478 int distance = (height * 2 + 30) * line_number_display_limit_width;
18479
18480 if (startpos - distance > limit)
18481 {
18482 limit = startpos - distance;
18483 limit_byte = CHAR_TO_BYTE (limit);
18484 }
18485
18486 nlines = display_count_lines (startpos, startpos_byte,
18487 limit_byte,
18488 - (height * 2 + 30),
18489 &position);
18490 /* If we couldn't find the lines we wanted within
18491 line_number_display_limit_width chars per line,
18492 give up on line numbers for this window. */
18493 if (position == limit_byte && limit == startpos - distance)
18494 {
18495 w->base_line_pos = w->buffer;
18496 w->base_line_number = Qnil;
18497 goto no_value;
18498 }
18499
18500 w->base_line_number = make_number (topline - nlines);
18501 w->base_line_pos = make_number (BYTE_TO_CHAR (position));
18502 }
18503
18504 /* Now count lines from the start pos to point. */
18505 nlines = display_count_lines (startpos, startpos_byte,
18506 PT_BYTE, PT, &junk);
18507
18508 /* Record that we did display the line number. */
18509 line_number_displayed = 1;
18510
18511 /* Make the string to show. */
18512 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
18513 return decode_mode_spec_buf;
18514 no_value:
18515 {
18516 char* p = decode_mode_spec_buf;
18517 int pad = field_width - 2;
18518 while (pad-- > 0)
18519 *p++ = ' ';
18520 *p++ = '?';
18521 *p++ = '?';
18522 *p = '\0';
18523 return decode_mode_spec_buf;
18524 }
18525 }
18526 break;
18527
18528 case 'm':
18529 obj = b->mode_name;
18530 break;
18531
18532 case 'n':
18533 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
18534 return " Narrow";
18535 break;
18536
18537 case 'p':
18538 {
18539 int pos = marker_position (w->start);
18540 int total = BUF_ZV (b) - BUF_BEGV (b);
18541
18542 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
18543 {
18544 if (pos <= BUF_BEGV (b))
18545 return "All";
18546 else
18547 return "Bottom";
18548 }
18549 else if (pos <= BUF_BEGV (b))
18550 return "Top";
18551 else
18552 {
18553 if (total > 1000000)
18554 /* Do it differently for a large value, to avoid overflow. */
18555 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
18556 else
18557 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
18558 /* We can't normally display a 3-digit number,
18559 so get us a 2-digit number that is close. */
18560 if (total == 100)
18561 total = 99;
18562 sprintf (decode_mode_spec_buf, "%2d%%", total);
18563 return decode_mode_spec_buf;
18564 }
18565 }
18566
18567 /* Display percentage of size above the bottom of the screen. */
18568 case 'P':
18569 {
18570 int toppos = marker_position (w->start);
18571 int botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
18572 int total = BUF_ZV (b) - BUF_BEGV (b);
18573
18574 if (botpos >= BUF_ZV (b))
18575 {
18576 if (toppos <= BUF_BEGV (b))
18577 return "All";
18578 else
18579 return "Bottom";
18580 }
18581 else
18582 {
18583 if (total > 1000000)
18584 /* Do it differently for a large value, to avoid overflow. */
18585 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
18586 else
18587 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
18588 /* We can't normally display a 3-digit number,
18589 so get us a 2-digit number that is close. */
18590 if (total == 100)
18591 total = 99;
18592 if (toppos <= BUF_BEGV (b))
18593 sprintf (decode_mode_spec_buf, "Top%2d%%", total);
18594 else
18595 sprintf (decode_mode_spec_buf, "%2d%%", total);
18596 return decode_mode_spec_buf;
18597 }
18598 }
18599
18600 case 's':
18601 /* status of process */
18602 obj = Fget_buffer_process (Fcurrent_buffer ());
18603 if (NILP (obj))
18604 return "no process";
18605 #ifdef subprocesses
18606 obj = Fsymbol_name (Fprocess_status (obj));
18607 #endif
18608 break;
18609
18610 case '@':
18611 {
18612 int count = inhibit_garbage_collection ();
18613 Lisp_Object val = call1 (intern ("file-remote-p"),
18614 current_buffer->directory);
18615 unbind_to (count, Qnil);
18616
18617 if (NILP (val))
18618 return "-";
18619 else
18620 return "@";
18621 }
18622
18623 case 't': /* indicate TEXT or BINARY */
18624 #ifdef MODE_LINE_BINARY_TEXT
18625 return MODE_LINE_BINARY_TEXT (b);
18626 #else
18627 return "T";
18628 #endif
18629
18630 case 'z':
18631 /* coding-system (not including end-of-line format) */
18632 case 'Z':
18633 /* coding-system (including end-of-line type) */
18634 {
18635 int eol_flag = (c == 'Z');
18636 char *p = decode_mode_spec_buf;
18637
18638 if (! FRAME_WINDOW_P (f))
18639 {
18640 /* No need to mention EOL here--the terminal never needs
18641 to do EOL conversion. */
18642 p = decode_mode_spec_coding (CODING_ID_NAME
18643 (FRAME_KEYBOARD_CODING (f)->id),
18644 p, 0);
18645 p = decode_mode_spec_coding (CODING_ID_NAME
18646 (FRAME_TERMINAL_CODING (f)->id),
18647 p, 0);
18648 }
18649 p = decode_mode_spec_coding (b->buffer_file_coding_system,
18650 p, eol_flag);
18651
18652 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
18653 #ifdef subprocesses
18654 obj = Fget_buffer_process (Fcurrent_buffer ());
18655 if (PROCESSP (obj))
18656 {
18657 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
18658 p, eol_flag);
18659 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
18660 p, eol_flag);
18661 }
18662 #endif /* subprocesses */
18663 #endif /* 0 */
18664 *p = 0;
18665 return decode_mode_spec_buf;
18666 }
18667 }
18668
18669 if (STRINGP (obj))
18670 {
18671 *string = obj;
18672 return (char *) SDATA (obj);
18673 }
18674 else
18675 return "";
18676 }
18677
18678
18679 /* Count up to COUNT lines starting from START / START_BYTE.
18680 But don't go beyond LIMIT_BYTE.
18681 Return the number of lines thus found (always nonnegative).
18682
18683 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
18684
18685 static int
18686 display_count_lines (start, start_byte, limit_byte, count, byte_pos_ptr)
18687 int start, start_byte, limit_byte, count;
18688 int *byte_pos_ptr;
18689 {
18690 register unsigned char *cursor;
18691 unsigned char *base;
18692
18693 register int ceiling;
18694 register unsigned char *ceiling_addr;
18695 int orig_count = count;
18696
18697 /* If we are not in selective display mode,
18698 check only for newlines. */
18699 int selective_display = (!NILP (current_buffer->selective_display)
18700 && !INTEGERP (current_buffer->selective_display));
18701
18702 if (count > 0)
18703 {
18704 while (start_byte < limit_byte)
18705 {
18706 ceiling = BUFFER_CEILING_OF (start_byte);
18707 ceiling = min (limit_byte - 1, ceiling);
18708 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
18709 base = (cursor = BYTE_POS_ADDR (start_byte));
18710 while (1)
18711 {
18712 if (selective_display)
18713 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
18714 ;
18715 else
18716 while (*cursor != '\n' && ++cursor != ceiling_addr)
18717 ;
18718
18719 if (cursor != ceiling_addr)
18720 {
18721 if (--count == 0)
18722 {
18723 start_byte += cursor - base + 1;
18724 *byte_pos_ptr = start_byte;
18725 return orig_count;
18726 }
18727 else
18728 if (++cursor == ceiling_addr)
18729 break;
18730 }
18731 else
18732 break;
18733 }
18734 start_byte += cursor - base;
18735 }
18736 }
18737 else
18738 {
18739 while (start_byte > limit_byte)
18740 {
18741 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
18742 ceiling = max (limit_byte, ceiling);
18743 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
18744 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
18745 while (1)
18746 {
18747 if (selective_display)
18748 while (--cursor != ceiling_addr
18749 && *cursor != '\n' && *cursor != 015)
18750 ;
18751 else
18752 while (--cursor != ceiling_addr && *cursor != '\n')
18753 ;
18754
18755 if (cursor != ceiling_addr)
18756 {
18757 if (++count == 0)
18758 {
18759 start_byte += cursor - base + 1;
18760 *byte_pos_ptr = start_byte;
18761 /* When scanning backwards, we should
18762 not count the newline posterior to which we stop. */
18763 return - orig_count - 1;
18764 }
18765 }
18766 else
18767 break;
18768 }
18769 /* Here we add 1 to compensate for the last decrement
18770 of CURSOR, which took it past the valid range. */
18771 start_byte += cursor - base + 1;
18772 }
18773 }
18774
18775 *byte_pos_ptr = limit_byte;
18776
18777 if (count < 0)
18778 return - orig_count + count;
18779 return orig_count - count;
18780
18781 }
18782
18783
18784 \f
18785 /***********************************************************************
18786 Displaying strings
18787 ***********************************************************************/
18788
18789 /* Display a NUL-terminated string, starting with index START.
18790
18791 If STRING is non-null, display that C string. Otherwise, the Lisp
18792 string LISP_STRING is displayed. There's a case that STRING is
18793 non-null and LISP_STRING is not nil. It means STRING is a string
18794 data of LISP_STRING. In that case, we display LISP_STRING while
18795 ignoring its text properties.
18796
18797 If FACE_STRING is not nil, FACE_STRING_POS is a position in
18798 FACE_STRING. Display STRING or LISP_STRING with the face at
18799 FACE_STRING_POS in FACE_STRING:
18800
18801 Display the string in the environment given by IT, but use the
18802 standard display table, temporarily.
18803
18804 FIELD_WIDTH is the minimum number of output glyphs to produce.
18805 If STRING has fewer characters than FIELD_WIDTH, pad to the right
18806 with spaces. If STRING has more characters, more than FIELD_WIDTH
18807 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
18808
18809 PRECISION is the maximum number of characters to output from
18810 STRING. PRECISION < 0 means don't truncate the string.
18811
18812 This is roughly equivalent to printf format specifiers:
18813
18814 FIELD_WIDTH PRECISION PRINTF
18815 ----------------------------------------
18816 -1 -1 %s
18817 -1 10 %.10s
18818 10 -1 %10s
18819 20 10 %20.10s
18820
18821 MULTIBYTE zero means do not display multibyte chars, > 0 means do
18822 display them, and < 0 means obey the current buffer's value of
18823 enable_multibyte_characters.
18824
18825 Value is the number of columns displayed. */
18826
18827 static int
18828 display_string (string, lisp_string, face_string, face_string_pos,
18829 start, it, field_width, precision, max_x, multibyte)
18830 unsigned char *string;
18831 Lisp_Object lisp_string;
18832 Lisp_Object face_string;
18833 EMACS_INT face_string_pos;
18834 EMACS_INT start;
18835 struct it *it;
18836 int field_width, precision, max_x;
18837 int multibyte;
18838 {
18839 int hpos_at_start = it->hpos;
18840 int saved_face_id = it->face_id;
18841 struct glyph_row *row = it->glyph_row;
18842
18843 /* Initialize the iterator IT for iteration over STRING beginning
18844 with index START. */
18845 reseat_to_string (it, NILP (lisp_string) ? string : NULL, lisp_string, start,
18846 precision, field_width, multibyte);
18847 if (string && STRINGP (lisp_string))
18848 /* LISP_STRING is the one returned by decode_mode_spec. We should
18849 ignore its text properties. */
18850 it->stop_charpos = -1;
18851
18852 /* If displaying STRING, set up the face of the iterator
18853 from LISP_STRING, if that's given. */
18854 if (STRINGP (face_string))
18855 {
18856 EMACS_INT endptr;
18857 struct face *face;
18858
18859 it->face_id
18860 = face_at_string_position (it->w, face_string, face_string_pos,
18861 0, it->region_beg_charpos,
18862 it->region_end_charpos,
18863 &endptr, it->base_face_id, 0);
18864 face = FACE_FROM_ID (it->f, it->face_id);
18865 it->face_box_p = face->box != FACE_NO_BOX;
18866 }
18867
18868 /* Set max_x to the maximum allowed X position. Don't let it go
18869 beyond the right edge of the window. */
18870 if (max_x <= 0)
18871 max_x = it->last_visible_x;
18872 else
18873 max_x = min (max_x, it->last_visible_x);
18874
18875 /* Skip over display elements that are not visible. because IT->w is
18876 hscrolled. */
18877 if (it->current_x < it->first_visible_x)
18878 move_it_in_display_line_to (it, 100000, it->first_visible_x,
18879 MOVE_TO_POS | MOVE_TO_X);
18880
18881 row->ascent = it->max_ascent;
18882 row->height = it->max_ascent + it->max_descent;
18883 row->phys_ascent = it->max_phys_ascent;
18884 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
18885 row->extra_line_spacing = it->max_extra_line_spacing;
18886
18887 /* This condition is for the case that we are called with current_x
18888 past last_visible_x. */
18889 while (it->current_x < max_x)
18890 {
18891 int x_before, x, n_glyphs_before, i, nglyphs;
18892
18893 /* Get the next display element. */
18894 if (!get_next_display_element (it))
18895 break;
18896
18897 /* Produce glyphs. */
18898 x_before = it->current_x;
18899 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
18900 PRODUCE_GLYPHS (it);
18901
18902 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
18903 i = 0;
18904 x = x_before;
18905 while (i < nglyphs)
18906 {
18907 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
18908
18909 if (it->line_wrap != TRUNCATE
18910 && x + glyph->pixel_width > max_x)
18911 {
18912 /* End of continued line or max_x reached. */
18913 if (CHAR_GLYPH_PADDING_P (*glyph))
18914 {
18915 /* A wide character is unbreakable. */
18916 it->glyph_row->used[TEXT_AREA] = n_glyphs_before;
18917 it->current_x = x_before;
18918 }
18919 else
18920 {
18921 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
18922 it->current_x = x;
18923 }
18924 break;
18925 }
18926 else if (x + glyph->pixel_width >= it->first_visible_x)
18927 {
18928 /* Glyph is at least partially visible. */
18929 ++it->hpos;
18930 if (x < it->first_visible_x)
18931 it->glyph_row->x = x - it->first_visible_x;
18932 }
18933 else
18934 {
18935 /* Glyph is off the left margin of the display area.
18936 Should not happen. */
18937 abort ();
18938 }
18939
18940 row->ascent = max (row->ascent, it->max_ascent);
18941 row->height = max (row->height, it->max_ascent + it->max_descent);
18942 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
18943 row->phys_height = max (row->phys_height,
18944 it->max_phys_ascent + it->max_phys_descent);
18945 row->extra_line_spacing = max (row->extra_line_spacing,
18946 it->max_extra_line_spacing);
18947 x += glyph->pixel_width;
18948 ++i;
18949 }
18950
18951 /* Stop if max_x reached. */
18952 if (i < nglyphs)
18953 break;
18954
18955 /* Stop at line ends. */
18956 if (ITERATOR_AT_END_OF_LINE_P (it))
18957 {
18958 it->continuation_lines_width = 0;
18959 break;
18960 }
18961
18962 set_iterator_to_next (it, 1);
18963
18964 /* Stop if truncating at the right edge. */
18965 if (it->line_wrap == TRUNCATE
18966 && it->current_x >= it->last_visible_x)
18967 {
18968 /* Add truncation mark, but don't do it if the line is
18969 truncated at a padding space. */
18970 if (IT_CHARPOS (*it) < it->string_nchars)
18971 {
18972 if (!FRAME_WINDOW_P (it->f))
18973 {
18974 int i, n;
18975
18976 if (it->current_x > it->last_visible_x)
18977 {
18978 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
18979 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
18980 break;
18981 for (n = row->used[TEXT_AREA]; i < n; ++i)
18982 {
18983 row->used[TEXT_AREA] = i;
18984 produce_special_glyphs (it, IT_TRUNCATION);
18985 }
18986 }
18987 produce_special_glyphs (it, IT_TRUNCATION);
18988 }
18989 it->glyph_row->truncated_on_right_p = 1;
18990 }
18991 break;
18992 }
18993 }
18994
18995 /* Maybe insert a truncation at the left. */
18996 if (it->first_visible_x
18997 && IT_CHARPOS (*it) > 0)
18998 {
18999 if (!FRAME_WINDOW_P (it->f))
19000 insert_left_trunc_glyphs (it);
19001 it->glyph_row->truncated_on_left_p = 1;
19002 }
19003
19004 it->face_id = saved_face_id;
19005
19006 /* Value is number of columns displayed. */
19007 return it->hpos - hpos_at_start;
19008 }
19009
19010
19011 \f
19012 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
19013 appears as an element of LIST or as the car of an element of LIST.
19014 If PROPVAL is a list, compare each element against LIST in that
19015 way, and return 1/2 if any element of PROPVAL is found in LIST.
19016 Otherwise return 0. This function cannot quit.
19017 The return value is 2 if the text is invisible but with an ellipsis
19018 and 1 if it's invisible and without an ellipsis. */
19019
19020 int
19021 invisible_p (propval, list)
19022 register Lisp_Object propval;
19023 Lisp_Object list;
19024 {
19025 register Lisp_Object tail, proptail;
19026
19027 for (tail = list; CONSP (tail); tail = XCDR (tail))
19028 {
19029 register Lisp_Object tem;
19030 tem = XCAR (tail);
19031 if (EQ (propval, tem))
19032 return 1;
19033 if (CONSP (tem) && EQ (propval, XCAR (tem)))
19034 return NILP (XCDR (tem)) ? 1 : 2;
19035 }
19036
19037 if (CONSP (propval))
19038 {
19039 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
19040 {
19041 Lisp_Object propelt;
19042 propelt = XCAR (proptail);
19043 for (tail = list; CONSP (tail); tail = XCDR (tail))
19044 {
19045 register Lisp_Object tem;
19046 tem = XCAR (tail);
19047 if (EQ (propelt, tem))
19048 return 1;
19049 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
19050 return NILP (XCDR (tem)) ? 1 : 2;
19051 }
19052 }
19053 }
19054
19055 return 0;
19056 }
19057
19058 DEFUN ("invisible-p", Finvisible_p, Sinvisible_p, 1, 1, 0,
19059 doc: /* Non-nil if the property makes the text invisible.
19060 POS-OR-PROP can be a marker or number, in which case it is taken to be
19061 a position in the current buffer and the value of the `invisible' property
19062 is checked; or it can be some other value, which is then presumed to be the
19063 value of the `invisible' property of the text of interest.
19064 The non-nil value returned can be t for truly invisible text or something
19065 else if the text is replaced by an ellipsis. */)
19066 (pos_or_prop)
19067 Lisp_Object pos_or_prop;
19068 {
19069 Lisp_Object prop
19070 = (NATNUMP (pos_or_prop) || MARKERP (pos_or_prop)
19071 ? Fget_char_property (pos_or_prop, Qinvisible, Qnil)
19072 : pos_or_prop);
19073 int invis = TEXT_PROP_MEANS_INVISIBLE (prop);
19074 return (invis == 0 ? Qnil
19075 : invis == 1 ? Qt
19076 : make_number (invis));
19077 }
19078
19079 /* Calculate a width or height in pixels from a specification using
19080 the following elements:
19081
19082 SPEC ::=
19083 NUM - a (fractional) multiple of the default font width/height
19084 (NUM) - specifies exactly NUM pixels
19085 UNIT - a fixed number of pixels, see below.
19086 ELEMENT - size of a display element in pixels, see below.
19087 (NUM . SPEC) - equals NUM * SPEC
19088 (+ SPEC SPEC ...) - add pixel values
19089 (- SPEC SPEC ...) - subtract pixel values
19090 (- SPEC) - negate pixel value
19091
19092 NUM ::=
19093 INT or FLOAT - a number constant
19094 SYMBOL - use symbol's (buffer local) variable binding.
19095
19096 UNIT ::=
19097 in - pixels per inch *)
19098 mm - pixels per 1/1000 meter *)
19099 cm - pixels per 1/100 meter *)
19100 width - width of current font in pixels.
19101 height - height of current font in pixels.
19102
19103 *) using the ratio(s) defined in display-pixels-per-inch.
19104
19105 ELEMENT ::=
19106
19107 left-fringe - left fringe width in pixels
19108 right-fringe - right fringe width in pixels
19109
19110 left-margin - left margin width in pixels
19111 right-margin - right margin width in pixels
19112
19113 scroll-bar - scroll-bar area width in pixels
19114
19115 Examples:
19116
19117 Pixels corresponding to 5 inches:
19118 (5 . in)
19119
19120 Total width of non-text areas on left side of window (if scroll-bar is on left):
19121 '(space :width (+ left-fringe left-margin scroll-bar))
19122
19123 Align to first text column (in header line):
19124 '(space :align-to 0)
19125
19126 Align to middle of text area minus half the width of variable `my-image'
19127 containing a loaded image:
19128 '(space :align-to (0.5 . (- text my-image)))
19129
19130 Width of left margin minus width of 1 character in the default font:
19131 '(space :width (- left-margin 1))
19132
19133 Width of left margin minus width of 2 characters in the current font:
19134 '(space :width (- left-margin (2 . width)))
19135
19136 Center 1 character over left-margin (in header line):
19137 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
19138
19139 Different ways to express width of left fringe plus left margin minus one pixel:
19140 '(space :width (- (+ left-fringe left-margin) (1)))
19141 '(space :width (+ left-fringe left-margin (- (1))))
19142 '(space :width (+ left-fringe left-margin (-1)))
19143
19144 */
19145
19146 #define NUMVAL(X) \
19147 ((INTEGERP (X) || FLOATP (X)) \
19148 ? XFLOATINT (X) \
19149 : - 1)
19150
19151 int
19152 calc_pixel_width_or_height (res, it, prop, font, width_p, align_to)
19153 double *res;
19154 struct it *it;
19155 Lisp_Object prop;
19156 struct font *font;
19157 int width_p, *align_to;
19158 {
19159 double pixels;
19160
19161 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
19162 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
19163
19164 if (NILP (prop))
19165 return OK_PIXELS (0);
19166
19167 xassert (FRAME_LIVE_P (it->f));
19168
19169 if (SYMBOLP (prop))
19170 {
19171 if (SCHARS (SYMBOL_NAME (prop)) == 2)
19172 {
19173 char *unit = SDATA (SYMBOL_NAME (prop));
19174
19175 if (unit[0] == 'i' && unit[1] == 'n')
19176 pixels = 1.0;
19177 else if (unit[0] == 'm' && unit[1] == 'm')
19178 pixels = 25.4;
19179 else if (unit[0] == 'c' && unit[1] == 'm')
19180 pixels = 2.54;
19181 else
19182 pixels = 0;
19183 if (pixels > 0)
19184 {
19185 double ppi;
19186 #ifdef HAVE_WINDOW_SYSTEM
19187 if (FRAME_WINDOW_P (it->f)
19188 && (ppi = (width_p
19189 ? FRAME_X_DISPLAY_INFO (it->f)->resx
19190 : FRAME_X_DISPLAY_INFO (it->f)->resy),
19191 ppi > 0))
19192 return OK_PIXELS (ppi / pixels);
19193 #endif
19194
19195 if ((ppi = NUMVAL (Vdisplay_pixels_per_inch), ppi > 0)
19196 || (CONSP (Vdisplay_pixels_per_inch)
19197 && (ppi = (width_p
19198 ? NUMVAL (XCAR (Vdisplay_pixels_per_inch))
19199 : NUMVAL (XCDR (Vdisplay_pixels_per_inch))),
19200 ppi > 0)))
19201 return OK_PIXELS (ppi / pixels);
19202
19203 return 0;
19204 }
19205 }
19206
19207 #ifdef HAVE_WINDOW_SYSTEM
19208 if (EQ (prop, Qheight))
19209 return OK_PIXELS (font ? FONT_HEIGHT (font) : FRAME_LINE_HEIGHT (it->f));
19210 if (EQ (prop, Qwidth))
19211 return OK_PIXELS (font ? FONT_WIDTH (font) : FRAME_COLUMN_WIDTH (it->f));
19212 #else
19213 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
19214 return OK_PIXELS (1);
19215 #endif
19216
19217 if (EQ (prop, Qtext))
19218 return OK_PIXELS (width_p
19219 ? window_box_width (it->w, TEXT_AREA)
19220 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
19221
19222 if (align_to && *align_to < 0)
19223 {
19224 *res = 0;
19225 if (EQ (prop, Qleft))
19226 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
19227 if (EQ (prop, Qright))
19228 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
19229 if (EQ (prop, Qcenter))
19230 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
19231 + window_box_width (it->w, TEXT_AREA) / 2);
19232 if (EQ (prop, Qleft_fringe))
19233 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
19234 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
19235 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
19236 if (EQ (prop, Qright_fringe))
19237 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
19238 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
19239 : window_box_right_offset (it->w, TEXT_AREA));
19240 if (EQ (prop, Qleft_margin))
19241 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
19242 if (EQ (prop, Qright_margin))
19243 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
19244 if (EQ (prop, Qscroll_bar))
19245 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
19246 ? 0
19247 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
19248 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
19249 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
19250 : 0)));
19251 }
19252 else
19253 {
19254 if (EQ (prop, Qleft_fringe))
19255 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
19256 if (EQ (prop, Qright_fringe))
19257 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
19258 if (EQ (prop, Qleft_margin))
19259 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
19260 if (EQ (prop, Qright_margin))
19261 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
19262 if (EQ (prop, Qscroll_bar))
19263 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
19264 }
19265
19266 prop = Fbuffer_local_value (prop, it->w->buffer);
19267 }
19268
19269 if (INTEGERP (prop) || FLOATP (prop))
19270 {
19271 int base_unit = (width_p
19272 ? FRAME_COLUMN_WIDTH (it->f)
19273 : FRAME_LINE_HEIGHT (it->f));
19274 return OK_PIXELS (XFLOATINT (prop) * base_unit);
19275 }
19276
19277 if (CONSP (prop))
19278 {
19279 Lisp_Object car = XCAR (prop);
19280 Lisp_Object cdr = XCDR (prop);
19281
19282 if (SYMBOLP (car))
19283 {
19284 #ifdef HAVE_WINDOW_SYSTEM
19285 if (FRAME_WINDOW_P (it->f)
19286 && valid_image_p (prop))
19287 {
19288 int id = lookup_image (it->f, prop);
19289 struct image *img = IMAGE_FROM_ID (it->f, id);
19290
19291 return OK_PIXELS (width_p ? img->width : img->height);
19292 }
19293 #endif
19294 if (EQ (car, Qplus) || EQ (car, Qminus))
19295 {
19296 int first = 1;
19297 double px;
19298
19299 pixels = 0;
19300 while (CONSP (cdr))
19301 {
19302 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
19303 font, width_p, align_to))
19304 return 0;
19305 if (first)
19306 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
19307 else
19308 pixels += px;
19309 cdr = XCDR (cdr);
19310 }
19311 if (EQ (car, Qminus))
19312 pixels = -pixels;
19313 return OK_PIXELS (pixels);
19314 }
19315
19316 car = Fbuffer_local_value (car, it->w->buffer);
19317 }
19318
19319 if (INTEGERP (car) || FLOATP (car))
19320 {
19321 double fact;
19322 pixels = XFLOATINT (car);
19323 if (NILP (cdr))
19324 return OK_PIXELS (pixels);
19325 if (calc_pixel_width_or_height (&fact, it, cdr,
19326 font, width_p, align_to))
19327 return OK_PIXELS (pixels * fact);
19328 return 0;
19329 }
19330
19331 return 0;
19332 }
19333
19334 return 0;
19335 }
19336
19337 \f
19338 /***********************************************************************
19339 Glyph Display
19340 ***********************************************************************/
19341
19342 #ifdef HAVE_WINDOW_SYSTEM
19343
19344 #if GLYPH_DEBUG
19345
19346 void
19347 dump_glyph_string (s)
19348 struct glyph_string *s;
19349 {
19350 fprintf (stderr, "glyph string\n");
19351 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
19352 s->x, s->y, s->width, s->height);
19353 fprintf (stderr, " ybase = %d\n", s->ybase);
19354 fprintf (stderr, " hl = %d\n", s->hl);
19355 fprintf (stderr, " left overhang = %d, right = %d\n",
19356 s->left_overhang, s->right_overhang);
19357 fprintf (stderr, " nchars = %d\n", s->nchars);
19358 fprintf (stderr, " extends to end of line = %d\n",
19359 s->extends_to_end_of_line_p);
19360 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
19361 fprintf (stderr, " bg width = %d\n", s->background_width);
19362 }
19363
19364 #endif /* GLYPH_DEBUG */
19365
19366 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
19367 of XChar2b structures for S; it can't be allocated in
19368 init_glyph_string because it must be allocated via `alloca'. W
19369 is the window on which S is drawn. ROW and AREA are the glyph row
19370 and area within the row from which S is constructed. START is the
19371 index of the first glyph structure covered by S. HL is a
19372 face-override for drawing S. */
19373
19374 #ifdef HAVE_NTGUI
19375 #define OPTIONAL_HDC(hdc) hdc,
19376 #define DECLARE_HDC(hdc) HDC hdc;
19377 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
19378 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
19379 #endif
19380
19381 #ifndef OPTIONAL_HDC
19382 #define OPTIONAL_HDC(hdc)
19383 #define DECLARE_HDC(hdc)
19384 #define ALLOCATE_HDC(hdc, f)
19385 #define RELEASE_HDC(hdc, f)
19386 #endif
19387
19388 static void
19389 init_glyph_string (s, OPTIONAL_HDC (hdc) char2b, w, row, area, start, hl)
19390 struct glyph_string *s;
19391 DECLARE_HDC (hdc)
19392 XChar2b *char2b;
19393 struct window *w;
19394 struct glyph_row *row;
19395 enum glyph_row_area area;
19396 int start;
19397 enum draw_glyphs_face hl;
19398 {
19399 bzero (s, sizeof *s);
19400 s->w = w;
19401 s->f = XFRAME (w->frame);
19402 #ifdef HAVE_NTGUI
19403 s->hdc = hdc;
19404 #endif
19405 s->display = FRAME_X_DISPLAY (s->f);
19406 s->window = FRAME_X_WINDOW (s->f);
19407 s->char2b = char2b;
19408 s->hl = hl;
19409 s->row = row;
19410 s->area = area;
19411 s->first_glyph = row->glyphs[area] + start;
19412 s->height = row->height;
19413 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
19414 s->ybase = s->y + row->ascent;
19415 }
19416
19417
19418 /* Append the list of glyph strings with head H and tail T to the list
19419 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
19420
19421 static INLINE void
19422 append_glyph_string_lists (head, tail, h, t)
19423 struct glyph_string **head, **tail;
19424 struct glyph_string *h, *t;
19425 {
19426 if (h)
19427 {
19428 if (*head)
19429 (*tail)->next = h;
19430 else
19431 *head = h;
19432 h->prev = *tail;
19433 *tail = t;
19434 }
19435 }
19436
19437
19438 /* Prepend the list of glyph strings with head H and tail T to the
19439 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
19440 result. */
19441
19442 static INLINE void
19443 prepend_glyph_string_lists (head, tail, h, t)
19444 struct glyph_string **head, **tail;
19445 struct glyph_string *h, *t;
19446 {
19447 if (h)
19448 {
19449 if (*head)
19450 (*head)->prev = t;
19451 else
19452 *tail = t;
19453 t->next = *head;
19454 *head = h;
19455 }
19456 }
19457
19458
19459 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
19460 Set *HEAD and *TAIL to the resulting list. */
19461
19462 static INLINE void
19463 append_glyph_string (head, tail, s)
19464 struct glyph_string **head, **tail;
19465 struct glyph_string *s;
19466 {
19467 s->next = s->prev = NULL;
19468 append_glyph_string_lists (head, tail, s, s);
19469 }
19470
19471
19472 /* Get face and two-byte form of character C in face FACE_ID on frame
19473 F. The encoding of C is returned in *CHAR2B. MULTIBYTE_P non-zero
19474 means we want to display multibyte text. DISPLAY_P non-zero means
19475 make sure that X resources for the face returned are allocated.
19476 Value is a pointer to a realized face that is ready for display if
19477 DISPLAY_P is non-zero. */
19478
19479 static INLINE struct face *
19480 get_char_face_and_encoding (f, c, face_id, char2b, multibyte_p, display_p)
19481 struct frame *f;
19482 int c, face_id;
19483 XChar2b *char2b;
19484 int multibyte_p, display_p;
19485 {
19486 struct face *face = FACE_FROM_ID (f, face_id);
19487
19488 if (face->font)
19489 {
19490 unsigned code = face->font->driver->encode_char (face->font, c);
19491
19492 if (code != FONT_INVALID_CODE)
19493 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
19494 else
19495 STORE_XCHAR2B (char2b, 0, 0);
19496 }
19497
19498 /* Make sure X resources of the face are allocated. */
19499 #ifdef HAVE_X_WINDOWS
19500 if (display_p)
19501 #endif
19502 {
19503 xassert (face != NULL);
19504 PREPARE_FACE_FOR_DISPLAY (f, face);
19505 }
19506
19507 return face;
19508 }
19509
19510
19511 /* Get face and two-byte form of character glyph GLYPH on frame F.
19512 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
19513 a pointer to a realized face that is ready for display. */
19514
19515 static INLINE struct face *
19516 get_glyph_face_and_encoding (f, glyph, char2b, two_byte_p)
19517 struct frame *f;
19518 struct glyph *glyph;
19519 XChar2b *char2b;
19520 int *two_byte_p;
19521 {
19522 struct face *face;
19523
19524 xassert (glyph->type == CHAR_GLYPH);
19525 face = FACE_FROM_ID (f, glyph->face_id);
19526
19527 if (two_byte_p)
19528 *two_byte_p = 0;
19529
19530 if (face->font)
19531 {
19532 unsigned code = face->font->driver->encode_char (face->font, glyph->u.ch);
19533
19534 if (code != FONT_INVALID_CODE)
19535 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
19536 else
19537 STORE_XCHAR2B (char2b, 0, 0);
19538 }
19539
19540 /* Make sure X resources of the face are allocated. */
19541 xassert (face != NULL);
19542 PREPARE_FACE_FOR_DISPLAY (f, face);
19543 return face;
19544 }
19545
19546
19547 /* Fill glyph string S with composition components specified by S->cmp.
19548
19549 BASE_FACE is the base face of the composition.
19550 S->cmp_from is the index of the first component for S.
19551
19552 OVERLAPS non-zero means S should draw the foreground only, and use
19553 its physical height for clipping. See also draw_glyphs.
19554
19555 Value is the index of a component not in S. */
19556
19557 static int
19558 fill_composite_glyph_string (s, base_face, overlaps)
19559 struct glyph_string *s;
19560 struct face *base_face;
19561 int overlaps;
19562 {
19563 int i;
19564 /* For all glyphs of this composition, starting at the offset
19565 S->cmp_from, until we reach the end of the definition or encounter a
19566 glyph that requires the different face, add it to S. */
19567 struct face *face;
19568
19569 xassert (s);
19570
19571 s->for_overlaps = overlaps;
19572 s->face = NULL;
19573 s->font = NULL;
19574 for (i = s->cmp_from; i < s->cmp->glyph_len; i++)
19575 {
19576 int c = COMPOSITION_GLYPH (s->cmp, i);
19577
19578 if (c != '\t')
19579 {
19580 int face_id = FACE_FOR_CHAR (s->f, base_face->ascii_face, c,
19581 -1, Qnil);
19582
19583 face = get_char_face_and_encoding (s->f, c, face_id,
19584 s->char2b + i, 1, 1);
19585 if (face)
19586 {
19587 if (! s->face)
19588 {
19589 s->face = face;
19590 s->font = s->face->font;
19591 }
19592 else if (s->face != face)
19593 break;
19594 }
19595 }
19596 ++s->nchars;
19597 }
19598 s->cmp_to = i;
19599
19600 /* All glyph strings for the same composition has the same width,
19601 i.e. the width set for the first component of the composition. */
19602 s->width = s->first_glyph->pixel_width;
19603
19604 /* If the specified font could not be loaded, use the frame's
19605 default font, but record the fact that we couldn't load it in
19606 the glyph string so that we can draw rectangles for the
19607 characters of the glyph string. */
19608 if (s->font == NULL)
19609 {
19610 s->font_not_found_p = 1;
19611 s->font = FRAME_FONT (s->f);
19612 }
19613
19614 /* Adjust base line for subscript/superscript text. */
19615 s->ybase += s->first_glyph->voffset;
19616
19617 /* This glyph string must always be drawn with 16-bit functions. */
19618 s->two_byte_p = 1;
19619
19620 return s->cmp_to;
19621 }
19622
19623 static int
19624 fill_gstring_glyph_string (s, face_id, start, end, overlaps)
19625 struct glyph_string *s;
19626 int face_id;
19627 int start, end, overlaps;
19628 {
19629 struct glyph *glyph, *last;
19630 Lisp_Object lgstring;
19631 int i;
19632
19633 s->for_overlaps = overlaps;
19634 glyph = s->row->glyphs[s->area] + start;
19635 last = s->row->glyphs[s->area] + end;
19636 s->cmp_id = glyph->u.cmp.id;
19637 s->cmp_from = glyph->u.cmp.from;
19638 s->cmp_to = glyph->u.cmp.to + 1;
19639 s->face = FACE_FROM_ID (s->f, face_id);
19640 lgstring = composition_gstring_from_id (s->cmp_id);
19641 s->font = XFONT_OBJECT (LGSTRING_FONT (lgstring));
19642 glyph++;
19643 while (glyph < last
19644 && glyph->u.cmp.automatic
19645 && glyph->u.cmp.id == s->cmp_id
19646 && s->cmp_to == glyph->u.cmp.from)
19647 s->cmp_to = (glyph++)->u.cmp.to + 1;
19648
19649 for (i = s->cmp_from; i < s->cmp_to; i++)
19650 {
19651 Lisp_Object lglyph = LGSTRING_GLYPH (lgstring, i);
19652 unsigned code = LGLYPH_CODE (lglyph);
19653
19654 STORE_XCHAR2B ((s->char2b + i), code >> 8, code & 0xFF);
19655 }
19656 s->width = composition_gstring_width (lgstring, s->cmp_from, s->cmp_to, NULL);
19657 return glyph - s->row->glyphs[s->area];
19658 }
19659
19660
19661 /* Fill glyph string S from a sequence of character glyphs.
19662
19663 FACE_ID is the face id of the string. START is the index of the
19664 first glyph to consider, END is the index of the last + 1.
19665 OVERLAPS non-zero means S should draw the foreground only, and use
19666 its physical height for clipping. See also draw_glyphs.
19667
19668 Value is the index of the first glyph not in S. */
19669
19670 static int
19671 fill_glyph_string (s, face_id, start, end, overlaps)
19672 struct glyph_string *s;
19673 int face_id;
19674 int start, end, overlaps;
19675 {
19676 struct glyph *glyph, *last;
19677 int voffset;
19678 int glyph_not_available_p;
19679
19680 xassert (s->f == XFRAME (s->w->frame));
19681 xassert (s->nchars == 0);
19682 xassert (start >= 0 && end > start);
19683
19684 s->for_overlaps = overlaps;
19685 glyph = s->row->glyphs[s->area] + start;
19686 last = s->row->glyphs[s->area] + end;
19687 voffset = glyph->voffset;
19688 s->padding_p = glyph->padding_p;
19689 glyph_not_available_p = glyph->glyph_not_available_p;
19690
19691 while (glyph < last
19692 && glyph->type == CHAR_GLYPH
19693 && glyph->voffset == voffset
19694 /* Same face id implies same font, nowadays. */
19695 && glyph->face_id == face_id
19696 && glyph->glyph_not_available_p == glyph_not_available_p)
19697 {
19698 int two_byte_p;
19699
19700 s->face = get_glyph_face_and_encoding (s->f, glyph,
19701 s->char2b + s->nchars,
19702 &two_byte_p);
19703 s->two_byte_p = two_byte_p;
19704 ++s->nchars;
19705 xassert (s->nchars <= end - start);
19706 s->width += glyph->pixel_width;
19707 if (glyph++->padding_p != s->padding_p)
19708 break;
19709 }
19710
19711 s->font = s->face->font;
19712
19713 /* If the specified font could not be loaded, use the frame's font,
19714 but record the fact that we couldn't load it in
19715 S->font_not_found_p so that we can draw rectangles for the
19716 characters of the glyph string. */
19717 if (s->font == NULL || glyph_not_available_p)
19718 {
19719 s->font_not_found_p = 1;
19720 s->font = FRAME_FONT (s->f);
19721 }
19722
19723 /* Adjust base line for subscript/superscript text. */
19724 s->ybase += voffset;
19725
19726 xassert (s->face && s->face->gc);
19727 return glyph - s->row->glyphs[s->area];
19728 }
19729
19730
19731 /* Fill glyph string S from image glyph S->first_glyph. */
19732
19733 static void
19734 fill_image_glyph_string (s)
19735 struct glyph_string *s;
19736 {
19737 xassert (s->first_glyph->type == IMAGE_GLYPH);
19738 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
19739 xassert (s->img);
19740 s->slice = s->first_glyph->slice;
19741 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
19742 s->font = s->face->font;
19743 s->width = s->first_glyph->pixel_width;
19744
19745 /* Adjust base line for subscript/superscript text. */
19746 s->ybase += s->first_glyph->voffset;
19747 }
19748
19749
19750 /* Fill glyph string S from a sequence of stretch glyphs.
19751
19752 ROW is the glyph row in which the glyphs are found, AREA is the
19753 area within the row. START is the index of the first glyph to
19754 consider, END is the index of the last + 1.
19755
19756 Value is the index of the first glyph not in S. */
19757
19758 static int
19759 fill_stretch_glyph_string (s, row, area, start, end)
19760 struct glyph_string *s;
19761 struct glyph_row *row;
19762 enum glyph_row_area area;
19763 int start, end;
19764 {
19765 struct glyph *glyph, *last;
19766 int voffset, face_id;
19767
19768 xassert (s->first_glyph->type == STRETCH_GLYPH);
19769
19770 glyph = s->row->glyphs[s->area] + start;
19771 last = s->row->glyphs[s->area] + end;
19772 face_id = glyph->face_id;
19773 s->face = FACE_FROM_ID (s->f, face_id);
19774 s->font = s->face->font;
19775 s->width = glyph->pixel_width;
19776 s->nchars = 1;
19777 voffset = glyph->voffset;
19778
19779 for (++glyph;
19780 (glyph < last
19781 && glyph->type == STRETCH_GLYPH
19782 && glyph->voffset == voffset
19783 && glyph->face_id == face_id);
19784 ++glyph)
19785 s->width += glyph->pixel_width;
19786
19787 /* Adjust base line for subscript/superscript text. */
19788 s->ybase += voffset;
19789
19790 /* The case that face->gc == 0 is handled when drawing the glyph
19791 string by calling PREPARE_FACE_FOR_DISPLAY. */
19792 xassert (s->face);
19793 return glyph - s->row->glyphs[s->area];
19794 }
19795
19796 static struct font_metrics *
19797 get_per_char_metric (f, font, char2b)
19798 struct frame *f;
19799 struct font *font;
19800 XChar2b *char2b;
19801 {
19802 static struct font_metrics metrics;
19803 unsigned code = (XCHAR2B_BYTE1 (char2b) << 8) | XCHAR2B_BYTE2 (char2b);
19804
19805 if (! font || code == FONT_INVALID_CODE)
19806 return NULL;
19807 font->driver->text_extents (font, &code, 1, &metrics);
19808 return &metrics;
19809 }
19810
19811 /* EXPORT for RIF:
19812 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
19813 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
19814 assumed to be zero. */
19815
19816 void
19817 x_get_glyph_overhangs (glyph, f, left, right)
19818 struct glyph *glyph;
19819 struct frame *f;
19820 int *left, *right;
19821 {
19822 *left = *right = 0;
19823
19824 if (glyph->type == CHAR_GLYPH)
19825 {
19826 struct face *face;
19827 XChar2b char2b;
19828 struct font_metrics *pcm;
19829
19830 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
19831 if (face->font && (pcm = get_per_char_metric (f, face->font, &char2b)))
19832 {
19833 if (pcm->rbearing > pcm->width)
19834 *right = pcm->rbearing - pcm->width;
19835 if (pcm->lbearing < 0)
19836 *left = -pcm->lbearing;
19837 }
19838 }
19839 else if (glyph->type == COMPOSITE_GLYPH)
19840 {
19841 if (! glyph->u.cmp.automatic)
19842 {
19843 struct composition *cmp = composition_table[glyph->u.cmp.id];
19844
19845 if (cmp->rbearing > cmp->pixel_width)
19846 *right = cmp->rbearing - cmp->pixel_width;
19847 if (cmp->lbearing < 0)
19848 *left = - cmp->lbearing;
19849 }
19850 else
19851 {
19852 Lisp_Object gstring = composition_gstring_from_id (glyph->u.cmp.id);
19853 struct font_metrics metrics;
19854
19855 composition_gstring_width (gstring, glyph->u.cmp.from,
19856 glyph->u.cmp.to + 1, &metrics);
19857 if (metrics.rbearing > metrics.width)
19858 *right = metrics.rbearing - metrics.width;
19859 if (metrics.lbearing < 0)
19860 *left = - metrics.lbearing;
19861 }
19862 }
19863 }
19864
19865
19866 /* Return the index of the first glyph preceding glyph string S that
19867 is overwritten by S because of S's left overhang. Value is -1
19868 if no glyphs are overwritten. */
19869
19870 static int
19871 left_overwritten (s)
19872 struct glyph_string *s;
19873 {
19874 int k;
19875
19876 if (s->left_overhang)
19877 {
19878 int x = 0, i;
19879 struct glyph *glyphs = s->row->glyphs[s->area];
19880 int first = s->first_glyph - glyphs;
19881
19882 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
19883 x -= glyphs[i].pixel_width;
19884
19885 k = i + 1;
19886 }
19887 else
19888 k = -1;
19889
19890 return k;
19891 }
19892
19893
19894 /* Return the index of the first glyph preceding glyph string S that
19895 is overwriting S because of its right overhang. Value is -1 if no
19896 glyph in front of S overwrites S. */
19897
19898 static int
19899 left_overwriting (s)
19900 struct glyph_string *s;
19901 {
19902 int i, k, x;
19903 struct glyph *glyphs = s->row->glyphs[s->area];
19904 int first = s->first_glyph - glyphs;
19905
19906 k = -1;
19907 x = 0;
19908 for (i = first - 1; i >= 0; --i)
19909 {
19910 int left, right;
19911 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
19912 if (x + right > 0)
19913 k = i;
19914 x -= glyphs[i].pixel_width;
19915 }
19916
19917 return k;
19918 }
19919
19920
19921 /* Return the index of the last glyph following glyph string S that is
19922 overwritten by S because of S's right overhang. Value is -1 if
19923 no such glyph is found. */
19924
19925 static int
19926 right_overwritten (s)
19927 struct glyph_string *s;
19928 {
19929 int k = -1;
19930
19931 if (s->right_overhang)
19932 {
19933 int x = 0, i;
19934 struct glyph *glyphs = s->row->glyphs[s->area];
19935 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
19936 int end = s->row->used[s->area];
19937
19938 for (i = first; i < end && s->right_overhang > x; ++i)
19939 x += glyphs[i].pixel_width;
19940
19941 k = i;
19942 }
19943
19944 return k;
19945 }
19946
19947
19948 /* Return the index of the last glyph following glyph string S that
19949 overwrites S because of its left overhang. Value is negative
19950 if no such glyph is found. */
19951
19952 static int
19953 right_overwriting (s)
19954 struct glyph_string *s;
19955 {
19956 int i, k, x;
19957 int end = s->row->used[s->area];
19958 struct glyph *glyphs = s->row->glyphs[s->area];
19959 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
19960
19961 k = -1;
19962 x = 0;
19963 for (i = first; i < end; ++i)
19964 {
19965 int left, right;
19966 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
19967 if (x - left < 0)
19968 k = i;
19969 x += glyphs[i].pixel_width;
19970 }
19971
19972 return k;
19973 }
19974
19975
19976 /* Set background width of glyph string S. START is the index of the
19977 first glyph following S. LAST_X is the right-most x-position + 1
19978 in the drawing area. */
19979
19980 static INLINE void
19981 set_glyph_string_background_width (s, start, last_x)
19982 struct glyph_string *s;
19983 int start;
19984 int last_x;
19985 {
19986 /* If the face of this glyph string has to be drawn to the end of
19987 the drawing area, set S->extends_to_end_of_line_p. */
19988
19989 if (start == s->row->used[s->area]
19990 && s->area == TEXT_AREA
19991 && ((s->row->fill_line_p
19992 && (s->hl == DRAW_NORMAL_TEXT
19993 || s->hl == DRAW_IMAGE_RAISED
19994 || s->hl == DRAW_IMAGE_SUNKEN))
19995 || s->hl == DRAW_MOUSE_FACE))
19996 s->extends_to_end_of_line_p = 1;
19997
19998 /* If S extends its face to the end of the line, set its
19999 background_width to the distance to the right edge of the drawing
20000 area. */
20001 if (s->extends_to_end_of_line_p)
20002 s->background_width = last_x - s->x + 1;
20003 else
20004 s->background_width = s->width;
20005 }
20006
20007
20008 /* Compute overhangs and x-positions for glyph string S and its
20009 predecessors, or successors. X is the starting x-position for S.
20010 BACKWARD_P non-zero means process predecessors. */
20011
20012 static void
20013 compute_overhangs_and_x (s, x, backward_p)
20014 struct glyph_string *s;
20015 int x;
20016 int backward_p;
20017 {
20018 if (backward_p)
20019 {
20020 while (s)
20021 {
20022 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
20023 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
20024 x -= s->width;
20025 s->x = x;
20026 s = s->prev;
20027 }
20028 }
20029 else
20030 {
20031 while (s)
20032 {
20033 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
20034 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
20035 s->x = x;
20036 x += s->width;
20037 s = s->next;
20038 }
20039 }
20040 }
20041
20042
20043
20044 /* The following macros are only called from draw_glyphs below.
20045 They reference the following parameters of that function directly:
20046 `w', `row', `area', and `overlap_p'
20047 as well as the following local variables:
20048 `s', `f', and `hdc' (in W32) */
20049
20050 #ifdef HAVE_NTGUI
20051 /* On W32, silently add local `hdc' variable to argument list of
20052 init_glyph_string. */
20053 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
20054 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
20055 #else
20056 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
20057 init_glyph_string (s, char2b, w, row, area, start, hl)
20058 #endif
20059
20060 /* Add a glyph string for a stretch glyph to the list of strings
20061 between HEAD and TAIL. START is the index of the stretch glyph in
20062 row area AREA of glyph row ROW. END is the index of the last glyph
20063 in that glyph row area. X is the current output position assigned
20064 to the new glyph string constructed. HL overrides that face of the
20065 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
20066 is the right-most x-position of the drawing area. */
20067
20068 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
20069 and below -- keep them on one line. */
20070 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
20071 do \
20072 { \
20073 s = (struct glyph_string *) alloca (sizeof *s); \
20074 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
20075 START = fill_stretch_glyph_string (s, row, area, START, END); \
20076 append_glyph_string (&HEAD, &TAIL, s); \
20077 s->x = (X); \
20078 } \
20079 while (0)
20080
20081
20082 /* Add a glyph string for an image glyph to the list of strings
20083 between HEAD and TAIL. START is the index of the image glyph in
20084 row area AREA of glyph row ROW. END is the index of the last glyph
20085 in that glyph row area. X is the current output position assigned
20086 to the new glyph string constructed. HL overrides that face of the
20087 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
20088 is the right-most x-position of the drawing area. */
20089
20090 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
20091 do \
20092 { \
20093 s = (struct glyph_string *) alloca (sizeof *s); \
20094 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
20095 fill_image_glyph_string (s); \
20096 append_glyph_string (&HEAD, &TAIL, s); \
20097 ++START; \
20098 s->x = (X); \
20099 } \
20100 while (0)
20101
20102
20103 /* Add a glyph string for a sequence of character glyphs to the list
20104 of strings between HEAD and TAIL. START is the index of the first
20105 glyph in row area AREA of glyph row ROW that is part of the new
20106 glyph string. END is the index of the last glyph in that glyph row
20107 area. X is the current output position assigned to the new glyph
20108 string constructed. HL overrides that face of the glyph; e.g. it
20109 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
20110 right-most x-position of the drawing area. */
20111
20112 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
20113 do \
20114 { \
20115 int face_id; \
20116 XChar2b *char2b; \
20117 \
20118 face_id = (row)->glyphs[area][START].face_id; \
20119 \
20120 s = (struct glyph_string *) alloca (sizeof *s); \
20121 char2b = (XChar2b *) alloca ((END - START) * sizeof *char2b); \
20122 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
20123 append_glyph_string (&HEAD, &TAIL, s); \
20124 s->x = (X); \
20125 START = fill_glyph_string (s, face_id, START, END, overlaps); \
20126 } \
20127 while (0)
20128
20129
20130 /* Add a glyph string for a composite sequence to the list of strings
20131 between HEAD and TAIL. START is the index of the first glyph in
20132 row area AREA of glyph row ROW that is part of the new glyph
20133 string. END is the index of the last glyph in that glyph row area.
20134 X is the current output position assigned to the new glyph string
20135 constructed. HL overrides that face of the glyph; e.g. it is
20136 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
20137 x-position of the drawing area. */
20138
20139 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
20140 do { \
20141 int face_id = (row)->glyphs[area][START].face_id; \
20142 struct face *base_face = FACE_FROM_ID (f, face_id); \
20143 int cmp_id = (row)->glyphs[area][START].u.cmp.id; \
20144 struct composition *cmp = composition_table[cmp_id]; \
20145 XChar2b *char2b; \
20146 struct glyph_string *first_s; \
20147 int n; \
20148 \
20149 char2b = (XChar2b *) alloca ((sizeof *char2b) * cmp->glyph_len); \
20150 \
20151 /* Make glyph_strings for each glyph sequence that is drawable by \
20152 the same face, and append them to HEAD/TAIL. */ \
20153 for (n = 0; n < cmp->glyph_len;) \
20154 { \
20155 s = (struct glyph_string *) alloca (sizeof *s); \
20156 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
20157 append_glyph_string (&(HEAD), &(TAIL), s); \
20158 s->cmp = cmp; \
20159 s->cmp_from = n; \
20160 s->x = (X); \
20161 if (n == 0) \
20162 first_s = s; \
20163 n = fill_composite_glyph_string (s, base_face, overlaps); \
20164 } \
20165 \
20166 ++START; \
20167 s = first_s; \
20168 } while (0)
20169
20170
20171 /* Add a glyph string for a glyph-string sequence to the list of strings
20172 between HEAD and TAIL. */
20173
20174 #define BUILD_GSTRING_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
20175 do { \
20176 int face_id; \
20177 XChar2b *char2b; \
20178 Lisp_Object gstring; \
20179 \
20180 face_id = (row)->glyphs[area][START].face_id; \
20181 gstring = (composition_gstring_from_id \
20182 ((row)->glyphs[area][START].u.cmp.id)); \
20183 s = (struct glyph_string *) alloca (sizeof *s); \
20184 char2b = (XChar2b *) alloca ((sizeof *char2b) \
20185 * LGSTRING_GLYPH_LEN (gstring)); \
20186 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
20187 append_glyph_string (&(HEAD), &(TAIL), s); \
20188 s->x = (X); \
20189 START = fill_gstring_glyph_string (s, face_id, START, END, overlaps); \
20190 } while (0)
20191
20192
20193 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
20194 of AREA of glyph row ROW on window W between indices START and END.
20195 HL overrides the face for drawing glyph strings, e.g. it is
20196 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
20197 x-positions of the drawing area.
20198
20199 This is an ugly monster macro construct because we must use alloca
20200 to allocate glyph strings (because draw_glyphs can be called
20201 asynchronously). */
20202
20203 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
20204 do \
20205 { \
20206 HEAD = TAIL = NULL; \
20207 while (START < END) \
20208 { \
20209 struct glyph *first_glyph = (row)->glyphs[area] + START; \
20210 switch (first_glyph->type) \
20211 { \
20212 case CHAR_GLYPH: \
20213 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
20214 HL, X, LAST_X); \
20215 break; \
20216 \
20217 case COMPOSITE_GLYPH: \
20218 if (first_glyph->u.cmp.automatic) \
20219 BUILD_GSTRING_GLYPH_STRING (START, END, HEAD, TAIL, \
20220 HL, X, LAST_X); \
20221 else \
20222 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
20223 HL, X, LAST_X); \
20224 break; \
20225 \
20226 case STRETCH_GLYPH: \
20227 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
20228 HL, X, LAST_X); \
20229 break; \
20230 \
20231 case IMAGE_GLYPH: \
20232 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
20233 HL, X, LAST_X); \
20234 break; \
20235 \
20236 default: \
20237 abort (); \
20238 } \
20239 \
20240 if (s) \
20241 { \
20242 set_glyph_string_background_width (s, START, LAST_X); \
20243 (X) += s->width; \
20244 } \
20245 } \
20246 } while (0)
20247
20248
20249 /* Draw glyphs between START and END in AREA of ROW on window W,
20250 starting at x-position X. X is relative to AREA in W. HL is a
20251 face-override with the following meaning:
20252
20253 DRAW_NORMAL_TEXT draw normally
20254 DRAW_CURSOR draw in cursor face
20255 DRAW_MOUSE_FACE draw in mouse face.
20256 DRAW_INVERSE_VIDEO draw in mode line face
20257 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
20258 DRAW_IMAGE_RAISED draw an image with a raised relief around it
20259
20260 If OVERLAPS is non-zero, draw only the foreground of characters and
20261 clip to the physical height of ROW. Non-zero value also defines
20262 the overlapping part to be drawn:
20263
20264 OVERLAPS_PRED overlap with preceding rows
20265 OVERLAPS_SUCC overlap with succeeding rows
20266 OVERLAPS_BOTH overlap with both preceding/succeeding rows
20267 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
20268
20269 Value is the x-position reached, relative to AREA of W. */
20270
20271 static int
20272 draw_glyphs (w, x, row, area, start, end, hl, overlaps)
20273 struct window *w;
20274 int x;
20275 struct glyph_row *row;
20276 enum glyph_row_area area;
20277 EMACS_INT start, end;
20278 enum draw_glyphs_face hl;
20279 int overlaps;
20280 {
20281 struct glyph_string *head, *tail;
20282 struct glyph_string *s;
20283 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
20284 int i, j, x_reached, last_x, area_left = 0;
20285 struct frame *f = XFRAME (WINDOW_FRAME (w));
20286 DECLARE_HDC (hdc);
20287
20288 ALLOCATE_HDC (hdc, f);
20289
20290 /* Let's rather be paranoid than getting a SEGV. */
20291 end = min (end, row->used[area]);
20292 start = max (0, start);
20293 start = min (end, start);
20294
20295 /* Translate X to frame coordinates. Set last_x to the right
20296 end of the drawing area. */
20297 if (row->full_width_p)
20298 {
20299 /* X is relative to the left edge of W, without scroll bars
20300 or fringes. */
20301 area_left = WINDOW_LEFT_EDGE_X (w);
20302 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
20303 }
20304 else
20305 {
20306 area_left = window_box_left (w, area);
20307 last_x = area_left + window_box_width (w, area);
20308 }
20309 x += area_left;
20310
20311 /* Build a doubly-linked list of glyph_string structures between
20312 head and tail from what we have to draw. Note that the macro
20313 BUILD_GLYPH_STRINGS will modify its start parameter. That's
20314 the reason we use a separate variable `i'. */
20315 i = start;
20316 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
20317 if (tail)
20318 x_reached = tail->x + tail->background_width;
20319 else
20320 x_reached = x;
20321
20322 /* If there are any glyphs with lbearing < 0 or rbearing > width in
20323 the row, redraw some glyphs in front or following the glyph
20324 strings built above. */
20325 if (head && !overlaps && row->contains_overlapping_glyphs_p)
20326 {
20327 struct glyph_string *h, *t;
20328 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
20329 int mouse_beg_col, mouse_end_col, check_mouse_face = 0;
20330 int dummy_x = 0;
20331
20332 /* If mouse highlighting is on, we may need to draw adjacent
20333 glyphs using mouse-face highlighting. */
20334 if (area == TEXT_AREA && row->mouse_face_p)
20335 {
20336 struct glyph_row *mouse_beg_row, *mouse_end_row;
20337
20338 mouse_beg_row = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_beg_row);
20339 mouse_end_row = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_end_row);
20340
20341 if (row >= mouse_beg_row && row <= mouse_end_row)
20342 {
20343 check_mouse_face = 1;
20344 mouse_beg_col = (row == mouse_beg_row)
20345 ? dpyinfo->mouse_face_beg_col : 0;
20346 mouse_end_col = (row == mouse_end_row)
20347 ? dpyinfo->mouse_face_end_col
20348 : row->used[TEXT_AREA];
20349 }
20350 }
20351
20352 /* Compute overhangs for all glyph strings. */
20353 if (FRAME_RIF (f)->compute_glyph_string_overhangs)
20354 for (s = head; s; s = s->next)
20355 FRAME_RIF (f)->compute_glyph_string_overhangs (s);
20356
20357 /* Prepend glyph strings for glyphs in front of the first glyph
20358 string that are overwritten because of the first glyph
20359 string's left overhang. The background of all strings
20360 prepended must be drawn because the first glyph string
20361 draws over it. */
20362 i = left_overwritten (head);
20363 if (i >= 0)
20364 {
20365 enum draw_glyphs_face overlap_hl;
20366
20367 /* If this row contains mouse highlighting, attempt to draw
20368 the overlapped glyphs with the correct highlight. This
20369 code fails if the overlap encompasses more than one glyph
20370 and mouse-highlight spans only some of these glyphs.
20371 However, making it work perfectly involves a lot more
20372 code, and I don't know if the pathological case occurs in
20373 practice, so we'll stick to this for now. --- cyd */
20374 if (check_mouse_face
20375 && mouse_beg_col < start && mouse_end_col > i)
20376 overlap_hl = DRAW_MOUSE_FACE;
20377 else
20378 overlap_hl = DRAW_NORMAL_TEXT;
20379
20380 j = i;
20381 BUILD_GLYPH_STRINGS (j, start, h, t,
20382 overlap_hl, dummy_x, last_x);
20383 start = i;
20384 compute_overhangs_and_x (t, head->x, 1);
20385 prepend_glyph_string_lists (&head, &tail, h, t);
20386 clip_head = head;
20387 }
20388
20389 /* Prepend glyph strings for glyphs in front of the first glyph
20390 string that overwrite that glyph string because of their
20391 right overhang. For these strings, only the foreground must
20392 be drawn, because it draws over the glyph string at `head'.
20393 The background must not be drawn because this would overwrite
20394 right overhangs of preceding glyphs for which no glyph
20395 strings exist. */
20396 i = left_overwriting (head);
20397 if (i >= 0)
20398 {
20399 enum draw_glyphs_face overlap_hl;
20400
20401 if (check_mouse_face
20402 && mouse_beg_col < start && mouse_end_col > i)
20403 overlap_hl = DRAW_MOUSE_FACE;
20404 else
20405 overlap_hl = DRAW_NORMAL_TEXT;
20406
20407 clip_head = head;
20408 BUILD_GLYPH_STRINGS (i, start, h, t,
20409 overlap_hl, dummy_x, last_x);
20410 for (s = h; s; s = s->next)
20411 s->background_filled_p = 1;
20412 compute_overhangs_and_x (t, head->x, 1);
20413 prepend_glyph_string_lists (&head, &tail, h, t);
20414 }
20415
20416 /* Append glyphs strings for glyphs following the last glyph
20417 string tail that are overwritten by tail. The background of
20418 these strings has to be drawn because tail's foreground draws
20419 over it. */
20420 i = right_overwritten (tail);
20421 if (i >= 0)
20422 {
20423 enum draw_glyphs_face overlap_hl;
20424
20425 if (check_mouse_face
20426 && mouse_beg_col < i && mouse_end_col > end)
20427 overlap_hl = DRAW_MOUSE_FACE;
20428 else
20429 overlap_hl = DRAW_NORMAL_TEXT;
20430
20431 BUILD_GLYPH_STRINGS (end, i, h, t,
20432 overlap_hl, x, last_x);
20433 /* Because BUILD_GLYPH_STRINGS updates the first argument,
20434 we don't have `end = i;' here. */
20435 compute_overhangs_and_x (h, tail->x + tail->width, 0);
20436 append_glyph_string_lists (&head, &tail, h, t);
20437 clip_tail = tail;
20438 }
20439
20440 /* Append glyph strings for glyphs following the last glyph
20441 string tail that overwrite tail. The foreground of such
20442 glyphs has to be drawn because it writes into the background
20443 of tail. The background must not be drawn because it could
20444 paint over the foreground of following glyphs. */
20445 i = right_overwriting (tail);
20446 if (i >= 0)
20447 {
20448 enum draw_glyphs_face overlap_hl;
20449 if (check_mouse_face
20450 && mouse_beg_col < i && mouse_end_col > end)
20451 overlap_hl = DRAW_MOUSE_FACE;
20452 else
20453 overlap_hl = DRAW_NORMAL_TEXT;
20454
20455 clip_tail = tail;
20456 i++; /* We must include the Ith glyph. */
20457 BUILD_GLYPH_STRINGS (end, i, h, t,
20458 overlap_hl, x, last_x);
20459 for (s = h; s; s = s->next)
20460 s->background_filled_p = 1;
20461 compute_overhangs_and_x (h, tail->x + tail->width, 0);
20462 append_glyph_string_lists (&head, &tail, h, t);
20463 }
20464 if (clip_head || clip_tail)
20465 for (s = head; s; s = s->next)
20466 {
20467 s->clip_head = clip_head;
20468 s->clip_tail = clip_tail;
20469 }
20470 }
20471
20472 /* Draw all strings. */
20473 for (s = head; s; s = s->next)
20474 FRAME_RIF (f)->draw_glyph_string (s);
20475
20476 #ifndef HAVE_NS
20477 /* When focus a sole frame and move horizontally, this sets on_p to 0
20478 causing a failure to erase prev cursor position. */
20479 if (area == TEXT_AREA
20480 && !row->full_width_p
20481 /* When drawing overlapping rows, only the glyph strings'
20482 foreground is drawn, which doesn't erase a cursor
20483 completely. */
20484 && !overlaps)
20485 {
20486 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
20487 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
20488 : (tail ? tail->x + tail->background_width : x));
20489 x0 -= area_left;
20490 x1 -= area_left;
20491
20492 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
20493 row->y, MATRIX_ROW_BOTTOM_Y (row));
20494 }
20495 #endif
20496
20497 /* Value is the x-position up to which drawn, relative to AREA of W.
20498 This doesn't include parts drawn because of overhangs. */
20499 if (row->full_width_p)
20500 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
20501 else
20502 x_reached -= area_left;
20503
20504 RELEASE_HDC (hdc, f);
20505
20506 return x_reached;
20507 }
20508
20509 /* Expand row matrix if too narrow. Don't expand if area
20510 is not present. */
20511
20512 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
20513 { \
20514 if (!fonts_changed_p \
20515 && (it->glyph_row->glyphs[area] \
20516 < it->glyph_row->glyphs[area + 1])) \
20517 { \
20518 it->w->ncols_scale_factor++; \
20519 fonts_changed_p = 1; \
20520 } \
20521 }
20522
20523 /* Store one glyph for IT->char_to_display in IT->glyph_row.
20524 Called from x_produce_glyphs when IT->glyph_row is non-null. */
20525
20526 static INLINE void
20527 append_glyph (it)
20528 struct it *it;
20529 {
20530 struct glyph *glyph;
20531 enum glyph_row_area area = it->area;
20532
20533 xassert (it->glyph_row);
20534 xassert (it->char_to_display != '\n' && it->char_to_display != '\t');
20535
20536 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
20537 if (glyph < it->glyph_row->glyphs[area + 1])
20538 {
20539 glyph->charpos = CHARPOS (it->position);
20540 glyph->object = it->object;
20541 if (it->pixel_width > 0)
20542 {
20543 glyph->pixel_width = it->pixel_width;
20544 glyph->padding_p = 0;
20545 }
20546 else
20547 {
20548 /* Assure at least 1-pixel width. Otherwise, cursor can't
20549 be displayed correctly. */
20550 glyph->pixel_width = 1;
20551 glyph->padding_p = 1;
20552 }
20553 glyph->ascent = it->ascent;
20554 glyph->descent = it->descent;
20555 glyph->voffset = it->voffset;
20556 glyph->type = CHAR_GLYPH;
20557 glyph->avoid_cursor_p = it->avoid_cursor_p;
20558 glyph->multibyte_p = it->multibyte_p;
20559 glyph->left_box_line_p = it->start_of_box_run_p;
20560 glyph->right_box_line_p = it->end_of_box_run_p;
20561 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
20562 || it->phys_descent > it->descent);
20563 glyph->glyph_not_available_p = it->glyph_not_available_p;
20564 glyph->face_id = it->face_id;
20565 glyph->u.ch = it->char_to_display;
20566 glyph->slice = null_glyph_slice;
20567 glyph->font_type = FONT_TYPE_UNKNOWN;
20568 ++it->glyph_row->used[area];
20569 }
20570 else
20571 IT_EXPAND_MATRIX_WIDTH (it, area);
20572 }
20573
20574 /* Store one glyph for the composition IT->cmp_it.id in
20575 IT->glyph_row. Called from x_produce_glyphs when IT->glyph_row is
20576 non-null. */
20577
20578 static INLINE void
20579 append_composite_glyph (it)
20580 struct it *it;
20581 {
20582 struct glyph *glyph;
20583 enum glyph_row_area area = it->area;
20584
20585 xassert (it->glyph_row);
20586
20587 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
20588 if (glyph < it->glyph_row->glyphs[area + 1])
20589 {
20590 glyph->charpos = CHARPOS (it->position);
20591 glyph->object = it->object;
20592 glyph->pixel_width = it->pixel_width;
20593 glyph->ascent = it->ascent;
20594 glyph->descent = it->descent;
20595 glyph->voffset = it->voffset;
20596 glyph->type = COMPOSITE_GLYPH;
20597 if (it->cmp_it.ch < 0)
20598 {
20599 glyph->u.cmp.automatic = 0;
20600 glyph->u.cmp.id = it->cmp_it.id;
20601 }
20602 else
20603 {
20604 glyph->u.cmp.automatic = 1;
20605 glyph->u.cmp.id = it->cmp_it.id;
20606 glyph->u.cmp.from = it->cmp_it.from;
20607 glyph->u.cmp.to = it->cmp_it.to - 1;
20608 }
20609 glyph->avoid_cursor_p = it->avoid_cursor_p;
20610 glyph->multibyte_p = it->multibyte_p;
20611 glyph->left_box_line_p = it->start_of_box_run_p;
20612 glyph->right_box_line_p = it->end_of_box_run_p;
20613 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
20614 || it->phys_descent > it->descent);
20615 glyph->padding_p = 0;
20616 glyph->glyph_not_available_p = 0;
20617 glyph->face_id = it->face_id;
20618 glyph->slice = null_glyph_slice;
20619 glyph->font_type = FONT_TYPE_UNKNOWN;
20620 ++it->glyph_row->used[area];
20621 }
20622 else
20623 IT_EXPAND_MATRIX_WIDTH (it, area);
20624 }
20625
20626
20627 /* Change IT->ascent and IT->height according to the setting of
20628 IT->voffset. */
20629
20630 static INLINE void
20631 take_vertical_position_into_account (it)
20632 struct it *it;
20633 {
20634 if (it->voffset)
20635 {
20636 if (it->voffset < 0)
20637 /* Increase the ascent so that we can display the text higher
20638 in the line. */
20639 it->ascent -= it->voffset;
20640 else
20641 /* Increase the descent so that we can display the text lower
20642 in the line. */
20643 it->descent += it->voffset;
20644 }
20645 }
20646
20647
20648 /* Produce glyphs/get display metrics for the image IT is loaded with.
20649 See the description of struct display_iterator in dispextern.h for
20650 an overview of struct display_iterator. */
20651
20652 static void
20653 produce_image_glyph (it)
20654 struct it *it;
20655 {
20656 struct image *img;
20657 struct face *face;
20658 int glyph_ascent, crop;
20659 struct glyph_slice slice;
20660
20661 xassert (it->what == IT_IMAGE);
20662
20663 face = FACE_FROM_ID (it->f, it->face_id);
20664 xassert (face);
20665 /* Make sure X resources of the face is loaded. */
20666 PREPARE_FACE_FOR_DISPLAY (it->f, face);
20667
20668 if (it->image_id < 0)
20669 {
20670 /* Fringe bitmap. */
20671 it->ascent = it->phys_ascent = 0;
20672 it->descent = it->phys_descent = 0;
20673 it->pixel_width = 0;
20674 it->nglyphs = 0;
20675 return;
20676 }
20677
20678 img = IMAGE_FROM_ID (it->f, it->image_id);
20679 xassert (img);
20680 /* Make sure X resources of the image is loaded. */
20681 prepare_image_for_display (it->f, img);
20682
20683 slice.x = slice.y = 0;
20684 slice.width = img->width;
20685 slice.height = img->height;
20686
20687 if (INTEGERP (it->slice.x))
20688 slice.x = XINT (it->slice.x);
20689 else if (FLOATP (it->slice.x))
20690 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
20691
20692 if (INTEGERP (it->slice.y))
20693 slice.y = XINT (it->slice.y);
20694 else if (FLOATP (it->slice.y))
20695 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
20696
20697 if (INTEGERP (it->slice.width))
20698 slice.width = XINT (it->slice.width);
20699 else if (FLOATP (it->slice.width))
20700 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
20701
20702 if (INTEGERP (it->slice.height))
20703 slice.height = XINT (it->slice.height);
20704 else if (FLOATP (it->slice.height))
20705 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
20706
20707 if (slice.x >= img->width)
20708 slice.x = img->width;
20709 if (slice.y >= img->height)
20710 slice.y = img->height;
20711 if (slice.x + slice.width >= img->width)
20712 slice.width = img->width - slice.x;
20713 if (slice.y + slice.height > img->height)
20714 slice.height = img->height - slice.y;
20715
20716 if (slice.width == 0 || slice.height == 0)
20717 return;
20718
20719 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
20720
20721 it->descent = slice.height - glyph_ascent;
20722 if (slice.y == 0)
20723 it->descent += img->vmargin;
20724 if (slice.y + slice.height == img->height)
20725 it->descent += img->vmargin;
20726 it->phys_descent = it->descent;
20727
20728 it->pixel_width = slice.width;
20729 if (slice.x == 0)
20730 it->pixel_width += img->hmargin;
20731 if (slice.x + slice.width == img->width)
20732 it->pixel_width += img->hmargin;
20733
20734 /* It's quite possible for images to have an ascent greater than
20735 their height, so don't get confused in that case. */
20736 if (it->descent < 0)
20737 it->descent = 0;
20738
20739 it->nglyphs = 1;
20740
20741 if (face->box != FACE_NO_BOX)
20742 {
20743 if (face->box_line_width > 0)
20744 {
20745 if (slice.y == 0)
20746 it->ascent += face->box_line_width;
20747 if (slice.y + slice.height == img->height)
20748 it->descent += face->box_line_width;
20749 }
20750
20751 if (it->start_of_box_run_p && slice.x == 0)
20752 it->pixel_width += eabs (face->box_line_width);
20753 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
20754 it->pixel_width += eabs (face->box_line_width);
20755 }
20756
20757 take_vertical_position_into_account (it);
20758
20759 /* Automatically crop wide image glyphs at right edge so we can
20760 draw the cursor on same display row. */
20761 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
20762 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
20763 {
20764 it->pixel_width -= crop;
20765 slice.width -= crop;
20766 }
20767
20768 if (it->glyph_row)
20769 {
20770 struct glyph *glyph;
20771 enum glyph_row_area area = it->area;
20772
20773 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
20774 if (glyph < it->glyph_row->glyphs[area + 1])
20775 {
20776 glyph->charpos = CHARPOS (it->position);
20777 glyph->object = it->object;
20778 glyph->pixel_width = it->pixel_width;
20779 glyph->ascent = glyph_ascent;
20780 glyph->descent = it->descent;
20781 glyph->voffset = it->voffset;
20782 glyph->type = IMAGE_GLYPH;
20783 glyph->avoid_cursor_p = it->avoid_cursor_p;
20784 glyph->multibyte_p = it->multibyte_p;
20785 glyph->left_box_line_p = it->start_of_box_run_p;
20786 glyph->right_box_line_p = it->end_of_box_run_p;
20787 glyph->overlaps_vertically_p = 0;
20788 glyph->padding_p = 0;
20789 glyph->glyph_not_available_p = 0;
20790 glyph->face_id = it->face_id;
20791 glyph->u.img_id = img->id;
20792 glyph->slice = slice;
20793 glyph->font_type = FONT_TYPE_UNKNOWN;
20794 ++it->glyph_row->used[area];
20795 }
20796 else
20797 IT_EXPAND_MATRIX_WIDTH (it, area);
20798 }
20799 }
20800
20801
20802 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
20803 of the glyph, WIDTH and HEIGHT are the width and height of the
20804 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
20805
20806 static void
20807 append_stretch_glyph (it, object, width, height, ascent)
20808 struct it *it;
20809 Lisp_Object object;
20810 int width, height;
20811 int ascent;
20812 {
20813 struct glyph *glyph;
20814 enum glyph_row_area area = it->area;
20815
20816 xassert (ascent >= 0 && ascent <= height);
20817
20818 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
20819 if (glyph < it->glyph_row->glyphs[area + 1])
20820 {
20821 glyph->charpos = CHARPOS (it->position);
20822 glyph->object = object;
20823 glyph->pixel_width = width;
20824 glyph->ascent = ascent;
20825 glyph->descent = height - ascent;
20826 glyph->voffset = it->voffset;
20827 glyph->type = STRETCH_GLYPH;
20828 glyph->avoid_cursor_p = it->avoid_cursor_p;
20829 glyph->multibyte_p = it->multibyte_p;
20830 glyph->left_box_line_p = it->start_of_box_run_p;
20831 glyph->right_box_line_p = it->end_of_box_run_p;
20832 glyph->overlaps_vertically_p = 0;
20833 glyph->padding_p = 0;
20834 glyph->glyph_not_available_p = 0;
20835 glyph->face_id = it->face_id;
20836 glyph->u.stretch.ascent = ascent;
20837 glyph->u.stretch.height = height;
20838 glyph->slice = null_glyph_slice;
20839 glyph->font_type = FONT_TYPE_UNKNOWN;
20840 ++it->glyph_row->used[area];
20841 }
20842 else
20843 IT_EXPAND_MATRIX_WIDTH (it, area);
20844 }
20845
20846
20847 /* Produce a stretch glyph for iterator IT. IT->object is the value
20848 of the glyph property displayed. The value must be a list
20849 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
20850 being recognized:
20851
20852 1. `:width WIDTH' specifies that the space should be WIDTH *
20853 canonical char width wide. WIDTH may be an integer or floating
20854 point number.
20855
20856 2. `:relative-width FACTOR' specifies that the width of the stretch
20857 should be computed from the width of the first character having the
20858 `glyph' property, and should be FACTOR times that width.
20859
20860 3. `:align-to HPOS' specifies that the space should be wide enough
20861 to reach HPOS, a value in canonical character units.
20862
20863 Exactly one of the above pairs must be present.
20864
20865 4. `:height HEIGHT' specifies that the height of the stretch produced
20866 should be HEIGHT, measured in canonical character units.
20867
20868 5. `:relative-height FACTOR' specifies that the height of the
20869 stretch should be FACTOR times the height of the characters having
20870 the glyph property.
20871
20872 Either none or exactly one of 4 or 5 must be present.
20873
20874 6. `:ascent ASCENT' specifies that ASCENT percent of the height
20875 of the stretch should be used for the ascent of the stretch.
20876 ASCENT must be in the range 0 <= ASCENT <= 100. */
20877
20878 static void
20879 produce_stretch_glyph (it)
20880 struct it *it;
20881 {
20882 /* (space :width WIDTH :height HEIGHT ...) */
20883 Lisp_Object prop, plist;
20884 int width = 0, height = 0, align_to = -1;
20885 int zero_width_ok_p = 0, zero_height_ok_p = 0;
20886 int ascent = 0;
20887 double tem;
20888 struct face *face = FACE_FROM_ID (it->f, it->face_id);
20889 struct font *font = face->font ? face->font : FRAME_FONT (it->f);
20890
20891 PREPARE_FACE_FOR_DISPLAY (it->f, face);
20892
20893 /* List should start with `space'. */
20894 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
20895 plist = XCDR (it->object);
20896
20897 /* Compute the width of the stretch. */
20898 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
20899 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
20900 {
20901 /* Absolute width `:width WIDTH' specified and valid. */
20902 zero_width_ok_p = 1;
20903 width = (int)tem;
20904 }
20905 else if (prop = Fplist_get (plist, QCrelative_width),
20906 NUMVAL (prop) > 0)
20907 {
20908 /* Relative width `:relative-width FACTOR' specified and valid.
20909 Compute the width of the characters having the `glyph'
20910 property. */
20911 struct it it2;
20912 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
20913
20914 it2 = *it;
20915 if (it->multibyte_p)
20916 {
20917 int maxlen = ((IT_BYTEPOS (*it) >= GPT ? ZV : GPT)
20918 - IT_BYTEPOS (*it));
20919 it2.c = STRING_CHAR_AND_LENGTH (p, it2.len);
20920 }
20921 else
20922 it2.c = *p, it2.len = 1;
20923
20924 it2.glyph_row = NULL;
20925 it2.what = IT_CHARACTER;
20926 x_produce_glyphs (&it2);
20927 width = NUMVAL (prop) * it2.pixel_width;
20928 }
20929 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
20930 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
20931 {
20932 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
20933 align_to = (align_to < 0
20934 ? 0
20935 : align_to - window_box_left_offset (it->w, TEXT_AREA));
20936 else if (align_to < 0)
20937 align_to = window_box_left_offset (it->w, TEXT_AREA);
20938 width = max (0, (int)tem + align_to - it->current_x);
20939 zero_width_ok_p = 1;
20940 }
20941 else
20942 /* Nothing specified -> width defaults to canonical char width. */
20943 width = FRAME_COLUMN_WIDTH (it->f);
20944
20945 if (width <= 0 && (width < 0 || !zero_width_ok_p))
20946 width = 1;
20947
20948 /* Compute height. */
20949 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
20950 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
20951 {
20952 height = (int)tem;
20953 zero_height_ok_p = 1;
20954 }
20955 else if (prop = Fplist_get (plist, QCrelative_height),
20956 NUMVAL (prop) > 0)
20957 height = FONT_HEIGHT (font) * NUMVAL (prop);
20958 else
20959 height = FONT_HEIGHT (font);
20960
20961 if (height <= 0 && (height < 0 || !zero_height_ok_p))
20962 height = 1;
20963
20964 /* Compute percentage of height used for ascent. If
20965 `:ascent ASCENT' is present and valid, use that. Otherwise,
20966 derive the ascent from the font in use. */
20967 if (prop = Fplist_get (plist, QCascent),
20968 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
20969 ascent = height * NUMVAL (prop) / 100.0;
20970 else if (!NILP (prop)
20971 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
20972 ascent = min (max (0, (int)tem), height);
20973 else
20974 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
20975
20976 if (width > 0 && it->line_wrap != TRUNCATE
20977 && it->current_x + width > it->last_visible_x)
20978 width = it->last_visible_x - it->current_x - 1;
20979
20980 if (width > 0 && height > 0 && it->glyph_row)
20981 {
20982 Lisp_Object object = it->stack[it->sp - 1].string;
20983 if (!STRINGP (object))
20984 object = it->w->buffer;
20985 append_stretch_glyph (it, object, width, height, ascent);
20986 }
20987
20988 it->pixel_width = width;
20989 it->ascent = it->phys_ascent = ascent;
20990 it->descent = it->phys_descent = height - it->ascent;
20991 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
20992
20993 take_vertical_position_into_account (it);
20994 }
20995
20996 /* Calculate line-height and line-spacing properties.
20997 An integer value specifies explicit pixel value.
20998 A float value specifies relative value to current face height.
20999 A cons (float . face-name) specifies relative value to
21000 height of specified face font.
21001
21002 Returns height in pixels, or nil. */
21003
21004
21005 static Lisp_Object
21006 calc_line_height_property (it, val, font, boff, override)
21007 struct it *it;
21008 Lisp_Object val;
21009 struct font *font;
21010 int boff, override;
21011 {
21012 Lisp_Object face_name = Qnil;
21013 int ascent, descent, height;
21014
21015 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
21016 return val;
21017
21018 if (CONSP (val))
21019 {
21020 face_name = XCAR (val);
21021 val = XCDR (val);
21022 if (!NUMBERP (val))
21023 val = make_number (1);
21024 if (NILP (face_name))
21025 {
21026 height = it->ascent + it->descent;
21027 goto scale;
21028 }
21029 }
21030
21031 if (NILP (face_name))
21032 {
21033 font = FRAME_FONT (it->f);
21034 boff = FRAME_BASELINE_OFFSET (it->f);
21035 }
21036 else if (EQ (face_name, Qt))
21037 {
21038 override = 0;
21039 }
21040 else
21041 {
21042 int face_id;
21043 struct face *face;
21044
21045 face_id = lookup_named_face (it->f, face_name, 0);
21046 if (face_id < 0)
21047 return make_number (-1);
21048
21049 face = FACE_FROM_ID (it->f, face_id);
21050 font = face->font;
21051 if (font == NULL)
21052 return make_number (-1);
21053 boff = font->baseline_offset;
21054 if (font->vertical_centering)
21055 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
21056 }
21057
21058 ascent = FONT_BASE (font) + boff;
21059 descent = FONT_DESCENT (font) - boff;
21060
21061 if (override)
21062 {
21063 it->override_ascent = ascent;
21064 it->override_descent = descent;
21065 it->override_boff = boff;
21066 }
21067
21068 height = ascent + descent;
21069
21070 scale:
21071 if (FLOATP (val))
21072 height = (int)(XFLOAT_DATA (val) * height);
21073 else if (INTEGERP (val))
21074 height *= XINT (val);
21075
21076 return make_number (height);
21077 }
21078
21079
21080 /* RIF:
21081 Produce glyphs/get display metrics for the display element IT is
21082 loaded with. See the description of struct it in dispextern.h
21083 for an overview of struct it. */
21084
21085 void
21086 x_produce_glyphs (it)
21087 struct it *it;
21088 {
21089 int extra_line_spacing = it->extra_line_spacing;
21090
21091 it->glyph_not_available_p = 0;
21092
21093 if (it->what == IT_CHARACTER)
21094 {
21095 XChar2b char2b;
21096 struct font *font;
21097 struct face *face = FACE_FROM_ID (it->f, it->face_id);
21098 struct font_metrics *pcm;
21099 int font_not_found_p;
21100 int boff; /* baseline offset */
21101 /* We may change it->multibyte_p upon unibyte<->multibyte
21102 conversion. So, save the current value now and restore it
21103 later.
21104
21105 Note: It seems that we don't have to record multibyte_p in
21106 struct glyph because the character code itself tells whether
21107 or not the character is multibyte. Thus, in the future, we
21108 must consider eliminating the field `multibyte_p' in the
21109 struct glyph. */
21110 int saved_multibyte_p = it->multibyte_p;
21111
21112 /* Maybe translate single-byte characters to multibyte, or the
21113 other way. */
21114 it->char_to_display = it->c;
21115 if (!ASCII_BYTE_P (it->c)
21116 && ! it->multibyte_p)
21117 {
21118 if (SINGLE_BYTE_CHAR_P (it->c)
21119 && unibyte_display_via_language_environment)
21120 {
21121 struct charset *unibyte = CHARSET_FROM_ID (charset_unibyte);
21122
21123 /* get_next_display_element assures that this decoding
21124 never fails. */
21125 it->char_to_display = DECODE_CHAR (unibyte, it->c);
21126 it->multibyte_p = 1;
21127 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display,
21128 -1, Qnil);
21129 face = FACE_FROM_ID (it->f, it->face_id);
21130 }
21131 }
21132
21133 /* Get font to use. Encode IT->char_to_display. */
21134 get_char_face_and_encoding (it->f, it->char_to_display, it->face_id,
21135 &char2b, it->multibyte_p, 0);
21136 font = face->font;
21137
21138 font_not_found_p = font == NULL;
21139 if (font_not_found_p)
21140 {
21141 /* When no suitable font found, display an empty box based
21142 on the metrics of the font of the default face (or what
21143 remapped). */
21144 struct face *no_font_face
21145 = FACE_FROM_ID (it->f,
21146 NILP (Vface_remapping_alist) ? DEFAULT_FACE_ID
21147 : lookup_basic_face (it->f, DEFAULT_FACE_ID));
21148 font = no_font_face->font;
21149 boff = font->baseline_offset;
21150 }
21151 else
21152 {
21153 boff = font->baseline_offset;
21154 if (font->vertical_centering)
21155 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
21156 }
21157
21158 if (it->char_to_display >= ' '
21159 && (!it->multibyte_p || it->char_to_display < 128))
21160 {
21161 /* Either unibyte or ASCII. */
21162 int stretched_p;
21163
21164 it->nglyphs = 1;
21165
21166 pcm = get_per_char_metric (it->f, font, &char2b);
21167
21168 if (it->override_ascent >= 0)
21169 {
21170 it->ascent = it->override_ascent;
21171 it->descent = it->override_descent;
21172 boff = it->override_boff;
21173 }
21174 else
21175 {
21176 it->ascent = FONT_BASE (font) + boff;
21177 it->descent = FONT_DESCENT (font) - boff;
21178 }
21179
21180 if (pcm)
21181 {
21182 it->phys_ascent = pcm->ascent + boff;
21183 it->phys_descent = pcm->descent - boff;
21184 it->pixel_width = pcm->width;
21185 }
21186 else
21187 {
21188 it->glyph_not_available_p = 1;
21189 it->phys_ascent = it->ascent;
21190 it->phys_descent = it->descent;
21191 it->pixel_width = FONT_WIDTH (font);
21192 }
21193
21194 if (it->constrain_row_ascent_descent_p)
21195 {
21196 if (it->descent > it->max_descent)
21197 {
21198 it->ascent += it->descent - it->max_descent;
21199 it->descent = it->max_descent;
21200 }
21201 if (it->ascent > it->max_ascent)
21202 {
21203 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
21204 it->ascent = it->max_ascent;
21205 }
21206 it->phys_ascent = min (it->phys_ascent, it->ascent);
21207 it->phys_descent = min (it->phys_descent, it->descent);
21208 extra_line_spacing = 0;
21209 }
21210
21211 /* If this is a space inside a region of text with
21212 `space-width' property, change its width. */
21213 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
21214 if (stretched_p)
21215 it->pixel_width *= XFLOATINT (it->space_width);
21216
21217 /* If face has a box, add the box thickness to the character
21218 height. If character has a box line to the left and/or
21219 right, add the box line width to the character's width. */
21220 if (face->box != FACE_NO_BOX)
21221 {
21222 int thick = face->box_line_width;
21223
21224 if (thick > 0)
21225 {
21226 it->ascent += thick;
21227 it->descent += thick;
21228 }
21229 else
21230 thick = -thick;
21231
21232 if (it->start_of_box_run_p)
21233 it->pixel_width += thick;
21234 if (it->end_of_box_run_p)
21235 it->pixel_width += thick;
21236 }
21237
21238 /* If face has an overline, add the height of the overline
21239 (1 pixel) and a 1 pixel margin to the character height. */
21240 if (face->overline_p)
21241 it->ascent += overline_margin;
21242
21243 if (it->constrain_row_ascent_descent_p)
21244 {
21245 if (it->ascent > it->max_ascent)
21246 it->ascent = it->max_ascent;
21247 if (it->descent > it->max_descent)
21248 it->descent = it->max_descent;
21249 }
21250
21251 take_vertical_position_into_account (it);
21252
21253 /* If we have to actually produce glyphs, do it. */
21254 if (it->glyph_row)
21255 {
21256 if (stretched_p)
21257 {
21258 /* Translate a space with a `space-width' property
21259 into a stretch glyph. */
21260 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
21261 / FONT_HEIGHT (font));
21262 append_stretch_glyph (it, it->object, it->pixel_width,
21263 it->ascent + it->descent, ascent);
21264 }
21265 else
21266 append_glyph (it);
21267
21268 /* If characters with lbearing or rbearing are displayed
21269 in this line, record that fact in a flag of the
21270 glyph row. This is used to optimize X output code. */
21271 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
21272 it->glyph_row->contains_overlapping_glyphs_p = 1;
21273 }
21274 if (! stretched_p && it->pixel_width == 0)
21275 /* We assure that all visible glyphs have at least 1-pixel
21276 width. */
21277 it->pixel_width = 1;
21278 }
21279 else if (it->char_to_display == '\n')
21280 {
21281 /* A newline has no width, but we need the height of the
21282 line. But if previous part of the line sets a height,
21283 don't increase that height */
21284
21285 Lisp_Object height;
21286 Lisp_Object total_height = Qnil;
21287
21288 it->override_ascent = -1;
21289 it->pixel_width = 0;
21290 it->nglyphs = 0;
21291
21292 height = get_it_property(it, Qline_height);
21293 /* Split (line-height total-height) list */
21294 if (CONSP (height)
21295 && CONSP (XCDR (height))
21296 && NILP (XCDR (XCDR (height))))
21297 {
21298 total_height = XCAR (XCDR (height));
21299 height = XCAR (height);
21300 }
21301 height = calc_line_height_property(it, height, font, boff, 1);
21302
21303 if (it->override_ascent >= 0)
21304 {
21305 it->ascent = it->override_ascent;
21306 it->descent = it->override_descent;
21307 boff = it->override_boff;
21308 }
21309 else
21310 {
21311 it->ascent = FONT_BASE (font) + boff;
21312 it->descent = FONT_DESCENT (font) - boff;
21313 }
21314
21315 if (EQ (height, Qt))
21316 {
21317 if (it->descent > it->max_descent)
21318 {
21319 it->ascent += it->descent - it->max_descent;
21320 it->descent = it->max_descent;
21321 }
21322 if (it->ascent > it->max_ascent)
21323 {
21324 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
21325 it->ascent = it->max_ascent;
21326 }
21327 it->phys_ascent = min (it->phys_ascent, it->ascent);
21328 it->phys_descent = min (it->phys_descent, it->descent);
21329 it->constrain_row_ascent_descent_p = 1;
21330 extra_line_spacing = 0;
21331 }
21332 else
21333 {
21334 Lisp_Object spacing;
21335
21336 it->phys_ascent = it->ascent;
21337 it->phys_descent = it->descent;
21338
21339 if ((it->max_ascent > 0 || it->max_descent > 0)
21340 && face->box != FACE_NO_BOX
21341 && face->box_line_width > 0)
21342 {
21343 it->ascent += face->box_line_width;
21344 it->descent += face->box_line_width;
21345 }
21346 if (!NILP (height)
21347 && XINT (height) > it->ascent + it->descent)
21348 it->ascent = XINT (height) - it->descent;
21349
21350 if (!NILP (total_height))
21351 spacing = calc_line_height_property(it, total_height, font, boff, 0);
21352 else
21353 {
21354 spacing = get_it_property(it, Qline_spacing);
21355 spacing = calc_line_height_property(it, spacing, font, boff, 0);
21356 }
21357 if (INTEGERP (spacing))
21358 {
21359 extra_line_spacing = XINT (spacing);
21360 if (!NILP (total_height))
21361 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
21362 }
21363 }
21364 }
21365 else if (it->char_to_display == '\t')
21366 {
21367 if (font->space_width > 0)
21368 {
21369 int tab_width = it->tab_width * font->space_width;
21370 int x = it->current_x + it->continuation_lines_width;
21371 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
21372
21373 /* If the distance from the current position to the next tab
21374 stop is less than a space character width, use the
21375 tab stop after that. */
21376 if (next_tab_x - x < font->space_width)
21377 next_tab_x += tab_width;
21378
21379 it->pixel_width = next_tab_x - x;
21380 it->nglyphs = 1;
21381 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
21382 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
21383
21384 if (it->glyph_row)
21385 {
21386 append_stretch_glyph (it, it->object, it->pixel_width,
21387 it->ascent + it->descent, it->ascent);
21388 }
21389 }
21390 else
21391 {
21392 it->pixel_width = 0;
21393 it->nglyphs = 1;
21394 }
21395 }
21396 else
21397 {
21398 /* A multi-byte character. Assume that the display width of the
21399 character is the width of the character multiplied by the
21400 width of the font. */
21401
21402 /* If we found a font, this font should give us the right
21403 metrics. If we didn't find a font, use the frame's
21404 default font and calculate the width of the character by
21405 multiplying the width of font by the width of the
21406 character. */
21407
21408 pcm = get_per_char_metric (it->f, font, &char2b);
21409
21410 if (font_not_found_p || !pcm)
21411 {
21412 int char_width = CHAR_WIDTH (it->char_to_display);
21413
21414 if (char_width == 0)
21415 /* This is a non spacing character. But, as we are
21416 going to display an empty box, the box must occupy
21417 at least one column. */
21418 char_width = 1;
21419 it->glyph_not_available_p = 1;
21420 it->pixel_width = font->space_width * char_width;
21421 it->phys_ascent = FONT_BASE (font) + boff;
21422 it->phys_descent = FONT_DESCENT (font) - boff;
21423 }
21424 else
21425 {
21426 it->pixel_width = pcm->width;
21427 it->phys_ascent = pcm->ascent + boff;
21428 it->phys_descent = pcm->descent - boff;
21429 if (it->glyph_row
21430 && (pcm->lbearing < 0
21431 || pcm->rbearing > pcm->width))
21432 it->glyph_row->contains_overlapping_glyphs_p = 1;
21433 }
21434 it->nglyphs = 1;
21435 it->ascent = FONT_BASE (font) + boff;
21436 it->descent = FONT_DESCENT (font) - boff;
21437 if (face->box != FACE_NO_BOX)
21438 {
21439 int thick = face->box_line_width;
21440
21441 if (thick > 0)
21442 {
21443 it->ascent += thick;
21444 it->descent += thick;
21445 }
21446 else
21447 thick = - thick;
21448
21449 if (it->start_of_box_run_p)
21450 it->pixel_width += thick;
21451 if (it->end_of_box_run_p)
21452 it->pixel_width += thick;
21453 }
21454
21455 /* If face has an overline, add the height of the overline
21456 (1 pixel) and a 1 pixel margin to the character height. */
21457 if (face->overline_p)
21458 it->ascent += overline_margin;
21459
21460 take_vertical_position_into_account (it);
21461
21462 if (it->ascent < 0)
21463 it->ascent = 0;
21464 if (it->descent < 0)
21465 it->descent = 0;
21466
21467 if (it->glyph_row)
21468 append_glyph (it);
21469 if (it->pixel_width == 0)
21470 /* We assure that all visible glyphs have at least 1-pixel
21471 width. */
21472 it->pixel_width = 1;
21473 }
21474 it->multibyte_p = saved_multibyte_p;
21475 }
21476 else if (it->what == IT_COMPOSITION && it->cmp_it.ch < 0)
21477 {
21478 /* A static composition.
21479
21480 Note: A composition is represented as one glyph in the
21481 glyph matrix. There are no padding glyphs.
21482
21483 Important note: pixel_width, ascent, and descent are the
21484 values of what is drawn by draw_glyphs (i.e. the values of
21485 the overall glyphs composed). */
21486 struct face *face = FACE_FROM_ID (it->f, it->face_id);
21487 int boff; /* baseline offset */
21488 struct composition *cmp = composition_table[it->cmp_it.id];
21489 int glyph_len = cmp->glyph_len;
21490 struct font *font = face->font;
21491
21492 it->nglyphs = 1;
21493
21494 /* If we have not yet calculated pixel size data of glyphs of
21495 the composition for the current face font, calculate them
21496 now. Theoretically, we have to check all fonts for the
21497 glyphs, but that requires much time and memory space. So,
21498 here we check only the font of the first glyph. This may
21499 lead to incorrect display, but it's very rare, and C-l
21500 (recenter-top-bottom) can correct the display anyway. */
21501 if (! cmp->font || cmp->font != font)
21502 {
21503 /* Ascent and descent of the font of the first character
21504 of this composition (adjusted by baseline offset).
21505 Ascent and descent of overall glyphs should not be less
21506 than these, respectively. */
21507 int font_ascent, font_descent, font_height;
21508 /* Bounding box of the overall glyphs. */
21509 int leftmost, rightmost, lowest, highest;
21510 int lbearing, rbearing;
21511 int i, width, ascent, descent;
21512 int left_padded = 0, right_padded = 0;
21513 int c;
21514 XChar2b char2b;
21515 struct font_metrics *pcm;
21516 int font_not_found_p;
21517 int pos;
21518
21519 for (glyph_len = cmp->glyph_len; glyph_len > 0; glyph_len--)
21520 if ((c = COMPOSITION_GLYPH (cmp, glyph_len - 1)) != '\t')
21521 break;
21522 if (glyph_len < cmp->glyph_len)
21523 right_padded = 1;
21524 for (i = 0; i < glyph_len; i++)
21525 {
21526 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
21527 break;
21528 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
21529 }
21530 if (i > 0)
21531 left_padded = 1;
21532
21533 pos = (STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
21534 : IT_CHARPOS (*it));
21535 /* If no suitable font is found, use the default font. */
21536 font_not_found_p = font == NULL;
21537 if (font_not_found_p)
21538 {
21539 face = face->ascii_face;
21540 font = face->font;
21541 }
21542 boff = font->baseline_offset;
21543 if (font->vertical_centering)
21544 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
21545 font_ascent = FONT_BASE (font) + boff;
21546 font_descent = FONT_DESCENT (font) - boff;
21547 font_height = FONT_HEIGHT (font);
21548
21549 cmp->font = (void *) font;
21550
21551 pcm = NULL;
21552 if (! font_not_found_p)
21553 {
21554 get_char_face_and_encoding (it->f, c, it->face_id,
21555 &char2b, it->multibyte_p, 0);
21556 pcm = get_per_char_metric (it->f, font, &char2b);
21557 }
21558
21559 /* Initialize the bounding box. */
21560 if (pcm)
21561 {
21562 width = pcm->width;
21563 ascent = pcm->ascent;
21564 descent = pcm->descent;
21565 lbearing = pcm->lbearing;
21566 rbearing = pcm->rbearing;
21567 }
21568 else
21569 {
21570 width = FONT_WIDTH (font);
21571 ascent = FONT_BASE (font);
21572 descent = FONT_DESCENT (font);
21573 lbearing = 0;
21574 rbearing = width;
21575 }
21576
21577 rightmost = width;
21578 leftmost = 0;
21579 lowest = - descent + boff;
21580 highest = ascent + boff;
21581
21582 if (! font_not_found_p
21583 && font->default_ascent
21584 && CHAR_TABLE_P (Vuse_default_ascent)
21585 && !NILP (Faref (Vuse_default_ascent,
21586 make_number (it->char_to_display))))
21587 highest = font->default_ascent + boff;
21588
21589 /* Draw the first glyph at the normal position. It may be
21590 shifted to right later if some other glyphs are drawn
21591 at the left. */
21592 cmp->offsets[i * 2] = 0;
21593 cmp->offsets[i * 2 + 1] = boff;
21594 cmp->lbearing = lbearing;
21595 cmp->rbearing = rbearing;
21596
21597 /* Set cmp->offsets for the remaining glyphs. */
21598 for (i++; i < glyph_len; i++)
21599 {
21600 int left, right, btm, top;
21601 int ch = COMPOSITION_GLYPH (cmp, i);
21602 int face_id;
21603 struct face *this_face;
21604 int this_boff;
21605
21606 if (ch == '\t')
21607 ch = ' ';
21608 face_id = FACE_FOR_CHAR (it->f, face, ch, pos, it->string);
21609 this_face = FACE_FROM_ID (it->f, face_id);
21610 font = this_face->font;
21611
21612 if (font == NULL)
21613 pcm = NULL;
21614 else
21615 {
21616 this_boff = font->baseline_offset;
21617 if (font->vertical_centering)
21618 this_boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
21619 get_char_face_and_encoding (it->f, ch, face_id,
21620 &char2b, it->multibyte_p, 0);
21621 pcm = get_per_char_metric (it->f, font, &char2b);
21622 }
21623 if (! pcm)
21624 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
21625 else
21626 {
21627 width = pcm->width;
21628 ascent = pcm->ascent;
21629 descent = pcm->descent;
21630 lbearing = pcm->lbearing;
21631 rbearing = pcm->rbearing;
21632 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
21633 {
21634 /* Relative composition with or without
21635 alternate chars. */
21636 left = (leftmost + rightmost - width) / 2;
21637 btm = - descent + boff;
21638 if (font->relative_compose
21639 && (! CHAR_TABLE_P (Vignore_relative_composition)
21640 || NILP (Faref (Vignore_relative_composition,
21641 make_number (ch)))))
21642 {
21643
21644 if (- descent >= font->relative_compose)
21645 /* One extra pixel between two glyphs. */
21646 btm = highest + 1;
21647 else if (ascent <= 0)
21648 /* One extra pixel between two glyphs. */
21649 btm = lowest - 1 - ascent - descent;
21650 }
21651 }
21652 else
21653 {
21654 /* A composition rule is specified by an integer
21655 value that encodes global and new reference
21656 points (GREF and NREF). GREF and NREF are
21657 specified by numbers as below:
21658
21659 0---1---2 -- ascent
21660 | |
21661 | |
21662 | |
21663 9--10--11 -- center
21664 | |
21665 ---3---4---5--- baseline
21666 | |
21667 6---7---8 -- descent
21668 */
21669 int rule = COMPOSITION_RULE (cmp, i);
21670 int gref, nref, grefx, grefy, nrefx, nrefy, xoff, yoff;
21671
21672 COMPOSITION_DECODE_RULE (rule, gref, nref, xoff, yoff);
21673 grefx = gref % 3, nrefx = nref % 3;
21674 grefy = gref / 3, nrefy = nref / 3;
21675 if (xoff)
21676 xoff = font_height * (xoff - 128) / 256;
21677 if (yoff)
21678 yoff = font_height * (yoff - 128) / 256;
21679
21680 left = (leftmost
21681 + grefx * (rightmost - leftmost) / 2
21682 - nrefx * width / 2
21683 + xoff);
21684
21685 btm = ((grefy == 0 ? highest
21686 : grefy == 1 ? 0
21687 : grefy == 2 ? lowest
21688 : (highest + lowest) / 2)
21689 - (nrefy == 0 ? ascent + descent
21690 : nrefy == 1 ? descent - boff
21691 : nrefy == 2 ? 0
21692 : (ascent + descent) / 2)
21693 + yoff);
21694 }
21695
21696 cmp->offsets[i * 2] = left;
21697 cmp->offsets[i * 2 + 1] = btm + descent;
21698
21699 /* Update the bounding box of the overall glyphs. */
21700 if (width > 0)
21701 {
21702 right = left + width;
21703 if (left < leftmost)
21704 leftmost = left;
21705 if (right > rightmost)
21706 rightmost = right;
21707 }
21708 top = btm + descent + ascent;
21709 if (top > highest)
21710 highest = top;
21711 if (btm < lowest)
21712 lowest = btm;
21713
21714 if (cmp->lbearing > left + lbearing)
21715 cmp->lbearing = left + lbearing;
21716 if (cmp->rbearing < left + rbearing)
21717 cmp->rbearing = left + rbearing;
21718 }
21719 }
21720
21721 /* If there are glyphs whose x-offsets are negative,
21722 shift all glyphs to the right and make all x-offsets
21723 non-negative. */
21724 if (leftmost < 0)
21725 {
21726 for (i = 0; i < cmp->glyph_len; i++)
21727 cmp->offsets[i * 2] -= leftmost;
21728 rightmost -= leftmost;
21729 cmp->lbearing -= leftmost;
21730 cmp->rbearing -= leftmost;
21731 }
21732
21733 if (left_padded && cmp->lbearing < 0)
21734 {
21735 for (i = 0; i < cmp->glyph_len; i++)
21736 cmp->offsets[i * 2] -= cmp->lbearing;
21737 rightmost -= cmp->lbearing;
21738 cmp->rbearing -= cmp->lbearing;
21739 cmp->lbearing = 0;
21740 }
21741 if (right_padded && rightmost < cmp->rbearing)
21742 {
21743 rightmost = cmp->rbearing;
21744 }
21745
21746 cmp->pixel_width = rightmost;
21747 cmp->ascent = highest;
21748 cmp->descent = - lowest;
21749 if (cmp->ascent < font_ascent)
21750 cmp->ascent = font_ascent;
21751 if (cmp->descent < font_descent)
21752 cmp->descent = font_descent;
21753 }
21754
21755 if (it->glyph_row
21756 && (cmp->lbearing < 0
21757 || cmp->rbearing > cmp->pixel_width))
21758 it->glyph_row->contains_overlapping_glyphs_p = 1;
21759
21760 it->pixel_width = cmp->pixel_width;
21761 it->ascent = it->phys_ascent = cmp->ascent;
21762 it->descent = it->phys_descent = cmp->descent;
21763 if (face->box != FACE_NO_BOX)
21764 {
21765 int thick = face->box_line_width;
21766
21767 if (thick > 0)
21768 {
21769 it->ascent += thick;
21770 it->descent += thick;
21771 }
21772 else
21773 thick = - thick;
21774
21775 if (it->start_of_box_run_p)
21776 it->pixel_width += thick;
21777 if (it->end_of_box_run_p)
21778 it->pixel_width += thick;
21779 }
21780
21781 /* If face has an overline, add the height of the overline
21782 (1 pixel) and a 1 pixel margin to the character height. */
21783 if (face->overline_p)
21784 it->ascent += overline_margin;
21785
21786 take_vertical_position_into_account (it);
21787 if (it->ascent < 0)
21788 it->ascent = 0;
21789 if (it->descent < 0)
21790 it->descent = 0;
21791
21792 if (it->glyph_row)
21793 append_composite_glyph (it);
21794 }
21795 else if (it->what == IT_COMPOSITION)
21796 {
21797 /* A dynamic (automatic) composition. */
21798 struct face *face = FACE_FROM_ID (it->f, it->face_id);
21799 Lisp_Object gstring;
21800 struct font_metrics metrics;
21801
21802 gstring = composition_gstring_from_id (it->cmp_it.id);
21803 it->pixel_width
21804 = composition_gstring_width (gstring, it->cmp_it.from, it->cmp_it.to,
21805 &metrics);
21806 if (it->glyph_row
21807 && (metrics.lbearing < 0 || metrics.rbearing > metrics.width))
21808 it->glyph_row->contains_overlapping_glyphs_p = 1;
21809 it->ascent = it->phys_ascent = metrics.ascent;
21810 it->descent = it->phys_descent = metrics.descent;
21811 if (face->box != FACE_NO_BOX)
21812 {
21813 int thick = face->box_line_width;
21814
21815 if (thick > 0)
21816 {
21817 it->ascent += thick;
21818 it->descent += thick;
21819 }
21820 else
21821 thick = - thick;
21822
21823 if (it->start_of_box_run_p)
21824 it->pixel_width += thick;
21825 if (it->end_of_box_run_p)
21826 it->pixel_width += thick;
21827 }
21828 /* If face has an overline, add the height of the overline
21829 (1 pixel) and a 1 pixel margin to the character height. */
21830 if (face->overline_p)
21831 it->ascent += overline_margin;
21832 take_vertical_position_into_account (it);
21833 if (it->ascent < 0)
21834 it->ascent = 0;
21835 if (it->descent < 0)
21836 it->descent = 0;
21837
21838 if (it->glyph_row)
21839 append_composite_glyph (it);
21840 }
21841 else if (it->what == IT_IMAGE)
21842 produce_image_glyph (it);
21843 else if (it->what == IT_STRETCH)
21844 produce_stretch_glyph (it);
21845
21846 /* Accumulate dimensions. Note: can't assume that it->descent > 0
21847 because this isn't true for images with `:ascent 100'. */
21848 xassert (it->ascent >= 0 && it->descent >= 0);
21849 if (it->area == TEXT_AREA)
21850 it->current_x += it->pixel_width;
21851
21852 if (extra_line_spacing > 0)
21853 {
21854 it->descent += extra_line_spacing;
21855 if (extra_line_spacing > it->max_extra_line_spacing)
21856 it->max_extra_line_spacing = extra_line_spacing;
21857 }
21858
21859 it->max_ascent = max (it->max_ascent, it->ascent);
21860 it->max_descent = max (it->max_descent, it->descent);
21861 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
21862 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
21863 }
21864
21865 /* EXPORT for RIF:
21866 Output LEN glyphs starting at START at the nominal cursor position.
21867 Advance the nominal cursor over the text. The global variable
21868 updated_window contains the window being updated, updated_row is
21869 the glyph row being updated, and updated_area is the area of that
21870 row being updated. */
21871
21872 void
21873 x_write_glyphs (start, len)
21874 struct glyph *start;
21875 int len;
21876 {
21877 int x, hpos;
21878
21879 xassert (updated_window && updated_row);
21880 BLOCK_INPUT;
21881
21882 /* Write glyphs. */
21883
21884 hpos = start - updated_row->glyphs[updated_area];
21885 x = draw_glyphs (updated_window, output_cursor.x,
21886 updated_row, updated_area,
21887 hpos, hpos + len,
21888 DRAW_NORMAL_TEXT, 0);
21889
21890 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
21891 if (updated_area == TEXT_AREA
21892 && updated_window->phys_cursor_on_p
21893 && updated_window->phys_cursor.vpos == output_cursor.vpos
21894 && updated_window->phys_cursor.hpos >= hpos
21895 && updated_window->phys_cursor.hpos < hpos + len)
21896 updated_window->phys_cursor_on_p = 0;
21897
21898 UNBLOCK_INPUT;
21899
21900 /* Advance the output cursor. */
21901 output_cursor.hpos += len;
21902 output_cursor.x = x;
21903 }
21904
21905
21906 /* EXPORT for RIF:
21907 Insert LEN glyphs from START at the nominal cursor position. */
21908
21909 void
21910 x_insert_glyphs (start, len)
21911 struct glyph *start;
21912 int len;
21913 {
21914 struct frame *f;
21915 struct window *w;
21916 int line_height, shift_by_width, shifted_region_width;
21917 struct glyph_row *row;
21918 struct glyph *glyph;
21919 int frame_x, frame_y;
21920 EMACS_INT hpos;
21921
21922 xassert (updated_window && updated_row);
21923 BLOCK_INPUT;
21924 w = updated_window;
21925 f = XFRAME (WINDOW_FRAME (w));
21926
21927 /* Get the height of the line we are in. */
21928 row = updated_row;
21929 line_height = row->height;
21930
21931 /* Get the width of the glyphs to insert. */
21932 shift_by_width = 0;
21933 for (glyph = start; glyph < start + len; ++glyph)
21934 shift_by_width += glyph->pixel_width;
21935
21936 /* Get the width of the region to shift right. */
21937 shifted_region_width = (window_box_width (w, updated_area)
21938 - output_cursor.x
21939 - shift_by_width);
21940
21941 /* Shift right. */
21942 frame_x = window_box_left (w, updated_area) + output_cursor.x;
21943 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
21944
21945 FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
21946 line_height, shift_by_width);
21947
21948 /* Write the glyphs. */
21949 hpos = start - row->glyphs[updated_area];
21950 draw_glyphs (w, output_cursor.x, row, updated_area,
21951 hpos, hpos + len,
21952 DRAW_NORMAL_TEXT, 0);
21953
21954 /* Advance the output cursor. */
21955 output_cursor.hpos += len;
21956 output_cursor.x += shift_by_width;
21957 UNBLOCK_INPUT;
21958 }
21959
21960
21961 /* EXPORT for RIF:
21962 Erase the current text line from the nominal cursor position
21963 (inclusive) to pixel column TO_X (exclusive). The idea is that
21964 everything from TO_X onward is already erased.
21965
21966 TO_X is a pixel position relative to updated_area of
21967 updated_window. TO_X == -1 means clear to the end of this area. */
21968
21969 void
21970 x_clear_end_of_line (to_x)
21971 int to_x;
21972 {
21973 struct frame *f;
21974 struct window *w = updated_window;
21975 int max_x, min_y, max_y;
21976 int from_x, from_y, to_y;
21977
21978 xassert (updated_window && updated_row);
21979 f = XFRAME (w->frame);
21980
21981 if (updated_row->full_width_p)
21982 max_x = WINDOW_TOTAL_WIDTH (w);
21983 else
21984 max_x = window_box_width (w, updated_area);
21985 max_y = window_text_bottom_y (w);
21986
21987 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
21988 of window. For TO_X > 0, truncate to end of drawing area. */
21989 if (to_x == 0)
21990 return;
21991 else if (to_x < 0)
21992 to_x = max_x;
21993 else
21994 to_x = min (to_x, max_x);
21995
21996 to_y = min (max_y, output_cursor.y + updated_row->height);
21997
21998 /* Notice if the cursor will be cleared by this operation. */
21999 if (!updated_row->full_width_p)
22000 notice_overwritten_cursor (w, updated_area,
22001 output_cursor.x, -1,
22002 updated_row->y,
22003 MATRIX_ROW_BOTTOM_Y (updated_row));
22004
22005 from_x = output_cursor.x;
22006
22007 /* Translate to frame coordinates. */
22008 if (updated_row->full_width_p)
22009 {
22010 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
22011 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
22012 }
22013 else
22014 {
22015 int area_left = window_box_left (w, updated_area);
22016 from_x += area_left;
22017 to_x += area_left;
22018 }
22019
22020 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
22021 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
22022 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
22023
22024 /* Prevent inadvertently clearing to end of the X window. */
22025 if (to_x > from_x && to_y > from_y)
22026 {
22027 BLOCK_INPUT;
22028 FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
22029 to_x - from_x, to_y - from_y);
22030 UNBLOCK_INPUT;
22031 }
22032 }
22033
22034 #endif /* HAVE_WINDOW_SYSTEM */
22035
22036
22037 \f
22038 /***********************************************************************
22039 Cursor types
22040 ***********************************************************************/
22041
22042 /* Value is the internal representation of the specified cursor type
22043 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
22044 of the bar cursor. */
22045
22046 static enum text_cursor_kinds
22047 get_specified_cursor_type (arg, width)
22048 Lisp_Object arg;
22049 int *width;
22050 {
22051 enum text_cursor_kinds type;
22052
22053 if (NILP (arg))
22054 return NO_CURSOR;
22055
22056 if (EQ (arg, Qbox))
22057 return FILLED_BOX_CURSOR;
22058
22059 if (EQ (arg, Qhollow))
22060 return HOLLOW_BOX_CURSOR;
22061
22062 if (EQ (arg, Qbar))
22063 {
22064 *width = 2;
22065 return BAR_CURSOR;
22066 }
22067
22068 if (CONSP (arg)
22069 && EQ (XCAR (arg), Qbar)
22070 && INTEGERP (XCDR (arg))
22071 && XINT (XCDR (arg)) >= 0)
22072 {
22073 *width = XINT (XCDR (arg));
22074 return BAR_CURSOR;
22075 }
22076
22077 if (EQ (arg, Qhbar))
22078 {
22079 *width = 2;
22080 return HBAR_CURSOR;
22081 }
22082
22083 if (CONSP (arg)
22084 && EQ (XCAR (arg), Qhbar)
22085 && INTEGERP (XCDR (arg))
22086 && XINT (XCDR (arg)) >= 0)
22087 {
22088 *width = XINT (XCDR (arg));
22089 return HBAR_CURSOR;
22090 }
22091
22092 /* Treat anything unknown as "hollow box cursor".
22093 It was bad to signal an error; people have trouble fixing
22094 .Xdefaults with Emacs, when it has something bad in it. */
22095 type = HOLLOW_BOX_CURSOR;
22096
22097 return type;
22098 }
22099
22100 /* Set the default cursor types for specified frame. */
22101 void
22102 set_frame_cursor_types (f, arg)
22103 struct frame *f;
22104 Lisp_Object arg;
22105 {
22106 int width;
22107 Lisp_Object tem;
22108
22109 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
22110 FRAME_CURSOR_WIDTH (f) = width;
22111
22112 /* By default, set up the blink-off state depending on the on-state. */
22113
22114 tem = Fassoc (arg, Vblink_cursor_alist);
22115 if (!NILP (tem))
22116 {
22117 FRAME_BLINK_OFF_CURSOR (f)
22118 = get_specified_cursor_type (XCDR (tem), &width);
22119 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
22120 }
22121 else
22122 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
22123 }
22124
22125
22126 /* Return the cursor we want to be displayed in window W. Return
22127 width of bar/hbar cursor through WIDTH arg. Return with
22128 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
22129 (i.e. if the `system caret' should track this cursor).
22130
22131 In a mini-buffer window, we want the cursor only to appear if we
22132 are reading input from this window. For the selected window, we
22133 want the cursor type given by the frame parameter or buffer local
22134 setting of cursor-type. If explicitly marked off, draw no cursor.
22135 In all other cases, we want a hollow box cursor. */
22136
22137 static enum text_cursor_kinds
22138 get_window_cursor_type (w, glyph, width, active_cursor)
22139 struct window *w;
22140 struct glyph *glyph;
22141 int *width;
22142 int *active_cursor;
22143 {
22144 struct frame *f = XFRAME (w->frame);
22145 struct buffer *b = XBUFFER (w->buffer);
22146 int cursor_type = DEFAULT_CURSOR;
22147 Lisp_Object alt_cursor;
22148 int non_selected = 0;
22149
22150 *active_cursor = 1;
22151
22152 /* Echo area */
22153 if (cursor_in_echo_area
22154 && FRAME_HAS_MINIBUF_P (f)
22155 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
22156 {
22157 if (w == XWINDOW (echo_area_window))
22158 {
22159 if (EQ (b->cursor_type, Qt) || NILP (b->cursor_type))
22160 {
22161 *width = FRAME_CURSOR_WIDTH (f);
22162 return FRAME_DESIRED_CURSOR (f);
22163 }
22164 else
22165 return get_specified_cursor_type (b->cursor_type, width);
22166 }
22167
22168 *active_cursor = 0;
22169 non_selected = 1;
22170 }
22171
22172 /* Detect a nonselected window or nonselected frame. */
22173 else if (w != XWINDOW (f->selected_window)
22174 #ifdef HAVE_WINDOW_SYSTEM
22175 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame
22176 #endif
22177 )
22178 {
22179 *active_cursor = 0;
22180
22181 if (MINI_WINDOW_P (w) && minibuf_level == 0)
22182 return NO_CURSOR;
22183
22184 non_selected = 1;
22185 }
22186
22187 /* Never display a cursor in a window in which cursor-type is nil. */
22188 if (NILP (b->cursor_type))
22189 return NO_CURSOR;
22190
22191 /* Get the normal cursor type for this window. */
22192 if (EQ (b->cursor_type, Qt))
22193 {
22194 cursor_type = FRAME_DESIRED_CURSOR (f);
22195 *width = FRAME_CURSOR_WIDTH (f);
22196 }
22197 else
22198 cursor_type = get_specified_cursor_type (b->cursor_type, width);
22199
22200 /* Use cursor-in-non-selected-windows instead
22201 for non-selected window or frame. */
22202 if (non_selected)
22203 {
22204 alt_cursor = b->cursor_in_non_selected_windows;
22205 if (!EQ (Qt, alt_cursor))
22206 return get_specified_cursor_type (alt_cursor, width);
22207 /* t means modify the normal cursor type. */
22208 if (cursor_type == FILLED_BOX_CURSOR)
22209 cursor_type = HOLLOW_BOX_CURSOR;
22210 else if (cursor_type == BAR_CURSOR && *width > 1)
22211 --*width;
22212 return cursor_type;
22213 }
22214
22215 /* Use normal cursor if not blinked off. */
22216 if (!w->cursor_off_p)
22217 {
22218 #ifdef HAVE_WINDOW_SYSTEM
22219 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
22220 {
22221 if (cursor_type == FILLED_BOX_CURSOR)
22222 {
22223 /* Using a block cursor on large images can be very annoying.
22224 So use a hollow cursor for "large" images.
22225 If image is not transparent (no mask), also use hollow cursor. */
22226 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
22227 if (img != NULL && IMAGEP (img->spec))
22228 {
22229 /* Arbitrarily, interpret "Large" as >32x32 and >NxN
22230 where N = size of default frame font size.
22231 This should cover most of the "tiny" icons people may use. */
22232 if (!img->mask
22233 || img->width > max (32, WINDOW_FRAME_COLUMN_WIDTH (w))
22234 || img->height > max (32, WINDOW_FRAME_LINE_HEIGHT (w)))
22235 cursor_type = HOLLOW_BOX_CURSOR;
22236 }
22237 }
22238 else if (cursor_type != NO_CURSOR)
22239 {
22240 /* Display current only supports BOX and HOLLOW cursors for images.
22241 So for now, unconditionally use a HOLLOW cursor when cursor is
22242 not a solid box cursor. */
22243 cursor_type = HOLLOW_BOX_CURSOR;
22244 }
22245 }
22246 #endif
22247 return cursor_type;
22248 }
22249
22250 /* Cursor is blinked off, so determine how to "toggle" it. */
22251
22252 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
22253 if ((alt_cursor = Fassoc (b->cursor_type, Vblink_cursor_alist), !NILP (alt_cursor)))
22254 return get_specified_cursor_type (XCDR (alt_cursor), width);
22255
22256 /* Then see if frame has specified a specific blink off cursor type. */
22257 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
22258 {
22259 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
22260 return FRAME_BLINK_OFF_CURSOR (f);
22261 }
22262
22263 #if 0
22264 /* Some people liked having a permanently visible blinking cursor,
22265 while others had very strong opinions against it. So it was
22266 decided to remove it. KFS 2003-09-03 */
22267
22268 /* Finally perform built-in cursor blinking:
22269 filled box <-> hollow box
22270 wide [h]bar <-> narrow [h]bar
22271 narrow [h]bar <-> no cursor
22272 other type <-> no cursor */
22273
22274 if (cursor_type == FILLED_BOX_CURSOR)
22275 return HOLLOW_BOX_CURSOR;
22276
22277 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
22278 {
22279 *width = 1;
22280 return cursor_type;
22281 }
22282 #endif
22283
22284 return NO_CURSOR;
22285 }
22286
22287
22288 #ifdef HAVE_WINDOW_SYSTEM
22289
22290 /* Notice when the text cursor of window W has been completely
22291 overwritten by a drawing operation that outputs glyphs in AREA
22292 starting at X0 and ending at X1 in the line starting at Y0 and
22293 ending at Y1. X coordinates are area-relative. X1 < 0 means all
22294 the rest of the line after X0 has been written. Y coordinates
22295 are window-relative. */
22296
22297 static void
22298 notice_overwritten_cursor (w, area, x0, x1, y0, y1)
22299 struct window *w;
22300 enum glyph_row_area area;
22301 int x0, y0, x1, y1;
22302 {
22303 int cx0, cx1, cy0, cy1;
22304 struct glyph_row *row;
22305
22306 if (!w->phys_cursor_on_p)
22307 return;
22308 if (area != TEXT_AREA)
22309 return;
22310
22311 if (w->phys_cursor.vpos < 0
22312 || w->phys_cursor.vpos >= w->current_matrix->nrows
22313 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
22314 !(row->enabled_p && row->displays_text_p)))
22315 return;
22316
22317 if (row->cursor_in_fringe_p)
22318 {
22319 row->cursor_in_fringe_p = 0;
22320 draw_fringe_bitmap (w, row, 0);
22321 w->phys_cursor_on_p = 0;
22322 return;
22323 }
22324
22325 cx0 = w->phys_cursor.x;
22326 cx1 = cx0 + w->phys_cursor_width;
22327 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
22328 return;
22329
22330 /* The cursor image will be completely removed from the
22331 screen if the output area intersects the cursor area in
22332 y-direction. When we draw in [y0 y1[, and some part of
22333 the cursor is at y < y0, that part must have been drawn
22334 before. When scrolling, the cursor is erased before
22335 actually scrolling, so we don't come here. When not
22336 scrolling, the rows above the old cursor row must have
22337 changed, and in this case these rows must have written
22338 over the cursor image.
22339
22340 Likewise if part of the cursor is below y1, with the
22341 exception of the cursor being in the first blank row at
22342 the buffer and window end because update_text_area
22343 doesn't draw that row. (Except when it does, but
22344 that's handled in update_text_area.) */
22345
22346 cy0 = w->phys_cursor.y;
22347 cy1 = cy0 + w->phys_cursor_height;
22348 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
22349 return;
22350
22351 w->phys_cursor_on_p = 0;
22352 }
22353
22354 #endif /* HAVE_WINDOW_SYSTEM */
22355
22356 \f
22357 /************************************************************************
22358 Mouse Face
22359 ************************************************************************/
22360
22361 #ifdef HAVE_WINDOW_SYSTEM
22362
22363 /* EXPORT for RIF:
22364 Fix the display of area AREA of overlapping row ROW in window W
22365 with respect to the overlapping part OVERLAPS. */
22366
22367 void
22368 x_fix_overlapping_area (w, row, area, overlaps)
22369 struct window *w;
22370 struct glyph_row *row;
22371 enum glyph_row_area area;
22372 int overlaps;
22373 {
22374 int i, x;
22375
22376 BLOCK_INPUT;
22377
22378 x = 0;
22379 for (i = 0; i < row->used[area];)
22380 {
22381 if (row->glyphs[area][i].overlaps_vertically_p)
22382 {
22383 int start = i, start_x = x;
22384
22385 do
22386 {
22387 x += row->glyphs[area][i].pixel_width;
22388 ++i;
22389 }
22390 while (i < row->used[area]
22391 && row->glyphs[area][i].overlaps_vertically_p);
22392
22393 draw_glyphs (w, start_x, row, area,
22394 start, i,
22395 DRAW_NORMAL_TEXT, overlaps);
22396 }
22397 else
22398 {
22399 x += row->glyphs[area][i].pixel_width;
22400 ++i;
22401 }
22402 }
22403
22404 UNBLOCK_INPUT;
22405 }
22406
22407
22408 /* EXPORT:
22409 Draw the cursor glyph of window W in glyph row ROW. See the
22410 comment of draw_glyphs for the meaning of HL. */
22411
22412 void
22413 draw_phys_cursor_glyph (w, row, hl)
22414 struct window *w;
22415 struct glyph_row *row;
22416 enum draw_glyphs_face hl;
22417 {
22418 /* If cursor hpos is out of bounds, don't draw garbage. This can
22419 happen in mini-buffer windows when switching between echo area
22420 glyphs and mini-buffer. */
22421 if (w->phys_cursor.hpos < row->used[TEXT_AREA])
22422 {
22423 int on_p = w->phys_cursor_on_p;
22424 int x1;
22425 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA,
22426 w->phys_cursor.hpos, w->phys_cursor.hpos + 1,
22427 hl, 0);
22428 w->phys_cursor_on_p = on_p;
22429
22430 if (hl == DRAW_CURSOR)
22431 w->phys_cursor_width = x1 - w->phys_cursor.x;
22432 /* When we erase the cursor, and ROW is overlapped by other
22433 rows, make sure that these overlapping parts of other rows
22434 are redrawn. */
22435 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
22436 {
22437 w->phys_cursor_width = x1 - w->phys_cursor.x;
22438
22439 if (row > w->current_matrix->rows
22440 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
22441 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
22442 OVERLAPS_ERASED_CURSOR);
22443
22444 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
22445 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
22446 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
22447 OVERLAPS_ERASED_CURSOR);
22448 }
22449 }
22450 }
22451
22452
22453 /* EXPORT:
22454 Erase the image of a cursor of window W from the screen. */
22455
22456 void
22457 erase_phys_cursor (w)
22458 struct window *w;
22459 {
22460 struct frame *f = XFRAME (w->frame);
22461 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
22462 int hpos = w->phys_cursor.hpos;
22463 int vpos = w->phys_cursor.vpos;
22464 int mouse_face_here_p = 0;
22465 struct glyph_matrix *active_glyphs = w->current_matrix;
22466 struct glyph_row *cursor_row;
22467 struct glyph *cursor_glyph;
22468 enum draw_glyphs_face hl;
22469
22470 /* No cursor displayed or row invalidated => nothing to do on the
22471 screen. */
22472 if (w->phys_cursor_type == NO_CURSOR)
22473 goto mark_cursor_off;
22474
22475 /* VPOS >= active_glyphs->nrows means that window has been resized.
22476 Don't bother to erase the cursor. */
22477 if (vpos >= active_glyphs->nrows)
22478 goto mark_cursor_off;
22479
22480 /* If row containing cursor is marked invalid, there is nothing we
22481 can do. */
22482 cursor_row = MATRIX_ROW (active_glyphs, vpos);
22483 if (!cursor_row->enabled_p)
22484 goto mark_cursor_off;
22485
22486 /* If line spacing is > 0, old cursor may only be partially visible in
22487 window after split-window. So adjust visible height. */
22488 cursor_row->visible_height = min (cursor_row->visible_height,
22489 window_text_bottom_y (w) - cursor_row->y);
22490
22491 /* If row is completely invisible, don't attempt to delete a cursor which
22492 isn't there. This can happen if cursor is at top of a window, and
22493 we switch to a buffer with a header line in that window. */
22494 if (cursor_row->visible_height <= 0)
22495 goto mark_cursor_off;
22496
22497 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
22498 if (cursor_row->cursor_in_fringe_p)
22499 {
22500 cursor_row->cursor_in_fringe_p = 0;
22501 draw_fringe_bitmap (w, cursor_row, 0);
22502 goto mark_cursor_off;
22503 }
22504
22505 /* This can happen when the new row is shorter than the old one.
22506 In this case, either draw_glyphs or clear_end_of_line
22507 should have cleared the cursor. Note that we wouldn't be
22508 able to erase the cursor in this case because we don't have a
22509 cursor glyph at hand. */
22510 if (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])
22511 goto mark_cursor_off;
22512
22513 /* If the cursor is in the mouse face area, redisplay that when
22514 we clear the cursor. */
22515 if (! NILP (dpyinfo->mouse_face_window)
22516 && w == XWINDOW (dpyinfo->mouse_face_window)
22517 && (vpos > dpyinfo->mouse_face_beg_row
22518 || (vpos == dpyinfo->mouse_face_beg_row
22519 && hpos >= dpyinfo->mouse_face_beg_col))
22520 && (vpos < dpyinfo->mouse_face_end_row
22521 || (vpos == dpyinfo->mouse_face_end_row
22522 && hpos < dpyinfo->mouse_face_end_col))
22523 /* Don't redraw the cursor's spot in mouse face if it is at the
22524 end of a line (on a newline). The cursor appears there, but
22525 mouse highlighting does not. */
22526 && cursor_row->used[TEXT_AREA] > hpos)
22527 mouse_face_here_p = 1;
22528
22529 /* Maybe clear the display under the cursor. */
22530 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
22531 {
22532 int x, y, left_x;
22533 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
22534 int width;
22535
22536 cursor_glyph = get_phys_cursor_glyph (w);
22537 if (cursor_glyph == NULL)
22538 goto mark_cursor_off;
22539
22540 width = cursor_glyph->pixel_width;
22541 left_x = window_box_left_offset (w, TEXT_AREA);
22542 x = w->phys_cursor.x;
22543 if (x < left_x)
22544 width -= left_x - x;
22545 width = min (width, window_box_width (w, TEXT_AREA) - x);
22546 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
22547 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, max (x, left_x));
22548
22549 if (width > 0)
22550 FRAME_RIF (f)->clear_frame_area (f, x, y, width, cursor_row->visible_height);
22551 }
22552
22553 /* Erase the cursor by redrawing the character underneath it. */
22554 if (mouse_face_here_p)
22555 hl = DRAW_MOUSE_FACE;
22556 else
22557 hl = DRAW_NORMAL_TEXT;
22558 draw_phys_cursor_glyph (w, cursor_row, hl);
22559
22560 mark_cursor_off:
22561 w->phys_cursor_on_p = 0;
22562 w->phys_cursor_type = NO_CURSOR;
22563 }
22564
22565
22566 /* EXPORT:
22567 Display or clear cursor of window W. If ON is zero, clear the
22568 cursor. If it is non-zero, display the cursor. If ON is nonzero,
22569 where to put the cursor is specified by HPOS, VPOS, X and Y. */
22570
22571 void
22572 display_and_set_cursor (w, on, hpos, vpos, x, y)
22573 struct window *w;
22574 int on, hpos, vpos, x, y;
22575 {
22576 struct frame *f = XFRAME (w->frame);
22577 int new_cursor_type;
22578 int new_cursor_width;
22579 int active_cursor;
22580 struct glyph_row *glyph_row;
22581 struct glyph *glyph;
22582
22583 /* This is pointless on invisible frames, and dangerous on garbaged
22584 windows and frames; in the latter case, the frame or window may
22585 be in the midst of changing its size, and x and y may be off the
22586 window. */
22587 if (! FRAME_VISIBLE_P (f)
22588 || FRAME_GARBAGED_P (f)
22589 || vpos >= w->current_matrix->nrows
22590 || hpos >= w->current_matrix->matrix_w)
22591 return;
22592
22593 /* If cursor is off and we want it off, return quickly. */
22594 if (!on && !w->phys_cursor_on_p)
22595 return;
22596
22597 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
22598 /* If cursor row is not enabled, we don't really know where to
22599 display the cursor. */
22600 if (!glyph_row->enabled_p)
22601 {
22602 w->phys_cursor_on_p = 0;
22603 return;
22604 }
22605
22606 glyph = NULL;
22607 if (!glyph_row->exact_window_width_line_p
22608 || hpos < glyph_row->used[TEXT_AREA])
22609 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
22610
22611 xassert (interrupt_input_blocked);
22612
22613 /* Set new_cursor_type to the cursor we want to be displayed. */
22614 new_cursor_type = get_window_cursor_type (w, glyph,
22615 &new_cursor_width, &active_cursor);
22616
22617 /* If cursor is currently being shown and we don't want it to be or
22618 it is in the wrong place, or the cursor type is not what we want,
22619 erase it. */
22620 if (w->phys_cursor_on_p
22621 && (!on
22622 || w->phys_cursor.x != x
22623 || w->phys_cursor.y != y
22624 || new_cursor_type != w->phys_cursor_type
22625 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
22626 && new_cursor_width != w->phys_cursor_width)))
22627 erase_phys_cursor (w);
22628
22629 /* Don't check phys_cursor_on_p here because that flag is only set
22630 to zero in some cases where we know that the cursor has been
22631 completely erased, to avoid the extra work of erasing the cursor
22632 twice. In other words, phys_cursor_on_p can be 1 and the cursor
22633 still not be visible, or it has only been partly erased. */
22634 if (on)
22635 {
22636 w->phys_cursor_ascent = glyph_row->ascent;
22637 w->phys_cursor_height = glyph_row->height;
22638
22639 /* Set phys_cursor_.* before x_draw_.* is called because some
22640 of them may need the information. */
22641 w->phys_cursor.x = x;
22642 w->phys_cursor.y = glyph_row->y;
22643 w->phys_cursor.hpos = hpos;
22644 w->phys_cursor.vpos = vpos;
22645 }
22646
22647 FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
22648 new_cursor_type, new_cursor_width,
22649 on, active_cursor);
22650 }
22651
22652
22653 /* Switch the display of W's cursor on or off, according to the value
22654 of ON. */
22655
22656 #ifndef HAVE_NS
22657 static
22658 #endif
22659 void
22660 update_window_cursor (w, on)
22661 struct window *w;
22662 int on;
22663 {
22664 /* Don't update cursor in windows whose frame is in the process
22665 of being deleted. */
22666 if (w->current_matrix)
22667 {
22668 BLOCK_INPUT;
22669 display_and_set_cursor (w, on, w->phys_cursor.hpos, w->phys_cursor.vpos,
22670 w->phys_cursor.x, w->phys_cursor.y);
22671 UNBLOCK_INPUT;
22672 }
22673 }
22674
22675
22676 /* Call update_window_cursor with parameter ON_P on all leaf windows
22677 in the window tree rooted at W. */
22678
22679 static void
22680 update_cursor_in_window_tree (w, on_p)
22681 struct window *w;
22682 int on_p;
22683 {
22684 while (w)
22685 {
22686 if (!NILP (w->hchild))
22687 update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
22688 else if (!NILP (w->vchild))
22689 update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
22690 else
22691 update_window_cursor (w, on_p);
22692
22693 w = NILP (w->next) ? 0 : XWINDOW (w->next);
22694 }
22695 }
22696
22697
22698 /* EXPORT:
22699 Display the cursor on window W, or clear it, according to ON_P.
22700 Don't change the cursor's position. */
22701
22702 void
22703 x_update_cursor (f, on_p)
22704 struct frame *f;
22705 int on_p;
22706 {
22707 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
22708 }
22709
22710
22711 /* EXPORT:
22712 Clear the cursor of window W to background color, and mark the
22713 cursor as not shown. This is used when the text where the cursor
22714 is about to be rewritten. */
22715
22716 void
22717 x_clear_cursor (w)
22718 struct window *w;
22719 {
22720 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
22721 update_window_cursor (w, 0);
22722 }
22723
22724
22725 /* EXPORT:
22726 Display the active region described by mouse_face_* according to DRAW. */
22727
22728 void
22729 show_mouse_face (dpyinfo, draw)
22730 Display_Info *dpyinfo;
22731 enum draw_glyphs_face draw;
22732 {
22733 struct window *w = XWINDOW (dpyinfo->mouse_face_window);
22734 struct frame *f = XFRAME (WINDOW_FRAME (w));
22735
22736 if (/* If window is in the process of being destroyed, don't bother
22737 to do anything. */
22738 w->current_matrix != NULL
22739 /* Don't update mouse highlight if hidden */
22740 && (draw != DRAW_MOUSE_FACE || !dpyinfo->mouse_face_hidden)
22741 /* Recognize when we are called to operate on rows that don't exist
22742 anymore. This can happen when a window is split. */
22743 && dpyinfo->mouse_face_end_row < w->current_matrix->nrows)
22744 {
22745 int phys_cursor_on_p = w->phys_cursor_on_p;
22746 struct glyph_row *row, *first, *last;
22747
22748 first = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_beg_row);
22749 last = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_end_row);
22750
22751 for (row = first; row <= last && row->enabled_p; ++row)
22752 {
22753 int start_hpos, end_hpos, start_x;
22754
22755 /* For all but the first row, the highlight starts at column 0. */
22756 if (row == first)
22757 {
22758 start_hpos = dpyinfo->mouse_face_beg_col;
22759 start_x = dpyinfo->mouse_face_beg_x;
22760 }
22761 else
22762 {
22763 start_hpos = 0;
22764 start_x = 0;
22765 }
22766
22767 if (row == last)
22768 end_hpos = dpyinfo->mouse_face_end_col;
22769 else
22770 {
22771 end_hpos = row->used[TEXT_AREA];
22772 if (draw == DRAW_NORMAL_TEXT)
22773 row->fill_line_p = 1; /* Clear to end of line */
22774 }
22775
22776 if (end_hpos > start_hpos)
22777 {
22778 draw_glyphs (w, start_x, row, TEXT_AREA,
22779 start_hpos, end_hpos,
22780 draw, 0);
22781
22782 row->mouse_face_p
22783 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
22784 }
22785 }
22786
22787 /* When we've written over the cursor, arrange for it to
22788 be displayed again. */
22789 if (phys_cursor_on_p && !w->phys_cursor_on_p)
22790 {
22791 BLOCK_INPUT;
22792 display_and_set_cursor (w, 1,
22793 w->phys_cursor.hpos, w->phys_cursor.vpos,
22794 w->phys_cursor.x, w->phys_cursor.y);
22795 UNBLOCK_INPUT;
22796 }
22797 }
22798
22799 /* Change the mouse cursor. */
22800 if (draw == DRAW_NORMAL_TEXT && !EQ (dpyinfo->mouse_face_window, f->tool_bar_window))
22801 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
22802 else if (draw == DRAW_MOUSE_FACE)
22803 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
22804 else
22805 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
22806 }
22807
22808 /* EXPORT:
22809 Clear out the mouse-highlighted active region.
22810 Redraw it un-highlighted first. Value is non-zero if mouse
22811 face was actually drawn unhighlighted. */
22812
22813 int
22814 clear_mouse_face (dpyinfo)
22815 Display_Info *dpyinfo;
22816 {
22817 int cleared = 0;
22818
22819 if (!dpyinfo->mouse_face_hidden && !NILP (dpyinfo->mouse_face_window))
22820 {
22821 show_mouse_face (dpyinfo, DRAW_NORMAL_TEXT);
22822 cleared = 1;
22823 }
22824
22825 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
22826 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
22827 dpyinfo->mouse_face_window = Qnil;
22828 dpyinfo->mouse_face_overlay = Qnil;
22829 return cleared;
22830 }
22831
22832
22833 /* EXPORT:
22834 Non-zero if physical cursor of window W is within mouse face. */
22835
22836 int
22837 cursor_in_mouse_face_p (w)
22838 struct window *w;
22839 {
22840 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
22841 int in_mouse_face = 0;
22842
22843 if (WINDOWP (dpyinfo->mouse_face_window)
22844 && XWINDOW (dpyinfo->mouse_face_window) == w)
22845 {
22846 int hpos = w->phys_cursor.hpos;
22847 int vpos = w->phys_cursor.vpos;
22848
22849 if (vpos >= dpyinfo->mouse_face_beg_row
22850 && vpos <= dpyinfo->mouse_face_end_row
22851 && (vpos > dpyinfo->mouse_face_beg_row
22852 || hpos >= dpyinfo->mouse_face_beg_col)
22853 && (vpos < dpyinfo->mouse_face_end_row
22854 || hpos < dpyinfo->mouse_face_end_col
22855 || dpyinfo->mouse_face_past_end))
22856 in_mouse_face = 1;
22857 }
22858
22859 return in_mouse_face;
22860 }
22861
22862
22863
22864 \f
22865 /* This function sets the mouse_face_* elements of DPYINFO, assuming
22866 the mouse cursor is on a glyph with buffer charpos MOUSE_CHARPOS in
22867 window WINDOW. START_CHARPOS and END_CHARPOS are buffer positions
22868 for the overlay or run of text properties specifying the mouse
22869 face. BEFORE_STRING and AFTER_STRING, if non-nil, are a
22870 before-string and after-string that must also be highlighted.
22871 DISPLAY_STRING, if non-nil, is a display string that may cover some
22872 or all of the highlighted text. */
22873
22874 static void
22875 mouse_face_from_buffer_pos (Lisp_Object window,
22876 Display_Info *dpyinfo,
22877 EMACS_INT mouse_charpos,
22878 EMACS_INT start_charpos,
22879 EMACS_INT end_charpos,
22880 Lisp_Object before_string,
22881 Lisp_Object after_string,
22882 Lisp_Object display_string)
22883 {
22884 struct window *w = XWINDOW (window);
22885 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
22886 struct glyph_row *row;
22887 struct glyph *glyph, *end;
22888 EMACS_INT ignore;
22889 int x;
22890
22891 xassert (NILP (display_string) || STRINGP (display_string));
22892 xassert (NILP (before_string) || STRINGP (before_string));
22893 xassert (NILP (after_string) || STRINGP (after_string));
22894
22895 /* Find the first highlighted glyph. */
22896 if (start_charpos < MATRIX_ROW_START_CHARPOS (first))
22897 {
22898 dpyinfo->mouse_face_beg_col = 0;
22899 dpyinfo->mouse_face_beg_row = MATRIX_ROW_VPOS (first, w->current_matrix);
22900 dpyinfo->mouse_face_beg_x = first->x;
22901 dpyinfo->mouse_face_beg_y = first->y;
22902 }
22903 else
22904 {
22905 row = row_containing_pos (w, start_charpos, first, NULL, 0);
22906 if (row == NULL)
22907 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
22908
22909 /* If the before-string or display-string contains newlines,
22910 row_containing_pos skips to its last row. Move back. */
22911 if (!NILP (before_string) || !NILP (display_string))
22912 {
22913 struct glyph_row *prev;
22914 while ((prev = row - 1, prev >= first)
22915 && MATRIX_ROW_END_CHARPOS (prev) == start_charpos
22916 && prev->used[TEXT_AREA] > 0)
22917 {
22918 struct glyph *beg = prev->glyphs[TEXT_AREA];
22919 glyph = beg + prev->used[TEXT_AREA];
22920 while (--glyph >= beg && INTEGERP (glyph->object));
22921 if (glyph < beg
22922 || !(EQ (glyph->object, before_string)
22923 || EQ (glyph->object, display_string)))
22924 break;
22925 row = prev;
22926 }
22927 }
22928
22929 glyph = row->glyphs[TEXT_AREA];
22930 end = glyph + row->used[TEXT_AREA];
22931 x = row->x;
22932 dpyinfo->mouse_face_beg_y = row->y;
22933 dpyinfo->mouse_face_beg_row = MATRIX_ROW_VPOS (row, w->current_matrix);
22934
22935 /* Skip truncation glyphs at the start of the glyph row. */
22936 if (row->displays_text_p)
22937 for (; glyph < end
22938 && INTEGERP (glyph->object)
22939 && glyph->charpos < 0;
22940 ++glyph)
22941 x += glyph->pixel_width;
22942
22943 /* Scan the glyph row, stopping before BEFORE_STRING or
22944 DISPLAY_STRING or START_CHARPOS. */
22945 for (; glyph < end
22946 && !INTEGERP (glyph->object)
22947 && !EQ (glyph->object, before_string)
22948 && !EQ (glyph->object, display_string)
22949 && !(BUFFERP (glyph->object)
22950 && glyph->charpos >= start_charpos);
22951 ++glyph)
22952 x += glyph->pixel_width;
22953
22954 dpyinfo->mouse_face_beg_x = x;
22955 dpyinfo->mouse_face_beg_col = glyph - row->glyphs[TEXT_AREA];
22956 }
22957
22958 /* Find the last highlighted glyph. */
22959 row = row_containing_pos (w, end_charpos, first, NULL, 0);
22960 if (row == NULL)
22961 {
22962 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
22963 dpyinfo->mouse_face_past_end = 1;
22964 }
22965 else if (!NILP (after_string))
22966 {
22967 /* If the after-string has newlines, advance to its last row. */
22968 struct glyph_row *next;
22969 struct glyph_row *last
22970 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
22971
22972 for (next = row + 1;
22973 next <= last
22974 && next->used[TEXT_AREA] > 0
22975 && EQ (next->glyphs[TEXT_AREA]->object, after_string);
22976 ++next)
22977 row = next;
22978 }
22979
22980 glyph = row->glyphs[TEXT_AREA];
22981 end = glyph + row->used[TEXT_AREA];
22982 x = row->x;
22983 dpyinfo->mouse_face_end_y = row->y;
22984 dpyinfo->mouse_face_end_row = MATRIX_ROW_VPOS (row, w->current_matrix);
22985
22986 /* Skip truncation glyphs at the start of the row. */
22987 if (row->displays_text_p)
22988 for (; glyph < end
22989 && INTEGERP (glyph->object)
22990 && glyph->charpos < 0;
22991 ++glyph)
22992 x += glyph->pixel_width;
22993
22994 /* Scan the glyph row, stopping at END_CHARPOS or when we encounter
22995 AFTER_STRING. */
22996 for (; glyph < end
22997 && !INTEGERP (glyph->object)
22998 && !EQ (glyph->object, after_string)
22999 && !(BUFFERP (glyph->object) && glyph->charpos >= end_charpos);
23000 ++glyph)
23001 x += glyph->pixel_width;
23002
23003 /* If we found AFTER_STRING, consume it and stop. */
23004 if (EQ (glyph->object, after_string))
23005 {
23006 for (; EQ (glyph->object, after_string) && glyph < end; ++glyph)
23007 x += glyph->pixel_width;
23008 }
23009 else
23010 {
23011 /* If there's no after-string, we must check if we overshot,
23012 which might be the case if we stopped after a string glyph.
23013 That glyph may belong to a before-string or display-string
23014 associated with the end position, which must not be
23015 highlighted. */
23016 Lisp_Object prev_object;
23017 int pos;
23018
23019 while (glyph > row->glyphs[TEXT_AREA])
23020 {
23021 prev_object = (glyph - 1)->object;
23022 if (!STRINGP (prev_object) || EQ (prev_object, display_string))
23023 break;
23024
23025 pos = string_buffer_position (w, prev_object, end_charpos);
23026 if (pos && pos < end_charpos)
23027 break;
23028
23029 for (; glyph > row->glyphs[TEXT_AREA]
23030 && EQ ((glyph - 1)->object, prev_object);
23031 --glyph)
23032 x -= (glyph - 1)->pixel_width;
23033 }
23034 }
23035
23036 dpyinfo->mouse_face_end_x = x;
23037 dpyinfo->mouse_face_end_col = glyph - row->glyphs[TEXT_AREA];
23038 dpyinfo->mouse_face_window = window;
23039 dpyinfo->mouse_face_face_id
23040 = face_at_buffer_position (w, mouse_charpos, 0, 0, &ignore,
23041 mouse_charpos + 1,
23042 !dpyinfo->mouse_face_hidden, -1);
23043 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
23044 }
23045
23046
23047 /* Find the position of the glyph for position POS in OBJECT in
23048 window W's current matrix, and return in *X, *Y the pixel
23049 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
23050
23051 RIGHT_P non-zero means return the position of the right edge of the
23052 glyph, RIGHT_P zero means return the left edge position.
23053
23054 If no glyph for POS exists in the matrix, return the position of
23055 the glyph with the next smaller position that is in the matrix, if
23056 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
23057 exists in the matrix, return the position of the glyph with the
23058 next larger position in OBJECT.
23059
23060 Value is non-zero if a glyph was found. */
23061
23062 static int
23063 fast_find_string_pos (w, pos, object, hpos, vpos, x, y, right_p)
23064 struct window *w;
23065 EMACS_INT pos;
23066 Lisp_Object object;
23067 int *hpos, *vpos, *x, *y;
23068 int right_p;
23069 {
23070 int yb = window_text_bottom_y (w);
23071 struct glyph_row *r;
23072 struct glyph *best_glyph = NULL;
23073 struct glyph_row *best_row = NULL;
23074 int best_x = 0;
23075
23076 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
23077 r->enabled_p && r->y < yb;
23078 ++r)
23079 {
23080 struct glyph *g = r->glyphs[TEXT_AREA];
23081 struct glyph *e = g + r->used[TEXT_AREA];
23082 int gx;
23083
23084 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
23085 if (EQ (g->object, object))
23086 {
23087 if (g->charpos == pos)
23088 {
23089 best_glyph = g;
23090 best_x = gx;
23091 best_row = r;
23092 goto found;
23093 }
23094 else if (best_glyph == NULL
23095 || ((eabs (g->charpos - pos)
23096 < eabs (best_glyph->charpos - pos))
23097 && (right_p
23098 ? g->charpos < pos
23099 : g->charpos > pos)))
23100 {
23101 best_glyph = g;
23102 best_x = gx;
23103 best_row = r;
23104 }
23105 }
23106 }
23107
23108 found:
23109
23110 if (best_glyph)
23111 {
23112 *x = best_x;
23113 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
23114
23115 if (right_p)
23116 {
23117 *x += best_glyph->pixel_width;
23118 ++*hpos;
23119 }
23120
23121 *y = best_row->y;
23122 *vpos = best_row - w->current_matrix->rows;
23123 }
23124
23125 return best_glyph != NULL;
23126 }
23127
23128
23129 /* See if position X, Y is within a hot-spot of an image. */
23130
23131 static int
23132 on_hot_spot_p (hot_spot, x, y)
23133 Lisp_Object hot_spot;
23134 int x, y;
23135 {
23136 if (!CONSP (hot_spot))
23137 return 0;
23138
23139 if (EQ (XCAR (hot_spot), Qrect))
23140 {
23141 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
23142 Lisp_Object rect = XCDR (hot_spot);
23143 Lisp_Object tem;
23144 if (!CONSP (rect))
23145 return 0;
23146 if (!CONSP (XCAR (rect)))
23147 return 0;
23148 if (!CONSP (XCDR (rect)))
23149 return 0;
23150 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
23151 return 0;
23152 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
23153 return 0;
23154 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
23155 return 0;
23156 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
23157 return 0;
23158 return 1;
23159 }
23160 else if (EQ (XCAR (hot_spot), Qcircle))
23161 {
23162 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
23163 Lisp_Object circ = XCDR (hot_spot);
23164 Lisp_Object lr, lx0, ly0;
23165 if (CONSP (circ)
23166 && CONSP (XCAR (circ))
23167 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
23168 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
23169 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
23170 {
23171 double r = XFLOATINT (lr);
23172 double dx = XINT (lx0) - x;
23173 double dy = XINT (ly0) - y;
23174 return (dx * dx + dy * dy <= r * r);
23175 }
23176 }
23177 else if (EQ (XCAR (hot_spot), Qpoly))
23178 {
23179 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
23180 if (VECTORP (XCDR (hot_spot)))
23181 {
23182 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
23183 Lisp_Object *poly = v->contents;
23184 int n = v->size;
23185 int i;
23186 int inside = 0;
23187 Lisp_Object lx, ly;
23188 int x0, y0;
23189
23190 /* Need an even number of coordinates, and at least 3 edges. */
23191 if (n < 6 || n & 1)
23192 return 0;
23193
23194 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
23195 If count is odd, we are inside polygon. Pixels on edges
23196 may or may not be included depending on actual geometry of the
23197 polygon. */
23198 if ((lx = poly[n-2], !INTEGERP (lx))
23199 || (ly = poly[n-1], !INTEGERP (lx)))
23200 return 0;
23201 x0 = XINT (lx), y0 = XINT (ly);
23202 for (i = 0; i < n; i += 2)
23203 {
23204 int x1 = x0, y1 = y0;
23205 if ((lx = poly[i], !INTEGERP (lx))
23206 || (ly = poly[i+1], !INTEGERP (ly)))
23207 return 0;
23208 x0 = XINT (lx), y0 = XINT (ly);
23209
23210 /* Does this segment cross the X line? */
23211 if (x0 >= x)
23212 {
23213 if (x1 >= x)
23214 continue;
23215 }
23216 else if (x1 < x)
23217 continue;
23218 if (y > y0 && y > y1)
23219 continue;
23220 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
23221 inside = !inside;
23222 }
23223 return inside;
23224 }
23225 }
23226 return 0;
23227 }
23228
23229 Lisp_Object
23230 find_hot_spot (map, x, y)
23231 Lisp_Object map;
23232 int x, y;
23233 {
23234 while (CONSP (map))
23235 {
23236 if (CONSP (XCAR (map))
23237 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
23238 return XCAR (map);
23239 map = XCDR (map);
23240 }
23241
23242 return Qnil;
23243 }
23244
23245 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
23246 3, 3, 0,
23247 doc: /* Lookup in image map MAP coordinates X and Y.
23248 An image map is an alist where each element has the format (AREA ID PLIST).
23249 An AREA is specified as either a rectangle, a circle, or a polygon:
23250 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
23251 pixel coordinates of the upper left and bottom right corners.
23252 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
23253 and the radius of the circle; r may be a float or integer.
23254 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
23255 vector describes one corner in the polygon.
23256 Returns the alist element for the first matching AREA in MAP. */)
23257 (map, x, y)
23258 Lisp_Object map;
23259 Lisp_Object x, y;
23260 {
23261 if (NILP (map))
23262 return Qnil;
23263
23264 CHECK_NUMBER (x);
23265 CHECK_NUMBER (y);
23266
23267 return find_hot_spot (map, XINT (x), XINT (y));
23268 }
23269
23270
23271 /* Display frame CURSOR, optionally using shape defined by POINTER. */
23272 static void
23273 define_frame_cursor1 (f, cursor, pointer)
23274 struct frame *f;
23275 Cursor cursor;
23276 Lisp_Object pointer;
23277 {
23278 /* Do not change cursor shape while dragging mouse. */
23279 if (!NILP (do_mouse_tracking))
23280 return;
23281
23282 if (!NILP (pointer))
23283 {
23284 if (EQ (pointer, Qarrow))
23285 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
23286 else if (EQ (pointer, Qhand))
23287 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
23288 else if (EQ (pointer, Qtext))
23289 cursor = FRAME_X_OUTPUT (f)->text_cursor;
23290 else if (EQ (pointer, intern ("hdrag")))
23291 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
23292 #ifdef HAVE_X_WINDOWS
23293 else if (EQ (pointer, intern ("vdrag")))
23294 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
23295 #endif
23296 else if (EQ (pointer, intern ("hourglass")))
23297 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
23298 else if (EQ (pointer, Qmodeline))
23299 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
23300 else
23301 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
23302 }
23303
23304 if (cursor != No_Cursor)
23305 FRAME_RIF (f)->define_frame_cursor (f, cursor);
23306 }
23307
23308 /* Take proper action when mouse has moved to the mode or header line
23309 or marginal area AREA of window W, x-position X and y-position Y.
23310 X is relative to the start of the text display area of W, so the
23311 width of bitmap areas and scroll bars must be subtracted to get a
23312 position relative to the start of the mode line. */
23313
23314 static void
23315 note_mode_line_or_margin_highlight (window, x, y, area)
23316 Lisp_Object window;
23317 int x, y;
23318 enum window_part area;
23319 {
23320 struct window *w = XWINDOW (window);
23321 struct frame *f = XFRAME (w->frame);
23322 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
23323 Cursor cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
23324 Lisp_Object pointer = Qnil;
23325 int charpos, dx, dy, width, height;
23326 Lisp_Object string, object = Qnil;
23327 Lisp_Object pos, help;
23328
23329 Lisp_Object mouse_face;
23330 int original_x_pixel = x;
23331 struct glyph * glyph = NULL, * row_start_glyph = NULL;
23332 struct glyph_row *row;
23333
23334 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
23335 {
23336 int x0;
23337 struct glyph *end;
23338
23339 string = mode_line_string (w, area, &x, &y, &charpos,
23340 &object, &dx, &dy, &width, &height);
23341
23342 row = (area == ON_MODE_LINE
23343 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
23344 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
23345
23346 /* Find glyph */
23347 if (row->mode_line_p && row->enabled_p)
23348 {
23349 glyph = row_start_glyph = row->glyphs[TEXT_AREA];
23350 end = glyph + row->used[TEXT_AREA];
23351
23352 for (x0 = original_x_pixel;
23353 glyph < end && x0 >= glyph->pixel_width;
23354 ++glyph)
23355 x0 -= glyph->pixel_width;
23356
23357 if (glyph >= end)
23358 glyph = NULL;
23359 }
23360 }
23361 else
23362 {
23363 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
23364 string = marginal_area_string (w, area, &x, &y, &charpos,
23365 &object, &dx, &dy, &width, &height);
23366 }
23367
23368 help = Qnil;
23369
23370 if (IMAGEP (object))
23371 {
23372 Lisp_Object image_map, hotspot;
23373 if ((image_map = Fplist_get (XCDR (object), QCmap),
23374 !NILP (image_map))
23375 && (hotspot = find_hot_spot (image_map, dx, dy),
23376 CONSP (hotspot))
23377 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
23378 {
23379 Lisp_Object area_id, plist;
23380
23381 area_id = XCAR (hotspot);
23382 /* Could check AREA_ID to see if we enter/leave this hot-spot.
23383 If so, we could look for mouse-enter, mouse-leave
23384 properties in PLIST (and do something...). */
23385 hotspot = XCDR (hotspot);
23386 if (CONSP (hotspot)
23387 && (plist = XCAR (hotspot), CONSP (plist)))
23388 {
23389 pointer = Fplist_get (plist, Qpointer);
23390 if (NILP (pointer))
23391 pointer = Qhand;
23392 help = Fplist_get (plist, Qhelp_echo);
23393 if (!NILP (help))
23394 {
23395 help_echo_string = help;
23396 /* Is this correct? ++kfs */
23397 XSETWINDOW (help_echo_window, w);
23398 help_echo_object = w->buffer;
23399 help_echo_pos = charpos;
23400 }
23401 }
23402 }
23403 if (NILP (pointer))
23404 pointer = Fplist_get (XCDR (object), QCpointer);
23405 }
23406
23407 if (STRINGP (string))
23408 {
23409 pos = make_number (charpos);
23410 /* If we're on a string with `help-echo' text property, arrange
23411 for the help to be displayed. This is done by setting the
23412 global variable help_echo_string to the help string. */
23413 if (NILP (help))
23414 {
23415 help = Fget_text_property (pos, Qhelp_echo, string);
23416 if (!NILP (help))
23417 {
23418 help_echo_string = help;
23419 XSETWINDOW (help_echo_window, w);
23420 help_echo_object = string;
23421 help_echo_pos = charpos;
23422 }
23423 }
23424
23425 if (NILP (pointer))
23426 pointer = Fget_text_property (pos, Qpointer, string);
23427
23428 /* Change the mouse pointer according to what is under X/Y. */
23429 if (NILP (pointer) && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
23430 {
23431 Lisp_Object map;
23432 map = Fget_text_property (pos, Qlocal_map, string);
23433 if (!KEYMAPP (map))
23434 map = Fget_text_property (pos, Qkeymap, string);
23435 if (!KEYMAPP (map))
23436 cursor = dpyinfo->vertical_scroll_bar_cursor;
23437 }
23438
23439 /* Change the mouse face according to what is under X/Y. */
23440 mouse_face = Fget_text_property (pos, Qmouse_face, string);
23441 if (!NILP (mouse_face)
23442 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
23443 && glyph)
23444 {
23445 Lisp_Object b, e;
23446
23447 struct glyph * tmp_glyph;
23448
23449 int gpos;
23450 int gseq_length;
23451 int total_pixel_width;
23452 EMACS_INT ignore;
23453
23454 int vpos, hpos;
23455
23456 b = Fprevious_single_property_change (make_number (charpos + 1),
23457 Qmouse_face, string, Qnil);
23458 if (NILP (b))
23459 b = make_number (0);
23460
23461 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
23462 if (NILP (e))
23463 e = make_number (SCHARS (string));
23464
23465 /* Calculate the position(glyph position: GPOS) of GLYPH in
23466 displayed string. GPOS is different from CHARPOS.
23467
23468 CHARPOS is the position of glyph in internal string
23469 object. A mode line string format has structures which
23470 is converted to a flatten by emacs lisp interpreter.
23471 The internal string is an element of the structures.
23472 The displayed string is the flatten string. */
23473 gpos = 0;
23474 if (glyph > row_start_glyph)
23475 {
23476 tmp_glyph = glyph - 1;
23477 while (tmp_glyph >= row_start_glyph
23478 && tmp_glyph->charpos >= XINT (b)
23479 && EQ (tmp_glyph->object, glyph->object))
23480 {
23481 tmp_glyph--;
23482 gpos++;
23483 }
23484 }
23485
23486 /* Calculate the lenght(glyph sequence length: GSEQ_LENGTH) of
23487 displayed string holding GLYPH.
23488
23489 GSEQ_LENGTH is different from SCHARS (STRING).
23490 SCHARS (STRING) returns the length of the internal string. */
23491 for (tmp_glyph = glyph, gseq_length = gpos;
23492 tmp_glyph->charpos < XINT (e);
23493 tmp_glyph++, gseq_length++)
23494 {
23495 if (!EQ (tmp_glyph->object, glyph->object))
23496 break;
23497 }
23498
23499 total_pixel_width = 0;
23500 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
23501 total_pixel_width += tmp_glyph->pixel_width;
23502
23503 /* Pre calculation of re-rendering position */
23504 vpos = (x - gpos);
23505 hpos = (area == ON_MODE_LINE
23506 ? (w->current_matrix)->nrows - 1
23507 : 0);
23508
23509 /* If the re-rendering position is included in the last
23510 re-rendering area, we should do nothing. */
23511 if ( EQ (window, dpyinfo->mouse_face_window)
23512 && dpyinfo->mouse_face_beg_col <= vpos
23513 && vpos < dpyinfo->mouse_face_end_col
23514 && dpyinfo->mouse_face_beg_row == hpos )
23515 return;
23516
23517 if (clear_mouse_face (dpyinfo))
23518 cursor = No_Cursor;
23519
23520 dpyinfo->mouse_face_beg_col = vpos;
23521 dpyinfo->mouse_face_beg_row = hpos;
23522
23523 dpyinfo->mouse_face_beg_x = original_x_pixel - (total_pixel_width + dx);
23524 dpyinfo->mouse_face_beg_y = 0;
23525
23526 dpyinfo->mouse_face_end_col = vpos + gseq_length;
23527 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_beg_row;
23528
23529 dpyinfo->mouse_face_end_x = 0;
23530 dpyinfo->mouse_face_end_y = 0;
23531
23532 dpyinfo->mouse_face_past_end = 0;
23533 dpyinfo->mouse_face_window = window;
23534
23535 dpyinfo->mouse_face_face_id = face_at_string_position (w, string,
23536 charpos,
23537 0, 0, 0, &ignore,
23538 glyph->face_id, 1);
23539 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
23540
23541 if (NILP (pointer))
23542 pointer = Qhand;
23543 }
23544 else if ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
23545 clear_mouse_face (dpyinfo);
23546 }
23547 define_frame_cursor1 (f, cursor, pointer);
23548 }
23549
23550
23551 /* EXPORT:
23552 Take proper action when the mouse has moved to position X, Y on
23553 frame F as regards highlighting characters that have mouse-face
23554 properties. Also de-highlighting chars where the mouse was before.
23555 X and Y can be negative or out of range. */
23556
23557 void
23558 note_mouse_highlight (f, x, y)
23559 struct frame *f;
23560 int x, y;
23561 {
23562 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
23563 enum window_part part;
23564 Lisp_Object window;
23565 struct window *w;
23566 Cursor cursor = No_Cursor;
23567 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
23568 struct buffer *b;
23569
23570 /* When a menu is active, don't highlight because this looks odd. */
23571 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS)
23572 if (popup_activated ())
23573 return;
23574 #endif
23575
23576 if (NILP (Vmouse_highlight)
23577 || !f->glyphs_initialized_p)
23578 return;
23579
23580 dpyinfo->mouse_face_mouse_x = x;
23581 dpyinfo->mouse_face_mouse_y = y;
23582 dpyinfo->mouse_face_mouse_frame = f;
23583
23584 if (dpyinfo->mouse_face_defer)
23585 return;
23586
23587 if (gc_in_progress)
23588 {
23589 dpyinfo->mouse_face_deferred_gc = 1;
23590 return;
23591 }
23592
23593 /* Which window is that in? */
23594 window = window_from_coordinates (f, x, y, &part, 0, 0, 1);
23595
23596 /* If we were displaying active text in another window, clear that.
23597 Also clear if we move out of text area in same window. */
23598 if (! EQ (window, dpyinfo->mouse_face_window)
23599 || (part != ON_TEXT && part != ON_MODE_LINE && part != ON_HEADER_LINE
23600 && !NILP (dpyinfo->mouse_face_window)))
23601 clear_mouse_face (dpyinfo);
23602
23603 /* Not on a window -> return. */
23604 if (!WINDOWP (window))
23605 return;
23606
23607 /* Reset help_echo_string. It will get recomputed below. */
23608 help_echo_string = Qnil;
23609
23610 /* Convert to window-relative pixel coordinates. */
23611 w = XWINDOW (window);
23612 frame_to_window_pixel_xy (w, &x, &y);
23613
23614 /* Handle tool-bar window differently since it doesn't display a
23615 buffer. */
23616 if (EQ (window, f->tool_bar_window))
23617 {
23618 note_tool_bar_highlight (f, x, y);
23619 return;
23620 }
23621
23622 /* Mouse is on the mode, header line or margin? */
23623 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
23624 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
23625 {
23626 note_mode_line_or_margin_highlight (window, x, y, part);
23627 return;
23628 }
23629
23630 if (part == ON_VERTICAL_BORDER)
23631 {
23632 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
23633 help_echo_string = build_string ("drag-mouse-1: resize");
23634 }
23635 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
23636 || part == ON_SCROLL_BAR)
23637 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
23638 else
23639 cursor = FRAME_X_OUTPUT (f)->text_cursor;
23640
23641 /* Are we in a window whose display is up to date?
23642 And verify the buffer's text has not changed. */
23643 b = XBUFFER (w->buffer);
23644 if (part == ON_TEXT
23645 && EQ (w->window_end_valid, w->buffer)
23646 && XFASTINT (w->last_modified) == BUF_MODIFF (b)
23647 && XFASTINT (w->last_overlay_modified) == BUF_OVERLAY_MODIFF (b))
23648 {
23649 int hpos, vpos, pos, i, dx, dy, area;
23650 struct glyph *glyph;
23651 Lisp_Object object;
23652 Lisp_Object mouse_face = Qnil, overlay = Qnil, position;
23653 Lisp_Object *overlay_vec = NULL;
23654 int noverlays;
23655 struct buffer *obuf;
23656 int obegv, ozv, same_region;
23657
23658 /* Find the glyph under X/Y. */
23659 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
23660
23661 /* Look for :pointer property on image. */
23662 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
23663 {
23664 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
23665 if (img != NULL && IMAGEP (img->spec))
23666 {
23667 Lisp_Object image_map, hotspot;
23668 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
23669 !NILP (image_map))
23670 && (hotspot = find_hot_spot (image_map,
23671 glyph->slice.x + dx,
23672 glyph->slice.y + dy),
23673 CONSP (hotspot))
23674 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
23675 {
23676 Lisp_Object area_id, plist;
23677
23678 area_id = XCAR (hotspot);
23679 /* Could check AREA_ID to see if we enter/leave this hot-spot.
23680 If so, we could look for mouse-enter, mouse-leave
23681 properties in PLIST (and do something...). */
23682 hotspot = XCDR (hotspot);
23683 if (CONSP (hotspot)
23684 && (plist = XCAR (hotspot), CONSP (plist)))
23685 {
23686 pointer = Fplist_get (plist, Qpointer);
23687 if (NILP (pointer))
23688 pointer = Qhand;
23689 help_echo_string = Fplist_get (plist, Qhelp_echo);
23690 if (!NILP (help_echo_string))
23691 {
23692 help_echo_window = window;
23693 help_echo_object = glyph->object;
23694 help_echo_pos = glyph->charpos;
23695 }
23696 }
23697 }
23698 if (NILP (pointer))
23699 pointer = Fplist_get (XCDR (img->spec), QCpointer);
23700 }
23701 }
23702
23703 /* Clear mouse face if X/Y not over text. */
23704 if (glyph == NULL
23705 || area != TEXT_AREA
23706 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p)
23707 {
23708 if (clear_mouse_face (dpyinfo))
23709 cursor = No_Cursor;
23710 if (NILP (pointer))
23711 {
23712 if (area != TEXT_AREA)
23713 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
23714 else
23715 pointer = Vvoid_text_area_pointer;
23716 }
23717 goto set_cursor;
23718 }
23719
23720 pos = glyph->charpos;
23721 object = glyph->object;
23722 if (!STRINGP (object) && !BUFFERP (object))
23723 goto set_cursor;
23724
23725 /* If we get an out-of-range value, return now; avoid an error. */
23726 if (BUFFERP (object) && pos > BUF_Z (b))
23727 goto set_cursor;
23728
23729 /* Make the window's buffer temporarily current for
23730 overlays_at and compute_char_face. */
23731 obuf = current_buffer;
23732 current_buffer = b;
23733 obegv = BEGV;
23734 ozv = ZV;
23735 BEGV = BEG;
23736 ZV = Z;
23737
23738 /* Is this char mouse-active or does it have help-echo? */
23739 position = make_number (pos);
23740
23741 if (BUFFERP (object))
23742 {
23743 /* Put all the overlays we want in a vector in overlay_vec. */
23744 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
23745 /* Sort overlays into increasing priority order. */
23746 noverlays = sort_overlays (overlay_vec, noverlays, w);
23747 }
23748 else
23749 noverlays = 0;
23750
23751 same_region = (EQ (window, dpyinfo->mouse_face_window)
23752 && vpos >= dpyinfo->mouse_face_beg_row
23753 && vpos <= dpyinfo->mouse_face_end_row
23754 && (vpos > dpyinfo->mouse_face_beg_row
23755 || hpos >= dpyinfo->mouse_face_beg_col)
23756 && (vpos < dpyinfo->mouse_face_end_row
23757 || hpos < dpyinfo->mouse_face_end_col
23758 || dpyinfo->mouse_face_past_end));
23759
23760 if (same_region)
23761 cursor = No_Cursor;
23762
23763 /* Check mouse-face highlighting. */
23764 if (! same_region
23765 /* If there exists an overlay with mouse-face overlapping
23766 the one we are currently highlighting, we have to
23767 check if we enter the overlapping overlay, and then
23768 highlight only that. */
23769 || (OVERLAYP (dpyinfo->mouse_face_overlay)
23770 && mouse_face_overlay_overlaps (dpyinfo->mouse_face_overlay)))
23771 {
23772 /* Find the highest priority overlay with a mouse-face. */
23773 overlay = Qnil;
23774 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
23775 {
23776 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
23777 if (!NILP (mouse_face))
23778 overlay = overlay_vec[i];
23779 }
23780
23781 /* If we're highlighting the same overlay as before, there's
23782 no need to do that again. */
23783 if (!NILP (overlay) && EQ (overlay, dpyinfo->mouse_face_overlay))
23784 goto check_help_echo;
23785 dpyinfo->mouse_face_overlay = overlay;
23786
23787 /* Clear the display of the old active region, if any. */
23788 if (clear_mouse_face (dpyinfo))
23789 cursor = No_Cursor;
23790
23791 /* If no overlay applies, get a text property. */
23792 if (NILP (overlay))
23793 mouse_face = Fget_text_property (position, Qmouse_face, object);
23794
23795 /* Next, compute the bounds of the mouse highlighting and
23796 display it. */
23797 if (!NILP (mouse_face) && STRINGP (object))
23798 {
23799 /* The mouse-highlighting comes from a display string
23800 with a mouse-face. */
23801 Lisp_Object b, e;
23802 EMACS_INT ignore;
23803
23804 b = Fprevious_single_property_change
23805 (make_number (pos + 1), Qmouse_face, object, Qnil);
23806 e = Fnext_single_property_change
23807 (position, Qmouse_face, object, Qnil);
23808 if (NILP (b))
23809 b = make_number (0);
23810 if (NILP (e))
23811 e = make_number (SCHARS (object) - 1);
23812
23813 fast_find_string_pos (w, XINT (b), object,
23814 &dpyinfo->mouse_face_beg_col,
23815 &dpyinfo->mouse_face_beg_row,
23816 &dpyinfo->mouse_face_beg_x,
23817 &dpyinfo->mouse_face_beg_y, 0);
23818 fast_find_string_pos (w, XINT (e), object,
23819 &dpyinfo->mouse_face_end_col,
23820 &dpyinfo->mouse_face_end_row,
23821 &dpyinfo->mouse_face_end_x,
23822 &dpyinfo->mouse_face_end_y, 1);
23823 dpyinfo->mouse_face_past_end = 0;
23824 dpyinfo->mouse_face_window = window;
23825 dpyinfo->mouse_face_face_id
23826 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
23827 glyph->face_id, 1);
23828 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
23829 cursor = No_Cursor;
23830 }
23831 else
23832 {
23833 /* The mouse-highlighting, if any, comes from an overlay
23834 or text property in the buffer. */
23835 Lisp_Object buffer, display_string;
23836
23837 if (STRINGP (object))
23838 {
23839 /* If we are on a display string with no mouse-face,
23840 check if the text under it has one. */
23841 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
23842 int start = MATRIX_ROW_START_CHARPOS (r);
23843 pos = string_buffer_position (w, object, start);
23844 if (pos > 0)
23845 {
23846 mouse_face = get_char_property_and_overlay
23847 (make_number (pos), Qmouse_face, w->buffer, &overlay);
23848 buffer = w->buffer;
23849 display_string = object;
23850 }
23851 }
23852 else
23853 {
23854 buffer = object;
23855 display_string = Qnil;
23856 }
23857
23858 if (!NILP (mouse_face))
23859 {
23860 Lisp_Object before, after;
23861 Lisp_Object before_string, after_string;
23862
23863 if (NILP (overlay))
23864 {
23865 /* Handle the text property case. */
23866 before = Fprevious_single_property_change
23867 (make_number (pos + 1), Qmouse_face, buffer,
23868 Fmarker_position (w->start));
23869 after = Fnext_single_property_change
23870 (make_number (pos), Qmouse_face, buffer,
23871 make_number (BUF_Z (XBUFFER (buffer))
23872 - XFASTINT (w->window_end_pos)));
23873 before_string = after_string = Qnil;
23874 }
23875 else
23876 {
23877 /* Handle the overlay case. */
23878 before = Foverlay_start (overlay);
23879 after = Foverlay_end (overlay);
23880 before_string = Foverlay_get (overlay, Qbefore_string);
23881 after_string = Foverlay_get (overlay, Qafter_string);
23882
23883 if (!STRINGP (before_string)) before_string = Qnil;
23884 if (!STRINGP (after_string)) after_string = Qnil;
23885 }
23886
23887 mouse_face_from_buffer_pos (window, dpyinfo, pos,
23888 XFASTINT (before),
23889 XFASTINT (after),
23890 before_string, after_string,
23891 display_string);
23892 cursor = No_Cursor;
23893 }
23894 }
23895 }
23896
23897 check_help_echo:
23898
23899 /* Look for a `help-echo' property. */
23900 if (NILP (help_echo_string)) {
23901 Lisp_Object help, overlay;
23902
23903 /* Check overlays first. */
23904 help = overlay = Qnil;
23905 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
23906 {
23907 overlay = overlay_vec[i];
23908 help = Foverlay_get (overlay, Qhelp_echo);
23909 }
23910
23911 if (!NILP (help))
23912 {
23913 help_echo_string = help;
23914 help_echo_window = window;
23915 help_echo_object = overlay;
23916 help_echo_pos = pos;
23917 }
23918 else
23919 {
23920 Lisp_Object object = glyph->object;
23921 int charpos = glyph->charpos;
23922
23923 /* Try text properties. */
23924 if (STRINGP (object)
23925 && charpos >= 0
23926 && charpos < SCHARS (object))
23927 {
23928 help = Fget_text_property (make_number (charpos),
23929 Qhelp_echo, object);
23930 if (NILP (help))
23931 {
23932 /* If the string itself doesn't specify a help-echo,
23933 see if the buffer text ``under'' it does. */
23934 struct glyph_row *r
23935 = MATRIX_ROW (w->current_matrix, vpos);
23936 int start = MATRIX_ROW_START_CHARPOS (r);
23937 int pos = string_buffer_position (w, object, start);
23938 if (pos > 0)
23939 {
23940 help = Fget_char_property (make_number (pos),
23941 Qhelp_echo, w->buffer);
23942 if (!NILP (help))
23943 {
23944 charpos = pos;
23945 object = w->buffer;
23946 }
23947 }
23948 }
23949 }
23950 else if (BUFFERP (object)
23951 && charpos >= BEGV
23952 && charpos < ZV)
23953 help = Fget_text_property (make_number (charpos), Qhelp_echo,
23954 object);
23955
23956 if (!NILP (help))
23957 {
23958 help_echo_string = help;
23959 help_echo_window = window;
23960 help_echo_object = object;
23961 help_echo_pos = charpos;
23962 }
23963 }
23964 }
23965
23966 /* Look for a `pointer' property. */
23967 if (NILP (pointer))
23968 {
23969 /* Check overlays first. */
23970 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
23971 pointer = Foverlay_get (overlay_vec[i], Qpointer);
23972
23973 if (NILP (pointer))
23974 {
23975 Lisp_Object object = glyph->object;
23976 int charpos = glyph->charpos;
23977
23978 /* Try text properties. */
23979 if (STRINGP (object)
23980 && charpos >= 0
23981 && charpos < SCHARS (object))
23982 {
23983 pointer = Fget_text_property (make_number (charpos),
23984 Qpointer, object);
23985 if (NILP (pointer))
23986 {
23987 /* If the string itself doesn't specify a pointer,
23988 see if the buffer text ``under'' it does. */
23989 struct glyph_row *r
23990 = MATRIX_ROW (w->current_matrix, vpos);
23991 int start = MATRIX_ROW_START_CHARPOS (r);
23992 int pos = string_buffer_position (w, object, start);
23993 if (pos > 0)
23994 pointer = Fget_char_property (make_number (pos),
23995 Qpointer, w->buffer);
23996 }
23997 }
23998 else if (BUFFERP (object)
23999 && charpos >= BEGV
24000 && charpos < ZV)
24001 pointer = Fget_text_property (make_number (charpos),
24002 Qpointer, object);
24003 }
24004 }
24005
24006 BEGV = obegv;
24007 ZV = ozv;
24008 current_buffer = obuf;
24009 }
24010
24011 set_cursor:
24012
24013 define_frame_cursor1 (f, cursor, pointer);
24014 }
24015
24016
24017 /* EXPORT for RIF:
24018 Clear any mouse-face on window W. This function is part of the
24019 redisplay interface, and is called from try_window_id and similar
24020 functions to ensure the mouse-highlight is off. */
24021
24022 void
24023 x_clear_window_mouse_face (w)
24024 struct window *w;
24025 {
24026 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
24027 Lisp_Object window;
24028
24029 BLOCK_INPUT;
24030 XSETWINDOW (window, w);
24031 if (EQ (window, dpyinfo->mouse_face_window))
24032 clear_mouse_face (dpyinfo);
24033 UNBLOCK_INPUT;
24034 }
24035
24036
24037 /* EXPORT:
24038 Just discard the mouse face information for frame F, if any.
24039 This is used when the size of F is changed. */
24040
24041 void
24042 cancel_mouse_face (f)
24043 struct frame *f;
24044 {
24045 Lisp_Object window;
24046 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
24047
24048 window = dpyinfo->mouse_face_window;
24049 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
24050 {
24051 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
24052 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
24053 dpyinfo->mouse_face_window = Qnil;
24054 }
24055 }
24056
24057
24058 #endif /* HAVE_WINDOW_SYSTEM */
24059
24060 \f
24061 /***********************************************************************
24062 Exposure Events
24063 ***********************************************************************/
24064
24065 #ifdef HAVE_WINDOW_SYSTEM
24066
24067 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
24068 which intersects rectangle R. R is in window-relative coordinates. */
24069
24070 static void
24071 expose_area (w, row, r, area)
24072 struct window *w;
24073 struct glyph_row *row;
24074 XRectangle *r;
24075 enum glyph_row_area area;
24076 {
24077 struct glyph *first = row->glyphs[area];
24078 struct glyph *end = row->glyphs[area] + row->used[area];
24079 struct glyph *last;
24080 int first_x, start_x, x;
24081
24082 if (area == TEXT_AREA && row->fill_line_p)
24083 /* If row extends face to end of line write the whole line. */
24084 draw_glyphs (w, 0, row, area,
24085 0, row->used[area],
24086 DRAW_NORMAL_TEXT, 0);
24087 else
24088 {
24089 /* Set START_X to the window-relative start position for drawing glyphs of
24090 AREA. The first glyph of the text area can be partially visible.
24091 The first glyphs of other areas cannot. */
24092 start_x = window_box_left_offset (w, area);
24093 x = start_x;
24094 if (area == TEXT_AREA)
24095 x += row->x;
24096
24097 /* Find the first glyph that must be redrawn. */
24098 while (first < end
24099 && x + first->pixel_width < r->x)
24100 {
24101 x += first->pixel_width;
24102 ++first;
24103 }
24104
24105 /* Find the last one. */
24106 last = first;
24107 first_x = x;
24108 while (last < end
24109 && x < r->x + r->width)
24110 {
24111 x += last->pixel_width;
24112 ++last;
24113 }
24114
24115 /* Repaint. */
24116 if (last > first)
24117 draw_glyphs (w, first_x - start_x, row, area,
24118 first - row->glyphs[area], last - row->glyphs[area],
24119 DRAW_NORMAL_TEXT, 0);
24120 }
24121 }
24122
24123
24124 /* Redraw the parts of the glyph row ROW on window W intersecting
24125 rectangle R. R is in window-relative coordinates. Value is
24126 non-zero if mouse-face was overwritten. */
24127
24128 static int
24129 expose_line (w, row, r)
24130 struct window *w;
24131 struct glyph_row *row;
24132 XRectangle *r;
24133 {
24134 xassert (row->enabled_p);
24135
24136 if (row->mode_line_p || w->pseudo_window_p)
24137 draw_glyphs (w, 0, row, TEXT_AREA,
24138 0, row->used[TEXT_AREA],
24139 DRAW_NORMAL_TEXT, 0);
24140 else
24141 {
24142 if (row->used[LEFT_MARGIN_AREA])
24143 expose_area (w, row, r, LEFT_MARGIN_AREA);
24144 if (row->used[TEXT_AREA])
24145 expose_area (w, row, r, TEXT_AREA);
24146 if (row->used[RIGHT_MARGIN_AREA])
24147 expose_area (w, row, r, RIGHT_MARGIN_AREA);
24148 draw_row_fringe_bitmaps (w, row);
24149 }
24150
24151 return row->mouse_face_p;
24152 }
24153
24154
24155 /* Redraw those parts of glyphs rows during expose event handling that
24156 overlap other rows. Redrawing of an exposed line writes over parts
24157 of lines overlapping that exposed line; this function fixes that.
24158
24159 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
24160 row in W's current matrix that is exposed and overlaps other rows.
24161 LAST_OVERLAPPING_ROW is the last such row. */
24162
24163 static void
24164 expose_overlaps (w, first_overlapping_row, last_overlapping_row, r)
24165 struct window *w;
24166 struct glyph_row *first_overlapping_row;
24167 struct glyph_row *last_overlapping_row;
24168 XRectangle *r;
24169 {
24170 struct glyph_row *row;
24171
24172 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
24173 if (row->overlapping_p)
24174 {
24175 xassert (row->enabled_p && !row->mode_line_p);
24176
24177 row->clip = r;
24178 if (row->used[LEFT_MARGIN_AREA])
24179 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
24180
24181 if (row->used[TEXT_AREA])
24182 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
24183
24184 if (row->used[RIGHT_MARGIN_AREA])
24185 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
24186 row->clip = NULL;
24187 }
24188 }
24189
24190
24191 /* Return non-zero if W's cursor intersects rectangle R. */
24192
24193 static int
24194 phys_cursor_in_rect_p (w, r)
24195 struct window *w;
24196 XRectangle *r;
24197 {
24198 XRectangle cr, result;
24199 struct glyph *cursor_glyph;
24200 struct glyph_row *row;
24201
24202 if (w->phys_cursor.vpos >= 0
24203 && w->phys_cursor.vpos < w->current_matrix->nrows
24204 && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
24205 row->enabled_p)
24206 && row->cursor_in_fringe_p)
24207 {
24208 /* Cursor is in the fringe. */
24209 cr.x = window_box_right_offset (w,
24210 (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
24211 ? RIGHT_MARGIN_AREA
24212 : TEXT_AREA));
24213 cr.y = row->y;
24214 cr.width = WINDOW_RIGHT_FRINGE_WIDTH (w);
24215 cr.height = row->height;
24216 return x_intersect_rectangles (&cr, r, &result);
24217 }
24218
24219 cursor_glyph = get_phys_cursor_glyph (w);
24220 if (cursor_glyph)
24221 {
24222 /* r is relative to W's box, but w->phys_cursor.x is relative
24223 to left edge of W's TEXT area. Adjust it. */
24224 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
24225 cr.y = w->phys_cursor.y;
24226 cr.width = cursor_glyph->pixel_width;
24227 cr.height = w->phys_cursor_height;
24228 /* ++KFS: W32 version used W32-specific IntersectRect here, but
24229 I assume the effect is the same -- and this is portable. */
24230 return x_intersect_rectangles (&cr, r, &result);
24231 }
24232 /* If we don't understand the format, pretend we're not in the hot-spot. */
24233 return 0;
24234 }
24235
24236
24237 /* EXPORT:
24238 Draw a vertical window border to the right of window W if W doesn't
24239 have vertical scroll bars. */
24240
24241 void
24242 x_draw_vertical_border (w)
24243 struct window *w;
24244 {
24245 struct frame *f = XFRAME (WINDOW_FRAME (w));
24246
24247 /* We could do better, if we knew what type of scroll-bar the adjacent
24248 windows (on either side) have... But we don't :-(
24249 However, I think this works ok. ++KFS 2003-04-25 */
24250
24251 /* Redraw borders between horizontally adjacent windows. Don't
24252 do it for frames with vertical scroll bars because either the
24253 right scroll bar of a window, or the left scroll bar of its
24254 neighbor will suffice as a border. */
24255 if (FRAME_HAS_VERTICAL_SCROLL_BARS (XFRAME (w->frame)))
24256 return;
24257
24258 if (!WINDOW_RIGHTMOST_P (w)
24259 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
24260 {
24261 int x0, x1, y0, y1;
24262
24263 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
24264 y1 -= 1;
24265
24266 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
24267 x1 -= 1;
24268
24269 FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
24270 }
24271 else if (!WINDOW_LEFTMOST_P (w)
24272 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
24273 {
24274 int x0, x1, y0, y1;
24275
24276 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
24277 y1 -= 1;
24278
24279 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
24280 x0 -= 1;
24281
24282 FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
24283 }
24284 }
24285
24286
24287 /* Redraw the part of window W intersection rectangle FR. Pixel
24288 coordinates in FR are frame-relative. Call this function with
24289 input blocked. Value is non-zero if the exposure overwrites
24290 mouse-face. */
24291
24292 static int
24293 expose_window (w, fr)
24294 struct window *w;
24295 XRectangle *fr;
24296 {
24297 struct frame *f = XFRAME (w->frame);
24298 XRectangle wr, r;
24299 int mouse_face_overwritten_p = 0;
24300
24301 /* If window is not yet fully initialized, do nothing. This can
24302 happen when toolkit scroll bars are used and a window is split.
24303 Reconfiguring the scroll bar will generate an expose for a newly
24304 created window. */
24305 if (w->current_matrix == NULL)
24306 return 0;
24307
24308 /* When we're currently updating the window, display and current
24309 matrix usually don't agree. Arrange for a thorough display
24310 later. */
24311 if (w == updated_window)
24312 {
24313 SET_FRAME_GARBAGED (f);
24314 return 0;
24315 }
24316
24317 /* Frame-relative pixel rectangle of W. */
24318 wr.x = WINDOW_LEFT_EDGE_X (w);
24319 wr.y = WINDOW_TOP_EDGE_Y (w);
24320 wr.width = WINDOW_TOTAL_WIDTH (w);
24321 wr.height = WINDOW_TOTAL_HEIGHT (w);
24322
24323 if (x_intersect_rectangles (fr, &wr, &r))
24324 {
24325 int yb = window_text_bottom_y (w);
24326 struct glyph_row *row;
24327 int cursor_cleared_p;
24328 struct glyph_row *first_overlapping_row, *last_overlapping_row;
24329
24330 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
24331 r.x, r.y, r.width, r.height));
24332
24333 /* Convert to window coordinates. */
24334 r.x -= WINDOW_LEFT_EDGE_X (w);
24335 r.y -= WINDOW_TOP_EDGE_Y (w);
24336
24337 /* Turn off the cursor. */
24338 if (!w->pseudo_window_p
24339 && phys_cursor_in_rect_p (w, &r))
24340 {
24341 x_clear_cursor (w);
24342 cursor_cleared_p = 1;
24343 }
24344 else
24345 cursor_cleared_p = 0;
24346
24347 /* Update lines intersecting rectangle R. */
24348 first_overlapping_row = last_overlapping_row = NULL;
24349 for (row = w->current_matrix->rows;
24350 row->enabled_p;
24351 ++row)
24352 {
24353 int y0 = row->y;
24354 int y1 = MATRIX_ROW_BOTTOM_Y (row);
24355
24356 if ((y0 >= r.y && y0 < r.y + r.height)
24357 || (y1 > r.y && y1 < r.y + r.height)
24358 || (r.y >= y0 && r.y < y1)
24359 || (r.y + r.height > y0 && r.y + r.height < y1))
24360 {
24361 /* A header line may be overlapping, but there is no need
24362 to fix overlapping areas for them. KFS 2005-02-12 */
24363 if (row->overlapping_p && !row->mode_line_p)
24364 {
24365 if (first_overlapping_row == NULL)
24366 first_overlapping_row = row;
24367 last_overlapping_row = row;
24368 }
24369
24370 row->clip = fr;
24371 if (expose_line (w, row, &r))
24372 mouse_face_overwritten_p = 1;
24373 row->clip = NULL;
24374 }
24375 else if (row->overlapping_p)
24376 {
24377 /* We must redraw a row overlapping the exposed area. */
24378 if (y0 < r.y
24379 ? y0 + row->phys_height > r.y
24380 : y0 + row->ascent - row->phys_ascent < r.y +r.height)
24381 {
24382 if (first_overlapping_row == NULL)
24383 first_overlapping_row = row;
24384 last_overlapping_row = row;
24385 }
24386 }
24387
24388 if (y1 >= yb)
24389 break;
24390 }
24391
24392 /* Display the mode line if there is one. */
24393 if (WINDOW_WANTS_MODELINE_P (w)
24394 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
24395 row->enabled_p)
24396 && row->y < r.y + r.height)
24397 {
24398 if (expose_line (w, row, &r))
24399 mouse_face_overwritten_p = 1;
24400 }
24401
24402 if (!w->pseudo_window_p)
24403 {
24404 /* Fix the display of overlapping rows. */
24405 if (first_overlapping_row)
24406 expose_overlaps (w, first_overlapping_row, last_overlapping_row,
24407 fr);
24408
24409 /* Draw border between windows. */
24410 x_draw_vertical_border (w);
24411
24412 /* Turn the cursor on again. */
24413 if (cursor_cleared_p)
24414 update_window_cursor (w, 1);
24415 }
24416 }
24417
24418 return mouse_face_overwritten_p;
24419 }
24420
24421
24422
24423 /* Redraw (parts) of all windows in the window tree rooted at W that
24424 intersect R. R contains frame pixel coordinates. Value is
24425 non-zero if the exposure overwrites mouse-face. */
24426
24427 static int
24428 expose_window_tree (w, r)
24429 struct window *w;
24430 XRectangle *r;
24431 {
24432 struct frame *f = XFRAME (w->frame);
24433 int mouse_face_overwritten_p = 0;
24434
24435 while (w && !FRAME_GARBAGED_P (f))
24436 {
24437 if (!NILP (w->hchild))
24438 mouse_face_overwritten_p
24439 |= expose_window_tree (XWINDOW (w->hchild), r);
24440 else if (!NILP (w->vchild))
24441 mouse_face_overwritten_p
24442 |= expose_window_tree (XWINDOW (w->vchild), r);
24443 else
24444 mouse_face_overwritten_p |= expose_window (w, r);
24445
24446 w = NILP (w->next) ? NULL : XWINDOW (w->next);
24447 }
24448
24449 return mouse_face_overwritten_p;
24450 }
24451
24452
24453 /* EXPORT:
24454 Redisplay an exposed area of frame F. X and Y are the upper-left
24455 corner of the exposed rectangle. W and H are width and height of
24456 the exposed area. All are pixel values. W or H zero means redraw
24457 the entire frame. */
24458
24459 void
24460 expose_frame (f, x, y, w, h)
24461 struct frame *f;
24462 int x, y, w, h;
24463 {
24464 XRectangle r;
24465 int mouse_face_overwritten_p = 0;
24466
24467 TRACE ((stderr, "expose_frame "));
24468
24469 /* No need to redraw if frame will be redrawn soon. */
24470 if (FRAME_GARBAGED_P (f))
24471 {
24472 TRACE ((stderr, " garbaged\n"));
24473 return;
24474 }
24475
24476 /* If basic faces haven't been realized yet, there is no point in
24477 trying to redraw anything. This can happen when we get an expose
24478 event while Emacs is starting, e.g. by moving another window. */
24479 if (FRAME_FACE_CACHE (f) == NULL
24480 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
24481 {
24482 TRACE ((stderr, " no faces\n"));
24483 return;
24484 }
24485
24486 if (w == 0 || h == 0)
24487 {
24488 r.x = r.y = 0;
24489 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
24490 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
24491 }
24492 else
24493 {
24494 r.x = x;
24495 r.y = y;
24496 r.width = w;
24497 r.height = h;
24498 }
24499
24500 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
24501 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
24502
24503 if (WINDOWP (f->tool_bar_window))
24504 mouse_face_overwritten_p
24505 |= expose_window (XWINDOW (f->tool_bar_window), &r);
24506
24507 #ifdef HAVE_X_WINDOWS
24508 #ifndef MSDOS
24509 #ifndef USE_X_TOOLKIT
24510 if (WINDOWP (f->menu_bar_window))
24511 mouse_face_overwritten_p
24512 |= expose_window (XWINDOW (f->menu_bar_window), &r);
24513 #endif /* not USE_X_TOOLKIT */
24514 #endif
24515 #endif
24516
24517 /* Some window managers support a focus-follows-mouse style with
24518 delayed raising of frames. Imagine a partially obscured frame,
24519 and moving the mouse into partially obscured mouse-face on that
24520 frame. The visible part of the mouse-face will be highlighted,
24521 then the WM raises the obscured frame. With at least one WM, KDE
24522 2.1, Emacs is not getting any event for the raising of the frame
24523 (even tried with SubstructureRedirectMask), only Expose events.
24524 These expose events will draw text normally, i.e. not
24525 highlighted. Which means we must redo the highlight here.
24526 Subsume it under ``we love X''. --gerd 2001-08-15 */
24527 /* Included in Windows version because Windows most likely does not
24528 do the right thing if any third party tool offers
24529 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
24530 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
24531 {
24532 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
24533 if (f == dpyinfo->mouse_face_mouse_frame)
24534 {
24535 int x = dpyinfo->mouse_face_mouse_x;
24536 int y = dpyinfo->mouse_face_mouse_y;
24537 clear_mouse_face (dpyinfo);
24538 note_mouse_highlight (f, x, y);
24539 }
24540 }
24541 }
24542
24543
24544 /* EXPORT:
24545 Determine the intersection of two rectangles R1 and R2. Return
24546 the intersection in *RESULT. Value is non-zero if RESULT is not
24547 empty. */
24548
24549 int
24550 x_intersect_rectangles (r1, r2, result)
24551 XRectangle *r1, *r2, *result;
24552 {
24553 XRectangle *left, *right;
24554 XRectangle *upper, *lower;
24555 int intersection_p = 0;
24556
24557 /* Rearrange so that R1 is the left-most rectangle. */
24558 if (r1->x < r2->x)
24559 left = r1, right = r2;
24560 else
24561 left = r2, right = r1;
24562
24563 /* X0 of the intersection is right.x0, if this is inside R1,
24564 otherwise there is no intersection. */
24565 if (right->x <= left->x + left->width)
24566 {
24567 result->x = right->x;
24568
24569 /* The right end of the intersection is the minimum of the
24570 the right ends of left and right. */
24571 result->width = (min (left->x + left->width, right->x + right->width)
24572 - result->x);
24573
24574 /* Same game for Y. */
24575 if (r1->y < r2->y)
24576 upper = r1, lower = r2;
24577 else
24578 upper = r2, lower = r1;
24579
24580 /* The upper end of the intersection is lower.y0, if this is inside
24581 of upper. Otherwise, there is no intersection. */
24582 if (lower->y <= upper->y + upper->height)
24583 {
24584 result->y = lower->y;
24585
24586 /* The lower end of the intersection is the minimum of the lower
24587 ends of upper and lower. */
24588 result->height = (min (lower->y + lower->height,
24589 upper->y + upper->height)
24590 - result->y);
24591 intersection_p = 1;
24592 }
24593 }
24594
24595 return intersection_p;
24596 }
24597
24598 #endif /* HAVE_WINDOW_SYSTEM */
24599
24600 \f
24601 /***********************************************************************
24602 Initialization
24603 ***********************************************************************/
24604
24605 void
24606 syms_of_xdisp ()
24607 {
24608 Vwith_echo_area_save_vector = Qnil;
24609 staticpro (&Vwith_echo_area_save_vector);
24610
24611 Vmessage_stack = Qnil;
24612 staticpro (&Vmessage_stack);
24613
24614 Qinhibit_redisplay = intern_c_string ("inhibit-redisplay");
24615 staticpro (&Qinhibit_redisplay);
24616
24617 message_dolog_marker1 = Fmake_marker ();
24618 staticpro (&message_dolog_marker1);
24619 message_dolog_marker2 = Fmake_marker ();
24620 staticpro (&message_dolog_marker2);
24621 message_dolog_marker3 = Fmake_marker ();
24622 staticpro (&message_dolog_marker3);
24623
24624 #if GLYPH_DEBUG
24625 defsubr (&Sdump_frame_glyph_matrix);
24626 defsubr (&Sdump_glyph_matrix);
24627 defsubr (&Sdump_glyph_row);
24628 defsubr (&Sdump_tool_bar_row);
24629 defsubr (&Strace_redisplay);
24630 defsubr (&Strace_to_stderr);
24631 #endif
24632 #ifdef HAVE_WINDOW_SYSTEM
24633 defsubr (&Stool_bar_lines_needed);
24634 defsubr (&Slookup_image_map);
24635 #endif
24636 defsubr (&Sformat_mode_line);
24637 defsubr (&Sinvisible_p);
24638
24639 staticpro (&Qmenu_bar_update_hook);
24640 Qmenu_bar_update_hook = intern_c_string ("menu-bar-update-hook");
24641
24642 staticpro (&Qoverriding_terminal_local_map);
24643 Qoverriding_terminal_local_map = intern_c_string ("overriding-terminal-local-map");
24644
24645 staticpro (&Qoverriding_local_map);
24646 Qoverriding_local_map = intern_c_string ("overriding-local-map");
24647
24648 staticpro (&Qwindow_scroll_functions);
24649 Qwindow_scroll_functions = intern_c_string ("window-scroll-functions");
24650
24651 staticpro (&Qwindow_text_change_functions);
24652 Qwindow_text_change_functions = intern_c_string ("window-text-change-functions");
24653
24654 staticpro (&Qredisplay_end_trigger_functions);
24655 Qredisplay_end_trigger_functions = intern_c_string ("redisplay-end-trigger-functions");
24656
24657 staticpro (&Qinhibit_point_motion_hooks);
24658 Qinhibit_point_motion_hooks = intern_c_string ("inhibit-point-motion-hooks");
24659
24660 Qeval = intern_c_string ("eval");
24661 staticpro (&Qeval);
24662
24663 QCdata = intern_c_string (":data");
24664 staticpro (&QCdata);
24665 Qdisplay = intern_c_string ("display");
24666 staticpro (&Qdisplay);
24667 Qspace_width = intern_c_string ("space-width");
24668 staticpro (&Qspace_width);
24669 Qraise = intern_c_string ("raise");
24670 staticpro (&Qraise);
24671 Qslice = intern_c_string ("slice");
24672 staticpro (&Qslice);
24673 Qspace = intern_c_string ("space");
24674 staticpro (&Qspace);
24675 Qmargin = intern_c_string ("margin");
24676 staticpro (&Qmargin);
24677 Qpointer = intern_c_string ("pointer");
24678 staticpro (&Qpointer);
24679 Qleft_margin = intern_c_string ("left-margin");
24680 staticpro (&Qleft_margin);
24681 Qright_margin = intern_c_string ("right-margin");
24682 staticpro (&Qright_margin);
24683 Qcenter = intern_c_string ("center");
24684 staticpro (&Qcenter);
24685 Qline_height = intern_c_string ("line-height");
24686 staticpro (&Qline_height);
24687 QCalign_to = intern_c_string (":align-to");
24688 staticpro (&QCalign_to);
24689 QCrelative_width = intern_c_string (":relative-width");
24690 staticpro (&QCrelative_width);
24691 QCrelative_height = intern_c_string (":relative-height");
24692 staticpro (&QCrelative_height);
24693 QCeval = intern_c_string (":eval");
24694 staticpro (&QCeval);
24695 QCpropertize = intern_c_string (":propertize");
24696 staticpro (&QCpropertize);
24697 QCfile = intern_c_string (":file");
24698 staticpro (&QCfile);
24699 Qfontified = intern_c_string ("fontified");
24700 staticpro (&Qfontified);
24701 Qfontification_functions = intern_c_string ("fontification-functions");
24702 staticpro (&Qfontification_functions);
24703 Qtrailing_whitespace = intern_c_string ("trailing-whitespace");
24704 staticpro (&Qtrailing_whitespace);
24705 Qescape_glyph = intern_c_string ("escape-glyph");
24706 staticpro (&Qescape_glyph);
24707 Qnobreak_space = intern_c_string ("nobreak-space");
24708 staticpro (&Qnobreak_space);
24709 Qimage = intern_c_string ("image");
24710 staticpro (&Qimage);
24711 QCmap = intern_c_string (":map");
24712 staticpro (&QCmap);
24713 QCpointer = intern_c_string (":pointer");
24714 staticpro (&QCpointer);
24715 Qrect = intern_c_string ("rect");
24716 staticpro (&Qrect);
24717 Qcircle = intern_c_string ("circle");
24718 staticpro (&Qcircle);
24719 Qpoly = intern_c_string ("poly");
24720 staticpro (&Qpoly);
24721 Qmessage_truncate_lines = intern_c_string ("message-truncate-lines");
24722 staticpro (&Qmessage_truncate_lines);
24723 Qgrow_only = intern_c_string ("grow-only");
24724 staticpro (&Qgrow_only);
24725 Qinhibit_menubar_update = intern_c_string ("inhibit-menubar-update");
24726 staticpro (&Qinhibit_menubar_update);
24727 Qinhibit_eval_during_redisplay = intern_c_string ("inhibit-eval-during-redisplay");
24728 staticpro (&Qinhibit_eval_during_redisplay);
24729 Qposition = intern_c_string ("position");
24730 staticpro (&Qposition);
24731 Qbuffer_position = intern_c_string ("buffer-position");
24732 staticpro (&Qbuffer_position);
24733 Qobject = intern_c_string ("object");
24734 staticpro (&Qobject);
24735 Qbar = intern_c_string ("bar");
24736 staticpro (&Qbar);
24737 Qhbar = intern_c_string ("hbar");
24738 staticpro (&Qhbar);
24739 Qbox = intern_c_string ("box");
24740 staticpro (&Qbox);
24741 Qhollow = intern_c_string ("hollow");
24742 staticpro (&Qhollow);
24743 Qhand = intern_c_string ("hand");
24744 staticpro (&Qhand);
24745 Qarrow = intern_c_string ("arrow");
24746 staticpro (&Qarrow);
24747 Qtext = intern_c_string ("text");
24748 staticpro (&Qtext);
24749 Qrisky_local_variable = intern_c_string ("risky-local-variable");
24750 staticpro (&Qrisky_local_variable);
24751 Qinhibit_free_realized_faces = intern_c_string ("inhibit-free-realized-faces");
24752 staticpro (&Qinhibit_free_realized_faces);
24753
24754 list_of_error = Fcons (Fcons (intern_c_string ("error"),
24755 Fcons (intern_c_string ("void-variable"), Qnil)),
24756 Qnil);
24757 staticpro (&list_of_error);
24758
24759 Qlast_arrow_position = intern_c_string ("last-arrow-position");
24760 staticpro (&Qlast_arrow_position);
24761 Qlast_arrow_string = intern_c_string ("last-arrow-string");
24762 staticpro (&Qlast_arrow_string);
24763
24764 Qoverlay_arrow_string = intern_c_string ("overlay-arrow-string");
24765 staticpro (&Qoverlay_arrow_string);
24766 Qoverlay_arrow_bitmap = intern_c_string ("overlay-arrow-bitmap");
24767 staticpro (&Qoverlay_arrow_bitmap);
24768
24769 echo_buffer[0] = echo_buffer[1] = Qnil;
24770 staticpro (&echo_buffer[0]);
24771 staticpro (&echo_buffer[1]);
24772
24773 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
24774 staticpro (&echo_area_buffer[0]);
24775 staticpro (&echo_area_buffer[1]);
24776
24777 Vmessages_buffer_name = make_pure_c_string ("*Messages*");
24778 staticpro (&Vmessages_buffer_name);
24779
24780 mode_line_proptrans_alist = Qnil;
24781 staticpro (&mode_line_proptrans_alist);
24782 mode_line_string_list = Qnil;
24783 staticpro (&mode_line_string_list);
24784 mode_line_string_face = Qnil;
24785 staticpro (&mode_line_string_face);
24786 mode_line_string_face_prop = Qnil;
24787 staticpro (&mode_line_string_face_prop);
24788 Vmode_line_unwind_vector = Qnil;
24789 staticpro (&Vmode_line_unwind_vector);
24790
24791 help_echo_string = Qnil;
24792 staticpro (&help_echo_string);
24793 help_echo_object = Qnil;
24794 staticpro (&help_echo_object);
24795 help_echo_window = Qnil;
24796 staticpro (&help_echo_window);
24797 previous_help_echo_string = Qnil;
24798 staticpro (&previous_help_echo_string);
24799 help_echo_pos = -1;
24800
24801 #ifdef HAVE_WINDOW_SYSTEM
24802 DEFVAR_BOOL ("x-stretch-cursor", &x_stretch_cursor_p,
24803 doc: /* *Non-nil means draw block cursor as wide as the glyph under it.
24804 For example, if a block cursor is over a tab, it will be drawn as
24805 wide as that tab on the display. */);
24806 x_stretch_cursor_p = 0;
24807 #endif
24808
24809 DEFVAR_LISP ("show-trailing-whitespace", &Vshow_trailing_whitespace,
24810 doc: /* *Non-nil means highlight trailing whitespace.
24811 The face used for trailing whitespace is `trailing-whitespace'. */);
24812 Vshow_trailing_whitespace = Qnil;
24813
24814 DEFVAR_LISP ("nobreak-char-display", &Vnobreak_char_display,
24815 doc: /* *Control highlighting of nobreak space and soft hyphen.
24816 A value of t means highlight the character itself (for nobreak space,
24817 use face `nobreak-space').
24818 A value of nil means no highlighting.
24819 Other values mean display the escape glyph followed by an ordinary
24820 space or ordinary hyphen. */);
24821 Vnobreak_char_display = Qt;
24822
24823 DEFVAR_LISP ("void-text-area-pointer", &Vvoid_text_area_pointer,
24824 doc: /* *The pointer shape to show in void text areas.
24825 A value of nil means to show the text pointer. Other options are `arrow',
24826 `text', `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
24827 Vvoid_text_area_pointer = Qarrow;
24828
24829 DEFVAR_LISP ("inhibit-redisplay", &Vinhibit_redisplay,
24830 doc: /* Non-nil means don't actually do any redisplay.
24831 This is used for internal purposes. */);
24832 Vinhibit_redisplay = Qnil;
24833
24834 DEFVAR_LISP ("global-mode-string", &Vglobal_mode_string,
24835 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
24836 Vglobal_mode_string = Qnil;
24837
24838 DEFVAR_LISP ("overlay-arrow-position", &Voverlay_arrow_position,
24839 doc: /* Marker for where to display an arrow on top of the buffer text.
24840 This must be the beginning of a line in order to work.
24841 See also `overlay-arrow-string'. */);
24842 Voverlay_arrow_position = Qnil;
24843
24844 DEFVAR_LISP ("overlay-arrow-string", &Voverlay_arrow_string,
24845 doc: /* String to display as an arrow in non-window frames.
24846 See also `overlay-arrow-position'. */);
24847 Voverlay_arrow_string = make_pure_c_string ("=>");
24848
24849 DEFVAR_LISP ("overlay-arrow-variable-list", &Voverlay_arrow_variable_list,
24850 doc: /* List of variables (symbols) which hold markers for overlay arrows.
24851 The symbols on this list are examined during redisplay to determine
24852 where to display overlay arrows. */);
24853 Voverlay_arrow_variable_list
24854 = Fcons (intern_c_string ("overlay-arrow-position"), Qnil);
24855
24856 DEFVAR_INT ("scroll-step", &scroll_step,
24857 doc: /* *The number of lines to try scrolling a window by when point moves out.
24858 If that fails to bring point back on frame, point is centered instead.
24859 If this is zero, point is always centered after it moves off frame.
24860 If you want scrolling to always be a line at a time, you should set
24861 `scroll-conservatively' to a large value rather than set this to 1. */);
24862
24863 DEFVAR_INT ("scroll-conservatively", &scroll_conservatively,
24864 doc: /* *Scroll up to this many lines, to bring point back on screen.
24865 If point moves off-screen, redisplay will scroll by up to
24866 `scroll-conservatively' lines in order to bring point just barely
24867 onto the screen again. If that cannot be done, then redisplay
24868 recenters point as usual.
24869
24870 A value of zero means always recenter point if it moves off screen. */);
24871 scroll_conservatively = 0;
24872
24873 DEFVAR_INT ("scroll-margin", &scroll_margin,
24874 doc: /* *Number of lines of margin at the top and bottom of a window.
24875 Recenter the window whenever point gets within this many lines
24876 of the top or bottom of the window. */);
24877 scroll_margin = 0;
24878
24879 DEFVAR_LISP ("display-pixels-per-inch", &Vdisplay_pixels_per_inch,
24880 doc: /* Pixels per inch value for non-window system displays.
24881 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
24882 Vdisplay_pixels_per_inch = make_float (72.0);
24883
24884 #if GLYPH_DEBUG
24885 DEFVAR_INT ("debug-end-pos", &debug_end_pos, doc: /* Don't ask. */);
24886 #endif
24887
24888 DEFVAR_LISP ("truncate-partial-width-windows",
24889 &Vtruncate_partial_width_windows,
24890 doc: /* Non-nil means truncate lines in windows narrower than the frame.
24891 For an integer value, truncate lines in each window narrower than the
24892 full frame width, provided the window width is less than that integer;
24893 otherwise, respect the value of `truncate-lines'.
24894
24895 For any other non-nil value, truncate lines in all windows that do
24896 not span the full frame width.
24897
24898 A value of nil means to respect the value of `truncate-lines'.
24899
24900 If `word-wrap' is enabled, you might want to reduce this. */);
24901 Vtruncate_partial_width_windows = make_number (50);
24902
24903 DEFVAR_BOOL ("mode-line-inverse-video", &mode_line_inverse_video,
24904 doc: /* When nil, display the mode-line/header-line/menu-bar in the default face.
24905 Any other value means to use the appropriate face, `mode-line',
24906 `header-line', or `menu' respectively. */);
24907 mode_line_inverse_video = 1;
24908
24909 DEFVAR_LISP ("line-number-display-limit", &Vline_number_display_limit,
24910 doc: /* *Maximum buffer size for which line number should be displayed.
24911 If the buffer is bigger than this, the line number does not appear
24912 in the mode line. A value of nil means no limit. */);
24913 Vline_number_display_limit = Qnil;
24914
24915 DEFVAR_INT ("line-number-display-limit-width",
24916 &line_number_display_limit_width,
24917 doc: /* *Maximum line width (in characters) for line number display.
24918 If the average length of the lines near point is bigger than this, then the
24919 line number may be omitted from the mode line. */);
24920 line_number_display_limit_width = 200;
24921
24922 DEFVAR_BOOL ("highlight-nonselected-windows", &highlight_nonselected_windows,
24923 doc: /* *Non-nil means highlight region even in nonselected windows. */);
24924 highlight_nonselected_windows = 0;
24925
24926 DEFVAR_BOOL ("multiple-frames", &multiple_frames,
24927 doc: /* Non-nil if more than one frame is visible on this display.
24928 Minibuffer-only frames don't count, but iconified frames do.
24929 This variable is not guaranteed to be accurate except while processing
24930 `frame-title-format' and `icon-title-format'. */);
24931
24932 DEFVAR_LISP ("frame-title-format", &Vframe_title_format,
24933 doc: /* Template for displaying the title bar of visible frames.
24934 \(Assuming the window manager supports this feature.)
24935
24936 This variable has the same structure as `mode-line-format', except that
24937 the %c and %l constructs are ignored. It is used only on frames for
24938 which no explicit name has been set \(see `modify-frame-parameters'). */);
24939
24940 DEFVAR_LISP ("icon-title-format", &Vicon_title_format,
24941 doc: /* Template for displaying the title bar of an iconified frame.
24942 \(Assuming the window manager supports this feature.)
24943 This variable has the same structure as `mode-line-format' (which see),
24944 and is used only on frames for which no explicit name has been set
24945 \(see `modify-frame-parameters'). */);
24946 Vicon_title_format
24947 = Vframe_title_format
24948 = pure_cons (intern_c_string ("multiple-frames"),
24949 pure_cons (make_pure_c_string ("%b"),
24950 pure_cons (pure_cons (empty_unibyte_string,
24951 pure_cons (intern_c_string ("invocation-name"),
24952 pure_cons (make_pure_c_string ("@"),
24953 pure_cons (intern_c_string ("system-name"),
24954 Qnil)))),
24955 Qnil)));
24956
24957 DEFVAR_LISP ("message-log-max", &Vmessage_log_max,
24958 doc: /* Maximum number of lines to keep in the message log buffer.
24959 If nil, disable message logging. If t, log messages but don't truncate
24960 the buffer when it becomes large. */);
24961 Vmessage_log_max = make_number (100);
24962
24963 DEFVAR_LISP ("window-size-change-functions", &Vwindow_size_change_functions,
24964 doc: /* Functions called before redisplay, if window sizes have changed.
24965 The value should be a list of functions that take one argument.
24966 Just before redisplay, for each frame, if any of its windows have changed
24967 size since the last redisplay, or have been split or deleted,
24968 all the functions in the list are called, with the frame as argument. */);
24969 Vwindow_size_change_functions = Qnil;
24970
24971 DEFVAR_LISP ("window-scroll-functions", &Vwindow_scroll_functions,
24972 doc: /* List of functions to call before redisplaying a window with scrolling.
24973 Each function is called with two arguments, the window and its new
24974 display-start position. Note that these functions are also called by
24975 `set-window-buffer'. Also note that the value of `window-end' is not
24976 valid when these functions are called. */);
24977 Vwindow_scroll_functions = Qnil;
24978
24979 DEFVAR_LISP ("window-text-change-functions",
24980 &Vwindow_text_change_functions,
24981 doc: /* Functions to call in redisplay when text in the window might change. */);
24982 Vwindow_text_change_functions = Qnil;
24983
24984 DEFVAR_LISP ("redisplay-end-trigger-functions", &Vredisplay_end_trigger_functions,
24985 doc: /* Functions called when redisplay of a window reaches the end trigger.
24986 Each function is called with two arguments, the window and the end trigger value.
24987 See `set-window-redisplay-end-trigger'. */);
24988 Vredisplay_end_trigger_functions = Qnil;
24989
24990 DEFVAR_LISP ("mouse-autoselect-window", &Vmouse_autoselect_window,
24991 doc: /* *Non-nil means autoselect window with mouse pointer.
24992 If nil, do not autoselect windows.
24993 A positive number means delay autoselection by that many seconds: a
24994 window is autoselected only after the mouse has remained in that
24995 window for the duration of the delay.
24996 A negative number has a similar effect, but causes windows to be
24997 autoselected only after the mouse has stopped moving. \(Because of
24998 the way Emacs compares mouse events, you will occasionally wait twice
24999 that time before the window gets selected.\)
25000 Any other value means to autoselect window instantaneously when the
25001 mouse pointer enters it.
25002
25003 Autoselection selects the minibuffer only if it is active, and never
25004 unselects the minibuffer if it is active.
25005
25006 When customizing this variable make sure that the actual value of
25007 `focus-follows-mouse' matches the behavior of your window manager. */);
25008 Vmouse_autoselect_window = Qnil;
25009
25010 DEFVAR_LISP ("auto-resize-tool-bars", &Vauto_resize_tool_bars,
25011 doc: /* *Non-nil means automatically resize tool-bars.
25012 This dynamically changes the tool-bar's height to the minimum height
25013 that is needed to make all tool-bar items visible.
25014 If value is `grow-only', the tool-bar's height is only increased
25015 automatically; to decrease the tool-bar height, use \\[recenter]. */);
25016 Vauto_resize_tool_bars = Qt;
25017
25018 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", &auto_raise_tool_bar_buttons_p,
25019 doc: /* *Non-nil means raise tool-bar buttons when the mouse moves over them. */);
25020 auto_raise_tool_bar_buttons_p = 1;
25021
25022 DEFVAR_BOOL ("make-cursor-line-fully-visible", &make_cursor_line_fully_visible_p,
25023 doc: /* *Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
25024 make_cursor_line_fully_visible_p = 1;
25025
25026 DEFVAR_LISP ("tool-bar-border", &Vtool_bar_border,
25027 doc: /* *Border below tool-bar in pixels.
25028 If an integer, use it as the height of the border.
25029 If it is one of `internal-border-width' or `border-width', use the
25030 value of the corresponding frame parameter.
25031 Otherwise, no border is added below the tool-bar. */);
25032 Vtool_bar_border = Qinternal_border_width;
25033
25034 DEFVAR_LISP ("tool-bar-button-margin", &Vtool_bar_button_margin,
25035 doc: /* *Margin around tool-bar buttons in pixels.
25036 If an integer, use that for both horizontal and vertical margins.
25037 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
25038 HORZ specifying the horizontal margin, and VERT specifying the
25039 vertical margin. */);
25040 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
25041
25042 DEFVAR_INT ("tool-bar-button-relief", &tool_bar_button_relief,
25043 doc: /* *Relief thickness of tool-bar buttons. */);
25044 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
25045
25046 DEFVAR_LISP ("fontification-functions", &Vfontification_functions,
25047 doc: /* List of functions to call to fontify regions of text.
25048 Each function is called with one argument POS. Functions must
25049 fontify a region starting at POS in the current buffer, and give
25050 fontified regions the property `fontified'. */);
25051 Vfontification_functions = Qnil;
25052 Fmake_variable_buffer_local (Qfontification_functions);
25053
25054 DEFVAR_BOOL ("unibyte-display-via-language-environment",
25055 &unibyte_display_via_language_environment,
25056 doc: /* *Non-nil means display unibyte text according to language environment.
25057 Specifically, this means that raw bytes in the range 160-255 decimal
25058 are displayed by converting them to the equivalent multibyte characters
25059 according to the current language environment. As a result, they are
25060 displayed according to the current fontset.
25061
25062 Note that this variable affects only how these bytes are displayed,
25063 but does not change the fact they are interpreted as raw bytes. */);
25064 unibyte_display_via_language_environment = 0;
25065
25066 DEFVAR_LISP ("max-mini-window-height", &Vmax_mini_window_height,
25067 doc: /* *Maximum height for resizing mini-windows.
25068 If a float, it specifies a fraction of the mini-window frame's height.
25069 If an integer, it specifies a number of lines. */);
25070 Vmax_mini_window_height = make_float (0.25);
25071
25072 DEFVAR_LISP ("resize-mini-windows", &Vresize_mini_windows,
25073 doc: /* *How to resize mini-windows.
25074 A value of nil means don't automatically resize mini-windows.
25075 A value of t means resize them to fit the text displayed in them.
25076 A value of `grow-only', the default, means let mini-windows grow
25077 only, until their display becomes empty, at which point the windows
25078 go back to their normal size. */);
25079 Vresize_mini_windows = Qgrow_only;
25080
25081 DEFVAR_LISP ("blink-cursor-alist", &Vblink_cursor_alist,
25082 doc: /* Alist specifying how to blink the cursor off.
25083 Each element has the form (ON-STATE . OFF-STATE). Whenever the
25084 `cursor-type' frame-parameter or variable equals ON-STATE,
25085 comparing using `equal', Emacs uses OFF-STATE to specify
25086 how to blink it off. ON-STATE and OFF-STATE are values for
25087 the `cursor-type' frame parameter.
25088
25089 If a frame's ON-STATE has no entry in this list,
25090 the frame's other specifications determine how to blink the cursor off. */);
25091 Vblink_cursor_alist = Qnil;
25092
25093 DEFVAR_BOOL ("auto-hscroll-mode", &automatic_hscrolling_p,
25094 doc: /* *Non-nil means scroll the display automatically to make point visible. */);
25095 automatic_hscrolling_p = 1;
25096 Qauto_hscroll_mode = intern_c_string ("auto-hscroll-mode");
25097 staticpro (&Qauto_hscroll_mode);
25098
25099 DEFVAR_INT ("hscroll-margin", &hscroll_margin,
25100 doc: /* *How many columns away from the window edge point is allowed to get
25101 before automatic hscrolling will horizontally scroll the window. */);
25102 hscroll_margin = 5;
25103
25104 DEFVAR_LISP ("hscroll-step", &Vhscroll_step,
25105 doc: /* *How many columns to scroll the window when point gets too close to the edge.
25106 When point is less than `hscroll-margin' columns from the window
25107 edge, automatic hscrolling will scroll the window by the amount of columns
25108 determined by this variable. If its value is a positive integer, scroll that
25109 many columns. If it's a positive floating-point number, it specifies the
25110 fraction of the window's width to scroll. If it's nil or zero, point will be
25111 centered horizontally after the scroll. Any other value, including negative
25112 numbers, are treated as if the value were zero.
25113
25114 Automatic hscrolling always moves point outside the scroll margin, so if
25115 point was more than scroll step columns inside the margin, the window will
25116 scroll more than the value given by the scroll step.
25117
25118 Note that the lower bound for automatic hscrolling specified by `scroll-left'
25119 and `scroll-right' overrides this variable's effect. */);
25120 Vhscroll_step = make_number (0);
25121
25122 DEFVAR_BOOL ("message-truncate-lines", &message_truncate_lines,
25123 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
25124 Bind this around calls to `message' to let it take effect. */);
25125 message_truncate_lines = 0;
25126
25127 DEFVAR_LISP ("menu-bar-update-hook", &Vmenu_bar_update_hook,
25128 doc: /* Normal hook run to update the menu bar definitions.
25129 Redisplay runs this hook before it redisplays the menu bar.
25130 This is used to update submenus such as Buffers,
25131 whose contents depend on various data. */);
25132 Vmenu_bar_update_hook = Qnil;
25133
25134 DEFVAR_LISP ("menu-updating-frame", &Vmenu_updating_frame,
25135 doc: /* Frame for which we are updating a menu.
25136 The enable predicate for a menu binding should check this variable. */);
25137 Vmenu_updating_frame = Qnil;
25138
25139 DEFVAR_BOOL ("inhibit-menubar-update", &inhibit_menubar_update,
25140 doc: /* Non-nil means don't update menu bars. Internal use only. */);
25141 inhibit_menubar_update = 0;
25142
25143 DEFVAR_LISP ("wrap-prefix", &Vwrap_prefix,
25144 doc: /* Prefix prepended to all continuation lines at display time.
25145 The value may be a string, an image, or a stretch-glyph; it is
25146 interpreted in the same way as the value of a `display' text property.
25147
25148 This variable is overridden by any `wrap-prefix' text or overlay
25149 property.
25150
25151 To add a prefix to non-continuation lines, use `line-prefix'. */);
25152 Vwrap_prefix = Qnil;
25153 staticpro (&Qwrap_prefix);
25154 Qwrap_prefix = intern_c_string ("wrap-prefix");
25155 Fmake_variable_buffer_local (Qwrap_prefix);
25156
25157 DEFVAR_LISP ("line-prefix", &Vline_prefix,
25158 doc: /* Prefix prepended to all non-continuation lines at display time.
25159 The value may be a string, an image, or a stretch-glyph; it is
25160 interpreted in the same way as the value of a `display' text property.
25161
25162 This variable is overridden by any `line-prefix' text or overlay
25163 property.
25164
25165 To add a prefix to continuation lines, use `wrap-prefix'. */);
25166 Vline_prefix = Qnil;
25167 staticpro (&Qline_prefix);
25168 Qline_prefix = intern_c_string ("line-prefix");
25169 Fmake_variable_buffer_local (Qline_prefix);
25170
25171 DEFVAR_BOOL ("inhibit-eval-during-redisplay", &inhibit_eval_during_redisplay,
25172 doc: /* Non-nil means don't eval Lisp during redisplay. */);
25173 inhibit_eval_during_redisplay = 0;
25174
25175 DEFVAR_BOOL ("inhibit-free-realized-faces", &inhibit_free_realized_faces,
25176 doc: /* Non-nil means don't free realized faces. Internal use only. */);
25177 inhibit_free_realized_faces = 0;
25178
25179 #if GLYPH_DEBUG
25180 DEFVAR_BOOL ("inhibit-try-window-id", &inhibit_try_window_id,
25181 doc: /* Inhibit try_window_id display optimization. */);
25182 inhibit_try_window_id = 0;
25183
25184 DEFVAR_BOOL ("inhibit-try-window-reusing", &inhibit_try_window_reusing,
25185 doc: /* Inhibit try_window_reusing display optimization. */);
25186 inhibit_try_window_reusing = 0;
25187
25188 DEFVAR_BOOL ("inhibit-try-cursor-movement", &inhibit_try_cursor_movement,
25189 doc: /* Inhibit try_cursor_movement display optimization. */);
25190 inhibit_try_cursor_movement = 0;
25191 #endif /* GLYPH_DEBUG */
25192
25193 DEFVAR_INT ("overline-margin", &overline_margin,
25194 doc: /* *Space between overline and text, in pixels.
25195 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
25196 margin to the caracter height. */);
25197 overline_margin = 2;
25198
25199 DEFVAR_INT ("underline-minimum-offset",
25200 &underline_minimum_offset,
25201 doc: /* Minimum distance between baseline and underline.
25202 This can improve legibility of underlined text at small font sizes,
25203 particularly when using variable `x-use-underline-position-properties'
25204 with fonts that specify an UNDERLINE_POSITION relatively close to the
25205 baseline. The default value is 1. */);
25206 underline_minimum_offset = 1;
25207
25208 DEFVAR_BOOL ("display-hourglass", &display_hourglass_p,
25209 doc: /* Non-zero means Emacs displays an hourglass pointer on window systems. */);
25210 display_hourglass_p = 1;
25211
25212 DEFVAR_LISP ("hourglass-delay", &Vhourglass_delay,
25213 doc: /* *Seconds to wait before displaying an hourglass pointer.
25214 Value must be an integer or float. */);
25215 Vhourglass_delay = make_number (DEFAULT_HOURGLASS_DELAY);
25216
25217 hourglass_atimer = NULL;
25218 hourglass_shown_p = 0;
25219 }
25220
25221
25222 /* Initialize this module when Emacs starts. */
25223
25224 void
25225 init_xdisp ()
25226 {
25227 Lisp_Object root_window;
25228 struct window *mini_w;
25229
25230 current_header_line_height = current_mode_line_height = -1;
25231
25232 CHARPOS (this_line_start_pos) = 0;
25233
25234 mini_w = XWINDOW (minibuf_window);
25235 root_window = FRAME_ROOT_WINDOW (XFRAME (WINDOW_FRAME (mini_w)));
25236
25237 if (!noninteractive)
25238 {
25239 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (root_window)));
25240 int i;
25241
25242 XWINDOW (root_window)->top_line = make_number (FRAME_TOP_MARGIN (f));
25243 set_window_height (root_window,
25244 FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f),
25245 0);
25246 mini_w->top_line = make_number (FRAME_LINES (f) - 1);
25247 set_window_height (minibuf_window, 1, 0);
25248
25249 XWINDOW (root_window)->total_cols = make_number (FRAME_COLS (f));
25250 mini_w->total_cols = make_number (FRAME_COLS (f));
25251
25252 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
25253 scratch_glyph_row.glyphs[TEXT_AREA + 1]
25254 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
25255
25256 /* The default ellipsis glyphs `...'. */
25257 for (i = 0; i < 3; ++i)
25258 default_invis_vector[i] = make_number ('.');
25259 }
25260
25261 {
25262 /* Allocate the buffer for frame titles.
25263 Also used for `format-mode-line'. */
25264 int size = 100;
25265 mode_line_noprop_buf = (char *) xmalloc (size);
25266 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
25267 mode_line_noprop_ptr = mode_line_noprop_buf;
25268 mode_line_target = MODE_LINE_DISPLAY;
25269 }
25270
25271 help_echo_showing_p = 0;
25272 }
25273
25274 /* Since w32 does not support atimers, it defines its own implementation of
25275 the following three functions in w32fns.c. */
25276 #ifndef WINDOWSNT
25277
25278 /* Platform-independent portion of hourglass implementation. */
25279
25280 /* Return non-zero if houglass timer has been started or hourglass is shown. */
25281 int
25282 hourglass_started ()
25283 {
25284 return hourglass_shown_p || hourglass_atimer != NULL;
25285 }
25286
25287 /* Cancel a currently active hourglass timer, and start a new one. */
25288 void
25289 start_hourglass ()
25290 {
25291 #if defined (HAVE_WINDOW_SYSTEM)
25292 EMACS_TIME delay;
25293 int secs, usecs = 0;
25294
25295 cancel_hourglass ();
25296
25297 if (INTEGERP (Vhourglass_delay)
25298 && XINT (Vhourglass_delay) > 0)
25299 secs = XFASTINT (Vhourglass_delay);
25300 else if (FLOATP (Vhourglass_delay)
25301 && XFLOAT_DATA (Vhourglass_delay) > 0)
25302 {
25303 Lisp_Object tem;
25304 tem = Ftruncate (Vhourglass_delay, Qnil);
25305 secs = XFASTINT (tem);
25306 usecs = (XFLOAT_DATA (Vhourglass_delay) - secs) * 1000000;
25307 }
25308 else
25309 secs = DEFAULT_HOURGLASS_DELAY;
25310
25311 EMACS_SET_SECS_USECS (delay, secs, usecs);
25312 hourglass_atimer = start_atimer (ATIMER_RELATIVE, delay,
25313 show_hourglass, NULL);
25314 #endif
25315 }
25316
25317
25318 /* Cancel the hourglass cursor timer if active, hide a busy cursor if
25319 shown. */
25320 void
25321 cancel_hourglass ()
25322 {
25323 #if defined (HAVE_WINDOW_SYSTEM)
25324 if (hourglass_atimer)
25325 {
25326 cancel_atimer (hourglass_atimer);
25327 hourglass_atimer = NULL;
25328 }
25329
25330 if (hourglass_shown_p)
25331 hide_hourglass ();
25332 #endif
25333 }
25334 #endif /* ! WINDOWSNT */
25335
25336 /* arch-tag: eacc864d-bb6a-4b74-894a-1a4399a1358b
25337 (do not change this comment) */