(update_tool_bar_unwind): New function.
[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 Free Software Foundation, Inc.
5
6 This file is part of GNU Emacs.
7
8 GNU Emacs is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
12
13 GNU Emacs is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
20
21 /* New redisplay written by Gerd Moellmann <gerd@gnu.org>.
22
23 Redisplay.
24
25 Emacs separates the task of updating the display from code
26 modifying global state, e.g. buffer text. This way functions
27 operating on buffers don't also have to be concerned with updating
28 the display.
29
30 Updating the display is triggered by the Lisp interpreter when it
31 decides it's time to do it. This is done either automatically for
32 you as part of the interpreter's command loop or as the result of
33 calling Lisp functions like `sit-for'. The C function `redisplay'
34 in xdisp.c is the only entry into the inner redisplay code. (Or,
35 let's say almost---see the description of direct update
36 operations, below.)
37
38 The following diagram shows how redisplay code is invoked. As you
39 can see, Lisp calls redisplay and vice versa. Under window systems
40 like X, some portions of the redisplay code are also called
41 asynchronously during mouse movement or expose events. It is very
42 important that these code parts do NOT use the C library (malloc,
43 free) because many C libraries under Unix are not reentrant. They
44 may also NOT call functions of the Lisp interpreter which could
45 change the interpreter's state. If you don't follow these rules,
46 you will encounter bugs which are very hard to explain.
47
48 (Direct functions, see below)
49 direct_output_for_insert,
50 direct_forward_char (dispnew.c)
51 +---------------------------------+
52 | |
53 | V
54 +--------------+ redisplay +----------------+
55 | Lisp machine |---------------->| Redisplay code |<--+
56 +--------------+ (xdisp.c) +----------------+ |
57 ^ | |
58 +----------------------------------+ |
59 Don't use this path when called |
60 asynchronously! |
61 |
62 expose_window (asynchronous) |
63 |
64 X expose events -----+
65
66 What does redisplay do? Obviously, it has to figure out somehow what
67 has been changed since the last time the display has been updated,
68 and to make these changes visible. Preferably it would do that in
69 a moderately intelligent way, i.e. fast.
70
71 Changes in buffer text can be deduced from window and buffer
72 structures, and from some global variables like `beg_unchanged' and
73 `end_unchanged'. The contents of the display are additionally
74 recorded in a `glyph matrix', a two-dimensional matrix of glyph
75 structures. Each row in such a matrix corresponds to a line on the
76 display, and each glyph in a row corresponds to a column displaying
77 a character, an image, or what else. This matrix is called the
78 `current glyph matrix' or `current matrix' in redisplay
79 terminology.
80
81 For buffer parts that have been changed since the last update, a
82 second glyph matrix is constructed, the so called `desired glyph
83 matrix' or short `desired matrix'. Current and desired matrix are
84 then compared to find a cheap way to update the display, e.g. by
85 reusing part of the display by scrolling lines.
86
87
88 Direct operations.
89
90 You will find a lot of redisplay optimizations when you start
91 looking at the innards of redisplay. The overall goal of all these
92 optimizations is to make redisplay fast because it is done
93 frequently.
94
95 Two optimizations are not found in xdisp.c. These are the direct
96 operations mentioned above. As the name suggests they follow a
97 different principle than the rest of redisplay. Instead of
98 building a desired matrix and then comparing it with the current
99 display, they perform their actions directly on the display and on
100 the current matrix.
101
102 One direct operation updates the display after one character has
103 been entered. The other one moves the cursor by one position
104 forward or backward. You find these functions under the names
105 `direct_output_for_insert' and `direct_output_forward_char' in
106 dispnew.c.
107
108
109 Desired matrices.
110
111 Desired matrices are always built per Emacs window. The function
112 `display_line' is the central function to look at if you are
113 interested. It constructs one row in a desired matrix given an
114 iterator structure containing both a buffer position and a
115 description of the environment in which the text is to be
116 displayed. But this is too early, read on.
117
118 Characters and pixmaps displayed for a range of buffer text depend
119 on various settings of buffers and windows, on overlays and text
120 properties, on display tables, on selective display. The good news
121 is that all this hairy stuff is hidden behind a small set of
122 interface functions taking an iterator structure (struct it)
123 argument.
124
125 Iteration over things to be displayed is then simple. It is
126 started by initializing an iterator with a call to init_iterator.
127 Calls to get_next_display_element fill the iterator structure with
128 relevant information about the next thing to display. Calls to
129 set_iterator_to_next move the iterator to the next thing.
130
131 Besides this, an iterator also contains information about the
132 display environment in which glyphs for display elements are to be
133 produced. It has fields for the width and height of the display,
134 the information whether long lines are truncated or continued, a
135 current X and Y position, and lots of other stuff you can better
136 see in dispextern.h.
137
138 Glyphs in a desired matrix are normally constructed in a loop
139 calling get_next_display_element and then produce_glyphs. The call
140 to produce_glyphs will fill the iterator structure with pixel
141 information about the element being displayed and at the same time
142 produce glyphs for it. If the display element fits on the line
143 being displayed, set_iterator_to_next is called next, otherwise the
144 glyphs produced are discarded.
145
146
147 Frame matrices.
148
149 That just couldn't be all, could it? What about terminal types not
150 supporting operations on sub-windows of the screen? To update the
151 display on such a terminal, window-based glyph matrices are not
152 well suited. To be able to reuse part of the display (scrolling
153 lines up and down), we must instead have a view of the whole
154 screen. This is what `frame matrices' are for. They are a trick.
155
156 Frames on terminals like above have a glyph pool. Windows on such
157 a frame sub-allocate their glyph memory from their frame's glyph
158 pool. The frame itself is given its own glyph matrices. By
159 coincidence---or maybe something else---rows in window glyph
160 matrices are slices of corresponding rows in frame matrices. Thus
161 writing to window matrices implicitly updates a frame matrix which
162 provides us with the view of the whole screen that we originally
163 wanted to have without having to move many bytes around. To be
164 honest, there is a little bit more done, but not much more. If you
165 plan to extend that code, take a look at dispnew.c. The function
166 build_frame_matrix is a good starting point. */
167
168 #include <config.h>
169 #include <stdio.h>
170 #include <limits.h>
171
172 #include "lisp.h"
173 #include "keyboard.h"
174 #include "frame.h"
175 #include "window.h"
176 #include "termchar.h"
177 #include "dispextern.h"
178 #include "buffer.h"
179 #include "character.h"
180 #include "charset.h"
181 #include "indent.h"
182 #include "commands.h"
183 #include "keymap.h"
184 #include "macros.h"
185 #include "disptab.h"
186 #include "termhooks.h"
187 #include "intervals.h"
188 #include "coding.h"
189 #include "process.h"
190 #include "region-cache.h"
191 #include "fontset.h"
192 #include "blockinput.h"
193
194 #ifdef HAVE_X_WINDOWS
195 #include "xterm.h"
196 #endif
197 #ifdef WINDOWSNT
198 #include "w32term.h"
199 #endif
200 #ifdef HAVE_NS
201 #include "nsterm.h"
202 #endif
203 #ifdef USE_GTK
204 #include "gtkutil.h"
205 #endif
206
207 #include "font.h"
208
209 #ifndef FRAME_X_OUTPUT
210 #define FRAME_X_OUTPUT(f) ((f)->output_data.x)
211 #endif
212
213 #define INFINITY 10000000
214
215 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
216 || defined(HAVE_NS) || defined (USE_GTK)
217 extern void set_frame_menubar P_ ((struct frame *f, int, int));
218 extern int pending_menu_activation;
219 #endif
220
221 extern int interrupt_input;
222 extern int command_loop_level;
223
224 extern Lisp_Object do_mouse_tracking;
225
226 extern int minibuffer_auto_raise;
227 extern Lisp_Object Vminibuffer_list;
228
229 extern Lisp_Object Qface;
230 extern Lisp_Object Qmode_line, Qmode_line_inactive, Qheader_line;
231
232 extern Lisp_Object Voverriding_local_map;
233 extern Lisp_Object Voverriding_local_map_menu_flag;
234 extern Lisp_Object Qmenu_item;
235 extern Lisp_Object Qwhen;
236 extern Lisp_Object Qhelp_echo;
237
238 Lisp_Object Qoverriding_local_map, Qoverriding_terminal_local_map;
239 Lisp_Object Qwindow_scroll_functions, Vwindow_scroll_functions;
240 Lisp_Object Qwindow_text_change_functions, Vwindow_text_change_functions;
241 Lisp_Object Qredisplay_end_trigger_functions, Vredisplay_end_trigger_functions;
242 Lisp_Object Qinhibit_point_motion_hooks;
243 Lisp_Object QCeval, QCfile, QCdata, QCpropertize;
244 Lisp_Object Qfontified;
245 Lisp_Object Qgrow_only;
246 Lisp_Object Qinhibit_eval_during_redisplay;
247 Lisp_Object Qbuffer_position, Qposition, Qobject;
248
249 /* Cursor shapes */
250 Lisp_Object Qbar, Qhbar, Qbox, Qhollow;
251
252 /* Pointer shapes */
253 Lisp_Object Qarrow, Qhand, Qtext;
254
255 Lisp_Object Qrisky_local_variable;
256
257 /* Holds the list (error). */
258 Lisp_Object list_of_error;
259
260 /* Functions called to fontify regions of text. */
261
262 Lisp_Object Vfontification_functions;
263 Lisp_Object Qfontification_functions;
264
265 /* Non-nil means automatically select any window when the mouse
266 cursor moves into it. */
267 Lisp_Object Vmouse_autoselect_window;
268
269 Lisp_Object Vwrap_prefix, Qwrap_prefix;
270 Lisp_Object Vline_prefix, Qline_prefix;
271
272 /* Non-zero means draw tool bar buttons raised when the mouse moves
273 over them. */
274
275 int auto_raise_tool_bar_buttons_p;
276
277 /* Non-zero means to reposition window if cursor line is only partially visible. */
278
279 int make_cursor_line_fully_visible_p;
280
281 /* Margin below tool bar in pixels. 0 or nil means no margin.
282 If value is `internal-border-width' or `border-width',
283 the corresponding frame parameter is used. */
284
285 Lisp_Object Vtool_bar_border;
286
287 /* Margin around tool bar buttons in pixels. */
288
289 Lisp_Object Vtool_bar_button_margin;
290
291 /* Thickness of shadow to draw around tool bar buttons. */
292
293 EMACS_INT tool_bar_button_relief;
294
295 /* Non-nil means automatically resize tool-bars so that all tool-bar
296 items are visible, and no blank lines remain.
297
298 If value is `grow-only', only make tool-bar bigger. */
299
300 Lisp_Object Vauto_resize_tool_bars;
301
302 /* Non-zero means draw block and hollow cursor as wide as the glyph
303 under it. For example, if a block cursor is over a tab, it will be
304 drawn as wide as that tab on the display. */
305
306 int x_stretch_cursor_p;
307
308 /* Non-nil means don't actually do any redisplay. */
309
310 Lisp_Object Vinhibit_redisplay, Qinhibit_redisplay;
311
312 /* Non-zero means Lisp evaluation during redisplay is inhibited. */
313
314 int inhibit_eval_during_redisplay;
315
316 /* Names of text properties relevant for redisplay. */
317
318 Lisp_Object Qdisplay;
319 extern Lisp_Object Qface, Qinvisible, Qwidth;
320
321 /* Symbols used in text property values. */
322
323 Lisp_Object Vdisplay_pixels_per_inch;
324 Lisp_Object Qspace, QCalign_to, QCrelative_width, QCrelative_height;
325 Lisp_Object Qleft_margin, Qright_margin, Qspace_width, Qraise;
326 Lisp_Object Qslice;
327 Lisp_Object Qcenter;
328 Lisp_Object Qmargin, Qpointer;
329 Lisp_Object Qline_height;
330 extern Lisp_Object Qheight;
331 extern Lisp_Object QCwidth, QCheight, QCascent;
332 extern Lisp_Object Qscroll_bar;
333 extern Lisp_Object Qcursor;
334
335 /* Non-nil means highlight trailing whitespace. */
336
337 Lisp_Object Vshow_trailing_whitespace;
338
339 /* Non-nil means escape non-break space and hyphens. */
340
341 Lisp_Object Vnobreak_char_display;
342
343 #ifdef HAVE_WINDOW_SYSTEM
344 extern Lisp_Object Voverflow_newline_into_fringe;
345
346 /* Test if overflow newline into fringe. Called with iterator IT
347 at or past right window margin, and with IT->current_x set. */
348
349 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(it) \
350 (!NILP (Voverflow_newline_into_fringe) \
351 && FRAME_WINDOW_P (it->f) \
352 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) > 0 \
353 && it->current_x == it->last_visible_x \
354 && it->line_wrap != WORD_WRAP)
355
356 #endif /* HAVE_WINDOW_SYSTEM */
357
358 /* Test if the display element loaded in IT is a space or tab
359 character. This is used to determine word wrapping. */
360
361 #define IT_DISPLAYING_WHITESPACE(it) \
362 (it->what == IT_CHARACTER && (it->c == ' ' || it->c == '\t'))
363
364 /* Non-nil means show the text cursor in void text areas
365 i.e. in blank areas after eol and eob. This used to be
366 the default in 21.3. */
367
368 Lisp_Object Vvoid_text_area_pointer;
369
370 /* Name of the face used to highlight trailing whitespace. */
371
372 Lisp_Object Qtrailing_whitespace;
373
374 /* Name and number of the face used to highlight escape glyphs. */
375
376 Lisp_Object Qescape_glyph;
377
378 /* Name and number of the face used to highlight non-breaking spaces. */
379
380 Lisp_Object Qnobreak_space;
381
382 /* The symbol `image' which is the car of the lists used to represent
383 images in Lisp. */
384
385 Lisp_Object Qimage;
386
387 /* The image map types. */
388 Lisp_Object QCmap, QCpointer;
389 Lisp_Object Qrect, Qcircle, Qpoly;
390
391 /* Non-zero means print newline to stdout before next mini-buffer
392 message. */
393
394 int noninteractive_need_newline;
395
396 /* Non-zero means print newline to message log before next message. */
397
398 static int message_log_need_newline;
399
400 /* Three markers that message_dolog uses.
401 It could allocate them itself, but that causes trouble
402 in handling memory-full errors. */
403 static Lisp_Object message_dolog_marker1;
404 static Lisp_Object message_dolog_marker2;
405 static Lisp_Object message_dolog_marker3;
406 \f
407 /* The buffer position of the first character appearing entirely or
408 partially on the line of the selected window which contains the
409 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
410 redisplay optimization in redisplay_internal. */
411
412 static struct text_pos this_line_start_pos;
413
414 /* Number of characters past the end of the line above, including the
415 terminating newline. */
416
417 static struct text_pos this_line_end_pos;
418
419 /* The vertical positions and the height of this line. */
420
421 static int this_line_vpos;
422 static int this_line_y;
423 static int this_line_pixel_height;
424
425 /* X position at which this display line starts. Usually zero;
426 negative if first character is partially visible. */
427
428 static int this_line_start_x;
429
430 /* Buffer that this_line_.* variables are referring to. */
431
432 static struct buffer *this_line_buffer;
433
434 /* Nonzero means truncate lines in all windows less wide than the
435 frame. */
436
437 Lisp_Object Vtruncate_partial_width_windows;
438
439 /* A flag to control how to display unibyte 8-bit character. */
440
441 int unibyte_display_via_language_environment;
442
443 /* Nonzero means we have more than one non-mini-buffer-only frame.
444 Not guaranteed to be accurate except while parsing
445 frame-title-format. */
446
447 int multiple_frames;
448
449 Lisp_Object Vglobal_mode_string;
450
451
452 /* List of variables (symbols) which hold markers for overlay arrows.
453 The symbols on this list are examined during redisplay to determine
454 where to display overlay arrows. */
455
456 Lisp_Object Voverlay_arrow_variable_list;
457
458 /* Marker for where to display an arrow on top of the buffer text. */
459
460 Lisp_Object Voverlay_arrow_position;
461
462 /* String to display for the arrow. Only used on terminal frames. */
463
464 Lisp_Object Voverlay_arrow_string;
465
466 /* Values of those variables at last redisplay are stored as
467 properties on `overlay-arrow-position' symbol. However, if
468 Voverlay_arrow_position is a marker, last-arrow-position is its
469 numerical position. */
470
471 Lisp_Object Qlast_arrow_position, Qlast_arrow_string;
472
473 /* Alternative overlay-arrow-string and overlay-arrow-bitmap
474 properties on a symbol in overlay-arrow-variable-list. */
475
476 Lisp_Object Qoverlay_arrow_string, Qoverlay_arrow_bitmap;
477
478 /* Like mode-line-format, but for the title bar on a visible frame. */
479
480 Lisp_Object Vframe_title_format;
481
482 /* Like mode-line-format, but for the title bar on an iconified frame. */
483
484 Lisp_Object Vicon_title_format;
485
486 /* List of functions to call when a window's size changes. These
487 functions get one arg, a frame on which one or more windows' sizes
488 have changed. */
489
490 static Lisp_Object Vwindow_size_change_functions;
491
492 Lisp_Object Qmenu_bar_update_hook, Vmenu_bar_update_hook;
493
494 /* Nonzero if an overlay arrow has been displayed in this window. */
495
496 static int overlay_arrow_seen;
497
498 /* Nonzero means highlight the region even in nonselected windows. */
499
500 int highlight_nonselected_windows;
501
502 /* If cursor motion alone moves point off frame, try scrolling this
503 many lines up or down if that will bring it back. */
504
505 static EMACS_INT scroll_step;
506
507 /* Nonzero means scroll just far enough to bring point back on the
508 screen, when appropriate. */
509
510 static EMACS_INT scroll_conservatively;
511
512 /* Recenter the window whenever point gets within this many lines of
513 the top or bottom of the window. This value is translated into a
514 pixel value by multiplying it with FRAME_LINE_HEIGHT, which means
515 that there is really a fixed pixel height scroll margin. */
516
517 EMACS_INT scroll_margin;
518
519 /* Number of windows showing the buffer of the selected window (or
520 another buffer with the same base buffer). keyboard.c refers to
521 this. */
522
523 int buffer_shared;
524
525 /* Vector containing glyphs for an ellipsis `...'. */
526
527 static Lisp_Object default_invis_vector[3];
528
529 /* Zero means display the mode-line/header-line/menu-bar in the default face
530 (this slightly odd definition is for compatibility with previous versions
531 of emacs), non-zero means display them using their respective faces.
532
533 This variable is deprecated. */
534
535 int mode_line_inverse_video;
536
537 /* Prompt to display in front of the mini-buffer contents. */
538
539 Lisp_Object minibuf_prompt;
540
541 /* Width of current mini-buffer prompt. Only set after display_line
542 of the line that contains the prompt. */
543
544 int minibuf_prompt_width;
545
546 /* This is the window where the echo area message was displayed. It
547 is always a mini-buffer window, but it may not be the same window
548 currently active as a mini-buffer. */
549
550 Lisp_Object echo_area_window;
551
552 /* List of pairs (MESSAGE . MULTIBYTE). The function save_message
553 pushes the current message and the value of
554 message_enable_multibyte on the stack, the function restore_message
555 pops the stack and displays MESSAGE again. */
556
557 Lisp_Object Vmessage_stack;
558
559 /* Nonzero means multibyte characters were enabled when the echo area
560 message was specified. */
561
562 int message_enable_multibyte;
563
564 /* Nonzero if we should redraw the mode lines on the next redisplay. */
565
566 int update_mode_lines;
567
568 /* Nonzero if window sizes or contents have changed since last
569 redisplay that finished. */
570
571 int windows_or_buffers_changed;
572
573 /* Nonzero means a frame's cursor type has been changed. */
574
575 int cursor_type_changed;
576
577 /* Nonzero after display_mode_line if %l was used and it displayed a
578 line number. */
579
580 int line_number_displayed;
581
582 /* Maximum buffer size for which to display line numbers. */
583
584 Lisp_Object Vline_number_display_limit;
585
586 /* Line width to consider when repositioning for line number display. */
587
588 static EMACS_INT line_number_display_limit_width;
589
590 /* Number of lines to keep in the message log buffer. t means
591 infinite. nil means don't log at all. */
592
593 Lisp_Object Vmessage_log_max;
594
595 /* The name of the *Messages* buffer, a string. */
596
597 static Lisp_Object Vmessages_buffer_name;
598
599 /* Current, index 0, and last displayed echo area message. Either
600 buffers from echo_buffers, or nil to indicate no message. */
601
602 Lisp_Object echo_area_buffer[2];
603
604 /* The buffers referenced from echo_area_buffer. */
605
606 static Lisp_Object echo_buffer[2];
607
608 /* A vector saved used in with_area_buffer to reduce consing. */
609
610 static Lisp_Object Vwith_echo_area_save_vector;
611
612 /* Non-zero means display_echo_area should display the last echo area
613 message again. Set by redisplay_preserve_echo_area. */
614
615 static int display_last_displayed_message_p;
616
617 /* Nonzero if echo area is being used by print; zero if being used by
618 message. */
619
620 int message_buf_print;
621
622 /* The symbol `inhibit-menubar-update' and its DEFVAR_BOOL variable. */
623
624 Lisp_Object Qinhibit_menubar_update;
625 int inhibit_menubar_update;
626
627 /* When evaluating expressions from menu bar items (enable conditions,
628 for instance), this is the frame they are being processed for. */
629
630 Lisp_Object Vmenu_updating_frame;
631
632 /* Maximum height for resizing mini-windows. Either a float
633 specifying a fraction of the available height, or an integer
634 specifying a number of lines. */
635
636 Lisp_Object Vmax_mini_window_height;
637
638 /* Non-zero means messages should be displayed with truncated
639 lines instead of being continued. */
640
641 int message_truncate_lines;
642 Lisp_Object Qmessage_truncate_lines;
643
644 /* Set to 1 in clear_message to make redisplay_internal aware
645 of an emptied echo area. */
646
647 static int message_cleared_p;
648
649 /* How to blink the default frame cursor off. */
650 Lisp_Object Vblink_cursor_alist;
651
652 /* A scratch glyph row with contents used for generating truncation
653 glyphs. Also used in direct_output_for_insert. */
654
655 #define MAX_SCRATCH_GLYPHS 100
656 struct glyph_row scratch_glyph_row;
657 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
658
659 /* Ascent and height of the last line processed by move_it_to. */
660
661 static int last_max_ascent, last_height;
662
663 /* Non-zero if there's a help-echo in the echo area. */
664
665 int help_echo_showing_p;
666
667 /* If >= 0, computed, exact values of mode-line and header-line height
668 to use in the macros CURRENT_MODE_LINE_HEIGHT and
669 CURRENT_HEADER_LINE_HEIGHT. */
670
671 int current_mode_line_height, current_header_line_height;
672
673 /* The maximum distance to look ahead for text properties. Values
674 that are too small let us call compute_char_face and similar
675 functions too often which is expensive. Values that are too large
676 let us call compute_char_face and alike too often because we
677 might not be interested in text properties that far away. */
678
679 #define TEXT_PROP_DISTANCE_LIMIT 100
680
681 #if GLYPH_DEBUG
682
683 /* Variables to turn off display optimizations from Lisp. */
684
685 int inhibit_try_window_id, inhibit_try_window_reusing;
686 int inhibit_try_cursor_movement;
687
688 /* Non-zero means print traces of redisplay if compiled with
689 GLYPH_DEBUG != 0. */
690
691 int trace_redisplay_p;
692
693 #endif /* GLYPH_DEBUG */
694
695 #ifdef DEBUG_TRACE_MOVE
696 /* Non-zero means trace with TRACE_MOVE to stderr. */
697 int trace_move;
698
699 #define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
700 #else
701 #define TRACE_MOVE(x) (void) 0
702 #endif
703
704 /* Non-zero means automatically scroll windows horizontally to make
705 point visible. */
706
707 int automatic_hscrolling_p;
708 Lisp_Object Qauto_hscroll_mode;
709
710 /* How close to the margin can point get before the window is scrolled
711 horizontally. */
712 EMACS_INT hscroll_margin;
713
714 /* How much to scroll horizontally when point is inside the above margin. */
715 Lisp_Object Vhscroll_step;
716
717 /* The variable `resize-mini-windows'. If nil, don't resize
718 mini-windows. If t, always resize them to fit the text they
719 display. If `grow-only', let mini-windows grow only until they
720 become empty. */
721
722 Lisp_Object Vresize_mini_windows;
723
724 /* Buffer being redisplayed -- for redisplay_window_error. */
725
726 struct buffer *displayed_buffer;
727
728 /* Space between overline and text. */
729
730 EMACS_INT overline_margin;
731
732 /* Require underline to be at least this many screen pixels below baseline
733 This to avoid underline "merging" with the base of letters at small
734 font sizes, particularly when x_use_underline_position_properties is on. */
735
736 EMACS_INT underline_minimum_offset;
737
738 /* Value returned from text property handlers (see below). */
739
740 enum prop_handled
741 {
742 HANDLED_NORMALLY,
743 HANDLED_RECOMPUTE_PROPS,
744 HANDLED_OVERLAY_STRING_CONSUMED,
745 HANDLED_RETURN
746 };
747
748 /* A description of text properties that redisplay is interested
749 in. */
750
751 struct props
752 {
753 /* The name of the property. */
754 Lisp_Object *name;
755
756 /* A unique index for the property. */
757 enum prop_idx idx;
758
759 /* A handler function called to set up iterator IT from the property
760 at IT's current position. Value is used to steer handle_stop. */
761 enum prop_handled (*handler) P_ ((struct it *it));
762 };
763
764 static enum prop_handled handle_face_prop P_ ((struct it *));
765 static enum prop_handled handle_invisible_prop P_ ((struct it *));
766 static enum prop_handled handle_display_prop P_ ((struct it *));
767 static enum prop_handled handle_composition_prop P_ ((struct it *));
768 static enum prop_handled handle_overlay_change P_ ((struct it *));
769 static enum prop_handled handle_fontified_prop P_ ((struct it *));
770 static enum prop_handled handle_auto_composed_prop P_ ((struct it *));
771
772 /* Properties handled by iterators. */
773
774 static struct props it_props[] =
775 {
776 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
777 /* Handle `face' before `display' because some sub-properties of
778 `display' need to know the face. */
779 {&Qface, FACE_PROP_IDX, handle_face_prop},
780 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
781 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
782 {&Qauto_composed, AUTO_COMPOSED_PROP_IDX, handle_auto_composed_prop},
783 {&Qcomposition, COMPOSITION_PROP_IDX, handle_composition_prop},
784 {NULL, 0, NULL}
785 };
786
787 /* Value is the position described by X. If X is a marker, value is
788 the marker_position of X. Otherwise, value is X. */
789
790 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
791
792 /* Enumeration returned by some move_it_.* functions internally. */
793
794 enum move_it_result
795 {
796 /* Not used. Undefined value. */
797 MOVE_UNDEFINED,
798
799 /* Move ended at the requested buffer position or ZV. */
800 MOVE_POS_MATCH_OR_ZV,
801
802 /* Move ended at the requested X pixel position. */
803 MOVE_X_REACHED,
804
805 /* Move within a line ended at the end of a line that must be
806 continued. */
807 MOVE_LINE_CONTINUED,
808
809 /* Move within a line ended at the end of a line that would
810 be displayed truncated. */
811 MOVE_LINE_TRUNCATED,
812
813 /* Move within a line ended at a line end. */
814 MOVE_NEWLINE_OR_CR
815 };
816
817 /* This counter is used to clear the face cache every once in a while
818 in redisplay_internal. It is incremented for each redisplay.
819 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
820 cleared. */
821
822 #define CLEAR_FACE_CACHE_COUNT 500
823 static int clear_face_cache_count;
824
825 /* Similarly for the image cache. */
826
827 #ifdef HAVE_WINDOW_SYSTEM
828 #define CLEAR_IMAGE_CACHE_COUNT 101
829 static int clear_image_cache_count;
830 #endif
831
832 /* Non-zero while redisplay_internal is in progress. */
833
834 int redisplaying_p;
835
836 /* Non-zero means don't free realized faces. Bound while freeing
837 realized faces is dangerous because glyph matrices might still
838 reference them. */
839
840 int inhibit_free_realized_faces;
841 Lisp_Object Qinhibit_free_realized_faces;
842
843 /* If a string, XTread_socket generates an event to display that string.
844 (The display is done in read_char.) */
845
846 Lisp_Object help_echo_string;
847 Lisp_Object help_echo_window;
848 Lisp_Object help_echo_object;
849 int help_echo_pos;
850
851 /* Temporary variable for XTread_socket. */
852
853 Lisp_Object previous_help_echo_string;
854
855 /* Null glyph slice */
856
857 static struct glyph_slice null_glyph_slice = { 0, 0, 0, 0 };
858
859 /* Platform-independent portion of hourglass implementation. */
860
861 /* Non-zero means we're allowed to display a hourglass pointer. */
862 int display_hourglass_p;
863
864 /* Non-zero means an hourglass cursor is currently shown. */
865 int hourglass_shown_p;
866
867 /* If non-null, an asynchronous timer that, when it expires, displays
868 an hourglass cursor on all frames. */
869 struct atimer *hourglass_atimer;
870
871 /* Number of seconds to wait before displaying an hourglass cursor. */
872 Lisp_Object Vhourglass_delay;
873
874 /* Default number of seconds to wait before displaying an hourglass
875 cursor. */
876 #define DEFAULT_HOURGLASS_DELAY 1
877
878 \f
879 /* Function prototypes. */
880
881 static void setup_for_ellipsis P_ ((struct it *, int));
882 static void mark_window_display_accurate_1 P_ ((struct window *, int));
883 static int single_display_spec_string_p P_ ((Lisp_Object, Lisp_Object));
884 static int display_prop_string_p P_ ((Lisp_Object, Lisp_Object));
885 static int cursor_row_p P_ ((struct window *, struct glyph_row *));
886 static int redisplay_mode_lines P_ ((Lisp_Object, int));
887 static char *decode_mode_spec_coding P_ ((Lisp_Object, char *, int));
888
889 static Lisp_Object get_it_property P_ ((struct it *it, Lisp_Object prop));
890
891 static void handle_line_prefix P_ ((struct it *));
892
893 static void pint2str P_ ((char *, int, int));
894 static void pint2hrstr P_ ((char *, int, int));
895 static struct text_pos run_window_scroll_functions P_ ((Lisp_Object,
896 struct text_pos));
897 static void reconsider_clip_changes P_ ((struct window *, struct buffer *));
898 static int text_outside_line_unchanged_p P_ ((struct window *, int, int));
899 static void store_mode_line_noprop_char P_ ((char));
900 static int store_mode_line_noprop P_ ((const unsigned char *, int, int));
901 static void x_consider_frame_title P_ ((Lisp_Object));
902 static void handle_stop P_ ((struct it *));
903 static int tool_bar_lines_needed P_ ((struct frame *, int *));
904 static int single_display_spec_intangible_p P_ ((Lisp_Object));
905 static void ensure_echo_area_buffers P_ ((void));
906 static Lisp_Object unwind_with_echo_area_buffer P_ ((Lisp_Object));
907 static Lisp_Object with_echo_area_buffer_unwind_data P_ ((struct window *));
908 static int with_echo_area_buffer P_ ((struct window *, int,
909 int (*) (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT),
910 EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
911 static void clear_garbaged_frames P_ ((void));
912 static int current_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
913 static int truncate_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
914 static int set_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
915 static int display_echo_area P_ ((struct window *));
916 static int display_echo_area_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
917 static int resize_mini_window_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
918 static Lisp_Object unwind_redisplay P_ ((Lisp_Object));
919 static int string_char_and_length P_ ((const unsigned char *, int, int *));
920 static struct text_pos display_prop_end P_ ((struct it *, Lisp_Object,
921 struct text_pos));
922 static int compute_window_start_on_continuation_line P_ ((struct window *));
923 static Lisp_Object safe_eval_handler P_ ((Lisp_Object));
924 static void insert_left_trunc_glyphs P_ ((struct it *));
925 static struct glyph_row *get_overlay_arrow_glyph_row P_ ((struct window *,
926 Lisp_Object));
927 static void extend_face_to_end_of_line P_ ((struct it *));
928 static int append_space_for_newline P_ ((struct it *, int));
929 static int cursor_row_fully_visible_p P_ ((struct window *, int, int));
930 static int try_scrolling P_ ((Lisp_Object, int, EMACS_INT, EMACS_INT, int, int));
931 static int try_cursor_movement P_ ((Lisp_Object, struct text_pos, int *));
932 static int trailing_whitespace_p P_ ((int));
933 static int message_log_check_duplicate P_ ((int, int, int, int));
934 static void push_it P_ ((struct it *));
935 static void pop_it P_ ((struct it *));
936 static void sync_frame_with_window_matrix_rows P_ ((struct window *));
937 static void select_frame_for_redisplay P_ ((Lisp_Object));
938 static void redisplay_internal P_ ((int));
939 static int echo_area_display P_ ((int));
940 static void redisplay_windows P_ ((Lisp_Object));
941 static void redisplay_window P_ ((Lisp_Object, int));
942 static Lisp_Object redisplay_window_error ();
943 static Lisp_Object redisplay_window_0 P_ ((Lisp_Object));
944 static Lisp_Object redisplay_window_1 P_ ((Lisp_Object));
945 static int update_menu_bar P_ ((struct frame *, int, int));
946 static int try_window_reusing_current_matrix P_ ((struct window *));
947 static int try_window_id P_ ((struct window *));
948 static int display_line P_ ((struct it *));
949 static int display_mode_lines P_ ((struct window *));
950 static int display_mode_line P_ ((struct window *, enum face_id, Lisp_Object));
951 static int display_mode_element P_ ((struct it *, int, int, int, Lisp_Object, Lisp_Object, int));
952 static int store_mode_line_string P_ ((char *, Lisp_Object, int, int, int, Lisp_Object));
953 static char *decode_mode_spec P_ ((struct window *, int, int, int, int *));
954 static void display_menu_bar P_ ((struct window *));
955 static int display_count_lines P_ ((int, int, int, int, int *));
956 static int display_string P_ ((unsigned char *, Lisp_Object, Lisp_Object,
957 EMACS_INT, EMACS_INT, struct it *, int, int, int, int));
958 static void compute_line_metrics P_ ((struct it *));
959 static void run_redisplay_end_trigger_hook P_ ((struct it *));
960 static int get_overlay_strings P_ ((struct it *, int));
961 static int get_overlay_strings_1 P_ ((struct it *, int, int));
962 static void next_overlay_string P_ ((struct it *));
963 static void reseat P_ ((struct it *, struct text_pos, int));
964 static void reseat_1 P_ ((struct it *, struct text_pos, int));
965 static void back_to_previous_visible_line_start P_ ((struct it *));
966 void reseat_at_previous_visible_line_start P_ ((struct it *));
967 static void reseat_at_next_visible_line_start P_ ((struct it *, int));
968 static int next_element_from_ellipsis P_ ((struct it *));
969 static int next_element_from_display_vector P_ ((struct it *));
970 static int next_element_from_string P_ ((struct it *));
971 static int next_element_from_c_string P_ ((struct it *));
972 static int next_element_from_buffer P_ ((struct it *));
973 static int next_element_from_composition P_ ((struct it *));
974 static int next_element_from_image P_ ((struct it *));
975 static int next_element_from_stretch P_ ((struct it *));
976 static void load_overlay_strings P_ ((struct it *, int));
977 static int init_from_display_pos P_ ((struct it *, struct window *,
978 struct display_pos *));
979 static void reseat_to_string P_ ((struct it *, unsigned char *,
980 Lisp_Object, int, int, int, int));
981 static enum move_it_result
982 move_it_in_display_line_to (struct it *, EMACS_INT, int,
983 enum move_operation_enum);
984 void move_it_vertically_backward P_ ((struct it *, int));
985 static void init_to_row_start P_ ((struct it *, struct window *,
986 struct glyph_row *));
987 static int init_to_row_end P_ ((struct it *, struct window *,
988 struct glyph_row *));
989 static void back_to_previous_line_start P_ ((struct it *));
990 static int forward_to_next_line_start P_ ((struct it *, int *));
991 static struct text_pos string_pos_nchars_ahead P_ ((struct text_pos,
992 Lisp_Object, int));
993 static struct text_pos string_pos P_ ((int, Lisp_Object));
994 static struct text_pos c_string_pos P_ ((int, unsigned char *, int));
995 static int number_of_chars P_ ((unsigned char *, int));
996 static void compute_stop_pos P_ ((struct it *));
997 static void compute_string_pos P_ ((struct text_pos *, struct text_pos,
998 Lisp_Object));
999 static int face_before_or_after_it_pos P_ ((struct it *, int));
1000 static EMACS_INT next_overlay_change P_ ((EMACS_INT));
1001 static int handle_single_display_spec P_ ((struct it *, Lisp_Object,
1002 Lisp_Object, Lisp_Object,
1003 struct text_pos *, int));
1004 static int underlying_face_id P_ ((struct it *));
1005 static int in_ellipses_for_invisible_text_p P_ ((struct display_pos *,
1006 struct window *));
1007
1008 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
1009 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
1010
1011 #ifdef HAVE_WINDOW_SYSTEM
1012
1013 static void update_tool_bar P_ ((struct frame *, int));
1014 static void build_desired_tool_bar_string P_ ((struct frame *f));
1015 static int redisplay_tool_bar P_ ((struct frame *));
1016 static void display_tool_bar_line P_ ((struct it *, int));
1017 static void notice_overwritten_cursor P_ ((struct window *,
1018 enum glyph_row_area,
1019 int, int, int, int));
1020
1021
1022
1023 #endif /* HAVE_WINDOW_SYSTEM */
1024
1025 \f
1026 /***********************************************************************
1027 Window display dimensions
1028 ***********************************************************************/
1029
1030 /* Return the bottom boundary y-position for text lines in window W.
1031 This is the first y position at which a line cannot start.
1032 It is relative to the top of the window.
1033
1034 This is the height of W minus the height of a mode line, if any. */
1035
1036 INLINE int
1037 window_text_bottom_y (w)
1038 struct window *w;
1039 {
1040 int height = WINDOW_TOTAL_HEIGHT (w);
1041
1042 if (WINDOW_WANTS_MODELINE_P (w))
1043 height -= CURRENT_MODE_LINE_HEIGHT (w);
1044 return height;
1045 }
1046
1047 /* Return the pixel width of display area AREA of window W. AREA < 0
1048 means return the total width of W, not including fringes to
1049 the left and right of the window. */
1050
1051 INLINE int
1052 window_box_width (w, area)
1053 struct window *w;
1054 int area;
1055 {
1056 int cols = XFASTINT (w->total_cols);
1057 int pixels = 0;
1058
1059 if (!w->pseudo_window_p)
1060 {
1061 cols -= WINDOW_SCROLL_BAR_COLS (w);
1062
1063 if (area == TEXT_AREA)
1064 {
1065 if (INTEGERP (w->left_margin_cols))
1066 cols -= XFASTINT (w->left_margin_cols);
1067 if (INTEGERP (w->right_margin_cols))
1068 cols -= XFASTINT (w->right_margin_cols);
1069 pixels = -WINDOW_TOTAL_FRINGE_WIDTH (w);
1070 }
1071 else if (area == LEFT_MARGIN_AREA)
1072 {
1073 cols = (INTEGERP (w->left_margin_cols)
1074 ? XFASTINT (w->left_margin_cols) : 0);
1075 pixels = 0;
1076 }
1077 else if (area == RIGHT_MARGIN_AREA)
1078 {
1079 cols = (INTEGERP (w->right_margin_cols)
1080 ? XFASTINT (w->right_margin_cols) : 0);
1081 pixels = 0;
1082 }
1083 }
1084
1085 return cols * WINDOW_FRAME_COLUMN_WIDTH (w) + pixels;
1086 }
1087
1088
1089 /* Return the pixel height of the display area of window W, not
1090 including mode lines of W, if any. */
1091
1092 INLINE int
1093 window_box_height (w)
1094 struct window *w;
1095 {
1096 struct frame *f = XFRAME (w->frame);
1097 int height = WINDOW_TOTAL_HEIGHT (w);
1098
1099 xassert (height >= 0);
1100
1101 /* Note: the code below that determines the mode-line/header-line
1102 height is essentially the same as that contained in the macro
1103 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
1104 the appropriate glyph row has its `mode_line_p' flag set,
1105 and if it doesn't, uses estimate_mode_line_height instead. */
1106
1107 if (WINDOW_WANTS_MODELINE_P (w))
1108 {
1109 struct glyph_row *ml_row
1110 = (w->current_matrix && w->current_matrix->rows
1111 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
1112 : 0);
1113 if (ml_row && ml_row->mode_line_p)
1114 height -= ml_row->height;
1115 else
1116 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
1117 }
1118
1119 if (WINDOW_WANTS_HEADER_LINE_P (w))
1120 {
1121 struct glyph_row *hl_row
1122 = (w->current_matrix && w->current_matrix->rows
1123 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
1124 : 0);
1125 if (hl_row && hl_row->mode_line_p)
1126 height -= hl_row->height;
1127 else
1128 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
1129 }
1130
1131 /* With a very small font and a mode-line that's taller than
1132 default, we might end up with a negative height. */
1133 return max (0, height);
1134 }
1135
1136 /* Return the window-relative coordinate of the left edge of display
1137 area AREA of window W. AREA < 0 means return the left edge of the
1138 whole window, to the right of the left fringe of W. */
1139
1140 INLINE int
1141 window_box_left_offset (w, area)
1142 struct window *w;
1143 int area;
1144 {
1145 int x;
1146
1147 if (w->pseudo_window_p)
1148 return 0;
1149
1150 x = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
1151
1152 if (area == TEXT_AREA)
1153 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1154 + window_box_width (w, LEFT_MARGIN_AREA));
1155 else if (area == RIGHT_MARGIN_AREA)
1156 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1157 + window_box_width (w, LEFT_MARGIN_AREA)
1158 + window_box_width (w, TEXT_AREA)
1159 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1160 ? 0
1161 : WINDOW_RIGHT_FRINGE_WIDTH (w)));
1162 else if (area == LEFT_MARGIN_AREA
1163 && WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1164 x += WINDOW_LEFT_FRINGE_WIDTH (w);
1165
1166 return x;
1167 }
1168
1169
1170 /* Return the window-relative coordinate of the right edge of display
1171 area AREA of window W. AREA < 0 means return the left edge of the
1172 whole window, to the left of the right fringe of W. */
1173
1174 INLINE int
1175 window_box_right_offset (w, area)
1176 struct window *w;
1177 int area;
1178 {
1179 return window_box_left_offset (w, area) + window_box_width (w, area);
1180 }
1181
1182 /* Return the frame-relative coordinate of the left edge of display
1183 area AREA of window W. AREA < 0 means return the left edge of the
1184 whole window, to the right of the left fringe of W. */
1185
1186 INLINE int
1187 window_box_left (w, area)
1188 struct window *w;
1189 int area;
1190 {
1191 struct frame *f = XFRAME (w->frame);
1192 int x;
1193
1194 if (w->pseudo_window_p)
1195 return FRAME_INTERNAL_BORDER_WIDTH (f);
1196
1197 x = (WINDOW_LEFT_EDGE_X (w)
1198 + window_box_left_offset (w, area));
1199
1200 return x;
1201 }
1202
1203
1204 /* Return the frame-relative coordinate of the right edge of display
1205 area AREA of window W. AREA < 0 means return the left edge of the
1206 whole window, to the left of the right fringe of W. */
1207
1208 INLINE int
1209 window_box_right (w, area)
1210 struct window *w;
1211 int area;
1212 {
1213 return window_box_left (w, area) + window_box_width (w, area);
1214 }
1215
1216 /* Get the bounding box of the display area AREA of window W, without
1217 mode lines, in frame-relative coordinates. AREA < 0 means the
1218 whole window, not including the left and right fringes of
1219 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1220 coordinates of the upper-left corner of the box. Return in
1221 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1222
1223 INLINE void
1224 window_box (w, area, box_x, box_y, box_width, box_height)
1225 struct window *w;
1226 int area;
1227 int *box_x, *box_y, *box_width, *box_height;
1228 {
1229 if (box_width)
1230 *box_width = window_box_width (w, area);
1231 if (box_height)
1232 *box_height = window_box_height (w);
1233 if (box_x)
1234 *box_x = window_box_left (w, area);
1235 if (box_y)
1236 {
1237 *box_y = WINDOW_TOP_EDGE_Y (w);
1238 if (WINDOW_WANTS_HEADER_LINE_P (w))
1239 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1240 }
1241 }
1242
1243
1244 /* Get the bounding box of the display area AREA of window W, without
1245 mode lines. AREA < 0 means the whole window, not including the
1246 left and right fringe of the window. Return in *TOP_LEFT_X
1247 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1248 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1249 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1250 box. */
1251
1252 INLINE void
1253 window_box_edges (w, area, top_left_x, top_left_y,
1254 bottom_right_x, bottom_right_y)
1255 struct window *w;
1256 int area;
1257 int *top_left_x, *top_left_y, *bottom_right_x, *bottom_right_y;
1258 {
1259 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
1260 bottom_right_y);
1261 *bottom_right_x += *top_left_x;
1262 *bottom_right_y += *top_left_y;
1263 }
1264
1265
1266 \f
1267 /***********************************************************************
1268 Utilities
1269 ***********************************************************************/
1270
1271 /* Return the bottom y-position of the line the iterator IT is in.
1272 This can modify IT's settings. */
1273
1274 int
1275 line_bottom_y (it)
1276 struct it *it;
1277 {
1278 int line_height = it->max_ascent + it->max_descent;
1279 int line_top_y = it->current_y;
1280
1281 if (line_height == 0)
1282 {
1283 if (last_height)
1284 line_height = last_height;
1285 else if (IT_CHARPOS (*it) < ZV)
1286 {
1287 move_it_by_lines (it, 1, 1);
1288 line_height = (it->max_ascent || it->max_descent
1289 ? it->max_ascent + it->max_descent
1290 : last_height);
1291 }
1292 else
1293 {
1294 struct glyph_row *row = it->glyph_row;
1295
1296 /* Use the default character height. */
1297 it->glyph_row = NULL;
1298 it->what = IT_CHARACTER;
1299 it->c = ' ';
1300 it->len = 1;
1301 PRODUCE_GLYPHS (it);
1302 line_height = it->ascent + it->descent;
1303 it->glyph_row = row;
1304 }
1305 }
1306
1307 return line_top_y + line_height;
1308 }
1309
1310
1311 /* Return 1 if position CHARPOS is visible in window W.
1312 CHARPOS < 0 means return info about WINDOW_END position.
1313 If visible, set *X and *Y to pixel coordinates of top left corner.
1314 Set *RTOP and *RBOT to pixel height of an invisible area of glyph at POS.
1315 Set *ROWH and *VPOS to row's visible height and VPOS (row number). */
1316
1317 int
1318 pos_visible_p (w, charpos, x, y, rtop, rbot, rowh, vpos)
1319 struct window *w;
1320 int charpos, *x, *y, *rtop, *rbot, *rowh, *vpos;
1321 {
1322 struct it it;
1323 struct text_pos top;
1324 int visible_p = 0;
1325 struct buffer *old_buffer = NULL;
1326
1327 if (noninteractive)
1328 return visible_p;
1329
1330 if (XBUFFER (w->buffer) != current_buffer)
1331 {
1332 old_buffer = current_buffer;
1333 set_buffer_internal_1 (XBUFFER (w->buffer));
1334 }
1335
1336 SET_TEXT_POS_FROM_MARKER (top, w->start);
1337
1338 /* Compute exact mode line heights. */
1339 if (WINDOW_WANTS_MODELINE_P (w))
1340 current_mode_line_height
1341 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1342 current_buffer->mode_line_format);
1343
1344 if (WINDOW_WANTS_HEADER_LINE_P (w))
1345 current_header_line_height
1346 = display_mode_line (w, HEADER_LINE_FACE_ID,
1347 current_buffer->header_line_format);
1348
1349 start_display (&it, w, top);
1350 move_it_to (&it, charpos, -1, it.last_visible_y-1, -1,
1351 (charpos >= 0 ? MOVE_TO_POS : 0) | MOVE_TO_Y);
1352
1353 /* Note that we may overshoot because of invisible text. */
1354 if (charpos >= 0 && IT_CHARPOS (it) >= charpos)
1355 {
1356 int top_x = it.current_x;
1357 int top_y = it.current_y;
1358 int bottom_y = (last_height = 0, line_bottom_y (&it));
1359 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
1360
1361 if (top_y < window_top_y)
1362 visible_p = bottom_y > window_top_y;
1363 else if (top_y < it.last_visible_y)
1364 visible_p = 1;
1365 if (visible_p)
1366 {
1367 if (it.method == GET_FROM_BUFFER)
1368 {
1369 Lisp_Object window, prop;
1370
1371 XSETWINDOW (window, w);
1372 prop = Fget_char_property (make_number (it.position.charpos),
1373 Qinvisible, window);
1374
1375 /* If charpos coincides with invisible text covered with an
1376 ellipsis, use the first glyph of the ellipsis to compute
1377 the pixel positions. */
1378 if (TEXT_PROP_MEANS_INVISIBLE (prop) == 2)
1379 {
1380 struct glyph_row *row = it.glyph_row;
1381 struct glyph *glyph = row->glyphs[TEXT_AREA];
1382 struct glyph *end = glyph + row->used[TEXT_AREA];
1383 int x = row->x;
1384
1385 for (; glyph < end && glyph->charpos < charpos; glyph++)
1386 x += glyph->pixel_width;
1387
1388 top_x = x;
1389 }
1390 }
1391
1392 *x = top_x;
1393 *y = max (top_y + max (0, it.max_ascent - it.ascent), window_top_y);
1394 *rtop = max (0, window_top_y - top_y);
1395 *rbot = max (0, bottom_y - it.last_visible_y);
1396 *rowh = max (0, (min (bottom_y, it.last_visible_y)
1397 - max (top_y, window_top_y)));
1398 *vpos = it.vpos;
1399 }
1400 }
1401 else
1402 {
1403 struct it it2;
1404
1405 it2 = it;
1406 if (IT_CHARPOS (it) < ZV && FETCH_BYTE (IT_BYTEPOS (it)) != '\n')
1407 move_it_by_lines (&it, 1, 0);
1408 if (charpos < IT_CHARPOS (it)
1409 || (it.what == IT_EOB && charpos == IT_CHARPOS (it)))
1410 {
1411 visible_p = 1;
1412 move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS);
1413 *x = it2.current_x;
1414 *y = it2.current_y + it2.max_ascent - it2.ascent;
1415 *rtop = max (0, -it2.current_y);
1416 *rbot = max (0, ((it2.current_y + it2.max_ascent + it2.max_descent)
1417 - it.last_visible_y));
1418 *rowh = max (0, (min (it2.current_y + it2.max_ascent + it2.max_descent,
1419 it.last_visible_y)
1420 - max (it2.current_y,
1421 WINDOW_HEADER_LINE_HEIGHT (w))));
1422 *vpos = it2.vpos;
1423 }
1424 }
1425
1426 if (old_buffer)
1427 set_buffer_internal_1 (old_buffer);
1428
1429 current_header_line_height = current_mode_line_height = -1;
1430
1431 if (visible_p && XFASTINT (w->hscroll) > 0)
1432 *x -= XFASTINT (w->hscroll) * WINDOW_FRAME_COLUMN_WIDTH (w);
1433
1434 #if 0
1435 /* Debugging code. */
1436 if (visible_p)
1437 fprintf (stderr, "+pv pt=%d vs=%d --> x=%d y=%d rt=%d rb=%d rh=%d vp=%d\n",
1438 charpos, w->vscroll, *x, *y, *rtop, *rbot, *rowh, *vpos);
1439 else
1440 fprintf (stderr, "-pv pt=%d vs=%d\n", charpos, w->vscroll);
1441 #endif
1442
1443 return visible_p;
1444 }
1445
1446
1447 /* Return the next character from STR which is MAXLEN bytes long.
1448 Return in *LEN the length of the character. This is like
1449 STRING_CHAR_AND_LENGTH but never returns an invalid character. If
1450 we find one, we return a `?', but with the length of the invalid
1451 character. */
1452
1453 static INLINE int
1454 string_char_and_length (str, maxlen, len)
1455 const unsigned char *str;
1456 int maxlen, *len;
1457 {
1458 int c;
1459
1460 c = STRING_CHAR_AND_LENGTH (str, maxlen, *len);
1461 if (!CHAR_VALID_P (c, 1))
1462 /* We may not change the length here because other places in Emacs
1463 don't use this function, i.e. they silently accept invalid
1464 characters. */
1465 c = '?';
1466
1467 return c;
1468 }
1469
1470
1471
1472 /* Given a position POS containing a valid character and byte position
1473 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1474
1475 static struct text_pos
1476 string_pos_nchars_ahead (pos, string, nchars)
1477 struct text_pos pos;
1478 Lisp_Object string;
1479 int nchars;
1480 {
1481 xassert (STRINGP (string) && nchars >= 0);
1482
1483 if (STRING_MULTIBYTE (string))
1484 {
1485 int rest = SBYTES (string) - BYTEPOS (pos);
1486 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1487 int len;
1488
1489 while (nchars--)
1490 {
1491 string_char_and_length (p, rest, &len);
1492 p += len, rest -= len;
1493 xassert (rest >= 0);
1494 CHARPOS (pos) += 1;
1495 BYTEPOS (pos) += len;
1496 }
1497 }
1498 else
1499 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1500
1501 return pos;
1502 }
1503
1504
1505 /* Value is the text position, i.e. character and byte position,
1506 for character position CHARPOS in STRING. */
1507
1508 static INLINE struct text_pos
1509 string_pos (charpos, string)
1510 int charpos;
1511 Lisp_Object string;
1512 {
1513 struct text_pos pos;
1514 xassert (STRINGP (string));
1515 xassert (charpos >= 0);
1516 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1517 return pos;
1518 }
1519
1520
1521 /* Value is a text position, i.e. character and byte position, for
1522 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1523 means recognize multibyte characters. */
1524
1525 static struct text_pos
1526 c_string_pos (charpos, s, multibyte_p)
1527 int charpos;
1528 unsigned char *s;
1529 int multibyte_p;
1530 {
1531 struct text_pos pos;
1532
1533 xassert (s != NULL);
1534 xassert (charpos >= 0);
1535
1536 if (multibyte_p)
1537 {
1538 int rest = strlen (s), len;
1539
1540 SET_TEXT_POS (pos, 0, 0);
1541 while (charpos--)
1542 {
1543 string_char_and_length (s, rest, &len);
1544 s += len, rest -= len;
1545 xassert (rest >= 0);
1546 CHARPOS (pos) += 1;
1547 BYTEPOS (pos) += len;
1548 }
1549 }
1550 else
1551 SET_TEXT_POS (pos, charpos, charpos);
1552
1553 return pos;
1554 }
1555
1556
1557 /* Value is the number of characters in C string S. MULTIBYTE_P
1558 non-zero means recognize multibyte characters. */
1559
1560 static int
1561 number_of_chars (s, multibyte_p)
1562 unsigned char *s;
1563 int multibyte_p;
1564 {
1565 int nchars;
1566
1567 if (multibyte_p)
1568 {
1569 int rest = strlen (s), len;
1570 unsigned char *p = (unsigned char *) s;
1571
1572 for (nchars = 0; rest > 0; ++nchars)
1573 {
1574 string_char_and_length (p, rest, &len);
1575 rest -= len, p += len;
1576 }
1577 }
1578 else
1579 nchars = strlen (s);
1580
1581 return nchars;
1582 }
1583
1584
1585 /* Compute byte position NEWPOS->bytepos corresponding to
1586 NEWPOS->charpos. POS is a known position in string STRING.
1587 NEWPOS->charpos must be >= POS.charpos. */
1588
1589 static void
1590 compute_string_pos (newpos, pos, string)
1591 struct text_pos *newpos, pos;
1592 Lisp_Object string;
1593 {
1594 xassert (STRINGP (string));
1595 xassert (CHARPOS (*newpos) >= CHARPOS (pos));
1596
1597 if (STRING_MULTIBYTE (string))
1598 *newpos = string_pos_nchars_ahead (pos, string,
1599 CHARPOS (*newpos) - CHARPOS (pos));
1600 else
1601 BYTEPOS (*newpos) = CHARPOS (*newpos);
1602 }
1603
1604 /* EXPORT:
1605 Return an estimation of the pixel height of mode or top lines on
1606 frame F. FACE_ID specifies what line's height to estimate. */
1607
1608 int
1609 estimate_mode_line_height (f, face_id)
1610 struct frame *f;
1611 enum face_id face_id;
1612 {
1613 #ifdef HAVE_WINDOW_SYSTEM
1614 if (FRAME_WINDOW_P (f))
1615 {
1616 int height = FONT_HEIGHT (FRAME_FONT (f));
1617
1618 /* This function is called so early when Emacs starts that the face
1619 cache and mode line face are not yet initialized. */
1620 if (FRAME_FACE_CACHE (f))
1621 {
1622 struct face *face = FACE_FROM_ID (f, face_id);
1623 if (face)
1624 {
1625 if (face->font)
1626 height = FONT_HEIGHT (face->font);
1627 if (face->box_line_width > 0)
1628 height += 2 * face->box_line_width;
1629 }
1630 }
1631
1632 return height;
1633 }
1634 #endif
1635
1636 return 1;
1637 }
1638
1639 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1640 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1641 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
1642 not force the value into range. */
1643
1644 void
1645 pixel_to_glyph_coords (f, pix_x, pix_y, x, y, bounds, noclip)
1646 FRAME_PTR f;
1647 register int pix_x, pix_y;
1648 int *x, *y;
1649 NativeRectangle *bounds;
1650 int noclip;
1651 {
1652
1653 #ifdef HAVE_WINDOW_SYSTEM
1654 if (FRAME_WINDOW_P (f))
1655 {
1656 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1657 even for negative values. */
1658 if (pix_x < 0)
1659 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1660 if (pix_y < 0)
1661 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1662
1663 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1664 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1665
1666 if (bounds)
1667 STORE_NATIVE_RECT (*bounds,
1668 FRAME_COL_TO_PIXEL_X (f, pix_x),
1669 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1670 FRAME_COLUMN_WIDTH (f) - 1,
1671 FRAME_LINE_HEIGHT (f) - 1);
1672
1673 if (!noclip)
1674 {
1675 if (pix_x < 0)
1676 pix_x = 0;
1677 else if (pix_x > FRAME_TOTAL_COLS (f))
1678 pix_x = FRAME_TOTAL_COLS (f);
1679
1680 if (pix_y < 0)
1681 pix_y = 0;
1682 else if (pix_y > FRAME_LINES (f))
1683 pix_y = FRAME_LINES (f);
1684 }
1685 }
1686 #endif
1687
1688 *x = pix_x;
1689 *y = pix_y;
1690 }
1691
1692
1693 /* Given HPOS/VPOS in the current matrix of W, return corresponding
1694 frame-relative pixel positions in *FRAME_X and *FRAME_Y. If we
1695 can't tell the positions because W's display is not up to date,
1696 return 0. */
1697
1698 int
1699 glyph_to_pixel_coords (w, hpos, vpos, frame_x, frame_y)
1700 struct window *w;
1701 int hpos, vpos;
1702 int *frame_x, *frame_y;
1703 {
1704 #ifdef HAVE_WINDOW_SYSTEM
1705 if (FRAME_WINDOW_P (XFRAME (WINDOW_FRAME (w))))
1706 {
1707 int success_p;
1708
1709 xassert (hpos >= 0 && hpos < w->current_matrix->matrix_w);
1710 xassert (vpos >= 0 && vpos < w->current_matrix->matrix_h);
1711
1712 if (display_completed)
1713 {
1714 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
1715 struct glyph *glyph = row->glyphs[TEXT_AREA];
1716 struct glyph *end = glyph + min (hpos, row->used[TEXT_AREA]);
1717
1718 hpos = row->x;
1719 vpos = row->y;
1720 while (glyph < end)
1721 {
1722 hpos += glyph->pixel_width;
1723 ++glyph;
1724 }
1725
1726 /* If first glyph is partially visible, its first visible position is still 0. */
1727 if (hpos < 0)
1728 hpos = 0;
1729
1730 success_p = 1;
1731 }
1732 else
1733 {
1734 hpos = vpos = 0;
1735 success_p = 0;
1736 }
1737
1738 *frame_x = WINDOW_TO_FRAME_PIXEL_X (w, hpos);
1739 *frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, vpos);
1740 return success_p;
1741 }
1742 #endif
1743
1744 *frame_x = hpos;
1745 *frame_y = vpos;
1746 return 1;
1747 }
1748
1749
1750 #ifdef HAVE_WINDOW_SYSTEM
1751
1752 /* Find the glyph under window-relative coordinates X/Y in window W.
1753 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1754 strings. Return in *HPOS and *VPOS the row and column number of
1755 the glyph found. Return in *AREA the glyph area containing X.
1756 Value is a pointer to the glyph found or null if X/Y is not on
1757 text, or we can't tell because W's current matrix is not up to
1758 date. */
1759
1760 static
1761 struct glyph *
1762 x_y_to_hpos_vpos (w, x, y, hpos, vpos, dx, dy, area)
1763 struct window *w;
1764 int x, y;
1765 int *hpos, *vpos, *dx, *dy, *area;
1766 {
1767 struct glyph *glyph, *end;
1768 struct glyph_row *row = NULL;
1769 int x0, i;
1770
1771 /* Find row containing Y. Give up if some row is not enabled. */
1772 for (i = 0; i < w->current_matrix->nrows; ++i)
1773 {
1774 row = MATRIX_ROW (w->current_matrix, i);
1775 if (!row->enabled_p)
1776 return NULL;
1777 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1778 break;
1779 }
1780
1781 *vpos = i;
1782 *hpos = 0;
1783
1784 /* Give up if Y is not in the window. */
1785 if (i == w->current_matrix->nrows)
1786 return NULL;
1787
1788 /* Get the glyph area containing X. */
1789 if (w->pseudo_window_p)
1790 {
1791 *area = TEXT_AREA;
1792 x0 = 0;
1793 }
1794 else
1795 {
1796 if (x < window_box_left_offset (w, TEXT_AREA))
1797 {
1798 *area = LEFT_MARGIN_AREA;
1799 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1800 }
1801 else if (x < window_box_right_offset (w, TEXT_AREA))
1802 {
1803 *area = TEXT_AREA;
1804 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
1805 }
1806 else
1807 {
1808 *area = RIGHT_MARGIN_AREA;
1809 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1810 }
1811 }
1812
1813 /* Find glyph containing X. */
1814 glyph = row->glyphs[*area];
1815 end = glyph + row->used[*area];
1816 x -= x0;
1817 while (glyph < end && x >= glyph->pixel_width)
1818 {
1819 x -= glyph->pixel_width;
1820 ++glyph;
1821 }
1822
1823 if (glyph == end)
1824 return NULL;
1825
1826 if (dx)
1827 {
1828 *dx = x;
1829 *dy = y - (row->y + row->ascent - glyph->ascent);
1830 }
1831
1832 *hpos = glyph - row->glyphs[*area];
1833 return glyph;
1834 }
1835
1836
1837 /* EXPORT:
1838 Convert frame-relative x/y to coordinates relative to window W.
1839 Takes pseudo-windows into account. */
1840
1841 void
1842 frame_to_window_pixel_xy (w, x, y)
1843 struct window *w;
1844 int *x, *y;
1845 {
1846 if (w->pseudo_window_p)
1847 {
1848 /* A pseudo-window is always full-width, and starts at the
1849 left edge of the frame, plus a frame border. */
1850 struct frame *f = XFRAME (w->frame);
1851 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
1852 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1853 }
1854 else
1855 {
1856 *x -= WINDOW_LEFT_EDGE_X (w);
1857 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1858 }
1859 }
1860
1861 /* EXPORT:
1862 Return in RECTS[] at most N clipping rectangles for glyph string S.
1863 Return the number of stored rectangles. */
1864
1865 int
1866 get_glyph_string_clip_rects (s, rects, n)
1867 struct glyph_string *s;
1868 NativeRectangle *rects;
1869 int n;
1870 {
1871 XRectangle r;
1872
1873 if (n <= 0)
1874 return 0;
1875
1876 if (s->row->full_width_p)
1877 {
1878 /* Draw full-width. X coordinates are relative to S->w->left_col. */
1879 r.x = WINDOW_LEFT_EDGE_X (s->w);
1880 r.width = WINDOW_TOTAL_WIDTH (s->w);
1881
1882 /* Unless displaying a mode or menu bar line, which are always
1883 fully visible, clip to the visible part of the row. */
1884 if (s->w->pseudo_window_p)
1885 r.height = s->row->visible_height;
1886 else
1887 r.height = s->height;
1888 }
1889 else
1890 {
1891 /* This is a text line that may be partially visible. */
1892 r.x = window_box_left (s->w, s->area);
1893 r.width = window_box_width (s->w, s->area);
1894 r.height = s->row->visible_height;
1895 }
1896
1897 if (s->clip_head)
1898 if (r.x < s->clip_head->x)
1899 {
1900 if (r.width >= s->clip_head->x - r.x)
1901 r.width -= s->clip_head->x - r.x;
1902 else
1903 r.width = 0;
1904 r.x = s->clip_head->x;
1905 }
1906 if (s->clip_tail)
1907 if (r.x + r.width > s->clip_tail->x + s->clip_tail->background_width)
1908 {
1909 if (s->clip_tail->x + s->clip_tail->background_width >= r.x)
1910 r.width = s->clip_tail->x + s->clip_tail->background_width - r.x;
1911 else
1912 r.width = 0;
1913 }
1914
1915 /* If S draws overlapping rows, it's sufficient to use the top and
1916 bottom of the window for clipping because this glyph string
1917 intentionally draws over other lines. */
1918 if (s->for_overlaps)
1919 {
1920 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1921 r.height = window_text_bottom_y (s->w) - r.y;
1922
1923 /* Alas, the above simple strategy does not work for the
1924 environments with anti-aliased text: if the same text is
1925 drawn onto the same place multiple times, it gets thicker.
1926 If the overlap we are processing is for the erased cursor, we
1927 take the intersection with the rectagle of the cursor. */
1928 if (s->for_overlaps & OVERLAPS_ERASED_CURSOR)
1929 {
1930 XRectangle rc, r_save = r;
1931
1932 rc.x = WINDOW_TEXT_TO_FRAME_PIXEL_X (s->w, s->w->phys_cursor.x);
1933 rc.y = s->w->phys_cursor.y;
1934 rc.width = s->w->phys_cursor_width;
1935 rc.height = s->w->phys_cursor_height;
1936
1937 x_intersect_rectangles (&r_save, &rc, &r);
1938 }
1939 }
1940 else
1941 {
1942 /* Don't use S->y for clipping because it doesn't take partially
1943 visible lines into account. For example, it can be negative for
1944 partially visible lines at the top of a window. */
1945 if (!s->row->full_width_p
1946 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
1947 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1948 else
1949 r.y = max (0, s->row->y);
1950
1951 /* If drawing a tool-bar window, draw it over the internal border
1952 at the top of the window. */
1953 if (WINDOWP (s->f->tool_bar_window)
1954 && s->w == XWINDOW (s->f->tool_bar_window))
1955 r.y -= FRAME_INTERNAL_BORDER_WIDTH (s->f);
1956 }
1957
1958 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
1959
1960 /* If drawing the cursor, don't let glyph draw outside its
1961 advertised boundaries. Cleartype does this under some circumstances. */
1962 if (s->hl == DRAW_CURSOR)
1963 {
1964 struct glyph *glyph = s->first_glyph;
1965 int height, max_y;
1966
1967 if (s->x > r.x)
1968 {
1969 r.width -= s->x - r.x;
1970 r.x = s->x;
1971 }
1972 r.width = min (r.width, glyph->pixel_width);
1973
1974 /* If r.y is below window bottom, ensure that we still see a cursor. */
1975 height = min (glyph->ascent + glyph->descent,
1976 min (FRAME_LINE_HEIGHT (s->f), s->row->visible_height));
1977 max_y = window_text_bottom_y (s->w) - height;
1978 max_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, max_y);
1979 if (s->ybase - glyph->ascent > max_y)
1980 {
1981 r.y = max_y;
1982 r.height = height;
1983 }
1984 else
1985 {
1986 /* Don't draw cursor glyph taller than our actual glyph. */
1987 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
1988 if (height < r.height)
1989 {
1990 max_y = r.y + r.height;
1991 r.y = min (max_y, max (r.y, s->ybase + glyph->descent - height));
1992 r.height = min (max_y - r.y, height);
1993 }
1994 }
1995 }
1996
1997 if (s->row->clip)
1998 {
1999 XRectangle r_save = r;
2000
2001 if (! x_intersect_rectangles (&r_save, s->row->clip, &r))
2002 r.width = 0;
2003 }
2004
2005 if ((s->for_overlaps & OVERLAPS_BOTH) == 0
2006 || ((s->for_overlaps & OVERLAPS_BOTH) == OVERLAPS_BOTH && n == 1))
2007 {
2008 #ifdef CONVERT_FROM_XRECT
2009 CONVERT_FROM_XRECT (r, *rects);
2010 #else
2011 *rects = r;
2012 #endif
2013 return 1;
2014 }
2015 else
2016 {
2017 /* If we are processing overlapping and allowed to return
2018 multiple clipping rectangles, we exclude the row of the glyph
2019 string from the clipping rectangle. This is to avoid drawing
2020 the same text on the environment with anti-aliasing. */
2021 #ifdef CONVERT_FROM_XRECT
2022 XRectangle rs[2];
2023 #else
2024 XRectangle *rs = rects;
2025 #endif
2026 int i = 0, row_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, s->row->y);
2027
2028 if (s->for_overlaps & OVERLAPS_PRED)
2029 {
2030 rs[i] = r;
2031 if (r.y + r.height > row_y)
2032 {
2033 if (r.y < row_y)
2034 rs[i].height = row_y - r.y;
2035 else
2036 rs[i].height = 0;
2037 }
2038 i++;
2039 }
2040 if (s->for_overlaps & OVERLAPS_SUCC)
2041 {
2042 rs[i] = r;
2043 if (r.y < row_y + s->row->visible_height)
2044 {
2045 if (r.y + r.height > row_y + s->row->visible_height)
2046 {
2047 rs[i].y = row_y + s->row->visible_height;
2048 rs[i].height = r.y + r.height - rs[i].y;
2049 }
2050 else
2051 rs[i].height = 0;
2052 }
2053 i++;
2054 }
2055
2056 n = i;
2057 #ifdef CONVERT_FROM_XRECT
2058 for (i = 0; i < n; i++)
2059 CONVERT_FROM_XRECT (rs[i], rects[i]);
2060 #endif
2061 return n;
2062 }
2063 }
2064
2065 /* EXPORT:
2066 Return in *NR the clipping rectangle for glyph string S. */
2067
2068 void
2069 get_glyph_string_clip_rect (s, nr)
2070 struct glyph_string *s;
2071 NativeRectangle *nr;
2072 {
2073 get_glyph_string_clip_rects (s, nr, 1);
2074 }
2075
2076
2077 /* EXPORT:
2078 Return the position and height of the phys cursor in window W.
2079 Set w->phys_cursor_width to width of phys cursor.
2080 */
2081
2082 void
2083 get_phys_cursor_geometry (w, row, glyph, xp, yp, heightp)
2084 struct window *w;
2085 struct glyph_row *row;
2086 struct glyph *glyph;
2087 int *xp, *yp, *heightp;
2088 {
2089 struct frame *f = XFRAME (WINDOW_FRAME (w));
2090 int x, y, wd, h, h0, y0;
2091
2092 /* Compute the width of the rectangle to draw. If on a stretch
2093 glyph, and `x-stretch-block-cursor' is nil, don't draw a
2094 rectangle as wide as the glyph, but use a canonical character
2095 width instead. */
2096 wd = glyph->pixel_width - 1;
2097 #if defined(HAVE_NTGUI) || defined(HAVE_NS)
2098 wd++; /* Why? */
2099 #endif
2100
2101 x = w->phys_cursor.x;
2102 if (x < 0)
2103 {
2104 wd += x;
2105 x = 0;
2106 }
2107
2108 if (glyph->type == STRETCH_GLYPH
2109 && !x_stretch_cursor_p)
2110 wd = min (FRAME_COLUMN_WIDTH (f), wd);
2111 w->phys_cursor_width = wd;
2112
2113 y = w->phys_cursor.y + row->ascent - glyph->ascent;
2114
2115 /* If y is below window bottom, ensure that we still see a cursor. */
2116 h0 = min (FRAME_LINE_HEIGHT (f), row->visible_height);
2117
2118 h = max (h0, glyph->ascent + glyph->descent);
2119 h0 = min (h0, glyph->ascent + glyph->descent);
2120
2121 y0 = WINDOW_HEADER_LINE_HEIGHT (w);
2122 if (y < y0)
2123 {
2124 h = max (h - (y0 - y) + 1, h0);
2125 y = y0 - 1;
2126 }
2127 else
2128 {
2129 y0 = window_text_bottom_y (w) - h0;
2130 if (y > y0)
2131 {
2132 h += y - y0;
2133 y = y0;
2134 }
2135 }
2136
2137 *xp = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
2138 *yp = WINDOW_TO_FRAME_PIXEL_Y (w, y);
2139 *heightp = h;
2140 }
2141
2142 /*
2143 * Remember which glyph the mouse is over.
2144 */
2145
2146 void
2147 remember_mouse_glyph (f, gx, gy, rect)
2148 struct frame *f;
2149 int gx, gy;
2150 NativeRectangle *rect;
2151 {
2152 Lisp_Object window;
2153 struct window *w;
2154 struct glyph_row *r, *gr, *end_row;
2155 enum window_part part;
2156 enum glyph_row_area area;
2157 int x, y, width, height;
2158
2159 /* Try to determine frame pixel position and size of the glyph under
2160 frame pixel coordinates X/Y on frame F. */
2161
2162 if (!f->glyphs_initialized_p
2163 || (window = window_from_coordinates (f, gx, gy, &part, &x, &y, 0),
2164 NILP (window)))
2165 {
2166 width = FRAME_SMALLEST_CHAR_WIDTH (f);
2167 height = FRAME_SMALLEST_FONT_HEIGHT (f);
2168 goto virtual_glyph;
2169 }
2170
2171 w = XWINDOW (window);
2172 width = WINDOW_FRAME_COLUMN_WIDTH (w);
2173 height = WINDOW_FRAME_LINE_HEIGHT (w);
2174
2175 r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
2176 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
2177
2178 if (w->pseudo_window_p)
2179 {
2180 area = TEXT_AREA;
2181 part = ON_MODE_LINE; /* Don't adjust margin. */
2182 goto text_glyph;
2183 }
2184
2185 switch (part)
2186 {
2187 case ON_LEFT_MARGIN:
2188 area = LEFT_MARGIN_AREA;
2189 goto text_glyph;
2190
2191 case ON_RIGHT_MARGIN:
2192 area = RIGHT_MARGIN_AREA;
2193 goto text_glyph;
2194
2195 case ON_HEADER_LINE:
2196 case ON_MODE_LINE:
2197 gr = (part == ON_HEADER_LINE
2198 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
2199 : MATRIX_MODE_LINE_ROW (w->current_matrix));
2200 gy = gr->y;
2201 area = TEXT_AREA;
2202 goto text_glyph_row_found;
2203
2204 case ON_TEXT:
2205 area = TEXT_AREA;
2206
2207 text_glyph:
2208 gr = 0; gy = 0;
2209 for (; r <= end_row && r->enabled_p; ++r)
2210 if (r->y + r->height > y)
2211 {
2212 gr = r; gy = r->y;
2213 break;
2214 }
2215
2216 text_glyph_row_found:
2217 if (gr && gy <= y)
2218 {
2219 struct glyph *g = gr->glyphs[area];
2220 struct glyph *end = g + gr->used[area];
2221
2222 height = gr->height;
2223 for (gx = gr->x; g < end; gx += g->pixel_width, ++g)
2224 if (gx + g->pixel_width > x)
2225 break;
2226
2227 if (g < end)
2228 {
2229 if (g->type == IMAGE_GLYPH)
2230 {
2231 /* Don't remember when mouse is over image, as
2232 image may have hot-spots. */
2233 STORE_NATIVE_RECT (*rect, 0, 0, 0, 0);
2234 return;
2235 }
2236 width = g->pixel_width;
2237 }
2238 else
2239 {
2240 /* Use nominal char spacing at end of line. */
2241 x -= gx;
2242 gx += (x / width) * width;
2243 }
2244
2245 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2246 gx += window_box_left_offset (w, area);
2247 }
2248 else
2249 {
2250 /* Use nominal line height at end of window. */
2251 gx = (x / width) * width;
2252 y -= gy;
2253 gy += (y / height) * height;
2254 }
2255 break;
2256
2257 case ON_LEFT_FRINGE:
2258 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2259 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w)
2260 : window_box_right_offset (w, LEFT_MARGIN_AREA));
2261 width = WINDOW_LEFT_FRINGE_WIDTH (w);
2262 goto row_glyph;
2263
2264 case ON_RIGHT_FRINGE:
2265 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2266 ? window_box_right_offset (w, RIGHT_MARGIN_AREA)
2267 : window_box_right_offset (w, TEXT_AREA));
2268 width = WINDOW_RIGHT_FRINGE_WIDTH (w);
2269 goto row_glyph;
2270
2271 case ON_SCROLL_BAR:
2272 gx = (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
2273 ? 0
2274 : (window_box_right_offset (w, RIGHT_MARGIN_AREA)
2275 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2276 ? WINDOW_RIGHT_FRINGE_WIDTH (w)
2277 : 0)));
2278 width = WINDOW_SCROLL_BAR_AREA_WIDTH (w);
2279
2280 row_glyph:
2281 gr = 0, gy = 0;
2282 for (; r <= end_row && r->enabled_p; ++r)
2283 if (r->y + r->height > y)
2284 {
2285 gr = r; gy = r->y;
2286 break;
2287 }
2288
2289 if (gr && gy <= y)
2290 height = gr->height;
2291 else
2292 {
2293 /* Use nominal line height at end of window. */
2294 y -= gy;
2295 gy += (y / height) * height;
2296 }
2297 break;
2298
2299 default:
2300 ;
2301 virtual_glyph:
2302 /* If there is no glyph under the mouse, then we divide the screen
2303 into a grid of the smallest glyph in the frame, and use that
2304 as our "glyph". */
2305
2306 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to
2307 round down even for negative values. */
2308 if (gx < 0)
2309 gx -= width - 1;
2310 if (gy < 0)
2311 gy -= height - 1;
2312
2313 gx = (gx / width) * width;
2314 gy = (gy / height) * height;
2315
2316 goto store_rect;
2317 }
2318
2319 gx += WINDOW_LEFT_EDGE_X (w);
2320 gy += WINDOW_TOP_EDGE_Y (w);
2321
2322 store_rect:
2323 STORE_NATIVE_RECT (*rect, gx, gy, width, height);
2324
2325 /* Visible feedback for debugging. */
2326 #if 0
2327 #if HAVE_X_WINDOWS
2328 XDrawRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2329 f->output_data.x->normal_gc,
2330 gx, gy, width, height);
2331 #endif
2332 #endif
2333 }
2334
2335
2336 #endif /* HAVE_WINDOW_SYSTEM */
2337
2338 \f
2339 /***********************************************************************
2340 Lisp form evaluation
2341 ***********************************************************************/
2342
2343 /* Error handler for safe_eval and safe_call. */
2344
2345 static Lisp_Object
2346 safe_eval_handler (arg)
2347 Lisp_Object arg;
2348 {
2349 add_to_log ("Error during redisplay: %s", arg, Qnil);
2350 return Qnil;
2351 }
2352
2353
2354 /* Evaluate SEXPR and return the result, or nil if something went
2355 wrong. Prevent redisplay during the evaluation. */
2356
2357 /* Call function ARGS[0] with arguments ARGS[1] to ARGS[NARGS - 1].
2358 Return the result, or nil if something went wrong. Prevent
2359 redisplay during the evaluation. */
2360
2361 Lisp_Object
2362 safe_call (nargs, args)
2363 int nargs;
2364 Lisp_Object *args;
2365 {
2366 Lisp_Object val;
2367
2368 if (inhibit_eval_during_redisplay)
2369 val = Qnil;
2370 else
2371 {
2372 int count = SPECPDL_INDEX ();
2373 struct gcpro gcpro1;
2374
2375 GCPRO1 (args[0]);
2376 gcpro1.nvars = nargs;
2377 specbind (Qinhibit_redisplay, Qt);
2378 /* Use Qt to ensure debugger does not run,
2379 so there is no possibility of wanting to redisplay. */
2380 val = internal_condition_case_2 (Ffuncall, nargs, args, Qt,
2381 safe_eval_handler);
2382 UNGCPRO;
2383 val = unbind_to (count, val);
2384 }
2385
2386 return val;
2387 }
2388
2389
2390 /* Call function FN with one argument ARG.
2391 Return the result, or nil if something went wrong. */
2392
2393 Lisp_Object
2394 safe_call1 (fn, arg)
2395 Lisp_Object fn, arg;
2396 {
2397 Lisp_Object args[2];
2398 args[0] = fn;
2399 args[1] = arg;
2400 return safe_call (2, args);
2401 }
2402
2403 static Lisp_Object Qeval;
2404
2405 Lisp_Object
2406 safe_eval (Lisp_Object sexpr)
2407 {
2408 return safe_call1 (Qeval, sexpr);
2409 }
2410
2411 /* Call function FN with one argument ARG.
2412 Return the result, or nil if something went wrong. */
2413
2414 Lisp_Object
2415 safe_call2 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2)
2416 {
2417 Lisp_Object args[3];
2418 args[0] = fn;
2419 args[1] = arg1;
2420 args[2] = arg2;
2421 return safe_call (3, args);
2422 }
2423
2424
2425 \f
2426 /***********************************************************************
2427 Debugging
2428 ***********************************************************************/
2429
2430 #if 0
2431
2432 /* Define CHECK_IT to perform sanity checks on iterators.
2433 This is for debugging. It is too slow to do unconditionally. */
2434
2435 static void
2436 check_it (it)
2437 struct it *it;
2438 {
2439 if (it->method == GET_FROM_STRING)
2440 {
2441 xassert (STRINGP (it->string));
2442 xassert (IT_STRING_CHARPOS (*it) >= 0);
2443 }
2444 else
2445 {
2446 xassert (IT_STRING_CHARPOS (*it) < 0);
2447 if (it->method == GET_FROM_BUFFER)
2448 {
2449 /* Check that character and byte positions agree. */
2450 xassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
2451 }
2452 }
2453
2454 if (it->dpvec)
2455 xassert (it->current.dpvec_index >= 0);
2456 else
2457 xassert (it->current.dpvec_index < 0);
2458 }
2459
2460 #define CHECK_IT(IT) check_it ((IT))
2461
2462 #else /* not 0 */
2463
2464 #define CHECK_IT(IT) (void) 0
2465
2466 #endif /* not 0 */
2467
2468
2469 #if GLYPH_DEBUG
2470
2471 /* Check that the window end of window W is what we expect it
2472 to be---the last row in the current matrix displaying text. */
2473
2474 static void
2475 check_window_end (w)
2476 struct window *w;
2477 {
2478 if (!MINI_WINDOW_P (w)
2479 && !NILP (w->window_end_valid))
2480 {
2481 struct glyph_row *row;
2482 xassert ((row = MATRIX_ROW (w->current_matrix,
2483 XFASTINT (w->window_end_vpos)),
2484 !row->enabled_p
2485 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
2486 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
2487 }
2488 }
2489
2490 #define CHECK_WINDOW_END(W) check_window_end ((W))
2491
2492 #else /* not GLYPH_DEBUG */
2493
2494 #define CHECK_WINDOW_END(W) (void) 0
2495
2496 #endif /* not GLYPH_DEBUG */
2497
2498
2499 \f
2500 /***********************************************************************
2501 Iterator initialization
2502 ***********************************************************************/
2503
2504 /* Initialize IT for displaying current_buffer in window W, starting
2505 at character position CHARPOS. CHARPOS < 0 means that no buffer
2506 position is specified which is useful when the iterator is assigned
2507 a position later. BYTEPOS is the byte position corresponding to
2508 CHARPOS. BYTEPOS < 0 means compute it from CHARPOS.
2509
2510 If ROW is not null, calls to produce_glyphs with IT as parameter
2511 will produce glyphs in that row.
2512
2513 BASE_FACE_ID is the id of a base face to use. It must be one of
2514 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2515 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2516 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2517
2518 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2519 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2520 will be initialized to use the corresponding mode line glyph row of
2521 the desired matrix of W. */
2522
2523 void
2524 init_iterator (it, w, charpos, bytepos, row, base_face_id)
2525 struct it *it;
2526 struct window *w;
2527 int charpos, bytepos;
2528 struct glyph_row *row;
2529 enum face_id base_face_id;
2530 {
2531 int highlight_region_p;
2532 enum face_id remapped_base_face_id = base_face_id;
2533
2534 /* Some precondition checks. */
2535 xassert (w != NULL && it != NULL);
2536 xassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2537 && charpos <= ZV));
2538
2539 /* If face attributes have been changed since the last redisplay,
2540 free realized faces now because they depend on face definitions
2541 that might have changed. Don't free faces while there might be
2542 desired matrices pending which reference these faces. */
2543 if (face_change_count && !inhibit_free_realized_faces)
2544 {
2545 face_change_count = 0;
2546 free_all_realized_faces (Qnil);
2547 }
2548
2549 /* Perhaps remap BASE_FACE_ID to a user-specified alternative. */
2550 if (! NILP (Vface_remapping_alist))
2551 remapped_base_face_id = lookup_basic_face (XFRAME (w->frame), base_face_id);
2552
2553 /* Use one of the mode line rows of W's desired matrix if
2554 appropriate. */
2555 if (row == NULL)
2556 {
2557 if (base_face_id == MODE_LINE_FACE_ID
2558 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2559 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2560 else if (base_face_id == HEADER_LINE_FACE_ID)
2561 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2562 }
2563
2564 /* Clear IT. */
2565 bzero (it, sizeof *it);
2566 it->current.overlay_string_index = -1;
2567 it->current.dpvec_index = -1;
2568 it->base_face_id = remapped_base_face_id;
2569 it->string = Qnil;
2570 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2571
2572 /* The window in which we iterate over current_buffer: */
2573 XSETWINDOW (it->window, w);
2574 it->w = w;
2575 it->f = XFRAME (w->frame);
2576
2577 /* Extra space between lines (on window systems only). */
2578 if (base_face_id == DEFAULT_FACE_ID
2579 && FRAME_WINDOW_P (it->f))
2580 {
2581 if (NATNUMP (current_buffer->extra_line_spacing))
2582 it->extra_line_spacing = XFASTINT (current_buffer->extra_line_spacing);
2583 else if (FLOATP (current_buffer->extra_line_spacing))
2584 it->extra_line_spacing = (XFLOAT_DATA (current_buffer->extra_line_spacing)
2585 * FRAME_LINE_HEIGHT (it->f));
2586 else if (it->f->extra_line_spacing > 0)
2587 it->extra_line_spacing = it->f->extra_line_spacing;
2588 it->max_extra_line_spacing = 0;
2589 }
2590
2591 /* If realized faces have been removed, e.g. because of face
2592 attribute changes of named faces, recompute them. When running
2593 in batch mode, the face cache of the initial frame is null. If
2594 we happen to get called, make a dummy face cache. */
2595 if (FRAME_FACE_CACHE (it->f) == NULL)
2596 init_frame_faces (it->f);
2597 if (FRAME_FACE_CACHE (it->f)->used == 0)
2598 recompute_basic_faces (it->f);
2599
2600 /* Current value of the `slice', `space-width', and 'height' properties. */
2601 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
2602 it->space_width = Qnil;
2603 it->font_height = Qnil;
2604 it->override_ascent = -1;
2605
2606 /* Are control characters displayed as `^C'? */
2607 it->ctl_arrow_p = !NILP (current_buffer->ctl_arrow);
2608
2609 /* -1 means everything between a CR and the following line end
2610 is invisible. >0 means lines indented more than this value are
2611 invisible. */
2612 it->selective = (INTEGERP (current_buffer->selective_display)
2613 ? XFASTINT (current_buffer->selective_display)
2614 : (!NILP (current_buffer->selective_display)
2615 ? -1 : 0));
2616 it->selective_display_ellipsis_p
2617 = !NILP (current_buffer->selective_display_ellipses);
2618
2619 /* Display table to use. */
2620 it->dp = window_display_table (w);
2621
2622 /* Are multibyte characters enabled in current_buffer? */
2623 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
2624
2625 /* Non-zero if we should highlight the region. */
2626 highlight_region_p
2627 = (!NILP (Vtransient_mark_mode)
2628 && !NILP (current_buffer->mark_active)
2629 && XMARKER (current_buffer->mark)->buffer != 0);
2630
2631 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
2632 start and end of a visible region in window IT->w. Set both to
2633 -1 to indicate no region. */
2634 if (highlight_region_p
2635 /* Maybe highlight only in selected window. */
2636 && (/* Either show region everywhere. */
2637 highlight_nonselected_windows
2638 /* Or show region in the selected window. */
2639 || w == XWINDOW (selected_window)
2640 /* Or show the region if we are in the mini-buffer and W is
2641 the window the mini-buffer refers to. */
2642 || (MINI_WINDOW_P (XWINDOW (selected_window))
2643 && WINDOWP (minibuf_selected_window)
2644 && w == XWINDOW (minibuf_selected_window))))
2645 {
2646 int charpos = marker_position (current_buffer->mark);
2647 it->region_beg_charpos = min (PT, charpos);
2648 it->region_end_charpos = max (PT, charpos);
2649 }
2650 else
2651 it->region_beg_charpos = it->region_end_charpos = -1;
2652
2653 /* Get the position at which the redisplay_end_trigger hook should
2654 be run, if it is to be run at all. */
2655 if (MARKERP (w->redisplay_end_trigger)
2656 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2657 it->redisplay_end_trigger_charpos
2658 = marker_position (w->redisplay_end_trigger);
2659 else if (INTEGERP (w->redisplay_end_trigger))
2660 it->redisplay_end_trigger_charpos = XINT (w->redisplay_end_trigger);
2661
2662 /* Correct bogus values of tab_width. */
2663 it->tab_width = XINT (current_buffer->tab_width);
2664 if (it->tab_width <= 0 || it->tab_width > 1000)
2665 it->tab_width = 8;
2666
2667 /* Are lines in the display truncated? */
2668 if (base_face_id != DEFAULT_FACE_ID
2669 || XINT (it->w->hscroll)
2670 || (! WINDOW_FULL_WIDTH_P (it->w)
2671 && ((!NILP (Vtruncate_partial_width_windows)
2672 && !INTEGERP (Vtruncate_partial_width_windows))
2673 || (INTEGERP (Vtruncate_partial_width_windows)
2674 && (WINDOW_TOTAL_COLS (it->w)
2675 < XINT (Vtruncate_partial_width_windows))))))
2676 it->line_wrap = TRUNCATE;
2677 else if (NILP (current_buffer->truncate_lines))
2678 it->line_wrap = NILP (current_buffer->word_wrap)
2679 ? WINDOW_WRAP : WORD_WRAP;
2680 else
2681 it->line_wrap = TRUNCATE;
2682
2683 /* Get dimensions of truncation and continuation glyphs. These are
2684 displayed as fringe bitmaps under X, so we don't need them for such
2685 frames. */
2686 if (!FRAME_WINDOW_P (it->f))
2687 {
2688 if (it->line_wrap == TRUNCATE)
2689 {
2690 /* We will need the truncation glyph. */
2691 xassert (it->glyph_row == NULL);
2692 produce_special_glyphs (it, IT_TRUNCATION);
2693 it->truncation_pixel_width = it->pixel_width;
2694 }
2695 else
2696 {
2697 /* We will need the continuation glyph. */
2698 xassert (it->glyph_row == NULL);
2699 produce_special_glyphs (it, IT_CONTINUATION);
2700 it->continuation_pixel_width = it->pixel_width;
2701 }
2702
2703 /* Reset these values to zero because the produce_special_glyphs
2704 above has changed them. */
2705 it->pixel_width = it->ascent = it->descent = 0;
2706 it->phys_ascent = it->phys_descent = 0;
2707 }
2708
2709 /* Set this after getting the dimensions of truncation and
2710 continuation glyphs, so that we don't produce glyphs when calling
2711 produce_special_glyphs, above. */
2712 it->glyph_row = row;
2713 it->area = TEXT_AREA;
2714
2715 /* Get the dimensions of the display area. The display area
2716 consists of the visible window area plus a horizontally scrolled
2717 part to the left of the window. All x-values are relative to the
2718 start of this total display area. */
2719 if (base_face_id != DEFAULT_FACE_ID)
2720 {
2721 /* Mode lines, menu bar in terminal frames. */
2722 it->first_visible_x = 0;
2723 it->last_visible_x = WINDOW_TOTAL_WIDTH (w);
2724 }
2725 else
2726 {
2727 it->first_visible_x
2728 = XFASTINT (it->w->hscroll) * FRAME_COLUMN_WIDTH (it->f);
2729 it->last_visible_x = (it->first_visible_x
2730 + window_box_width (w, TEXT_AREA));
2731
2732 /* If we truncate lines, leave room for the truncator glyph(s) at
2733 the right margin. Otherwise, leave room for the continuation
2734 glyph(s). Truncation and continuation glyphs are not inserted
2735 for window-based redisplay. */
2736 if (!FRAME_WINDOW_P (it->f))
2737 {
2738 if (it->line_wrap == TRUNCATE)
2739 it->last_visible_x -= it->truncation_pixel_width;
2740 else
2741 it->last_visible_x -= it->continuation_pixel_width;
2742 }
2743
2744 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2745 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2746 }
2747
2748 /* Leave room for a border glyph. */
2749 if (!FRAME_WINDOW_P (it->f)
2750 && !WINDOW_RIGHTMOST_P (it->w))
2751 it->last_visible_x -= 1;
2752
2753 it->last_visible_y = window_text_bottom_y (w);
2754
2755 /* For mode lines and alike, arrange for the first glyph having a
2756 left box line if the face specifies a box. */
2757 if (base_face_id != DEFAULT_FACE_ID)
2758 {
2759 struct face *face;
2760
2761 it->face_id = remapped_base_face_id;
2762
2763 /* If we have a boxed mode line, make the first character appear
2764 with a left box line. */
2765 face = FACE_FROM_ID (it->f, remapped_base_face_id);
2766 if (face->box != FACE_NO_BOX)
2767 it->start_of_box_run_p = 1;
2768 }
2769
2770 /* If a buffer position was specified, set the iterator there,
2771 getting overlays and face properties from that position. */
2772 if (charpos >= BUF_BEG (current_buffer))
2773 {
2774 it->end_charpos = ZV;
2775 it->face_id = -1;
2776 IT_CHARPOS (*it) = charpos;
2777
2778 /* Compute byte position if not specified. */
2779 if (bytepos < charpos)
2780 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
2781 else
2782 IT_BYTEPOS (*it) = bytepos;
2783
2784 it->start = it->current;
2785
2786 /* Compute faces etc. */
2787 reseat (it, it->current.pos, 1);
2788 }
2789
2790 CHECK_IT (it);
2791 }
2792
2793
2794 /* Initialize IT for the display of window W with window start POS. */
2795
2796 void
2797 start_display (it, w, pos)
2798 struct it *it;
2799 struct window *w;
2800 struct text_pos pos;
2801 {
2802 struct glyph_row *row;
2803 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
2804
2805 row = w->desired_matrix->rows + first_vpos;
2806 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
2807 it->first_vpos = first_vpos;
2808
2809 /* Don't reseat to previous visible line start if current start
2810 position is in a string or image. */
2811 if (it->method == GET_FROM_BUFFER && it->line_wrap != TRUNCATE)
2812 {
2813 int start_at_line_beg_p;
2814 int first_y = it->current_y;
2815
2816 /* If window start is not at a line start, skip forward to POS to
2817 get the correct continuation lines width. */
2818 start_at_line_beg_p = (CHARPOS (pos) == BEGV
2819 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
2820 if (!start_at_line_beg_p)
2821 {
2822 int new_x;
2823
2824 reseat_at_previous_visible_line_start (it);
2825 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
2826
2827 new_x = it->current_x + it->pixel_width;
2828
2829 /* If lines are continued, this line may end in the middle
2830 of a multi-glyph character (e.g. a control character
2831 displayed as \003, or in the middle of an overlay
2832 string). In this case move_it_to above will not have
2833 taken us to the start of the continuation line but to the
2834 end of the continued line. */
2835 if (it->current_x > 0
2836 && it->line_wrap != TRUNCATE /* Lines are continued. */
2837 && (/* And glyph doesn't fit on the line. */
2838 new_x > it->last_visible_x
2839 /* Or it fits exactly and we're on a window
2840 system frame. */
2841 || (new_x == it->last_visible_x
2842 && FRAME_WINDOW_P (it->f))))
2843 {
2844 if (it->current.dpvec_index >= 0
2845 || it->current.overlay_string_index >= 0)
2846 {
2847 set_iterator_to_next (it, 1);
2848 move_it_in_display_line_to (it, -1, -1, 0);
2849 }
2850
2851 it->continuation_lines_width += it->current_x;
2852 }
2853
2854 /* We're starting a new display line, not affected by the
2855 height of the continued line, so clear the appropriate
2856 fields in the iterator structure. */
2857 it->max_ascent = it->max_descent = 0;
2858 it->max_phys_ascent = it->max_phys_descent = 0;
2859
2860 it->current_y = first_y;
2861 it->vpos = 0;
2862 it->current_x = it->hpos = 0;
2863 }
2864 }
2865
2866 #if 0 /* Don't assert the following because start_display is sometimes
2867 called intentionally with a window start that is not at a
2868 line start. Please leave this code in as a comment. */
2869
2870 /* Window start should be on a line start, now. */
2871 xassert (it->continuation_lines_width
2872 || IT_CHARPOS (it) == BEGV
2873 || FETCH_BYTE (IT_BYTEPOS (it) - 1) == '\n');
2874 #endif /* 0 */
2875 }
2876
2877
2878 /* Return 1 if POS is a position in ellipses displayed for invisible
2879 text. W is the window we display, for text property lookup. */
2880
2881 static int
2882 in_ellipses_for_invisible_text_p (pos, w)
2883 struct display_pos *pos;
2884 struct window *w;
2885 {
2886 Lisp_Object prop, window;
2887 int ellipses_p = 0;
2888 int charpos = CHARPOS (pos->pos);
2889
2890 /* If POS specifies a position in a display vector, this might
2891 be for an ellipsis displayed for invisible text. We won't
2892 get the iterator set up for delivering that ellipsis unless
2893 we make sure that it gets aware of the invisible text. */
2894 if (pos->dpvec_index >= 0
2895 && pos->overlay_string_index < 0
2896 && CHARPOS (pos->string_pos) < 0
2897 && charpos > BEGV
2898 && (XSETWINDOW (window, w),
2899 prop = Fget_char_property (make_number (charpos),
2900 Qinvisible, window),
2901 !TEXT_PROP_MEANS_INVISIBLE (prop)))
2902 {
2903 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
2904 window);
2905 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
2906 }
2907
2908 return ellipses_p;
2909 }
2910
2911
2912 /* Initialize IT for stepping through current_buffer in window W,
2913 starting at position POS that includes overlay string and display
2914 vector/ control character translation position information. Value
2915 is zero if there are overlay strings with newlines at POS. */
2916
2917 static int
2918 init_from_display_pos (it, w, pos)
2919 struct it *it;
2920 struct window *w;
2921 struct display_pos *pos;
2922 {
2923 int charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
2924 int i, overlay_strings_with_newlines = 0;
2925
2926 /* If POS specifies a position in a display vector, this might
2927 be for an ellipsis displayed for invisible text. We won't
2928 get the iterator set up for delivering that ellipsis unless
2929 we make sure that it gets aware of the invisible text. */
2930 if (in_ellipses_for_invisible_text_p (pos, w))
2931 {
2932 --charpos;
2933 bytepos = 0;
2934 }
2935
2936 /* Keep in mind: the call to reseat in init_iterator skips invisible
2937 text, so we might end up at a position different from POS. This
2938 is only a problem when POS is a row start after a newline and an
2939 overlay starts there with an after-string, and the overlay has an
2940 invisible property. Since we don't skip invisible text in
2941 display_line and elsewhere immediately after consuming the
2942 newline before the row start, such a POS will not be in a string,
2943 but the call to init_iterator below will move us to the
2944 after-string. */
2945 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
2946
2947 /* This only scans the current chunk -- it should scan all chunks.
2948 However, OVERLAY_STRING_CHUNK_SIZE has been increased from 3 in 21.1
2949 to 16 in 22.1 to make this a lesser problem. */
2950 for (i = 0; i < it->n_overlay_strings && i < OVERLAY_STRING_CHUNK_SIZE; ++i)
2951 {
2952 const char *s = SDATA (it->overlay_strings[i]);
2953 const char *e = s + SBYTES (it->overlay_strings[i]);
2954
2955 while (s < e && *s != '\n')
2956 ++s;
2957
2958 if (s < e)
2959 {
2960 overlay_strings_with_newlines = 1;
2961 break;
2962 }
2963 }
2964
2965 /* If position is within an overlay string, set up IT to the right
2966 overlay string. */
2967 if (pos->overlay_string_index >= 0)
2968 {
2969 int relative_index;
2970
2971 /* If the first overlay string happens to have a `display'
2972 property for an image, the iterator will be set up for that
2973 image, and we have to undo that setup first before we can
2974 correct the overlay string index. */
2975 if (it->method == GET_FROM_IMAGE)
2976 pop_it (it);
2977
2978 /* We already have the first chunk of overlay strings in
2979 IT->overlay_strings. Load more until the one for
2980 pos->overlay_string_index is in IT->overlay_strings. */
2981 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
2982 {
2983 int n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
2984 it->current.overlay_string_index = 0;
2985 while (n--)
2986 {
2987 load_overlay_strings (it, 0);
2988 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
2989 }
2990 }
2991
2992 it->current.overlay_string_index = pos->overlay_string_index;
2993 relative_index = (it->current.overlay_string_index
2994 % OVERLAY_STRING_CHUNK_SIZE);
2995 it->string = it->overlay_strings[relative_index];
2996 xassert (STRINGP (it->string));
2997 it->current.string_pos = pos->string_pos;
2998 it->method = GET_FROM_STRING;
2999 }
3000
3001 #if 0 /* This is bogus because POS not having an overlay string
3002 position does not mean it's after the string. Example: A
3003 line starting with a before-string and initialization of IT
3004 to the previous row's end position. */
3005 else if (it->current.overlay_string_index >= 0)
3006 {
3007 /* If POS says we're already after an overlay string ending at
3008 POS, make sure to pop the iterator because it will be in
3009 front of that overlay string. When POS is ZV, we've thereby
3010 also ``processed'' overlay strings at ZV. */
3011 while (it->sp)
3012 pop_it (it);
3013 xassert (it->current.overlay_string_index == -1);
3014 xassert (it->method == GET_FROM_BUFFER);
3015 if (CHARPOS (pos->pos) == ZV)
3016 it->overlay_strings_at_end_processed_p = 1;
3017 }
3018 #endif /* 0 */
3019
3020 if (CHARPOS (pos->string_pos) >= 0)
3021 {
3022 /* Recorded position is not in an overlay string, but in another
3023 string. This can only be a string from a `display' property.
3024 IT should already be filled with that string. */
3025 it->current.string_pos = pos->string_pos;
3026 xassert (STRINGP (it->string));
3027 }
3028
3029 /* Restore position in display vector translations, control
3030 character translations or ellipses. */
3031 if (pos->dpvec_index >= 0)
3032 {
3033 if (it->dpvec == NULL)
3034 get_next_display_element (it);
3035 xassert (it->dpvec && it->current.dpvec_index == 0);
3036 it->current.dpvec_index = pos->dpvec_index;
3037 }
3038
3039 CHECK_IT (it);
3040 return !overlay_strings_with_newlines;
3041 }
3042
3043
3044 /* Initialize IT for stepping through current_buffer in window W
3045 starting at ROW->start. */
3046
3047 static void
3048 init_to_row_start (it, w, row)
3049 struct it *it;
3050 struct window *w;
3051 struct glyph_row *row;
3052 {
3053 init_from_display_pos (it, w, &row->start);
3054 it->start = row->start;
3055 it->continuation_lines_width = row->continuation_lines_width;
3056 CHECK_IT (it);
3057 }
3058
3059
3060 /* Initialize IT for stepping through current_buffer in window W
3061 starting in the line following ROW, i.e. starting at ROW->end.
3062 Value is zero if there are overlay strings with newlines at ROW's
3063 end position. */
3064
3065 static int
3066 init_to_row_end (it, w, row)
3067 struct it *it;
3068 struct window *w;
3069 struct glyph_row *row;
3070 {
3071 int success = 0;
3072
3073 if (init_from_display_pos (it, w, &row->end))
3074 {
3075 if (row->continued_p)
3076 it->continuation_lines_width
3077 = row->continuation_lines_width + row->pixel_width;
3078 CHECK_IT (it);
3079 success = 1;
3080 }
3081
3082 return success;
3083 }
3084
3085
3086
3087 \f
3088 /***********************************************************************
3089 Text properties
3090 ***********************************************************************/
3091
3092 /* Called when IT reaches IT->stop_charpos. Handle text property and
3093 overlay changes. Set IT->stop_charpos to the next position where
3094 to stop. */
3095
3096 static void
3097 handle_stop (it)
3098 struct it *it;
3099 {
3100 enum prop_handled handled;
3101 int handle_overlay_change_p;
3102 struct props *p;
3103
3104 it->dpvec = NULL;
3105 it->current.dpvec_index = -1;
3106 handle_overlay_change_p = !it->ignore_overlay_strings_at_pos_p;
3107 it->ignore_overlay_strings_at_pos_p = 0;
3108 it->ellipsis_p = 0;
3109
3110 /* Use face of preceding text for ellipsis (if invisible) */
3111 if (it->selective_display_ellipsis_p)
3112 it->saved_face_id = it->face_id;
3113
3114 do
3115 {
3116 handled = HANDLED_NORMALLY;
3117
3118 /* Call text property handlers. */
3119 for (p = it_props; p->handler; ++p)
3120 {
3121 handled = p->handler (it);
3122
3123 if (handled == HANDLED_RECOMPUTE_PROPS)
3124 break;
3125 else if (handled == HANDLED_RETURN)
3126 {
3127 /* We still want to show before and after strings from
3128 overlays even if the actual buffer text is replaced. */
3129 if (!handle_overlay_change_p
3130 || it->sp > 1
3131 || !get_overlay_strings_1 (it, 0, 0))
3132 {
3133 if (it->ellipsis_p)
3134 setup_for_ellipsis (it, 0);
3135 return;
3136 }
3137 it->ignore_overlay_strings_at_pos_p = 1;
3138 it->string_from_display_prop_p = 0;
3139 handle_overlay_change_p = 0;
3140 handled = HANDLED_RECOMPUTE_PROPS;
3141 break;
3142 }
3143 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
3144 handle_overlay_change_p = 0;
3145 }
3146
3147 if (handled != HANDLED_RECOMPUTE_PROPS)
3148 {
3149 /* Don't check for overlay strings below when set to deliver
3150 characters from a display vector. */
3151 if (it->method == GET_FROM_DISPLAY_VECTOR)
3152 handle_overlay_change_p = 0;
3153
3154 /* Handle overlay changes.
3155 This sets HANDLED to HANDLED_RECOMPUTE_PROPS
3156 if it finds overlays. */
3157 if (handle_overlay_change_p)
3158 handled = handle_overlay_change (it);
3159 }
3160
3161 if (it->ellipsis_p)
3162 {
3163 setup_for_ellipsis (it, 0);
3164 break;
3165 }
3166 }
3167 while (handled == HANDLED_RECOMPUTE_PROPS);
3168
3169 /* Determine where to stop next. */
3170 if (handled == HANDLED_NORMALLY)
3171 compute_stop_pos (it);
3172 }
3173
3174
3175 /* Compute IT->stop_charpos from text property and overlay change
3176 information for IT's current position. */
3177
3178 static void
3179 compute_stop_pos (it)
3180 struct it *it;
3181 {
3182 register INTERVAL iv, next_iv;
3183 Lisp_Object object, limit, position;
3184
3185 /* If nowhere else, stop at the end. */
3186 it->stop_charpos = it->end_charpos;
3187
3188 if (STRINGP (it->string))
3189 {
3190 /* Strings are usually short, so don't limit the search for
3191 properties. */
3192 object = it->string;
3193 limit = Qnil;
3194 position = make_number (IT_STRING_CHARPOS (*it));
3195 }
3196 else
3197 {
3198 int charpos;
3199
3200 /* If next overlay change is in front of the current stop pos
3201 (which is IT->end_charpos), stop there. Note: value of
3202 next_overlay_change is point-max if no overlay change
3203 follows. */
3204 charpos = next_overlay_change (IT_CHARPOS (*it));
3205 if (charpos < it->stop_charpos)
3206 it->stop_charpos = charpos;
3207
3208 /* If showing the region, we have to stop at the region
3209 start or end because the face might change there. */
3210 if (it->region_beg_charpos > 0)
3211 {
3212 if (IT_CHARPOS (*it) < it->region_beg_charpos)
3213 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
3214 else if (IT_CHARPOS (*it) < it->region_end_charpos)
3215 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
3216 }
3217
3218 /* Set up variables for computing the stop position from text
3219 property changes. */
3220 XSETBUFFER (object, current_buffer);
3221 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
3222 position = make_number (IT_CHARPOS (*it));
3223
3224 }
3225
3226 /* Get the interval containing IT's position. Value is a null
3227 interval if there isn't such an interval. */
3228 iv = validate_interval_range (object, &position, &position, 0);
3229 if (!NULL_INTERVAL_P (iv))
3230 {
3231 Lisp_Object values_here[LAST_PROP_IDX];
3232 struct props *p;
3233
3234 /* Get properties here. */
3235 for (p = it_props; p->handler; ++p)
3236 values_here[p->idx] = textget (iv->plist, *p->name);
3237
3238 /* Look for an interval following iv that has different
3239 properties. */
3240 for (next_iv = next_interval (iv);
3241 (!NULL_INTERVAL_P (next_iv)
3242 && (NILP (limit)
3243 || XFASTINT (limit) > next_iv->position));
3244 next_iv = next_interval (next_iv))
3245 {
3246 for (p = it_props; p->handler; ++p)
3247 {
3248 Lisp_Object new_value;
3249
3250 new_value = textget (next_iv->plist, *p->name);
3251 if (!EQ (values_here[p->idx], new_value))
3252 break;
3253 }
3254
3255 if (p->handler)
3256 break;
3257 }
3258
3259 if (!NULL_INTERVAL_P (next_iv))
3260 {
3261 if (INTEGERP (limit)
3262 && next_iv->position >= XFASTINT (limit))
3263 /* No text property change up to limit. */
3264 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
3265 else
3266 /* Text properties change in next_iv. */
3267 it->stop_charpos = min (it->stop_charpos, next_iv->position);
3268 }
3269 }
3270
3271 xassert (STRINGP (it->string)
3272 || (it->stop_charpos >= BEGV
3273 && it->stop_charpos >= IT_CHARPOS (*it)));
3274 }
3275
3276
3277 /* Return the position of the next overlay change after POS in
3278 current_buffer. Value is point-max if no overlay change
3279 follows. This is like `next-overlay-change' but doesn't use
3280 xmalloc. */
3281
3282 static EMACS_INT
3283 next_overlay_change (pos)
3284 EMACS_INT pos;
3285 {
3286 int noverlays;
3287 EMACS_INT endpos;
3288 Lisp_Object *overlays;
3289 int i;
3290
3291 /* Get all overlays at the given position. */
3292 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, 1);
3293
3294 /* If any of these overlays ends before endpos,
3295 use its ending point instead. */
3296 for (i = 0; i < noverlays; ++i)
3297 {
3298 Lisp_Object oend;
3299 EMACS_INT oendpos;
3300
3301 oend = OVERLAY_END (overlays[i]);
3302 oendpos = OVERLAY_POSITION (oend);
3303 endpos = min (endpos, oendpos);
3304 }
3305
3306 return endpos;
3307 }
3308
3309
3310 \f
3311 /***********************************************************************
3312 Fontification
3313 ***********************************************************************/
3314
3315 /* Handle changes in the `fontified' property of the current buffer by
3316 calling hook functions from Qfontification_functions to fontify
3317 regions of text. */
3318
3319 static enum prop_handled
3320 handle_fontified_prop (it)
3321 struct it *it;
3322 {
3323 Lisp_Object prop, pos;
3324 enum prop_handled handled = HANDLED_NORMALLY;
3325
3326 if (!NILP (Vmemory_full))
3327 return handled;
3328
3329 /* Get the value of the `fontified' property at IT's current buffer
3330 position. (The `fontified' property doesn't have a special
3331 meaning in strings.) If the value is nil, call functions from
3332 Qfontification_functions. */
3333 if (!STRINGP (it->string)
3334 && it->s == NULL
3335 && !NILP (Vfontification_functions)
3336 && !NILP (Vrun_hooks)
3337 && (pos = make_number (IT_CHARPOS (*it)),
3338 prop = Fget_char_property (pos, Qfontified, Qnil),
3339 /* Ignore the special cased nil value always present at EOB since
3340 no amount of fontifying will be able to change it. */
3341 NILP (prop) && IT_CHARPOS (*it) < Z))
3342 {
3343 int count = SPECPDL_INDEX ();
3344 Lisp_Object val;
3345
3346 val = Vfontification_functions;
3347 specbind (Qfontification_functions, Qnil);
3348
3349 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
3350 safe_call1 (val, pos);
3351 else
3352 {
3353 Lisp_Object globals, fn;
3354 struct gcpro gcpro1, gcpro2;
3355
3356 globals = Qnil;
3357 GCPRO2 (val, globals);
3358
3359 for (; CONSP (val); val = XCDR (val))
3360 {
3361 fn = XCAR (val);
3362
3363 if (EQ (fn, Qt))
3364 {
3365 /* A value of t indicates this hook has a local
3366 binding; it means to run the global binding too.
3367 In a global value, t should not occur. If it
3368 does, we must ignore it to avoid an endless
3369 loop. */
3370 for (globals = Fdefault_value (Qfontification_functions);
3371 CONSP (globals);
3372 globals = XCDR (globals))
3373 {
3374 fn = XCAR (globals);
3375 if (!EQ (fn, Qt))
3376 safe_call1 (fn, pos);
3377 }
3378 }
3379 else
3380 safe_call1 (fn, pos);
3381 }
3382
3383 UNGCPRO;
3384 }
3385
3386 unbind_to (count, Qnil);
3387
3388 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
3389 something. This avoids an endless loop if they failed to
3390 fontify the text for which reason ever. */
3391 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
3392 handled = HANDLED_RECOMPUTE_PROPS;
3393 }
3394
3395 return handled;
3396 }
3397
3398
3399 \f
3400 /***********************************************************************
3401 Faces
3402 ***********************************************************************/
3403
3404 /* Set up iterator IT from face properties at its current position.
3405 Called from handle_stop. */
3406
3407 static enum prop_handled
3408 handle_face_prop (it)
3409 struct it *it;
3410 {
3411 int new_face_id;
3412 EMACS_INT next_stop;
3413
3414 if (!STRINGP (it->string))
3415 {
3416 new_face_id
3417 = face_at_buffer_position (it->w,
3418 IT_CHARPOS (*it),
3419 it->region_beg_charpos,
3420 it->region_end_charpos,
3421 &next_stop,
3422 (IT_CHARPOS (*it)
3423 + TEXT_PROP_DISTANCE_LIMIT),
3424 0);
3425
3426 /* Is this a start of a run of characters with box face?
3427 Caveat: this can be called for a freshly initialized
3428 iterator; face_id is -1 in this case. We know that the new
3429 face will not change until limit, i.e. if the new face has a
3430 box, all characters up to limit will have one. But, as
3431 usual, we don't know whether limit is really the end. */
3432 if (new_face_id != it->face_id)
3433 {
3434 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3435
3436 /* If new face has a box but old face has not, this is
3437 the start of a run of characters with box, i.e. it has
3438 a shadow on the left side. The value of face_id of the
3439 iterator will be -1 if this is the initial call that gets
3440 the face. In this case, we have to look in front of IT's
3441 position and see whether there is a face != new_face_id. */
3442 it->start_of_box_run_p
3443 = (new_face->box != FACE_NO_BOX
3444 && (it->face_id >= 0
3445 || IT_CHARPOS (*it) == BEG
3446 || new_face_id != face_before_it_pos (it)));
3447 it->face_box_p = new_face->box != FACE_NO_BOX;
3448 }
3449 }
3450 else
3451 {
3452 int base_face_id, bufpos;
3453 int i;
3454 Lisp_Object from_overlay
3455 = (it->current.overlay_string_index >= 0
3456 ? it->string_overlays[it->current.overlay_string_index]
3457 : Qnil);
3458
3459 /* See if we got to this string directly or indirectly from
3460 an overlay property. That includes the before-string or
3461 after-string of an overlay, strings in display properties
3462 provided by an overlay, their text properties, etc.
3463
3464 FROM_OVERLAY is the overlay that brought us here, or nil if none. */
3465 if (! NILP (from_overlay))
3466 for (i = it->sp - 1; i >= 0; i--)
3467 {
3468 if (it->stack[i].current.overlay_string_index >= 0)
3469 from_overlay
3470 = it->string_overlays[it->stack[i].current.overlay_string_index];
3471 else if (! NILP (it->stack[i].from_overlay))
3472 from_overlay = it->stack[i].from_overlay;
3473
3474 if (!NILP (from_overlay))
3475 break;
3476 }
3477
3478 if (! NILP (from_overlay))
3479 {
3480 bufpos = IT_CHARPOS (*it);
3481 /* For a string from an overlay, the base face depends
3482 only on text properties and ignores overlays. */
3483 base_face_id
3484 = face_for_overlay_string (it->w,
3485 IT_CHARPOS (*it),
3486 it->region_beg_charpos,
3487 it->region_end_charpos,
3488 &next_stop,
3489 (IT_CHARPOS (*it)
3490 + TEXT_PROP_DISTANCE_LIMIT),
3491 0,
3492 from_overlay);
3493 }
3494 else
3495 {
3496 bufpos = 0;
3497
3498 /* For strings from a `display' property, use the face at
3499 IT's current buffer position as the base face to merge
3500 with, so that overlay strings appear in the same face as
3501 surrounding text, unless they specify their own
3502 faces. */
3503 base_face_id = underlying_face_id (it);
3504 }
3505
3506 new_face_id = face_at_string_position (it->w,
3507 it->string,
3508 IT_STRING_CHARPOS (*it),
3509 bufpos,
3510 it->region_beg_charpos,
3511 it->region_end_charpos,
3512 &next_stop,
3513 base_face_id, 0);
3514
3515 #if 0 /* This shouldn't be neccessary. Let's check it. */
3516 /* If IT is used to display a mode line we would really like to
3517 use the mode line face instead of the frame's default face. */
3518 if (it->glyph_row == MATRIX_MODE_LINE_ROW (it->w->desired_matrix)
3519 && new_face_id == DEFAULT_FACE_ID)
3520 new_face_id = CURRENT_MODE_LINE_FACE_ID (it->w);
3521 #endif
3522
3523 /* Is this a start of a run of characters with box? Caveat:
3524 this can be called for a freshly allocated iterator; face_id
3525 is -1 is this case. We know that the new face will not
3526 change until the next check pos, i.e. if the new face has a
3527 box, all characters up to that position will have a
3528 box. But, as usual, we don't know whether that position
3529 is really the end. */
3530 if (new_face_id != it->face_id)
3531 {
3532 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3533 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3534
3535 /* If new face has a box but old face hasn't, this is the
3536 start of a run of characters with box, i.e. it has a
3537 shadow on the left side. */
3538 it->start_of_box_run_p
3539 = new_face->box && (old_face == NULL || !old_face->box);
3540 it->face_box_p = new_face->box != FACE_NO_BOX;
3541 }
3542 }
3543
3544 it->face_id = new_face_id;
3545 return HANDLED_NORMALLY;
3546 }
3547
3548
3549 /* Return the ID of the face ``underlying'' IT's current position,
3550 which is in a string. If the iterator is associated with a
3551 buffer, return the face at IT's current buffer position.
3552 Otherwise, use the iterator's base_face_id. */
3553
3554 static int
3555 underlying_face_id (it)
3556 struct it *it;
3557 {
3558 int face_id = it->base_face_id, i;
3559
3560 xassert (STRINGP (it->string));
3561
3562 for (i = it->sp - 1; i >= 0; --i)
3563 if (NILP (it->stack[i].string))
3564 face_id = it->stack[i].face_id;
3565
3566 return face_id;
3567 }
3568
3569
3570 /* Compute the face one character before or after the current position
3571 of IT. BEFORE_P non-zero means get the face in front of IT's
3572 position. Value is the id of the face. */
3573
3574 static int
3575 face_before_or_after_it_pos (it, before_p)
3576 struct it *it;
3577 int before_p;
3578 {
3579 int face_id, limit;
3580 EMACS_INT next_check_charpos;
3581 struct text_pos pos;
3582
3583 xassert (it->s == NULL);
3584
3585 if (STRINGP (it->string))
3586 {
3587 int bufpos, base_face_id;
3588
3589 /* No face change past the end of the string (for the case
3590 we are padding with spaces). No face change before the
3591 string start. */
3592 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
3593 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
3594 return it->face_id;
3595
3596 /* Set pos to the position before or after IT's current position. */
3597 if (before_p)
3598 pos = string_pos (IT_STRING_CHARPOS (*it) - 1, it->string);
3599 else
3600 /* For composition, we must check the character after the
3601 composition. */
3602 pos = (it->what == IT_COMPOSITION
3603 ? string_pos (IT_STRING_CHARPOS (*it) + it->cmp_len, it->string)
3604 : string_pos (IT_STRING_CHARPOS (*it) + 1, it->string));
3605
3606 if (it->current.overlay_string_index >= 0)
3607 bufpos = IT_CHARPOS (*it);
3608 else
3609 bufpos = 0;
3610
3611 base_face_id = underlying_face_id (it);
3612
3613 /* Get the face for ASCII, or unibyte. */
3614 face_id = face_at_string_position (it->w,
3615 it->string,
3616 CHARPOS (pos),
3617 bufpos,
3618 it->region_beg_charpos,
3619 it->region_end_charpos,
3620 &next_check_charpos,
3621 base_face_id, 0);
3622
3623 /* Correct the face for charsets different from ASCII. Do it
3624 for the multibyte case only. The face returned above is
3625 suitable for unibyte text if IT->string is unibyte. */
3626 if (STRING_MULTIBYTE (it->string))
3627 {
3628 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos);
3629 int rest = SBYTES (it->string) - BYTEPOS (pos);
3630 int c, len;
3631 struct face *face = FACE_FROM_ID (it->f, face_id);
3632
3633 c = string_char_and_length (p, rest, &len);
3634 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), it->string);
3635 }
3636 }
3637 else
3638 {
3639 if ((IT_CHARPOS (*it) >= ZV && !before_p)
3640 || (IT_CHARPOS (*it) <= BEGV && before_p))
3641 return it->face_id;
3642
3643 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
3644 pos = it->current.pos;
3645
3646 if (before_p)
3647 DEC_TEXT_POS (pos, it->multibyte_p);
3648 else
3649 {
3650 if (it->what == IT_COMPOSITION)
3651 /* For composition, we must check the position after the
3652 composition. */
3653 pos.charpos += it->cmp_len, pos.bytepos += it->len;
3654 else
3655 INC_TEXT_POS (pos, it->multibyte_p);
3656 }
3657
3658 /* Determine face for CHARSET_ASCII, or unibyte. */
3659 face_id = face_at_buffer_position (it->w,
3660 CHARPOS (pos),
3661 it->region_beg_charpos,
3662 it->region_end_charpos,
3663 &next_check_charpos,
3664 limit, 0);
3665
3666 /* Correct the face for charsets different from ASCII. Do it
3667 for the multibyte case only. The face returned above is
3668 suitable for unibyte text if current_buffer is unibyte. */
3669 if (it->multibyte_p)
3670 {
3671 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
3672 struct face *face = FACE_FROM_ID (it->f, face_id);
3673 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), Qnil);
3674 }
3675 }
3676
3677 return face_id;
3678 }
3679
3680
3681 \f
3682 /***********************************************************************
3683 Invisible text
3684 ***********************************************************************/
3685
3686 /* Set up iterator IT from invisible properties at its current
3687 position. Called from handle_stop. */
3688
3689 static enum prop_handled
3690 handle_invisible_prop (it)
3691 struct it *it;
3692 {
3693 enum prop_handled handled = HANDLED_NORMALLY;
3694
3695 if (STRINGP (it->string))
3696 {
3697 extern Lisp_Object Qinvisible;
3698 Lisp_Object prop, end_charpos, limit, charpos;
3699
3700 /* Get the value of the invisible text property at the
3701 current position. Value will be nil if there is no such
3702 property. */
3703 charpos = make_number (IT_STRING_CHARPOS (*it));
3704 prop = Fget_text_property (charpos, Qinvisible, it->string);
3705
3706 if (!NILP (prop)
3707 && IT_STRING_CHARPOS (*it) < it->end_charpos)
3708 {
3709 handled = HANDLED_RECOMPUTE_PROPS;
3710
3711 /* Get the position at which the next change of the
3712 invisible text property can be found in IT->string.
3713 Value will be nil if the property value is the same for
3714 all the rest of IT->string. */
3715 XSETINT (limit, SCHARS (it->string));
3716 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
3717 it->string, limit);
3718
3719 /* Text at current position is invisible. The next
3720 change in the property is at position end_charpos.
3721 Move IT's current position to that position. */
3722 if (INTEGERP (end_charpos)
3723 && XFASTINT (end_charpos) < XFASTINT (limit))
3724 {
3725 struct text_pos old;
3726 old = it->current.string_pos;
3727 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
3728 compute_string_pos (&it->current.string_pos, old, it->string);
3729 }
3730 else
3731 {
3732 /* The rest of the string is invisible. If this is an
3733 overlay string, proceed with the next overlay string
3734 or whatever comes and return a character from there. */
3735 if (it->current.overlay_string_index >= 0)
3736 {
3737 next_overlay_string (it);
3738 /* Don't check for overlay strings when we just
3739 finished processing them. */
3740 handled = HANDLED_OVERLAY_STRING_CONSUMED;
3741 }
3742 else
3743 {
3744 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
3745 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
3746 }
3747 }
3748 }
3749 }
3750 else
3751 {
3752 int invis_p;
3753 EMACS_INT newpos, next_stop, start_charpos;
3754 Lisp_Object pos, prop, overlay;
3755
3756 /* First of all, is there invisible text at this position? */
3757 start_charpos = IT_CHARPOS (*it);
3758 pos = make_number (IT_CHARPOS (*it));
3759 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
3760 &overlay);
3761 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3762
3763 /* If we are on invisible text, skip over it. */
3764 if (invis_p && IT_CHARPOS (*it) < it->end_charpos)
3765 {
3766 /* Record whether we have to display an ellipsis for the
3767 invisible text. */
3768 int display_ellipsis_p = invis_p == 2;
3769
3770 handled = HANDLED_RECOMPUTE_PROPS;
3771
3772 /* Loop skipping over invisible text. The loop is left at
3773 ZV or with IT on the first char being visible again. */
3774 do
3775 {
3776 /* Try to skip some invisible text. Return value is the
3777 position reached which can be equal to IT's position
3778 if there is nothing invisible here. This skips both
3779 over invisible text properties and overlays with
3780 invisible property. */
3781 newpos = skip_invisible (IT_CHARPOS (*it),
3782 &next_stop, ZV, it->window);
3783
3784 /* If we skipped nothing at all we weren't at invisible
3785 text in the first place. If everything to the end of
3786 the buffer was skipped, end the loop. */
3787 if (newpos == IT_CHARPOS (*it) || newpos >= ZV)
3788 invis_p = 0;
3789 else
3790 {
3791 /* We skipped some characters but not necessarily
3792 all there are. Check if we ended up on visible
3793 text. Fget_char_property returns the property of
3794 the char before the given position, i.e. if we
3795 get invis_p = 0, this means that the char at
3796 newpos is visible. */
3797 pos = make_number (newpos);
3798 prop = Fget_char_property (pos, Qinvisible, it->window);
3799 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3800 }
3801
3802 /* If we ended up on invisible text, proceed to
3803 skip starting with next_stop. */
3804 if (invis_p)
3805 IT_CHARPOS (*it) = next_stop;
3806
3807 /* If there are adjacent invisible texts, don't lose the
3808 second one's ellipsis. */
3809 if (invis_p == 2)
3810 display_ellipsis_p = 1;
3811 }
3812 while (invis_p);
3813
3814 /* The position newpos is now either ZV or on visible text. */
3815 IT_CHARPOS (*it) = newpos;
3816 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
3817
3818 /* If there are before-strings at the start of invisible
3819 text, and the text is invisible because of a text
3820 property, arrange to show before-strings because 20.x did
3821 it that way. (If the text is invisible because of an
3822 overlay property instead of a text property, this is
3823 already handled in the overlay code.) */
3824 if (NILP (overlay)
3825 && get_overlay_strings (it, start_charpos))
3826 {
3827 handled = HANDLED_RECOMPUTE_PROPS;
3828 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
3829 }
3830 else if (display_ellipsis_p)
3831 {
3832 /* Make sure that the glyphs of the ellipsis will get
3833 correct `charpos' values. If we would not update
3834 it->position here, the glyphs would belong to the
3835 last visible character _before_ the invisible
3836 text, which confuses `set_cursor_from_row'.
3837
3838 We use the last invisible position instead of the
3839 first because this way the cursor is always drawn on
3840 the first "." of the ellipsis, whenever PT is inside
3841 the invisible text. Otherwise the cursor would be
3842 placed _after_ the ellipsis when the point is after the
3843 first invisible character. */
3844 if (!STRINGP (it->object))
3845 {
3846 it->position.charpos = IT_CHARPOS (*it) - 1;
3847 it->position.bytepos = CHAR_TO_BYTE (it->position.charpos);
3848 }
3849 it->ellipsis_p = 1;
3850 /* Let the ellipsis display before
3851 considering any properties of the following char.
3852 Fixes jasonr@gnu.org 01 Oct 07 bug. */
3853 handled = HANDLED_RETURN;
3854 }
3855 }
3856 }
3857
3858 return handled;
3859 }
3860
3861
3862 /* Make iterator IT return `...' next.
3863 Replaces LEN characters from buffer. */
3864
3865 static void
3866 setup_for_ellipsis (it, len)
3867 struct it *it;
3868 int len;
3869 {
3870 /* Use the display table definition for `...'. Invalid glyphs
3871 will be handled by the method returning elements from dpvec. */
3872 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
3873 {
3874 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
3875 it->dpvec = v->contents;
3876 it->dpend = v->contents + v->size;
3877 }
3878 else
3879 {
3880 /* Default `...'. */
3881 it->dpvec = default_invis_vector;
3882 it->dpend = default_invis_vector + 3;
3883 }
3884
3885 it->dpvec_char_len = len;
3886 it->current.dpvec_index = 0;
3887 it->dpvec_face_id = -1;
3888
3889 /* Remember the current face id in case glyphs specify faces.
3890 IT's face is restored in set_iterator_to_next.
3891 saved_face_id was set to preceding char's face in handle_stop. */
3892 if (it->saved_face_id < 0 || it->saved_face_id != it->face_id)
3893 it->saved_face_id = it->face_id = DEFAULT_FACE_ID;
3894
3895 it->method = GET_FROM_DISPLAY_VECTOR;
3896 it->ellipsis_p = 1;
3897 }
3898
3899
3900 \f
3901 /***********************************************************************
3902 'display' property
3903 ***********************************************************************/
3904
3905 /* Set up iterator IT from `display' property at its current position.
3906 Called from handle_stop.
3907 We return HANDLED_RETURN if some part of the display property
3908 overrides the display of the buffer text itself.
3909 Otherwise we return HANDLED_NORMALLY. */
3910
3911 static enum prop_handled
3912 handle_display_prop (it)
3913 struct it *it;
3914 {
3915 Lisp_Object prop, object, overlay;
3916 struct text_pos *position;
3917 /* Nonzero if some property replaces the display of the text itself. */
3918 int display_replaced_p = 0;
3919
3920 if (STRINGP (it->string))
3921 {
3922 object = it->string;
3923 position = &it->current.string_pos;
3924 }
3925 else
3926 {
3927 XSETWINDOW (object, it->w);
3928 position = &it->current.pos;
3929 }
3930
3931 /* Reset those iterator values set from display property values. */
3932 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
3933 it->space_width = Qnil;
3934 it->font_height = Qnil;
3935 it->voffset = 0;
3936
3937 /* We don't support recursive `display' properties, i.e. string
3938 values that have a string `display' property, that have a string
3939 `display' property etc. */
3940 if (!it->string_from_display_prop_p)
3941 it->area = TEXT_AREA;
3942
3943 prop = get_char_property_and_overlay (make_number (position->charpos),
3944 Qdisplay, object, &overlay);
3945 if (NILP (prop))
3946 return HANDLED_NORMALLY;
3947 /* Now OVERLAY is the overlay that gave us this property, or nil
3948 if it was a text property. */
3949
3950 if (!STRINGP (it->string))
3951 object = it->w->buffer;
3952
3953 if (CONSP (prop)
3954 /* Simple properties. */
3955 && !EQ (XCAR (prop), Qimage)
3956 && !EQ (XCAR (prop), Qspace)
3957 && !EQ (XCAR (prop), Qwhen)
3958 && !EQ (XCAR (prop), Qslice)
3959 && !EQ (XCAR (prop), Qspace_width)
3960 && !EQ (XCAR (prop), Qheight)
3961 && !EQ (XCAR (prop), Qraise)
3962 /* Marginal area specifications. */
3963 && !(CONSP (XCAR (prop)) && EQ (XCAR (XCAR (prop)), Qmargin))
3964 && !EQ (XCAR (prop), Qleft_fringe)
3965 && !EQ (XCAR (prop), Qright_fringe)
3966 && !NILP (XCAR (prop)))
3967 {
3968 for (; CONSP (prop); prop = XCDR (prop))
3969 {
3970 if (handle_single_display_spec (it, XCAR (prop), object, overlay,
3971 position, display_replaced_p))
3972 {
3973 display_replaced_p = 1;
3974 /* If some text in a string is replaced, `position' no
3975 longer points to the position of `object'. */
3976 if (STRINGP (object))
3977 break;
3978 }
3979 }
3980 }
3981 else if (VECTORP (prop))
3982 {
3983 int i;
3984 for (i = 0; i < ASIZE (prop); ++i)
3985 if (handle_single_display_spec (it, AREF (prop, i), object, overlay,
3986 position, display_replaced_p))
3987 {
3988 display_replaced_p = 1;
3989 /* If some text in a string is replaced, `position' no
3990 longer points to the position of `object'. */
3991 if (STRINGP (object))
3992 break;
3993 }
3994 }
3995 else
3996 {
3997 int ret = handle_single_display_spec (it, prop, object, overlay,
3998 position, 0);
3999 if (ret < 0) /* Replaced by "", i.e. nothing. */
4000 return HANDLED_RECOMPUTE_PROPS;
4001 if (ret)
4002 display_replaced_p = 1;
4003 }
4004
4005 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
4006 }
4007
4008
4009 /* Value is the position of the end of the `display' property starting
4010 at START_POS in OBJECT. */
4011
4012 static struct text_pos
4013 display_prop_end (it, object, start_pos)
4014 struct it *it;
4015 Lisp_Object object;
4016 struct text_pos start_pos;
4017 {
4018 Lisp_Object end;
4019 struct text_pos end_pos;
4020
4021 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
4022 Qdisplay, object, Qnil);
4023 CHARPOS (end_pos) = XFASTINT (end);
4024 if (STRINGP (object))
4025 compute_string_pos (&end_pos, start_pos, it->string);
4026 else
4027 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
4028
4029 return end_pos;
4030 }
4031
4032
4033 /* Set up IT from a single `display' specification PROP. OBJECT
4034 is the object in which the `display' property was found. *POSITION
4035 is the position at which it was found. DISPLAY_REPLACED_P non-zero
4036 means that we previously saw a display specification which already
4037 replaced text display with something else, for example an image;
4038 we ignore such properties after the first one has been processed.
4039
4040 OVERLAY is the overlay this `display' property came from,
4041 or nil if it was a text property.
4042
4043 If PROP is a `space' or `image' specification, and in some other
4044 cases too, set *POSITION to the position where the `display'
4045 property ends.
4046
4047 Value is non-zero if something was found which replaces the display
4048 of buffer or string text. Specifically, the value is -1 if that
4049 "something" is "nothing". */
4050
4051 static int
4052 handle_single_display_spec (it, spec, object, overlay, position,
4053 display_replaced_before_p)
4054 struct it *it;
4055 Lisp_Object spec;
4056 Lisp_Object object;
4057 Lisp_Object overlay;
4058 struct text_pos *position;
4059 int display_replaced_before_p;
4060 {
4061 Lisp_Object form;
4062 Lisp_Object location, value;
4063 struct text_pos start_pos, save_pos;
4064 int valid_p;
4065
4066 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
4067 If the result is non-nil, use VALUE instead of SPEC. */
4068 form = Qt;
4069 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
4070 {
4071 spec = XCDR (spec);
4072 if (!CONSP (spec))
4073 return 0;
4074 form = XCAR (spec);
4075 spec = XCDR (spec);
4076 }
4077
4078 if (!NILP (form) && !EQ (form, Qt))
4079 {
4080 int count = SPECPDL_INDEX ();
4081 struct gcpro gcpro1;
4082
4083 /* Bind `object' to the object having the `display' property, a
4084 buffer or string. Bind `position' to the position in the
4085 object where the property was found, and `buffer-position'
4086 to the current position in the buffer. */
4087 specbind (Qobject, object);
4088 specbind (Qposition, make_number (CHARPOS (*position)));
4089 specbind (Qbuffer_position,
4090 make_number (STRINGP (object)
4091 ? IT_CHARPOS (*it) : CHARPOS (*position)));
4092 GCPRO1 (form);
4093 form = safe_eval (form);
4094 UNGCPRO;
4095 unbind_to (count, Qnil);
4096 }
4097
4098 if (NILP (form))
4099 return 0;
4100
4101 /* Handle `(height HEIGHT)' specifications. */
4102 if (CONSP (spec)
4103 && EQ (XCAR (spec), Qheight)
4104 && CONSP (XCDR (spec)))
4105 {
4106 if (!FRAME_WINDOW_P (it->f))
4107 return 0;
4108
4109 it->font_height = XCAR (XCDR (spec));
4110 if (!NILP (it->font_height))
4111 {
4112 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4113 int new_height = -1;
4114
4115 if (CONSP (it->font_height)
4116 && (EQ (XCAR (it->font_height), Qplus)
4117 || EQ (XCAR (it->font_height), Qminus))
4118 && CONSP (XCDR (it->font_height))
4119 && INTEGERP (XCAR (XCDR (it->font_height))))
4120 {
4121 /* `(+ N)' or `(- N)' where N is an integer. */
4122 int steps = XINT (XCAR (XCDR (it->font_height)));
4123 if (EQ (XCAR (it->font_height), Qplus))
4124 steps = - steps;
4125 it->face_id = smaller_face (it->f, it->face_id, steps);
4126 }
4127 else if (FUNCTIONP (it->font_height))
4128 {
4129 /* Call function with current height as argument.
4130 Value is the new height. */
4131 Lisp_Object height;
4132 height = safe_call1 (it->font_height,
4133 face->lface[LFACE_HEIGHT_INDEX]);
4134 if (NUMBERP (height))
4135 new_height = XFLOATINT (height);
4136 }
4137 else if (NUMBERP (it->font_height))
4138 {
4139 /* Value is a multiple of the canonical char height. */
4140 struct face *face;
4141
4142 face = FACE_FROM_ID (it->f,
4143 lookup_basic_face (it->f, DEFAULT_FACE_ID));
4144 new_height = (XFLOATINT (it->font_height)
4145 * XINT (face->lface[LFACE_HEIGHT_INDEX]));
4146 }
4147 else
4148 {
4149 /* Evaluate IT->font_height with `height' bound to the
4150 current specified height to get the new height. */
4151 int count = SPECPDL_INDEX ();
4152
4153 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
4154 value = safe_eval (it->font_height);
4155 unbind_to (count, Qnil);
4156
4157 if (NUMBERP (value))
4158 new_height = XFLOATINT (value);
4159 }
4160
4161 if (new_height > 0)
4162 it->face_id = face_with_height (it->f, it->face_id, new_height);
4163 }
4164
4165 return 0;
4166 }
4167
4168 /* Handle `(space-width WIDTH)'. */
4169 if (CONSP (spec)
4170 && EQ (XCAR (spec), Qspace_width)
4171 && CONSP (XCDR (spec)))
4172 {
4173 if (!FRAME_WINDOW_P (it->f))
4174 return 0;
4175
4176 value = XCAR (XCDR (spec));
4177 if (NUMBERP (value) && XFLOATINT (value) > 0)
4178 it->space_width = value;
4179
4180 return 0;
4181 }
4182
4183 /* Handle `(slice X Y WIDTH HEIGHT)'. */
4184 if (CONSP (spec)
4185 && EQ (XCAR (spec), Qslice))
4186 {
4187 Lisp_Object tem;
4188
4189 if (!FRAME_WINDOW_P (it->f))
4190 return 0;
4191
4192 if (tem = XCDR (spec), CONSP (tem))
4193 {
4194 it->slice.x = XCAR (tem);
4195 if (tem = XCDR (tem), CONSP (tem))
4196 {
4197 it->slice.y = XCAR (tem);
4198 if (tem = XCDR (tem), CONSP (tem))
4199 {
4200 it->slice.width = XCAR (tem);
4201 if (tem = XCDR (tem), CONSP (tem))
4202 it->slice.height = XCAR (tem);
4203 }
4204 }
4205 }
4206
4207 return 0;
4208 }
4209
4210 /* Handle `(raise FACTOR)'. */
4211 if (CONSP (spec)
4212 && EQ (XCAR (spec), Qraise)
4213 && CONSP (XCDR (spec)))
4214 {
4215 if (!FRAME_WINDOW_P (it->f))
4216 return 0;
4217
4218 #ifdef HAVE_WINDOW_SYSTEM
4219 value = XCAR (XCDR (spec));
4220 if (NUMBERP (value))
4221 {
4222 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4223 it->voffset = - (XFLOATINT (value)
4224 * (FONT_HEIGHT (face->font)));
4225 }
4226 #endif /* HAVE_WINDOW_SYSTEM */
4227
4228 return 0;
4229 }
4230
4231 /* Don't handle the other kinds of display specifications
4232 inside a string that we got from a `display' property. */
4233 if (it->string_from_display_prop_p)
4234 return 0;
4235
4236 /* Characters having this form of property are not displayed, so
4237 we have to find the end of the property. */
4238 start_pos = *position;
4239 *position = display_prop_end (it, object, start_pos);
4240 value = Qnil;
4241
4242 /* Stop the scan at that end position--we assume that all
4243 text properties change there. */
4244 it->stop_charpos = position->charpos;
4245
4246 /* Handle `(left-fringe BITMAP [FACE])'
4247 and `(right-fringe BITMAP [FACE])'. */
4248 if (CONSP (spec)
4249 && (EQ (XCAR (spec), Qleft_fringe)
4250 || EQ (XCAR (spec), Qright_fringe))
4251 && CONSP (XCDR (spec)))
4252 {
4253 int face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);
4254 int fringe_bitmap;
4255
4256 if (!FRAME_WINDOW_P (it->f))
4257 /* If we return here, POSITION has been advanced
4258 across the text with this property. */
4259 return 0;
4260
4261 #ifdef HAVE_WINDOW_SYSTEM
4262 value = XCAR (XCDR (spec));
4263 if (!SYMBOLP (value)
4264 || !(fringe_bitmap = lookup_fringe_bitmap (value)))
4265 /* If we return here, POSITION has been advanced
4266 across the text with this property. */
4267 return 0;
4268
4269 if (CONSP (XCDR (XCDR (spec))))
4270 {
4271 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
4272 int face_id2 = lookup_derived_face (it->f, face_name,
4273 FRINGE_FACE_ID, 0);
4274 if (face_id2 >= 0)
4275 face_id = face_id2;
4276 }
4277
4278 /* Save current settings of IT so that we can restore them
4279 when we are finished with the glyph property value. */
4280
4281 save_pos = it->position;
4282 it->position = *position;
4283 push_it (it);
4284 it->position = save_pos;
4285
4286 it->area = TEXT_AREA;
4287 it->what = IT_IMAGE;
4288 it->image_id = -1; /* no image */
4289 it->position = start_pos;
4290 it->object = NILP (object) ? it->w->buffer : object;
4291 it->method = GET_FROM_IMAGE;
4292 it->from_overlay = Qnil;
4293 it->face_id = face_id;
4294
4295 /* Say that we haven't consumed the characters with
4296 `display' property yet. The call to pop_it in
4297 set_iterator_to_next will clean this up. */
4298 *position = start_pos;
4299
4300 if (EQ (XCAR (spec), Qleft_fringe))
4301 {
4302 it->left_user_fringe_bitmap = fringe_bitmap;
4303 it->left_user_fringe_face_id = face_id;
4304 }
4305 else
4306 {
4307 it->right_user_fringe_bitmap = fringe_bitmap;
4308 it->right_user_fringe_face_id = face_id;
4309 }
4310 #endif /* HAVE_WINDOW_SYSTEM */
4311 return 1;
4312 }
4313
4314 /* Prepare to handle `((margin left-margin) ...)',
4315 `((margin right-margin) ...)' and `((margin nil) ...)'
4316 prefixes for display specifications. */
4317 location = Qunbound;
4318 if (CONSP (spec) && CONSP (XCAR (spec)))
4319 {
4320 Lisp_Object tem;
4321
4322 value = XCDR (spec);
4323 if (CONSP (value))
4324 value = XCAR (value);
4325
4326 tem = XCAR (spec);
4327 if (EQ (XCAR (tem), Qmargin)
4328 && (tem = XCDR (tem),
4329 tem = CONSP (tem) ? XCAR (tem) : Qnil,
4330 (NILP (tem)
4331 || EQ (tem, Qleft_margin)
4332 || EQ (tem, Qright_margin))))
4333 location = tem;
4334 }
4335
4336 if (EQ (location, Qunbound))
4337 {
4338 location = Qnil;
4339 value = spec;
4340 }
4341
4342 /* After this point, VALUE is the property after any
4343 margin prefix has been stripped. It must be a string,
4344 an image specification, or `(space ...)'.
4345
4346 LOCATION specifies where to display: `left-margin',
4347 `right-margin' or nil. */
4348
4349 valid_p = (STRINGP (value)
4350 #ifdef HAVE_WINDOW_SYSTEM
4351 || (FRAME_WINDOW_P (it->f) && valid_image_p (value))
4352 #endif /* not HAVE_WINDOW_SYSTEM */
4353 || (CONSP (value) && EQ (XCAR (value), Qspace)));
4354
4355 if (valid_p && !display_replaced_before_p)
4356 {
4357 /* Save current settings of IT so that we can restore them
4358 when we are finished with the glyph property value. */
4359 save_pos = it->position;
4360 it->position = *position;
4361 push_it (it);
4362 it->position = save_pos;
4363 it->from_overlay = overlay;
4364
4365 if (NILP (location))
4366 it->area = TEXT_AREA;
4367 else if (EQ (location, Qleft_margin))
4368 it->area = LEFT_MARGIN_AREA;
4369 else
4370 it->area = RIGHT_MARGIN_AREA;
4371
4372 if (STRINGP (value))
4373 {
4374 if (SCHARS (value) == 0)
4375 {
4376 pop_it (it);
4377 return -1; /* Replaced by "", i.e. nothing. */
4378 }
4379 it->string = value;
4380 it->multibyte_p = STRING_MULTIBYTE (it->string);
4381 it->current.overlay_string_index = -1;
4382 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
4383 it->end_charpos = it->string_nchars = SCHARS (it->string);
4384 it->method = GET_FROM_STRING;
4385 it->stop_charpos = 0;
4386 it->string_from_display_prop_p = 1;
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 if (BUFFERP (object))
4391 *position = start_pos;
4392 }
4393 else if (CONSP (value) && EQ (XCAR (value), Qspace))
4394 {
4395 it->method = GET_FROM_STRETCH;
4396 it->object = value;
4397 *position = it->position = start_pos;
4398 }
4399 #ifdef HAVE_WINDOW_SYSTEM
4400 else
4401 {
4402 it->what = IT_IMAGE;
4403 it->image_id = lookup_image (it->f, value);
4404 it->position = start_pos;
4405 it->object = NILP (object) ? it->w->buffer : object;
4406 it->method = GET_FROM_IMAGE;
4407
4408 /* Say that we haven't consumed the characters with
4409 `display' property yet. The call to pop_it in
4410 set_iterator_to_next will clean this up. */
4411 *position = start_pos;
4412 }
4413 #endif /* HAVE_WINDOW_SYSTEM */
4414
4415 return 1;
4416 }
4417
4418 /* Invalid property or property not supported. Restore
4419 POSITION to what it was before. */
4420 *position = start_pos;
4421 return 0;
4422 }
4423
4424
4425 /* Check if SPEC is a display sub-property value whose text should be
4426 treated as intangible. */
4427
4428 static int
4429 single_display_spec_intangible_p (prop)
4430 Lisp_Object prop;
4431 {
4432 /* Skip over `when FORM'. */
4433 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
4434 {
4435 prop = XCDR (prop);
4436 if (!CONSP (prop))
4437 return 0;
4438 prop = XCDR (prop);
4439 }
4440
4441 if (STRINGP (prop))
4442 return 1;
4443
4444 if (!CONSP (prop))
4445 return 0;
4446
4447 /* Skip over `margin LOCATION'. If LOCATION is in the margins,
4448 we don't need to treat text as intangible. */
4449 if (EQ (XCAR (prop), Qmargin))
4450 {
4451 prop = XCDR (prop);
4452 if (!CONSP (prop))
4453 return 0;
4454
4455 prop = XCDR (prop);
4456 if (!CONSP (prop)
4457 || EQ (XCAR (prop), Qleft_margin)
4458 || EQ (XCAR (prop), Qright_margin))
4459 return 0;
4460 }
4461
4462 return (CONSP (prop)
4463 && (EQ (XCAR (prop), Qimage)
4464 || EQ (XCAR (prop), Qspace)));
4465 }
4466
4467
4468 /* Check if PROP is a display property value whose text should be
4469 treated as intangible. */
4470
4471 int
4472 display_prop_intangible_p (prop)
4473 Lisp_Object prop;
4474 {
4475 if (CONSP (prop)
4476 && CONSP (XCAR (prop))
4477 && !EQ (Qmargin, XCAR (XCAR (prop))))
4478 {
4479 /* A list of sub-properties. */
4480 while (CONSP (prop))
4481 {
4482 if (single_display_spec_intangible_p (XCAR (prop)))
4483 return 1;
4484 prop = XCDR (prop);
4485 }
4486 }
4487 else if (VECTORP (prop))
4488 {
4489 /* A vector of sub-properties. */
4490 int i;
4491 for (i = 0; i < ASIZE (prop); ++i)
4492 if (single_display_spec_intangible_p (AREF (prop, i)))
4493 return 1;
4494 }
4495 else
4496 return single_display_spec_intangible_p (prop);
4497
4498 return 0;
4499 }
4500
4501
4502 /* Return 1 if PROP is a display sub-property value containing STRING. */
4503
4504 static int
4505 single_display_spec_string_p (prop, string)
4506 Lisp_Object prop, string;
4507 {
4508 if (EQ (string, prop))
4509 return 1;
4510
4511 /* Skip over `when FORM'. */
4512 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
4513 {
4514 prop = XCDR (prop);
4515 if (!CONSP (prop))
4516 return 0;
4517 prop = XCDR (prop);
4518 }
4519
4520 if (CONSP (prop))
4521 /* Skip over `margin LOCATION'. */
4522 if (EQ (XCAR (prop), Qmargin))
4523 {
4524 prop = XCDR (prop);
4525 if (!CONSP (prop))
4526 return 0;
4527
4528 prop = XCDR (prop);
4529 if (!CONSP (prop))
4530 return 0;
4531 }
4532
4533 return CONSP (prop) && EQ (XCAR (prop), string);
4534 }
4535
4536
4537 /* Return 1 if STRING appears in the `display' property PROP. */
4538
4539 static int
4540 display_prop_string_p (prop, string)
4541 Lisp_Object prop, string;
4542 {
4543 if (CONSP (prop)
4544 && CONSP (XCAR (prop))
4545 && !EQ (Qmargin, XCAR (XCAR (prop))))
4546 {
4547 /* A list of sub-properties. */
4548 while (CONSP (prop))
4549 {
4550 if (single_display_spec_string_p (XCAR (prop), string))
4551 return 1;
4552 prop = XCDR (prop);
4553 }
4554 }
4555 else if (VECTORP (prop))
4556 {
4557 /* A vector of sub-properties. */
4558 int i;
4559 for (i = 0; i < ASIZE (prop); ++i)
4560 if (single_display_spec_string_p (AREF (prop, i), string))
4561 return 1;
4562 }
4563 else
4564 return single_display_spec_string_p (prop, string);
4565
4566 return 0;
4567 }
4568
4569
4570 /* Determine from which buffer position in W's buffer STRING comes
4571 from. AROUND_CHARPOS is an approximate position where it could
4572 be from. Value is the buffer position or 0 if it couldn't be
4573 determined.
4574
4575 W's buffer must be current.
4576
4577 This function is necessary because we don't record buffer positions
4578 in glyphs generated from strings (to keep struct glyph small).
4579 This function may only use code that doesn't eval because it is
4580 called asynchronously from note_mouse_highlight. */
4581
4582 int
4583 string_buffer_position (w, string, around_charpos)
4584 struct window *w;
4585 Lisp_Object string;
4586 int around_charpos;
4587 {
4588 Lisp_Object limit, prop, pos;
4589 const int MAX_DISTANCE = 1000;
4590 int found = 0;
4591
4592 pos = make_number (around_charpos);
4593 limit = make_number (min (XINT (pos) + MAX_DISTANCE, ZV));
4594 while (!found && !EQ (pos, limit))
4595 {
4596 prop = Fget_char_property (pos, Qdisplay, Qnil);
4597 if (!NILP (prop) && display_prop_string_p (prop, string))
4598 found = 1;
4599 else
4600 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil, limit);
4601 }
4602
4603 if (!found)
4604 {
4605 pos = make_number (around_charpos);
4606 limit = make_number (max (XINT (pos) - MAX_DISTANCE, BEGV));
4607 while (!found && !EQ (pos, limit))
4608 {
4609 prop = Fget_char_property (pos, Qdisplay, Qnil);
4610 if (!NILP (prop) && display_prop_string_p (prop, string))
4611 found = 1;
4612 else
4613 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
4614 limit);
4615 }
4616 }
4617
4618 return found ? XINT (pos) : 0;
4619 }
4620
4621
4622 \f
4623 /***********************************************************************
4624 `composition' property
4625 ***********************************************************************/
4626
4627 static enum prop_handled
4628 handle_auto_composed_prop (it)
4629 struct it *it;
4630 {
4631 enum prop_handled handled = HANDLED_NORMALLY;
4632
4633 if (FUNCTIONP (Vauto_composition_function))
4634 {
4635 Lisp_Object val = Qnil;
4636 EMACS_INT pos, limit = -1;
4637
4638 if (STRINGP (it->string))
4639 pos = IT_STRING_CHARPOS (*it);
4640 else
4641 pos = IT_CHARPOS (*it);
4642
4643 val = Fget_text_property (make_number (pos), Qauto_composed, it->string);
4644 if (! NILP (val))
4645 {
4646 Lisp_Object cmp_prop;
4647 EMACS_INT cmp_start, cmp_end;
4648
4649 if (get_property_and_range (pos, Qcomposition, &cmp_prop,
4650 &cmp_start, &cmp_end, it->string)
4651 && cmp_start == pos
4652 && COMPOSITION_METHOD (cmp_prop) == COMPOSITION_WITH_GLYPH_STRING)
4653 {
4654 Lisp_Object gstring = COMPOSITION_COMPONENTS (cmp_prop);
4655 Lisp_Object font_object = LGSTRING_FONT (gstring);
4656
4657 if (! EQ (font_object,
4658 font_at (-1, pos, FACE_FROM_ID (it->f, it->face_id),
4659 it->w, it->string)))
4660 /* We must re-compute the composition for the
4661 different font. */
4662 val = Qnil;
4663 }
4664
4665 if (! NILP (val))
4666 {
4667 Lisp_Object end;
4668
4669 /* As Fnext_single_char_property_change is very slow, we
4670 limit the search to the current line. */
4671 if (STRINGP (it->string))
4672 limit = SCHARS (it->string);
4673 else
4674 limit = find_next_newline_no_quit (pos, 1);
4675 end = Fnext_single_char_property_change (make_number (pos),
4676 Qauto_composed,
4677 it->string,
4678 make_number (limit));
4679
4680 if (XINT (end) < limit)
4681 /* The current point is auto-composed, but there exist
4682 characters not yet composed beyond the
4683 auto-composed region. There's a possiblity that
4684 the last characters in the region may be newly
4685 composed. */
4686 val = Qnil;
4687 }
4688 }
4689 if (NILP (val) && ! STRINGP (it->string))
4690 {
4691 if (limit < 0)
4692 limit = (STRINGP (it->string) ? SCHARS (it->string)
4693 : find_next_newline_no_quit (pos, 1));
4694 if (pos < limit)
4695 {
4696 int count = SPECPDL_INDEX ();
4697 Lisp_Object args[5];
4698
4699 if (FRAME_WINDOW_P (it->f))
4700 limit = font_range (pos, limit,
4701 FACE_FROM_ID (it->f, it->face_id),
4702 it->f, it->string);
4703 args[0] = Vauto_composition_function;
4704 specbind (Qauto_composition_function, Qnil);
4705 args[1] = make_number (pos);
4706 args[2] = make_number (limit);
4707 args[3] = it->window;
4708 args[4] = it->string;
4709 safe_call (5, args);
4710 unbind_to (count, Qnil);
4711 }
4712 }
4713 }
4714
4715 return handled;
4716 }
4717
4718 /* Set up iterator IT from `composition' property at its current
4719 position. Called from handle_stop. */
4720
4721 static enum prop_handled
4722 handle_composition_prop (it)
4723 struct it *it;
4724 {
4725 Lisp_Object prop, string;
4726 EMACS_INT pos, pos_byte, start, end;
4727 enum prop_handled handled = HANDLED_NORMALLY;
4728
4729 if (STRINGP (it->string))
4730 {
4731 unsigned char *s;
4732
4733 pos = IT_STRING_CHARPOS (*it);
4734 pos_byte = IT_STRING_BYTEPOS (*it);
4735 string = it->string;
4736 s = SDATA (string) + pos_byte;
4737 it->c = STRING_CHAR (s, 0);
4738 }
4739 else
4740 {
4741 pos = IT_CHARPOS (*it);
4742 pos_byte = IT_BYTEPOS (*it);
4743 string = Qnil;
4744 it->c = FETCH_CHAR (pos_byte);
4745 }
4746
4747 /* If there's a valid composition and point is not inside of the
4748 composition (in the case that the composition is from the current
4749 buffer), draw a glyph composed from the composition components. */
4750 if (find_composition (pos, -1, &start, &end, &prop, string)
4751 && COMPOSITION_VALID_P (start, end, prop)
4752 && (STRINGP (it->string) || (PT <= start || PT >= end)))
4753 {
4754 int id;
4755
4756 if (start != pos)
4757 {
4758 if (STRINGP (it->string))
4759 pos_byte = string_char_to_byte (it->string, start);
4760 else
4761 pos_byte = CHAR_TO_BYTE (start);
4762 }
4763 id = get_composition_id (start, pos_byte, end - start, prop, string);
4764
4765 if (id >= 0)
4766 {
4767 struct composition *cmp = composition_table[id];
4768
4769 if (cmp->glyph_len == 0)
4770 {
4771 /* No glyph. */
4772 if (STRINGP (it->string))
4773 {
4774 IT_STRING_CHARPOS (*it) = end;
4775 IT_STRING_BYTEPOS (*it) = string_char_to_byte (it->string,
4776 end);
4777 }
4778 else
4779 {
4780 IT_CHARPOS (*it) = end;
4781 IT_BYTEPOS (*it) = CHAR_TO_BYTE (end);
4782 }
4783 return HANDLED_RECOMPUTE_PROPS;
4784 }
4785
4786 it->stop_charpos = end;
4787 push_it (it);
4788
4789 it->method = GET_FROM_COMPOSITION;
4790 it->cmp_id = id;
4791 it->cmp_len = COMPOSITION_LENGTH (prop);
4792 /* For a terminal, draw only the first (non-TAB) character
4793 of the components. */
4794 if (composition_table[id]->method == COMPOSITION_WITH_GLYPH_STRING)
4795 {
4796 /* FIXME: This doesn't do anything!?! */
4797 Lisp_Object lgstring = AREF (XHASH_TABLE (composition_hash_table)
4798 ->key_and_value,
4799 cmp->hash_index * 2);
4800 }
4801 else
4802 {
4803 int i;
4804
4805 for (i = 0; i < cmp->glyph_len; i++)
4806 if ((it->c = COMPOSITION_GLYPH (composition_table[id], i))
4807 != '\t')
4808 break;
4809 }
4810 if (it->c == '\t')
4811 it->c = ' ';
4812 it->len = (STRINGP (it->string)
4813 ? string_char_to_byte (it->string, end)
4814 : CHAR_TO_BYTE (end)) - pos_byte;
4815 handled = HANDLED_RETURN;
4816 }
4817 }
4818
4819 return handled;
4820 }
4821
4822
4823 \f
4824 /***********************************************************************
4825 Overlay strings
4826 ***********************************************************************/
4827
4828 /* The following structure is used to record overlay strings for
4829 later sorting in load_overlay_strings. */
4830
4831 struct overlay_entry
4832 {
4833 Lisp_Object overlay;
4834 Lisp_Object string;
4835 int priority;
4836 int after_string_p;
4837 };
4838
4839
4840 /* Set up iterator IT from overlay strings at its current position.
4841 Called from handle_stop. */
4842
4843 static enum prop_handled
4844 handle_overlay_change (it)
4845 struct it *it;
4846 {
4847 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
4848 return HANDLED_RECOMPUTE_PROPS;
4849 else
4850 return HANDLED_NORMALLY;
4851 }
4852
4853
4854 /* Set up the next overlay string for delivery by IT, if there is an
4855 overlay string to deliver. Called by set_iterator_to_next when the
4856 end of the current overlay string is reached. If there are more
4857 overlay strings to display, IT->string and
4858 IT->current.overlay_string_index are set appropriately here.
4859 Otherwise IT->string is set to nil. */
4860
4861 static void
4862 next_overlay_string (it)
4863 struct it *it;
4864 {
4865 ++it->current.overlay_string_index;
4866 if (it->current.overlay_string_index == it->n_overlay_strings)
4867 {
4868 /* No more overlay strings. Restore IT's settings to what
4869 they were before overlay strings were processed, and
4870 continue to deliver from current_buffer. */
4871
4872 it->ellipsis_p = (it->stack[it->sp - 1].display_ellipsis_p != 0);
4873 pop_it (it);
4874 xassert (it->sp > 0
4875 || it->method == GET_FROM_COMPOSITION
4876 || (NILP (it->string)
4877 && it->method == GET_FROM_BUFFER
4878 && it->stop_charpos >= BEGV
4879 && it->stop_charpos <= it->end_charpos));
4880 it->current.overlay_string_index = -1;
4881 it->n_overlay_strings = 0;
4882
4883 /* If we're at the end of the buffer, record that we have
4884 processed the overlay strings there already, so that
4885 next_element_from_buffer doesn't try it again. */
4886 if (NILP (it->string) && IT_CHARPOS (*it) >= it->end_charpos)
4887 it->overlay_strings_at_end_processed_p = 1;
4888 }
4889 else
4890 {
4891 /* There are more overlay strings to process. If
4892 IT->current.overlay_string_index has advanced to a position
4893 where we must load IT->overlay_strings with more strings, do
4894 it. */
4895 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
4896
4897 if (it->current.overlay_string_index && i == 0)
4898 load_overlay_strings (it, 0);
4899
4900 /* Initialize IT to deliver display elements from the overlay
4901 string. */
4902 it->string = it->overlay_strings[i];
4903 it->multibyte_p = STRING_MULTIBYTE (it->string);
4904 SET_TEXT_POS (it->current.string_pos, 0, 0);
4905 it->method = GET_FROM_STRING;
4906 it->stop_charpos = 0;
4907 }
4908
4909 CHECK_IT (it);
4910 }
4911
4912
4913 /* Compare two overlay_entry structures E1 and E2. Used as a
4914 comparison function for qsort in load_overlay_strings. Overlay
4915 strings for the same position are sorted so that
4916
4917 1. All after-strings come in front of before-strings, except
4918 when they come from the same overlay.
4919
4920 2. Within after-strings, strings are sorted so that overlay strings
4921 from overlays with higher priorities come first.
4922
4923 2. Within before-strings, strings are sorted so that overlay
4924 strings from overlays with higher priorities come last.
4925
4926 Value is analogous to strcmp. */
4927
4928
4929 static int
4930 compare_overlay_entries (e1, e2)
4931 void *e1, *e2;
4932 {
4933 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
4934 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
4935 int result;
4936
4937 if (entry1->after_string_p != entry2->after_string_p)
4938 {
4939 /* Let after-strings appear in front of before-strings if
4940 they come from different overlays. */
4941 if (EQ (entry1->overlay, entry2->overlay))
4942 result = entry1->after_string_p ? 1 : -1;
4943 else
4944 result = entry1->after_string_p ? -1 : 1;
4945 }
4946 else if (entry1->after_string_p)
4947 /* After-strings sorted in order of decreasing priority. */
4948 result = entry2->priority - entry1->priority;
4949 else
4950 /* Before-strings sorted in order of increasing priority. */
4951 result = entry1->priority - entry2->priority;
4952
4953 return result;
4954 }
4955
4956
4957 /* Load the vector IT->overlay_strings with overlay strings from IT's
4958 current buffer position, or from CHARPOS if that is > 0. Set
4959 IT->n_overlays to the total number of overlay strings found.
4960
4961 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
4962 a time. On entry into load_overlay_strings,
4963 IT->current.overlay_string_index gives the number of overlay
4964 strings that have already been loaded by previous calls to this
4965 function.
4966
4967 IT->add_overlay_start contains an additional overlay start
4968 position to consider for taking overlay strings from, if non-zero.
4969 This position comes into play when the overlay has an `invisible'
4970 property, and both before and after-strings. When we've skipped to
4971 the end of the overlay, because of its `invisible' property, we
4972 nevertheless want its before-string to appear.
4973 IT->add_overlay_start will contain the overlay start position
4974 in this case.
4975
4976 Overlay strings are sorted so that after-string strings come in
4977 front of before-string strings. Within before and after-strings,
4978 strings are sorted by overlay priority. See also function
4979 compare_overlay_entries. */
4980
4981 static void
4982 load_overlay_strings (it, charpos)
4983 struct it *it;
4984 int charpos;
4985 {
4986 extern Lisp_Object Qafter_string, Qbefore_string, Qwindow, Qpriority;
4987 Lisp_Object overlay, window, str, invisible;
4988 struct Lisp_Overlay *ov;
4989 int start, end;
4990 int size = 20;
4991 int n = 0, i, j, invis_p;
4992 struct overlay_entry *entries
4993 = (struct overlay_entry *) alloca (size * sizeof *entries);
4994
4995 if (charpos <= 0)
4996 charpos = IT_CHARPOS (*it);
4997
4998 /* Append the overlay string STRING of overlay OVERLAY to vector
4999 `entries' which has size `size' and currently contains `n'
5000 elements. AFTER_P non-zero means STRING is an after-string of
5001 OVERLAY. */
5002 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
5003 do \
5004 { \
5005 Lisp_Object priority; \
5006 \
5007 if (n == size) \
5008 { \
5009 int new_size = 2 * size; \
5010 struct overlay_entry *old = entries; \
5011 entries = \
5012 (struct overlay_entry *) alloca (new_size \
5013 * sizeof *entries); \
5014 bcopy (old, entries, size * sizeof *entries); \
5015 size = new_size; \
5016 } \
5017 \
5018 entries[n].string = (STRING); \
5019 entries[n].overlay = (OVERLAY); \
5020 priority = Foverlay_get ((OVERLAY), Qpriority); \
5021 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
5022 entries[n].after_string_p = (AFTER_P); \
5023 ++n; \
5024 } \
5025 while (0)
5026
5027 /* Process overlay before the overlay center. */
5028 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
5029 {
5030 XSETMISC (overlay, ov);
5031 xassert (OVERLAYP (overlay));
5032 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5033 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5034
5035 if (end < charpos)
5036 break;
5037
5038 /* Skip this overlay if it doesn't start or end at IT's current
5039 position. */
5040 if (end != charpos && start != charpos)
5041 continue;
5042
5043 /* Skip this overlay if it doesn't apply to IT->w. */
5044 window = Foverlay_get (overlay, Qwindow);
5045 if (WINDOWP (window) && XWINDOW (window) != it->w)
5046 continue;
5047
5048 /* If the text ``under'' the overlay is invisible, both before-
5049 and after-strings from this overlay are visible; start and
5050 end position are indistinguishable. */
5051 invisible = Foverlay_get (overlay, Qinvisible);
5052 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5053
5054 /* If overlay has a non-empty before-string, record it. */
5055 if ((start == charpos || (end == charpos && invis_p))
5056 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5057 && SCHARS (str))
5058 RECORD_OVERLAY_STRING (overlay, str, 0);
5059
5060 /* If overlay has a non-empty after-string, record it. */
5061 if ((end == charpos || (start == charpos && invis_p))
5062 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5063 && SCHARS (str))
5064 RECORD_OVERLAY_STRING (overlay, str, 1);
5065 }
5066
5067 /* Process overlays after the overlay center. */
5068 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
5069 {
5070 XSETMISC (overlay, ov);
5071 xassert (OVERLAYP (overlay));
5072 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5073 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5074
5075 if (start > charpos)
5076 break;
5077
5078 /* Skip this overlay if it doesn't start or end at IT's current
5079 position. */
5080 if (end != charpos && start != charpos)
5081 continue;
5082
5083 /* Skip this overlay if it doesn't apply to IT->w. */
5084 window = Foverlay_get (overlay, Qwindow);
5085 if (WINDOWP (window) && XWINDOW (window) != it->w)
5086 continue;
5087
5088 /* If the text ``under'' the overlay is invisible, it has a zero
5089 dimension, and both before- and after-strings apply. */
5090 invisible = Foverlay_get (overlay, Qinvisible);
5091 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5092
5093 /* If overlay has a non-empty before-string, record it. */
5094 if ((start == charpos || (end == charpos && invis_p))
5095 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5096 && SCHARS (str))
5097 RECORD_OVERLAY_STRING (overlay, str, 0);
5098
5099 /* If overlay has a non-empty after-string, record it. */
5100 if ((end == charpos || (start == charpos && invis_p))
5101 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5102 && SCHARS (str))
5103 RECORD_OVERLAY_STRING (overlay, str, 1);
5104 }
5105
5106 #undef RECORD_OVERLAY_STRING
5107
5108 /* Sort entries. */
5109 if (n > 1)
5110 qsort (entries, n, sizeof *entries, compare_overlay_entries);
5111
5112 /* Record the total number of strings to process. */
5113 it->n_overlay_strings = n;
5114
5115 /* IT->current.overlay_string_index is the number of overlay strings
5116 that have already been consumed by IT. Copy some of the
5117 remaining overlay strings to IT->overlay_strings. */
5118 i = 0;
5119 j = it->current.overlay_string_index;
5120 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
5121 {
5122 it->overlay_strings[i] = entries[j].string;
5123 it->string_overlays[i++] = entries[j++].overlay;
5124 }
5125
5126 CHECK_IT (it);
5127 }
5128
5129
5130 /* Get the first chunk of overlay strings at IT's current buffer
5131 position, or at CHARPOS if that is > 0. Value is non-zero if at
5132 least one overlay string was found. */
5133
5134 static int
5135 get_overlay_strings_1 (it, charpos, compute_stop_p)
5136 struct it *it;
5137 int charpos;
5138 int compute_stop_p;
5139 {
5140 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
5141 process. This fills IT->overlay_strings with strings, and sets
5142 IT->n_overlay_strings to the total number of strings to process.
5143 IT->pos.overlay_string_index has to be set temporarily to zero
5144 because load_overlay_strings needs this; it must be set to -1
5145 when no overlay strings are found because a zero value would
5146 indicate a position in the first overlay string. */
5147 it->current.overlay_string_index = 0;
5148 load_overlay_strings (it, charpos);
5149
5150 /* If we found overlay strings, set up IT to deliver display
5151 elements from the first one. Otherwise set up IT to deliver
5152 from current_buffer. */
5153 if (it->n_overlay_strings)
5154 {
5155 /* Make sure we know settings in current_buffer, so that we can
5156 restore meaningful values when we're done with the overlay
5157 strings. */
5158 if (compute_stop_p)
5159 compute_stop_pos (it);
5160 xassert (it->face_id >= 0);
5161
5162 /* Save IT's settings. They are restored after all overlay
5163 strings have been processed. */
5164 xassert (!compute_stop_p || it->sp == 0);
5165 push_it (it);
5166
5167 /* Set up IT to deliver display elements from the first overlay
5168 string. */
5169 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5170 it->string = it->overlay_strings[0];
5171 it->from_overlay = Qnil;
5172 it->stop_charpos = 0;
5173 xassert (STRINGP (it->string));
5174 it->end_charpos = SCHARS (it->string);
5175 it->multibyte_p = STRING_MULTIBYTE (it->string);
5176 it->method = GET_FROM_STRING;
5177 return 1;
5178 }
5179
5180 it->current.overlay_string_index = -1;
5181 return 0;
5182 }
5183
5184 static int
5185 get_overlay_strings (it, charpos)
5186 struct it *it;
5187 int charpos;
5188 {
5189 it->string = Qnil;
5190 it->method = GET_FROM_BUFFER;
5191
5192 (void) get_overlay_strings_1 (it, charpos, 1);
5193
5194 CHECK_IT (it);
5195
5196 /* Value is non-zero if we found at least one overlay string. */
5197 return STRINGP (it->string);
5198 }
5199
5200
5201 \f
5202 /***********************************************************************
5203 Saving and restoring state
5204 ***********************************************************************/
5205
5206 /* Save current settings of IT on IT->stack. Called, for example,
5207 before setting up IT for an overlay string, to be able to restore
5208 IT's settings to what they were after the overlay string has been
5209 processed. */
5210
5211 static void
5212 push_it (it)
5213 struct it *it;
5214 {
5215 struct iterator_stack_entry *p;
5216
5217 xassert (it->sp < IT_STACK_SIZE);
5218 p = it->stack + it->sp;
5219
5220 p->stop_charpos = it->stop_charpos;
5221 xassert (it->face_id >= 0);
5222 p->face_id = it->face_id;
5223 p->string = it->string;
5224 p->method = it->method;
5225 p->from_overlay = it->from_overlay;
5226 switch (p->method)
5227 {
5228 case GET_FROM_IMAGE:
5229 p->u.image.object = it->object;
5230 p->u.image.image_id = it->image_id;
5231 p->u.image.slice = it->slice;
5232 break;
5233 case GET_FROM_COMPOSITION:
5234 p->u.comp.object = it->object;
5235 p->u.comp.c = it->c;
5236 p->u.comp.len = it->len;
5237 p->u.comp.cmp_id = it->cmp_id;
5238 p->u.comp.cmp_len = it->cmp_len;
5239 break;
5240 case GET_FROM_STRETCH:
5241 p->u.stretch.object = it->object;
5242 break;
5243 }
5244 p->position = it->position;
5245 p->current = it->current;
5246 p->end_charpos = it->end_charpos;
5247 p->string_nchars = it->string_nchars;
5248 p->area = it->area;
5249 p->multibyte_p = it->multibyte_p;
5250 p->avoid_cursor_p = it->avoid_cursor_p;
5251 p->space_width = it->space_width;
5252 p->font_height = it->font_height;
5253 p->voffset = it->voffset;
5254 p->string_from_display_prop_p = it->string_from_display_prop_p;
5255 p->display_ellipsis_p = 0;
5256 ++it->sp;
5257 }
5258
5259
5260 /* Restore IT's settings from IT->stack. Called, for example, when no
5261 more overlay strings must be processed, and we return to delivering
5262 display elements from a buffer, or when the end of a string from a
5263 `display' property is reached and we return to delivering display
5264 elements from an overlay string, or from a buffer. */
5265
5266 static void
5267 pop_it (it)
5268 struct it *it;
5269 {
5270 struct iterator_stack_entry *p;
5271
5272 xassert (it->sp > 0);
5273 --it->sp;
5274 p = it->stack + it->sp;
5275 it->stop_charpos = p->stop_charpos;
5276 it->face_id = p->face_id;
5277 it->current = p->current;
5278 it->position = p->position;
5279 it->string = p->string;
5280 it->from_overlay = p->from_overlay;
5281 if (NILP (it->string))
5282 SET_TEXT_POS (it->current.string_pos, -1, -1);
5283 it->method = p->method;
5284 switch (it->method)
5285 {
5286 case GET_FROM_IMAGE:
5287 it->image_id = p->u.image.image_id;
5288 it->object = p->u.image.object;
5289 it->slice = p->u.image.slice;
5290 break;
5291 case GET_FROM_COMPOSITION:
5292 it->object = p->u.comp.object;
5293 it->c = p->u.comp.c;
5294 it->len = p->u.comp.len;
5295 it->cmp_id = p->u.comp.cmp_id;
5296 it->cmp_len = p->u.comp.cmp_len;
5297 break;
5298 case GET_FROM_STRETCH:
5299 it->object = p->u.comp.object;
5300 break;
5301 case GET_FROM_BUFFER:
5302 it->object = it->w->buffer;
5303 break;
5304 case GET_FROM_STRING:
5305 it->object = it->string;
5306 break;
5307 }
5308 it->end_charpos = p->end_charpos;
5309 it->string_nchars = p->string_nchars;
5310 it->area = p->area;
5311 it->multibyte_p = p->multibyte_p;
5312 it->avoid_cursor_p = p->avoid_cursor_p;
5313 it->space_width = p->space_width;
5314 it->font_height = p->font_height;
5315 it->voffset = p->voffset;
5316 it->string_from_display_prop_p = p->string_from_display_prop_p;
5317 }
5318
5319
5320 \f
5321 /***********************************************************************
5322 Moving over lines
5323 ***********************************************************************/
5324
5325 /* Set IT's current position to the previous line start. */
5326
5327 static void
5328 back_to_previous_line_start (it)
5329 struct it *it;
5330 {
5331 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
5332 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
5333 }
5334
5335
5336 /* Move IT to the next line start.
5337
5338 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
5339 we skipped over part of the text (as opposed to moving the iterator
5340 continuously over the text). Otherwise, don't change the value
5341 of *SKIPPED_P.
5342
5343 Newlines may come from buffer text, overlay strings, or strings
5344 displayed via the `display' property. That's the reason we can't
5345 simply use find_next_newline_no_quit.
5346
5347 Note that this function may not skip over invisible text that is so
5348 because of text properties and immediately follows a newline. If
5349 it would, function reseat_at_next_visible_line_start, when called
5350 from set_iterator_to_next, would effectively make invisible
5351 characters following a newline part of the wrong glyph row, which
5352 leads to wrong cursor motion. */
5353
5354 static int
5355 forward_to_next_line_start (it, skipped_p)
5356 struct it *it;
5357 int *skipped_p;
5358 {
5359 int old_selective, newline_found_p, n;
5360 const int MAX_NEWLINE_DISTANCE = 500;
5361
5362 /* If already on a newline, just consume it to avoid unintended
5363 skipping over invisible text below. */
5364 if (it->what == IT_CHARACTER
5365 && it->c == '\n'
5366 && CHARPOS (it->position) == IT_CHARPOS (*it))
5367 {
5368 set_iterator_to_next (it, 0);
5369 it->c = 0;
5370 return 1;
5371 }
5372
5373 /* Don't handle selective display in the following. It's (a)
5374 unnecessary because it's done by the caller, and (b) leads to an
5375 infinite recursion because next_element_from_ellipsis indirectly
5376 calls this function. */
5377 old_selective = it->selective;
5378 it->selective = 0;
5379
5380 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
5381 from buffer text. */
5382 for (n = newline_found_p = 0;
5383 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
5384 n += STRINGP (it->string) ? 0 : 1)
5385 {
5386 if (!get_next_display_element (it))
5387 return 0;
5388 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
5389 set_iterator_to_next (it, 0);
5390 }
5391
5392 /* If we didn't find a newline near enough, see if we can use a
5393 short-cut. */
5394 if (!newline_found_p)
5395 {
5396 int start = IT_CHARPOS (*it);
5397 int limit = find_next_newline_no_quit (start, 1);
5398 Lisp_Object pos;
5399
5400 xassert (!STRINGP (it->string));
5401
5402 /* If there isn't any `display' property in sight, and no
5403 overlays, we can just use the position of the newline in
5404 buffer text. */
5405 if (it->stop_charpos >= limit
5406 || ((pos = Fnext_single_property_change (make_number (start),
5407 Qdisplay,
5408 Qnil, make_number (limit)),
5409 NILP (pos))
5410 && next_overlay_change (start) == ZV))
5411 {
5412 IT_CHARPOS (*it) = limit;
5413 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
5414 *skipped_p = newline_found_p = 1;
5415 }
5416 else
5417 {
5418 while (get_next_display_element (it)
5419 && !newline_found_p)
5420 {
5421 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
5422 set_iterator_to_next (it, 0);
5423 }
5424 }
5425 }
5426
5427 it->selective = old_selective;
5428 return newline_found_p;
5429 }
5430
5431
5432 /* Set IT's current position to the previous visible line start. Skip
5433 invisible text that is so either due to text properties or due to
5434 selective display. Caution: this does not change IT->current_x and
5435 IT->hpos. */
5436
5437 static void
5438 back_to_previous_visible_line_start (it)
5439 struct it *it;
5440 {
5441 while (IT_CHARPOS (*it) > BEGV)
5442 {
5443 back_to_previous_line_start (it);
5444
5445 if (IT_CHARPOS (*it) <= BEGV)
5446 break;
5447
5448 /* If selective > 0, then lines indented more than that values
5449 are invisible. */
5450 if (it->selective > 0
5451 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
5452 (double) it->selective)) /* iftc */
5453 continue;
5454
5455 /* Check the newline before point for invisibility. */
5456 {
5457 Lisp_Object prop;
5458 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
5459 Qinvisible, it->window);
5460 if (TEXT_PROP_MEANS_INVISIBLE (prop))
5461 continue;
5462 }
5463
5464 if (IT_CHARPOS (*it) <= BEGV)
5465 break;
5466
5467 {
5468 struct it it2;
5469 int pos;
5470 EMACS_INT beg, end;
5471 Lisp_Object val, overlay;
5472
5473 /* If newline is part of a composition, continue from start of composition */
5474 if (find_composition (IT_CHARPOS (*it), -1, &beg, &end, &val, Qnil)
5475 && beg < IT_CHARPOS (*it))
5476 goto replaced;
5477
5478 /* If newline is replaced by a display property, find start of overlay
5479 or interval and continue search from that point. */
5480 it2 = *it;
5481 pos = --IT_CHARPOS (it2);
5482 --IT_BYTEPOS (it2);
5483 it2.sp = 0;
5484 it2.string_from_display_prop_p = 0;
5485 if (handle_display_prop (&it2) == HANDLED_RETURN
5486 && !NILP (val = get_char_property_and_overlay
5487 (make_number (pos), Qdisplay, Qnil, &overlay))
5488 && (OVERLAYP (overlay)
5489 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
5490 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
5491 goto replaced;
5492
5493 /* Newline is not replaced by anything -- so we are done. */
5494 break;
5495
5496 replaced:
5497 if (beg < BEGV)
5498 beg = BEGV;
5499 IT_CHARPOS (*it) = beg;
5500 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
5501 }
5502 }
5503
5504 it->continuation_lines_width = 0;
5505
5506 xassert (IT_CHARPOS (*it) >= BEGV);
5507 xassert (IT_CHARPOS (*it) == BEGV
5508 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
5509 CHECK_IT (it);
5510 }
5511
5512
5513 /* Reseat iterator IT at the previous visible line start. Skip
5514 invisible text that is so either due to text properties or due to
5515 selective display. At the end, update IT's overlay information,
5516 face information etc. */
5517
5518 void
5519 reseat_at_previous_visible_line_start (it)
5520 struct it *it;
5521 {
5522 back_to_previous_visible_line_start (it);
5523 reseat (it, it->current.pos, 1);
5524 CHECK_IT (it);
5525 }
5526
5527
5528 /* Reseat iterator IT on the next visible line start in the current
5529 buffer. ON_NEWLINE_P non-zero means position IT on the newline
5530 preceding the line start. Skip over invisible text that is so
5531 because of selective display. Compute faces, overlays etc at the
5532 new position. Note that this function does not skip over text that
5533 is invisible because of text properties. */
5534
5535 static void
5536 reseat_at_next_visible_line_start (it, on_newline_p)
5537 struct it *it;
5538 int on_newline_p;
5539 {
5540 int newline_found_p, skipped_p = 0;
5541
5542 newline_found_p = forward_to_next_line_start (it, &skipped_p);
5543
5544 /* Skip over lines that are invisible because they are indented
5545 more than the value of IT->selective. */
5546 if (it->selective > 0)
5547 while (IT_CHARPOS (*it) < ZV
5548 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
5549 (double) it->selective)) /* iftc */
5550 {
5551 xassert (IT_BYTEPOS (*it) == BEGV
5552 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
5553 newline_found_p = forward_to_next_line_start (it, &skipped_p);
5554 }
5555
5556 /* Position on the newline if that's what's requested. */
5557 if (on_newline_p && newline_found_p)
5558 {
5559 if (STRINGP (it->string))
5560 {
5561 if (IT_STRING_CHARPOS (*it) > 0)
5562 {
5563 --IT_STRING_CHARPOS (*it);
5564 --IT_STRING_BYTEPOS (*it);
5565 }
5566 }
5567 else if (IT_CHARPOS (*it) > BEGV)
5568 {
5569 --IT_CHARPOS (*it);
5570 --IT_BYTEPOS (*it);
5571 reseat (it, it->current.pos, 0);
5572 }
5573 }
5574 else if (skipped_p)
5575 reseat (it, it->current.pos, 0);
5576
5577 CHECK_IT (it);
5578 }
5579
5580
5581 \f
5582 /***********************************************************************
5583 Changing an iterator's position
5584 ***********************************************************************/
5585
5586 /* Change IT's current position to POS in current_buffer. If FORCE_P
5587 is non-zero, always check for text properties at the new position.
5588 Otherwise, text properties are only looked up if POS >=
5589 IT->check_charpos of a property. */
5590
5591 static void
5592 reseat (it, pos, force_p)
5593 struct it *it;
5594 struct text_pos pos;
5595 int force_p;
5596 {
5597 int original_pos = IT_CHARPOS (*it);
5598
5599 reseat_1 (it, pos, 0);
5600
5601 /* Determine where to check text properties. Avoid doing it
5602 where possible because text property lookup is very expensive. */
5603 if (force_p
5604 || CHARPOS (pos) > it->stop_charpos
5605 || CHARPOS (pos) < original_pos)
5606 handle_stop (it);
5607
5608 CHECK_IT (it);
5609 }
5610
5611
5612 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
5613 IT->stop_pos to POS, also. */
5614
5615 static void
5616 reseat_1 (it, pos, set_stop_p)
5617 struct it *it;
5618 struct text_pos pos;
5619 int set_stop_p;
5620 {
5621 /* Don't call this function when scanning a C string. */
5622 xassert (it->s == NULL);
5623
5624 /* POS must be a reasonable value. */
5625 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
5626
5627 it->current.pos = it->position = pos;
5628 it->end_charpos = ZV;
5629 it->dpvec = NULL;
5630 it->current.dpvec_index = -1;
5631 it->current.overlay_string_index = -1;
5632 IT_STRING_CHARPOS (*it) = -1;
5633 IT_STRING_BYTEPOS (*it) = -1;
5634 it->string = Qnil;
5635 it->string_from_display_prop_p = 0;
5636 it->method = GET_FROM_BUFFER;
5637 it->object = it->w->buffer;
5638 it->area = TEXT_AREA;
5639 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
5640 it->sp = 0;
5641 it->string_from_display_prop_p = 0;
5642 it->face_before_selective_p = 0;
5643
5644 if (set_stop_p)
5645 it->stop_charpos = CHARPOS (pos);
5646 }
5647
5648
5649 /* Set up IT for displaying a string, starting at CHARPOS in window W.
5650 If S is non-null, it is a C string to iterate over. Otherwise,
5651 STRING gives a Lisp string to iterate over.
5652
5653 If PRECISION > 0, don't return more then PRECISION number of
5654 characters from the string.
5655
5656 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
5657 characters have been returned. FIELD_WIDTH < 0 means an infinite
5658 field width.
5659
5660 MULTIBYTE = 0 means disable processing of multibyte characters,
5661 MULTIBYTE > 0 means enable it,
5662 MULTIBYTE < 0 means use IT->multibyte_p.
5663
5664 IT must be initialized via a prior call to init_iterator before
5665 calling this function. */
5666
5667 static void
5668 reseat_to_string (it, s, string, charpos, precision, field_width, multibyte)
5669 struct it *it;
5670 unsigned char *s;
5671 Lisp_Object string;
5672 int charpos;
5673 int precision, field_width, multibyte;
5674 {
5675 /* No region in strings. */
5676 it->region_beg_charpos = it->region_end_charpos = -1;
5677
5678 /* No text property checks performed by default, but see below. */
5679 it->stop_charpos = -1;
5680
5681 /* Set iterator position and end position. */
5682 bzero (&it->current, sizeof it->current);
5683 it->current.overlay_string_index = -1;
5684 it->current.dpvec_index = -1;
5685 xassert (charpos >= 0);
5686
5687 /* If STRING is specified, use its multibyteness, otherwise use the
5688 setting of MULTIBYTE, if specified. */
5689 if (multibyte >= 0)
5690 it->multibyte_p = multibyte > 0;
5691
5692 if (s == NULL)
5693 {
5694 xassert (STRINGP (string));
5695 it->string = string;
5696 it->s = NULL;
5697 it->end_charpos = it->string_nchars = SCHARS (string);
5698 it->method = GET_FROM_STRING;
5699 it->current.string_pos = string_pos (charpos, string);
5700 }
5701 else
5702 {
5703 it->s = s;
5704 it->string = Qnil;
5705
5706 /* Note that we use IT->current.pos, not it->current.string_pos,
5707 for displaying C strings. */
5708 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
5709 if (it->multibyte_p)
5710 {
5711 it->current.pos = c_string_pos (charpos, s, 1);
5712 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
5713 }
5714 else
5715 {
5716 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
5717 it->end_charpos = it->string_nchars = strlen (s);
5718 }
5719
5720 it->method = GET_FROM_C_STRING;
5721 }
5722
5723 /* PRECISION > 0 means don't return more than PRECISION characters
5724 from the string. */
5725 if (precision > 0 && it->end_charpos - charpos > precision)
5726 it->end_charpos = it->string_nchars = charpos + precision;
5727
5728 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
5729 characters have been returned. FIELD_WIDTH == 0 means don't pad,
5730 FIELD_WIDTH < 0 means infinite field width. This is useful for
5731 padding with `-' at the end of a mode line. */
5732 if (field_width < 0)
5733 field_width = INFINITY;
5734 if (field_width > it->end_charpos - charpos)
5735 it->end_charpos = charpos + field_width;
5736
5737 /* Use the standard display table for displaying strings. */
5738 if (DISP_TABLE_P (Vstandard_display_table))
5739 it->dp = XCHAR_TABLE (Vstandard_display_table);
5740
5741 it->stop_charpos = charpos;
5742 CHECK_IT (it);
5743 }
5744
5745
5746 \f
5747 /***********************************************************************
5748 Iteration
5749 ***********************************************************************/
5750
5751 /* Map enum it_method value to corresponding next_element_from_* function. */
5752
5753 static int (* get_next_element[NUM_IT_METHODS]) P_ ((struct it *it)) =
5754 {
5755 next_element_from_buffer,
5756 next_element_from_display_vector,
5757 next_element_from_composition,
5758 next_element_from_string,
5759 next_element_from_c_string,
5760 next_element_from_image,
5761 next_element_from_stretch
5762 };
5763
5764 #define GET_NEXT_DISPLAY_ELEMENT(it) (*get_next_element[(it)->method]) (it)
5765
5766 /* Load IT's display element fields with information about the next
5767 display element from the current position of IT. Value is zero if
5768 end of buffer (or C string) is reached. */
5769
5770 static struct frame *last_escape_glyph_frame = NULL;
5771 static unsigned last_escape_glyph_face_id = (1 << FACE_ID_BITS);
5772 static int last_escape_glyph_merged_face_id = 0;
5773
5774 int
5775 get_next_display_element (it)
5776 struct it *it;
5777 {
5778 /* Non-zero means that we found a display element. Zero means that
5779 we hit the end of what we iterate over. Performance note: the
5780 function pointer `method' used here turns out to be faster than
5781 using a sequence of if-statements. */
5782 int success_p;
5783
5784 get_next:
5785 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
5786
5787 if (it->what == IT_CHARACTER)
5788 {
5789 /* Map via display table or translate control characters.
5790 IT->c, IT->len etc. have been set to the next character by
5791 the function call above. If we have a display table, and it
5792 contains an entry for IT->c, translate it. Don't do this if
5793 IT->c itself comes from a display table, otherwise we could
5794 end up in an infinite recursion. (An alternative could be to
5795 count the recursion depth of this function and signal an
5796 error when a certain maximum depth is reached.) Is it worth
5797 it? */
5798 if (success_p && it->dpvec == NULL)
5799 {
5800 Lisp_Object dv;
5801
5802 if (it->dp
5803 && (dv = DISP_CHAR_VECTOR (it->dp, it->c),
5804 VECTORP (dv)))
5805 {
5806 struct Lisp_Vector *v = XVECTOR (dv);
5807
5808 /* Return the first character from the display table
5809 entry, if not empty. If empty, don't display the
5810 current character. */
5811 if (v->size)
5812 {
5813 it->dpvec_char_len = it->len;
5814 it->dpvec = v->contents;
5815 it->dpend = v->contents + v->size;
5816 it->current.dpvec_index = 0;
5817 it->dpvec_face_id = -1;
5818 it->saved_face_id = it->face_id;
5819 it->method = GET_FROM_DISPLAY_VECTOR;
5820 it->ellipsis_p = 0;
5821 }
5822 else
5823 {
5824 set_iterator_to_next (it, 0);
5825 }
5826 goto get_next;
5827 }
5828
5829 /* Translate control characters into `\003' or `^C' form.
5830 Control characters coming from a display table entry are
5831 currently not translated because we use IT->dpvec to hold
5832 the translation. This could easily be changed but I
5833 don't believe that it is worth doing.
5834
5835 If it->multibyte_p is nonzero, non-printable non-ASCII
5836 characters are also translated to octal form.
5837
5838 If it->multibyte_p is zero, eight-bit characters that
5839 don't have corresponding multibyte char code are also
5840 translated to octal form. */
5841 else if ((it->c < ' '
5842 ? (it->area != TEXT_AREA
5843 /* In mode line, treat \n, \t like other crl chars. */
5844 || (it->c != '\t'
5845 && it->glyph_row && it->glyph_row->mode_line_p)
5846 || (it->c != '\n' && it->c != '\t'))
5847 : (it->multibyte_p
5848 ? (!CHAR_PRINTABLE_P (it->c)
5849 || (!NILP (Vnobreak_char_display)
5850 && (it->c == 0xA0 /* NO-BREAK SPACE */
5851 || it->c == 0xAD /* SOFT HYPHEN */)))
5852 : (it->c >= 127
5853 && (! unibyte_display_via_language_environment
5854 || (UNIBYTE_CHAR_HAS_MULTIBYTE_P (it->c)))))))
5855 {
5856 /* IT->c is a control character which must be displayed
5857 either as '\003' or as `^C' where the '\\' and '^'
5858 can be defined in the display table. Fill
5859 IT->ctl_chars with glyphs for what we have to
5860 display. Then, set IT->dpvec to these glyphs. */
5861 Lisp_Object gc;
5862 int ctl_len;
5863 int face_id, lface_id = 0 ;
5864 int escape_glyph;
5865
5866 /* Handle control characters with ^. */
5867
5868 if (it->c < 128 && it->ctl_arrow_p)
5869 {
5870 int g;
5871
5872 g = '^'; /* default glyph for Control */
5873 /* Set IT->ctl_chars[0] to the glyph for `^'. */
5874 if (it->dp
5875 && (gc = DISP_CTRL_GLYPH (it->dp), GLYPH_CODE_P (gc))
5876 && GLYPH_CODE_CHAR_VALID_P (gc))
5877 {
5878 g = GLYPH_CODE_CHAR (gc);
5879 lface_id = GLYPH_CODE_FACE (gc);
5880 }
5881 if (lface_id)
5882 {
5883 face_id = merge_faces (it->f, Qt, lface_id, it->face_id);
5884 }
5885 else if (it->f == last_escape_glyph_frame
5886 && it->face_id == last_escape_glyph_face_id)
5887 {
5888 face_id = last_escape_glyph_merged_face_id;
5889 }
5890 else
5891 {
5892 /* Merge the escape-glyph face into the current face. */
5893 face_id = merge_faces (it->f, Qescape_glyph, 0,
5894 it->face_id);
5895 last_escape_glyph_frame = it->f;
5896 last_escape_glyph_face_id = it->face_id;
5897 last_escape_glyph_merged_face_id = face_id;
5898 }
5899
5900 XSETINT (it->ctl_chars[0], g);
5901 XSETINT (it->ctl_chars[1], it->c ^ 0100);
5902 ctl_len = 2;
5903 goto display_control;
5904 }
5905
5906 /* Handle non-break space in the mode where it only gets
5907 highlighting. */
5908
5909 if (EQ (Vnobreak_char_display, Qt)
5910 && it->c == 0xA0)
5911 {
5912 /* Merge the no-break-space face into the current face. */
5913 face_id = merge_faces (it->f, Qnobreak_space, 0,
5914 it->face_id);
5915
5916 it->c = ' ';
5917 XSETINT (it->ctl_chars[0], ' ');
5918 ctl_len = 1;
5919 goto display_control;
5920 }
5921
5922 /* Handle sequences that start with the "escape glyph". */
5923
5924 /* the default escape glyph is \. */
5925 escape_glyph = '\\';
5926
5927 if (it->dp
5928 && (gc = DISP_ESCAPE_GLYPH (it->dp), GLYPH_CODE_P (gc))
5929 && GLYPH_CODE_CHAR_VALID_P (gc))
5930 {
5931 escape_glyph = GLYPH_CODE_CHAR (gc);
5932 lface_id = GLYPH_CODE_FACE (gc);
5933 }
5934 if (lface_id)
5935 {
5936 /* The display table specified a face.
5937 Merge it into face_id and also into escape_glyph. */
5938 face_id = merge_faces (it->f, Qt, lface_id,
5939 it->face_id);
5940 }
5941 else if (it->f == last_escape_glyph_frame
5942 && it->face_id == last_escape_glyph_face_id)
5943 {
5944 face_id = last_escape_glyph_merged_face_id;
5945 }
5946 else
5947 {
5948 /* Merge the escape-glyph face into the current face. */
5949 face_id = merge_faces (it->f, Qescape_glyph, 0,
5950 it->face_id);
5951 last_escape_glyph_frame = it->f;
5952 last_escape_glyph_face_id = it->face_id;
5953 last_escape_glyph_merged_face_id = face_id;
5954 }
5955
5956 /* Handle soft hyphens in the mode where they only get
5957 highlighting. */
5958
5959 if (EQ (Vnobreak_char_display, Qt)
5960 && it->c == 0xAD)
5961 {
5962 it->c = '-';
5963 XSETINT (it->ctl_chars[0], '-');
5964 ctl_len = 1;
5965 goto display_control;
5966 }
5967
5968 /* Handle non-break space and soft hyphen
5969 with the escape glyph. */
5970
5971 if (it->c == 0xA0 || it->c == 0xAD)
5972 {
5973 XSETINT (it->ctl_chars[0], escape_glyph);
5974 it->c = (it->c == 0xA0 ? ' ' : '-');
5975 XSETINT (it->ctl_chars[1], it->c);
5976 ctl_len = 2;
5977 goto display_control;
5978 }
5979
5980 {
5981 unsigned char str[MAX_MULTIBYTE_LENGTH];
5982 int len;
5983 int i;
5984
5985 /* Set IT->ctl_chars[0] to the glyph for `\\'. */
5986 if (CHAR_BYTE8_P (it->c))
5987 {
5988 str[0] = CHAR_TO_BYTE8 (it->c);
5989 len = 1;
5990 }
5991 else if (it->c < 256)
5992 {
5993 str[0] = it->c;
5994 len = 1;
5995 }
5996 else
5997 {
5998 /* It's an invalid character, which shouldn't
5999 happen actually, but due to bugs it may
6000 happen. Let's print the char as is, there's
6001 not much meaningful we can do with it. */
6002 str[0] = it->c;
6003 str[1] = it->c >> 8;
6004 str[2] = it->c >> 16;
6005 str[3] = it->c >> 24;
6006 len = 4;
6007 }
6008
6009 for (i = 0; i < len; i++)
6010 {
6011 int g;
6012 XSETINT (it->ctl_chars[i * 4], escape_glyph);
6013 /* Insert three more glyphs into IT->ctl_chars for
6014 the octal display of the character. */
6015 g = ((str[i] >> 6) & 7) + '0';
6016 XSETINT (it->ctl_chars[i * 4 + 1], g);
6017 g = ((str[i] >> 3) & 7) + '0';
6018 XSETINT (it->ctl_chars[i * 4 + 2], g);
6019 g = (str[i] & 7) + '0';
6020 XSETINT (it->ctl_chars[i * 4 + 3], g);
6021 }
6022 ctl_len = len * 4;
6023 }
6024
6025 display_control:
6026 /* Set up IT->dpvec and return first character from it. */
6027 it->dpvec_char_len = it->len;
6028 it->dpvec = it->ctl_chars;
6029 it->dpend = it->dpvec + ctl_len;
6030 it->current.dpvec_index = 0;
6031 it->dpvec_face_id = face_id;
6032 it->saved_face_id = it->face_id;
6033 it->method = GET_FROM_DISPLAY_VECTOR;
6034 it->ellipsis_p = 0;
6035 goto get_next;
6036 }
6037 }
6038 }
6039
6040 /* Adjust face id for a multibyte character. There are no multibyte
6041 character in unibyte text. */
6042 if ((it->what == IT_CHARACTER || it->what == IT_COMPOSITION)
6043 && it->multibyte_p
6044 && success_p
6045 && FRAME_WINDOW_P (it->f))
6046 {
6047 struct face *face = FACE_FROM_ID (it->f, it->face_id);
6048 int pos = (it->s ? -1
6049 : STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
6050 : IT_CHARPOS (*it));
6051
6052 it->face_id = FACE_FOR_CHAR (it->f, face, it->c, pos, it->string);
6053 }
6054
6055 /* Is this character the last one of a run of characters with
6056 box? If yes, set IT->end_of_box_run_p to 1. */
6057 if (it->face_box_p
6058 && it->s == NULL)
6059 {
6060 int face_id;
6061 struct face *face;
6062
6063 it->end_of_box_run_p
6064 = ((face_id = face_after_it_pos (it),
6065 face_id != it->face_id)
6066 && (face = FACE_FROM_ID (it->f, face_id),
6067 face->box == FACE_NO_BOX));
6068 }
6069
6070 /* Value is 0 if end of buffer or string reached. */
6071 return success_p;
6072 }
6073
6074
6075 /* Move IT to the next display element.
6076
6077 RESEAT_P non-zero means if called on a newline in buffer text,
6078 skip to the next visible line start.
6079
6080 Functions get_next_display_element and set_iterator_to_next are
6081 separate because I find this arrangement easier to handle than a
6082 get_next_display_element function that also increments IT's
6083 position. The way it is we can first look at an iterator's current
6084 display element, decide whether it fits on a line, and if it does,
6085 increment the iterator position. The other way around we probably
6086 would either need a flag indicating whether the iterator has to be
6087 incremented the next time, or we would have to implement a
6088 decrement position function which would not be easy to write. */
6089
6090 void
6091 set_iterator_to_next (it, reseat_p)
6092 struct it *it;
6093 int reseat_p;
6094 {
6095 /* Reset flags indicating start and end of a sequence of characters
6096 with box. Reset them at the start of this function because
6097 moving the iterator to a new position might set them. */
6098 it->start_of_box_run_p = it->end_of_box_run_p = 0;
6099
6100 switch (it->method)
6101 {
6102 case GET_FROM_BUFFER:
6103 /* The current display element of IT is a character from
6104 current_buffer. Advance in the buffer, and maybe skip over
6105 invisible lines that are so because of selective display. */
6106 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
6107 reseat_at_next_visible_line_start (it, 0);
6108 else
6109 {
6110 xassert (it->len != 0);
6111 IT_BYTEPOS (*it) += it->len;
6112 IT_CHARPOS (*it) += 1;
6113 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
6114 }
6115 break;
6116
6117 case GET_FROM_COMPOSITION:
6118 xassert (it->cmp_id >= 0 && it->cmp_id < n_compositions);
6119 xassert (it->sp > 0);
6120 pop_it (it);
6121 if (it->method == GET_FROM_STRING)
6122 {
6123 IT_STRING_BYTEPOS (*it) += it->len;
6124 IT_STRING_CHARPOS (*it) += it->cmp_len;
6125 goto consider_string_end;
6126 }
6127 else if (it->method == GET_FROM_BUFFER)
6128 {
6129 IT_BYTEPOS (*it) += it->len;
6130 IT_CHARPOS (*it) += it->cmp_len;
6131 }
6132 break;
6133
6134 case GET_FROM_C_STRING:
6135 /* Current display element of IT is from a C string. */
6136 IT_BYTEPOS (*it) += it->len;
6137 IT_CHARPOS (*it) += 1;
6138 break;
6139
6140 case GET_FROM_DISPLAY_VECTOR:
6141 /* Current display element of IT is from a display table entry.
6142 Advance in the display table definition. Reset it to null if
6143 end reached, and continue with characters from buffers/
6144 strings. */
6145 ++it->current.dpvec_index;
6146
6147 /* Restore face of the iterator to what they were before the
6148 display vector entry (these entries may contain faces). */
6149 it->face_id = it->saved_face_id;
6150
6151 if (it->dpvec + it->current.dpvec_index == it->dpend)
6152 {
6153 int recheck_faces = it->ellipsis_p;
6154
6155 if (it->s)
6156 it->method = GET_FROM_C_STRING;
6157 else if (STRINGP (it->string))
6158 it->method = GET_FROM_STRING;
6159 else
6160 {
6161 it->method = GET_FROM_BUFFER;
6162 it->object = it->w->buffer;
6163 }
6164
6165 it->dpvec = NULL;
6166 it->current.dpvec_index = -1;
6167
6168 /* Skip over characters which were displayed via IT->dpvec. */
6169 if (it->dpvec_char_len < 0)
6170 reseat_at_next_visible_line_start (it, 1);
6171 else if (it->dpvec_char_len > 0)
6172 {
6173 if (it->method == GET_FROM_STRING
6174 && it->n_overlay_strings > 0)
6175 it->ignore_overlay_strings_at_pos_p = 1;
6176 it->len = it->dpvec_char_len;
6177 set_iterator_to_next (it, reseat_p);
6178 }
6179
6180 /* Maybe recheck faces after display vector */
6181 if (recheck_faces)
6182 it->stop_charpos = IT_CHARPOS (*it);
6183 }
6184 break;
6185
6186 case GET_FROM_STRING:
6187 /* Current display element is a character from a Lisp string. */
6188 xassert (it->s == NULL && STRINGP (it->string));
6189 IT_STRING_BYTEPOS (*it) += it->len;
6190 IT_STRING_CHARPOS (*it) += 1;
6191
6192 consider_string_end:
6193
6194 if (it->current.overlay_string_index >= 0)
6195 {
6196 /* IT->string is an overlay string. Advance to the
6197 next, if there is one. */
6198 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
6199 {
6200 it->ellipsis_p = 0;
6201 next_overlay_string (it);
6202 if (it->ellipsis_p)
6203 setup_for_ellipsis (it, 0);
6204 }
6205 }
6206 else
6207 {
6208 /* IT->string is not an overlay string. If we reached
6209 its end, and there is something on IT->stack, proceed
6210 with what is on the stack. This can be either another
6211 string, this time an overlay string, or a buffer. */
6212 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
6213 && it->sp > 0)
6214 {
6215 pop_it (it);
6216 if (it->method == GET_FROM_STRING)
6217 goto consider_string_end;
6218 }
6219 }
6220 break;
6221
6222 case GET_FROM_IMAGE:
6223 case GET_FROM_STRETCH:
6224 /* The position etc with which we have to proceed are on
6225 the stack. The position may be at the end of a string,
6226 if the `display' property takes up the whole string. */
6227 xassert (it->sp > 0);
6228 pop_it (it);
6229 if (it->method == GET_FROM_STRING)
6230 goto consider_string_end;
6231 break;
6232
6233 default:
6234 /* There are no other methods defined, so this should be a bug. */
6235 abort ();
6236 }
6237
6238 xassert (it->method != GET_FROM_STRING
6239 || (STRINGP (it->string)
6240 && IT_STRING_CHARPOS (*it) >= 0));
6241 }
6242
6243 /* Load IT's display element fields with information about the next
6244 display element which comes from a display table entry or from the
6245 result of translating a control character to one of the forms `^C'
6246 or `\003'.
6247
6248 IT->dpvec holds the glyphs to return as characters.
6249 IT->saved_face_id holds the face id before the display vector--
6250 it is restored into IT->face_idin set_iterator_to_next. */
6251
6252 static int
6253 next_element_from_display_vector (it)
6254 struct it *it;
6255 {
6256 Lisp_Object gc;
6257
6258 /* Precondition. */
6259 xassert (it->dpvec && it->current.dpvec_index >= 0);
6260
6261 it->face_id = it->saved_face_id;
6262
6263 /* KFS: This code used to check ip->dpvec[0] instead of the current element.
6264 That seemed totally bogus - so I changed it... */
6265 gc = it->dpvec[it->current.dpvec_index];
6266
6267 if (GLYPH_CODE_P (gc) && GLYPH_CODE_CHAR_VALID_P (gc))
6268 {
6269 it->c = GLYPH_CODE_CHAR (gc);
6270 it->len = CHAR_BYTES (it->c);
6271
6272 /* The entry may contain a face id to use. Such a face id is
6273 the id of a Lisp face, not a realized face. A face id of
6274 zero means no face is specified. */
6275 if (it->dpvec_face_id >= 0)
6276 it->face_id = it->dpvec_face_id;
6277 else
6278 {
6279 int lface_id = GLYPH_CODE_FACE (gc);
6280 if (lface_id > 0)
6281 it->face_id = merge_faces (it->f, Qt, lface_id,
6282 it->saved_face_id);
6283 }
6284 }
6285 else
6286 /* Display table entry is invalid. Return a space. */
6287 it->c = ' ', it->len = 1;
6288
6289 /* Don't change position and object of the iterator here. They are
6290 still the values of the character that had this display table
6291 entry or was translated, and that's what we want. */
6292 it->what = IT_CHARACTER;
6293 return 1;
6294 }
6295
6296
6297 /* Load IT with the next display element from Lisp string IT->string.
6298 IT->current.string_pos is the current position within the string.
6299 If IT->current.overlay_string_index >= 0, the Lisp string is an
6300 overlay string. */
6301
6302 static int
6303 next_element_from_string (it)
6304 struct it *it;
6305 {
6306 struct text_pos position;
6307
6308 xassert (STRINGP (it->string));
6309 xassert (IT_STRING_CHARPOS (*it) >= 0);
6310 position = it->current.string_pos;
6311
6312 /* Time to check for invisible text? */
6313 if (IT_STRING_CHARPOS (*it) < it->end_charpos
6314 && IT_STRING_CHARPOS (*it) == it->stop_charpos)
6315 {
6316 handle_stop (it);
6317
6318 /* Since a handler may have changed IT->method, we must
6319 recurse here. */
6320 return GET_NEXT_DISPLAY_ELEMENT (it);
6321 }
6322
6323 if (it->current.overlay_string_index >= 0)
6324 {
6325 /* Get the next character from an overlay string. In overlay
6326 strings, There is no field width or padding with spaces to
6327 do. */
6328 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
6329 {
6330 it->what = IT_EOB;
6331 return 0;
6332 }
6333 else if (STRING_MULTIBYTE (it->string))
6334 {
6335 int remaining = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
6336 const unsigned char *s = (SDATA (it->string)
6337 + IT_STRING_BYTEPOS (*it));
6338 it->c = string_char_and_length (s, remaining, &it->len);
6339 }
6340 else
6341 {
6342 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
6343 it->len = 1;
6344 }
6345 }
6346 else
6347 {
6348 /* Get the next character from a Lisp string that is not an
6349 overlay string. Such strings come from the mode line, for
6350 example. We may have to pad with spaces, or truncate the
6351 string. See also next_element_from_c_string. */
6352 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
6353 {
6354 it->what = IT_EOB;
6355 return 0;
6356 }
6357 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
6358 {
6359 /* Pad with spaces. */
6360 it->c = ' ', it->len = 1;
6361 CHARPOS (position) = BYTEPOS (position) = -1;
6362 }
6363 else if (STRING_MULTIBYTE (it->string))
6364 {
6365 int maxlen = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
6366 const unsigned char *s = (SDATA (it->string)
6367 + IT_STRING_BYTEPOS (*it));
6368 it->c = string_char_and_length (s, maxlen, &it->len);
6369 }
6370 else
6371 {
6372 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
6373 it->len = 1;
6374 }
6375 }
6376
6377 /* Record what we have and where it came from. */
6378 it->what = IT_CHARACTER;
6379 it->object = it->string;
6380 it->position = position;
6381 return 1;
6382 }
6383
6384
6385 /* Load IT with next display element from C string IT->s.
6386 IT->string_nchars is the maximum number of characters to return
6387 from the string. IT->end_charpos may be greater than
6388 IT->string_nchars when this function is called, in which case we
6389 may have to return padding spaces. Value is zero if end of string
6390 reached, including padding spaces. */
6391
6392 static int
6393 next_element_from_c_string (it)
6394 struct it *it;
6395 {
6396 int success_p = 1;
6397
6398 xassert (it->s);
6399 it->what = IT_CHARACTER;
6400 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
6401 it->object = Qnil;
6402
6403 /* IT's position can be greater IT->string_nchars in case a field
6404 width or precision has been specified when the iterator was
6405 initialized. */
6406 if (IT_CHARPOS (*it) >= it->end_charpos)
6407 {
6408 /* End of the game. */
6409 it->what = IT_EOB;
6410 success_p = 0;
6411 }
6412 else if (IT_CHARPOS (*it) >= it->string_nchars)
6413 {
6414 /* Pad with spaces. */
6415 it->c = ' ', it->len = 1;
6416 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
6417 }
6418 else if (it->multibyte_p)
6419 {
6420 /* Implementation note: The calls to strlen apparently aren't a
6421 performance problem because there is no noticeable performance
6422 difference between Emacs running in unibyte or multibyte mode. */
6423 int maxlen = strlen (it->s) - IT_BYTEPOS (*it);
6424 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it),
6425 maxlen, &it->len);
6426 }
6427 else
6428 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
6429
6430 return success_p;
6431 }
6432
6433
6434 /* Set up IT to return characters from an ellipsis, if appropriate.
6435 The definition of the ellipsis glyphs may come from a display table
6436 entry. This function Fills IT with the first glyph from the
6437 ellipsis if an ellipsis is to be displayed. */
6438
6439 static int
6440 next_element_from_ellipsis (it)
6441 struct it *it;
6442 {
6443 if (it->selective_display_ellipsis_p)
6444 setup_for_ellipsis (it, it->len);
6445 else
6446 {
6447 /* The face at the current position may be different from the
6448 face we find after the invisible text. Remember what it
6449 was in IT->saved_face_id, and signal that it's there by
6450 setting face_before_selective_p. */
6451 it->saved_face_id = it->face_id;
6452 it->method = GET_FROM_BUFFER;
6453 it->object = it->w->buffer;
6454 reseat_at_next_visible_line_start (it, 1);
6455 it->face_before_selective_p = 1;
6456 }
6457
6458 return GET_NEXT_DISPLAY_ELEMENT (it);
6459 }
6460
6461
6462 /* Deliver an image display element. The iterator IT is already
6463 filled with image information (done in handle_display_prop). Value
6464 is always 1. */
6465
6466
6467 static int
6468 next_element_from_image (it)
6469 struct it *it;
6470 {
6471 it->what = IT_IMAGE;
6472 return 1;
6473 }
6474
6475
6476 /* Fill iterator IT with next display element from a stretch glyph
6477 property. IT->object is the value of the text property. Value is
6478 always 1. */
6479
6480 static int
6481 next_element_from_stretch (it)
6482 struct it *it;
6483 {
6484 it->what = IT_STRETCH;
6485 return 1;
6486 }
6487
6488
6489 /* Load IT with the next display element from current_buffer. Value
6490 is zero if end of buffer reached. IT->stop_charpos is the next
6491 position at which to stop and check for text properties or buffer
6492 end. */
6493
6494 static int
6495 next_element_from_buffer (it)
6496 struct it *it;
6497 {
6498 int success_p = 1;
6499
6500 /* Check this assumption, otherwise, we would never enter the
6501 if-statement, below. */
6502 xassert (IT_CHARPOS (*it) >= BEGV
6503 && IT_CHARPOS (*it) <= it->stop_charpos);
6504
6505 if (IT_CHARPOS (*it) >= it->stop_charpos)
6506 {
6507 if (IT_CHARPOS (*it) >= it->end_charpos)
6508 {
6509 int overlay_strings_follow_p;
6510
6511 /* End of the game, except when overlay strings follow that
6512 haven't been returned yet. */
6513 if (it->overlay_strings_at_end_processed_p)
6514 overlay_strings_follow_p = 0;
6515 else
6516 {
6517 it->overlay_strings_at_end_processed_p = 1;
6518 overlay_strings_follow_p = get_overlay_strings (it, 0);
6519 }
6520
6521 if (overlay_strings_follow_p)
6522 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
6523 else
6524 {
6525 it->what = IT_EOB;
6526 it->position = it->current.pos;
6527 success_p = 0;
6528 }
6529 }
6530 else
6531 {
6532 handle_stop (it);
6533 return GET_NEXT_DISPLAY_ELEMENT (it);
6534 }
6535 }
6536 else
6537 {
6538 /* No face changes, overlays etc. in sight, so just return a
6539 character from current_buffer. */
6540 unsigned char *p;
6541
6542 /* Maybe run the redisplay end trigger hook. Performance note:
6543 This doesn't seem to cost measurable time. */
6544 if (it->redisplay_end_trigger_charpos
6545 && it->glyph_row
6546 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
6547 run_redisplay_end_trigger_hook (it);
6548
6549 /* Get the next character, maybe multibyte. */
6550 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
6551 if (it->multibyte_p && !ASCII_BYTE_P (*p))
6552 {
6553 int maxlen = ((IT_BYTEPOS (*it) >= GPT_BYTE ? ZV_BYTE : GPT_BYTE)
6554 - IT_BYTEPOS (*it));
6555 it->c = string_char_and_length (p, maxlen, &it->len);
6556 }
6557 else
6558 it->c = *p, it->len = 1;
6559
6560 /* Record what we have and where it came from. */
6561 it->what = IT_CHARACTER;
6562 it->object = it->w->buffer;
6563 it->position = it->current.pos;
6564
6565 /* Normally we return the character found above, except when we
6566 really want to return an ellipsis for selective display. */
6567 if (it->selective)
6568 {
6569 if (it->c == '\n')
6570 {
6571 /* A value of selective > 0 means hide lines indented more
6572 than that number of columns. */
6573 if (it->selective > 0
6574 && IT_CHARPOS (*it) + 1 < ZV
6575 && indented_beyond_p (IT_CHARPOS (*it) + 1,
6576 IT_BYTEPOS (*it) + 1,
6577 (double) it->selective)) /* iftc */
6578 {
6579 success_p = next_element_from_ellipsis (it);
6580 it->dpvec_char_len = -1;
6581 }
6582 }
6583 else if (it->c == '\r' && it->selective == -1)
6584 {
6585 /* A value of selective == -1 means that everything from the
6586 CR to the end of the line is invisible, with maybe an
6587 ellipsis displayed for it. */
6588 success_p = next_element_from_ellipsis (it);
6589 it->dpvec_char_len = -1;
6590 }
6591 }
6592 }
6593
6594 /* Value is zero if end of buffer reached. */
6595 xassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
6596 return success_p;
6597 }
6598
6599
6600 /* Run the redisplay end trigger hook for IT. */
6601
6602 static void
6603 run_redisplay_end_trigger_hook (it)
6604 struct it *it;
6605 {
6606 Lisp_Object args[3];
6607
6608 /* IT->glyph_row should be non-null, i.e. we should be actually
6609 displaying something, or otherwise we should not run the hook. */
6610 xassert (it->glyph_row);
6611
6612 /* Set up hook arguments. */
6613 args[0] = Qredisplay_end_trigger_functions;
6614 args[1] = it->window;
6615 XSETINT (args[2], it->redisplay_end_trigger_charpos);
6616 it->redisplay_end_trigger_charpos = 0;
6617
6618 /* Since we are *trying* to run these functions, don't try to run
6619 them again, even if they get an error. */
6620 it->w->redisplay_end_trigger = Qnil;
6621 Frun_hook_with_args (3, args);
6622
6623 /* Notice if it changed the face of the character we are on. */
6624 handle_face_prop (it);
6625 }
6626
6627
6628 /* Deliver a composition display element. The iterator IT is already
6629 filled with composition information (done in
6630 handle_composition_prop). Value is always 1. */
6631
6632 static int
6633 next_element_from_composition (it)
6634 struct it *it;
6635 {
6636 it->what = IT_COMPOSITION;
6637 it->position = (STRINGP (it->string)
6638 ? it->current.string_pos
6639 : it->current.pos);
6640 if (STRINGP (it->string))
6641 it->object = it->string;
6642 else
6643 it->object = it->w->buffer;
6644 return 1;
6645 }
6646
6647
6648 \f
6649 /***********************************************************************
6650 Moving an iterator without producing glyphs
6651 ***********************************************************************/
6652
6653 /* Check if iterator is at a position corresponding to a valid buffer
6654 position after some move_it_ call. */
6655
6656 #define IT_POS_VALID_AFTER_MOVE_P(it) \
6657 ((it)->method == GET_FROM_STRING \
6658 ? IT_STRING_CHARPOS (*it) == 0 \
6659 : 1)
6660
6661
6662 /* Move iterator IT to a specified buffer or X position within one
6663 line on the display without producing glyphs.
6664
6665 OP should be a bit mask including some or all of these bits:
6666 MOVE_TO_X: Stop on reaching x-position TO_X.
6667 MOVE_TO_POS: Stop on reaching buffer or string position TO_CHARPOS.
6668 Regardless of OP's value, stop in reaching the end of the display line.
6669
6670 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
6671 This means, in particular, that TO_X includes window's horizontal
6672 scroll amount.
6673
6674 The return value has several possible values that
6675 say what condition caused the scan to stop:
6676
6677 MOVE_POS_MATCH_OR_ZV
6678 - when TO_POS or ZV was reached.
6679
6680 MOVE_X_REACHED
6681 -when TO_X was reached before TO_POS or ZV were reached.
6682
6683 MOVE_LINE_CONTINUED
6684 - when we reached the end of the display area and the line must
6685 be continued.
6686
6687 MOVE_LINE_TRUNCATED
6688 - when we reached the end of the display area and the line is
6689 truncated.
6690
6691 MOVE_NEWLINE_OR_CR
6692 - when we stopped at a line end, i.e. a newline or a CR and selective
6693 display is on. */
6694
6695 static enum move_it_result
6696 move_it_in_display_line_to (struct it *it,
6697 EMACS_INT to_charpos, int to_x,
6698 enum move_operation_enum op)
6699 {
6700 enum move_it_result result = MOVE_UNDEFINED;
6701 struct glyph_row *saved_glyph_row;
6702 struct it wrap_it, atpos_it, atx_it;
6703 int may_wrap = 0;
6704
6705 /* Don't produce glyphs in produce_glyphs. */
6706 saved_glyph_row = it->glyph_row;
6707 it->glyph_row = NULL;
6708
6709 /* Use wrap_it to save a copy of IT wherever a word wrap could
6710 occur. Use atpos_it to save a copy of IT at the desired buffer
6711 position, if found, so that we can scan ahead and check if the
6712 word later overshoots the window edge. Use atx_it similarly, for
6713 pixel positions. */
6714 wrap_it.sp = -1;
6715 atpos_it.sp = -1;
6716 atx_it.sp = -1;
6717
6718 #define BUFFER_POS_REACHED_P() \
6719 ((op & MOVE_TO_POS) != 0 \
6720 && BUFFERP (it->object) \
6721 && IT_CHARPOS (*it) >= to_charpos \
6722 && (it->method == GET_FROM_BUFFER \
6723 || (it->method == GET_FROM_DISPLAY_VECTOR \
6724 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
6725
6726 /* If there's a line-/wrap-prefix, handle it. */
6727 if (it->hpos == 0 && it->method == GET_FROM_BUFFER
6728 && it->current_y < it->last_visible_y)
6729 handle_line_prefix (it);
6730
6731 while (1)
6732 {
6733 int x, i, ascent = 0, descent = 0;
6734
6735 /* Utility macro to reset an iterator with x, ascent, and descent. */
6736 #define IT_RESET_X_ASCENT_DESCENT(IT) \
6737 ((IT)->current_x = x, (IT)->max_ascent = ascent, \
6738 (IT)->max_descent = descent)
6739
6740 /* Stop if we move beyond TO_CHARPOS (after an image or stretch
6741 glyph). */
6742 if ((op & MOVE_TO_POS) != 0
6743 && BUFFERP (it->object)
6744 && it->method == GET_FROM_BUFFER
6745 && IT_CHARPOS (*it) > to_charpos)
6746 {
6747 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
6748 {
6749 result = MOVE_POS_MATCH_OR_ZV;
6750 break;
6751 }
6752 else if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
6753 /* If wrap_it is valid, the current position might be in a
6754 word that is wrapped. So, save the iterator in
6755 atpos_it and continue to see if wrapping happens. */
6756 atpos_it = *it;
6757 }
6758
6759 /* Stop when ZV reached.
6760 We used to stop here when TO_CHARPOS reached as well, but that is
6761 too soon if this glyph does not fit on this line. So we handle it
6762 explicitly below. */
6763 if (!get_next_display_element (it))
6764 {
6765 result = MOVE_POS_MATCH_OR_ZV;
6766 break;
6767 }
6768
6769 if (it->line_wrap == TRUNCATE)
6770 {
6771 if (BUFFER_POS_REACHED_P ())
6772 {
6773 result = MOVE_POS_MATCH_OR_ZV;
6774 break;
6775 }
6776 }
6777 else
6778 {
6779 if (it->line_wrap == WORD_WRAP)
6780 {
6781 if (IT_DISPLAYING_WHITESPACE (it))
6782 may_wrap = 1;
6783 else if (may_wrap)
6784 {
6785 /* We have reached a glyph that follows one or more
6786 whitespace characters. If the position is
6787 already found, we are done. */
6788 if (atpos_it.sp >= 0)
6789 {
6790 *it = atpos_it;
6791 result = MOVE_POS_MATCH_OR_ZV;
6792 goto done;
6793 }
6794 if (atx_it.sp >= 0)
6795 {
6796 *it = atx_it;
6797 result = MOVE_X_REACHED;
6798 goto done;
6799 }
6800 /* Otherwise, we can wrap here. */
6801 wrap_it = *it;
6802 may_wrap = 0;
6803 }
6804 }
6805 }
6806
6807 /* Remember the line height for the current line, in case
6808 the next element doesn't fit on the line. */
6809 ascent = it->max_ascent;
6810 descent = it->max_descent;
6811
6812 /* The call to produce_glyphs will get the metrics of the
6813 display element IT is loaded with. Record the x-position
6814 before this display element, in case it doesn't fit on the
6815 line. */
6816 x = it->current_x;
6817
6818 PRODUCE_GLYPHS (it);
6819
6820 if (it->area != TEXT_AREA)
6821 {
6822 set_iterator_to_next (it, 1);
6823 continue;
6824 }
6825
6826 /* The number of glyphs we get back in IT->nglyphs will normally
6827 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
6828 character on a terminal frame, or (iii) a line end. For the
6829 second case, IT->nglyphs - 1 padding glyphs will be present
6830 (on X frames, there is only one glyph produced for a
6831 composite character.
6832
6833 The behavior implemented below means, for continuation lines,
6834 that as many spaces of a TAB as fit on the current line are
6835 displayed there. For terminal frames, as many glyphs of a
6836 multi-glyph character are displayed in the current line, too.
6837 This is what the old redisplay code did, and we keep it that
6838 way. Under X, the whole shape of a complex character must
6839 fit on the line or it will be completely displayed in the
6840 next line.
6841
6842 Note that both for tabs and padding glyphs, all glyphs have
6843 the same width. */
6844 if (it->nglyphs)
6845 {
6846 /* More than one glyph or glyph doesn't fit on line. All
6847 glyphs have the same width. */
6848 int single_glyph_width = it->pixel_width / it->nglyphs;
6849 int new_x;
6850 int x_before_this_char = x;
6851 int hpos_before_this_char = it->hpos;
6852
6853 for (i = 0; i < it->nglyphs; ++i, x = new_x)
6854 {
6855 new_x = x + single_glyph_width;
6856
6857 /* We want to leave anything reaching TO_X to the caller. */
6858 if ((op & MOVE_TO_X) && new_x > to_x)
6859 {
6860 if (BUFFER_POS_REACHED_P ())
6861 {
6862 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
6863 goto buffer_pos_reached;
6864 if (atpos_it.sp < 0)
6865 {
6866 atpos_it = *it;
6867 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
6868 }
6869 }
6870 else
6871 {
6872 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
6873 {
6874 it->current_x = x;
6875 result = MOVE_X_REACHED;
6876 break;
6877 }
6878 if (atx_it.sp < 0)
6879 {
6880 atx_it = *it;
6881 IT_RESET_X_ASCENT_DESCENT (&atx_it);
6882 }
6883 }
6884 }
6885
6886 if (/* Lines are continued. */
6887 it->line_wrap != TRUNCATE
6888 && (/* And glyph doesn't fit on the line. */
6889 new_x > it->last_visible_x
6890 /* Or it fits exactly and we're on a window
6891 system frame. */
6892 || (new_x == it->last_visible_x
6893 && FRAME_WINDOW_P (it->f))))
6894 {
6895 if (/* IT->hpos == 0 means the very first glyph
6896 doesn't fit on the line, e.g. a wide image. */
6897 it->hpos == 0
6898 || (new_x == it->last_visible_x
6899 && FRAME_WINDOW_P (it->f)))
6900 {
6901 ++it->hpos;
6902 it->current_x = new_x;
6903
6904 /* The character's last glyph just barely fits
6905 in this row. */
6906 if (i == it->nglyphs - 1)
6907 {
6908 /* If this is the destination position,
6909 return a position *before* it in this row,
6910 now that we know it fits in this row. */
6911 if (BUFFER_POS_REACHED_P ())
6912 {
6913 if (it->line_wrap != WORD_WRAP
6914 || wrap_it.sp < 0)
6915 {
6916 it->hpos = hpos_before_this_char;
6917 it->current_x = x_before_this_char;
6918 result = MOVE_POS_MATCH_OR_ZV;
6919 break;
6920 }
6921 if (it->line_wrap == WORD_WRAP
6922 && atpos_it.sp < 0)
6923 {
6924 atpos_it = *it;
6925 atpos_it.current_x = x_before_this_char;
6926 atpos_it.hpos = hpos_before_this_char;
6927 }
6928 }
6929
6930 set_iterator_to_next (it, 1);
6931 #ifdef HAVE_WINDOW_SYSTEM
6932 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
6933 {
6934 if (!get_next_display_element (it))
6935 {
6936 result = MOVE_POS_MATCH_OR_ZV;
6937 break;
6938 }
6939 if (BUFFER_POS_REACHED_P ())
6940 {
6941 if (ITERATOR_AT_END_OF_LINE_P (it))
6942 result = MOVE_POS_MATCH_OR_ZV;
6943 else
6944 result = MOVE_LINE_CONTINUED;
6945 break;
6946 }
6947 if (ITERATOR_AT_END_OF_LINE_P (it))
6948 {
6949 result = MOVE_NEWLINE_OR_CR;
6950 break;
6951 }
6952 }
6953 #endif /* HAVE_WINDOW_SYSTEM */
6954 }
6955 }
6956 else
6957 IT_RESET_X_ASCENT_DESCENT (it);
6958
6959 if (wrap_it.sp >= 0)
6960 {
6961 *it = wrap_it;
6962 atpos_it.sp = -1;
6963 atx_it.sp = -1;
6964 }
6965
6966 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
6967 IT_CHARPOS (*it)));
6968 result = MOVE_LINE_CONTINUED;
6969 break;
6970 }
6971
6972 if (BUFFER_POS_REACHED_P ())
6973 {
6974 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
6975 goto buffer_pos_reached;
6976 if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
6977 {
6978 atpos_it = *it;
6979 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
6980 }
6981 }
6982
6983 if (new_x > it->first_visible_x)
6984 {
6985 /* Glyph is visible. Increment number of glyphs that
6986 would be displayed. */
6987 ++it->hpos;
6988 }
6989 }
6990
6991 if (result != MOVE_UNDEFINED)
6992 break;
6993 }
6994 else if (BUFFER_POS_REACHED_P ())
6995 {
6996 buffer_pos_reached:
6997 IT_RESET_X_ASCENT_DESCENT (it);
6998 result = MOVE_POS_MATCH_OR_ZV;
6999 break;
7000 }
7001 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
7002 {
7003 /* Stop when TO_X specified and reached. This check is
7004 necessary here because of lines consisting of a line end,
7005 only. The line end will not produce any glyphs and we
7006 would never get MOVE_X_REACHED. */
7007 xassert (it->nglyphs == 0);
7008 result = MOVE_X_REACHED;
7009 break;
7010 }
7011
7012 /* Is this a line end? If yes, we're done. */
7013 if (ITERATOR_AT_END_OF_LINE_P (it))
7014 {
7015 result = MOVE_NEWLINE_OR_CR;
7016 break;
7017 }
7018
7019 /* The current display element has been consumed. Advance
7020 to the next. */
7021 set_iterator_to_next (it, 1);
7022
7023 /* Stop if lines are truncated and IT's current x-position is
7024 past the right edge of the window now. */
7025 if (it->line_wrap == TRUNCATE
7026 && it->current_x >= it->last_visible_x)
7027 {
7028 #ifdef HAVE_WINDOW_SYSTEM
7029 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
7030 {
7031 if (!get_next_display_element (it)
7032 || BUFFER_POS_REACHED_P ())
7033 {
7034 result = MOVE_POS_MATCH_OR_ZV;
7035 break;
7036 }
7037 if (ITERATOR_AT_END_OF_LINE_P (it))
7038 {
7039 result = MOVE_NEWLINE_OR_CR;
7040 break;
7041 }
7042 }
7043 #endif /* HAVE_WINDOW_SYSTEM */
7044 result = MOVE_LINE_TRUNCATED;
7045 break;
7046 }
7047 #undef IT_RESET_X_ASCENT_DESCENT
7048 }
7049
7050 #undef BUFFER_POS_REACHED_P
7051
7052 /* If we scanned beyond to_pos and didn't find a point to wrap at,
7053 restore the saved iterator. */
7054 if (atpos_it.sp >= 0)
7055 *it = atpos_it;
7056 else if (atx_it.sp >= 0)
7057 *it = atx_it;
7058
7059 done:
7060
7061 /* Restore the iterator settings altered at the beginning of this
7062 function. */
7063 it->glyph_row = saved_glyph_row;
7064 return result;
7065 }
7066
7067 /* For external use. */
7068 void
7069 move_it_in_display_line (struct it *it,
7070 EMACS_INT to_charpos, int to_x,
7071 enum move_operation_enum op)
7072 {
7073 if (it->line_wrap == WORD_WRAP
7074 && (op & MOVE_TO_X))
7075 {
7076 struct it save_it = *it;
7077 int skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
7078 /* When word-wrap is on, TO_X may lie past the end
7079 of a wrapped line. Then it->current is the
7080 character on the next line, so backtrack to the
7081 space before the wrap point. */
7082 if (skip == MOVE_LINE_CONTINUED)
7083 {
7084 int prev_x = max (it->current_x - 1, 0);
7085 *it = save_it;
7086 move_it_in_display_line_to
7087 (it, -1, prev_x, MOVE_TO_X);
7088 }
7089 }
7090 else
7091 move_it_in_display_line_to (it, to_charpos, to_x, op);
7092 }
7093
7094
7095 /* Move IT forward until it satisfies one or more of the criteria in
7096 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
7097
7098 OP is a bit-mask that specifies where to stop, and in particular,
7099 which of those four position arguments makes a difference. See the
7100 description of enum move_operation_enum.
7101
7102 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
7103 screen line, this function will set IT to the next position >
7104 TO_CHARPOS. */
7105
7106 void
7107 move_it_to (it, to_charpos, to_x, to_y, to_vpos, op)
7108 struct it *it;
7109 int to_charpos, to_x, to_y, to_vpos;
7110 int op;
7111 {
7112 enum move_it_result skip, skip2 = MOVE_X_REACHED;
7113 int line_height;
7114 int reached = 0;
7115
7116 for (;;)
7117 {
7118 if (op & MOVE_TO_VPOS)
7119 {
7120 /* If no TO_CHARPOS and no TO_X specified, stop at the
7121 start of the line TO_VPOS. */
7122 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
7123 {
7124 if (it->vpos == to_vpos)
7125 {
7126 reached = 1;
7127 break;
7128 }
7129 else
7130 skip = move_it_in_display_line_to (it, -1, -1, 0);
7131 }
7132 else
7133 {
7134 /* TO_VPOS >= 0 means stop at TO_X in the line at
7135 TO_VPOS, or at TO_POS, whichever comes first. */
7136 if (it->vpos == to_vpos)
7137 {
7138 reached = 2;
7139 break;
7140 }
7141
7142 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
7143
7144 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
7145 {
7146 reached = 3;
7147 break;
7148 }
7149 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
7150 {
7151 /* We have reached TO_X but not in the line we want. */
7152 skip = move_it_in_display_line_to (it, to_charpos,
7153 -1, MOVE_TO_POS);
7154 if (skip == MOVE_POS_MATCH_OR_ZV)
7155 {
7156 reached = 4;
7157 break;
7158 }
7159 }
7160 }
7161 }
7162 else if (op & MOVE_TO_Y)
7163 {
7164 struct it it_backup;
7165
7166 if (it->line_wrap == WORD_WRAP)
7167 it_backup = *it;
7168
7169 /* TO_Y specified means stop at TO_X in the line containing
7170 TO_Y---or at TO_CHARPOS if this is reached first. The
7171 problem is that we can't really tell whether the line
7172 contains TO_Y before we have completely scanned it, and
7173 this may skip past TO_X. What we do is to first scan to
7174 TO_X.
7175
7176 If TO_X is not specified, use a TO_X of zero. The reason
7177 is to make the outcome of this function more predictable.
7178 If we didn't use TO_X == 0, we would stop at the end of
7179 the line which is probably not what a caller would expect
7180 to happen. */
7181 skip = move_it_in_display_line_to
7182 (it, to_charpos, ((op & MOVE_TO_X) ? to_x : 0),
7183 (MOVE_TO_X | (op & MOVE_TO_POS)));
7184
7185 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
7186 if (skip == MOVE_POS_MATCH_OR_ZV)
7187 reached = 5;
7188 else if (skip == MOVE_X_REACHED)
7189 {
7190 /* If TO_X was reached, we want to know whether TO_Y is
7191 in the line. We know this is the case if the already
7192 scanned glyphs make the line tall enough. Otherwise,
7193 we must check by scanning the rest of the line. */
7194 line_height = it->max_ascent + it->max_descent;
7195 if (to_y >= it->current_y
7196 && to_y < it->current_y + line_height)
7197 {
7198 reached = 6;
7199 break;
7200 }
7201 it_backup = *it;
7202 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
7203 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
7204 op & MOVE_TO_POS);
7205 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
7206 line_height = it->max_ascent + it->max_descent;
7207 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
7208
7209 if (to_y >= it->current_y
7210 && to_y < it->current_y + line_height)
7211 {
7212 /* If TO_Y is in this line and TO_X was reached
7213 above, we scanned too far. We have to restore
7214 IT's settings to the ones before skipping. */
7215 *it = it_backup;
7216 reached = 6;
7217 }
7218 else
7219 {
7220 skip = skip2;
7221 if (skip == MOVE_POS_MATCH_OR_ZV)
7222 reached = 7;
7223 }
7224 }
7225 else
7226 {
7227 /* Check whether TO_Y is in this line. */
7228 line_height = it->max_ascent + it->max_descent;
7229 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
7230
7231 if (to_y >= it->current_y
7232 && to_y < it->current_y + line_height)
7233 {
7234 /* When word-wrap is on, TO_X may lie past the end
7235 of a wrapped line. Then it->current is the
7236 character on the next line, so backtrack to the
7237 space before the wrap point. */
7238 if (skip == MOVE_LINE_CONTINUED
7239 && it->line_wrap == WORD_WRAP)
7240 {
7241 int prev_x = max (it->current_x - 1, 0);
7242 *it = it_backup;
7243 skip = move_it_in_display_line_to
7244 (it, -1, prev_x, MOVE_TO_X);
7245 }
7246 reached = 6;
7247 }
7248 }
7249
7250 if (reached)
7251 break;
7252 }
7253 else if (BUFFERP (it->object)
7254 && it->method == GET_FROM_BUFFER
7255 && IT_CHARPOS (*it) >= to_charpos)
7256 skip = MOVE_POS_MATCH_OR_ZV;
7257 else
7258 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
7259
7260 switch (skip)
7261 {
7262 case MOVE_POS_MATCH_OR_ZV:
7263 reached = 8;
7264 goto out;
7265
7266 case MOVE_NEWLINE_OR_CR:
7267 set_iterator_to_next (it, 1);
7268 it->continuation_lines_width = 0;
7269 break;
7270
7271 case MOVE_LINE_TRUNCATED:
7272 it->continuation_lines_width = 0;
7273 reseat_at_next_visible_line_start (it, 0);
7274 if ((op & MOVE_TO_POS) != 0
7275 && IT_CHARPOS (*it) > to_charpos)
7276 {
7277 reached = 9;
7278 goto out;
7279 }
7280 break;
7281
7282 case MOVE_LINE_CONTINUED:
7283 /* For continued lines ending in a tab, some of the glyphs
7284 associated with the tab are displayed on the current
7285 line. Since it->current_x does not include these glyphs,
7286 we use it->last_visible_x instead. */
7287 it->continuation_lines_width +=
7288 (it->c == '\t') ? it->last_visible_x : 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 = it->hpos = 0;
7298 it->current_y += it->max_ascent + it->max_descent;
7299 ++it->vpos;
7300 last_height = it->max_ascent + it->max_descent;
7301 last_max_ascent = it->max_ascent;
7302 it->max_ascent = it->max_descent = 0;
7303 }
7304
7305 out:
7306
7307 /* On text terminals, we may stop at the end of a line in the middle
7308 of a multi-character glyph. If the glyph itself is continued,
7309 i.e. it is actually displayed on the next line, don't treat this
7310 stopping point as valid; move to the next line instead (unless
7311 that brings us offscreen). */
7312 if (!FRAME_WINDOW_P (it->f)
7313 && op & MOVE_TO_POS
7314 && IT_CHARPOS (*it) == to_charpos
7315 && it->what == IT_CHARACTER
7316 && it->nglyphs > 1
7317 && it->line_wrap == WINDOW_WRAP
7318 && it->current_x == it->last_visible_x - 1
7319 && it->c != '\n'
7320 && it->c != '\t'
7321 && it->vpos < XFASTINT (it->w->window_end_vpos))
7322 {
7323 it->continuation_lines_width += it->current_x;
7324 it->current_x = it->hpos = it->max_ascent = it->max_descent = 0;
7325 it->current_y += it->max_ascent + it->max_descent;
7326 ++it->vpos;
7327 last_height = it->max_ascent + it->max_descent;
7328 last_max_ascent = it->max_ascent;
7329 }
7330
7331 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
7332 }
7333
7334
7335 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
7336
7337 If DY > 0, move IT backward at least that many pixels. DY = 0
7338 means move IT backward to the preceding line start or BEGV. This
7339 function may move over more than DY pixels if IT->current_y - DY
7340 ends up in the middle of a line; in this case IT->current_y will be
7341 set to the top of the line moved to. */
7342
7343 void
7344 move_it_vertically_backward (it, dy)
7345 struct it *it;
7346 int dy;
7347 {
7348 int nlines, h;
7349 struct it it2, it3;
7350 int start_pos;
7351
7352 move_further_back:
7353 xassert (dy >= 0);
7354
7355 start_pos = IT_CHARPOS (*it);
7356
7357 /* Estimate how many newlines we must move back. */
7358 nlines = max (1, dy / FRAME_LINE_HEIGHT (it->f));
7359
7360 /* Set the iterator's position that many lines back. */
7361 while (nlines-- && IT_CHARPOS (*it) > BEGV)
7362 back_to_previous_visible_line_start (it);
7363
7364 /* Reseat the iterator here. When moving backward, we don't want
7365 reseat to skip forward over invisible text, set up the iterator
7366 to deliver from overlay strings at the new position etc. So,
7367 use reseat_1 here. */
7368 reseat_1 (it, it->current.pos, 1);
7369
7370 /* We are now surely at a line start. */
7371 it->current_x = it->hpos = 0;
7372 it->continuation_lines_width = 0;
7373
7374 /* Move forward and see what y-distance we moved. First move to the
7375 start of the next line so that we get its height. We need this
7376 height to be able to tell whether we reached the specified
7377 y-distance. */
7378 it2 = *it;
7379 it2.max_ascent = it2.max_descent = 0;
7380 do
7381 {
7382 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
7383 MOVE_TO_POS | MOVE_TO_VPOS);
7384 }
7385 while (!IT_POS_VALID_AFTER_MOVE_P (&it2));
7386 xassert (IT_CHARPOS (*it) >= BEGV);
7387 it3 = it2;
7388
7389 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
7390 xassert (IT_CHARPOS (*it) >= BEGV);
7391 /* H is the actual vertical distance from the position in *IT
7392 and the starting position. */
7393 h = it2.current_y - it->current_y;
7394 /* NLINES is the distance in number of lines. */
7395 nlines = it2.vpos - it->vpos;
7396
7397 /* Correct IT's y and vpos position
7398 so that they are relative to the starting point. */
7399 it->vpos -= nlines;
7400 it->current_y -= h;
7401
7402 if (dy == 0)
7403 {
7404 /* DY == 0 means move to the start of the screen line. The
7405 value of nlines is > 0 if continuation lines were involved. */
7406 if (nlines > 0)
7407 move_it_by_lines (it, nlines, 1);
7408 #if 0
7409 /* I think this assert is bogus if buffer contains
7410 invisible text or images. KFS. */
7411 xassert (IT_CHARPOS (*it) <= start_pos);
7412 #endif
7413 }
7414 else
7415 {
7416 /* The y-position we try to reach, relative to *IT.
7417 Note that H has been subtracted in front of the if-statement. */
7418 int target_y = it->current_y + h - dy;
7419 int y0 = it3.current_y;
7420 int y1 = line_bottom_y (&it3);
7421 int line_height = y1 - y0;
7422
7423 /* If we did not reach target_y, try to move further backward if
7424 we can. If we moved too far backward, try to move forward. */
7425 if (target_y < it->current_y
7426 /* This is heuristic. In a window that's 3 lines high, with
7427 a line height of 13 pixels each, recentering with point
7428 on the bottom line will try to move -39/2 = 19 pixels
7429 backward. Try to avoid moving into the first line. */
7430 && (it->current_y - target_y
7431 > min (window_box_height (it->w), line_height * 2 / 3))
7432 && IT_CHARPOS (*it) > BEGV)
7433 {
7434 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
7435 target_y - it->current_y));
7436 dy = it->current_y - target_y;
7437 goto move_further_back;
7438 }
7439 else if (target_y >= it->current_y + line_height
7440 && IT_CHARPOS (*it) < ZV)
7441 {
7442 /* Should move forward by at least one line, maybe more.
7443
7444 Note: Calling move_it_by_lines can be expensive on
7445 terminal frames, where compute_motion is used (via
7446 vmotion) to do the job, when there are very long lines
7447 and truncate-lines is nil. That's the reason for
7448 treating terminal frames specially here. */
7449
7450 if (!FRAME_WINDOW_P (it->f))
7451 move_it_vertically (it, target_y - (it->current_y + line_height));
7452 else
7453 {
7454 do
7455 {
7456 move_it_by_lines (it, 1, 1);
7457 }
7458 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
7459 }
7460
7461 #if 0
7462 /* I think this assert is bogus if buffer contains
7463 invisible text or images. KFS. */
7464 xassert (IT_CHARPOS (*it) >= BEGV);
7465 #endif
7466 }
7467 }
7468 }
7469
7470
7471 /* Move IT by a specified amount of pixel lines DY. DY negative means
7472 move backwards. DY = 0 means move to start of screen line. At the
7473 end, IT will be on the start of a screen line. */
7474
7475 void
7476 move_it_vertically (it, dy)
7477 struct it *it;
7478 int dy;
7479 {
7480 if (dy <= 0)
7481 move_it_vertically_backward (it, -dy);
7482 else
7483 {
7484 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
7485 move_it_to (it, ZV, -1, it->current_y + dy, -1,
7486 MOVE_TO_POS | MOVE_TO_Y);
7487 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
7488
7489 /* If buffer ends in ZV without a newline, move to the start of
7490 the line to satisfy the post-condition. */
7491 if (IT_CHARPOS (*it) == ZV
7492 && ZV > BEGV
7493 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
7494 move_it_by_lines (it, 0, 0);
7495 }
7496 }
7497
7498
7499 /* Move iterator IT past the end of the text line it is in. */
7500
7501 void
7502 move_it_past_eol (it)
7503 struct it *it;
7504 {
7505 enum move_it_result rc;
7506
7507 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
7508 if (rc == MOVE_NEWLINE_OR_CR)
7509 set_iterator_to_next (it, 0);
7510 }
7511
7512
7513 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
7514 negative means move up. DVPOS == 0 means move to the start of the
7515 screen line. NEED_Y_P non-zero means calculate IT->current_y. If
7516 NEED_Y_P is zero, IT->current_y will be left unchanged.
7517
7518 Further optimization ideas: If we would know that IT->f doesn't use
7519 a face with proportional font, we could be faster for
7520 truncate-lines nil. */
7521
7522 void
7523 move_it_by_lines (it, dvpos, need_y_p)
7524 struct it *it;
7525 int dvpos, need_y_p;
7526 {
7527 struct position pos;
7528
7529 /* The commented-out optimization uses vmotion on terminals. This
7530 gives bad results, because elements like it->what, on which
7531 callers such as pos_visible_p rely, aren't updated. */
7532 /* if (!FRAME_WINDOW_P (it->f))
7533 {
7534 struct text_pos textpos;
7535
7536 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
7537 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
7538 reseat (it, textpos, 1);
7539 it->vpos += pos.vpos;
7540 it->current_y += pos.vpos;
7541 }
7542 else */
7543
7544 if (dvpos == 0)
7545 {
7546 /* DVPOS == 0 means move to the start of the screen line. */
7547 move_it_vertically_backward (it, 0);
7548 xassert (it->current_x == 0 && it->hpos == 0);
7549 /* Let next call to line_bottom_y calculate real line height */
7550 last_height = 0;
7551 }
7552 else if (dvpos > 0)
7553 {
7554 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
7555 if (!IT_POS_VALID_AFTER_MOVE_P (it))
7556 move_it_to (it, IT_CHARPOS (*it) + 1, -1, -1, -1, MOVE_TO_POS);
7557 }
7558 else
7559 {
7560 struct it it2;
7561 int start_charpos, i;
7562
7563 /* Start at the beginning of the screen line containing IT's
7564 position. This may actually move vertically backwards,
7565 in case of overlays, so adjust dvpos accordingly. */
7566 dvpos += it->vpos;
7567 move_it_vertically_backward (it, 0);
7568 dvpos -= it->vpos;
7569
7570 /* Go back -DVPOS visible lines and reseat the iterator there. */
7571 start_charpos = IT_CHARPOS (*it);
7572 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > BEGV; --i)
7573 back_to_previous_visible_line_start (it);
7574 reseat (it, it->current.pos, 1);
7575
7576 /* Move further back if we end up in a string or an image. */
7577 while (!IT_POS_VALID_AFTER_MOVE_P (it))
7578 {
7579 /* First try to move to start of display line. */
7580 dvpos += it->vpos;
7581 move_it_vertically_backward (it, 0);
7582 dvpos -= it->vpos;
7583 if (IT_POS_VALID_AFTER_MOVE_P (it))
7584 break;
7585 /* If start of line is still in string or image,
7586 move further back. */
7587 back_to_previous_visible_line_start (it);
7588 reseat (it, it->current.pos, 1);
7589 dvpos--;
7590 }
7591
7592 it->current_x = it->hpos = 0;
7593
7594 /* Above call may have moved too far if continuation lines
7595 are involved. Scan forward and see if it did. */
7596 it2 = *it;
7597 it2.vpos = it2.current_y = 0;
7598 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
7599 it->vpos -= it2.vpos;
7600 it->current_y -= it2.current_y;
7601 it->current_x = it->hpos = 0;
7602
7603 /* If we moved too far back, move IT some lines forward. */
7604 if (it2.vpos > -dvpos)
7605 {
7606 int delta = it2.vpos + dvpos;
7607 it2 = *it;
7608 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
7609 /* Move back again if we got too far ahead. */
7610 if (IT_CHARPOS (*it) >= start_charpos)
7611 *it = it2;
7612 }
7613 }
7614 }
7615
7616 /* Return 1 if IT points into the middle of a display vector. */
7617
7618 int
7619 in_display_vector_p (it)
7620 struct it *it;
7621 {
7622 return (it->method == GET_FROM_DISPLAY_VECTOR
7623 && it->current.dpvec_index > 0
7624 && it->dpvec + it->current.dpvec_index != it->dpend);
7625 }
7626
7627 \f
7628 /***********************************************************************
7629 Messages
7630 ***********************************************************************/
7631
7632
7633 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
7634 to *Messages*. */
7635
7636 void
7637 add_to_log (format, arg1, arg2)
7638 char *format;
7639 Lisp_Object arg1, arg2;
7640 {
7641 Lisp_Object args[3];
7642 Lisp_Object msg, fmt;
7643 char *buffer;
7644 int len;
7645 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
7646 USE_SAFE_ALLOCA;
7647
7648 /* Do nothing if called asynchronously. Inserting text into
7649 a buffer may call after-change-functions and alike and
7650 that would means running Lisp asynchronously. */
7651 if (handling_signal)
7652 return;
7653
7654 fmt = msg = Qnil;
7655 GCPRO4 (fmt, msg, arg1, arg2);
7656
7657 args[0] = fmt = build_string (format);
7658 args[1] = arg1;
7659 args[2] = arg2;
7660 msg = Fformat (3, args);
7661
7662 len = SBYTES (msg) + 1;
7663 SAFE_ALLOCA (buffer, char *, len);
7664 bcopy (SDATA (msg), buffer, len);
7665
7666 message_dolog (buffer, len - 1, 1, 0);
7667 SAFE_FREE ();
7668
7669 UNGCPRO;
7670 }
7671
7672
7673 /* Output a newline in the *Messages* buffer if "needs" one. */
7674
7675 void
7676 message_log_maybe_newline ()
7677 {
7678 if (message_log_need_newline)
7679 message_dolog ("", 0, 1, 0);
7680 }
7681
7682
7683 /* Add a string M of length NBYTES to the message log, optionally
7684 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
7685 nonzero, means interpret the contents of M as multibyte. This
7686 function calls low-level routines in order to bypass text property
7687 hooks, etc. which might not be safe to run.
7688
7689 This may GC (insert may run before/after change hooks),
7690 so the buffer M must NOT point to a Lisp string. */
7691
7692 void
7693 message_dolog (m, nbytes, nlflag, multibyte)
7694 const char *m;
7695 int nbytes, nlflag, multibyte;
7696 {
7697 if (!NILP (Vmemory_full))
7698 return;
7699
7700 if (!NILP (Vmessage_log_max))
7701 {
7702 struct buffer *oldbuf;
7703 Lisp_Object oldpoint, oldbegv, oldzv;
7704 int old_windows_or_buffers_changed = windows_or_buffers_changed;
7705 int point_at_end = 0;
7706 int zv_at_end = 0;
7707 Lisp_Object old_deactivate_mark, tem;
7708 struct gcpro gcpro1;
7709
7710 old_deactivate_mark = Vdeactivate_mark;
7711 oldbuf = current_buffer;
7712 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
7713 current_buffer->undo_list = Qt;
7714
7715 oldpoint = message_dolog_marker1;
7716 set_marker_restricted (oldpoint, make_number (PT), Qnil);
7717 oldbegv = message_dolog_marker2;
7718 set_marker_restricted (oldbegv, make_number (BEGV), Qnil);
7719 oldzv = message_dolog_marker3;
7720 set_marker_restricted (oldzv, make_number (ZV), Qnil);
7721 GCPRO1 (old_deactivate_mark);
7722
7723 if (PT == Z)
7724 point_at_end = 1;
7725 if (ZV == Z)
7726 zv_at_end = 1;
7727
7728 BEGV = BEG;
7729 BEGV_BYTE = BEG_BYTE;
7730 ZV = Z;
7731 ZV_BYTE = Z_BYTE;
7732 TEMP_SET_PT_BOTH (Z, Z_BYTE);
7733
7734 /* Insert the string--maybe converting multibyte to single byte
7735 or vice versa, so that all the text fits the buffer. */
7736 if (multibyte
7737 && NILP (current_buffer->enable_multibyte_characters))
7738 {
7739 int i, c, char_bytes;
7740 unsigned char work[1];
7741
7742 /* Convert a multibyte string to single-byte
7743 for the *Message* buffer. */
7744 for (i = 0; i < nbytes; i += char_bytes)
7745 {
7746 c = string_char_and_length (m + i, nbytes - i, &char_bytes);
7747 work[0] = (ASCII_CHAR_P (c)
7748 ? c
7749 : multibyte_char_to_unibyte (c, Qnil));
7750 insert_1_both (work, 1, 1, 1, 0, 0);
7751 }
7752 }
7753 else if (! multibyte
7754 && ! NILP (current_buffer->enable_multibyte_characters))
7755 {
7756 int i, c, char_bytes;
7757 unsigned char *msg = (unsigned char *) m;
7758 unsigned char str[MAX_MULTIBYTE_LENGTH];
7759 /* Convert a single-byte string to multibyte
7760 for the *Message* buffer. */
7761 for (i = 0; i < nbytes; i++)
7762 {
7763 c = msg[i];
7764 c = unibyte_char_to_multibyte (c);
7765 char_bytes = CHAR_STRING (c, str);
7766 insert_1_both (str, 1, char_bytes, 1, 0, 0);
7767 }
7768 }
7769 else if (nbytes)
7770 insert_1 (m, nbytes, 1, 0, 0);
7771
7772 if (nlflag)
7773 {
7774 int this_bol, this_bol_byte, prev_bol, prev_bol_byte, dup;
7775 insert_1 ("\n", 1, 1, 0, 0);
7776
7777 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
7778 this_bol = PT;
7779 this_bol_byte = PT_BYTE;
7780
7781 /* See if this line duplicates the previous one.
7782 If so, combine duplicates. */
7783 if (this_bol > BEG)
7784 {
7785 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
7786 prev_bol = PT;
7787 prev_bol_byte = PT_BYTE;
7788
7789 dup = message_log_check_duplicate (prev_bol, prev_bol_byte,
7790 this_bol, this_bol_byte);
7791 if (dup)
7792 {
7793 del_range_both (prev_bol, prev_bol_byte,
7794 this_bol, this_bol_byte, 0);
7795 if (dup > 1)
7796 {
7797 char dupstr[40];
7798 int duplen;
7799
7800 /* If you change this format, don't forget to also
7801 change message_log_check_duplicate. */
7802 sprintf (dupstr, " [%d times]", dup);
7803 duplen = strlen (dupstr);
7804 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
7805 insert_1 (dupstr, duplen, 1, 0, 1);
7806 }
7807 }
7808 }
7809
7810 /* If we have more than the desired maximum number of lines
7811 in the *Messages* buffer now, delete the oldest ones.
7812 This is safe because we don't have undo in this buffer. */
7813
7814 if (NATNUMP (Vmessage_log_max))
7815 {
7816 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
7817 -XFASTINT (Vmessage_log_max) - 1, 0);
7818 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
7819 }
7820 }
7821 BEGV = XMARKER (oldbegv)->charpos;
7822 BEGV_BYTE = marker_byte_position (oldbegv);
7823
7824 if (zv_at_end)
7825 {
7826 ZV = Z;
7827 ZV_BYTE = Z_BYTE;
7828 }
7829 else
7830 {
7831 ZV = XMARKER (oldzv)->charpos;
7832 ZV_BYTE = marker_byte_position (oldzv);
7833 }
7834
7835 if (point_at_end)
7836 TEMP_SET_PT_BOTH (Z, Z_BYTE);
7837 else
7838 /* We can't do Fgoto_char (oldpoint) because it will run some
7839 Lisp code. */
7840 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
7841 XMARKER (oldpoint)->bytepos);
7842
7843 UNGCPRO;
7844 unchain_marker (XMARKER (oldpoint));
7845 unchain_marker (XMARKER (oldbegv));
7846 unchain_marker (XMARKER (oldzv));
7847
7848 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
7849 set_buffer_internal (oldbuf);
7850 if (NILP (tem))
7851 windows_or_buffers_changed = old_windows_or_buffers_changed;
7852 message_log_need_newline = !nlflag;
7853 Vdeactivate_mark = old_deactivate_mark;
7854 }
7855 }
7856
7857
7858 /* We are at the end of the buffer after just having inserted a newline.
7859 (Note: We depend on the fact we won't be crossing the gap.)
7860 Check to see if the most recent message looks a lot like the previous one.
7861 Return 0 if different, 1 if the new one should just replace it, or a
7862 value N > 1 if we should also append " [N times]". */
7863
7864 static int
7865 message_log_check_duplicate (prev_bol, prev_bol_byte, this_bol, this_bol_byte)
7866 int prev_bol, this_bol;
7867 int prev_bol_byte, this_bol_byte;
7868 {
7869 int i;
7870 int len = Z_BYTE - 1 - this_bol_byte;
7871 int seen_dots = 0;
7872 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
7873 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
7874
7875 for (i = 0; i < len; i++)
7876 {
7877 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
7878 seen_dots = 1;
7879 if (p1[i] != p2[i])
7880 return seen_dots;
7881 }
7882 p1 += len;
7883 if (*p1 == '\n')
7884 return 2;
7885 if (*p1++ == ' ' && *p1++ == '[')
7886 {
7887 int n = 0;
7888 while (*p1 >= '0' && *p1 <= '9')
7889 n = n * 10 + *p1++ - '0';
7890 if (strncmp (p1, " times]\n", 8) == 0)
7891 return n+1;
7892 }
7893 return 0;
7894 }
7895 \f
7896
7897 /* Display an echo area message M with a specified length of NBYTES
7898 bytes. The string may include null characters. If M is 0, clear
7899 out any existing message, and let the mini-buffer text show
7900 through.
7901
7902 This may GC, so the buffer M must NOT point to a Lisp string. */
7903
7904 void
7905 message2 (m, nbytes, multibyte)
7906 const char *m;
7907 int nbytes;
7908 int multibyte;
7909 {
7910 /* First flush out any partial line written with print. */
7911 message_log_maybe_newline ();
7912 if (m)
7913 message_dolog (m, nbytes, 1, multibyte);
7914 message2_nolog (m, nbytes, multibyte);
7915 }
7916
7917
7918 /* The non-logging counterpart of message2. */
7919
7920 void
7921 message2_nolog (m, nbytes, multibyte)
7922 const char *m;
7923 int nbytes, multibyte;
7924 {
7925 struct frame *sf = SELECTED_FRAME ();
7926 message_enable_multibyte = multibyte;
7927
7928 if (noninteractive)
7929 {
7930 if (noninteractive_need_newline)
7931 putc ('\n', stderr);
7932 noninteractive_need_newline = 0;
7933 if (m)
7934 fwrite (m, nbytes, 1, stderr);
7935 if (cursor_in_echo_area == 0)
7936 fprintf (stderr, "\n");
7937 fflush (stderr);
7938 }
7939 /* A null message buffer means that the frame hasn't really been
7940 initialized yet. Error messages get reported properly by
7941 cmd_error, so this must be just an informative message; toss it. */
7942 else if (INTERACTIVE
7943 && sf->glyphs_initialized_p
7944 && FRAME_MESSAGE_BUF (sf))
7945 {
7946 Lisp_Object mini_window;
7947 struct frame *f;
7948
7949 /* Get the frame containing the mini-buffer
7950 that the selected frame is using. */
7951 mini_window = FRAME_MINIBUF_WINDOW (sf);
7952 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
7953
7954 FRAME_SAMPLE_VISIBILITY (f);
7955 if (FRAME_VISIBLE_P (sf)
7956 && ! FRAME_VISIBLE_P (f))
7957 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
7958
7959 if (m)
7960 {
7961 set_message (m, Qnil, nbytes, multibyte);
7962 if (minibuffer_auto_raise)
7963 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
7964 }
7965 else
7966 clear_message (1, 1);
7967
7968 do_pending_window_change (0);
7969 echo_area_display (1);
7970 do_pending_window_change (0);
7971 if (FRAME_TERMINAL (f)->frame_up_to_date_hook != 0 && ! gc_in_progress)
7972 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
7973 }
7974 }
7975
7976
7977 /* Display an echo area message M with a specified length of NBYTES
7978 bytes. The string may include null characters. If M is not a
7979 string, clear out any existing message, and let the mini-buffer
7980 text show through.
7981
7982 This function cancels echoing. */
7983
7984 void
7985 message3 (m, nbytes, multibyte)
7986 Lisp_Object m;
7987 int nbytes;
7988 int multibyte;
7989 {
7990 struct gcpro gcpro1;
7991
7992 GCPRO1 (m);
7993 clear_message (1,1);
7994 cancel_echoing ();
7995
7996 /* First flush out any partial line written with print. */
7997 message_log_maybe_newline ();
7998 if (STRINGP (m))
7999 {
8000 char *buffer;
8001 USE_SAFE_ALLOCA;
8002
8003 SAFE_ALLOCA (buffer, char *, nbytes);
8004 bcopy (SDATA (m), buffer, nbytes);
8005 message_dolog (buffer, nbytes, 1, multibyte);
8006 SAFE_FREE ();
8007 }
8008 message3_nolog (m, nbytes, multibyte);
8009
8010 UNGCPRO;
8011 }
8012
8013
8014 /* The non-logging version of message3.
8015 This does not cancel echoing, because it is used for echoing.
8016 Perhaps we need to make a separate function for echoing
8017 and make this cancel echoing. */
8018
8019 void
8020 message3_nolog (m, nbytes, multibyte)
8021 Lisp_Object m;
8022 int nbytes, multibyte;
8023 {
8024 struct frame *sf = SELECTED_FRAME ();
8025 message_enable_multibyte = multibyte;
8026
8027 if (noninteractive)
8028 {
8029 if (noninteractive_need_newline)
8030 putc ('\n', stderr);
8031 noninteractive_need_newline = 0;
8032 if (STRINGP (m))
8033 fwrite (SDATA (m), nbytes, 1, stderr);
8034 if (cursor_in_echo_area == 0)
8035 fprintf (stderr, "\n");
8036 fflush (stderr);
8037 }
8038 /* A null message buffer means that the frame hasn't really been
8039 initialized yet. Error messages get reported properly by
8040 cmd_error, so this must be just an informative message; toss it. */
8041 else if (INTERACTIVE
8042 && sf->glyphs_initialized_p
8043 && FRAME_MESSAGE_BUF (sf))
8044 {
8045 Lisp_Object mini_window;
8046 Lisp_Object frame;
8047 struct frame *f;
8048
8049 /* Get the frame containing the mini-buffer
8050 that the selected frame is using. */
8051 mini_window = FRAME_MINIBUF_WINDOW (sf);
8052 frame = XWINDOW (mini_window)->frame;
8053 f = XFRAME (frame);
8054
8055 FRAME_SAMPLE_VISIBILITY (f);
8056 if (FRAME_VISIBLE_P (sf)
8057 && !FRAME_VISIBLE_P (f))
8058 Fmake_frame_visible (frame);
8059
8060 if (STRINGP (m) && SCHARS (m) > 0)
8061 {
8062 set_message (NULL, m, nbytes, multibyte);
8063 if (minibuffer_auto_raise)
8064 Fraise_frame (frame);
8065 /* Assume we are not echoing.
8066 (If we are, echo_now will override this.) */
8067 echo_message_buffer = Qnil;
8068 }
8069 else
8070 clear_message (1, 1);
8071
8072 do_pending_window_change (0);
8073 echo_area_display (1);
8074 do_pending_window_change (0);
8075 if (FRAME_TERMINAL (f)->frame_up_to_date_hook != 0 && ! gc_in_progress)
8076 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
8077 }
8078 }
8079
8080
8081 /* Display a null-terminated echo area message M. If M is 0, clear
8082 out any existing message, and let the mini-buffer text show through.
8083
8084 The buffer M must continue to exist until after the echo area gets
8085 cleared or some other message gets displayed there. Do not pass
8086 text that is stored in a Lisp string. Do not pass text in a buffer
8087 that was alloca'd. */
8088
8089 void
8090 message1 (m)
8091 char *m;
8092 {
8093 message2 (m, (m ? strlen (m) : 0), 0);
8094 }
8095
8096
8097 /* The non-logging counterpart of message1. */
8098
8099 void
8100 message1_nolog (m)
8101 char *m;
8102 {
8103 message2_nolog (m, (m ? strlen (m) : 0), 0);
8104 }
8105
8106 /* Display a message M which contains a single %s
8107 which gets replaced with STRING. */
8108
8109 void
8110 message_with_string (m, string, log)
8111 char *m;
8112 Lisp_Object string;
8113 int log;
8114 {
8115 CHECK_STRING (string);
8116
8117 if (noninteractive)
8118 {
8119 if (m)
8120 {
8121 if (noninteractive_need_newline)
8122 putc ('\n', stderr);
8123 noninteractive_need_newline = 0;
8124 fprintf (stderr, m, SDATA (string));
8125 if (cursor_in_echo_area == 0)
8126 fprintf (stderr, "\n");
8127 fflush (stderr);
8128 }
8129 }
8130 else if (INTERACTIVE)
8131 {
8132 /* The frame whose minibuffer we're going to display the message on.
8133 It may be larger than the selected frame, so we need
8134 to use its buffer, not the selected frame's buffer. */
8135 Lisp_Object mini_window;
8136 struct frame *f, *sf = SELECTED_FRAME ();
8137
8138 /* Get the frame containing the minibuffer
8139 that the selected frame is using. */
8140 mini_window = FRAME_MINIBUF_WINDOW (sf);
8141 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
8142
8143 /* A null message buffer means that the frame hasn't really been
8144 initialized yet. Error messages get reported properly by
8145 cmd_error, so this must be just an informative message; toss it. */
8146 if (FRAME_MESSAGE_BUF (f))
8147 {
8148 Lisp_Object args[2], message;
8149 struct gcpro gcpro1, gcpro2;
8150
8151 args[0] = build_string (m);
8152 args[1] = message = string;
8153 GCPRO2 (args[0], message);
8154 gcpro1.nvars = 2;
8155
8156 message = Fformat (2, args);
8157
8158 if (log)
8159 message3 (message, SBYTES (message), STRING_MULTIBYTE (message));
8160 else
8161 message3_nolog (message, SBYTES (message), STRING_MULTIBYTE (message));
8162
8163 UNGCPRO;
8164
8165 /* Print should start at the beginning of the message
8166 buffer next time. */
8167 message_buf_print = 0;
8168 }
8169 }
8170 }
8171
8172
8173 /* Dump an informative message to the minibuf. If M is 0, clear out
8174 any existing message, and let the mini-buffer text show through. */
8175
8176 /* VARARGS 1 */
8177 void
8178 message (m, a1, a2, a3)
8179 char *m;
8180 EMACS_INT a1, a2, a3;
8181 {
8182 if (noninteractive)
8183 {
8184 if (m)
8185 {
8186 if (noninteractive_need_newline)
8187 putc ('\n', stderr);
8188 noninteractive_need_newline = 0;
8189 fprintf (stderr, m, a1, a2, a3);
8190 if (cursor_in_echo_area == 0)
8191 fprintf (stderr, "\n");
8192 fflush (stderr);
8193 }
8194 }
8195 else if (INTERACTIVE)
8196 {
8197 /* The frame whose mini-buffer we're going to display the message
8198 on. It may be larger than the selected frame, so we need to
8199 use its buffer, not the selected frame's buffer. */
8200 Lisp_Object mini_window;
8201 struct frame *f, *sf = SELECTED_FRAME ();
8202
8203 /* Get the frame containing the mini-buffer
8204 that the selected frame is using. */
8205 mini_window = FRAME_MINIBUF_WINDOW (sf);
8206 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
8207
8208 /* A null message buffer means that the frame hasn't really been
8209 initialized yet. Error messages get reported properly by
8210 cmd_error, so this must be just an informative message; toss
8211 it. */
8212 if (FRAME_MESSAGE_BUF (f))
8213 {
8214 if (m)
8215 {
8216 int len;
8217 #ifdef NO_ARG_ARRAY
8218 char *a[3];
8219 a[0] = (char *) a1;
8220 a[1] = (char *) a2;
8221 a[2] = (char *) a3;
8222
8223 len = doprnt (FRAME_MESSAGE_BUF (f),
8224 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
8225 #else
8226 len = doprnt (FRAME_MESSAGE_BUF (f),
8227 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3,
8228 (char **) &a1);
8229 #endif /* NO_ARG_ARRAY */
8230
8231 message2 (FRAME_MESSAGE_BUF (f), len, 0);
8232 }
8233 else
8234 message1 (0);
8235
8236 /* Print should start at the beginning of the message
8237 buffer next time. */
8238 message_buf_print = 0;
8239 }
8240 }
8241 }
8242
8243
8244 /* The non-logging version of message. */
8245
8246 void
8247 message_nolog (m, a1, a2, a3)
8248 char *m;
8249 EMACS_INT a1, a2, a3;
8250 {
8251 Lisp_Object old_log_max;
8252 old_log_max = Vmessage_log_max;
8253 Vmessage_log_max = Qnil;
8254 message (m, a1, a2, a3);
8255 Vmessage_log_max = old_log_max;
8256 }
8257
8258
8259 /* Display the current message in the current mini-buffer. This is
8260 only called from error handlers in process.c, and is not time
8261 critical. */
8262
8263 void
8264 update_echo_area ()
8265 {
8266 if (!NILP (echo_area_buffer[0]))
8267 {
8268 Lisp_Object string;
8269 string = Fcurrent_message ();
8270 message3 (string, SBYTES (string),
8271 !NILP (current_buffer->enable_multibyte_characters));
8272 }
8273 }
8274
8275
8276 /* Make sure echo area buffers in `echo_buffers' are live.
8277 If they aren't, make new ones. */
8278
8279 static void
8280 ensure_echo_area_buffers ()
8281 {
8282 int i;
8283
8284 for (i = 0; i < 2; ++i)
8285 if (!BUFFERP (echo_buffer[i])
8286 || NILP (XBUFFER (echo_buffer[i])->name))
8287 {
8288 char name[30];
8289 Lisp_Object old_buffer;
8290 int j;
8291
8292 old_buffer = echo_buffer[i];
8293 sprintf (name, " *Echo Area %d*", i);
8294 echo_buffer[i] = Fget_buffer_create (build_string (name));
8295 XBUFFER (echo_buffer[i])->truncate_lines = Qnil;
8296 /* to force word wrap in echo area -
8297 it was decided to postpone this*/
8298 /* XBUFFER (echo_buffer[i])->word_wrap = Qt; */
8299
8300 for (j = 0; j < 2; ++j)
8301 if (EQ (old_buffer, echo_area_buffer[j]))
8302 echo_area_buffer[j] = echo_buffer[i];
8303 }
8304 }
8305
8306
8307 /* Call FN with args A1..A4 with either the current or last displayed
8308 echo_area_buffer as current buffer.
8309
8310 WHICH zero means use the current message buffer
8311 echo_area_buffer[0]. If that is nil, choose a suitable buffer
8312 from echo_buffer[] and clear it.
8313
8314 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
8315 suitable buffer from echo_buffer[] and clear it.
8316
8317 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
8318 that the current message becomes the last displayed one, make
8319 choose a suitable buffer for echo_area_buffer[0], and clear it.
8320
8321 Value is what FN returns. */
8322
8323 static int
8324 with_echo_area_buffer (w, which, fn, a1, a2, a3, a4)
8325 struct window *w;
8326 int which;
8327 int (*fn) P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
8328 EMACS_INT a1;
8329 Lisp_Object a2;
8330 EMACS_INT a3, a4;
8331 {
8332 Lisp_Object buffer;
8333 int this_one, the_other, clear_buffer_p, rc;
8334 int count = SPECPDL_INDEX ();
8335
8336 /* If buffers aren't live, make new ones. */
8337 ensure_echo_area_buffers ();
8338
8339 clear_buffer_p = 0;
8340
8341 if (which == 0)
8342 this_one = 0, the_other = 1;
8343 else if (which > 0)
8344 this_one = 1, the_other = 0;
8345 else
8346 {
8347 this_one = 0, the_other = 1;
8348 clear_buffer_p = 1;
8349
8350 /* We need a fresh one in case the current echo buffer equals
8351 the one containing the last displayed echo area message. */
8352 if (!NILP (echo_area_buffer[this_one])
8353 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
8354 echo_area_buffer[this_one] = Qnil;
8355 }
8356
8357 /* Choose a suitable buffer from echo_buffer[] is we don't
8358 have one. */
8359 if (NILP (echo_area_buffer[this_one]))
8360 {
8361 echo_area_buffer[this_one]
8362 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
8363 ? echo_buffer[the_other]
8364 : echo_buffer[this_one]);
8365 clear_buffer_p = 1;
8366 }
8367
8368 buffer = echo_area_buffer[this_one];
8369
8370 /* Don't get confused by reusing the buffer used for echoing
8371 for a different purpose. */
8372 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
8373 cancel_echoing ();
8374
8375 record_unwind_protect (unwind_with_echo_area_buffer,
8376 with_echo_area_buffer_unwind_data (w));
8377
8378 /* Make the echo area buffer current. Note that for display
8379 purposes, it is not necessary that the displayed window's buffer
8380 == current_buffer, except for text property lookup. So, let's
8381 only set that buffer temporarily here without doing a full
8382 Fset_window_buffer. We must also change w->pointm, though,
8383 because otherwise an assertions in unshow_buffer fails, and Emacs
8384 aborts. */
8385 set_buffer_internal_1 (XBUFFER (buffer));
8386 if (w)
8387 {
8388 w->buffer = buffer;
8389 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
8390 }
8391
8392 current_buffer->undo_list = Qt;
8393 current_buffer->read_only = Qnil;
8394 specbind (Qinhibit_read_only, Qt);
8395 specbind (Qinhibit_modification_hooks, Qt);
8396
8397 if (clear_buffer_p && Z > BEG)
8398 del_range (BEG, Z);
8399
8400 xassert (BEGV >= BEG);
8401 xassert (ZV <= Z && ZV >= BEGV);
8402
8403 rc = fn (a1, a2, a3, a4);
8404
8405 xassert (BEGV >= BEG);
8406 xassert (ZV <= Z && ZV >= BEGV);
8407
8408 unbind_to (count, Qnil);
8409 return rc;
8410 }
8411
8412
8413 /* Save state that should be preserved around the call to the function
8414 FN called in with_echo_area_buffer. */
8415
8416 static Lisp_Object
8417 with_echo_area_buffer_unwind_data (w)
8418 struct window *w;
8419 {
8420 int i = 0;
8421 Lisp_Object vector, tmp;
8422
8423 /* Reduce consing by keeping one vector in
8424 Vwith_echo_area_save_vector. */
8425 vector = Vwith_echo_area_save_vector;
8426 Vwith_echo_area_save_vector = Qnil;
8427
8428 if (NILP (vector))
8429 vector = Fmake_vector (make_number (7), Qnil);
8430
8431 XSETBUFFER (tmp, current_buffer); ASET (vector, i, tmp); ++i;
8432 ASET (vector, i, Vdeactivate_mark); ++i;
8433 ASET (vector, i, make_number (windows_or_buffers_changed)); ++i;
8434
8435 if (w)
8436 {
8437 XSETWINDOW (tmp, w); ASET (vector, i, tmp); ++i;
8438 ASET (vector, i, w->buffer); ++i;
8439 ASET (vector, i, make_number (XMARKER (w->pointm)->charpos)); ++i;
8440 ASET (vector, i, make_number (XMARKER (w->pointm)->bytepos)); ++i;
8441 }
8442 else
8443 {
8444 int end = i + 4;
8445 for (; i < end; ++i)
8446 ASET (vector, i, Qnil);
8447 }
8448
8449 xassert (i == ASIZE (vector));
8450 return vector;
8451 }
8452
8453
8454 /* Restore global state from VECTOR which was created by
8455 with_echo_area_buffer_unwind_data. */
8456
8457 static Lisp_Object
8458 unwind_with_echo_area_buffer (vector)
8459 Lisp_Object vector;
8460 {
8461 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
8462 Vdeactivate_mark = AREF (vector, 1);
8463 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
8464
8465 if (WINDOWP (AREF (vector, 3)))
8466 {
8467 struct window *w;
8468 Lisp_Object buffer, charpos, bytepos;
8469
8470 w = XWINDOW (AREF (vector, 3));
8471 buffer = AREF (vector, 4);
8472 charpos = AREF (vector, 5);
8473 bytepos = AREF (vector, 6);
8474
8475 w->buffer = buffer;
8476 set_marker_both (w->pointm, buffer,
8477 XFASTINT (charpos), XFASTINT (bytepos));
8478 }
8479
8480 Vwith_echo_area_save_vector = vector;
8481 return Qnil;
8482 }
8483
8484
8485 /* Set up the echo area for use by print functions. MULTIBYTE_P
8486 non-zero means we will print multibyte. */
8487
8488 void
8489 setup_echo_area_for_printing (multibyte_p)
8490 int multibyte_p;
8491 {
8492 /* If we can't find an echo area any more, exit. */
8493 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
8494 Fkill_emacs (Qnil);
8495
8496 ensure_echo_area_buffers ();
8497
8498 if (!message_buf_print)
8499 {
8500 /* A message has been output since the last time we printed.
8501 Choose a fresh echo area buffer. */
8502 if (EQ (echo_area_buffer[1], echo_buffer[0]))
8503 echo_area_buffer[0] = echo_buffer[1];
8504 else
8505 echo_area_buffer[0] = echo_buffer[0];
8506
8507 /* Switch to that buffer and clear it. */
8508 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
8509 current_buffer->truncate_lines = Qnil;
8510
8511 if (Z > BEG)
8512 {
8513 int count = SPECPDL_INDEX ();
8514 specbind (Qinhibit_read_only, Qt);
8515 /* Note that undo recording is always disabled. */
8516 del_range (BEG, Z);
8517 unbind_to (count, Qnil);
8518 }
8519 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
8520
8521 /* Set up the buffer for the multibyteness we need. */
8522 if (multibyte_p
8523 != !NILP (current_buffer->enable_multibyte_characters))
8524 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
8525
8526 /* Raise the frame containing the echo area. */
8527 if (minibuffer_auto_raise)
8528 {
8529 struct frame *sf = SELECTED_FRAME ();
8530 Lisp_Object mini_window;
8531 mini_window = FRAME_MINIBUF_WINDOW (sf);
8532 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
8533 }
8534
8535 message_log_maybe_newline ();
8536 message_buf_print = 1;
8537 }
8538 else
8539 {
8540 if (NILP (echo_area_buffer[0]))
8541 {
8542 if (EQ (echo_area_buffer[1], echo_buffer[0]))
8543 echo_area_buffer[0] = echo_buffer[1];
8544 else
8545 echo_area_buffer[0] = echo_buffer[0];
8546 }
8547
8548 if (current_buffer != XBUFFER (echo_area_buffer[0]))
8549 {
8550 /* Someone switched buffers between print requests. */
8551 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
8552 current_buffer->truncate_lines = Qnil;
8553 }
8554 }
8555 }
8556
8557
8558 /* Display an echo area message in window W. Value is non-zero if W's
8559 height is changed. If display_last_displayed_message_p is
8560 non-zero, display the message that was last displayed, otherwise
8561 display the current message. */
8562
8563 static int
8564 display_echo_area (w)
8565 struct window *w;
8566 {
8567 int i, no_message_p, window_height_changed_p, count;
8568
8569 /* Temporarily disable garbage collections while displaying the echo
8570 area. This is done because a GC can print a message itself.
8571 That message would modify the echo area buffer's contents while a
8572 redisplay of the buffer is going on, and seriously confuse
8573 redisplay. */
8574 count = inhibit_garbage_collection ();
8575
8576 /* If there is no message, we must call display_echo_area_1
8577 nevertheless because it resizes the window. But we will have to
8578 reset the echo_area_buffer in question to nil at the end because
8579 with_echo_area_buffer will sets it to an empty buffer. */
8580 i = display_last_displayed_message_p ? 1 : 0;
8581 no_message_p = NILP (echo_area_buffer[i]);
8582
8583 window_height_changed_p
8584 = with_echo_area_buffer (w, display_last_displayed_message_p,
8585 display_echo_area_1,
8586 (EMACS_INT) w, Qnil, 0, 0);
8587
8588 if (no_message_p)
8589 echo_area_buffer[i] = Qnil;
8590
8591 unbind_to (count, Qnil);
8592 return window_height_changed_p;
8593 }
8594
8595
8596 /* Helper for display_echo_area. Display the current buffer which
8597 contains the current echo area message in window W, a mini-window,
8598 a pointer to which is passed in A1. A2..A4 are currently not used.
8599 Change the height of W so that all of the message is displayed.
8600 Value is non-zero if height of W was changed. */
8601
8602 static int
8603 display_echo_area_1 (a1, a2, a3, a4)
8604 EMACS_INT a1;
8605 Lisp_Object a2;
8606 EMACS_INT a3, a4;
8607 {
8608 struct window *w = (struct window *) a1;
8609 Lisp_Object window;
8610 struct text_pos start;
8611 int window_height_changed_p = 0;
8612
8613 /* Do this before displaying, so that we have a large enough glyph
8614 matrix for the display. If we can't get enough space for the
8615 whole text, display the last N lines. That works by setting w->start. */
8616 window_height_changed_p = resize_mini_window (w, 0);
8617
8618 /* Use the starting position chosen by resize_mini_window. */
8619 SET_TEXT_POS_FROM_MARKER (start, w->start);
8620
8621 /* Display. */
8622 clear_glyph_matrix (w->desired_matrix);
8623 XSETWINDOW (window, w);
8624 try_window (window, start, 0);
8625
8626 return window_height_changed_p;
8627 }
8628
8629
8630 /* Resize the echo area window to exactly the size needed for the
8631 currently displayed message, if there is one. If a mini-buffer
8632 is active, don't shrink it. */
8633
8634 void
8635 resize_echo_area_exactly ()
8636 {
8637 if (BUFFERP (echo_area_buffer[0])
8638 && WINDOWP (echo_area_window))
8639 {
8640 struct window *w = XWINDOW (echo_area_window);
8641 int resized_p;
8642 Lisp_Object resize_exactly;
8643
8644 if (minibuf_level == 0)
8645 resize_exactly = Qt;
8646 else
8647 resize_exactly = Qnil;
8648
8649 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
8650 (EMACS_INT) w, resize_exactly, 0, 0);
8651 if (resized_p)
8652 {
8653 ++windows_or_buffers_changed;
8654 ++update_mode_lines;
8655 redisplay_internal (0);
8656 }
8657 }
8658 }
8659
8660
8661 /* Callback function for with_echo_area_buffer, when used from
8662 resize_echo_area_exactly. A1 contains a pointer to the window to
8663 resize, EXACTLY non-nil means resize the mini-window exactly to the
8664 size of the text displayed. A3 and A4 are not used. Value is what
8665 resize_mini_window returns. */
8666
8667 static int
8668 resize_mini_window_1 (a1, exactly, a3, a4)
8669 EMACS_INT a1;
8670 Lisp_Object exactly;
8671 EMACS_INT a3, a4;
8672 {
8673 return resize_mini_window ((struct window *) a1, !NILP (exactly));
8674 }
8675
8676
8677 /* Resize mini-window W to fit the size of its contents. EXACT_P
8678 means size the window exactly to the size needed. Otherwise, it's
8679 only enlarged until W's buffer is empty.
8680
8681 Set W->start to the right place to begin display. If the whole
8682 contents fit, start at the beginning. Otherwise, start so as
8683 to make the end of the contents appear. This is particularly
8684 important for y-or-n-p, but seems desirable generally.
8685
8686 Value is non-zero if the window height has been changed. */
8687
8688 int
8689 resize_mini_window (w, exact_p)
8690 struct window *w;
8691 int exact_p;
8692 {
8693 struct frame *f = XFRAME (w->frame);
8694 int window_height_changed_p = 0;
8695
8696 xassert (MINI_WINDOW_P (w));
8697
8698 /* By default, start display at the beginning. */
8699 set_marker_both (w->start, w->buffer,
8700 BUF_BEGV (XBUFFER (w->buffer)),
8701 BUF_BEGV_BYTE (XBUFFER (w->buffer)));
8702
8703 /* Don't resize windows while redisplaying a window; it would
8704 confuse redisplay functions when the size of the window they are
8705 displaying changes from under them. Such a resizing can happen,
8706 for instance, when which-func prints a long message while
8707 we are running fontification-functions. We're running these
8708 functions with safe_call which binds inhibit-redisplay to t. */
8709 if (!NILP (Vinhibit_redisplay))
8710 return 0;
8711
8712 /* Nil means don't try to resize. */
8713 if (NILP (Vresize_mini_windows)
8714 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
8715 return 0;
8716
8717 if (!FRAME_MINIBUF_ONLY_P (f))
8718 {
8719 struct it it;
8720 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
8721 int total_height = WINDOW_TOTAL_LINES (root) + WINDOW_TOTAL_LINES (w);
8722 int height, max_height;
8723 int unit = FRAME_LINE_HEIGHT (f);
8724 struct text_pos start;
8725 struct buffer *old_current_buffer = NULL;
8726
8727 if (current_buffer != XBUFFER (w->buffer))
8728 {
8729 old_current_buffer = current_buffer;
8730 set_buffer_internal (XBUFFER (w->buffer));
8731 }
8732
8733 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
8734
8735 /* Compute the max. number of lines specified by the user. */
8736 if (FLOATP (Vmax_mini_window_height))
8737 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_LINES (f);
8738 else if (INTEGERP (Vmax_mini_window_height))
8739 max_height = XINT (Vmax_mini_window_height);
8740 else
8741 max_height = total_height / 4;
8742
8743 /* Correct that max. height if it's bogus. */
8744 max_height = max (1, max_height);
8745 max_height = min (total_height, max_height);
8746
8747 /* Find out the height of the text in the window. */
8748 if (it.line_wrap == TRUNCATE)
8749 height = 1;
8750 else
8751 {
8752 last_height = 0;
8753 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
8754 if (it.max_ascent == 0 && it.max_descent == 0)
8755 height = it.current_y + last_height;
8756 else
8757 height = it.current_y + it.max_ascent + it.max_descent;
8758 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
8759 height = (height + unit - 1) / unit;
8760 }
8761
8762 /* Compute a suitable window start. */
8763 if (height > max_height)
8764 {
8765 height = max_height;
8766 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
8767 move_it_vertically_backward (&it, (height - 1) * unit);
8768 start = it.current.pos;
8769 }
8770 else
8771 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
8772 SET_MARKER_FROM_TEXT_POS (w->start, start);
8773
8774 if (EQ (Vresize_mini_windows, Qgrow_only))
8775 {
8776 /* Let it grow only, until we display an empty message, in which
8777 case the window shrinks again. */
8778 if (height > WINDOW_TOTAL_LINES (w))
8779 {
8780 int old_height = WINDOW_TOTAL_LINES (w);
8781 freeze_window_starts (f, 1);
8782 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
8783 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8784 }
8785 else if (height < WINDOW_TOTAL_LINES (w)
8786 && (exact_p || BEGV == ZV))
8787 {
8788 int old_height = WINDOW_TOTAL_LINES (w);
8789 freeze_window_starts (f, 0);
8790 shrink_mini_window (w);
8791 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8792 }
8793 }
8794 else
8795 {
8796 /* Always resize to exact size needed. */
8797 if (height > WINDOW_TOTAL_LINES (w))
8798 {
8799 int old_height = WINDOW_TOTAL_LINES (w);
8800 freeze_window_starts (f, 1);
8801 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
8802 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8803 }
8804 else if (height < WINDOW_TOTAL_LINES (w))
8805 {
8806 int old_height = WINDOW_TOTAL_LINES (w);
8807 freeze_window_starts (f, 0);
8808 shrink_mini_window (w);
8809
8810 if (height)
8811 {
8812 freeze_window_starts (f, 1);
8813 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
8814 }
8815
8816 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8817 }
8818 }
8819
8820 if (old_current_buffer)
8821 set_buffer_internal (old_current_buffer);
8822 }
8823
8824 return window_height_changed_p;
8825 }
8826
8827
8828 /* Value is the current message, a string, or nil if there is no
8829 current message. */
8830
8831 Lisp_Object
8832 current_message ()
8833 {
8834 Lisp_Object msg;
8835
8836 if (!BUFFERP (echo_area_buffer[0]))
8837 msg = Qnil;
8838 else
8839 {
8840 with_echo_area_buffer (0, 0, current_message_1,
8841 (EMACS_INT) &msg, Qnil, 0, 0);
8842 if (NILP (msg))
8843 echo_area_buffer[0] = Qnil;
8844 }
8845
8846 return msg;
8847 }
8848
8849
8850 static int
8851 current_message_1 (a1, a2, a3, a4)
8852 EMACS_INT a1;
8853 Lisp_Object a2;
8854 EMACS_INT a3, a4;
8855 {
8856 Lisp_Object *msg = (Lisp_Object *) a1;
8857
8858 if (Z > BEG)
8859 *msg = make_buffer_string (BEG, Z, 1);
8860 else
8861 *msg = Qnil;
8862 return 0;
8863 }
8864
8865
8866 /* Push the current message on Vmessage_stack for later restauration
8867 by restore_message. Value is non-zero if the current message isn't
8868 empty. This is a relatively infrequent operation, so it's not
8869 worth optimizing. */
8870
8871 int
8872 push_message ()
8873 {
8874 Lisp_Object msg;
8875 msg = current_message ();
8876 Vmessage_stack = Fcons (msg, Vmessage_stack);
8877 return STRINGP (msg);
8878 }
8879
8880
8881 /* Restore message display from the top of Vmessage_stack. */
8882
8883 void
8884 restore_message ()
8885 {
8886 Lisp_Object msg;
8887
8888 xassert (CONSP (Vmessage_stack));
8889 msg = XCAR (Vmessage_stack);
8890 if (STRINGP (msg))
8891 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
8892 else
8893 message3_nolog (msg, 0, 0);
8894 }
8895
8896
8897 /* Handler for record_unwind_protect calling pop_message. */
8898
8899 Lisp_Object
8900 pop_message_unwind (dummy)
8901 Lisp_Object dummy;
8902 {
8903 pop_message ();
8904 return Qnil;
8905 }
8906
8907 /* Pop the top-most entry off Vmessage_stack. */
8908
8909 void
8910 pop_message ()
8911 {
8912 xassert (CONSP (Vmessage_stack));
8913 Vmessage_stack = XCDR (Vmessage_stack);
8914 }
8915
8916
8917 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
8918 exits. If the stack is not empty, we have a missing pop_message
8919 somewhere. */
8920
8921 void
8922 check_message_stack ()
8923 {
8924 if (!NILP (Vmessage_stack))
8925 abort ();
8926 }
8927
8928
8929 /* Truncate to NCHARS what will be displayed in the echo area the next
8930 time we display it---but don't redisplay it now. */
8931
8932 void
8933 truncate_echo_area (nchars)
8934 int nchars;
8935 {
8936 if (nchars == 0)
8937 echo_area_buffer[0] = Qnil;
8938 /* A null message buffer means that the frame hasn't really been
8939 initialized yet. Error messages get reported properly by
8940 cmd_error, so this must be just an informative message; toss it. */
8941 else if (!noninteractive
8942 && INTERACTIVE
8943 && !NILP (echo_area_buffer[0]))
8944 {
8945 struct frame *sf = SELECTED_FRAME ();
8946 if (FRAME_MESSAGE_BUF (sf))
8947 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
8948 }
8949 }
8950
8951
8952 /* Helper function for truncate_echo_area. Truncate the current
8953 message to at most NCHARS characters. */
8954
8955 static int
8956 truncate_message_1 (nchars, a2, a3, a4)
8957 EMACS_INT nchars;
8958 Lisp_Object a2;
8959 EMACS_INT a3, a4;
8960 {
8961 if (BEG + nchars < Z)
8962 del_range (BEG + nchars, Z);
8963 if (Z == BEG)
8964 echo_area_buffer[0] = Qnil;
8965 return 0;
8966 }
8967
8968
8969 /* Set the current message to a substring of S or STRING.
8970
8971 If STRING is a Lisp string, set the message to the first NBYTES
8972 bytes from STRING. NBYTES zero means use the whole string. If
8973 STRING is multibyte, the message will be displayed multibyte.
8974
8975 If S is not null, set the message to the first LEN bytes of S. LEN
8976 zero means use the whole string. MULTIBYTE_P non-zero means S is
8977 multibyte. Display the message multibyte in that case.
8978
8979 Doesn't GC, as with_echo_area_buffer binds Qinhibit_modification_hooks
8980 to t before calling set_message_1 (which calls insert).
8981 */
8982
8983 void
8984 set_message (s, string, nbytes, multibyte_p)
8985 const char *s;
8986 Lisp_Object string;
8987 int nbytes, multibyte_p;
8988 {
8989 message_enable_multibyte
8990 = ((s && multibyte_p)
8991 || (STRINGP (string) && STRING_MULTIBYTE (string)));
8992
8993 with_echo_area_buffer (0, -1, set_message_1,
8994 (EMACS_INT) s, string, nbytes, multibyte_p);
8995 message_buf_print = 0;
8996 help_echo_showing_p = 0;
8997 }
8998
8999
9000 /* Helper function for set_message. Arguments have the same meaning
9001 as there, with A1 corresponding to S and A2 corresponding to STRING
9002 This function is called with the echo area buffer being
9003 current. */
9004
9005 static int
9006 set_message_1 (a1, a2, nbytes, multibyte_p)
9007 EMACS_INT a1;
9008 Lisp_Object a2;
9009 EMACS_INT nbytes, multibyte_p;
9010 {
9011 const char *s = (const char *) a1;
9012 Lisp_Object string = a2;
9013
9014 /* Change multibyteness of the echo buffer appropriately. */
9015 if (message_enable_multibyte
9016 != !NILP (current_buffer->enable_multibyte_characters))
9017 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
9018
9019 current_buffer->truncate_lines = message_truncate_lines ? Qt : Qnil;
9020
9021 /* Insert new message at BEG. */
9022 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
9023
9024 if (STRINGP (string))
9025 {
9026 int nchars;
9027
9028 if (nbytes == 0)
9029 nbytes = SBYTES (string);
9030 nchars = string_byte_to_char (string, nbytes);
9031
9032 /* This function takes care of single/multibyte conversion. We
9033 just have to ensure that the echo area buffer has the right
9034 setting of enable_multibyte_characters. */
9035 insert_from_string (string, 0, 0, nchars, nbytes, 1);
9036 }
9037 else if (s)
9038 {
9039 if (nbytes == 0)
9040 nbytes = strlen (s);
9041
9042 if (multibyte_p && NILP (current_buffer->enable_multibyte_characters))
9043 {
9044 /* Convert from multi-byte to single-byte. */
9045 int i, c, n;
9046 unsigned char work[1];
9047
9048 /* Convert a multibyte string to single-byte. */
9049 for (i = 0; i < nbytes; i += n)
9050 {
9051 c = string_char_and_length (s + i, nbytes - i, &n);
9052 work[0] = (ASCII_CHAR_P (c)
9053 ? c
9054 : multibyte_char_to_unibyte (c, Qnil));
9055 insert_1_both (work, 1, 1, 1, 0, 0);
9056 }
9057 }
9058 else if (!multibyte_p
9059 && !NILP (current_buffer->enable_multibyte_characters))
9060 {
9061 /* Convert from single-byte to multi-byte. */
9062 int i, c, n;
9063 const unsigned char *msg = (const unsigned char *) s;
9064 unsigned char str[MAX_MULTIBYTE_LENGTH];
9065
9066 /* Convert a single-byte string to multibyte. */
9067 for (i = 0; i < nbytes; i++)
9068 {
9069 c = msg[i];
9070 c = unibyte_char_to_multibyte (c);
9071 n = CHAR_STRING (c, str);
9072 insert_1_both (str, 1, n, 1, 0, 0);
9073 }
9074 }
9075 else
9076 insert_1 (s, nbytes, 1, 0, 0);
9077 }
9078
9079 return 0;
9080 }
9081
9082
9083 /* Clear messages. CURRENT_P non-zero means clear the current
9084 message. LAST_DISPLAYED_P non-zero means clear the message
9085 last displayed. */
9086
9087 void
9088 clear_message (current_p, last_displayed_p)
9089 int current_p, last_displayed_p;
9090 {
9091 if (current_p)
9092 {
9093 echo_area_buffer[0] = Qnil;
9094 message_cleared_p = 1;
9095 }
9096
9097 if (last_displayed_p)
9098 echo_area_buffer[1] = Qnil;
9099
9100 message_buf_print = 0;
9101 }
9102
9103 /* Clear garbaged frames.
9104
9105 This function is used where the old redisplay called
9106 redraw_garbaged_frames which in turn called redraw_frame which in
9107 turn called clear_frame. The call to clear_frame was a source of
9108 flickering. I believe a clear_frame is not necessary. It should
9109 suffice in the new redisplay to invalidate all current matrices,
9110 and ensure a complete redisplay of all windows. */
9111
9112 static void
9113 clear_garbaged_frames ()
9114 {
9115 if (frame_garbaged)
9116 {
9117 Lisp_Object tail, frame;
9118 int changed_count = 0;
9119
9120 FOR_EACH_FRAME (tail, frame)
9121 {
9122 struct frame *f = XFRAME (frame);
9123
9124 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
9125 {
9126 if (f->resized_p)
9127 {
9128 Fredraw_frame (frame);
9129 f->force_flush_display_p = 1;
9130 }
9131 clear_current_matrices (f);
9132 changed_count++;
9133 f->garbaged = 0;
9134 f->resized_p = 0;
9135 }
9136 }
9137
9138 frame_garbaged = 0;
9139 if (changed_count)
9140 ++windows_or_buffers_changed;
9141 }
9142 }
9143
9144
9145 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
9146 is non-zero update selected_frame. Value is non-zero if the
9147 mini-windows height has been changed. */
9148
9149 static int
9150 echo_area_display (update_frame_p)
9151 int update_frame_p;
9152 {
9153 Lisp_Object mini_window;
9154 struct window *w;
9155 struct frame *f;
9156 int window_height_changed_p = 0;
9157 struct frame *sf = SELECTED_FRAME ();
9158
9159 mini_window = FRAME_MINIBUF_WINDOW (sf);
9160 w = XWINDOW (mini_window);
9161 f = XFRAME (WINDOW_FRAME (w));
9162
9163 /* Don't display if frame is invisible or not yet initialized. */
9164 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
9165 return 0;
9166
9167 #ifdef HAVE_WINDOW_SYSTEM
9168 /* When Emacs starts, selected_frame may be the initial terminal
9169 frame. If we let this through, a message would be displayed on
9170 the terminal. */
9171 if (FRAME_INITIAL_P (XFRAME (selected_frame)))
9172 return 0;
9173 #endif /* HAVE_WINDOW_SYSTEM */
9174
9175 /* Redraw garbaged frames. */
9176 if (frame_garbaged)
9177 clear_garbaged_frames ();
9178
9179 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
9180 {
9181 echo_area_window = mini_window;
9182 window_height_changed_p = display_echo_area (w);
9183 w->must_be_updated_p = 1;
9184
9185 /* Update the display, unless called from redisplay_internal.
9186 Also don't update the screen during redisplay itself. The
9187 update will happen at the end of redisplay, and an update
9188 here could cause confusion. */
9189 if (update_frame_p && !redisplaying_p)
9190 {
9191 int n = 0;
9192
9193 /* If the display update has been interrupted by pending
9194 input, update mode lines in the frame. Due to the
9195 pending input, it might have been that redisplay hasn't
9196 been called, so that mode lines above the echo area are
9197 garbaged. This looks odd, so we prevent it here. */
9198 if (!display_completed)
9199 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
9200
9201 if (window_height_changed_p
9202 /* Don't do this if Emacs is shutting down. Redisplay
9203 needs to run hooks. */
9204 && !NILP (Vrun_hooks))
9205 {
9206 /* Must update other windows. Likewise as in other
9207 cases, don't let this update be interrupted by
9208 pending input. */
9209 int count = SPECPDL_INDEX ();
9210 specbind (Qredisplay_dont_pause, Qt);
9211 windows_or_buffers_changed = 1;
9212 redisplay_internal (0);
9213 unbind_to (count, Qnil);
9214 }
9215 else if (FRAME_WINDOW_P (f) && n == 0)
9216 {
9217 /* Window configuration is the same as before.
9218 Can do with a display update of the echo area,
9219 unless we displayed some mode lines. */
9220 update_single_window (w, 1);
9221 FRAME_RIF (f)->flush_display (f);
9222 }
9223 else
9224 update_frame (f, 1, 1);
9225
9226 /* If cursor is in the echo area, make sure that the next
9227 redisplay displays the minibuffer, so that the cursor will
9228 be replaced with what the minibuffer wants. */
9229 if (cursor_in_echo_area)
9230 ++windows_or_buffers_changed;
9231 }
9232 }
9233 else if (!EQ (mini_window, selected_window))
9234 windows_or_buffers_changed++;
9235
9236 /* Last displayed message is now the current message. */
9237 echo_area_buffer[1] = echo_area_buffer[0];
9238 /* Inform read_char that we're not echoing. */
9239 echo_message_buffer = Qnil;
9240
9241 /* Prevent redisplay optimization in redisplay_internal by resetting
9242 this_line_start_pos. This is done because the mini-buffer now
9243 displays the message instead of its buffer text. */
9244 if (EQ (mini_window, selected_window))
9245 CHARPOS (this_line_start_pos) = 0;
9246
9247 return window_height_changed_p;
9248 }
9249
9250
9251 \f
9252 /***********************************************************************
9253 Mode Lines and Frame Titles
9254 ***********************************************************************/
9255
9256 /* A buffer for constructing non-propertized mode-line strings and
9257 frame titles in it; allocated from the heap in init_xdisp and
9258 resized as needed in store_mode_line_noprop_char. */
9259
9260 static char *mode_line_noprop_buf;
9261
9262 /* The buffer's end, and a current output position in it. */
9263
9264 static char *mode_line_noprop_buf_end;
9265 static char *mode_line_noprop_ptr;
9266
9267 #define MODE_LINE_NOPROP_LEN(start) \
9268 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
9269
9270 static enum {
9271 MODE_LINE_DISPLAY = 0,
9272 MODE_LINE_TITLE,
9273 MODE_LINE_NOPROP,
9274 MODE_LINE_STRING
9275 } mode_line_target;
9276
9277 /* Alist that caches the results of :propertize.
9278 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
9279 static Lisp_Object mode_line_proptrans_alist;
9280
9281 /* List of strings making up the mode-line. */
9282 static Lisp_Object mode_line_string_list;
9283
9284 /* Base face property when building propertized mode line string. */
9285 static Lisp_Object mode_line_string_face;
9286 static Lisp_Object mode_line_string_face_prop;
9287
9288
9289 /* Unwind data for mode line strings */
9290
9291 static Lisp_Object Vmode_line_unwind_vector;
9292
9293 static Lisp_Object
9294 format_mode_line_unwind_data (struct buffer *obuf,
9295 Lisp_Object owin,
9296 int save_proptrans)
9297 {
9298 Lisp_Object vector, tmp;
9299
9300 /* Reduce consing by keeping one vector in
9301 Vwith_echo_area_save_vector. */
9302 vector = Vmode_line_unwind_vector;
9303 Vmode_line_unwind_vector = Qnil;
9304
9305 if (NILP (vector))
9306 vector = Fmake_vector (make_number (8), Qnil);
9307
9308 ASET (vector, 0, make_number (mode_line_target));
9309 ASET (vector, 1, make_number (MODE_LINE_NOPROP_LEN (0)));
9310 ASET (vector, 2, mode_line_string_list);
9311 ASET (vector, 3, save_proptrans ? mode_line_proptrans_alist : Qt);
9312 ASET (vector, 4, mode_line_string_face);
9313 ASET (vector, 5, mode_line_string_face_prop);
9314
9315 if (obuf)
9316 XSETBUFFER (tmp, obuf);
9317 else
9318 tmp = Qnil;
9319 ASET (vector, 6, tmp);
9320 ASET (vector, 7, owin);
9321
9322 return vector;
9323 }
9324
9325 static Lisp_Object
9326 unwind_format_mode_line (vector)
9327 Lisp_Object vector;
9328 {
9329 mode_line_target = XINT (AREF (vector, 0));
9330 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
9331 mode_line_string_list = AREF (vector, 2);
9332 if (! EQ (AREF (vector, 3), Qt))
9333 mode_line_proptrans_alist = AREF (vector, 3);
9334 mode_line_string_face = AREF (vector, 4);
9335 mode_line_string_face_prop = AREF (vector, 5);
9336
9337 if (!NILP (AREF (vector, 7)))
9338 /* Select window before buffer, since it may change the buffer. */
9339 Fselect_window (AREF (vector, 7), Qt);
9340
9341 if (!NILP (AREF (vector, 6)))
9342 {
9343 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
9344 ASET (vector, 6, Qnil);
9345 }
9346
9347 Vmode_line_unwind_vector = vector;
9348 return Qnil;
9349 }
9350
9351
9352 /* Store a single character C for the frame title in mode_line_noprop_buf.
9353 Re-allocate mode_line_noprop_buf if necessary. */
9354
9355 static void
9356 #ifdef PROTOTYPES
9357 store_mode_line_noprop_char (char c)
9358 #else
9359 store_mode_line_noprop_char (c)
9360 char c;
9361 #endif
9362 {
9363 /* If output position has reached the end of the allocated buffer,
9364 double the buffer's size. */
9365 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
9366 {
9367 int len = MODE_LINE_NOPROP_LEN (0);
9368 int new_size = 2 * len * sizeof *mode_line_noprop_buf;
9369 mode_line_noprop_buf = (char *) xrealloc (mode_line_noprop_buf, new_size);
9370 mode_line_noprop_buf_end = mode_line_noprop_buf + new_size;
9371 mode_line_noprop_ptr = mode_line_noprop_buf + len;
9372 }
9373
9374 *mode_line_noprop_ptr++ = c;
9375 }
9376
9377
9378 /* Store part of a frame title in mode_line_noprop_buf, beginning at
9379 mode_line_noprop_ptr. STR is the string to store. Do not copy
9380 characters that yield more columns than PRECISION; PRECISION <= 0
9381 means copy the whole string. Pad with spaces until FIELD_WIDTH
9382 number of characters have been copied; FIELD_WIDTH <= 0 means don't
9383 pad. Called from display_mode_element when it is used to build a
9384 frame title. */
9385
9386 static int
9387 store_mode_line_noprop (str, field_width, precision)
9388 const unsigned char *str;
9389 int field_width, precision;
9390 {
9391 int n = 0;
9392 int dummy, nbytes;
9393
9394 /* Copy at most PRECISION chars from STR. */
9395 nbytes = strlen (str);
9396 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
9397 while (nbytes--)
9398 store_mode_line_noprop_char (*str++);
9399
9400 /* Fill up with spaces until FIELD_WIDTH reached. */
9401 while (field_width > 0
9402 && n < field_width)
9403 {
9404 store_mode_line_noprop_char (' ');
9405 ++n;
9406 }
9407
9408 return n;
9409 }
9410
9411 /***********************************************************************
9412 Frame Titles
9413 ***********************************************************************/
9414
9415 #ifdef HAVE_WINDOW_SYSTEM
9416
9417 /* Set the title of FRAME, if it has changed. The title format is
9418 Vicon_title_format if FRAME is iconified, otherwise it is
9419 frame_title_format. */
9420
9421 static void
9422 x_consider_frame_title (frame)
9423 Lisp_Object frame;
9424 {
9425 struct frame *f = XFRAME (frame);
9426
9427 if (FRAME_WINDOW_P (f)
9428 || FRAME_MINIBUF_ONLY_P (f)
9429 || f->explicit_name)
9430 {
9431 /* Do we have more than one visible frame on this X display? */
9432 Lisp_Object tail;
9433 Lisp_Object fmt;
9434 int title_start;
9435 char *title;
9436 int len;
9437 struct it it;
9438 int count = SPECPDL_INDEX ();
9439
9440 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
9441 {
9442 Lisp_Object other_frame = XCAR (tail);
9443 struct frame *tf = XFRAME (other_frame);
9444
9445 if (tf != f
9446 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
9447 && !FRAME_MINIBUF_ONLY_P (tf)
9448 && !EQ (other_frame, tip_frame)
9449 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
9450 break;
9451 }
9452
9453 /* Set global variable indicating that multiple frames exist. */
9454 multiple_frames = CONSP (tail);
9455
9456 /* Switch to the buffer of selected window of the frame. Set up
9457 mode_line_target so that display_mode_element will output into
9458 mode_line_noprop_buf; then display the title. */
9459 record_unwind_protect (unwind_format_mode_line,
9460 format_mode_line_unwind_data
9461 (current_buffer, selected_window, 0));
9462
9463 Fselect_window (f->selected_window, Qt);
9464 set_buffer_internal_1 (XBUFFER (XWINDOW (f->selected_window)->buffer));
9465 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
9466
9467 mode_line_target = MODE_LINE_TITLE;
9468 title_start = MODE_LINE_NOPROP_LEN (0);
9469 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
9470 NULL, DEFAULT_FACE_ID);
9471 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
9472 len = MODE_LINE_NOPROP_LEN (title_start);
9473 title = mode_line_noprop_buf + title_start;
9474 unbind_to (count, Qnil);
9475
9476 /* Set the title only if it's changed. This avoids consing in
9477 the common case where it hasn't. (If it turns out that we've
9478 already wasted too much time by walking through the list with
9479 display_mode_element, then we might need to optimize at a
9480 higher level than this.) */
9481 if (! STRINGP (f->name)
9482 || SBYTES (f->name) != len
9483 || bcmp (title, SDATA (f->name), len) != 0)
9484 {
9485 #ifdef HAVE_NS
9486 if (FRAME_NS_P (f))
9487 {
9488 if (!MINI_WINDOW_P(XWINDOW(f->selected_window)))
9489 {
9490 if (EQ (fmt, Qt))
9491 ns_set_name_as_filename (f);
9492 else
9493 x_implicitly_set_name (f, make_string(title, len),
9494 Qnil);
9495 }
9496 }
9497 else
9498 #endif
9499 x_implicitly_set_name (f, make_string (title, len), Qnil);
9500 }
9501 #ifdef HAVE_NS
9502 if (FRAME_NS_P (f))
9503 {
9504 /* do this also for frames with explicit names */
9505 ns_implicitly_set_icon_type(f);
9506 ns_set_doc_edited(f, Fbuffer_modified_p
9507 (XWINDOW (f->selected_window)->buffer), Qnil);
9508 }
9509 #endif
9510 }
9511 }
9512
9513 #endif /* not HAVE_WINDOW_SYSTEM */
9514
9515
9516
9517 \f
9518 /***********************************************************************
9519 Menu Bars
9520 ***********************************************************************/
9521
9522
9523 /* Prepare for redisplay by updating menu-bar item lists when
9524 appropriate. This can call eval. */
9525
9526 void
9527 prepare_menu_bars ()
9528 {
9529 int all_windows;
9530 struct gcpro gcpro1, gcpro2;
9531 struct frame *f;
9532 Lisp_Object tooltip_frame;
9533
9534 #ifdef HAVE_WINDOW_SYSTEM
9535 tooltip_frame = tip_frame;
9536 #else
9537 tooltip_frame = Qnil;
9538 #endif
9539
9540 /* Update all frame titles based on their buffer names, etc. We do
9541 this before the menu bars so that the buffer-menu will show the
9542 up-to-date frame titles. */
9543 #ifdef HAVE_WINDOW_SYSTEM
9544 if (windows_or_buffers_changed || update_mode_lines)
9545 {
9546 Lisp_Object tail, frame;
9547
9548 FOR_EACH_FRAME (tail, frame)
9549 {
9550 f = XFRAME (frame);
9551 if (!EQ (frame, tooltip_frame)
9552 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
9553 x_consider_frame_title (frame);
9554 }
9555 }
9556 #endif /* HAVE_WINDOW_SYSTEM */
9557
9558 /* Update the menu bar item lists, if appropriate. This has to be
9559 done before any actual redisplay or generation of display lines. */
9560 all_windows = (update_mode_lines
9561 || buffer_shared > 1
9562 || windows_or_buffers_changed);
9563 if (all_windows)
9564 {
9565 Lisp_Object tail, frame;
9566 int count = SPECPDL_INDEX ();
9567 /* 1 means that update_menu_bar has run its hooks
9568 so any further calls to update_menu_bar shouldn't do so again. */
9569 int menu_bar_hooks_run = 0;
9570
9571 record_unwind_save_match_data ();
9572
9573 FOR_EACH_FRAME (tail, frame)
9574 {
9575 f = XFRAME (frame);
9576
9577 /* Ignore tooltip frame. */
9578 if (EQ (frame, tooltip_frame))
9579 continue;
9580
9581 /* If a window on this frame changed size, report that to
9582 the user and clear the size-change flag. */
9583 if (FRAME_WINDOW_SIZES_CHANGED (f))
9584 {
9585 Lisp_Object functions;
9586
9587 /* Clear flag first in case we get an error below. */
9588 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
9589 functions = Vwindow_size_change_functions;
9590 GCPRO2 (tail, functions);
9591
9592 while (CONSP (functions))
9593 {
9594 call1 (XCAR (functions), frame);
9595 functions = XCDR (functions);
9596 }
9597 UNGCPRO;
9598 }
9599
9600 GCPRO1 (tail);
9601 menu_bar_hooks_run = update_menu_bar (f, 0, menu_bar_hooks_run);
9602 #ifdef HAVE_WINDOW_SYSTEM
9603 update_tool_bar (f, 0);
9604 #endif
9605 UNGCPRO;
9606 }
9607
9608 unbind_to (count, Qnil);
9609 }
9610 else
9611 {
9612 struct frame *sf = SELECTED_FRAME ();
9613 update_menu_bar (sf, 1, 0);
9614 #ifdef HAVE_WINDOW_SYSTEM
9615 update_tool_bar (sf, 1);
9616 #endif
9617 }
9618
9619 /* Motif needs this. See comment in xmenu.c. Turn it off when
9620 pending_menu_activation is not defined. */
9621 #ifdef USE_X_TOOLKIT
9622 pending_menu_activation = 0;
9623 #endif
9624 }
9625
9626
9627 /* Update the menu bar item list for frame F. This has to be done
9628 before we start to fill in any display lines, because it can call
9629 eval.
9630
9631 If SAVE_MATCH_DATA is non-zero, we must save and restore it here.
9632
9633 If HOOKS_RUN is 1, that means a previous call to update_menu_bar
9634 already ran the menu bar hooks for this redisplay, so there
9635 is no need to run them again. The return value is the
9636 updated value of this flag, to pass to the next call. */
9637
9638 static int
9639 update_menu_bar (f, save_match_data, hooks_run)
9640 struct frame *f;
9641 int save_match_data;
9642 int hooks_run;
9643 {
9644 Lisp_Object window;
9645 register struct window *w;
9646
9647 /* If called recursively during a menu update, do nothing. This can
9648 happen when, for instance, an activate-menubar-hook causes a
9649 redisplay. */
9650 if (inhibit_menubar_update)
9651 return hooks_run;
9652
9653 window = FRAME_SELECTED_WINDOW (f);
9654 w = XWINDOW (window);
9655
9656 if (FRAME_WINDOW_P (f)
9657 ?
9658 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
9659 || defined (HAVE_NS) || defined (USE_GTK)
9660 FRAME_EXTERNAL_MENU_BAR (f)
9661 #else
9662 FRAME_MENU_BAR_LINES (f) > 0
9663 #endif
9664 : FRAME_MENU_BAR_LINES (f) > 0)
9665 {
9666 /* If the user has switched buffers or windows, we need to
9667 recompute to reflect the new bindings. But we'll
9668 recompute when update_mode_lines is set too; that means
9669 that people can use force-mode-line-update to request
9670 that the menu bar be recomputed. The adverse effect on
9671 the rest of the redisplay algorithm is about the same as
9672 windows_or_buffers_changed anyway. */
9673 if (windows_or_buffers_changed
9674 /* This used to test w->update_mode_line, but we believe
9675 there is no need to recompute the menu in that case. */
9676 || update_mode_lines
9677 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
9678 < BUF_MODIFF (XBUFFER (w->buffer)))
9679 != !NILP (w->last_had_star))
9680 || ((!NILP (Vtransient_mark_mode)
9681 && !NILP (XBUFFER (w->buffer)->mark_active))
9682 != !NILP (w->region_showing)))
9683 {
9684 struct buffer *prev = current_buffer;
9685 int count = SPECPDL_INDEX ();
9686
9687 specbind (Qinhibit_menubar_update, Qt);
9688
9689 set_buffer_internal_1 (XBUFFER (w->buffer));
9690 if (save_match_data)
9691 record_unwind_save_match_data ();
9692 if (NILP (Voverriding_local_map_menu_flag))
9693 {
9694 specbind (Qoverriding_terminal_local_map, Qnil);
9695 specbind (Qoverriding_local_map, Qnil);
9696 }
9697
9698 if (!hooks_run)
9699 {
9700 /* Run the Lucid hook. */
9701 safe_run_hooks (Qactivate_menubar_hook);
9702
9703 /* If it has changed current-menubar from previous value,
9704 really recompute the menu-bar from the value. */
9705 if (! NILP (Vlucid_menu_bar_dirty_flag))
9706 call0 (Qrecompute_lucid_menubar);
9707
9708 safe_run_hooks (Qmenu_bar_update_hook);
9709
9710 hooks_run = 1;
9711 }
9712
9713 XSETFRAME (Vmenu_updating_frame, f);
9714 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
9715
9716 /* Redisplay the menu bar in case we changed it. */
9717 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
9718 || defined (HAVE_NS) || defined (USE_GTK)
9719 if (FRAME_WINDOW_P (f))
9720 {
9721 #if defined (HAVE_NS)
9722 /* All frames on Mac OS share the same menubar. So only
9723 the selected frame should be allowed to set it. */
9724 if (f == SELECTED_FRAME ())
9725 #endif
9726 set_frame_menubar (f, 0, 0);
9727 }
9728 else
9729 /* On a terminal screen, the menu bar is an ordinary screen
9730 line, and this makes it get updated. */
9731 w->update_mode_line = Qt;
9732 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
9733 /* In the non-toolkit version, the menu bar is an ordinary screen
9734 line, and this makes it get updated. */
9735 w->update_mode_line = Qt;
9736 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
9737
9738 unbind_to (count, Qnil);
9739 set_buffer_internal_1 (prev);
9740 }
9741 }
9742
9743 return hooks_run;
9744 }
9745
9746
9747 \f
9748 /***********************************************************************
9749 Output Cursor
9750 ***********************************************************************/
9751
9752 #ifdef HAVE_WINDOW_SYSTEM
9753
9754 /* EXPORT:
9755 Nominal cursor position -- where to draw output.
9756 HPOS and VPOS are window relative glyph matrix coordinates.
9757 X and Y are window relative pixel coordinates. */
9758
9759 struct cursor_pos output_cursor;
9760
9761
9762 /* EXPORT:
9763 Set the global variable output_cursor to CURSOR. All cursor
9764 positions are relative to updated_window. */
9765
9766 void
9767 set_output_cursor (cursor)
9768 struct cursor_pos *cursor;
9769 {
9770 output_cursor.hpos = cursor->hpos;
9771 output_cursor.vpos = cursor->vpos;
9772 output_cursor.x = cursor->x;
9773 output_cursor.y = cursor->y;
9774 }
9775
9776
9777 /* EXPORT for RIF:
9778 Set a nominal cursor position.
9779
9780 HPOS and VPOS are column/row positions in a window glyph matrix. X
9781 and Y are window text area relative pixel positions.
9782
9783 If this is done during an update, updated_window will contain the
9784 window that is being updated and the position is the future output
9785 cursor position for that window. If updated_window is null, use
9786 selected_window and display the cursor at the given position. */
9787
9788 void
9789 x_cursor_to (vpos, hpos, y, x)
9790 int vpos, hpos, y, x;
9791 {
9792 struct window *w;
9793
9794 /* If updated_window is not set, work on selected_window. */
9795 if (updated_window)
9796 w = updated_window;
9797 else
9798 w = XWINDOW (selected_window);
9799
9800 /* Set the output cursor. */
9801 output_cursor.hpos = hpos;
9802 output_cursor.vpos = vpos;
9803 output_cursor.x = x;
9804 output_cursor.y = y;
9805
9806 /* If not called as part of an update, really display the cursor.
9807 This will also set the cursor position of W. */
9808 if (updated_window == NULL)
9809 {
9810 BLOCK_INPUT;
9811 display_and_set_cursor (w, 1, hpos, vpos, x, y);
9812 if (FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
9813 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (SELECTED_FRAME ());
9814 UNBLOCK_INPUT;
9815 }
9816 }
9817
9818 #endif /* HAVE_WINDOW_SYSTEM */
9819
9820 \f
9821 /***********************************************************************
9822 Tool-bars
9823 ***********************************************************************/
9824
9825 #ifdef HAVE_WINDOW_SYSTEM
9826
9827 /* Where the mouse was last time we reported a mouse event. */
9828
9829 FRAME_PTR last_mouse_frame;
9830
9831 /* Tool-bar item index of the item on which a mouse button was pressed
9832 or -1. */
9833
9834 int last_tool_bar_item;
9835
9836
9837 static Lisp_Object
9838 update_tool_bar_unwind (frame)
9839 Lisp_Object frame;
9840 {
9841 selected_frame = frame;
9842 return Qnil;
9843 }
9844
9845 /* Update the tool-bar item list for frame F. This has to be done
9846 before we start to fill in any display lines. Called from
9847 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
9848 and restore it here. */
9849
9850 static void
9851 update_tool_bar (f, save_match_data)
9852 struct frame *f;
9853 int save_match_data;
9854 {
9855 #if defined (USE_GTK) || defined (HAVE_NS) || USE_MAC_TOOLBAR
9856 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
9857 #else
9858 int do_update = WINDOWP (f->tool_bar_window)
9859 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0;
9860 #endif
9861
9862 if (do_update)
9863 {
9864 Lisp_Object window;
9865 struct window *w;
9866
9867 window = FRAME_SELECTED_WINDOW (f);
9868 w = XWINDOW (window);
9869
9870 /* If the user has switched buffers or windows, we need to
9871 recompute to reflect the new bindings. But we'll
9872 recompute when update_mode_lines is set too; that means
9873 that people can use force-mode-line-update to request
9874 that the menu bar be recomputed. The adverse effect on
9875 the rest of the redisplay algorithm is about the same as
9876 windows_or_buffers_changed anyway. */
9877 if (windows_or_buffers_changed
9878 || !NILP (w->update_mode_line)
9879 || update_mode_lines
9880 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
9881 < BUF_MODIFF (XBUFFER (w->buffer)))
9882 != !NILP (w->last_had_star))
9883 || ((!NILP (Vtransient_mark_mode)
9884 && !NILP (XBUFFER (w->buffer)->mark_active))
9885 != !NILP (w->region_showing)))
9886 {
9887 struct buffer *prev = current_buffer;
9888 int count = SPECPDL_INDEX ();
9889 Lisp_Object frame, new_tool_bar;
9890 int new_n_tool_bar;
9891 struct gcpro gcpro1;
9892
9893 /* Set current_buffer to the buffer of the selected
9894 window of the frame, so that we get the right local
9895 keymaps. */
9896 set_buffer_internal_1 (XBUFFER (w->buffer));
9897
9898 /* Save match data, if we must. */
9899 if (save_match_data)
9900 record_unwind_save_match_data ();
9901
9902 /* Make sure that we don't accidentally use bogus keymaps. */
9903 if (NILP (Voverriding_local_map_menu_flag))
9904 {
9905 specbind (Qoverriding_terminal_local_map, Qnil);
9906 specbind (Qoverriding_local_map, Qnil);
9907 }
9908
9909 GCPRO1 (new_tool_bar);
9910
9911 /* We must temporarily set the selected frame to this frame
9912 before calling tool_bar_items, because the calculation of
9913 the tool-bar keymap uses the selected frame (see
9914 `tool-bar-make-keymap' in tool-bar.el). */
9915 record_unwind_protect (update_tool_bar_unwind, selected_frame);
9916 XSETFRAME (frame, f);
9917 selected_frame = frame;
9918
9919 /* Build desired tool-bar items from keymaps. */
9920 new_tool_bar = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
9921 &new_n_tool_bar);
9922
9923 /* Redisplay the tool-bar if we changed it. */
9924 if (new_n_tool_bar != f->n_tool_bar_items
9925 || NILP (Fequal (new_tool_bar, f->tool_bar_items)))
9926 {
9927 /* Redisplay that happens asynchronously due to an expose event
9928 may access f->tool_bar_items. Make sure we update both
9929 variables within BLOCK_INPUT so no such event interrupts. */
9930 BLOCK_INPUT;
9931 f->tool_bar_items = new_tool_bar;
9932 f->n_tool_bar_items = new_n_tool_bar;
9933 w->update_mode_line = Qt;
9934 UNBLOCK_INPUT;
9935 }
9936
9937 UNGCPRO;
9938
9939 unbind_to (count, Qnil);
9940 set_buffer_internal_1 (prev);
9941 }
9942 }
9943 }
9944
9945
9946 /* Set F->desired_tool_bar_string to a Lisp string representing frame
9947 F's desired tool-bar contents. F->tool_bar_items must have
9948 been set up previously by calling prepare_menu_bars. */
9949
9950 static void
9951 build_desired_tool_bar_string (f)
9952 struct frame *f;
9953 {
9954 int i, size, size_needed;
9955 struct gcpro gcpro1, gcpro2, gcpro3;
9956 Lisp_Object image, plist, props;
9957
9958 image = plist = props = Qnil;
9959 GCPRO3 (image, plist, props);
9960
9961 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
9962 Otherwise, make a new string. */
9963
9964 /* The size of the string we might be able to reuse. */
9965 size = (STRINGP (f->desired_tool_bar_string)
9966 ? SCHARS (f->desired_tool_bar_string)
9967 : 0);
9968
9969 /* We need one space in the string for each image. */
9970 size_needed = f->n_tool_bar_items;
9971
9972 /* Reuse f->desired_tool_bar_string, if possible. */
9973 if (size < size_needed || NILP (f->desired_tool_bar_string))
9974 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
9975 make_number (' '));
9976 else
9977 {
9978 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
9979 Fremove_text_properties (make_number (0), make_number (size),
9980 props, f->desired_tool_bar_string);
9981 }
9982
9983 /* Put a `display' property on the string for the images to display,
9984 put a `menu_item' property on tool-bar items with a value that
9985 is the index of the item in F's tool-bar item vector. */
9986 for (i = 0; i < f->n_tool_bar_items; ++i)
9987 {
9988 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
9989
9990 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
9991 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
9992 int hmargin, vmargin, relief, idx, end;
9993 extern Lisp_Object QCrelief, QCmargin, QCconversion;
9994
9995 /* If image is a vector, choose the image according to the
9996 button state. */
9997 image = PROP (TOOL_BAR_ITEM_IMAGES);
9998 if (VECTORP (image))
9999 {
10000 if (enabled_p)
10001 idx = (selected_p
10002 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
10003 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
10004 else
10005 idx = (selected_p
10006 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
10007 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
10008
10009 xassert (ASIZE (image) >= idx);
10010 image = AREF (image, idx);
10011 }
10012 else
10013 idx = -1;
10014
10015 /* Ignore invalid image specifications. */
10016 if (!valid_image_p (image))
10017 continue;
10018
10019 /* Display the tool-bar button pressed, or depressed. */
10020 plist = Fcopy_sequence (XCDR (image));
10021
10022 /* Compute margin and relief to draw. */
10023 relief = (tool_bar_button_relief >= 0
10024 ? tool_bar_button_relief
10025 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
10026 hmargin = vmargin = relief;
10027
10028 if (INTEGERP (Vtool_bar_button_margin)
10029 && XINT (Vtool_bar_button_margin) > 0)
10030 {
10031 hmargin += XFASTINT (Vtool_bar_button_margin);
10032 vmargin += XFASTINT (Vtool_bar_button_margin);
10033 }
10034 else if (CONSP (Vtool_bar_button_margin))
10035 {
10036 if (INTEGERP (XCAR (Vtool_bar_button_margin))
10037 && XINT (XCAR (Vtool_bar_button_margin)) > 0)
10038 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
10039
10040 if (INTEGERP (XCDR (Vtool_bar_button_margin))
10041 && XINT (XCDR (Vtool_bar_button_margin)) > 0)
10042 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
10043 }
10044
10045 if (auto_raise_tool_bar_buttons_p)
10046 {
10047 /* Add a `:relief' property to the image spec if the item is
10048 selected. */
10049 if (selected_p)
10050 {
10051 plist = Fplist_put (plist, QCrelief, make_number (-relief));
10052 hmargin -= relief;
10053 vmargin -= relief;
10054 }
10055 }
10056 else
10057 {
10058 /* If image is selected, display it pressed, i.e. with a
10059 negative relief. If it's not selected, display it with a
10060 raised relief. */
10061 plist = Fplist_put (plist, QCrelief,
10062 (selected_p
10063 ? make_number (-relief)
10064 : make_number (relief)));
10065 hmargin -= relief;
10066 vmargin -= relief;
10067 }
10068
10069 /* Put a margin around the image. */
10070 if (hmargin || vmargin)
10071 {
10072 if (hmargin == vmargin)
10073 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
10074 else
10075 plist = Fplist_put (plist, QCmargin,
10076 Fcons (make_number (hmargin),
10077 make_number (vmargin)));
10078 }
10079
10080 /* If button is not enabled, and we don't have special images
10081 for the disabled state, make the image appear disabled by
10082 applying an appropriate algorithm to it. */
10083 if (!enabled_p && idx < 0)
10084 plist = Fplist_put (plist, QCconversion, Qdisabled);
10085
10086 /* Put a `display' text property on the string for the image to
10087 display. Put a `menu-item' property on the string that gives
10088 the start of this item's properties in the tool-bar items
10089 vector. */
10090 image = Fcons (Qimage, plist);
10091 props = list4 (Qdisplay, image,
10092 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
10093
10094 /* Let the last image hide all remaining spaces in the tool bar
10095 string. The string can be longer than needed when we reuse a
10096 previous string. */
10097 if (i + 1 == f->n_tool_bar_items)
10098 end = SCHARS (f->desired_tool_bar_string);
10099 else
10100 end = i + 1;
10101 Fadd_text_properties (make_number (i), make_number (end),
10102 props, f->desired_tool_bar_string);
10103 #undef PROP
10104 }
10105
10106 UNGCPRO;
10107 }
10108
10109
10110 /* Display one line of the tool-bar of frame IT->f.
10111
10112 HEIGHT specifies the desired height of the tool-bar line.
10113 If the actual height of the glyph row is less than HEIGHT, the
10114 row's height is increased to HEIGHT, and the icons are centered
10115 vertically in the new height.
10116
10117 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
10118 count a final empty row in case the tool-bar width exactly matches
10119 the window width.
10120 */
10121
10122 static void
10123 display_tool_bar_line (it, height)
10124 struct it *it;
10125 int height;
10126 {
10127 struct glyph_row *row = it->glyph_row;
10128 int max_x = it->last_visible_x;
10129 struct glyph *last;
10130
10131 prepare_desired_row (row);
10132 row->y = it->current_y;
10133
10134 /* Note that this isn't made use of if the face hasn't a box,
10135 so there's no need to check the face here. */
10136 it->start_of_box_run_p = 1;
10137
10138 while (it->current_x < max_x)
10139 {
10140 int x, n_glyphs_before, i, nglyphs;
10141 struct it it_before;
10142
10143 /* Get the next display element. */
10144 if (!get_next_display_element (it))
10145 {
10146 /* Don't count empty row if we are counting needed tool-bar lines. */
10147 if (height < 0 && !it->hpos)
10148 return;
10149 break;
10150 }
10151
10152 /* Produce glyphs. */
10153 n_glyphs_before = row->used[TEXT_AREA];
10154 it_before = *it;
10155
10156 PRODUCE_GLYPHS (it);
10157
10158 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
10159 i = 0;
10160 x = it_before.current_x;
10161 while (i < nglyphs)
10162 {
10163 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
10164
10165 if (x + glyph->pixel_width > max_x)
10166 {
10167 /* Glyph doesn't fit on line. Backtrack. */
10168 row->used[TEXT_AREA] = n_glyphs_before;
10169 *it = it_before;
10170 /* If this is the only glyph on this line, it will never fit on the
10171 toolbar, so skip it. But ensure there is at least one glyph,
10172 so we don't accidentally disable the tool-bar. */
10173 if (n_glyphs_before == 0
10174 && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
10175 break;
10176 goto out;
10177 }
10178
10179 ++it->hpos;
10180 x += glyph->pixel_width;
10181 ++i;
10182 }
10183
10184 /* Stop at line ends. */
10185 if (ITERATOR_AT_END_OF_LINE_P (it))
10186 break;
10187
10188 set_iterator_to_next (it, 1);
10189 }
10190
10191 out:;
10192
10193 row->displays_text_p = row->used[TEXT_AREA] != 0;
10194
10195 /* Use default face for the border below the tool bar.
10196
10197 FIXME: When auto-resize-tool-bars is grow-only, there is
10198 no additional border below the possibly empty tool-bar lines.
10199 So to make the extra empty lines look "normal", we have to
10200 use the tool-bar face for the border too. */
10201 if (!row->displays_text_p && !EQ (Vauto_resize_tool_bars, Qgrow_only))
10202 it->face_id = DEFAULT_FACE_ID;
10203
10204 extend_face_to_end_of_line (it);
10205 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
10206 last->right_box_line_p = 1;
10207 if (last == row->glyphs[TEXT_AREA])
10208 last->left_box_line_p = 1;
10209
10210 /* Make line the desired height and center it vertically. */
10211 if ((height -= it->max_ascent + it->max_descent) > 0)
10212 {
10213 /* Don't add more than one line height. */
10214 height %= FRAME_LINE_HEIGHT (it->f);
10215 it->max_ascent += height / 2;
10216 it->max_descent += (height + 1) / 2;
10217 }
10218
10219 compute_line_metrics (it);
10220
10221 /* If line is empty, make it occupy the rest of the tool-bar. */
10222 if (!row->displays_text_p)
10223 {
10224 row->height = row->phys_height = it->last_visible_y - row->y;
10225 row->visible_height = row->height;
10226 row->ascent = row->phys_ascent = 0;
10227 row->extra_line_spacing = 0;
10228 }
10229
10230 row->full_width_p = 1;
10231 row->continued_p = 0;
10232 row->truncated_on_left_p = 0;
10233 row->truncated_on_right_p = 0;
10234
10235 it->current_x = it->hpos = 0;
10236 it->current_y += row->height;
10237 ++it->vpos;
10238 ++it->glyph_row;
10239 }
10240
10241
10242 /* Max tool-bar height. */
10243
10244 #define MAX_FRAME_TOOL_BAR_HEIGHT(f) \
10245 ((FRAME_LINE_HEIGHT (f) * FRAME_LINES (f)))
10246
10247 /* Value is the number of screen lines needed to make all tool-bar
10248 items of frame F visible. The number of actual rows needed is
10249 returned in *N_ROWS if non-NULL. */
10250
10251 static int
10252 tool_bar_lines_needed (f, n_rows)
10253 struct frame *f;
10254 int *n_rows;
10255 {
10256 struct window *w = XWINDOW (f->tool_bar_window);
10257 struct it it;
10258 /* tool_bar_lines_needed is called from redisplay_tool_bar after building
10259 the desired matrix, so use (unused) mode-line row as temporary row to
10260 avoid destroying the first tool-bar row. */
10261 struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
10262
10263 /* Initialize an iterator for iteration over
10264 F->desired_tool_bar_string in the tool-bar window of frame F. */
10265 init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
10266 it.first_visible_x = 0;
10267 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
10268 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
10269
10270 while (!ITERATOR_AT_END_P (&it))
10271 {
10272 clear_glyph_row (temp_row);
10273 it.glyph_row = temp_row;
10274 display_tool_bar_line (&it, -1);
10275 }
10276 clear_glyph_row (temp_row);
10277
10278 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
10279 if (n_rows)
10280 *n_rows = it.vpos > 0 ? it.vpos : -1;
10281
10282 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
10283 }
10284
10285
10286 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
10287 0, 1, 0,
10288 doc: /* Return the number of lines occupied by the tool bar of FRAME. */)
10289 (frame)
10290 Lisp_Object frame;
10291 {
10292 struct frame *f;
10293 struct window *w;
10294 int nlines = 0;
10295
10296 if (NILP (frame))
10297 frame = selected_frame;
10298 else
10299 CHECK_FRAME (frame);
10300 f = XFRAME (frame);
10301
10302 if (WINDOWP (f->tool_bar_window)
10303 || (w = XWINDOW (f->tool_bar_window),
10304 WINDOW_TOTAL_LINES (w) > 0))
10305 {
10306 update_tool_bar (f, 1);
10307 if (f->n_tool_bar_items)
10308 {
10309 build_desired_tool_bar_string (f);
10310 nlines = tool_bar_lines_needed (f, NULL);
10311 }
10312 }
10313
10314 return make_number (nlines);
10315 }
10316
10317
10318 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
10319 height should be changed. */
10320
10321 static int
10322 redisplay_tool_bar (f)
10323 struct frame *f;
10324 {
10325 struct window *w;
10326 struct it it;
10327 struct glyph_row *row;
10328
10329 #if defined (USE_GTK) || defined (HAVE_NS) || USE_MAC_TOOLBAR
10330 if (FRAME_EXTERNAL_TOOL_BAR (f))
10331 update_frame_tool_bar (f);
10332 return 0;
10333 #endif
10334
10335 /* If frame hasn't a tool-bar window or if it is zero-height, don't
10336 do anything. This means you must start with tool-bar-lines
10337 non-zero to get the auto-sizing effect. Or in other words, you
10338 can turn off tool-bars by specifying tool-bar-lines zero. */
10339 if (!WINDOWP (f->tool_bar_window)
10340 || (w = XWINDOW (f->tool_bar_window),
10341 WINDOW_TOTAL_LINES (w) == 0))
10342 return 0;
10343
10344 /* Set up an iterator for the tool-bar window. */
10345 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
10346 it.first_visible_x = 0;
10347 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
10348 row = it.glyph_row;
10349
10350 /* Build a string that represents the contents of the tool-bar. */
10351 build_desired_tool_bar_string (f);
10352 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
10353
10354 if (f->n_tool_bar_rows == 0)
10355 {
10356 int nlines;
10357
10358 if ((nlines = tool_bar_lines_needed (f, &f->n_tool_bar_rows),
10359 nlines != WINDOW_TOTAL_LINES (w)))
10360 {
10361 extern Lisp_Object Qtool_bar_lines;
10362 Lisp_Object frame;
10363 int old_height = WINDOW_TOTAL_LINES (w);
10364
10365 XSETFRAME (frame, f);
10366 Fmodify_frame_parameters (frame,
10367 Fcons (Fcons (Qtool_bar_lines,
10368 make_number (nlines)),
10369 Qnil));
10370 if (WINDOW_TOTAL_LINES (w) != old_height)
10371 {
10372 clear_glyph_matrix (w->desired_matrix);
10373 fonts_changed_p = 1;
10374 return 1;
10375 }
10376 }
10377 }
10378
10379 /* Display as many lines as needed to display all tool-bar items. */
10380
10381 if (f->n_tool_bar_rows > 0)
10382 {
10383 int border, rows, height, extra;
10384
10385 if (INTEGERP (Vtool_bar_border))
10386 border = XINT (Vtool_bar_border);
10387 else if (EQ (Vtool_bar_border, Qinternal_border_width))
10388 border = FRAME_INTERNAL_BORDER_WIDTH (f);
10389 else if (EQ (Vtool_bar_border, Qborder_width))
10390 border = f->border_width;
10391 else
10392 border = 0;
10393 if (border < 0)
10394 border = 0;
10395
10396 rows = f->n_tool_bar_rows;
10397 height = max (1, (it.last_visible_y - border) / rows);
10398 extra = it.last_visible_y - border - height * rows;
10399
10400 while (it.current_y < it.last_visible_y)
10401 {
10402 int h = 0;
10403 if (extra > 0 && rows-- > 0)
10404 {
10405 h = (extra + rows - 1) / rows;
10406 extra -= h;
10407 }
10408 display_tool_bar_line (&it, height + h);
10409 }
10410 }
10411 else
10412 {
10413 while (it.current_y < it.last_visible_y)
10414 display_tool_bar_line (&it, 0);
10415 }
10416
10417 /* It doesn't make much sense to try scrolling in the tool-bar
10418 window, so don't do it. */
10419 w->desired_matrix->no_scrolling_p = 1;
10420 w->must_be_updated_p = 1;
10421
10422 if (!NILP (Vauto_resize_tool_bars))
10423 {
10424 int max_tool_bar_height = MAX_FRAME_TOOL_BAR_HEIGHT (f);
10425 int change_height_p = 0;
10426
10427 /* If we couldn't display everything, change the tool-bar's
10428 height if there is room for more. */
10429 if (IT_STRING_CHARPOS (it) < it.end_charpos
10430 && it.current_y < max_tool_bar_height)
10431 change_height_p = 1;
10432
10433 row = it.glyph_row - 1;
10434
10435 /* If there are blank lines at the end, except for a partially
10436 visible blank line at the end that is smaller than
10437 FRAME_LINE_HEIGHT, change the tool-bar's height. */
10438 if (!row->displays_text_p
10439 && row->height >= FRAME_LINE_HEIGHT (f))
10440 change_height_p = 1;
10441
10442 /* If row displays tool-bar items, but is partially visible,
10443 change the tool-bar's height. */
10444 if (row->displays_text_p
10445 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y
10446 && MATRIX_ROW_BOTTOM_Y (row) < max_tool_bar_height)
10447 change_height_p = 1;
10448
10449 /* Resize windows as needed by changing the `tool-bar-lines'
10450 frame parameter. */
10451 if (change_height_p)
10452 {
10453 extern Lisp_Object Qtool_bar_lines;
10454 Lisp_Object frame;
10455 int old_height = WINDOW_TOTAL_LINES (w);
10456 int nrows;
10457 int nlines = tool_bar_lines_needed (f, &nrows);
10458
10459 change_height_p = ((EQ (Vauto_resize_tool_bars, Qgrow_only)
10460 && !f->minimize_tool_bar_window_p)
10461 ? (nlines > old_height)
10462 : (nlines != old_height));
10463 f->minimize_tool_bar_window_p = 0;
10464
10465 if (change_height_p)
10466 {
10467 XSETFRAME (frame, f);
10468 Fmodify_frame_parameters (frame,
10469 Fcons (Fcons (Qtool_bar_lines,
10470 make_number (nlines)),
10471 Qnil));
10472 if (WINDOW_TOTAL_LINES (w) != old_height)
10473 {
10474 clear_glyph_matrix (w->desired_matrix);
10475 f->n_tool_bar_rows = nrows;
10476 fonts_changed_p = 1;
10477 return 1;
10478 }
10479 }
10480 }
10481 }
10482
10483 f->minimize_tool_bar_window_p = 0;
10484 return 0;
10485 }
10486
10487
10488 /* Get information about the tool-bar item which is displayed in GLYPH
10489 on frame F. Return in *PROP_IDX the index where tool-bar item
10490 properties start in F->tool_bar_items. Value is zero if
10491 GLYPH doesn't display a tool-bar item. */
10492
10493 static int
10494 tool_bar_item_info (f, glyph, prop_idx)
10495 struct frame *f;
10496 struct glyph *glyph;
10497 int *prop_idx;
10498 {
10499 Lisp_Object prop;
10500 int success_p;
10501 int charpos;
10502
10503 /* This function can be called asynchronously, which means we must
10504 exclude any possibility that Fget_text_property signals an
10505 error. */
10506 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
10507 charpos = max (0, charpos);
10508
10509 /* Get the text property `menu-item' at pos. The value of that
10510 property is the start index of this item's properties in
10511 F->tool_bar_items. */
10512 prop = Fget_text_property (make_number (charpos),
10513 Qmenu_item, f->current_tool_bar_string);
10514 if (INTEGERP (prop))
10515 {
10516 *prop_idx = XINT (prop);
10517 success_p = 1;
10518 }
10519 else
10520 success_p = 0;
10521
10522 return success_p;
10523 }
10524
10525 \f
10526 /* Get information about the tool-bar item at position X/Y on frame F.
10527 Return in *GLYPH a pointer to the glyph of the tool-bar item in
10528 the current matrix of the tool-bar window of F, or NULL if not
10529 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
10530 item in F->tool_bar_items. Value is
10531
10532 -1 if X/Y is not on a tool-bar item
10533 0 if X/Y is on the same item that was highlighted before.
10534 1 otherwise. */
10535
10536 static int
10537 get_tool_bar_item (f, x, y, glyph, hpos, vpos, prop_idx)
10538 struct frame *f;
10539 int x, y;
10540 struct glyph **glyph;
10541 int *hpos, *vpos, *prop_idx;
10542 {
10543 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10544 struct window *w = XWINDOW (f->tool_bar_window);
10545 int area;
10546
10547 /* Find the glyph under X/Y. */
10548 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
10549 if (*glyph == NULL)
10550 return -1;
10551
10552 /* Get the start of this tool-bar item's properties in
10553 f->tool_bar_items. */
10554 if (!tool_bar_item_info (f, *glyph, prop_idx))
10555 return -1;
10556
10557 /* Is mouse on the highlighted item? */
10558 if (EQ (f->tool_bar_window, dpyinfo->mouse_face_window)
10559 && *vpos >= dpyinfo->mouse_face_beg_row
10560 && *vpos <= dpyinfo->mouse_face_end_row
10561 && (*vpos > dpyinfo->mouse_face_beg_row
10562 || *hpos >= dpyinfo->mouse_face_beg_col)
10563 && (*vpos < dpyinfo->mouse_face_end_row
10564 || *hpos < dpyinfo->mouse_face_end_col
10565 || dpyinfo->mouse_face_past_end))
10566 return 0;
10567
10568 return 1;
10569 }
10570
10571
10572 /* EXPORT:
10573 Handle mouse button event on the tool-bar of frame F, at
10574 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
10575 0 for button release. MODIFIERS is event modifiers for button
10576 release. */
10577
10578 void
10579 handle_tool_bar_click (f, x, y, down_p, modifiers)
10580 struct frame *f;
10581 int x, y, down_p;
10582 unsigned int modifiers;
10583 {
10584 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10585 struct window *w = XWINDOW (f->tool_bar_window);
10586 int hpos, vpos, prop_idx;
10587 struct glyph *glyph;
10588 Lisp_Object enabled_p;
10589
10590 /* If not on the highlighted tool-bar item, return. */
10591 frame_to_window_pixel_xy (w, &x, &y);
10592 if (get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
10593 return;
10594
10595 /* If item is disabled, do nothing. */
10596 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
10597 if (NILP (enabled_p))
10598 return;
10599
10600 if (down_p)
10601 {
10602 /* Show item in pressed state. */
10603 show_mouse_face (dpyinfo, DRAW_IMAGE_SUNKEN);
10604 dpyinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
10605 last_tool_bar_item = prop_idx;
10606 }
10607 else
10608 {
10609 Lisp_Object key, frame;
10610 struct input_event event;
10611 EVENT_INIT (event);
10612
10613 /* Show item in released state. */
10614 show_mouse_face (dpyinfo, DRAW_IMAGE_RAISED);
10615 dpyinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
10616
10617 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
10618
10619 XSETFRAME (frame, f);
10620 event.kind = TOOL_BAR_EVENT;
10621 event.frame_or_window = frame;
10622 event.arg = frame;
10623 kbd_buffer_store_event (&event);
10624
10625 event.kind = TOOL_BAR_EVENT;
10626 event.frame_or_window = frame;
10627 event.arg = key;
10628 event.modifiers = modifiers;
10629 kbd_buffer_store_event (&event);
10630 last_tool_bar_item = -1;
10631 }
10632 }
10633
10634
10635 /* Possibly highlight a tool-bar item on frame F when mouse moves to
10636 tool-bar window-relative coordinates X/Y. Called from
10637 note_mouse_highlight. */
10638
10639 static void
10640 note_tool_bar_highlight (f, x, y)
10641 struct frame *f;
10642 int x, y;
10643 {
10644 Lisp_Object window = f->tool_bar_window;
10645 struct window *w = XWINDOW (window);
10646 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10647 int hpos, vpos;
10648 struct glyph *glyph;
10649 struct glyph_row *row;
10650 int i;
10651 Lisp_Object enabled_p;
10652 int prop_idx;
10653 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
10654 int mouse_down_p, rc;
10655
10656 /* Function note_mouse_highlight is called with negative x(y
10657 values when mouse moves outside of the frame. */
10658 if (x <= 0 || y <= 0)
10659 {
10660 clear_mouse_face (dpyinfo);
10661 return;
10662 }
10663
10664 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
10665 if (rc < 0)
10666 {
10667 /* Not on tool-bar item. */
10668 clear_mouse_face (dpyinfo);
10669 return;
10670 }
10671 else if (rc == 0)
10672 /* On same tool-bar item as before. */
10673 goto set_help_echo;
10674
10675 clear_mouse_face (dpyinfo);
10676
10677 /* Mouse is down, but on different tool-bar item? */
10678 mouse_down_p = (dpyinfo->grabbed
10679 && f == last_mouse_frame
10680 && FRAME_LIVE_P (f));
10681 if (mouse_down_p
10682 && last_tool_bar_item != prop_idx)
10683 return;
10684
10685 dpyinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
10686 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
10687
10688 /* If tool-bar item is not enabled, don't highlight it. */
10689 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
10690 if (!NILP (enabled_p))
10691 {
10692 /* Compute the x-position of the glyph. In front and past the
10693 image is a space. We include this in the highlighted area. */
10694 row = MATRIX_ROW (w->current_matrix, vpos);
10695 for (i = x = 0; i < hpos; ++i)
10696 x += row->glyphs[TEXT_AREA][i].pixel_width;
10697
10698 /* Record this as the current active region. */
10699 dpyinfo->mouse_face_beg_col = hpos;
10700 dpyinfo->mouse_face_beg_row = vpos;
10701 dpyinfo->mouse_face_beg_x = x;
10702 dpyinfo->mouse_face_beg_y = row->y;
10703 dpyinfo->mouse_face_past_end = 0;
10704
10705 dpyinfo->mouse_face_end_col = hpos + 1;
10706 dpyinfo->mouse_face_end_row = vpos;
10707 dpyinfo->mouse_face_end_x = x + glyph->pixel_width;
10708 dpyinfo->mouse_face_end_y = row->y;
10709 dpyinfo->mouse_face_window = window;
10710 dpyinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
10711
10712 /* Display it as active. */
10713 show_mouse_face (dpyinfo, draw);
10714 dpyinfo->mouse_face_image_state = draw;
10715 }
10716
10717 set_help_echo:
10718
10719 /* Set help_echo_string to a help string to display for this tool-bar item.
10720 XTread_socket does the rest. */
10721 help_echo_object = help_echo_window = Qnil;
10722 help_echo_pos = -1;
10723 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
10724 if (NILP (help_echo_string))
10725 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
10726 }
10727
10728 #endif /* HAVE_WINDOW_SYSTEM */
10729
10730
10731 \f
10732 /************************************************************************
10733 Horizontal scrolling
10734 ************************************************************************/
10735
10736 static int hscroll_window_tree P_ ((Lisp_Object));
10737 static int hscroll_windows P_ ((Lisp_Object));
10738
10739 /* For all leaf windows in the window tree rooted at WINDOW, set their
10740 hscroll value so that PT is (i) visible in the window, and (ii) so
10741 that it is not within a certain margin at the window's left and
10742 right border. Value is non-zero if any window's hscroll has been
10743 changed. */
10744
10745 static int
10746 hscroll_window_tree (window)
10747 Lisp_Object window;
10748 {
10749 int hscrolled_p = 0;
10750 int hscroll_relative_p = FLOATP (Vhscroll_step);
10751 int hscroll_step_abs = 0;
10752 double hscroll_step_rel = 0;
10753
10754 if (hscroll_relative_p)
10755 {
10756 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
10757 if (hscroll_step_rel < 0)
10758 {
10759 hscroll_relative_p = 0;
10760 hscroll_step_abs = 0;
10761 }
10762 }
10763 else if (INTEGERP (Vhscroll_step))
10764 {
10765 hscroll_step_abs = XINT (Vhscroll_step);
10766 if (hscroll_step_abs < 0)
10767 hscroll_step_abs = 0;
10768 }
10769 else
10770 hscroll_step_abs = 0;
10771
10772 while (WINDOWP (window))
10773 {
10774 struct window *w = XWINDOW (window);
10775
10776 if (WINDOWP (w->hchild))
10777 hscrolled_p |= hscroll_window_tree (w->hchild);
10778 else if (WINDOWP (w->vchild))
10779 hscrolled_p |= hscroll_window_tree (w->vchild);
10780 else if (w->cursor.vpos >= 0)
10781 {
10782 int h_margin;
10783 int text_area_width;
10784 struct glyph_row *current_cursor_row
10785 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
10786 struct glyph_row *desired_cursor_row
10787 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
10788 struct glyph_row *cursor_row
10789 = (desired_cursor_row->enabled_p
10790 ? desired_cursor_row
10791 : current_cursor_row);
10792
10793 text_area_width = window_box_width (w, TEXT_AREA);
10794
10795 /* Scroll when cursor is inside this scroll margin. */
10796 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
10797
10798 if (!NILP (Fbuffer_local_value (Qauto_hscroll_mode, w->buffer))
10799 && ((XFASTINT (w->hscroll)
10800 && w->cursor.x <= h_margin)
10801 || (cursor_row->enabled_p
10802 && cursor_row->truncated_on_right_p
10803 && (w->cursor.x >= text_area_width - h_margin))))
10804 {
10805 struct it it;
10806 int hscroll;
10807 struct buffer *saved_current_buffer;
10808 int pt;
10809 int wanted_x;
10810
10811 /* Find point in a display of infinite width. */
10812 saved_current_buffer = current_buffer;
10813 current_buffer = XBUFFER (w->buffer);
10814
10815 if (w == XWINDOW (selected_window))
10816 pt = BUF_PT (current_buffer);
10817 else
10818 {
10819 pt = marker_position (w->pointm);
10820 pt = max (BEGV, pt);
10821 pt = min (ZV, pt);
10822 }
10823
10824 /* Move iterator to pt starting at cursor_row->start in
10825 a line with infinite width. */
10826 init_to_row_start (&it, w, cursor_row);
10827 it.last_visible_x = INFINITY;
10828 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
10829 current_buffer = saved_current_buffer;
10830
10831 /* Position cursor in window. */
10832 if (!hscroll_relative_p && hscroll_step_abs == 0)
10833 hscroll = max (0, (it.current_x
10834 - (ITERATOR_AT_END_OF_LINE_P (&it)
10835 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
10836 : (text_area_width / 2))))
10837 / FRAME_COLUMN_WIDTH (it.f);
10838 else if (w->cursor.x >= text_area_width - h_margin)
10839 {
10840 if (hscroll_relative_p)
10841 wanted_x = text_area_width * (1 - hscroll_step_rel)
10842 - h_margin;
10843 else
10844 wanted_x = text_area_width
10845 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
10846 - h_margin;
10847 hscroll
10848 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
10849 }
10850 else
10851 {
10852 if (hscroll_relative_p)
10853 wanted_x = text_area_width * hscroll_step_rel
10854 + h_margin;
10855 else
10856 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
10857 + h_margin;
10858 hscroll
10859 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
10860 }
10861 hscroll = max (hscroll, XFASTINT (w->min_hscroll));
10862
10863 /* Don't call Fset_window_hscroll if value hasn't
10864 changed because it will prevent redisplay
10865 optimizations. */
10866 if (XFASTINT (w->hscroll) != hscroll)
10867 {
10868 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
10869 w->hscroll = make_number (hscroll);
10870 hscrolled_p = 1;
10871 }
10872 }
10873 }
10874
10875 window = w->next;
10876 }
10877
10878 /* Value is non-zero if hscroll of any leaf window has been changed. */
10879 return hscrolled_p;
10880 }
10881
10882
10883 /* Set hscroll so that cursor is visible and not inside horizontal
10884 scroll margins for all windows in the tree rooted at WINDOW. See
10885 also hscroll_window_tree above. Value is non-zero if any window's
10886 hscroll has been changed. If it has, desired matrices on the frame
10887 of WINDOW are cleared. */
10888
10889 static int
10890 hscroll_windows (window)
10891 Lisp_Object window;
10892 {
10893 int hscrolled_p = hscroll_window_tree (window);
10894 if (hscrolled_p)
10895 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
10896 return hscrolled_p;
10897 }
10898
10899
10900 \f
10901 /************************************************************************
10902 Redisplay
10903 ************************************************************************/
10904
10905 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
10906 to a non-zero value. This is sometimes handy to have in a debugger
10907 session. */
10908
10909 #if GLYPH_DEBUG
10910
10911 /* First and last unchanged row for try_window_id. */
10912
10913 int debug_first_unchanged_at_end_vpos;
10914 int debug_last_unchanged_at_beg_vpos;
10915
10916 /* Delta vpos and y. */
10917
10918 int debug_dvpos, debug_dy;
10919
10920 /* Delta in characters and bytes for try_window_id. */
10921
10922 int debug_delta, debug_delta_bytes;
10923
10924 /* Values of window_end_pos and window_end_vpos at the end of
10925 try_window_id. */
10926
10927 EMACS_INT debug_end_pos, debug_end_vpos;
10928
10929 /* Append a string to W->desired_matrix->method. FMT is a printf
10930 format string. A1...A9 are a supplement for a variable-length
10931 argument list. If trace_redisplay_p is non-zero also printf the
10932 resulting string to stderr. */
10933
10934 static void
10935 debug_method_add (w, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9)
10936 struct window *w;
10937 char *fmt;
10938 int a1, a2, a3, a4, a5, a6, a7, a8, a9;
10939 {
10940 char buffer[512];
10941 char *method = w->desired_matrix->method;
10942 int len = strlen (method);
10943 int size = sizeof w->desired_matrix->method;
10944 int remaining = size - len - 1;
10945
10946 sprintf (buffer, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9);
10947 if (len && remaining)
10948 {
10949 method[len] = '|';
10950 --remaining, ++len;
10951 }
10952
10953 strncpy (method + len, buffer, remaining);
10954
10955 if (trace_redisplay_p)
10956 fprintf (stderr, "%p (%s): %s\n",
10957 w,
10958 ((BUFFERP (w->buffer)
10959 && STRINGP (XBUFFER (w->buffer)->name))
10960 ? (char *) SDATA (XBUFFER (w->buffer)->name)
10961 : "no buffer"),
10962 buffer);
10963 }
10964
10965 #endif /* GLYPH_DEBUG */
10966
10967
10968 /* Value is non-zero if all changes in window W, which displays
10969 current_buffer, are in the text between START and END. START is a
10970 buffer position, END is given as a distance from Z. Used in
10971 redisplay_internal for display optimization. */
10972
10973 static INLINE int
10974 text_outside_line_unchanged_p (w, start, end)
10975 struct window *w;
10976 int start, end;
10977 {
10978 int unchanged_p = 1;
10979
10980 /* If text or overlays have changed, see where. */
10981 if (XFASTINT (w->last_modified) < MODIFF
10982 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
10983 {
10984 /* Gap in the line? */
10985 if (GPT < start || Z - GPT < end)
10986 unchanged_p = 0;
10987
10988 /* Changes start in front of the line, or end after it? */
10989 if (unchanged_p
10990 && (BEG_UNCHANGED < start - 1
10991 || END_UNCHANGED < end))
10992 unchanged_p = 0;
10993
10994 /* If selective display, can't optimize if changes start at the
10995 beginning of the line. */
10996 if (unchanged_p
10997 && INTEGERP (current_buffer->selective_display)
10998 && XINT (current_buffer->selective_display) > 0
10999 && (BEG_UNCHANGED < start || GPT <= start))
11000 unchanged_p = 0;
11001
11002 /* If there are overlays at the start or end of the line, these
11003 may have overlay strings with newlines in them. A change at
11004 START, for instance, may actually concern the display of such
11005 overlay strings as well, and they are displayed on different
11006 lines. So, quickly rule out this case. (For the future, it
11007 might be desirable to implement something more telling than
11008 just BEG/END_UNCHANGED.) */
11009 if (unchanged_p)
11010 {
11011 if (BEG + BEG_UNCHANGED == start
11012 && overlay_touches_p (start))
11013 unchanged_p = 0;
11014 if (END_UNCHANGED == end
11015 && overlay_touches_p (Z - end))
11016 unchanged_p = 0;
11017 }
11018 }
11019
11020 return unchanged_p;
11021 }
11022
11023
11024 /* Do a frame update, taking possible shortcuts into account. This is
11025 the main external entry point for redisplay.
11026
11027 If the last redisplay displayed an echo area message and that message
11028 is no longer requested, we clear the echo area or bring back the
11029 mini-buffer if that is in use. */
11030
11031 void
11032 redisplay ()
11033 {
11034 redisplay_internal (0);
11035 }
11036
11037
11038 static Lisp_Object
11039 overlay_arrow_string_or_property (var)
11040 Lisp_Object var;
11041 {
11042 Lisp_Object val;
11043
11044 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
11045 return val;
11046
11047 return Voverlay_arrow_string;
11048 }
11049
11050 /* Return 1 if there are any overlay-arrows in current_buffer. */
11051 static int
11052 overlay_arrow_in_current_buffer_p ()
11053 {
11054 Lisp_Object vlist;
11055
11056 for (vlist = Voverlay_arrow_variable_list;
11057 CONSP (vlist);
11058 vlist = XCDR (vlist))
11059 {
11060 Lisp_Object var = XCAR (vlist);
11061 Lisp_Object val;
11062
11063 if (!SYMBOLP (var))
11064 continue;
11065 val = find_symbol_value (var);
11066 if (MARKERP (val)
11067 && current_buffer == XMARKER (val)->buffer)
11068 return 1;
11069 }
11070 return 0;
11071 }
11072
11073
11074 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
11075 has changed. */
11076
11077 static int
11078 overlay_arrows_changed_p ()
11079 {
11080 Lisp_Object vlist;
11081
11082 for (vlist = Voverlay_arrow_variable_list;
11083 CONSP (vlist);
11084 vlist = XCDR (vlist))
11085 {
11086 Lisp_Object var = XCAR (vlist);
11087 Lisp_Object val, pstr;
11088
11089 if (!SYMBOLP (var))
11090 continue;
11091 val = find_symbol_value (var);
11092 if (!MARKERP (val))
11093 continue;
11094 if (! EQ (COERCE_MARKER (val),
11095 Fget (var, Qlast_arrow_position))
11096 || ! (pstr = overlay_arrow_string_or_property (var),
11097 EQ (pstr, Fget (var, Qlast_arrow_string))))
11098 return 1;
11099 }
11100 return 0;
11101 }
11102
11103 /* Mark overlay arrows to be updated on next redisplay. */
11104
11105 static void
11106 update_overlay_arrows (up_to_date)
11107 int up_to_date;
11108 {
11109 Lisp_Object vlist;
11110
11111 for (vlist = Voverlay_arrow_variable_list;
11112 CONSP (vlist);
11113 vlist = XCDR (vlist))
11114 {
11115 Lisp_Object var = XCAR (vlist);
11116
11117 if (!SYMBOLP (var))
11118 continue;
11119
11120 if (up_to_date > 0)
11121 {
11122 Lisp_Object val = find_symbol_value (var);
11123 Fput (var, Qlast_arrow_position,
11124 COERCE_MARKER (val));
11125 Fput (var, Qlast_arrow_string,
11126 overlay_arrow_string_or_property (var));
11127 }
11128 else if (up_to_date < 0
11129 || !NILP (Fget (var, Qlast_arrow_position)))
11130 {
11131 Fput (var, Qlast_arrow_position, Qt);
11132 Fput (var, Qlast_arrow_string, Qt);
11133 }
11134 }
11135 }
11136
11137
11138 /* Return overlay arrow string to display at row.
11139 Return integer (bitmap number) for arrow bitmap in left fringe.
11140 Return nil if no overlay arrow. */
11141
11142 static Lisp_Object
11143 overlay_arrow_at_row (it, row)
11144 struct it *it;
11145 struct glyph_row *row;
11146 {
11147 Lisp_Object vlist;
11148
11149 for (vlist = Voverlay_arrow_variable_list;
11150 CONSP (vlist);
11151 vlist = XCDR (vlist))
11152 {
11153 Lisp_Object var = XCAR (vlist);
11154 Lisp_Object val;
11155
11156 if (!SYMBOLP (var))
11157 continue;
11158
11159 val = find_symbol_value (var);
11160
11161 if (MARKERP (val)
11162 && current_buffer == XMARKER (val)->buffer
11163 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
11164 {
11165 if (FRAME_WINDOW_P (it->f)
11166 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
11167 {
11168 #ifdef HAVE_WINDOW_SYSTEM
11169 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
11170 {
11171 int fringe_bitmap;
11172 if ((fringe_bitmap = lookup_fringe_bitmap (val)) != 0)
11173 return make_number (fringe_bitmap);
11174 }
11175 #endif
11176 return make_number (-1); /* Use default arrow bitmap */
11177 }
11178 return overlay_arrow_string_or_property (var);
11179 }
11180 }
11181
11182 return Qnil;
11183 }
11184
11185 /* Return 1 if point moved out of or into a composition. Otherwise
11186 return 0. PREV_BUF and PREV_PT are the last point buffer and
11187 position. BUF and PT are the current point buffer and position. */
11188
11189 int
11190 check_point_in_composition (prev_buf, prev_pt, buf, pt)
11191 struct buffer *prev_buf, *buf;
11192 int prev_pt, pt;
11193 {
11194 EMACS_INT start, end;
11195 Lisp_Object prop;
11196 Lisp_Object buffer;
11197
11198 XSETBUFFER (buffer, buf);
11199 /* Check a composition at the last point if point moved within the
11200 same buffer. */
11201 if (prev_buf == buf)
11202 {
11203 if (prev_pt == pt)
11204 /* Point didn't move. */
11205 return 0;
11206
11207 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
11208 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
11209 && COMPOSITION_VALID_P (start, end, prop)
11210 && start < prev_pt && end > prev_pt)
11211 /* The last point was within the composition. Return 1 iff
11212 point moved out of the composition. */
11213 return (pt <= start || pt >= end);
11214 }
11215
11216 /* Check a composition at the current point. */
11217 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
11218 && find_composition (pt, -1, &start, &end, &prop, buffer)
11219 && COMPOSITION_VALID_P (start, end, prop)
11220 && start < pt && end > pt);
11221 }
11222
11223
11224 /* Reconsider the setting of B->clip_changed which is displayed
11225 in window W. */
11226
11227 static INLINE void
11228 reconsider_clip_changes (w, b)
11229 struct window *w;
11230 struct buffer *b;
11231 {
11232 if (b->clip_changed
11233 && !NILP (w->window_end_valid)
11234 && w->current_matrix->buffer == b
11235 && w->current_matrix->zv == BUF_ZV (b)
11236 && w->current_matrix->begv == BUF_BEGV (b))
11237 b->clip_changed = 0;
11238
11239 /* If display wasn't paused, and W is not a tool bar window, see if
11240 point has been moved into or out of a composition. In that case,
11241 we set b->clip_changed to 1 to force updating the screen. If
11242 b->clip_changed has already been set to 1, we can skip this
11243 check. */
11244 if (!b->clip_changed
11245 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
11246 {
11247 int pt;
11248
11249 if (w == XWINDOW (selected_window))
11250 pt = BUF_PT (current_buffer);
11251 else
11252 pt = marker_position (w->pointm);
11253
11254 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
11255 || pt != XINT (w->last_point))
11256 && check_point_in_composition (w->current_matrix->buffer,
11257 XINT (w->last_point),
11258 XBUFFER (w->buffer), pt))
11259 b->clip_changed = 1;
11260 }
11261 }
11262 \f
11263
11264 /* Select FRAME to forward the values of frame-local variables into C
11265 variables so that the redisplay routines can access those values
11266 directly. */
11267
11268 static void
11269 select_frame_for_redisplay (frame)
11270 Lisp_Object frame;
11271 {
11272 Lisp_Object tail, symbol, val;
11273 Lisp_Object old = selected_frame;
11274 struct Lisp_Symbol *sym;
11275
11276 xassert (FRAMEP (frame) && FRAME_LIVE_P (XFRAME (frame)));
11277
11278 selected_frame = frame;
11279
11280 do
11281 {
11282 for (tail = XFRAME (frame)->param_alist; CONSP (tail); tail = XCDR (tail))
11283 if (CONSP (XCAR (tail))
11284 && (symbol = XCAR (XCAR (tail)),
11285 SYMBOLP (symbol))
11286 && (sym = indirect_variable (XSYMBOL (symbol)),
11287 val = sym->value,
11288 (BUFFER_LOCAL_VALUEP (val)))
11289 && XBUFFER_LOCAL_VALUE (val)->check_frame)
11290 /* Use find_symbol_value rather than Fsymbol_value
11291 to avoid an error if it is void. */
11292 find_symbol_value (symbol);
11293 } while (!EQ (frame, old) && (frame = old, 1));
11294 }
11295
11296
11297 #define STOP_POLLING \
11298 do { if (! polling_stopped_here) stop_polling (); \
11299 polling_stopped_here = 1; } while (0)
11300
11301 #define RESUME_POLLING \
11302 do { if (polling_stopped_here) start_polling (); \
11303 polling_stopped_here = 0; } while (0)
11304
11305
11306 /* If PRESERVE_ECHO_AREA is nonzero, it means this redisplay is not in
11307 response to any user action; therefore, we should preserve the echo
11308 area. (Actually, our caller does that job.) Perhaps in the future
11309 avoid recentering windows if it is not necessary; currently that
11310 causes some problems. */
11311
11312 static void
11313 redisplay_internal (preserve_echo_area)
11314 int preserve_echo_area;
11315 {
11316 struct window *w = XWINDOW (selected_window);
11317 struct frame *f;
11318 int pause;
11319 int must_finish = 0;
11320 struct text_pos tlbufpos, tlendpos;
11321 int number_of_visible_frames;
11322 int count, count1;
11323 struct frame *sf;
11324 int polling_stopped_here = 0;
11325 Lisp_Object old_frame = selected_frame;
11326
11327 /* Non-zero means redisplay has to consider all windows on all
11328 frames. Zero means, only selected_window is considered. */
11329 int consider_all_windows_p;
11330
11331 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
11332
11333 /* No redisplay if running in batch mode or frame is not yet fully
11334 initialized, or redisplay is explicitly turned off by setting
11335 Vinhibit_redisplay. */
11336 if (noninteractive
11337 || !NILP (Vinhibit_redisplay))
11338 return;
11339
11340 /* Don't examine these until after testing Vinhibit_redisplay.
11341 When Emacs is shutting down, perhaps because its connection to
11342 X has dropped, we should not look at them at all. */
11343 f = XFRAME (w->frame);
11344 sf = SELECTED_FRAME ();
11345
11346 if (!f->glyphs_initialized_p)
11347 return;
11348
11349 /* The flag redisplay_performed_directly_p is set by
11350 direct_output_for_insert when it already did the whole screen
11351 update necessary. */
11352 if (redisplay_performed_directly_p)
11353 {
11354 redisplay_performed_directly_p = 0;
11355 if (!hscroll_windows (selected_window))
11356 return;
11357 }
11358
11359 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS)
11360 if (popup_activated ())
11361 return;
11362 #endif
11363
11364 /* I don't think this happens but let's be paranoid. */
11365 if (redisplaying_p)
11366 return;
11367
11368 /* Record a function that resets redisplaying_p to its old value
11369 when we leave this function. */
11370 count = SPECPDL_INDEX ();
11371 record_unwind_protect (unwind_redisplay,
11372 Fcons (make_number (redisplaying_p), selected_frame));
11373 ++redisplaying_p;
11374 specbind (Qinhibit_free_realized_faces, Qnil);
11375
11376 {
11377 Lisp_Object tail, frame;
11378
11379 FOR_EACH_FRAME (tail, frame)
11380 {
11381 struct frame *f = XFRAME (frame);
11382 f->already_hscrolled_p = 0;
11383 }
11384 }
11385
11386 retry:
11387 if (!EQ (old_frame, selected_frame)
11388 && FRAME_LIVE_P (XFRAME (old_frame)))
11389 /* When running redisplay, we play a bit fast-and-loose and allow e.g.
11390 selected_frame and selected_window to be temporarily out-of-sync so
11391 when we come back here via `goto retry', we need to resync because we
11392 may need to run Elisp code (via prepare_menu_bars). */
11393 select_frame_for_redisplay (old_frame);
11394
11395 pause = 0;
11396 reconsider_clip_changes (w, current_buffer);
11397 last_escape_glyph_frame = NULL;
11398 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
11399
11400 /* If new fonts have been loaded that make a glyph matrix adjustment
11401 necessary, do it. */
11402 if (fonts_changed_p)
11403 {
11404 adjust_glyphs (NULL);
11405 ++windows_or_buffers_changed;
11406 fonts_changed_p = 0;
11407 }
11408
11409 /* If face_change_count is non-zero, init_iterator will free all
11410 realized faces, which includes the faces referenced from current
11411 matrices. So, we can't reuse current matrices in this case. */
11412 if (face_change_count)
11413 ++windows_or_buffers_changed;
11414
11415 if (FRAME_TERMCAP_P (sf)
11416 && FRAME_TTY (sf)->previous_frame != sf)
11417 {
11418 /* Since frames on a single ASCII terminal share the same
11419 display area, displaying a different frame means redisplay
11420 the whole thing. */
11421 windows_or_buffers_changed++;
11422 SET_FRAME_GARBAGED (sf);
11423 #ifndef DOS_NT
11424 set_tty_color_mode (FRAME_TTY (sf), sf);
11425 #endif
11426 FRAME_TTY (sf)->previous_frame = sf;
11427 }
11428
11429 /* Set the visible flags for all frames. Do this before checking
11430 for resized or garbaged frames; they want to know if their frames
11431 are visible. See the comment in frame.h for
11432 FRAME_SAMPLE_VISIBILITY. */
11433 {
11434 Lisp_Object tail, frame;
11435
11436 number_of_visible_frames = 0;
11437
11438 FOR_EACH_FRAME (tail, frame)
11439 {
11440 struct frame *f = XFRAME (frame);
11441
11442 FRAME_SAMPLE_VISIBILITY (f);
11443 if (FRAME_VISIBLE_P (f))
11444 ++number_of_visible_frames;
11445 clear_desired_matrices (f);
11446 }
11447 }
11448
11449
11450 /* Notice any pending interrupt request to change frame size. */
11451 do_pending_window_change (1);
11452
11453 /* Clear frames marked as garbaged. */
11454 if (frame_garbaged)
11455 clear_garbaged_frames ();
11456
11457 /* Build menubar and tool-bar items. */
11458 if (NILP (Vmemory_full))
11459 prepare_menu_bars ();
11460
11461 if (windows_or_buffers_changed)
11462 update_mode_lines++;
11463
11464 /* Detect case that we need to write or remove a star in the mode line. */
11465 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
11466 {
11467 w->update_mode_line = Qt;
11468 if (buffer_shared > 1)
11469 update_mode_lines++;
11470 }
11471
11472 /* Avoid invocation of point motion hooks by `current_column' below. */
11473 count1 = SPECPDL_INDEX ();
11474 specbind (Qinhibit_point_motion_hooks, Qt);
11475
11476 /* If %c is in the mode line, update it if needed. */
11477 if (!NILP (w->column_number_displayed)
11478 /* This alternative quickly identifies a common case
11479 where no change is needed. */
11480 && !(PT == XFASTINT (w->last_point)
11481 && XFASTINT (w->last_modified) >= MODIFF
11482 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
11483 && (XFASTINT (w->column_number_displayed)
11484 != (int) current_column ())) /* iftc */
11485 w->update_mode_line = Qt;
11486
11487 unbind_to (count1, Qnil);
11488
11489 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
11490
11491 /* The variable buffer_shared is set in redisplay_window and
11492 indicates that we redisplay a buffer in different windows. See
11493 there. */
11494 consider_all_windows_p = (update_mode_lines || buffer_shared > 1
11495 || cursor_type_changed);
11496
11497 /* If specs for an arrow have changed, do thorough redisplay
11498 to ensure we remove any arrow that should no longer exist. */
11499 if (overlay_arrows_changed_p ())
11500 consider_all_windows_p = windows_or_buffers_changed = 1;
11501
11502 /* Normally the message* functions will have already displayed and
11503 updated the echo area, but the frame may have been trashed, or
11504 the update may have been preempted, so display the echo area
11505 again here. Checking message_cleared_p captures the case that
11506 the echo area should be cleared. */
11507 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
11508 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
11509 || (message_cleared_p
11510 && minibuf_level == 0
11511 /* If the mini-window is currently selected, this means the
11512 echo-area doesn't show through. */
11513 && !MINI_WINDOW_P (XWINDOW (selected_window))))
11514 {
11515 int window_height_changed_p = echo_area_display (0);
11516 must_finish = 1;
11517
11518 /* If we don't display the current message, don't clear the
11519 message_cleared_p flag, because, if we did, we wouldn't clear
11520 the echo area in the next redisplay which doesn't preserve
11521 the echo area. */
11522 if (!display_last_displayed_message_p)
11523 message_cleared_p = 0;
11524
11525 if (fonts_changed_p)
11526 goto retry;
11527 else if (window_height_changed_p)
11528 {
11529 consider_all_windows_p = 1;
11530 ++update_mode_lines;
11531 ++windows_or_buffers_changed;
11532
11533 /* If window configuration was changed, frames may have been
11534 marked garbaged. Clear them or we will experience
11535 surprises wrt scrolling. */
11536 if (frame_garbaged)
11537 clear_garbaged_frames ();
11538 }
11539 }
11540 else if (EQ (selected_window, minibuf_window)
11541 && (current_buffer->clip_changed
11542 || XFASTINT (w->last_modified) < MODIFF
11543 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
11544 && resize_mini_window (w, 0))
11545 {
11546 /* Resized active mini-window to fit the size of what it is
11547 showing if its contents might have changed. */
11548 must_finish = 1;
11549 /* FIXME: this causes all frames to be updated, which seems unnecessary
11550 since only the current frame needs to be considered. This function needs
11551 to be rewritten with two variables, consider_all_windows and
11552 consider_all_frames. */
11553 consider_all_windows_p = 1;
11554 ++windows_or_buffers_changed;
11555 ++update_mode_lines;
11556
11557 /* If window configuration was changed, frames may have been
11558 marked garbaged. Clear them or we will experience
11559 surprises wrt scrolling. */
11560 if (frame_garbaged)
11561 clear_garbaged_frames ();
11562 }
11563
11564
11565 /* If showing the region, and mark has changed, we must redisplay
11566 the whole window. The assignment to this_line_start_pos prevents
11567 the optimization directly below this if-statement. */
11568 if (((!NILP (Vtransient_mark_mode)
11569 && !NILP (XBUFFER (w->buffer)->mark_active))
11570 != !NILP (w->region_showing))
11571 || (!NILP (w->region_showing)
11572 && !EQ (w->region_showing,
11573 Fmarker_position (XBUFFER (w->buffer)->mark))))
11574 CHARPOS (this_line_start_pos) = 0;
11575
11576 /* Optimize the case that only the line containing the cursor in the
11577 selected window has changed. Variables starting with this_ are
11578 set in display_line and record information about the line
11579 containing the cursor. */
11580 tlbufpos = this_line_start_pos;
11581 tlendpos = this_line_end_pos;
11582 if (!consider_all_windows_p
11583 && CHARPOS (tlbufpos) > 0
11584 && NILP (w->update_mode_line)
11585 && !current_buffer->clip_changed
11586 && !current_buffer->prevent_redisplay_optimizations_p
11587 && FRAME_VISIBLE_P (XFRAME (w->frame))
11588 && !FRAME_OBSCURED_P (XFRAME (w->frame))
11589 /* Make sure recorded data applies to current buffer, etc. */
11590 && this_line_buffer == current_buffer
11591 && current_buffer == XBUFFER (w->buffer)
11592 && NILP (w->force_start)
11593 && NILP (w->optional_new_start)
11594 /* Point must be on the line that we have info recorded about. */
11595 && PT >= CHARPOS (tlbufpos)
11596 && PT <= Z - CHARPOS (tlendpos)
11597 /* All text outside that line, including its final newline,
11598 must be unchanged */
11599 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
11600 CHARPOS (tlendpos)))
11601 {
11602 if (CHARPOS (tlbufpos) > BEGV
11603 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
11604 && (CHARPOS (tlbufpos) == ZV
11605 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
11606 /* Former continuation line has disappeared by becoming empty */
11607 goto cancel;
11608 else if (XFASTINT (w->last_modified) < MODIFF
11609 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
11610 || MINI_WINDOW_P (w))
11611 {
11612 /* We have to handle the case of continuation around a
11613 wide-column character (See the comment in indent.c around
11614 line 885).
11615
11616 For instance, in the following case:
11617
11618 -------- Insert --------
11619 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
11620 J_I_ ==> J_I_ `^^' are cursors.
11621 ^^ ^^
11622 -------- --------
11623
11624 As we have to redraw the line above, we should goto cancel. */
11625
11626 struct it it;
11627 int line_height_before = this_line_pixel_height;
11628
11629 /* Note that start_display will handle the case that the
11630 line starting at tlbufpos is a continuation lines. */
11631 start_display (&it, w, tlbufpos);
11632
11633 /* Implementation note: It this still necessary? */
11634 if (it.current_x != this_line_start_x)
11635 goto cancel;
11636
11637 TRACE ((stderr, "trying display optimization 1\n"));
11638 w->cursor.vpos = -1;
11639 overlay_arrow_seen = 0;
11640 it.vpos = this_line_vpos;
11641 it.current_y = this_line_y;
11642 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
11643 display_line (&it);
11644
11645 /* If line contains point, is not continued,
11646 and ends at same distance from eob as before, we win */
11647 if (w->cursor.vpos >= 0
11648 /* Line is not continued, otherwise this_line_start_pos
11649 would have been set to 0 in display_line. */
11650 && CHARPOS (this_line_start_pos)
11651 /* Line ends as before. */
11652 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
11653 /* Line has same height as before. Otherwise other lines
11654 would have to be shifted up or down. */
11655 && this_line_pixel_height == line_height_before)
11656 {
11657 /* If this is not the window's last line, we must adjust
11658 the charstarts of the lines below. */
11659 if (it.current_y < it.last_visible_y)
11660 {
11661 struct glyph_row *row
11662 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
11663 int delta, delta_bytes;
11664
11665 if (Z - CHARPOS (tlendpos) == ZV)
11666 {
11667 /* This line ends at end of (accessible part of)
11668 buffer. There is no newline to count. */
11669 delta = (Z
11670 - CHARPOS (tlendpos)
11671 - MATRIX_ROW_START_CHARPOS (row));
11672 delta_bytes = (Z_BYTE
11673 - BYTEPOS (tlendpos)
11674 - MATRIX_ROW_START_BYTEPOS (row));
11675 }
11676 else
11677 {
11678 /* This line ends in a newline. Must take
11679 account of the newline and the rest of the
11680 text that follows. */
11681 delta = (Z
11682 - CHARPOS (tlendpos)
11683 - MATRIX_ROW_START_CHARPOS (row));
11684 delta_bytes = (Z_BYTE
11685 - BYTEPOS (tlendpos)
11686 - MATRIX_ROW_START_BYTEPOS (row));
11687 }
11688
11689 increment_matrix_positions (w->current_matrix,
11690 this_line_vpos + 1,
11691 w->current_matrix->nrows,
11692 delta, delta_bytes);
11693 }
11694
11695 /* If this row displays text now but previously didn't,
11696 or vice versa, w->window_end_vpos may have to be
11697 adjusted. */
11698 if ((it.glyph_row - 1)->displays_text_p)
11699 {
11700 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
11701 XSETINT (w->window_end_vpos, this_line_vpos);
11702 }
11703 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
11704 && this_line_vpos > 0)
11705 XSETINT (w->window_end_vpos, this_line_vpos - 1);
11706 w->window_end_valid = Qnil;
11707
11708 /* Update hint: No need to try to scroll in update_window. */
11709 w->desired_matrix->no_scrolling_p = 1;
11710
11711 #if GLYPH_DEBUG
11712 *w->desired_matrix->method = 0;
11713 debug_method_add (w, "optimization 1");
11714 #endif
11715 #ifdef HAVE_WINDOW_SYSTEM
11716 update_window_fringes (w, 0);
11717 #endif
11718 goto update;
11719 }
11720 else
11721 goto cancel;
11722 }
11723 else if (/* Cursor position hasn't changed. */
11724 PT == XFASTINT (w->last_point)
11725 /* Make sure the cursor was last displayed
11726 in this window. Otherwise we have to reposition it. */
11727 && 0 <= w->cursor.vpos
11728 && WINDOW_TOTAL_LINES (w) > w->cursor.vpos)
11729 {
11730 if (!must_finish)
11731 {
11732 do_pending_window_change (1);
11733
11734 /* We used to always goto end_of_redisplay here, but this
11735 isn't enough if we have a blinking cursor. */
11736 if (w->cursor_off_p == w->last_cursor_off_p)
11737 goto end_of_redisplay;
11738 }
11739 goto update;
11740 }
11741 /* If highlighting the region, or if the cursor is in the echo area,
11742 then we can't just move the cursor. */
11743 else if (! (!NILP (Vtransient_mark_mode)
11744 && !NILP (current_buffer->mark_active))
11745 && (EQ (selected_window, current_buffer->last_selected_window)
11746 || highlight_nonselected_windows)
11747 && NILP (w->region_showing)
11748 && NILP (Vshow_trailing_whitespace)
11749 && !cursor_in_echo_area)
11750 {
11751 struct it it;
11752 struct glyph_row *row;
11753
11754 /* Skip from tlbufpos to PT and see where it is. Note that
11755 PT may be in invisible text. If so, we will end at the
11756 next visible position. */
11757 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
11758 NULL, DEFAULT_FACE_ID);
11759 it.current_x = this_line_start_x;
11760 it.current_y = this_line_y;
11761 it.vpos = this_line_vpos;
11762
11763 /* The call to move_it_to stops in front of PT, but
11764 moves over before-strings. */
11765 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
11766
11767 if (it.vpos == this_line_vpos
11768 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
11769 row->enabled_p))
11770 {
11771 xassert (this_line_vpos == it.vpos);
11772 xassert (this_line_y == it.current_y);
11773 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
11774 #if GLYPH_DEBUG
11775 *w->desired_matrix->method = 0;
11776 debug_method_add (w, "optimization 3");
11777 #endif
11778 goto update;
11779 }
11780 else
11781 goto cancel;
11782 }
11783
11784 cancel:
11785 /* Text changed drastically or point moved off of line. */
11786 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
11787 }
11788
11789 CHARPOS (this_line_start_pos) = 0;
11790 consider_all_windows_p |= buffer_shared > 1;
11791 ++clear_face_cache_count;
11792 #ifdef HAVE_WINDOW_SYSTEM
11793 ++clear_image_cache_count;
11794 #endif
11795
11796 /* Build desired matrices, and update the display. If
11797 consider_all_windows_p is non-zero, do it for all windows on all
11798 frames. Otherwise do it for selected_window, only. */
11799
11800 if (consider_all_windows_p)
11801 {
11802 Lisp_Object tail, frame;
11803
11804 FOR_EACH_FRAME (tail, frame)
11805 XFRAME (frame)->updated_p = 0;
11806
11807 /* Recompute # windows showing selected buffer. This will be
11808 incremented each time such a window is displayed. */
11809 buffer_shared = 0;
11810
11811 FOR_EACH_FRAME (tail, frame)
11812 {
11813 struct frame *f = XFRAME (frame);
11814
11815 if (FRAME_WINDOW_P (f) || FRAME_TERMCAP_P (f) || f == sf)
11816 {
11817 if (! EQ (frame, selected_frame))
11818 /* Select the frame, for the sake of frame-local
11819 variables. */
11820 select_frame_for_redisplay (frame);
11821
11822 /* Mark all the scroll bars to be removed; we'll redeem
11823 the ones we want when we redisplay their windows. */
11824 if (FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
11825 FRAME_TERMINAL (f)->condemn_scroll_bars_hook (f);
11826
11827 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
11828 redisplay_windows (FRAME_ROOT_WINDOW (f));
11829
11830 /* Any scroll bars which redisplay_windows should have
11831 nuked should now go away. */
11832 if (FRAME_TERMINAL (f)->judge_scroll_bars_hook)
11833 FRAME_TERMINAL (f)->judge_scroll_bars_hook (f);
11834
11835 /* If fonts changed, display again. */
11836 /* ??? rms: I suspect it is a mistake to jump all the way
11837 back to retry here. It should just retry this frame. */
11838 if (fonts_changed_p)
11839 goto retry;
11840
11841 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
11842 {
11843 /* See if we have to hscroll. */
11844 if (!f->already_hscrolled_p)
11845 {
11846 f->already_hscrolled_p = 1;
11847 if (hscroll_windows (f->root_window))
11848 goto retry;
11849 }
11850
11851 /* Prevent various kinds of signals during display
11852 update. stdio is not robust about handling
11853 signals, which can cause an apparent I/O
11854 error. */
11855 if (interrupt_input)
11856 unrequest_sigio ();
11857 STOP_POLLING;
11858
11859 /* Update the display. */
11860 set_window_update_flags (XWINDOW (f->root_window), 1);
11861 pause |= update_frame (f, 0, 0);
11862 f->updated_p = 1;
11863 }
11864 }
11865 }
11866
11867 if (!EQ (old_frame, selected_frame)
11868 && FRAME_LIVE_P (XFRAME (old_frame)))
11869 /* We played a bit fast-and-loose above and allowed selected_frame
11870 and selected_window to be temporarily out-of-sync but let's make
11871 sure this stays contained. */
11872 select_frame_for_redisplay (old_frame);
11873 eassert (EQ (XFRAME (selected_frame)->selected_window, selected_window));
11874
11875 if (!pause)
11876 {
11877 /* Do the mark_window_display_accurate after all windows have
11878 been redisplayed because this call resets flags in buffers
11879 which are needed for proper redisplay. */
11880 FOR_EACH_FRAME (tail, frame)
11881 {
11882 struct frame *f = XFRAME (frame);
11883 if (f->updated_p)
11884 {
11885 mark_window_display_accurate (f->root_window, 1);
11886 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
11887 FRAME_TERMINAL (f)->frame_up_to_date_hook (f);
11888 }
11889 }
11890 }
11891 }
11892 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
11893 {
11894 Lisp_Object mini_window;
11895 struct frame *mini_frame;
11896
11897 displayed_buffer = XBUFFER (XWINDOW (selected_window)->buffer);
11898 /* Use list_of_error, not Qerror, so that
11899 we catch only errors and don't run the debugger. */
11900 internal_condition_case_1 (redisplay_window_1, selected_window,
11901 list_of_error,
11902 redisplay_window_error);
11903
11904 /* Compare desired and current matrices, perform output. */
11905
11906 update:
11907 /* If fonts changed, display again. */
11908 if (fonts_changed_p)
11909 goto retry;
11910
11911 /* Prevent various kinds of signals during display update.
11912 stdio is not robust about handling signals,
11913 which can cause an apparent I/O error. */
11914 if (interrupt_input)
11915 unrequest_sigio ();
11916 STOP_POLLING;
11917
11918 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
11919 {
11920 if (hscroll_windows (selected_window))
11921 goto retry;
11922
11923 XWINDOW (selected_window)->must_be_updated_p = 1;
11924 pause = update_frame (sf, 0, 0);
11925 }
11926
11927 /* We may have called echo_area_display at the top of this
11928 function. If the echo area is on another frame, that may
11929 have put text on a frame other than the selected one, so the
11930 above call to update_frame would not have caught it. Catch
11931 it here. */
11932 mini_window = FRAME_MINIBUF_WINDOW (sf);
11933 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
11934
11935 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
11936 {
11937 XWINDOW (mini_window)->must_be_updated_p = 1;
11938 pause |= update_frame (mini_frame, 0, 0);
11939 if (!pause && hscroll_windows (mini_window))
11940 goto retry;
11941 }
11942 }
11943
11944 /* If display was paused because of pending input, make sure we do a
11945 thorough update the next time. */
11946 if (pause)
11947 {
11948 /* Prevent the optimization at the beginning of
11949 redisplay_internal that tries a single-line update of the
11950 line containing the cursor in the selected window. */
11951 CHARPOS (this_line_start_pos) = 0;
11952
11953 /* Let the overlay arrow be updated the next time. */
11954 update_overlay_arrows (0);
11955
11956 /* If we pause after scrolling, some rows in the current
11957 matrices of some windows are not valid. */
11958 if (!WINDOW_FULL_WIDTH_P (w)
11959 && !FRAME_WINDOW_P (XFRAME (w->frame)))
11960 update_mode_lines = 1;
11961 }
11962 else
11963 {
11964 if (!consider_all_windows_p)
11965 {
11966 /* This has already been done above if
11967 consider_all_windows_p is set. */
11968 mark_window_display_accurate_1 (w, 1);
11969
11970 /* Say overlay arrows are up to date. */
11971 update_overlay_arrows (1);
11972
11973 if (FRAME_TERMINAL (sf)->frame_up_to_date_hook != 0)
11974 FRAME_TERMINAL (sf)->frame_up_to_date_hook (sf);
11975 }
11976
11977 update_mode_lines = 0;
11978 windows_or_buffers_changed = 0;
11979 cursor_type_changed = 0;
11980 }
11981
11982 /* Start SIGIO interrupts coming again. Having them off during the
11983 code above makes it less likely one will discard output, but not
11984 impossible, since there might be stuff in the system buffer here.
11985 But it is much hairier to try to do anything about that. */
11986 if (interrupt_input)
11987 request_sigio ();
11988 RESUME_POLLING;
11989
11990 /* If a frame has become visible which was not before, redisplay
11991 again, so that we display it. Expose events for such a frame
11992 (which it gets when becoming visible) don't call the parts of
11993 redisplay constructing glyphs, so simply exposing a frame won't
11994 display anything in this case. So, we have to display these
11995 frames here explicitly. */
11996 if (!pause)
11997 {
11998 Lisp_Object tail, frame;
11999 int new_count = 0;
12000
12001 FOR_EACH_FRAME (tail, frame)
12002 {
12003 int this_is_visible = 0;
12004
12005 if (XFRAME (frame)->visible)
12006 this_is_visible = 1;
12007 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
12008 if (XFRAME (frame)->visible)
12009 this_is_visible = 1;
12010
12011 if (this_is_visible)
12012 new_count++;
12013 }
12014
12015 if (new_count != number_of_visible_frames)
12016 windows_or_buffers_changed++;
12017 }
12018
12019 /* Change frame size now if a change is pending. */
12020 do_pending_window_change (1);
12021
12022 /* If we just did a pending size change, or have additional
12023 visible frames, redisplay again. */
12024 if (windows_or_buffers_changed && !pause)
12025 goto retry;
12026
12027 /* Clear the face cache eventually. */
12028 if (consider_all_windows_p)
12029 {
12030 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
12031 {
12032 clear_face_cache (0);
12033 clear_face_cache_count = 0;
12034 }
12035 #ifdef HAVE_WINDOW_SYSTEM
12036 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
12037 {
12038 clear_image_caches (Qnil);
12039 clear_image_cache_count = 0;
12040 }
12041 #endif /* HAVE_WINDOW_SYSTEM */
12042 }
12043
12044 end_of_redisplay:
12045 unbind_to (count, Qnil);
12046 RESUME_POLLING;
12047 }
12048
12049
12050 /* Redisplay, but leave alone any recent echo area message unless
12051 another message has been requested in its place.
12052
12053 This is useful in situations where you need to redisplay but no
12054 user action has occurred, making it inappropriate for the message
12055 area to be cleared. See tracking_off and
12056 wait_reading_process_output for examples of these situations.
12057
12058 FROM_WHERE is an integer saying from where this function was
12059 called. This is useful for debugging. */
12060
12061 void
12062 redisplay_preserve_echo_area (from_where)
12063 int from_where;
12064 {
12065 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
12066
12067 if (!NILP (echo_area_buffer[1]))
12068 {
12069 /* We have a previously displayed message, but no current
12070 message. Redisplay the previous message. */
12071 display_last_displayed_message_p = 1;
12072 redisplay_internal (1);
12073 display_last_displayed_message_p = 0;
12074 }
12075 else
12076 redisplay_internal (1);
12077
12078 if (FRAME_RIF (SELECTED_FRAME ()) != NULL
12079 && FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
12080 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (NULL);
12081 }
12082
12083
12084 /* Function registered with record_unwind_protect in
12085 redisplay_internal. Reset redisplaying_p to the value it had
12086 before redisplay_internal was called, and clear
12087 prevent_freeing_realized_faces_p. It also selects the previously
12088 selected frame, unless it has been deleted (by an X connection
12089 failure during redisplay, for example). */
12090
12091 static Lisp_Object
12092 unwind_redisplay (val)
12093 Lisp_Object val;
12094 {
12095 Lisp_Object old_redisplaying_p, old_frame;
12096
12097 old_redisplaying_p = XCAR (val);
12098 redisplaying_p = XFASTINT (old_redisplaying_p);
12099 old_frame = XCDR (val);
12100 if (! EQ (old_frame, selected_frame)
12101 && FRAME_LIVE_P (XFRAME (old_frame)))
12102 select_frame_for_redisplay (old_frame);
12103 return Qnil;
12104 }
12105
12106
12107 /* Mark the display of window W as accurate or inaccurate. If
12108 ACCURATE_P is non-zero mark display of W as accurate. If
12109 ACCURATE_P is zero, arrange for W to be redisplayed the next time
12110 redisplay_internal is called. */
12111
12112 static void
12113 mark_window_display_accurate_1 (w, accurate_p)
12114 struct window *w;
12115 int accurate_p;
12116 {
12117 if (BUFFERP (w->buffer))
12118 {
12119 struct buffer *b = XBUFFER (w->buffer);
12120
12121 w->last_modified
12122 = make_number (accurate_p ? BUF_MODIFF (b) : 0);
12123 w->last_overlay_modified
12124 = make_number (accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
12125 w->last_had_star
12126 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b) ? Qt : Qnil;
12127
12128 if (accurate_p)
12129 {
12130 b->clip_changed = 0;
12131 b->prevent_redisplay_optimizations_p = 0;
12132
12133 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
12134 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
12135 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
12136 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
12137
12138 w->current_matrix->buffer = b;
12139 w->current_matrix->begv = BUF_BEGV (b);
12140 w->current_matrix->zv = BUF_ZV (b);
12141
12142 w->last_cursor = w->cursor;
12143 w->last_cursor_off_p = w->cursor_off_p;
12144
12145 if (w == XWINDOW (selected_window))
12146 w->last_point = make_number (BUF_PT (b));
12147 else
12148 w->last_point = make_number (XMARKER (w->pointm)->charpos);
12149 }
12150 }
12151
12152 if (accurate_p)
12153 {
12154 w->window_end_valid = w->buffer;
12155 w->update_mode_line = Qnil;
12156 }
12157 }
12158
12159
12160 /* Mark the display of windows in the window tree rooted at WINDOW as
12161 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
12162 windows as accurate. If ACCURATE_P is zero, arrange for windows to
12163 be redisplayed the next time redisplay_internal is called. */
12164
12165 void
12166 mark_window_display_accurate (window, accurate_p)
12167 Lisp_Object window;
12168 int accurate_p;
12169 {
12170 struct window *w;
12171
12172 for (; !NILP (window); window = w->next)
12173 {
12174 w = XWINDOW (window);
12175 mark_window_display_accurate_1 (w, accurate_p);
12176
12177 if (!NILP (w->vchild))
12178 mark_window_display_accurate (w->vchild, accurate_p);
12179 if (!NILP (w->hchild))
12180 mark_window_display_accurate (w->hchild, accurate_p);
12181 }
12182
12183 if (accurate_p)
12184 {
12185 update_overlay_arrows (1);
12186 }
12187 else
12188 {
12189 /* Force a thorough redisplay the next time by setting
12190 last_arrow_position and last_arrow_string to t, which is
12191 unequal to any useful value of Voverlay_arrow_... */
12192 update_overlay_arrows (-1);
12193 }
12194 }
12195
12196
12197 /* Return value in display table DP (Lisp_Char_Table *) for character
12198 C. Since a display table doesn't have any parent, we don't have to
12199 follow parent. Do not call this function directly but use the
12200 macro DISP_CHAR_VECTOR. */
12201
12202 Lisp_Object
12203 disp_char_vector (dp, c)
12204 struct Lisp_Char_Table *dp;
12205 int c;
12206 {
12207 Lisp_Object val;
12208
12209 if (ASCII_CHAR_P (c))
12210 {
12211 val = dp->ascii;
12212 if (SUB_CHAR_TABLE_P (val))
12213 val = XSUB_CHAR_TABLE (val)->contents[c];
12214 }
12215 else
12216 {
12217 Lisp_Object table;
12218
12219 XSETCHAR_TABLE (table, dp);
12220 val = char_table_ref (table, c);
12221 }
12222 if (NILP (val))
12223 val = dp->defalt;
12224 return val;
12225 }
12226
12227
12228 \f
12229 /***********************************************************************
12230 Window Redisplay
12231 ***********************************************************************/
12232
12233 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
12234
12235 static void
12236 redisplay_windows (window)
12237 Lisp_Object window;
12238 {
12239 while (!NILP (window))
12240 {
12241 struct window *w = XWINDOW (window);
12242
12243 if (!NILP (w->hchild))
12244 redisplay_windows (w->hchild);
12245 else if (!NILP (w->vchild))
12246 redisplay_windows (w->vchild);
12247 else
12248 {
12249 displayed_buffer = XBUFFER (w->buffer);
12250 /* Use list_of_error, not Qerror, so that
12251 we catch only errors and don't run the debugger. */
12252 internal_condition_case_1 (redisplay_window_0, window,
12253 list_of_error,
12254 redisplay_window_error);
12255 }
12256
12257 window = w->next;
12258 }
12259 }
12260
12261 static Lisp_Object
12262 redisplay_window_error ()
12263 {
12264 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
12265 return Qnil;
12266 }
12267
12268 static Lisp_Object
12269 redisplay_window_0 (window)
12270 Lisp_Object window;
12271 {
12272 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
12273 redisplay_window (window, 0);
12274 return Qnil;
12275 }
12276
12277 static Lisp_Object
12278 redisplay_window_1 (window)
12279 Lisp_Object window;
12280 {
12281 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
12282 redisplay_window (window, 1);
12283 return Qnil;
12284 }
12285 \f
12286
12287 /* Increment GLYPH until it reaches END or CONDITION fails while
12288 adding (GLYPH)->pixel_width to X. */
12289
12290 #define SKIP_GLYPHS(glyph, end, x, condition) \
12291 do \
12292 { \
12293 (x) += (glyph)->pixel_width; \
12294 ++(glyph); \
12295 } \
12296 while ((glyph) < (end) && (condition))
12297
12298
12299 /* Set cursor position of W. PT is assumed to be displayed in ROW.
12300 DELTA is the number of bytes by which positions recorded in ROW
12301 differ from current buffer positions.
12302
12303 Return 0 if cursor is not on this row. 1 otherwise. */
12304
12305 int
12306 set_cursor_from_row (w, row, matrix, delta, delta_bytes, dy, dvpos)
12307 struct window *w;
12308 struct glyph_row *row;
12309 struct glyph_matrix *matrix;
12310 int delta, delta_bytes, dy, dvpos;
12311 {
12312 struct glyph *glyph = row->glyphs[TEXT_AREA];
12313 struct glyph *end = glyph + row->used[TEXT_AREA];
12314 struct glyph *cursor = NULL;
12315 /* The first glyph that starts a sequence of glyphs from string. */
12316 struct glyph *string_start;
12317 /* The X coordinate of string_start. */
12318 int string_start_x;
12319 /* The last known character position. */
12320 int last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
12321 /* The last known character position before string_start. */
12322 int string_before_pos;
12323 int x = row->x;
12324 int cursor_x = x;
12325 int cursor_from_overlay_pos = 0;
12326 int pt_old = PT - delta;
12327
12328 /* Skip over glyphs not having an object at the start of the row.
12329 These are special glyphs like truncation marks on terminal
12330 frames. */
12331 if (row->displays_text_p)
12332 while (glyph < end
12333 && INTEGERP (glyph->object)
12334 && glyph->charpos < 0)
12335 {
12336 x += glyph->pixel_width;
12337 ++glyph;
12338 }
12339
12340 string_start = NULL;
12341 while (glyph < end
12342 && !INTEGERP (glyph->object)
12343 && (!BUFFERP (glyph->object)
12344 || (last_pos = glyph->charpos) < pt_old
12345 || glyph->avoid_cursor_p))
12346 {
12347 if (! STRINGP (glyph->object))
12348 {
12349 string_start = NULL;
12350 x += glyph->pixel_width;
12351 ++glyph;
12352 if (cursor_from_overlay_pos
12353 && last_pos >= cursor_from_overlay_pos)
12354 {
12355 cursor_from_overlay_pos = 0;
12356 cursor = 0;
12357 }
12358 }
12359 else
12360 {
12361 if (string_start == NULL)
12362 {
12363 string_before_pos = last_pos;
12364 string_start = glyph;
12365 string_start_x = x;
12366 }
12367 /* Skip all glyphs from string. */
12368 do
12369 {
12370 Lisp_Object cprop;
12371 int pos;
12372 if ((cursor == NULL || glyph > cursor)
12373 && (cprop = Fget_char_property (make_number ((glyph)->charpos),
12374 Qcursor, (glyph)->object),
12375 !NILP (cprop))
12376 && (pos = string_buffer_position (w, glyph->object,
12377 string_before_pos),
12378 (pos == 0 /* From overlay */
12379 || pos == pt_old)))
12380 {
12381 /* Estimate overlay buffer position from the buffer
12382 positions of the glyphs before and after the overlay.
12383 Add 1 to last_pos so that if point corresponds to the
12384 glyph right after the overlay, we still use a 'cursor'
12385 property found in that overlay. */
12386 cursor_from_overlay_pos = (pos ? 0 : last_pos
12387 + (INTEGERP (cprop) ? XINT (cprop) : 0));
12388 cursor = glyph;
12389 cursor_x = x;
12390 }
12391 x += glyph->pixel_width;
12392 ++glyph;
12393 }
12394 while (glyph < end && EQ (glyph->object, string_start->object));
12395 }
12396 }
12397
12398 if (cursor != NULL)
12399 {
12400 glyph = cursor;
12401 x = cursor_x;
12402 }
12403 else if (row->ends_in_ellipsis_p && glyph == end)
12404 {
12405 /* Scan back over the ellipsis glyphs, decrementing positions. */
12406 while (glyph > row->glyphs[TEXT_AREA]
12407 && (glyph - 1)->charpos == last_pos)
12408 glyph--, x -= glyph->pixel_width;
12409 /* That loop always goes one position too far,
12410 including the glyph before the ellipsis.
12411 So scan forward over that one. */
12412 x += glyph->pixel_width;
12413 glyph++;
12414 }
12415 else if (string_start
12416 && (glyph == end || !BUFFERP (glyph->object) || last_pos > pt_old))
12417 {
12418 /* We may have skipped over point because the previous glyphs
12419 are from string. As there's no easy way to know the
12420 character position of the current glyph, find the correct
12421 glyph on point by scanning from string_start again. */
12422 Lisp_Object limit;
12423 Lisp_Object string;
12424 struct glyph *stop = glyph;
12425 int pos;
12426
12427 limit = make_number (pt_old + 1);
12428 glyph = string_start;
12429 x = string_start_x;
12430 string = glyph->object;
12431 pos = string_buffer_position (w, string, string_before_pos);
12432 /* If STRING is from overlay, LAST_POS == 0. We skip such glyphs
12433 because we always put cursor after overlay strings. */
12434 while (pos == 0 && glyph < stop)
12435 {
12436 string = glyph->object;
12437 SKIP_GLYPHS (glyph, stop, x, EQ (glyph->object, string));
12438 if (glyph < stop)
12439 pos = string_buffer_position (w, glyph->object, string_before_pos);
12440 }
12441
12442 while (glyph < stop)
12443 {
12444 pos = XINT (Fnext_single_char_property_change
12445 (make_number (pos), Qdisplay, Qnil, limit));
12446 if (pos > pt_old)
12447 break;
12448 /* Skip glyphs from the same string. */
12449 string = glyph->object;
12450 SKIP_GLYPHS (glyph, stop, x, EQ (glyph->object, string));
12451 /* Skip glyphs from an overlay. */
12452 while (glyph < stop
12453 && ! string_buffer_position (w, glyph->object, pos))
12454 {
12455 string = glyph->object;
12456 SKIP_GLYPHS (glyph, stop, x, EQ (glyph->object, string));
12457 }
12458 }
12459
12460 /* If we reached the end of the line, and end was from a string,
12461 cursor is not on this line. */
12462 if (glyph == end && row->continued_p)
12463 return 0;
12464 }
12465
12466 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
12467 w->cursor.x = x;
12468 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
12469 w->cursor.y = row->y + dy;
12470
12471 if (w == XWINDOW (selected_window))
12472 {
12473 if (!row->continued_p
12474 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
12475 && row->x == 0)
12476 {
12477 this_line_buffer = XBUFFER (w->buffer);
12478
12479 CHARPOS (this_line_start_pos)
12480 = MATRIX_ROW_START_CHARPOS (row) + delta;
12481 BYTEPOS (this_line_start_pos)
12482 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
12483
12484 CHARPOS (this_line_end_pos)
12485 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
12486 BYTEPOS (this_line_end_pos)
12487 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
12488
12489 this_line_y = w->cursor.y;
12490 this_line_pixel_height = row->height;
12491 this_line_vpos = w->cursor.vpos;
12492 this_line_start_x = row->x;
12493 }
12494 else
12495 CHARPOS (this_line_start_pos) = 0;
12496 }
12497
12498 return 1;
12499 }
12500
12501
12502 /* Run window scroll functions, if any, for WINDOW with new window
12503 start STARTP. Sets the window start of WINDOW to that position.
12504
12505 We assume that the window's buffer is really current. */
12506
12507 static INLINE struct text_pos
12508 run_window_scroll_functions (window, startp)
12509 Lisp_Object window;
12510 struct text_pos startp;
12511 {
12512 struct window *w = XWINDOW (window);
12513 SET_MARKER_FROM_TEXT_POS (w->start, startp);
12514
12515 if (current_buffer != XBUFFER (w->buffer))
12516 abort ();
12517
12518 if (!NILP (Vwindow_scroll_functions))
12519 {
12520 run_hook_with_args_2 (Qwindow_scroll_functions, window,
12521 make_number (CHARPOS (startp)));
12522 SET_TEXT_POS_FROM_MARKER (startp, w->start);
12523 /* In case the hook functions switch buffers. */
12524 if (current_buffer != XBUFFER (w->buffer))
12525 set_buffer_internal_1 (XBUFFER (w->buffer));
12526 }
12527
12528 return startp;
12529 }
12530
12531
12532 /* Make sure the line containing the cursor is fully visible.
12533 A value of 1 means there is nothing to be done.
12534 (Either the line is fully visible, or it cannot be made so,
12535 or we cannot tell.)
12536
12537 If FORCE_P is non-zero, return 0 even if partial visible cursor row
12538 is higher than window.
12539
12540 A value of 0 means the caller should do scrolling
12541 as if point had gone off the screen. */
12542
12543 static int
12544 cursor_row_fully_visible_p (w, force_p, current_matrix_p)
12545 struct window *w;
12546 int force_p;
12547 int current_matrix_p;
12548 {
12549 struct glyph_matrix *matrix;
12550 struct glyph_row *row;
12551 int window_height;
12552
12553 if (!make_cursor_line_fully_visible_p)
12554 return 1;
12555
12556 /* It's not always possible to find the cursor, e.g, when a window
12557 is full of overlay strings. Don't do anything in that case. */
12558 if (w->cursor.vpos < 0)
12559 return 1;
12560
12561 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
12562 row = MATRIX_ROW (matrix, w->cursor.vpos);
12563
12564 /* If the cursor row is not partially visible, there's nothing to do. */
12565 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
12566 return 1;
12567
12568 /* If the row the cursor is in is taller than the window's height,
12569 it's not clear what to do, so do nothing. */
12570 window_height = window_box_height (w);
12571 if (row->height >= window_height)
12572 {
12573 if (!force_p || MINI_WINDOW_P (w)
12574 || w->vscroll || w->cursor.vpos == 0)
12575 return 1;
12576 }
12577 return 0;
12578
12579 #if 0
12580 /* This code used to try to scroll the window just enough to make
12581 the line visible. It returned 0 to say that the caller should
12582 allocate larger glyph matrices. */
12583
12584 if (MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (w, row))
12585 {
12586 int dy = row->height - row->visible_height;
12587 w->vscroll = 0;
12588 w->cursor.y += dy;
12589 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
12590 }
12591 else /* MATRIX_ROW_PARTIALLY_VISIBLE_AT_BOTTOM_P (w, row)) */
12592 {
12593 int dy = - (row->height - row->visible_height);
12594 w->vscroll = dy;
12595 w->cursor.y += dy;
12596 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
12597 }
12598
12599 /* When we change the cursor y-position of the selected window,
12600 change this_line_y as well so that the display optimization for
12601 the cursor line of the selected window in redisplay_internal uses
12602 the correct y-position. */
12603 if (w == XWINDOW (selected_window))
12604 this_line_y = w->cursor.y;
12605
12606 /* If vscrolling requires a larger glyph matrix, arrange for a fresh
12607 redisplay with larger matrices. */
12608 if (matrix->nrows < required_matrix_height (w))
12609 {
12610 fonts_changed_p = 1;
12611 return 0;
12612 }
12613
12614 return 1;
12615 #endif /* 0 */
12616 }
12617
12618
12619 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
12620 non-zero means only WINDOW is redisplayed in redisplay_internal.
12621 TEMP_SCROLL_STEP has the same meaning as scroll_step, and is used
12622 in redisplay_window to bring a partially visible line into view in
12623 the case that only the cursor has moved.
12624
12625 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
12626 last screen line's vertical height extends past the end of the screen.
12627
12628 Value is
12629
12630 1 if scrolling succeeded
12631
12632 0 if scrolling didn't find point.
12633
12634 -1 if new fonts have been loaded so that we must interrupt
12635 redisplay, adjust glyph matrices, and try again. */
12636
12637 enum
12638 {
12639 SCROLLING_SUCCESS,
12640 SCROLLING_FAILED,
12641 SCROLLING_NEED_LARGER_MATRICES
12642 };
12643
12644 static int
12645 try_scrolling (window, just_this_one_p, scroll_conservatively,
12646 scroll_step, temp_scroll_step, last_line_misfit)
12647 Lisp_Object window;
12648 int just_this_one_p;
12649 EMACS_INT scroll_conservatively, scroll_step;
12650 int temp_scroll_step;
12651 int last_line_misfit;
12652 {
12653 struct window *w = XWINDOW (window);
12654 struct frame *f = XFRAME (w->frame);
12655 struct text_pos pos, startp;
12656 struct it it;
12657 int this_scroll_margin, scroll_max, rc, height;
12658 int dy = 0, amount_to_scroll = 0, scroll_down_p = 0;
12659 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
12660 Lisp_Object aggressive;
12661 int scroll_limit = INT_MAX / FRAME_LINE_HEIGHT (f);
12662
12663 #if GLYPH_DEBUG
12664 debug_method_add (w, "try_scrolling");
12665 #endif
12666
12667 SET_TEXT_POS_FROM_MARKER (startp, w->start);
12668
12669 /* Compute scroll margin height in pixels. We scroll when point is
12670 within this distance from the top or bottom of the window. */
12671 if (scroll_margin > 0)
12672 {
12673 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
12674 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
12675 }
12676 else
12677 this_scroll_margin = 0;
12678
12679 /* Force scroll_conservatively to have a reasonable value, to avoid
12680 overflow while computing how much to scroll. Note that it's
12681 fairly common for users to supply scroll-conservatively equal to
12682 `most-positive-fixnum', which can be larger than INT_MAX. */
12683 if (scroll_conservatively > scroll_limit)
12684 {
12685 scroll_conservatively = scroll_limit;
12686 scroll_max = INT_MAX;
12687 }
12688 else if (scroll_step || scroll_conservatively || temp_scroll_step)
12689 /* Compute how much we should try to scroll maximally to bring
12690 point into view. */
12691 scroll_max = (max (scroll_step,
12692 max (scroll_conservatively, temp_scroll_step))
12693 * FRAME_LINE_HEIGHT (f));
12694 else if (NUMBERP (current_buffer->scroll_down_aggressively)
12695 || NUMBERP (current_buffer->scroll_up_aggressively))
12696 /* We're trying to scroll because of aggressive scrolling but no
12697 scroll_step is set. Choose an arbitrary one. */
12698 scroll_max = 10 * FRAME_LINE_HEIGHT (f);
12699 else
12700 scroll_max = 0;
12701
12702 too_near_end:
12703
12704 /* Decide whether we have to scroll down. */
12705 if (PT > CHARPOS (startp))
12706 {
12707 int scroll_margin_y;
12708
12709 /* Compute the pixel ypos of the scroll margin, then move it to
12710 either that ypos or PT, whichever comes first. */
12711 start_display (&it, w, startp);
12712 scroll_margin_y = it.last_visible_y - this_scroll_margin
12713 - FRAME_LINE_HEIGHT (f) * extra_scroll_margin_lines;
12714 move_it_to (&it, PT, -1, scroll_margin_y - 1, -1,
12715 (MOVE_TO_POS | MOVE_TO_Y));
12716
12717 if (PT > CHARPOS (it.current.pos))
12718 {
12719 /* Point is in the scroll margin at the bottom of the
12720 window, or below. Compute the distance from the scroll
12721 margin to PT, and give up if the distance is greater than
12722 scroll_max. */
12723 move_it_to (&it, PT, -1, it.last_visible_y - 1, -1,
12724 MOVE_TO_POS | MOVE_TO_Y);
12725
12726 /* To make point visible, we must move the window start down
12727 so that the cursor line is visible, which means we have
12728 to add in the height of the cursor line. */
12729 dy = line_bottom_y (&it) - scroll_margin_y;
12730
12731 if (dy > scroll_max)
12732 return SCROLLING_FAILED;
12733
12734 scroll_down_p = 1;
12735 }
12736 }
12737
12738 if (scroll_down_p)
12739 {
12740 /* Move the window start down. If scrolling conservatively,
12741 move it just enough down to make point visible. If
12742 scroll_step is set, move it down by scroll_step. */
12743 start_display (&it, w, startp);
12744
12745 if (scroll_conservatively)
12746 /* Set AMOUNT_TO_SCROLL to at least one line,
12747 and at most scroll_conservatively lines. */
12748 amount_to_scroll
12749 = min (max (dy, FRAME_LINE_HEIGHT (f)),
12750 FRAME_LINE_HEIGHT (f) * scroll_conservatively);
12751 else if (scroll_step || temp_scroll_step)
12752 amount_to_scroll = scroll_max;
12753 else
12754 {
12755 aggressive = current_buffer->scroll_up_aggressively;
12756 height = WINDOW_BOX_TEXT_HEIGHT (w);
12757 if (NUMBERP (aggressive))
12758 {
12759 double float_amount = XFLOATINT (aggressive) * height;
12760 amount_to_scroll = float_amount;
12761 if (amount_to_scroll == 0 && float_amount > 0)
12762 amount_to_scroll = 1;
12763 }
12764 }
12765
12766 if (amount_to_scroll <= 0)
12767 return SCROLLING_FAILED;
12768
12769 /* If moving by amount_to_scroll leaves STARTP unchanged,
12770 move it down one screen line. */
12771
12772 move_it_vertically (&it, amount_to_scroll);
12773 if (CHARPOS (it.current.pos) == CHARPOS (startp))
12774 move_it_by_lines (&it, 1, 1);
12775 startp = it.current.pos;
12776 }
12777 else
12778 {
12779 struct text_pos scroll_margin_pos = startp;
12780
12781 /* See if point is inside the scroll margin at the top of the
12782 window. */
12783 if (this_scroll_margin)
12784 {
12785 start_display (&it, w, startp);
12786 move_it_vertically (&it, this_scroll_margin);
12787 scroll_margin_pos = it.current.pos;
12788 }
12789
12790 if (PT < CHARPOS (scroll_margin_pos))
12791 {
12792 /* Point is in the scroll margin at the top of the window or
12793 above what is displayed in the window. */
12794 int y0;
12795
12796 /* Compute the vertical distance from PT to the scroll
12797 margin position. Give up if distance is greater than
12798 scroll_max. */
12799 SET_TEXT_POS (pos, PT, PT_BYTE);
12800 start_display (&it, w, pos);
12801 y0 = it.current_y;
12802 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
12803 it.last_visible_y, -1,
12804 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
12805 dy = it.current_y - y0;
12806 if (dy > scroll_max)
12807 return SCROLLING_FAILED;
12808
12809 /* Compute new window start. */
12810 start_display (&it, w, startp);
12811
12812 if (scroll_conservatively)
12813 amount_to_scroll
12814 = max (dy, FRAME_LINE_HEIGHT (f) * max (scroll_step, temp_scroll_step));
12815 else if (scroll_step || temp_scroll_step)
12816 amount_to_scroll = scroll_max;
12817 else
12818 {
12819 aggressive = current_buffer->scroll_down_aggressively;
12820 height = WINDOW_BOX_TEXT_HEIGHT (w);
12821 if (NUMBERP (aggressive))
12822 {
12823 double float_amount = XFLOATINT (aggressive) * height;
12824 amount_to_scroll = float_amount;
12825 if (amount_to_scroll == 0 && float_amount > 0)
12826 amount_to_scroll = 1;
12827 }
12828 }
12829
12830 if (amount_to_scroll <= 0)
12831 return SCROLLING_FAILED;
12832
12833 move_it_vertically_backward (&it, amount_to_scroll);
12834 startp = it.current.pos;
12835 }
12836 }
12837
12838 /* Run window scroll functions. */
12839 startp = run_window_scroll_functions (window, startp);
12840
12841 /* Display the window. Give up if new fonts are loaded, or if point
12842 doesn't appear. */
12843 if (!try_window (window, startp, 0))
12844 rc = SCROLLING_NEED_LARGER_MATRICES;
12845 else if (w->cursor.vpos < 0)
12846 {
12847 clear_glyph_matrix (w->desired_matrix);
12848 rc = SCROLLING_FAILED;
12849 }
12850 else
12851 {
12852 /* Maybe forget recorded base line for line number display. */
12853 if (!just_this_one_p
12854 || current_buffer->clip_changed
12855 || BEG_UNCHANGED < CHARPOS (startp))
12856 w->base_line_number = Qnil;
12857
12858 /* If cursor ends up on a partially visible line,
12859 treat that as being off the bottom of the screen. */
12860 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1, 0))
12861 {
12862 clear_glyph_matrix (w->desired_matrix);
12863 ++extra_scroll_margin_lines;
12864 goto too_near_end;
12865 }
12866 rc = SCROLLING_SUCCESS;
12867 }
12868
12869 return rc;
12870 }
12871
12872
12873 /* Compute a suitable window start for window W if display of W starts
12874 on a continuation line. Value is non-zero if a new window start
12875 was computed.
12876
12877 The new window start will be computed, based on W's width, starting
12878 from the start of the continued line. It is the start of the
12879 screen line with the minimum distance from the old start W->start. */
12880
12881 static int
12882 compute_window_start_on_continuation_line (w)
12883 struct window *w;
12884 {
12885 struct text_pos pos, start_pos;
12886 int window_start_changed_p = 0;
12887
12888 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
12889
12890 /* If window start is on a continuation line... Window start may be
12891 < BEGV in case there's invisible text at the start of the
12892 buffer (M-x rmail, for example). */
12893 if (CHARPOS (start_pos) > BEGV
12894 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
12895 {
12896 struct it it;
12897 struct glyph_row *row;
12898
12899 /* Handle the case that the window start is out of range. */
12900 if (CHARPOS (start_pos) < BEGV)
12901 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
12902 else if (CHARPOS (start_pos) > ZV)
12903 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
12904
12905 /* Find the start of the continued line. This should be fast
12906 because scan_buffer is fast (newline cache). */
12907 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
12908 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
12909 row, DEFAULT_FACE_ID);
12910 reseat_at_previous_visible_line_start (&it);
12911
12912 /* If the line start is "too far" away from the window start,
12913 say it takes too much time to compute a new window start. */
12914 if (CHARPOS (start_pos) - IT_CHARPOS (it)
12915 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
12916 {
12917 int min_distance, distance;
12918
12919 /* Move forward by display lines to find the new window
12920 start. If window width was enlarged, the new start can
12921 be expected to be > the old start. If window width was
12922 decreased, the new window start will be < the old start.
12923 So, we're looking for the display line start with the
12924 minimum distance from the old window start. */
12925 pos = it.current.pos;
12926 min_distance = INFINITY;
12927 while ((distance = eabs (CHARPOS (start_pos) - IT_CHARPOS (it))),
12928 distance < min_distance)
12929 {
12930 min_distance = distance;
12931 pos = it.current.pos;
12932 move_it_by_lines (&it, 1, 0);
12933 }
12934
12935 /* Set the window start there. */
12936 SET_MARKER_FROM_TEXT_POS (w->start, pos);
12937 window_start_changed_p = 1;
12938 }
12939 }
12940
12941 return window_start_changed_p;
12942 }
12943
12944
12945 /* Try cursor movement in case text has not changed in window WINDOW,
12946 with window start STARTP. Value is
12947
12948 CURSOR_MOVEMENT_SUCCESS if successful
12949
12950 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
12951
12952 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
12953 display. *SCROLL_STEP is set to 1, under certain circumstances, if
12954 we want to scroll as if scroll-step were set to 1. See the code.
12955
12956 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
12957 which case we have to abort this redisplay, and adjust matrices
12958 first. */
12959
12960 enum
12961 {
12962 CURSOR_MOVEMENT_SUCCESS,
12963 CURSOR_MOVEMENT_CANNOT_BE_USED,
12964 CURSOR_MOVEMENT_MUST_SCROLL,
12965 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
12966 };
12967
12968 static int
12969 try_cursor_movement (window, startp, scroll_step)
12970 Lisp_Object window;
12971 struct text_pos startp;
12972 int *scroll_step;
12973 {
12974 struct window *w = XWINDOW (window);
12975 struct frame *f = XFRAME (w->frame);
12976 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
12977
12978 #if GLYPH_DEBUG
12979 if (inhibit_try_cursor_movement)
12980 return rc;
12981 #endif
12982
12983 /* Handle case where text has not changed, only point, and it has
12984 not moved off the frame. */
12985 if (/* Point may be in this window. */
12986 PT >= CHARPOS (startp)
12987 /* Selective display hasn't changed. */
12988 && !current_buffer->clip_changed
12989 /* Function force-mode-line-update is used to force a thorough
12990 redisplay. It sets either windows_or_buffers_changed or
12991 update_mode_lines. So don't take a shortcut here for these
12992 cases. */
12993 && !update_mode_lines
12994 && !windows_or_buffers_changed
12995 && !cursor_type_changed
12996 /* Can't use this case if highlighting a region. When a
12997 region exists, cursor movement has to do more than just
12998 set the cursor. */
12999 && !(!NILP (Vtransient_mark_mode)
13000 && !NILP (current_buffer->mark_active))
13001 && NILP (w->region_showing)
13002 && NILP (Vshow_trailing_whitespace)
13003 /* Right after splitting windows, last_point may be nil. */
13004 && INTEGERP (w->last_point)
13005 /* This code is not used for mini-buffer for the sake of the case
13006 of redisplaying to replace an echo area message; since in
13007 that case the mini-buffer contents per se are usually
13008 unchanged. This code is of no real use in the mini-buffer
13009 since the handling of this_line_start_pos, etc., in redisplay
13010 handles the same cases. */
13011 && !EQ (window, minibuf_window)
13012 /* When splitting windows or for new windows, it happens that
13013 redisplay is called with a nil window_end_vpos or one being
13014 larger than the window. This should really be fixed in
13015 window.c. I don't have this on my list, now, so we do
13016 approximately the same as the old redisplay code. --gerd. */
13017 && INTEGERP (w->window_end_vpos)
13018 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
13019 && (FRAME_WINDOW_P (f)
13020 || !overlay_arrow_in_current_buffer_p ()))
13021 {
13022 int this_scroll_margin, top_scroll_margin;
13023 struct glyph_row *row = NULL;
13024
13025 #if GLYPH_DEBUG
13026 debug_method_add (w, "cursor movement");
13027 #endif
13028
13029 /* Scroll if point within this distance from the top or bottom
13030 of the window. This is a pixel value. */
13031 if (scroll_margin > 0)
13032 {
13033 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
13034 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
13035 }
13036 else
13037 this_scroll_margin = 0;
13038
13039 top_scroll_margin = this_scroll_margin;
13040 if (WINDOW_WANTS_HEADER_LINE_P (w))
13041 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
13042
13043 /* Start with the row the cursor was displayed during the last
13044 not paused redisplay. Give up if that row is not valid. */
13045 if (w->last_cursor.vpos < 0
13046 || w->last_cursor.vpos >= w->current_matrix->nrows)
13047 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13048 else
13049 {
13050 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
13051 if (row->mode_line_p)
13052 ++row;
13053 if (!row->enabled_p)
13054 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13055 }
13056
13057 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
13058 {
13059 int scroll_p = 0;
13060 int last_y = window_text_bottom_y (w) - this_scroll_margin;
13061
13062 if (PT > XFASTINT (w->last_point))
13063 {
13064 /* Point has moved forward. */
13065 while (MATRIX_ROW_END_CHARPOS (row) < PT
13066 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
13067 {
13068 xassert (row->enabled_p);
13069 ++row;
13070 }
13071
13072 /* The end position of a row equals the start position
13073 of the next row. If PT is there, we would rather
13074 display it in the next line. */
13075 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
13076 && MATRIX_ROW_END_CHARPOS (row) == PT
13077 && !cursor_row_p (w, row))
13078 ++row;
13079
13080 /* If within the scroll margin, scroll. Note that
13081 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
13082 the next line would be drawn, and that
13083 this_scroll_margin can be zero. */
13084 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
13085 || PT > MATRIX_ROW_END_CHARPOS (row)
13086 /* Line is completely visible last line in window
13087 and PT is to be set in the next line. */
13088 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
13089 && PT == MATRIX_ROW_END_CHARPOS (row)
13090 && !row->ends_at_zv_p
13091 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
13092 scroll_p = 1;
13093 }
13094 else if (PT < XFASTINT (w->last_point))
13095 {
13096 /* Cursor has to be moved backward. Note that PT >=
13097 CHARPOS (startp) because of the outer if-statement. */
13098 while (!row->mode_line_p
13099 && (MATRIX_ROW_START_CHARPOS (row) > PT
13100 || (MATRIX_ROW_START_CHARPOS (row) == PT
13101 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
13102 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
13103 row > w->current_matrix->rows
13104 && (row-1)->ends_in_newline_from_string_p))))
13105 && (row->y > top_scroll_margin
13106 || CHARPOS (startp) == BEGV))
13107 {
13108 xassert (row->enabled_p);
13109 --row;
13110 }
13111
13112 /* Consider the following case: Window starts at BEGV,
13113 there is invisible, intangible text at BEGV, so that
13114 display starts at some point START > BEGV. It can
13115 happen that we are called with PT somewhere between
13116 BEGV and START. Try to handle that case. */
13117 if (row < w->current_matrix->rows
13118 || row->mode_line_p)
13119 {
13120 row = w->current_matrix->rows;
13121 if (row->mode_line_p)
13122 ++row;
13123 }
13124
13125 /* Due to newlines in overlay strings, we may have to
13126 skip forward over overlay strings. */
13127 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
13128 && MATRIX_ROW_END_CHARPOS (row) == PT
13129 && !cursor_row_p (w, row))
13130 ++row;
13131
13132 /* If within the scroll margin, scroll. */
13133 if (row->y < top_scroll_margin
13134 && CHARPOS (startp) != BEGV)
13135 scroll_p = 1;
13136 }
13137 else
13138 {
13139 /* Cursor did not move. So don't scroll even if cursor line
13140 is partially visible, as it was so before. */
13141 rc = CURSOR_MOVEMENT_SUCCESS;
13142 }
13143
13144 if (PT < MATRIX_ROW_START_CHARPOS (row)
13145 || PT > MATRIX_ROW_END_CHARPOS (row))
13146 {
13147 /* if PT is not in the glyph row, give up. */
13148 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13149 }
13150 else if (rc != CURSOR_MOVEMENT_SUCCESS
13151 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
13152 && make_cursor_line_fully_visible_p)
13153 {
13154 if (PT == MATRIX_ROW_END_CHARPOS (row)
13155 && !row->ends_at_zv_p
13156 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
13157 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13158 else if (row->height > window_box_height (w))
13159 {
13160 /* If we end up in a partially visible line, let's
13161 make it fully visible, except when it's taller
13162 than the window, in which case we can't do much
13163 about it. */
13164 *scroll_step = 1;
13165 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13166 }
13167 else
13168 {
13169 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13170 if (!cursor_row_fully_visible_p (w, 0, 1))
13171 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13172 else
13173 rc = CURSOR_MOVEMENT_SUCCESS;
13174 }
13175 }
13176 else if (scroll_p)
13177 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13178 else
13179 {
13180 do
13181 {
13182 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
13183 {
13184 rc = CURSOR_MOVEMENT_SUCCESS;
13185 break;
13186 }
13187 ++row;
13188 }
13189 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
13190 && MATRIX_ROW_START_CHARPOS (row) == PT
13191 && cursor_row_p (w, row));
13192 }
13193 }
13194 }
13195
13196 return rc;
13197 }
13198
13199 void
13200 set_vertical_scroll_bar (w)
13201 struct window *w;
13202 {
13203 int start, end, whole;
13204
13205 /* Calculate the start and end positions for the current window.
13206 At some point, it would be nice to choose between scrollbars
13207 which reflect the whole buffer size, with special markers
13208 indicating narrowing, and scrollbars which reflect only the
13209 visible region.
13210
13211 Note that mini-buffers sometimes aren't displaying any text. */
13212 if (!MINI_WINDOW_P (w)
13213 || (w == XWINDOW (minibuf_window)
13214 && NILP (echo_area_buffer[0])))
13215 {
13216 struct buffer *buf = XBUFFER (w->buffer);
13217 whole = BUF_ZV (buf) - BUF_BEGV (buf);
13218 start = marker_position (w->start) - BUF_BEGV (buf);
13219 /* I don't think this is guaranteed to be right. For the
13220 moment, we'll pretend it is. */
13221 end = BUF_Z (buf) - XFASTINT (w->window_end_pos) - BUF_BEGV (buf);
13222
13223 if (end < start)
13224 end = start;
13225 if (whole < (end - start))
13226 whole = end - start;
13227 }
13228 else
13229 start = end = whole = 0;
13230
13231 /* Indicate what this scroll bar ought to be displaying now. */
13232 if (FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
13233 (*FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
13234 (w, end - start, whole, start);
13235 }
13236
13237
13238 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
13239 selected_window is redisplayed.
13240
13241 We can return without actually redisplaying the window if
13242 fonts_changed_p is nonzero. In that case, redisplay_internal will
13243 retry. */
13244
13245 static void
13246 redisplay_window (window, just_this_one_p)
13247 Lisp_Object window;
13248 int just_this_one_p;
13249 {
13250 struct window *w = XWINDOW (window);
13251 struct frame *f = XFRAME (w->frame);
13252 struct buffer *buffer = XBUFFER (w->buffer);
13253 struct buffer *old = current_buffer;
13254 struct text_pos lpoint, opoint, startp;
13255 int update_mode_line;
13256 int tem;
13257 struct it it;
13258 /* Record it now because it's overwritten. */
13259 int current_matrix_up_to_date_p = 0;
13260 int used_current_matrix_p = 0;
13261 /* This is less strict than current_matrix_up_to_date_p.
13262 It indictes that the buffer contents and narrowing are unchanged. */
13263 int buffer_unchanged_p = 0;
13264 int temp_scroll_step = 0;
13265 int count = SPECPDL_INDEX ();
13266 int rc;
13267 int centering_position = -1;
13268 int last_line_misfit = 0;
13269 int beg_unchanged, end_unchanged;
13270
13271 SET_TEXT_POS (lpoint, PT, PT_BYTE);
13272 opoint = lpoint;
13273
13274 /* W must be a leaf window here. */
13275 xassert (!NILP (w->buffer));
13276 #if GLYPH_DEBUG
13277 *w->desired_matrix->method = 0;
13278 #endif
13279
13280 restart:
13281 reconsider_clip_changes (w, buffer);
13282
13283 /* Has the mode line to be updated? */
13284 update_mode_line = (!NILP (w->update_mode_line)
13285 || update_mode_lines
13286 || buffer->clip_changed
13287 || buffer->prevent_redisplay_optimizations_p);
13288
13289 if (MINI_WINDOW_P (w))
13290 {
13291 if (w == XWINDOW (echo_area_window)
13292 && !NILP (echo_area_buffer[0]))
13293 {
13294 if (update_mode_line)
13295 /* We may have to update a tty frame's menu bar or a
13296 tool-bar. Example `M-x C-h C-h C-g'. */
13297 goto finish_menu_bars;
13298 else
13299 /* We've already displayed the echo area glyphs in this window. */
13300 goto finish_scroll_bars;
13301 }
13302 else if ((w != XWINDOW (minibuf_window)
13303 || minibuf_level == 0)
13304 /* When buffer is nonempty, redisplay window normally. */
13305 && BUF_Z (XBUFFER (w->buffer)) == BUF_BEG (XBUFFER (w->buffer))
13306 /* Quail displays non-mini buffers in minibuffer window.
13307 In that case, redisplay the window normally. */
13308 && !NILP (Fmemq (w->buffer, Vminibuffer_list)))
13309 {
13310 /* W is a mini-buffer window, but it's not active, so clear
13311 it. */
13312 int yb = window_text_bottom_y (w);
13313 struct glyph_row *row;
13314 int y;
13315
13316 for (y = 0, row = w->desired_matrix->rows;
13317 y < yb;
13318 y += row->height, ++row)
13319 blank_row (w, row, y);
13320 goto finish_scroll_bars;
13321 }
13322
13323 clear_glyph_matrix (w->desired_matrix);
13324 }
13325
13326 /* Otherwise set up data on this window; select its buffer and point
13327 value. */
13328 /* Really select the buffer, for the sake of buffer-local
13329 variables. */
13330 set_buffer_internal_1 (XBUFFER (w->buffer));
13331
13332 current_matrix_up_to_date_p
13333 = (!NILP (w->window_end_valid)
13334 && !current_buffer->clip_changed
13335 && !current_buffer->prevent_redisplay_optimizations_p
13336 && XFASTINT (w->last_modified) >= MODIFF
13337 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
13338
13339 /* Run the window-bottom-change-functions
13340 if it is possible that the text on the screen has changed
13341 (either due to modification of the text, or any other reason). */
13342 if (!current_matrix_up_to_date_p
13343 && !NILP (Vwindow_text_change_functions))
13344 {
13345 safe_run_hooks (Qwindow_text_change_functions);
13346 goto restart;
13347 }
13348
13349 beg_unchanged = BEG_UNCHANGED;
13350 end_unchanged = END_UNCHANGED;
13351
13352 SET_TEXT_POS (opoint, PT, PT_BYTE);
13353
13354 specbind (Qinhibit_point_motion_hooks, Qt);
13355
13356 buffer_unchanged_p
13357 = (!NILP (w->window_end_valid)
13358 && !current_buffer->clip_changed
13359 && XFASTINT (w->last_modified) >= MODIFF
13360 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
13361
13362 /* When windows_or_buffers_changed is non-zero, we can't rely on
13363 the window end being valid, so set it to nil there. */
13364 if (windows_or_buffers_changed)
13365 {
13366 /* If window starts on a continuation line, maybe adjust the
13367 window start in case the window's width changed. */
13368 if (XMARKER (w->start)->buffer == current_buffer)
13369 compute_window_start_on_continuation_line (w);
13370
13371 w->window_end_valid = Qnil;
13372 }
13373
13374 /* Some sanity checks. */
13375 CHECK_WINDOW_END (w);
13376 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
13377 abort ();
13378 if (BYTEPOS (opoint) < CHARPOS (opoint))
13379 abort ();
13380
13381 /* If %c is in mode line, update it if needed. */
13382 if (!NILP (w->column_number_displayed)
13383 /* This alternative quickly identifies a common case
13384 where no change is needed. */
13385 && !(PT == XFASTINT (w->last_point)
13386 && XFASTINT (w->last_modified) >= MODIFF
13387 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
13388 && (XFASTINT (w->column_number_displayed)
13389 != (int) current_column ())) /* iftc */
13390 update_mode_line = 1;
13391
13392 /* Count number of windows showing the selected buffer. An indirect
13393 buffer counts as its base buffer. */
13394 if (!just_this_one_p)
13395 {
13396 struct buffer *current_base, *window_base;
13397 current_base = current_buffer;
13398 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
13399 if (current_base->base_buffer)
13400 current_base = current_base->base_buffer;
13401 if (window_base->base_buffer)
13402 window_base = window_base->base_buffer;
13403 if (current_base == window_base)
13404 buffer_shared++;
13405 }
13406
13407 /* Point refers normally to the selected window. For any other
13408 window, set up appropriate value. */
13409 if (!EQ (window, selected_window))
13410 {
13411 int new_pt = XMARKER (w->pointm)->charpos;
13412 int new_pt_byte = marker_byte_position (w->pointm);
13413 if (new_pt < BEGV)
13414 {
13415 new_pt = BEGV;
13416 new_pt_byte = BEGV_BYTE;
13417 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
13418 }
13419 else if (new_pt > (ZV - 1))
13420 {
13421 new_pt = ZV;
13422 new_pt_byte = ZV_BYTE;
13423 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
13424 }
13425
13426 /* We don't use SET_PT so that the point-motion hooks don't run. */
13427 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
13428 }
13429
13430 /* If any of the character widths specified in the display table
13431 have changed, invalidate the width run cache. It's true that
13432 this may be a bit late to catch such changes, but the rest of
13433 redisplay goes (non-fatally) haywire when the display table is
13434 changed, so why should we worry about doing any better? */
13435 if (current_buffer->width_run_cache)
13436 {
13437 struct Lisp_Char_Table *disptab = buffer_display_table ();
13438
13439 if (! disptab_matches_widthtab (disptab,
13440 XVECTOR (current_buffer->width_table)))
13441 {
13442 invalidate_region_cache (current_buffer,
13443 current_buffer->width_run_cache,
13444 BEG, Z);
13445 recompute_width_table (current_buffer, disptab);
13446 }
13447 }
13448
13449 /* If window-start is screwed up, choose a new one. */
13450 if (XMARKER (w->start)->buffer != current_buffer)
13451 goto recenter;
13452
13453 SET_TEXT_POS_FROM_MARKER (startp, w->start);
13454
13455 /* If someone specified a new starting point but did not insist,
13456 check whether it can be used. */
13457 if (!NILP (w->optional_new_start)
13458 && CHARPOS (startp) >= BEGV
13459 && CHARPOS (startp) <= ZV)
13460 {
13461 w->optional_new_start = Qnil;
13462 start_display (&it, w, startp);
13463 move_it_to (&it, PT, 0, it.last_visible_y, -1,
13464 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
13465 if (IT_CHARPOS (it) == PT)
13466 w->force_start = Qt;
13467 /* IT may overshoot PT if text at PT is invisible. */
13468 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
13469 w->force_start = Qt;
13470 }
13471
13472 force_start:
13473
13474 /* Handle case where place to start displaying has been specified,
13475 unless the specified location is outside the accessible range. */
13476 if (!NILP (w->force_start)
13477 || w->frozen_window_start_p)
13478 {
13479 /* We set this later on if we have to adjust point. */
13480 int new_vpos = -1;
13481
13482 w->force_start = Qnil;
13483 w->vscroll = 0;
13484 w->window_end_valid = Qnil;
13485
13486 /* Forget any recorded base line for line number display. */
13487 if (!buffer_unchanged_p)
13488 w->base_line_number = Qnil;
13489
13490 /* Redisplay the mode line. Select the buffer properly for that.
13491 Also, run the hook window-scroll-functions
13492 because we have scrolled. */
13493 /* Note, we do this after clearing force_start because
13494 if there's an error, it is better to forget about force_start
13495 than to get into an infinite loop calling the hook functions
13496 and having them get more errors. */
13497 if (!update_mode_line
13498 || ! NILP (Vwindow_scroll_functions))
13499 {
13500 update_mode_line = 1;
13501 w->update_mode_line = Qt;
13502 startp = run_window_scroll_functions (window, startp);
13503 }
13504
13505 w->last_modified = make_number (0);
13506 w->last_overlay_modified = make_number (0);
13507 if (CHARPOS (startp) < BEGV)
13508 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
13509 else if (CHARPOS (startp) > ZV)
13510 SET_TEXT_POS (startp, ZV, ZV_BYTE);
13511
13512 /* Redisplay, then check if cursor has been set during the
13513 redisplay. Give up if new fonts were loaded. */
13514 /* We used to issue a CHECK_MARGINS argument to try_window here,
13515 but this causes scrolling to fail when point begins inside
13516 the scroll margin (bug#148) -- cyd */
13517 if (!try_window (window, startp, 0))
13518 {
13519 w->force_start = Qt;
13520 clear_glyph_matrix (w->desired_matrix);
13521 goto need_larger_matrices;
13522 }
13523
13524 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
13525 {
13526 /* If point does not appear, try to move point so it does
13527 appear. The desired matrix has been built above, so we
13528 can use it here. */
13529 new_vpos = window_box_height (w) / 2;
13530 }
13531
13532 if (!cursor_row_fully_visible_p (w, 0, 0))
13533 {
13534 /* Point does appear, but on a line partly visible at end of window.
13535 Move it back to a fully-visible line. */
13536 new_vpos = window_box_height (w);
13537 }
13538
13539 /* If we need to move point for either of the above reasons,
13540 now actually do it. */
13541 if (new_vpos >= 0)
13542 {
13543 struct glyph_row *row;
13544
13545 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
13546 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
13547 ++row;
13548
13549 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
13550 MATRIX_ROW_START_BYTEPOS (row));
13551
13552 if (w != XWINDOW (selected_window))
13553 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
13554 else if (current_buffer == old)
13555 SET_TEXT_POS (lpoint, PT, PT_BYTE);
13556
13557 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
13558
13559 /* If we are highlighting the region, then we just changed
13560 the region, so redisplay to show it. */
13561 if (!NILP (Vtransient_mark_mode)
13562 && !NILP (current_buffer->mark_active))
13563 {
13564 clear_glyph_matrix (w->desired_matrix);
13565 if (!try_window (window, startp, 0))
13566 goto need_larger_matrices;
13567 }
13568 }
13569
13570 #if GLYPH_DEBUG
13571 debug_method_add (w, "forced window start");
13572 #endif
13573 goto done;
13574 }
13575
13576 /* Handle case where text has not changed, only point, and it has
13577 not moved off the frame, and we are not retrying after hscroll.
13578 (current_matrix_up_to_date_p is nonzero when retrying.) */
13579 if (current_matrix_up_to_date_p
13580 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
13581 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
13582 {
13583 switch (rc)
13584 {
13585 case CURSOR_MOVEMENT_SUCCESS:
13586 used_current_matrix_p = 1;
13587 goto done;
13588
13589 #if 0 /* try_cursor_movement never returns this value. */
13590 case CURSOR_MOVEMENT_NEED_LARGER_MATRICES:
13591 goto need_larger_matrices;
13592 #endif
13593
13594 case CURSOR_MOVEMENT_MUST_SCROLL:
13595 goto try_to_scroll;
13596
13597 default:
13598 abort ();
13599 }
13600 }
13601 /* If current starting point was originally the beginning of a line
13602 but no longer is, find a new starting point. */
13603 else if (!NILP (w->start_at_line_beg)
13604 && !(CHARPOS (startp) <= BEGV
13605 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
13606 {
13607 #if GLYPH_DEBUG
13608 debug_method_add (w, "recenter 1");
13609 #endif
13610 goto recenter;
13611 }
13612
13613 /* Try scrolling with try_window_id. Value is > 0 if update has
13614 been done, it is -1 if we know that the same window start will
13615 not work. It is 0 if unsuccessful for some other reason. */
13616 else if ((tem = try_window_id (w)) != 0)
13617 {
13618 #if GLYPH_DEBUG
13619 debug_method_add (w, "try_window_id %d", tem);
13620 #endif
13621
13622 if (fonts_changed_p)
13623 goto need_larger_matrices;
13624 if (tem > 0)
13625 goto done;
13626
13627 /* Otherwise try_window_id has returned -1 which means that we
13628 don't want the alternative below this comment to execute. */
13629 }
13630 else if (CHARPOS (startp) >= BEGV
13631 && CHARPOS (startp) <= ZV
13632 && PT >= CHARPOS (startp)
13633 && (CHARPOS (startp) < ZV
13634 /* Avoid starting at end of buffer. */
13635 || CHARPOS (startp) == BEGV
13636 || (XFASTINT (w->last_modified) >= MODIFF
13637 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
13638 {
13639
13640 /* If first window line is a continuation line, and window start
13641 is inside the modified region, but the first change is before
13642 current window start, we must select a new window start.
13643
13644 However, if this is the result of a down-mouse event (e.g. by
13645 extending the mouse-drag-overlay), we don't want to select a
13646 new window start, since that would change the position under
13647 the mouse, resulting in an unwanted mouse-movement rather
13648 than a simple mouse-click. */
13649 if (NILP (w->start_at_line_beg)
13650 && NILP (do_mouse_tracking)
13651 && CHARPOS (startp) > BEGV
13652 && CHARPOS (startp) > BEG + beg_unchanged
13653 && CHARPOS (startp) <= Z - end_unchanged
13654 /* Even if w->start_at_line_beg is nil, a new window may
13655 start at a line_beg, since that's how set_buffer_window
13656 sets it. So, we need to check the return value of
13657 compute_window_start_on_continuation_line. (See also
13658 bug#197). */
13659 && XMARKER (w->start)->buffer == current_buffer
13660 && compute_window_start_on_continuation_line (w))
13661 {
13662 w->force_start = Qt;
13663 SET_TEXT_POS_FROM_MARKER (startp, w->start);
13664 goto force_start;
13665 }
13666
13667 #if GLYPH_DEBUG
13668 debug_method_add (w, "same window start");
13669 #endif
13670
13671 /* Try to redisplay starting at same place as before.
13672 If point has not moved off frame, accept the results. */
13673 if (!current_matrix_up_to_date_p
13674 /* Don't use try_window_reusing_current_matrix in this case
13675 because a window scroll function can have changed the
13676 buffer. */
13677 || !NILP (Vwindow_scroll_functions)
13678 || MINI_WINDOW_P (w)
13679 || !(used_current_matrix_p
13680 = try_window_reusing_current_matrix (w)))
13681 {
13682 IF_DEBUG (debug_method_add (w, "1"));
13683 if (try_window (window, startp, 1) < 0)
13684 /* -1 means we need to scroll.
13685 0 means we need new matrices, but fonts_changed_p
13686 is set in that case, so we will detect it below. */
13687 goto try_to_scroll;
13688 }
13689
13690 if (fonts_changed_p)
13691 goto need_larger_matrices;
13692
13693 if (w->cursor.vpos >= 0)
13694 {
13695 if (!just_this_one_p
13696 || current_buffer->clip_changed
13697 || BEG_UNCHANGED < CHARPOS (startp))
13698 /* Forget any recorded base line for line number display. */
13699 w->base_line_number = Qnil;
13700
13701 if (!cursor_row_fully_visible_p (w, 1, 0))
13702 {
13703 clear_glyph_matrix (w->desired_matrix);
13704 last_line_misfit = 1;
13705 }
13706 /* Drop through and scroll. */
13707 else
13708 goto done;
13709 }
13710 else
13711 clear_glyph_matrix (w->desired_matrix);
13712 }
13713
13714 try_to_scroll:
13715
13716 w->last_modified = make_number (0);
13717 w->last_overlay_modified = make_number (0);
13718
13719 /* Redisplay the mode line. Select the buffer properly for that. */
13720 if (!update_mode_line)
13721 {
13722 update_mode_line = 1;
13723 w->update_mode_line = Qt;
13724 }
13725
13726 /* Try to scroll by specified few lines. */
13727 if ((scroll_conservatively
13728 || scroll_step
13729 || temp_scroll_step
13730 || NUMBERP (current_buffer->scroll_up_aggressively)
13731 || NUMBERP (current_buffer->scroll_down_aggressively))
13732 && !current_buffer->clip_changed
13733 && CHARPOS (startp) >= BEGV
13734 && CHARPOS (startp) <= ZV)
13735 {
13736 /* The function returns -1 if new fonts were loaded, 1 if
13737 successful, 0 if not successful. */
13738 int rc = try_scrolling (window, just_this_one_p,
13739 scroll_conservatively,
13740 scroll_step,
13741 temp_scroll_step, last_line_misfit);
13742 switch (rc)
13743 {
13744 case SCROLLING_SUCCESS:
13745 goto done;
13746
13747 case SCROLLING_NEED_LARGER_MATRICES:
13748 goto need_larger_matrices;
13749
13750 case SCROLLING_FAILED:
13751 break;
13752
13753 default:
13754 abort ();
13755 }
13756 }
13757
13758 /* Finally, just choose place to start which centers point */
13759
13760 recenter:
13761 if (centering_position < 0)
13762 centering_position = window_box_height (w) / 2;
13763
13764 #if GLYPH_DEBUG
13765 debug_method_add (w, "recenter");
13766 #endif
13767
13768 /* w->vscroll = 0; */
13769
13770 /* Forget any previously recorded base line for line number display. */
13771 if (!buffer_unchanged_p)
13772 w->base_line_number = Qnil;
13773
13774 /* Move backward half the height of the window. */
13775 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
13776 it.current_y = it.last_visible_y;
13777 move_it_vertically_backward (&it, centering_position);
13778 xassert (IT_CHARPOS (it) >= BEGV);
13779
13780 /* The function move_it_vertically_backward may move over more
13781 than the specified y-distance. If it->w is small, e.g. a
13782 mini-buffer window, we may end up in front of the window's
13783 display area. Start displaying at the start of the line
13784 containing PT in this case. */
13785 if (it.current_y <= 0)
13786 {
13787 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
13788 move_it_vertically_backward (&it, 0);
13789 it.current_y = 0;
13790 }
13791
13792 it.current_x = it.hpos = 0;
13793
13794 /* Set startp here explicitly in case that helps avoid an infinite loop
13795 in case the window-scroll-functions functions get errors. */
13796 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
13797
13798 /* Run scroll hooks. */
13799 startp = run_window_scroll_functions (window, it.current.pos);
13800
13801 /* Redisplay the window. */
13802 if (!current_matrix_up_to_date_p
13803 || windows_or_buffers_changed
13804 || cursor_type_changed
13805 /* Don't use try_window_reusing_current_matrix in this case
13806 because it can have changed the buffer. */
13807 || !NILP (Vwindow_scroll_functions)
13808 || !just_this_one_p
13809 || MINI_WINDOW_P (w)
13810 || !(used_current_matrix_p
13811 = try_window_reusing_current_matrix (w)))
13812 try_window (window, startp, 0);
13813
13814 /* If new fonts have been loaded (due to fontsets), give up. We
13815 have to start a new redisplay since we need to re-adjust glyph
13816 matrices. */
13817 if (fonts_changed_p)
13818 goto need_larger_matrices;
13819
13820 /* If cursor did not appear assume that the middle of the window is
13821 in the first line of the window. Do it again with the next line.
13822 (Imagine a window of height 100, displaying two lines of height
13823 60. Moving back 50 from it->last_visible_y will end in the first
13824 line.) */
13825 if (w->cursor.vpos < 0)
13826 {
13827 if (!NILP (w->window_end_valid)
13828 && PT >= Z - XFASTINT (w->window_end_pos))
13829 {
13830 clear_glyph_matrix (w->desired_matrix);
13831 move_it_by_lines (&it, 1, 0);
13832 try_window (window, it.current.pos, 0);
13833 }
13834 else if (PT < IT_CHARPOS (it))
13835 {
13836 clear_glyph_matrix (w->desired_matrix);
13837 move_it_by_lines (&it, -1, 0);
13838 try_window (window, it.current.pos, 0);
13839 }
13840 else
13841 {
13842 /* Not much we can do about it. */
13843 }
13844 }
13845
13846 /* Consider the following case: Window starts at BEGV, there is
13847 invisible, intangible text at BEGV, so that display starts at
13848 some point START > BEGV. It can happen that we are called with
13849 PT somewhere between BEGV and START. Try to handle that case. */
13850 if (w->cursor.vpos < 0)
13851 {
13852 struct glyph_row *row = w->current_matrix->rows;
13853 if (row->mode_line_p)
13854 ++row;
13855 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13856 }
13857
13858 if (!cursor_row_fully_visible_p (w, 0, 0))
13859 {
13860 /* If vscroll is enabled, disable it and try again. */
13861 if (w->vscroll)
13862 {
13863 w->vscroll = 0;
13864 clear_glyph_matrix (w->desired_matrix);
13865 goto recenter;
13866 }
13867
13868 /* If centering point failed to make the whole line visible,
13869 put point at the top instead. That has to make the whole line
13870 visible, if it can be done. */
13871 if (centering_position == 0)
13872 goto done;
13873
13874 clear_glyph_matrix (w->desired_matrix);
13875 centering_position = 0;
13876 goto recenter;
13877 }
13878
13879 done:
13880
13881 SET_TEXT_POS_FROM_MARKER (startp, w->start);
13882 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
13883 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
13884 ? Qt : Qnil);
13885
13886 /* Display the mode line, if we must. */
13887 if ((update_mode_line
13888 /* If window not full width, must redo its mode line
13889 if (a) the window to its side is being redone and
13890 (b) we do a frame-based redisplay. This is a consequence
13891 of how inverted lines are drawn in frame-based redisplay. */
13892 || (!just_this_one_p
13893 && !FRAME_WINDOW_P (f)
13894 && !WINDOW_FULL_WIDTH_P (w))
13895 /* Line number to display. */
13896 || INTEGERP (w->base_line_pos)
13897 /* Column number is displayed and different from the one displayed. */
13898 || (!NILP (w->column_number_displayed)
13899 && (XFASTINT (w->column_number_displayed)
13900 != (int) current_column ()))) /* iftc */
13901 /* This means that the window has a mode line. */
13902 && (WINDOW_WANTS_MODELINE_P (w)
13903 || WINDOW_WANTS_HEADER_LINE_P (w)))
13904 {
13905 display_mode_lines (w);
13906
13907 /* If mode line height has changed, arrange for a thorough
13908 immediate redisplay using the correct mode line height. */
13909 if (WINDOW_WANTS_MODELINE_P (w)
13910 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
13911 {
13912 fonts_changed_p = 1;
13913 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
13914 = DESIRED_MODE_LINE_HEIGHT (w);
13915 }
13916
13917 /* If top line height has changed, arrange for a thorough
13918 immediate redisplay using the correct mode line height. */
13919 if (WINDOW_WANTS_HEADER_LINE_P (w)
13920 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
13921 {
13922 fonts_changed_p = 1;
13923 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
13924 = DESIRED_HEADER_LINE_HEIGHT (w);
13925 }
13926
13927 if (fonts_changed_p)
13928 goto need_larger_matrices;
13929 }
13930
13931 if (!line_number_displayed
13932 && !BUFFERP (w->base_line_pos))
13933 {
13934 w->base_line_pos = Qnil;
13935 w->base_line_number = Qnil;
13936 }
13937
13938 finish_menu_bars:
13939
13940 /* When we reach a frame's selected window, redo the frame's menu bar. */
13941 if (update_mode_line
13942 && EQ (FRAME_SELECTED_WINDOW (f), window))
13943 {
13944 int redisplay_menu_p = 0;
13945 int redisplay_tool_bar_p = 0;
13946
13947 if (FRAME_WINDOW_P (f))
13948 {
13949 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
13950 || defined (HAVE_NS) || defined (USE_GTK)
13951 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
13952 #else
13953 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
13954 #endif
13955 }
13956 else
13957 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
13958
13959 if (redisplay_menu_p)
13960 display_menu_bar (w);
13961
13962 #ifdef HAVE_WINDOW_SYSTEM
13963 if (FRAME_WINDOW_P (f))
13964 {
13965 #if defined (USE_GTK) || defined (HAVE_NS) || USE_MAC_TOOLBAR
13966 redisplay_tool_bar_p = FRAME_EXTERNAL_TOOL_BAR (f);
13967 #else
13968 redisplay_tool_bar_p = WINDOWP (f->tool_bar_window)
13969 && (FRAME_TOOL_BAR_LINES (f) > 0
13970 || !NILP (Vauto_resize_tool_bars));
13971 #endif
13972
13973 if (redisplay_tool_bar_p && redisplay_tool_bar (f))
13974 {
13975 extern int ignore_mouse_drag_p;
13976 ignore_mouse_drag_p = 1;
13977 }
13978 }
13979 #endif
13980 }
13981
13982 #ifdef HAVE_WINDOW_SYSTEM
13983 if (FRAME_WINDOW_P (f)
13984 && update_window_fringes (w, (just_this_one_p
13985 || (!used_current_matrix_p && !overlay_arrow_seen)
13986 || w->pseudo_window_p)))
13987 {
13988 update_begin (f);
13989 BLOCK_INPUT;
13990 if (draw_window_fringes (w, 1))
13991 x_draw_vertical_border (w);
13992 UNBLOCK_INPUT;
13993 update_end (f);
13994 }
13995 #endif /* HAVE_WINDOW_SYSTEM */
13996
13997 /* We go to this label, with fonts_changed_p nonzero,
13998 if it is necessary to try again using larger glyph matrices.
13999 We have to redeem the scroll bar even in this case,
14000 because the loop in redisplay_internal expects that. */
14001 need_larger_matrices:
14002 ;
14003 finish_scroll_bars:
14004
14005 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
14006 {
14007 /* Set the thumb's position and size. */
14008 set_vertical_scroll_bar (w);
14009
14010 /* Note that we actually used the scroll bar attached to this
14011 window, so it shouldn't be deleted at the end of redisplay. */
14012 if (FRAME_TERMINAL (f)->redeem_scroll_bar_hook)
14013 (*FRAME_TERMINAL (f)->redeem_scroll_bar_hook) (w);
14014 }
14015
14016 /* Restore current_buffer and value of point in it. */
14017 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
14018 set_buffer_internal_1 (old);
14019 /* Avoid an abort in TEMP_SET_PT_BOTH if the buffer has become
14020 shorter. This can be caused by log truncation in *Messages*. */
14021 if (CHARPOS (lpoint) <= ZV)
14022 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
14023
14024 unbind_to (count, Qnil);
14025 }
14026
14027
14028 /* Build the complete desired matrix of WINDOW with a window start
14029 buffer position POS.
14030
14031 Value is 1 if successful. It is zero if fonts were loaded during
14032 redisplay which makes re-adjusting glyph matrices necessary, and -1
14033 if point would appear in the scroll margins.
14034 (We check that only if CHECK_MARGINS is nonzero. */
14035
14036 int
14037 try_window (window, pos, check_margins)
14038 Lisp_Object window;
14039 struct text_pos pos;
14040 int check_margins;
14041 {
14042 struct window *w = XWINDOW (window);
14043 struct it it;
14044 struct glyph_row *last_text_row = NULL;
14045 struct frame *f = XFRAME (w->frame);
14046
14047 /* Make POS the new window start. */
14048 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
14049
14050 /* Mark cursor position as unknown. No overlay arrow seen. */
14051 w->cursor.vpos = -1;
14052 overlay_arrow_seen = 0;
14053
14054 /* Initialize iterator and info to start at POS. */
14055 start_display (&it, w, pos);
14056
14057 /* Display all lines of W. */
14058 while (it.current_y < it.last_visible_y)
14059 {
14060 if (display_line (&it))
14061 last_text_row = it.glyph_row - 1;
14062 if (fonts_changed_p)
14063 return 0;
14064 }
14065
14066 /* Don't let the cursor end in the scroll margins. */
14067 if (check_margins
14068 && !MINI_WINDOW_P (w))
14069 {
14070 int this_scroll_margin;
14071
14072 if (scroll_margin > 0)
14073 {
14074 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
14075 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
14076 }
14077 else
14078 this_scroll_margin = 0;
14079
14080 if ((w->cursor.y >= 0 /* not vscrolled */
14081 && w->cursor.y < this_scroll_margin
14082 && CHARPOS (pos) > BEGV
14083 && IT_CHARPOS (it) < ZV)
14084 /* rms: considering make_cursor_line_fully_visible_p here
14085 seems to give wrong results. We don't want to recenter
14086 when the last line is partly visible, we want to allow
14087 that case to be handled in the usual way. */
14088 || w->cursor.y > it.last_visible_y - this_scroll_margin - 1)
14089 {
14090 w->cursor.vpos = -1;
14091 clear_glyph_matrix (w->desired_matrix);
14092 return -1;
14093 }
14094 }
14095
14096 /* If bottom moved off end of frame, change mode line percentage. */
14097 if (XFASTINT (w->window_end_pos) <= 0
14098 && Z != IT_CHARPOS (it))
14099 w->update_mode_line = Qt;
14100
14101 /* Set window_end_pos to the offset of the last character displayed
14102 on the window from the end of current_buffer. Set
14103 window_end_vpos to its row number. */
14104 if (last_text_row)
14105 {
14106 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
14107 w->window_end_bytepos
14108 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
14109 w->window_end_pos
14110 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
14111 w->window_end_vpos
14112 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
14113 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
14114 ->displays_text_p);
14115 }
14116 else
14117 {
14118 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
14119 w->window_end_pos = make_number (Z - ZV);
14120 w->window_end_vpos = make_number (0);
14121 }
14122
14123 /* But that is not valid info until redisplay finishes. */
14124 w->window_end_valid = Qnil;
14125 return 1;
14126 }
14127
14128
14129 \f
14130 /************************************************************************
14131 Window redisplay reusing current matrix when buffer has not changed
14132 ************************************************************************/
14133
14134 /* Try redisplay of window W showing an unchanged buffer with a
14135 different window start than the last time it was displayed by
14136 reusing its current matrix. Value is non-zero if successful.
14137 W->start is the new window start. */
14138
14139 static int
14140 try_window_reusing_current_matrix (w)
14141 struct window *w;
14142 {
14143 struct frame *f = XFRAME (w->frame);
14144 struct glyph_row *row, *bottom_row;
14145 struct it it;
14146 struct run run;
14147 struct text_pos start, new_start;
14148 int nrows_scrolled, i;
14149 struct glyph_row *last_text_row;
14150 struct glyph_row *last_reused_text_row;
14151 struct glyph_row *start_row;
14152 int start_vpos, min_y, max_y;
14153
14154 #if GLYPH_DEBUG
14155 if (inhibit_try_window_reusing)
14156 return 0;
14157 #endif
14158
14159 if (/* This function doesn't handle terminal frames. */
14160 !FRAME_WINDOW_P (f)
14161 /* Don't try to reuse the display if windows have been split
14162 or such. */
14163 || windows_or_buffers_changed
14164 || cursor_type_changed)
14165 return 0;
14166
14167 /* Can't do this if region may have changed. */
14168 if ((!NILP (Vtransient_mark_mode)
14169 && !NILP (current_buffer->mark_active))
14170 || !NILP (w->region_showing)
14171 || !NILP (Vshow_trailing_whitespace))
14172 return 0;
14173
14174 /* If top-line visibility has changed, give up. */
14175 if (WINDOW_WANTS_HEADER_LINE_P (w)
14176 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
14177 return 0;
14178
14179 /* Give up if old or new display is scrolled vertically. We could
14180 make this function handle this, but right now it doesn't. */
14181 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
14182 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
14183 return 0;
14184
14185 /* The variable new_start now holds the new window start. The old
14186 start `start' can be determined from the current matrix. */
14187 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
14188 start = start_row->start.pos;
14189 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
14190
14191 /* Clear the desired matrix for the display below. */
14192 clear_glyph_matrix (w->desired_matrix);
14193
14194 if (CHARPOS (new_start) <= CHARPOS (start))
14195 {
14196 int first_row_y;
14197
14198 /* Don't use this method if the display starts with an ellipsis
14199 displayed for invisible text. It's not easy to handle that case
14200 below, and it's certainly not worth the effort since this is
14201 not a frequent case. */
14202 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
14203 return 0;
14204
14205 IF_DEBUG (debug_method_add (w, "twu1"));
14206
14207 /* Display up to a row that can be reused. The variable
14208 last_text_row is set to the last row displayed that displays
14209 text. Note that it.vpos == 0 if or if not there is a
14210 header-line; it's not the same as the MATRIX_ROW_VPOS! */
14211 start_display (&it, w, new_start);
14212 first_row_y = it.current_y;
14213 w->cursor.vpos = -1;
14214 last_text_row = last_reused_text_row = NULL;
14215
14216 while (it.current_y < it.last_visible_y
14217 && !fonts_changed_p)
14218 {
14219 /* If we have reached into the characters in the START row,
14220 that means the line boundaries have changed. So we
14221 can't start copying with the row START. Maybe it will
14222 work to start copying with the following row. */
14223 while (IT_CHARPOS (it) > CHARPOS (start))
14224 {
14225 /* Advance to the next row as the "start". */
14226 start_row++;
14227 start = start_row->start.pos;
14228 /* If there are no more rows to try, or just one, give up. */
14229 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
14230 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
14231 || CHARPOS (start) == ZV)
14232 {
14233 clear_glyph_matrix (w->desired_matrix);
14234 return 0;
14235 }
14236
14237 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
14238 }
14239 /* If we have reached alignment,
14240 we can copy the rest of the rows. */
14241 if (IT_CHARPOS (it) == CHARPOS (start))
14242 break;
14243
14244 if (display_line (&it))
14245 last_text_row = it.glyph_row - 1;
14246 }
14247
14248 /* A value of current_y < last_visible_y means that we stopped
14249 at the previous window start, which in turn means that we
14250 have at least one reusable row. */
14251 if (it.current_y < it.last_visible_y)
14252 {
14253 /* IT.vpos always starts from 0; it counts text lines. */
14254 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
14255
14256 /* Find PT if not already found in the lines displayed. */
14257 if (w->cursor.vpos < 0)
14258 {
14259 int dy = it.current_y - start_row->y;
14260
14261 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
14262 row = row_containing_pos (w, PT, row, NULL, dy);
14263 if (row)
14264 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
14265 dy, nrows_scrolled);
14266 else
14267 {
14268 clear_glyph_matrix (w->desired_matrix);
14269 return 0;
14270 }
14271 }
14272
14273 /* Scroll the display. Do it before the current matrix is
14274 changed. The problem here is that update has not yet
14275 run, i.e. part of the current matrix is not up to date.
14276 scroll_run_hook will clear the cursor, and use the
14277 current matrix to get the height of the row the cursor is
14278 in. */
14279 run.current_y = start_row->y;
14280 run.desired_y = it.current_y;
14281 run.height = it.last_visible_y - it.current_y;
14282
14283 if (run.height > 0 && run.current_y != run.desired_y)
14284 {
14285 update_begin (f);
14286 FRAME_RIF (f)->update_window_begin_hook (w);
14287 FRAME_RIF (f)->clear_window_mouse_face (w);
14288 FRAME_RIF (f)->scroll_run_hook (w, &run);
14289 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
14290 update_end (f);
14291 }
14292
14293 /* Shift current matrix down by nrows_scrolled lines. */
14294 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
14295 rotate_matrix (w->current_matrix,
14296 start_vpos,
14297 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
14298 nrows_scrolled);
14299
14300 /* Disable lines that must be updated. */
14301 for (i = 0; i < nrows_scrolled; ++i)
14302 (start_row + i)->enabled_p = 0;
14303
14304 /* Re-compute Y positions. */
14305 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
14306 max_y = it.last_visible_y;
14307 for (row = start_row + nrows_scrolled;
14308 row < bottom_row;
14309 ++row)
14310 {
14311 row->y = it.current_y;
14312 row->visible_height = row->height;
14313
14314 if (row->y < min_y)
14315 row->visible_height -= min_y - row->y;
14316 if (row->y + row->height > max_y)
14317 row->visible_height -= row->y + row->height - max_y;
14318 row->redraw_fringe_bitmaps_p = 1;
14319
14320 it.current_y += row->height;
14321
14322 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
14323 last_reused_text_row = row;
14324 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
14325 break;
14326 }
14327
14328 /* Disable lines in the current matrix which are now
14329 below the window. */
14330 for (++row; row < bottom_row; ++row)
14331 row->enabled_p = row->mode_line_p = 0;
14332 }
14333
14334 /* Update window_end_pos etc.; last_reused_text_row is the last
14335 reused row from the current matrix containing text, if any.
14336 The value of last_text_row is the last displayed line
14337 containing text. */
14338 if (last_reused_text_row)
14339 {
14340 w->window_end_bytepos
14341 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
14342 w->window_end_pos
14343 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
14344 w->window_end_vpos
14345 = make_number (MATRIX_ROW_VPOS (last_reused_text_row,
14346 w->current_matrix));
14347 }
14348 else if (last_text_row)
14349 {
14350 w->window_end_bytepos
14351 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
14352 w->window_end_pos
14353 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
14354 w->window_end_vpos
14355 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
14356 }
14357 else
14358 {
14359 /* This window must be completely empty. */
14360 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
14361 w->window_end_pos = make_number (Z - ZV);
14362 w->window_end_vpos = make_number (0);
14363 }
14364 w->window_end_valid = Qnil;
14365
14366 /* Update hint: don't try scrolling again in update_window. */
14367 w->desired_matrix->no_scrolling_p = 1;
14368
14369 #if GLYPH_DEBUG
14370 debug_method_add (w, "try_window_reusing_current_matrix 1");
14371 #endif
14372 return 1;
14373 }
14374 else if (CHARPOS (new_start) > CHARPOS (start))
14375 {
14376 struct glyph_row *pt_row, *row;
14377 struct glyph_row *first_reusable_row;
14378 struct glyph_row *first_row_to_display;
14379 int dy;
14380 int yb = window_text_bottom_y (w);
14381
14382 /* Find the row starting at new_start, if there is one. Don't
14383 reuse a partially visible line at the end. */
14384 first_reusable_row = start_row;
14385 while (first_reusable_row->enabled_p
14386 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
14387 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
14388 < CHARPOS (new_start)))
14389 ++first_reusable_row;
14390
14391 /* Give up if there is no row to reuse. */
14392 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
14393 || !first_reusable_row->enabled_p
14394 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
14395 != CHARPOS (new_start)))
14396 return 0;
14397
14398 /* We can reuse fully visible rows beginning with
14399 first_reusable_row to the end of the window. Set
14400 first_row_to_display to the first row that cannot be reused.
14401 Set pt_row to the row containing point, if there is any. */
14402 pt_row = NULL;
14403 for (first_row_to_display = first_reusable_row;
14404 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
14405 ++first_row_to_display)
14406 {
14407 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
14408 && PT < MATRIX_ROW_END_CHARPOS (first_row_to_display))
14409 pt_row = first_row_to_display;
14410 }
14411
14412 /* Start displaying at the start of first_row_to_display. */
14413 xassert (first_row_to_display->y < yb);
14414 init_to_row_start (&it, w, first_row_to_display);
14415
14416 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
14417 - start_vpos);
14418 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
14419 - nrows_scrolled);
14420 it.current_y = (first_row_to_display->y - first_reusable_row->y
14421 + WINDOW_HEADER_LINE_HEIGHT (w));
14422
14423 /* Display lines beginning with first_row_to_display in the
14424 desired matrix. Set last_text_row to the last row displayed
14425 that displays text. */
14426 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
14427 if (pt_row == NULL)
14428 w->cursor.vpos = -1;
14429 last_text_row = NULL;
14430 while (it.current_y < it.last_visible_y && !fonts_changed_p)
14431 if (display_line (&it))
14432 last_text_row = it.glyph_row - 1;
14433
14434 /* Give up If point isn't in a row displayed or reused. */
14435 if (w->cursor.vpos < 0)
14436 {
14437 clear_glyph_matrix (w->desired_matrix);
14438 return 0;
14439 }
14440
14441 /* If point is in a reused row, adjust y and vpos of the cursor
14442 position. */
14443 if (pt_row)
14444 {
14445 w->cursor.vpos -= nrows_scrolled;
14446 w->cursor.y -= first_reusable_row->y - start_row->y;
14447 }
14448
14449 /* Scroll the display. */
14450 run.current_y = first_reusable_row->y;
14451 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
14452 run.height = it.last_visible_y - run.current_y;
14453 dy = run.current_y - run.desired_y;
14454
14455 if (run.height)
14456 {
14457 update_begin (f);
14458 FRAME_RIF (f)->update_window_begin_hook (w);
14459 FRAME_RIF (f)->clear_window_mouse_face (w);
14460 FRAME_RIF (f)->scroll_run_hook (w, &run);
14461 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
14462 update_end (f);
14463 }
14464
14465 /* Adjust Y positions of reused rows. */
14466 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
14467 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
14468 max_y = it.last_visible_y;
14469 for (row = first_reusable_row; row < first_row_to_display; ++row)
14470 {
14471 row->y -= dy;
14472 row->visible_height = row->height;
14473 if (row->y < min_y)
14474 row->visible_height -= min_y - row->y;
14475 if (row->y + row->height > max_y)
14476 row->visible_height -= row->y + row->height - max_y;
14477 row->redraw_fringe_bitmaps_p = 1;
14478 }
14479
14480 /* Scroll the current matrix. */
14481 xassert (nrows_scrolled > 0);
14482 rotate_matrix (w->current_matrix,
14483 start_vpos,
14484 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
14485 -nrows_scrolled);
14486
14487 /* Disable rows not reused. */
14488 for (row -= nrows_scrolled; row < bottom_row; ++row)
14489 row->enabled_p = 0;
14490
14491 /* Point may have moved to a different line, so we cannot assume that
14492 the previous cursor position is valid; locate the correct row. */
14493 if (pt_row)
14494 {
14495 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
14496 row < bottom_row && PT >= MATRIX_ROW_END_CHARPOS (row);
14497 row++)
14498 {
14499 w->cursor.vpos++;
14500 w->cursor.y = row->y;
14501 }
14502 if (row < bottom_row)
14503 {
14504 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
14505 while (glyph->charpos < PT)
14506 {
14507 w->cursor.hpos++;
14508 w->cursor.x += glyph->pixel_width;
14509 glyph++;
14510 }
14511 }
14512 }
14513
14514 /* Adjust window end. A null value of last_text_row means that
14515 the window end is in reused rows which in turn means that
14516 only its vpos can have changed. */
14517 if (last_text_row)
14518 {
14519 w->window_end_bytepos
14520 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
14521 w->window_end_pos
14522 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
14523 w->window_end_vpos
14524 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
14525 }
14526 else
14527 {
14528 w->window_end_vpos
14529 = make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled);
14530 }
14531
14532 w->window_end_valid = Qnil;
14533 w->desired_matrix->no_scrolling_p = 1;
14534
14535 #if GLYPH_DEBUG
14536 debug_method_add (w, "try_window_reusing_current_matrix 2");
14537 #endif
14538 return 1;
14539 }
14540
14541 return 0;
14542 }
14543
14544
14545 \f
14546 /************************************************************************
14547 Window redisplay reusing current matrix when buffer has changed
14548 ************************************************************************/
14549
14550 static struct glyph_row *find_last_unchanged_at_beg_row P_ ((struct window *));
14551 static struct glyph_row *find_first_unchanged_at_end_row P_ ((struct window *,
14552 int *, int *));
14553 static struct glyph_row *
14554 find_last_row_displaying_text P_ ((struct glyph_matrix *, struct it *,
14555 struct glyph_row *));
14556
14557
14558 /* Return the last row in MATRIX displaying text. If row START is
14559 non-null, start searching with that row. IT gives the dimensions
14560 of the display. Value is null if matrix is empty; otherwise it is
14561 a pointer to the row found. */
14562
14563 static struct glyph_row *
14564 find_last_row_displaying_text (matrix, it, start)
14565 struct glyph_matrix *matrix;
14566 struct it *it;
14567 struct glyph_row *start;
14568 {
14569 struct glyph_row *row, *row_found;
14570
14571 /* Set row_found to the last row in IT->w's current matrix
14572 displaying text. The loop looks funny but think of partially
14573 visible lines. */
14574 row_found = NULL;
14575 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
14576 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
14577 {
14578 xassert (row->enabled_p);
14579 row_found = row;
14580 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
14581 break;
14582 ++row;
14583 }
14584
14585 return row_found;
14586 }
14587
14588
14589 /* Return the last row in the current matrix of W that is not affected
14590 by changes at the start of current_buffer that occurred since W's
14591 current matrix was built. Value is null if no such row exists.
14592
14593 BEG_UNCHANGED us the number of characters unchanged at the start of
14594 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
14595 first changed character in current_buffer. Characters at positions <
14596 BEG + BEG_UNCHANGED are at the same buffer positions as they were
14597 when the current matrix was built. */
14598
14599 static struct glyph_row *
14600 find_last_unchanged_at_beg_row (w)
14601 struct window *w;
14602 {
14603 int first_changed_pos = BEG + BEG_UNCHANGED;
14604 struct glyph_row *row;
14605 struct glyph_row *row_found = NULL;
14606 int yb = window_text_bottom_y (w);
14607
14608 /* Find the last row displaying unchanged text. */
14609 for (row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
14610 MATRIX_ROW_DISPLAYS_TEXT_P (row)
14611 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos;
14612 ++row)
14613 {
14614 if (/* If row ends before first_changed_pos, it is unchanged,
14615 except in some case. */
14616 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
14617 /* When row ends in ZV and we write at ZV it is not
14618 unchanged. */
14619 && !row->ends_at_zv_p
14620 /* When first_changed_pos is the end of a continued line,
14621 row is not unchanged because it may be no longer
14622 continued. */
14623 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
14624 && (row->continued_p
14625 || row->exact_window_width_line_p)))
14626 row_found = row;
14627
14628 /* Stop if last visible row. */
14629 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
14630 break;
14631 }
14632
14633 return row_found;
14634 }
14635
14636
14637 /* Find the first glyph row in the current matrix of W that is not
14638 affected by changes at the end of current_buffer since the
14639 time W's current matrix was built.
14640
14641 Return in *DELTA the number of chars by which buffer positions in
14642 unchanged text at the end of current_buffer must be adjusted.
14643
14644 Return in *DELTA_BYTES the corresponding number of bytes.
14645
14646 Value is null if no such row exists, i.e. all rows are affected by
14647 changes. */
14648
14649 static struct glyph_row *
14650 find_first_unchanged_at_end_row (w, delta, delta_bytes)
14651 struct window *w;
14652 int *delta, *delta_bytes;
14653 {
14654 struct glyph_row *row;
14655 struct glyph_row *row_found = NULL;
14656
14657 *delta = *delta_bytes = 0;
14658
14659 /* Display must not have been paused, otherwise the current matrix
14660 is not up to date. */
14661 eassert (!NILP (w->window_end_valid));
14662
14663 /* A value of window_end_pos >= END_UNCHANGED means that the window
14664 end is in the range of changed text. If so, there is no
14665 unchanged row at the end of W's current matrix. */
14666 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
14667 return NULL;
14668
14669 /* Set row to the last row in W's current matrix displaying text. */
14670 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
14671
14672 /* If matrix is entirely empty, no unchanged row exists. */
14673 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
14674 {
14675 /* The value of row is the last glyph row in the matrix having a
14676 meaningful buffer position in it. The end position of row
14677 corresponds to window_end_pos. This allows us to translate
14678 buffer positions in the current matrix to current buffer
14679 positions for characters not in changed text. */
14680 int Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
14681 int Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
14682 int last_unchanged_pos, last_unchanged_pos_old;
14683 struct glyph_row *first_text_row
14684 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
14685
14686 *delta = Z - Z_old;
14687 *delta_bytes = Z_BYTE - Z_BYTE_old;
14688
14689 /* Set last_unchanged_pos to the buffer position of the last
14690 character in the buffer that has not been changed. Z is the
14691 index + 1 of the last character in current_buffer, i.e. by
14692 subtracting END_UNCHANGED we get the index of the last
14693 unchanged character, and we have to add BEG to get its buffer
14694 position. */
14695 last_unchanged_pos = Z - END_UNCHANGED + BEG;
14696 last_unchanged_pos_old = last_unchanged_pos - *delta;
14697
14698 /* Search backward from ROW for a row displaying a line that
14699 starts at a minimum position >= last_unchanged_pos_old. */
14700 for (; row > first_text_row; --row)
14701 {
14702 /* This used to abort, but it can happen.
14703 It is ok to just stop the search instead here. KFS. */
14704 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
14705 break;
14706
14707 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
14708 row_found = row;
14709 }
14710 }
14711
14712 eassert (!row_found || MATRIX_ROW_DISPLAYS_TEXT_P (row_found));
14713
14714 return row_found;
14715 }
14716
14717
14718 /* Make sure that glyph rows in the current matrix of window W
14719 reference the same glyph memory as corresponding rows in the
14720 frame's frame matrix. This function is called after scrolling W's
14721 current matrix on a terminal frame in try_window_id and
14722 try_window_reusing_current_matrix. */
14723
14724 static void
14725 sync_frame_with_window_matrix_rows (w)
14726 struct window *w;
14727 {
14728 struct frame *f = XFRAME (w->frame);
14729 struct glyph_row *window_row, *window_row_end, *frame_row;
14730
14731 /* Preconditions: W must be a leaf window and full-width. Its frame
14732 must have a frame matrix. */
14733 xassert (NILP (w->hchild) && NILP (w->vchild));
14734 xassert (WINDOW_FULL_WIDTH_P (w));
14735 xassert (!FRAME_WINDOW_P (f));
14736
14737 /* If W is a full-width window, glyph pointers in W's current matrix
14738 have, by definition, to be the same as glyph pointers in the
14739 corresponding frame matrix. Note that frame matrices have no
14740 marginal areas (see build_frame_matrix). */
14741 window_row = w->current_matrix->rows;
14742 window_row_end = window_row + w->current_matrix->nrows;
14743 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
14744 while (window_row < window_row_end)
14745 {
14746 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
14747 struct glyph *end = window_row->glyphs[LAST_AREA];
14748
14749 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
14750 frame_row->glyphs[TEXT_AREA] = start;
14751 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
14752 frame_row->glyphs[LAST_AREA] = end;
14753
14754 /* Disable frame rows whose corresponding window rows have
14755 been disabled in try_window_id. */
14756 if (!window_row->enabled_p)
14757 frame_row->enabled_p = 0;
14758
14759 ++window_row, ++frame_row;
14760 }
14761 }
14762
14763
14764 /* Find the glyph row in window W containing CHARPOS. Consider all
14765 rows between START and END (not inclusive). END null means search
14766 all rows to the end of the display area of W. Value is the row
14767 containing CHARPOS or null. */
14768
14769 struct glyph_row *
14770 row_containing_pos (w, charpos, start, end, dy)
14771 struct window *w;
14772 int charpos;
14773 struct glyph_row *start, *end;
14774 int dy;
14775 {
14776 struct glyph_row *row = start;
14777 int last_y;
14778
14779 /* If we happen to start on a header-line, skip that. */
14780 if (row->mode_line_p)
14781 ++row;
14782
14783 if ((end && row >= end) || !row->enabled_p)
14784 return NULL;
14785
14786 last_y = window_text_bottom_y (w) - dy;
14787
14788 while (1)
14789 {
14790 /* Give up if we have gone too far. */
14791 if (end && row >= end)
14792 return NULL;
14793 /* This formerly returned if they were equal.
14794 I think that both quantities are of a "last plus one" type;
14795 if so, when they are equal, the row is within the screen. -- rms. */
14796 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
14797 return NULL;
14798
14799 /* If it is in this row, return this row. */
14800 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
14801 || (MATRIX_ROW_END_CHARPOS (row) == charpos
14802 /* The end position of a row equals the start
14803 position of the next row. If CHARPOS is there, we
14804 would rather display it in the next line, except
14805 when this line ends in ZV. */
14806 && !row->ends_at_zv_p
14807 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
14808 && charpos >= MATRIX_ROW_START_CHARPOS (row))
14809 return row;
14810 ++row;
14811 }
14812 }
14813
14814
14815 /* Try to redisplay window W by reusing its existing display. W's
14816 current matrix must be up to date when this function is called,
14817 i.e. window_end_valid must not be nil.
14818
14819 Value is
14820
14821 1 if display has been updated
14822 0 if otherwise unsuccessful
14823 -1 if redisplay with same window start is known not to succeed
14824
14825 The following steps are performed:
14826
14827 1. Find the last row in the current matrix of W that is not
14828 affected by changes at the start of current_buffer. If no such row
14829 is found, give up.
14830
14831 2. Find the first row in W's current matrix that is not affected by
14832 changes at the end of current_buffer. Maybe there is no such row.
14833
14834 3. Display lines beginning with the row + 1 found in step 1 to the
14835 row found in step 2 or, if step 2 didn't find a row, to the end of
14836 the window.
14837
14838 4. If cursor is not known to appear on the window, give up.
14839
14840 5. If display stopped at the row found in step 2, scroll the
14841 display and current matrix as needed.
14842
14843 6. Maybe display some lines at the end of W, if we must. This can
14844 happen under various circumstances, like a partially visible line
14845 becoming fully visible, or because newly displayed lines are displayed
14846 in smaller font sizes.
14847
14848 7. Update W's window end information. */
14849
14850 static int
14851 try_window_id (w)
14852 struct window *w;
14853 {
14854 struct frame *f = XFRAME (w->frame);
14855 struct glyph_matrix *current_matrix = w->current_matrix;
14856 struct glyph_matrix *desired_matrix = w->desired_matrix;
14857 struct glyph_row *last_unchanged_at_beg_row;
14858 struct glyph_row *first_unchanged_at_end_row;
14859 struct glyph_row *row;
14860 struct glyph_row *bottom_row;
14861 int bottom_vpos;
14862 struct it it;
14863 int delta = 0, delta_bytes = 0, stop_pos, dvpos, dy;
14864 struct text_pos start_pos;
14865 struct run run;
14866 int first_unchanged_at_end_vpos = 0;
14867 struct glyph_row *last_text_row, *last_text_row_at_end;
14868 struct text_pos start;
14869 int first_changed_charpos, last_changed_charpos;
14870
14871 #if GLYPH_DEBUG
14872 if (inhibit_try_window_id)
14873 return 0;
14874 #endif
14875
14876 /* This is handy for debugging. */
14877 #if 0
14878 #define GIVE_UP(X) \
14879 do { \
14880 fprintf (stderr, "try_window_id give up %d\n", (X)); \
14881 return 0; \
14882 } while (0)
14883 #else
14884 #define GIVE_UP(X) return 0
14885 #endif
14886
14887 SET_TEXT_POS_FROM_MARKER (start, w->start);
14888
14889 /* Don't use this for mini-windows because these can show
14890 messages and mini-buffers, and we don't handle that here. */
14891 if (MINI_WINDOW_P (w))
14892 GIVE_UP (1);
14893
14894 /* This flag is used to prevent redisplay optimizations. */
14895 if (windows_or_buffers_changed || cursor_type_changed)
14896 GIVE_UP (2);
14897
14898 /* Verify that narrowing has not changed.
14899 Also verify that we were not told to prevent redisplay optimizations.
14900 It would be nice to further
14901 reduce the number of cases where this prevents try_window_id. */
14902 if (current_buffer->clip_changed
14903 || current_buffer->prevent_redisplay_optimizations_p)
14904 GIVE_UP (3);
14905
14906 /* Window must either use window-based redisplay or be full width. */
14907 if (!FRAME_WINDOW_P (f)
14908 && (!FRAME_LINE_INS_DEL_OK (f)
14909 || !WINDOW_FULL_WIDTH_P (w)))
14910 GIVE_UP (4);
14911
14912 /* Give up if point is not known NOT to appear in W. */
14913 if (PT < CHARPOS (start))
14914 GIVE_UP (5);
14915
14916 /* Another way to prevent redisplay optimizations. */
14917 if (XFASTINT (w->last_modified) == 0)
14918 GIVE_UP (6);
14919
14920 /* Verify that window is not hscrolled. */
14921 if (XFASTINT (w->hscroll) != 0)
14922 GIVE_UP (7);
14923
14924 /* Verify that display wasn't paused. */
14925 if (NILP (w->window_end_valid))
14926 GIVE_UP (8);
14927
14928 /* Can't use this if highlighting a region because a cursor movement
14929 will do more than just set the cursor. */
14930 if (!NILP (Vtransient_mark_mode)
14931 && !NILP (current_buffer->mark_active))
14932 GIVE_UP (9);
14933
14934 /* Likewise if highlighting trailing whitespace. */
14935 if (!NILP (Vshow_trailing_whitespace))
14936 GIVE_UP (11);
14937
14938 /* Likewise if showing a region. */
14939 if (!NILP (w->region_showing))
14940 GIVE_UP (10);
14941
14942 /* Can use this if overlay arrow position and or string have changed. */
14943 if (overlay_arrows_changed_p ())
14944 GIVE_UP (12);
14945
14946 /* When word-wrap is on, adding a space to the first word of a
14947 wrapped line can change the wrap position, altering the line
14948 above it. It might be worthwhile to handle this more
14949 intelligently, but for now just redisplay from scratch. */
14950 if (!NILP (XBUFFER (w->buffer)->word_wrap))
14951 GIVE_UP (21);
14952
14953 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
14954 only if buffer has really changed. The reason is that the gap is
14955 initially at Z for freshly visited files. The code below would
14956 set end_unchanged to 0 in that case. */
14957 if (MODIFF > SAVE_MODIFF
14958 /* This seems to happen sometimes after saving a buffer. */
14959 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
14960 {
14961 if (GPT - BEG < BEG_UNCHANGED)
14962 BEG_UNCHANGED = GPT - BEG;
14963 if (Z - GPT < END_UNCHANGED)
14964 END_UNCHANGED = Z - GPT;
14965 }
14966
14967 /* The position of the first and last character that has been changed. */
14968 first_changed_charpos = BEG + BEG_UNCHANGED;
14969 last_changed_charpos = Z - END_UNCHANGED;
14970
14971 /* If window starts after a line end, and the last change is in
14972 front of that newline, then changes don't affect the display.
14973 This case happens with stealth-fontification. Note that although
14974 the display is unchanged, glyph positions in the matrix have to
14975 be adjusted, of course. */
14976 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
14977 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
14978 && ((last_changed_charpos < CHARPOS (start)
14979 && CHARPOS (start) == BEGV)
14980 || (last_changed_charpos < CHARPOS (start) - 1
14981 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
14982 {
14983 int Z_old, delta, Z_BYTE_old, delta_bytes;
14984 struct glyph_row *r0;
14985
14986 /* Compute how many chars/bytes have been added to or removed
14987 from the buffer. */
14988 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
14989 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
14990 delta = Z - Z_old;
14991 delta_bytes = Z_BYTE - Z_BYTE_old;
14992
14993 /* Give up if PT is not in the window. Note that it already has
14994 been checked at the start of try_window_id that PT is not in
14995 front of the window start. */
14996 if (PT >= MATRIX_ROW_END_CHARPOS (row) + delta)
14997 GIVE_UP (13);
14998
14999 /* If window start is unchanged, we can reuse the whole matrix
15000 as is, after adjusting glyph positions. No need to compute
15001 the window end again, since its offset from Z hasn't changed. */
15002 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
15003 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + delta
15004 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + delta_bytes
15005 /* PT must not be in a partially visible line. */
15006 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + delta
15007 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
15008 {
15009 /* Adjust positions in the glyph matrix. */
15010 if (delta || delta_bytes)
15011 {
15012 struct glyph_row *r1
15013 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
15014 increment_matrix_positions (w->current_matrix,
15015 MATRIX_ROW_VPOS (r0, current_matrix),
15016 MATRIX_ROW_VPOS (r1, current_matrix),
15017 delta, delta_bytes);
15018 }
15019
15020 /* Set the cursor. */
15021 row = row_containing_pos (w, PT, r0, NULL, 0);
15022 if (row)
15023 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
15024 else
15025 abort ();
15026 return 1;
15027 }
15028 }
15029
15030 /* Handle the case that changes are all below what is displayed in
15031 the window, and that PT is in the window. This shortcut cannot
15032 be taken if ZV is visible in the window, and text has been added
15033 there that is visible in the window. */
15034 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
15035 /* ZV is not visible in the window, or there are no
15036 changes at ZV, actually. */
15037 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
15038 || first_changed_charpos == last_changed_charpos))
15039 {
15040 struct glyph_row *r0;
15041
15042 /* Give up if PT is not in the window. Note that it already has
15043 been checked at the start of try_window_id that PT is not in
15044 front of the window start. */
15045 if (PT >= MATRIX_ROW_END_CHARPOS (row))
15046 GIVE_UP (14);
15047
15048 /* If window start is unchanged, we can reuse the whole matrix
15049 as is, without changing glyph positions since no text has
15050 been added/removed in front of the window end. */
15051 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
15052 if (TEXT_POS_EQUAL_P (start, r0->start.pos)
15053 /* PT must not be in a partially visible line. */
15054 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
15055 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
15056 {
15057 /* We have to compute the window end anew since text
15058 can have been added/removed after it. */
15059 w->window_end_pos
15060 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
15061 w->window_end_bytepos
15062 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
15063
15064 /* Set the cursor. */
15065 row = row_containing_pos (w, PT, r0, NULL, 0);
15066 if (row)
15067 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
15068 else
15069 abort ();
15070 return 2;
15071 }
15072 }
15073
15074 /* Give up if window start is in the changed area.
15075
15076 The condition used to read
15077
15078 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
15079
15080 but why that was tested escapes me at the moment. */
15081 if (CHARPOS (start) >= first_changed_charpos
15082 && CHARPOS (start) <= last_changed_charpos)
15083 GIVE_UP (15);
15084
15085 /* Check that window start agrees with the start of the first glyph
15086 row in its current matrix. Check this after we know the window
15087 start is not in changed text, otherwise positions would not be
15088 comparable. */
15089 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
15090 if (!TEXT_POS_EQUAL_P (start, row->start.pos))
15091 GIVE_UP (16);
15092
15093 /* Give up if the window ends in strings. Overlay strings
15094 at the end are difficult to handle, so don't try. */
15095 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
15096 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
15097 GIVE_UP (20);
15098
15099 /* Compute the position at which we have to start displaying new
15100 lines. Some of the lines at the top of the window might be
15101 reusable because they are not displaying changed text. Find the
15102 last row in W's current matrix not affected by changes at the
15103 start of current_buffer. Value is null if changes start in the
15104 first line of window. */
15105 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
15106 if (last_unchanged_at_beg_row)
15107 {
15108 /* Avoid starting to display in the moddle of a character, a TAB
15109 for instance. This is easier than to set up the iterator
15110 exactly, and it's not a frequent case, so the additional
15111 effort wouldn't really pay off. */
15112 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
15113 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
15114 && last_unchanged_at_beg_row > w->current_matrix->rows)
15115 --last_unchanged_at_beg_row;
15116
15117 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
15118 GIVE_UP (17);
15119
15120 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
15121 GIVE_UP (18);
15122 start_pos = it.current.pos;
15123
15124 /* Start displaying new lines in the desired matrix at the same
15125 vpos we would use in the current matrix, i.e. below
15126 last_unchanged_at_beg_row. */
15127 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
15128 current_matrix);
15129 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
15130 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
15131
15132 xassert (it.hpos == 0 && it.current_x == 0);
15133 }
15134 else
15135 {
15136 /* There are no reusable lines at the start of the window.
15137 Start displaying in the first text line. */
15138 start_display (&it, w, start);
15139 it.vpos = it.first_vpos;
15140 start_pos = it.current.pos;
15141 }
15142
15143 /* Find the first row that is not affected by changes at the end of
15144 the buffer. Value will be null if there is no unchanged row, in
15145 which case we must redisplay to the end of the window. delta
15146 will be set to the value by which buffer positions beginning with
15147 first_unchanged_at_end_row have to be adjusted due to text
15148 changes. */
15149 first_unchanged_at_end_row
15150 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
15151 IF_DEBUG (debug_delta = delta);
15152 IF_DEBUG (debug_delta_bytes = delta_bytes);
15153
15154 /* Set stop_pos to the buffer position up to which we will have to
15155 display new lines. If first_unchanged_at_end_row != NULL, this
15156 is the buffer position of the start of the line displayed in that
15157 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
15158 that we don't stop at a buffer position. */
15159 stop_pos = 0;
15160 if (first_unchanged_at_end_row)
15161 {
15162 xassert (last_unchanged_at_beg_row == NULL
15163 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
15164
15165 /* If this is a continuation line, move forward to the next one
15166 that isn't. Changes in lines above affect this line.
15167 Caution: this may move first_unchanged_at_end_row to a row
15168 not displaying text. */
15169 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
15170 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
15171 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
15172 < it.last_visible_y))
15173 ++first_unchanged_at_end_row;
15174
15175 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
15176 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
15177 >= it.last_visible_y))
15178 first_unchanged_at_end_row = NULL;
15179 else
15180 {
15181 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
15182 + delta);
15183 first_unchanged_at_end_vpos
15184 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
15185 xassert (stop_pos >= Z - END_UNCHANGED);
15186 }
15187 }
15188 else if (last_unchanged_at_beg_row == NULL)
15189 GIVE_UP (19);
15190
15191
15192 #if GLYPH_DEBUG
15193
15194 /* Either there is no unchanged row at the end, or the one we have
15195 now displays text. This is a necessary condition for the window
15196 end pos calculation at the end of this function. */
15197 xassert (first_unchanged_at_end_row == NULL
15198 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
15199
15200 debug_last_unchanged_at_beg_vpos
15201 = (last_unchanged_at_beg_row
15202 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
15203 : -1);
15204 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
15205
15206 #endif /* GLYPH_DEBUG != 0 */
15207
15208
15209 /* Display new lines. Set last_text_row to the last new line
15210 displayed which has text on it, i.e. might end up as being the
15211 line where the window_end_vpos is. */
15212 w->cursor.vpos = -1;
15213 last_text_row = NULL;
15214 overlay_arrow_seen = 0;
15215 while (it.current_y < it.last_visible_y
15216 && !fonts_changed_p
15217 && (first_unchanged_at_end_row == NULL
15218 || IT_CHARPOS (it) < stop_pos))
15219 {
15220 if (display_line (&it))
15221 last_text_row = it.glyph_row - 1;
15222 }
15223
15224 if (fonts_changed_p)
15225 return -1;
15226
15227
15228 /* Compute differences in buffer positions, y-positions etc. for
15229 lines reused at the bottom of the window. Compute what we can
15230 scroll. */
15231 if (first_unchanged_at_end_row
15232 /* No lines reused because we displayed everything up to the
15233 bottom of the window. */
15234 && it.current_y < it.last_visible_y)
15235 {
15236 dvpos = (it.vpos
15237 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
15238 current_matrix));
15239 dy = it.current_y - first_unchanged_at_end_row->y;
15240 run.current_y = first_unchanged_at_end_row->y;
15241 run.desired_y = run.current_y + dy;
15242 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
15243 }
15244 else
15245 {
15246 delta = delta_bytes = dvpos = dy
15247 = run.current_y = run.desired_y = run.height = 0;
15248 first_unchanged_at_end_row = NULL;
15249 }
15250 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
15251
15252
15253 /* Find the cursor if not already found. We have to decide whether
15254 PT will appear on this window (it sometimes doesn't, but this is
15255 not a very frequent case.) This decision has to be made before
15256 the current matrix is altered. A value of cursor.vpos < 0 means
15257 that PT is either in one of the lines beginning at
15258 first_unchanged_at_end_row or below the window. Don't care for
15259 lines that might be displayed later at the window end; as
15260 mentioned, this is not a frequent case. */
15261 if (w->cursor.vpos < 0)
15262 {
15263 /* Cursor in unchanged rows at the top? */
15264 if (PT < CHARPOS (start_pos)
15265 && last_unchanged_at_beg_row)
15266 {
15267 row = row_containing_pos (w, PT,
15268 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
15269 last_unchanged_at_beg_row + 1, 0);
15270 if (row)
15271 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
15272 }
15273
15274 /* Start from first_unchanged_at_end_row looking for PT. */
15275 else if (first_unchanged_at_end_row)
15276 {
15277 row = row_containing_pos (w, PT - delta,
15278 first_unchanged_at_end_row, NULL, 0);
15279 if (row)
15280 set_cursor_from_row (w, row, w->current_matrix, delta,
15281 delta_bytes, dy, dvpos);
15282 }
15283
15284 /* Give up if cursor was not found. */
15285 if (w->cursor.vpos < 0)
15286 {
15287 clear_glyph_matrix (w->desired_matrix);
15288 return -1;
15289 }
15290 }
15291
15292 /* Don't let the cursor end in the scroll margins. */
15293 {
15294 int this_scroll_margin, cursor_height;
15295
15296 this_scroll_margin = max (0, scroll_margin);
15297 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
15298 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
15299 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
15300
15301 if ((w->cursor.y < this_scroll_margin
15302 && CHARPOS (start) > BEGV)
15303 /* Old redisplay didn't take scroll margin into account at the bottom,
15304 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
15305 || (w->cursor.y + (make_cursor_line_fully_visible_p
15306 ? cursor_height + this_scroll_margin
15307 : 1)) > it.last_visible_y)
15308 {
15309 w->cursor.vpos = -1;
15310 clear_glyph_matrix (w->desired_matrix);
15311 return -1;
15312 }
15313 }
15314
15315 /* Scroll the display. Do it before changing the current matrix so
15316 that xterm.c doesn't get confused about where the cursor glyph is
15317 found. */
15318 if (dy && run.height)
15319 {
15320 update_begin (f);
15321
15322 if (FRAME_WINDOW_P (f))
15323 {
15324 FRAME_RIF (f)->update_window_begin_hook (w);
15325 FRAME_RIF (f)->clear_window_mouse_face (w);
15326 FRAME_RIF (f)->scroll_run_hook (w, &run);
15327 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
15328 }
15329 else
15330 {
15331 /* Terminal frame. In this case, dvpos gives the number of
15332 lines to scroll by; dvpos < 0 means scroll up. */
15333 int first_unchanged_at_end_vpos
15334 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
15335 int from = WINDOW_TOP_EDGE_LINE (w) + first_unchanged_at_end_vpos;
15336 int end = (WINDOW_TOP_EDGE_LINE (w)
15337 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
15338 + window_internal_height (w));
15339
15340 /* Perform the operation on the screen. */
15341 if (dvpos > 0)
15342 {
15343 /* Scroll last_unchanged_at_beg_row to the end of the
15344 window down dvpos lines. */
15345 set_terminal_window (f, end);
15346
15347 /* On dumb terminals delete dvpos lines at the end
15348 before inserting dvpos empty lines. */
15349 if (!FRAME_SCROLL_REGION_OK (f))
15350 ins_del_lines (f, end - dvpos, -dvpos);
15351
15352 /* Insert dvpos empty lines in front of
15353 last_unchanged_at_beg_row. */
15354 ins_del_lines (f, from, dvpos);
15355 }
15356 else if (dvpos < 0)
15357 {
15358 /* Scroll up last_unchanged_at_beg_vpos to the end of
15359 the window to last_unchanged_at_beg_vpos - |dvpos|. */
15360 set_terminal_window (f, end);
15361
15362 /* Delete dvpos lines in front of
15363 last_unchanged_at_beg_vpos. ins_del_lines will set
15364 the cursor to the given vpos and emit |dvpos| delete
15365 line sequences. */
15366 ins_del_lines (f, from + dvpos, dvpos);
15367
15368 /* On a dumb terminal insert dvpos empty lines at the
15369 end. */
15370 if (!FRAME_SCROLL_REGION_OK (f))
15371 ins_del_lines (f, end + dvpos, -dvpos);
15372 }
15373
15374 set_terminal_window (f, 0);
15375 }
15376
15377 update_end (f);
15378 }
15379
15380 /* Shift reused rows of the current matrix to the right position.
15381 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
15382 text. */
15383 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
15384 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
15385 if (dvpos < 0)
15386 {
15387 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
15388 bottom_vpos, dvpos);
15389 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
15390 bottom_vpos, 0);
15391 }
15392 else if (dvpos > 0)
15393 {
15394 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
15395 bottom_vpos, dvpos);
15396 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
15397 first_unchanged_at_end_vpos + dvpos, 0);
15398 }
15399
15400 /* For frame-based redisplay, make sure that current frame and window
15401 matrix are in sync with respect to glyph memory. */
15402 if (!FRAME_WINDOW_P (f))
15403 sync_frame_with_window_matrix_rows (w);
15404
15405 /* Adjust buffer positions in reused rows. */
15406 if (delta || delta_bytes)
15407 increment_matrix_positions (current_matrix,
15408 first_unchanged_at_end_vpos + dvpos,
15409 bottom_vpos, delta, delta_bytes);
15410
15411 /* Adjust Y positions. */
15412 if (dy)
15413 shift_glyph_matrix (w, current_matrix,
15414 first_unchanged_at_end_vpos + dvpos,
15415 bottom_vpos, dy);
15416
15417 if (first_unchanged_at_end_row)
15418 {
15419 first_unchanged_at_end_row += dvpos;
15420 if (first_unchanged_at_end_row->y >= it.last_visible_y
15421 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
15422 first_unchanged_at_end_row = NULL;
15423 }
15424
15425 /* If scrolling up, there may be some lines to display at the end of
15426 the window. */
15427 last_text_row_at_end = NULL;
15428 if (dy < 0)
15429 {
15430 /* Scrolling up can leave for example a partially visible line
15431 at the end of the window to be redisplayed. */
15432 /* Set last_row to the glyph row in the current matrix where the
15433 window end line is found. It has been moved up or down in
15434 the matrix by dvpos. */
15435 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
15436 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
15437
15438 /* If last_row is the window end line, it should display text. */
15439 xassert (last_row->displays_text_p);
15440
15441 /* If window end line was partially visible before, begin
15442 displaying at that line. Otherwise begin displaying with the
15443 line following it. */
15444 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
15445 {
15446 init_to_row_start (&it, w, last_row);
15447 it.vpos = last_vpos;
15448 it.current_y = last_row->y;
15449 }
15450 else
15451 {
15452 init_to_row_end (&it, w, last_row);
15453 it.vpos = 1 + last_vpos;
15454 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
15455 ++last_row;
15456 }
15457
15458 /* We may start in a continuation line. If so, we have to
15459 get the right continuation_lines_width and current_x. */
15460 it.continuation_lines_width = last_row->continuation_lines_width;
15461 it.hpos = it.current_x = 0;
15462
15463 /* Display the rest of the lines at the window end. */
15464 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
15465 while (it.current_y < it.last_visible_y
15466 && !fonts_changed_p)
15467 {
15468 /* Is it always sure that the display agrees with lines in
15469 the current matrix? I don't think so, so we mark rows
15470 displayed invalid in the current matrix by setting their
15471 enabled_p flag to zero. */
15472 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
15473 if (display_line (&it))
15474 last_text_row_at_end = it.glyph_row - 1;
15475 }
15476 }
15477
15478 /* Update window_end_pos and window_end_vpos. */
15479 if (first_unchanged_at_end_row
15480 && !last_text_row_at_end)
15481 {
15482 /* Window end line if one of the preserved rows from the current
15483 matrix. Set row to the last row displaying text in current
15484 matrix starting at first_unchanged_at_end_row, after
15485 scrolling. */
15486 xassert (first_unchanged_at_end_row->displays_text_p);
15487 row = find_last_row_displaying_text (w->current_matrix, &it,
15488 first_unchanged_at_end_row);
15489 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
15490
15491 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
15492 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
15493 w->window_end_vpos
15494 = make_number (MATRIX_ROW_VPOS (row, w->current_matrix));
15495 xassert (w->window_end_bytepos >= 0);
15496 IF_DEBUG (debug_method_add (w, "A"));
15497 }
15498 else if (last_text_row_at_end)
15499 {
15500 w->window_end_pos
15501 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
15502 w->window_end_bytepos
15503 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
15504 w->window_end_vpos
15505 = make_number (MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
15506 xassert (w->window_end_bytepos >= 0);
15507 IF_DEBUG (debug_method_add (w, "B"));
15508 }
15509 else if (last_text_row)
15510 {
15511 /* We have displayed either to the end of the window or at the
15512 end of the window, i.e. the last row with text is to be found
15513 in the desired matrix. */
15514 w->window_end_pos
15515 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
15516 w->window_end_bytepos
15517 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
15518 w->window_end_vpos
15519 = make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix));
15520 xassert (w->window_end_bytepos >= 0);
15521 }
15522 else if (first_unchanged_at_end_row == NULL
15523 && last_text_row == NULL
15524 && last_text_row_at_end == NULL)
15525 {
15526 /* Displayed to end of window, but no line containing text was
15527 displayed. Lines were deleted at the end of the window. */
15528 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
15529 int vpos = XFASTINT (w->window_end_vpos);
15530 struct glyph_row *current_row = current_matrix->rows + vpos;
15531 struct glyph_row *desired_row = desired_matrix->rows + vpos;
15532
15533 for (row = NULL;
15534 row == NULL && vpos >= first_vpos;
15535 --vpos, --current_row, --desired_row)
15536 {
15537 if (desired_row->enabled_p)
15538 {
15539 if (desired_row->displays_text_p)
15540 row = desired_row;
15541 }
15542 else if (current_row->displays_text_p)
15543 row = current_row;
15544 }
15545
15546 xassert (row != NULL);
15547 w->window_end_vpos = make_number (vpos + 1);
15548 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
15549 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
15550 xassert (w->window_end_bytepos >= 0);
15551 IF_DEBUG (debug_method_add (w, "C"));
15552 }
15553 else
15554 abort ();
15555
15556 #if 0 /* This leads to problems, for instance when the cursor is
15557 at ZV, and the cursor line displays no text. */
15558 /* Disable rows below what's displayed in the window. This makes
15559 debugging easier. */
15560 enable_glyph_matrix_rows (current_matrix,
15561 XFASTINT (w->window_end_vpos) + 1,
15562 bottom_vpos, 0);
15563 #endif
15564
15565 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
15566 debug_end_vpos = XFASTINT (w->window_end_vpos));
15567
15568 /* Record that display has not been completed. */
15569 w->window_end_valid = Qnil;
15570 w->desired_matrix->no_scrolling_p = 1;
15571 return 3;
15572
15573 #undef GIVE_UP
15574 }
15575
15576
15577 \f
15578 /***********************************************************************
15579 More debugging support
15580 ***********************************************************************/
15581
15582 #if GLYPH_DEBUG
15583
15584 void dump_glyph_row P_ ((struct glyph_row *, int, int));
15585 void dump_glyph_matrix P_ ((struct glyph_matrix *, int));
15586 void dump_glyph P_ ((struct glyph_row *, struct glyph *, int));
15587
15588
15589 /* Dump the contents of glyph matrix MATRIX on stderr.
15590
15591 GLYPHS 0 means don't show glyph contents.
15592 GLYPHS 1 means show glyphs in short form
15593 GLYPHS > 1 means show glyphs in long form. */
15594
15595 void
15596 dump_glyph_matrix (matrix, glyphs)
15597 struct glyph_matrix *matrix;
15598 int glyphs;
15599 {
15600 int i;
15601 for (i = 0; i < matrix->nrows; ++i)
15602 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
15603 }
15604
15605
15606 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
15607 the glyph row and area where the glyph comes from. */
15608
15609 void
15610 dump_glyph (row, glyph, area)
15611 struct glyph_row *row;
15612 struct glyph *glyph;
15613 int area;
15614 {
15615 if (glyph->type == CHAR_GLYPH)
15616 {
15617 fprintf (stderr,
15618 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
15619 glyph - row->glyphs[TEXT_AREA],
15620 'C',
15621 glyph->charpos,
15622 (BUFFERP (glyph->object)
15623 ? 'B'
15624 : (STRINGP (glyph->object)
15625 ? 'S'
15626 : '-')),
15627 glyph->pixel_width,
15628 glyph->u.ch,
15629 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
15630 ? glyph->u.ch
15631 : '.'),
15632 glyph->face_id,
15633 glyph->left_box_line_p,
15634 glyph->right_box_line_p);
15635 }
15636 else if (glyph->type == STRETCH_GLYPH)
15637 {
15638 fprintf (stderr,
15639 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
15640 glyph - row->glyphs[TEXT_AREA],
15641 'S',
15642 glyph->charpos,
15643 (BUFFERP (glyph->object)
15644 ? 'B'
15645 : (STRINGP (glyph->object)
15646 ? 'S'
15647 : '-')),
15648 glyph->pixel_width,
15649 0,
15650 '.',
15651 glyph->face_id,
15652 glyph->left_box_line_p,
15653 glyph->right_box_line_p);
15654 }
15655 else if (glyph->type == IMAGE_GLYPH)
15656 {
15657 fprintf (stderr,
15658 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
15659 glyph - row->glyphs[TEXT_AREA],
15660 'I',
15661 glyph->charpos,
15662 (BUFFERP (glyph->object)
15663 ? 'B'
15664 : (STRINGP (glyph->object)
15665 ? 'S'
15666 : '-')),
15667 glyph->pixel_width,
15668 glyph->u.img_id,
15669 '.',
15670 glyph->face_id,
15671 glyph->left_box_line_p,
15672 glyph->right_box_line_p);
15673 }
15674 else if (glyph->type == COMPOSITE_GLYPH)
15675 {
15676 fprintf (stderr,
15677 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
15678 glyph - row->glyphs[TEXT_AREA],
15679 '+',
15680 glyph->charpos,
15681 (BUFFERP (glyph->object)
15682 ? 'B'
15683 : (STRINGP (glyph->object)
15684 ? 'S'
15685 : '-')),
15686 glyph->pixel_width,
15687 glyph->u.cmp_id,
15688 '.',
15689 glyph->face_id,
15690 glyph->left_box_line_p,
15691 glyph->right_box_line_p);
15692 }
15693 }
15694
15695
15696 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
15697 GLYPHS 0 means don't show glyph contents.
15698 GLYPHS 1 means show glyphs in short form
15699 GLYPHS > 1 means show glyphs in long form. */
15700
15701 void
15702 dump_glyph_row (row, vpos, glyphs)
15703 struct glyph_row *row;
15704 int vpos, glyphs;
15705 {
15706 if (glyphs != 1)
15707 {
15708 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
15709 fprintf (stderr, "======================================================================\n");
15710
15711 fprintf (stderr, "%3d %5d %5d %4d %1.1d%1.1d%1.1d%1.1d\
15712 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
15713 vpos,
15714 MATRIX_ROW_START_CHARPOS (row),
15715 MATRIX_ROW_END_CHARPOS (row),
15716 row->used[TEXT_AREA],
15717 row->contains_overlapping_glyphs_p,
15718 row->enabled_p,
15719 row->truncated_on_left_p,
15720 row->truncated_on_right_p,
15721 row->continued_p,
15722 MATRIX_ROW_CONTINUATION_LINE_P (row),
15723 row->displays_text_p,
15724 row->ends_at_zv_p,
15725 row->fill_line_p,
15726 row->ends_in_middle_of_char_p,
15727 row->starts_in_middle_of_char_p,
15728 row->mouse_face_p,
15729 row->x,
15730 row->y,
15731 row->pixel_width,
15732 row->height,
15733 row->visible_height,
15734 row->ascent,
15735 row->phys_ascent);
15736 fprintf (stderr, "%9d %5d\t%5d\n", row->start.overlay_string_index,
15737 row->end.overlay_string_index,
15738 row->continuation_lines_width);
15739 fprintf (stderr, "%9d %5d\n",
15740 CHARPOS (row->start.string_pos),
15741 CHARPOS (row->end.string_pos));
15742 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
15743 row->end.dpvec_index);
15744 }
15745
15746 if (glyphs > 1)
15747 {
15748 int area;
15749
15750 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
15751 {
15752 struct glyph *glyph = row->glyphs[area];
15753 struct glyph *glyph_end = glyph + row->used[area];
15754
15755 /* Glyph for a line end in text. */
15756 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
15757 ++glyph_end;
15758
15759 if (glyph < glyph_end)
15760 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
15761
15762 for (; glyph < glyph_end; ++glyph)
15763 dump_glyph (row, glyph, area);
15764 }
15765 }
15766 else if (glyphs == 1)
15767 {
15768 int area;
15769
15770 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
15771 {
15772 char *s = (char *) alloca (row->used[area] + 1);
15773 int i;
15774
15775 for (i = 0; i < row->used[area]; ++i)
15776 {
15777 struct glyph *glyph = row->glyphs[area] + i;
15778 if (glyph->type == CHAR_GLYPH
15779 && glyph->u.ch < 0x80
15780 && glyph->u.ch >= ' ')
15781 s[i] = glyph->u.ch;
15782 else
15783 s[i] = '.';
15784 }
15785
15786 s[i] = '\0';
15787 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
15788 }
15789 }
15790 }
15791
15792
15793 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
15794 Sdump_glyph_matrix, 0, 1, "p",
15795 doc: /* Dump the current matrix of the selected window to stderr.
15796 Shows contents of glyph row structures. With non-nil
15797 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
15798 glyphs in short form, otherwise show glyphs in long form. */)
15799 (glyphs)
15800 Lisp_Object glyphs;
15801 {
15802 struct window *w = XWINDOW (selected_window);
15803 struct buffer *buffer = XBUFFER (w->buffer);
15804
15805 fprintf (stderr, "PT = %d, BEGV = %d. ZV = %d\n",
15806 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
15807 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
15808 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
15809 fprintf (stderr, "=============================================\n");
15810 dump_glyph_matrix (w->current_matrix,
15811 NILP (glyphs) ? 0 : XINT (glyphs));
15812 return Qnil;
15813 }
15814
15815
15816 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
15817 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
15818 ()
15819 {
15820 struct frame *f = XFRAME (selected_frame);
15821 dump_glyph_matrix (f->current_matrix, 1);
15822 return Qnil;
15823 }
15824
15825
15826 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
15827 doc: /* Dump glyph row ROW to stderr.
15828 GLYPH 0 means don't dump glyphs.
15829 GLYPH 1 means dump glyphs in short form.
15830 GLYPH > 1 or omitted means dump glyphs in long form. */)
15831 (row, glyphs)
15832 Lisp_Object row, glyphs;
15833 {
15834 struct glyph_matrix *matrix;
15835 int vpos;
15836
15837 CHECK_NUMBER (row);
15838 matrix = XWINDOW (selected_window)->current_matrix;
15839 vpos = XINT (row);
15840 if (vpos >= 0 && vpos < matrix->nrows)
15841 dump_glyph_row (MATRIX_ROW (matrix, vpos),
15842 vpos,
15843 INTEGERP (glyphs) ? XINT (glyphs) : 2);
15844 return Qnil;
15845 }
15846
15847
15848 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
15849 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
15850 GLYPH 0 means don't dump glyphs.
15851 GLYPH 1 means dump glyphs in short form.
15852 GLYPH > 1 or omitted means dump glyphs in long form. */)
15853 (row, glyphs)
15854 Lisp_Object row, glyphs;
15855 {
15856 struct frame *sf = SELECTED_FRAME ();
15857 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
15858 int vpos;
15859
15860 CHECK_NUMBER (row);
15861 vpos = XINT (row);
15862 if (vpos >= 0 && vpos < m->nrows)
15863 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
15864 INTEGERP (glyphs) ? XINT (glyphs) : 2);
15865 return Qnil;
15866 }
15867
15868
15869 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
15870 doc: /* Toggle tracing of redisplay.
15871 With ARG, turn tracing on if and only if ARG is positive. */)
15872 (arg)
15873 Lisp_Object arg;
15874 {
15875 if (NILP (arg))
15876 trace_redisplay_p = !trace_redisplay_p;
15877 else
15878 {
15879 arg = Fprefix_numeric_value (arg);
15880 trace_redisplay_p = XINT (arg) > 0;
15881 }
15882
15883 return Qnil;
15884 }
15885
15886
15887 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
15888 doc: /* Like `format', but print result to stderr.
15889 usage: (trace-to-stderr STRING &rest OBJECTS) */)
15890 (nargs, args)
15891 int nargs;
15892 Lisp_Object *args;
15893 {
15894 Lisp_Object s = Fformat (nargs, args);
15895 fprintf (stderr, "%s", SDATA (s));
15896 return Qnil;
15897 }
15898
15899 #endif /* GLYPH_DEBUG */
15900
15901
15902 \f
15903 /***********************************************************************
15904 Building Desired Matrix Rows
15905 ***********************************************************************/
15906
15907 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
15908 Used for non-window-redisplay windows, and for windows w/o left fringe. */
15909
15910 static struct glyph_row *
15911 get_overlay_arrow_glyph_row (w, overlay_arrow_string)
15912 struct window *w;
15913 Lisp_Object overlay_arrow_string;
15914 {
15915 struct frame *f = XFRAME (WINDOW_FRAME (w));
15916 struct buffer *buffer = XBUFFER (w->buffer);
15917 struct buffer *old = current_buffer;
15918 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
15919 int arrow_len = SCHARS (overlay_arrow_string);
15920 const unsigned char *arrow_end = arrow_string + arrow_len;
15921 const unsigned char *p;
15922 struct it it;
15923 int multibyte_p;
15924 int n_glyphs_before;
15925
15926 set_buffer_temp (buffer);
15927 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
15928 it.glyph_row->used[TEXT_AREA] = 0;
15929 SET_TEXT_POS (it.position, 0, 0);
15930
15931 multibyte_p = !NILP (buffer->enable_multibyte_characters);
15932 p = arrow_string;
15933 while (p < arrow_end)
15934 {
15935 Lisp_Object face, ilisp;
15936
15937 /* Get the next character. */
15938 if (multibyte_p)
15939 it.c = string_char_and_length (p, arrow_len, &it.len);
15940 else
15941 it.c = *p, it.len = 1;
15942 p += it.len;
15943
15944 /* Get its face. */
15945 ilisp = make_number (p - arrow_string);
15946 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
15947 it.face_id = compute_char_face (f, it.c, face);
15948
15949 /* Compute its width, get its glyphs. */
15950 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
15951 SET_TEXT_POS (it.position, -1, -1);
15952 PRODUCE_GLYPHS (&it);
15953
15954 /* If this character doesn't fit any more in the line, we have
15955 to remove some glyphs. */
15956 if (it.current_x > it.last_visible_x)
15957 {
15958 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
15959 break;
15960 }
15961 }
15962
15963 set_buffer_temp (old);
15964 return it.glyph_row;
15965 }
15966
15967
15968 /* Insert truncation glyphs at the start of IT->glyph_row. Truncation
15969 glyphs are only inserted for terminal frames since we can't really
15970 win with truncation glyphs when partially visible glyphs are
15971 involved. Which glyphs to insert is determined by
15972 produce_special_glyphs. */
15973
15974 static void
15975 insert_left_trunc_glyphs (it)
15976 struct it *it;
15977 {
15978 struct it truncate_it;
15979 struct glyph *from, *end, *to, *toend;
15980
15981 xassert (!FRAME_WINDOW_P (it->f));
15982
15983 /* Get the truncation glyphs. */
15984 truncate_it = *it;
15985 truncate_it.current_x = 0;
15986 truncate_it.face_id = DEFAULT_FACE_ID;
15987 truncate_it.glyph_row = &scratch_glyph_row;
15988 truncate_it.glyph_row->used[TEXT_AREA] = 0;
15989 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
15990 truncate_it.object = make_number (0);
15991 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
15992
15993 /* Overwrite glyphs from IT with truncation glyphs. */
15994 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
15995 end = from + truncate_it.glyph_row->used[TEXT_AREA];
15996 to = it->glyph_row->glyphs[TEXT_AREA];
15997 toend = to + it->glyph_row->used[TEXT_AREA];
15998
15999 while (from < end)
16000 *to++ = *from++;
16001
16002 /* There may be padding glyphs left over. Overwrite them too. */
16003 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
16004 {
16005 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
16006 while (from < end)
16007 *to++ = *from++;
16008 }
16009
16010 if (to > toend)
16011 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
16012 }
16013
16014
16015 /* Compute the pixel height and width of IT->glyph_row.
16016
16017 Most of the time, ascent and height of a display line will be equal
16018 to the max_ascent and max_height values of the display iterator
16019 structure. This is not the case if
16020
16021 1. We hit ZV without displaying anything. In this case, max_ascent
16022 and max_height will be zero.
16023
16024 2. We have some glyphs that don't contribute to the line height.
16025 (The glyph row flag contributes_to_line_height_p is for future
16026 pixmap extensions).
16027
16028 The first case is easily covered by using default values because in
16029 these cases, the line height does not really matter, except that it
16030 must not be zero. */
16031
16032 static void
16033 compute_line_metrics (it)
16034 struct it *it;
16035 {
16036 struct glyph_row *row = it->glyph_row;
16037 int area, i;
16038
16039 if (FRAME_WINDOW_P (it->f))
16040 {
16041 int i, min_y, max_y;
16042
16043 /* The line may consist of one space only, that was added to
16044 place the cursor on it. If so, the row's height hasn't been
16045 computed yet. */
16046 if (row->height == 0)
16047 {
16048 if (it->max_ascent + it->max_descent == 0)
16049 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
16050 row->ascent = it->max_ascent;
16051 row->height = it->max_ascent + it->max_descent;
16052 row->phys_ascent = it->max_phys_ascent;
16053 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
16054 row->extra_line_spacing = it->max_extra_line_spacing;
16055 }
16056
16057 /* Compute the width of this line. */
16058 row->pixel_width = row->x;
16059 for (i = 0; i < row->used[TEXT_AREA]; ++i)
16060 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
16061
16062 xassert (row->pixel_width >= 0);
16063 xassert (row->ascent >= 0 && row->height > 0);
16064
16065 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
16066 || MATRIX_ROW_OVERLAPS_PRED_P (row));
16067
16068 /* If first line's physical ascent is larger than its logical
16069 ascent, use the physical ascent, and make the row taller.
16070 This makes accented characters fully visible. */
16071 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
16072 && row->phys_ascent > row->ascent)
16073 {
16074 row->height += row->phys_ascent - row->ascent;
16075 row->ascent = row->phys_ascent;
16076 }
16077
16078 /* Compute how much of the line is visible. */
16079 row->visible_height = row->height;
16080
16081 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
16082 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
16083
16084 if (row->y < min_y)
16085 row->visible_height -= min_y - row->y;
16086 if (row->y + row->height > max_y)
16087 row->visible_height -= row->y + row->height - max_y;
16088 }
16089 else
16090 {
16091 row->pixel_width = row->used[TEXT_AREA];
16092 if (row->continued_p)
16093 row->pixel_width -= it->continuation_pixel_width;
16094 else if (row->truncated_on_right_p)
16095 row->pixel_width -= it->truncation_pixel_width;
16096 row->ascent = row->phys_ascent = 0;
16097 row->height = row->phys_height = row->visible_height = 1;
16098 row->extra_line_spacing = 0;
16099 }
16100
16101 /* Compute a hash code for this row. */
16102 row->hash = 0;
16103 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
16104 for (i = 0; i < row->used[area]; ++i)
16105 row->hash = ((((row->hash << 4) + (row->hash >> 24)) & 0x0fffffff)
16106 + row->glyphs[area][i].u.val
16107 + row->glyphs[area][i].face_id
16108 + row->glyphs[area][i].padding_p
16109 + (row->glyphs[area][i].type << 2));
16110
16111 it->max_ascent = it->max_descent = 0;
16112 it->max_phys_ascent = it->max_phys_descent = 0;
16113 }
16114
16115
16116 /* Append one space to the glyph row of iterator IT if doing a
16117 window-based redisplay. The space has the same face as
16118 IT->face_id. Value is non-zero if a space was added.
16119
16120 This function is called to make sure that there is always one glyph
16121 at the end of a glyph row that the cursor can be set on under
16122 window-systems. (If there weren't such a glyph we would not know
16123 how wide and tall a box cursor should be displayed).
16124
16125 At the same time this space let's a nicely handle clearing to the
16126 end of the line if the row ends in italic text. */
16127
16128 static int
16129 append_space_for_newline (it, default_face_p)
16130 struct it *it;
16131 int default_face_p;
16132 {
16133 if (FRAME_WINDOW_P (it->f))
16134 {
16135 int n = it->glyph_row->used[TEXT_AREA];
16136
16137 if (it->glyph_row->glyphs[TEXT_AREA] + n
16138 < it->glyph_row->glyphs[1 + TEXT_AREA])
16139 {
16140 /* Save some values that must not be changed.
16141 Must save IT->c and IT->len because otherwise
16142 ITERATOR_AT_END_P wouldn't work anymore after
16143 append_space_for_newline has been called. */
16144 enum display_element_type saved_what = it->what;
16145 int saved_c = it->c, saved_len = it->len;
16146 int saved_x = it->current_x;
16147 int saved_face_id = it->face_id;
16148 struct text_pos saved_pos;
16149 Lisp_Object saved_object;
16150 struct face *face;
16151
16152 saved_object = it->object;
16153 saved_pos = it->position;
16154
16155 it->what = IT_CHARACTER;
16156 bzero (&it->position, sizeof it->position);
16157 it->object = make_number (0);
16158 it->c = ' ';
16159 it->len = 1;
16160
16161 if (default_face_p)
16162 it->face_id = DEFAULT_FACE_ID;
16163 else if (it->face_before_selective_p)
16164 it->face_id = it->saved_face_id;
16165 face = FACE_FROM_ID (it->f, it->face_id);
16166 it->face_id = FACE_FOR_CHAR (it->f, face, 0, -1, Qnil);
16167
16168 PRODUCE_GLYPHS (it);
16169
16170 it->override_ascent = -1;
16171 it->constrain_row_ascent_descent_p = 0;
16172 it->current_x = saved_x;
16173 it->object = saved_object;
16174 it->position = saved_pos;
16175 it->what = saved_what;
16176 it->face_id = saved_face_id;
16177 it->len = saved_len;
16178 it->c = saved_c;
16179 return 1;
16180 }
16181 }
16182
16183 return 0;
16184 }
16185
16186
16187 /* Extend the face of the last glyph in the text area of IT->glyph_row
16188 to the end of the display line. Called from display_line.
16189 If the glyph row is empty, add a space glyph to it so that we
16190 know the face to draw. Set the glyph row flag fill_line_p. */
16191
16192 static void
16193 extend_face_to_end_of_line (it)
16194 struct it *it;
16195 {
16196 struct face *face;
16197 struct frame *f = it->f;
16198
16199 /* If line is already filled, do nothing. */
16200 if (it->current_x >= it->last_visible_x)
16201 return;
16202
16203 /* Face extension extends the background and box of IT->face_id
16204 to the end of the line. If the background equals the background
16205 of the frame, we don't have to do anything. */
16206 if (it->face_before_selective_p)
16207 face = FACE_FROM_ID (it->f, it->saved_face_id);
16208 else
16209 face = FACE_FROM_ID (f, it->face_id);
16210
16211 if (FRAME_WINDOW_P (f)
16212 && it->glyph_row->displays_text_p
16213 && face->box == FACE_NO_BOX
16214 && face->background == FRAME_BACKGROUND_PIXEL (f)
16215 && !face->stipple)
16216 return;
16217
16218 /* Set the glyph row flag indicating that the face of the last glyph
16219 in the text area has to be drawn to the end of the text area. */
16220 it->glyph_row->fill_line_p = 1;
16221
16222 /* If current character of IT is not ASCII, make sure we have the
16223 ASCII face. This will be automatically undone the next time
16224 get_next_display_element returns a multibyte character. Note
16225 that the character will always be single byte in unibyte text. */
16226 if (!ASCII_CHAR_P (it->c))
16227 {
16228 it->face_id = FACE_FOR_CHAR (f, face, 0, -1, Qnil);
16229 }
16230
16231 if (FRAME_WINDOW_P (f))
16232 {
16233 /* If the row is empty, add a space with the current face of IT,
16234 so that we know which face to draw. */
16235 if (it->glyph_row->used[TEXT_AREA] == 0)
16236 {
16237 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
16238 it->glyph_row->glyphs[TEXT_AREA][0].face_id = it->face_id;
16239 it->glyph_row->used[TEXT_AREA] = 1;
16240 }
16241 }
16242 else
16243 {
16244 /* Save some values that must not be changed. */
16245 int saved_x = it->current_x;
16246 struct text_pos saved_pos;
16247 Lisp_Object saved_object;
16248 enum display_element_type saved_what = it->what;
16249 int saved_face_id = it->face_id;
16250
16251 saved_object = it->object;
16252 saved_pos = it->position;
16253
16254 it->what = IT_CHARACTER;
16255 bzero (&it->position, sizeof it->position);
16256 it->object = make_number (0);
16257 it->c = ' ';
16258 it->len = 1;
16259 it->face_id = face->id;
16260
16261 PRODUCE_GLYPHS (it);
16262
16263 while (it->current_x <= it->last_visible_x)
16264 PRODUCE_GLYPHS (it);
16265
16266 /* Don't count these blanks really. It would let us insert a left
16267 truncation glyph below and make us set the cursor on them, maybe. */
16268 it->current_x = saved_x;
16269 it->object = saved_object;
16270 it->position = saved_pos;
16271 it->what = saved_what;
16272 it->face_id = saved_face_id;
16273 }
16274 }
16275
16276
16277 /* Value is non-zero if text starting at CHARPOS in current_buffer is
16278 trailing whitespace. */
16279
16280 static int
16281 trailing_whitespace_p (charpos)
16282 int charpos;
16283 {
16284 int bytepos = CHAR_TO_BYTE (charpos);
16285 int c = 0;
16286
16287 while (bytepos < ZV_BYTE
16288 && (c = FETCH_CHAR (bytepos),
16289 c == ' ' || c == '\t'))
16290 ++bytepos;
16291
16292 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
16293 {
16294 if (bytepos != PT_BYTE)
16295 return 1;
16296 }
16297 return 0;
16298 }
16299
16300
16301 /* Highlight trailing whitespace, if any, in ROW. */
16302
16303 void
16304 highlight_trailing_whitespace (f, row)
16305 struct frame *f;
16306 struct glyph_row *row;
16307 {
16308 int used = row->used[TEXT_AREA];
16309
16310 if (used)
16311 {
16312 struct glyph *start = row->glyphs[TEXT_AREA];
16313 struct glyph *glyph = start + used - 1;
16314
16315 /* Skip over glyphs inserted to display the cursor at the
16316 end of a line, for extending the face of the last glyph
16317 to the end of the line on terminals, and for truncation
16318 and continuation glyphs. */
16319 while (glyph >= start
16320 && glyph->type == CHAR_GLYPH
16321 && INTEGERP (glyph->object))
16322 --glyph;
16323
16324 /* If last glyph is a space or stretch, and it's trailing
16325 whitespace, set the face of all trailing whitespace glyphs in
16326 IT->glyph_row to `trailing-whitespace'. */
16327 if (glyph >= start
16328 && BUFFERP (glyph->object)
16329 && (glyph->type == STRETCH_GLYPH
16330 || (glyph->type == CHAR_GLYPH
16331 && glyph->u.ch == ' '))
16332 && trailing_whitespace_p (glyph->charpos))
16333 {
16334 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
16335 if (face_id < 0)
16336 return;
16337
16338 while (glyph >= start
16339 && BUFFERP (glyph->object)
16340 && (glyph->type == STRETCH_GLYPH
16341 || (glyph->type == CHAR_GLYPH
16342 && glyph->u.ch == ' ')))
16343 (glyph--)->face_id = face_id;
16344 }
16345 }
16346 }
16347
16348
16349 /* Value is non-zero if glyph row ROW in window W should be
16350 used to hold the cursor. */
16351
16352 static int
16353 cursor_row_p (w, row)
16354 struct window *w;
16355 struct glyph_row *row;
16356 {
16357 int cursor_row_p = 1;
16358
16359 if (PT == MATRIX_ROW_END_CHARPOS (row))
16360 {
16361 /* Suppose the row ends on a string.
16362 Unless the row is continued, that means it ends on a newline
16363 in the string. If it's anything other than a display string
16364 (e.g. a before-string from an overlay), we don't want the
16365 cursor there. (This heuristic seems to give the optimal
16366 behavior for the various types of multi-line strings.) */
16367 if (CHARPOS (row->end.string_pos) >= 0)
16368 {
16369 if (row->continued_p)
16370 cursor_row_p = 1;
16371 else
16372 {
16373 /* Check for `display' property. */
16374 struct glyph *beg = row->glyphs[TEXT_AREA];
16375 struct glyph *end = beg + row->used[TEXT_AREA] - 1;
16376 struct glyph *glyph;
16377
16378 cursor_row_p = 0;
16379 for (glyph = end; glyph >= beg; --glyph)
16380 if (STRINGP (glyph->object))
16381 {
16382 Lisp_Object prop
16383 = Fget_char_property (make_number (PT),
16384 Qdisplay, Qnil);
16385 cursor_row_p =
16386 (!NILP (prop)
16387 && display_prop_string_p (prop, glyph->object));
16388 break;
16389 }
16390 }
16391 }
16392 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
16393 {
16394 /* If the row ends in middle of a real character,
16395 and the line is continued, we want the cursor here.
16396 That's because MATRIX_ROW_END_CHARPOS would equal
16397 PT if PT is before the character. */
16398 if (!row->ends_in_ellipsis_p)
16399 cursor_row_p = row->continued_p;
16400 else
16401 /* If the row ends in an ellipsis, then
16402 MATRIX_ROW_END_CHARPOS will equal point after the invisible text.
16403 We want that position to be displayed after the ellipsis. */
16404 cursor_row_p = 0;
16405 }
16406 /* If the row ends at ZV, display the cursor at the end of that
16407 row instead of at the start of the row below. */
16408 else if (row->ends_at_zv_p)
16409 cursor_row_p = 1;
16410 else
16411 cursor_row_p = 0;
16412 }
16413
16414 return cursor_row_p;
16415 }
16416
16417 \f
16418
16419 /* Push the display property PROP so that it will be rendered at the
16420 current position in IT. */
16421
16422 static void
16423 push_display_prop (struct it *it, Lisp_Object prop)
16424 {
16425 push_it (it);
16426
16427 /* Never display a cursor on the prefix. */
16428 it->avoid_cursor_p = 1;
16429
16430 if (STRINGP (prop))
16431 {
16432 if (SCHARS (prop) == 0)
16433 {
16434 pop_it (it);
16435 return;
16436 }
16437
16438 it->string = prop;
16439 it->multibyte_p = STRING_MULTIBYTE (it->string);
16440 it->current.overlay_string_index = -1;
16441 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
16442 it->end_charpos = it->string_nchars = SCHARS (it->string);
16443 it->method = GET_FROM_STRING;
16444 it->stop_charpos = 0;
16445 }
16446 else if (CONSP (prop) && EQ (XCAR (prop), Qspace))
16447 {
16448 it->method = GET_FROM_STRETCH;
16449 it->object = prop;
16450 }
16451 #ifdef HAVE_WINDOW_SYSTEM
16452 else if (IMAGEP (prop))
16453 {
16454 it->what = IT_IMAGE;
16455 it->image_id = lookup_image (it->f, prop);
16456 it->method = GET_FROM_IMAGE;
16457 }
16458 #endif /* HAVE_WINDOW_SYSTEM */
16459 else
16460 {
16461 pop_it (it); /* bogus display property, give up */
16462 return;
16463 }
16464 }
16465
16466 /* Return the character-property PROP at the current position in IT. */
16467
16468 static Lisp_Object
16469 get_it_property (it, prop)
16470 struct it *it;
16471 Lisp_Object prop;
16472 {
16473 Lisp_Object position;
16474
16475 if (STRINGP (it->object))
16476 position = make_number (IT_STRING_CHARPOS (*it));
16477 else if (BUFFERP (it->object))
16478 position = make_number (IT_CHARPOS (*it));
16479 else
16480 return Qnil;
16481
16482 return Fget_char_property (position, prop, it->object);
16483 }
16484
16485 /* See if there's a line- or wrap-prefix, and if so, push it on IT. */
16486
16487 static void
16488 handle_line_prefix (struct it *it)
16489 {
16490 Lisp_Object prefix;
16491 if (it->continuation_lines_width > 0)
16492 {
16493 prefix = get_it_property (it, Qwrap_prefix);
16494 if (NILP (prefix))
16495 prefix = Vwrap_prefix;
16496 }
16497 else
16498 {
16499 prefix = get_it_property (it, Qline_prefix);
16500 if (NILP (prefix))
16501 prefix = Vline_prefix;
16502 }
16503 if (! NILP (prefix))
16504 push_display_prop (it, prefix);
16505 }
16506
16507 \f
16508
16509 /* Construct the glyph row IT->glyph_row in the desired matrix of
16510 IT->w from text at the current position of IT. See dispextern.h
16511 for an overview of struct it. Value is non-zero if
16512 IT->glyph_row displays text, as opposed to a line displaying ZV
16513 only. */
16514
16515 static int
16516 display_line (it)
16517 struct it *it;
16518 {
16519 struct glyph_row *row = it->glyph_row;
16520 Lisp_Object overlay_arrow_string;
16521 struct it wrap_it;
16522 int may_wrap = 0, wrap_x;
16523 int wrap_row_used = -1, wrap_row_ascent, wrap_row_height;
16524 int wrap_row_phys_ascent, wrap_row_phys_height;
16525 int wrap_row_extra_line_spacing;
16526
16527 /* We always start displaying at hpos zero even if hscrolled. */
16528 xassert (it->hpos == 0 && it->current_x == 0);
16529
16530 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
16531 >= it->w->desired_matrix->nrows)
16532 {
16533 it->w->nrows_scale_factor++;
16534 fonts_changed_p = 1;
16535 return 0;
16536 }
16537
16538 /* Is IT->w showing the region? */
16539 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
16540
16541 /* Clear the result glyph row and enable it. */
16542 prepare_desired_row (row);
16543
16544 row->y = it->current_y;
16545 row->start = it->start;
16546 row->continuation_lines_width = it->continuation_lines_width;
16547 row->displays_text_p = 1;
16548 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
16549 it->starts_in_middle_of_char_p = 0;
16550
16551 /* Arrange the overlays nicely for our purposes. Usually, we call
16552 display_line on only one line at a time, in which case this
16553 can't really hurt too much, or we call it on lines which appear
16554 one after another in the buffer, in which case all calls to
16555 recenter_overlay_lists but the first will be pretty cheap. */
16556 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
16557
16558 /* Move over display elements that are not visible because we are
16559 hscrolled. This may stop at an x-position < IT->first_visible_x
16560 if the first glyph is partially visible or if we hit a line end. */
16561 if (it->current_x < it->first_visible_x)
16562 {
16563 move_it_in_display_line_to (it, ZV, it->first_visible_x,
16564 MOVE_TO_POS | MOVE_TO_X);
16565 }
16566 else
16567 {
16568 /* We only do this when not calling `move_it_in_display_line_to'
16569 above, because move_it_in_display_line_to calls
16570 handle_line_prefix itself. */
16571 handle_line_prefix (it);
16572 }
16573
16574 /* Get the initial row height. This is either the height of the
16575 text hscrolled, if there is any, or zero. */
16576 row->ascent = it->max_ascent;
16577 row->height = it->max_ascent + it->max_descent;
16578 row->phys_ascent = it->max_phys_ascent;
16579 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
16580 row->extra_line_spacing = it->max_extra_line_spacing;
16581
16582 /* Loop generating characters. The loop is left with IT on the next
16583 character to display. */
16584 while (1)
16585 {
16586 int n_glyphs_before, hpos_before, x_before;
16587 int x, i, nglyphs;
16588 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
16589
16590 /* Retrieve the next thing to display. Value is zero if end of
16591 buffer reached. */
16592 if (!get_next_display_element (it))
16593 {
16594 /* Maybe add a space at the end of this line that is used to
16595 display the cursor there under X. Set the charpos of the
16596 first glyph of blank lines not corresponding to any text
16597 to -1. */
16598 #ifdef HAVE_WINDOW_SYSTEM
16599 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
16600 row->exact_window_width_line_p = 1;
16601 else
16602 #endif /* HAVE_WINDOW_SYSTEM */
16603 if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
16604 || row->used[TEXT_AREA] == 0)
16605 {
16606 row->glyphs[TEXT_AREA]->charpos = -1;
16607 row->displays_text_p = 0;
16608
16609 if (!NILP (XBUFFER (it->w->buffer)->indicate_empty_lines)
16610 && (!MINI_WINDOW_P (it->w)
16611 || (minibuf_level && EQ (it->window, minibuf_window))))
16612 row->indicate_empty_line_p = 1;
16613 }
16614
16615 it->continuation_lines_width = 0;
16616 row->ends_at_zv_p = 1;
16617 break;
16618 }
16619
16620 /* Now, get the metrics of what we want to display. This also
16621 generates glyphs in `row' (which is IT->glyph_row). */
16622 n_glyphs_before = row->used[TEXT_AREA];
16623 x = it->current_x;
16624
16625 /* Remember the line height so far in case the next element doesn't
16626 fit on the line. */
16627 if (it->line_wrap != TRUNCATE)
16628 {
16629 ascent = it->max_ascent;
16630 descent = it->max_descent;
16631 phys_ascent = it->max_phys_ascent;
16632 phys_descent = it->max_phys_descent;
16633
16634 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
16635 {
16636 if (IT_DISPLAYING_WHITESPACE (it))
16637 may_wrap = 1;
16638 else if (may_wrap)
16639 {
16640 wrap_it = *it;
16641 wrap_x = x;
16642 wrap_row_used = row->used[TEXT_AREA];
16643 wrap_row_ascent = row->ascent;
16644 wrap_row_height = row->height;
16645 wrap_row_phys_ascent = row->phys_ascent;
16646 wrap_row_phys_height = row->phys_height;
16647 wrap_row_extra_line_spacing = row->extra_line_spacing;
16648 may_wrap = 0;
16649 }
16650 }
16651 }
16652
16653 PRODUCE_GLYPHS (it);
16654
16655 /* If this display element was in marginal areas, continue with
16656 the next one. */
16657 if (it->area != TEXT_AREA)
16658 {
16659 row->ascent = max (row->ascent, it->max_ascent);
16660 row->height = max (row->height, it->max_ascent + it->max_descent);
16661 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
16662 row->phys_height = max (row->phys_height,
16663 it->max_phys_ascent + it->max_phys_descent);
16664 row->extra_line_spacing = max (row->extra_line_spacing,
16665 it->max_extra_line_spacing);
16666 set_iterator_to_next (it, 1);
16667 continue;
16668 }
16669
16670 /* Does the display element fit on the line? If we truncate
16671 lines, we should draw past the right edge of the window. If
16672 we don't truncate, we want to stop so that we can display the
16673 continuation glyph before the right margin. If lines are
16674 continued, there are two possible strategies for characters
16675 resulting in more than 1 glyph (e.g. tabs): Display as many
16676 glyphs as possible in this line and leave the rest for the
16677 continuation line, or display the whole element in the next
16678 line. Original redisplay did the former, so we do it also. */
16679 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
16680 hpos_before = it->hpos;
16681 x_before = x;
16682
16683 if (/* Not a newline. */
16684 nglyphs > 0
16685 /* Glyphs produced fit entirely in the line. */
16686 && it->current_x < it->last_visible_x)
16687 {
16688 it->hpos += nglyphs;
16689 row->ascent = max (row->ascent, it->max_ascent);
16690 row->height = max (row->height, it->max_ascent + it->max_descent);
16691 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
16692 row->phys_height = max (row->phys_height,
16693 it->max_phys_ascent + it->max_phys_descent);
16694 row->extra_line_spacing = max (row->extra_line_spacing,
16695 it->max_extra_line_spacing);
16696 if (it->current_x - it->pixel_width < it->first_visible_x)
16697 row->x = x - it->first_visible_x;
16698 }
16699 else
16700 {
16701 int new_x;
16702 struct glyph *glyph;
16703
16704 for (i = 0; i < nglyphs; ++i, x = new_x)
16705 {
16706 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
16707 new_x = x + glyph->pixel_width;
16708
16709 if (/* Lines are continued. */
16710 it->line_wrap != TRUNCATE
16711 && (/* Glyph doesn't fit on the line. */
16712 new_x > it->last_visible_x
16713 /* Or it fits exactly on a window system frame. */
16714 || (new_x == it->last_visible_x
16715 && FRAME_WINDOW_P (it->f))))
16716 {
16717 /* End of a continued line. */
16718
16719 if (it->hpos == 0
16720 || (new_x == it->last_visible_x
16721 && FRAME_WINDOW_P (it->f)))
16722 {
16723 /* Current glyph is the only one on the line or
16724 fits exactly on the line. We must continue
16725 the line because we can't draw the cursor
16726 after the glyph. */
16727 row->continued_p = 1;
16728 it->current_x = new_x;
16729 it->continuation_lines_width += new_x;
16730 ++it->hpos;
16731 if (i == nglyphs - 1)
16732 {
16733 /* If line-wrap is on, check if a previous
16734 wrap point was found. */
16735 if (wrap_row_used > 0
16736 /* Even if there is a previous wrap
16737 point, continue the line here as
16738 usual, if (i) the previous character
16739 was a space or tab AND (ii) the
16740 current character is not. */
16741 && (!may_wrap
16742 || IT_DISPLAYING_WHITESPACE (it)))
16743 goto back_to_wrap;
16744
16745 set_iterator_to_next (it, 1);
16746 #ifdef HAVE_WINDOW_SYSTEM
16747 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
16748 {
16749 if (!get_next_display_element (it))
16750 {
16751 row->exact_window_width_line_p = 1;
16752 it->continuation_lines_width = 0;
16753 row->continued_p = 0;
16754 row->ends_at_zv_p = 1;
16755 }
16756 else if (ITERATOR_AT_END_OF_LINE_P (it))
16757 {
16758 row->continued_p = 0;
16759 row->exact_window_width_line_p = 1;
16760 }
16761 }
16762 #endif /* HAVE_WINDOW_SYSTEM */
16763 }
16764 }
16765 else if (CHAR_GLYPH_PADDING_P (*glyph)
16766 && !FRAME_WINDOW_P (it->f))
16767 {
16768 /* A padding glyph that doesn't fit on this line.
16769 This means the whole character doesn't fit
16770 on the line. */
16771 row->used[TEXT_AREA] = n_glyphs_before;
16772
16773 /* Fill the rest of the row with continuation
16774 glyphs like in 20.x. */
16775 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
16776 < row->glyphs[1 + TEXT_AREA])
16777 produce_special_glyphs (it, IT_CONTINUATION);
16778
16779 row->continued_p = 1;
16780 it->current_x = x_before;
16781 it->continuation_lines_width += x_before;
16782
16783 /* Restore the height to what it was before the
16784 element not fitting on the line. */
16785 it->max_ascent = ascent;
16786 it->max_descent = descent;
16787 it->max_phys_ascent = phys_ascent;
16788 it->max_phys_descent = phys_descent;
16789 }
16790 else if (wrap_row_used > 0)
16791 {
16792 back_to_wrap:
16793 *it = wrap_it;
16794 it->continuation_lines_width += wrap_x;
16795 row->used[TEXT_AREA] = wrap_row_used;
16796 row->ascent = wrap_row_ascent;
16797 row->height = wrap_row_height;
16798 row->phys_ascent = wrap_row_phys_ascent;
16799 row->phys_height = wrap_row_phys_height;
16800 row->extra_line_spacing = wrap_row_extra_line_spacing;
16801 row->continued_p = 1;
16802 row->ends_at_zv_p = 0;
16803 row->exact_window_width_line_p = 0;
16804 it->continuation_lines_width += x;
16805
16806 /* Make sure that a non-default face is extended
16807 up to the right margin of the window. */
16808 extend_face_to_end_of_line (it);
16809 }
16810 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
16811 {
16812 /* A TAB that extends past the right edge of the
16813 window. This produces a single glyph on
16814 window system frames. We leave the glyph in
16815 this row and let it fill the row, but don't
16816 consume the TAB. */
16817 it->continuation_lines_width += it->last_visible_x;
16818 row->ends_in_middle_of_char_p = 1;
16819 row->continued_p = 1;
16820 glyph->pixel_width = it->last_visible_x - x;
16821 it->starts_in_middle_of_char_p = 1;
16822 }
16823 else
16824 {
16825 /* Something other than a TAB that draws past
16826 the right edge of the window. Restore
16827 positions to values before the element. */
16828 row->used[TEXT_AREA] = n_glyphs_before + i;
16829
16830 /* Display continuation glyphs. */
16831 if (!FRAME_WINDOW_P (it->f))
16832 produce_special_glyphs (it, IT_CONTINUATION);
16833 row->continued_p = 1;
16834
16835 it->current_x = x_before;
16836 it->continuation_lines_width += x;
16837 extend_face_to_end_of_line (it);
16838
16839 if (nglyphs > 1 && i > 0)
16840 {
16841 row->ends_in_middle_of_char_p = 1;
16842 it->starts_in_middle_of_char_p = 1;
16843 }
16844
16845 /* Restore the height to what it was before the
16846 element not fitting on the line. */
16847 it->max_ascent = ascent;
16848 it->max_descent = descent;
16849 it->max_phys_ascent = phys_ascent;
16850 it->max_phys_descent = phys_descent;
16851 }
16852
16853 break;
16854 }
16855 else if (new_x > it->first_visible_x)
16856 {
16857 /* Increment number of glyphs actually displayed. */
16858 ++it->hpos;
16859
16860 if (x < it->first_visible_x)
16861 /* Glyph is partially visible, i.e. row starts at
16862 negative X position. */
16863 row->x = x - it->first_visible_x;
16864 }
16865 else
16866 {
16867 /* Glyph is completely off the left margin of the
16868 window. This should not happen because of the
16869 move_it_in_display_line at the start of this
16870 function, unless the text display area of the
16871 window is empty. */
16872 xassert (it->first_visible_x <= it->last_visible_x);
16873 }
16874 }
16875
16876 row->ascent = max (row->ascent, it->max_ascent);
16877 row->height = max (row->height, it->max_ascent + it->max_descent);
16878 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
16879 row->phys_height = max (row->phys_height,
16880 it->max_phys_ascent + it->max_phys_descent);
16881 row->extra_line_spacing = max (row->extra_line_spacing,
16882 it->max_extra_line_spacing);
16883
16884 /* End of this display line if row is continued. */
16885 if (row->continued_p || row->ends_at_zv_p)
16886 break;
16887 }
16888
16889 at_end_of_line:
16890 /* Is this a line end? If yes, we're also done, after making
16891 sure that a non-default face is extended up to the right
16892 margin of the window. */
16893 if (ITERATOR_AT_END_OF_LINE_P (it))
16894 {
16895 int used_before = row->used[TEXT_AREA];
16896
16897 row->ends_in_newline_from_string_p = STRINGP (it->object);
16898
16899 #ifdef HAVE_WINDOW_SYSTEM
16900 /* Add a space at the end of the line that is used to
16901 display the cursor there. */
16902 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
16903 append_space_for_newline (it, 0);
16904 #endif /* HAVE_WINDOW_SYSTEM */
16905
16906 /* Extend the face to the end of the line. */
16907 extend_face_to_end_of_line (it);
16908
16909 /* Make sure we have the position. */
16910 if (used_before == 0)
16911 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
16912
16913 /* Consume the line end. This skips over invisible lines. */
16914 set_iterator_to_next (it, 1);
16915 it->continuation_lines_width = 0;
16916 break;
16917 }
16918
16919 /* Proceed with next display element. Note that this skips
16920 over lines invisible because of selective display. */
16921 set_iterator_to_next (it, 1);
16922
16923 /* If we truncate lines, we are done when the last displayed
16924 glyphs reach past the right margin of the window. */
16925 if (it->line_wrap == TRUNCATE
16926 && (FRAME_WINDOW_P (it->f)
16927 ? (it->current_x >= it->last_visible_x)
16928 : (it->current_x > it->last_visible_x)))
16929 {
16930 /* Maybe add truncation glyphs. */
16931 if (!FRAME_WINDOW_P (it->f))
16932 {
16933 int i, n;
16934
16935 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
16936 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
16937 break;
16938
16939 for (n = row->used[TEXT_AREA]; i < n; ++i)
16940 {
16941 row->used[TEXT_AREA] = i;
16942 produce_special_glyphs (it, IT_TRUNCATION);
16943 }
16944 }
16945 #ifdef HAVE_WINDOW_SYSTEM
16946 else
16947 {
16948 /* Don't truncate if we can overflow newline into fringe. */
16949 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
16950 {
16951 if (!get_next_display_element (it))
16952 {
16953 it->continuation_lines_width = 0;
16954 row->ends_at_zv_p = 1;
16955 row->exact_window_width_line_p = 1;
16956 break;
16957 }
16958 if (ITERATOR_AT_END_OF_LINE_P (it))
16959 {
16960 row->exact_window_width_line_p = 1;
16961 goto at_end_of_line;
16962 }
16963 }
16964 }
16965 #endif /* HAVE_WINDOW_SYSTEM */
16966
16967 row->truncated_on_right_p = 1;
16968 it->continuation_lines_width = 0;
16969 reseat_at_next_visible_line_start (it, 0);
16970 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
16971 it->hpos = hpos_before;
16972 it->current_x = x_before;
16973 break;
16974 }
16975 }
16976
16977 /* If line is not empty and hscrolled, maybe insert truncation glyphs
16978 at the left window margin. */
16979 if (it->first_visible_x
16980 && IT_CHARPOS (*it) != MATRIX_ROW_START_CHARPOS (row))
16981 {
16982 if (!FRAME_WINDOW_P (it->f))
16983 insert_left_trunc_glyphs (it);
16984 row->truncated_on_left_p = 1;
16985 }
16986
16987 /* If the start of this line is the overlay arrow-position, then
16988 mark this glyph row as the one containing the overlay arrow.
16989 This is clearly a mess with variable size fonts. It would be
16990 better to let it be displayed like cursors under X. */
16991 if ((row->displays_text_p || !overlay_arrow_seen)
16992 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
16993 !NILP (overlay_arrow_string)))
16994 {
16995 /* Overlay arrow in window redisplay is a fringe bitmap. */
16996 if (STRINGP (overlay_arrow_string))
16997 {
16998 struct glyph_row *arrow_row
16999 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
17000 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
17001 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
17002 struct glyph *p = row->glyphs[TEXT_AREA];
17003 struct glyph *p2, *end;
17004
17005 /* Copy the arrow glyphs. */
17006 while (glyph < arrow_end)
17007 *p++ = *glyph++;
17008
17009 /* Throw away padding glyphs. */
17010 p2 = p;
17011 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
17012 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
17013 ++p2;
17014 if (p2 > p)
17015 {
17016 while (p2 < end)
17017 *p++ = *p2++;
17018 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
17019 }
17020 }
17021 else
17022 {
17023 xassert (INTEGERP (overlay_arrow_string));
17024 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
17025 }
17026 overlay_arrow_seen = 1;
17027 }
17028
17029 /* Compute pixel dimensions of this line. */
17030 compute_line_metrics (it);
17031
17032 /* Remember the position at which this line ends. */
17033 row->end = it->current;
17034
17035 /* Record whether this row ends inside an ellipsis. */
17036 row->ends_in_ellipsis_p
17037 = (it->method == GET_FROM_DISPLAY_VECTOR
17038 && it->ellipsis_p);
17039
17040 /* Save fringe bitmaps in this row. */
17041 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
17042 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
17043 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
17044 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
17045
17046 it->left_user_fringe_bitmap = 0;
17047 it->left_user_fringe_face_id = 0;
17048 it->right_user_fringe_bitmap = 0;
17049 it->right_user_fringe_face_id = 0;
17050
17051 /* Maybe set the cursor. */
17052 if (it->w->cursor.vpos < 0
17053 && PT >= MATRIX_ROW_START_CHARPOS (row)
17054 && PT <= MATRIX_ROW_END_CHARPOS (row)
17055 && cursor_row_p (it->w, row))
17056 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
17057
17058 /* Highlight trailing whitespace. */
17059 if (!NILP (Vshow_trailing_whitespace))
17060 highlight_trailing_whitespace (it->f, it->glyph_row);
17061
17062 /* Prepare for the next line. This line starts horizontally at (X
17063 HPOS) = (0 0). Vertical positions are incremented. As a
17064 convenience for the caller, IT->glyph_row is set to the next
17065 row to be used. */
17066 it->current_x = it->hpos = 0;
17067 it->current_y += row->height;
17068 ++it->vpos;
17069 ++it->glyph_row;
17070 it->start = it->current;
17071 return row->displays_text_p;
17072 }
17073
17074
17075 \f
17076 /***********************************************************************
17077 Menu Bar
17078 ***********************************************************************/
17079
17080 /* Redisplay the menu bar in the frame for window W.
17081
17082 The menu bar of X frames that don't have X toolkit support is
17083 displayed in a special window W->frame->menu_bar_window.
17084
17085 The menu bar of terminal frames is treated specially as far as
17086 glyph matrices are concerned. Menu bar lines are not part of
17087 windows, so the update is done directly on the frame matrix rows
17088 for the menu bar. */
17089
17090 static void
17091 display_menu_bar (w)
17092 struct window *w;
17093 {
17094 struct frame *f = XFRAME (WINDOW_FRAME (w));
17095 struct it it;
17096 Lisp_Object items;
17097 int i;
17098
17099 /* Don't do all this for graphical frames. */
17100 #ifdef HAVE_NTGUI
17101 if (FRAME_W32_P (f))
17102 return;
17103 #endif
17104 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
17105 if (FRAME_X_P (f))
17106 return;
17107 #endif
17108
17109 #ifdef HAVE_NS
17110 if (FRAME_NS_P (f))
17111 return;
17112 #endif /* HAVE_NS */
17113
17114 #ifdef USE_X_TOOLKIT
17115 xassert (!FRAME_WINDOW_P (f));
17116 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
17117 it.first_visible_x = 0;
17118 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
17119 #else /* not USE_X_TOOLKIT */
17120 if (FRAME_WINDOW_P (f))
17121 {
17122 /* Menu bar lines are displayed in the desired matrix of the
17123 dummy window menu_bar_window. */
17124 struct window *menu_w;
17125 xassert (WINDOWP (f->menu_bar_window));
17126 menu_w = XWINDOW (f->menu_bar_window);
17127 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
17128 MENU_FACE_ID);
17129 it.first_visible_x = 0;
17130 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
17131 }
17132 else
17133 {
17134 /* This is a TTY frame, i.e. character hpos/vpos are used as
17135 pixel x/y. */
17136 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
17137 MENU_FACE_ID);
17138 it.first_visible_x = 0;
17139 it.last_visible_x = FRAME_COLS (f);
17140 }
17141 #endif /* not USE_X_TOOLKIT */
17142
17143 if (! mode_line_inverse_video)
17144 /* Force the menu-bar to be displayed in the default face. */
17145 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
17146
17147 /* Clear all rows of the menu bar. */
17148 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
17149 {
17150 struct glyph_row *row = it.glyph_row + i;
17151 clear_glyph_row (row);
17152 row->enabled_p = 1;
17153 row->full_width_p = 1;
17154 }
17155
17156 /* Display all items of the menu bar. */
17157 items = FRAME_MENU_BAR_ITEMS (it.f);
17158 for (i = 0; i < XVECTOR (items)->size; i += 4)
17159 {
17160 Lisp_Object string;
17161
17162 /* Stop at nil string. */
17163 string = AREF (items, i + 1);
17164 if (NILP (string))
17165 break;
17166
17167 /* Remember where item was displayed. */
17168 ASET (items, i + 3, make_number (it.hpos));
17169
17170 /* Display the item, pad with one space. */
17171 if (it.current_x < it.last_visible_x)
17172 display_string (NULL, string, Qnil, 0, 0, &it,
17173 SCHARS (string) + 1, 0, 0, -1);
17174 }
17175
17176 /* Fill out the line with spaces. */
17177 if (it.current_x < it.last_visible_x)
17178 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
17179
17180 /* Compute the total height of the lines. */
17181 compute_line_metrics (&it);
17182 }
17183
17184
17185 \f
17186 /***********************************************************************
17187 Mode Line
17188 ***********************************************************************/
17189
17190 /* Redisplay mode lines in the window tree whose root is WINDOW. If
17191 FORCE is non-zero, redisplay mode lines unconditionally.
17192 Otherwise, redisplay only mode lines that are garbaged. Value is
17193 the number of windows whose mode lines were redisplayed. */
17194
17195 static int
17196 redisplay_mode_lines (window, force)
17197 Lisp_Object window;
17198 int force;
17199 {
17200 int nwindows = 0;
17201
17202 while (!NILP (window))
17203 {
17204 struct window *w = XWINDOW (window);
17205
17206 if (WINDOWP (w->hchild))
17207 nwindows += redisplay_mode_lines (w->hchild, force);
17208 else if (WINDOWP (w->vchild))
17209 nwindows += redisplay_mode_lines (w->vchild, force);
17210 else if (force
17211 || FRAME_GARBAGED_P (XFRAME (w->frame))
17212 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
17213 {
17214 struct text_pos lpoint;
17215 struct buffer *old = current_buffer;
17216
17217 /* Set the window's buffer for the mode line display. */
17218 SET_TEXT_POS (lpoint, PT, PT_BYTE);
17219 set_buffer_internal_1 (XBUFFER (w->buffer));
17220
17221 /* Point refers normally to the selected window. For any
17222 other window, set up appropriate value. */
17223 if (!EQ (window, selected_window))
17224 {
17225 struct text_pos pt;
17226
17227 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
17228 if (CHARPOS (pt) < BEGV)
17229 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
17230 else if (CHARPOS (pt) > (ZV - 1))
17231 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
17232 else
17233 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
17234 }
17235
17236 /* Display mode lines. */
17237 clear_glyph_matrix (w->desired_matrix);
17238 if (display_mode_lines (w))
17239 {
17240 ++nwindows;
17241 w->must_be_updated_p = 1;
17242 }
17243
17244 /* Restore old settings. */
17245 set_buffer_internal_1 (old);
17246 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
17247 }
17248
17249 window = w->next;
17250 }
17251
17252 return nwindows;
17253 }
17254
17255
17256 /* Display the mode and/or top line of window W. Value is the number
17257 of mode lines displayed. */
17258
17259 static int
17260 display_mode_lines (w)
17261 struct window *w;
17262 {
17263 Lisp_Object old_selected_window, old_selected_frame;
17264 int n = 0;
17265
17266 old_selected_frame = selected_frame;
17267 selected_frame = w->frame;
17268 old_selected_window = selected_window;
17269 XSETWINDOW (selected_window, w);
17270
17271 /* These will be set while the mode line specs are processed. */
17272 line_number_displayed = 0;
17273 w->column_number_displayed = Qnil;
17274
17275 if (WINDOW_WANTS_MODELINE_P (w))
17276 {
17277 struct window *sel_w = XWINDOW (old_selected_window);
17278
17279 /* Select mode line face based on the real selected window. */
17280 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
17281 current_buffer->mode_line_format);
17282 ++n;
17283 }
17284
17285 if (WINDOW_WANTS_HEADER_LINE_P (w))
17286 {
17287 display_mode_line (w, HEADER_LINE_FACE_ID,
17288 current_buffer->header_line_format);
17289 ++n;
17290 }
17291
17292 selected_frame = old_selected_frame;
17293 selected_window = old_selected_window;
17294 return n;
17295 }
17296
17297
17298 /* Display mode or top line of window W. FACE_ID specifies which line
17299 to display; it is either MODE_LINE_FACE_ID or HEADER_LINE_FACE_ID.
17300 FORMAT is the mode line format to display. Value is the pixel
17301 height of the mode line displayed. */
17302
17303 static int
17304 display_mode_line (w, face_id, format)
17305 struct window *w;
17306 enum face_id face_id;
17307 Lisp_Object format;
17308 {
17309 struct it it;
17310 struct face *face;
17311 int count = SPECPDL_INDEX ();
17312
17313 init_iterator (&it, w, -1, -1, NULL, face_id);
17314 /* Don't extend on a previously drawn mode-line.
17315 This may happen if called from pos_visible_p. */
17316 it.glyph_row->enabled_p = 0;
17317 prepare_desired_row (it.glyph_row);
17318
17319 it.glyph_row->mode_line_p = 1;
17320
17321 if (! mode_line_inverse_video)
17322 /* Force the mode-line to be displayed in the default face. */
17323 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
17324
17325 record_unwind_protect (unwind_format_mode_line,
17326 format_mode_line_unwind_data (NULL, Qnil, 0));
17327
17328 mode_line_target = MODE_LINE_DISPLAY;
17329
17330 /* Temporarily make frame's keyboard the current kboard so that
17331 kboard-local variables in the mode_line_format will get the right
17332 values. */
17333 push_kboard (FRAME_KBOARD (it.f));
17334 record_unwind_save_match_data ();
17335 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
17336 pop_kboard ();
17337
17338 unbind_to (count, Qnil);
17339
17340 /* Fill up with spaces. */
17341 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
17342
17343 compute_line_metrics (&it);
17344 it.glyph_row->full_width_p = 1;
17345 it.glyph_row->continued_p = 0;
17346 it.glyph_row->truncated_on_left_p = 0;
17347 it.glyph_row->truncated_on_right_p = 0;
17348
17349 /* Make a 3D mode-line have a shadow at its right end. */
17350 face = FACE_FROM_ID (it.f, face_id);
17351 extend_face_to_end_of_line (&it);
17352 if (face->box != FACE_NO_BOX)
17353 {
17354 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
17355 + it.glyph_row->used[TEXT_AREA] - 1);
17356 last->right_box_line_p = 1;
17357 }
17358
17359 return it.glyph_row->height;
17360 }
17361
17362 /* Move element ELT in LIST to the front of LIST.
17363 Return the updated list. */
17364
17365 static Lisp_Object
17366 move_elt_to_front (elt, list)
17367 Lisp_Object elt, list;
17368 {
17369 register Lisp_Object tail, prev;
17370 register Lisp_Object tem;
17371
17372 tail = list;
17373 prev = Qnil;
17374 while (CONSP (tail))
17375 {
17376 tem = XCAR (tail);
17377
17378 if (EQ (elt, tem))
17379 {
17380 /* Splice out the link TAIL. */
17381 if (NILP (prev))
17382 list = XCDR (tail);
17383 else
17384 Fsetcdr (prev, XCDR (tail));
17385
17386 /* Now make it the first. */
17387 Fsetcdr (tail, list);
17388 return tail;
17389 }
17390 else
17391 prev = tail;
17392 tail = XCDR (tail);
17393 QUIT;
17394 }
17395
17396 /* Not found--return unchanged LIST. */
17397 return list;
17398 }
17399
17400 /* Contribute ELT to the mode line for window IT->w. How it
17401 translates into text depends on its data type.
17402
17403 IT describes the display environment in which we display, as usual.
17404
17405 DEPTH is the depth in recursion. It is used to prevent
17406 infinite recursion here.
17407
17408 FIELD_WIDTH is the number of characters the display of ELT should
17409 occupy in the mode line, and PRECISION is the maximum number of
17410 characters to display from ELT's representation. See
17411 display_string for details.
17412
17413 Returns the hpos of the end of the text generated by ELT.
17414
17415 PROPS is a property list to add to any string we encounter.
17416
17417 If RISKY is nonzero, remove (disregard) any properties in any string
17418 we encounter, and ignore :eval and :propertize.
17419
17420 The global variable `mode_line_target' determines whether the
17421 output is passed to `store_mode_line_noprop',
17422 `store_mode_line_string', or `display_string'. */
17423
17424 static int
17425 display_mode_element (it, depth, field_width, precision, elt, props, risky)
17426 struct it *it;
17427 int depth;
17428 int field_width, precision;
17429 Lisp_Object elt, props;
17430 int risky;
17431 {
17432 int n = 0, field, prec;
17433 int literal = 0;
17434
17435 tail_recurse:
17436 if (depth > 100)
17437 elt = build_string ("*too-deep*");
17438
17439 depth++;
17440
17441 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
17442 {
17443 case Lisp_String:
17444 {
17445 /* A string: output it and check for %-constructs within it. */
17446 unsigned char c;
17447 int offset = 0;
17448
17449 if (SCHARS (elt) > 0
17450 && (!NILP (props) || risky))
17451 {
17452 Lisp_Object oprops, aelt;
17453 oprops = Ftext_properties_at (make_number (0), elt);
17454
17455 /* If the starting string's properties are not what
17456 we want, translate the string. Also, if the string
17457 is risky, do that anyway. */
17458
17459 if (NILP (Fequal (props, oprops)) || risky)
17460 {
17461 /* If the starting string has properties,
17462 merge the specified ones onto the existing ones. */
17463 if (! NILP (oprops) && !risky)
17464 {
17465 Lisp_Object tem;
17466
17467 oprops = Fcopy_sequence (oprops);
17468 tem = props;
17469 while (CONSP (tem))
17470 {
17471 oprops = Fplist_put (oprops, XCAR (tem),
17472 XCAR (XCDR (tem)));
17473 tem = XCDR (XCDR (tem));
17474 }
17475 props = oprops;
17476 }
17477
17478 aelt = Fassoc (elt, mode_line_proptrans_alist);
17479 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
17480 {
17481 /* AELT is what we want. Move it to the front
17482 without consing. */
17483 elt = XCAR (aelt);
17484 mode_line_proptrans_alist
17485 = move_elt_to_front (aelt, mode_line_proptrans_alist);
17486 }
17487 else
17488 {
17489 Lisp_Object tem;
17490
17491 /* If AELT has the wrong props, it is useless.
17492 so get rid of it. */
17493 if (! NILP (aelt))
17494 mode_line_proptrans_alist
17495 = Fdelq (aelt, mode_line_proptrans_alist);
17496
17497 elt = Fcopy_sequence (elt);
17498 Fset_text_properties (make_number (0), Flength (elt),
17499 props, elt);
17500 /* Add this item to mode_line_proptrans_alist. */
17501 mode_line_proptrans_alist
17502 = Fcons (Fcons (elt, props),
17503 mode_line_proptrans_alist);
17504 /* Truncate mode_line_proptrans_alist
17505 to at most 50 elements. */
17506 tem = Fnthcdr (make_number (50),
17507 mode_line_proptrans_alist);
17508 if (! NILP (tem))
17509 XSETCDR (tem, Qnil);
17510 }
17511 }
17512 }
17513
17514 offset = 0;
17515
17516 if (literal)
17517 {
17518 prec = precision - n;
17519 switch (mode_line_target)
17520 {
17521 case MODE_LINE_NOPROP:
17522 case MODE_LINE_TITLE:
17523 n += store_mode_line_noprop (SDATA (elt), -1, prec);
17524 break;
17525 case MODE_LINE_STRING:
17526 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
17527 break;
17528 case MODE_LINE_DISPLAY:
17529 n += display_string (NULL, elt, Qnil, 0, 0, it,
17530 0, prec, 0, STRING_MULTIBYTE (elt));
17531 break;
17532 }
17533
17534 break;
17535 }
17536
17537 /* Handle the non-literal case. */
17538
17539 while ((precision <= 0 || n < precision)
17540 && SREF (elt, offset) != 0
17541 && (mode_line_target != MODE_LINE_DISPLAY
17542 || it->current_x < it->last_visible_x))
17543 {
17544 int last_offset = offset;
17545
17546 /* Advance to end of string or next format specifier. */
17547 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
17548 ;
17549
17550 if (offset - 1 != last_offset)
17551 {
17552 int nchars, nbytes;
17553
17554 /* Output to end of string or up to '%'. Field width
17555 is length of string. Don't output more than
17556 PRECISION allows us. */
17557 offset--;
17558
17559 prec = c_string_width (SDATA (elt) + last_offset,
17560 offset - last_offset, precision - n,
17561 &nchars, &nbytes);
17562
17563 switch (mode_line_target)
17564 {
17565 case MODE_LINE_NOPROP:
17566 case MODE_LINE_TITLE:
17567 n += store_mode_line_noprop (SDATA (elt) + last_offset, 0, prec);
17568 break;
17569 case MODE_LINE_STRING:
17570 {
17571 int bytepos = last_offset;
17572 int charpos = string_byte_to_char (elt, bytepos);
17573 int endpos = (precision <= 0
17574 ? string_byte_to_char (elt, offset)
17575 : charpos + nchars);
17576
17577 n += store_mode_line_string (NULL,
17578 Fsubstring (elt, make_number (charpos),
17579 make_number (endpos)),
17580 0, 0, 0, Qnil);
17581 }
17582 break;
17583 case MODE_LINE_DISPLAY:
17584 {
17585 int bytepos = last_offset;
17586 int charpos = string_byte_to_char (elt, bytepos);
17587
17588 if (precision <= 0)
17589 nchars = string_byte_to_char (elt, offset) - charpos;
17590 n += display_string (NULL, elt, Qnil, 0, charpos,
17591 it, 0, nchars, 0,
17592 STRING_MULTIBYTE (elt));
17593 }
17594 break;
17595 }
17596 }
17597 else /* c == '%' */
17598 {
17599 int percent_position = offset;
17600
17601 /* Get the specified minimum width. Zero means
17602 don't pad. */
17603 field = 0;
17604 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
17605 field = field * 10 + c - '0';
17606
17607 /* Don't pad beyond the total padding allowed. */
17608 if (field_width - n > 0 && field > field_width - n)
17609 field = field_width - n;
17610
17611 /* Note that either PRECISION <= 0 or N < PRECISION. */
17612 prec = precision - n;
17613
17614 if (c == 'M')
17615 n += display_mode_element (it, depth, field, prec,
17616 Vglobal_mode_string, props,
17617 risky);
17618 else if (c != 0)
17619 {
17620 int multibyte;
17621 int bytepos, charpos;
17622 unsigned char *spec;
17623
17624 bytepos = percent_position;
17625 charpos = (STRING_MULTIBYTE (elt)
17626 ? string_byte_to_char (elt, bytepos)
17627 : bytepos);
17628 spec
17629 = decode_mode_spec (it->w, c, field, prec, &multibyte);
17630
17631 switch (mode_line_target)
17632 {
17633 case MODE_LINE_NOPROP:
17634 case MODE_LINE_TITLE:
17635 n += store_mode_line_noprop (spec, field, prec);
17636 break;
17637 case MODE_LINE_STRING:
17638 {
17639 int len = strlen (spec);
17640 Lisp_Object tem = make_string (spec, len);
17641 props = Ftext_properties_at (make_number (charpos), elt);
17642 /* Should only keep face property in props */
17643 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
17644 }
17645 break;
17646 case MODE_LINE_DISPLAY:
17647 {
17648 int nglyphs_before, nwritten;
17649
17650 nglyphs_before = it->glyph_row->used[TEXT_AREA];
17651 nwritten = display_string (spec, Qnil, elt,
17652 charpos, 0, it,
17653 field, prec, 0,
17654 multibyte);
17655
17656 /* Assign to the glyphs written above the
17657 string where the `%x' came from, position
17658 of the `%'. */
17659 if (nwritten > 0)
17660 {
17661 struct glyph *glyph
17662 = (it->glyph_row->glyphs[TEXT_AREA]
17663 + nglyphs_before);
17664 int i;
17665
17666 for (i = 0; i < nwritten; ++i)
17667 {
17668 glyph[i].object = elt;
17669 glyph[i].charpos = charpos;
17670 }
17671
17672 n += nwritten;
17673 }
17674 }
17675 break;
17676 }
17677 }
17678 else /* c == 0 */
17679 break;
17680 }
17681 }
17682 }
17683 break;
17684
17685 case Lisp_Symbol:
17686 /* A symbol: process the value of the symbol recursively
17687 as if it appeared here directly. Avoid error if symbol void.
17688 Special case: if value of symbol is a string, output the string
17689 literally. */
17690 {
17691 register Lisp_Object tem;
17692
17693 /* If the variable is not marked as risky to set
17694 then its contents are risky to use. */
17695 if (NILP (Fget (elt, Qrisky_local_variable)))
17696 risky = 1;
17697
17698 tem = Fboundp (elt);
17699 if (!NILP (tem))
17700 {
17701 tem = Fsymbol_value (elt);
17702 /* If value is a string, output that string literally:
17703 don't check for % within it. */
17704 if (STRINGP (tem))
17705 literal = 1;
17706
17707 if (!EQ (tem, elt))
17708 {
17709 /* Give up right away for nil or t. */
17710 elt = tem;
17711 goto tail_recurse;
17712 }
17713 }
17714 }
17715 break;
17716
17717 case Lisp_Cons:
17718 {
17719 register Lisp_Object car, tem;
17720
17721 /* A cons cell: five distinct cases.
17722 If first element is :eval or :propertize, do something special.
17723 If first element is a string or a cons, process all the elements
17724 and effectively concatenate them.
17725 If first element is a negative number, truncate displaying cdr to
17726 at most that many characters. If positive, pad (with spaces)
17727 to at least that many characters.
17728 If first element is a symbol, process the cadr or caddr recursively
17729 according to whether the symbol's value is non-nil or nil. */
17730 car = XCAR (elt);
17731 if (EQ (car, QCeval))
17732 {
17733 /* An element of the form (:eval FORM) means evaluate FORM
17734 and use the result as mode line elements. */
17735
17736 if (risky)
17737 break;
17738
17739 if (CONSP (XCDR (elt)))
17740 {
17741 Lisp_Object spec;
17742 spec = safe_eval (XCAR (XCDR (elt)));
17743 n += display_mode_element (it, depth, field_width - n,
17744 precision - n, spec, props,
17745 risky);
17746 }
17747 }
17748 else if (EQ (car, QCpropertize))
17749 {
17750 /* An element of the form (:propertize ELT PROPS...)
17751 means display ELT but applying properties PROPS. */
17752
17753 if (risky)
17754 break;
17755
17756 if (CONSP (XCDR (elt)))
17757 n += display_mode_element (it, depth, field_width - n,
17758 precision - n, XCAR (XCDR (elt)),
17759 XCDR (XCDR (elt)), risky);
17760 }
17761 else if (SYMBOLP (car))
17762 {
17763 tem = Fboundp (car);
17764 elt = XCDR (elt);
17765 if (!CONSP (elt))
17766 goto invalid;
17767 /* elt is now the cdr, and we know it is a cons cell.
17768 Use its car if CAR has a non-nil value. */
17769 if (!NILP (tem))
17770 {
17771 tem = Fsymbol_value (car);
17772 if (!NILP (tem))
17773 {
17774 elt = XCAR (elt);
17775 goto tail_recurse;
17776 }
17777 }
17778 /* Symbol's value is nil (or symbol is unbound)
17779 Get the cddr of the original list
17780 and if possible find the caddr and use that. */
17781 elt = XCDR (elt);
17782 if (NILP (elt))
17783 break;
17784 else if (!CONSP (elt))
17785 goto invalid;
17786 elt = XCAR (elt);
17787 goto tail_recurse;
17788 }
17789 else if (INTEGERP (car))
17790 {
17791 register int lim = XINT (car);
17792 elt = XCDR (elt);
17793 if (lim < 0)
17794 {
17795 /* Negative int means reduce maximum width. */
17796 if (precision <= 0)
17797 precision = -lim;
17798 else
17799 precision = min (precision, -lim);
17800 }
17801 else if (lim > 0)
17802 {
17803 /* Padding specified. Don't let it be more than
17804 current maximum. */
17805 if (precision > 0)
17806 lim = min (precision, lim);
17807
17808 /* If that's more padding than already wanted, queue it.
17809 But don't reduce padding already specified even if
17810 that is beyond the current truncation point. */
17811 field_width = max (lim, field_width);
17812 }
17813 goto tail_recurse;
17814 }
17815 else if (STRINGP (car) || CONSP (car))
17816 {
17817 register int limit = 50;
17818 /* Limit is to protect against circular lists. */
17819 while (CONSP (elt)
17820 && --limit > 0
17821 && (precision <= 0 || n < precision))
17822 {
17823 n += display_mode_element (it, depth,
17824 /* Do padding only after the last
17825 element in the list. */
17826 (! CONSP (XCDR (elt))
17827 ? field_width - n
17828 : 0),
17829 precision - n, XCAR (elt),
17830 props, risky);
17831 elt = XCDR (elt);
17832 }
17833 }
17834 }
17835 break;
17836
17837 default:
17838 invalid:
17839 elt = build_string ("*invalid*");
17840 goto tail_recurse;
17841 }
17842
17843 /* Pad to FIELD_WIDTH. */
17844 if (field_width > 0 && n < field_width)
17845 {
17846 switch (mode_line_target)
17847 {
17848 case MODE_LINE_NOPROP:
17849 case MODE_LINE_TITLE:
17850 n += store_mode_line_noprop ("", field_width - n, 0);
17851 break;
17852 case MODE_LINE_STRING:
17853 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
17854 break;
17855 case MODE_LINE_DISPLAY:
17856 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
17857 0, 0, 0);
17858 break;
17859 }
17860 }
17861
17862 return n;
17863 }
17864
17865 /* Store a mode-line string element in mode_line_string_list.
17866
17867 If STRING is non-null, display that C string. Otherwise, the Lisp
17868 string LISP_STRING is displayed.
17869
17870 FIELD_WIDTH is the minimum number of output glyphs to produce.
17871 If STRING has fewer characters than FIELD_WIDTH, pad to the right
17872 with spaces. FIELD_WIDTH <= 0 means don't pad.
17873
17874 PRECISION is the maximum number of characters to output from
17875 STRING. PRECISION <= 0 means don't truncate the string.
17876
17877 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
17878 properties to the string.
17879
17880 PROPS are the properties to add to the string.
17881 The mode_line_string_face face property is always added to the string.
17882 */
17883
17884 static int
17885 store_mode_line_string (string, lisp_string, copy_string, field_width, precision, props)
17886 char *string;
17887 Lisp_Object lisp_string;
17888 int copy_string;
17889 int field_width;
17890 int precision;
17891 Lisp_Object props;
17892 {
17893 int len;
17894 int n = 0;
17895
17896 if (string != NULL)
17897 {
17898 len = strlen (string);
17899 if (precision > 0 && len > precision)
17900 len = precision;
17901 lisp_string = make_string (string, len);
17902 if (NILP (props))
17903 props = mode_line_string_face_prop;
17904 else if (!NILP (mode_line_string_face))
17905 {
17906 Lisp_Object face = Fplist_get (props, Qface);
17907 props = Fcopy_sequence (props);
17908 if (NILP (face))
17909 face = mode_line_string_face;
17910 else
17911 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
17912 props = Fplist_put (props, Qface, face);
17913 }
17914 Fadd_text_properties (make_number (0), make_number (len),
17915 props, lisp_string);
17916 }
17917 else
17918 {
17919 len = XFASTINT (Flength (lisp_string));
17920 if (precision > 0 && len > precision)
17921 {
17922 len = precision;
17923 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
17924 precision = -1;
17925 }
17926 if (!NILP (mode_line_string_face))
17927 {
17928 Lisp_Object face;
17929 if (NILP (props))
17930 props = Ftext_properties_at (make_number (0), lisp_string);
17931 face = Fplist_get (props, Qface);
17932 if (NILP (face))
17933 face = mode_line_string_face;
17934 else
17935 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
17936 props = Fcons (Qface, Fcons (face, Qnil));
17937 if (copy_string)
17938 lisp_string = Fcopy_sequence (lisp_string);
17939 }
17940 if (!NILP (props))
17941 Fadd_text_properties (make_number (0), make_number (len),
17942 props, lisp_string);
17943 }
17944
17945 if (len > 0)
17946 {
17947 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
17948 n += len;
17949 }
17950
17951 if (field_width > len)
17952 {
17953 field_width -= len;
17954 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
17955 if (!NILP (props))
17956 Fadd_text_properties (make_number (0), make_number (field_width),
17957 props, lisp_string);
17958 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
17959 n += field_width;
17960 }
17961
17962 return n;
17963 }
17964
17965
17966 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
17967 1, 4, 0,
17968 doc: /* Format a string out of a mode line format specification.
17969 First arg FORMAT specifies the mode line format (see `mode-line-format'
17970 for details) to use.
17971
17972 Optional second arg FACE specifies the face property to put
17973 on all characters for which no face is specified.
17974 The value t means whatever face the window's mode line currently uses
17975 \(either `mode-line' or `mode-line-inactive', depending).
17976 A value of nil means the default is no face property.
17977 If FACE is an integer, the value string has no text properties.
17978
17979 Optional third and fourth args WINDOW and BUFFER specify the window
17980 and buffer to use as the context for the formatting (defaults
17981 are the selected window and the window's buffer). */)
17982 (format, face, window, buffer)
17983 Lisp_Object format, face, window, buffer;
17984 {
17985 struct it it;
17986 int len;
17987 struct window *w;
17988 struct buffer *old_buffer = NULL;
17989 int face_id = -1;
17990 int no_props = INTEGERP (face);
17991 int count = SPECPDL_INDEX ();
17992 Lisp_Object str;
17993 int string_start = 0;
17994
17995 if (NILP (window))
17996 window = selected_window;
17997 CHECK_WINDOW (window);
17998 w = XWINDOW (window);
17999
18000 if (NILP (buffer))
18001 buffer = w->buffer;
18002 CHECK_BUFFER (buffer);
18003
18004 /* Make formatting the modeline a non-op when noninteractive, otherwise
18005 there will be problems later caused by a partially initialized frame. */
18006 if (NILP (format) || noninteractive)
18007 return empty_unibyte_string;
18008
18009 if (no_props)
18010 face = Qnil;
18011
18012 if (!NILP (face))
18013 {
18014 if (EQ (face, Qt))
18015 face = (EQ (window, selected_window) ? Qmode_line : Qmode_line_inactive);
18016 face_id = lookup_named_face (XFRAME (WINDOW_FRAME (w)), face, 0);
18017 }
18018
18019 if (face_id < 0)
18020 face_id = DEFAULT_FACE_ID;
18021
18022 if (XBUFFER (buffer) != current_buffer)
18023 old_buffer = current_buffer;
18024
18025 /* Save things including mode_line_proptrans_alist,
18026 and set that to nil so that we don't alter the outer value. */
18027 record_unwind_protect (unwind_format_mode_line,
18028 format_mode_line_unwind_data
18029 (old_buffer, selected_window, 1));
18030 mode_line_proptrans_alist = Qnil;
18031
18032 Fselect_window (window, Qt);
18033 if (old_buffer)
18034 set_buffer_internal_1 (XBUFFER (buffer));
18035
18036 init_iterator (&it, w, -1, -1, NULL, face_id);
18037
18038 if (no_props)
18039 {
18040 mode_line_target = MODE_LINE_NOPROP;
18041 mode_line_string_face_prop = Qnil;
18042 mode_line_string_list = Qnil;
18043 string_start = MODE_LINE_NOPROP_LEN (0);
18044 }
18045 else
18046 {
18047 mode_line_target = MODE_LINE_STRING;
18048 mode_line_string_list = Qnil;
18049 mode_line_string_face = face;
18050 mode_line_string_face_prop
18051 = (NILP (face) ? Qnil : Fcons (Qface, Fcons (face, Qnil)));
18052 }
18053
18054 push_kboard (FRAME_KBOARD (it.f));
18055 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
18056 pop_kboard ();
18057
18058 if (no_props)
18059 {
18060 len = MODE_LINE_NOPROP_LEN (string_start);
18061 str = make_string (mode_line_noprop_buf + string_start, len);
18062 }
18063 else
18064 {
18065 mode_line_string_list = Fnreverse (mode_line_string_list);
18066 str = Fmapconcat (intern ("identity"), mode_line_string_list,
18067 empty_unibyte_string);
18068 }
18069
18070 unbind_to (count, Qnil);
18071 return str;
18072 }
18073
18074 /* Write a null-terminated, right justified decimal representation of
18075 the positive integer D to BUF using a minimal field width WIDTH. */
18076
18077 static void
18078 pint2str (buf, width, d)
18079 register char *buf;
18080 register int width;
18081 register int d;
18082 {
18083 register char *p = buf;
18084
18085 if (d <= 0)
18086 *p++ = '0';
18087 else
18088 {
18089 while (d > 0)
18090 {
18091 *p++ = d % 10 + '0';
18092 d /= 10;
18093 }
18094 }
18095
18096 for (width -= (int) (p - buf); width > 0; --width)
18097 *p++ = ' ';
18098 *p-- = '\0';
18099 while (p > buf)
18100 {
18101 d = *buf;
18102 *buf++ = *p;
18103 *p-- = d;
18104 }
18105 }
18106
18107 /* Write a null-terminated, right justified decimal and "human
18108 readable" representation of the nonnegative integer D to BUF using
18109 a minimal field width WIDTH. D should be smaller than 999.5e24. */
18110
18111 static const char power_letter[] =
18112 {
18113 0, /* not used */
18114 'k', /* kilo */
18115 'M', /* mega */
18116 'G', /* giga */
18117 'T', /* tera */
18118 'P', /* peta */
18119 'E', /* exa */
18120 'Z', /* zetta */
18121 'Y' /* yotta */
18122 };
18123
18124 static void
18125 pint2hrstr (buf, width, d)
18126 char *buf;
18127 int width;
18128 int d;
18129 {
18130 /* We aim to represent the nonnegative integer D as
18131 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
18132 int quotient = d;
18133 int remainder = 0;
18134 /* -1 means: do not use TENTHS. */
18135 int tenths = -1;
18136 int exponent = 0;
18137
18138 /* Length of QUOTIENT.TENTHS as a string. */
18139 int length;
18140
18141 char * psuffix;
18142 char * p;
18143
18144 if (1000 <= quotient)
18145 {
18146 /* Scale to the appropriate EXPONENT. */
18147 do
18148 {
18149 remainder = quotient % 1000;
18150 quotient /= 1000;
18151 exponent++;
18152 }
18153 while (1000 <= quotient);
18154
18155 /* Round to nearest and decide whether to use TENTHS or not. */
18156 if (quotient <= 9)
18157 {
18158 tenths = remainder / 100;
18159 if (50 <= remainder % 100)
18160 {
18161 if (tenths < 9)
18162 tenths++;
18163 else
18164 {
18165 quotient++;
18166 if (quotient == 10)
18167 tenths = -1;
18168 else
18169 tenths = 0;
18170 }
18171 }
18172 }
18173 else
18174 if (500 <= remainder)
18175 {
18176 if (quotient < 999)
18177 quotient++;
18178 else
18179 {
18180 quotient = 1;
18181 exponent++;
18182 tenths = 0;
18183 }
18184 }
18185 }
18186
18187 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
18188 if (tenths == -1 && quotient <= 99)
18189 if (quotient <= 9)
18190 length = 1;
18191 else
18192 length = 2;
18193 else
18194 length = 3;
18195 p = psuffix = buf + max (width, length);
18196
18197 /* Print EXPONENT. */
18198 if (exponent)
18199 *psuffix++ = power_letter[exponent];
18200 *psuffix = '\0';
18201
18202 /* Print TENTHS. */
18203 if (tenths >= 0)
18204 {
18205 *--p = '0' + tenths;
18206 *--p = '.';
18207 }
18208
18209 /* Print QUOTIENT. */
18210 do
18211 {
18212 int digit = quotient % 10;
18213 *--p = '0' + digit;
18214 }
18215 while ((quotient /= 10) != 0);
18216
18217 /* Print leading spaces. */
18218 while (buf < p)
18219 *--p = ' ';
18220 }
18221
18222 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
18223 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
18224 type of CODING_SYSTEM. Return updated pointer into BUF. */
18225
18226 static unsigned char invalid_eol_type[] = "(*invalid*)";
18227
18228 static char *
18229 decode_mode_spec_coding (coding_system, buf, eol_flag)
18230 Lisp_Object coding_system;
18231 register char *buf;
18232 int eol_flag;
18233 {
18234 Lisp_Object val;
18235 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
18236 const unsigned char *eol_str;
18237 int eol_str_len;
18238 /* The EOL conversion we are using. */
18239 Lisp_Object eoltype;
18240
18241 val = CODING_SYSTEM_SPEC (coding_system);
18242 eoltype = Qnil;
18243
18244 if (!VECTORP (val)) /* Not yet decided. */
18245 {
18246 if (multibyte)
18247 *buf++ = '-';
18248 if (eol_flag)
18249 eoltype = eol_mnemonic_undecided;
18250 /* Don't mention EOL conversion if it isn't decided. */
18251 }
18252 else
18253 {
18254 Lisp_Object attrs;
18255 Lisp_Object eolvalue;
18256
18257 attrs = AREF (val, 0);
18258 eolvalue = AREF (val, 2);
18259
18260 if (multibyte)
18261 *buf++ = XFASTINT (CODING_ATTR_MNEMONIC (attrs));
18262
18263 if (eol_flag)
18264 {
18265 /* The EOL conversion that is normal on this system. */
18266
18267 if (NILP (eolvalue)) /* Not yet decided. */
18268 eoltype = eol_mnemonic_undecided;
18269 else if (VECTORP (eolvalue)) /* Not yet decided. */
18270 eoltype = eol_mnemonic_undecided;
18271 else /* eolvalue is Qunix, Qdos, or Qmac. */
18272 eoltype = (EQ (eolvalue, Qunix)
18273 ? eol_mnemonic_unix
18274 : (EQ (eolvalue, Qdos) == 1
18275 ? eol_mnemonic_dos : eol_mnemonic_mac));
18276 }
18277 }
18278
18279 if (eol_flag)
18280 {
18281 /* Mention the EOL conversion if it is not the usual one. */
18282 if (STRINGP (eoltype))
18283 {
18284 eol_str = SDATA (eoltype);
18285 eol_str_len = SBYTES (eoltype);
18286 }
18287 else if (CHARACTERP (eoltype))
18288 {
18289 unsigned char *tmp = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
18290 eol_str_len = CHAR_STRING (XINT (eoltype), tmp);
18291 eol_str = tmp;
18292 }
18293 else
18294 {
18295 eol_str = invalid_eol_type;
18296 eol_str_len = sizeof (invalid_eol_type) - 1;
18297 }
18298 bcopy (eol_str, buf, eol_str_len);
18299 buf += eol_str_len;
18300 }
18301
18302 return buf;
18303 }
18304
18305 /* Return a string for the output of a mode line %-spec for window W,
18306 generated by character C. PRECISION >= 0 means don't return a
18307 string longer than that value. FIELD_WIDTH > 0 means pad the
18308 string returned with spaces to that value. Return 1 in *MULTIBYTE
18309 if the result is multibyte text.
18310
18311 Note we operate on the current buffer for most purposes,
18312 the exception being w->base_line_pos. */
18313
18314 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
18315
18316 static char *
18317 decode_mode_spec (w, c, field_width, precision, multibyte)
18318 struct window *w;
18319 register int c;
18320 int field_width, precision;
18321 int *multibyte;
18322 {
18323 Lisp_Object obj;
18324 struct frame *f = XFRAME (WINDOW_FRAME (w));
18325 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
18326 struct buffer *b = current_buffer;
18327
18328 obj = Qnil;
18329 *multibyte = 0;
18330
18331 switch (c)
18332 {
18333 case '*':
18334 if (!NILP (b->read_only))
18335 return "%";
18336 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
18337 return "*";
18338 return "-";
18339
18340 case '+':
18341 /* This differs from %* only for a modified read-only buffer. */
18342 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
18343 return "*";
18344 if (!NILP (b->read_only))
18345 return "%";
18346 return "-";
18347
18348 case '&':
18349 /* This differs from %* in ignoring read-only-ness. */
18350 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
18351 return "*";
18352 return "-";
18353
18354 case '%':
18355 return "%";
18356
18357 case '[':
18358 {
18359 int i;
18360 char *p;
18361
18362 if (command_loop_level > 5)
18363 return "[[[... ";
18364 p = decode_mode_spec_buf;
18365 for (i = 0; i < command_loop_level; i++)
18366 *p++ = '[';
18367 *p = 0;
18368 return decode_mode_spec_buf;
18369 }
18370
18371 case ']':
18372 {
18373 int i;
18374 char *p;
18375
18376 if (command_loop_level > 5)
18377 return " ...]]]";
18378 p = decode_mode_spec_buf;
18379 for (i = 0; i < command_loop_level; i++)
18380 *p++ = ']';
18381 *p = 0;
18382 return decode_mode_spec_buf;
18383 }
18384
18385 case '-':
18386 {
18387 register int i;
18388
18389 /* Let lots_of_dashes be a string of infinite length. */
18390 if (mode_line_target == MODE_LINE_NOPROP ||
18391 mode_line_target == MODE_LINE_STRING)
18392 return "--";
18393 if (field_width <= 0
18394 || field_width > sizeof (lots_of_dashes))
18395 {
18396 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
18397 decode_mode_spec_buf[i] = '-';
18398 decode_mode_spec_buf[i] = '\0';
18399 return decode_mode_spec_buf;
18400 }
18401 else
18402 return lots_of_dashes;
18403 }
18404
18405 case 'b':
18406 obj = b->name;
18407 break;
18408
18409 case 'c':
18410 /* %c and %l are ignored in `frame-title-format'.
18411 (In redisplay_internal, the frame title is drawn _before_ the
18412 windows are updated, so the stuff which depends on actual
18413 window contents (such as %l) may fail to render properly, or
18414 even crash emacs.) */
18415 if (mode_line_target == MODE_LINE_TITLE)
18416 return "";
18417 else
18418 {
18419 int col = (int) current_column (); /* iftc */
18420 w->column_number_displayed = make_number (col);
18421 pint2str (decode_mode_spec_buf, field_width, col);
18422 return decode_mode_spec_buf;
18423 }
18424
18425 case 'e':
18426 #ifndef SYSTEM_MALLOC
18427 {
18428 if (NILP (Vmemory_full))
18429 return "";
18430 else
18431 return "!MEM FULL! ";
18432 }
18433 #else
18434 return "";
18435 #endif
18436
18437 case 'F':
18438 /* %F displays the frame name. */
18439 if (!NILP (f->title))
18440 return (char *) SDATA (f->title);
18441 if (f->explicit_name || ! FRAME_WINDOW_P (f))
18442 return (char *) SDATA (f->name);
18443 return "Emacs";
18444
18445 case 'f':
18446 obj = b->filename;
18447 break;
18448
18449 case 'i':
18450 {
18451 int size = ZV - BEGV;
18452 pint2str (decode_mode_spec_buf, field_width, size);
18453 return decode_mode_spec_buf;
18454 }
18455
18456 case 'I':
18457 {
18458 int size = ZV - BEGV;
18459 pint2hrstr (decode_mode_spec_buf, field_width, size);
18460 return decode_mode_spec_buf;
18461 }
18462
18463 case 'l':
18464 {
18465 int startpos, startpos_byte, line, linepos, linepos_byte;
18466 int topline, nlines, junk, height;
18467
18468 /* %c and %l are ignored in `frame-title-format'. */
18469 if (mode_line_target == MODE_LINE_TITLE)
18470 return "";
18471
18472 startpos = XMARKER (w->start)->charpos;
18473 startpos_byte = marker_byte_position (w->start);
18474 height = WINDOW_TOTAL_LINES (w);
18475
18476 /* If we decided that this buffer isn't suitable for line numbers,
18477 don't forget that too fast. */
18478 if (EQ (w->base_line_pos, w->buffer))
18479 goto no_value;
18480 /* But do forget it, if the window shows a different buffer now. */
18481 else if (BUFFERP (w->base_line_pos))
18482 w->base_line_pos = Qnil;
18483
18484 /* If the buffer is very big, don't waste time. */
18485 if (INTEGERP (Vline_number_display_limit)
18486 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
18487 {
18488 w->base_line_pos = Qnil;
18489 w->base_line_number = Qnil;
18490 goto no_value;
18491 }
18492
18493 if (INTEGERP (w->base_line_number)
18494 && INTEGERP (w->base_line_pos)
18495 && XFASTINT (w->base_line_pos) <= startpos)
18496 {
18497 line = XFASTINT (w->base_line_number);
18498 linepos = XFASTINT (w->base_line_pos);
18499 linepos_byte = buf_charpos_to_bytepos (b, linepos);
18500 }
18501 else
18502 {
18503 line = 1;
18504 linepos = BUF_BEGV (b);
18505 linepos_byte = BUF_BEGV_BYTE (b);
18506 }
18507
18508 /* Count lines from base line to window start position. */
18509 nlines = display_count_lines (linepos, linepos_byte,
18510 startpos_byte,
18511 startpos, &junk);
18512
18513 topline = nlines + line;
18514
18515 /* Determine a new base line, if the old one is too close
18516 or too far away, or if we did not have one.
18517 "Too close" means it's plausible a scroll-down would
18518 go back past it. */
18519 if (startpos == BUF_BEGV (b))
18520 {
18521 w->base_line_number = make_number (topline);
18522 w->base_line_pos = make_number (BUF_BEGV (b));
18523 }
18524 else if (nlines < height + 25 || nlines > height * 3 + 50
18525 || linepos == BUF_BEGV (b))
18526 {
18527 int limit = BUF_BEGV (b);
18528 int limit_byte = BUF_BEGV_BYTE (b);
18529 int position;
18530 int distance = (height * 2 + 30) * line_number_display_limit_width;
18531
18532 if (startpos - distance > limit)
18533 {
18534 limit = startpos - distance;
18535 limit_byte = CHAR_TO_BYTE (limit);
18536 }
18537
18538 nlines = display_count_lines (startpos, startpos_byte,
18539 limit_byte,
18540 - (height * 2 + 30),
18541 &position);
18542 /* If we couldn't find the lines we wanted within
18543 line_number_display_limit_width chars per line,
18544 give up on line numbers for this window. */
18545 if (position == limit_byte && limit == startpos - distance)
18546 {
18547 w->base_line_pos = w->buffer;
18548 w->base_line_number = Qnil;
18549 goto no_value;
18550 }
18551
18552 w->base_line_number = make_number (topline - nlines);
18553 w->base_line_pos = make_number (BYTE_TO_CHAR (position));
18554 }
18555
18556 /* Now count lines from the start pos to point. */
18557 nlines = display_count_lines (startpos, startpos_byte,
18558 PT_BYTE, PT, &junk);
18559
18560 /* Record that we did display the line number. */
18561 line_number_displayed = 1;
18562
18563 /* Make the string to show. */
18564 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
18565 return decode_mode_spec_buf;
18566 no_value:
18567 {
18568 char* p = decode_mode_spec_buf;
18569 int pad = field_width - 2;
18570 while (pad-- > 0)
18571 *p++ = ' ';
18572 *p++ = '?';
18573 *p++ = '?';
18574 *p = '\0';
18575 return decode_mode_spec_buf;
18576 }
18577 }
18578 break;
18579
18580 case 'm':
18581 obj = b->mode_name;
18582 break;
18583
18584 case 'n':
18585 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
18586 return " Narrow";
18587 break;
18588
18589 case 'p':
18590 {
18591 int pos = marker_position (w->start);
18592 int total = BUF_ZV (b) - BUF_BEGV (b);
18593
18594 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
18595 {
18596 if (pos <= BUF_BEGV (b))
18597 return "All";
18598 else
18599 return "Bottom";
18600 }
18601 else if (pos <= BUF_BEGV (b))
18602 return "Top";
18603 else
18604 {
18605 if (total > 1000000)
18606 /* Do it differently for a large value, to avoid overflow. */
18607 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
18608 else
18609 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
18610 /* We can't normally display a 3-digit number,
18611 so get us a 2-digit number that is close. */
18612 if (total == 100)
18613 total = 99;
18614 sprintf (decode_mode_spec_buf, "%2d%%", total);
18615 return decode_mode_spec_buf;
18616 }
18617 }
18618
18619 /* Display percentage of size above the bottom of the screen. */
18620 case 'P':
18621 {
18622 int toppos = marker_position (w->start);
18623 int botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
18624 int total = BUF_ZV (b) - BUF_BEGV (b);
18625
18626 if (botpos >= BUF_ZV (b))
18627 {
18628 if (toppos <= BUF_BEGV (b))
18629 return "All";
18630 else
18631 return "Bottom";
18632 }
18633 else
18634 {
18635 if (total > 1000000)
18636 /* Do it differently for a large value, to avoid overflow. */
18637 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
18638 else
18639 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
18640 /* We can't normally display a 3-digit number,
18641 so get us a 2-digit number that is close. */
18642 if (total == 100)
18643 total = 99;
18644 if (toppos <= BUF_BEGV (b))
18645 sprintf (decode_mode_spec_buf, "Top%2d%%", total);
18646 else
18647 sprintf (decode_mode_spec_buf, "%2d%%", total);
18648 return decode_mode_spec_buf;
18649 }
18650 }
18651
18652 case 's':
18653 /* status of process */
18654 obj = Fget_buffer_process (Fcurrent_buffer ());
18655 if (NILP (obj))
18656 return "no process";
18657 #ifdef subprocesses
18658 obj = Fsymbol_name (Fprocess_status (obj));
18659 #endif
18660 break;
18661
18662 case '@':
18663 {
18664 Lisp_Object val;
18665 val = call1 (intern ("file-remote-p"), current_buffer->directory);
18666 if (NILP (val))
18667 return "-";
18668 else
18669 return "@";
18670 }
18671
18672 case 't': /* indicate TEXT or BINARY */
18673 #ifdef MODE_LINE_BINARY_TEXT
18674 return MODE_LINE_BINARY_TEXT (b);
18675 #else
18676 return "T";
18677 #endif
18678
18679 case 'z':
18680 /* coding-system (not including end-of-line format) */
18681 case 'Z':
18682 /* coding-system (including end-of-line type) */
18683 {
18684 int eol_flag = (c == 'Z');
18685 char *p = decode_mode_spec_buf;
18686
18687 if (! FRAME_WINDOW_P (f))
18688 {
18689 /* No need to mention EOL here--the terminal never needs
18690 to do EOL conversion. */
18691 p = decode_mode_spec_coding (CODING_ID_NAME
18692 (FRAME_KEYBOARD_CODING (f)->id),
18693 p, 0);
18694 p = decode_mode_spec_coding (CODING_ID_NAME
18695 (FRAME_TERMINAL_CODING (f)->id),
18696 p, 0);
18697 }
18698 p = decode_mode_spec_coding (b->buffer_file_coding_system,
18699 p, eol_flag);
18700
18701 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
18702 #ifdef subprocesses
18703 obj = Fget_buffer_process (Fcurrent_buffer ());
18704 if (PROCESSP (obj))
18705 {
18706 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
18707 p, eol_flag);
18708 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
18709 p, eol_flag);
18710 }
18711 #endif /* subprocesses */
18712 #endif /* 0 */
18713 *p = 0;
18714 return decode_mode_spec_buf;
18715 }
18716 }
18717
18718 if (STRINGP (obj))
18719 {
18720 *multibyte = STRING_MULTIBYTE (obj);
18721 return (char *) SDATA (obj);
18722 }
18723 else
18724 return "";
18725 }
18726
18727
18728 /* Count up to COUNT lines starting from START / START_BYTE.
18729 But don't go beyond LIMIT_BYTE.
18730 Return the number of lines thus found (always nonnegative).
18731
18732 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
18733
18734 static int
18735 display_count_lines (start, start_byte, limit_byte, count, byte_pos_ptr)
18736 int start, start_byte, limit_byte, count;
18737 int *byte_pos_ptr;
18738 {
18739 register unsigned char *cursor;
18740 unsigned char *base;
18741
18742 register int ceiling;
18743 register unsigned char *ceiling_addr;
18744 int orig_count = count;
18745
18746 /* If we are not in selective display mode,
18747 check only for newlines. */
18748 int selective_display = (!NILP (current_buffer->selective_display)
18749 && !INTEGERP (current_buffer->selective_display));
18750
18751 if (count > 0)
18752 {
18753 while (start_byte < limit_byte)
18754 {
18755 ceiling = BUFFER_CEILING_OF (start_byte);
18756 ceiling = min (limit_byte - 1, ceiling);
18757 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
18758 base = (cursor = BYTE_POS_ADDR (start_byte));
18759 while (1)
18760 {
18761 if (selective_display)
18762 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
18763 ;
18764 else
18765 while (*cursor != '\n' && ++cursor != ceiling_addr)
18766 ;
18767
18768 if (cursor != ceiling_addr)
18769 {
18770 if (--count == 0)
18771 {
18772 start_byte += cursor - base + 1;
18773 *byte_pos_ptr = start_byte;
18774 return orig_count;
18775 }
18776 else
18777 if (++cursor == ceiling_addr)
18778 break;
18779 }
18780 else
18781 break;
18782 }
18783 start_byte += cursor - base;
18784 }
18785 }
18786 else
18787 {
18788 while (start_byte > limit_byte)
18789 {
18790 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
18791 ceiling = max (limit_byte, ceiling);
18792 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
18793 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
18794 while (1)
18795 {
18796 if (selective_display)
18797 while (--cursor != ceiling_addr
18798 && *cursor != '\n' && *cursor != 015)
18799 ;
18800 else
18801 while (--cursor != ceiling_addr && *cursor != '\n')
18802 ;
18803
18804 if (cursor != ceiling_addr)
18805 {
18806 if (++count == 0)
18807 {
18808 start_byte += cursor - base + 1;
18809 *byte_pos_ptr = start_byte;
18810 /* When scanning backwards, we should
18811 not count the newline posterior to which we stop. */
18812 return - orig_count - 1;
18813 }
18814 }
18815 else
18816 break;
18817 }
18818 /* Here we add 1 to compensate for the last decrement
18819 of CURSOR, which took it past the valid range. */
18820 start_byte += cursor - base + 1;
18821 }
18822 }
18823
18824 *byte_pos_ptr = limit_byte;
18825
18826 if (count < 0)
18827 return - orig_count + count;
18828 return orig_count - count;
18829
18830 }
18831
18832
18833 \f
18834 /***********************************************************************
18835 Displaying strings
18836 ***********************************************************************/
18837
18838 /* Display a NUL-terminated string, starting with index START.
18839
18840 If STRING is non-null, display that C string. Otherwise, the Lisp
18841 string LISP_STRING is displayed.
18842
18843 If FACE_STRING is not nil, FACE_STRING_POS is a position in
18844 FACE_STRING. Display STRING or LISP_STRING with the face at
18845 FACE_STRING_POS in FACE_STRING:
18846
18847 Display the string in the environment given by IT, but use the
18848 standard display table, temporarily.
18849
18850 FIELD_WIDTH is the minimum number of output glyphs to produce.
18851 If STRING has fewer characters than FIELD_WIDTH, pad to the right
18852 with spaces. If STRING has more characters, more than FIELD_WIDTH
18853 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
18854
18855 PRECISION is the maximum number of characters to output from
18856 STRING. PRECISION < 0 means don't truncate the string.
18857
18858 This is roughly equivalent to printf format specifiers:
18859
18860 FIELD_WIDTH PRECISION PRINTF
18861 ----------------------------------------
18862 -1 -1 %s
18863 -1 10 %.10s
18864 10 -1 %10s
18865 20 10 %20.10s
18866
18867 MULTIBYTE zero means do not display multibyte chars, > 0 means do
18868 display them, and < 0 means obey the current buffer's value of
18869 enable_multibyte_characters.
18870
18871 Value is the number of columns displayed. */
18872
18873 static int
18874 display_string (string, lisp_string, face_string, face_string_pos,
18875 start, it, field_width, precision, max_x, multibyte)
18876 unsigned char *string;
18877 Lisp_Object lisp_string;
18878 Lisp_Object face_string;
18879 EMACS_INT face_string_pos;
18880 EMACS_INT start;
18881 struct it *it;
18882 int field_width, precision, max_x;
18883 int multibyte;
18884 {
18885 int hpos_at_start = it->hpos;
18886 int saved_face_id = it->face_id;
18887 struct glyph_row *row = it->glyph_row;
18888
18889 /* Initialize the iterator IT for iteration over STRING beginning
18890 with index START. */
18891 reseat_to_string (it, string, lisp_string, start,
18892 precision, field_width, multibyte);
18893
18894 /* If displaying STRING, set up the face of the iterator
18895 from LISP_STRING, if that's given. */
18896 if (STRINGP (face_string))
18897 {
18898 EMACS_INT endptr;
18899 struct face *face;
18900
18901 it->face_id
18902 = face_at_string_position (it->w, face_string, face_string_pos,
18903 0, it->region_beg_charpos,
18904 it->region_end_charpos,
18905 &endptr, it->base_face_id, 0);
18906 face = FACE_FROM_ID (it->f, it->face_id);
18907 it->face_box_p = face->box != FACE_NO_BOX;
18908 }
18909
18910 /* Set max_x to the maximum allowed X position. Don't let it go
18911 beyond the right edge of the window. */
18912 if (max_x <= 0)
18913 max_x = it->last_visible_x;
18914 else
18915 max_x = min (max_x, it->last_visible_x);
18916
18917 /* Skip over display elements that are not visible. because IT->w is
18918 hscrolled. */
18919 if (it->current_x < it->first_visible_x)
18920 move_it_in_display_line_to (it, 100000, it->first_visible_x,
18921 MOVE_TO_POS | MOVE_TO_X);
18922
18923 row->ascent = it->max_ascent;
18924 row->height = it->max_ascent + it->max_descent;
18925 row->phys_ascent = it->max_phys_ascent;
18926 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
18927 row->extra_line_spacing = it->max_extra_line_spacing;
18928
18929 /* This condition is for the case that we are called with current_x
18930 past last_visible_x. */
18931 while (it->current_x < max_x)
18932 {
18933 int x_before, x, n_glyphs_before, i, nglyphs;
18934
18935 /* Get the next display element. */
18936 if (!get_next_display_element (it))
18937 break;
18938
18939 /* Produce glyphs. */
18940 x_before = it->current_x;
18941 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
18942 PRODUCE_GLYPHS (it);
18943
18944 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
18945 i = 0;
18946 x = x_before;
18947 while (i < nglyphs)
18948 {
18949 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
18950
18951 if (it->line_wrap != TRUNCATE
18952 && x + glyph->pixel_width > max_x)
18953 {
18954 /* End of continued line or max_x reached. */
18955 if (CHAR_GLYPH_PADDING_P (*glyph))
18956 {
18957 /* A wide character is unbreakable. */
18958 it->glyph_row->used[TEXT_AREA] = n_glyphs_before;
18959 it->current_x = x_before;
18960 }
18961 else
18962 {
18963 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
18964 it->current_x = x;
18965 }
18966 break;
18967 }
18968 else if (x + glyph->pixel_width >= it->first_visible_x)
18969 {
18970 /* Glyph is at least partially visible. */
18971 ++it->hpos;
18972 if (x < it->first_visible_x)
18973 it->glyph_row->x = x - it->first_visible_x;
18974 }
18975 else
18976 {
18977 /* Glyph is off the left margin of the display area.
18978 Should not happen. */
18979 abort ();
18980 }
18981
18982 row->ascent = max (row->ascent, it->max_ascent);
18983 row->height = max (row->height, it->max_ascent + it->max_descent);
18984 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
18985 row->phys_height = max (row->phys_height,
18986 it->max_phys_ascent + it->max_phys_descent);
18987 row->extra_line_spacing = max (row->extra_line_spacing,
18988 it->max_extra_line_spacing);
18989 x += glyph->pixel_width;
18990 ++i;
18991 }
18992
18993 /* Stop if max_x reached. */
18994 if (i < nglyphs)
18995 break;
18996
18997 /* Stop at line ends. */
18998 if (ITERATOR_AT_END_OF_LINE_P (it))
18999 {
19000 it->continuation_lines_width = 0;
19001 break;
19002 }
19003
19004 set_iterator_to_next (it, 1);
19005
19006 /* Stop if truncating at the right edge. */
19007 if (it->line_wrap == TRUNCATE
19008 && it->current_x >= it->last_visible_x)
19009 {
19010 /* Add truncation mark, but don't do it if the line is
19011 truncated at a padding space. */
19012 if (IT_CHARPOS (*it) < it->string_nchars)
19013 {
19014 if (!FRAME_WINDOW_P (it->f))
19015 {
19016 int i, n;
19017
19018 if (it->current_x > it->last_visible_x)
19019 {
19020 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
19021 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
19022 break;
19023 for (n = row->used[TEXT_AREA]; i < n; ++i)
19024 {
19025 row->used[TEXT_AREA] = i;
19026 produce_special_glyphs (it, IT_TRUNCATION);
19027 }
19028 }
19029 produce_special_glyphs (it, IT_TRUNCATION);
19030 }
19031 it->glyph_row->truncated_on_right_p = 1;
19032 }
19033 break;
19034 }
19035 }
19036
19037 /* Maybe insert a truncation at the left. */
19038 if (it->first_visible_x
19039 && IT_CHARPOS (*it) > 0)
19040 {
19041 if (!FRAME_WINDOW_P (it->f))
19042 insert_left_trunc_glyphs (it);
19043 it->glyph_row->truncated_on_left_p = 1;
19044 }
19045
19046 it->face_id = saved_face_id;
19047
19048 /* Value is number of columns displayed. */
19049 return it->hpos - hpos_at_start;
19050 }
19051
19052
19053 \f
19054 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
19055 appears as an element of LIST or as the car of an element of LIST.
19056 If PROPVAL is a list, compare each element against LIST in that
19057 way, and return 1/2 if any element of PROPVAL is found in LIST.
19058 Otherwise return 0. This function cannot quit.
19059 The return value is 2 if the text is invisible but with an ellipsis
19060 and 1 if it's invisible and without an ellipsis. */
19061
19062 int
19063 invisible_p (propval, list)
19064 register Lisp_Object propval;
19065 Lisp_Object list;
19066 {
19067 register Lisp_Object tail, proptail;
19068
19069 for (tail = list; CONSP (tail); tail = XCDR (tail))
19070 {
19071 register Lisp_Object tem;
19072 tem = XCAR (tail);
19073 if (EQ (propval, tem))
19074 return 1;
19075 if (CONSP (tem) && EQ (propval, XCAR (tem)))
19076 return NILP (XCDR (tem)) ? 1 : 2;
19077 }
19078
19079 if (CONSP (propval))
19080 {
19081 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
19082 {
19083 Lisp_Object propelt;
19084 propelt = XCAR (proptail);
19085 for (tail = list; CONSP (tail); tail = XCDR (tail))
19086 {
19087 register Lisp_Object tem;
19088 tem = XCAR (tail);
19089 if (EQ (propelt, tem))
19090 return 1;
19091 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
19092 return NILP (XCDR (tem)) ? 1 : 2;
19093 }
19094 }
19095 }
19096
19097 return 0;
19098 }
19099
19100 DEFUN ("invisible-p", Finvisible_p, Sinvisible_p, 1, 1, 0,
19101 doc: /* Non-nil if the property makes the text invisible.
19102 POS-OR-PROP can be a marker or number, in which case it is taken to be
19103 a position in the current buffer and the value of the `invisible' property
19104 is checked; or it can be some other value, which is then presumed to be the
19105 value of the `invisible' property of the text of interest.
19106 The non-nil value returned can be t for truly invisible text or something
19107 else if the text is replaced by an ellipsis. */)
19108 (pos_or_prop)
19109 Lisp_Object pos_or_prop;
19110 {
19111 Lisp_Object prop
19112 = (NATNUMP (pos_or_prop) || MARKERP (pos_or_prop)
19113 ? Fget_char_property (pos_or_prop, Qinvisible, Qnil)
19114 : pos_or_prop);
19115 int invis = TEXT_PROP_MEANS_INVISIBLE (prop);
19116 return (invis == 0 ? Qnil
19117 : invis == 1 ? Qt
19118 : make_number (invis));
19119 }
19120
19121 /* Calculate a width or height in pixels from a specification using
19122 the following elements:
19123
19124 SPEC ::=
19125 NUM - a (fractional) multiple of the default font width/height
19126 (NUM) - specifies exactly NUM pixels
19127 UNIT - a fixed number of pixels, see below.
19128 ELEMENT - size of a display element in pixels, see below.
19129 (NUM . SPEC) - equals NUM * SPEC
19130 (+ SPEC SPEC ...) - add pixel values
19131 (- SPEC SPEC ...) - subtract pixel values
19132 (- SPEC) - negate pixel value
19133
19134 NUM ::=
19135 INT or FLOAT - a number constant
19136 SYMBOL - use symbol's (buffer local) variable binding.
19137
19138 UNIT ::=
19139 in - pixels per inch *)
19140 mm - pixels per 1/1000 meter *)
19141 cm - pixels per 1/100 meter *)
19142 width - width of current font in pixels.
19143 height - height of current font in pixels.
19144
19145 *) using the ratio(s) defined in display-pixels-per-inch.
19146
19147 ELEMENT ::=
19148
19149 left-fringe - left fringe width in pixels
19150 right-fringe - right fringe width in pixels
19151
19152 left-margin - left margin width in pixels
19153 right-margin - right margin width in pixels
19154
19155 scroll-bar - scroll-bar area width in pixels
19156
19157 Examples:
19158
19159 Pixels corresponding to 5 inches:
19160 (5 . in)
19161
19162 Total width of non-text areas on left side of window (if scroll-bar is on left):
19163 '(space :width (+ left-fringe left-margin scroll-bar))
19164
19165 Align to first text column (in header line):
19166 '(space :align-to 0)
19167
19168 Align to middle of text area minus half the width of variable `my-image'
19169 containing a loaded image:
19170 '(space :align-to (0.5 . (- text my-image)))
19171
19172 Width of left margin minus width of 1 character in the default font:
19173 '(space :width (- left-margin 1))
19174
19175 Width of left margin minus width of 2 characters in the current font:
19176 '(space :width (- left-margin (2 . width)))
19177
19178 Center 1 character over left-margin (in header line):
19179 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
19180
19181 Different ways to express width of left fringe plus left margin minus one pixel:
19182 '(space :width (- (+ left-fringe left-margin) (1)))
19183 '(space :width (+ left-fringe left-margin (- (1))))
19184 '(space :width (+ left-fringe left-margin (-1)))
19185
19186 */
19187
19188 #define NUMVAL(X) \
19189 ((INTEGERP (X) || FLOATP (X)) \
19190 ? XFLOATINT (X) \
19191 : - 1)
19192
19193 int
19194 calc_pixel_width_or_height (res, it, prop, font, width_p, align_to)
19195 double *res;
19196 struct it *it;
19197 Lisp_Object prop;
19198 struct font *font;
19199 int width_p, *align_to;
19200 {
19201 double pixels;
19202
19203 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
19204 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
19205
19206 if (NILP (prop))
19207 return OK_PIXELS (0);
19208
19209 xassert (FRAME_LIVE_P (it->f));
19210
19211 if (SYMBOLP (prop))
19212 {
19213 if (SCHARS (SYMBOL_NAME (prop)) == 2)
19214 {
19215 char *unit = SDATA (SYMBOL_NAME (prop));
19216
19217 if (unit[0] == 'i' && unit[1] == 'n')
19218 pixels = 1.0;
19219 else if (unit[0] == 'm' && unit[1] == 'm')
19220 pixels = 25.4;
19221 else if (unit[0] == 'c' && unit[1] == 'm')
19222 pixels = 2.54;
19223 else
19224 pixels = 0;
19225 if (pixels > 0)
19226 {
19227 double ppi;
19228 #ifdef HAVE_WINDOW_SYSTEM
19229 if (FRAME_WINDOW_P (it->f)
19230 && (ppi = (width_p
19231 ? FRAME_X_DISPLAY_INFO (it->f)->resx
19232 : FRAME_X_DISPLAY_INFO (it->f)->resy),
19233 ppi > 0))
19234 return OK_PIXELS (ppi / pixels);
19235 #endif
19236
19237 if ((ppi = NUMVAL (Vdisplay_pixels_per_inch), ppi > 0)
19238 || (CONSP (Vdisplay_pixels_per_inch)
19239 && (ppi = (width_p
19240 ? NUMVAL (XCAR (Vdisplay_pixels_per_inch))
19241 : NUMVAL (XCDR (Vdisplay_pixels_per_inch))),
19242 ppi > 0)))
19243 return OK_PIXELS (ppi / pixels);
19244
19245 return 0;
19246 }
19247 }
19248
19249 #ifdef HAVE_WINDOW_SYSTEM
19250 if (EQ (prop, Qheight))
19251 return OK_PIXELS (font ? FONT_HEIGHT (font) : FRAME_LINE_HEIGHT (it->f));
19252 if (EQ (prop, Qwidth))
19253 return OK_PIXELS (font ? FONT_WIDTH (font) : FRAME_COLUMN_WIDTH (it->f));
19254 #else
19255 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
19256 return OK_PIXELS (1);
19257 #endif
19258
19259 if (EQ (prop, Qtext))
19260 return OK_PIXELS (width_p
19261 ? window_box_width (it->w, TEXT_AREA)
19262 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
19263
19264 if (align_to && *align_to < 0)
19265 {
19266 *res = 0;
19267 if (EQ (prop, Qleft))
19268 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
19269 if (EQ (prop, Qright))
19270 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
19271 if (EQ (prop, Qcenter))
19272 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
19273 + window_box_width (it->w, TEXT_AREA) / 2);
19274 if (EQ (prop, Qleft_fringe))
19275 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
19276 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
19277 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
19278 if (EQ (prop, Qright_fringe))
19279 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
19280 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
19281 : window_box_right_offset (it->w, TEXT_AREA));
19282 if (EQ (prop, Qleft_margin))
19283 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
19284 if (EQ (prop, Qright_margin))
19285 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
19286 if (EQ (prop, Qscroll_bar))
19287 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
19288 ? 0
19289 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
19290 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
19291 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
19292 : 0)));
19293 }
19294 else
19295 {
19296 if (EQ (prop, Qleft_fringe))
19297 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
19298 if (EQ (prop, Qright_fringe))
19299 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
19300 if (EQ (prop, Qleft_margin))
19301 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
19302 if (EQ (prop, Qright_margin))
19303 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
19304 if (EQ (prop, Qscroll_bar))
19305 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
19306 }
19307
19308 prop = Fbuffer_local_value (prop, it->w->buffer);
19309 }
19310
19311 if (INTEGERP (prop) || FLOATP (prop))
19312 {
19313 int base_unit = (width_p
19314 ? FRAME_COLUMN_WIDTH (it->f)
19315 : FRAME_LINE_HEIGHT (it->f));
19316 return OK_PIXELS (XFLOATINT (prop) * base_unit);
19317 }
19318
19319 if (CONSP (prop))
19320 {
19321 Lisp_Object car = XCAR (prop);
19322 Lisp_Object cdr = XCDR (prop);
19323
19324 if (SYMBOLP (car))
19325 {
19326 #ifdef HAVE_WINDOW_SYSTEM
19327 if (FRAME_WINDOW_P (it->f)
19328 && valid_image_p (prop))
19329 {
19330 int id = lookup_image (it->f, prop);
19331 struct image *img = IMAGE_FROM_ID (it->f, id);
19332
19333 return OK_PIXELS (width_p ? img->width : img->height);
19334 }
19335 #endif
19336 if (EQ (car, Qplus) || EQ (car, Qminus))
19337 {
19338 int first = 1;
19339 double px;
19340
19341 pixels = 0;
19342 while (CONSP (cdr))
19343 {
19344 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
19345 font, width_p, align_to))
19346 return 0;
19347 if (first)
19348 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
19349 else
19350 pixels += px;
19351 cdr = XCDR (cdr);
19352 }
19353 if (EQ (car, Qminus))
19354 pixels = -pixels;
19355 return OK_PIXELS (pixels);
19356 }
19357
19358 car = Fbuffer_local_value (car, it->w->buffer);
19359 }
19360
19361 if (INTEGERP (car) || FLOATP (car))
19362 {
19363 double fact;
19364 pixels = XFLOATINT (car);
19365 if (NILP (cdr))
19366 return OK_PIXELS (pixels);
19367 if (calc_pixel_width_or_height (&fact, it, cdr,
19368 font, width_p, align_to))
19369 return OK_PIXELS (pixels * fact);
19370 return 0;
19371 }
19372
19373 return 0;
19374 }
19375
19376 return 0;
19377 }
19378
19379 \f
19380 /***********************************************************************
19381 Glyph Display
19382 ***********************************************************************/
19383
19384 #ifdef HAVE_WINDOW_SYSTEM
19385
19386 #if GLYPH_DEBUG
19387
19388 void
19389 dump_glyph_string (s)
19390 struct glyph_string *s;
19391 {
19392 fprintf (stderr, "glyph string\n");
19393 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
19394 s->x, s->y, s->width, s->height);
19395 fprintf (stderr, " ybase = %d\n", s->ybase);
19396 fprintf (stderr, " hl = %d\n", s->hl);
19397 fprintf (stderr, " left overhang = %d, right = %d\n",
19398 s->left_overhang, s->right_overhang);
19399 fprintf (stderr, " nchars = %d\n", s->nchars);
19400 fprintf (stderr, " extends to end of line = %d\n",
19401 s->extends_to_end_of_line_p);
19402 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
19403 fprintf (stderr, " bg width = %d\n", s->background_width);
19404 }
19405
19406 #endif /* GLYPH_DEBUG */
19407
19408 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
19409 of XChar2b structures for S; it can't be allocated in
19410 init_glyph_string because it must be allocated via `alloca'. W
19411 is the window on which S is drawn. ROW and AREA are the glyph row
19412 and area within the row from which S is constructed. START is the
19413 index of the first glyph structure covered by S. HL is a
19414 face-override for drawing S. */
19415
19416 #ifdef HAVE_NTGUI
19417 #define OPTIONAL_HDC(hdc) hdc,
19418 #define DECLARE_HDC(hdc) HDC hdc;
19419 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
19420 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
19421 #endif
19422
19423 #ifndef OPTIONAL_HDC
19424 #define OPTIONAL_HDC(hdc)
19425 #define DECLARE_HDC(hdc)
19426 #define ALLOCATE_HDC(hdc, f)
19427 #define RELEASE_HDC(hdc, f)
19428 #endif
19429
19430 static void
19431 init_glyph_string (s, OPTIONAL_HDC (hdc) char2b, w, row, area, start, hl)
19432 struct glyph_string *s;
19433 DECLARE_HDC (hdc)
19434 XChar2b *char2b;
19435 struct window *w;
19436 struct glyph_row *row;
19437 enum glyph_row_area area;
19438 int start;
19439 enum draw_glyphs_face hl;
19440 {
19441 bzero (s, sizeof *s);
19442 s->w = w;
19443 s->f = XFRAME (w->frame);
19444 #ifdef HAVE_NTGUI
19445 s->hdc = hdc;
19446 #endif
19447 s->display = FRAME_X_DISPLAY (s->f);
19448 s->window = FRAME_X_WINDOW (s->f);
19449 s->char2b = char2b;
19450 s->hl = hl;
19451 s->row = row;
19452 s->area = area;
19453 s->first_glyph = row->glyphs[area] + start;
19454 s->height = row->height;
19455 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
19456
19457 /* Display the internal border below the tool-bar window. */
19458 if (WINDOWP (s->f->tool_bar_window)
19459 && s->w == XWINDOW (s->f->tool_bar_window))
19460 s->y -= FRAME_INTERNAL_BORDER_WIDTH (s->f);
19461
19462 s->ybase = s->y + row->ascent;
19463 }
19464
19465
19466 /* Append the list of glyph strings with head H and tail T to the list
19467 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
19468
19469 static INLINE void
19470 append_glyph_string_lists (head, tail, h, t)
19471 struct glyph_string **head, **tail;
19472 struct glyph_string *h, *t;
19473 {
19474 if (h)
19475 {
19476 if (*head)
19477 (*tail)->next = h;
19478 else
19479 *head = h;
19480 h->prev = *tail;
19481 *tail = t;
19482 }
19483 }
19484
19485
19486 /* Prepend the list of glyph strings with head H and tail T to the
19487 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
19488 result. */
19489
19490 static INLINE void
19491 prepend_glyph_string_lists (head, tail, h, t)
19492 struct glyph_string **head, **tail;
19493 struct glyph_string *h, *t;
19494 {
19495 if (h)
19496 {
19497 if (*head)
19498 (*head)->prev = t;
19499 else
19500 *tail = t;
19501 t->next = *head;
19502 *head = h;
19503 }
19504 }
19505
19506
19507 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
19508 Set *HEAD and *TAIL to the resulting list. */
19509
19510 static INLINE void
19511 append_glyph_string (head, tail, s)
19512 struct glyph_string **head, **tail;
19513 struct glyph_string *s;
19514 {
19515 s->next = s->prev = NULL;
19516 append_glyph_string_lists (head, tail, s, s);
19517 }
19518
19519
19520 /* Get face and two-byte form of character C in face FACE_ID on frame
19521 F. The encoding of C is returned in *CHAR2B. MULTIBYTE_P non-zero
19522 means we want to display multibyte text. DISPLAY_P non-zero means
19523 make sure that X resources for the face returned are allocated.
19524 Value is a pointer to a realized face that is ready for display if
19525 DISPLAY_P is non-zero. */
19526
19527 static INLINE struct face *
19528 get_char_face_and_encoding (f, c, face_id, char2b, multibyte_p, display_p)
19529 struct frame *f;
19530 int c, face_id;
19531 XChar2b *char2b;
19532 int multibyte_p, display_p;
19533 {
19534 struct face *face = FACE_FROM_ID (f, face_id);
19535
19536 if (face->font)
19537 {
19538 unsigned code = face->font->driver->encode_char (face->font, c);
19539
19540 if (code != FONT_INVALID_CODE)
19541 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
19542 else
19543 STORE_XCHAR2B (char2b, 0, 0);
19544 }
19545
19546 /* Make sure X resources of the face are allocated. */
19547 #ifdef HAVE_X_WINDOWS
19548 if (display_p)
19549 #endif
19550 {
19551 xassert (face != NULL);
19552 PREPARE_FACE_FOR_DISPLAY (f, face);
19553 }
19554
19555 return face;
19556 }
19557
19558
19559 /* Get face and two-byte form of character glyph GLYPH on frame F.
19560 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
19561 a pointer to a realized face that is ready for display. */
19562
19563 static INLINE struct face *
19564 get_glyph_face_and_encoding (f, glyph, char2b, two_byte_p)
19565 struct frame *f;
19566 struct glyph *glyph;
19567 XChar2b *char2b;
19568 int *two_byte_p;
19569 {
19570 struct face *face;
19571
19572 xassert (glyph->type == CHAR_GLYPH);
19573 face = FACE_FROM_ID (f, glyph->face_id);
19574
19575 if (two_byte_p)
19576 *two_byte_p = 0;
19577
19578 if (face->font)
19579 {
19580 unsigned code = face->font->driver->encode_char (face->font, glyph->u.ch);
19581
19582 if (code != FONT_INVALID_CODE)
19583 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
19584 else
19585 STORE_XCHAR2B (char2b, 0, 0);
19586 }
19587
19588 /* Make sure X resources of the face are allocated. */
19589 xassert (face != NULL);
19590 PREPARE_FACE_FOR_DISPLAY (f, face);
19591 return face;
19592 }
19593
19594
19595 /* Fill glyph string S with composition components specified by S->cmp.
19596
19597 BASE_FACE is the base face of the composition.
19598 S->gidx is the index of the first component for S.
19599
19600 OVERLAPS non-zero means S should draw the foreground only, and use
19601 its physical height for clipping. See also draw_glyphs.
19602
19603 Value is the index of a component not in S. */
19604
19605 static int
19606 fill_composite_glyph_string (s, base_face, overlaps)
19607 struct glyph_string *s;
19608 struct face *base_face;
19609 int overlaps;
19610 {
19611 int i;
19612
19613 xassert (s);
19614
19615 s->for_overlaps = overlaps;
19616
19617 if (s->cmp->method == COMPOSITION_WITH_GLYPH_STRING)
19618 {
19619 Lisp_Object gstring
19620 = AREF (XHASH_TABLE (composition_hash_table)->key_and_value,
19621 s->cmp->hash_index * 2);
19622
19623 s->face = base_face;
19624 s->font = base_face->font;
19625 for (i = 0, s->nchars = 0; i < s->cmp->glyph_len; i++, s->nchars++)
19626 {
19627 Lisp_Object g = LGSTRING_GLYPH (gstring, i);
19628 unsigned code;
19629 XChar2b * store_pos;
19630 if (NILP (g))
19631 break;
19632 code = LGLYPH_CODE (g);
19633 store_pos = s->char2b + i;
19634 STORE_XCHAR2B (store_pos, code >> 8, code & 0xFF);
19635 }
19636 s->width = s->cmp->pixel_width;
19637 }
19638 else
19639 {
19640 /* For all glyphs of this composition, starting at the offset
19641 S->gidx, until we reach the end of the definition or encounter a
19642 glyph that requires the different face, add it to S. */
19643 struct face *face;
19644
19645 s->face = NULL;
19646 s->font = NULL;
19647 for (i = s->gidx; i < s->cmp->glyph_len; i++)
19648 {
19649 int c = COMPOSITION_GLYPH (s->cmp, i);
19650
19651 if (c != '\t')
19652 {
19653 int face_id = FACE_FOR_CHAR (s->f, base_face->ascii_face, c,
19654 -1, Qnil);
19655
19656 face = get_char_face_and_encoding (s->f, c, face_id,
19657 s->char2b + i, 1, 1);
19658 if (face)
19659 {
19660 if (! s->face)
19661 {
19662 s->face = face;
19663 s->font = s->face->font;
19664 }
19665 else if (s->face != face)
19666 break;
19667 }
19668 }
19669 ++s->nchars;
19670 }
19671
19672 /* All glyph strings for the same composition has the same width,
19673 i.e. the width set for the first component of the composition. */
19674 s->width = s->first_glyph->pixel_width;
19675 }
19676
19677 /* If the specified font could not be loaded, use the frame's
19678 default font, but record the fact that we couldn't load it in
19679 the glyph string so that we can draw rectangles for the
19680 characters of the glyph string. */
19681 if (s->font == NULL)
19682 {
19683 s->font_not_found_p = 1;
19684 s->font = FRAME_FONT (s->f);
19685 }
19686
19687 /* Adjust base line for subscript/superscript text. */
19688 s->ybase += s->first_glyph->voffset;
19689
19690 /* This glyph string must always be drawn with 16-bit functions. */
19691 s->two_byte_p = 1;
19692
19693 return s->gidx + s->nchars;
19694 }
19695
19696
19697 /* Fill glyph string S from a sequence of character glyphs.
19698
19699 FACE_ID is the face id of the string. START is the index of the
19700 first glyph to consider, END is the index of the last + 1.
19701 OVERLAPS non-zero means S should draw the foreground only, and use
19702 its physical height for clipping. See also draw_glyphs.
19703
19704 Value is the index of the first glyph not in S. */
19705
19706 static int
19707 fill_glyph_string (s, face_id, start, end, overlaps)
19708 struct glyph_string *s;
19709 int face_id;
19710 int start, end, overlaps;
19711 {
19712 struct glyph *glyph, *last;
19713 int voffset;
19714 int glyph_not_available_p;
19715
19716 xassert (s->f == XFRAME (s->w->frame));
19717 xassert (s->nchars == 0);
19718 xassert (start >= 0 && end > start);
19719
19720 s->for_overlaps = overlaps,
19721 glyph = s->row->glyphs[s->area] + start;
19722 last = s->row->glyphs[s->area] + end;
19723 voffset = glyph->voffset;
19724 s->padding_p = glyph->padding_p;
19725 glyph_not_available_p = glyph->glyph_not_available_p;
19726
19727 while (glyph < last
19728 && glyph->type == CHAR_GLYPH
19729 && glyph->voffset == voffset
19730 /* Same face id implies same font, nowadays. */
19731 && glyph->face_id == face_id
19732 && glyph->glyph_not_available_p == glyph_not_available_p)
19733 {
19734 int two_byte_p;
19735
19736 s->face = get_glyph_face_and_encoding (s->f, glyph,
19737 s->char2b + s->nchars,
19738 &two_byte_p);
19739 s->two_byte_p = two_byte_p;
19740 ++s->nchars;
19741 xassert (s->nchars <= end - start);
19742 s->width += glyph->pixel_width;
19743 if (glyph++->padding_p != s->padding_p)
19744 break;
19745 }
19746
19747 s->font = s->face->font;
19748
19749 /* If the specified font could not be loaded, use the frame's font,
19750 but record the fact that we couldn't load it in
19751 S->font_not_found_p so that we can draw rectangles for the
19752 characters of the glyph string. */
19753 if (s->font == NULL || glyph_not_available_p)
19754 {
19755 s->font_not_found_p = 1;
19756 s->font = FRAME_FONT (s->f);
19757 }
19758
19759 /* Adjust base line for subscript/superscript text. */
19760 s->ybase += voffset;
19761
19762 xassert (s->face && s->face->gc);
19763 return glyph - s->row->glyphs[s->area];
19764 }
19765
19766
19767 /* Fill glyph string S from image glyph S->first_glyph. */
19768
19769 static void
19770 fill_image_glyph_string (s)
19771 struct glyph_string *s;
19772 {
19773 xassert (s->first_glyph->type == IMAGE_GLYPH);
19774 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
19775 xassert (s->img);
19776 s->slice = s->first_glyph->slice;
19777 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
19778 s->font = s->face->font;
19779 s->width = s->first_glyph->pixel_width;
19780
19781 /* Adjust base line for subscript/superscript text. */
19782 s->ybase += s->first_glyph->voffset;
19783 }
19784
19785
19786 /* Fill glyph string S from a sequence of stretch glyphs.
19787
19788 ROW is the glyph row in which the glyphs are found, AREA is the
19789 area within the row. START is the index of the first glyph to
19790 consider, END is the index of the last + 1.
19791
19792 Value is the index of the first glyph not in S. */
19793
19794 static int
19795 fill_stretch_glyph_string (s, row, area, start, end)
19796 struct glyph_string *s;
19797 struct glyph_row *row;
19798 enum glyph_row_area area;
19799 int start, end;
19800 {
19801 struct glyph *glyph, *last;
19802 int voffset, face_id;
19803
19804 xassert (s->first_glyph->type == STRETCH_GLYPH);
19805
19806 glyph = s->row->glyphs[s->area] + start;
19807 last = s->row->glyphs[s->area] + end;
19808 face_id = glyph->face_id;
19809 s->face = FACE_FROM_ID (s->f, face_id);
19810 s->font = s->face->font;
19811 s->width = glyph->pixel_width;
19812 s->nchars = 1;
19813 voffset = glyph->voffset;
19814
19815 for (++glyph;
19816 (glyph < last
19817 && glyph->type == STRETCH_GLYPH
19818 && glyph->voffset == voffset
19819 && glyph->face_id == face_id);
19820 ++glyph)
19821 s->width += glyph->pixel_width;
19822
19823 /* Adjust base line for subscript/superscript text. */
19824 s->ybase += voffset;
19825
19826 /* The case that face->gc == 0 is handled when drawing the glyph
19827 string by calling PREPARE_FACE_FOR_DISPLAY. */
19828 xassert (s->face);
19829 return glyph - s->row->glyphs[s->area];
19830 }
19831
19832 static struct font_metrics *
19833 get_per_char_metric (f, font, char2b)
19834 struct frame *f;
19835 struct font *font;
19836 XChar2b *char2b;
19837 {
19838 static struct font_metrics metrics;
19839 unsigned code = (XCHAR2B_BYTE1 (char2b) << 8) | XCHAR2B_BYTE2 (char2b);
19840 struct font *fontp;
19841
19842 if (! font || code == FONT_INVALID_CODE)
19843 return NULL;
19844 font->driver->text_extents (font, &code, 1, &metrics);
19845 return &metrics;
19846 }
19847
19848 /* EXPORT for RIF:
19849 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
19850 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
19851 assumed to be zero. */
19852
19853 void
19854 x_get_glyph_overhangs (glyph, f, left, right)
19855 struct glyph *glyph;
19856 struct frame *f;
19857 int *left, *right;
19858 {
19859 *left = *right = 0;
19860
19861 if (glyph->type == CHAR_GLYPH)
19862 {
19863 struct face *face;
19864 XChar2b char2b;
19865 struct font_metrics *pcm;
19866
19867 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
19868 if (face->font && (pcm = get_per_char_metric (f, face->font, &char2b)))
19869 {
19870 if (pcm->rbearing > pcm->width)
19871 *right = pcm->rbearing - pcm->width;
19872 if (pcm->lbearing < 0)
19873 *left = -pcm->lbearing;
19874 }
19875 }
19876 else if (glyph->type == COMPOSITE_GLYPH)
19877 {
19878 struct composition *cmp = composition_table[glyph->u.cmp_id];
19879
19880 *right = cmp->rbearing - cmp->pixel_width;
19881 *left = - cmp->lbearing;
19882 }
19883 }
19884
19885
19886 /* Return the index of the first glyph preceding glyph string S that
19887 is overwritten by S because of S's left overhang. Value is -1
19888 if no glyphs are overwritten. */
19889
19890 static int
19891 left_overwritten (s)
19892 struct glyph_string *s;
19893 {
19894 int k;
19895
19896 if (s->left_overhang)
19897 {
19898 int x = 0, i;
19899 struct glyph *glyphs = s->row->glyphs[s->area];
19900 int first = s->first_glyph - glyphs;
19901
19902 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
19903 x -= glyphs[i].pixel_width;
19904
19905 k = i + 1;
19906 }
19907 else
19908 k = -1;
19909
19910 return k;
19911 }
19912
19913
19914 /* Return the index of the first glyph preceding glyph string S that
19915 is overwriting S because of its right overhang. Value is -1 if no
19916 glyph in front of S overwrites S. */
19917
19918 static int
19919 left_overwriting (s)
19920 struct glyph_string *s;
19921 {
19922 int i, k, x;
19923 struct glyph *glyphs = s->row->glyphs[s->area];
19924 int first = s->first_glyph - glyphs;
19925
19926 k = -1;
19927 x = 0;
19928 for (i = first - 1; i >= 0; --i)
19929 {
19930 int left, right;
19931 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
19932 if (x + right > 0)
19933 k = i;
19934 x -= glyphs[i].pixel_width;
19935 }
19936
19937 return k;
19938 }
19939
19940
19941 /* Return the index of the last glyph following glyph string S that is
19942 overwritten by S because of S's right overhang. Value is -1 if
19943 no such glyph is found. */
19944
19945 static int
19946 right_overwritten (s)
19947 struct glyph_string *s;
19948 {
19949 int k = -1;
19950
19951 if (s->right_overhang)
19952 {
19953 int x = 0, i;
19954 struct glyph *glyphs = s->row->glyphs[s->area];
19955 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
19956 int end = s->row->used[s->area];
19957
19958 for (i = first; i < end && s->right_overhang > x; ++i)
19959 x += glyphs[i].pixel_width;
19960
19961 k = i;
19962 }
19963
19964 return k;
19965 }
19966
19967
19968 /* Return the index of the last glyph following glyph string S that
19969 overwrites S because of its left overhang. Value is negative
19970 if no such glyph is found. */
19971
19972 static int
19973 right_overwriting (s)
19974 struct glyph_string *s;
19975 {
19976 int i, k, x;
19977 int end = s->row->used[s->area];
19978 struct glyph *glyphs = s->row->glyphs[s->area];
19979 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
19980
19981 k = -1;
19982 x = 0;
19983 for (i = first; i < end; ++i)
19984 {
19985 int left, right;
19986 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
19987 if (x - left < 0)
19988 k = i;
19989 x += glyphs[i].pixel_width;
19990 }
19991
19992 return k;
19993 }
19994
19995
19996 /* Set background width of glyph string S. START is the index of the
19997 first glyph following S. LAST_X is the right-most x-position + 1
19998 in the drawing area. */
19999
20000 static INLINE void
20001 set_glyph_string_background_width (s, start, last_x)
20002 struct glyph_string *s;
20003 int start;
20004 int last_x;
20005 {
20006 /* If the face of this glyph string has to be drawn to the end of
20007 the drawing area, set S->extends_to_end_of_line_p. */
20008
20009 if (start == s->row->used[s->area]
20010 && s->area == TEXT_AREA
20011 && ((s->row->fill_line_p
20012 && (s->hl == DRAW_NORMAL_TEXT
20013 || s->hl == DRAW_IMAGE_RAISED
20014 || s->hl == DRAW_IMAGE_SUNKEN))
20015 || s->hl == DRAW_MOUSE_FACE))
20016 s->extends_to_end_of_line_p = 1;
20017
20018 /* If S extends its face to the end of the line, set its
20019 background_width to the distance to the right edge of the drawing
20020 area. */
20021 if (s->extends_to_end_of_line_p)
20022 s->background_width = last_x - s->x + 1;
20023 else
20024 s->background_width = s->width;
20025 }
20026
20027
20028 /* Compute overhangs and x-positions for glyph string S and its
20029 predecessors, or successors. X is the starting x-position for S.
20030 BACKWARD_P non-zero means process predecessors. */
20031
20032 static void
20033 compute_overhangs_and_x (s, x, backward_p)
20034 struct glyph_string *s;
20035 int x;
20036 int backward_p;
20037 {
20038 if (backward_p)
20039 {
20040 while (s)
20041 {
20042 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
20043 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
20044 x -= s->width;
20045 s->x = x;
20046 s = s->prev;
20047 }
20048 }
20049 else
20050 {
20051 while (s)
20052 {
20053 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
20054 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
20055 s->x = x;
20056 x += s->width;
20057 s = s->next;
20058 }
20059 }
20060 }
20061
20062
20063
20064 /* The following macros are only called from draw_glyphs below.
20065 They reference the following parameters of that function directly:
20066 `w', `row', `area', and `overlap_p'
20067 as well as the following local variables:
20068 `s', `f', and `hdc' (in W32) */
20069
20070 #ifdef HAVE_NTGUI
20071 /* On W32, silently add local `hdc' variable to argument list of
20072 init_glyph_string. */
20073 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
20074 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
20075 #else
20076 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
20077 init_glyph_string (s, char2b, w, row, area, start, hl)
20078 #endif
20079
20080 /* Add a glyph string for a stretch glyph to the list of strings
20081 between HEAD and TAIL. START is the index of the stretch glyph in
20082 row area AREA of glyph row ROW. END is the index of the last glyph
20083 in that glyph row area. X is the current output position assigned
20084 to the new glyph string constructed. HL overrides that face of the
20085 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
20086 is the right-most x-position of the drawing area. */
20087
20088 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
20089 and below -- keep them on one line. */
20090 #define BUILD_STRETCH_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 START = fill_stretch_glyph_string (s, row, area, START, END); \
20096 append_glyph_string (&HEAD, &TAIL, s); \
20097 s->x = (X); \
20098 } \
20099 while (0)
20100
20101
20102 /* Add a glyph string for an image glyph to the list of strings
20103 between HEAD and TAIL. START is the index of the image glyph in
20104 row area AREA of glyph row ROW. END is the index of the last glyph
20105 in that glyph row area. X is the current output position assigned
20106 to the new glyph string constructed. HL overrides that face of the
20107 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
20108 is the right-most x-position of the drawing area. */
20109
20110 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
20111 do \
20112 { \
20113 s = (struct glyph_string *) alloca (sizeof *s); \
20114 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
20115 fill_image_glyph_string (s); \
20116 append_glyph_string (&HEAD, &TAIL, s); \
20117 ++START; \
20118 s->x = (X); \
20119 } \
20120 while (0)
20121
20122
20123 /* Add a glyph string for a sequence of character glyphs to the list
20124 of strings between HEAD and TAIL. START is the index of the first
20125 glyph in row area AREA of glyph row ROW that is part of the new
20126 glyph string. END is the index of the last glyph in that glyph row
20127 area. X is the current output position assigned to the new glyph
20128 string constructed. HL overrides that face of the glyph; e.g. it
20129 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
20130 right-most x-position of the drawing area. */
20131
20132 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
20133 do \
20134 { \
20135 int face_id; \
20136 XChar2b *char2b; \
20137 \
20138 face_id = (row)->glyphs[area][START].face_id; \
20139 \
20140 s = (struct glyph_string *) alloca (sizeof *s); \
20141 char2b = (XChar2b *) alloca ((END - START) * sizeof *char2b); \
20142 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
20143 append_glyph_string (&HEAD, &TAIL, s); \
20144 s->x = (X); \
20145 START = fill_glyph_string (s, face_id, START, END, overlaps); \
20146 } \
20147 while (0)
20148
20149
20150 /* Add a glyph string for a composite sequence to the list of strings
20151 between HEAD and TAIL. START is the index of the first glyph in
20152 row area AREA of glyph row ROW that is part of the new glyph
20153 string. END is the index of the last glyph in that glyph row area.
20154 X is the current output position assigned to the new glyph string
20155 constructed. HL overrides that face of the glyph; e.g. it is
20156 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
20157 x-position of the drawing area. */
20158
20159 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
20160 do { \
20161 int face_id = (row)->glyphs[area][START].face_id; \
20162 struct face *base_face = FACE_FROM_ID (f, face_id); \
20163 int cmp_id = (row)->glyphs[area][START].u.cmp_id; \
20164 struct composition *cmp = composition_table[cmp_id]; \
20165 XChar2b *char2b; \
20166 struct glyph_string *first_s; \
20167 int n; \
20168 \
20169 char2b = (XChar2b *) alloca ((sizeof *char2b) * cmp->glyph_len); \
20170 \
20171 /* Make glyph_strings for each glyph sequence that is drawable by \
20172 the same face, and append them to HEAD/TAIL. */ \
20173 for (n = 0; n < cmp->glyph_len;) \
20174 { \
20175 s = (struct glyph_string *) alloca (sizeof *s); \
20176 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
20177 append_glyph_string (&(HEAD), &(TAIL), s); \
20178 s->cmp = cmp; \
20179 s->gidx = n; \
20180 s->x = (X); \
20181 if (n == 0) \
20182 first_s = s; \
20183 n = fill_composite_glyph_string (s, base_face, overlaps); \
20184 } \
20185 \
20186 ++START; \
20187 s = first_s; \
20188 } while (0)
20189
20190
20191 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
20192 of AREA of glyph row ROW on window W between indices START and END.
20193 HL overrides the face for drawing glyph strings, e.g. it is
20194 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
20195 x-positions of the drawing area.
20196
20197 This is an ugly monster macro construct because we must use alloca
20198 to allocate glyph strings (because draw_glyphs can be called
20199 asynchronously). */
20200
20201 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
20202 do \
20203 { \
20204 HEAD = TAIL = NULL; \
20205 while (START < END) \
20206 { \
20207 struct glyph *first_glyph = (row)->glyphs[area] + START; \
20208 switch (first_glyph->type) \
20209 { \
20210 case CHAR_GLYPH: \
20211 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
20212 HL, X, LAST_X); \
20213 break; \
20214 \
20215 case COMPOSITE_GLYPH: \
20216 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
20217 HL, X, LAST_X); \
20218 break; \
20219 \
20220 case STRETCH_GLYPH: \
20221 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
20222 HL, X, LAST_X); \
20223 break; \
20224 \
20225 case IMAGE_GLYPH: \
20226 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
20227 HL, X, LAST_X); \
20228 break; \
20229 \
20230 default: \
20231 abort (); \
20232 } \
20233 \
20234 if (s) \
20235 { \
20236 set_glyph_string_background_width (s, START, LAST_X); \
20237 (X) += s->width; \
20238 } \
20239 } \
20240 } \
20241 while (0)
20242
20243
20244 /* Draw glyphs between START and END in AREA of ROW on window W,
20245 starting at x-position X. X is relative to AREA in W. HL is a
20246 face-override with the following meaning:
20247
20248 DRAW_NORMAL_TEXT draw normally
20249 DRAW_CURSOR draw in cursor face
20250 DRAW_MOUSE_FACE draw in mouse face.
20251 DRAW_INVERSE_VIDEO draw in mode line face
20252 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
20253 DRAW_IMAGE_RAISED draw an image with a raised relief around it
20254
20255 If OVERLAPS is non-zero, draw only the foreground of characters and
20256 clip to the physical height of ROW. Non-zero value also defines
20257 the overlapping part to be drawn:
20258
20259 OVERLAPS_PRED overlap with preceding rows
20260 OVERLAPS_SUCC overlap with succeeding rows
20261 OVERLAPS_BOTH overlap with both preceding/succeeding rows
20262 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
20263
20264 Value is the x-position reached, relative to AREA of W. */
20265
20266 static int
20267 draw_glyphs (w, x, row, area, start, end, hl, overlaps)
20268 struct window *w;
20269 int x;
20270 struct glyph_row *row;
20271 enum glyph_row_area area;
20272 EMACS_INT start, end;
20273 enum draw_glyphs_face hl;
20274 int overlaps;
20275 {
20276 struct glyph_string *head, *tail;
20277 struct glyph_string *s;
20278 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
20279 int i, j, x_reached, last_x, area_left = 0;
20280 struct frame *f = XFRAME (WINDOW_FRAME (w));
20281 DECLARE_HDC (hdc);
20282
20283 ALLOCATE_HDC (hdc, f);
20284
20285 /* Let's rather be paranoid than getting a SEGV. */
20286 end = min (end, row->used[area]);
20287 start = max (0, start);
20288 start = min (end, start);
20289
20290 /* Translate X to frame coordinates. Set last_x to the right
20291 end of the drawing area. */
20292 if (row->full_width_p)
20293 {
20294 /* X is relative to the left edge of W, without scroll bars
20295 or fringes. */
20296 area_left = WINDOW_LEFT_EDGE_X (w);
20297 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
20298 }
20299 else
20300 {
20301 area_left = window_box_left (w, area);
20302 last_x = area_left + window_box_width (w, area);
20303 }
20304 x += area_left;
20305
20306 /* Build a doubly-linked list of glyph_string structures between
20307 head and tail from what we have to draw. Note that the macro
20308 BUILD_GLYPH_STRINGS will modify its start parameter. That's
20309 the reason we use a separate variable `i'. */
20310 i = start;
20311 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
20312 if (tail)
20313 x_reached = tail->x + tail->background_width;
20314 else
20315 x_reached = x;
20316
20317 /* If there are any glyphs with lbearing < 0 or rbearing > width in
20318 the row, redraw some glyphs in front or following the glyph
20319 strings built above. */
20320 if (head && !overlaps && row->contains_overlapping_glyphs_p)
20321 {
20322 struct glyph_string *h, *t;
20323 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
20324 int mouse_beg_col, mouse_end_col, check_mouse_face = 0;
20325 int dummy_x = 0;
20326
20327 /* If mouse highlighting is on, we may need to draw adjacent
20328 glyphs using mouse-face highlighting. */
20329 if (area == TEXT_AREA && row->mouse_face_p)
20330 {
20331 struct glyph_row *mouse_beg_row, *mouse_end_row;
20332
20333 mouse_beg_row = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_beg_row);
20334 mouse_end_row = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_end_row);
20335
20336 if (row >= mouse_beg_row && row <= mouse_end_row)
20337 {
20338 check_mouse_face = 1;
20339 mouse_beg_col = (row == mouse_beg_row)
20340 ? dpyinfo->mouse_face_beg_col : 0;
20341 mouse_end_col = (row == mouse_end_row)
20342 ? dpyinfo->mouse_face_end_col
20343 : row->used[TEXT_AREA];
20344 }
20345 }
20346
20347 /* Compute overhangs for all glyph strings. */
20348 if (FRAME_RIF (f)->compute_glyph_string_overhangs)
20349 for (s = head; s; s = s->next)
20350 FRAME_RIF (f)->compute_glyph_string_overhangs (s);
20351
20352 /* Prepend glyph strings for glyphs in front of the first glyph
20353 string that are overwritten because of the first glyph
20354 string's left overhang. The background of all strings
20355 prepended must be drawn because the first glyph string
20356 draws over it. */
20357 i = left_overwritten (head);
20358 if (i >= 0)
20359 {
20360 enum draw_glyphs_face overlap_hl;
20361
20362 /* If this row contains mouse highlighting, attempt to draw
20363 the overlapped glyphs with the correct highlight. This
20364 code fails if the overlap encompasses more than one glyph
20365 and mouse-highlight spans only some of these glyphs.
20366 However, making it work perfectly involves a lot more
20367 code, and I don't know if the pathological case occurs in
20368 practice, so we'll stick to this for now. --- cyd */
20369 if (check_mouse_face
20370 && mouse_beg_col < start && mouse_end_col > i)
20371 overlap_hl = DRAW_MOUSE_FACE;
20372 else
20373 overlap_hl = DRAW_NORMAL_TEXT;
20374
20375 j = i;
20376 BUILD_GLYPH_STRINGS (j, start, h, t,
20377 overlap_hl, dummy_x, last_x);
20378 compute_overhangs_and_x (t, head->x, 1);
20379 prepend_glyph_string_lists (&head, &tail, h, t);
20380 clip_head = head;
20381 }
20382
20383 /* Prepend glyph strings for glyphs in front of the first glyph
20384 string that overwrite that glyph string because of their
20385 right overhang. For these strings, only the foreground must
20386 be drawn, because it draws over the glyph string at `head'.
20387 The background must not be drawn because this would overwrite
20388 right overhangs of preceding glyphs for which no glyph
20389 strings exist. */
20390 i = left_overwriting (head);
20391 if (i >= 0)
20392 {
20393 enum draw_glyphs_face overlap_hl;
20394
20395 if (check_mouse_face
20396 && mouse_beg_col < start && mouse_end_col > i)
20397 overlap_hl = DRAW_MOUSE_FACE;
20398 else
20399 overlap_hl = DRAW_NORMAL_TEXT;
20400
20401 clip_head = head;
20402 BUILD_GLYPH_STRINGS (i, start, h, t,
20403 overlap_hl, dummy_x, last_x);
20404 for (s = h; s; s = s->next)
20405 s->background_filled_p = 1;
20406 compute_overhangs_and_x (t, head->x, 1);
20407 prepend_glyph_string_lists (&head, &tail, h, t);
20408 }
20409
20410 /* Append glyphs strings for glyphs following the last glyph
20411 string tail that are overwritten by tail. The background of
20412 these strings has to be drawn because tail's foreground draws
20413 over it. */
20414 i = right_overwritten (tail);
20415 if (i >= 0)
20416 {
20417 enum draw_glyphs_face overlap_hl;
20418
20419 if (check_mouse_face
20420 && mouse_beg_col < i && mouse_end_col > end)
20421 overlap_hl = DRAW_MOUSE_FACE;
20422 else
20423 overlap_hl = DRAW_NORMAL_TEXT;
20424
20425 BUILD_GLYPH_STRINGS (end, i, h, t,
20426 overlap_hl, x, last_x);
20427 compute_overhangs_and_x (h, tail->x + tail->width, 0);
20428 append_glyph_string_lists (&head, &tail, h, t);
20429 clip_tail = tail;
20430 }
20431
20432 /* Append glyph strings for glyphs following the last glyph
20433 string tail that overwrite tail. The foreground of such
20434 glyphs has to be drawn because it writes into the background
20435 of tail. The background must not be drawn because it could
20436 paint over the foreground of following glyphs. */
20437 i = right_overwriting (tail);
20438 if (i >= 0)
20439 {
20440 enum draw_glyphs_face overlap_hl;
20441 if (check_mouse_face
20442 && mouse_beg_col < i && mouse_end_col > end)
20443 overlap_hl = DRAW_MOUSE_FACE;
20444 else
20445 overlap_hl = DRAW_NORMAL_TEXT;
20446
20447 clip_tail = tail;
20448 i++; /* We must include the Ith glyph. */
20449 BUILD_GLYPH_STRINGS (end, i, h, t,
20450 overlap_hl, x, last_x);
20451 for (s = h; s; s = s->next)
20452 s->background_filled_p = 1;
20453 compute_overhangs_and_x (h, tail->x + tail->width, 0);
20454 append_glyph_string_lists (&head, &tail, h, t);
20455 }
20456 if (clip_head || clip_tail)
20457 for (s = head; s; s = s->next)
20458 {
20459 s->clip_head = clip_head;
20460 s->clip_tail = clip_tail;
20461 }
20462 }
20463
20464 /* Draw all strings. */
20465 for (s = head; s; s = s->next)
20466 FRAME_RIF (f)->draw_glyph_string (s);
20467
20468 if (area == TEXT_AREA
20469 && !row->full_width_p
20470 /* When drawing overlapping rows, only the glyph strings'
20471 foreground is drawn, which doesn't erase a cursor
20472 completely. */
20473 && !overlaps)
20474 {
20475 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
20476 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
20477 : (tail ? tail->x + tail->background_width : x));
20478 x0 -= area_left;
20479 x1 -= area_left;
20480
20481 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
20482 row->y, MATRIX_ROW_BOTTOM_Y (row));
20483 }
20484
20485 /* Value is the x-position up to which drawn, relative to AREA of W.
20486 This doesn't include parts drawn because of overhangs. */
20487 if (row->full_width_p)
20488 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
20489 else
20490 x_reached -= area_left;
20491
20492 RELEASE_HDC (hdc, f);
20493
20494 return x_reached;
20495 }
20496
20497 /* Expand row matrix if too narrow. Don't expand if area
20498 is not present. */
20499
20500 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
20501 { \
20502 if (!fonts_changed_p \
20503 && (it->glyph_row->glyphs[area] \
20504 < it->glyph_row->glyphs[area + 1])) \
20505 { \
20506 it->w->ncols_scale_factor++; \
20507 fonts_changed_p = 1; \
20508 } \
20509 }
20510
20511 /* Store one glyph for IT->char_to_display in IT->glyph_row.
20512 Called from x_produce_glyphs when IT->glyph_row is non-null. */
20513
20514 static INLINE void
20515 append_glyph (it)
20516 struct it *it;
20517 {
20518 struct glyph *glyph;
20519 enum glyph_row_area area = it->area;
20520
20521 xassert (it->glyph_row);
20522 xassert (it->char_to_display != '\n' && it->char_to_display != '\t');
20523
20524 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
20525 if (glyph < it->glyph_row->glyphs[area + 1])
20526 {
20527 glyph->charpos = CHARPOS (it->position);
20528 glyph->object = it->object;
20529 if (it->pixel_width > 0)
20530 {
20531 glyph->pixel_width = it->pixel_width;
20532 glyph->padding_p = 0;
20533 }
20534 else
20535 {
20536 /* Assure at least 1-pixel width. Otherwise, cursor can't
20537 be displayed correctly. */
20538 glyph->pixel_width = 1;
20539 glyph->padding_p = 1;
20540 }
20541 glyph->ascent = it->ascent;
20542 glyph->descent = it->descent;
20543 glyph->voffset = it->voffset;
20544 glyph->type = CHAR_GLYPH;
20545 glyph->avoid_cursor_p = it->avoid_cursor_p;
20546 glyph->multibyte_p = it->multibyte_p;
20547 glyph->left_box_line_p = it->start_of_box_run_p;
20548 glyph->right_box_line_p = it->end_of_box_run_p;
20549 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
20550 || it->phys_descent > it->descent);
20551 glyph->glyph_not_available_p = it->glyph_not_available_p;
20552 glyph->face_id = it->face_id;
20553 glyph->u.ch = it->char_to_display;
20554 glyph->slice = null_glyph_slice;
20555 glyph->font_type = FONT_TYPE_UNKNOWN;
20556 ++it->glyph_row->used[area];
20557 }
20558 else
20559 IT_EXPAND_MATRIX_WIDTH (it, area);
20560 }
20561
20562 /* Store one glyph for the composition IT->cmp_id in IT->glyph_row.
20563 Called from x_produce_glyphs when IT->glyph_row is non-null. */
20564
20565 static INLINE void
20566 append_composite_glyph (it)
20567 struct it *it;
20568 {
20569 struct glyph *glyph;
20570 enum glyph_row_area area = it->area;
20571
20572 xassert (it->glyph_row);
20573
20574 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
20575 if (glyph < it->glyph_row->glyphs[area + 1])
20576 {
20577 glyph->charpos = CHARPOS (it->position);
20578 glyph->object = it->object;
20579 glyph->pixel_width = it->pixel_width;
20580 glyph->ascent = it->ascent;
20581 glyph->descent = it->descent;
20582 glyph->voffset = it->voffset;
20583 glyph->type = COMPOSITE_GLYPH;
20584 glyph->avoid_cursor_p = it->avoid_cursor_p;
20585 glyph->multibyte_p = it->multibyte_p;
20586 glyph->left_box_line_p = it->start_of_box_run_p;
20587 glyph->right_box_line_p = it->end_of_box_run_p;
20588 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
20589 || it->phys_descent > it->descent);
20590 glyph->padding_p = 0;
20591 glyph->glyph_not_available_p = 0;
20592 glyph->face_id = it->face_id;
20593 glyph->u.cmp_id = it->cmp_id;
20594 glyph->slice = null_glyph_slice;
20595 glyph->font_type = FONT_TYPE_UNKNOWN;
20596 ++it->glyph_row->used[area];
20597 }
20598 else
20599 IT_EXPAND_MATRIX_WIDTH (it, area);
20600 }
20601
20602
20603 /* Change IT->ascent and IT->height according to the setting of
20604 IT->voffset. */
20605
20606 static INLINE void
20607 take_vertical_position_into_account (it)
20608 struct it *it;
20609 {
20610 if (it->voffset)
20611 {
20612 if (it->voffset < 0)
20613 /* Increase the ascent so that we can display the text higher
20614 in the line. */
20615 it->ascent -= it->voffset;
20616 else
20617 /* Increase the descent so that we can display the text lower
20618 in the line. */
20619 it->descent += it->voffset;
20620 }
20621 }
20622
20623
20624 /* Produce glyphs/get display metrics for the image IT is loaded with.
20625 See the description of struct display_iterator in dispextern.h for
20626 an overview of struct display_iterator. */
20627
20628 static void
20629 produce_image_glyph (it)
20630 struct it *it;
20631 {
20632 struct image *img;
20633 struct face *face;
20634 int glyph_ascent, crop;
20635 struct glyph_slice slice;
20636
20637 xassert (it->what == IT_IMAGE);
20638
20639 face = FACE_FROM_ID (it->f, it->face_id);
20640 xassert (face);
20641 /* Make sure X resources of the face is loaded. */
20642 PREPARE_FACE_FOR_DISPLAY (it->f, face);
20643
20644 if (it->image_id < 0)
20645 {
20646 /* Fringe bitmap. */
20647 it->ascent = it->phys_ascent = 0;
20648 it->descent = it->phys_descent = 0;
20649 it->pixel_width = 0;
20650 it->nglyphs = 0;
20651 return;
20652 }
20653
20654 img = IMAGE_FROM_ID (it->f, it->image_id);
20655 xassert (img);
20656 /* Make sure X resources of the image is loaded. */
20657 prepare_image_for_display (it->f, img);
20658
20659 slice.x = slice.y = 0;
20660 slice.width = img->width;
20661 slice.height = img->height;
20662
20663 if (INTEGERP (it->slice.x))
20664 slice.x = XINT (it->slice.x);
20665 else if (FLOATP (it->slice.x))
20666 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
20667
20668 if (INTEGERP (it->slice.y))
20669 slice.y = XINT (it->slice.y);
20670 else if (FLOATP (it->slice.y))
20671 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
20672
20673 if (INTEGERP (it->slice.width))
20674 slice.width = XINT (it->slice.width);
20675 else if (FLOATP (it->slice.width))
20676 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
20677
20678 if (INTEGERP (it->slice.height))
20679 slice.height = XINT (it->slice.height);
20680 else if (FLOATP (it->slice.height))
20681 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
20682
20683 if (slice.x >= img->width)
20684 slice.x = img->width;
20685 if (slice.y >= img->height)
20686 slice.y = img->height;
20687 if (slice.x + slice.width >= img->width)
20688 slice.width = img->width - slice.x;
20689 if (slice.y + slice.height > img->height)
20690 slice.height = img->height - slice.y;
20691
20692 if (slice.width == 0 || slice.height == 0)
20693 return;
20694
20695 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
20696
20697 it->descent = slice.height - glyph_ascent;
20698 if (slice.y == 0)
20699 it->descent += img->vmargin;
20700 if (slice.y + slice.height == img->height)
20701 it->descent += img->vmargin;
20702 it->phys_descent = it->descent;
20703
20704 it->pixel_width = slice.width;
20705 if (slice.x == 0)
20706 it->pixel_width += img->hmargin;
20707 if (slice.x + slice.width == img->width)
20708 it->pixel_width += img->hmargin;
20709
20710 /* It's quite possible for images to have an ascent greater than
20711 their height, so don't get confused in that case. */
20712 if (it->descent < 0)
20713 it->descent = 0;
20714
20715 #if 0 /* this breaks image tiling */
20716 /* If this glyph is alone on the last line, adjust it.ascent to minimum row ascent. */
20717 int face_ascent = face->font ? FONT_BASE (face->font) : FRAME_BASELINE_OFFSET (it->f);
20718 if (face_ascent > it->ascent)
20719 it->ascent = it->phys_ascent = face_ascent;
20720 #endif
20721
20722 it->nglyphs = 1;
20723
20724 if (face->box != FACE_NO_BOX)
20725 {
20726 if (face->box_line_width > 0)
20727 {
20728 if (slice.y == 0)
20729 it->ascent += face->box_line_width;
20730 if (slice.y + slice.height == img->height)
20731 it->descent += face->box_line_width;
20732 }
20733
20734 if (it->start_of_box_run_p && slice.x == 0)
20735 it->pixel_width += eabs (face->box_line_width);
20736 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
20737 it->pixel_width += eabs (face->box_line_width);
20738 }
20739
20740 take_vertical_position_into_account (it);
20741
20742 /* Automatically crop wide image glyphs at right edge so we can
20743 draw the cursor on same display row. */
20744 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
20745 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
20746 {
20747 it->pixel_width -= crop;
20748 slice.width -= crop;
20749 }
20750
20751 if (it->glyph_row)
20752 {
20753 struct glyph *glyph;
20754 enum glyph_row_area area = it->area;
20755
20756 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
20757 if (glyph < it->glyph_row->glyphs[area + 1])
20758 {
20759 glyph->charpos = CHARPOS (it->position);
20760 glyph->object = it->object;
20761 glyph->pixel_width = it->pixel_width;
20762 glyph->ascent = glyph_ascent;
20763 glyph->descent = it->descent;
20764 glyph->voffset = it->voffset;
20765 glyph->type = IMAGE_GLYPH;
20766 glyph->avoid_cursor_p = it->avoid_cursor_p;
20767 glyph->multibyte_p = it->multibyte_p;
20768 glyph->left_box_line_p = it->start_of_box_run_p;
20769 glyph->right_box_line_p = it->end_of_box_run_p;
20770 glyph->overlaps_vertically_p = 0;
20771 glyph->padding_p = 0;
20772 glyph->glyph_not_available_p = 0;
20773 glyph->face_id = it->face_id;
20774 glyph->u.img_id = img->id;
20775 glyph->slice = slice;
20776 glyph->font_type = FONT_TYPE_UNKNOWN;
20777 ++it->glyph_row->used[area];
20778 }
20779 else
20780 IT_EXPAND_MATRIX_WIDTH (it, area);
20781 }
20782 }
20783
20784
20785 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
20786 of the glyph, WIDTH and HEIGHT are the width and height of the
20787 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
20788
20789 static void
20790 append_stretch_glyph (it, object, width, height, ascent)
20791 struct it *it;
20792 Lisp_Object object;
20793 int width, height;
20794 int ascent;
20795 {
20796 struct glyph *glyph;
20797 enum glyph_row_area area = it->area;
20798
20799 xassert (ascent >= 0 && ascent <= height);
20800
20801 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
20802 if (glyph < it->glyph_row->glyphs[area + 1])
20803 {
20804 glyph->charpos = CHARPOS (it->position);
20805 glyph->object = object;
20806 glyph->pixel_width = width;
20807 glyph->ascent = ascent;
20808 glyph->descent = height - ascent;
20809 glyph->voffset = it->voffset;
20810 glyph->type = STRETCH_GLYPH;
20811 glyph->avoid_cursor_p = it->avoid_cursor_p;
20812 glyph->multibyte_p = it->multibyte_p;
20813 glyph->left_box_line_p = it->start_of_box_run_p;
20814 glyph->right_box_line_p = it->end_of_box_run_p;
20815 glyph->overlaps_vertically_p = 0;
20816 glyph->padding_p = 0;
20817 glyph->glyph_not_available_p = 0;
20818 glyph->face_id = it->face_id;
20819 glyph->u.stretch.ascent = ascent;
20820 glyph->u.stretch.height = height;
20821 glyph->slice = null_glyph_slice;
20822 glyph->font_type = FONT_TYPE_UNKNOWN;
20823 ++it->glyph_row->used[area];
20824 }
20825 else
20826 IT_EXPAND_MATRIX_WIDTH (it, area);
20827 }
20828
20829
20830 /* Produce a stretch glyph for iterator IT. IT->object is the value
20831 of the glyph property displayed. The value must be a list
20832 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
20833 being recognized:
20834
20835 1. `:width WIDTH' specifies that the space should be WIDTH *
20836 canonical char width wide. WIDTH may be an integer or floating
20837 point number.
20838
20839 2. `:relative-width FACTOR' specifies that the width of the stretch
20840 should be computed from the width of the first character having the
20841 `glyph' property, and should be FACTOR times that width.
20842
20843 3. `:align-to HPOS' specifies that the space should be wide enough
20844 to reach HPOS, a value in canonical character units.
20845
20846 Exactly one of the above pairs must be present.
20847
20848 4. `:height HEIGHT' specifies that the height of the stretch produced
20849 should be HEIGHT, measured in canonical character units.
20850
20851 5. `:relative-height FACTOR' specifies that the height of the
20852 stretch should be FACTOR times the height of the characters having
20853 the glyph property.
20854
20855 Either none or exactly one of 4 or 5 must be present.
20856
20857 6. `:ascent ASCENT' specifies that ASCENT percent of the height
20858 of the stretch should be used for the ascent of the stretch.
20859 ASCENT must be in the range 0 <= ASCENT <= 100. */
20860
20861 static void
20862 produce_stretch_glyph (it)
20863 struct it *it;
20864 {
20865 /* (space :width WIDTH :height HEIGHT ...) */
20866 Lisp_Object prop, plist;
20867 int width = 0, height = 0, align_to = -1;
20868 int zero_width_ok_p = 0, zero_height_ok_p = 0;
20869 int ascent = 0;
20870 double tem;
20871 struct face *face = FACE_FROM_ID (it->f, it->face_id);
20872 struct font *font = face->font ? face->font : FRAME_FONT (it->f);
20873
20874 PREPARE_FACE_FOR_DISPLAY (it->f, face);
20875
20876 /* List should start with `space'. */
20877 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
20878 plist = XCDR (it->object);
20879
20880 /* Compute the width of the stretch. */
20881 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
20882 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
20883 {
20884 /* Absolute width `:width WIDTH' specified and valid. */
20885 zero_width_ok_p = 1;
20886 width = (int)tem;
20887 }
20888 else if (prop = Fplist_get (plist, QCrelative_width),
20889 NUMVAL (prop) > 0)
20890 {
20891 /* Relative width `:relative-width FACTOR' specified and valid.
20892 Compute the width of the characters having the `glyph'
20893 property. */
20894 struct it it2;
20895 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
20896
20897 it2 = *it;
20898 if (it->multibyte_p)
20899 {
20900 int maxlen = ((IT_BYTEPOS (*it) >= GPT ? ZV : GPT)
20901 - IT_BYTEPOS (*it));
20902 it2.c = STRING_CHAR_AND_LENGTH (p, maxlen, it2.len);
20903 }
20904 else
20905 it2.c = *p, it2.len = 1;
20906
20907 it2.glyph_row = NULL;
20908 it2.what = IT_CHARACTER;
20909 x_produce_glyphs (&it2);
20910 width = NUMVAL (prop) * it2.pixel_width;
20911 }
20912 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
20913 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
20914 {
20915 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
20916 align_to = (align_to < 0
20917 ? 0
20918 : align_to - window_box_left_offset (it->w, TEXT_AREA));
20919 else if (align_to < 0)
20920 align_to = window_box_left_offset (it->w, TEXT_AREA);
20921 width = max (0, (int)tem + align_to - it->current_x);
20922 zero_width_ok_p = 1;
20923 }
20924 else
20925 /* Nothing specified -> width defaults to canonical char width. */
20926 width = FRAME_COLUMN_WIDTH (it->f);
20927
20928 if (width <= 0 && (width < 0 || !zero_width_ok_p))
20929 width = 1;
20930
20931 /* Compute height. */
20932 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
20933 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
20934 {
20935 height = (int)tem;
20936 zero_height_ok_p = 1;
20937 }
20938 else if (prop = Fplist_get (plist, QCrelative_height),
20939 NUMVAL (prop) > 0)
20940 height = FONT_HEIGHT (font) * NUMVAL (prop);
20941 else
20942 height = FONT_HEIGHT (font);
20943
20944 if (height <= 0 && (height < 0 || !zero_height_ok_p))
20945 height = 1;
20946
20947 /* Compute percentage of height used for ascent. If
20948 `:ascent ASCENT' is present and valid, use that. Otherwise,
20949 derive the ascent from the font in use. */
20950 if (prop = Fplist_get (plist, QCascent),
20951 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
20952 ascent = height * NUMVAL (prop) / 100.0;
20953 else if (!NILP (prop)
20954 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
20955 ascent = min (max (0, (int)tem), height);
20956 else
20957 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
20958
20959 if (width > 0 && it->line_wrap != TRUNCATE
20960 && it->current_x + width > it->last_visible_x)
20961 width = it->last_visible_x - it->current_x - 1;
20962
20963 if (width > 0 && height > 0 && it->glyph_row)
20964 {
20965 Lisp_Object object = it->stack[it->sp - 1].string;
20966 if (!STRINGP (object))
20967 object = it->w->buffer;
20968 append_stretch_glyph (it, object, width, height, ascent);
20969 }
20970
20971 it->pixel_width = width;
20972 it->ascent = it->phys_ascent = ascent;
20973 it->descent = it->phys_descent = height - it->ascent;
20974 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
20975
20976 take_vertical_position_into_account (it);
20977 }
20978
20979 /* Calculate line-height and line-spacing properties.
20980 An integer value specifies explicit pixel value.
20981 A float value specifies relative value to current face height.
20982 A cons (float . face-name) specifies relative value to
20983 height of specified face font.
20984
20985 Returns height in pixels, or nil. */
20986
20987
20988 static Lisp_Object
20989 calc_line_height_property (it, val, font, boff, override)
20990 struct it *it;
20991 Lisp_Object val;
20992 struct font *font;
20993 int boff, override;
20994 {
20995 Lisp_Object face_name = Qnil;
20996 int ascent, descent, height;
20997
20998 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
20999 return val;
21000
21001 if (CONSP (val))
21002 {
21003 face_name = XCAR (val);
21004 val = XCDR (val);
21005 if (!NUMBERP (val))
21006 val = make_number (1);
21007 if (NILP (face_name))
21008 {
21009 height = it->ascent + it->descent;
21010 goto scale;
21011 }
21012 }
21013
21014 if (NILP (face_name))
21015 {
21016 font = FRAME_FONT (it->f);
21017 boff = FRAME_BASELINE_OFFSET (it->f);
21018 }
21019 else if (EQ (face_name, Qt))
21020 {
21021 override = 0;
21022 }
21023 else
21024 {
21025 int face_id;
21026 struct face *face;
21027
21028 face_id = lookup_named_face (it->f, face_name, 0);
21029 if (face_id < 0)
21030 return make_number (-1);
21031
21032 face = FACE_FROM_ID (it->f, face_id);
21033 font = face->font;
21034 if (font == NULL)
21035 return make_number (-1);
21036 boff = font->baseline_offset;
21037 if (font->vertical_centering)
21038 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
21039 }
21040
21041 ascent = FONT_BASE (font) + boff;
21042 descent = FONT_DESCENT (font) - boff;
21043
21044 if (override)
21045 {
21046 it->override_ascent = ascent;
21047 it->override_descent = descent;
21048 it->override_boff = boff;
21049 }
21050
21051 height = ascent + descent;
21052
21053 scale:
21054 if (FLOATP (val))
21055 height = (int)(XFLOAT_DATA (val) * height);
21056 else if (INTEGERP (val))
21057 height *= XINT (val);
21058
21059 return make_number (height);
21060 }
21061
21062
21063 /* RIF:
21064 Produce glyphs/get display metrics for the display element IT is
21065 loaded with. See the description of struct it in dispextern.h
21066 for an overview of struct it. */
21067
21068 void
21069 x_produce_glyphs (it)
21070 struct it *it;
21071 {
21072 int extra_line_spacing = it->extra_line_spacing;
21073
21074 it->glyph_not_available_p = 0;
21075
21076 if (it->what == IT_CHARACTER)
21077 {
21078 XChar2b char2b;
21079 struct font *font;
21080 struct face *face = FACE_FROM_ID (it->f, it->face_id);
21081 struct font_metrics *pcm;
21082 int font_not_found_p;
21083 int boff; /* baseline offset */
21084 /* We may change it->multibyte_p upon unibyte<->multibyte
21085 conversion. So, save the current value now and restore it
21086 later.
21087
21088 Note: It seems that we don't have to record multibyte_p in
21089 struct glyph because the character code itself tells if or
21090 not the character is multibyte. Thus, in the future, we must
21091 consider eliminating the field `multibyte_p' in the struct
21092 glyph. */
21093 int saved_multibyte_p = it->multibyte_p;
21094
21095 /* Maybe translate single-byte characters to multibyte, or the
21096 other way. */
21097 it->char_to_display = it->c;
21098 if (!ASCII_BYTE_P (it->c)
21099 && ! it->multibyte_p)
21100 {
21101 if (SINGLE_BYTE_CHAR_P (it->c)
21102 && unibyte_display_via_language_environment)
21103 it->char_to_display = unibyte_char_to_multibyte (it->c);
21104 if (! SINGLE_BYTE_CHAR_P (it->char_to_display))
21105 {
21106 it->multibyte_p = 1;
21107 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display,
21108 -1, Qnil);
21109 face = FACE_FROM_ID (it->f, it->face_id);
21110 }
21111 }
21112
21113 /* Get font to use. Encode IT->char_to_display. */
21114 get_char_face_and_encoding (it->f, it->char_to_display, it->face_id,
21115 &char2b, it->multibyte_p, 0);
21116 font = face->font;
21117
21118 /* When no suitable font found, use the default font. */
21119 font_not_found_p = font == NULL;
21120 if (font_not_found_p)
21121 {
21122 font = FRAME_FONT (it->f);
21123 boff = FRAME_BASELINE_OFFSET (it->f);
21124 }
21125 else
21126 {
21127 boff = font->baseline_offset;
21128 if (font->vertical_centering)
21129 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
21130 }
21131
21132 if (it->char_to_display >= ' '
21133 && (!it->multibyte_p || it->char_to_display < 128))
21134 {
21135 /* Either unibyte or ASCII. */
21136 int stretched_p;
21137
21138 it->nglyphs = 1;
21139
21140 pcm = get_per_char_metric (it->f, font, &char2b);
21141
21142 if (it->override_ascent >= 0)
21143 {
21144 it->ascent = it->override_ascent;
21145 it->descent = it->override_descent;
21146 boff = it->override_boff;
21147 }
21148 else
21149 {
21150 it->ascent = FONT_BASE (font) + boff;
21151 it->descent = FONT_DESCENT (font) - boff;
21152 }
21153
21154 if (pcm)
21155 {
21156 it->phys_ascent = pcm->ascent + boff;
21157 it->phys_descent = pcm->descent - boff;
21158 it->pixel_width = pcm->width;
21159 }
21160 else
21161 {
21162 it->glyph_not_available_p = 1;
21163 it->phys_ascent = it->ascent;
21164 it->phys_descent = it->descent;
21165 it->pixel_width = FONT_WIDTH (font);
21166 }
21167
21168 if (it->constrain_row_ascent_descent_p)
21169 {
21170 if (it->descent > it->max_descent)
21171 {
21172 it->ascent += it->descent - it->max_descent;
21173 it->descent = it->max_descent;
21174 }
21175 if (it->ascent > it->max_ascent)
21176 {
21177 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
21178 it->ascent = it->max_ascent;
21179 }
21180 it->phys_ascent = min (it->phys_ascent, it->ascent);
21181 it->phys_descent = min (it->phys_descent, it->descent);
21182 extra_line_spacing = 0;
21183 }
21184
21185 /* If this is a space inside a region of text with
21186 `space-width' property, change its width. */
21187 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
21188 if (stretched_p)
21189 it->pixel_width *= XFLOATINT (it->space_width);
21190
21191 /* If face has a box, add the box thickness to the character
21192 height. If character has a box line to the left and/or
21193 right, add the box line width to the character's width. */
21194 if (face->box != FACE_NO_BOX)
21195 {
21196 int thick = face->box_line_width;
21197
21198 if (thick > 0)
21199 {
21200 it->ascent += thick;
21201 it->descent += thick;
21202 }
21203 else
21204 thick = -thick;
21205
21206 if (it->start_of_box_run_p)
21207 it->pixel_width += thick;
21208 if (it->end_of_box_run_p)
21209 it->pixel_width += thick;
21210 }
21211
21212 /* If face has an overline, add the height of the overline
21213 (1 pixel) and a 1 pixel margin to the character height. */
21214 if (face->overline_p)
21215 it->ascent += overline_margin;
21216
21217 if (it->constrain_row_ascent_descent_p)
21218 {
21219 if (it->ascent > it->max_ascent)
21220 it->ascent = it->max_ascent;
21221 if (it->descent > it->max_descent)
21222 it->descent = it->max_descent;
21223 }
21224
21225 take_vertical_position_into_account (it);
21226
21227 /* If we have to actually produce glyphs, do it. */
21228 if (it->glyph_row)
21229 {
21230 if (stretched_p)
21231 {
21232 /* Translate a space with a `space-width' property
21233 into a stretch glyph. */
21234 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
21235 / FONT_HEIGHT (font));
21236 append_stretch_glyph (it, it->object, it->pixel_width,
21237 it->ascent + it->descent, ascent);
21238 }
21239 else
21240 append_glyph (it);
21241
21242 /* If characters with lbearing or rbearing are displayed
21243 in this line, record that fact in a flag of the
21244 glyph row. This is used to optimize X output code. */
21245 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
21246 it->glyph_row->contains_overlapping_glyphs_p = 1;
21247 }
21248 if (! stretched_p && it->pixel_width == 0)
21249 /* We assure that all visible glyphs have at least 1-pixel
21250 width. */
21251 it->pixel_width = 1;
21252 }
21253 else if (it->char_to_display == '\n')
21254 {
21255 /* A newline has no width but we need the height of the line.
21256 But if previous part of the line set a height, don't
21257 increase that height */
21258
21259 Lisp_Object height;
21260 Lisp_Object total_height = Qnil;
21261
21262 it->override_ascent = -1;
21263 it->pixel_width = 0;
21264 it->nglyphs = 0;
21265
21266 height = get_it_property(it, Qline_height);
21267 /* Split (line-height total-height) list */
21268 if (CONSP (height)
21269 && CONSP (XCDR (height))
21270 && NILP (XCDR (XCDR (height))))
21271 {
21272 total_height = XCAR (XCDR (height));
21273 height = XCAR (height);
21274 }
21275 height = calc_line_height_property(it, height, font, boff, 1);
21276
21277 if (it->override_ascent >= 0)
21278 {
21279 it->ascent = it->override_ascent;
21280 it->descent = it->override_descent;
21281 boff = it->override_boff;
21282 }
21283 else
21284 {
21285 it->ascent = FONT_BASE (font) + boff;
21286 it->descent = FONT_DESCENT (font) - boff;
21287 }
21288
21289 if (EQ (height, Qt))
21290 {
21291 if (it->descent > it->max_descent)
21292 {
21293 it->ascent += it->descent - it->max_descent;
21294 it->descent = it->max_descent;
21295 }
21296 if (it->ascent > it->max_ascent)
21297 {
21298 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
21299 it->ascent = it->max_ascent;
21300 }
21301 it->phys_ascent = min (it->phys_ascent, it->ascent);
21302 it->phys_descent = min (it->phys_descent, it->descent);
21303 it->constrain_row_ascent_descent_p = 1;
21304 extra_line_spacing = 0;
21305 }
21306 else
21307 {
21308 Lisp_Object spacing;
21309
21310 it->phys_ascent = it->ascent;
21311 it->phys_descent = it->descent;
21312
21313 if ((it->max_ascent > 0 || it->max_descent > 0)
21314 && face->box != FACE_NO_BOX
21315 && face->box_line_width > 0)
21316 {
21317 it->ascent += face->box_line_width;
21318 it->descent += face->box_line_width;
21319 }
21320 if (!NILP (height)
21321 && XINT (height) > it->ascent + it->descent)
21322 it->ascent = XINT (height) - it->descent;
21323
21324 if (!NILP (total_height))
21325 spacing = calc_line_height_property(it, total_height, font, boff, 0);
21326 else
21327 {
21328 spacing = get_it_property(it, Qline_spacing);
21329 spacing = calc_line_height_property(it, spacing, font, boff, 0);
21330 }
21331 if (INTEGERP (spacing))
21332 {
21333 extra_line_spacing = XINT (spacing);
21334 if (!NILP (total_height))
21335 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
21336 }
21337 }
21338 }
21339 else if (it->char_to_display == '\t')
21340 {
21341 if (font->space_width > 0)
21342 {
21343 int tab_width = it->tab_width * font->space_width;
21344 int x = it->current_x + it->continuation_lines_width;
21345 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
21346
21347 /* If the distance from the current position to the next tab
21348 stop is less than a space character width, use the
21349 tab stop after that. */
21350 if (next_tab_x - x < font->space_width)
21351 next_tab_x += tab_width;
21352
21353 it->pixel_width = next_tab_x - x;
21354 it->nglyphs = 1;
21355 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
21356 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
21357
21358 if (it->glyph_row)
21359 {
21360 append_stretch_glyph (it, it->object, it->pixel_width,
21361 it->ascent + it->descent, it->ascent);
21362 }
21363 }
21364 else
21365 {
21366 it->pixel_width = 0;
21367 it->nglyphs = 1;
21368 }
21369 }
21370 else
21371 {
21372 /* A multi-byte character. Assume that the display width of the
21373 character is the width of the character multiplied by the
21374 width of the font. */
21375
21376 /* If we found a font, this font should give us the right
21377 metrics. If we didn't find a font, use the frame's
21378 default font and calculate the width of the character by
21379 multiplying the width of font by the width of the
21380 character. */
21381
21382 pcm = get_per_char_metric (it->f, font, &char2b);
21383
21384 if (font_not_found_p || !pcm)
21385 {
21386 int char_width = CHAR_WIDTH (it->char_to_display);
21387
21388 if (char_width == 0)
21389 /* This is a non spacing character. But, as we are
21390 going to display an empty box, the box must occupy
21391 at least one column. */
21392 char_width = 1;
21393 it->glyph_not_available_p = 1;
21394 it->pixel_width = FRAME_COLUMN_WIDTH (it->f) * char_width;
21395 it->phys_ascent = FONT_BASE (font) + boff;
21396 it->phys_descent = FONT_DESCENT (font) - boff;
21397 }
21398 else
21399 {
21400 it->pixel_width = pcm->width;
21401 it->phys_ascent = pcm->ascent + boff;
21402 it->phys_descent = pcm->descent - boff;
21403 if (it->glyph_row
21404 && (pcm->lbearing < 0
21405 || pcm->rbearing > pcm->width))
21406 it->glyph_row->contains_overlapping_glyphs_p = 1;
21407 }
21408 it->nglyphs = 1;
21409 it->ascent = FONT_BASE (font) + boff;
21410 it->descent = FONT_DESCENT (font) - boff;
21411 if (face->box != FACE_NO_BOX)
21412 {
21413 int thick = face->box_line_width;
21414
21415 if (thick > 0)
21416 {
21417 it->ascent += thick;
21418 it->descent += thick;
21419 }
21420 else
21421 thick = - thick;
21422
21423 if (it->start_of_box_run_p)
21424 it->pixel_width += thick;
21425 if (it->end_of_box_run_p)
21426 it->pixel_width += thick;
21427 }
21428
21429 /* If face has an overline, add the height of the overline
21430 (1 pixel) and a 1 pixel margin to the character height. */
21431 if (face->overline_p)
21432 it->ascent += overline_margin;
21433
21434 take_vertical_position_into_account (it);
21435
21436 if (it->ascent < 0)
21437 it->ascent = 0;
21438 if (it->descent < 0)
21439 it->descent = 0;
21440
21441 if (it->glyph_row)
21442 append_glyph (it);
21443 if (it->pixel_width == 0)
21444 /* We assure that all visible glyphs have at least 1-pixel
21445 width. */
21446 it->pixel_width = 1;
21447 }
21448 it->multibyte_p = saved_multibyte_p;
21449 }
21450 else if (it->what == IT_COMPOSITION)
21451 {
21452 /* Note: A composition is represented as one glyph in the
21453 glyph matrix. There are no padding glyphs.
21454
21455 Important is that pixel_width, ascent, and descent are the
21456 values of what is drawn by draw_glyphs (i.e. the values of
21457 the overall glyphs composed). */
21458 struct face *face = FACE_FROM_ID (it->f, it->face_id);
21459 int boff; /* baseline offset */
21460 struct composition *cmp = composition_table[it->cmp_id];
21461 int glyph_len = cmp->glyph_len;
21462 struct font *font = face->font;
21463
21464 it->nglyphs = 1;
21465
21466 if (cmp->method == COMPOSITION_WITH_GLYPH_STRING)
21467 {
21468 PREPARE_FACE_FOR_DISPLAY (it->f, face);
21469 font_prepare_composition (cmp, it->f);
21470 }
21471 else
21472 /* If we have not yet calculated pixel size data of glyphs of
21473 the composition for the current face font, calculate them
21474 now. Theoretically, we have to check all fonts for the
21475 glyphs, but that requires much time and memory space. So,
21476 here we check only the font of the first glyph. This leads
21477 to incorrect display, but it's very rare, and C-l (recenter)
21478 can correct the display anyway. */
21479 if (! cmp->font || cmp->font != font)
21480 {
21481 /* Ascent and descent of the font of the first character
21482 of this composition (adjusted by baseline offset).
21483 Ascent and descent of overall glyphs should not be less
21484 than them respectively. */
21485 int font_ascent, font_descent, font_height;
21486 /* Bounding box of the overall glyphs. */
21487 int leftmost, rightmost, lowest, highest;
21488 int lbearing, rbearing;
21489 int i, width, ascent, descent;
21490 int left_padded = 0, right_padded = 0;
21491 int face_id;
21492 int c;
21493 XChar2b char2b;
21494 struct font_metrics *pcm;
21495 int font_not_found_p;
21496 int pos;
21497
21498 for (glyph_len = cmp->glyph_len; glyph_len > 0; glyph_len--)
21499 if ((c = COMPOSITION_GLYPH (cmp, glyph_len - 1)) != '\t')
21500 break;
21501 if (glyph_len < cmp->glyph_len)
21502 right_padded = 1;
21503 for (i = 0; i < glyph_len; i++)
21504 {
21505 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
21506 break;
21507 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
21508 }
21509 if (i > 0)
21510 left_padded = 1;
21511
21512 pos = (STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
21513 : IT_CHARPOS (*it));
21514 /* When no suitable font found, use the default font. */
21515 font_not_found_p = font == NULL;
21516 if (font_not_found_p)
21517 {
21518 face = face->ascii_face;
21519 font = face->font;
21520 }
21521 boff = font->baseline_offset;
21522 if (font->vertical_centering)
21523 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
21524 font_ascent = FONT_BASE (font) + boff;
21525 font_descent = FONT_DESCENT (font) - boff;
21526 font_height = FONT_HEIGHT (font);
21527
21528 cmp->font = (void *) font;
21529
21530 pcm = NULL;
21531 if (! font_not_found_p)
21532 {
21533 get_char_face_and_encoding (it->f, c, it->face_id,
21534 &char2b, it->multibyte_p, 0);
21535 pcm = get_per_char_metric (it->f, font, &char2b);
21536 }
21537
21538 /* Initialize the bounding box. */
21539 if (pcm)
21540 {
21541 width = pcm->width;
21542 ascent = pcm->ascent;
21543 descent = pcm->descent;
21544 lbearing = pcm->lbearing;
21545 rbearing = pcm->rbearing;
21546 }
21547 else
21548 {
21549 width = FONT_WIDTH (font);
21550 ascent = FONT_BASE (font);
21551 descent = FONT_DESCENT (font);
21552 lbearing = 0;
21553 rbearing = width;
21554 }
21555
21556 rightmost = width;
21557 leftmost = 0;
21558 lowest = - descent + boff;
21559 highest = ascent + boff;
21560
21561 if (! font_not_found_p
21562 && font->default_ascent
21563 && CHAR_TABLE_P (Vuse_default_ascent)
21564 && !NILP (Faref (Vuse_default_ascent,
21565 make_number (it->char_to_display))))
21566 highest = font->default_ascent + boff;
21567
21568 /* Draw the first glyph at the normal position. It may be
21569 shifted to right later if some other glyphs are drawn
21570 at the left. */
21571 cmp->offsets[i * 2] = 0;
21572 cmp->offsets[i * 2 + 1] = boff;
21573 cmp->lbearing = lbearing;
21574 cmp->rbearing = rbearing;
21575
21576 /* Set cmp->offsets for the remaining glyphs. */
21577 for (i++; i < glyph_len; i++)
21578 {
21579 int left, right, btm, top;
21580 int ch = COMPOSITION_GLYPH (cmp, i);
21581 int face_id;
21582 struct face *this_face;
21583 int this_boff;
21584
21585 if (ch == '\t')
21586 ch = ' ';
21587 face_id = FACE_FOR_CHAR (it->f, face, ch, pos, it->string);
21588 this_face = FACE_FROM_ID (it->f, face_id);
21589 font = this_face->font;
21590
21591 if (font == NULL)
21592 pcm = NULL;
21593 else
21594 {
21595 this_boff = font->baseline_offset;
21596 if (font->vertical_centering)
21597 this_boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
21598 get_char_face_and_encoding (it->f, ch, face_id,
21599 &char2b, it->multibyte_p, 0);
21600 pcm = get_per_char_metric (it->f, font, &char2b);
21601 }
21602 if (! pcm)
21603 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
21604 else
21605 {
21606 width = pcm->width;
21607 ascent = pcm->ascent;
21608 descent = pcm->descent;
21609 lbearing = pcm->lbearing;
21610 rbearing = pcm->rbearing;
21611 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
21612 {
21613 /* Relative composition with or without
21614 alternate chars. */
21615 left = (leftmost + rightmost - width) / 2;
21616 btm = - descent + boff;
21617 if (font->relative_compose
21618 && (! CHAR_TABLE_P (Vignore_relative_composition)
21619 || NILP (Faref (Vignore_relative_composition,
21620 make_number (ch)))))
21621 {
21622
21623 if (- descent >= font->relative_compose)
21624 /* One extra pixel between two glyphs. */
21625 btm = highest + 1;
21626 else if (ascent <= 0)
21627 /* One extra pixel between two glyphs. */
21628 btm = lowest - 1 - ascent - descent;
21629 }
21630 }
21631 else
21632 {
21633 /* A composition rule is specified by an integer
21634 value that encodes global and new reference
21635 points (GREF and NREF). GREF and NREF are
21636 specified by numbers as below:
21637
21638 0---1---2 -- ascent
21639 | |
21640 | |
21641 | |
21642 9--10--11 -- center
21643 | |
21644 ---3---4---5--- baseline
21645 | |
21646 6---7---8 -- descent
21647 */
21648 int rule = COMPOSITION_RULE (cmp, i);
21649 int gref, nref, grefx, grefy, nrefx, nrefy, xoff, yoff;
21650
21651 COMPOSITION_DECODE_RULE (rule, gref, nref, xoff, yoff);
21652 grefx = gref % 3, nrefx = nref % 3;
21653 grefy = gref / 3, nrefy = nref / 3;
21654 if (xoff)
21655 xoff = font_height * (xoff - 128) / 256;
21656 if (yoff)
21657 yoff = font_height * (yoff - 128) / 256;
21658
21659 left = (leftmost
21660 + grefx * (rightmost - leftmost) / 2
21661 - nrefx * width / 2
21662 + xoff);
21663
21664 btm = ((grefy == 0 ? highest
21665 : grefy == 1 ? 0
21666 : grefy == 2 ? lowest
21667 : (highest + lowest) / 2)
21668 - (nrefy == 0 ? ascent + descent
21669 : nrefy == 1 ? descent - boff
21670 : nrefy == 2 ? 0
21671 : (ascent + descent) / 2)
21672 + yoff);
21673 }
21674
21675 cmp->offsets[i * 2] = left;
21676 cmp->offsets[i * 2 + 1] = btm + descent;
21677
21678 /* Update the bounding box of the overall glyphs. */
21679 if (width > 0)
21680 {
21681 right = left + width;
21682 if (left < leftmost)
21683 leftmost = left;
21684 if (right > rightmost)
21685 rightmost = right;
21686 }
21687 top = btm + descent + ascent;
21688 if (top > highest)
21689 highest = top;
21690 if (btm < lowest)
21691 lowest = btm;
21692
21693 if (cmp->lbearing > left + lbearing)
21694 cmp->lbearing = left + lbearing;
21695 if (cmp->rbearing < left + rbearing)
21696 cmp->rbearing = left + rbearing;
21697 }
21698 }
21699
21700 /* If there are glyphs whose x-offsets are negative,
21701 shift all glyphs to the right and make all x-offsets
21702 non-negative. */
21703 if (leftmost < 0)
21704 {
21705 for (i = 0; i < cmp->glyph_len; i++)
21706 cmp->offsets[i * 2] -= leftmost;
21707 rightmost -= leftmost;
21708 cmp->lbearing -= leftmost;
21709 cmp->rbearing -= leftmost;
21710 }
21711
21712 if (left_padded && cmp->lbearing < 0)
21713 {
21714 for (i = 0; i < cmp->glyph_len; i++)
21715 cmp->offsets[i * 2] -= cmp->lbearing;
21716 rightmost -= cmp->lbearing;
21717 cmp->rbearing -= cmp->lbearing;
21718 cmp->lbearing = 0;
21719 }
21720 if (right_padded && rightmost < cmp->rbearing)
21721 {
21722 rightmost = cmp->rbearing;
21723 }
21724
21725 cmp->pixel_width = rightmost;
21726 cmp->ascent = highest;
21727 cmp->descent = - lowest;
21728 if (cmp->ascent < font_ascent)
21729 cmp->ascent = font_ascent;
21730 if (cmp->descent < font_descent)
21731 cmp->descent = font_descent;
21732 }
21733
21734 if (it->glyph_row
21735 && (cmp->lbearing < 0
21736 || cmp->rbearing > cmp->pixel_width))
21737 it->glyph_row->contains_overlapping_glyphs_p = 1;
21738
21739 it->pixel_width = cmp->pixel_width;
21740 it->ascent = it->phys_ascent = cmp->ascent;
21741 it->descent = it->phys_descent = cmp->descent;
21742 if (face->box != FACE_NO_BOX)
21743 {
21744 int thick = face->box_line_width;
21745
21746 if (thick > 0)
21747 {
21748 it->ascent += thick;
21749 it->descent += thick;
21750 }
21751 else
21752 thick = - thick;
21753
21754 if (it->start_of_box_run_p)
21755 it->pixel_width += thick;
21756 if (it->end_of_box_run_p)
21757 it->pixel_width += thick;
21758 }
21759
21760 /* If face has an overline, add the height of the overline
21761 (1 pixel) and a 1 pixel margin to the character height. */
21762 if (face->overline_p)
21763 it->ascent += overline_margin;
21764
21765 take_vertical_position_into_account (it);
21766 if (it->ascent < 0)
21767 it->ascent = 0;
21768 if (it->descent < 0)
21769 it->descent = 0;
21770
21771 if (it->glyph_row)
21772 append_composite_glyph (it);
21773 }
21774 else if (it->what == IT_IMAGE)
21775 produce_image_glyph (it);
21776 else if (it->what == IT_STRETCH)
21777 produce_stretch_glyph (it);
21778
21779 /* Accumulate dimensions. Note: can't assume that it->descent > 0
21780 because this isn't true for images with `:ascent 100'. */
21781 xassert (it->ascent >= 0 && it->descent >= 0);
21782 if (it->area == TEXT_AREA)
21783 it->current_x += it->pixel_width;
21784
21785 if (extra_line_spacing > 0)
21786 {
21787 it->descent += extra_line_spacing;
21788 if (extra_line_spacing > it->max_extra_line_spacing)
21789 it->max_extra_line_spacing = extra_line_spacing;
21790 }
21791
21792 it->max_ascent = max (it->max_ascent, it->ascent);
21793 it->max_descent = max (it->max_descent, it->descent);
21794 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
21795 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
21796 }
21797
21798 /* EXPORT for RIF:
21799 Output LEN glyphs starting at START at the nominal cursor position.
21800 Advance the nominal cursor over the text. The global variable
21801 updated_window contains the window being updated, updated_row is
21802 the glyph row being updated, and updated_area is the area of that
21803 row being updated. */
21804
21805 void
21806 x_write_glyphs (start, len)
21807 struct glyph *start;
21808 int len;
21809 {
21810 int x, hpos;
21811
21812 xassert (updated_window && updated_row);
21813 BLOCK_INPUT;
21814
21815 /* Write glyphs. */
21816
21817 hpos = start - updated_row->glyphs[updated_area];
21818 x = draw_glyphs (updated_window, output_cursor.x,
21819 updated_row, updated_area,
21820 hpos, hpos + len,
21821 DRAW_NORMAL_TEXT, 0);
21822
21823 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
21824 if (updated_area == TEXT_AREA
21825 && updated_window->phys_cursor_on_p
21826 && updated_window->phys_cursor.vpos == output_cursor.vpos
21827 && updated_window->phys_cursor.hpos >= hpos
21828 && updated_window->phys_cursor.hpos < hpos + len)
21829 updated_window->phys_cursor_on_p = 0;
21830
21831 UNBLOCK_INPUT;
21832
21833 /* Advance the output cursor. */
21834 output_cursor.hpos += len;
21835 output_cursor.x = x;
21836 }
21837
21838
21839 /* EXPORT for RIF:
21840 Insert LEN glyphs from START at the nominal cursor position. */
21841
21842 void
21843 x_insert_glyphs (start, len)
21844 struct glyph *start;
21845 int len;
21846 {
21847 struct frame *f;
21848 struct window *w;
21849 int line_height, shift_by_width, shifted_region_width;
21850 struct glyph_row *row;
21851 struct glyph *glyph;
21852 int frame_x, frame_y;
21853 EMACS_INT hpos;
21854
21855 xassert (updated_window && updated_row);
21856 BLOCK_INPUT;
21857 w = updated_window;
21858 f = XFRAME (WINDOW_FRAME (w));
21859
21860 /* Get the height of the line we are in. */
21861 row = updated_row;
21862 line_height = row->height;
21863
21864 /* Get the width of the glyphs to insert. */
21865 shift_by_width = 0;
21866 for (glyph = start; glyph < start + len; ++glyph)
21867 shift_by_width += glyph->pixel_width;
21868
21869 /* Get the width of the region to shift right. */
21870 shifted_region_width = (window_box_width (w, updated_area)
21871 - output_cursor.x
21872 - shift_by_width);
21873
21874 /* Shift right. */
21875 frame_x = window_box_left (w, updated_area) + output_cursor.x;
21876 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
21877
21878 FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
21879 line_height, shift_by_width);
21880
21881 /* Write the glyphs. */
21882 hpos = start - row->glyphs[updated_area];
21883 draw_glyphs (w, output_cursor.x, row, updated_area,
21884 hpos, hpos + len,
21885 DRAW_NORMAL_TEXT, 0);
21886
21887 /* Advance the output cursor. */
21888 output_cursor.hpos += len;
21889 output_cursor.x += shift_by_width;
21890 UNBLOCK_INPUT;
21891 }
21892
21893
21894 /* EXPORT for RIF:
21895 Erase the current text line from the nominal cursor position
21896 (inclusive) to pixel column TO_X (exclusive). The idea is that
21897 everything from TO_X onward is already erased.
21898
21899 TO_X is a pixel position relative to updated_area of
21900 updated_window. TO_X == -1 means clear to the end of this area. */
21901
21902 void
21903 x_clear_end_of_line (to_x)
21904 int to_x;
21905 {
21906 struct frame *f;
21907 struct window *w = updated_window;
21908 int max_x, min_y, max_y;
21909 int from_x, from_y, to_y;
21910
21911 xassert (updated_window && updated_row);
21912 f = XFRAME (w->frame);
21913
21914 if (updated_row->full_width_p)
21915 max_x = WINDOW_TOTAL_WIDTH (w);
21916 else
21917 max_x = window_box_width (w, updated_area);
21918 max_y = window_text_bottom_y (w);
21919
21920 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
21921 of window. For TO_X > 0, truncate to end of drawing area. */
21922 if (to_x == 0)
21923 return;
21924 else if (to_x < 0)
21925 to_x = max_x;
21926 else
21927 to_x = min (to_x, max_x);
21928
21929 to_y = min (max_y, output_cursor.y + updated_row->height);
21930
21931 /* Notice if the cursor will be cleared by this operation. */
21932 if (!updated_row->full_width_p)
21933 notice_overwritten_cursor (w, updated_area,
21934 output_cursor.x, -1,
21935 updated_row->y,
21936 MATRIX_ROW_BOTTOM_Y (updated_row));
21937
21938 from_x = output_cursor.x;
21939
21940 /* Translate to frame coordinates. */
21941 if (updated_row->full_width_p)
21942 {
21943 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
21944 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
21945 }
21946 else
21947 {
21948 int area_left = window_box_left (w, updated_area);
21949 from_x += area_left;
21950 to_x += area_left;
21951 }
21952
21953 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
21954 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
21955 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
21956
21957 /* Prevent inadvertently clearing to end of the X window. */
21958 if (to_x > from_x && to_y > from_y)
21959 {
21960 BLOCK_INPUT;
21961 FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
21962 to_x - from_x, to_y - from_y);
21963 UNBLOCK_INPUT;
21964 }
21965 }
21966
21967 #endif /* HAVE_WINDOW_SYSTEM */
21968
21969
21970 \f
21971 /***********************************************************************
21972 Cursor types
21973 ***********************************************************************/
21974
21975 /* Value is the internal representation of the specified cursor type
21976 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
21977 of the bar cursor. */
21978
21979 static enum text_cursor_kinds
21980 get_specified_cursor_type (arg, width)
21981 Lisp_Object arg;
21982 int *width;
21983 {
21984 enum text_cursor_kinds type;
21985
21986 if (NILP (arg))
21987 return NO_CURSOR;
21988
21989 if (EQ (arg, Qbox))
21990 return FILLED_BOX_CURSOR;
21991
21992 if (EQ (arg, Qhollow))
21993 return HOLLOW_BOX_CURSOR;
21994
21995 if (EQ (arg, Qbar))
21996 {
21997 *width = 2;
21998 return BAR_CURSOR;
21999 }
22000
22001 if (CONSP (arg)
22002 && EQ (XCAR (arg), Qbar)
22003 && INTEGERP (XCDR (arg))
22004 && XINT (XCDR (arg)) >= 0)
22005 {
22006 *width = XINT (XCDR (arg));
22007 return BAR_CURSOR;
22008 }
22009
22010 if (EQ (arg, Qhbar))
22011 {
22012 *width = 2;
22013 return HBAR_CURSOR;
22014 }
22015
22016 if (CONSP (arg)
22017 && EQ (XCAR (arg), Qhbar)
22018 && INTEGERP (XCDR (arg))
22019 && XINT (XCDR (arg)) >= 0)
22020 {
22021 *width = XINT (XCDR (arg));
22022 return HBAR_CURSOR;
22023 }
22024
22025 /* Treat anything unknown as "hollow box cursor".
22026 It was bad to signal an error; people have trouble fixing
22027 .Xdefaults with Emacs, when it has something bad in it. */
22028 type = HOLLOW_BOX_CURSOR;
22029
22030 return type;
22031 }
22032
22033 /* Set the default cursor types for specified frame. */
22034 void
22035 set_frame_cursor_types (f, arg)
22036 struct frame *f;
22037 Lisp_Object arg;
22038 {
22039 int width;
22040 Lisp_Object tem;
22041
22042 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
22043 FRAME_CURSOR_WIDTH (f) = width;
22044
22045 /* By default, set up the blink-off state depending on the on-state. */
22046
22047 tem = Fassoc (arg, Vblink_cursor_alist);
22048 if (!NILP (tem))
22049 {
22050 FRAME_BLINK_OFF_CURSOR (f)
22051 = get_specified_cursor_type (XCDR (tem), &width);
22052 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
22053 }
22054 else
22055 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
22056 }
22057
22058
22059 /* Return the cursor we want to be displayed in window W. Return
22060 width of bar/hbar cursor through WIDTH arg. Return with
22061 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
22062 (i.e. if the `system caret' should track this cursor).
22063
22064 In a mini-buffer window, we want the cursor only to appear if we
22065 are reading input from this window. For the selected window, we
22066 want the cursor type given by the frame parameter or buffer local
22067 setting of cursor-type. If explicitly marked off, draw no cursor.
22068 In all other cases, we want a hollow box cursor. */
22069
22070 static enum text_cursor_kinds
22071 get_window_cursor_type (w, glyph, width, active_cursor)
22072 struct window *w;
22073 struct glyph *glyph;
22074 int *width;
22075 int *active_cursor;
22076 {
22077 struct frame *f = XFRAME (w->frame);
22078 struct buffer *b = XBUFFER (w->buffer);
22079 int cursor_type = DEFAULT_CURSOR;
22080 Lisp_Object alt_cursor;
22081 int non_selected = 0;
22082
22083 *active_cursor = 1;
22084
22085 /* Echo area */
22086 if (cursor_in_echo_area
22087 && FRAME_HAS_MINIBUF_P (f)
22088 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
22089 {
22090 if (w == XWINDOW (echo_area_window))
22091 {
22092 if (EQ (b->cursor_type, Qt) || NILP (b->cursor_type))
22093 {
22094 *width = FRAME_CURSOR_WIDTH (f);
22095 return FRAME_DESIRED_CURSOR (f);
22096 }
22097 else
22098 return get_specified_cursor_type (b->cursor_type, width);
22099 }
22100
22101 *active_cursor = 0;
22102 non_selected = 1;
22103 }
22104
22105 /* Detect a nonselected window or nonselected frame. */
22106 else if (w != XWINDOW (f->selected_window)
22107 #ifdef HAVE_WINDOW_SYSTEM
22108 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame
22109 #endif
22110 )
22111 {
22112 *active_cursor = 0;
22113
22114 if (MINI_WINDOW_P (w) && minibuf_level == 0)
22115 return NO_CURSOR;
22116
22117 non_selected = 1;
22118 }
22119
22120 /* Never display a cursor in a window in which cursor-type is nil. */
22121 if (NILP (b->cursor_type))
22122 return NO_CURSOR;
22123
22124 /* Get the normal cursor type for this window. */
22125 if (EQ (b->cursor_type, Qt))
22126 {
22127 cursor_type = FRAME_DESIRED_CURSOR (f);
22128 *width = FRAME_CURSOR_WIDTH (f);
22129 }
22130 else
22131 cursor_type = get_specified_cursor_type (b->cursor_type, width);
22132
22133 /* Use cursor-in-non-selected-windows instead
22134 for non-selected window or frame. */
22135 if (non_selected)
22136 {
22137 alt_cursor = b->cursor_in_non_selected_windows;
22138 if (!EQ (Qt, alt_cursor))
22139 return get_specified_cursor_type (alt_cursor, width);
22140 /* t means modify the normal cursor type. */
22141 if (cursor_type == FILLED_BOX_CURSOR)
22142 cursor_type = HOLLOW_BOX_CURSOR;
22143 else if (cursor_type == BAR_CURSOR && *width > 1)
22144 --*width;
22145 return cursor_type;
22146 }
22147
22148 /* Use normal cursor if not blinked off. */
22149 if (!w->cursor_off_p)
22150 {
22151 #ifdef HAVE_WINDOW_SYSTEM
22152 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
22153 {
22154 if (cursor_type == FILLED_BOX_CURSOR)
22155 {
22156 /* Using a block cursor on large images can be very annoying.
22157 So use a hollow cursor for "large" images.
22158 If image is not transparent (no mask), also use hollow cursor. */
22159 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
22160 if (img != NULL && IMAGEP (img->spec))
22161 {
22162 /* Arbitrarily, interpret "Large" as >32x32 and >NxN
22163 where N = size of default frame font size.
22164 This should cover most of the "tiny" icons people may use. */
22165 if (!img->mask
22166 || img->width > max (32, WINDOW_FRAME_COLUMN_WIDTH (w))
22167 || img->height > max (32, WINDOW_FRAME_LINE_HEIGHT (w)))
22168 cursor_type = HOLLOW_BOX_CURSOR;
22169 }
22170 }
22171 else if (cursor_type != NO_CURSOR)
22172 {
22173 /* Display current only supports BOX and HOLLOW cursors for images.
22174 So for now, unconditionally use a HOLLOW cursor when cursor is
22175 not a solid box cursor. */
22176 cursor_type = HOLLOW_BOX_CURSOR;
22177 }
22178 }
22179 #endif
22180 return cursor_type;
22181 }
22182
22183 /* Cursor is blinked off, so determine how to "toggle" it. */
22184
22185 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
22186 if ((alt_cursor = Fassoc (b->cursor_type, Vblink_cursor_alist), !NILP (alt_cursor)))
22187 return get_specified_cursor_type (XCDR (alt_cursor), width);
22188
22189 /* Then see if frame has specified a specific blink off cursor type. */
22190 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
22191 {
22192 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
22193 return FRAME_BLINK_OFF_CURSOR (f);
22194 }
22195
22196 #if 0
22197 /* Some people liked having a permanently visible blinking cursor,
22198 while others had very strong opinions against it. So it was
22199 decided to remove it. KFS 2003-09-03 */
22200
22201 /* Finally perform built-in cursor blinking:
22202 filled box <-> hollow box
22203 wide [h]bar <-> narrow [h]bar
22204 narrow [h]bar <-> no cursor
22205 other type <-> no cursor */
22206
22207 if (cursor_type == FILLED_BOX_CURSOR)
22208 return HOLLOW_BOX_CURSOR;
22209
22210 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
22211 {
22212 *width = 1;
22213 return cursor_type;
22214 }
22215 #endif
22216
22217 return NO_CURSOR;
22218 }
22219
22220
22221 #ifdef HAVE_WINDOW_SYSTEM
22222
22223 /* Notice when the text cursor of window W has been completely
22224 overwritten by a drawing operation that outputs glyphs in AREA
22225 starting at X0 and ending at X1 in the line starting at Y0 and
22226 ending at Y1. X coordinates are area-relative. X1 < 0 means all
22227 the rest of the line after X0 has been written. Y coordinates
22228 are window-relative. */
22229
22230 static void
22231 notice_overwritten_cursor (w, area, x0, x1, y0, y1)
22232 struct window *w;
22233 enum glyph_row_area area;
22234 int x0, y0, x1, y1;
22235 {
22236 int cx0, cx1, cy0, cy1;
22237 struct glyph_row *row;
22238
22239 if (!w->phys_cursor_on_p)
22240 return;
22241 if (area != TEXT_AREA)
22242 return;
22243
22244 if (w->phys_cursor.vpos < 0
22245 || w->phys_cursor.vpos >= w->current_matrix->nrows
22246 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
22247 !(row->enabled_p && row->displays_text_p)))
22248 return;
22249
22250 if (row->cursor_in_fringe_p)
22251 {
22252 row->cursor_in_fringe_p = 0;
22253 draw_fringe_bitmap (w, row, 0);
22254 w->phys_cursor_on_p = 0;
22255 return;
22256 }
22257
22258 cx0 = w->phys_cursor.x;
22259 cx1 = cx0 + w->phys_cursor_width;
22260 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
22261 return;
22262
22263 /* The cursor image will be completely removed from the
22264 screen if the output area intersects the cursor area in
22265 y-direction. When we draw in [y0 y1[, and some part of
22266 the cursor is at y < y0, that part must have been drawn
22267 before. When scrolling, the cursor is erased before
22268 actually scrolling, so we don't come here. When not
22269 scrolling, the rows above the old cursor row must have
22270 changed, and in this case these rows must have written
22271 over the cursor image.
22272
22273 Likewise if part of the cursor is below y1, with the
22274 exception of the cursor being in the first blank row at
22275 the buffer and window end because update_text_area
22276 doesn't draw that row. (Except when it does, but
22277 that's handled in update_text_area.) */
22278
22279 cy0 = w->phys_cursor.y;
22280 cy1 = cy0 + w->phys_cursor_height;
22281 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
22282 return;
22283
22284 w->phys_cursor_on_p = 0;
22285 }
22286
22287 #endif /* HAVE_WINDOW_SYSTEM */
22288
22289 \f
22290 /************************************************************************
22291 Mouse Face
22292 ************************************************************************/
22293
22294 #ifdef HAVE_WINDOW_SYSTEM
22295
22296 /* EXPORT for RIF:
22297 Fix the display of area AREA of overlapping row ROW in window W
22298 with respect to the overlapping part OVERLAPS. */
22299
22300 void
22301 x_fix_overlapping_area (w, row, area, overlaps)
22302 struct window *w;
22303 struct glyph_row *row;
22304 enum glyph_row_area area;
22305 int overlaps;
22306 {
22307 int i, x;
22308
22309 BLOCK_INPUT;
22310
22311 x = 0;
22312 for (i = 0; i < row->used[area];)
22313 {
22314 if (row->glyphs[area][i].overlaps_vertically_p)
22315 {
22316 int start = i, start_x = x;
22317
22318 do
22319 {
22320 x += row->glyphs[area][i].pixel_width;
22321 ++i;
22322 }
22323 while (i < row->used[area]
22324 && row->glyphs[area][i].overlaps_vertically_p);
22325
22326 draw_glyphs (w, start_x, row, area,
22327 start, i,
22328 DRAW_NORMAL_TEXT, overlaps);
22329 }
22330 else
22331 {
22332 x += row->glyphs[area][i].pixel_width;
22333 ++i;
22334 }
22335 }
22336
22337 UNBLOCK_INPUT;
22338 }
22339
22340
22341 /* EXPORT:
22342 Draw the cursor glyph of window W in glyph row ROW. See the
22343 comment of draw_glyphs for the meaning of HL. */
22344
22345 void
22346 draw_phys_cursor_glyph (w, row, hl)
22347 struct window *w;
22348 struct glyph_row *row;
22349 enum draw_glyphs_face hl;
22350 {
22351 /* If cursor hpos is out of bounds, don't draw garbage. This can
22352 happen in mini-buffer windows when switching between echo area
22353 glyphs and mini-buffer. */
22354 if (w->phys_cursor.hpos < row->used[TEXT_AREA])
22355 {
22356 int on_p = w->phys_cursor_on_p;
22357 int x1;
22358 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA,
22359 w->phys_cursor.hpos, w->phys_cursor.hpos + 1,
22360 hl, 0);
22361 w->phys_cursor_on_p = on_p;
22362
22363 if (hl == DRAW_CURSOR)
22364 w->phys_cursor_width = x1 - w->phys_cursor.x;
22365 /* When we erase the cursor, and ROW is overlapped by other
22366 rows, make sure that these overlapping parts of other rows
22367 are redrawn. */
22368 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
22369 {
22370 w->phys_cursor_width = x1 - w->phys_cursor.x;
22371
22372 if (row > w->current_matrix->rows
22373 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
22374 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
22375 OVERLAPS_ERASED_CURSOR);
22376
22377 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
22378 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
22379 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
22380 OVERLAPS_ERASED_CURSOR);
22381 }
22382 }
22383 }
22384
22385
22386 /* EXPORT:
22387 Erase the image of a cursor of window W from the screen. */
22388
22389 void
22390 erase_phys_cursor (w)
22391 struct window *w;
22392 {
22393 struct frame *f = XFRAME (w->frame);
22394 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
22395 int hpos = w->phys_cursor.hpos;
22396 int vpos = w->phys_cursor.vpos;
22397 int mouse_face_here_p = 0;
22398 struct glyph_matrix *active_glyphs = w->current_matrix;
22399 struct glyph_row *cursor_row;
22400 struct glyph *cursor_glyph;
22401 enum draw_glyphs_face hl;
22402
22403 /* No cursor displayed or row invalidated => nothing to do on the
22404 screen. */
22405 if (w->phys_cursor_type == NO_CURSOR)
22406 goto mark_cursor_off;
22407
22408 /* VPOS >= active_glyphs->nrows means that window has been resized.
22409 Don't bother to erase the cursor. */
22410 if (vpos >= active_glyphs->nrows)
22411 goto mark_cursor_off;
22412
22413 /* If row containing cursor is marked invalid, there is nothing we
22414 can do. */
22415 cursor_row = MATRIX_ROW (active_glyphs, vpos);
22416 if (!cursor_row->enabled_p)
22417 goto mark_cursor_off;
22418
22419 /* If line spacing is > 0, old cursor may only be partially visible in
22420 window after split-window. So adjust visible height. */
22421 cursor_row->visible_height = min (cursor_row->visible_height,
22422 window_text_bottom_y (w) - cursor_row->y);
22423
22424 /* If row is completely invisible, don't attempt to delete a cursor which
22425 isn't there. This can happen if cursor is at top of a window, and
22426 we switch to a buffer with a header line in that window. */
22427 if (cursor_row->visible_height <= 0)
22428 goto mark_cursor_off;
22429
22430 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
22431 if (cursor_row->cursor_in_fringe_p)
22432 {
22433 cursor_row->cursor_in_fringe_p = 0;
22434 draw_fringe_bitmap (w, cursor_row, 0);
22435 goto mark_cursor_off;
22436 }
22437
22438 /* This can happen when the new row is shorter than the old one.
22439 In this case, either draw_glyphs or clear_end_of_line
22440 should have cleared the cursor. Note that we wouldn't be
22441 able to erase the cursor in this case because we don't have a
22442 cursor glyph at hand. */
22443 if (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])
22444 goto mark_cursor_off;
22445
22446 /* If the cursor is in the mouse face area, redisplay that when
22447 we clear the cursor. */
22448 if (! NILP (dpyinfo->mouse_face_window)
22449 && w == XWINDOW (dpyinfo->mouse_face_window)
22450 && (vpos > dpyinfo->mouse_face_beg_row
22451 || (vpos == dpyinfo->mouse_face_beg_row
22452 && hpos >= dpyinfo->mouse_face_beg_col))
22453 && (vpos < dpyinfo->mouse_face_end_row
22454 || (vpos == dpyinfo->mouse_face_end_row
22455 && hpos < dpyinfo->mouse_face_end_col))
22456 /* Don't redraw the cursor's spot in mouse face if it is at the
22457 end of a line (on a newline). The cursor appears there, but
22458 mouse highlighting does not. */
22459 && cursor_row->used[TEXT_AREA] > hpos)
22460 mouse_face_here_p = 1;
22461
22462 /* Maybe clear the display under the cursor. */
22463 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
22464 {
22465 int x, y, left_x;
22466 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
22467 int width;
22468
22469 cursor_glyph = get_phys_cursor_glyph (w);
22470 if (cursor_glyph == NULL)
22471 goto mark_cursor_off;
22472
22473 width = cursor_glyph->pixel_width;
22474 left_x = window_box_left_offset (w, TEXT_AREA);
22475 x = w->phys_cursor.x;
22476 if (x < left_x)
22477 width -= left_x - x;
22478 width = min (width, window_box_width (w, TEXT_AREA) - x);
22479 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
22480 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, max (x, left_x));
22481
22482 if (width > 0)
22483 FRAME_RIF (f)->clear_frame_area (f, x, y, width, cursor_row->visible_height);
22484 }
22485
22486 /* Erase the cursor by redrawing the character underneath it. */
22487 if (mouse_face_here_p)
22488 hl = DRAW_MOUSE_FACE;
22489 else
22490 hl = DRAW_NORMAL_TEXT;
22491 draw_phys_cursor_glyph (w, cursor_row, hl);
22492
22493 mark_cursor_off:
22494 w->phys_cursor_on_p = 0;
22495 w->phys_cursor_type = NO_CURSOR;
22496 }
22497
22498
22499 /* EXPORT:
22500 Display or clear cursor of window W. If ON is zero, clear the
22501 cursor. If it is non-zero, display the cursor. If ON is nonzero,
22502 where to put the cursor is specified by HPOS, VPOS, X and Y. */
22503
22504 void
22505 display_and_set_cursor (w, on, hpos, vpos, x, y)
22506 struct window *w;
22507 int on, hpos, vpos, x, y;
22508 {
22509 struct frame *f = XFRAME (w->frame);
22510 int new_cursor_type;
22511 int new_cursor_width;
22512 int active_cursor;
22513 struct glyph_row *glyph_row;
22514 struct glyph *glyph;
22515
22516 /* This is pointless on invisible frames, and dangerous on garbaged
22517 windows and frames; in the latter case, the frame or window may
22518 be in the midst of changing its size, and x and y may be off the
22519 window. */
22520 if (! FRAME_VISIBLE_P (f)
22521 || FRAME_GARBAGED_P (f)
22522 || vpos >= w->current_matrix->nrows
22523 || hpos >= w->current_matrix->matrix_w)
22524 return;
22525
22526 /* If cursor is off and we want it off, return quickly. */
22527 if (!on && !w->phys_cursor_on_p)
22528 return;
22529
22530 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
22531 /* If cursor row is not enabled, we don't really know where to
22532 display the cursor. */
22533 if (!glyph_row->enabled_p)
22534 {
22535 w->phys_cursor_on_p = 0;
22536 return;
22537 }
22538
22539 glyph = NULL;
22540 if (!glyph_row->exact_window_width_line_p
22541 || hpos < glyph_row->used[TEXT_AREA])
22542 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
22543
22544 xassert (interrupt_input_blocked);
22545
22546 /* Set new_cursor_type to the cursor we want to be displayed. */
22547 new_cursor_type = get_window_cursor_type (w, glyph,
22548 &new_cursor_width, &active_cursor);
22549
22550 /* If cursor is currently being shown and we don't want it to be or
22551 it is in the wrong place, or the cursor type is not what we want,
22552 erase it. */
22553 if (w->phys_cursor_on_p
22554 && (!on
22555 || w->phys_cursor.x != x
22556 || w->phys_cursor.y != y
22557 || new_cursor_type != w->phys_cursor_type
22558 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
22559 && new_cursor_width != w->phys_cursor_width)))
22560 erase_phys_cursor (w);
22561
22562 /* Don't check phys_cursor_on_p here because that flag is only set
22563 to zero in some cases where we know that the cursor has been
22564 completely erased, to avoid the extra work of erasing the cursor
22565 twice. In other words, phys_cursor_on_p can be 1 and the cursor
22566 still not be visible, or it has only been partly erased. */
22567 if (on)
22568 {
22569 w->phys_cursor_ascent = glyph_row->ascent;
22570 w->phys_cursor_height = glyph_row->height;
22571
22572 /* Set phys_cursor_.* before x_draw_.* is called because some
22573 of them may need the information. */
22574 w->phys_cursor.x = x;
22575 w->phys_cursor.y = glyph_row->y;
22576 w->phys_cursor.hpos = hpos;
22577 w->phys_cursor.vpos = vpos;
22578 }
22579
22580 FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
22581 new_cursor_type, new_cursor_width,
22582 on, active_cursor);
22583 }
22584
22585
22586 /* Switch the display of W's cursor on or off, according to the value
22587 of ON. */
22588
22589 #ifndef HAVE_NS
22590 static
22591 #endif
22592 void
22593 update_window_cursor (w, on)
22594 struct window *w;
22595 int on;
22596 {
22597 /* Don't update cursor in windows whose frame is in the process
22598 of being deleted. */
22599 if (w->current_matrix)
22600 {
22601 BLOCK_INPUT;
22602 display_and_set_cursor (w, on, w->phys_cursor.hpos, w->phys_cursor.vpos,
22603 w->phys_cursor.x, w->phys_cursor.y);
22604 UNBLOCK_INPUT;
22605 }
22606 }
22607
22608
22609 /* Call update_window_cursor with parameter ON_P on all leaf windows
22610 in the window tree rooted at W. */
22611
22612 static void
22613 update_cursor_in_window_tree (w, on_p)
22614 struct window *w;
22615 int on_p;
22616 {
22617 while (w)
22618 {
22619 if (!NILP (w->hchild))
22620 update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
22621 else if (!NILP (w->vchild))
22622 update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
22623 else
22624 update_window_cursor (w, on_p);
22625
22626 w = NILP (w->next) ? 0 : XWINDOW (w->next);
22627 }
22628 }
22629
22630
22631 /* EXPORT:
22632 Display the cursor on window W, or clear it, according to ON_P.
22633 Don't change the cursor's position. */
22634
22635 void
22636 x_update_cursor (f, on_p)
22637 struct frame *f;
22638 int on_p;
22639 {
22640 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
22641 }
22642
22643
22644 /* EXPORT:
22645 Clear the cursor of window W to background color, and mark the
22646 cursor as not shown. This is used when the text where the cursor
22647 is is about to be rewritten. */
22648
22649 void
22650 x_clear_cursor (w)
22651 struct window *w;
22652 {
22653 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
22654 update_window_cursor (w, 0);
22655 }
22656
22657
22658 /* EXPORT:
22659 Display the active region described by mouse_face_* according to DRAW. */
22660
22661 void
22662 show_mouse_face (dpyinfo, draw)
22663 Display_Info *dpyinfo;
22664 enum draw_glyphs_face draw;
22665 {
22666 struct window *w = XWINDOW (dpyinfo->mouse_face_window);
22667 struct frame *f = XFRAME (WINDOW_FRAME (w));
22668
22669 if (/* If window is in the process of being destroyed, don't bother
22670 to do anything. */
22671 w->current_matrix != NULL
22672 /* Don't update mouse highlight if hidden */
22673 && (draw != DRAW_MOUSE_FACE || !dpyinfo->mouse_face_hidden)
22674 /* Recognize when we are called to operate on rows that don't exist
22675 anymore. This can happen when a window is split. */
22676 && dpyinfo->mouse_face_end_row < w->current_matrix->nrows)
22677 {
22678 int phys_cursor_on_p = w->phys_cursor_on_p;
22679 struct glyph_row *row, *first, *last;
22680
22681 first = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_beg_row);
22682 last = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_end_row);
22683
22684 for (row = first; row <= last && row->enabled_p; ++row)
22685 {
22686 int start_hpos, end_hpos, start_x;
22687
22688 /* For all but the first row, the highlight starts at column 0. */
22689 if (row == first)
22690 {
22691 start_hpos = dpyinfo->mouse_face_beg_col;
22692 start_x = dpyinfo->mouse_face_beg_x;
22693 }
22694 else
22695 {
22696 start_hpos = 0;
22697 start_x = 0;
22698 }
22699
22700 if (row == last)
22701 end_hpos = dpyinfo->mouse_face_end_col;
22702 else
22703 {
22704 end_hpos = row->used[TEXT_AREA];
22705 if (draw == DRAW_NORMAL_TEXT)
22706 row->fill_line_p = 1; /* Clear to end of line */
22707 }
22708
22709 if (end_hpos > start_hpos)
22710 {
22711 draw_glyphs (w, start_x, row, TEXT_AREA,
22712 start_hpos, end_hpos,
22713 draw, 0);
22714
22715 row->mouse_face_p
22716 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
22717 }
22718 }
22719
22720 /* When we've written over the cursor, arrange for it to
22721 be displayed again. */
22722 if (phys_cursor_on_p && !w->phys_cursor_on_p)
22723 {
22724 BLOCK_INPUT;
22725 display_and_set_cursor (w, 1,
22726 w->phys_cursor.hpos, w->phys_cursor.vpos,
22727 w->phys_cursor.x, w->phys_cursor.y);
22728 UNBLOCK_INPUT;
22729 }
22730 }
22731
22732 /* Change the mouse cursor. */
22733 if (draw == DRAW_NORMAL_TEXT && !EQ (dpyinfo->mouse_face_window, f->tool_bar_window))
22734 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
22735 else if (draw == DRAW_MOUSE_FACE)
22736 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
22737 else
22738 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
22739 }
22740
22741 /* EXPORT:
22742 Clear out the mouse-highlighted active region.
22743 Redraw it un-highlighted first. Value is non-zero if mouse
22744 face was actually drawn unhighlighted. */
22745
22746 int
22747 clear_mouse_face (dpyinfo)
22748 Display_Info *dpyinfo;
22749 {
22750 int cleared = 0;
22751
22752 if (!dpyinfo->mouse_face_hidden && !NILP (dpyinfo->mouse_face_window))
22753 {
22754 show_mouse_face (dpyinfo, DRAW_NORMAL_TEXT);
22755 cleared = 1;
22756 }
22757
22758 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
22759 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
22760 dpyinfo->mouse_face_window = Qnil;
22761 dpyinfo->mouse_face_overlay = Qnil;
22762 return cleared;
22763 }
22764
22765
22766 /* EXPORT:
22767 Non-zero if physical cursor of window W is within mouse face. */
22768
22769 int
22770 cursor_in_mouse_face_p (w)
22771 struct window *w;
22772 {
22773 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
22774 int in_mouse_face = 0;
22775
22776 if (WINDOWP (dpyinfo->mouse_face_window)
22777 && XWINDOW (dpyinfo->mouse_face_window) == w)
22778 {
22779 int hpos = w->phys_cursor.hpos;
22780 int vpos = w->phys_cursor.vpos;
22781
22782 if (vpos >= dpyinfo->mouse_face_beg_row
22783 && vpos <= dpyinfo->mouse_face_end_row
22784 && (vpos > dpyinfo->mouse_face_beg_row
22785 || hpos >= dpyinfo->mouse_face_beg_col)
22786 && (vpos < dpyinfo->mouse_face_end_row
22787 || hpos < dpyinfo->mouse_face_end_col
22788 || dpyinfo->mouse_face_past_end))
22789 in_mouse_face = 1;
22790 }
22791
22792 return in_mouse_face;
22793 }
22794
22795
22796
22797 \f
22798 /* Find the glyph matrix position of buffer position CHARPOS in window
22799 *W. HPOS, *VPOS, *X, and *Y are set to the positions found. W's
22800 current glyphs must be up to date. If CHARPOS is above window
22801 start return (0, 0, 0, 0). If CHARPOS is after end of W, return end
22802 of last line in W. In the row containing CHARPOS, stop before glyphs
22803 having STOP as object. */
22804
22805 #if 1 /* This is a version of fast_find_position that's more correct
22806 in the presence of hscrolling, for example. I didn't install
22807 it right away because the problem fixed is minor, it failed
22808 in 20.x as well, and I think it's too risky to install
22809 so near the release of 21.1. 2001-09-25 gerd. */
22810
22811 static
22812 int
22813 fast_find_position (w, charpos, hpos, vpos, x, y, stop)
22814 struct window *w;
22815 EMACS_INT charpos;
22816 int *hpos, *vpos, *x, *y;
22817 Lisp_Object stop;
22818 {
22819 struct glyph_row *row, *first;
22820 struct glyph *glyph, *end;
22821 int past_end = 0;
22822
22823 first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
22824 if (charpos < MATRIX_ROW_START_CHARPOS (first))
22825 {
22826 *x = first->x;
22827 *y = first->y;
22828 *hpos = 0;
22829 *vpos = MATRIX_ROW_VPOS (first, w->current_matrix);
22830 return 1;
22831 }
22832
22833 row = row_containing_pos (w, charpos, first, NULL, 0);
22834 if (row == NULL)
22835 {
22836 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
22837 past_end = 1;
22838 }
22839
22840 /* If whole rows or last part of a row came from a display overlay,
22841 row_containing_pos will skip over such rows because their end pos
22842 equals the start pos of the overlay or interval.
22843
22844 Move back if we have a STOP object and previous row's
22845 end glyph came from STOP. */
22846 if (!NILP (stop))
22847 {
22848 struct glyph_row *prev;
22849 while ((prev = row - 1, prev >= first)
22850 && MATRIX_ROW_END_CHARPOS (prev) == charpos
22851 && prev->used[TEXT_AREA] > 0)
22852 {
22853 struct glyph *beg = prev->glyphs[TEXT_AREA];
22854 glyph = beg + prev->used[TEXT_AREA];
22855 while (--glyph >= beg
22856 && INTEGERP (glyph->object));
22857 if (glyph < beg
22858 || !EQ (stop, glyph->object))
22859 break;
22860 row = prev;
22861 }
22862 }
22863
22864 *x = row->x;
22865 *y = row->y;
22866 *vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
22867
22868 glyph = row->glyphs[TEXT_AREA];
22869 end = glyph + row->used[TEXT_AREA];
22870
22871 /* Skip over glyphs not having an object at the start of the row.
22872 These are special glyphs like truncation marks on terminal
22873 frames. */
22874 if (row->displays_text_p)
22875 while (glyph < end
22876 && INTEGERP (glyph->object)
22877 && !EQ (stop, glyph->object)
22878 && glyph->charpos < 0)
22879 {
22880 *x += glyph->pixel_width;
22881 ++glyph;
22882 }
22883
22884 while (glyph < end
22885 && !INTEGERP (glyph->object)
22886 && !EQ (stop, glyph->object)
22887 && (!BUFFERP (glyph->object)
22888 || glyph->charpos < charpos))
22889 {
22890 *x += glyph->pixel_width;
22891 ++glyph;
22892 }
22893
22894 *hpos = glyph - row->glyphs[TEXT_AREA];
22895 return !past_end;
22896 }
22897
22898 #else /* not 1 */
22899
22900 static int
22901 fast_find_position (w, pos, hpos, vpos, x, y, stop)
22902 struct window *w;
22903 EMACS_INT pos;
22904 int *hpos, *vpos, *x, *y;
22905 Lisp_Object stop;
22906 {
22907 int i;
22908 int lastcol;
22909 int maybe_next_line_p = 0;
22910 int line_start_position;
22911 int yb = window_text_bottom_y (w);
22912 struct glyph_row *row, *best_row;
22913 int row_vpos, best_row_vpos;
22914 int current_x;
22915
22916 row = best_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
22917 row_vpos = best_row_vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
22918
22919 while (row->y < yb)
22920 {
22921 if (row->used[TEXT_AREA])
22922 line_start_position = row->glyphs[TEXT_AREA]->charpos;
22923 else
22924 line_start_position = 0;
22925
22926 if (line_start_position > pos)
22927 break;
22928 /* If the position sought is the end of the buffer,
22929 don't include the blank lines at the bottom of the window. */
22930 else if (line_start_position == pos
22931 && pos == BUF_ZV (XBUFFER (w->buffer)))
22932 {
22933 maybe_next_line_p = 1;
22934 break;
22935 }
22936 else if (line_start_position > 0)
22937 {
22938 best_row = row;
22939 best_row_vpos = row_vpos;
22940 }
22941
22942 if (row->y + row->height >= yb)
22943 break;
22944
22945 ++row;
22946 ++row_vpos;
22947 }
22948
22949 /* Find the right column within BEST_ROW. */
22950 lastcol = 0;
22951 current_x = best_row->x;
22952 for (i = 0; i < best_row->used[TEXT_AREA]; i++)
22953 {
22954 struct glyph *glyph = best_row->glyphs[TEXT_AREA] + i;
22955 int charpos = glyph->charpos;
22956
22957 if (BUFFERP (glyph->object))
22958 {
22959 if (charpos == pos)
22960 {
22961 *hpos = i;
22962 *vpos = best_row_vpos;
22963 *x = current_x;
22964 *y = best_row->y;
22965 return 1;
22966 }
22967 else if (charpos > pos)
22968 break;
22969 }
22970 else if (EQ (glyph->object, stop))
22971 break;
22972
22973 if (charpos > 0)
22974 lastcol = i;
22975 current_x += glyph->pixel_width;
22976 }
22977
22978 /* If we're looking for the end of the buffer,
22979 and we didn't find it in the line we scanned,
22980 use the start of the following line. */
22981 if (maybe_next_line_p)
22982 {
22983 ++best_row;
22984 ++best_row_vpos;
22985 lastcol = 0;
22986 current_x = best_row->x;
22987 }
22988
22989 *vpos = best_row_vpos;
22990 *hpos = lastcol + 1;
22991 *x = current_x;
22992 *y = best_row->y;
22993 return 0;
22994 }
22995
22996 #endif /* not 1 */
22997
22998
22999 /* Find the position of the glyph for position POS in OBJECT in
23000 window W's current matrix, and return in *X, *Y the pixel
23001 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
23002
23003 RIGHT_P non-zero means return the position of the right edge of the
23004 glyph, RIGHT_P zero means return the left edge position.
23005
23006 If no glyph for POS exists in the matrix, return the position of
23007 the glyph with the next smaller position that is in the matrix, if
23008 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
23009 exists in the matrix, return the position of the glyph with the
23010 next larger position in OBJECT.
23011
23012 Value is non-zero if a glyph was found. */
23013
23014 static int
23015 fast_find_string_pos (w, pos, object, hpos, vpos, x, y, right_p)
23016 struct window *w;
23017 EMACS_INT pos;
23018 Lisp_Object object;
23019 int *hpos, *vpos, *x, *y;
23020 int right_p;
23021 {
23022 int yb = window_text_bottom_y (w);
23023 struct glyph_row *r;
23024 struct glyph *best_glyph = NULL;
23025 struct glyph_row *best_row = NULL;
23026 int best_x = 0;
23027
23028 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
23029 r->enabled_p && r->y < yb;
23030 ++r)
23031 {
23032 struct glyph *g = r->glyphs[TEXT_AREA];
23033 struct glyph *e = g + r->used[TEXT_AREA];
23034 int gx;
23035
23036 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
23037 if (EQ (g->object, object))
23038 {
23039 if (g->charpos == pos)
23040 {
23041 best_glyph = g;
23042 best_x = gx;
23043 best_row = r;
23044 goto found;
23045 }
23046 else if (best_glyph == NULL
23047 || ((eabs (g->charpos - pos)
23048 < eabs (best_glyph->charpos - pos))
23049 && (right_p
23050 ? g->charpos < pos
23051 : g->charpos > pos)))
23052 {
23053 best_glyph = g;
23054 best_x = gx;
23055 best_row = r;
23056 }
23057 }
23058 }
23059
23060 found:
23061
23062 if (best_glyph)
23063 {
23064 *x = best_x;
23065 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
23066
23067 if (right_p)
23068 {
23069 *x += best_glyph->pixel_width;
23070 ++*hpos;
23071 }
23072
23073 *y = best_row->y;
23074 *vpos = best_row - w->current_matrix->rows;
23075 }
23076
23077 return best_glyph != NULL;
23078 }
23079
23080
23081 /* See if position X, Y is within a hot-spot of an image. */
23082
23083 static int
23084 on_hot_spot_p (hot_spot, x, y)
23085 Lisp_Object hot_spot;
23086 int x, y;
23087 {
23088 if (!CONSP (hot_spot))
23089 return 0;
23090
23091 if (EQ (XCAR (hot_spot), Qrect))
23092 {
23093 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
23094 Lisp_Object rect = XCDR (hot_spot);
23095 Lisp_Object tem;
23096 if (!CONSP (rect))
23097 return 0;
23098 if (!CONSP (XCAR (rect)))
23099 return 0;
23100 if (!CONSP (XCDR (rect)))
23101 return 0;
23102 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
23103 return 0;
23104 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
23105 return 0;
23106 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
23107 return 0;
23108 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
23109 return 0;
23110 return 1;
23111 }
23112 else if (EQ (XCAR (hot_spot), Qcircle))
23113 {
23114 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
23115 Lisp_Object circ = XCDR (hot_spot);
23116 Lisp_Object lr, lx0, ly0;
23117 if (CONSP (circ)
23118 && CONSP (XCAR (circ))
23119 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
23120 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
23121 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
23122 {
23123 double r = XFLOATINT (lr);
23124 double dx = XINT (lx0) - x;
23125 double dy = XINT (ly0) - y;
23126 return (dx * dx + dy * dy <= r * r);
23127 }
23128 }
23129 else if (EQ (XCAR (hot_spot), Qpoly))
23130 {
23131 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
23132 if (VECTORP (XCDR (hot_spot)))
23133 {
23134 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
23135 Lisp_Object *poly = v->contents;
23136 int n = v->size;
23137 int i;
23138 int inside = 0;
23139 Lisp_Object lx, ly;
23140 int x0, y0;
23141
23142 /* Need an even number of coordinates, and at least 3 edges. */
23143 if (n < 6 || n & 1)
23144 return 0;
23145
23146 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
23147 If count is odd, we are inside polygon. Pixels on edges
23148 may or may not be included depending on actual geometry of the
23149 polygon. */
23150 if ((lx = poly[n-2], !INTEGERP (lx))
23151 || (ly = poly[n-1], !INTEGERP (lx)))
23152 return 0;
23153 x0 = XINT (lx), y0 = XINT (ly);
23154 for (i = 0; i < n; i += 2)
23155 {
23156 int x1 = x0, y1 = y0;
23157 if ((lx = poly[i], !INTEGERP (lx))
23158 || (ly = poly[i+1], !INTEGERP (ly)))
23159 return 0;
23160 x0 = XINT (lx), y0 = XINT (ly);
23161
23162 /* Does this segment cross the X line? */
23163 if (x0 >= x)
23164 {
23165 if (x1 >= x)
23166 continue;
23167 }
23168 else if (x1 < x)
23169 continue;
23170 if (y > y0 && y > y1)
23171 continue;
23172 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
23173 inside = !inside;
23174 }
23175 return inside;
23176 }
23177 }
23178 return 0;
23179 }
23180
23181 Lisp_Object
23182 find_hot_spot (map, x, y)
23183 Lisp_Object map;
23184 int x, y;
23185 {
23186 while (CONSP (map))
23187 {
23188 if (CONSP (XCAR (map))
23189 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
23190 return XCAR (map);
23191 map = XCDR (map);
23192 }
23193
23194 return Qnil;
23195 }
23196
23197 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
23198 3, 3, 0,
23199 doc: /* Lookup in image map MAP coordinates X and Y.
23200 An image map is an alist where each element has the format (AREA ID PLIST).
23201 An AREA is specified as either a rectangle, a circle, or a polygon:
23202 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
23203 pixel coordinates of the upper left and bottom right corners.
23204 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
23205 and the radius of the circle; r may be a float or integer.
23206 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
23207 vector describes one corner in the polygon.
23208 Returns the alist element for the first matching AREA in MAP. */)
23209 (map, x, y)
23210 Lisp_Object map;
23211 Lisp_Object x, y;
23212 {
23213 if (NILP (map))
23214 return Qnil;
23215
23216 CHECK_NUMBER (x);
23217 CHECK_NUMBER (y);
23218
23219 return find_hot_spot (map, XINT (x), XINT (y));
23220 }
23221
23222
23223 /* Display frame CURSOR, optionally using shape defined by POINTER. */
23224 static void
23225 define_frame_cursor1 (f, cursor, pointer)
23226 struct frame *f;
23227 Cursor cursor;
23228 Lisp_Object pointer;
23229 {
23230 /* Do not change cursor shape while dragging mouse. */
23231 if (!NILP (do_mouse_tracking))
23232 return;
23233
23234 if (!NILP (pointer))
23235 {
23236 if (EQ (pointer, Qarrow))
23237 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
23238 else if (EQ (pointer, Qhand))
23239 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
23240 else if (EQ (pointer, Qtext))
23241 cursor = FRAME_X_OUTPUT (f)->text_cursor;
23242 else if (EQ (pointer, intern ("hdrag")))
23243 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
23244 #ifdef HAVE_X_WINDOWS
23245 else if (EQ (pointer, intern ("vdrag")))
23246 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
23247 #endif
23248 else if (EQ (pointer, intern ("hourglass")))
23249 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
23250 else if (EQ (pointer, Qmodeline))
23251 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
23252 else
23253 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
23254 }
23255
23256 if (cursor != No_Cursor)
23257 FRAME_RIF (f)->define_frame_cursor (f, cursor);
23258 }
23259
23260 /* Take proper action when mouse has moved to the mode or header line
23261 or marginal area AREA of window W, x-position X and y-position Y.
23262 X is relative to the start of the text display area of W, so the
23263 width of bitmap areas and scroll bars must be subtracted to get a
23264 position relative to the start of the mode line. */
23265
23266 static void
23267 note_mode_line_or_margin_highlight (window, x, y, area)
23268 Lisp_Object window;
23269 int x, y;
23270 enum window_part area;
23271 {
23272 struct window *w = XWINDOW (window);
23273 struct frame *f = XFRAME (w->frame);
23274 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
23275 Cursor cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
23276 Lisp_Object pointer = Qnil;
23277 int charpos, dx, dy, width, height;
23278 Lisp_Object string, object = Qnil;
23279 Lisp_Object pos, help;
23280
23281 Lisp_Object mouse_face;
23282 int original_x_pixel = x;
23283 struct glyph * glyph = NULL, * row_start_glyph = NULL;
23284 struct glyph_row *row;
23285
23286 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
23287 {
23288 int x0;
23289 struct glyph *end;
23290
23291 string = mode_line_string (w, area, &x, &y, &charpos,
23292 &object, &dx, &dy, &width, &height);
23293
23294 row = (area == ON_MODE_LINE
23295 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
23296 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
23297
23298 /* Find glyph */
23299 if (row->mode_line_p && row->enabled_p)
23300 {
23301 glyph = row_start_glyph = row->glyphs[TEXT_AREA];
23302 end = glyph + row->used[TEXT_AREA];
23303
23304 for (x0 = original_x_pixel;
23305 glyph < end && x0 >= glyph->pixel_width;
23306 ++glyph)
23307 x0 -= glyph->pixel_width;
23308
23309 if (glyph >= end)
23310 glyph = NULL;
23311 }
23312 }
23313 else
23314 {
23315 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
23316 string = marginal_area_string (w, area, &x, &y, &charpos,
23317 &object, &dx, &dy, &width, &height);
23318 }
23319
23320 help = Qnil;
23321
23322 if (IMAGEP (object))
23323 {
23324 Lisp_Object image_map, hotspot;
23325 if ((image_map = Fplist_get (XCDR (object), QCmap),
23326 !NILP (image_map))
23327 && (hotspot = find_hot_spot (image_map, dx, dy),
23328 CONSP (hotspot))
23329 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
23330 {
23331 Lisp_Object area_id, plist;
23332
23333 area_id = XCAR (hotspot);
23334 /* Could check AREA_ID to see if we enter/leave this hot-spot.
23335 If so, we could look for mouse-enter, mouse-leave
23336 properties in PLIST (and do something...). */
23337 hotspot = XCDR (hotspot);
23338 if (CONSP (hotspot)
23339 && (plist = XCAR (hotspot), CONSP (plist)))
23340 {
23341 pointer = Fplist_get (plist, Qpointer);
23342 if (NILP (pointer))
23343 pointer = Qhand;
23344 help = Fplist_get (plist, Qhelp_echo);
23345 if (!NILP (help))
23346 {
23347 help_echo_string = help;
23348 /* Is this correct? ++kfs */
23349 XSETWINDOW (help_echo_window, w);
23350 help_echo_object = w->buffer;
23351 help_echo_pos = charpos;
23352 }
23353 }
23354 }
23355 if (NILP (pointer))
23356 pointer = Fplist_get (XCDR (object), QCpointer);
23357 }
23358
23359 if (STRINGP (string))
23360 {
23361 pos = make_number (charpos);
23362 /* If we're on a string with `help-echo' text property, arrange
23363 for the help to be displayed. This is done by setting the
23364 global variable help_echo_string to the help string. */
23365 if (NILP (help))
23366 {
23367 help = Fget_text_property (pos, Qhelp_echo, string);
23368 if (!NILP (help))
23369 {
23370 help_echo_string = help;
23371 XSETWINDOW (help_echo_window, w);
23372 help_echo_object = string;
23373 help_echo_pos = charpos;
23374 }
23375 }
23376
23377 if (NILP (pointer))
23378 pointer = Fget_text_property (pos, Qpointer, string);
23379
23380 /* Change the mouse pointer according to what is under X/Y. */
23381 if (NILP (pointer) && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
23382 {
23383 Lisp_Object map;
23384 map = Fget_text_property (pos, Qlocal_map, string);
23385 if (!KEYMAPP (map))
23386 map = Fget_text_property (pos, Qkeymap, string);
23387 if (!KEYMAPP (map))
23388 cursor = dpyinfo->vertical_scroll_bar_cursor;
23389 }
23390
23391 /* Change the mouse face according to what is under X/Y. */
23392 mouse_face = Fget_text_property (pos, Qmouse_face, string);
23393 if (!NILP (mouse_face)
23394 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
23395 && glyph)
23396 {
23397 Lisp_Object b, e;
23398
23399 struct glyph * tmp_glyph;
23400
23401 int gpos;
23402 int gseq_length;
23403 int total_pixel_width;
23404 EMACS_INT ignore;
23405
23406 int vpos, hpos;
23407
23408 b = Fprevious_single_property_change (make_number (charpos + 1),
23409 Qmouse_face, string, Qnil);
23410 if (NILP (b))
23411 b = make_number (0);
23412
23413 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
23414 if (NILP (e))
23415 e = make_number (SCHARS (string));
23416
23417 /* Calculate the position(glyph position: GPOS) of GLYPH in
23418 displayed string. GPOS is different from CHARPOS.
23419
23420 CHARPOS is the position of glyph in internal string
23421 object. A mode line string format has structures which
23422 is converted to a flatten by emacs lisp interpreter.
23423 The internal string is an element of the structures.
23424 The displayed string is the flatten string. */
23425 gpos = 0;
23426 if (glyph > row_start_glyph)
23427 {
23428 tmp_glyph = glyph - 1;
23429 while (tmp_glyph >= row_start_glyph
23430 && tmp_glyph->charpos >= XINT (b)
23431 && EQ (tmp_glyph->object, glyph->object))
23432 {
23433 tmp_glyph--;
23434 gpos++;
23435 }
23436 }
23437
23438 /* Calculate the lenght(glyph sequence length: GSEQ_LENGTH) of
23439 displayed string holding GLYPH.
23440
23441 GSEQ_LENGTH is different from SCHARS (STRING).
23442 SCHARS (STRING) returns the length of the internal string. */
23443 for (tmp_glyph = glyph, gseq_length = gpos;
23444 tmp_glyph->charpos < XINT (e);
23445 tmp_glyph++, gseq_length++)
23446 {
23447 if (!EQ (tmp_glyph->object, glyph->object))
23448 break;
23449 }
23450
23451 total_pixel_width = 0;
23452 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
23453 total_pixel_width += tmp_glyph->pixel_width;
23454
23455 /* Pre calculation of re-rendering position */
23456 vpos = (x - gpos);
23457 hpos = (area == ON_MODE_LINE
23458 ? (w->current_matrix)->nrows - 1
23459 : 0);
23460
23461 /* If the re-rendering position is included in the last
23462 re-rendering area, we should do nothing. */
23463 if ( EQ (window, dpyinfo->mouse_face_window)
23464 && dpyinfo->mouse_face_beg_col <= vpos
23465 && vpos < dpyinfo->mouse_face_end_col
23466 && dpyinfo->mouse_face_beg_row == hpos )
23467 return;
23468
23469 if (clear_mouse_face (dpyinfo))
23470 cursor = No_Cursor;
23471
23472 dpyinfo->mouse_face_beg_col = vpos;
23473 dpyinfo->mouse_face_beg_row = hpos;
23474
23475 dpyinfo->mouse_face_beg_x = original_x_pixel - (total_pixel_width + dx);
23476 dpyinfo->mouse_face_beg_y = 0;
23477
23478 dpyinfo->mouse_face_end_col = vpos + gseq_length;
23479 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_beg_row;
23480
23481 dpyinfo->mouse_face_end_x = 0;
23482 dpyinfo->mouse_face_end_y = 0;
23483
23484 dpyinfo->mouse_face_past_end = 0;
23485 dpyinfo->mouse_face_window = window;
23486
23487 dpyinfo->mouse_face_face_id = face_at_string_position (w, string,
23488 charpos,
23489 0, 0, 0, &ignore,
23490 glyph->face_id, 1);
23491 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
23492
23493 if (NILP (pointer))
23494 pointer = Qhand;
23495 }
23496 else if ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
23497 clear_mouse_face (dpyinfo);
23498 }
23499 define_frame_cursor1 (f, cursor, pointer);
23500 }
23501
23502
23503 /* EXPORT:
23504 Take proper action when the mouse has moved to position X, Y on
23505 frame F as regards highlighting characters that have mouse-face
23506 properties. Also de-highlighting chars where the mouse was before.
23507 X and Y can be negative or out of range. */
23508
23509 void
23510 note_mouse_highlight (f, x, y)
23511 struct frame *f;
23512 int x, y;
23513 {
23514 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
23515 enum window_part part;
23516 Lisp_Object window;
23517 struct window *w;
23518 Cursor cursor = No_Cursor;
23519 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
23520 struct buffer *b;
23521
23522 /* When a menu is active, don't highlight because this looks odd. */
23523 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS)
23524 if (popup_activated ())
23525 return;
23526 #endif
23527
23528 if (NILP (Vmouse_highlight)
23529 || !f->glyphs_initialized_p)
23530 return;
23531
23532 dpyinfo->mouse_face_mouse_x = x;
23533 dpyinfo->mouse_face_mouse_y = y;
23534 dpyinfo->mouse_face_mouse_frame = f;
23535
23536 if (dpyinfo->mouse_face_defer)
23537 return;
23538
23539 if (gc_in_progress)
23540 {
23541 dpyinfo->mouse_face_deferred_gc = 1;
23542 return;
23543 }
23544
23545 /* Which window is that in? */
23546 window = window_from_coordinates (f, x, y, &part, 0, 0, 1);
23547
23548 /* If we were displaying active text in another window, clear that.
23549 Also clear if we move out of text area in same window. */
23550 if (! EQ (window, dpyinfo->mouse_face_window)
23551 || (part != ON_TEXT && part != ON_MODE_LINE && part != ON_HEADER_LINE
23552 && !NILP (dpyinfo->mouse_face_window)))
23553 clear_mouse_face (dpyinfo);
23554
23555 /* Not on a window -> return. */
23556 if (!WINDOWP (window))
23557 return;
23558
23559 /* Reset help_echo_string. It will get recomputed below. */
23560 help_echo_string = Qnil;
23561
23562 /* Convert to window-relative pixel coordinates. */
23563 w = XWINDOW (window);
23564 frame_to_window_pixel_xy (w, &x, &y);
23565
23566 /* Handle tool-bar window differently since it doesn't display a
23567 buffer. */
23568 if (EQ (window, f->tool_bar_window))
23569 {
23570 note_tool_bar_highlight (f, x, y);
23571 return;
23572 }
23573
23574 /* Mouse is on the mode, header line or margin? */
23575 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
23576 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
23577 {
23578 note_mode_line_or_margin_highlight (window, x, y, part);
23579 return;
23580 }
23581
23582 if (part == ON_VERTICAL_BORDER)
23583 {
23584 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
23585 help_echo_string = build_string ("drag-mouse-1: resize");
23586 }
23587 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
23588 || part == ON_SCROLL_BAR)
23589 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
23590 else
23591 cursor = FRAME_X_OUTPUT (f)->text_cursor;
23592
23593 /* Are we in a window whose display is up to date?
23594 And verify the buffer's text has not changed. */
23595 b = XBUFFER (w->buffer);
23596 if (part == ON_TEXT
23597 && EQ (w->window_end_valid, w->buffer)
23598 && XFASTINT (w->last_modified) == BUF_MODIFF (b)
23599 && XFASTINT (w->last_overlay_modified) == BUF_OVERLAY_MODIFF (b))
23600 {
23601 int hpos, vpos, pos, i, dx, dy, area;
23602 struct glyph *glyph;
23603 Lisp_Object object;
23604 Lisp_Object mouse_face = Qnil, overlay = Qnil, position;
23605 Lisp_Object *overlay_vec = NULL;
23606 int noverlays;
23607 struct buffer *obuf;
23608 int obegv, ozv, same_region;
23609
23610 /* Find the glyph under X/Y. */
23611 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
23612
23613 /* Look for :pointer property on image. */
23614 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
23615 {
23616 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
23617 if (img != NULL && IMAGEP (img->spec))
23618 {
23619 Lisp_Object image_map, hotspot;
23620 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
23621 !NILP (image_map))
23622 && (hotspot = find_hot_spot (image_map,
23623 glyph->slice.x + dx,
23624 glyph->slice.y + dy),
23625 CONSP (hotspot))
23626 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
23627 {
23628 Lisp_Object area_id, plist;
23629
23630 area_id = XCAR (hotspot);
23631 /* Could check AREA_ID to see if we enter/leave this hot-spot.
23632 If so, we could look for mouse-enter, mouse-leave
23633 properties in PLIST (and do something...). */
23634 hotspot = XCDR (hotspot);
23635 if (CONSP (hotspot)
23636 && (plist = XCAR (hotspot), CONSP (plist)))
23637 {
23638 pointer = Fplist_get (plist, Qpointer);
23639 if (NILP (pointer))
23640 pointer = Qhand;
23641 help_echo_string = Fplist_get (plist, Qhelp_echo);
23642 if (!NILP (help_echo_string))
23643 {
23644 help_echo_window = window;
23645 help_echo_object = glyph->object;
23646 help_echo_pos = glyph->charpos;
23647 }
23648 }
23649 }
23650 if (NILP (pointer))
23651 pointer = Fplist_get (XCDR (img->spec), QCpointer);
23652 }
23653 }
23654
23655 /* Clear mouse face if X/Y not over text. */
23656 if (glyph == NULL
23657 || area != TEXT_AREA
23658 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p)
23659 {
23660 if (clear_mouse_face (dpyinfo))
23661 cursor = No_Cursor;
23662 if (NILP (pointer))
23663 {
23664 if (area != TEXT_AREA)
23665 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
23666 else
23667 pointer = Vvoid_text_area_pointer;
23668 }
23669 goto set_cursor;
23670 }
23671
23672 pos = glyph->charpos;
23673 object = glyph->object;
23674 if (!STRINGP (object) && !BUFFERP (object))
23675 goto set_cursor;
23676
23677 /* If we get an out-of-range value, return now; avoid an error. */
23678 if (BUFFERP (object) && pos > BUF_Z (b))
23679 goto set_cursor;
23680
23681 /* Make the window's buffer temporarily current for
23682 overlays_at and compute_char_face. */
23683 obuf = current_buffer;
23684 current_buffer = b;
23685 obegv = BEGV;
23686 ozv = ZV;
23687 BEGV = BEG;
23688 ZV = Z;
23689
23690 /* Is this char mouse-active or does it have help-echo? */
23691 position = make_number (pos);
23692
23693 if (BUFFERP (object))
23694 {
23695 /* Put all the overlays we want in a vector in overlay_vec. */
23696 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
23697 /* Sort overlays into increasing priority order. */
23698 noverlays = sort_overlays (overlay_vec, noverlays, w);
23699 }
23700 else
23701 noverlays = 0;
23702
23703 same_region = (EQ (window, dpyinfo->mouse_face_window)
23704 && vpos >= dpyinfo->mouse_face_beg_row
23705 && vpos <= dpyinfo->mouse_face_end_row
23706 && (vpos > dpyinfo->mouse_face_beg_row
23707 || hpos >= dpyinfo->mouse_face_beg_col)
23708 && (vpos < dpyinfo->mouse_face_end_row
23709 || hpos < dpyinfo->mouse_face_end_col
23710 || dpyinfo->mouse_face_past_end));
23711
23712 if (same_region)
23713 cursor = No_Cursor;
23714
23715 /* Check mouse-face highlighting. */
23716 if (! same_region
23717 /* If there exists an overlay with mouse-face overlapping
23718 the one we are currently highlighting, we have to
23719 check if we enter the overlapping overlay, and then
23720 highlight only that. */
23721 || (OVERLAYP (dpyinfo->mouse_face_overlay)
23722 && mouse_face_overlay_overlaps (dpyinfo->mouse_face_overlay)))
23723 {
23724 /* Find the highest priority overlay that has a mouse-face
23725 property. */
23726 overlay = Qnil;
23727 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
23728 {
23729 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
23730 if (!NILP (mouse_face))
23731 overlay = overlay_vec[i];
23732 }
23733
23734 /* If we're actually highlighting the same overlay as
23735 before, there's no need to do that again. */
23736 if (!NILP (overlay)
23737 && EQ (overlay, dpyinfo->mouse_face_overlay))
23738 goto check_help_echo;
23739
23740 dpyinfo->mouse_face_overlay = overlay;
23741
23742 /* Clear the display of the old active region, if any. */
23743 if (clear_mouse_face (dpyinfo))
23744 cursor = No_Cursor;
23745
23746 /* If no overlay applies, get a text property. */
23747 if (NILP (overlay))
23748 mouse_face = Fget_text_property (position, Qmouse_face, object);
23749
23750 /* Handle the overlay case. */
23751 if (!NILP (overlay))
23752 {
23753 /* Find the range of text around this char that
23754 should be active. */
23755 Lisp_Object before, after;
23756 EMACS_INT ignore;
23757
23758 before = Foverlay_start (overlay);
23759 after = Foverlay_end (overlay);
23760 /* Record this as the current active region. */
23761 fast_find_position (w, XFASTINT (before),
23762 &dpyinfo->mouse_face_beg_col,
23763 &dpyinfo->mouse_face_beg_row,
23764 &dpyinfo->mouse_face_beg_x,
23765 &dpyinfo->mouse_face_beg_y, Qnil);
23766
23767 dpyinfo->mouse_face_past_end
23768 = !fast_find_position (w, XFASTINT (after),
23769 &dpyinfo->mouse_face_end_col,
23770 &dpyinfo->mouse_face_end_row,
23771 &dpyinfo->mouse_face_end_x,
23772 &dpyinfo->mouse_face_end_y, Qnil);
23773 dpyinfo->mouse_face_window = window;
23774
23775 dpyinfo->mouse_face_face_id
23776 = face_at_buffer_position (w, pos, 0, 0,
23777 &ignore, pos + 1,
23778 !dpyinfo->mouse_face_hidden);
23779
23780 /* Display it as active. */
23781 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
23782 cursor = No_Cursor;
23783 }
23784 /* Handle the text property case. */
23785 else if (!NILP (mouse_face) && BUFFERP (object))
23786 {
23787 /* Find the range of text around this char that
23788 should be active. */
23789 Lisp_Object before, after, beginning, end;
23790 EMACS_INT ignore;
23791
23792 beginning = Fmarker_position (w->start);
23793 end = make_number (BUF_Z (XBUFFER (object))
23794 - XFASTINT (w->window_end_pos));
23795 before
23796 = Fprevious_single_property_change (make_number (pos + 1),
23797 Qmouse_face,
23798 object, beginning);
23799 after
23800 = Fnext_single_property_change (position, Qmouse_face,
23801 object, end);
23802
23803 /* Record this as the current active region. */
23804 fast_find_position (w, XFASTINT (before),
23805 &dpyinfo->mouse_face_beg_col,
23806 &dpyinfo->mouse_face_beg_row,
23807 &dpyinfo->mouse_face_beg_x,
23808 &dpyinfo->mouse_face_beg_y, Qnil);
23809 dpyinfo->mouse_face_past_end
23810 = !fast_find_position (w, XFASTINT (after),
23811 &dpyinfo->mouse_face_end_col,
23812 &dpyinfo->mouse_face_end_row,
23813 &dpyinfo->mouse_face_end_x,
23814 &dpyinfo->mouse_face_end_y, Qnil);
23815 dpyinfo->mouse_face_window = window;
23816
23817 if (BUFFERP (object))
23818 dpyinfo->mouse_face_face_id
23819 = face_at_buffer_position (w, pos, 0, 0,
23820 &ignore, pos + 1,
23821 !dpyinfo->mouse_face_hidden);
23822
23823 /* Display it as active. */
23824 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
23825 cursor = No_Cursor;
23826 }
23827 else if (!NILP (mouse_face) && STRINGP (object))
23828 {
23829 Lisp_Object b, e;
23830 EMACS_INT ignore;
23831
23832 b = Fprevious_single_property_change (make_number (pos + 1),
23833 Qmouse_face,
23834 object, Qnil);
23835 e = Fnext_single_property_change (position, Qmouse_face,
23836 object, Qnil);
23837 if (NILP (b))
23838 b = make_number (0);
23839 if (NILP (e))
23840 e = make_number (SCHARS (object) - 1);
23841
23842 fast_find_string_pos (w, XINT (b), object,
23843 &dpyinfo->mouse_face_beg_col,
23844 &dpyinfo->mouse_face_beg_row,
23845 &dpyinfo->mouse_face_beg_x,
23846 &dpyinfo->mouse_face_beg_y, 0);
23847 fast_find_string_pos (w, XINT (e), object,
23848 &dpyinfo->mouse_face_end_col,
23849 &dpyinfo->mouse_face_end_row,
23850 &dpyinfo->mouse_face_end_x,
23851 &dpyinfo->mouse_face_end_y, 1);
23852 dpyinfo->mouse_face_past_end = 0;
23853 dpyinfo->mouse_face_window = window;
23854 dpyinfo->mouse_face_face_id
23855 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
23856 glyph->face_id, 1);
23857 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
23858 cursor = No_Cursor;
23859 }
23860 else if (STRINGP (object) && NILP (mouse_face))
23861 {
23862 /* A string which doesn't have mouse-face, but
23863 the text ``under'' it might have. */
23864 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
23865 int start = MATRIX_ROW_START_CHARPOS (r);
23866
23867 pos = string_buffer_position (w, object, start);
23868 if (pos > 0)
23869 mouse_face = get_char_property_and_overlay (make_number (pos),
23870 Qmouse_face,
23871 w->buffer,
23872 &overlay);
23873 if (!NILP (mouse_face) && !NILP (overlay))
23874 {
23875 Lisp_Object before = Foverlay_start (overlay);
23876 Lisp_Object after = Foverlay_end (overlay);
23877 EMACS_INT ignore;
23878
23879 /* Note that we might not be able to find position
23880 BEFORE in the glyph matrix if the overlay is
23881 entirely covered by a `display' property. In
23882 this case, we overshoot. So let's stop in
23883 the glyph matrix before glyphs for OBJECT. */
23884 fast_find_position (w, XFASTINT (before),
23885 &dpyinfo->mouse_face_beg_col,
23886 &dpyinfo->mouse_face_beg_row,
23887 &dpyinfo->mouse_face_beg_x,
23888 &dpyinfo->mouse_face_beg_y,
23889 object);
23890
23891 dpyinfo->mouse_face_past_end
23892 = !fast_find_position (w, XFASTINT (after),
23893 &dpyinfo->mouse_face_end_col,
23894 &dpyinfo->mouse_face_end_row,
23895 &dpyinfo->mouse_face_end_x,
23896 &dpyinfo->mouse_face_end_y,
23897 Qnil);
23898 dpyinfo->mouse_face_window = window;
23899 dpyinfo->mouse_face_face_id
23900 = face_at_buffer_position (w, pos, 0, 0,
23901 &ignore, pos + 1,
23902 !dpyinfo->mouse_face_hidden);
23903
23904 /* Display it as active. */
23905 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
23906 cursor = No_Cursor;
23907 }
23908 }
23909 }
23910
23911 check_help_echo:
23912
23913 /* Look for a `help-echo' property. */
23914 if (NILP (help_echo_string)) {
23915 Lisp_Object help, overlay;
23916
23917 /* Check overlays first. */
23918 help = overlay = Qnil;
23919 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
23920 {
23921 overlay = overlay_vec[i];
23922 help = Foverlay_get (overlay, Qhelp_echo);
23923 }
23924
23925 if (!NILP (help))
23926 {
23927 help_echo_string = help;
23928 help_echo_window = window;
23929 help_echo_object = overlay;
23930 help_echo_pos = pos;
23931 }
23932 else
23933 {
23934 Lisp_Object object = glyph->object;
23935 int charpos = glyph->charpos;
23936
23937 /* Try text properties. */
23938 if (STRINGP (object)
23939 && charpos >= 0
23940 && charpos < SCHARS (object))
23941 {
23942 help = Fget_text_property (make_number (charpos),
23943 Qhelp_echo, object);
23944 if (NILP (help))
23945 {
23946 /* If the string itself doesn't specify a help-echo,
23947 see if the buffer text ``under'' it does. */
23948 struct glyph_row *r
23949 = MATRIX_ROW (w->current_matrix, vpos);
23950 int start = MATRIX_ROW_START_CHARPOS (r);
23951 int pos = string_buffer_position (w, object, start);
23952 if (pos > 0)
23953 {
23954 help = Fget_char_property (make_number (pos),
23955 Qhelp_echo, w->buffer);
23956 if (!NILP (help))
23957 {
23958 charpos = pos;
23959 object = w->buffer;
23960 }
23961 }
23962 }
23963 }
23964 else if (BUFFERP (object)
23965 && charpos >= BEGV
23966 && charpos < ZV)
23967 help = Fget_text_property (make_number (charpos), Qhelp_echo,
23968 object);
23969
23970 if (!NILP (help))
23971 {
23972 help_echo_string = help;
23973 help_echo_window = window;
23974 help_echo_object = object;
23975 help_echo_pos = charpos;
23976 }
23977 }
23978 }
23979
23980 /* Look for a `pointer' property. */
23981 if (NILP (pointer))
23982 {
23983 /* Check overlays first. */
23984 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
23985 pointer = Foverlay_get (overlay_vec[i], Qpointer);
23986
23987 if (NILP (pointer))
23988 {
23989 Lisp_Object object = glyph->object;
23990 int charpos = glyph->charpos;
23991
23992 /* Try text properties. */
23993 if (STRINGP (object)
23994 && charpos >= 0
23995 && charpos < SCHARS (object))
23996 {
23997 pointer = Fget_text_property (make_number (charpos),
23998 Qpointer, object);
23999 if (NILP (pointer))
24000 {
24001 /* If the string itself doesn't specify a pointer,
24002 see if the buffer text ``under'' it does. */
24003 struct glyph_row *r
24004 = MATRIX_ROW (w->current_matrix, vpos);
24005 int start = MATRIX_ROW_START_CHARPOS (r);
24006 int pos = string_buffer_position (w, object, start);
24007 if (pos > 0)
24008 pointer = Fget_char_property (make_number (pos),
24009 Qpointer, w->buffer);
24010 }
24011 }
24012 else if (BUFFERP (object)
24013 && charpos >= BEGV
24014 && charpos < ZV)
24015 pointer = Fget_text_property (make_number (charpos),
24016 Qpointer, object);
24017 }
24018 }
24019
24020 BEGV = obegv;
24021 ZV = ozv;
24022 current_buffer = obuf;
24023 }
24024
24025 set_cursor:
24026
24027 define_frame_cursor1 (f, cursor, pointer);
24028 }
24029
24030
24031 /* EXPORT for RIF:
24032 Clear any mouse-face on window W. This function is part of the
24033 redisplay interface, and is called from try_window_id and similar
24034 functions to ensure the mouse-highlight is off. */
24035
24036 void
24037 x_clear_window_mouse_face (w)
24038 struct window *w;
24039 {
24040 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
24041 Lisp_Object window;
24042
24043 BLOCK_INPUT;
24044 XSETWINDOW (window, w);
24045 if (EQ (window, dpyinfo->mouse_face_window))
24046 clear_mouse_face (dpyinfo);
24047 UNBLOCK_INPUT;
24048 }
24049
24050
24051 /* EXPORT:
24052 Just discard the mouse face information for frame F, if any.
24053 This is used when the size of F is changed. */
24054
24055 void
24056 cancel_mouse_face (f)
24057 struct frame *f;
24058 {
24059 Lisp_Object window;
24060 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
24061
24062 window = dpyinfo->mouse_face_window;
24063 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
24064 {
24065 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
24066 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
24067 dpyinfo->mouse_face_window = Qnil;
24068 }
24069 }
24070
24071
24072 #endif /* HAVE_WINDOW_SYSTEM */
24073
24074 \f
24075 /***********************************************************************
24076 Exposure Events
24077 ***********************************************************************/
24078
24079 #ifdef HAVE_WINDOW_SYSTEM
24080
24081 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
24082 which intersects rectangle R. R is in window-relative coordinates. */
24083
24084 static void
24085 expose_area (w, row, r, area)
24086 struct window *w;
24087 struct glyph_row *row;
24088 XRectangle *r;
24089 enum glyph_row_area area;
24090 {
24091 struct glyph *first = row->glyphs[area];
24092 struct glyph *end = row->glyphs[area] + row->used[area];
24093 struct glyph *last;
24094 int first_x, start_x, x;
24095
24096 if (area == TEXT_AREA && row->fill_line_p)
24097 /* If row extends face to end of line write the whole line. */
24098 draw_glyphs (w, 0, row, area,
24099 0, row->used[area],
24100 DRAW_NORMAL_TEXT, 0);
24101 else
24102 {
24103 /* Set START_X to the window-relative start position for drawing glyphs of
24104 AREA. The first glyph of the text area can be partially visible.
24105 The first glyphs of other areas cannot. */
24106 start_x = window_box_left_offset (w, area);
24107 x = start_x;
24108 if (area == TEXT_AREA)
24109 x += row->x;
24110
24111 /* Find the first glyph that must be redrawn. */
24112 while (first < end
24113 && x + first->pixel_width < r->x)
24114 {
24115 x += first->pixel_width;
24116 ++first;
24117 }
24118
24119 /* Find the last one. */
24120 last = first;
24121 first_x = x;
24122 while (last < end
24123 && x < r->x + r->width)
24124 {
24125 x += last->pixel_width;
24126 ++last;
24127 }
24128
24129 /* Repaint. */
24130 if (last > first)
24131 draw_glyphs (w, first_x - start_x, row, area,
24132 first - row->glyphs[area], last - row->glyphs[area],
24133 DRAW_NORMAL_TEXT, 0);
24134 }
24135 }
24136
24137
24138 /* Redraw the parts of the glyph row ROW on window W intersecting
24139 rectangle R. R is in window-relative coordinates. Value is
24140 non-zero if mouse-face was overwritten. */
24141
24142 static int
24143 expose_line (w, row, r)
24144 struct window *w;
24145 struct glyph_row *row;
24146 XRectangle *r;
24147 {
24148 xassert (row->enabled_p);
24149
24150 if (row->mode_line_p || w->pseudo_window_p)
24151 draw_glyphs (w, 0, row, TEXT_AREA,
24152 0, row->used[TEXT_AREA],
24153 DRAW_NORMAL_TEXT, 0);
24154 else
24155 {
24156 if (row->used[LEFT_MARGIN_AREA])
24157 expose_area (w, row, r, LEFT_MARGIN_AREA);
24158 if (row->used[TEXT_AREA])
24159 expose_area (w, row, r, TEXT_AREA);
24160 if (row->used[RIGHT_MARGIN_AREA])
24161 expose_area (w, row, r, RIGHT_MARGIN_AREA);
24162 draw_row_fringe_bitmaps (w, row);
24163 }
24164
24165 return row->mouse_face_p;
24166 }
24167
24168
24169 /* Redraw those parts of glyphs rows during expose event handling that
24170 overlap other rows. Redrawing of an exposed line writes over parts
24171 of lines overlapping that exposed line; this function fixes that.
24172
24173 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
24174 row in W's current matrix that is exposed and overlaps other rows.
24175 LAST_OVERLAPPING_ROW is the last such row. */
24176
24177 static void
24178 expose_overlaps (w, first_overlapping_row, last_overlapping_row, r)
24179 struct window *w;
24180 struct glyph_row *first_overlapping_row;
24181 struct glyph_row *last_overlapping_row;
24182 XRectangle *r;
24183 {
24184 struct glyph_row *row;
24185
24186 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
24187 if (row->overlapping_p)
24188 {
24189 xassert (row->enabled_p && !row->mode_line_p);
24190
24191 row->clip = r;
24192 if (row->used[LEFT_MARGIN_AREA])
24193 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
24194
24195 if (row->used[TEXT_AREA])
24196 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
24197
24198 if (row->used[RIGHT_MARGIN_AREA])
24199 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
24200 row->clip = NULL;
24201 }
24202 }
24203
24204
24205 /* Return non-zero if W's cursor intersects rectangle R. */
24206
24207 static int
24208 phys_cursor_in_rect_p (w, r)
24209 struct window *w;
24210 XRectangle *r;
24211 {
24212 XRectangle cr, result;
24213 struct glyph *cursor_glyph;
24214 struct glyph_row *row;
24215
24216 if (w->phys_cursor.vpos >= 0
24217 && w->phys_cursor.vpos < w->current_matrix->nrows
24218 && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
24219 row->enabled_p)
24220 && row->cursor_in_fringe_p)
24221 {
24222 /* Cursor is in the fringe. */
24223 cr.x = window_box_right_offset (w,
24224 (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
24225 ? RIGHT_MARGIN_AREA
24226 : TEXT_AREA));
24227 cr.y = row->y;
24228 cr.width = WINDOW_RIGHT_FRINGE_WIDTH (w);
24229 cr.height = row->height;
24230 return x_intersect_rectangles (&cr, r, &result);
24231 }
24232
24233 cursor_glyph = get_phys_cursor_glyph (w);
24234 if (cursor_glyph)
24235 {
24236 /* r is relative to W's box, but w->phys_cursor.x is relative
24237 to left edge of W's TEXT area. Adjust it. */
24238 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
24239 cr.y = w->phys_cursor.y;
24240 cr.width = cursor_glyph->pixel_width;
24241 cr.height = w->phys_cursor_height;
24242 /* ++KFS: W32 version used W32-specific IntersectRect here, but
24243 I assume the effect is the same -- and this is portable. */
24244 return x_intersect_rectangles (&cr, r, &result);
24245 }
24246 /* If we don't understand the format, pretend we're not in the hot-spot. */
24247 return 0;
24248 }
24249
24250
24251 /* EXPORT:
24252 Draw a vertical window border to the right of window W if W doesn't
24253 have vertical scroll bars. */
24254
24255 void
24256 x_draw_vertical_border (w)
24257 struct window *w;
24258 {
24259 struct frame *f = XFRAME (WINDOW_FRAME (w));
24260
24261 /* We could do better, if we knew what type of scroll-bar the adjacent
24262 windows (on either side) have... But we don't :-(
24263 However, I think this works ok. ++KFS 2003-04-25 */
24264
24265 /* Redraw borders between horizontally adjacent windows. Don't
24266 do it for frames with vertical scroll bars because either the
24267 right scroll bar of a window, or the left scroll bar of its
24268 neighbor will suffice as a border. */
24269 if (FRAME_HAS_VERTICAL_SCROLL_BARS (XFRAME (w->frame)))
24270 return;
24271
24272 if (!WINDOW_RIGHTMOST_P (w)
24273 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
24274 {
24275 int x0, x1, y0, y1;
24276
24277 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
24278 y1 -= 1;
24279
24280 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
24281 x1 -= 1;
24282
24283 FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
24284 }
24285 else if (!WINDOW_LEFTMOST_P (w)
24286 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
24287 {
24288 int x0, x1, y0, y1;
24289
24290 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
24291 y1 -= 1;
24292
24293 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
24294 x0 -= 1;
24295
24296 FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
24297 }
24298 }
24299
24300
24301 /* Redraw the part of window W intersection rectangle FR. Pixel
24302 coordinates in FR are frame-relative. Call this function with
24303 input blocked. Value is non-zero if the exposure overwrites
24304 mouse-face. */
24305
24306 static int
24307 expose_window (w, fr)
24308 struct window *w;
24309 XRectangle *fr;
24310 {
24311 struct frame *f = XFRAME (w->frame);
24312 XRectangle wr, r;
24313 int mouse_face_overwritten_p = 0;
24314
24315 /* If window is not yet fully initialized, do nothing. This can
24316 happen when toolkit scroll bars are used and a window is split.
24317 Reconfiguring the scroll bar will generate an expose for a newly
24318 created window. */
24319 if (w->current_matrix == NULL)
24320 return 0;
24321
24322 /* When we're currently updating the window, display and current
24323 matrix usually don't agree. Arrange for a thorough display
24324 later. */
24325 if (w == updated_window)
24326 {
24327 SET_FRAME_GARBAGED (f);
24328 return 0;
24329 }
24330
24331 /* Frame-relative pixel rectangle of W. */
24332 wr.x = WINDOW_LEFT_EDGE_X (w);
24333 wr.y = WINDOW_TOP_EDGE_Y (w);
24334 wr.width = WINDOW_TOTAL_WIDTH (w);
24335 wr.height = WINDOW_TOTAL_HEIGHT (w);
24336
24337 if (x_intersect_rectangles (fr, &wr, &r))
24338 {
24339 int yb = window_text_bottom_y (w);
24340 struct glyph_row *row;
24341 int cursor_cleared_p;
24342 struct glyph_row *first_overlapping_row, *last_overlapping_row;
24343
24344 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
24345 r.x, r.y, r.width, r.height));
24346
24347 /* Convert to window coordinates. */
24348 r.x -= WINDOW_LEFT_EDGE_X (w);
24349 r.y -= WINDOW_TOP_EDGE_Y (w);
24350
24351 /* Turn off the cursor. */
24352 if (!w->pseudo_window_p
24353 && phys_cursor_in_rect_p (w, &r))
24354 {
24355 x_clear_cursor (w);
24356 cursor_cleared_p = 1;
24357 }
24358 else
24359 cursor_cleared_p = 0;
24360
24361 /* Update lines intersecting rectangle R. */
24362 first_overlapping_row = last_overlapping_row = NULL;
24363 for (row = w->current_matrix->rows;
24364 row->enabled_p;
24365 ++row)
24366 {
24367 int y0 = row->y;
24368 int y1 = MATRIX_ROW_BOTTOM_Y (row);
24369
24370 if ((y0 >= r.y && y0 < r.y + r.height)
24371 || (y1 > r.y && y1 < r.y + r.height)
24372 || (r.y >= y0 && r.y < y1)
24373 || (r.y + r.height > y0 && r.y + r.height < y1))
24374 {
24375 /* A header line may be overlapping, but there is no need
24376 to fix overlapping areas for them. KFS 2005-02-12 */
24377 if (row->overlapping_p && !row->mode_line_p)
24378 {
24379 if (first_overlapping_row == NULL)
24380 first_overlapping_row = row;
24381 last_overlapping_row = row;
24382 }
24383
24384 row->clip = fr;
24385 if (expose_line (w, row, &r))
24386 mouse_face_overwritten_p = 1;
24387 row->clip = NULL;
24388 }
24389 else if (row->overlapping_p)
24390 {
24391 /* We must redraw a row overlapping the exposed area. */
24392 if (y0 < r.y
24393 ? y0 + row->phys_height > r.y
24394 : y0 + row->ascent - row->phys_ascent < r.y +r.height)
24395 {
24396 if (first_overlapping_row == NULL)
24397 first_overlapping_row = row;
24398 last_overlapping_row = row;
24399 }
24400 }
24401
24402 if (y1 >= yb)
24403 break;
24404 }
24405
24406 /* Display the mode line if there is one. */
24407 if (WINDOW_WANTS_MODELINE_P (w)
24408 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
24409 row->enabled_p)
24410 && row->y < r.y + r.height)
24411 {
24412 if (expose_line (w, row, &r))
24413 mouse_face_overwritten_p = 1;
24414 }
24415
24416 if (!w->pseudo_window_p)
24417 {
24418 /* Fix the display of overlapping rows. */
24419 if (first_overlapping_row)
24420 expose_overlaps (w, first_overlapping_row, last_overlapping_row,
24421 fr);
24422
24423 /* Draw border between windows. */
24424 x_draw_vertical_border (w);
24425
24426 /* Turn the cursor on again. */
24427 if (cursor_cleared_p)
24428 update_window_cursor (w, 1);
24429 }
24430 }
24431
24432 return mouse_face_overwritten_p;
24433 }
24434
24435
24436
24437 /* Redraw (parts) of all windows in the window tree rooted at W that
24438 intersect R. R contains frame pixel coordinates. Value is
24439 non-zero if the exposure overwrites mouse-face. */
24440
24441 static int
24442 expose_window_tree (w, r)
24443 struct window *w;
24444 XRectangle *r;
24445 {
24446 struct frame *f = XFRAME (w->frame);
24447 int mouse_face_overwritten_p = 0;
24448
24449 while (w && !FRAME_GARBAGED_P (f))
24450 {
24451 if (!NILP (w->hchild))
24452 mouse_face_overwritten_p
24453 |= expose_window_tree (XWINDOW (w->hchild), r);
24454 else if (!NILP (w->vchild))
24455 mouse_face_overwritten_p
24456 |= expose_window_tree (XWINDOW (w->vchild), r);
24457 else
24458 mouse_face_overwritten_p |= expose_window (w, r);
24459
24460 w = NILP (w->next) ? NULL : XWINDOW (w->next);
24461 }
24462
24463 return mouse_face_overwritten_p;
24464 }
24465
24466
24467 /* EXPORT:
24468 Redisplay an exposed area of frame F. X and Y are the upper-left
24469 corner of the exposed rectangle. W and H are width and height of
24470 the exposed area. All are pixel values. W or H zero means redraw
24471 the entire frame. */
24472
24473 void
24474 expose_frame (f, x, y, w, h)
24475 struct frame *f;
24476 int x, y, w, h;
24477 {
24478 XRectangle r;
24479 int mouse_face_overwritten_p = 0;
24480
24481 TRACE ((stderr, "expose_frame "));
24482
24483 /* No need to redraw if frame will be redrawn soon. */
24484 if (FRAME_GARBAGED_P (f))
24485 {
24486 TRACE ((stderr, " garbaged\n"));
24487 return;
24488 }
24489
24490 /* If basic faces haven't been realized yet, there is no point in
24491 trying to redraw anything. This can happen when we get an expose
24492 event while Emacs is starting, e.g. by moving another window. */
24493 if (FRAME_FACE_CACHE (f) == NULL
24494 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
24495 {
24496 TRACE ((stderr, " no faces\n"));
24497 return;
24498 }
24499
24500 if (w == 0 || h == 0)
24501 {
24502 r.x = r.y = 0;
24503 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
24504 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
24505 }
24506 else
24507 {
24508 r.x = x;
24509 r.y = y;
24510 r.width = w;
24511 r.height = h;
24512 }
24513
24514 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
24515 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
24516
24517 if (WINDOWP (f->tool_bar_window))
24518 mouse_face_overwritten_p
24519 |= expose_window (XWINDOW (f->tool_bar_window), &r);
24520
24521 #ifdef HAVE_X_WINDOWS
24522 #ifndef MSDOS
24523 #ifndef USE_X_TOOLKIT
24524 if (WINDOWP (f->menu_bar_window))
24525 mouse_face_overwritten_p
24526 |= expose_window (XWINDOW (f->menu_bar_window), &r);
24527 #endif /* not USE_X_TOOLKIT */
24528 #endif
24529 #endif
24530
24531 /* Some window managers support a focus-follows-mouse style with
24532 delayed raising of frames. Imagine a partially obscured frame,
24533 and moving the mouse into partially obscured mouse-face on that
24534 frame. The visible part of the mouse-face will be highlighted,
24535 then the WM raises the obscured frame. With at least one WM, KDE
24536 2.1, Emacs is not getting any event for the raising of the frame
24537 (even tried with SubstructureRedirectMask), only Expose events.
24538 These expose events will draw text normally, i.e. not
24539 highlighted. Which means we must redo the highlight here.
24540 Subsume it under ``we love X''. --gerd 2001-08-15 */
24541 /* Included in Windows version because Windows most likely does not
24542 do the right thing if any third party tool offers
24543 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
24544 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
24545 {
24546 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
24547 if (f == dpyinfo->mouse_face_mouse_frame)
24548 {
24549 int x = dpyinfo->mouse_face_mouse_x;
24550 int y = dpyinfo->mouse_face_mouse_y;
24551 clear_mouse_face (dpyinfo);
24552 note_mouse_highlight (f, x, y);
24553 }
24554 }
24555 }
24556
24557
24558 /* EXPORT:
24559 Determine the intersection of two rectangles R1 and R2. Return
24560 the intersection in *RESULT. Value is non-zero if RESULT is not
24561 empty. */
24562
24563 int
24564 x_intersect_rectangles (r1, r2, result)
24565 XRectangle *r1, *r2, *result;
24566 {
24567 XRectangle *left, *right;
24568 XRectangle *upper, *lower;
24569 int intersection_p = 0;
24570
24571 /* Rearrange so that R1 is the left-most rectangle. */
24572 if (r1->x < r2->x)
24573 left = r1, right = r2;
24574 else
24575 left = r2, right = r1;
24576
24577 /* X0 of the intersection is right.x0, if this is inside R1,
24578 otherwise there is no intersection. */
24579 if (right->x <= left->x + left->width)
24580 {
24581 result->x = right->x;
24582
24583 /* The right end of the intersection is the minimum of the
24584 the right ends of left and right. */
24585 result->width = (min (left->x + left->width, right->x + right->width)
24586 - result->x);
24587
24588 /* Same game for Y. */
24589 if (r1->y < r2->y)
24590 upper = r1, lower = r2;
24591 else
24592 upper = r2, lower = r1;
24593
24594 /* The upper end of the intersection is lower.y0, if this is inside
24595 of upper. Otherwise, there is no intersection. */
24596 if (lower->y <= upper->y + upper->height)
24597 {
24598 result->y = lower->y;
24599
24600 /* The lower end of the intersection is the minimum of the lower
24601 ends of upper and lower. */
24602 result->height = (min (lower->y + lower->height,
24603 upper->y + upper->height)
24604 - result->y);
24605 intersection_p = 1;
24606 }
24607 }
24608
24609 return intersection_p;
24610 }
24611
24612 #endif /* HAVE_WINDOW_SYSTEM */
24613
24614 \f
24615 /***********************************************************************
24616 Initialization
24617 ***********************************************************************/
24618
24619 void
24620 syms_of_xdisp ()
24621 {
24622 Vwith_echo_area_save_vector = Qnil;
24623 staticpro (&Vwith_echo_area_save_vector);
24624
24625 Vmessage_stack = Qnil;
24626 staticpro (&Vmessage_stack);
24627
24628 Qinhibit_redisplay = intern ("inhibit-redisplay");
24629 staticpro (&Qinhibit_redisplay);
24630
24631 message_dolog_marker1 = Fmake_marker ();
24632 staticpro (&message_dolog_marker1);
24633 message_dolog_marker2 = Fmake_marker ();
24634 staticpro (&message_dolog_marker2);
24635 message_dolog_marker3 = Fmake_marker ();
24636 staticpro (&message_dolog_marker3);
24637
24638 #if GLYPH_DEBUG
24639 defsubr (&Sdump_frame_glyph_matrix);
24640 defsubr (&Sdump_glyph_matrix);
24641 defsubr (&Sdump_glyph_row);
24642 defsubr (&Sdump_tool_bar_row);
24643 defsubr (&Strace_redisplay);
24644 defsubr (&Strace_to_stderr);
24645 #endif
24646 #ifdef HAVE_WINDOW_SYSTEM
24647 defsubr (&Stool_bar_lines_needed);
24648 defsubr (&Slookup_image_map);
24649 #endif
24650 defsubr (&Sformat_mode_line);
24651 defsubr (&Sinvisible_p);
24652
24653 staticpro (&Qmenu_bar_update_hook);
24654 Qmenu_bar_update_hook = intern ("menu-bar-update-hook");
24655
24656 staticpro (&Qoverriding_terminal_local_map);
24657 Qoverriding_terminal_local_map = intern ("overriding-terminal-local-map");
24658
24659 staticpro (&Qoverriding_local_map);
24660 Qoverriding_local_map = intern ("overriding-local-map");
24661
24662 staticpro (&Qwindow_scroll_functions);
24663 Qwindow_scroll_functions = intern ("window-scroll-functions");
24664
24665 staticpro (&Qwindow_text_change_functions);
24666 Qwindow_text_change_functions = intern ("window-text-change-functions");
24667
24668 staticpro (&Qredisplay_end_trigger_functions);
24669 Qredisplay_end_trigger_functions = intern ("redisplay-end-trigger-functions");
24670
24671 staticpro (&Qinhibit_point_motion_hooks);
24672 Qinhibit_point_motion_hooks = intern ("inhibit-point-motion-hooks");
24673
24674 Qeval = intern ("eval");
24675 staticpro (&Qeval);
24676
24677 QCdata = intern (":data");
24678 staticpro (&QCdata);
24679 Qdisplay = intern ("display");
24680 staticpro (&Qdisplay);
24681 Qspace_width = intern ("space-width");
24682 staticpro (&Qspace_width);
24683 Qraise = intern ("raise");
24684 staticpro (&Qraise);
24685 Qslice = intern ("slice");
24686 staticpro (&Qslice);
24687 Qspace = intern ("space");
24688 staticpro (&Qspace);
24689 Qmargin = intern ("margin");
24690 staticpro (&Qmargin);
24691 Qpointer = intern ("pointer");
24692 staticpro (&Qpointer);
24693 Qleft_margin = intern ("left-margin");
24694 staticpro (&Qleft_margin);
24695 Qright_margin = intern ("right-margin");
24696 staticpro (&Qright_margin);
24697 Qcenter = intern ("center");
24698 staticpro (&Qcenter);
24699 Qline_height = intern ("line-height");
24700 staticpro (&Qline_height);
24701 QCalign_to = intern (":align-to");
24702 staticpro (&QCalign_to);
24703 QCrelative_width = intern (":relative-width");
24704 staticpro (&QCrelative_width);
24705 QCrelative_height = intern (":relative-height");
24706 staticpro (&QCrelative_height);
24707 QCeval = intern (":eval");
24708 staticpro (&QCeval);
24709 QCpropertize = intern (":propertize");
24710 staticpro (&QCpropertize);
24711 QCfile = intern (":file");
24712 staticpro (&QCfile);
24713 Qfontified = intern ("fontified");
24714 staticpro (&Qfontified);
24715 Qfontification_functions = intern ("fontification-functions");
24716 staticpro (&Qfontification_functions);
24717 Qtrailing_whitespace = intern ("trailing-whitespace");
24718 staticpro (&Qtrailing_whitespace);
24719 Qescape_glyph = intern ("escape-glyph");
24720 staticpro (&Qescape_glyph);
24721 Qnobreak_space = intern ("nobreak-space");
24722 staticpro (&Qnobreak_space);
24723 Qimage = intern ("image");
24724 staticpro (&Qimage);
24725 QCmap = intern (":map");
24726 staticpro (&QCmap);
24727 QCpointer = intern (":pointer");
24728 staticpro (&QCpointer);
24729 Qrect = intern ("rect");
24730 staticpro (&Qrect);
24731 Qcircle = intern ("circle");
24732 staticpro (&Qcircle);
24733 Qpoly = intern ("poly");
24734 staticpro (&Qpoly);
24735 Qmessage_truncate_lines = intern ("message-truncate-lines");
24736 staticpro (&Qmessage_truncate_lines);
24737 Qgrow_only = intern ("grow-only");
24738 staticpro (&Qgrow_only);
24739 Qinhibit_menubar_update = intern ("inhibit-menubar-update");
24740 staticpro (&Qinhibit_menubar_update);
24741 Qinhibit_eval_during_redisplay = intern ("inhibit-eval-during-redisplay");
24742 staticpro (&Qinhibit_eval_during_redisplay);
24743 Qposition = intern ("position");
24744 staticpro (&Qposition);
24745 Qbuffer_position = intern ("buffer-position");
24746 staticpro (&Qbuffer_position);
24747 Qobject = intern ("object");
24748 staticpro (&Qobject);
24749 Qbar = intern ("bar");
24750 staticpro (&Qbar);
24751 Qhbar = intern ("hbar");
24752 staticpro (&Qhbar);
24753 Qbox = intern ("box");
24754 staticpro (&Qbox);
24755 Qhollow = intern ("hollow");
24756 staticpro (&Qhollow);
24757 Qhand = intern ("hand");
24758 staticpro (&Qhand);
24759 Qarrow = intern ("arrow");
24760 staticpro (&Qarrow);
24761 Qtext = intern ("text");
24762 staticpro (&Qtext);
24763 Qrisky_local_variable = intern ("risky-local-variable");
24764 staticpro (&Qrisky_local_variable);
24765 Qinhibit_free_realized_faces = intern ("inhibit-free-realized-faces");
24766 staticpro (&Qinhibit_free_realized_faces);
24767
24768 list_of_error = Fcons (Fcons (intern ("error"),
24769 Fcons (intern ("void-variable"), Qnil)),
24770 Qnil);
24771 staticpro (&list_of_error);
24772
24773 Qlast_arrow_position = intern ("last-arrow-position");
24774 staticpro (&Qlast_arrow_position);
24775 Qlast_arrow_string = intern ("last-arrow-string");
24776 staticpro (&Qlast_arrow_string);
24777
24778 Qoverlay_arrow_string = intern ("overlay-arrow-string");
24779 staticpro (&Qoverlay_arrow_string);
24780 Qoverlay_arrow_bitmap = intern ("overlay-arrow-bitmap");
24781 staticpro (&Qoverlay_arrow_bitmap);
24782
24783 echo_buffer[0] = echo_buffer[1] = Qnil;
24784 staticpro (&echo_buffer[0]);
24785 staticpro (&echo_buffer[1]);
24786
24787 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
24788 staticpro (&echo_area_buffer[0]);
24789 staticpro (&echo_area_buffer[1]);
24790
24791 Vmessages_buffer_name = build_string ("*Messages*");
24792 staticpro (&Vmessages_buffer_name);
24793
24794 mode_line_proptrans_alist = Qnil;
24795 staticpro (&mode_line_proptrans_alist);
24796 mode_line_string_list = Qnil;
24797 staticpro (&mode_line_string_list);
24798 mode_line_string_face = Qnil;
24799 staticpro (&mode_line_string_face);
24800 mode_line_string_face_prop = Qnil;
24801 staticpro (&mode_line_string_face_prop);
24802 Vmode_line_unwind_vector = Qnil;
24803 staticpro (&Vmode_line_unwind_vector);
24804
24805 help_echo_string = Qnil;
24806 staticpro (&help_echo_string);
24807 help_echo_object = Qnil;
24808 staticpro (&help_echo_object);
24809 help_echo_window = Qnil;
24810 staticpro (&help_echo_window);
24811 previous_help_echo_string = Qnil;
24812 staticpro (&previous_help_echo_string);
24813 help_echo_pos = -1;
24814
24815 #ifdef HAVE_WINDOW_SYSTEM
24816 DEFVAR_BOOL ("x-stretch-cursor", &x_stretch_cursor_p,
24817 doc: /* *Non-nil means draw block cursor as wide as the glyph under it.
24818 For example, if a block cursor is over a tab, it will be drawn as
24819 wide as that tab on the display. */);
24820 x_stretch_cursor_p = 0;
24821 #endif
24822
24823 DEFVAR_LISP ("show-trailing-whitespace", &Vshow_trailing_whitespace,
24824 doc: /* *Non-nil means highlight trailing whitespace.
24825 The face used for trailing whitespace is `trailing-whitespace'. */);
24826 Vshow_trailing_whitespace = Qnil;
24827
24828 DEFVAR_LISP ("nobreak-char-display", &Vnobreak_char_display,
24829 doc: /* *Control highlighting of nobreak space and soft hyphen.
24830 A value of t means highlight the character itself (for nobreak space,
24831 use face `nobreak-space').
24832 A value of nil means no highlighting.
24833 Other values mean display the escape glyph followed by an ordinary
24834 space or ordinary hyphen. */);
24835 Vnobreak_char_display = Qt;
24836
24837 DEFVAR_LISP ("void-text-area-pointer", &Vvoid_text_area_pointer,
24838 doc: /* *The pointer shape to show in void text areas.
24839 A value of nil means to show the text pointer. Other options are `arrow',
24840 `text', `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
24841 Vvoid_text_area_pointer = Qarrow;
24842
24843 DEFVAR_LISP ("inhibit-redisplay", &Vinhibit_redisplay,
24844 doc: /* Non-nil means don't actually do any redisplay.
24845 This is used for internal purposes. */);
24846 Vinhibit_redisplay = Qnil;
24847
24848 DEFVAR_LISP ("global-mode-string", &Vglobal_mode_string,
24849 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
24850 Vglobal_mode_string = Qnil;
24851
24852 DEFVAR_LISP ("overlay-arrow-position", &Voverlay_arrow_position,
24853 doc: /* Marker for where to display an arrow on top of the buffer text.
24854 This must be the beginning of a line in order to work.
24855 See also `overlay-arrow-string'. */);
24856 Voverlay_arrow_position = Qnil;
24857
24858 DEFVAR_LISP ("overlay-arrow-string", &Voverlay_arrow_string,
24859 doc: /* String to display as an arrow in non-window frames.
24860 See also `overlay-arrow-position'. */);
24861 Voverlay_arrow_string = build_string ("=>");
24862
24863 DEFVAR_LISP ("overlay-arrow-variable-list", &Voverlay_arrow_variable_list,
24864 doc: /* List of variables (symbols) which hold markers for overlay arrows.
24865 The symbols on this list are examined during redisplay to determine
24866 where to display overlay arrows. */);
24867 Voverlay_arrow_variable_list
24868 = Fcons (intern ("overlay-arrow-position"), Qnil);
24869
24870 DEFVAR_INT ("scroll-step", &scroll_step,
24871 doc: /* *The number of lines to try scrolling a window by when point moves out.
24872 If that fails to bring point back on frame, point is centered instead.
24873 If this is zero, point is always centered after it moves off frame.
24874 If you want scrolling to always be a line at a time, you should set
24875 `scroll-conservatively' to a large value rather than set this to 1. */);
24876
24877 DEFVAR_INT ("scroll-conservatively", &scroll_conservatively,
24878 doc: /* *Scroll up to this many lines, to bring point back on screen.
24879 If point moves off-screen, redisplay will scroll by up to
24880 `scroll-conservatively' lines in order to bring point just barely
24881 onto the screen again. If that cannot be done, then redisplay
24882 recenters point as usual.
24883
24884 A value of zero means always recenter point if it moves off screen. */);
24885 scroll_conservatively = 0;
24886
24887 DEFVAR_INT ("scroll-margin", &scroll_margin,
24888 doc: /* *Number of lines of margin at the top and bottom of a window.
24889 Recenter the window whenever point gets within this many lines
24890 of the top or bottom of the window. */);
24891 scroll_margin = 0;
24892
24893 DEFVAR_LISP ("display-pixels-per-inch", &Vdisplay_pixels_per_inch,
24894 doc: /* Pixels per inch value for non-window system displays.
24895 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
24896 Vdisplay_pixels_per_inch = make_float (72.0);
24897
24898 #if GLYPH_DEBUG
24899 DEFVAR_INT ("debug-end-pos", &debug_end_pos, doc: /* Don't ask. */);
24900 #endif
24901
24902 DEFVAR_LISP ("truncate-partial-width-windows",
24903 &Vtruncate_partial_width_windows,
24904 doc: /* Non-nil means truncate lines in windows with less than the frame width.
24905 For an integer value, truncate lines in each window with less than the
24906 full frame width, provided the window width is less than that integer;
24907 otherwise, respect the value of `truncate-lines'.
24908
24909 For any other non-nil value, truncate lines in all windows with
24910 less than the full frame width.
24911
24912 A value of nil means to respect the value of `truncate-lines'. */);
24913 Vtruncate_partial_width_windows = make_number (30);
24914
24915 DEFVAR_BOOL ("mode-line-inverse-video", &mode_line_inverse_video,
24916 doc: /* When nil, display the mode-line/header-line/menu-bar in the default face.
24917 Any other value means to use the appropriate face, `mode-line',
24918 `header-line', or `menu' respectively. */);
24919 mode_line_inverse_video = 1;
24920
24921 DEFVAR_LISP ("line-number-display-limit", &Vline_number_display_limit,
24922 doc: /* *Maximum buffer size for which line number should be displayed.
24923 If the buffer is bigger than this, the line number does not appear
24924 in the mode line. A value of nil means no limit. */);
24925 Vline_number_display_limit = Qnil;
24926
24927 DEFVAR_INT ("line-number-display-limit-width",
24928 &line_number_display_limit_width,
24929 doc: /* *Maximum line width (in characters) for line number display.
24930 If the average length of the lines near point is bigger than this, then the
24931 line number may be omitted from the mode line. */);
24932 line_number_display_limit_width = 200;
24933
24934 DEFVAR_BOOL ("highlight-nonselected-windows", &highlight_nonselected_windows,
24935 doc: /* *Non-nil means highlight region even in nonselected windows. */);
24936 highlight_nonselected_windows = 0;
24937
24938 DEFVAR_BOOL ("multiple-frames", &multiple_frames,
24939 doc: /* Non-nil if more than one frame is visible on this display.
24940 Minibuffer-only frames don't count, but iconified frames do.
24941 This variable is not guaranteed to be accurate except while processing
24942 `frame-title-format' and `icon-title-format'. */);
24943
24944 DEFVAR_LISP ("frame-title-format", &Vframe_title_format,
24945 doc: /* Template for displaying the title bar of visible frames.
24946 \(Assuming the window manager supports this feature.)
24947
24948 This variable has the same structure as `mode-line-format', except that
24949 the %c and %l constructs are ignored. It is used only on frames for
24950 which no explicit name has been set \(see `modify-frame-parameters'). */);
24951
24952 DEFVAR_LISP ("icon-title-format", &Vicon_title_format,
24953 doc: /* Template for displaying the title bar of an iconified frame.
24954 \(Assuming the window manager supports this feature.)
24955 This variable has the same structure as `mode-line-format' (which see),
24956 and is used only on frames for which no explicit name has been set
24957 \(see `modify-frame-parameters'). */);
24958 Vicon_title_format
24959 = Vframe_title_format
24960 = Fcons (intern ("multiple-frames"),
24961 Fcons (build_string ("%b"),
24962 Fcons (Fcons (empty_unibyte_string,
24963 Fcons (intern ("invocation-name"),
24964 Fcons (build_string ("@"),
24965 Fcons (intern ("system-name"),
24966 Qnil)))),
24967 Qnil)));
24968
24969 DEFVAR_LISP ("message-log-max", &Vmessage_log_max,
24970 doc: /* Maximum number of lines to keep in the message log buffer.
24971 If nil, disable message logging. If t, log messages but don't truncate
24972 the buffer when it becomes large. */);
24973 Vmessage_log_max = make_number (100);
24974
24975 DEFVAR_LISP ("window-size-change-functions", &Vwindow_size_change_functions,
24976 doc: /* Functions called before redisplay, if window sizes have changed.
24977 The value should be a list of functions that take one argument.
24978 Just before redisplay, for each frame, if any of its windows have changed
24979 size since the last redisplay, or have been split or deleted,
24980 all the functions in the list are called, with the frame as argument. */);
24981 Vwindow_size_change_functions = Qnil;
24982
24983 DEFVAR_LISP ("window-scroll-functions", &Vwindow_scroll_functions,
24984 doc: /* List of functions to call before redisplaying a window with scrolling.
24985 Each function is called with two arguments, the window
24986 and its new display-start position. Note that the value of `window-end'
24987 is not valid when these functions are called. */);
24988 Vwindow_scroll_functions = Qnil;
24989
24990 DEFVAR_LISP ("window-text-change-functions",
24991 &Vwindow_text_change_functions,
24992 doc: /* Functions to call in redisplay when text in the window might change. */);
24993 Vwindow_text_change_functions = Qnil;
24994
24995 DEFVAR_LISP ("redisplay-end-trigger-functions", &Vredisplay_end_trigger_functions,
24996 doc: /* Functions called when redisplay of a window reaches the end trigger.
24997 Each function is called with two arguments, the window and the end trigger value.
24998 See `set-window-redisplay-end-trigger'. */);
24999 Vredisplay_end_trigger_functions = Qnil;
25000
25001 DEFVAR_LISP ("mouse-autoselect-window", &Vmouse_autoselect_window,
25002 doc: /* *Non-nil means autoselect window with mouse pointer.
25003 If nil, do not autoselect windows.
25004 A positive number means delay autoselection by that many seconds: a
25005 window is autoselected only after the mouse has remained in that
25006 window for the duration of the delay.
25007 A negative number has a similar effect, but causes windows to be
25008 autoselected only after the mouse has stopped moving. \(Because of
25009 the way Emacs compares mouse events, you will occasionally wait twice
25010 that time before the window gets selected.\)
25011 Any other value means to autoselect window instantaneously when the
25012 mouse pointer enters it.
25013
25014 Autoselection selects the minibuffer only if it is active, and never
25015 unselects the minibuffer if it is active.
25016
25017 When customizing this variable make sure that the actual value of
25018 `focus-follows-mouse' matches the behavior of your window manager. */);
25019 Vmouse_autoselect_window = Qnil;
25020
25021 DEFVAR_LISP ("auto-resize-tool-bars", &Vauto_resize_tool_bars,
25022 doc: /* *Non-nil means automatically resize tool-bars.
25023 This dynamically changes the tool-bar's height to the minimum height
25024 that is needed to make all tool-bar items visible.
25025 If value is `grow-only', the tool-bar's height is only increased
25026 automatically; to decrease the tool-bar height, use \\[recenter]. */);
25027 Vauto_resize_tool_bars = Qt;
25028
25029 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", &auto_raise_tool_bar_buttons_p,
25030 doc: /* *Non-nil means raise tool-bar buttons when the mouse moves over them. */);
25031 auto_raise_tool_bar_buttons_p = 1;
25032
25033 DEFVAR_BOOL ("make-cursor-line-fully-visible", &make_cursor_line_fully_visible_p,
25034 doc: /* *Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
25035 make_cursor_line_fully_visible_p = 1;
25036
25037 DEFVAR_LISP ("tool-bar-border", &Vtool_bar_border,
25038 doc: /* *Border below tool-bar in pixels.
25039 If an integer, use it as the height of the border.
25040 If it is one of `internal-border-width' or `border-width', use the
25041 value of the corresponding frame parameter.
25042 Otherwise, no border is added below the tool-bar. */);
25043 Vtool_bar_border = Qinternal_border_width;
25044
25045 DEFVAR_LISP ("tool-bar-button-margin", &Vtool_bar_button_margin,
25046 doc: /* *Margin around tool-bar buttons in pixels.
25047 If an integer, use that for both horizontal and vertical margins.
25048 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
25049 HORZ specifying the horizontal margin, and VERT specifying the
25050 vertical margin. */);
25051 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
25052
25053 DEFVAR_INT ("tool-bar-button-relief", &tool_bar_button_relief,
25054 doc: /* *Relief thickness of tool-bar buttons. */);
25055 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
25056
25057 DEFVAR_LISP ("fontification-functions", &Vfontification_functions,
25058 doc: /* List of functions to call to fontify regions of text.
25059 Each function is called with one argument POS. Functions must
25060 fontify a region starting at POS in the current buffer, and give
25061 fontified regions the property `fontified'. */);
25062 Vfontification_functions = Qnil;
25063 Fmake_variable_buffer_local (Qfontification_functions);
25064
25065 DEFVAR_BOOL ("unibyte-display-via-language-environment",
25066 &unibyte_display_via_language_environment,
25067 doc: /* *Non-nil means display unibyte text according to language environment.
25068 Specifically this means that unibyte non-ASCII characters
25069 are displayed by converting them to the equivalent multibyte characters
25070 according to the current language environment. As a result, they are
25071 displayed according to the current fontset. */);
25072 unibyte_display_via_language_environment = 0;
25073
25074 DEFVAR_LISP ("max-mini-window-height", &Vmax_mini_window_height,
25075 doc: /* *Maximum height for resizing mini-windows.
25076 If a float, it specifies a fraction of the mini-window frame's height.
25077 If an integer, it specifies a number of lines. */);
25078 Vmax_mini_window_height = make_float (0.25);
25079
25080 DEFVAR_LISP ("resize-mini-windows", &Vresize_mini_windows,
25081 doc: /* *How to resize mini-windows.
25082 A value of nil means don't automatically resize mini-windows.
25083 A value of t means resize them to fit the text displayed in them.
25084 A value of `grow-only', the default, means let mini-windows grow
25085 only, until their display becomes empty, at which point the windows
25086 go back to their normal size. */);
25087 Vresize_mini_windows = Qgrow_only;
25088
25089 DEFVAR_LISP ("blink-cursor-alist", &Vblink_cursor_alist,
25090 doc: /* Alist specifying how to blink the cursor off.
25091 Each element has the form (ON-STATE . OFF-STATE). Whenever the
25092 `cursor-type' frame-parameter or variable equals ON-STATE,
25093 comparing using `equal', Emacs uses OFF-STATE to specify
25094 how to blink it off. ON-STATE and OFF-STATE are values for
25095 the `cursor-type' frame parameter.
25096
25097 If a frame's ON-STATE has no entry in this list,
25098 the frame's other specifications determine how to blink the cursor off. */);
25099 Vblink_cursor_alist = Qnil;
25100
25101 DEFVAR_BOOL ("auto-hscroll-mode", &automatic_hscrolling_p,
25102 doc: /* *Non-nil means scroll the display automatically to make point visible. */);
25103 automatic_hscrolling_p = 1;
25104 Qauto_hscroll_mode = intern ("auto-hscroll-mode");
25105 staticpro (&Qauto_hscroll_mode);
25106
25107 DEFVAR_INT ("hscroll-margin", &hscroll_margin,
25108 doc: /* *How many columns away from the window edge point is allowed to get
25109 before automatic hscrolling will horizontally scroll the window. */);
25110 hscroll_margin = 5;
25111
25112 DEFVAR_LISP ("hscroll-step", &Vhscroll_step,
25113 doc: /* *How many columns to scroll the window when point gets too close to the edge.
25114 When point is less than `hscroll-margin' columns from the window
25115 edge, automatic hscrolling will scroll the window by the amount of columns
25116 determined by this variable. If its value is a positive integer, scroll that
25117 many columns. If it's a positive floating-point number, it specifies the
25118 fraction of the window's width to scroll. If it's nil or zero, point will be
25119 centered horizontally after the scroll. Any other value, including negative
25120 numbers, are treated as if the value were zero.
25121
25122 Automatic hscrolling always moves point outside the scroll margin, so if
25123 point was more than scroll step columns inside the margin, the window will
25124 scroll more than the value given by the scroll step.
25125
25126 Note that the lower bound for automatic hscrolling specified by `scroll-left'
25127 and `scroll-right' overrides this variable's effect. */);
25128 Vhscroll_step = make_number (0);
25129
25130 DEFVAR_BOOL ("message-truncate-lines", &message_truncate_lines,
25131 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
25132 Bind this around calls to `message' to let it take effect. */);
25133 message_truncate_lines = 0;
25134
25135 DEFVAR_LISP ("menu-bar-update-hook", &Vmenu_bar_update_hook,
25136 doc: /* Normal hook run to update the menu bar definitions.
25137 Redisplay runs this hook before it redisplays the menu bar.
25138 This is used to update submenus such as Buffers,
25139 whose contents depend on various data. */);
25140 Vmenu_bar_update_hook = Qnil;
25141
25142 DEFVAR_LISP ("menu-updating-frame", &Vmenu_updating_frame,
25143 doc: /* Frame for which we are updating a menu.
25144 The enable predicate for a menu binding should check this variable. */);
25145 Vmenu_updating_frame = Qnil;
25146
25147 DEFVAR_BOOL ("inhibit-menubar-update", &inhibit_menubar_update,
25148 doc: /* Non-nil means don't update menu bars. Internal use only. */);
25149 inhibit_menubar_update = 0;
25150
25151 DEFVAR_LISP ("wrap-prefix", &Vwrap_prefix,
25152 doc: /* Prefix added to the beginning of all continuation lines at display-time.
25153 May be a string, an image, or a stretch-glyph such as used by the
25154 `display' text-property.
25155
25156 This variable is overridden by any `wrap-prefix' text-property.
25157
25158 To add a prefix to non-continuation lines, use the `line-prefix' variable. */);
25159 Vwrap_prefix = Qnil;
25160 staticpro (&Qwrap_prefix);
25161 Qwrap_prefix = intern ("wrap-prefix");
25162 Fmake_variable_buffer_local (Qwrap_prefix);
25163
25164 DEFVAR_LISP ("line-prefix", &Vline_prefix,
25165 doc: /* Prefix added to the beginning of all non-continuation lines at display-time.
25166 May be a string, an image, or a stretch-glyph such as used by the
25167 `display' text-property.
25168
25169 This variable is overridden by any `line-prefix' text-property.
25170
25171 To add a prefix to continuation lines, use the `wrap-prefix' variable. */);
25172 Vline_prefix = Qnil;
25173 staticpro (&Qline_prefix);
25174 Qline_prefix = intern ("line-prefix");
25175 Fmake_variable_buffer_local (Qline_prefix);
25176
25177 DEFVAR_BOOL ("inhibit-eval-during-redisplay", &inhibit_eval_during_redisplay,
25178 doc: /* Non-nil means don't eval Lisp during redisplay. */);
25179 inhibit_eval_during_redisplay = 0;
25180
25181 DEFVAR_BOOL ("inhibit-free-realized-faces", &inhibit_free_realized_faces,
25182 doc: /* Non-nil means don't free realized faces. Internal use only. */);
25183 inhibit_free_realized_faces = 0;
25184
25185 #if GLYPH_DEBUG
25186 DEFVAR_BOOL ("inhibit-try-window-id", &inhibit_try_window_id,
25187 doc: /* Inhibit try_window_id display optimization. */);
25188 inhibit_try_window_id = 0;
25189
25190 DEFVAR_BOOL ("inhibit-try-window-reusing", &inhibit_try_window_reusing,
25191 doc: /* Inhibit try_window_reusing display optimization. */);
25192 inhibit_try_window_reusing = 0;
25193
25194 DEFVAR_BOOL ("inhibit-try-cursor-movement", &inhibit_try_cursor_movement,
25195 doc: /* Inhibit try_cursor_movement display optimization. */);
25196 inhibit_try_cursor_movement = 0;
25197 #endif /* GLYPH_DEBUG */
25198
25199 DEFVAR_INT ("overline-margin", &overline_margin,
25200 doc: /* *Space between overline and text, in pixels.
25201 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
25202 margin to the caracter height. */);
25203 overline_margin = 2;
25204
25205 DEFVAR_INT ("underline-minimum-offset",
25206 &underline_minimum_offset,
25207 doc: /* Minimum distance between baseline and underline.
25208 This can improve legibility of underlined text at small font sizes,
25209 particularly when using variable `x-use-underline-position-properties'
25210 with fonts that specify an UNDERLINE_POSITION relatively close to the
25211 baseline. The default value is 1. */);
25212 underline_minimum_offset = 1;
25213
25214 DEFVAR_BOOL ("display-hourglass", &display_hourglass_p,
25215 doc: /* Non-zero means Emacs displays an hourglass pointer on window systems. */);
25216 display_hourglass_p = 1;
25217
25218 DEFVAR_LISP ("hourglass-delay", &Vhourglass_delay,
25219 doc: /* *Seconds to wait before displaying an hourglass pointer.
25220 Value must be an integer or float. */);
25221 Vhourglass_delay = make_number (DEFAULT_HOURGLASS_DELAY);
25222
25223 hourglass_atimer = NULL;
25224 hourglass_shown_p = 0;
25225 }
25226
25227
25228 /* Initialize this module when Emacs starts. */
25229
25230 void
25231 init_xdisp ()
25232 {
25233 Lisp_Object root_window;
25234 struct window *mini_w;
25235
25236 current_header_line_height = current_mode_line_height = -1;
25237
25238 CHARPOS (this_line_start_pos) = 0;
25239
25240 mini_w = XWINDOW (minibuf_window);
25241 root_window = FRAME_ROOT_WINDOW (XFRAME (WINDOW_FRAME (mini_w)));
25242
25243 if (!noninteractive)
25244 {
25245 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (root_window)));
25246 int i;
25247
25248 XWINDOW (root_window)->top_line = make_number (FRAME_TOP_MARGIN (f));
25249 set_window_height (root_window,
25250 FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f),
25251 0);
25252 mini_w->top_line = make_number (FRAME_LINES (f) - 1);
25253 set_window_height (minibuf_window, 1, 0);
25254
25255 XWINDOW (root_window)->total_cols = make_number (FRAME_COLS (f));
25256 mini_w->total_cols = make_number (FRAME_COLS (f));
25257
25258 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
25259 scratch_glyph_row.glyphs[TEXT_AREA + 1]
25260 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
25261
25262 /* The default ellipsis glyphs `...'. */
25263 for (i = 0; i < 3; ++i)
25264 default_invis_vector[i] = make_number ('.');
25265 }
25266
25267 {
25268 /* Allocate the buffer for frame titles.
25269 Also used for `format-mode-line'. */
25270 int size = 100;
25271 mode_line_noprop_buf = (char *) xmalloc (size);
25272 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
25273 mode_line_noprop_ptr = mode_line_noprop_buf;
25274 mode_line_target = MODE_LINE_DISPLAY;
25275 }
25276
25277 help_echo_showing_p = 0;
25278 }
25279
25280 /* Since w32 does not support atimers, it defines its own implementation of
25281 the following three functions in w32fns.c. */
25282 #ifndef WINDOWSNT
25283
25284 /* Platform-independent portion of hourglass implementation. */
25285
25286 /* Return non-zero if houglass timer has been started or hourglass is shown. */
25287 int
25288 hourglass_started ()
25289 {
25290 return hourglass_shown_p || hourglass_atimer != NULL;
25291 }
25292
25293 /* Cancel a currently active hourglass timer, and start a new one. */
25294 void
25295 start_hourglass ()
25296 {
25297 #if defined (HAVE_WINDOW_SYSTEM)
25298 EMACS_TIME delay;
25299 int secs, usecs = 0;
25300
25301 cancel_hourglass ();
25302
25303 if (INTEGERP (Vhourglass_delay)
25304 && XINT (Vhourglass_delay) > 0)
25305 secs = XFASTINT (Vhourglass_delay);
25306 else if (FLOATP (Vhourglass_delay)
25307 && XFLOAT_DATA (Vhourglass_delay) > 0)
25308 {
25309 Lisp_Object tem;
25310 tem = Ftruncate (Vhourglass_delay, Qnil);
25311 secs = XFASTINT (tem);
25312 usecs = (XFLOAT_DATA (Vhourglass_delay) - secs) * 1000000;
25313 }
25314 else
25315 secs = DEFAULT_HOURGLASS_DELAY;
25316
25317 EMACS_SET_SECS_USECS (delay, secs, usecs);
25318 hourglass_atimer = start_atimer (ATIMER_RELATIVE, delay,
25319 show_hourglass, NULL);
25320 #endif
25321 }
25322
25323
25324 /* Cancel the hourglass cursor timer if active, hide a busy cursor if
25325 shown. */
25326 void
25327 cancel_hourglass ()
25328 {
25329 #if defined (HAVE_WINDOW_SYSTEM)
25330 if (hourglass_atimer)
25331 {
25332 cancel_atimer (hourglass_atimer);
25333 hourglass_atimer = NULL;
25334 }
25335
25336 if (hourglass_shown_p)
25337 hide_hourglass ();
25338 #endif
25339 }
25340 #endif /* ! WINDOWSNT */
25341
25342 /* arch-tag: eacc864d-bb6a-4b74-894a-1a4399a1358b
25343 (do not change this comment) */