]> code.delx.au - gnu-emacs/blob - src/xdisp.c
* xdisp.c (fill_glyph_string): Fix typo in source (though the
[gnu-emacs] / src / xdisp.c
1 /* Display generation from window structure and buffer text.
2 Copyright (C) 1985, 1986, 1987, 1988, 1993, 1994, 1995,
3 1997, 1998, 1999, 2000, 2001, 2002, 2003,
4 2004, 2005, 2006, 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 "font.h"
192 #include "fontset.h"
193 #include "blockinput.h"
194
195 #ifdef HAVE_X_WINDOWS
196 #include "xterm.h"
197 #endif
198 #ifdef WINDOWSNT
199 #include "w32term.h"
200 #endif
201 #ifdef HAVE_NS
202 #include "nsterm.h"
203 #endif
204 #ifdef USE_GTK
205 #include "gtkutil.h"
206 #endif
207
208 #include "font.h"
209
210 #ifndef FRAME_X_OUTPUT
211 #define FRAME_X_OUTPUT(f) ((f)->output_data.x)
212 #endif
213
214 #define INFINITY 10000000
215
216 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
217 || defined(HAVE_NS) || defined (USE_GTK)
218 extern void set_frame_menubar P_ ((struct frame *f, int, int));
219 extern int pending_menu_activation;
220 #endif
221
222 extern int interrupt_input;
223 extern int command_loop_level;
224
225 extern Lisp_Object do_mouse_tracking;
226
227 extern int minibuffer_auto_raise;
228 extern Lisp_Object Vminibuffer_list;
229
230 extern Lisp_Object Qface;
231 extern Lisp_Object Qmode_line, Qmode_line_inactive, Qheader_line;
232
233 extern Lisp_Object Voverriding_local_map;
234 extern Lisp_Object Voverriding_local_map_menu_flag;
235 extern Lisp_Object Qmenu_item;
236 extern Lisp_Object Qwhen;
237 extern Lisp_Object Qhelp_echo;
238
239 Lisp_Object Qoverriding_local_map, Qoverriding_terminal_local_map;
240 Lisp_Object Qwindow_scroll_functions, Vwindow_scroll_functions;
241 Lisp_Object Qwindow_text_change_functions, Vwindow_text_change_functions;
242 Lisp_Object Qredisplay_end_trigger_functions, Vredisplay_end_trigger_functions;
243 Lisp_Object Qinhibit_point_motion_hooks;
244 Lisp_Object QCeval, QCfile, QCdata, QCpropertize;
245 Lisp_Object Qfontified;
246 Lisp_Object Qgrow_only;
247 Lisp_Object Qinhibit_eval_during_redisplay;
248 Lisp_Object Qbuffer_position, Qposition, Qobject;
249
250 /* Cursor shapes */
251 Lisp_Object Qbar, Qhbar, Qbox, Qhollow;
252
253 /* Pointer shapes */
254 Lisp_Object Qarrow, Qhand, Qtext;
255
256 Lisp_Object Qrisky_local_variable;
257
258 /* Holds the list (error). */
259 Lisp_Object list_of_error;
260
261 /* Functions called to fontify regions of text. */
262
263 Lisp_Object Vfontification_functions;
264 Lisp_Object Qfontification_functions;
265
266 /* Non-nil means automatically select any window when the mouse
267 cursor moves into it. */
268 Lisp_Object Vmouse_autoselect_window;
269
270 Lisp_Object Vwrap_prefix, Qwrap_prefix;
271 Lisp_Object Vline_prefix, Qline_prefix;
272
273 /* Non-zero means draw tool bar buttons raised when the mouse moves
274 over them. */
275
276 int auto_raise_tool_bar_buttons_p;
277
278 /* Non-zero means to reposition window if cursor line is only partially visible. */
279
280 int make_cursor_line_fully_visible_p;
281
282 /* Margin below tool bar in pixels. 0 or nil means no margin.
283 If value is `internal-border-width' or `border-width',
284 the corresponding frame parameter is used. */
285
286 Lisp_Object Vtool_bar_border;
287
288 /* Margin around tool bar buttons in pixels. */
289
290 Lisp_Object Vtool_bar_button_margin;
291
292 /* Thickness of shadow to draw around tool bar buttons. */
293
294 EMACS_INT tool_bar_button_relief;
295
296 /* Non-nil means automatically resize tool-bars so that all tool-bar
297 items are visible, and no blank lines remain.
298
299 If value is `grow-only', only make tool-bar bigger. */
300
301 Lisp_Object Vauto_resize_tool_bars;
302
303 /* Non-zero means draw block and hollow cursor as wide as the glyph
304 under it. For example, if a block cursor is over a tab, it will be
305 drawn as wide as that tab on the display. */
306
307 int x_stretch_cursor_p;
308
309 /* Non-nil means don't actually do any redisplay. */
310
311 Lisp_Object Vinhibit_redisplay, Qinhibit_redisplay;
312
313 /* Non-zero means Lisp evaluation during redisplay is inhibited. */
314
315 int inhibit_eval_during_redisplay;
316
317 /* Names of text properties relevant for redisplay. */
318
319 Lisp_Object Qdisplay;
320 extern Lisp_Object Qface, Qinvisible, Qwidth;
321
322 /* Symbols used in text property values. */
323
324 Lisp_Object Vdisplay_pixels_per_inch;
325 Lisp_Object Qspace, QCalign_to, QCrelative_width, QCrelative_height;
326 Lisp_Object Qleft_margin, Qright_margin, Qspace_width, Qraise;
327 Lisp_Object Qslice;
328 Lisp_Object Qcenter;
329 Lisp_Object Qmargin, Qpointer;
330 Lisp_Object Qline_height;
331 extern Lisp_Object Qheight;
332 extern Lisp_Object QCwidth, QCheight, QCascent;
333 extern Lisp_Object Qscroll_bar;
334 extern Lisp_Object Qcursor;
335
336 /* Non-nil means highlight trailing whitespace. */
337
338 Lisp_Object Vshow_trailing_whitespace;
339
340 /* Non-nil means escape non-break space and hyphens. */
341
342 Lisp_Object Vnobreak_char_display;
343
344 #ifdef HAVE_WINDOW_SYSTEM
345 extern Lisp_Object Voverflow_newline_into_fringe;
346
347 /* Test if overflow newline into fringe. Called with iterator IT
348 at or past right window margin, and with IT->current_x set. */
349
350 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(it) \
351 (!NILP (Voverflow_newline_into_fringe) \
352 && FRAME_WINDOW_P (it->f) \
353 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) > 0 \
354 && it->current_x == it->last_visible_x \
355 && it->line_wrap != WORD_WRAP)
356
357 #endif /* HAVE_WINDOW_SYSTEM */
358
359 /* Test if the display element loaded in IT is a space or tab
360 character. This is used to determine word wrapping. */
361
362 #define IT_DISPLAYING_WHITESPACE(it) \
363 (it->what == IT_CHARACTER && (it->c == ' ' || it->c == '\t'))
364
365 /* Non-nil means show the text cursor in void text areas
366 i.e. in blank areas after eol and eob. This used to be
367 the default in 21.3. */
368
369 Lisp_Object Vvoid_text_area_pointer;
370
371 /* Name of the face used to highlight trailing whitespace. */
372
373 Lisp_Object Qtrailing_whitespace;
374
375 /* Name and number of the face used to highlight escape glyphs. */
376
377 Lisp_Object Qescape_glyph;
378
379 /* Name and number of the face used to highlight non-breaking spaces. */
380
381 Lisp_Object Qnobreak_space;
382
383 /* The symbol `image' which is the car of the lists used to represent
384 images in Lisp. */
385
386 Lisp_Object Qimage;
387
388 /* The image map types. */
389 Lisp_Object QCmap, QCpointer;
390 Lisp_Object Qrect, Qcircle, Qpoly;
391
392 /* Non-zero means print newline to stdout before next mini-buffer
393 message. */
394
395 int noninteractive_need_newline;
396
397 /* Non-zero means print newline to message log before next message. */
398
399 static int message_log_need_newline;
400
401 /* Three markers that message_dolog uses.
402 It could allocate them itself, but that causes trouble
403 in handling memory-full errors. */
404 static Lisp_Object message_dolog_marker1;
405 static Lisp_Object message_dolog_marker2;
406 static Lisp_Object message_dolog_marker3;
407 \f
408 /* The buffer position of the first character appearing entirely or
409 partially on the line of the selected window which contains the
410 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
411 redisplay optimization in redisplay_internal. */
412
413 static struct text_pos this_line_start_pos;
414
415 /* Number of characters past the end of the line above, including the
416 terminating newline. */
417
418 static struct text_pos this_line_end_pos;
419
420 /* The vertical positions and the height of this line. */
421
422 static int this_line_vpos;
423 static int this_line_y;
424 static int this_line_pixel_height;
425
426 /* X position at which this display line starts. Usually zero;
427 negative if first character is partially visible. */
428
429 static int this_line_start_x;
430
431 /* Buffer that this_line_.* variables are referring to. */
432
433 static struct buffer *this_line_buffer;
434
435 /* Nonzero means truncate lines in all windows less wide than the
436 frame. */
437
438 Lisp_Object Vtruncate_partial_width_windows;
439
440 /* A flag to control how to display unibyte 8-bit character. */
441
442 int unibyte_display_via_language_environment;
443
444 /* Nonzero means we have more than one non-mini-buffer-only frame.
445 Not guaranteed to be accurate except while parsing
446 frame-title-format. */
447
448 int multiple_frames;
449
450 Lisp_Object Vglobal_mode_string;
451
452
453 /* List of variables (symbols) which hold markers for overlay arrows.
454 The symbols on this list are examined during redisplay to determine
455 where to display overlay arrows. */
456
457 Lisp_Object Voverlay_arrow_variable_list;
458
459 /* Marker for where to display an arrow on top of the buffer text. */
460
461 Lisp_Object Voverlay_arrow_position;
462
463 /* String to display for the arrow. Only used on terminal frames. */
464
465 Lisp_Object Voverlay_arrow_string;
466
467 /* Values of those variables at last redisplay are stored as
468 properties on `overlay-arrow-position' symbol. However, if
469 Voverlay_arrow_position is a marker, last-arrow-position is its
470 numerical position. */
471
472 Lisp_Object Qlast_arrow_position, Qlast_arrow_string;
473
474 /* Alternative overlay-arrow-string and overlay-arrow-bitmap
475 properties on a symbol in overlay-arrow-variable-list. */
476
477 Lisp_Object Qoverlay_arrow_string, Qoverlay_arrow_bitmap;
478
479 /* Like mode-line-format, but for the title bar on a visible frame. */
480
481 Lisp_Object Vframe_title_format;
482
483 /* Like mode-line-format, but for the title bar on an iconified frame. */
484
485 Lisp_Object Vicon_title_format;
486
487 /* List of functions to call when a window's size changes. These
488 functions get one arg, a frame on which one or more windows' sizes
489 have changed. */
490
491 static Lisp_Object Vwindow_size_change_functions;
492
493 Lisp_Object Qmenu_bar_update_hook, Vmenu_bar_update_hook;
494
495 /* Nonzero if an overlay arrow has been displayed in this window. */
496
497 static int overlay_arrow_seen;
498
499 /* Nonzero means highlight the region even in nonselected windows. */
500
501 int highlight_nonselected_windows;
502
503 /* If cursor motion alone moves point off frame, try scrolling this
504 many lines up or down if that will bring it back. */
505
506 static EMACS_INT scroll_step;
507
508 /* Nonzero means scroll just far enough to bring point back on the
509 screen, when appropriate. */
510
511 static EMACS_INT scroll_conservatively;
512
513 /* Recenter the window whenever point gets within this many lines of
514 the top or bottom of the window. This value is translated into a
515 pixel value by multiplying it with FRAME_LINE_HEIGHT, which means
516 that there is really a fixed pixel height scroll margin. */
517
518 EMACS_INT scroll_margin;
519
520 /* Number of windows showing the buffer of the selected window (or
521 another buffer with the same base buffer). keyboard.c refers to
522 this. */
523
524 int buffer_shared;
525
526 /* Vector containing glyphs for an ellipsis `...'. */
527
528 static Lisp_Object default_invis_vector[3];
529
530 /* Zero means display the mode-line/header-line/menu-bar in the default face
531 (this slightly odd definition is for compatibility with previous versions
532 of emacs), non-zero means display them using their respective faces.
533
534 This variable is deprecated. */
535
536 int mode_line_inverse_video;
537
538 /* Prompt to display in front of the mini-buffer contents. */
539
540 Lisp_Object minibuf_prompt;
541
542 /* Width of current mini-buffer prompt. Only set after display_line
543 of the line that contains the prompt. */
544
545 int minibuf_prompt_width;
546
547 /* This is the window where the echo area message was displayed. It
548 is always a mini-buffer window, but it may not be the same window
549 currently active as a mini-buffer. */
550
551 Lisp_Object echo_area_window;
552
553 /* List of pairs (MESSAGE . MULTIBYTE). The function save_message
554 pushes the current message and the value of
555 message_enable_multibyte on the stack, the function restore_message
556 pops the stack and displays MESSAGE again. */
557
558 Lisp_Object Vmessage_stack;
559
560 /* Nonzero means multibyte characters were enabled when the echo area
561 message was specified. */
562
563 int message_enable_multibyte;
564
565 /* Nonzero if we should redraw the mode lines on the next redisplay. */
566
567 int update_mode_lines;
568
569 /* Nonzero if window sizes or contents have changed since last
570 redisplay that finished. */
571
572 int windows_or_buffers_changed;
573
574 /* Nonzero means a frame's cursor type has been changed. */
575
576 int cursor_type_changed;
577
578 /* Nonzero after display_mode_line if %l was used and it displayed a
579 line number. */
580
581 int line_number_displayed;
582
583 /* Maximum buffer size for which to display line numbers. */
584
585 Lisp_Object Vline_number_display_limit;
586
587 /* Line width to consider when repositioning for line number display. */
588
589 static EMACS_INT line_number_display_limit_width;
590
591 /* Number of lines to keep in the message log buffer. t means
592 infinite. nil means don't log at all. */
593
594 Lisp_Object Vmessage_log_max;
595
596 /* The name of the *Messages* buffer, a string. */
597
598 static Lisp_Object Vmessages_buffer_name;
599
600 /* Current, index 0, and last displayed echo area message. Either
601 buffers from echo_buffers, or nil to indicate no message. */
602
603 Lisp_Object echo_area_buffer[2];
604
605 /* The buffers referenced from echo_area_buffer. */
606
607 static Lisp_Object echo_buffer[2];
608
609 /* A vector saved used in with_area_buffer to reduce consing. */
610
611 static Lisp_Object Vwith_echo_area_save_vector;
612
613 /* Non-zero means display_echo_area should display the last echo area
614 message again. Set by redisplay_preserve_echo_area. */
615
616 static int display_last_displayed_message_p;
617
618 /* Nonzero if echo area is being used by print; zero if being used by
619 message. */
620
621 int message_buf_print;
622
623 /* The symbol `inhibit-menubar-update' and its DEFVAR_BOOL variable. */
624
625 Lisp_Object Qinhibit_menubar_update;
626 int inhibit_menubar_update;
627
628 /* When evaluating expressions from menu bar items (enable conditions,
629 for instance), this is the frame they are being processed for. */
630
631 Lisp_Object Vmenu_updating_frame;
632
633 /* Maximum height for resizing mini-windows. Either a float
634 specifying a fraction of the available height, or an integer
635 specifying a number of lines. */
636
637 Lisp_Object Vmax_mini_window_height;
638
639 /* Non-zero means messages should be displayed with truncated
640 lines instead of being continued. */
641
642 int message_truncate_lines;
643 Lisp_Object Qmessage_truncate_lines;
644
645 /* Set to 1 in clear_message to make redisplay_internal aware
646 of an emptied echo area. */
647
648 static int message_cleared_p;
649
650 /* How to blink the default frame cursor off. */
651 Lisp_Object Vblink_cursor_alist;
652
653 /* A scratch glyph row with contents used for generating truncation
654 glyphs. Also used in direct_output_for_insert. */
655
656 #define MAX_SCRATCH_GLYPHS 100
657 struct glyph_row scratch_glyph_row;
658 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
659
660 /* Ascent and height of the last line processed by move_it_to. */
661
662 static int last_max_ascent, last_height;
663
664 /* Non-zero if there's a help-echo in the echo area. */
665
666 int help_echo_showing_p;
667
668 /* If >= 0, computed, exact values of mode-line and header-line height
669 to use in the macros CURRENT_MODE_LINE_HEIGHT and
670 CURRENT_HEADER_LINE_HEIGHT. */
671
672 int current_mode_line_height, current_header_line_height;
673
674 /* The maximum distance to look ahead for text properties. Values
675 that are too small let us call compute_char_face and similar
676 functions too often which is expensive. Values that are too large
677 let us call compute_char_face and alike too often because we
678 might not be interested in text properties that far away. */
679
680 #define TEXT_PROP_DISTANCE_LIMIT 100
681
682 #if GLYPH_DEBUG
683
684 /* Variables to turn off display optimizations from Lisp. */
685
686 int inhibit_try_window_id, inhibit_try_window_reusing;
687 int inhibit_try_cursor_movement;
688
689 /* Non-zero means print traces of redisplay if compiled with
690 GLYPH_DEBUG != 0. */
691
692 int trace_redisplay_p;
693
694 #endif /* GLYPH_DEBUG */
695
696 #ifdef DEBUG_TRACE_MOVE
697 /* Non-zero means trace with TRACE_MOVE to stderr. */
698 int trace_move;
699
700 #define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
701 #else
702 #define TRACE_MOVE(x) (void) 0
703 #endif
704
705 /* Non-zero means automatically scroll windows horizontally to make
706 point visible. */
707
708 int automatic_hscrolling_p;
709 Lisp_Object Qauto_hscroll_mode;
710
711 /* How close to the margin can point get before the window is scrolled
712 horizontally. */
713 EMACS_INT hscroll_margin;
714
715 /* How much to scroll horizontally when point is inside the above margin. */
716 Lisp_Object Vhscroll_step;
717
718 /* The variable `resize-mini-windows'. If nil, don't resize
719 mini-windows. If t, always resize them to fit the text they
720 display. If `grow-only', let mini-windows grow only until they
721 become empty. */
722
723 Lisp_Object Vresize_mini_windows;
724
725 /* Buffer being redisplayed -- for redisplay_window_error. */
726
727 struct buffer *displayed_buffer;
728
729 /* Space between overline and text. */
730
731 EMACS_INT overline_margin;
732
733 /* Require underline to be at least this many screen pixels below baseline
734 This to avoid underline "merging" with the base of letters at small
735 font sizes, particularly when x_use_underline_position_properties is on. */
736
737 EMACS_INT underline_minimum_offset;
738
739 /* Value returned from text property handlers (see below). */
740
741 enum prop_handled
742 {
743 HANDLED_NORMALLY,
744 HANDLED_RECOMPUTE_PROPS,
745 HANDLED_OVERLAY_STRING_CONSUMED,
746 HANDLED_RETURN
747 };
748
749 /* A description of text properties that redisplay is interested
750 in. */
751
752 struct props
753 {
754 /* The name of the property. */
755 Lisp_Object *name;
756
757 /* A unique index for the property. */
758 enum prop_idx idx;
759
760 /* A handler function called to set up iterator IT from the property
761 at IT's current position. Value is used to steer handle_stop. */
762 enum prop_handled (*handler) P_ ((struct it *it));
763 };
764
765 static enum prop_handled handle_face_prop P_ ((struct it *));
766 static enum prop_handled handle_invisible_prop P_ ((struct it *));
767 static enum prop_handled handle_display_prop P_ ((struct it *));
768 static enum prop_handled handle_composition_prop P_ ((struct it *));
769 static enum prop_handled handle_overlay_change P_ ((struct it *));
770 static enum prop_handled handle_fontified_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 {&Qcomposition, COMPOSITION_PROP_IDX, handle_composition_prop},
783 {NULL, 0, NULL}
784 };
785
786 /* Value is the position described by X. If X is a marker, value is
787 the marker_position of X. Otherwise, value is X. */
788
789 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
790
791 /* Enumeration returned by some move_it_.* functions internally. */
792
793 enum move_it_result
794 {
795 /* Not used. Undefined value. */
796 MOVE_UNDEFINED,
797
798 /* Move ended at the requested buffer position or ZV. */
799 MOVE_POS_MATCH_OR_ZV,
800
801 /* Move ended at the requested X pixel position. */
802 MOVE_X_REACHED,
803
804 /* Move within a line ended at the end of a line that must be
805 continued. */
806 MOVE_LINE_CONTINUED,
807
808 /* Move within a line ended at the end of a line that would
809 be displayed truncated. */
810 MOVE_LINE_TRUNCATED,
811
812 /* Move within a line ended at a line end. */
813 MOVE_NEWLINE_OR_CR
814 };
815
816 /* This counter is used to clear the face cache every once in a while
817 in redisplay_internal. It is incremented for each redisplay.
818 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
819 cleared. */
820
821 #define CLEAR_FACE_CACHE_COUNT 500
822 static int clear_face_cache_count;
823
824 /* Similarly for the image cache. */
825
826 #ifdef HAVE_WINDOW_SYSTEM
827 #define CLEAR_IMAGE_CACHE_COUNT 101
828 static int clear_image_cache_count;
829 #endif
830
831 /* Non-zero while redisplay_internal is in progress. */
832
833 int redisplaying_p;
834
835 /* Non-zero means don't free realized faces. Bound while freeing
836 realized faces is dangerous because glyph matrices might still
837 reference them. */
838
839 int inhibit_free_realized_faces;
840 Lisp_Object Qinhibit_free_realized_faces;
841
842 /* If a string, XTread_socket generates an event to display that string.
843 (The display is done in read_char.) */
844
845 Lisp_Object help_echo_string;
846 Lisp_Object help_echo_window;
847 Lisp_Object help_echo_object;
848 int help_echo_pos;
849
850 /* Temporary variable for XTread_socket. */
851
852 Lisp_Object previous_help_echo_string;
853
854 /* Null glyph slice */
855
856 static struct glyph_slice null_glyph_slice = { 0, 0, 0, 0 };
857
858 /* Platform-independent portion of hourglass implementation. */
859
860 /* Non-zero means we're allowed to display a hourglass pointer. */
861 int display_hourglass_p;
862
863 /* Non-zero means an hourglass cursor is currently shown. */
864 int hourglass_shown_p;
865
866 /* If non-null, an asynchronous timer that, when it expires, displays
867 an hourglass cursor on all frames. */
868 struct atimer *hourglass_atimer;
869
870 /* Number of seconds to wait before displaying an hourglass cursor. */
871 Lisp_Object Vhourglass_delay;
872
873 /* Default number of seconds to wait before displaying an hourglass
874 cursor. */
875 #define DEFAULT_HOURGLASS_DELAY 1
876
877 \f
878 /* Function prototypes. */
879
880 static void setup_for_ellipsis P_ ((struct it *, int));
881 static void mark_window_display_accurate_1 P_ ((struct window *, int));
882 static int single_display_spec_string_p P_ ((Lisp_Object, Lisp_Object));
883 static int display_prop_string_p P_ ((Lisp_Object, Lisp_Object));
884 static int cursor_row_p P_ ((struct window *, struct glyph_row *));
885 static int redisplay_mode_lines P_ ((Lisp_Object, int));
886 static char *decode_mode_spec_coding P_ ((Lisp_Object, char *, int));
887
888 static Lisp_Object get_it_property P_ ((struct it *it, Lisp_Object prop));
889
890 static void handle_line_prefix P_ ((struct it *));
891
892 static void pint2str P_ ((char *, int, int));
893 static void pint2hrstr P_ ((char *, int, int));
894 static struct text_pos run_window_scroll_functions P_ ((Lisp_Object,
895 struct text_pos));
896 static void reconsider_clip_changes P_ ((struct window *, struct buffer *));
897 static int text_outside_line_unchanged_p P_ ((struct window *, int, int));
898 static void store_mode_line_noprop_char P_ ((char));
899 static int store_mode_line_noprop P_ ((const unsigned char *, int, int));
900 static void x_consider_frame_title P_ ((Lisp_Object));
901 static void handle_stop P_ ((struct it *));
902 static int tool_bar_lines_needed P_ ((struct frame *, int *));
903 static int single_display_spec_intangible_p P_ ((Lisp_Object));
904 static void ensure_echo_area_buffers P_ ((void));
905 static Lisp_Object unwind_with_echo_area_buffer P_ ((Lisp_Object));
906 static Lisp_Object with_echo_area_buffer_unwind_data P_ ((struct window *));
907 static int with_echo_area_buffer P_ ((struct window *, int,
908 int (*) (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT),
909 EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
910 static void clear_garbaged_frames P_ ((void));
911 static int current_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
912 static int truncate_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
913 static int set_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
914 static int display_echo_area P_ ((struct window *));
915 static int display_echo_area_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
916 static int resize_mini_window_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
917 static Lisp_Object unwind_redisplay P_ ((Lisp_Object));
918 static int string_char_and_length P_ ((const unsigned char *, int, int *));
919 static struct text_pos display_prop_end P_ ((struct it *, Lisp_Object,
920 struct text_pos));
921 static int compute_window_start_on_continuation_line P_ ((struct window *));
922 static Lisp_Object safe_eval_handler P_ ((Lisp_Object));
923 static void insert_left_trunc_glyphs P_ ((struct it *));
924 static struct glyph_row *get_overlay_arrow_glyph_row P_ ((struct window *,
925 Lisp_Object));
926 static void extend_face_to_end_of_line P_ ((struct it *));
927 static int append_space_for_newline P_ ((struct it *, int));
928 static int cursor_row_fully_visible_p P_ ((struct window *, int, int));
929 static int try_scrolling P_ ((Lisp_Object, int, EMACS_INT, EMACS_INT, int, int));
930 static int try_cursor_movement P_ ((Lisp_Object, struct text_pos, int *));
931 static int trailing_whitespace_p P_ ((int));
932 static int message_log_check_duplicate P_ ((int, int, int, int));
933 static void push_it P_ ((struct it *));
934 static void pop_it P_ ((struct it *));
935 static void sync_frame_with_window_matrix_rows P_ ((struct window *));
936 static void select_frame_for_redisplay P_ ((Lisp_Object));
937 static void redisplay_internal P_ ((int));
938 static int echo_area_display P_ ((int));
939 static void redisplay_windows P_ ((Lisp_Object));
940 static void redisplay_window P_ ((Lisp_Object, int));
941 static Lisp_Object redisplay_window_error ();
942 static Lisp_Object redisplay_window_0 P_ ((Lisp_Object));
943 static Lisp_Object redisplay_window_1 P_ ((Lisp_Object));
944 static int update_menu_bar P_ ((struct frame *, int, int));
945 static int try_window_reusing_current_matrix P_ ((struct window *));
946 static int try_window_id P_ ((struct window *));
947 static int display_line P_ ((struct it *));
948 static int display_mode_lines P_ ((struct window *));
949 static int display_mode_line P_ ((struct window *, enum face_id, Lisp_Object));
950 static int display_mode_element P_ ((struct it *, int, int, int, Lisp_Object, Lisp_Object, int));
951 static int store_mode_line_string P_ ((char *, Lisp_Object, int, int, int, Lisp_Object));
952 static char *decode_mode_spec P_ ((struct window *, int, int, int, int *));
953 static void display_menu_bar P_ ((struct window *));
954 static int display_count_lines P_ ((int, int, int, int, int *));
955 static int display_string P_ ((unsigned char *, Lisp_Object, Lisp_Object,
956 EMACS_INT, EMACS_INT, struct it *, int, int, int, int));
957 static void compute_line_metrics P_ ((struct it *));
958 static void run_redisplay_end_trigger_hook P_ ((struct it *));
959 static int get_overlay_strings P_ ((struct it *, int));
960 static int get_overlay_strings_1 P_ ((struct it *, int, int));
961 static void next_overlay_string P_ ((struct it *));
962 static void reseat P_ ((struct it *, struct text_pos, int));
963 static void reseat_1 P_ ((struct it *, struct text_pos, int));
964 static void back_to_previous_visible_line_start P_ ((struct it *));
965 void reseat_at_previous_visible_line_start P_ ((struct it *));
966 static void reseat_at_next_visible_line_start P_ ((struct it *, int));
967 static int next_element_from_ellipsis P_ ((struct it *));
968 static int next_element_from_display_vector P_ ((struct it *));
969 static int next_element_from_string P_ ((struct it *));
970 static int next_element_from_c_string P_ ((struct it *));
971 static int next_element_from_buffer P_ ((struct it *));
972 static int next_element_from_composition P_ ((struct it *));
973 static int next_element_from_image P_ ((struct it *));
974 static int next_element_from_stretch P_ ((struct it *));
975 static void load_overlay_strings P_ ((struct it *, int));
976 static int init_from_display_pos P_ ((struct it *, struct window *,
977 struct display_pos *));
978 static void reseat_to_string P_ ((struct it *, unsigned char *,
979 Lisp_Object, int, int, int, int));
980 static enum move_it_result
981 move_it_in_display_line_to (struct it *, EMACS_INT, int,
982 enum move_operation_enum);
983 void move_it_vertically_backward P_ ((struct it *, int));
984 static void init_to_row_start P_ ((struct it *, struct window *,
985 struct glyph_row *));
986 static int init_to_row_end P_ ((struct it *, struct window *,
987 struct glyph_row *));
988 static void back_to_previous_line_start P_ ((struct it *));
989 static int forward_to_next_line_start P_ ((struct it *, int *));
990 static struct text_pos string_pos_nchars_ahead P_ ((struct text_pos,
991 Lisp_Object, int));
992 static struct text_pos string_pos P_ ((int, Lisp_Object));
993 static struct text_pos c_string_pos P_ ((int, unsigned char *, int));
994 static int number_of_chars P_ ((unsigned char *, int));
995 static void compute_stop_pos P_ ((struct it *));
996 static void compute_string_pos P_ ((struct text_pos *, struct text_pos,
997 Lisp_Object));
998 static int face_before_or_after_it_pos P_ ((struct it *, int));
999 static EMACS_INT next_overlay_change P_ ((EMACS_INT));
1000 static int handle_single_display_spec P_ ((struct it *, Lisp_Object,
1001 Lisp_Object, Lisp_Object,
1002 struct text_pos *, int));
1003 static int underlying_face_id P_ ((struct it *));
1004 static int in_ellipses_for_invisible_text_p P_ ((struct display_pos *,
1005 struct window *));
1006
1007 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
1008 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
1009
1010 #ifdef HAVE_WINDOW_SYSTEM
1011
1012 static void update_tool_bar P_ ((struct frame *, int));
1013 static void build_desired_tool_bar_string P_ ((struct frame *f));
1014 static int redisplay_tool_bar P_ ((struct frame *));
1015 static void display_tool_bar_line P_ ((struct it *, int));
1016 static void notice_overwritten_cursor P_ ((struct window *,
1017 enum glyph_row_area,
1018 int, int, int, int));
1019
1020
1021
1022 #endif /* HAVE_WINDOW_SYSTEM */
1023
1024 \f
1025 /***********************************************************************
1026 Window display dimensions
1027 ***********************************************************************/
1028
1029 /* Return the bottom boundary y-position for text lines in window W.
1030 This is the first y position at which a line cannot start.
1031 It is relative to the top of the window.
1032
1033 This is the height of W minus the height of a mode line, if any. */
1034
1035 INLINE int
1036 window_text_bottom_y (w)
1037 struct window *w;
1038 {
1039 int height = WINDOW_TOTAL_HEIGHT (w);
1040
1041 if (WINDOW_WANTS_MODELINE_P (w))
1042 height -= CURRENT_MODE_LINE_HEIGHT (w);
1043 return height;
1044 }
1045
1046 /* Return the pixel width of display area AREA of window W. AREA < 0
1047 means return the total width of W, not including fringes to
1048 the left and right of the window. */
1049
1050 INLINE int
1051 window_box_width (w, area)
1052 struct window *w;
1053 int area;
1054 {
1055 int cols = XFASTINT (w->total_cols);
1056 int pixels = 0;
1057
1058 if (!w->pseudo_window_p)
1059 {
1060 cols -= WINDOW_SCROLL_BAR_COLS (w);
1061
1062 if (area == TEXT_AREA)
1063 {
1064 if (INTEGERP (w->left_margin_cols))
1065 cols -= XFASTINT (w->left_margin_cols);
1066 if (INTEGERP (w->right_margin_cols))
1067 cols -= XFASTINT (w->right_margin_cols);
1068 pixels = -WINDOW_TOTAL_FRINGE_WIDTH (w);
1069 }
1070 else if (area == LEFT_MARGIN_AREA)
1071 {
1072 cols = (INTEGERP (w->left_margin_cols)
1073 ? XFASTINT (w->left_margin_cols) : 0);
1074 pixels = 0;
1075 }
1076 else if (area == RIGHT_MARGIN_AREA)
1077 {
1078 cols = (INTEGERP (w->right_margin_cols)
1079 ? XFASTINT (w->right_margin_cols) : 0);
1080 pixels = 0;
1081 }
1082 }
1083
1084 return cols * WINDOW_FRAME_COLUMN_WIDTH (w) + pixels;
1085 }
1086
1087
1088 /* Return the pixel height of the display area of window W, not
1089 including mode lines of W, if any. */
1090
1091 INLINE int
1092 window_box_height (w)
1093 struct window *w;
1094 {
1095 struct frame *f = XFRAME (w->frame);
1096 int height = WINDOW_TOTAL_HEIGHT (w);
1097
1098 xassert (height >= 0);
1099
1100 /* Note: the code below that determines the mode-line/header-line
1101 height is essentially the same as that contained in the macro
1102 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
1103 the appropriate glyph row has its `mode_line_p' flag set,
1104 and if it doesn't, uses estimate_mode_line_height instead. */
1105
1106 if (WINDOW_WANTS_MODELINE_P (w))
1107 {
1108 struct glyph_row *ml_row
1109 = (w->current_matrix && w->current_matrix->rows
1110 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
1111 : 0);
1112 if (ml_row && ml_row->mode_line_p)
1113 height -= ml_row->height;
1114 else
1115 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
1116 }
1117
1118 if (WINDOW_WANTS_HEADER_LINE_P (w))
1119 {
1120 struct glyph_row *hl_row
1121 = (w->current_matrix && w->current_matrix->rows
1122 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
1123 : 0);
1124 if (hl_row && hl_row->mode_line_p)
1125 height -= hl_row->height;
1126 else
1127 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
1128 }
1129
1130 /* With a very small font and a mode-line that's taller than
1131 default, we might end up with a negative height. */
1132 return max (0, height);
1133 }
1134
1135 /* Return the window-relative coordinate of the left edge of display
1136 area AREA of window W. AREA < 0 means return the left edge of the
1137 whole window, to the right of the left fringe of W. */
1138
1139 INLINE int
1140 window_box_left_offset (w, area)
1141 struct window *w;
1142 int area;
1143 {
1144 int x;
1145
1146 if (w->pseudo_window_p)
1147 return 0;
1148
1149 x = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
1150
1151 if (area == TEXT_AREA)
1152 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1153 + window_box_width (w, LEFT_MARGIN_AREA));
1154 else if (area == RIGHT_MARGIN_AREA)
1155 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1156 + window_box_width (w, LEFT_MARGIN_AREA)
1157 + window_box_width (w, TEXT_AREA)
1158 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1159 ? 0
1160 : WINDOW_RIGHT_FRINGE_WIDTH (w)));
1161 else if (area == LEFT_MARGIN_AREA
1162 && WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1163 x += WINDOW_LEFT_FRINGE_WIDTH (w);
1164
1165 return x;
1166 }
1167
1168
1169 /* Return the window-relative coordinate of the right edge of display
1170 area AREA of window W. AREA < 0 means return the left edge of the
1171 whole window, to the left of the right fringe of W. */
1172
1173 INLINE int
1174 window_box_right_offset (w, area)
1175 struct window *w;
1176 int area;
1177 {
1178 return window_box_left_offset (w, area) + window_box_width (w, area);
1179 }
1180
1181 /* Return the frame-relative coordinate of the left edge of display
1182 area AREA of window W. AREA < 0 means return the left edge of the
1183 whole window, to the right of the left fringe of W. */
1184
1185 INLINE int
1186 window_box_left (w, area)
1187 struct window *w;
1188 int area;
1189 {
1190 struct frame *f = XFRAME (w->frame);
1191 int x;
1192
1193 if (w->pseudo_window_p)
1194 return FRAME_INTERNAL_BORDER_WIDTH (f);
1195
1196 x = (WINDOW_LEFT_EDGE_X (w)
1197 + window_box_left_offset (w, area));
1198
1199 return x;
1200 }
1201
1202
1203 /* Return the frame-relative coordinate of the right edge of display
1204 area AREA of window W. AREA < 0 means return the left edge of the
1205 whole window, to the left of the right fringe of W. */
1206
1207 INLINE int
1208 window_box_right (w, area)
1209 struct window *w;
1210 int area;
1211 {
1212 return window_box_left (w, area) + window_box_width (w, area);
1213 }
1214
1215 /* Get the bounding box of the display area AREA of window W, without
1216 mode lines, in frame-relative coordinates. AREA < 0 means the
1217 whole window, not including the left and right fringes of
1218 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1219 coordinates of the upper-left corner of the box. Return in
1220 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1221
1222 INLINE void
1223 window_box (w, area, box_x, box_y, box_width, box_height)
1224 struct window *w;
1225 int area;
1226 int *box_x, *box_y, *box_width, *box_height;
1227 {
1228 if (box_width)
1229 *box_width = window_box_width (w, area);
1230 if (box_height)
1231 *box_height = window_box_height (w);
1232 if (box_x)
1233 *box_x = window_box_left (w, area);
1234 if (box_y)
1235 {
1236 *box_y = WINDOW_TOP_EDGE_Y (w);
1237 if (WINDOW_WANTS_HEADER_LINE_P (w))
1238 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1239 }
1240 }
1241
1242
1243 /* Get the bounding box of the display area AREA of window W, without
1244 mode lines. AREA < 0 means the whole window, not including the
1245 left and right fringe of the window. Return in *TOP_LEFT_X
1246 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1247 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1248 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1249 box. */
1250
1251 INLINE void
1252 window_box_edges (w, area, top_left_x, top_left_y,
1253 bottom_right_x, bottom_right_y)
1254 struct window *w;
1255 int area;
1256 int *top_left_x, *top_left_y, *bottom_right_x, *bottom_right_y;
1257 {
1258 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
1259 bottom_right_y);
1260 *bottom_right_x += *top_left_x;
1261 *bottom_right_y += *top_left_y;
1262 }
1263
1264
1265 \f
1266 /***********************************************************************
1267 Utilities
1268 ***********************************************************************/
1269
1270 /* Return the bottom y-position of the line the iterator IT is in.
1271 This can modify IT's settings. */
1272
1273 int
1274 line_bottom_y (it)
1275 struct it *it;
1276 {
1277 int line_height = it->max_ascent + it->max_descent;
1278 int line_top_y = it->current_y;
1279
1280 if (line_height == 0)
1281 {
1282 if (last_height)
1283 line_height = last_height;
1284 else if (IT_CHARPOS (*it) < ZV)
1285 {
1286 move_it_by_lines (it, 1, 1);
1287 line_height = (it->max_ascent || it->max_descent
1288 ? it->max_ascent + it->max_descent
1289 : last_height);
1290 }
1291 else
1292 {
1293 struct glyph_row *row = it->glyph_row;
1294
1295 /* Use the default character height. */
1296 it->glyph_row = NULL;
1297 it->what = IT_CHARACTER;
1298 it->c = ' ';
1299 it->len = 1;
1300 PRODUCE_GLYPHS (it);
1301 line_height = it->ascent + it->descent;
1302 it->glyph_row = row;
1303 }
1304 }
1305
1306 return line_top_y + line_height;
1307 }
1308
1309
1310 /* Return 1 if position CHARPOS is visible in window W.
1311 CHARPOS < 0 means return info about WINDOW_END position.
1312 If visible, set *X and *Y to pixel coordinates of top left corner.
1313 Set *RTOP and *RBOT to pixel height of an invisible area of glyph at POS.
1314 Set *ROWH and *VPOS to row's visible height and VPOS (row number). */
1315
1316 int
1317 pos_visible_p (w, charpos, x, y, rtop, rbot, rowh, vpos)
1318 struct window *w;
1319 int charpos, *x, *y, *rtop, *rbot, *rowh, *vpos;
1320 {
1321 struct it it;
1322 struct text_pos top;
1323 int visible_p = 0;
1324 struct buffer *old_buffer = NULL;
1325
1326 if (noninteractive)
1327 return visible_p;
1328
1329 if (XBUFFER (w->buffer) != current_buffer)
1330 {
1331 old_buffer = current_buffer;
1332 set_buffer_internal_1 (XBUFFER (w->buffer));
1333 }
1334
1335 SET_TEXT_POS_FROM_MARKER (top, w->start);
1336
1337 /* Compute exact mode line heights. */
1338 if (WINDOW_WANTS_MODELINE_P (w))
1339 current_mode_line_height
1340 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1341 current_buffer->mode_line_format);
1342
1343 if (WINDOW_WANTS_HEADER_LINE_P (w))
1344 current_header_line_height
1345 = display_mode_line (w, HEADER_LINE_FACE_ID,
1346 current_buffer->header_line_format);
1347
1348 start_display (&it, w, top);
1349 move_it_to (&it, charpos, -1, it.last_visible_y-1, -1,
1350 (charpos >= 0 ? MOVE_TO_POS : 0) | MOVE_TO_Y);
1351
1352 /* Note that we may overshoot because of invisible text. */
1353 if (charpos >= 0 && IT_CHARPOS (it) >= charpos)
1354 {
1355 int top_x = it.current_x;
1356 int top_y = it.current_y;
1357 int bottom_y = (last_height = 0, line_bottom_y (&it));
1358 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
1359
1360 if (top_y < window_top_y)
1361 visible_p = bottom_y > window_top_y;
1362 else if (top_y < it.last_visible_y)
1363 visible_p = 1;
1364 if (visible_p)
1365 {
1366 if (it.method == GET_FROM_BUFFER)
1367 {
1368 Lisp_Object window, prop;
1369
1370 XSETWINDOW (window, w);
1371 prop = Fget_char_property (make_number (it.position.charpos),
1372 Qinvisible, window);
1373
1374 /* If charpos coincides with invisible text covered with an
1375 ellipsis, use the first glyph of the ellipsis to compute
1376 the pixel positions. */
1377 if (TEXT_PROP_MEANS_INVISIBLE (prop) == 2)
1378 {
1379 struct glyph_row *row = it.glyph_row;
1380 struct glyph *glyph = row->glyphs[TEXT_AREA];
1381 struct glyph *end = glyph + row->used[TEXT_AREA];
1382 int x = row->x;
1383
1384 for (; glyph < end && glyph->charpos < charpos; glyph++)
1385 x += glyph->pixel_width;
1386
1387 top_x = x;
1388 }
1389 }
1390
1391 *x = top_x;
1392 *y = max (top_y + max (0, it.max_ascent - it.ascent), window_top_y);
1393 *rtop = max (0, window_top_y - top_y);
1394 *rbot = max (0, bottom_y - it.last_visible_y);
1395 *rowh = max (0, (min (bottom_y, it.last_visible_y)
1396 - max (top_y, window_top_y)));
1397 *vpos = it.vpos;
1398 }
1399 }
1400 else
1401 {
1402 struct it it2;
1403
1404 it2 = it;
1405 if (IT_CHARPOS (it) < ZV && FETCH_BYTE (IT_BYTEPOS (it)) != '\n')
1406 move_it_by_lines (&it, 1, 0);
1407 if (charpos < IT_CHARPOS (it)
1408 || (it.what == IT_EOB && charpos == IT_CHARPOS (it)))
1409 {
1410 visible_p = 1;
1411 move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS);
1412 *x = it2.current_x;
1413 *y = it2.current_y + it2.max_ascent - it2.ascent;
1414 *rtop = max (0, -it2.current_y);
1415 *rbot = max (0, ((it2.current_y + it2.max_ascent + it2.max_descent)
1416 - it.last_visible_y));
1417 *rowh = max (0, (min (it2.current_y + it2.max_ascent + it2.max_descent,
1418 it.last_visible_y)
1419 - max (it2.current_y,
1420 WINDOW_HEADER_LINE_HEIGHT (w))));
1421 *vpos = it2.vpos;
1422 }
1423 }
1424
1425 if (old_buffer)
1426 set_buffer_internal_1 (old_buffer);
1427
1428 current_header_line_height = current_mode_line_height = -1;
1429
1430 if (visible_p && XFASTINT (w->hscroll) > 0)
1431 *x -= XFASTINT (w->hscroll) * WINDOW_FRAME_COLUMN_WIDTH (w);
1432
1433 #if 0
1434 /* Debugging code. */
1435 if (visible_p)
1436 fprintf (stderr, "+pv pt=%d vs=%d --> x=%d y=%d rt=%d rb=%d rh=%d vp=%d\n",
1437 charpos, w->vscroll, *x, *y, *rtop, *rbot, *rowh, *vpos);
1438 else
1439 fprintf (stderr, "-pv pt=%d vs=%d\n", charpos, w->vscroll);
1440 #endif
1441
1442 return visible_p;
1443 }
1444
1445
1446 /* Return the next character from STR which is MAXLEN bytes long.
1447 Return in *LEN the length of the character. This is like
1448 STRING_CHAR_AND_LENGTH but never returns an invalid character. If
1449 we find one, we return a `?', but with the length of the invalid
1450 character. */
1451
1452 static INLINE int
1453 string_char_and_length (str, maxlen, len)
1454 const unsigned char *str;
1455 int maxlen, *len;
1456 {
1457 int c;
1458
1459 c = STRING_CHAR_AND_LENGTH (str, maxlen, *len);
1460 if (!CHAR_VALID_P (c, 1))
1461 /* We may not change the length here because other places in Emacs
1462 don't use this function, i.e. they silently accept invalid
1463 characters. */
1464 c = '?';
1465
1466 return c;
1467 }
1468
1469
1470
1471 /* Given a position POS containing a valid character and byte position
1472 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1473
1474 static struct text_pos
1475 string_pos_nchars_ahead (pos, string, nchars)
1476 struct text_pos pos;
1477 Lisp_Object string;
1478 int nchars;
1479 {
1480 xassert (STRINGP (string) && nchars >= 0);
1481
1482 if (STRING_MULTIBYTE (string))
1483 {
1484 int rest = SBYTES (string) - BYTEPOS (pos);
1485 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1486 int len;
1487
1488 while (nchars--)
1489 {
1490 string_char_and_length (p, rest, &len);
1491 p += len, rest -= len;
1492 xassert (rest >= 0);
1493 CHARPOS (pos) += 1;
1494 BYTEPOS (pos) += len;
1495 }
1496 }
1497 else
1498 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1499
1500 return pos;
1501 }
1502
1503
1504 /* Value is the text position, i.e. character and byte position,
1505 for character position CHARPOS in STRING. */
1506
1507 static INLINE struct text_pos
1508 string_pos (charpos, string)
1509 int charpos;
1510 Lisp_Object string;
1511 {
1512 struct text_pos pos;
1513 xassert (STRINGP (string));
1514 xassert (charpos >= 0);
1515 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1516 return pos;
1517 }
1518
1519
1520 /* Value is a text position, i.e. character and byte position, for
1521 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1522 means recognize multibyte characters. */
1523
1524 static struct text_pos
1525 c_string_pos (charpos, s, multibyte_p)
1526 int charpos;
1527 unsigned char *s;
1528 int multibyte_p;
1529 {
1530 struct text_pos pos;
1531
1532 xassert (s != NULL);
1533 xassert (charpos >= 0);
1534
1535 if (multibyte_p)
1536 {
1537 int rest = strlen (s), len;
1538
1539 SET_TEXT_POS (pos, 0, 0);
1540 while (charpos--)
1541 {
1542 string_char_and_length (s, rest, &len);
1543 s += len, rest -= len;
1544 xassert (rest >= 0);
1545 CHARPOS (pos) += 1;
1546 BYTEPOS (pos) += len;
1547 }
1548 }
1549 else
1550 SET_TEXT_POS (pos, charpos, charpos);
1551
1552 return pos;
1553 }
1554
1555
1556 /* Value is the number of characters in C string S. MULTIBYTE_P
1557 non-zero means recognize multibyte characters. */
1558
1559 static int
1560 number_of_chars (s, multibyte_p)
1561 unsigned char *s;
1562 int multibyte_p;
1563 {
1564 int nchars;
1565
1566 if (multibyte_p)
1567 {
1568 int rest = strlen (s), len;
1569 unsigned char *p = (unsigned char *) s;
1570
1571 for (nchars = 0; rest > 0; ++nchars)
1572 {
1573 string_char_and_length (p, rest, &len);
1574 rest -= len, p += len;
1575 }
1576 }
1577 else
1578 nchars = strlen (s);
1579
1580 return nchars;
1581 }
1582
1583
1584 /* Compute byte position NEWPOS->bytepos corresponding to
1585 NEWPOS->charpos. POS is a known position in string STRING.
1586 NEWPOS->charpos must be >= POS.charpos. */
1587
1588 static void
1589 compute_string_pos (newpos, pos, string)
1590 struct text_pos *newpos, pos;
1591 Lisp_Object string;
1592 {
1593 xassert (STRINGP (string));
1594 xassert (CHARPOS (*newpos) >= CHARPOS (pos));
1595
1596 if (STRING_MULTIBYTE (string))
1597 *newpos = string_pos_nchars_ahead (pos, string,
1598 CHARPOS (*newpos) - CHARPOS (pos));
1599 else
1600 BYTEPOS (*newpos) = CHARPOS (*newpos);
1601 }
1602
1603 /* EXPORT:
1604 Return an estimation of the pixel height of mode or top lines on
1605 frame F. FACE_ID specifies what line's height to estimate. */
1606
1607 int
1608 estimate_mode_line_height (f, face_id)
1609 struct frame *f;
1610 enum face_id face_id;
1611 {
1612 #ifdef HAVE_WINDOW_SYSTEM
1613 if (FRAME_WINDOW_P (f))
1614 {
1615 int height = FONT_HEIGHT (FRAME_FONT (f));
1616
1617 /* This function is called so early when Emacs starts that the face
1618 cache and mode line face are not yet initialized. */
1619 if (FRAME_FACE_CACHE (f))
1620 {
1621 struct face *face = FACE_FROM_ID (f, face_id);
1622 if (face)
1623 {
1624 if (face->font)
1625 height = FONT_HEIGHT (face->font);
1626 if (face->box_line_width > 0)
1627 height += 2 * face->box_line_width;
1628 }
1629 }
1630
1631 return height;
1632 }
1633 #endif
1634
1635 return 1;
1636 }
1637
1638 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1639 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1640 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
1641 not force the value into range. */
1642
1643 void
1644 pixel_to_glyph_coords (f, pix_x, pix_y, x, y, bounds, noclip)
1645 FRAME_PTR f;
1646 register int pix_x, pix_y;
1647 int *x, *y;
1648 NativeRectangle *bounds;
1649 int noclip;
1650 {
1651
1652 #ifdef HAVE_WINDOW_SYSTEM
1653 if (FRAME_WINDOW_P (f))
1654 {
1655 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1656 even for negative values. */
1657 if (pix_x < 0)
1658 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1659 if (pix_y < 0)
1660 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1661
1662 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1663 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1664
1665 if (bounds)
1666 STORE_NATIVE_RECT (*bounds,
1667 FRAME_COL_TO_PIXEL_X (f, pix_x),
1668 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1669 FRAME_COLUMN_WIDTH (f) - 1,
1670 FRAME_LINE_HEIGHT (f) - 1);
1671
1672 if (!noclip)
1673 {
1674 if (pix_x < 0)
1675 pix_x = 0;
1676 else if (pix_x > FRAME_TOTAL_COLS (f))
1677 pix_x = FRAME_TOTAL_COLS (f);
1678
1679 if (pix_y < 0)
1680 pix_y = 0;
1681 else if (pix_y > FRAME_LINES (f))
1682 pix_y = FRAME_LINES (f);
1683 }
1684 }
1685 #endif
1686
1687 *x = pix_x;
1688 *y = pix_y;
1689 }
1690
1691
1692 /* Given HPOS/VPOS in the current matrix of W, return corresponding
1693 frame-relative pixel positions in *FRAME_X and *FRAME_Y. If we
1694 can't tell the positions because W's display is not up to date,
1695 return 0. */
1696
1697 int
1698 glyph_to_pixel_coords (w, hpos, vpos, frame_x, frame_y)
1699 struct window *w;
1700 int hpos, vpos;
1701 int *frame_x, *frame_y;
1702 {
1703 #ifdef HAVE_WINDOW_SYSTEM
1704 if (FRAME_WINDOW_P (XFRAME (WINDOW_FRAME (w))))
1705 {
1706 int success_p;
1707
1708 xassert (hpos >= 0 && hpos < w->current_matrix->matrix_w);
1709 xassert (vpos >= 0 && vpos < w->current_matrix->matrix_h);
1710
1711 if (display_completed)
1712 {
1713 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
1714 struct glyph *glyph = row->glyphs[TEXT_AREA];
1715 struct glyph *end = glyph + min (hpos, row->used[TEXT_AREA]);
1716
1717 hpos = row->x;
1718 vpos = row->y;
1719 while (glyph < end)
1720 {
1721 hpos += glyph->pixel_width;
1722 ++glyph;
1723 }
1724
1725 /* If first glyph is partially visible, its first visible position is still 0. */
1726 if (hpos < 0)
1727 hpos = 0;
1728
1729 success_p = 1;
1730 }
1731 else
1732 {
1733 hpos = vpos = 0;
1734 success_p = 0;
1735 }
1736
1737 *frame_x = WINDOW_TO_FRAME_PIXEL_X (w, hpos);
1738 *frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, vpos);
1739 return success_p;
1740 }
1741 #endif
1742
1743 *frame_x = hpos;
1744 *frame_y = vpos;
1745 return 1;
1746 }
1747
1748
1749 #ifdef HAVE_WINDOW_SYSTEM
1750
1751 /* Find the glyph under window-relative coordinates X/Y in window W.
1752 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1753 strings. Return in *HPOS and *VPOS the row and column number of
1754 the glyph found. Return in *AREA the glyph area containing X.
1755 Value is a pointer to the glyph found or null if X/Y is not on
1756 text, or we can't tell because W's current matrix is not up to
1757 date. */
1758
1759 static
1760 struct glyph *
1761 x_y_to_hpos_vpos (w, x, y, hpos, vpos, dx, dy, area)
1762 struct window *w;
1763 int x, y;
1764 int *hpos, *vpos, *dx, *dy, *area;
1765 {
1766 struct glyph *glyph, *end;
1767 struct glyph_row *row = NULL;
1768 int x0, i;
1769
1770 /* Find row containing Y. Give up if some row is not enabled. */
1771 for (i = 0; i < w->current_matrix->nrows; ++i)
1772 {
1773 row = MATRIX_ROW (w->current_matrix, i);
1774 if (!row->enabled_p)
1775 return NULL;
1776 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1777 break;
1778 }
1779
1780 *vpos = i;
1781 *hpos = 0;
1782
1783 /* Give up if Y is not in the window. */
1784 if (i == w->current_matrix->nrows)
1785 return NULL;
1786
1787 /* Get the glyph area containing X. */
1788 if (w->pseudo_window_p)
1789 {
1790 *area = TEXT_AREA;
1791 x0 = 0;
1792 }
1793 else
1794 {
1795 if (x < window_box_left_offset (w, TEXT_AREA))
1796 {
1797 *area = LEFT_MARGIN_AREA;
1798 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1799 }
1800 else if (x < window_box_right_offset (w, TEXT_AREA))
1801 {
1802 *area = TEXT_AREA;
1803 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
1804 }
1805 else
1806 {
1807 *area = RIGHT_MARGIN_AREA;
1808 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1809 }
1810 }
1811
1812 /* Find glyph containing X. */
1813 glyph = row->glyphs[*area];
1814 end = glyph + row->used[*area];
1815 x -= x0;
1816 while (glyph < end && x >= glyph->pixel_width)
1817 {
1818 x -= glyph->pixel_width;
1819 ++glyph;
1820 }
1821
1822 if (glyph == end)
1823 return NULL;
1824
1825 if (dx)
1826 {
1827 *dx = x;
1828 *dy = y - (row->y + row->ascent - glyph->ascent);
1829 }
1830
1831 *hpos = glyph - row->glyphs[*area];
1832 return glyph;
1833 }
1834
1835
1836 /* EXPORT:
1837 Convert frame-relative x/y to coordinates relative to window W.
1838 Takes pseudo-windows into account. */
1839
1840 void
1841 frame_to_window_pixel_xy (w, x, y)
1842 struct window *w;
1843 int *x, *y;
1844 {
1845 if (w->pseudo_window_p)
1846 {
1847 /* A pseudo-window is always full-width, and starts at the
1848 left edge of the frame, plus a frame border. */
1849 struct frame *f = XFRAME (w->frame);
1850 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
1851 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1852 }
1853 else
1854 {
1855 *x -= WINDOW_LEFT_EDGE_X (w);
1856 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1857 }
1858 }
1859
1860 /* EXPORT:
1861 Return in RECTS[] at most N clipping rectangles for glyph string S.
1862 Return the number of stored rectangles. */
1863
1864 int
1865 get_glyph_string_clip_rects (s, rects, n)
1866 struct glyph_string *s;
1867 NativeRectangle *rects;
1868 int n;
1869 {
1870 XRectangle r;
1871
1872 if (n <= 0)
1873 return 0;
1874
1875 if (s->row->full_width_p)
1876 {
1877 /* Draw full-width. X coordinates are relative to S->w->left_col. */
1878 r.x = WINDOW_LEFT_EDGE_X (s->w);
1879 r.width = WINDOW_TOTAL_WIDTH (s->w);
1880
1881 /* Unless displaying a mode or menu bar line, which are always
1882 fully visible, clip to the visible part of the row. */
1883 if (s->w->pseudo_window_p)
1884 r.height = s->row->visible_height;
1885 else
1886 r.height = s->height;
1887 }
1888 else
1889 {
1890 /* This is a text line that may be partially visible. */
1891 r.x = window_box_left (s->w, s->area);
1892 r.width = window_box_width (s->w, s->area);
1893 r.height = s->row->visible_height;
1894 }
1895
1896 if (s->clip_head)
1897 if (r.x < s->clip_head->x)
1898 {
1899 if (r.width >= s->clip_head->x - r.x)
1900 r.width -= s->clip_head->x - r.x;
1901 else
1902 r.width = 0;
1903 r.x = s->clip_head->x;
1904 }
1905 if (s->clip_tail)
1906 if (r.x + r.width > s->clip_tail->x + s->clip_tail->background_width)
1907 {
1908 if (s->clip_tail->x + s->clip_tail->background_width >= r.x)
1909 r.width = s->clip_tail->x + s->clip_tail->background_width - r.x;
1910 else
1911 r.width = 0;
1912 }
1913
1914 /* If S draws overlapping rows, it's sufficient to use the top and
1915 bottom of the window for clipping because this glyph string
1916 intentionally draws over other lines. */
1917 if (s->for_overlaps)
1918 {
1919 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1920 r.height = window_text_bottom_y (s->w) - r.y;
1921
1922 /* Alas, the above simple strategy does not work for the
1923 environments with anti-aliased text: if the same text is
1924 drawn onto the same place multiple times, it gets thicker.
1925 If the overlap we are processing is for the erased cursor, we
1926 take the intersection with the rectagle of the cursor. */
1927 if (s->for_overlaps & OVERLAPS_ERASED_CURSOR)
1928 {
1929 XRectangle rc, r_save = r;
1930
1931 rc.x = WINDOW_TEXT_TO_FRAME_PIXEL_X (s->w, s->w->phys_cursor.x);
1932 rc.y = s->w->phys_cursor.y;
1933 rc.width = s->w->phys_cursor_width;
1934 rc.height = s->w->phys_cursor_height;
1935
1936 x_intersect_rectangles (&r_save, &rc, &r);
1937 }
1938 }
1939 else
1940 {
1941 /* Don't use S->y for clipping because it doesn't take partially
1942 visible lines into account. For example, it can be negative for
1943 partially visible lines at the top of a window. */
1944 if (!s->row->full_width_p
1945 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
1946 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1947 else
1948 r.y = max (0, s->row->y);
1949
1950 /* If drawing a tool-bar window, draw it over the internal border
1951 at the top of the window. */
1952 if (WINDOWP (s->f->tool_bar_window)
1953 && s->w == XWINDOW (s->f->tool_bar_window))
1954 r.y -= FRAME_INTERNAL_BORDER_WIDTH (s->f);
1955 }
1956
1957 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
1958
1959 /* If drawing the cursor, don't let glyph draw outside its
1960 advertised boundaries. Cleartype does this under some circumstances. */
1961 if (s->hl == DRAW_CURSOR)
1962 {
1963 struct glyph *glyph = s->first_glyph;
1964 int height, max_y;
1965
1966 if (s->x > r.x)
1967 {
1968 r.width -= s->x - r.x;
1969 r.x = s->x;
1970 }
1971 r.width = min (r.width, glyph->pixel_width);
1972
1973 /* If r.y is below window bottom, ensure that we still see a cursor. */
1974 height = min (glyph->ascent + glyph->descent,
1975 min (FRAME_LINE_HEIGHT (s->f), s->row->visible_height));
1976 max_y = window_text_bottom_y (s->w) - height;
1977 max_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, max_y);
1978 if (s->ybase - glyph->ascent > max_y)
1979 {
1980 r.y = max_y;
1981 r.height = height;
1982 }
1983 else
1984 {
1985 /* Don't draw cursor glyph taller than our actual glyph. */
1986 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
1987 if (height < r.height)
1988 {
1989 max_y = r.y + r.height;
1990 r.y = min (max_y, max (r.y, s->ybase + glyph->descent - height));
1991 r.height = min (max_y - r.y, height);
1992 }
1993 }
1994 }
1995
1996 if (s->row->clip)
1997 {
1998 XRectangle r_save = r;
1999
2000 if (! x_intersect_rectangles (&r_save, s->row->clip, &r))
2001 r.width = 0;
2002 }
2003
2004 if ((s->for_overlaps & OVERLAPS_BOTH) == 0
2005 || ((s->for_overlaps & OVERLAPS_BOTH) == OVERLAPS_BOTH && n == 1))
2006 {
2007 #ifdef CONVERT_FROM_XRECT
2008 CONVERT_FROM_XRECT (r, *rects);
2009 #else
2010 *rects = r;
2011 #endif
2012 return 1;
2013 }
2014 else
2015 {
2016 /* If we are processing overlapping and allowed to return
2017 multiple clipping rectangles, we exclude the row of the glyph
2018 string from the clipping rectangle. This is to avoid drawing
2019 the same text on the environment with anti-aliasing. */
2020 #ifdef CONVERT_FROM_XRECT
2021 XRectangle rs[2];
2022 #else
2023 XRectangle *rs = rects;
2024 #endif
2025 int i = 0, row_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, s->row->y);
2026
2027 if (s->for_overlaps & OVERLAPS_PRED)
2028 {
2029 rs[i] = r;
2030 if (r.y + r.height > row_y)
2031 {
2032 if (r.y < row_y)
2033 rs[i].height = row_y - r.y;
2034 else
2035 rs[i].height = 0;
2036 }
2037 i++;
2038 }
2039 if (s->for_overlaps & OVERLAPS_SUCC)
2040 {
2041 rs[i] = r;
2042 if (r.y < row_y + s->row->visible_height)
2043 {
2044 if (r.y + r.height > row_y + s->row->visible_height)
2045 {
2046 rs[i].y = row_y + s->row->visible_height;
2047 rs[i].height = r.y + r.height - rs[i].y;
2048 }
2049 else
2050 rs[i].height = 0;
2051 }
2052 i++;
2053 }
2054
2055 n = i;
2056 #ifdef CONVERT_FROM_XRECT
2057 for (i = 0; i < n; i++)
2058 CONVERT_FROM_XRECT (rs[i], rects[i]);
2059 #endif
2060 return n;
2061 }
2062 }
2063
2064 /* EXPORT:
2065 Return in *NR the clipping rectangle for glyph string S. */
2066
2067 void
2068 get_glyph_string_clip_rect (s, nr)
2069 struct glyph_string *s;
2070 NativeRectangle *nr;
2071 {
2072 get_glyph_string_clip_rects (s, nr, 1);
2073 }
2074
2075
2076 /* EXPORT:
2077 Return the position and height of the phys cursor in window W.
2078 Set w->phys_cursor_width to width of phys cursor.
2079 */
2080
2081 void
2082 get_phys_cursor_geometry (w, row, glyph, xp, yp, heightp)
2083 struct window *w;
2084 struct glyph_row *row;
2085 struct glyph *glyph;
2086 int *xp, *yp, *heightp;
2087 {
2088 struct frame *f = XFRAME (WINDOW_FRAME (w));
2089 int x, y, wd, h, h0, y0;
2090
2091 /* Compute the width of the rectangle to draw. If on a stretch
2092 glyph, and `x-stretch-block-cursor' is nil, don't draw a
2093 rectangle as wide as the glyph, but use a canonical character
2094 width instead. */
2095 wd = glyph->pixel_width - 1;
2096 #if defined(HAVE_NTGUI) || defined(HAVE_NS)
2097 wd++; /* Why? */
2098 #endif
2099
2100 x = w->phys_cursor.x;
2101 if (x < 0)
2102 {
2103 wd += x;
2104 x = 0;
2105 }
2106
2107 if (glyph->type == STRETCH_GLYPH
2108 && !x_stretch_cursor_p)
2109 wd = min (FRAME_COLUMN_WIDTH (f), wd);
2110 w->phys_cursor_width = wd;
2111
2112 y = w->phys_cursor.y + row->ascent - glyph->ascent;
2113
2114 /* If y is below window bottom, ensure that we still see a cursor. */
2115 h0 = min (FRAME_LINE_HEIGHT (f), row->visible_height);
2116
2117 h = max (h0, glyph->ascent + glyph->descent);
2118 h0 = min (h0, glyph->ascent + glyph->descent);
2119
2120 y0 = WINDOW_HEADER_LINE_HEIGHT (w);
2121 if (y < y0)
2122 {
2123 h = max (h - (y0 - y) + 1, h0);
2124 y = y0 - 1;
2125 }
2126 else
2127 {
2128 y0 = window_text_bottom_y (w) - h0;
2129 if (y > y0)
2130 {
2131 h += y - y0;
2132 y = y0;
2133 }
2134 }
2135
2136 *xp = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
2137 *yp = WINDOW_TO_FRAME_PIXEL_Y (w, y);
2138 *heightp = h;
2139 }
2140
2141 /*
2142 * Remember which glyph the mouse is over.
2143 */
2144
2145 void
2146 remember_mouse_glyph (f, gx, gy, rect)
2147 struct frame *f;
2148 int gx, gy;
2149 NativeRectangle *rect;
2150 {
2151 Lisp_Object window;
2152 struct window *w;
2153 struct glyph_row *r, *gr, *end_row;
2154 enum window_part part;
2155 enum glyph_row_area area;
2156 int x, y, width, height;
2157
2158 /* Try to determine frame pixel position and size of the glyph under
2159 frame pixel coordinates X/Y on frame F. */
2160
2161 if (!f->glyphs_initialized_p
2162 || (window = window_from_coordinates (f, gx, gy, &part, &x, &y, 0),
2163 NILP (window)))
2164 {
2165 width = FRAME_SMALLEST_CHAR_WIDTH (f);
2166 height = FRAME_SMALLEST_FONT_HEIGHT (f);
2167 goto virtual_glyph;
2168 }
2169
2170 w = XWINDOW (window);
2171 width = WINDOW_FRAME_COLUMN_WIDTH (w);
2172 height = WINDOW_FRAME_LINE_HEIGHT (w);
2173
2174 r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
2175 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
2176
2177 if (w->pseudo_window_p)
2178 {
2179 area = TEXT_AREA;
2180 part = ON_MODE_LINE; /* Don't adjust margin. */
2181 goto text_glyph;
2182 }
2183
2184 switch (part)
2185 {
2186 case ON_LEFT_MARGIN:
2187 area = LEFT_MARGIN_AREA;
2188 goto text_glyph;
2189
2190 case ON_RIGHT_MARGIN:
2191 area = RIGHT_MARGIN_AREA;
2192 goto text_glyph;
2193
2194 case ON_HEADER_LINE:
2195 case ON_MODE_LINE:
2196 gr = (part == ON_HEADER_LINE
2197 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
2198 : MATRIX_MODE_LINE_ROW (w->current_matrix));
2199 gy = gr->y;
2200 area = TEXT_AREA;
2201 goto text_glyph_row_found;
2202
2203 case ON_TEXT:
2204 area = TEXT_AREA;
2205
2206 text_glyph:
2207 gr = 0; gy = 0;
2208 for (; r <= end_row && r->enabled_p; ++r)
2209 if (r->y + r->height > y)
2210 {
2211 gr = r; gy = r->y;
2212 break;
2213 }
2214
2215 text_glyph_row_found:
2216 if (gr && gy <= y)
2217 {
2218 struct glyph *g = gr->glyphs[area];
2219 struct glyph *end = g + gr->used[area];
2220
2221 height = gr->height;
2222 for (gx = gr->x; g < end; gx += g->pixel_width, ++g)
2223 if (gx + g->pixel_width > x)
2224 break;
2225
2226 if (g < end)
2227 {
2228 if (g->type == IMAGE_GLYPH)
2229 {
2230 /* Don't remember when mouse is over image, as
2231 image may have hot-spots. */
2232 STORE_NATIVE_RECT (*rect, 0, 0, 0, 0);
2233 return;
2234 }
2235 width = g->pixel_width;
2236 }
2237 else
2238 {
2239 /* Use nominal char spacing at end of line. */
2240 x -= gx;
2241 gx += (x / width) * width;
2242 }
2243
2244 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2245 gx += window_box_left_offset (w, area);
2246 }
2247 else
2248 {
2249 /* Use nominal line height at end of window. */
2250 gx = (x / width) * width;
2251 y -= gy;
2252 gy += (y / height) * height;
2253 }
2254 break;
2255
2256 case ON_LEFT_FRINGE:
2257 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2258 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w)
2259 : window_box_right_offset (w, LEFT_MARGIN_AREA));
2260 width = WINDOW_LEFT_FRINGE_WIDTH (w);
2261 goto row_glyph;
2262
2263 case ON_RIGHT_FRINGE:
2264 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2265 ? window_box_right_offset (w, RIGHT_MARGIN_AREA)
2266 : window_box_right_offset (w, TEXT_AREA));
2267 width = WINDOW_RIGHT_FRINGE_WIDTH (w);
2268 goto row_glyph;
2269
2270 case ON_SCROLL_BAR:
2271 gx = (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
2272 ? 0
2273 : (window_box_right_offset (w, RIGHT_MARGIN_AREA)
2274 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2275 ? WINDOW_RIGHT_FRINGE_WIDTH (w)
2276 : 0)));
2277 width = WINDOW_SCROLL_BAR_AREA_WIDTH (w);
2278
2279 row_glyph:
2280 gr = 0, gy = 0;
2281 for (; r <= end_row && r->enabled_p; ++r)
2282 if (r->y + r->height > y)
2283 {
2284 gr = r; gy = r->y;
2285 break;
2286 }
2287
2288 if (gr && gy <= y)
2289 height = gr->height;
2290 else
2291 {
2292 /* Use nominal line height at end of window. */
2293 y -= gy;
2294 gy += (y / height) * height;
2295 }
2296 break;
2297
2298 default:
2299 ;
2300 virtual_glyph:
2301 /* If there is no glyph under the mouse, then we divide the screen
2302 into a grid of the smallest glyph in the frame, and use that
2303 as our "glyph". */
2304
2305 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to
2306 round down even for negative values. */
2307 if (gx < 0)
2308 gx -= width - 1;
2309 if (gy < 0)
2310 gy -= height - 1;
2311
2312 gx = (gx / width) * width;
2313 gy = (gy / height) * height;
2314
2315 goto store_rect;
2316 }
2317
2318 gx += WINDOW_LEFT_EDGE_X (w);
2319 gy += WINDOW_TOP_EDGE_Y (w);
2320
2321 store_rect:
2322 STORE_NATIVE_RECT (*rect, gx, gy, width, height);
2323
2324 /* Visible feedback for debugging. */
2325 #if 0
2326 #if HAVE_X_WINDOWS
2327 XDrawRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2328 f->output_data.x->normal_gc,
2329 gx, gy, width, height);
2330 #endif
2331 #endif
2332 }
2333
2334
2335 #endif /* HAVE_WINDOW_SYSTEM */
2336
2337 \f
2338 /***********************************************************************
2339 Lisp form evaluation
2340 ***********************************************************************/
2341
2342 /* Error handler for safe_eval and safe_call. */
2343
2344 static Lisp_Object
2345 safe_eval_handler (arg)
2346 Lisp_Object arg;
2347 {
2348 add_to_log ("Error during redisplay: %s", arg, Qnil);
2349 return Qnil;
2350 }
2351
2352
2353 /* Evaluate SEXPR and return the result, or nil if something went
2354 wrong. Prevent redisplay during the evaluation. */
2355
2356 /* Call function ARGS[0] with arguments ARGS[1] to ARGS[NARGS - 1].
2357 Return the result, or nil if something went wrong. Prevent
2358 redisplay during the evaluation. */
2359
2360 Lisp_Object
2361 safe_call (nargs, args)
2362 int nargs;
2363 Lisp_Object *args;
2364 {
2365 Lisp_Object val;
2366
2367 if (inhibit_eval_during_redisplay)
2368 val = Qnil;
2369 else
2370 {
2371 int count = SPECPDL_INDEX ();
2372 struct gcpro gcpro1;
2373
2374 GCPRO1 (args[0]);
2375 gcpro1.nvars = nargs;
2376 specbind (Qinhibit_redisplay, Qt);
2377 /* Use Qt to ensure debugger does not run,
2378 so there is no possibility of wanting to redisplay. */
2379 val = internal_condition_case_2 (Ffuncall, nargs, args, Qt,
2380 safe_eval_handler);
2381 UNGCPRO;
2382 val = unbind_to (count, val);
2383 }
2384
2385 return val;
2386 }
2387
2388
2389 /* Call function FN with one argument ARG.
2390 Return the result, or nil if something went wrong. */
2391
2392 Lisp_Object
2393 safe_call1 (fn, arg)
2394 Lisp_Object fn, arg;
2395 {
2396 Lisp_Object args[2];
2397 args[0] = fn;
2398 args[1] = arg;
2399 return safe_call (2, args);
2400 }
2401
2402 static Lisp_Object Qeval;
2403
2404 Lisp_Object
2405 safe_eval (Lisp_Object sexpr)
2406 {
2407 return safe_call1 (Qeval, sexpr);
2408 }
2409
2410 /* Call function FN with one argument ARG.
2411 Return the result, or nil if something went wrong. */
2412
2413 Lisp_Object
2414 safe_call2 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2)
2415 {
2416 Lisp_Object args[3];
2417 args[0] = fn;
2418 args[1] = arg1;
2419 args[2] = arg2;
2420 return safe_call (3, args);
2421 }
2422
2423
2424 \f
2425 /***********************************************************************
2426 Debugging
2427 ***********************************************************************/
2428
2429 #if 0
2430
2431 /* Define CHECK_IT to perform sanity checks on iterators.
2432 This is for debugging. It is too slow to do unconditionally. */
2433
2434 static void
2435 check_it (it)
2436 struct it *it;
2437 {
2438 if (it->method == GET_FROM_STRING)
2439 {
2440 xassert (STRINGP (it->string));
2441 xassert (IT_STRING_CHARPOS (*it) >= 0);
2442 }
2443 else
2444 {
2445 xassert (IT_STRING_CHARPOS (*it) < 0);
2446 if (it->method == GET_FROM_BUFFER)
2447 {
2448 /* Check that character and byte positions agree. */
2449 xassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
2450 }
2451 }
2452
2453 if (it->dpvec)
2454 xassert (it->current.dpvec_index >= 0);
2455 else
2456 xassert (it->current.dpvec_index < 0);
2457 }
2458
2459 #define CHECK_IT(IT) check_it ((IT))
2460
2461 #else /* not 0 */
2462
2463 #define CHECK_IT(IT) (void) 0
2464
2465 #endif /* not 0 */
2466
2467
2468 #if GLYPH_DEBUG
2469
2470 /* Check that the window end of window W is what we expect it
2471 to be---the last row in the current matrix displaying text. */
2472
2473 static void
2474 check_window_end (w)
2475 struct window *w;
2476 {
2477 if (!MINI_WINDOW_P (w)
2478 && !NILP (w->window_end_valid))
2479 {
2480 struct glyph_row *row;
2481 xassert ((row = MATRIX_ROW (w->current_matrix,
2482 XFASTINT (w->window_end_vpos)),
2483 !row->enabled_p
2484 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
2485 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
2486 }
2487 }
2488
2489 #define CHECK_WINDOW_END(W) check_window_end ((W))
2490
2491 #else /* not GLYPH_DEBUG */
2492
2493 #define CHECK_WINDOW_END(W) (void) 0
2494
2495 #endif /* not GLYPH_DEBUG */
2496
2497
2498 \f
2499 /***********************************************************************
2500 Iterator initialization
2501 ***********************************************************************/
2502
2503 /* Initialize IT for displaying current_buffer in window W, starting
2504 at character position CHARPOS. CHARPOS < 0 means that no buffer
2505 position is specified which is useful when the iterator is assigned
2506 a position later. BYTEPOS is the byte position corresponding to
2507 CHARPOS. BYTEPOS < 0 means compute it from CHARPOS.
2508
2509 If ROW is not null, calls to produce_glyphs with IT as parameter
2510 will produce glyphs in that row.
2511
2512 BASE_FACE_ID is the id of a base face to use. It must be one of
2513 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2514 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2515 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2516
2517 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2518 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2519 will be initialized to use the corresponding mode line glyph row of
2520 the desired matrix of W. */
2521
2522 void
2523 init_iterator (it, w, charpos, bytepos, row, base_face_id)
2524 struct it *it;
2525 struct window *w;
2526 int charpos, bytepos;
2527 struct glyph_row *row;
2528 enum face_id base_face_id;
2529 {
2530 int highlight_region_p;
2531 enum face_id remapped_base_face_id = base_face_id;
2532
2533 /* Some precondition checks. */
2534 xassert (w != NULL && it != NULL);
2535 xassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2536 && charpos <= ZV));
2537
2538 /* If face attributes have been changed since the last redisplay,
2539 free realized faces now because they depend on face definitions
2540 that might have changed. Don't free faces while there might be
2541 desired matrices pending which reference these faces. */
2542 if (face_change_count && !inhibit_free_realized_faces)
2543 {
2544 face_change_count = 0;
2545 free_all_realized_faces (Qnil);
2546 }
2547
2548 /* Perhaps remap BASE_FACE_ID to a user-specified alternative. */
2549 if (! NILP (Vface_remapping_alist))
2550 remapped_base_face_id = lookup_basic_face (XFRAME (w->frame), base_face_id);
2551
2552 /* Use one of the mode line rows of W's desired matrix if
2553 appropriate. */
2554 if (row == NULL)
2555 {
2556 if (base_face_id == MODE_LINE_FACE_ID
2557 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2558 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2559 else if (base_face_id == HEADER_LINE_FACE_ID)
2560 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2561 }
2562
2563 /* Clear IT. */
2564 bzero (it, sizeof *it);
2565 it->current.overlay_string_index = -1;
2566 it->current.dpvec_index = -1;
2567 it->base_face_id = remapped_base_face_id;
2568 it->string = Qnil;
2569 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2570
2571 /* The window in which we iterate over current_buffer: */
2572 XSETWINDOW (it->window, w);
2573 it->w = w;
2574 it->f = XFRAME (w->frame);
2575
2576 it->cmp_it.id = -1;
2577
2578 /* Extra space between lines (on window systems only). */
2579 if (base_face_id == DEFAULT_FACE_ID
2580 && FRAME_WINDOW_P (it->f))
2581 {
2582 if (NATNUMP (current_buffer->extra_line_spacing))
2583 it->extra_line_spacing = XFASTINT (current_buffer->extra_line_spacing);
2584 else if (FLOATP (current_buffer->extra_line_spacing))
2585 it->extra_line_spacing = (XFLOAT_DATA (current_buffer->extra_line_spacing)
2586 * FRAME_LINE_HEIGHT (it->f));
2587 else if (it->f->extra_line_spacing > 0)
2588 it->extra_line_spacing = it->f->extra_line_spacing;
2589 it->max_extra_line_spacing = 0;
2590 }
2591
2592 /* If realized faces have been removed, e.g. because of face
2593 attribute changes of named faces, recompute them. When running
2594 in batch mode, the face cache of the initial frame is null. If
2595 we happen to get called, make a dummy face cache. */
2596 if (FRAME_FACE_CACHE (it->f) == NULL)
2597 init_frame_faces (it->f);
2598 if (FRAME_FACE_CACHE (it->f)->used == 0)
2599 recompute_basic_faces (it->f);
2600
2601 /* Current value of the `slice', `space-width', and 'height' properties. */
2602 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
2603 it->space_width = Qnil;
2604 it->font_height = Qnil;
2605 it->override_ascent = -1;
2606
2607 /* Are control characters displayed as `^C'? */
2608 it->ctl_arrow_p = !NILP (current_buffer->ctl_arrow);
2609
2610 /* -1 means everything between a CR and the following line end
2611 is invisible. >0 means lines indented more than this value are
2612 invisible. */
2613 it->selective = (INTEGERP (current_buffer->selective_display)
2614 ? XFASTINT (current_buffer->selective_display)
2615 : (!NILP (current_buffer->selective_display)
2616 ? -1 : 0));
2617 it->selective_display_ellipsis_p
2618 = !NILP (current_buffer->selective_display_ellipses);
2619
2620 /* Display table to use. */
2621 it->dp = window_display_table (w);
2622
2623 /* Are multibyte characters enabled in current_buffer? */
2624 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
2625
2626 /* Non-zero if we should highlight the region. */
2627 highlight_region_p
2628 = (!NILP (Vtransient_mark_mode)
2629 && !NILP (current_buffer->mark_active)
2630 && XMARKER (current_buffer->mark)->buffer != 0);
2631
2632 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
2633 start and end of a visible region in window IT->w. Set both to
2634 -1 to indicate no region. */
2635 if (highlight_region_p
2636 /* Maybe highlight only in selected window. */
2637 && (/* Either show region everywhere. */
2638 highlight_nonselected_windows
2639 /* Or show region in the selected window. */
2640 || w == XWINDOW (selected_window)
2641 /* Or show the region if we are in the mini-buffer and W is
2642 the window the mini-buffer refers to. */
2643 || (MINI_WINDOW_P (XWINDOW (selected_window))
2644 && WINDOWP (minibuf_selected_window)
2645 && w == XWINDOW (minibuf_selected_window))))
2646 {
2647 int charpos = marker_position (current_buffer->mark);
2648 it->region_beg_charpos = min (PT, charpos);
2649 it->region_end_charpos = max (PT, charpos);
2650 }
2651 else
2652 it->region_beg_charpos = it->region_end_charpos = -1;
2653
2654 /* Get the position at which the redisplay_end_trigger hook should
2655 be run, if it is to be run at all. */
2656 if (MARKERP (w->redisplay_end_trigger)
2657 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2658 it->redisplay_end_trigger_charpos
2659 = marker_position (w->redisplay_end_trigger);
2660 else if (INTEGERP (w->redisplay_end_trigger))
2661 it->redisplay_end_trigger_charpos = XINT (w->redisplay_end_trigger);
2662
2663 /* Correct bogus values of tab_width. */
2664 it->tab_width = XINT (current_buffer->tab_width);
2665 if (it->tab_width <= 0 || it->tab_width > 1000)
2666 it->tab_width = 8;
2667
2668 /* Are lines in the display truncated? */
2669 if (base_face_id != DEFAULT_FACE_ID
2670 || XINT (it->w->hscroll)
2671 || (! WINDOW_FULL_WIDTH_P (it->w)
2672 && ((!NILP (Vtruncate_partial_width_windows)
2673 && !INTEGERP (Vtruncate_partial_width_windows))
2674 || (INTEGERP (Vtruncate_partial_width_windows)
2675 && (WINDOW_TOTAL_COLS (it->w)
2676 < XINT (Vtruncate_partial_width_windows))))))
2677 it->line_wrap = TRUNCATE;
2678 else if (NILP (current_buffer->truncate_lines))
2679 it->line_wrap = NILP (current_buffer->word_wrap)
2680 ? WINDOW_WRAP : WORD_WRAP;
2681 else
2682 it->line_wrap = TRUNCATE;
2683
2684 /* Get dimensions of truncation and continuation glyphs. These are
2685 displayed as fringe bitmaps under X, so we don't need them for such
2686 frames. */
2687 if (!FRAME_WINDOW_P (it->f))
2688 {
2689 if (it->line_wrap == TRUNCATE)
2690 {
2691 /* We will need the truncation glyph. */
2692 xassert (it->glyph_row == NULL);
2693 produce_special_glyphs (it, IT_TRUNCATION);
2694 it->truncation_pixel_width = it->pixel_width;
2695 }
2696 else
2697 {
2698 /* We will need the continuation glyph. */
2699 xassert (it->glyph_row == NULL);
2700 produce_special_glyphs (it, IT_CONTINUATION);
2701 it->continuation_pixel_width = it->pixel_width;
2702 }
2703
2704 /* Reset these values to zero because the produce_special_glyphs
2705 above has changed them. */
2706 it->pixel_width = it->ascent = it->descent = 0;
2707 it->phys_ascent = it->phys_descent = 0;
2708 }
2709
2710 /* Set this after getting the dimensions of truncation and
2711 continuation glyphs, so that we don't produce glyphs when calling
2712 produce_special_glyphs, above. */
2713 it->glyph_row = row;
2714 it->area = TEXT_AREA;
2715
2716 /* Get the dimensions of the display area. The display area
2717 consists of the visible window area plus a horizontally scrolled
2718 part to the left of the window. All x-values are relative to the
2719 start of this total display area. */
2720 if (base_face_id != DEFAULT_FACE_ID)
2721 {
2722 /* Mode lines, menu bar in terminal frames. */
2723 it->first_visible_x = 0;
2724 it->last_visible_x = WINDOW_TOTAL_WIDTH (w);
2725 }
2726 else
2727 {
2728 it->first_visible_x
2729 = XFASTINT (it->w->hscroll) * FRAME_COLUMN_WIDTH (it->f);
2730 it->last_visible_x = (it->first_visible_x
2731 + window_box_width (w, TEXT_AREA));
2732
2733 /* If we truncate lines, leave room for the truncator glyph(s) at
2734 the right margin. Otherwise, leave room for the continuation
2735 glyph(s). Truncation and continuation glyphs are not inserted
2736 for window-based redisplay. */
2737 if (!FRAME_WINDOW_P (it->f))
2738 {
2739 if (it->line_wrap == TRUNCATE)
2740 it->last_visible_x -= it->truncation_pixel_width;
2741 else
2742 it->last_visible_x -= it->continuation_pixel_width;
2743 }
2744
2745 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2746 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2747 }
2748
2749 /* Leave room for a border glyph. */
2750 if (!FRAME_WINDOW_P (it->f)
2751 && !WINDOW_RIGHTMOST_P (it->w))
2752 it->last_visible_x -= 1;
2753
2754 it->last_visible_y = window_text_bottom_y (w);
2755
2756 /* For mode lines and alike, arrange for the first glyph having a
2757 left box line if the face specifies a box. */
2758 if (base_face_id != DEFAULT_FACE_ID)
2759 {
2760 struct face *face;
2761
2762 it->face_id = remapped_base_face_id;
2763
2764 /* If we have a boxed mode line, make the first character appear
2765 with a left box line. */
2766 face = FACE_FROM_ID (it->f, remapped_base_face_id);
2767 if (face->box != FACE_NO_BOX)
2768 it->start_of_box_run_p = 1;
2769 }
2770
2771 /* If a buffer position was specified, set the iterator there,
2772 getting overlays and face properties from that position. */
2773 if (charpos >= BUF_BEG (current_buffer))
2774 {
2775 it->end_charpos = ZV;
2776 it->face_id = -1;
2777 IT_CHARPOS (*it) = charpos;
2778
2779 /* Compute byte position if not specified. */
2780 if (bytepos < charpos)
2781 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
2782 else
2783 IT_BYTEPOS (*it) = bytepos;
2784
2785 it->start = it->current;
2786
2787 /* Compute faces etc. */
2788 reseat (it, it->current.pos, 1);
2789 }
2790
2791 CHECK_IT (it);
2792 }
2793
2794
2795 /* Initialize IT for the display of window W with window start POS. */
2796
2797 void
2798 start_display (it, w, pos)
2799 struct it *it;
2800 struct window *w;
2801 struct text_pos pos;
2802 {
2803 struct glyph_row *row;
2804 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
2805
2806 row = w->desired_matrix->rows + first_vpos;
2807 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
2808 it->first_vpos = first_vpos;
2809
2810 /* Don't reseat to previous visible line start if current start
2811 position is in a string or image. */
2812 if (it->method == GET_FROM_BUFFER && it->line_wrap != TRUNCATE)
2813 {
2814 int start_at_line_beg_p;
2815 int first_y = it->current_y;
2816
2817 /* If window start is not at a line start, skip forward to POS to
2818 get the correct continuation lines width. */
2819 start_at_line_beg_p = (CHARPOS (pos) == BEGV
2820 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
2821 if (!start_at_line_beg_p)
2822 {
2823 int new_x;
2824
2825 reseat_at_previous_visible_line_start (it);
2826 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
2827
2828 new_x = it->current_x + it->pixel_width;
2829
2830 /* If lines are continued, this line may end in the middle
2831 of a multi-glyph character (e.g. a control character
2832 displayed as \003, or in the middle of an overlay
2833 string). In this case move_it_to above will not have
2834 taken us to the start of the continuation line but to the
2835 end of the continued line. */
2836 if (it->current_x > 0
2837 && it->line_wrap != TRUNCATE /* Lines are continued. */
2838 && (/* And glyph doesn't fit on the line. */
2839 new_x > it->last_visible_x
2840 /* Or it fits exactly and we're on a window
2841 system frame. */
2842 || (new_x == it->last_visible_x
2843 && FRAME_WINDOW_P (it->f))))
2844 {
2845 if (it->current.dpvec_index >= 0
2846 || it->current.overlay_string_index >= 0)
2847 {
2848 set_iterator_to_next (it, 1);
2849 move_it_in_display_line_to (it, -1, -1, 0);
2850 }
2851
2852 it->continuation_lines_width += it->current_x;
2853 }
2854
2855 /* We're starting a new display line, not affected by the
2856 height of the continued line, so clear the appropriate
2857 fields in the iterator structure. */
2858 it->max_ascent = it->max_descent = 0;
2859 it->max_phys_ascent = it->max_phys_descent = 0;
2860
2861 it->current_y = first_y;
2862 it->vpos = 0;
2863 it->current_x = it->hpos = 0;
2864 }
2865 }
2866
2867 #if 0 /* Don't assert the following because start_display is sometimes
2868 called intentionally with a window start that is not at a
2869 line start. Please leave this code in as a comment. */
2870
2871 /* Window start should be on a line start, now. */
2872 xassert (it->continuation_lines_width
2873 || IT_CHARPOS (it) == BEGV
2874 || FETCH_BYTE (IT_BYTEPOS (it) - 1) == '\n');
2875 #endif /* 0 */
2876 }
2877
2878
2879 /* Return 1 if POS is a position in ellipses displayed for invisible
2880 text. W is the window we display, for text property lookup. */
2881
2882 static int
2883 in_ellipses_for_invisible_text_p (pos, w)
2884 struct display_pos *pos;
2885 struct window *w;
2886 {
2887 Lisp_Object prop, window;
2888 int ellipses_p = 0;
2889 int charpos = CHARPOS (pos->pos);
2890
2891 /* If POS specifies a position in a display vector, this might
2892 be for an ellipsis displayed for invisible text. We won't
2893 get the iterator set up for delivering that ellipsis unless
2894 we make sure that it gets aware of the invisible text. */
2895 if (pos->dpvec_index >= 0
2896 && pos->overlay_string_index < 0
2897 && CHARPOS (pos->string_pos) < 0
2898 && charpos > BEGV
2899 && (XSETWINDOW (window, w),
2900 prop = Fget_char_property (make_number (charpos),
2901 Qinvisible, window),
2902 !TEXT_PROP_MEANS_INVISIBLE (prop)))
2903 {
2904 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
2905 window);
2906 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
2907 }
2908
2909 return ellipses_p;
2910 }
2911
2912
2913 /* Initialize IT for stepping through current_buffer in window W,
2914 starting at position POS that includes overlay string and display
2915 vector/ control character translation position information. Value
2916 is zero if there are overlay strings with newlines at POS. */
2917
2918 static int
2919 init_from_display_pos (it, w, pos)
2920 struct it *it;
2921 struct window *w;
2922 struct display_pos *pos;
2923 {
2924 int charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
2925 int i, overlay_strings_with_newlines = 0;
2926
2927 /* If POS specifies a position in a display vector, this might
2928 be for an ellipsis displayed for invisible text. We won't
2929 get the iterator set up for delivering that ellipsis unless
2930 we make sure that it gets aware of the invisible text. */
2931 if (in_ellipses_for_invisible_text_p (pos, w))
2932 {
2933 --charpos;
2934 bytepos = 0;
2935 }
2936
2937 /* Keep in mind: the call to reseat in init_iterator skips invisible
2938 text, so we might end up at a position different from POS. This
2939 is only a problem when POS is a row start after a newline and an
2940 overlay starts there with an after-string, and the overlay has an
2941 invisible property. Since we don't skip invisible text in
2942 display_line and elsewhere immediately after consuming the
2943 newline before the row start, such a POS will not be in a string,
2944 but the call to init_iterator below will move us to the
2945 after-string. */
2946 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
2947
2948 /* This only scans the current chunk -- it should scan all chunks.
2949 However, OVERLAY_STRING_CHUNK_SIZE has been increased from 3 in 21.1
2950 to 16 in 22.1 to make this a lesser problem. */
2951 for (i = 0; i < it->n_overlay_strings && i < OVERLAY_STRING_CHUNK_SIZE; ++i)
2952 {
2953 const char *s = SDATA (it->overlay_strings[i]);
2954 const char *e = s + SBYTES (it->overlay_strings[i]);
2955
2956 while (s < e && *s != '\n')
2957 ++s;
2958
2959 if (s < e)
2960 {
2961 overlay_strings_with_newlines = 1;
2962 break;
2963 }
2964 }
2965
2966 /* If position is within an overlay string, set up IT to the right
2967 overlay string. */
2968 if (pos->overlay_string_index >= 0)
2969 {
2970 int relative_index;
2971
2972 /* If the first overlay string happens to have a `display'
2973 property for an image, the iterator will be set up for that
2974 image, and we have to undo that setup first before we can
2975 correct the overlay string index. */
2976 if (it->method == GET_FROM_IMAGE)
2977 pop_it (it);
2978
2979 /* We already have the first chunk of overlay strings in
2980 IT->overlay_strings. Load more until the one for
2981 pos->overlay_string_index is in IT->overlay_strings. */
2982 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
2983 {
2984 int n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
2985 it->current.overlay_string_index = 0;
2986 while (n--)
2987 {
2988 load_overlay_strings (it, 0);
2989 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
2990 }
2991 }
2992
2993 it->current.overlay_string_index = pos->overlay_string_index;
2994 relative_index = (it->current.overlay_string_index
2995 % OVERLAY_STRING_CHUNK_SIZE);
2996 it->string = it->overlay_strings[relative_index];
2997 xassert (STRINGP (it->string));
2998 it->current.string_pos = pos->string_pos;
2999 it->method = GET_FROM_STRING;
3000 }
3001
3002 #if 0 /* This is bogus because POS not having an overlay string
3003 position does not mean it's after the string. Example: A
3004 line starting with a before-string and initialization of IT
3005 to the previous row's end position. */
3006 else if (it->current.overlay_string_index >= 0)
3007 {
3008 /* If POS says we're already after an overlay string ending at
3009 POS, make sure to pop the iterator because it will be in
3010 front of that overlay string. When POS is ZV, we've thereby
3011 also ``processed'' overlay strings at ZV. */
3012 while (it->sp)
3013 pop_it (it);
3014 xassert (it->current.overlay_string_index == -1);
3015 xassert (it->method == GET_FROM_BUFFER);
3016 if (CHARPOS (pos->pos) == ZV)
3017 it->overlay_strings_at_end_processed_p = 1;
3018 }
3019 #endif /* 0 */
3020
3021 if (CHARPOS (pos->string_pos) >= 0)
3022 {
3023 /* Recorded position is not in an overlay string, but in another
3024 string. This can only be a string from a `display' property.
3025 IT should already be filled with that string. */
3026 it->current.string_pos = pos->string_pos;
3027 xassert (STRINGP (it->string));
3028 }
3029
3030 /* Restore position in display vector translations, control
3031 character translations or ellipses. */
3032 if (pos->dpvec_index >= 0)
3033 {
3034 if (it->dpvec == NULL)
3035 get_next_display_element (it);
3036 xassert (it->dpvec && it->current.dpvec_index == 0);
3037 it->current.dpvec_index = pos->dpvec_index;
3038 }
3039
3040 CHECK_IT (it);
3041 return !overlay_strings_with_newlines;
3042 }
3043
3044
3045 /* Initialize IT for stepping through current_buffer in window W
3046 starting at ROW->start. */
3047
3048 static void
3049 init_to_row_start (it, w, row)
3050 struct it *it;
3051 struct window *w;
3052 struct glyph_row *row;
3053 {
3054 init_from_display_pos (it, w, &row->start);
3055 it->start = row->start;
3056 it->continuation_lines_width = row->continuation_lines_width;
3057 CHECK_IT (it);
3058 }
3059
3060
3061 /* Initialize IT for stepping through current_buffer in window W
3062 starting in the line following ROW, i.e. starting at ROW->end.
3063 Value is zero if there are overlay strings with newlines at ROW's
3064 end position. */
3065
3066 static int
3067 init_to_row_end (it, w, row)
3068 struct it *it;
3069 struct window *w;
3070 struct glyph_row *row;
3071 {
3072 int success = 0;
3073
3074 if (init_from_display_pos (it, w, &row->end))
3075 {
3076 if (row->continued_p)
3077 it->continuation_lines_width
3078 = row->continuation_lines_width + row->pixel_width;
3079 CHECK_IT (it);
3080 success = 1;
3081 }
3082
3083 return success;
3084 }
3085
3086
3087
3088 \f
3089 /***********************************************************************
3090 Text properties
3091 ***********************************************************************/
3092
3093 /* Called when IT reaches IT->stop_charpos. Handle text property and
3094 overlay changes. Set IT->stop_charpos to the next position where
3095 to stop. */
3096
3097 static void
3098 handle_stop (it)
3099 struct it *it;
3100 {
3101 enum prop_handled handled;
3102 int handle_overlay_change_p;
3103 struct props *p;
3104
3105 it->dpvec = NULL;
3106 it->current.dpvec_index = -1;
3107 handle_overlay_change_p = !it->ignore_overlay_strings_at_pos_p;
3108 it->ignore_overlay_strings_at_pos_p = 0;
3109 it->ellipsis_p = 0;
3110
3111 /* Use face of preceding text for ellipsis (if invisible) */
3112 if (it->selective_display_ellipsis_p)
3113 it->saved_face_id = it->face_id;
3114
3115 do
3116 {
3117 handled = HANDLED_NORMALLY;
3118
3119 /* Call text property handlers. */
3120 for (p = it_props; p->handler; ++p)
3121 {
3122 handled = p->handler (it);
3123
3124 if (handled == HANDLED_RECOMPUTE_PROPS)
3125 break;
3126 else if (handled == HANDLED_RETURN)
3127 {
3128 /* We still want to show before and after strings from
3129 overlays even if the actual buffer text is replaced. */
3130 if (!handle_overlay_change_p
3131 || it->sp > 1
3132 || !get_overlay_strings_1 (it, 0, 0))
3133 {
3134 if (it->ellipsis_p)
3135 setup_for_ellipsis (it, 0);
3136 return;
3137 }
3138 it->ignore_overlay_strings_at_pos_p = 1;
3139 it->string_from_display_prop_p = 0;
3140 handle_overlay_change_p = 0;
3141 handled = HANDLED_RECOMPUTE_PROPS;
3142 break;
3143 }
3144 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
3145 handle_overlay_change_p = 0;
3146 }
3147
3148 if (handled != HANDLED_RECOMPUTE_PROPS)
3149 {
3150 /* Don't check for overlay strings below when set to deliver
3151 characters from a display vector. */
3152 if (it->method == GET_FROM_DISPLAY_VECTOR)
3153 handle_overlay_change_p = 0;
3154
3155 /* Handle overlay changes.
3156 This sets HANDLED to HANDLED_RECOMPUTE_PROPS
3157 if it finds overlays. */
3158 if (handle_overlay_change_p)
3159 handled = handle_overlay_change (it);
3160 }
3161
3162 if (it->ellipsis_p)
3163 {
3164 setup_for_ellipsis (it, 0);
3165 break;
3166 }
3167 }
3168 while (handled == HANDLED_RECOMPUTE_PROPS);
3169
3170 /* Determine where to stop next. */
3171 if (handled == HANDLED_NORMALLY)
3172 compute_stop_pos (it);
3173 }
3174
3175
3176 /* Compute IT->stop_charpos from text property and overlay change
3177 information for IT's current position. */
3178
3179 static void
3180 compute_stop_pos (it)
3181 struct it *it;
3182 {
3183 register INTERVAL iv, next_iv;
3184 Lisp_Object object, limit, position;
3185 EMACS_INT charpos, bytepos;
3186
3187 /* If nowhere else, stop at the end. */
3188 it->stop_charpos = it->end_charpos;
3189
3190 if (STRINGP (it->string))
3191 {
3192 /* Strings are usually short, so don't limit the search for
3193 properties. */
3194 object = it->string;
3195 limit = Qnil;
3196 charpos = IT_STRING_CHARPOS (*it);
3197 bytepos = IT_STRING_BYTEPOS (*it);
3198 }
3199 else
3200 {
3201 EMACS_INT pos;
3202
3203 /* If next overlay change is in front of the current stop pos
3204 (which is IT->end_charpos), stop there. Note: value of
3205 next_overlay_change is point-max if no overlay change
3206 follows. */
3207 charpos = IT_CHARPOS (*it);
3208 bytepos = IT_BYTEPOS (*it);
3209 pos = next_overlay_change (charpos);
3210 if (pos < it->stop_charpos)
3211 it->stop_charpos = pos;
3212
3213 /* If showing the region, we have to stop at the region
3214 start or end because the face might change there. */
3215 if (it->region_beg_charpos > 0)
3216 {
3217 if (IT_CHARPOS (*it) < it->region_beg_charpos)
3218 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
3219 else if (IT_CHARPOS (*it) < it->region_end_charpos)
3220 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
3221 }
3222
3223 /* Set up variables for computing the stop position from text
3224 property changes. */
3225 XSETBUFFER (object, current_buffer);
3226 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
3227 }
3228
3229 /* Get the interval containing IT's position. Value is a null
3230 interval if there isn't such an interval. */
3231 position = make_number (charpos);
3232 iv = validate_interval_range (object, &position, &position, 0);
3233 if (!NULL_INTERVAL_P (iv))
3234 {
3235 Lisp_Object values_here[LAST_PROP_IDX];
3236 struct props *p;
3237
3238 /* Get properties here. */
3239 for (p = it_props; p->handler; ++p)
3240 values_here[p->idx] = textget (iv->plist, *p->name);
3241
3242 /* Look for an interval following iv that has different
3243 properties. */
3244 for (next_iv = next_interval (iv);
3245 (!NULL_INTERVAL_P (next_iv)
3246 && (NILP (limit)
3247 || XFASTINT (limit) > next_iv->position));
3248 next_iv = next_interval (next_iv))
3249 {
3250 for (p = it_props; p->handler; ++p)
3251 {
3252 Lisp_Object new_value;
3253
3254 new_value = textget (next_iv->plist, *p->name);
3255 if (!EQ (values_here[p->idx], new_value))
3256 break;
3257 }
3258
3259 if (p->handler)
3260 break;
3261 }
3262
3263 if (!NULL_INTERVAL_P (next_iv))
3264 {
3265 if (INTEGERP (limit)
3266 && next_iv->position >= XFASTINT (limit))
3267 /* No text property change up to limit. */
3268 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
3269 else
3270 /* Text properties change in next_iv. */
3271 it->stop_charpos = min (it->stop_charpos, next_iv->position);
3272 }
3273 }
3274
3275 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos,
3276 it->stop_charpos, it->string);
3277
3278 xassert (STRINGP (it->string)
3279 || (it->stop_charpos >= BEGV
3280 && it->stop_charpos >= IT_CHARPOS (*it)));
3281 }
3282
3283
3284 /* Return the position of the next overlay change after POS in
3285 current_buffer. Value is point-max if no overlay change
3286 follows. This is like `next-overlay-change' but doesn't use
3287 xmalloc. */
3288
3289 static EMACS_INT
3290 next_overlay_change (pos)
3291 EMACS_INT pos;
3292 {
3293 int noverlays;
3294 EMACS_INT endpos;
3295 Lisp_Object *overlays;
3296 int i;
3297
3298 /* Get all overlays at the given position. */
3299 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, 1);
3300
3301 /* If any of these overlays ends before endpos,
3302 use its ending point instead. */
3303 for (i = 0; i < noverlays; ++i)
3304 {
3305 Lisp_Object oend;
3306 EMACS_INT oendpos;
3307
3308 oend = OVERLAY_END (overlays[i]);
3309 oendpos = OVERLAY_POSITION (oend);
3310 endpos = min (endpos, oendpos);
3311 }
3312
3313 return endpos;
3314 }
3315
3316
3317 \f
3318 /***********************************************************************
3319 Fontification
3320 ***********************************************************************/
3321
3322 /* Handle changes in the `fontified' property of the current buffer by
3323 calling hook functions from Qfontification_functions to fontify
3324 regions of text. */
3325
3326 static enum prop_handled
3327 handle_fontified_prop (it)
3328 struct it *it;
3329 {
3330 Lisp_Object prop, pos;
3331 enum prop_handled handled = HANDLED_NORMALLY;
3332
3333 if (!NILP (Vmemory_full))
3334 return handled;
3335
3336 /* Get the value of the `fontified' property at IT's current buffer
3337 position. (The `fontified' property doesn't have a special
3338 meaning in strings.) If the value is nil, call functions from
3339 Qfontification_functions. */
3340 if (!STRINGP (it->string)
3341 && it->s == NULL
3342 && !NILP (Vfontification_functions)
3343 && !NILP (Vrun_hooks)
3344 && (pos = make_number (IT_CHARPOS (*it)),
3345 prop = Fget_char_property (pos, Qfontified, Qnil),
3346 /* Ignore the special cased nil value always present at EOB since
3347 no amount of fontifying will be able to change it. */
3348 NILP (prop) && IT_CHARPOS (*it) < Z))
3349 {
3350 int count = SPECPDL_INDEX ();
3351 Lisp_Object val;
3352
3353 val = Vfontification_functions;
3354 specbind (Qfontification_functions, Qnil);
3355
3356 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
3357 safe_call1 (val, pos);
3358 else
3359 {
3360 Lisp_Object globals, fn;
3361 struct gcpro gcpro1, gcpro2;
3362
3363 globals = Qnil;
3364 GCPRO2 (val, globals);
3365
3366 for (; CONSP (val); val = XCDR (val))
3367 {
3368 fn = XCAR (val);
3369
3370 if (EQ (fn, Qt))
3371 {
3372 /* A value of t indicates this hook has a local
3373 binding; it means to run the global binding too.
3374 In a global value, t should not occur. If it
3375 does, we must ignore it to avoid an endless
3376 loop. */
3377 for (globals = Fdefault_value (Qfontification_functions);
3378 CONSP (globals);
3379 globals = XCDR (globals))
3380 {
3381 fn = XCAR (globals);
3382 if (!EQ (fn, Qt))
3383 safe_call1 (fn, pos);
3384 }
3385 }
3386 else
3387 safe_call1 (fn, pos);
3388 }
3389
3390 UNGCPRO;
3391 }
3392
3393 unbind_to (count, Qnil);
3394
3395 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
3396 something. This avoids an endless loop if they failed to
3397 fontify the text for which reason ever. */
3398 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
3399 handled = HANDLED_RECOMPUTE_PROPS;
3400 }
3401
3402 return handled;
3403 }
3404
3405
3406 \f
3407 /***********************************************************************
3408 Faces
3409 ***********************************************************************/
3410
3411 /* Set up iterator IT from face properties at its current position.
3412 Called from handle_stop. */
3413
3414 static enum prop_handled
3415 handle_face_prop (it)
3416 struct it *it;
3417 {
3418 int new_face_id;
3419 EMACS_INT next_stop;
3420
3421 if (!STRINGP (it->string))
3422 {
3423 new_face_id
3424 = face_at_buffer_position (it->w,
3425 IT_CHARPOS (*it),
3426 it->region_beg_charpos,
3427 it->region_end_charpos,
3428 &next_stop,
3429 (IT_CHARPOS (*it)
3430 + TEXT_PROP_DISTANCE_LIMIT),
3431 0);
3432
3433 /* Is this a start of a run of characters with box face?
3434 Caveat: this can be called for a freshly initialized
3435 iterator; face_id is -1 in this case. We know that the new
3436 face will not change until limit, i.e. if the new face has a
3437 box, all characters up to limit will have one. But, as
3438 usual, we don't know whether limit is really the end. */
3439 if (new_face_id != it->face_id)
3440 {
3441 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3442
3443 /* If new face has a box but old face has not, this is
3444 the start of a run of characters with box, i.e. it has
3445 a shadow on the left side. The value of face_id of the
3446 iterator will be -1 if this is the initial call that gets
3447 the face. In this case, we have to look in front of IT's
3448 position and see whether there is a face != new_face_id. */
3449 it->start_of_box_run_p
3450 = (new_face->box != FACE_NO_BOX
3451 && (it->face_id >= 0
3452 || IT_CHARPOS (*it) == BEG
3453 || new_face_id != face_before_it_pos (it)));
3454 it->face_box_p = new_face->box != FACE_NO_BOX;
3455 }
3456 }
3457 else
3458 {
3459 int base_face_id, bufpos;
3460 int i;
3461 Lisp_Object from_overlay
3462 = (it->current.overlay_string_index >= 0
3463 ? it->string_overlays[it->current.overlay_string_index]
3464 : Qnil);
3465
3466 /* See if we got to this string directly or indirectly from
3467 an overlay property. That includes the before-string or
3468 after-string of an overlay, strings in display properties
3469 provided by an overlay, their text properties, etc.
3470
3471 FROM_OVERLAY is the overlay that brought us here, or nil if none. */
3472 if (! NILP (from_overlay))
3473 for (i = it->sp - 1; i >= 0; i--)
3474 {
3475 if (it->stack[i].current.overlay_string_index >= 0)
3476 from_overlay
3477 = it->string_overlays[it->stack[i].current.overlay_string_index];
3478 else if (! NILP (it->stack[i].from_overlay))
3479 from_overlay = it->stack[i].from_overlay;
3480
3481 if (!NILP (from_overlay))
3482 break;
3483 }
3484
3485 if (! NILP (from_overlay))
3486 {
3487 bufpos = IT_CHARPOS (*it);
3488 /* For a string from an overlay, the base face depends
3489 only on text properties and ignores overlays. */
3490 base_face_id
3491 = face_for_overlay_string (it->w,
3492 IT_CHARPOS (*it),
3493 it->region_beg_charpos,
3494 it->region_end_charpos,
3495 &next_stop,
3496 (IT_CHARPOS (*it)
3497 + TEXT_PROP_DISTANCE_LIMIT),
3498 0,
3499 from_overlay);
3500 }
3501 else
3502 {
3503 bufpos = 0;
3504
3505 /* For strings from a `display' property, use the face at
3506 IT's current buffer position as the base face to merge
3507 with, so that overlay strings appear in the same face as
3508 surrounding text, unless they specify their own
3509 faces. */
3510 base_face_id = underlying_face_id (it);
3511 }
3512
3513 new_face_id = face_at_string_position (it->w,
3514 it->string,
3515 IT_STRING_CHARPOS (*it),
3516 bufpos,
3517 it->region_beg_charpos,
3518 it->region_end_charpos,
3519 &next_stop,
3520 base_face_id, 0);
3521
3522 #if 0 /* This shouldn't be neccessary. Let's check it. */
3523 /* If IT is used to display a mode line we would really like to
3524 use the mode line face instead of the frame's default face. */
3525 if (it->glyph_row == MATRIX_MODE_LINE_ROW (it->w->desired_matrix)
3526 && new_face_id == DEFAULT_FACE_ID)
3527 new_face_id = CURRENT_MODE_LINE_FACE_ID (it->w);
3528 #endif
3529
3530 /* Is this a start of a run of characters with box? Caveat:
3531 this can be called for a freshly allocated iterator; face_id
3532 is -1 is this case. We know that the new face will not
3533 change until the next check pos, i.e. if the new face has a
3534 box, all characters up to that position will have a
3535 box. But, as usual, we don't know whether that position
3536 is really the end. */
3537 if (new_face_id != it->face_id)
3538 {
3539 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3540 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3541
3542 /* If new face has a box but old face hasn't, this is the
3543 start of a run of characters with box, i.e. it has a
3544 shadow on the left side. */
3545 it->start_of_box_run_p
3546 = new_face->box && (old_face == NULL || !old_face->box);
3547 it->face_box_p = new_face->box != FACE_NO_BOX;
3548 }
3549 }
3550
3551 it->face_id = new_face_id;
3552 return HANDLED_NORMALLY;
3553 }
3554
3555
3556 /* Return the ID of the face ``underlying'' IT's current position,
3557 which is in a string. If the iterator is associated with a
3558 buffer, return the face at IT's current buffer position.
3559 Otherwise, use the iterator's base_face_id. */
3560
3561 static int
3562 underlying_face_id (it)
3563 struct it *it;
3564 {
3565 int face_id = it->base_face_id, i;
3566
3567 xassert (STRINGP (it->string));
3568
3569 for (i = it->sp - 1; i >= 0; --i)
3570 if (NILP (it->stack[i].string))
3571 face_id = it->stack[i].face_id;
3572
3573 return face_id;
3574 }
3575
3576
3577 /* Compute the face one character before or after the current position
3578 of IT. BEFORE_P non-zero means get the face in front of IT's
3579 position. Value is the id of the face. */
3580
3581 static int
3582 face_before_or_after_it_pos (it, before_p)
3583 struct it *it;
3584 int before_p;
3585 {
3586 int face_id, limit;
3587 EMACS_INT next_check_charpos;
3588 struct text_pos pos;
3589
3590 xassert (it->s == NULL);
3591
3592 if (STRINGP (it->string))
3593 {
3594 int bufpos, base_face_id;
3595
3596 /* No face change past the end of the string (for the case
3597 we are padding with spaces). No face change before the
3598 string start. */
3599 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
3600 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
3601 return it->face_id;
3602
3603 /* Set pos to the position before or after IT's current position. */
3604 if (before_p)
3605 pos = string_pos (IT_STRING_CHARPOS (*it) - 1, it->string);
3606 else
3607 /* For composition, we must check the character after the
3608 composition. */
3609 pos = (it->what == IT_COMPOSITION
3610 ? string_pos (IT_STRING_CHARPOS (*it)
3611 + it->cmp_it.nchars, it->string)
3612 : string_pos (IT_STRING_CHARPOS (*it) + 1, it->string));
3613
3614 if (it->current.overlay_string_index >= 0)
3615 bufpos = IT_CHARPOS (*it);
3616 else
3617 bufpos = 0;
3618
3619 base_face_id = underlying_face_id (it);
3620
3621 /* Get the face for ASCII, or unibyte. */
3622 face_id = face_at_string_position (it->w,
3623 it->string,
3624 CHARPOS (pos),
3625 bufpos,
3626 it->region_beg_charpos,
3627 it->region_end_charpos,
3628 &next_check_charpos,
3629 base_face_id, 0);
3630
3631 /* Correct the face for charsets different from ASCII. Do it
3632 for the multibyte case only. The face returned above is
3633 suitable for unibyte text if IT->string is unibyte. */
3634 if (STRING_MULTIBYTE (it->string))
3635 {
3636 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos);
3637 int rest = SBYTES (it->string) - BYTEPOS (pos);
3638 int c, len;
3639 struct face *face = FACE_FROM_ID (it->f, face_id);
3640
3641 c = string_char_and_length (p, rest, &len);
3642 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), it->string);
3643 }
3644 }
3645 else
3646 {
3647 if ((IT_CHARPOS (*it) >= ZV && !before_p)
3648 || (IT_CHARPOS (*it) <= BEGV && before_p))
3649 return it->face_id;
3650
3651 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
3652 pos = it->current.pos;
3653
3654 if (before_p)
3655 DEC_TEXT_POS (pos, it->multibyte_p);
3656 else
3657 {
3658 if (it->what == IT_COMPOSITION)
3659 /* For composition, we must check the position after the
3660 composition. */
3661 pos.charpos += it->cmp_it.nchars, pos.bytepos += it->len;
3662 else
3663 INC_TEXT_POS (pos, it->multibyte_p);
3664 }
3665
3666 /* Determine face for CHARSET_ASCII, or unibyte. */
3667 face_id = face_at_buffer_position (it->w,
3668 CHARPOS (pos),
3669 it->region_beg_charpos,
3670 it->region_end_charpos,
3671 &next_check_charpos,
3672 limit, 0);
3673
3674 /* Correct the face for charsets different from ASCII. Do it
3675 for the multibyte case only. The face returned above is
3676 suitable for unibyte text if current_buffer is unibyte. */
3677 if (it->multibyte_p)
3678 {
3679 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
3680 struct face *face = FACE_FROM_ID (it->f, face_id);
3681 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), Qnil);
3682 }
3683 }
3684
3685 return face_id;
3686 }
3687
3688
3689 \f
3690 /***********************************************************************
3691 Invisible text
3692 ***********************************************************************/
3693
3694 /* Set up iterator IT from invisible properties at its current
3695 position. Called from handle_stop. */
3696
3697 static enum prop_handled
3698 handle_invisible_prop (it)
3699 struct it *it;
3700 {
3701 enum prop_handled handled = HANDLED_NORMALLY;
3702
3703 if (STRINGP (it->string))
3704 {
3705 extern Lisp_Object Qinvisible;
3706 Lisp_Object prop, end_charpos, limit, charpos;
3707
3708 /* Get the value of the invisible text property at the
3709 current position. Value will be nil if there is no such
3710 property. */
3711 charpos = make_number (IT_STRING_CHARPOS (*it));
3712 prop = Fget_text_property (charpos, Qinvisible, it->string);
3713
3714 if (!NILP (prop)
3715 && IT_STRING_CHARPOS (*it) < it->end_charpos)
3716 {
3717 handled = HANDLED_RECOMPUTE_PROPS;
3718
3719 /* Get the position at which the next change of the
3720 invisible text property can be found in IT->string.
3721 Value will be nil if the property value is the same for
3722 all the rest of IT->string. */
3723 XSETINT (limit, SCHARS (it->string));
3724 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
3725 it->string, limit);
3726
3727 /* Text at current position is invisible. The next
3728 change in the property is at position end_charpos.
3729 Move IT's current position to that position. */
3730 if (INTEGERP (end_charpos)
3731 && XFASTINT (end_charpos) < XFASTINT (limit))
3732 {
3733 struct text_pos old;
3734 old = it->current.string_pos;
3735 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
3736 compute_string_pos (&it->current.string_pos, old, it->string);
3737 }
3738 else
3739 {
3740 /* The rest of the string is invisible. If this is an
3741 overlay string, proceed with the next overlay string
3742 or whatever comes and return a character from there. */
3743 if (it->current.overlay_string_index >= 0)
3744 {
3745 next_overlay_string (it);
3746 /* Don't check for overlay strings when we just
3747 finished processing them. */
3748 handled = HANDLED_OVERLAY_STRING_CONSUMED;
3749 }
3750 else
3751 {
3752 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
3753 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
3754 }
3755 }
3756 }
3757 }
3758 else
3759 {
3760 int invis_p;
3761 EMACS_INT newpos, next_stop, start_charpos;
3762 Lisp_Object pos, prop, overlay;
3763
3764 /* First of all, is there invisible text at this position? */
3765 start_charpos = IT_CHARPOS (*it);
3766 pos = make_number (IT_CHARPOS (*it));
3767 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
3768 &overlay);
3769 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3770
3771 /* If we are on invisible text, skip over it. */
3772 if (invis_p && IT_CHARPOS (*it) < it->end_charpos)
3773 {
3774 /* Record whether we have to display an ellipsis for the
3775 invisible text. */
3776 int display_ellipsis_p = invis_p == 2;
3777
3778 handled = HANDLED_RECOMPUTE_PROPS;
3779
3780 /* Loop skipping over invisible text. The loop is left at
3781 ZV or with IT on the first char being visible again. */
3782 do
3783 {
3784 /* Try to skip some invisible text. Return value is the
3785 position reached which can be equal to IT's position
3786 if there is nothing invisible here. This skips both
3787 over invisible text properties and overlays with
3788 invisible property. */
3789 newpos = skip_invisible (IT_CHARPOS (*it),
3790 &next_stop, ZV, it->window);
3791
3792 /* If we skipped nothing at all we weren't at invisible
3793 text in the first place. If everything to the end of
3794 the buffer was skipped, end the loop. */
3795 if (newpos == IT_CHARPOS (*it) || newpos >= ZV)
3796 invis_p = 0;
3797 else
3798 {
3799 /* We skipped some characters but not necessarily
3800 all there are. Check if we ended up on visible
3801 text. Fget_char_property returns the property of
3802 the char before the given position, i.e. if we
3803 get invis_p = 0, this means that the char at
3804 newpos is visible. */
3805 pos = make_number (newpos);
3806 prop = Fget_char_property (pos, Qinvisible, it->window);
3807 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3808 }
3809
3810 /* If we ended up on invisible text, proceed to
3811 skip starting with next_stop. */
3812 if (invis_p)
3813 IT_CHARPOS (*it) = next_stop;
3814
3815 /* If there are adjacent invisible texts, don't lose the
3816 second one's ellipsis. */
3817 if (invis_p == 2)
3818 display_ellipsis_p = 1;
3819 }
3820 while (invis_p);
3821
3822 /* The position newpos is now either ZV or on visible text. */
3823 IT_CHARPOS (*it) = newpos;
3824 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
3825
3826 /* If there are before-strings at the start of invisible
3827 text, and the text is invisible because of a text
3828 property, arrange to show before-strings because 20.x did
3829 it that way. (If the text is invisible because of an
3830 overlay property instead of a text property, this is
3831 already handled in the overlay code.) */
3832 if (NILP (overlay)
3833 && get_overlay_strings (it, start_charpos))
3834 {
3835 handled = HANDLED_RECOMPUTE_PROPS;
3836 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
3837 }
3838 else if (display_ellipsis_p)
3839 {
3840 /* Make sure that the glyphs of the ellipsis will get
3841 correct `charpos' values. If we would not update
3842 it->position here, the glyphs would belong to the
3843 last visible character _before_ the invisible
3844 text, which confuses `set_cursor_from_row'.
3845
3846 We use the last invisible position instead of the
3847 first because this way the cursor is always drawn on
3848 the first "." of the ellipsis, whenever PT is inside
3849 the invisible text. Otherwise the cursor would be
3850 placed _after_ the ellipsis when the point is after the
3851 first invisible character. */
3852 if (!STRINGP (it->object))
3853 {
3854 it->position.charpos = IT_CHARPOS (*it) - 1;
3855 it->position.bytepos = CHAR_TO_BYTE (it->position.charpos);
3856 }
3857 it->ellipsis_p = 1;
3858 /* Let the ellipsis display before
3859 considering any properties of the following char.
3860 Fixes jasonr@gnu.org 01 Oct 07 bug. */
3861 handled = HANDLED_RETURN;
3862 }
3863 }
3864 }
3865
3866 return handled;
3867 }
3868
3869
3870 /* Make iterator IT return `...' next.
3871 Replaces LEN characters from buffer. */
3872
3873 static void
3874 setup_for_ellipsis (it, len)
3875 struct it *it;
3876 int len;
3877 {
3878 /* Use the display table definition for `...'. Invalid glyphs
3879 will be handled by the method returning elements from dpvec. */
3880 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
3881 {
3882 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
3883 it->dpvec = v->contents;
3884 it->dpend = v->contents + v->size;
3885 }
3886 else
3887 {
3888 /* Default `...'. */
3889 it->dpvec = default_invis_vector;
3890 it->dpend = default_invis_vector + 3;
3891 }
3892
3893 it->dpvec_char_len = len;
3894 it->current.dpvec_index = 0;
3895 it->dpvec_face_id = -1;
3896
3897 /* Remember the current face id in case glyphs specify faces.
3898 IT's face is restored in set_iterator_to_next.
3899 saved_face_id was set to preceding char's face in handle_stop. */
3900 if (it->saved_face_id < 0 || it->saved_face_id != it->face_id)
3901 it->saved_face_id = it->face_id = DEFAULT_FACE_ID;
3902
3903 it->method = GET_FROM_DISPLAY_VECTOR;
3904 it->ellipsis_p = 1;
3905 }
3906
3907
3908 \f
3909 /***********************************************************************
3910 'display' property
3911 ***********************************************************************/
3912
3913 /* Set up iterator IT from `display' property at its current position.
3914 Called from handle_stop.
3915 We return HANDLED_RETURN if some part of the display property
3916 overrides the display of the buffer text itself.
3917 Otherwise we return HANDLED_NORMALLY. */
3918
3919 static enum prop_handled
3920 handle_display_prop (it)
3921 struct it *it;
3922 {
3923 Lisp_Object prop, object, overlay;
3924 struct text_pos *position;
3925 /* Nonzero if some property replaces the display of the text itself. */
3926 int display_replaced_p = 0;
3927
3928 if (STRINGP (it->string))
3929 {
3930 object = it->string;
3931 position = &it->current.string_pos;
3932 }
3933 else
3934 {
3935 XSETWINDOW (object, it->w);
3936 position = &it->current.pos;
3937 }
3938
3939 /* Reset those iterator values set from display property values. */
3940 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
3941 it->space_width = Qnil;
3942 it->font_height = Qnil;
3943 it->voffset = 0;
3944
3945 /* We don't support recursive `display' properties, i.e. string
3946 values that have a string `display' property, that have a string
3947 `display' property etc. */
3948 if (!it->string_from_display_prop_p)
3949 it->area = TEXT_AREA;
3950
3951 prop = get_char_property_and_overlay (make_number (position->charpos),
3952 Qdisplay, object, &overlay);
3953 if (NILP (prop))
3954 return HANDLED_NORMALLY;
3955 /* Now OVERLAY is the overlay that gave us this property, or nil
3956 if it was a text property. */
3957
3958 if (!STRINGP (it->string))
3959 object = it->w->buffer;
3960
3961 if (CONSP (prop)
3962 /* Simple properties. */
3963 && !EQ (XCAR (prop), Qimage)
3964 && !EQ (XCAR (prop), Qspace)
3965 && !EQ (XCAR (prop), Qwhen)
3966 && !EQ (XCAR (prop), Qslice)
3967 && !EQ (XCAR (prop), Qspace_width)
3968 && !EQ (XCAR (prop), Qheight)
3969 && !EQ (XCAR (prop), Qraise)
3970 /* Marginal area specifications. */
3971 && !(CONSP (XCAR (prop)) && EQ (XCAR (XCAR (prop)), Qmargin))
3972 && !EQ (XCAR (prop), Qleft_fringe)
3973 && !EQ (XCAR (prop), Qright_fringe)
3974 && !NILP (XCAR (prop)))
3975 {
3976 for (; CONSP (prop); prop = XCDR (prop))
3977 {
3978 if (handle_single_display_spec (it, XCAR (prop), object, overlay,
3979 position, display_replaced_p))
3980 {
3981 display_replaced_p = 1;
3982 /* If some text in a string is replaced, `position' no
3983 longer points to the position of `object'. */
3984 if (STRINGP (object))
3985 break;
3986 }
3987 }
3988 }
3989 else if (VECTORP (prop))
3990 {
3991 int i;
3992 for (i = 0; i < ASIZE (prop); ++i)
3993 if (handle_single_display_spec (it, AREF (prop, i), object, overlay,
3994 position, display_replaced_p))
3995 {
3996 display_replaced_p = 1;
3997 /* If some text in a string is replaced, `position' no
3998 longer points to the position of `object'. */
3999 if (STRINGP (object))
4000 break;
4001 }
4002 }
4003 else
4004 {
4005 int ret = handle_single_display_spec (it, prop, object, overlay,
4006 position, 0);
4007 if (ret < 0) /* Replaced by "", i.e. nothing. */
4008 return HANDLED_RECOMPUTE_PROPS;
4009 if (ret)
4010 display_replaced_p = 1;
4011 }
4012
4013 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
4014 }
4015
4016
4017 /* Value is the position of the end of the `display' property starting
4018 at START_POS in OBJECT. */
4019
4020 static struct text_pos
4021 display_prop_end (it, object, start_pos)
4022 struct it *it;
4023 Lisp_Object object;
4024 struct text_pos start_pos;
4025 {
4026 Lisp_Object end;
4027 struct text_pos end_pos;
4028
4029 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
4030 Qdisplay, object, Qnil);
4031 CHARPOS (end_pos) = XFASTINT (end);
4032 if (STRINGP (object))
4033 compute_string_pos (&end_pos, start_pos, it->string);
4034 else
4035 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
4036
4037 return end_pos;
4038 }
4039
4040
4041 /* Set up IT from a single `display' specification PROP. OBJECT
4042 is the object in which the `display' property was found. *POSITION
4043 is the position at which it was found. DISPLAY_REPLACED_P non-zero
4044 means that we previously saw a display specification which already
4045 replaced text display with something else, for example an image;
4046 we ignore such properties after the first one has been processed.
4047
4048 OVERLAY is the overlay this `display' property came from,
4049 or nil if it was a text property.
4050
4051 If PROP is a `space' or `image' specification, and in some other
4052 cases too, set *POSITION to the position where the `display'
4053 property ends.
4054
4055 Value is non-zero if something was found which replaces the display
4056 of buffer or string text. Specifically, the value is -1 if that
4057 "something" is "nothing". */
4058
4059 static int
4060 handle_single_display_spec (it, spec, object, overlay, position,
4061 display_replaced_before_p)
4062 struct it *it;
4063 Lisp_Object spec;
4064 Lisp_Object object;
4065 Lisp_Object overlay;
4066 struct text_pos *position;
4067 int display_replaced_before_p;
4068 {
4069 Lisp_Object form;
4070 Lisp_Object location, value;
4071 struct text_pos start_pos, save_pos;
4072 int valid_p;
4073
4074 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
4075 If the result is non-nil, use VALUE instead of SPEC. */
4076 form = Qt;
4077 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
4078 {
4079 spec = XCDR (spec);
4080 if (!CONSP (spec))
4081 return 0;
4082 form = XCAR (spec);
4083 spec = XCDR (spec);
4084 }
4085
4086 if (!NILP (form) && !EQ (form, Qt))
4087 {
4088 int count = SPECPDL_INDEX ();
4089 struct gcpro gcpro1;
4090
4091 /* Bind `object' to the object having the `display' property, a
4092 buffer or string. Bind `position' to the position in the
4093 object where the property was found, and `buffer-position'
4094 to the current position in the buffer. */
4095 specbind (Qobject, object);
4096 specbind (Qposition, make_number (CHARPOS (*position)));
4097 specbind (Qbuffer_position,
4098 make_number (STRINGP (object)
4099 ? IT_CHARPOS (*it) : CHARPOS (*position)));
4100 GCPRO1 (form);
4101 form = safe_eval (form);
4102 UNGCPRO;
4103 unbind_to (count, Qnil);
4104 }
4105
4106 if (NILP (form))
4107 return 0;
4108
4109 /* Handle `(height HEIGHT)' specifications. */
4110 if (CONSP (spec)
4111 && EQ (XCAR (spec), Qheight)
4112 && CONSP (XCDR (spec)))
4113 {
4114 if (!FRAME_WINDOW_P (it->f))
4115 return 0;
4116
4117 it->font_height = XCAR (XCDR (spec));
4118 if (!NILP (it->font_height))
4119 {
4120 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4121 int new_height = -1;
4122
4123 if (CONSP (it->font_height)
4124 && (EQ (XCAR (it->font_height), Qplus)
4125 || EQ (XCAR (it->font_height), Qminus))
4126 && CONSP (XCDR (it->font_height))
4127 && INTEGERP (XCAR (XCDR (it->font_height))))
4128 {
4129 /* `(+ N)' or `(- N)' where N is an integer. */
4130 int steps = XINT (XCAR (XCDR (it->font_height)));
4131 if (EQ (XCAR (it->font_height), Qplus))
4132 steps = - steps;
4133 it->face_id = smaller_face (it->f, it->face_id, steps);
4134 }
4135 else if (FUNCTIONP (it->font_height))
4136 {
4137 /* Call function with current height as argument.
4138 Value is the new height. */
4139 Lisp_Object height;
4140 height = safe_call1 (it->font_height,
4141 face->lface[LFACE_HEIGHT_INDEX]);
4142 if (NUMBERP (height))
4143 new_height = XFLOATINT (height);
4144 }
4145 else if (NUMBERP (it->font_height))
4146 {
4147 /* Value is a multiple of the canonical char height. */
4148 struct face *face;
4149
4150 face = FACE_FROM_ID (it->f,
4151 lookup_basic_face (it->f, DEFAULT_FACE_ID));
4152 new_height = (XFLOATINT (it->font_height)
4153 * XINT (face->lface[LFACE_HEIGHT_INDEX]));
4154 }
4155 else
4156 {
4157 /* Evaluate IT->font_height with `height' bound to the
4158 current specified height to get the new height. */
4159 int count = SPECPDL_INDEX ();
4160
4161 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
4162 value = safe_eval (it->font_height);
4163 unbind_to (count, Qnil);
4164
4165 if (NUMBERP (value))
4166 new_height = XFLOATINT (value);
4167 }
4168
4169 if (new_height > 0)
4170 it->face_id = face_with_height (it->f, it->face_id, new_height);
4171 }
4172
4173 return 0;
4174 }
4175
4176 /* Handle `(space-width WIDTH)'. */
4177 if (CONSP (spec)
4178 && EQ (XCAR (spec), Qspace_width)
4179 && CONSP (XCDR (spec)))
4180 {
4181 if (!FRAME_WINDOW_P (it->f))
4182 return 0;
4183
4184 value = XCAR (XCDR (spec));
4185 if (NUMBERP (value) && XFLOATINT (value) > 0)
4186 it->space_width = value;
4187
4188 return 0;
4189 }
4190
4191 /* Handle `(slice X Y WIDTH HEIGHT)'. */
4192 if (CONSP (spec)
4193 && EQ (XCAR (spec), Qslice))
4194 {
4195 Lisp_Object tem;
4196
4197 if (!FRAME_WINDOW_P (it->f))
4198 return 0;
4199
4200 if (tem = XCDR (spec), CONSP (tem))
4201 {
4202 it->slice.x = XCAR (tem);
4203 if (tem = XCDR (tem), CONSP (tem))
4204 {
4205 it->slice.y = XCAR (tem);
4206 if (tem = XCDR (tem), CONSP (tem))
4207 {
4208 it->slice.width = XCAR (tem);
4209 if (tem = XCDR (tem), CONSP (tem))
4210 it->slice.height = XCAR (tem);
4211 }
4212 }
4213 }
4214
4215 return 0;
4216 }
4217
4218 /* Handle `(raise FACTOR)'. */
4219 if (CONSP (spec)
4220 && EQ (XCAR (spec), Qraise)
4221 && CONSP (XCDR (spec)))
4222 {
4223 if (!FRAME_WINDOW_P (it->f))
4224 return 0;
4225
4226 #ifdef HAVE_WINDOW_SYSTEM
4227 value = XCAR (XCDR (spec));
4228 if (NUMBERP (value))
4229 {
4230 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4231 it->voffset = - (XFLOATINT (value)
4232 * (FONT_HEIGHT (face->font)));
4233 }
4234 #endif /* HAVE_WINDOW_SYSTEM */
4235
4236 return 0;
4237 }
4238
4239 /* Don't handle the other kinds of display specifications
4240 inside a string that we got from a `display' property. */
4241 if (it->string_from_display_prop_p)
4242 return 0;
4243
4244 /* Characters having this form of property are not displayed, so
4245 we have to find the end of the property. */
4246 start_pos = *position;
4247 *position = display_prop_end (it, object, start_pos);
4248 value = Qnil;
4249
4250 /* Stop the scan at that end position--we assume that all
4251 text properties change there. */
4252 it->stop_charpos = position->charpos;
4253
4254 /* Handle `(left-fringe BITMAP [FACE])'
4255 and `(right-fringe BITMAP [FACE])'. */
4256 if (CONSP (spec)
4257 && (EQ (XCAR (spec), Qleft_fringe)
4258 || EQ (XCAR (spec), Qright_fringe))
4259 && CONSP (XCDR (spec)))
4260 {
4261 int face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);
4262 int fringe_bitmap;
4263
4264 if (!FRAME_WINDOW_P (it->f))
4265 /* If we return here, POSITION has been advanced
4266 across the text with this property. */
4267 return 0;
4268
4269 #ifdef HAVE_WINDOW_SYSTEM
4270 value = XCAR (XCDR (spec));
4271 if (!SYMBOLP (value)
4272 || !(fringe_bitmap = lookup_fringe_bitmap (value)))
4273 /* If we return here, POSITION has been advanced
4274 across the text with this property. */
4275 return 0;
4276
4277 if (CONSP (XCDR (XCDR (spec))))
4278 {
4279 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
4280 int face_id2 = lookup_derived_face (it->f, face_name,
4281 FRINGE_FACE_ID, 0);
4282 if (face_id2 >= 0)
4283 face_id = face_id2;
4284 }
4285
4286 /* Save current settings of IT so that we can restore them
4287 when we are finished with the glyph property value. */
4288
4289 save_pos = it->position;
4290 it->position = *position;
4291 push_it (it);
4292 it->position = save_pos;
4293
4294 it->area = TEXT_AREA;
4295 it->what = IT_IMAGE;
4296 it->image_id = -1; /* no image */
4297 it->position = start_pos;
4298 it->object = NILP (object) ? it->w->buffer : object;
4299 it->method = GET_FROM_IMAGE;
4300 it->from_overlay = Qnil;
4301 it->face_id = face_id;
4302
4303 /* Say that we haven't consumed the characters with
4304 `display' property yet. The call to pop_it in
4305 set_iterator_to_next will clean this up. */
4306 *position = start_pos;
4307
4308 if (EQ (XCAR (spec), Qleft_fringe))
4309 {
4310 it->left_user_fringe_bitmap = fringe_bitmap;
4311 it->left_user_fringe_face_id = face_id;
4312 }
4313 else
4314 {
4315 it->right_user_fringe_bitmap = fringe_bitmap;
4316 it->right_user_fringe_face_id = face_id;
4317 }
4318 #endif /* HAVE_WINDOW_SYSTEM */
4319 return 1;
4320 }
4321
4322 /* Prepare to handle `((margin left-margin) ...)',
4323 `((margin right-margin) ...)' and `((margin nil) ...)'
4324 prefixes for display specifications. */
4325 location = Qunbound;
4326 if (CONSP (spec) && CONSP (XCAR (spec)))
4327 {
4328 Lisp_Object tem;
4329
4330 value = XCDR (spec);
4331 if (CONSP (value))
4332 value = XCAR (value);
4333
4334 tem = XCAR (spec);
4335 if (EQ (XCAR (tem), Qmargin)
4336 && (tem = XCDR (tem),
4337 tem = CONSP (tem) ? XCAR (tem) : Qnil,
4338 (NILP (tem)
4339 || EQ (tem, Qleft_margin)
4340 || EQ (tem, Qright_margin))))
4341 location = tem;
4342 }
4343
4344 if (EQ (location, Qunbound))
4345 {
4346 location = Qnil;
4347 value = spec;
4348 }
4349
4350 /* After this point, VALUE is the property after any
4351 margin prefix has been stripped. It must be a string,
4352 an image specification, or `(space ...)'.
4353
4354 LOCATION specifies where to display: `left-margin',
4355 `right-margin' or nil. */
4356
4357 valid_p = (STRINGP (value)
4358 #ifdef HAVE_WINDOW_SYSTEM
4359 || (FRAME_WINDOW_P (it->f) && valid_image_p (value))
4360 #endif /* not HAVE_WINDOW_SYSTEM */
4361 || (CONSP (value) && EQ (XCAR (value), Qspace)));
4362
4363 if (valid_p && !display_replaced_before_p)
4364 {
4365 /* Save current settings of IT so that we can restore them
4366 when we are finished with the glyph property value. */
4367 save_pos = it->position;
4368 it->position = *position;
4369 push_it (it);
4370 it->position = save_pos;
4371 it->from_overlay = overlay;
4372
4373 if (NILP (location))
4374 it->area = TEXT_AREA;
4375 else if (EQ (location, Qleft_margin))
4376 it->area = LEFT_MARGIN_AREA;
4377 else
4378 it->area = RIGHT_MARGIN_AREA;
4379
4380 if (STRINGP (value))
4381 {
4382 if (SCHARS (value) == 0)
4383 {
4384 pop_it (it);
4385 return -1; /* Replaced by "", i.e. nothing. */
4386 }
4387 it->string = value;
4388 it->multibyte_p = STRING_MULTIBYTE (it->string);
4389 it->current.overlay_string_index = -1;
4390 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
4391 it->end_charpos = it->string_nchars = SCHARS (it->string);
4392 it->method = GET_FROM_STRING;
4393 it->stop_charpos = 0;
4394 it->string_from_display_prop_p = 1;
4395 /* Say that we haven't consumed the characters with
4396 `display' property yet. The call to pop_it in
4397 set_iterator_to_next will clean this up. */
4398 if (BUFFERP (object))
4399 *position = start_pos;
4400 }
4401 else if (CONSP (value) && EQ (XCAR (value), Qspace))
4402 {
4403 it->method = GET_FROM_STRETCH;
4404 it->object = value;
4405 *position = it->position = start_pos;
4406 }
4407 #ifdef HAVE_WINDOW_SYSTEM
4408 else
4409 {
4410 it->what = IT_IMAGE;
4411 it->image_id = lookup_image (it->f, value);
4412 it->position = start_pos;
4413 it->object = NILP (object) ? it->w->buffer : object;
4414 it->method = GET_FROM_IMAGE;
4415
4416 /* Say that we haven't consumed the characters with
4417 `display' property yet. The call to pop_it in
4418 set_iterator_to_next will clean this up. */
4419 *position = start_pos;
4420 }
4421 #endif /* HAVE_WINDOW_SYSTEM */
4422
4423 return 1;
4424 }
4425
4426 /* Invalid property or property not supported. Restore
4427 POSITION to what it was before. */
4428 *position = start_pos;
4429 return 0;
4430 }
4431
4432
4433 /* Check if SPEC is a display sub-property value whose text should be
4434 treated as intangible. */
4435
4436 static int
4437 single_display_spec_intangible_p (prop)
4438 Lisp_Object prop;
4439 {
4440 /* Skip over `when FORM'. */
4441 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
4442 {
4443 prop = XCDR (prop);
4444 if (!CONSP (prop))
4445 return 0;
4446 prop = XCDR (prop);
4447 }
4448
4449 if (STRINGP (prop))
4450 return 1;
4451
4452 if (!CONSP (prop))
4453 return 0;
4454
4455 /* Skip over `margin LOCATION'. If LOCATION is in the margins,
4456 we don't need to treat text as intangible. */
4457 if (EQ (XCAR (prop), Qmargin))
4458 {
4459 prop = XCDR (prop);
4460 if (!CONSP (prop))
4461 return 0;
4462
4463 prop = XCDR (prop);
4464 if (!CONSP (prop)
4465 || EQ (XCAR (prop), Qleft_margin)
4466 || EQ (XCAR (prop), Qright_margin))
4467 return 0;
4468 }
4469
4470 return (CONSP (prop)
4471 && (EQ (XCAR (prop), Qimage)
4472 || EQ (XCAR (prop), Qspace)));
4473 }
4474
4475
4476 /* Check if PROP is a display property value whose text should be
4477 treated as intangible. */
4478
4479 int
4480 display_prop_intangible_p (prop)
4481 Lisp_Object prop;
4482 {
4483 if (CONSP (prop)
4484 && CONSP (XCAR (prop))
4485 && !EQ (Qmargin, XCAR (XCAR (prop))))
4486 {
4487 /* A list of sub-properties. */
4488 while (CONSP (prop))
4489 {
4490 if (single_display_spec_intangible_p (XCAR (prop)))
4491 return 1;
4492 prop = XCDR (prop);
4493 }
4494 }
4495 else if (VECTORP (prop))
4496 {
4497 /* A vector of sub-properties. */
4498 int i;
4499 for (i = 0; i < ASIZE (prop); ++i)
4500 if (single_display_spec_intangible_p (AREF (prop, i)))
4501 return 1;
4502 }
4503 else
4504 return single_display_spec_intangible_p (prop);
4505
4506 return 0;
4507 }
4508
4509
4510 /* Return 1 if PROP is a display sub-property value containing STRING. */
4511
4512 static int
4513 single_display_spec_string_p (prop, string)
4514 Lisp_Object prop, string;
4515 {
4516 if (EQ (string, prop))
4517 return 1;
4518
4519 /* Skip over `when FORM'. */
4520 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
4521 {
4522 prop = XCDR (prop);
4523 if (!CONSP (prop))
4524 return 0;
4525 prop = XCDR (prop);
4526 }
4527
4528 if (CONSP (prop))
4529 /* Skip over `margin LOCATION'. */
4530 if (EQ (XCAR (prop), Qmargin))
4531 {
4532 prop = XCDR (prop);
4533 if (!CONSP (prop))
4534 return 0;
4535
4536 prop = XCDR (prop);
4537 if (!CONSP (prop))
4538 return 0;
4539 }
4540
4541 return CONSP (prop) && EQ (XCAR (prop), string);
4542 }
4543
4544
4545 /* Return 1 if STRING appears in the `display' property PROP. */
4546
4547 static int
4548 display_prop_string_p (prop, string)
4549 Lisp_Object prop, string;
4550 {
4551 if (CONSP (prop)
4552 && CONSP (XCAR (prop))
4553 && !EQ (Qmargin, XCAR (XCAR (prop))))
4554 {
4555 /* A list of sub-properties. */
4556 while (CONSP (prop))
4557 {
4558 if (single_display_spec_string_p (XCAR (prop), string))
4559 return 1;
4560 prop = XCDR (prop);
4561 }
4562 }
4563 else if (VECTORP (prop))
4564 {
4565 /* A vector of sub-properties. */
4566 int i;
4567 for (i = 0; i < ASIZE (prop); ++i)
4568 if (single_display_spec_string_p (AREF (prop, i), string))
4569 return 1;
4570 }
4571 else
4572 return single_display_spec_string_p (prop, string);
4573
4574 return 0;
4575 }
4576
4577
4578 /* Determine from which buffer position in W's buffer STRING comes
4579 from. AROUND_CHARPOS is an approximate position where it could
4580 be from. Value is the buffer position or 0 if it couldn't be
4581 determined.
4582
4583 W's buffer must be current.
4584
4585 This function is necessary because we don't record buffer positions
4586 in glyphs generated from strings (to keep struct glyph small).
4587 This function may only use code that doesn't eval because it is
4588 called asynchronously from note_mouse_highlight. */
4589
4590 int
4591 string_buffer_position (w, string, around_charpos)
4592 struct window *w;
4593 Lisp_Object string;
4594 int around_charpos;
4595 {
4596 Lisp_Object limit, prop, pos;
4597 const int MAX_DISTANCE = 1000;
4598 int found = 0;
4599
4600 pos = make_number (around_charpos);
4601 limit = make_number (min (XINT (pos) + MAX_DISTANCE, ZV));
4602 while (!found && !EQ (pos, limit))
4603 {
4604 prop = Fget_char_property (pos, Qdisplay, Qnil);
4605 if (!NILP (prop) && display_prop_string_p (prop, string))
4606 found = 1;
4607 else
4608 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil, limit);
4609 }
4610
4611 if (!found)
4612 {
4613 pos = make_number (around_charpos);
4614 limit = make_number (max (XINT (pos) - MAX_DISTANCE, BEGV));
4615 while (!found && !EQ (pos, limit))
4616 {
4617 prop = Fget_char_property (pos, Qdisplay, Qnil);
4618 if (!NILP (prop) && display_prop_string_p (prop, string))
4619 found = 1;
4620 else
4621 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
4622 limit);
4623 }
4624 }
4625
4626 return found ? XINT (pos) : 0;
4627 }
4628
4629
4630 \f
4631 /***********************************************************************
4632 `composition' property
4633 ***********************************************************************/
4634
4635 /* Set up iterator IT from `composition' property at its current
4636 position. Called from handle_stop. */
4637
4638 static enum prop_handled
4639 handle_composition_prop (it)
4640 struct it *it;
4641 {
4642 Lisp_Object prop, string;
4643 EMACS_INT pos, pos_byte, start, end;
4644
4645 if (STRINGP (it->string))
4646 {
4647 unsigned char *s;
4648
4649 pos = IT_STRING_CHARPOS (*it);
4650 pos_byte = IT_STRING_BYTEPOS (*it);
4651 string = it->string;
4652 s = SDATA (string) + pos_byte;
4653 it->c = STRING_CHAR (s, 0);
4654 }
4655 else
4656 {
4657 pos = IT_CHARPOS (*it);
4658 pos_byte = IT_BYTEPOS (*it);
4659 string = Qnil;
4660 it->c = FETCH_CHAR (pos_byte);
4661 }
4662
4663 /* If there's a valid composition and point is not inside of the
4664 composition (in the case that the composition is from the current
4665 buffer), draw a glyph composed from the composition components. */
4666 if (find_composition (pos, -1, &start, &end, &prop, string)
4667 && COMPOSITION_VALID_P (start, end, prop)
4668 && (STRINGP (it->string) || (PT <= start || PT >= end)))
4669 {
4670 if (start != pos)
4671 {
4672 if (STRINGP (it->string))
4673 pos_byte = string_char_to_byte (it->string, start);
4674 else
4675 pos_byte = CHAR_TO_BYTE (start);
4676 }
4677 it->cmp_it.id = get_composition_id (start, pos_byte, end - start,
4678 prop, string);
4679
4680 if (it->cmp_it.id >= 0)
4681 {
4682 it->cmp_it.ch = -1;
4683 it->cmp_it.nchars = COMPOSITION_LENGTH (prop);
4684 it->cmp_it.nglyphs = -1;
4685 }
4686 }
4687
4688 return HANDLED_NORMALLY;
4689 }
4690
4691
4692 \f
4693 /***********************************************************************
4694 Overlay strings
4695 ***********************************************************************/
4696
4697 /* The following structure is used to record overlay strings for
4698 later sorting in load_overlay_strings. */
4699
4700 struct overlay_entry
4701 {
4702 Lisp_Object overlay;
4703 Lisp_Object string;
4704 int priority;
4705 int after_string_p;
4706 };
4707
4708
4709 /* Set up iterator IT from overlay strings at its current position.
4710 Called from handle_stop. */
4711
4712 static enum prop_handled
4713 handle_overlay_change (it)
4714 struct it *it;
4715 {
4716 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
4717 return HANDLED_RECOMPUTE_PROPS;
4718 else
4719 return HANDLED_NORMALLY;
4720 }
4721
4722
4723 /* Set up the next overlay string for delivery by IT, if there is an
4724 overlay string to deliver. Called by set_iterator_to_next when the
4725 end of the current overlay string is reached. If there are more
4726 overlay strings to display, IT->string and
4727 IT->current.overlay_string_index are set appropriately here.
4728 Otherwise IT->string is set to nil. */
4729
4730 static void
4731 next_overlay_string (it)
4732 struct it *it;
4733 {
4734 ++it->current.overlay_string_index;
4735 if (it->current.overlay_string_index == it->n_overlay_strings)
4736 {
4737 /* No more overlay strings. Restore IT's settings to what
4738 they were before overlay strings were processed, and
4739 continue to deliver from current_buffer. */
4740
4741 it->ellipsis_p = (it->stack[it->sp - 1].display_ellipsis_p != 0);
4742 pop_it (it);
4743 xassert (it->sp > 0
4744 || (NILP (it->string)
4745 && it->method == GET_FROM_BUFFER
4746 && it->stop_charpos >= BEGV
4747 && it->stop_charpos <= it->end_charpos));
4748 it->current.overlay_string_index = -1;
4749 it->n_overlay_strings = 0;
4750
4751 /* If we're at the end of the buffer, record that we have
4752 processed the overlay strings there already, so that
4753 next_element_from_buffer doesn't try it again. */
4754 if (NILP (it->string) && IT_CHARPOS (*it) >= it->end_charpos)
4755 it->overlay_strings_at_end_processed_p = 1;
4756 }
4757 else
4758 {
4759 /* There are more overlay strings to process. If
4760 IT->current.overlay_string_index has advanced to a position
4761 where we must load IT->overlay_strings with more strings, do
4762 it. */
4763 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
4764
4765 if (it->current.overlay_string_index && i == 0)
4766 load_overlay_strings (it, 0);
4767
4768 /* Initialize IT to deliver display elements from the overlay
4769 string. */
4770 it->string = it->overlay_strings[i];
4771 it->multibyte_p = STRING_MULTIBYTE (it->string);
4772 SET_TEXT_POS (it->current.string_pos, 0, 0);
4773 it->method = GET_FROM_STRING;
4774 it->stop_charpos = 0;
4775 if (it->cmp_it.stop_pos >= 0)
4776 it->cmp_it.stop_pos = 0;
4777 }
4778
4779 CHECK_IT (it);
4780 }
4781
4782
4783 /* Compare two overlay_entry structures E1 and E2. Used as a
4784 comparison function for qsort in load_overlay_strings. Overlay
4785 strings for the same position are sorted so that
4786
4787 1. All after-strings come in front of before-strings, except
4788 when they come from the same overlay.
4789
4790 2. Within after-strings, strings are sorted so that overlay strings
4791 from overlays with higher priorities come first.
4792
4793 2. Within before-strings, strings are sorted so that overlay
4794 strings from overlays with higher priorities come last.
4795
4796 Value is analogous to strcmp. */
4797
4798
4799 static int
4800 compare_overlay_entries (e1, e2)
4801 void *e1, *e2;
4802 {
4803 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
4804 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
4805 int result;
4806
4807 if (entry1->after_string_p != entry2->after_string_p)
4808 {
4809 /* Let after-strings appear in front of before-strings if
4810 they come from different overlays. */
4811 if (EQ (entry1->overlay, entry2->overlay))
4812 result = entry1->after_string_p ? 1 : -1;
4813 else
4814 result = entry1->after_string_p ? -1 : 1;
4815 }
4816 else if (entry1->after_string_p)
4817 /* After-strings sorted in order of decreasing priority. */
4818 result = entry2->priority - entry1->priority;
4819 else
4820 /* Before-strings sorted in order of increasing priority. */
4821 result = entry1->priority - entry2->priority;
4822
4823 return result;
4824 }
4825
4826
4827 /* Load the vector IT->overlay_strings with overlay strings from IT's
4828 current buffer position, or from CHARPOS if that is > 0. Set
4829 IT->n_overlays to the total number of overlay strings found.
4830
4831 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
4832 a time. On entry into load_overlay_strings,
4833 IT->current.overlay_string_index gives the number of overlay
4834 strings that have already been loaded by previous calls to this
4835 function.
4836
4837 IT->add_overlay_start contains an additional overlay start
4838 position to consider for taking overlay strings from, if non-zero.
4839 This position comes into play when the overlay has an `invisible'
4840 property, and both before and after-strings. When we've skipped to
4841 the end of the overlay, because of its `invisible' property, we
4842 nevertheless want its before-string to appear.
4843 IT->add_overlay_start will contain the overlay start position
4844 in this case.
4845
4846 Overlay strings are sorted so that after-string strings come in
4847 front of before-string strings. Within before and after-strings,
4848 strings are sorted by overlay priority. See also function
4849 compare_overlay_entries. */
4850
4851 static void
4852 load_overlay_strings (it, charpos)
4853 struct it *it;
4854 int charpos;
4855 {
4856 extern Lisp_Object Qafter_string, Qbefore_string, Qwindow, Qpriority;
4857 Lisp_Object overlay, window, str, invisible;
4858 struct Lisp_Overlay *ov;
4859 int start, end;
4860 int size = 20;
4861 int n = 0, i, j, invis_p;
4862 struct overlay_entry *entries
4863 = (struct overlay_entry *) alloca (size * sizeof *entries);
4864
4865 if (charpos <= 0)
4866 charpos = IT_CHARPOS (*it);
4867
4868 /* Append the overlay string STRING of overlay OVERLAY to vector
4869 `entries' which has size `size' and currently contains `n'
4870 elements. AFTER_P non-zero means STRING is an after-string of
4871 OVERLAY. */
4872 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
4873 do \
4874 { \
4875 Lisp_Object priority; \
4876 \
4877 if (n == size) \
4878 { \
4879 int new_size = 2 * size; \
4880 struct overlay_entry *old = entries; \
4881 entries = \
4882 (struct overlay_entry *) alloca (new_size \
4883 * sizeof *entries); \
4884 bcopy (old, entries, size * sizeof *entries); \
4885 size = new_size; \
4886 } \
4887 \
4888 entries[n].string = (STRING); \
4889 entries[n].overlay = (OVERLAY); \
4890 priority = Foverlay_get ((OVERLAY), Qpriority); \
4891 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
4892 entries[n].after_string_p = (AFTER_P); \
4893 ++n; \
4894 } \
4895 while (0)
4896
4897 /* Process overlay before the overlay center. */
4898 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
4899 {
4900 XSETMISC (overlay, ov);
4901 xassert (OVERLAYP (overlay));
4902 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4903 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4904
4905 if (end < charpos)
4906 break;
4907
4908 /* Skip this overlay if it doesn't start or end at IT's current
4909 position. */
4910 if (end != charpos && start != charpos)
4911 continue;
4912
4913 /* Skip this overlay if it doesn't apply to IT->w. */
4914 window = Foverlay_get (overlay, Qwindow);
4915 if (WINDOWP (window) && XWINDOW (window) != it->w)
4916 continue;
4917
4918 /* If the text ``under'' the overlay is invisible, both before-
4919 and after-strings from this overlay are visible; start and
4920 end position are indistinguishable. */
4921 invisible = Foverlay_get (overlay, Qinvisible);
4922 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4923
4924 /* If overlay has a non-empty before-string, record it. */
4925 if ((start == charpos || (end == charpos && invis_p))
4926 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
4927 && SCHARS (str))
4928 RECORD_OVERLAY_STRING (overlay, str, 0);
4929
4930 /* If overlay has a non-empty after-string, record it. */
4931 if ((end == charpos || (start == charpos && invis_p))
4932 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
4933 && SCHARS (str))
4934 RECORD_OVERLAY_STRING (overlay, str, 1);
4935 }
4936
4937 /* Process overlays after the overlay center. */
4938 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
4939 {
4940 XSETMISC (overlay, ov);
4941 xassert (OVERLAYP (overlay));
4942 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4943 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4944
4945 if (start > charpos)
4946 break;
4947
4948 /* Skip this overlay if it doesn't start or end at IT's current
4949 position. */
4950 if (end != charpos && start != charpos)
4951 continue;
4952
4953 /* Skip this overlay if it doesn't apply to IT->w. */
4954 window = Foverlay_get (overlay, Qwindow);
4955 if (WINDOWP (window) && XWINDOW (window) != it->w)
4956 continue;
4957
4958 /* If the text ``under'' the overlay is invisible, it has a zero
4959 dimension, and both before- and after-strings apply. */
4960 invisible = Foverlay_get (overlay, Qinvisible);
4961 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4962
4963 /* If overlay has a non-empty before-string, record it. */
4964 if ((start == charpos || (end == charpos && invis_p))
4965 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
4966 && SCHARS (str))
4967 RECORD_OVERLAY_STRING (overlay, str, 0);
4968
4969 /* If overlay has a non-empty after-string, record it. */
4970 if ((end == charpos || (start == charpos && invis_p))
4971 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
4972 && SCHARS (str))
4973 RECORD_OVERLAY_STRING (overlay, str, 1);
4974 }
4975
4976 #undef RECORD_OVERLAY_STRING
4977
4978 /* Sort entries. */
4979 if (n > 1)
4980 qsort (entries, n, sizeof *entries, compare_overlay_entries);
4981
4982 /* Record the total number of strings to process. */
4983 it->n_overlay_strings = n;
4984
4985 /* IT->current.overlay_string_index is the number of overlay strings
4986 that have already been consumed by IT. Copy some of the
4987 remaining overlay strings to IT->overlay_strings. */
4988 i = 0;
4989 j = it->current.overlay_string_index;
4990 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
4991 {
4992 it->overlay_strings[i] = entries[j].string;
4993 it->string_overlays[i++] = entries[j++].overlay;
4994 }
4995
4996 CHECK_IT (it);
4997 }
4998
4999
5000 /* Get the first chunk of overlay strings at IT's current buffer
5001 position, or at CHARPOS if that is > 0. Value is non-zero if at
5002 least one overlay string was found. */
5003
5004 static int
5005 get_overlay_strings_1 (it, charpos, compute_stop_p)
5006 struct it *it;
5007 int charpos;
5008 int compute_stop_p;
5009 {
5010 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
5011 process. This fills IT->overlay_strings with strings, and sets
5012 IT->n_overlay_strings to the total number of strings to process.
5013 IT->pos.overlay_string_index has to be set temporarily to zero
5014 because load_overlay_strings needs this; it must be set to -1
5015 when no overlay strings are found because a zero value would
5016 indicate a position in the first overlay string. */
5017 it->current.overlay_string_index = 0;
5018 load_overlay_strings (it, charpos);
5019
5020 /* If we found overlay strings, set up IT to deliver display
5021 elements from the first one. Otherwise set up IT to deliver
5022 from current_buffer. */
5023 if (it->n_overlay_strings)
5024 {
5025 /* Make sure we know settings in current_buffer, so that we can
5026 restore meaningful values when we're done with the overlay
5027 strings. */
5028 if (compute_stop_p)
5029 compute_stop_pos (it);
5030 xassert (it->face_id >= 0);
5031
5032 /* Save IT's settings. They are restored after all overlay
5033 strings have been processed. */
5034 xassert (!compute_stop_p || it->sp == 0);
5035 push_it (it);
5036
5037 /* Set up IT to deliver display elements from the first overlay
5038 string. */
5039 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5040 it->string = it->overlay_strings[0];
5041 it->from_overlay = Qnil;
5042 it->stop_charpos = 0;
5043 xassert (STRINGP (it->string));
5044 it->end_charpos = SCHARS (it->string);
5045 it->multibyte_p = STRING_MULTIBYTE (it->string);
5046 it->method = GET_FROM_STRING;
5047 return 1;
5048 }
5049
5050 it->current.overlay_string_index = -1;
5051 return 0;
5052 }
5053
5054 static int
5055 get_overlay_strings (it, charpos)
5056 struct it *it;
5057 int charpos;
5058 {
5059 it->string = Qnil;
5060 it->method = GET_FROM_BUFFER;
5061
5062 (void) get_overlay_strings_1 (it, charpos, 1);
5063
5064 CHECK_IT (it);
5065
5066 /* Value is non-zero if we found at least one overlay string. */
5067 return STRINGP (it->string);
5068 }
5069
5070
5071 \f
5072 /***********************************************************************
5073 Saving and restoring state
5074 ***********************************************************************/
5075
5076 /* Save current settings of IT on IT->stack. Called, for example,
5077 before setting up IT for an overlay string, to be able to restore
5078 IT's settings to what they were after the overlay string has been
5079 processed. */
5080
5081 static void
5082 push_it (it)
5083 struct it *it;
5084 {
5085 struct iterator_stack_entry *p;
5086
5087 xassert (it->sp < IT_STACK_SIZE);
5088 p = it->stack + it->sp;
5089
5090 p->stop_charpos = it->stop_charpos;
5091 p->cmp_it = it->cmp_it;
5092 xassert (it->face_id >= 0);
5093 p->face_id = it->face_id;
5094 p->string = it->string;
5095 p->method = it->method;
5096 p->from_overlay = it->from_overlay;
5097 switch (p->method)
5098 {
5099 case GET_FROM_IMAGE:
5100 p->u.image.object = it->object;
5101 p->u.image.image_id = it->image_id;
5102 p->u.image.slice = it->slice;
5103 break;
5104 case GET_FROM_STRETCH:
5105 p->u.stretch.object = it->object;
5106 break;
5107 }
5108 p->position = it->position;
5109 p->current = it->current;
5110 p->end_charpos = it->end_charpos;
5111 p->string_nchars = it->string_nchars;
5112 p->area = it->area;
5113 p->multibyte_p = it->multibyte_p;
5114 p->avoid_cursor_p = it->avoid_cursor_p;
5115 p->space_width = it->space_width;
5116 p->font_height = it->font_height;
5117 p->voffset = it->voffset;
5118 p->string_from_display_prop_p = it->string_from_display_prop_p;
5119 p->display_ellipsis_p = 0;
5120 ++it->sp;
5121 }
5122
5123
5124 /* Restore IT's settings from IT->stack. Called, for example, when no
5125 more overlay strings must be processed, and we return to delivering
5126 display elements from a buffer, or when the end of a string from a
5127 `display' property is reached and we return to delivering display
5128 elements from an overlay string, or from a buffer. */
5129
5130 static void
5131 pop_it (it)
5132 struct it *it;
5133 {
5134 struct iterator_stack_entry *p;
5135
5136 xassert (it->sp > 0);
5137 --it->sp;
5138 p = it->stack + it->sp;
5139 it->stop_charpos = p->stop_charpos;
5140 it->cmp_it = p->cmp_it;
5141 it->face_id = p->face_id;
5142 it->current = p->current;
5143 it->position = p->position;
5144 it->string = p->string;
5145 it->from_overlay = p->from_overlay;
5146 if (NILP (it->string))
5147 SET_TEXT_POS (it->current.string_pos, -1, -1);
5148 it->method = p->method;
5149 switch (it->method)
5150 {
5151 case GET_FROM_IMAGE:
5152 it->image_id = p->u.image.image_id;
5153 it->object = p->u.image.object;
5154 it->slice = p->u.image.slice;
5155 break;
5156 case GET_FROM_STRETCH:
5157 it->object = p->u.comp.object;
5158 break;
5159 case GET_FROM_BUFFER:
5160 it->object = it->w->buffer;
5161 break;
5162 case GET_FROM_STRING:
5163 it->object = it->string;
5164 break;
5165 }
5166 it->end_charpos = p->end_charpos;
5167 it->string_nchars = p->string_nchars;
5168 it->area = p->area;
5169 it->multibyte_p = p->multibyte_p;
5170 it->avoid_cursor_p = p->avoid_cursor_p;
5171 it->space_width = p->space_width;
5172 it->font_height = p->font_height;
5173 it->voffset = p->voffset;
5174 it->string_from_display_prop_p = p->string_from_display_prop_p;
5175 }
5176
5177
5178 \f
5179 /***********************************************************************
5180 Moving over lines
5181 ***********************************************************************/
5182
5183 /* Set IT's current position to the previous line start. */
5184
5185 static void
5186 back_to_previous_line_start (it)
5187 struct it *it;
5188 {
5189 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
5190 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
5191 }
5192
5193
5194 /* Move IT to the next line start.
5195
5196 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
5197 we skipped over part of the text (as opposed to moving the iterator
5198 continuously over the text). Otherwise, don't change the value
5199 of *SKIPPED_P.
5200
5201 Newlines may come from buffer text, overlay strings, or strings
5202 displayed via the `display' property. That's the reason we can't
5203 simply use find_next_newline_no_quit.
5204
5205 Note that this function may not skip over invisible text that is so
5206 because of text properties and immediately follows a newline. If
5207 it would, function reseat_at_next_visible_line_start, when called
5208 from set_iterator_to_next, would effectively make invisible
5209 characters following a newline part of the wrong glyph row, which
5210 leads to wrong cursor motion. */
5211
5212 static int
5213 forward_to_next_line_start (it, skipped_p)
5214 struct it *it;
5215 int *skipped_p;
5216 {
5217 int old_selective, newline_found_p, n;
5218 const int MAX_NEWLINE_DISTANCE = 500;
5219
5220 /* If already on a newline, just consume it to avoid unintended
5221 skipping over invisible text below. */
5222 if (it->what == IT_CHARACTER
5223 && it->c == '\n'
5224 && CHARPOS (it->position) == IT_CHARPOS (*it))
5225 {
5226 set_iterator_to_next (it, 0);
5227 it->c = 0;
5228 return 1;
5229 }
5230
5231 /* Don't handle selective display in the following. It's (a)
5232 unnecessary because it's done by the caller, and (b) leads to an
5233 infinite recursion because next_element_from_ellipsis indirectly
5234 calls this function. */
5235 old_selective = it->selective;
5236 it->selective = 0;
5237
5238 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
5239 from buffer text. */
5240 for (n = newline_found_p = 0;
5241 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
5242 n += STRINGP (it->string) ? 0 : 1)
5243 {
5244 if (!get_next_display_element (it))
5245 return 0;
5246 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
5247 set_iterator_to_next (it, 0);
5248 }
5249
5250 /* If we didn't find a newline near enough, see if we can use a
5251 short-cut. */
5252 if (!newline_found_p)
5253 {
5254 int start = IT_CHARPOS (*it);
5255 int limit = find_next_newline_no_quit (start, 1);
5256 Lisp_Object pos;
5257
5258 xassert (!STRINGP (it->string));
5259
5260 /* If there isn't any `display' property in sight, and no
5261 overlays, we can just use the position of the newline in
5262 buffer text. */
5263 if (it->stop_charpos >= limit
5264 || ((pos = Fnext_single_property_change (make_number (start),
5265 Qdisplay,
5266 Qnil, make_number (limit)),
5267 NILP (pos))
5268 && next_overlay_change (start) == ZV))
5269 {
5270 IT_CHARPOS (*it) = limit;
5271 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
5272 *skipped_p = newline_found_p = 1;
5273 }
5274 else
5275 {
5276 while (get_next_display_element (it)
5277 && !newline_found_p)
5278 {
5279 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
5280 set_iterator_to_next (it, 0);
5281 }
5282 }
5283 }
5284
5285 it->selective = old_selective;
5286 return newline_found_p;
5287 }
5288
5289
5290 /* Set IT's current position to the previous visible line start. Skip
5291 invisible text that is so either due to text properties or due to
5292 selective display. Caution: this does not change IT->current_x and
5293 IT->hpos. */
5294
5295 static void
5296 back_to_previous_visible_line_start (it)
5297 struct it *it;
5298 {
5299 while (IT_CHARPOS (*it) > BEGV)
5300 {
5301 back_to_previous_line_start (it);
5302
5303 if (IT_CHARPOS (*it) <= BEGV)
5304 break;
5305
5306 /* If selective > 0, then lines indented more than that values
5307 are invisible. */
5308 if (it->selective > 0
5309 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
5310 (double) it->selective)) /* iftc */
5311 continue;
5312
5313 /* Check the newline before point for invisibility. */
5314 {
5315 Lisp_Object prop;
5316 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
5317 Qinvisible, it->window);
5318 if (TEXT_PROP_MEANS_INVISIBLE (prop))
5319 continue;
5320 }
5321
5322 if (IT_CHARPOS (*it) <= BEGV)
5323 break;
5324
5325 {
5326 struct it it2;
5327 int pos;
5328 EMACS_INT beg, end;
5329 Lisp_Object val, overlay;
5330
5331 /* If newline is part of a composition, continue from start of composition */
5332 if (find_composition (IT_CHARPOS (*it), -1, &beg, &end, &val, Qnil)
5333 && beg < IT_CHARPOS (*it))
5334 goto replaced;
5335
5336 /* If newline is replaced by a display property, find start of overlay
5337 or interval and continue search from that point. */
5338 it2 = *it;
5339 pos = --IT_CHARPOS (it2);
5340 --IT_BYTEPOS (it2);
5341 it2.sp = 0;
5342 it2.string_from_display_prop_p = 0;
5343 if (handle_display_prop (&it2) == HANDLED_RETURN
5344 && !NILP (val = get_char_property_and_overlay
5345 (make_number (pos), Qdisplay, Qnil, &overlay))
5346 && (OVERLAYP (overlay)
5347 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
5348 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
5349 goto replaced;
5350
5351 /* Newline is not replaced by anything -- so we are done. */
5352 break;
5353
5354 replaced:
5355 if (beg < BEGV)
5356 beg = BEGV;
5357 IT_CHARPOS (*it) = beg;
5358 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
5359 }
5360 }
5361
5362 it->continuation_lines_width = 0;
5363
5364 xassert (IT_CHARPOS (*it) >= BEGV);
5365 xassert (IT_CHARPOS (*it) == BEGV
5366 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
5367 CHECK_IT (it);
5368 }
5369
5370
5371 /* Reseat iterator IT at the previous visible line start. Skip
5372 invisible text that is so either due to text properties or due to
5373 selective display. At the end, update IT's overlay information,
5374 face information etc. */
5375
5376 void
5377 reseat_at_previous_visible_line_start (it)
5378 struct it *it;
5379 {
5380 back_to_previous_visible_line_start (it);
5381 reseat (it, it->current.pos, 1);
5382 CHECK_IT (it);
5383 }
5384
5385
5386 /* Reseat iterator IT on the next visible line start in the current
5387 buffer. ON_NEWLINE_P non-zero means position IT on the newline
5388 preceding the line start. Skip over invisible text that is so
5389 because of selective display. Compute faces, overlays etc at the
5390 new position. Note that this function does not skip over text that
5391 is invisible because of text properties. */
5392
5393 static void
5394 reseat_at_next_visible_line_start (it, on_newline_p)
5395 struct it *it;
5396 int on_newline_p;
5397 {
5398 int newline_found_p, skipped_p = 0;
5399
5400 newline_found_p = forward_to_next_line_start (it, &skipped_p);
5401
5402 /* Skip over lines that are invisible because they are indented
5403 more than the value of IT->selective. */
5404 if (it->selective > 0)
5405 while (IT_CHARPOS (*it) < ZV
5406 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
5407 (double) it->selective)) /* iftc */
5408 {
5409 xassert (IT_BYTEPOS (*it) == BEGV
5410 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
5411 newline_found_p = forward_to_next_line_start (it, &skipped_p);
5412 }
5413
5414 /* Position on the newline if that's what's requested. */
5415 if (on_newline_p && newline_found_p)
5416 {
5417 if (STRINGP (it->string))
5418 {
5419 if (IT_STRING_CHARPOS (*it) > 0)
5420 {
5421 --IT_STRING_CHARPOS (*it);
5422 --IT_STRING_BYTEPOS (*it);
5423 }
5424 }
5425 else if (IT_CHARPOS (*it) > BEGV)
5426 {
5427 --IT_CHARPOS (*it);
5428 --IT_BYTEPOS (*it);
5429 reseat (it, it->current.pos, 0);
5430 }
5431 }
5432 else if (skipped_p)
5433 reseat (it, it->current.pos, 0);
5434
5435 CHECK_IT (it);
5436 }
5437
5438
5439 \f
5440 /***********************************************************************
5441 Changing an iterator's position
5442 ***********************************************************************/
5443
5444 /* Change IT's current position to POS in current_buffer. If FORCE_P
5445 is non-zero, always check for text properties at the new position.
5446 Otherwise, text properties are only looked up if POS >=
5447 IT->check_charpos of a property. */
5448
5449 static void
5450 reseat (it, pos, force_p)
5451 struct it *it;
5452 struct text_pos pos;
5453 int force_p;
5454 {
5455 int original_pos = IT_CHARPOS (*it);
5456
5457 reseat_1 (it, pos, 0);
5458
5459 /* Determine where to check text properties. Avoid doing it
5460 where possible because text property lookup is very expensive. */
5461 if (force_p
5462 || CHARPOS (pos) > it->stop_charpos
5463 || CHARPOS (pos) < original_pos)
5464 handle_stop (it);
5465
5466 CHECK_IT (it);
5467 }
5468
5469
5470 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
5471 IT->stop_pos to POS, also. */
5472
5473 static void
5474 reseat_1 (it, pos, set_stop_p)
5475 struct it *it;
5476 struct text_pos pos;
5477 int set_stop_p;
5478 {
5479 /* Don't call this function when scanning a C string. */
5480 xassert (it->s == NULL);
5481
5482 /* POS must be a reasonable value. */
5483 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
5484
5485 it->current.pos = it->position = pos;
5486 it->end_charpos = ZV;
5487 it->dpvec = NULL;
5488 it->current.dpvec_index = -1;
5489 it->current.overlay_string_index = -1;
5490 IT_STRING_CHARPOS (*it) = -1;
5491 IT_STRING_BYTEPOS (*it) = -1;
5492 it->string = Qnil;
5493 it->string_from_display_prop_p = 0;
5494 it->method = GET_FROM_BUFFER;
5495 it->object = it->w->buffer;
5496 it->area = TEXT_AREA;
5497 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
5498 it->sp = 0;
5499 it->string_from_display_prop_p = 0;
5500 it->face_before_selective_p = 0;
5501
5502 if (set_stop_p)
5503 it->stop_charpos = CHARPOS (pos);
5504 }
5505
5506
5507 /* Set up IT for displaying a string, starting at CHARPOS in window W.
5508 If S is non-null, it is a C string to iterate over. Otherwise,
5509 STRING gives a Lisp string to iterate over.
5510
5511 If PRECISION > 0, don't return more then PRECISION number of
5512 characters from the string.
5513
5514 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
5515 characters have been returned. FIELD_WIDTH < 0 means an infinite
5516 field width.
5517
5518 MULTIBYTE = 0 means disable processing of multibyte characters,
5519 MULTIBYTE > 0 means enable it,
5520 MULTIBYTE < 0 means use IT->multibyte_p.
5521
5522 IT must be initialized via a prior call to init_iterator before
5523 calling this function. */
5524
5525 static void
5526 reseat_to_string (it, s, string, charpos, precision, field_width, multibyte)
5527 struct it *it;
5528 unsigned char *s;
5529 Lisp_Object string;
5530 int charpos;
5531 int precision, field_width, multibyte;
5532 {
5533 /* No region in strings. */
5534 it->region_beg_charpos = it->region_end_charpos = -1;
5535
5536 /* No text property checks performed by default, but see below. */
5537 it->stop_charpos = -1;
5538
5539 /* Set iterator position and end position. */
5540 bzero (&it->current, sizeof it->current);
5541 it->current.overlay_string_index = -1;
5542 it->current.dpvec_index = -1;
5543 xassert (charpos >= 0);
5544
5545 /* If STRING is specified, use its multibyteness, otherwise use the
5546 setting of MULTIBYTE, if specified. */
5547 if (multibyte >= 0)
5548 it->multibyte_p = multibyte > 0;
5549
5550 if (s == NULL)
5551 {
5552 xassert (STRINGP (string));
5553 it->string = string;
5554 it->s = NULL;
5555 it->end_charpos = it->string_nchars = SCHARS (string);
5556 it->method = GET_FROM_STRING;
5557 it->current.string_pos = string_pos (charpos, string);
5558 }
5559 else
5560 {
5561 it->s = s;
5562 it->string = Qnil;
5563
5564 /* Note that we use IT->current.pos, not it->current.string_pos,
5565 for displaying C strings. */
5566 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
5567 if (it->multibyte_p)
5568 {
5569 it->current.pos = c_string_pos (charpos, s, 1);
5570 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
5571 }
5572 else
5573 {
5574 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
5575 it->end_charpos = it->string_nchars = strlen (s);
5576 }
5577
5578 it->method = GET_FROM_C_STRING;
5579 }
5580
5581 /* PRECISION > 0 means don't return more than PRECISION characters
5582 from the string. */
5583 if (precision > 0 && it->end_charpos - charpos > precision)
5584 it->end_charpos = it->string_nchars = charpos + precision;
5585
5586 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
5587 characters have been returned. FIELD_WIDTH == 0 means don't pad,
5588 FIELD_WIDTH < 0 means infinite field width. This is useful for
5589 padding with `-' at the end of a mode line. */
5590 if (field_width < 0)
5591 field_width = INFINITY;
5592 if (field_width > it->end_charpos - charpos)
5593 it->end_charpos = charpos + field_width;
5594
5595 /* Use the standard display table for displaying strings. */
5596 if (DISP_TABLE_P (Vstandard_display_table))
5597 it->dp = XCHAR_TABLE (Vstandard_display_table);
5598
5599 it->stop_charpos = charpos;
5600 CHECK_IT (it);
5601 }
5602
5603
5604 \f
5605 /***********************************************************************
5606 Iteration
5607 ***********************************************************************/
5608
5609 /* Map enum it_method value to corresponding next_element_from_* function. */
5610
5611 static int (* get_next_element[NUM_IT_METHODS]) P_ ((struct it *it)) =
5612 {
5613 next_element_from_buffer,
5614 next_element_from_display_vector,
5615 next_element_from_string,
5616 next_element_from_c_string,
5617 next_element_from_image,
5618 next_element_from_stretch
5619 };
5620
5621 #define GET_NEXT_DISPLAY_ELEMENT(it) (*get_next_element[(it)->method]) (it)
5622
5623
5624 /* Return 1 iff a character at CHARPOS (and BYTEPOS) is composed
5625 (possibly with the following characters). */
5626
5627 #define CHAR_COMPOSED_P(IT,CHARPOS,BYTEPOS) \
5628 ((IT)->cmp_it.id >= 0 \
5629 || ((IT)->cmp_it.stop_pos == (CHARPOS) \
5630 && composition_reseat_it (&(IT)->cmp_it, CHARPOS, BYTEPOS, \
5631 (IT)->end_charpos, (IT)->w, \
5632 FACE_FROM_ID ((IT)->f, (IT)->face_id), \
5633 (IT)->string)))
5634
5635
5636 /* Load IT's display element fields with information about the next
5637 display element from the current position of IT. Value is zero if
5638 end of buffer (or C string) is reached. */
5639
5640 static struct frame *last_escape_glyph_frame = NULL;
5641 static unsigned last_escape_glyph_face_id = (1 << FACE_ID_BITS);
5642 static int last_escape_glyph_merged_face_id = 0;
5643
5644 int
5645 get_next_display_element (it)
5646 struct it *it;
5647 {
5648 /* Non-zero means that we found a display element. Zero means that
5649 we hit the end of what we iterate over. Performance note: the
5650 function pointer `method' used here turns out to be faster than
5651 using a sequence of if-statements. */
5652 int success_p;
5653
5654 get_next:
5655 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
5656
5657 if (it->what == IT_CHARACTER)
5658 {
5659 /* Map via display table or translate control characters.
5660 IT->c, IT->len etc. have been set to the next character by
5661 the function call above. If we have a display table, and it
5662 contains an entry for IT->c, translate it. Don't do this if
5663 IT->c itself comes from a display table, otherwise we could
5664 end up in an infinite recursion. (An alternative could be to
5665 count the recursion depth of this function and signal an
5666 error when a certain maximum depth is reached.) Is it worth
5667 it? */
5668 if (success_p && it->dpvec == NULL)
5669 {
5670 Lisp_Object dv;
5671
5672 if (it->dp
5673 && (dv = DISP_CHAR_VECTOR (it->dp, it->c),
5674 VECTORP (dv)))
5675 {
5676 struct Lisp_Vector *v = XVECTOR (dv);
5677
5678 /* Return the first character from the display table
5679 entry, if not empty. If empty, don't display the
5680 current character. */
5681 if (v->size)
5682 {
5683 it->dpvec_char_len = it->len;
5684 it->dpvec = v->contents;
5685 it->dpend = v->contents + v->size;
5686 it->current.dpvec_index = 0;
5687 it->dpvec_face_id = -1;
5688 it->saved_face_id = it->face_id;
5689 it->method = GET_FROM_DISPLAY_VECTOR;
5690 it->ellipsis_p = 0;
5691 }
5692 else
5693 {
5694 set_iterator_to_next (it, 0);
5695 }
5696 goto get_next;
5697 }
5698
5699 /* Translate control characters into `\003' or `^C' form.
5700 Control characters coming from a display table entry are
5701 currently not translated because we use IT->dpvec to hold
5702 the translation. This could easily be changed but I
5703 don't believe that it is worth doing.
5704
5705 If it->multibyte_p is nonzero, non-printable non-ASCII
5706 characters are also translated to octal form.
5707
5708 If it->multibyte_p is zero, eight-bit characters that
5709 don't have corresponding multibyte char code are also
5710 translated to octal form. */
5711 else if ((it->c < ' '
5712 ? (it->area != TEXT_AREA
5713 /* In mode line, treat \n, \t like other crl chars. */
5714 || (it->c != '\t'
5715 && it->glyph_row && it->glyph_row->mode_line_p)
5716 || (it->c != '\n' && it->c != '\t'))
5717 : (it->multibyte_p
5718 ? (!CHAR_PRINTABLE_P (it->c)
5719 || (!NILP (Vnobreak_char_display)
5720 && (it->c == 0xA0 /* NO-BREAK SPACE */
5721 || it->c == 0xAD /* SOFT HYPHEN */)))
5722 : (it->c >= 127
5723 && (! unibyte_display_via_language_environment
5724 || (UNIBYTE_CHAR_HAS_MULTIBYTE_P (it->c)))))))
5725 {
5726 /* IT->c is a control character which must be displayed
5727 either as '\003' or as `^C' where the '\\' and '^'
5728 can be defined in the display table. Fill
5729 IT->ctl_chars with glyphs for what we have to
5730 display. Then, set IT->dpvec to these glyphs. */
5731 Lisp_Object gc;
5732 int ctl_len;
5733 int face_id, lface_id = 0 ;
5734 int escape_glyph;
5735
5736 /* Handle control characters with ^. */
5737
5738 if (it->c < 128 && it->ctl_arrow_p)
5739 {
5740 int g;
5741
5742 g = '^'; /* default glyph for Control */
5743 /* Set IT->ctl_chars[0] to the glyph for `^'. */
5744 if (it->dp
5745 && (gc = DISP_CTRL_GLYPH (it->dp), GLYPH_CODE_P (gc))
5746 && GLYPH_CODE_CHAR_VALID_P (gc))
5747 {
5748 g = GLYPH_CODE_CHAR (gc);
5749 lface_id = GLYPH_CODE_FACE (gc);
5750 }
5751 if (lface_id)
5752 {
5753 face_id = merge_faces (it->f, Qt, lface_id, it->face_id);
5754 }
5755 else if (it->f == last_escape_glyph_frame
5756 && it->face_id == last_escape_glyph_face_id)
5757 {
5758 face_id = last_escape_glyph_merged_face_id;
5759 }
5760 else
5761 {
5762 /* Merge the escape-glyph face into the current face. */
5763 face_id = merge_faces (it->f, Qescape_glyph, 0,
5764 it->face_id);
5765 last_escape_glyph_frame = it->f;
5766 last_escape_glyph_face_id = it->face_id;
5767 last_escape_glyph_merged_face_id = face_id;
5768 }
5769
5770 XSETINT (it->ctl_chars[0], g);
5771 XSETINT (it->ctl_chars[1], it->c ^ 0100);
5772 ctl_len = 2;
5773 goto display_control;
5774 }
5775
5776 /* Handle non-break space in the mode where it only gets
5777 highlighting. */
5778
5779 if (EQ (Vnobreak_char_display, Qt)
5780 && it->c == 0xA0)
5781 {
5782 /* Merge the no-break-space face into the current face. */
5783 face_id = merge_faces (it->f, Qnobreak_space, 0,
5784 it->face_id);
5785
5786 it->c = ' ';
5787 XSETINT (it->ctl_chars[0], ' ');
5788 ctl_len = 1;
5789 goto display_control;
5790 }
5791
5792 /* Handle sequences that start with the "escape glyph". */
5793
5794 /* the default escape glyph is \. */
5795 escape_glyph = '\\';
5796
5797 if (it->dp
5798 && (gc = DISP_ESCAPE_GLYPH (it->dp), GLYPH_CODE_P (gc))
5799 && GLYPH_CODE_CHAR_VALID_P (gc))
5800 {
5801 escape_glyph = GLYPH_CODE_CHAR (gc);
5802 lface_id = GLYPH_CODE_FACE (gc);
5803 }
5804 if (lface_id)
5805 {
5806 /* The display table specified a face.
5807 Merge it into face_id and also into escape_glyph. */
5808 face_id = merge_faces (it->f, Qt, lface_id,
5809 it->face_id);
5810 }
5811 else if (it->f == last_escape_glyph_frame
5812 && it->face_id == last_escape_glyph_face_id)
5813 {
5814 face_id = last_escape_glyph_merged_face_id;
5815 }
5816 else
5817 {
5818 /* Merge the escape-glyph face into the current face. */
5819 face_id = merge_faces (it->f, Qescape_glyph, 0,
5820 it->face_id);
5821 last_escape_glyph_frame = it->f;
5822 last_escape_glyph_face_id = it->face_id;
5823 last_escape_glyph_merged_face_id = face_id;
5824 }
5825
5826 /* Handle soft hyphens in the mode where they only get
5827 highlighting. */
5828
5829 if (EQ (Vnobreak_char_display, Qt)
5830 && it->c == 0xAD)
5831 {
5832 it->c = '-';
5833 XSETINT (it->ctl_chars[0], '-');
5834 ctl_len = 1;
5835 goto display_control;
5836 }
5837
5838 /* Handle non-break space and soft hyphen
5839 with the escape glyph. */
5840
5841 if (it->c == 0xA0 || it->c == 0xAD)
5842 {
5843 XSETINT (it->ctl_chars[0], escape_glyph);
5844 it->c = (it->c == 0xA0 ? ' ' : '-');
5845 XSETINT (it->ctl_chars[1], it->c);
5846 ctl_len = 2;
5847 goto display_control;
5848 }
5849
5850 {
5851 unsigned char str[MAX_MULTIBYTE_LENGTH];
5852 int len;
5853 int i;
5854
5855 /* Set IT->ctl_chars[0] to the glyph for `\\'. */
5856 if (CHAR_BYTE8_P (it->c))
5857 {
5858 str[0] = CHAR_TO_BYTE8 (it->c);
5859 len = 1;
5860 }
5861 else if (it->c < 256)
5862 {
5863 str[0] = it->c;
5864 len = 1;
5865 }
5866 else
5867 {
5868 /* It's an invalid character, which shouldn't
5869 happen actually, but due to bugs it may
5870 happen. Let's print the char as is, there's
5871 not much meaningful we can do with it. */
5872 str[0] = it->c;
5873 str[1] = it->c >> 8;
5874 str[2] = it->c >> 16;
5875 str[3] = it->c >> 24;
5876 len = 4;
5877 }
5878
5879 for (i = 0; i < len; i++)
5880 {
5881 int g;
5882 XSETINT (it->ctl_chars[i * 4], escape_glyph);
5883 /* Insert three more glyphs into IT->ctl_chars for
5884 the octal display of the character. */
5885 g = ((str[i] >> 6) & 7) + '0';
5886 XSETINT (it->ctl_chars[i * 4 + 1], g);
5887 g = ((str[i] >> 3) & 7) + '0';
5888 XSETINT (it->ctl_chars[i * 4 + 2], g);
5889 g = (str[i] & 7) + '0';
5890 XSETINT (it->ctl_chars[i * 4 + 3], g);
5891 }
5892 ctl_len = len * 4;
5893 }
5894
5895 display_control:
5896 /* Set up IT->dpvec and return first character from it. */
5897 it->dpvec_char_len = it->len;
5898 it->dpvec = it->ctl_chars;
5899 it->dpend = it->dpvec + ctl_len;
5900 it->current.dpvec_index = 0;
5901 it->dpvec_face_id = face_id;
5902 it->saved_face_id = it->face_id;
5903 it->method = GET_FROM_DISPLAY_VECTOR;
5904 it->ellipsis_p = 0;
5905 goto get_next;
5906 }
5907 }
5908 }
5909
5910 #ifdef HAVE_WINDOW_SYSTEM
5911 /* Adjust face id for a multibyte character. There are no multibyte
5912 character in unibyte text. */
5913 if ((it->what == IT_CHARACTER || it->what == IT_COMPOSITION)
5914 && it->multibyte_p
5915 && success_p
5916 && FRAME_WINDOW_P (it->f))
5917 {
5918 struct face *face = FACE_FROM_ID (it->f, it->face_id);
5919
5920 if (it->what == IT_COMPOSITION && it->cmp_it.ch >= 0)
5921 {
5922 /* Automatic composition with glyph-string. */
5923 Lisp_Object gstring = composition_gstring_from_id (it->cmp_it.id);
5924
5925 it->face_id = face_for_font (it->f, LGSTRING_FONT (gstring), face);
5926 }
5927 else
5928 {
5929 int pos = (it->s ? -1
5930 : STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
5931 : IT_CHARPOS (*it));
5932
5933 it->face_id = FACE_FOR_CHAR (it->f, face, it->c, pos, it->string);
5934 }
5935 }
5936 #endif
5937
5938 /* Is this character the last one of a run of characters with
5939 box? If yes, set IT->end_of_box_run_p to 1. */
5940 if (it->face_box_p
5941 && it->s == NULL)
5942 {
5943 if (it->method == GET_FROM_STRING && it->sp)
5944 {
5945 int face_id = underlying_face_id (it);
5946 struct face *face = FACE_FROM_ID (it->f, face_id);
5947
5948 if (face)
5949 {
5950 if (face->box == FACE_NO_BOX)
5951 {
5952 /* If the box comes from face properties in a
5953 display string, check faces in that string. */
5954 int string_face_id = face_after_it_pos (it);
5955 it->end_of_box_run_p
5956 = (FACE_FROM_ID (it->f, string_face_id)->box
5957 == FACE_NO_BOX);
5958 }
5959 /* Otherwise, the box comes from the underlying face.
5960 If this is the last string character displayed, check
5961 the next buffer location. */
5962 else if ((IT_STRING_CHARPOS (*it) >= SCHARS (it->string) - 1)
5963 && (it->current.overlay_string_index
5964 == it->n_overlay_strings - 1))
5965 {
5966 EMACS_INT ignore;
5967 int next_face_id;
5968 struct text_pos pos = it->current.pos;
5969 INC_TEXT_POS (pos, it->multibyte_p);
5970
5971 next_face_id = face_at_buffer_position
5972 (it->w, CHARPOS (pos), it->region_beg_charpos,
5973 it->region_end_charpos, &ignore,
5974 (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT), 0);
5975 it->end_of_box_run_p
5976 = (FACE_FROM_ID (it->f, next_face_id)->box
5977 == FACE_NO_BOX);
5978 }
5979 }
5980 }
5981 else
5982 {
5983 int face_id = face_after_it_pos (it);
5984 it->end_of_box_run_p
5985 = (face_id != it->face_id
5986 && FACE_FROM_ID (it->f, face_id)->box == FACE_NO_BOX);
5987 }
5988 }
5989
5990 /* Value is 0 if end of buffer or string reached. */
5991 return success_p;
5992 }
5993
5994
5995 /* Move IT to the next display element.
5996
5997 RESEAT_P non-zero means if called on a newline in buffer text,
5998 skip to the next visible line start.
5999
6000 Functions get_next_display_element and set_iterator_to_next are
6001 separate because I find this arrangement easier to handle than a
6002 get_next_display_element function that also increments IT's
6003 position. The way it is we can first look at an iterator's current
6004 display element, decide whether it fits on a line, and if it does,
6005 increment the iterator position. The other way around we probably
6006 would either need a flag indicating whether the iterator has to be
6007 incremented the next time, or we would have to implement a
6008 decrement position function which would not be easy to write. */
6009
6010 void
6011 set_iterator_to_next (it, reseat_p)
6012 struct it *it;
6013 int reseat_p;
6014 {
6015 /* Reset flags indicating start and end of a sequence of characters
6016 with box. Reset them at the start of this function because
6017 moving the iterator to a new position might set them. */
6018 it->start_of_box_run_p = it->end_of_box_run_p = 0;
6019
6020 switch (it->method)
6021 {
6022 case GET_FROM_BUFFER:
6023 /* The current display element of IT is a character from
6024 current_buffer. Advance in the buffer, and maybe skip over
6025 invisible lines that are so because of selective display. */
6026 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
6027 reseat_at_next_visible_line_start (it, 0);
6028 else if (it->cmp_it.id >= 0)
6029 {
6030 IT_CHARPOS (*it) += it->cmp_it.nchars;
6031 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
6032 if (it->cmp_it.to < it->cmp_it.nglyphs)
6033 it->cmp_it.from = it->cmp_it.to;
6034 else
6035 {
6036 it->cmp_it.id = -1;
6037 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
6038 IT_BYTEPOS (*it), it->stop_charpos,
6039 Qnil);
6040 }
6041 }
6042 else
6043 {
6044 xassert (it->len != 0);
6045 IT_BYTEPOS (*it) += it->len;
6046 IT_CHARPOS (*it) += 1;
6047 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
6048 }
6049 break;
6050
6051 case GET_FROM_C_STRING:
6052 /* Current display element of IT is from a C string. */
6053 IT_BYTEPOS (*it) += it->len;
6054 IT_CHARPOS (*it) += 1;
6055 break;
6056
6057 case GET_FROM_DISPLAY_VECTOR:
6058 /* Current display element of IT is from a display table entry.
6059 Advance in the display table definition. Reset it to null if
6060 end reached, and continue with characters from buffers/
6061 strings. */
6062 ++it->current.dpvec_index;
6063
6064 /* Restore face of the iterator to what they were before the
6065 display vector entry (these entries may contain faces). */
6066 it->face_id = it->saved_face_id;
6067
6068 if (it->dpvec + it->current.dpvec_index == it->dpend)
6069 {
6070 int recheck_faces = it->ellipsis_p;
6071
6072 if (it->s)
6073 it->method = GET_FROM_C_STRING;
6074 else if (STRINGP (it->string))
6075 it->method = GET_FROM_STRING;
6076 else
6077 {
6078 it->method = GET_FROM_BUFFER;
6079 it->object = it->w->buffer;
6080 }
6081
6082 it->dpvec = NULL;
6083 it->current.dpvec_index = -1;
6084
6085 /* Skip over characters which were displayed via IT->dpvec. */
6086 if (it->dpvec_char_len < 0)
6087 reseat_at_next_visible_line_start (it, 1);
6088 else if (it->dpvec_char_len > 0)
6089 {
6090 if (it->method == GET_FROM_STRING
6091 && it->n_overlay_strings > 0)
6092 it->ignore_overlay_strings_at_pos_p = 1;
6093 it->len = it->dpvec_char_len;
6094 set_iterator_to_next (it, reseat_p);
6095 }
6096
6097 /* Maybe recheck faces after display vector */
6098 if (recheck_faces)
6099 it->stop_charpos = IT_CHARPOS (*it);
6100 }
6101 break;
6102
6103 case GET_FROM_STRING:
6104 /* Current display element is a character from a Lisp string. */
6105 xassert (it->s == NULL && STRINGP (it->string));
6106 if (it->cmp_it.id >= 0)
6107 {
6108 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
6109 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
6110 if (it->cmp_it.to < it->cmp_it.nglyphs)
6111 it->cmp_it.from = it->cmp_it.to;
6112 else
6113 {
6114 it->cmp_it.id = -1;
6115 composition_compute_stop_pos (&it->cmp_it,
6116 IT_STRING_CHARPOS (*it),
6117 IT_STRING_BYTEPOS (*it),
6118 it->stop_charpos, it->string);
6119 }
6120 }
6121 else
6122 {
6123 IT_STRING_BYTEPOS (*it) += it->len;
6124 IT_STRING_CHARPOS (*it) += 1;
6125 }
6126
6127 consider_string_end:
6128
6129 if (it->current.overlay_string_index >= 0)
6130 {
6131 /* IT->string is an overlay string. Advance to the
6132 next, if there is one. */
6133 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
6134 {
6135 it->ellipsis_p = 0;
6136 next_overlay_string (it);
6137 if (it->ellipsis_p)
6138 setup_for_ellipsis (it, 0);
6139 }
6140 }
6141 else
6142 {
6143 /* IT->string is not an overlay string. If we reached
6144 its end, and there is something on IT->stack, proceed
6145 with what is on the stack. This can be either another
6146 string, this time an overlay string, or a buffer. */
6147 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
6148 && it->sp > 0)
6149 {
6150 pop_it (it);
6151 if (it->method == GET_FROM_STRING)
6152 goto consider_string_end;
6153 }
6154 }
6155 break;
6156
6157 case GET_FROM_IMAGE:
6158 case GET_FROM_STRETCH:
6159 /* The position etc with which we have to proceed are on
6160 the stack. The position may be at the end of a string,
6161 if the `display' property takes up the whole string. */
6162 xassert (it->sp > 0);
6163 pop_it (it);
6164 if (it->method == GET_FROM_STRING)
6165 goto consider_string_end;
6166 break;
6167
6168 default:
6169 /* There are no other methods defined, so this should be a bug. */
6170 abort ();
6171 }
6172
6173 xassert (it->method != GET_FROM_STRING
6174 || (STRINGP (it->string)
6175 && IT_STRING_CHARPOS (*it) >= 0));
6176 }
6177
6178 /* Load IT's display element fields with information about the next
6179 display element which comes from a display table entry or from the
6180 result of translating a control character to one of the forms `^C'
6181 or `\003'.
6182
6183 IT->dpvec holds the glyphs to return as characters.
6184 IT->saved_face_id holds the face id before the display vector--
6185 it is restored into IT->face_idin set_iterator_to_next. */
6186
6187 static int
6188 next_element_from_display_vector (it)
6189 struct it *it;
6190 {
6191 Lisp_Object gc;
6192
6193 /* Precondition. */
6194 xassert (it->dpvec && it->current.dpvec_index >= 0);
6195
6196 it->face_id = it->saved_face_id;
6197
6198 /* KFS: This code used to check ip->dpvec[0] instead of the current element.
6199 That seemed totally bogus - so I changed it... */
6200 gc = it->dpvec[it->current.dpvec_index];
6201
6202 if (GLYPH_CODE_P (gc) && GLYPH_CODE_CHAR_VALID_P (gc))
6203 {
6204 it->c = GLYPH_CODE_CHAR (gc);
6205 it->len = CHAR_BYTES (it->c);
6206
6207 /* The entry may contain a face id to use. Such a face id is
6208 the id of a Lisp face, not a realized face. A face id of
6209 zero means no face is specified. */
6210 if (it->dpvec_face_id >= 0)
6211 it->face_id = it->dpvec_face_id;
6212 else
6213 {
6214 int lface_id = GLYPH_CODE_FACE (gc);
6215 if (lface_id > 0)
6216 it->face_id = merge_faces (it->f, Qt, lface_id,
6217 it->saved_face_id);
6218 }
6219 }
6220 else
6221 /* Display table entry is invalid. Return a space. */
6222 it->c = ' ', it->len = 1;
6223
6224 /* Don't change position and object of the iterator here. They are
6225 still the values of the character that had this display table
6226 entry or was translated, and that's what we want. */
6227 it->what = IT_CHARACTER;
6228 return 1;
6229 }
6230
6231
6232 /* Load IT with the next display element from Lisp string IT->string.
6233 IT->current.string_pos is the current position within the string.
6234 If IT->current.overlay_string_index >= 0, the Lisp string is an
6235 overlay string. */
6236
6237 static int
6238 next_element_from_string (it)
6239 struct it *it;
6240 {
6241 struct text_pos position;
6242
6243 xassert (STRINGP (it->string));
6244 xassert (IT_STRING_CHARPOS (*it) >= 0);
6245 position = it->current.string_pos;
6246
6247 /* Time to check for invisible text? */
6248 if (IT_STRING_CHARPOS (*it) < it->end_charpos
6249 && IT_STRING_CHARPOS (*it) == it->stop_charpos)
6250 {
6251 handle_stop (it);
6252
6253 /* Since a handler may have changed IT->method, we must
6254 recurse here. */
6255 return GET_NEXT_DISPLAY_ELEMENT (it);
6256 }
6257
6258 if (it->current.overlay_string_index >= 0)
6259 {
6260 /* Get the next character from an overlay string. In overlay
6261 strings, There is no field width or padding with spaces to
6262 do. */
6263 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
6264 {
6265 it->what = IT_EOB;
6266 return 0;
6267 }
6268 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
6269 IT_STRING_BYTEPOS (*it))
6270 && next_element_from_composition (it))
6271 {
6272 return 1;
6273 }
6274 else if (STRING_MULTIBYTE (it->string))
6275 {
6276 int remaining = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
6277 const unsigned char *s = (SDATA (it->string)
6278 + IT_STRING_BYTEPOS (*it));
6279 it->c = string_char_and_length (s, remaining, &it->len);
6280 }
6281 else
6282 {
6283 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
6284 it->len = 1;
6285 }
6286 }
6287 else
6288 {
6289 /* Get the next character from a Lisp string that is not an
6290 overlay string. Such strings come from the mode line, for
6291 example. We may have to pad with spaces, or truncate the
6292 string. See also next_element_from_c_string. */
6293 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
6294 {
6295 it->what = IT_EOB;
6296 return 0;
6297 }
6298 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
6299 {
6300 /* Pad with spaces. */
6301 it->c = ' ', it->len = 1;
6302 CHARPOS (position) = BYTEPOS (position) = -1;
6303 }
6304 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
6305 IT_STRING_BYTEPOS (*it))
6306 && next_element_from_composition (it))
6307 {
6308 return 1;
6309 }
6310 else if (STRING_MULTIBYTE (it->string))
6311 {
6312 int maxlen = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
6313 const unsigned char *s = (SDATA (it->string)
6314 + IT_STRING_BYTEPOS (*it));
6315 it->c = string_char_and_length (s, maxlen, &it->len);
6316 }
6317 else
6318 {
6319 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
6320 it->len = 1;
6321 }
6322 }
6323
6324 /* Record what we have and where it came from. */
6325 it->what = IT_CHARACTER;
6326 it->object = it->string;
6327 it->position = position;
6328 return 1;
6329 }
6330
6331
6332 /* Load IT with next display element from C string IT->s.
6333 IT->string_nchars is the maximum number of characters to return
6334 from the string. IT->end_charpos may be greater than
6335 IT->string_nchars when this function is called, in which case we
6336 may have to return padding spaces. Value is zero if end of string
6337 reached, including padding spaces. */
6338
6339 static int
6340 next_element_from_c_string (it)
6341 struct it *it;
6342 {
6343 int success_p = 1;
6344
6345 xassert (it->s);
6346 it->what = IT_CHARACTER;
6347 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
6348 it->object = Qnil;
6349
6350 /* IT's position can be greater IT->string_nchars in case a field
6351 width or precision has been specified when the iterator was
6352 initialized. */
6353 if (IT_CHARPOS (*it) >= it->end_charpos)
6354 {
6355 /* End of the game. */
6356 it->what = IT_EOB;
6357 success_p = 0;
6358 }
6359 else if (IT_CHARPOS (*it) >= it->string_nchars)
6360 {
6361 /* Pad with spaces. */
6362 it->c = ' ', it->len = 1;
6363 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
6364 }
6365 else if (it->multibyte_p)
6366 {
6367 /* Implementation note: The calls to strlen apparently aren't a
6368 performance problem because there is no noticeable performance
6369 difference between Emacs running in unibyte or multibyte mode. */
6370 int maxlen = strlen (it->s) - IT_BYTEPOS (*it);
6371 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it),
6372 maxlen, &it->len);
6373 }
6374 else
6375 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
6376
6377 return success_p;
6378 }
6379
6380
6381 /* Set up IT to return characters from an ellipsis, if appropriate.
6382 The definition of the ellipsis glyphs may come from a display table
6383 entry. This function Fills IT with the first glyph from the
6384 ellipsis if an ellipsis is to be displayed. */
6385
6386 static int
6387 next_element_from_ellipsis (it)
6388 struct it *it;
6389 {
6390 if (it->selective_display_ellipsis_p)
6391 setup_for_ellipsis (it, it->len);
6392 else
6393 {
6394 /* The face at the current position may be different from the
6395 face we find after the invisible text. Remember what it
6396 was in IT->saved_face_id, and signal that it's there by
6397 setting face_before_selective_p. */
6398 it->saved_face_id = it->face_id;
6399 it->method = GET_FROM_BUFFER;
6400 it->object = it->w->buffer;
6401 reseat_at_next_visible_line_start (it, 1);
6402 it->face_before_selective_p = 1;
6403 }
6404
6405 return GET_NEXT_DISPLAY_ELEMENT (it);
6406 }
6407
6408
6409 /* Deliver an image display element. The iterator IT is already
6410 filled with image information (done in handle_display_prop). Value
6411 is always 1. */
6412
6413
6414 static int
6415 next_element_from_image (it)
6416 struct it *it;
6417 {
6418 it->what = IT_IMAGE;
6419 return 1;
6420 }
6421
6422
6423 /* Fill iterator IT with next display element from a stretch glyph
6424 property. IT->object is the value of the text property. Value is
6425 always 1. */
6426
6427 static int
6428 next_element_from_stretch (it)
6429 struct it *it;
6430 {
6431 it->what = IT_STRETCH;
6432 return 1;
6433 }
6434
6435
6436 /* Load IT with the next display element from current_buffer. Value
6437 is zero if end of buffer reached. IT->stop_charpos is the next
6438 position at which to stop and check for text properties or buffer
6439 end. */
6440
6441 static int
6442 next_element_from_buffer (it)
6443 struct it *it;
6444 {
6445 int success_p = 1;
6446
6447 xassert (IT_CHARPOS (*it) >= BEGV);
6448
6449 if (IT_CHARPOS (*it) >= it->stop_charpos)
6450 {
6451 if (IT_CHARPOS (*it) >= it->end_charpos)
6452 {
6453 int overlay_strings_follow_p;
6454
6455 /* End of the game, except when overlay strings follow that
6456 haven't been returned yet. */
6457 if (it->overlay_strings_at_end_processed_p)
6458 overlay_strings_follow_p = 0;
6459 else
6460 {
6461 it->overlay_strings_at_end_processed_p = 1;
6462 overlay_strings_follow_p = get_overlay_strings (it, 0);
6463 }
6464
6465 if (overlay_strings_follow_p)
6466 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
6467 else
6468 {
6469 it->what = IT_EOB;
6470 it->position = it->current.pos;
6471 success_p = 0;
6472 }
6473 }
6474 else
6475 {
6476 handle_stop (it);
6477 return GET_NEXT_DISPLAY_ELEMENT (it);
6478 }
6479 }
6480 else
6481 {
6482 /* No face changes, overlays etc. in sight, so just return a
6483 character from current_buffer. */
6484 unsigned char *p;
6485
6486 /* Maybe run the redisplay end trigger hook. Performance note:
6487 This doesn't seem to cost measurable time. */
6488 if (it->redisplay_end_trigger_charpos
6489 && it->glyph_row
6490 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
6491 run_redisplay_end_trigger_hook (it);
6492
6493 if (CHAR_COMPOSED_P (it, IT_CHARPOS (*it), IT_BYTEPOS (*it))
6494 && next_element_from_composition (it))
6495 {
6496 return 1;
6497 }
6498
6499 /* Get the next character, maybe multibyte. */
6500 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
6501 if (it->multibyte_p && !ASCII_BYTE_P (*p))
6502 it->c = STRING_CHAR_AND_LENGTH (p, 0, it->len);
6503 else
6504 it->c = *p, it->len = 1;
6505
6506 /* Record what we have and where it came from. */
6507 it->what = IT_CHARACTER;
6508 it->object = it->w->buffer;
6509 it->position = it->current.pos;
6510
6511 /* Normally we return the character found above, except when we
6512 really want to return an ellipsis for selective display. */
6513 if (it->selective)
6514 {
6515 if (it->c == '\n')
6516 {
6517 /* A value of selective > 0 means hide lines indented more
6518 than that number of columns. */
6519 if (it->selective > 0
6520 && IT_CHARPOS (*it) + 1 < ZV
6521 && indented_beyond_p (IT_CHARPOS (*it) + 1,
6522 IT_BYTEPOS (*it) + 1,
6523 (double) it->selective)) /* iftc */
6524 {
6525 success_p = next_element_from_ellipsis (it);
6526 it->dpvec_char_len = -1;
6527 }
6528 }
6529 else if (it->c == '\r' && it->selective == -1)
6530 {
6531 /* A value of selective == -1 means that everything from the
6532 CR to the end of the line is invisible, with maybe an
6533 ellipsis displayed for it. */
6534 success_p = next_element_from_ellipsis (it);
6535 it->dpvec_char_len = -1;
6536 }
6537 }
6538 }
6539
6540 /* Value is zero if end of buffer reached. */
6541 xassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
6542 return success_p;
6543 }
6544
6545
6546 /* Run the redisplay end trigger hook for IT. */
6547
6548 static void
6549 run_redisplay_end_trigger_hook (it)
6550 struct it *it;
6551 {
6552 Lisp_Object args[3];
6553
6554 /* IT->glyph_row should be non-null, i.e. we should be actually
6555 displaying something, or otherwise we should not run the hook. */
6556 xassert (it->glyph_row);
6557
6558 /* Set up hook arguments. */
6559 args[0] = Qredisplay_end_trigger_functions;
6560 args[1] = it->window;
6561 XSETINT (args[2], it->redisplay_end_trigger_charpos);
6562 it->redisplay_end_trigger_charpos = 0;
6563
6564 /* Since we are *trying* to run these functions, don't try to run
6565 them again, even if they get an error. */
6566 it->w->redisplay_end_trigger = Qnil;
6567 Frun_hook_with_args (3, args);
6568
6569 /* Notice if it changed the face of the character we are on. */
6570 handle_face_prop (it);
6571 }
6572
6573
6574 /* Deliver a composition display element. Unlike the other
6575 next_element_from_XXX, this function is not registered in the array
6576 get_next_element[]. It is called from next_element_from_buffer and
6577 next_element_from_string when necessary. */
6578
6579 static int
6580 next_element_from_composition (it)
6581 struct it *it;
6582 {
6583 it->what = IT_COMPOSITION;
6584 it->len = it->cmp_it.nbytes;
6585 if (STRINGP (it->string))
6586 {
6587 if (it->c < 0)
6588 {
6589 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
6590 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
6591 return 0;
6592 }
6593 it->position = it->current.string_pos;
6594 it->object = it->string;
6595 it->c = composition_update_it (&it->cmp_it, IT_STRING_CHARPOS (*it),
6596 IT_STRING_BYTEPOS (*it), it->string);
6597 }
6598 else
6599 {
6600 if (it->c < 0)
6601 {
6602 IT_CHARPOS (*it) += it->cmp_it.nchars;
6603 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
6604 return 0;
6605 }
6606 it->position = it->current.pos;
6607 it->object = it->w->buffer;
6608 it->c = composition_update_it (&it->cmp_it, IT_CHARPOS (*it),
6609 IT_BYTEPOS (*it), Qnil);
6610 }
6611 return 1;
6612 }
6613
6614
6615 \f
6616 /***********************************************************************
6617 Moving an iterator without producing glyphs
6618 ***********************************************************************/
6619
6620 /* Check if iterator is at a position corresponding to a valid buffer
6621 position after some move_it_ call. */
6622
6623 #define IT_POS_VALID_AFTER_MOVE_P(it) \
6624 ((it)->method == GET_FROM_STRING \
6625 ? IT_STRING_CHARPOS (*it) == 0 \
6626 : 1)
6627
6628
6629 /* Move iterator IT to a specified buffer or X position within one
6630 line on the display without producing glyphs.
6631
6632 OP should be a bit mask including some or all of these bits:
6633 MOVE_TO_X: Stop on reaching x-position TO_X.
6634 MOVE_TO_POS: Stop on reaching buffer or string position TO_CHARPOS.
6635 Regardless of OP's value, stop in reaching the end of the display line.
6636
6637 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
6638 This means, in particular, that TO_X includes window's horizontal
6639 scroll amount.
6640
6641 The return value has several possible values that
6642 say what condition caused the scan to stop:
6643
6644 MOVE_POS_MATCH_OR_ZV
6645 - when TO_POS or ZV was reached.
6646
6647 MOVE_X_REACHED
6648 -when TO_X was reached before TO_POS or ZV were reached.
6649
6650 MOVE_LINE_CONTINUED
6651 - when we reached the end of the display area and the line must
6652 be continued.
6653
6654 MOVE_LINE_TRUNCATED
6655 - when we reached the end of the display area and the line is
6656 truncated.
6657
6658 MOVE_NEWLINE_OR_CR
6659 - when we stopped at a line end, i.e. a newline or a CR and selective
6660 display is on. */
6661
6662 static enum move_it_result
6663 move_it_in_display_line_to (struct it *it,
6664 EMACS_INT to_charpos, int to_x,
6665 enum move_operation_enum op)
6666 {
6667 enum move_it_result result = MOVE_UNDEFINED;
6668 struct glyph_row *saved_glyph_row;
6669 struct it wrap_it, atpos_it, atx_it;
6670 int may_wrap = 0;
6671
6672 /* Don't produce glyphs in produce_glyphs. */
6673 saved_glyph_row = it->glyph_row;
6674 it->glyph_row = NULL;
6675
6676 /* Use wrap_it to save a copy of IT wherever a word wrap could
6677 occur. Use atpos_it to save a copy of IT at the desired buffer
6678 position, if found, so that we can scan ahead and check if the
6679 word later overshoots the window edge. Use atx_it similarly, for
6680 pixel positions. */
6681 wrap_it.sp = -1;
6682 atpos_it.sp = -1;
6683 atx_it.sp = -1;
6684
6685 #define BUFFER_POS_REACHED_P() \
6686 ((op & MOVE_TO_POS) != 0 \
6687 && BUFFERP (it->object) \
6688 && IT_CHARPOS (*it) >= to_charpos \
6689 && (it->method == GET_FROM_BUFFER \
6690 || (it->method == GET_FROM_DISPLAY_VECTOR \
6691 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
6692
6693 /* If there's a line-/wrap-prefix, handle it. */
6694 if (it->hpos == 0 && it->method == GET_FROM_BUFFER
6695 && it->current_y < it->last_visible_y)
6696 handle_line_prefix (it);
6697
6698 while (1)
6699 {
6700 int x, i, ascent = 0, descent = 0;
6701
6702 /* Utility macro to reset an iterator with x, ascent, and descent. */
6703 #define IT_RESET_X_ASCENT_DESCENT(IT) \
6704 ((IT)->current_x = x, (IT)->max_ascent = ascent, \
6705 (IT)->max_descent = descent)
6706
6707 /* Stop if we move beyond TO_CHARPOS (after an image or stretch
6708 glyph). */
6709 if ((op & MOVE_TO_POS) != 0
6710 && BUFFERP (it->object)
6711 && it->method == GET_FROM_BUFFER
6712 && IT_CHARPOS (*it) > to_charpos)
6713 {
6714 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
6715 {
6716 result = MOVE_POS_MATCH_OR_ZV;
6717 break;
6718 }
6719 else if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
6720 /* If wrap_it is valid, the current position might be in a
6721 word that is wrapped. So, save the iterator in
6722 atpos_it and continue to see if wrapping happens. */
6723 atpos_it = *it;
6724 }
6725
6726 /* Stop when ZV reached.
6727 We used to stop here when TO_CHARPOS reached as well, but that is
6728 too soon if this glyph does not fit on this line. So we handle it
6729 explicitly below. */
6730 if (!get_next_display_element (it))
6731 {
6732 result = MOVE_POS_MATCH_OR_ZV;
6733 break;
6734 }
6735
6736 if (it->line_wrap == TRUNCATE)
6737 {
6738 if (BUFFER_POS_REACHED_P ())
6739 {
6740 result = MOVE_POS_MATCH_OR_ZV;
6741 break;
6742 }
6743 }
6744 else
6745 {
6746 if (it->line_wrap == WORD_WRAP)
6747 {
6748 if (IT_DISPLAYING_WHITESPACE (it))
6749 may_wrap = 1;
6750 else if (may_wrap)
6751 {
6752 /* We have reached a glyph that follows one or more
6753 whitespace characters. If the position is
6754 already found, we are done. */
6755 if (atpos_it.sp >= 0)
6756 {
6757 *it = atpos_it;
6758 result = MOVE_POS_MATCH_OR_ZV;
6759 goto done;
6760 }
6761 if (atx_it.sp >= 0)
6762 {
6763 *it = atx_it;
6764 result = MOVE_X_REACHED;
6765 goto done;
6766 }
6767 /* Otherwise, we can wrap here. */
6768 wrap_it = *it;
6769 may_wrap = 0;
6770 }
6771 }
6772 }
6773
6774 /* Remember the line height for the current line, in case
6775 the next element doesn't fit on the line. */
6776 ascent = it->max_ascent;
6777 descent = it->max_descent;
6778
6779 /* The call to produce_glyphs will get the metrics of the
6780 display element IT is loaded with. Record the x-position
6781 before this display element, in case it doesn't fit on the
6782 line. */
6783 x = it->current_x;
6784
6785 PRODUCE_GLYPHS (it);
6786
6787 if (it->area != TEXT_AREA)
6788 {
6789 set_iterator_to_next (it, 1);
6790 continue;
6791 }
6792
6793 /* The number of glyphs we get back in IT->nglyphs will normally
6794 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
6795 character on a terminal frame, or (iii) a line end. For the
6796 second case, IT->nglyphs - 1 padding glyphs will be present
6797 (on X frames, there is only one glyph produced for a
6798 composite character.
6799
6800 The behavior implemented below means, for continuation lines,
6801 that as many spaces of a TAB as fit on the current line are
6802 displayed there. For terminal frames, as many glyphs of a
6803 multi-glyph character are displayed in the current line, too.
6804 This is what the old redisplay code did, and we keep it that
6805 way. Under X, the whole shape of a complex character must
6806 fit on the line or it will be completely displayed in the
6807 next line.
6808
6809 Note that both for tabs and padding glyphs, all glyphs have
6810 the same width. */
6811 if (it->nglyphs)
6812 {
6813 /* More than one glyph or glyph doesn't fit on line. All
6814 glyphs have the same width. */
6815 int single_glyph_width = it->pixel_width / it->nglyphs;
6816 int new_x;
6817 int x_before_this_char = x;
6818 int hpos_before_this_char = it->hpos;
6819
6820 for (i = 0; i < it->nglyphs; ++i, x = new_x)
6821 {
6822 new_x = x + single_glyph_width;
6823
6824 /* We want to leave anything reaching TO_X to the caller. */
6825 if ((op & MOVE_TO_X) && new_x > to_x)
6826 {
6827 if (BUFFER_POS_REACHED_P ())
6828 {
6829 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
6830 goto buffer_pos_reached;
6831 if (atpos_it.sp < 0)
6832 {
6833 atpos_it = *it;
6834 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
6835 }
6836 }
6837 else
6838 {
6839 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
6840 {
6841 it->current_x = x;
6842 result = MOVE_X_REACHED;
6843 break;
6844 }
6845 if (atx_it.sp < 0)
6846 {
6847 atx_it = *it;
6848 IT_RESET_X_ASCENT_DESCENT (&atx_it);
6849 }
6850 }
6851 }
6852
6853 if (/* Lines are continued. */
6854 it->line_wrap != TRUNCATE
6855 && (/* And glyph doesn't fit on the line. */
6856 new_x > it->last_visible_x
6857 /* Or it fits exactly and we're on a window
6858 system frame. */
6859 || (new_x == it->last_visible_x
6860 && FRAME_WINDOW_P (it->f))))
6861 {
6862 if (/* IT->hpos == 0 means the very first glyph
6863 doesn't fit on the line, e.g. a wide image. */
6864 it->hpos == 0
6865 || (new_x == it->last_visible_x
6866 && FRAME_WINDOW_P (it->f)))
6867 {
6868 ++it->hpos;
6869 it->current_x = new_x;
6870
6871 /* The character's last glyph just barely fits
6872 in this row. */
6873 if (i == it->nglyphs - 1)
6874 {
6875 /* If this is the destination position,
6876 return a position *before* it in this row,
6877 now that we know it fits in this row. */
6878 if (BUFFER_POS_REACHED_P ())
6879 {
6880 if (it->line_wrap != WORD_WRAP
6881 || wrap_it.sp < 0)
6882 {
6883 it->hpos = hpos_before_this_char;
6884 it->current_x = x_before_this_char;
6885 result = MOVE_POS_MATCH_OR_ZV;
6886 break;
6887 }
6888 if (it->line_wrap == WORD_WRAP
6889 && atpos_it.sp < 0)
6890 {
6891 atpos_it = *it;
6892 atpos_it.current_x = x_before_this_char;
6893 atpos_it.hpos = hpos_before_this_char;
6894 }
6895 }
6896
6897 set_iterator_to_next (it, 1);
6898 #ifdef HAVE_WINDOW_SYSTEM
6899 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
6900 {
6901 if (!get_next_display_element (it))
6902 {
6903 result = MOVE_POS_MATCH_OR_ZV;
6904 break;
6905 }
6906 if (BUFFER_POS_REACHED_P ())
6907 {
6908 if (ITERATOR_AT_END_OF_LINE_P (it))
6909 result = MOVE_POS_MATCH_OR_ZV;
6910 else
6911 result = MOVE_LINE_CONTINUED;
6912 break;
6913 }
6914 if (ITERATOR_AT_END_OF_LINE_P (it))
6915 {
6916 result = MOVE_NEWLINE_OR_CR;
6917 break;
6918 }
6919 }
6920 #endif /* HAVE_WINDOW_SYSTEM */
6921 }
6922 }
6923 else
6924 IT_RESET_X_ASCENT_DESCENT (it);
6925
6926 if (wrap_it.sp >= 0)
6927 {
6928 *it = wrap_it;
6929 atpos_it.sp = -1;
6930 atx_it.sp = -1;
6931 }
6932
6933 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
6934 IT_CHARPOS (*it)));
6935 result = MOVE_LINE_CONTINUED;
6936 break;
6937 }
6938
6939 if (BUFFER_POS_REACHED_P ())
6940 {
6941 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
6942 goto buffer_pos_reached;
6943 if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
6944 {
6945 atpos_it = *it;
6946 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
6947 }
6948 }
6949
6950 if (new_x > it->first_visible_x)
6951 {
6952 /* Glyph is visible. Increment number of glyphs that
6953 would be displayed. */
6954 ++it->hpos;
6955 }
6956 }
6957
6958 if (result != MOVE_UNDEFINED)
6959 break;
6960 }
6961 else if (BUFFER_POS_REACHED_P ())
6962 {
6963 buffer_pos_reached:
6964 IT_RESET_X_ASCENT_DESCENT (it);
6965 result = MOVE_POS_MATCH_OR_ZV;
6966 break;
6967 }
6968 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
6969 {
6970 /* Stop when TO_X specified and reached. This check is
6971 necessary here because of lines consisting of a line end,
6972 only. The line end will not produce any glyphs and we
6973 would never get MOVE_X_REACHED. */
6974 xassert (it->nglyphs == 0);
6975 result = MOVE_X_REACHED;
6976 break;
6977 }
6978
6979 /* Is this a line end? If yes, we're done. */
6980 if (ITERATOR_AT_END_OF_LINE_P (it))
6981 {
6982 result = MOVE_NEWLINE_OR_CR;
6983 break;
6984 }
6985
6986 /* The current display element has been consumed. Advance
6987 to the next. */
6988 set_iterator_to_next (it, 1);
6989
6990 /* Stop if lines are truncated and IT's current x-position is
6991 past the right edge of the window now. */
6992 if (it->line_wrap == TRUNCATE
6993 && it->current_x >= it->last_visible_x)
6994 {
6995 #ifdef HAVE_WINDOW_SYSTEM
6996 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
6997 {
6998 if (!get_next_display_element (it)
6999 || BUFFER_POS_REACHED_P ())
7000 {
7001 result = MOVE_POS_MATCH_OR_ZV;
7002 break;
7003 }
7004 if (ITERATOR_AT_END_OF_LINE_P (it))
7005 {
7006 result = MOVE_NEWLINE_OR_CR;
7007 break;
7008 }
7009 }
7010 #endif /* HAVE_WINDOW_SYSTEM */
7011 result = MOVE_LINE_TRUNCATED;
7012 break;
7013 }
7014 #undef IT_RESET_X_ASCENT_DESCENT
7015 }
7016
7017 #undef BUFFER_POS_REACHED_P
7018
7019 /* If we scanned beyond to_pos and didn't find a point to wrap at,
7020 restore the saved iterator. */
7021 if (atpos_it.sp >= 0)
7022 *it = atpos_it;
7023 else if (atx_it.sp >= 0)
7024 *it = atx_it;
7025
7026 done:
7027
7028 /* Restore the iterator settings altered at the beginning of this
7029 function. */
7030 it->glyph_row = saved_glyph_row;
7031 return result;
7032 }
7033
7034 /* For external use. */
7035 void
7036 move_it_in_display_line (struct it *it,
7037 EMACS_INT to_charpos, int to_x,
7038 enum move_operation_enum op)
7039 {
7040 if (it->line_wrap == WORD_WRAP
7041 && (op & MOVE_TO_X))
7042 {
7043 struct it save_it = *it;
7044 int skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
7045 /* When word-wrap is on, TO_X may lie past the end
7046 of a wrapped line. Then it->current is the
7047 character on the next line, so backtrack to the
7048 space before the wrap point. */
7049 if (skip == MOVE_LINE_CONTINUED)
7050 {
7051 int prev_x = max (it->current_x - 1, 0);
7052 *it = save_it;
7053 move_it_in_display_line_to
7054 (it, -1, prev_x, MOVE_TO_X);
7055 }
7056 }
7057 else
7058 move_it_in_display_line_to (it, to_charpos, to_x, op);
7059 }
7060
7061
7062 /* Move IT forward until it satisfies one or more of the criteria in
7063 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
7064
7065 OP is a bit-mask that specifies where to stop, and in particular,
7066 which of those four position arguments makes a difference. See the
7067 description of enum move_operation_enum.
7068
7069 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
7070 screen line, this function will set IT to the next position >
7071 TO_CHARPOS. */
7072
7073 void
7074 move_it_to (it, to_charpos, to_x, to_y, to_vpos, op)
7075 struct it *it;
7076 int to_charpos, to_x, to_y, to_vpos;
7077 int op;
7078 {
7079 enum move_it_result skip, skip2 = MOVE_X_REACHED;
7080 int line_height;
7081 int reached = 0;
7082
7083 for (;;)
7084 {
7085 if (op & MOVE_TO_VPOS)
7086 {
7087 /* If no TO_CHARPOS and no TO_X specified, stop at the
7088 start of the line TO_VPOS. */
7089 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
7090 {
7091 if (it->vpos == to_vpos)
7092 {
7093 reached = 1;
7094 break;
7095 }
7096 else
7097 skip = move_it_in_display_line_to (it, -1, -1, 0);
7098 }
7099 else
7100 {
7101 /* TO_VPOS >= 0 means stop at TO_X in the line at
7102 TO_VPOS, or at TO_POS, whichever comes first. */
7103 if (it->vpos == to_vpos)
7104 {
7105 reached = 2;
7106 break;
7107 }
7108
7109 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
7110
7111 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
7112 {
7113 reached = 3;
7114 break;
7115 }
7116 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
7117 {
7118 /* We have reached TO_X but not in the line we want. */
7119 skip = move_it_in_display_line_to (it, to_charpos,
7120 -1, MOVE_TO_POS);
7121 if (skip == MOVE_POS_MATCH_OR_ZV)
7122 {
7123 reached = 4;
7124 break;
7125 }
7126 }
7127 }
7128 }
7129 else if (op & MOVE_TO_Y)
7130 {
7131 struct it it_backup;
7132
7133 if (it->line_wrap == WORD_WRAP)
7134 it_backup = *it;
7135
7136 /* TO_Y specified means stop at TO_X in the line containing
7137 TO_Y---or at TO_CHARPOS if this is reached first. The
7138 problem is that we can't really tell whether the line
7139 contains TO_Y before we have completely scanned it, and
7140 this may skip past TO_X. What we do is to first scan to
7141 TO_X.
7142
7143 If TO_X is not specified, use a TO_X of zero. The reason
7144 is to make the outcome of this function more predictable.
7145 If we didn't use TO_X == 0, we would stop at the end of
7146 the line which is probably not what a caller would expect
7147 to happen. */
7148 skip = move_it_in_display_line_to
7149 (it, to_charpos, ((op & MOVE_TO_X) ? to_x : 0),
7150 (MOVE_TO_X | (op & MOVE_TO_POS)));
7151
7152 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
7153 if (skip == MOVE_POS_MATCH_OR_ZV)
7154 reached = 5;
7155 else if (skip == MOVE_X_REACHED)
7156 {
7157 /* If TO_X was reached, we want to know whether TO_Y is
7158 in the line. We know this is the case if the already
7159 scanned glyphs make the line tall enough. Otherwise,
7160 we must check by scanning the rest of the line. */
7161 line_height = it->max_ascent + it->max_descent;
7162 if (to_y >= it->current_y
7163 && to_y < it->current_y + line_height)
7164 {
7165 reached = 6;
7166 break;
7167 }
7168 it_backup = *it;
7169 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
7170 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
7171 op & MOVE_TO_POS);
7172 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
7173 line_height = it->max_ascent + it->max_descent;
7174 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
7175
7176 if (to_y >= it->current_y
7177 && to_y < it->current_y + line_height)
7178 {
7179 /* If TO_Y is in this line and TO_X was reached
7180 above, we scanned too far. We have to restore
7181 IT's settings to the ones before skipping. */
7182 *it = it_backup;
7183 reached = 6;
7184 }
7185 else
7186 {
7187 skip = skip2;
7188 if (skip == MOVE_POS_MATCH_OR_ZV)
7189 reached = 7;
7190 }
7191 }
7192 else
7193 {
7194 /* Check whether TO_Y is in this line. */
7195 line_height = it->max_ascent + it->max_descent;
7196 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
7197
7198 if (to_y >= it->current_y
7199 && to_y < it->current_y + line_height)
7200 {
7201 /* When word-wrap is on, TO_X may lie past the end
7202 of a wrapped line. Then it->current is the
7203 character on the next line, so backtrack to the
7204 space before the wrap point. */
7205 if (skip == MOVE_LINE_CONTINUED
7206 && it->line_wrap == WORD_WRAP)
7207 {
7208 int prev_x = max (it->current_x - 1, 0);
7209 *it = it_backup;
7210 skip = move_it_in_display_line_to
7211 (it, -1, prev_x, MOVE_TO_X);
7212 }
7213 reached = 6;
7214 }
7215 }
7216
7217 if (reached)
7218 break;
7219 }
7220 else if (BUFFERP (it->object)
7221 && it->method == GET_FROM_BUFFER
7222 && IT_CHARPOS (*it) >= to_charpos)
7223 skip = MOVE_POS_MATCH_OR_ZV;
7224 else
7225 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
7226
7227 switch (skip)
7228 {
7229 case MOVE_POS_MATCH_OR_ZV:
7230 reached = 8;
7231 goto out;
7232
7233 case MOVE_NEWLINE_OR_CR:
7234 set_iterator_to_next (it, 1);
7235 it->continuation_lines_width = 0;
7236 break;
7237
7238 case MOVE_LINE_TRUNCATED:
7239 it->continuation_lines_width = 0;
7240 reseat_at_next_visible_line_start (it, 0);
7241 if ((op & MOVE_TO_POS) != 0
7242 && IT_CHARPOS (*it) > to_charpos)
7243 {
7244 reached = 9;
7245 goto out;
7246 }
7247 break;
7248
7249 case MOVE_LINE_CONTINUED:
7250 /* For continued lines ending in a tab, some of the glyphs
7251 associated with the tab are displayed on the current
7252 line. Since it->current_x does not include these glyphs,
7253 we use it->last_visible_x instead. */
7254 if (it->c == '\t')
7255 {
7256 it->continuation_lines_width += it->last_visible_x;
7257 /* When moving by vpos, ensure that the iterator really
7258 advances to the next line (bug#847, bug#969). Fixme:
7259 do we need to do this in other circumstances? */
7260 if (it->current_x != it->last_visible_x
7261 && (op & MOVE_TO_VPOS)
7262 && !(op & (MOVE_TO_X | MOVE_TO_POS)))
7263 set_iterator_to_next (it, 0);
7264 }
7265 else
7266 it->continuation_lines_width += it->current_x;
7267 break;
7268
7269 default:
7270 abort ();
7271 }
7272
7273 /* Reset/increment for the next run. */
7274 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
7275 it->current_x = it->hpos = 0;
7276 it->current_y += it->max_ascent + it->max_descent;
7277 ++it->vpos;
7278 last_height = it->max_ascent + it->max_descent;
7279 last_max_ascent = it->max_ascent;
7280 it->max_ascent = it->max_descent = 0;
7281 }
7282
7283 out:
7284
7285 /* On text terminals, we may stop at the end of a line in the middle
7286 of a multi-character glyph. If the glyph itself is continued,
7287 i.e. it is actually displayed on the next line, don't treat this
7288 stopping point as valid; move to the next line instead (unless
7289 that brings us offscreen). */
7290 if (!FRAME_WINDOW_P (it->f)
7291 && op & MOVE_TO_POS
7292 && IT_CHARPOS (*it) == to_charpos
7293 && it->what == IT_CHARACTER
7294 && it->nglyphs > 1
7295 && it->line_wrap == WINDOW_WRAP
7296 && it->current_x == it->last_visible_x - 1
7297 && it->c != '\n'
7298 && it->c != '\t'
7299 && it->vpos < XFASTINT (it->w->window_end_vpos))
7300 {
7301 it->continuation_lines_width += it->current_x;
7302 it->current_x = it->hpos = it->max_ascent = it->max_descent = 0;
7303 it->current_y += it->max_ascent + it->max_descent;
7304 ++it->vpos;
7305 last_height = it->max_ascent + it->max_descent;
7306 last_max_ascent = it->max_ascent;
7307 }
7308
7309 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
7310 }
7311
7312
7313 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
7314
7315 If DY > 0, move IT backward at least that many pixels. DY = 0
7316 means move IT backward to the preceding line start or BEGV. This
7317 function may move over more than DY pixels if IT->current_y - DY
7318 ends up in the middle of a line; in this case IT->current_y will be
7319 set to the top of the line moved to. */
7320
7321 void
7322 move_it_vertically_backward (it, dy)
7323 struct it *it;
7324 int dy;
7325 {
7326 int nlines, h;
7327 struct it it2, it3;
7328 int start_pos;
7329
7330 move_further_back:
7331 xassert (dy >= 0);
7332
7333 start_pos = IT_CHARPOS (*it);
7334
7335 /* Estimate how many newlines we must move back. */
7336 nlines = max (1, dy / FRAME_LINE_HEIGHT (it->f));
7337
7338 /* Set the iterator's position that many lines back. */
7339 while (nlines-- && IT_CHARPOS (*it) > BEGV)
7340 back_to_previous_visible_line_start (it);
7341
7342 /* Reseat the iterator here. When moving backward, we don't want
7343 reseat to skip forward over invisible text, set up the iterator
7344 to deliver from overlay strings at the new position etc. So,
7345 use reseat_1 here. */
7346 reseat_1 (it, it->current.pos, 1);
7347
7348 /* We are now surely at a line start. */
7349 it->current_x = it->hpos = 0;
7350 it->continuation_lines_width = 0;
7351
7352 /* Move forward and see what y-distance we moved. First move to the
7353 start of the next line so that we get its height. We need this
7354 height to be able to tell whether we reached the specified
7355 y-distance. */
7356 it2 = *it;
7357 it2.max_ascent = it2.max_descent = 0;
7358 do
7359 {
7360 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
7361 MOVE_TO_POS | MOVE_TO_VPOS);
7362 }
7363 while (!IT_POS_VALID_AFTER_MOVE_P (&it2));
7364 xassert (IT_CHARPOS (*it) >= BEGV);
7365 it3 = it2;
7366
7367 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
7368 xassert (IT_CHARPOS (*it) >= BEGV);
7369 /* H is the actual vertical distance from the position in *IT
7370 and the starting position. */
7371 h = it2.current_y - it->current_y;
7372 /* NLINES is the distance in number of lines. */
7373 nlines = it2.vpos - it->vpos;
7374
7375 /* Correct IT's y and vpos position
7376 so that they are relative to the starting point. */
7377 it->vpos -= nlines;
7378 it->current_y -= h;
7379
7380 if (dy == 0)
7381 {
7382 /* DY == 0 means move to the start of the screen line. The
7383 value of nlines is > 0 if continuation lines were involved. */
7384 if (nlines > 0)
7385 move_it_by_lines (it, nlines, 1);
7386 #if 0
7387 /* I think this assert is bogus if buffer contains
7388 invisible text or images. KFS. */
7389 xassert (IT_CHARPOS (*it) <= start_pos);
7390 #endif
7391 }
7392 else
7393 {
7394 /* The y-position we try to reach, relative to *IT.
7395 Note that H has been subtracted in front of the if-statement. */
7396 int target_y = it->current_y + h - dy;
7397 int y0 = it3.current_y;
7398 int y1 = line_bottom_y (&it3);
7399 int line_height = y1 - y0;
7400
7401 /* If we did not reach target_y, try to move further backward if
7402 we can. If we moved too far backward, try to move forward. */
7403 if (target_y < it->current_y
7404 /* This is heuristic. In a window that's 3 lines high, with
7405 a line height of 13 pixels each, recentering with point
7406 on the bottom line will try to move -39/2 = 19 pixels
7407 backward. Try to avoid moving into the first line. */
7408 && (it->current_y - target_y
7409 > min (window_box_height (it->w), line_height * 2 / 3))
7410 && IT_CHARPOS (*it) > BEGV)
7411 {
7412 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
7413 target_y - it->current_y));
7414 dy = it->current_y - target_y;
7415 goto move_further_back;
7416 }
7417 else if (target_y >= it->current_y + line_height
7418 && IT_CHARPOS (*it) < ZV)
7419 {
7420 /* Should move forward by at least one line, maybe more.
7421
7422 Note: Calling move_it_by_lines can be expensive on
7423 terminal frames, where compute_motion is used (via
7424 vmotion) to do the job, when there are very long lines
7425 and truncate-lines is nil. That's the reason for
7426 treating terminal frames specially here. */
7427
7428 if (!FRAME_WINDOW_P (it->f))
7429 move_it_vertically (it, target_y - (it->current_y + line_height));
7430 else
7431 {
7432 do
7433 {
7434 move_it_by_lines (it, 1, 1);
7435 }
7436 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
7437 }
7438
7439 #if 0
7440 /* I think this assert is bogus if buffer contains
7441 invisible text or images. KFS. */
7442 xassert (IT_CHARPOS (*it) >= BEGV);
7443 #endif
7444 }
7445 }
7446 }
7447
7448
7449 /* Move IT by a specified amount of pixel lines DY. DY negative means
7450 move backwards. DY = 0 means move to start of screen line. At the
7451 end, IT will be on the start of a screen line. */
7452
7453 void
7454 move_it_vertically (it, dy)
7455 struct it *it;
7456 int dy;
7457 {
7458 if (dy <= 0)
7459 move_it_vertically_backward (it, -dy);
7460 else
7461 {
7462 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
7463 move_it_to (it, ZV, -1, it->current_y + dy, -1,
7464 MOVE_TO_POS | MOVE_TO_Y);
7465 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
7466
7467 /* If buffer ends in ZV without a newline, move to the start of
7468 the line to satisfy the post-condition. */
7469 if (IT_CHARPOS (*it) == ZV
7470 && ZV > BEGV
7471 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
7472 move_it_by_lines (it, 0, 0);
7473 }
7474 }
7475
7476
7477 /* Move iterator IT past the end of the text line it is in. */
7478
7479 void
7480 move_it_past_eol (it)
7481 struct it *it;
7482 {
7483 enum move_it_result rc;
7484
7485 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
7486 if (rc == MOVE_NEWLINE_OR_CR)
7487 set_iterator_to_next (it, 0);
7488 }
7489
7490
7491 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
7492 negative means move up. DVPOS == 0 means move to the start of the
7493 screen line. NEED_Y_P non-zero means calculate IT->current_y. If
7494 NEED_Y_P is zero, IT->current_y will be left unchanged.
7495
7496 Further optimization ideas: If we would know that IT->f doesn't use
7497 a face with proportional font, we could be faster for
7498 truncate-lines nil. */
7499
7500 void
7501 move_it_by_lines (it, dvpos, need_y_p)
7502 struct it *it;
7503 int dvpos, need_y_p;
7504 {
7505 struct position pos;
7506
7507 /* The commented-out optimization uses vmotion on terminals. This
7508 gives bad results, because elements like it->what, on which
7509 callers such as pos_visible_p rely, aren't updated. */
7510 /* if (!FRAME_WINDOW_P (it->f))
7511 {
7512 struct text_pos textpos;
7513
7514 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
7515 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
7516 reseat (it, textpos, 1);
7517 it->vpos += pos.vpos;
7518 it->current_y += pos.vpos;
7519 }
7520 else */
7521
7522 if (dvpos == 0)
7523 {
7524 /* DVPOS == 0 means move to the start of the screen line. */
7525 move_it_vertically_backward (it, 0);
7526 xassert (it->current_x == 0 && it->hpos == 0);
7527 /* Let next call to line_bottom_y calculate real line height */
7528 last_height = 0;
7529 }
7530 else if (dvpos > 0)
7531 {
7532 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
7533 if (!IT_POS_VALID_AFTER_MOVE_P (it))
7534 move_it_to (it, IT_CHARPOS (*it) + 1, -1, -1, -1, MOVE_TO_POS);
7535 }
7536 else
7537 {
7538 struct it it2;
7539 int start_charpos, i;
7540
7541 /* Start at the beginning of the screen line containing IT's
7542 position. This may actually move vertically backwards,
7543 in case of overlays, so adjust dvpos accordingly. */
7544 dvpos += it->vpos;
7545 move_it_vertically_backward (it, 0);
7546 dvpos -= it->vpos;
7547
7548 /* Go back -DVPOS visible lines and reseat the iterator there. */
7549 start_charpos = IT_CHARPOS (*it);
7550 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > BEGV; --i)
7551 back_to_previous_visible_line_start (it);
7552 reseat (it, it->current.pos, 1);
7553
7554 /* Move further back if we end up in a string or an image. */
7555 while (!IT_POS_VALID_AFTER_MOVE_P (it))
7556 {
7557 /* First try to move to start of display line. */
7558 dvpos += it->vpos;
7559 move_it_vertically_backward (it, 0);
7560 dvpos -= it->vpos;
7561 if (IT_POS_VALID_AFTER_MOVE_P (it))
7562 break;
7563 /* If start of line is still in string or image,
7564 move further back. */
7565 back_to_previous_visible_line_start (it);
7566 reseat (it, it->current.pos, 1);
7567 dvpos--;
7568 }
7569
7570 it->current_x = it->hpos = 0;
7571
7572 /* Above call may have moved too far if continuation lines
7573 are involved. Scan forward and see if it did. */
7574 it2 = *it;
7575 it2.vpos = it2.current_y = 0;
7576 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
7577 it->vpos -= it2.vpos;
7578 it->current_y -= it2.current_y;
7579 it->current_x = it->hpos = 0;
7580
7581 /* If we moved too far back, move IT some lines forward. */
7582 if (it2.vpos > -dvpos)
7583 {
7584 int delta = it2.vpos + dvpos;
7585 it2 = *it;
7586 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
7587 /* Move back again if we got too far ahead. */
7588 if (IT_CHARPOS (*it) >= start_charpos)
7589 *it = it2;
7590 }
7591 }
7592 }
7593
7594 /* Return 1 if IT points into the middle of a display vector. */
7595
7596 int
7597 in_display_vector_p (it)
7598 struct it *it;
7599 {
7600 return (it->method == GET_FROM_DISPLAY_VECTOR
7601 && it->current.dpvec_index > 0
7602 && it->dpvec + it->current.dpvec_index != it->dpend);
7603 }
7604
7605 \f
7606 /***********************************************************************
7607 Messages
7608 ***********************************************************************/
7609
7610
7611 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
7612 to *Messages*. */
7613
7614 void
7615 add_to_log (format, arg1, arg2)
7616 char *format;
7617 Lisp_Object arg1, arg2;
7618 {
7619 Lisp_Object args[3];
7620 Lisp_Object msg, fmt;
7621 char *buffer;
7622 int len;
7623 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
7624 USE_SAFE_ALLOCA;
7625
7626 /* Do nothing if called asynchronously. Inserting text into
7627 a buffer may call after-change-functions and alike and
7628 that would means running Lisp asynchronously. */
7629 if (handling_signal)
7630 return;
7631
7632 fmt = msg = Qnil;
7633 GCPRO4 (fmt, msg, arg1, arg2);
7634
7635 args[0] = fmt = build_string (format);
7636 args[1] = arg1;
7637 args[2] = arg2;
7638 msg = Fformat (3, args);
7639
7640 len = SBYTES (msg) + 1;
7641 SAFE_ALLOCA (buffer, char *, len);
7642 bcopy (SDATA (msg), buffer, len);
7643
7644 message_dolog (buffer, len - 1, 1, 0);
7645 SAFE_FREE ();
7646
7647 UNGCPRO;
7648 }
7649
7650
7651 /* Output a newline in the *Messages* buffer if "needs" one. */
7652
7653 void
7654 message_log_maybe_newline ()
7655 {
7656 if (message_log_need_newline)
7657 message_dolog ("", 0, 1, 0);
7658 }
7659
7660
7661 /* Add a string M of length NBYTES to the message log, optionally
7662 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
7663 nonzero, means interpret the contents of M as multibyte. This
7664 function calls low-level routines in order to bypass text property
7665 hooks, etc. which might not be safe to run.
7666
7667 This may GC (insert may run before/after change hooks),
7668 so the buffer M must NOT point to a Lisp string. */
7669
7670 void
7671 message_dolog (m, nbytes, nlflag, multibyte)
7672 const char *m;
7673 int nbytes, nlflag, multibyte;
7674 {
7675 if (!NILP (Vmemory_full))
7676 return;
7677
7678 if (!NILP (Vmessage_log_max))
7679 {
7680 struct buffer *oldbuf;
7681 Lisp_Object oldpoint, oldbegv, oldzv;
7682 int old_windows_or_buffers_changed = windows_or_buffers_changed;
7683 int point_at_end = 0;
7684 int zv_at_end = 0;
7685 Lisp_Object old_deactivate_mark, tem;
7686 struct gcpro gcpro1;
7687
7688 old_deactivate_mark = Vdeactivate_mark;
7689 oldbuf = current_buffer;
7690 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
7691 current_buffer->undo_list = Qt;
7692
7693 oldpoint = message_dolog_marker1;
7694 set_marker_restricted (oldpoint, make_number (PT), Qnil);
7695 oldbegv = message_dolog_marker2;
7696 set_marker_restricted (oldbegv, make_number (BEGV), Qnil);
7697 oldzv = message_dolog_marker3;
7698 set_marker_restricted (oldzv, make_number (ZV), Qnil);
7699 GCPRO1 (old_deactivate_mark);
7700
7701 if (PT == Z)
7702 point_at_end = 1;
7703 if (ZV == Z)
7704 zv_at_end = 1;
7705
7706 BEGV = BEG;
7707 BEGV_BYTE = BEG_BYTE;
7708 ZV = Z;
7709 ZV_BYTE = Z_BYTE;
7710 TEMP_SET_PT_BOTH (Z, Z_BYTE);
7711
7712 /* Insert the string--maybe converting multibyte to single byte
7713 or vice versa, so that all the text fits the buffer. */
7714 if (multibyte
7715 && NILP (current_buffer->enable_multibyte_characters))
7716 {
7717 int i, c, char_bytes;
7718 unsigned char work[1];
7719
7720 /* Convert a multibyte string to single-byte
7721 for the *Message* buffer. */
7722 for (i = 0; i < nbytes; i += char_bytes)
7723 {
7724 c = string_char_and_length (m + i, nbytes - i, &char_bytes);
7725 work[0] = (ASCII_CHAR_P (c)
7726 ? c
7727 : multibyte_char_to_unibyte (c, Qnil));
7728 insert_1_both (work, 1, 1, 1, 0, 0);
7729 }
7730 }
7731 else if (! multibyte
7732 && ! NILP (current_buffer->enable_multibyte_characters))
7733 {
7734 int i, c, char_bytes;
7735 unsigned char *msg = (unsigned char *) m;
7736 unsigned char str[MAX_MULTIBYTE_LENGTH];
7737 /* Convert a single-byte string to multibyte
7738 for the *Message* buffer. */
7739 for (i = 0; i < nbytes; i++)
7740 {
7741 c = msg[i];
7742 c = unibyte_char_to_multibyte (c);
7743 char_bytes = CHAR_STRING (c, str);
7744 insert_1_both (str, 1, char_bytes, 1, 0, 0);
7745 }
7746 }
7747 else if (nbytes)
7748 insert_1 (m, nbytes, 1, 0, 0);
7749
7750 if (nlflag)
7751 {
7752 int this_bol, this_bol_byte, prev_bol, prev_bol_byte, dup;
7753 insert_1 ("\n", 1, 1, 0, 0);
7754
7755 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
7756 this_bol = PT;
7757 this_bol_byte = PT_BYTE;
7758
7759 /* See if this line duplicates the previous one.
7760 If so, combine duplicates. */
7761 if (this_bol > BEG)
7762 {
7763 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
7764 prev_bol = PT;
7765 prev_bol_byte = PT_BYTE;
7766
7767 dup = message_log_check_duplicate (prev_bol, prev_bol_byte,
7768 this_bol, this_bol_byte);
7769 if (dup)
7770 {
7771 del_range_both (prev_bol, prev_bol_byte,
7772 this_bol, this_bol_byte, 0);
7773 if (dup > 1)
7774 {
7775 char dupstr[40];
7776 int duplen;
7777
7778 /* If you change this format, don't forget to also
7779 change message_log_check_duplicate. */
7780 sprintf (dupstr, " [%d times]", dup);
7781 duplen = strlen (dupstr);
7782 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
7783 insert_1 (dupstr, duplen, 1, 0, 1);
7784 }
7785 }
7786 }
7787
7788 /* If we have more than the desired maximum number of lines
7789 in the *Messages* buffer now, delete the oldest ones.
7790 This is safe because we don't have undo in this buffer. */
7791
7792 if (NATNUMP (Vmessage_log_max))
7793 {
7794 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
7795 -XFASTINT (Vmessage_log_max) - 1, 0);
7796 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
7797 }
7798 }
7799 BEGV = XMARKER (oldbegv)->charpos;
7800 BEGV_BYTE = marker_byte_position (oldbegv);
7801
7802 if (zv_at_end)
7803 {
7804 ZV = Z;
7805 ZV_BYTE = Z_BYTE;
7806 }
7807 else
7808 {
7809 ZV = XMARKER (oldzv)->charpos;
7810 ZV_BYTE = marker_byte_position (oldzv);
7811 }
7812
7813 if (point_at_end)
7814 TEMP_SET_PT_BOTH (Z, Z_BYTE);
7815 else
7816 /* We can't do Fgoto_char (oldpoint) because it will run some
7817 Lisp code. */
7818 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
7819 XMARKER (oldpoint)->bytepos);
7820
7821 UNGCPRO;
7822 unchain_marker (XMARKER (oldpoint));
7823 unchain_marker (XMARKER (oldbegv));
7824 unchain_marker (XMARKER (oldzv));
7825
7826 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
7827 set_buffer_internal (oldbuf);
7828 if (NILP (tem))
7829 windows_or_buffers_changed = old_windows_or_buffers_changed;
7830 message_log_need_newline = !nlflag;
7831 Vdeactivate_mark = old_deactivate_mark;
7832 }
7833 }
7834
7835
7836 /* We are at the end of the buffer after just having inserted a newline.
7837 (Note: We depend on the fact we won't be crossing the gap.)
7838 Check to see if the most recent message looks a lot like the previous one.
7839 Return 0 if different, 1 if the new one should just replace it, or a
7840 value N > 1 if we should also append " [N times]". */
7841
7842 static int
7843 message_log_check_duplicate (prev_bol, prev_bol_byte, this_bol, this_bol_byte)
7844 int prev_bol, this_bol;
7845 int prev_bol_byte, this_bol_byte;
7846 {
7847 int i;
7848 int len = Z_BYTE - 1 - this_bol_byte;
7849 int seen_dots = 0;
7850 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
7851 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
7852
7853 for (i = 0; i < len; i++)
7854 {
7855 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
7856 seen_dots = 1;
7857 if (p1[i] != p2[i])
7858 return seen_dots;
7859 }
7860 p1 += len;
7861 if (*p1 == '\n')
7862 return 2;
7863 if (*p1++ == ' ' && *p1++ == '[')
7864 {
7865 int n = 0;
7866 while (*p1 >= '0' && *p1 <= '9')
7867 n = n * 10 + *p1++ - '0';
7868 if (strncmp (p1, " times]\n", 8) == 0)
7869 return n+1;
7870 }
7871 return 0;
7872 }
7873 \f
7874
7875 /* Display an echo area message M with a specified length of NBYTES
7876 bytes. The string may include null characters. If M is 0, clear
7877 out any existing message, and let the mini-buffer text show
7878 through.
7879
7880 This may GC, so the buffer M must NOT point to a Lisp string. */
7881
7882 void
7883 message2 (m, nbytes, multibyte)
7884 const char *m;
7885 int nbytes;
7886 int multibyte;
7887 {
7888 /* First flush out any partial line written with print. */
7889 message_log_maybe_newline ();
7890 if (m)
7891 message_dolog (m, nbytes, 1, multibyte);
7892 message2_nolog (m, nbytes, multibyte);
7893 }
7894
7895
7896 /* The non-logging counterpart of message2. */
7897
7898 void
7899 message2_nolog (m, nbytes, multibyte)
7900 const char *m;
7901 int nbytes, multibyte;
7902 {
7903 struct frame *sf = SELECTED_FRAME ();
7904 message_enable_multibyte = multibyte;
7905
7906 if (noninteractive)
7907 {
7908 if (noninteractive_need_newline)
7909 putc ('\n', stderr);
7910 noninteractive_need_newline = 0;
7911 if (m)
7912 fwrite (m, nbytes, 1, stderr);
7913 if (cursor_in_echo_area == 0)
7914 fprintf (stderr, "\n");
7915 fflush (stderr);
7916 }
7917 /* A null message buffer means that the frame hasn't really been
7918 initialized yet. Error messages get reported properly by
7919 cmd_error, so this must be just an informative message; toss it. */
7920 else if (INTERACTIVE
7921 && sf->glyphs_initialized_p
7922 && FRAME_MESSAGE_BUF (sf))
7923 {
7924 Lisp_Object mini_window;
7925 struct frame *f;
7926
7927 /* Get the frame containing the mini-buffer
7928 that the selected frame is using. */
7929 mini_window = FRAME_MINIBUF_WINDOW (sf);
7930 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
7931
7932 FRAME_SAMPLE_VISIBILITY (f);
7933 if (FRAME_VISIBLE_P (sf)
7934 && ! FRAME_VISIBLE_P (f))
7935 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
7936
7937 if (m)
7938 {
7939 set_message (m, Qnil, nbytes, multibyte);
7940 if (minibuffer_auto_raise)
7941 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
7942 }
7943 else
7944 clear_message (1, 1);
7945
7946 do_pending_window_change (0);
7947 echo_area_display (1);
7948 do_pending_window_change (0);
7949 if (FRAME_TERMINAL (f)->frame_up_to_date_hook != 0 && ! gc_in_progress)
7950 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
7951 }
7952 }
7953
7954
7955 /* Display an echo area message M with a specified length of NBYTES
7956 bytes. The string may include null characters. If M is not a
7957 string, clear out any existing message, and let the mini-buffer
7958 text show through.
7959
7960 This function cancels echoing. */
7961
7962 void
7963 message3 (m, nbytes, multibyte)
7964 Lisp_Object m;
7965 int nbytes;
7966 int multibyte;
7967 {
7968 struct gcpro gcpro1;
7969
7970 GCPRO1 (m);
7971 clear_message (1,1);
7972 cancel_echoing ();
7973
7974 /* First flush out any partial line written with print. */
7975 message_log_maybe_newline ();
7976 if (STRINGP (m))
7977 {
7978 char *buffer;
7979 USE_SAFE_ALLOCA;
7980
7981 SAFE_ALLOCA (buffer, char *, nbytes);
7982 bcopy (SDATA (m), buffer, nbytes);
7983 message_dolog (buffer, nbytes, 1, multibyte);
7984 SAFE_FREE ();
7985 }
7986 message3_nolog (m, nbytes, multibyte);
7987
7988 UNGCPRO;
7989 }
7990
7991
7992 /* The non-logging version of message3.
7993 This does not cancel echoing, because it is used for echoing.
7994 Perhaps we need to make a separate function for echoing
7995 and make this cancel echoing. */
7996
7997 void
7998 message3_nolog (m, nbytes, multibyte)
7999 Lisp_Object m;
8000 int nbytes, multibyte;
8001 {
8002 struct frame *sf = SELECTED_FRAME ();
8003 message_enable_multibyte = multibyte;
8004
8005 if (noninteractive)
8006 {
8007 if (noninteractive_need_newline)
8008 putc ('\n', stderr);
8009 noninteractive_need_newline = 0;
8010 if (STRINGP (m))
8011 fwrite (SDATA (m), nbytes, 1, stderr);
8012 if (cursor_in_echo_area == 0)
8013 fprintf (stderr, "\n");
8014 fflush (stderr);
8015 }
8016 /* A null message buffer means that the frame hasn't really been
8017 initialized yet. Error messages get reported properly by
8018 cmd_error, so this must be just an informative message; toss it. */
8019 else if (INTERACTIVE
8020 && sf->glyphs_initialized_p
8021 && FRAME_MESSAGE_BUF (sf))
8022 {
8023 Lisp_Object mini_window;
8024 Lisp_Object frame;
8025 struct frame *f;
8026
8027 /* Get the frame containing the mini-buffer
8028 that the selected frame is using. */
8029 mini_window = FRAME_MINIBUF_WINDOW (sf);
8030 frame = XWINDOW (mini_window)->frame;
8031 f = XFRAME (frame);
8032
8033 FRAME_SAMPLE_VISIBILITY (f);
8034 if (FRAME_VISIBLE_P (sf)
8035 && !FRAME_VISIBLE_P (f))
8036 Fmake_frame_visible (frame);
8037
8038 if (STRINGP (m) && SCHARS (m) > 0)
8039 {
8040 set_message (NULL, m, nbytes, multibyte);
8041 if (minibuffer_auto_raise)
8042 Fraise_frame (frame);
8043 /* Assume we are not echoing.
8044 (If we are, echo_now will override this.) */
8045 echo_message_buffer = Qnil;
8046 }
8047 else
8048 clear_message (1, 1);
8049
8050 do_pending_window_change (0);
8051 echo_area_display (1);
8052 do_pending_window_change (0);
8053 if (FRAME_TERMINAL (f)->frame_up_to_date_hook != 0 && ! gc_in_progress)
8054 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
8055 }
8056 }
8057
8058
8059 /* Display a null-terminated echo area message M. If M is 0, clear
8060 out any existing message, and let the mini-buffer text show through.
8061
8062 The buffer M must continue to exist until after the echo area gets
8063 cleared or some other message gets displayed there. Do not pass
8064 text that is stored in a Lisp string. Do not pass text in a buffer
8065 that was alloca'd. */
8066
8067 void
8068 message1 (m)
8069 char *m;
8070 {
8071 message2 (m, (m ? strlen (m) : 0), 0);
8072 }
8073
8074
8075 /* The non-logging counterpart of message1. */
8076
8077 void
8078 message1_nolog (m)
8079 char *m;
8080 {
8081 message2_nolog (m, (m ? strlen (m) : 0), 0);
8082 }
8083
8084 /* Display a message M which contains a single %s
8085 which gets replaced with STRING. */
8086
8087 void
8088 message_with_string (m, string, log)
8089 char *m;
8090 Lisp_Object string;
8091 int log;
8092 {
8093 CHECK_STRING (string);
8094
8095 if (noninteractive)
8096 {
8097 if (m)
8098 {
8099 if (noninteractive_need_newline)
8100 putc ('\n', stderr);
8101 noninteractive_need_newline = 0;
8102 fprintf (stderr, m, SDATA (string));
8103 if (cursor_in_echo_area == 0)
8104 fprintf (stderr, "\n");
8105 fflush (stderr);
8106 }
8107 }
8108 else if (INTERACTIVE)
8109 {
8110 /* The frame whose minibuffer we're going to display the message on.
8111 It may be larger than the selected frame, so we need
8112 to use its buffer, not the selected frame's buffer. */
8113 Lisp_Object mini_window;
8114 struct frame *f, *sf = SELECTED_FRAME ();
8115
8116 /* Get the frame containing the minibuffer
8117 that the selected frame is using. */
8118 mini_window = FRAME_MINIBUF_WINDOW (sf);
8119 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
8120
8121 /* A null message buffer means that the frame hasn't really been
8122 initialized yet. Error messages get reported properly by
8123 cmd_error, so this must be just an informative message; toss it. */
8124 if (FRAME_MESSAGE_BUF (f))
8125 {
8126 Lisp_Object args[2], message;
8127 struct gcpro gcpro1, gcpro2;
8128
8129 args[0] = build_string (m);
8130 args[1] = message = string;
8131 GCPRO2 (args[0], message);
8132 gcpro1.nvars = 2;
8133
8134 message = Fformat (2, args);
8135
8136 if (log)
8137 message3 (message, SBYTES (message), STRING_MULTIBYTE (message));
8138 else
8139 message3_nolog (message, SBYTES (message), STRING_MULTIBYTE (message));
8140
8141 UNGCPRO;
8142
8143 /* Print should start at the beginning of the message
8144 buffer next time. */
8145 message_buf_print = 0;
8146 }
8147 }
8148 }
8149
8150
8151 /* Dump an informative message to the minibuf. If M is 0, clear out
8152 any existing message, and let the mini-buffer text show through. */
8153
8154 /* VARARGS 1 */
8155 void
8156 message (m, a1, a2, a3)
8157 char *m;
8158 EMACS_INT a1, a2, a3;
8159 {
8160 if (noninteractive)
8161 {
8162 if (m)
8163 {
8164 if (noninteractive_need_newline)
8165 putc ('\n', stderr);
8166 noninteractive_need_newline = 0;
8167 fprintf (stderr, m, a1, a2, a3);
8168 if (cursor_in_echo_area == 0)
8169 fprintf (stderr, "\n");
8170 fflush (stderr);
8171 }
8172 }
8173 else if (INTERACTIVE)
8174 {
8175 /* The frame whose mini-buffer we're going to display the message
8176 on. It may be larger than the selected frame, so we need to
8177 use its buffer, not the selected frame's buffer. */
8178 Lisp_Object mini_window;
8179 struct frame *f, *sf = SELECTED_FRAME ();
8180
8181 /* Get the frame containing the mini-buffer
8182 that the selected frame is using. */
8183 mini_window = FRAME_MINIBUF_WINDOW (sf);
8184 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
8185
8186 /* A null message buffer means that the frame hasn't really been
8187 initialized yet. Error messages get reported properly by
8188 cmd_error, so this must be just an informative message; toss
8189 it. */
8190 if (FRAME_MESSAGE_BUF (f))
8191 {
8192 if (m)
8193 {
8194 int len;
8195 #ifdef NO_ARG_ARRAY
8196 char *a[3];
8197 a[0] = (char *) a1;
8198 a[1] = (char *) a2;
8199 a[2] = (char *) a3;
8200
8201 len = doprnt (FRAME_MESSAGE_BUF (f),
8202 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
8203 #else
8204 len = doprnt (FRAME_MESSAGE_BUF (f),
8205 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3,
8206 (char **) &a1);
8207 #endif /* NO_ARG_ARRAY */
8208
8209 message2 (FRAME_MESSAGE_BUF (f), len, 0);
8210 }
8211 else
8212 message1 (0);
8213
8214 /* Print should start at the beginning of the message
8215 buffer next time. */
8216 message_buf_print = 0;
8217 }
8218 }
8219 }
8220
8221
8222 /* The non-logging version of message. */
8223
8224 void
8225 message_nolog (m, a1, a2, a3)
8226 char *m;
8227 EMACS_INT a1, a2, a3;
8228 {
8229 Lisp_Object old_log_max;
8230 old_log_max = Vmessage_log_max;
8231 Vmessage_log_max = Qnil;
8232 message (m, a1, a2, a3);
8233 Vmessage_log_max = old_log_max;
8234 }
8235
8236
8237 /* Display the current message in the current mini-buffer. This is
8238 only called from error handlers in process.c, and is not time
8239 critical. */
8240
8241 void
8242 update_echo_area ()
8243 {
8244 if (!NILP (echo_area_buffer[0]))
8245 {
8246 Lisp_Object string;
8247 string = Fcurrent_message ();
8248 message3 (string, SBYTES (string),
8249 !NILP (current_buffer->enable_multibyte_characters));
8250 }
8251 }
8252
8253
8254 /* Make sure echo area buffers in `echo_buffers' are live.
8255 If they aren't, make new ones. */
8256
8257 static void
8258 ensure_echo_area_buffers ()
8259 {
8260 int i;
8261
8262 for (i = 0; i < 2; ++i)
8263 if (!BUFFERP (echo_buffer[i])
8264 || NILP (XBUFFER (echo_buffer[i])->name))
8265 {
8266 char name[30];
8267 Lisp_Object old_buffer;
8268 int j;
8269
8270 old_buffer = echo_buffer[i];
8271 sprintf (name, " *Echo Area %d*", i);
8272 echo_buffer[i] = Fget_buffer_create (build_string (name));
8273 XBUFFER (echo_buffer[i])->truncate_lines = Qnil;
8274 /* to force word wrap in echo area -
8275 it was decided to postpone this*/
8276 /* XBUFFER (echo_buffer[i])->word_wrap = Qt; */
8277
8278 for (j = 0; j < 2; ++j)
8279 if (EQ (old_buffer, echo_area_buffer[j]))
8280 echo_area_buffer[j] = echo_buffer[i];
8281 }
8282 }
8283
8284
8285 /* Call FN with args A1..A4 with either the current or last displayed
8286 echo_area_buffer as current buffer.
8287
8288 WHICH zero means use the current message buffer
8289 echo_area_buffer[0]. If that is nil, choose a suitable buffer
8290 from echo_buffer[] and clear it.
8291
8292 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
8293 suitable buffer from echo_buffer[] and clear it.
8294
8295 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
8296 that the current message becomes the last displayed one, make
8297 choose a suitable buffer for echo_area_buffer[0], and clear it.
8298
8299 Value is what FN returns. */
8300
8301 static int
8302 with_echo_area_buffer (w, which, fn, a1, a2, a3, a4)
8303 struct window *w;
8304 int which;
8305 int (*fn) P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
8306 EMACS_INT a1;
8307 Lisp_Object a2;
8308 EMACS_INT a3, a4;
8309 {
8310 Lisp_Object buffer;
8311 int this_one, the_other, clear_buffer_p, rc;
8312 int count = SPECPDL_INDEX ();
8313
8314 /* If buffers aren't live, make new ones. */
8315 ensure_echo_area_buffers ();
8316
8317 clear_buffer_p = 0;
8318
8319 if (which == 0)
8320 this_one = 0, the_other = 1;
8321 else if (which > 0)
8322 this_one = 1, the_other = 0;
8323 else
8324 {
8325 this_one = 0, the_other = 1;
8326 clear_buffer_p = 1;
8327
8328 /* We need a fresh one in case the current echo buffer equals
8329 the one containing the last displayed echo area message. */
8330 if (!NILP (echo_area_buffer[this_one])
8331 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
8332 echo_area_buffer[this_one] = Qnil;
8333 }
8334
8335 /* Choose a suitable buffer from echo_buffer[] is we don't
8336 have one. */
8337 if (NILP (echo_area_buffer[this_one]))
8338 {
8339 echo_area_buffer[this_one]
8340 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
8341 ? echo_buffer[the_other]
8342 : echo_buffer[this_one]);
8343 clear_buffer_p = 1;
8344 }
8345
8346 buffer = echo_area_buffer[this_one];
8347
8348 /* Don't get confused by reusing the buffer used for echoing
8349 for a different purpose. */
8350 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
8351 cancel_echoing ();
8352
8353 record_unwind_protect (unwind_with_echo_area_buffer,
8354 with_echo_area_buffer_unwind_data (w));
8355
8356 /* Make the echo area buffer current. Note that for display
8357 purposes, it is not necessary that the displayed window's buffer
8358 == current_buffer, except for text property lookup. So, let's
8359 only set that buffer temporarily here without doing a full
8360 Fset_window_buffer. We must also change w->pointm, though,
8361 because otherwise an assertions in unshow_buffer fails, and Emacs
8362 aborts. */
8363 set_buffer_internal_1 (XBUFFER (buffer));
8364 if (w)
8365 {
8366 w->buffer = buffer;
8367 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
8368 }
8369
8370 current_buffer->undo_list = Qt;
8371 current_buffer->read_only = Qnil;
8372 specbind (Qinhibit_read_only, Qt);
8373 specbind (Qinhibit_modification_hooks, Qt);
8374
8375 if (clear_buffer_p && Z > BEG)
8376 del_range (BEG, Z);
8377
8378 xassert (BEGV >= BEG);
8379 xassert (ZV <= Z && ZV >= BEGV);
8380
8381 rc = fn (a1, a2, a3, a4);
8382
8383 xassert (BEGV >= BEG);
8384 xassert (ZV <= Z && ZV >= BEGV);
8385
8386 unbind_to (count, Qnil);
8387 return rc;
8388 }
8389
8390
8391 /* Save state that should be preserved around the call to the function
8392 FN called in with_echo_area_buffer. */
8393
8394 static Lisp_Object
8395 with_echo_area_buffer_unwind_data (w)
8396 struct window *w;
8397 {
8398 int i = 0;
8399 Lisp_Object vector, tmp;
8400
8401 /* Reduce consing by keeping one vector in
8402 Vwith_echo_area_save_vector. */
8403 vector = Vwith_echo_area_save_vector;
8404 Vwith_echo_area_save_vector = Qnil;
8405
8406 if (NILP (vector))
8407 vector = Fmake_vector (make_number (7), Qnil);
8408
8409 XSETBUFFER (tmp, current_buffer); ASET (vector, i, tmp); ++i;
8410 ASET (vector, i, Vdeactivate_mark); ++i;
8411 ASET (vector, i, make_number (windows_or_buffers_changed)); ++i;
8412
8413 if (w)
8414 {
8415 XSETWINDOW (tmp, w); ASET (vector, i, tmp); ++i;
8416 ASET (vector, i, w->buffer); ++i;
8417 ASET (vector, i, make_number (XMARKER (w->pointm)->charpos)); ++i;
8418 ASET (vector, i, make_number (XMARKER (w->pointm)->bytepos)); ++i;
8419 }
8420 else
8421 {
8422 int end = i + 4;
8423 for (; i < end; ++i)
8424 ASET (vector, i, Qnil);
8425 }
8426
8427 xassert (i == ASIZE (vector));
8428 return vector;
8429 }
8430
8431
8432 /* Restore global state from VECTOR which was created by
8433 with_echo_area_buffer_unwind_data. */
8434
8435 static Lisp_Object
8436 unwind_with_echo_area_buffer (vector)
8437 Lisp_Object vector;
8438 {
8439 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
8440 Vdeactivate_mark = AREF (vector, 1);
8441 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
8442
8443 if (WINDOWP (AREF (vector, 3)))
8444 {
8445 struct window *w;
8446 Lisp_Object buffer, charpos, bytepos;
8447
8448 w = XWINDOW (AREF (vector, 3));
8449 buffer = AREF (vector, 4);
8450 charpos = AREF (vector, 5);
8451 bytepos = AREF (vector, 6);
8452
8453 w->buffer = buffer;
8454 set_marker_both (w->pointm, buffer,
8455 XFASTINT (charpos), XFASTINT (bytepos));
8456 }
8457
8458 Vwith_echo_area_save_vector = vector;
8459 return Qnil;
8460 }
8461
8462
8463 /* Set up the echo area for use by print functions. MULTIBYTE_P
8464 non-zero means we will print multibyte. */
8465
8466 void
8467 setup_echo_area_for_printing (multibyte_p)
8468 int multibyte_p;
8469 {
8470 /* If we can't find an echo area any more, exit. */
8471 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
8472 Fkill_emacs (Qnil);
8473
8474 ensure_echo_area_buffers ();
8475
8476 if (!message_buf_print)
8477 {
8478 /* A message has been output since the last time we printed.
8479 Choose a fresh echo area buffer. */
8480 if (EQ (echo_area_buffer[1], echo_buffer[0]))
8481 echo_area_buffer[0] = echo_buffer[1];
8482 else
8483 echo_area_buffer[0] = echo_buffer[0];
8484
8485 /* Switch to that buffer and clear it. */
8486 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
8487 current_buffer->truncate_lines = Qnil;
8488
8489 if (Z > BEG)
8490 {
8491 int count = SPECPDL_INDEX ();
8492 specbind (Qinhibit_read_only, Qt);
8493 /* Note that undo recording is always disabled. */
8494 del_range (BEG, Z);
8495 unbind_to (count, Qnil);
8496 }
8497 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
8498
8499 /* Set up the buffer for the multibyteness we need. */
8500 if (multibyte_p
8501 != !NILP (current_buffer->enable_multibyte_characters))
8502 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
8503
8504 /* Raise the frame containing the echo area. */
8505 if (minibuffer_auto_raise)
8506 {
8507 struct frame *sf = SELECTED_FRAME ();
8508 Lisp_Object mini_window;
8509 mini_window = FRAME_MINIBUF_WINDOW (sf);
8510 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
8511 }
8512
8513 message_log_maybe_newline ();
8514 message_buf_print = 1;
8515 }
8516 else
8517 {
8518 if (NILP (echo_area_buffer[0]))
8519 {
8520 if (EQ (echo_area_buffer[1], echo_buffer[0]))
8521 echo_area_buffer[0] = echo_buffer[1];
8522 else
8523 echo_area_buffer[0] = echo_buffer[0];
8524 }
8525
8526 if (current_buffer != XBUFFER (echo_area_buffer[0]))
8527 {
8528 /* Someone switched buffers between print requests. */
8529 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
8530 current_buffer->truncate_lines = Qnil;
8531 }
8532 }
8533 }
8534
8535
8536 /* Display an echo area message in window W. Value is non-zero if W's
8537 height is changed. If display_last_displayed_message_p is
8538 non-zero, display the message that was last displayed, otherwise
8539 display the current message. */
8540
8541 static int
8542 display_echo_area (w)
8543 struct window *w;
8544 {
8545 int i, no_message_p, window_height_changed_p, count;
8546
8547 /* Temporarily disable garbage collections while displaying the echo
8548 area. This is done because a GC can print a message itself.
8549 That message would modify the echo area buffer's contents while a
8550 redisplay of the buffer is going on, and seriously confuse
8551 redisplay. */
8552 count = inhibit_garbage_collection ();
8553
8554 /* If there is no message, we must call display_echo_area_1
8555 nevertheless because it resizes the window. But we will have to
8556 reset the echo_area_buffer in question to nil at the end because
8557 with_echo_area_buffer will sets it to an empty buffer. */
8558 i = display_last_displayed_message_p ? 1 : 0;
8559 no_message_p = NILP (echo_area_buffer[i]);
8560
8561 window_height_changed_p
8562 = with_echo_area_buffer (w, display_last_displayed_message_p,
8563 display_echo_area_1,
8564 (EMACS_INT) w, Qnil, 0, 0);
8565
8566 if (no_message_p)
8567 echo_area_buffer[i] = Qnil;
8568
8569 unbind_to (count, Qnil);
8570 return window_height_changed_p;
8571 }
8572
8573
8574 /* Helper for display_echo_area. Display the current buffer which
8575 contains the current echo area message in window W, a mini-window,
8576 a pointer to which is passed in A1. A2..A4 are currently not used.
8577 Change the height of W so that all of the message is displayed.
8578 Value is non-zero if height of W was changed. */
8579
8580 static int
8581 display_echo_area_1 (a1, a2, a3, a4)
8582 EMACS_INT a1;
8583 Lisp_Object a2;
8584 EMACS_INT a3, a4;
8585 {
8586 struct window *w = (struct window *) a1;
8587 Lisp_Object window;
8588 struct text_pos start;
8589 int window_height_changed_p = 0;
8590
8591 /* Do this before displaying, so that we have a large enough glyph
8592 matrix for the display. If we can't get enough space for the
8593 whole text, display the last N lines. That works by setting w->start. */
8594 window_height_changed_p = resize_mini_window (w, 0);
8595
8596 /* Use the starting position chosen by resize_mini_window. */
8597 SET_TEXT_POS_FROM_MARKER (start, w->start);
8598
8599 /* Display. */
8600 clear_glyph_matrix (w->desired_matrix);
8601 XSETWINDOW (window, w);
8602 try_window (window, start, 0);
8603
8604 return window_height_changed_p;
8605 }
8606
8607
8608 /* Resize the echo area window to exactly the size needed for the
8609 currently displayed message, if there is one. If a mini-buffer
8610 is active, don't shrink it. */
8611
8612 void
8613 resize_echo_area_exactly ()
8614 {
8615 if (BUFFERP (echo_area_buffer[0])
8616 && WINDOWP (echo_area_window))
8617 {
8618 struct window *w = XWINDOW (echo_area_window);
8619 int resized_p;
8620 Lisp_Object resize_exactly;
8621
8622 if (minibuf_level == 0)
8623 resize_exactly = Qt;
8624 else
8625 resize_exactly = Qnil;
8626
8627 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
8628 (EMACS_INT) w, resize_exactly, 0, 0);
8629 if (resized_p)
8630 {
8631 ++windows_or_buffers_changed;
8632 ++update_mode_lines;
8633 redisplay_internal (0);
8634 }
8635 }
8636 }
8637
8638
8639 /* Callback function for with_echo_area_buffer, when used from
8640 resize_echo_area_exactly. A1 contains a pointer to the window to
8641 resize, EXACTLY non-nil means resize the mini-window exactly to the
8642 size of the text displayed. A3 and A4 are not used. Value is what
8643 resize_mini_window returns. */
8644
8645 static int
8646 resize_mini_window_1 (a1, exactly, a3, a4)
8647 EMACS_INT a1;
8648 Lisp_Object exactly;
8649 EMACS_INT a3, a4;
8650 {
8651 return resize_mini_window ((struct window *) a1, !NILP (exactly));
8652 }
8653
8654
8655 /* Resize mini-window W to fit the size of its contents. EXACT_P
8656 means size the window exactly to the size needed. Otherwise, it's
8657 only enlarged until W's buffer is empty.
8658
8659 Set W->start to the right place to begin display. If the whole
8660 contents fit, start at the beginning. Otherwise, start so as
8661 to make the end of the contents appear. This is particularly
8662 important for y-or-n-p, but seems desirable generally.
8663
8664 Value is non-zero if the window height has been changed. */
8665
8666 int
8667 resize_mini_window (w, exact_p)
8668 struct window *w;
8669 int exact_p;
8670 {
8671 struct frame *f = XFRAME (w->frame);
8672 int window_height_changed_p = 0;
8673
8674 xassert (MINI_WINDOW_P (w));
8675
8676 /* By default, start display at the beginning. */
8677 set_marker_both (w->start, w->buffer,
8678 BUF_BEGV (XBUFFER (w->buffer)),
8679 BUF_BEGV_BYTE (XBUFFER (w->buffer)));
8680
8681 /* Don't resize windows while redisplaying a window; it would
8682 confuse redisplay functions when the size of the window they are
8683 displaying changes from under them. Such a resizing can happen,
8684 for instance, when which-func prints a long message while
8685 we are running fontification-functions. We're running these
8686 functions with safe_call which binds inhibit-redisplay to t. */
8687 if (!NILP (Vinhibit_redisplay))
8688 return 0;
8689
8690 /* Nil means don't try to resize. */
8691 if (NILP (Vresize_mini_windows)
8692 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
8693 return 0;
8694
8695 if (!FRAME_MINIBUF_ONLY_P (f))
8696 {
8697 struct it it;
8698 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
8699 int total_height = WINDOW_TOTAL_LINES (root) + WINDOW_TOTAL_LINES (w);
8700 int height, max_height;
8701 int unit = FRAME_LINE_HEIGHT (f);
8702 struct text_pos start;
8703 struct buffer *old_current_buffer = NULL;
8704
8705 if (current_buffer != XBUFFER (w->buffer))
8706 {
8707 old_current_buffer = current_buffer;
8708 set_buffer_internal (XBUFFER (w->buffer));
8709 }
8710
8711 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
8712
8713 /* Compute the max. number of lines specified by the user. */
8714 if (FLOATP (Vmax_mini_window_height))
8715 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_LINES (f);
8716 else if (INTEGERP (Vmax_mini_window_height))
8717 max_height = XINT (Vmax_mini_window_height);
8718 else
8719 max_height = total_height / 4;
8720
8721 /* Correct that max. height if it's bogus. */
8722 max_height = max (1, max_height);
8723 max_height = min (total_height, max_height);
8724
8725 /* Find out the height of the text in the window. */
8726 if (it.line_wrap == TRUNCATE)
8727 height = 1;
8728 else
8729 {
8730 last_height = 0;
8731 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
8732 if (it.max_ascent == 0 && it.max_descent == 0)
8733 height = it.current_y + last_height;
8734 else
8735 height = it.current_y + it.max_ascent + it.max_descent;
8736 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
8737 height = (height + unit - 1) / unit;
8738 }
8739
8740 /* Compute a suitable window start. */
8741 if (height > max_height)
8742 {
8743 height = max_height;
8744 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
8745 move_it_vertically_backward (&it, (height - 1) * unit);
8746 start = it.current.pos;
8747 }
8748 else
8749 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
8750 SET_MARKER_FROM_TEXT_POS (w->start, start);
8751
8752 if (EQ (Vresize_mini_windows, Qgrow_only))
8753 {
8754 /* Let it grow only, until we display an empty message, in which
8755 case the window shrinks again. */
8756 if (height > WINDOW_TOTAL_LINES (w))
8757 {
8758 int old_height = WINDOW_TOTAL_LINES (w);
8759 freeze_window_starts (f, 1);
8760 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
8761 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8762 }
8763 else if (height < WINDOW_TOTAL_LINES (w)
8764 && (exact_p || BEGV == ZV))
8765 {
8766 int old_height = WINDOW_TOTAL_LINES (w);
8767 freeze_window_starts (f, 0);
8768 shrink_mini_window (w);
8769 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8770 }
8771 }
8772 else
8773 {
8774 /* Always resize to exact size needed. */
8775 if (height > WINDOW_TOTAL_LINES (w))
8776 {
8777 int old_height = WINDOW_TOTAL_LINES (w);
8778 freeze_window_starts (f, 1);
8779 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
8780 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8781 }
8782 else if (height < WINDOW_TOTAL_LINES (w))
8783 {
8784 int old_height = WINDOW_TOTAL_LINES (w);
8785 freeze_window_starts (f, 0);
8786 shrink_mini_window (w);
8787
8788 if (height)
8789 {
8790 freeze_window_starts (f, 1);
8791 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
8792 }
8793
8794 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8795 }
8796 }
8797
8798 if (old_current_buffer)
8799 set_buffer_internal (old_current_buffer);
8800 }
8801
8802 return window_height_changed_p;
8803 }
8804
8805
8806 /* Value is the current message, a string, or nil if there is no
8807 current message. */
8808
8809 Lisp_Object
8810 current_message ()
8811 {
8812 Lisp_Object msg;
8813
8814 if (!BUFFERP (echo_area_buffer[0]))
8815 msg = Qnil;
8816 else
8817 {
8818 with_echo_area_buffer (0, 0, current_message_1,
8819 (EMACS_INT) &msg, Qnil, 0, 0);
8820 if (NILP (msg))
8821 echo_area_buffer[0] = Qnil;
8822 }
8823
8824 return msg;
8825 }
8826
8827
8828 static int
8829 current_message_1 (a1, a2, a3, a4)
8830 EMACS_INT a1;
8831 Lisp_Object a2;
8832 EMACS_INT a3, a4;
8833 {
8834 Lisp_Object *msg = (Lisp_Object *) a1;
8835
8836 if (Z > BEG)
8837 *msg = make_buffer_string (BEG, Z, 1);
8838 else
8839 *msg = Qnil;
8840 return 0;
8841 }
8842
8843
8844 /* Push the current message on Vmessage_stack for later restauration
8845 by restore_message. Value is non-zero if the current message isn't
8846 empty. This is a relatively infrequent operation, so it's not
8847 worth optimizing. */
8848
8849 int
8850 push_message ()
8851 {
8852 Lisp_Object msg;
8853 msg = current_message ();
8854 Vmessage_stack = Fcons (msg, Vmessage_stack);
8855 return STRINGP (msg);
8856 }
8857
8858
8859 /* Restore message display from the top of Vmessage_stack. */
8860
8861 void
8862 restore_message ()
8863 {
8864 Lisp_Object msg;
8865
8866 xassert (CONSP (Vmessage_stack));
8867 msg = XCAR (Vmessage_stack);
8868 if (STRINGP (msg))
8869 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
8870 else
8871 message3_nolog (msg, 0, 0);
8872 }
8873
8874
8875 /* Handler for record_unwind_protect calling pop_message. */
8876
8877 Lisp_Object
8878 pop_message_unwind (dummy)
8879 Lisp_Object dummy;
8880 {
8881 pop_message ();
8882 return Qnil;
8883 }
8884
8885 /* Pop the top-most entry off Vmessage_stack. */
8886
8887 void
8888 pop_message ()
8889 {
8890 xassert (CONSP (Vmessage_stack));
8891 Vmessage_stack = XCDR (Vmessage_stack);
8892 }
8893
8894
8895 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
8896 exits. If the stack is not empty, we have a missing pop_message
8897 somewhere. */
8898
8899 void
8900 check_message_stack ()
8901 {
8902 if (!NILP (Vmessage_stack))
8903 abort ();
8904 }
8905
8906
8907 /* Truncate to NCHARS what will be displayed in the echo area the next
8908 time we display it---but don't redisplay it now. */
8909
8910 void
8911 truncate_echo_area (nchars)
8912 int nchars;
8913 {
8914 if (nchars == 0)
8915 echo_area_buffer[0] = Qnil;
8916 /* A null message buffer means that the frame hasn't really been
8917 initialized yet. Error messages get reported properly by
8918 cmd_error, so this must be just an informative message; toss it. */
8919 else if (!noninteractive
8920 && INTERACTIVE
8921 && !NILP (echo_area_buffer[0]))
8922 {
8923 struct frame *sf = SELECTED_FRAME ();
8924 if (FRAME_MESSAGE_BUF (sf))
8925 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
8926 }
8927 }
8928
8929
8930 /* Helper function for truncate_echo_area. Truncate the current
8931 message to at most NCHARS characters. */
8932
8933 static int
8934 truncate_message_1 (nchars, a2, a3, a4)
8935 EMACS_INT nchars;
8936 Lisp_Object a2;
8937 EMACS_INT a3, a4;
8938 {
8939 if (BEG + nchars < Z)
8940 del_range (BEG + nchars, Z);
8941 if (Z == BEG)
8942 echo_area_buffer[0] = Qnil;
8943 return 0;
8944 }
8945
8946
8947 /* Set the current message to a substring of S or STRING.
8948
8949 If STRING is a Lisp string, set the message to the first NBYTES
8950 bytes from STRING. NBYTES zero means use the whole string. If
8951 STRING is multibyte, the message will be displayed multibyte.
8952
8953 If S is not null, set the message to the first LEN bytes of S. LEN
8954 zero means use the whole string. MULTIBYTE_P non-zero means S is
8955 multibyte. Display the message multibyte in that case.
8956
8957 Doesn't GC, as with_echo_area_buffer binds Qinhibit_modification_hooks
8958 to t before calling set_message_1 (which calls insert).
8959 */
8960
8961 void
8962 set_message (s, string, nbytes, multibyte_p)
8963 const char *s;
8964 Lisp_Object string;
8965 int nbytes, multibyte_p;
8966 {
8967 message_enable_multibyte
8968 = ((s && multibyte_p)
8969 || (STRINGP (string) && STRING_MULTIBYTE (string)));
8970
8971 with_echo_area_buffer (0, -1, set_message_1,
8972 (EMACS_INT) s, string, nbytes, multibyte_p);
8973 message_buf_print = 0;
8974 help_echo_showing_p = 0;
8975 }
8976
8977
8978 /* Helper function for set_message. Arguments have the same meaning
8979 as there, with A1 corresponding to S and A2 corresponding to STRING
8980 This function is called with the echo area buffer being
8981 current. */
8982
8983 static int
8984 set_message_1 (a1, a2, nbytes, multibyte_p)
8985 EMACS_INT a1;
8986 Lisp_Object a2;
8987 EMACS_INT nbytes, multibyte_p;
8988 {
8989 const char *s = (const char *) a1;
8990 Lisp_Object string = a2;
8991
8992 /* Change multibyteness of the echo buffer appropriately. */
8993 if (message_enable_multibyte
8994 != !NILP (current_buffer->enable_multibyte_characters))
8995 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
8996
8997 current_buffer->truncate_lines = message_truncate_lines ? Qt : Qnil;
8998
8999 /* Insert new message at BEG. */
9000 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
9001
9002 if (STRINGP (string))
9003 {
9004 int nchars;
9005
9006 if (nbytes == 0)
9007 nbytes = SBYTES (string);
9008 nchars = string_byte_to_char (string, nbytes);
9009
9010 /* This function takes care of single/multibyte conversion. We
9011 just have to ensure that the echo area buffer has the right
9012 setting of enable_multibyte_characters. */
9013 insert_from_string (string, 0, 0, nchars, nbytes, 1);
9014 }
9015 else if (s)
9016 {
9017 if (nbytes == 0)
9018 nbytes = strlen (s);
9019
9020 if (multibyte_p && NILP (current_buffer->enable_multibyte_characters))
9021 {
9022 /* Convert from multi-byte to single-byte. */
9023 int i, c, n;
9024 unsigned char work[1];
9025
9026 /* Convert a multibyte string to single-byte. */
9027 for (i = 0; i < nbytes; i += n)
9028 {
9029 c = string_char_and_length (s + i, nbytes - i, &n);
9030 work[0] = (ASCII_CHAR_P (c)
9031 ? c
9032 : multibyte_char_to_unibyte (c, Qnil));
9033 insert_1_both (work, 1, 1, 1, 0, 0);
9034 }
9035 }
9036 else if (!multibyte_p
9037 && !NILP (current_buffer->enable_multibyte_characters))
9038 {
9039 /* Convert from single-byte to multi-byte. */
9040 int i, c, n;
9041 const unsigned char *msg = (const unsigned char *) s;
9042 unsigned char str[MAX_MULTIBYTE_LENGTH];
9043
9044 /* Convert a single-byte string to multibyte. */
9045 for (i = 0; i < nbytes; i++)
9046 {
9047 c = msg[i];
9048 c = unibyte_char_to_multibyte (c);
9049 n = CHAR_STRING (c, str);
9050 insert_1_both (str, 1, n, 1, 0, 0);
9051 }
9052 }
9053 else
9054 insert_1 (s, nbytes, 1, 0, 0);
9055 }
9056
9057 return 0;
9058 }
9059
9060
9061 /* Clear messages. CURRENT_P non-zero means clear the current
9062 message. LAST_DISPLAYED_P non-zero means clear the message
9063 last displayed. */
9064
9065 void
9066 clear_message (current_p, last_displayed_p)
9067 int current_p, last_displayed_p;
9068 {
9069 if (current_p)
9070 {
9071 echo_area_buffer[0] = Qnil;
9072 message_cleared_p = 1;
9073 }
9074
9075 if (last_displayed_p)
9076 echo_area_buffer[1] = Qnil;
9077
9078 message_buf_print = 0;
9079 }
9080
9081 /* Clear garbaged frames.
9082
9083 This function is used where the old redisplay called
9084 redraw_garbaged_frames which in turn called redraw_frame which in
9085 turn called clear_frame. The call to clear_frame was a source of
9086 flickering. I believe a clear_frame is not necessary. It should
9087 suffice in the new redisplay to invalidate all current matrices,
9088 and ensure a complete redisplay of all windows. */
9089
9090 static void
9091 clear_garbaged_frames ()
9092 {
9093 if (frame_garbaged)
9094 {
9095 Lisp_Object tail, frame;
9096 int changed_count = 0;
9097
9098 FOR_EACH_FRAME (tail, frame)
9099 {
9100 struct frame *f = XFRAME (frame);
9101
9102 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
9103 {
9104 if (f->resized_p)
9105 {
9106 Fredraw_frame (frame);
9107 f->force_flush_display_p = 1;
9108 }
9109 clear_current_matrices (f);
9110 changed_count++;
9111 f->garbaged = 0;
9112 f->resized_p = 0;
9113 }
9114 }
9115
9116 frame_garbaged = 0;
9117 if (changed_count)
9118 ++windows_or_buffers_changed;
9119 }
9120 }
9121
9122
9123 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
9124 is non-zero update selected_frame. Value is non-zero if the
9125 mini-windows height has been changed. */
9126
9127 static int
9128 echo_area_display (update_frame_p)
9129 int update_frame_p;
9130 {
9131 Lisp_Object mini_window;
9132 struct window *w;
9133 struct frame *f;
9134 int window_height_changed_p = 0;
9135 struct frame *sf = SELECTED_FRAME ();
9136
9137 mini_window = FRAME_MINIBUF_WINDOW (sf);
9138 w = XWINDOW (mini_window);
9139 f = XFRAME (WINDOW_FRAME (w));
9140
9141 /* Don't display if frame is invisible or not yet initialized. */
9142 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
9143 return 0;
9144
9145 #ifdef HAVE_WINDOW_SYSTEM
9146 /* When Emacs starts, selected_frame may be the initial terminal
9147 frame. If we let this through, a message would be displayed on
9148 the terminal. */
9149 if (FRAME_INITIAL_P (XFRAME (selected_frame)))
9150 return 0;
9151 #endif /* HAVE_WINDOW_SYSTEM */
9152
9153 /* Redraw garbaged frames. */
9154 if (frame_garbaged)
9155 clear_garbaged_frames ();
9156
9157 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
9158 {
9159 echo_area_window = mini_window;
9160 window_height_changed_p = display_echo_area (w);
9161 w->must_be_updated_p = 1;
9162
9163 /* Update the display, unless called from redisplay_internal.
9164 Also don't update the screen during redisplay itself. The
9165 update will happen at the end of redisplay, and an update
9166 here could cause confusion. */
9167 if (update_frame_p && !redisplaying_p)
9168 {
9169 int n = 0;
9170
9171 /* If the display update has been interrupted by pending
9172 input, update mode lines in the frame. Due to the
9173 pending input, it might have been that redisplay hasn't
9174 been called, so that mode lines above the echo area are
9175 garbaged. This looks odd, so we prevent it here. */
9176 if (!display_completed)
9177 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
9178
9179 if (window_height_changed_p
9180 /* Don't do this if Emacs is shutting down. Redisplay
9181 needs to run hooks. */
9182 && !NILP (Vrun_hooks))
9183 {
9184 /* Must update other windows. Likewise as in other
9185 cases, don't let this update be interrupted by
9186 pending input. */
9187 int count = SPECPDL_INDEX ();
9188 specbind (Qredisplay_dont_pause, Qt);
9189 windows_or_buffers_changed = 1;
9190 redisplay_internal (0);
9191 unbind_to (count, Qnil);
9192 }
9193 else if (FRAME_WINDOW_P (f) && n == 0)
9194 {
9195 /* Window configuration is the same as before.
9196 Can do with a display update of the echo area,
9197 unless we displayed some mode lines. */
9198 update_single_window (w, 1);
9199 FRAME_RIF (f)->flush_display (f);
9200 }
9201 else
9202 update_frame (f, 1, 1);
9203
9204 /* If cursor is in the echo area, make sure that the next
9205 redisplay displays the minibuffer, so that the cursor will
9206 be replaced with what the minibuffer wants. */
9207 if (cursor_in_echo_area)
9208 ++windows_or_buffers_changed;
9209 }
9210 }
9211 else if (!EQ (mini_window, selected_window))
9212 windows_or_buffers_changed++;
9213
9214 /* Last displayed message is now the current message. */
9215 echo_area_buffer[1] = echo_area_buffer[0];
9216 /* Inform read_char that we're not echoing. */
9217 echo_message_buffer = Qnil;
9218
9219 /* Prevent redisplay optimization in redisplay_internal by resetting
9220 this_line_start_pos. This is done because the mini-buffer now
9221 displays the message instead of its buffer text. */
9222 if (EQ (mini_window, selected_window))
9223 CHARPOS (this_line_start_pos) = 0;
9224
9225 return window_height_changed_p;
9226 }
9227
9228
9229 \f
9230 /***********************************************************************
9231 Mode Lines and Frame Titles
9232 ***********************************************************************/
9233
9234 /* A buffer for constructing non-propertized mode-line strings and
9235 frame titles in it; allocated from the heap in init_xdisp and
9236 resized as needed in store_mode_line_noprop_char. */
9237
9238 static char *mode_line_noprop_buf;
9239
9240 /* The buffer's end, and a current output position in it. */
9241
9242 static char *mode_line_noprop_buf_end;
9243 static char *mode_line_noprop_ptr;
9244
9245 #define MODE_LINE_NOPROP_LEN(start) \
9246 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
9247
9248 static enum {
9249 MODE_LINE_DISPLAY = 0,
9250 MODE_LINE_TITLE,
9251 MODE_LINE_NOPROP,
9252 MODE_LINE_STRING
9253 } mode_line_target;
9254
9255 /* Alist that caches the results of :propertize.
9256 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
9257 static Lisp_Object mode_line_proptrans_alist;
9258
9259 /* List of strings making up the mode-line. */
9260 static Lisp_Object mode_line_string_list;
9261
9262 /* Base face property when building propertized mode line string. */
9263 static Lisp_Object mode_line_string_face;
9264 static Lisp_Object mode_line_string_face_prop;
9265
9266
9267 /* Unwind data for mode line strings */
9268
9269 static Lisp_Object Vmode_line_unwind_vector;
9270
9271 static Lisp_Object
9272 format_mode_line_unwind_data (struct buffer *obuf,
9273 Lisp_Object owin,
9274 int save_proptrans)
9275 {
9276 Lisp_Object vector, tmp;
9277
9278 /* Reduce consing by keeping one vector in
9279 Vwith_echo_area_save_vector. */
9280 vector = Vmode_line_unwind_vector;
9281 Vmode_line_unwind_vector = Qnil;
9282
9283 if (NILP (vector))
9284 vector = Fmake_vector (make_number (8), Qnil);
9285
9286 ASET (vector, 0, make_number (mode_line_target));
9287 ASET (vector, 1, make_number (MODE_LINE_NOPROP_LEN (0)));
9288 ASET (vector, 2, mode_line_string_list);
9289 ASET (vector, 3, save_proptrans ? mode_line_proptrans_alist : Qt);
9290 ASET (vector, 4, mode_line_string_face);
9291 ASET (vector, 5, mode_line_string_face_prop);
9292
9293 if (obuf)
9294 XSETBUFFER (tmp, obuf);
9295 else
9296 tmp = Qnil;
9297 ASET (vector, 6, tmp);
9298 ASET (vector, 7, owin);
9299
9300 return vector;
9301 }
9302
9303 static Lisp_Object
9304 unwind_format_mode_line (vector)
9305 Lisp_Object vector;
9306 {
9307 mode_line_target = XINT (AREF (vector, 0));
9308 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
9309 mode_line_string_list = AREF (vector, 2);
9310 if (! EQ (AREF (vector, 3), Qt))
9311 mode_line_proptrans_alist = AREF (vector, 3);
9312 mode_line_string_face = AREF (vector, 4);
9313 mode_line_string_face_prop = AREF (vector, 5);
9314
9315 if (!NILP (AREF (vector, 7)))
9316 /* Select window before buffer, since it may change the buffer. */
9317 Fselect_window (AREF (vector, 7), Qt);
9318
9319 if (!NILP (AREF (vector, 6)))
9320 {
9321 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
9322 ASET (vector, 6, Qnil);
9323 }
9324
9325 Vmode_line_unwind_vector = vector;
9326 return Qnil;
9327 }
9328
9329
9330 /* Store a single character C for the frame title in mode_line_noprop_buf.
9331 Re-allocate mode_line_noprop_buf if necessary. */
9332
9333 static void
9334 #ifdef PROTOTYPES
9335 store_mode_line_noprop_char (char c)
9336 #else
9337 store_mode_line_noprop_char (c)
9338 char c;
9339 #endif
9340 {
9341 /* If output position has reached the end of the allocated buffer,
9342 double the buffer's size. */
9343 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
9344 {
9345 int len = MODE_LINE_NOPROP_LEN (0);
9346 int new_size = 2 * len * sizeof *mode_line_noprop_buf;
9347 mode_line_noprop_buf = (char *) xrealloc (mode_line_noprop_buf, new_size);
9348 mode_line_noprop_buf_end = mode_line_noprop_buf + new_size;
9349 mode_line_noprop_ptr = mode_line_noprop_buf + len;
9350 }
9351
9352 *mode_line_noprop_ptr++ = c;
9353 }
9354
9355
9356 /* Store part of a frame title in mode_line_noprop_buf, beginning at
9357 mode_line_noprop_ptr. STR is the string to store. Do not copy
9358 characters that yield more columns than PRECISION; PRECISION <= 0
9359 means copy the whole string. Pad with spaces until FIELD_WIDTH
9360 number of characters have been copied; FIELD_WIDTH <= 0 means don't
9361 pad. Called from display_mode_element when it is used to build a
9362 frame title. */
9363
9364 static int
9365 store_mode_line_noprop (str, field_width, precision)
9366 const unsigned char *str;
9367 int field_width, precision;
9368 {
9369 int n = 0;
9370 int dummy, nbytes;
9371
9372 /* Copy at most PRECISION chars from STR. */
9373 nbytes = strlen (str);
9374 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
9375 while (nbytes--)
9376 store_mode_line_noprop_char (*str++);
9377
9378 /* Fill up with spaces until FIELD_WIDTH reached. */
9379 while (field_width > 0
9380 && n < field_width)
9381 {
9382 store_mode_line_noprop_char (' ');
9383 ++n;
9384 }
9385
9386 return n;
9387 }
9388
9389 /***********************************************************************
9390 Frame Titles
9391 ***********************************************************************/
9392
9393 #ifdef HAVE_WINDOW_SYSTEM
9394
9395 /* Set the title of FRAME, if it has changed. The title format is
9396 Vicon_title_format if FRAME is iconified, otherwise it is
9397 frame_title_format. */
9398
9399 static void
9400 x_consider_frame_title (frame)
9401 Lisp_Object frame;
9402 {
9403 struct frame *f = XFRAME (frame);
9404
9405 if (FRAME_WINDOW_P (f)
9406 || FRAME_MINIBUF_ONLY_P (f)
9407 || f->explicit_name)
9408 {
9409 /* Do we have more than one visible frame on this X display? */
9410 Lisp_Object tail;
9411 Lisp_Object fmt;
9412 int title_start;
9413 char *title;
9414 int len;
9415 struct it it;
9416 int count = SPECPDL_INDEX ();
9417
9418 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
9419 {
9420 Lisp_Object other_frame = XCAR (tail);
9421 struct frame *tf = XFRAME (other_frame);
9422
9423 if (tf != f
9424 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
9425 && !FRAME_MINIBUF_ONLY_P (tf)
9426 && !EQ (other_frame, tip_frame)
9427 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
9428 break;
9429 }
9430
9431 /* Set global variable indicating that multiple frames exist. */
9432 multiple_frames = CONSP (tail);
9433
9434 /* Switch to the buffer of selected window of the frame. Set up
9435 mode_line_target so that display_mode_element will output into
9436 mode_line_noprop_buf; then display the title. */
9437 record_unwind_protect (unwind_format_mode_line,
9438 format_mode_line_unwind_data
9439 (current_buffer, selected_window, 0));
9440
9441 Fselect_window (f->selected_window, Qt);
9442 set_buffer_internal_1 (XBUFFER (XWINDOW (f->selected_window)->buffer));
9443 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
9444
9445 mode_line_target = MODE_LINE_TITLE;
9446 title_start = MODE_LINE_NOPROP_LEN (0);
9447 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
9448 NULL, DEFAULT_FACE_ID);
9449 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
9450 len = MODE_LINE_NOPROP_LEN (title_start);
9451 title = mode_line_noprop_buf + title_start;
9452 unbind_to (count, Qnil);
9453
9454 /* Set the title only if it's changed. This avoids consing in
9455 the common case where it hasn't. (If it turns out that we've
9456 already wasted too much time by walking through the list with
9457 display_mode_element, then we might need to optimize at a
9458 higher level than this.) */
9459 if (! STRINGP (f->name)
9460 || SBYTES (f->name) != len
9461 || bcmp (title, SDATA (f->name), len) != 0)
9462 {
9463 #ifdef HAVE_NS
9464 if (FRAME_NS_P (f))
9465 {
9466 if (!MINI_WINDOW_P(XWINDOW(f->selected_window)))
9467 {
9468 if (EQ (fmt, Qt))
9469 ns_set_name_as_filename (f);
9470 else
9471 x_implicitly_set_name (f, make_string(title, len),
9472 Qnil);
9473 }
9474 }
9475 else
9476 #endif
9477 x_implicitly_set_name (f, make_string (title, len), Qnil);
9478 }
9479 #ifdef HAVE_NS
9480 if (FRAME_NS_P (f))
9481 {
9482 /* do this also for frames with explicit names */
9483 ns_implicitly_set_icon_type(f);
9484 ns_set_doc_edited(f, Fbuffer_modified_p
9485 (XWINDOW (f->selected_window)->buffer), Qnil);
9486 }
9487 #endif
9488 }
9489 }
9490
9491 #endif /* not HAVE_WINDOW_SYSTEM */
9492
9493
9494
9495 \f
9496 /***********************************************************************
9497 Menu Bars
9498 ***********************************************************************/
9499
9500
9501 /* Prepare for redisplay by updating menu-bar item lists when
9502 appropriate. This can call eval. */
9503
9504 void
9505 prepare_menu_bars ()
9506 {
9507 int all_windows;
9508 struct gcpro gcpro1, gcpro2;
9509 struct frame *f;
9510 Lisp_Object tooltip_frame;
9511
9512 #ifdef HAVE_WINDOW_SYSTEM
9513 tooltip_frame = tip_frame;
9514 #else
9515 tooltip_frame = Qnil;
9516 #endif
9517
9518 /* Update all frame titles based on their buffer names, etc. We do
9519 this before the menu bars so that the buffer-menu will show the
9520 up-to-date frame titles. */
9521 #ifdef HAVE_WINDOW_SYSTEM
9522 if (windows_or_buffers_changed || update_mode_lines)
9523 {
9524 Lisp_Object tail, frame;
9525
9526 FOR_EACH_FRAME (tail, frame)
9527 {
9528 f = XFRAME (frame);
9529 if (!EQ (frame, tooltip_frame)
9530 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
9531 x_consider_frame_title (frame);
9532 }
9533 }
9534 #endif /* HAVE_WINDOW_SYSTEM */
9535
9536 /* Update the menu bar item lists, if appropriate. This has to be
9537 done before any actual redisplay or generation of display lines. */
9538 all_windows = (update_mode_lines
9539 || buffer_shared > 1
9540 || windows_or_buffers_changed);
9541 if (all_windows)
9542 {
9543 Lisp_Object tail, frame;
9544 int count = SPECPDL_INDEX ();
9545 /* 1 means that update_menu_bar has run its hooks
9546 so any further calls to update_menu_bar shouldn't do so again. */
9547 int menu_bar_hooks_run = 0;
9548
9549 record_unwind_save_match_data ();
9550
9551 FOR_EACH_FRAME (tail, frame)
9552 {
9553 f = XFRAME (frame);
9554
9555 /* Ignore tooltip frame. */
9556 if (EQ (frame, tooltip_frame))
9557 continue;
9558
9559 /* If a window on this frame changed size, report that to
9560 the user and clear the size-change flag. */
9561 if (FRAME_WINDOW_SIZES_CHANGED (f))
9562 {
9563 Lisp_Object functions;
9564
9565 /* Clear flag first in case we get an error below. */
9566 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
9567 functions = Vwindow_size_change_functions;
9568 GCPRO2 (tail, functions);
9569
9570 while (CONSP (functions))
9571 {
9572 if (!EQ (XCAR (functions), Qt))
9573 call1 (XCAR (functions), frame);
9574 functions = XCDR (functions);
9575 }
9576 UNGCPRO;
9577 }
9578
9579 GCPRO1 (tail);
9580 menu_bar_hooks_run = update_menu_bar (f, 0, menu_bar_hooks_run);
9581 #ifdef HAVE_WINDOW_SYSTEM
9582 update_tool_bar (f, 0);
9583 #endif
9584 UNGCPRO;
9585 }
9586
9587 unbind_to (count, Qnil);
9588 }
9589 else
9590 {
9591 struct frame *sf = SELECTED_FRAME ();
9592 update_menu_bar (sf, 1, 0);
9593 #ifdef HAVE_WINDOW_SYSTEM
9594 update_tool_bar (sf, 1);
9595 #endif
9596 }
9597
9598 /* Motif needs this. See comment in xmenu.c. Turn it off when
9599 pending_menu_activation is not defined. */
9600 #ifdef USE_X_TOOLKIT
9601 pending_menu_activation = 0;
9602 #endif
9603 }
9604
9605
9606 /* Update the menu bar item list for frame F. This has to be done
9607 before we start to fill in any display lines, because it can call
9608 eval.
9609
9610 If SAVE_MATCH_DATA is non-zero, we must save and restore it here.
9611
9612 If HOOKS_RUN is 1, that means a previous call to update_menu_bar
9613 already ran the menu bar hooks for this redisplay, so there
9614 is no need to run them again. The return value is the
9615 updated value of this flag, to pass to the next call. */
9616
9617 static int
9618 update_menu_bar (f, save_match_data, hooks_run)
9619 struct frame *f;
9620 int save_match_data;
9621 int hooks_run;
9622 {
9623 Lisp_Object window;
9624 register struct window *w;
9625
9626 /* If called recursively during a menu update, do nothing. This can
9627 happen when, for instance, an activate-menubar-hook causes a
9628 redisplay. */
9629 if (inhibit_menubar_update)
9630 return hooks_run;
9631
9632 window = FRAME_SELECTED_WINDOW (f);
9633 w = XWINDOW (window);
9634
9635 if (FRAME_WINDOW_P (f)
9636 ?
9637 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
9638 || defined (HAVE_NS) || defined (USE_GTK)
9639 FRAME_EXTERNAL_MENU_BAR (f)
9640 #else
9641 FRAME_MENU_BAR_LINES (f) > 0
9642 #endif
9643 : FRAME_MENU_BAR_LINES (f) > 0)
9644 {
9645 /* If the user has switched buffers or windows, we need to
9646 recompute to reflect the new bindings. But we'll
9647 recompute when update_mode_lines is set too; that means
9648 that people can use force-mode-line-update to request
9649 that the menu bar be recomputed. The adverse effect on
9650 the rest of the redisplay algorithm is about the same as
9651 windows_or_buffers_changed anyway. */
9652 if (windows_or_buffers_changed
9653 /* This used to test w->update_mode_line, but we believe
9654 there is no need to recompute the menu in that case. */
9655 || update_mode_lines
9656 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
9657 < BUF_MODIFF (XBUFFER (w->buffer)))
9658 != !NILP (w->last_had_star))
9659 || ((!NILP (Vtransient_mark_mode)
9660 && !NILP (XBUFFER (w->buffer)->mark_active))
9661 != !NILP (w->region_showing)))
9662 {
9663 struct buffer *prev = current_buffer;
9664 int count = SPECPDL_INDEX ();
9665
9666 specbind (Qinhibit_menubar_update, Qt);
9667
9668 set_buffer_internal_1 (XBUFFER (w->buffer));
9669 if (save_match_data)
9670 record_unwind_save_match_data ();
9671 if (NILP (Voverriding_local_map_menu_flag))
9672 {
9673 specbind (Qoverriding_terminal_local_map, Qnil);
9674 specbind (Qoverriding_local_map, Qnil);
9675 }
9676
9677 if (!hooks_run)
9678 {
9679 /* Run the Lucid hook. */
9680 safe_run_hooks (Qactivate_menubar_hook);
9681
9682 /* If it has changed current-menubar from previous value,
9683 really recompute the menu-bar from the value. */
9684 if (! NILP (Vlucid_menu_bar_dirty_flag))
9685 call0 (Qrecompute_lucid_menubar);
9686
9687 safe_run_hooks (Qmenu_bar_update_hook);
9688
9689 hooks_run = 1;
9690 }
9691
9692 XSETFRAME (Vmenu_updating_frame, f);
9693 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
9694
9695 /* Redisplay the menu bar in case we changed it. */
9696 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
9697 || defined (HAVE_NS) || defined (USE_GTK)
9698 if (FRAME_WINDOW_P (f))
9699 {
9700 #if defined (HAVE_NS)
9701 /* All frames on Mac OS share the same menubar. So only
9702 the selected frame should be allowed to set it. */
9703 if (f == SELECTED_FRAME ())
9704 #endif
9705 set_frame_menubar (f, 0, 0);
9706 }
9707 else
9708 /* On a terminal screen, the menu bar is an ordinary screen
9709 line, and this makes it get updated. */
9710 w->update_mode_line = Qt;
9711 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
9712 /* In the non-toolkit version, the menu bar is an ordinary screen
9713 line, and this makes it get updated. */
9714 w->update_mode_line = Qt;
9715 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
9716
9717 unbind_to (count, Qnil);
9718 set_buffer_internal_1 (prev);
9719 }
9720 }
9721
9722 return hooks_run;
9723 }
9724
9725
9726 \f
9727 /***********************************************************************
9728 Output Cursor
9729 ***********************************************************************/
9730
9731 #ifdef HAVE_WINDOW_SYSTEM
9732
9733 /* EXPORT:
9734 Nominal cursor position -- where to draw output.
9735 HPOS and VPOS are window relative glyph matrix coordinates.
9736 X and Y are window relative pixel coordinates. */
9737
9738 struct cursor_pos output_cursor;
9739
9740
9741 /* EXPORT:
9742 Set the global variable output_cursor to CURSOR. All cursor
9743 positions are relative to updated_window. */
9744
9745 void
9746 set_output_cursor (cursor)
9747 struct cursor_pos *cursor;
9748 {
9749 output_cursor.hpos = cursor->hpos;
9750 output_cursor.vpos = cursor->vpos;
9751 output_cursor.x = cursor->x;
9752 output_cursor.y = cursor->y;
9753 }
9754
9755
9756 /* EXPORT for RIF:
9757 Set a nominal cursor position.
9758
9759 HPOS and VPOS are column/row positions in a window glyph matrix. X
9760 and Y are window text area relative pixel positions.
9761
9762 If this is done during an update, updated_window will contain the
9763 window that is being updated and the position is the future output
9764 cursor position for that window. If updated_window is null, use
9765 selected_window and display the cursor at the given position. */
9766
9767 void
9768 x_cursor_to (vpos, hpos, y, x)
9769 int vpos, hpos, y, x;
9770 {
9771 struct window *w;
9772
9773 /* If updated_window is not set, work on selected_window. */
9774 if (updated_window)
9775 w = updated_window;
9776 else
9777 w = XWINDOW (selected_window);
9778
9779 /* Set the output cursor. */
9780 output_cursor.hpos = hpos;
9781 output_cursor.vpos = vpos;
9782 output_cursor.x = x;
9783 output_cursor.y = y;
9784
9785 /* If not called as part of an update, really display the cursor.
9786 This will also set the cursor position of W. */
9787 if (updated_window == NULL)
9788 {
9789 BLOCK_INPUT;
9790 display_and_set_cursor (w, 1, hpos, vpos, x, y);
9791 if (FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
9792 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (SELECTED_FRAME ());
9793 UNBLOCK_INPUT;
9794 }
9795 }
9796
9797 #endif /* HAVE_WINDOW_SYSTEM */
9798
9799 \f
9800 /***********************************************************************
9801 Tool-bars
9802 ***********************************************************************/
9803
9804 #ifdef HAVE_WINDOW_SYSTEM
9805
9806 /* Where the mouse was last time we reported a mouse event. */
9807
9808 FRAME_PTR last_mouse_frame;
9809
9810 /* Tool-bar item index of the item on which a mouse button was pressed
9811 or -1. */
9812
9813 int last_tool_bar_item;
9814
9815
9816 static Lisp_Object
9817 update_tool_bar_unwind (frame)
9818 Lisp_Object frame;
9819 {
9820 selected_frame = frame;
9821 return Qnil;
9822 }
9823
9824 /* Update the tool-bar item list for frame F. This has to be done
9825 before we start to fill in any display lines. Called from
9826 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
9827 and restore it here. */
9828
9829 static void
9830 update_tool_bar (f, save_match_data)
9831 struct frame *f;
9832 int save_match_data;
9833 {
9834 #if defined (USE_GTK) || defined (HAVE_NS) || USE_MAC_TOOLBAR
9835 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
9836 #else
9837 int do_update = WINDOWP (f->tool_bar_window)
9838 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0;
9839 #endif
9840
9841 if (do_update)
9842 {
9843 Lisp_Object window;
9844 struct window *w;
9845
9846 window = FRAME_SELECTED_WINDOW (f);
9847 w = XWINDOW (window);
9848
9849 /* If the user has switched buffers or windows, we need to
9850 recompute to reflect the new bindings. But we'll
9851 recompute when update_mode_lines is set too; that means
9852 that people can use force-mode-line-update to request
9853 that the menu bar be recomputed. The adverse effect on
9854 the rest of the redisplay algorithm is about the same as
9855 windows_or_buffers_changed anyway. */
9856 if (windows_or_buffers_changed
9857 || !NILP (w->update_mode_line)
9858 || update_mode_lines
9859 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
9860 < BUF_MODIFF (XBUFFER (w->buffer)))
9861 != !NILP (w->last_had_star))
9862 || ((!NILP (Vtransient_mark_mode)
9863 && !NILP (XBUFFER (w->buffer)->mark_active))
9864 != !NILP (w->region_showing)))
9865 {
9866 struct buffer *prev = current_buffer;
9867 int count = SPECPDL_INDEX ();
9868 Lisp_Object frame, new_tool_bar;
9869 int new_n_tool_bar;
9870 struct gcpro gcpro1;
9871
9872 /* Set current_buffer to the buffer of the selected
9873 window of the frame, so that we get the right local
9874 keymaps. */
9875 set_buffer_internal_1 (XBUFFER (w->buffer));
9876
9877 /* Save match data, if we must. */
9878 if (save_match_data)
9879 record_unwind_save_match_data ();
9880
9881 /* Make sure that we don't accidentally use bogus keymaps. */
9882 if (NILP (Voverriding_local_map_menu_flag))
9883 {
9884 specbind (Qoverriding_terminal_local_map, Qnil);
9885 specbind (Qoverriding_local_map, Qnil);
9886 }
9887
9888 GCPRO1 (new_tool_bar);
9889
9890 /* We must temporarily set the selected frame to this frame
9891 before calling tool_bar_items, because the calculation of
9892 the tool-bar keymap uses the selected frame (see
9893 `tool-bar-make-keymap' in tool-bar.el). */
9894 record_unwind_protect (update_tool_bar_unwind, selected_frame);
9895 XSETFRAME (frame, f);
9896 selected_frame = frame;
9897
9898 /* Build desired tool-bar items from keymaps. */
9899 new_tool_bar = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
9900 &new_n_tool_bar);
9901
9902 /* Redisplay the tool-bar if we changed it. */
9903 if (new_n_tool_bar != f->n_tool_bar_items
9904 || NILP (Fequal (new_tool_bar, f->tool_bar_items)))
9905 {
9906 /* Redisplay that happens asynchronously due to an expose event
9907 may access f->tool_bar_items. Make sure we update both
9908 variables within BLOCK_INPUT so no such event interrupts. */
9909 BLOCK_INPUT;
9910 f->tool_bar_items = new_tool_bar;
9911 f->n_tool_bar_items = new_n_tool_bar;
9912 w->update_mode_line = Qt;
9913 UNBLOCK_INPUT;
9914 }
9915
9916 UNGCPRO;
9917
9918 unbind_to (count, Qnil);
9919 set_buffer_internal_1 (prev);
9920 }
9921 }
9922 }
9923
9924
9925 /* Set F->desired_tool_bar_string to a Lisp string representing frame
9926 F's desired tool-bar contents. F->tool_bar_items must have
9927 been set up previously by calling prepare_menu_bars. */
9928
9929 static void
9930 build_desired_tool_bar_string (f)
9931 struct frame *f;
9932 {
9933 int i, size, size_needed;
9934 struct gcpro gcpro1, gcpro2, gcpro3;
9935 Lisp_Object image, plist, props;
9936
9937 image = plist = props = Qnil;
9938 GCPRO3 (image, plist, props);
9939
9940 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
9941 Otherwise, make a new string. */
9942
9943 /* The size of the string we might be able to reuse. */
9944 size = (STRINGP (f->desired_tool_bar_string)
9945 ? SCHARS (f->desired_tool_bar_string)
9946 : 0);
9947
9948 /* We need one space in the string for each image. */
9949 size_needed = f->n_tool_bar_items;
9950
9951 /* Reuse f->desired_tool_bar_string, if possible. */
9952 if (size < size_needed || NILP (f->desired_tool_bar_string))
9953 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
9954 make_number (' '));
9955 else
9956 {
9957 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
9958 Fremove_text_properties (make_number (0), make_number (size),
9959 props, f->desired_tool_bar_string);
9960 }
9961
9962 /* Put a `display' property on the string for the images to display,
9963 put a `menu_item' property on tool-bar items with a value that
9964 is the index of the item in F's tool-bar item vector. */
9965 for (i = 0; i < f->n_tool_bar_items; ++i)
9966 {
9967 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
9968
9969 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
9970 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
9971 int hmargin, vmargin, relief, idx, end;
9972 extern Lisp_Object QCrelief, QCmargin, QCconversion;
9973
9974 /* If image is a vector, choose the image according to the
9975 button state. */
9976 image = PROP (TOOL_BAR_ITEM_IMAGES);
9977 if (VECTORP (image))
9978 {
9979 if (enabled_p)
9980 idx = (selected_p
9981 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
9982 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
9983 else
9984 idx = (selected_p
9985 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
9986 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
9987
9988 xassert (ASIZE (image) >= idx);
9989 image = AREF (image, idx);
9990 }
9991 else
9992 idx = -1;
9993
9994 /* Ignore invalid image specifications. */
9995 if (!valid_image_p (image))
9996 continue;
9997
9998 /* Display the tool-bar button pressed, or depressed. */
9999 plist = Fcopy_sequence (XCDR (image));
10000
10001 /* Compute margin and relief to draw. */
10002 relief = (tool_bar_button_relief >= 0
10003 ? tool_bar_button_relief
10004 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
10005 hmargin = vmargin = relief;
10006
10007 if (INTEGERP (Vtool_bar_button_margin)
10008 && XINT (Vtool_bar_button_margin) > 0)
10009 {
10010 hmargin += XFASTINT (Vtool_bar_button_margin);
10011 vmargin += XFASTINT (Vtool_bar_button_margin);
10012 }
10013 else if (CONSP (Vtool_bar_button_margin))
10014 {
10015 if (INTEGERP (XCAR (Vtool_bar_button_margin))
10016 && XINT (XCAR (Vtool_bar_button_margin)) > 0)
10017 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
10018
10019 if (INTEGERP (XCDR (Vtool_bar_button_margin))
10020 && XINT (XCDR (Vtool_bar_button_margin)) > 0)
10021 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
10022 }
10023
10024 if (auto_raise_tool_bar_buttons_p)
10025 {
10026 /* Add a `:relief' property to the image spec if the item is
10027 selected. */
10028 if (selected_p)
10029 {
10030 plist = Fplist_put (plist, QCrelief, make_number (-relief));
10031 hmargin -= relief;
10032 vmargin -= relief;
10033 }
10034 }
10035 else
10036 {
10037 /* If image is selected, display it pressed, i.e. with a
10038 negative relief. If it's not selected, display it with a
10039 raised relief. */
10040 plist = Fplist_put (plist, QCrelief,
10041 (selected_p
10042 ? make_number (-relief)
10043 : make_number (relief)));
10044 hmargin -= relief;
10045 vmargin -= relief;
10046 }
10047
10048 /* Put a margin around the image. */
10049 if (hmargin || vmargin)
10050 {
10051 if (hmargin == vmargin)
10052 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
10053 else
10054 plist = Fplist_put (plist, QCmargin,
10055 Fcons (make_number (hmargin),
10056 make_number (vmargin)));
10057 }
10058
10059 /* If button is not enabled, and we don't have special images
10060 for the disabled state, make the image appear disabled by
10061 applying an appropriate algorithm to it. */
10062 if (!enabled_p && idx < 0)
10063 plist = Fplist_put (plist, QCconversion, Qdisabled);
10064
10065 /* Put a `display' text property on the string for the image to
10066 display. Put a `menu-item' property on the string that gives
10067 the start of this item's properties in the tool-bar items
10068 vector. */
10069 image = Fcons (Qimage, plist);
10070 props = list4 (Qdisplay, image,
10071 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
10072
10073 /* Let the last image hide all remaining spaces in the tool bar
10074 string. The string can be longer than needed when we reuse a
10075 previous string. */
10076 if (i + 1 == f->n_tool_bar_items)
10077 end = SCHARS (f->desired_tool_bar_string);
10078 else
10079 end = i + 1;
10080 Fadd_text_properties (make_number (i), make_number (end),
10081 props, f->desired_tool_bar_string);
10082 #undef PROP
10083 }
10084
10085 UNGCPRO;
10086 }
10087
10088
10089 /* Display one line of the tool-bar of frame IT->f.
10090
10091 HEIGHT specifies the desired height of the tool-bar line.
10092 If the actual height of the glyph row is less than HEIGHT, the
10093 row's height is increased to HEIGHT, and the icons are centered
10094 vertically in the new height.
10095
10096 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
10097 count a final empty row in case the tool-bar width exactly matches
10098 the window width.
10099 */
10100
10101 static void
10102 display_tool_bar_line (it, height)
10103 struct it *it;
10104 int height;
10105 {
10106 struct glyph_row *row = it->glyph_row;
10107 int max_x = it->last_visible_x;
10108 struct glyph *last;
10109
10110 prepare_desired_row (row);
10111 row->y = it->current_y;
10112
10113 /* Note that this isn't made use of if the face hasn't a box,
10114 so there's no need to check the face here. */
10115 it->start_of_box_run_p = 1;
10116
10117 while (it->current_x < max_x)
10118 {
10119 int x, n_glyphs_before, i, nglyphs;
10120 struct it it_before;
10121
10122 /* Get the next display element. */
10123 if (!get_next_display_element (it))
10124 {
10125 /* Don't count empty row if we are counting needed tool-bar lines. */
10126 if (height < 0 && !it->hpos)
10127 return;
10128 break;
10129 }
10130
10131 /* Produce glyphs. */
10132 n_glyphs_before = row->used[TEXT_AREA];
10133 it_before = *it;
10134
10135 PRODUCE_GLYPHS (it);
10136
10137 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
10138 i = 0;
10139 x = it_before.current_x;
10140 while (i < nglyphs)
10141 {
10142 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
10143
10144 if (x + glyph->pixel_width > max_x)
10145 {
10146 /* Glyph doesn't fit on line. Backtrack. */
10147 row->used[TEXT_AREA] = n_glyphs_before;
10148 *it = it_before;
10149 /* If this is the only glyph on this line, it will never fit on the
10150 toolbar, so skip it. But ensure there is at least one glyph,
10151 so we don't accidentally disable the tool-bar. */
10152 if (n_glyphs_before == 0
10153 && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
10154 break;
10155 goto out;
10156 }
10157
10158 ++it->hpos;
10159 x += glyph->pixel_width;
10160 ++i;
10161 }
10162
10163 /* Stop at line ends. */
10164 if (ITERATOR_AT_END_OF_LINE_P (it))
10165 break;
10166
10167 set_iterator_to_next (it, 1);
10168 }
10169
10170 out:;
10171
10172 row->displays_text_p = row->used[TEXT_AREA] != 0;
10173
10174 /* Use default face for the border below the tool bar.
10175
10176 FIXME: When auto-resize-tool-bars is grow-only, there is
10177 no additional border below the possibly empty tool-bar lines.
10178 So to make the extra empty lines look "normal", we have to
10179 use the tool-bar face for the border too. */
10180 if (!row->displays_text_p && !EQ (Vauto_resize_tool_bars, Qgrow_only))
10181 it->face_id = DEFAULT_FACE_ID;
10182
10183 extend_face_to_end_of_line (it);
10184 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
10185 last->right_box_line_p = 1;
10186 if (last == row->glyphs[TEXT_AREA])
10187 last->left_box_line_p = 1;
10188
10189 /* Make line the desired height and center it vertically. */
10190 if ((height -= it->max_ascent + it->max_descent) > 0)
10191 {
10192 /* Don't add more than one line height. */
10193 height %= FRAME_LINE_HEIGHT (it->f);
10194 it->max_ascent += height / 2;
10195 it->max_descent += (height + 1) / 2;
10196 }
10197
10198 compute_line_metrics (it);
10199
10200 /* If line is empty, make it occupy the rest of the tool-bar. */
10201 if (!row->displays_text_p)
10202 {
10203 row->height = row->phys_height = it->last_visible_y - row->y;
10204 row->visible_height = row->height;
10205 row->ascent = row->phys_ascent = 0;
10206 row->extra_line_spacing = 0;
10207 }
10208
10209 row->full_width_p = 1;
10210 row->continued_p = 0;
10211 row->truncated_on_left_p = 0;
10212 row->truncated_on_right_p = 0;
10213
10214 it->current_x = it->hpos = 0;
10215 it->current_y += row->height;
10216 ++it->vpos;
10217 ++it->glyph_row;
10218 }
10219
10220
10221 /* Max tool-bar height. */
10222
10223 #define MAX_FRAME_TOOL_BAR_HEIGHT(f) \
10224 ((FRAME_LINE_HEIGHT (f) * FRAME_LINES (f)))
10225
10226 /* Value is the number of screen lines needed to make all tool-bar
10227 items of frame F visible. The number of actual rows needed is
10228 returned in *N_ROWS if non-NULL. */
10229
10230 static int
10231 tool_bar_lines_needed (f, n_rows)
10232 struct frame *f;
10233 int *n_rows;
10234 {
10235 struct window *w = XWINDOW (f->tool_bar_window);
10236 struct it it;
10237 /* tool_bar_lines_needed is called from redisplay_tool_bar after building
10238 the desired matrix, so use (unused) mode-line row as temporary row to
10239 avoid destroying the first tool-bar row. */
10240 struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
10241
10242 /* Initialize an iterator for iteration over
10243 F->desired_tool_bar_string in the tool-bar window of frame F. */
10244 init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
10245 it.first_visible_x = 0;
10246 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
10247 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
10248
10249 while (!ITERATOR_AT_END_P (&it))
10250 {
10251 clear_glyph_row (temp_row);
10252 it.glyph_row = temp_row;
10253 display_tool_bar_line (&it, -1);
10254 }
10255 clear_glyph_row (temp_row);
10256
10257 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
10258 if (n_rows)
10259 *n_rows = it.vpos > 0 ? it.vpos : -1;
10260
10261 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
10262 }
10263
10264
10265 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
10266 0, 1, 0,
10267 doc: /* Return the number of lines occupied by the tool bar of FRAME. */)
10268 (frame)
10269 Lisp_Object frame;
10270 {
10271 struct frame *f;
10272 struct window *w;
10273 int nlines = 0;
10274
10275 if (NILP (frame))
10276 frame = selected_frame;
10277 else
10278 CHECK_FRAME (frame);
10279 f = XFRAME (frame);
10280
10281 if (WINDOWP (f->tool_bar_window)
10282 || (w = XWINDOW (f->tool_bar_window),
10283 WINDOW_TOTAL_LINES (w) > 0))
10284 {
10285 update_tool_bar (f, 1);
10286 if (f->n_tool_bar_items)
10287 {
10288 build_desired_tool_bar_string (f);
10289 nlines = tool_bar_lines_needed (f, NULL);
10290 }
10291 }
10292
10293 return make_number (nlines);
10294 }
10295
10296
10297 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
10298 height should be changed. */
10299
10300 static int
10301 redisplay_tool_bar (f)
10302 struct frame *f;
10303 {
10304 struct window *w;
10305 struct it it;
10306 struct glyph_row *row;
10307
10308 #if defined (USE_GTK) || defined (HAVE_NS) || USE_MAC_TOOLBAR
10309 if (FRAME_EXTERNAL_TOOL_BAR (f))
10310 update_frame_tool_bar (f);
10311 return 0;
10312 #endif
10313
10314 /* If frame hasn't a tool-bar window or if it is zero-height, don't
10315 do anything. This means you must start with tool-bar-lines
10316 non-zero to get the auto-sizing effect. Or in other words, you
10317 can turn off tool-bars by specifying tool-bar-lines zero. */
10318 if (!WINDOWP (f->tool_bar_window)
10319 || (w = XWINDOW (f->tool_bar_window),
10320 WINDOW_TOTAL_LINES (w) == 0))
10321 return 0;
10322
10323 /* Set up an iterator for the tool-bar window. */
10324 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
10325 it.first_visible_x = 0;
10326 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
10327 row = it.glyph_row;
10328
10329 /* Build a string that represents the contents of the tool-bar. */
10330 build_desired_tool_bar_string (f);
10331 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
10332
10333 if (f->n_tool_bar_rows == 0)
10334 {
10335 int nlines;
10336
10337 if ((nlines = tool_bar_lines_needed (f, &f->n_tool_bar_rows),
10338 nlines != WINDOW_TOTAL_LINES (w)))
10339 {
10340 extern Lisp_Object Qtool_bar_lines;
10341 Lisp_Object frame;
10342 int old_height = WINDOW_TOTAL_LINES (w);
10343
10344 XSETFRAME (frame, f);
10345 Fmodify_frame_parameters (frame,
10346 Fcons (Fcons (Qtool_bar_lines,
10347 make_number (nlines)),
10348 Qnil));
10349 if (WINDOW_TOTAL_LINES (w) != old_height)
10350 {
10351 clear_glyph_matrix (w->desired_matrix);
10352 fonts_changed_p = 1;
10353 return 1;
10354 }
10355 }
10356 }
10357
10358 /* Display as many lines as needed to display all tool-bar items. */
10359
10360 if (f->n_tool_bar_rows > 0)
10361 {
10362 int border, rows, height, extra;
10363
10364 if (INTEGERP (Vtool_bar_border))
10365 border = XINT (Vtool_bar_border);
10366 else if (EQ (Vtool_bar_border, Qinternal_border_width))
10367 border = FRAME_INTERNAL_BORDER_WIDTH (f);
10368 else if (EQ (Vtool_bar_border, Qborder_width))
10369 border = f->border_width;
10370 else
10371 border = 0;
10372 if (border < 0)
10373 border = 0;
10374
10375 rows = f->n_tool_bar_rows;
10376 height = max (1, (it.last_visible_y - border) / rows);
10377 extra = it.last_visible_y - border - height * rows;
10378
10379 while (it.current_y < it.last_visible_y)
10380 {
10381 int h = 0;
10382 if (extra > 0 && rows-- > 0)
10383 {
10384 h = (extra + rows - 1) / rows;
10385 extra -= h;
10386 }
10387 display_tool_bar_line (&it, height + h);
10388 }
10389 }
10390 else
10391 {
10392 while (it.current_y < it.last_visible_y)
10393 display_tool_bar_line (&it, 0);
10394 }
10395
10396 /* It doesn't make much sense to try scrolling in the tool-bar
10397 window, so don't do it. */
10398 w->desired_matrix->no_scrolling_p = 1;
10399 w->must_be_updated_p = 1;
10400
10401 if (!NILP (Vauto_resize_tool_bars))
10402 {
10403 int max_tool_bar_height = MAX_FRAME_TOOL_BAR_HEIGHT (f);
10404 int change_height_p = 0;
10405
10406 /* If we couldn't display everything, change the tool-bar's
10407 height if there is room for more. */
10408 if (IT_STRING_CHARPOS (it) < it.end_charpos
10409 && it.current_y < max_tool_bar_height)
10410 change_height_p = 1;
10411
10412 row = it.glyph_row - 1;
10413
10414 /* If there are blank lines at the end, except for a partially
10415 visible blank line at the end that is smaller than
10416 FRAME_LINE_HEIGHT, change the tool-bar's height. */
10417 if (!row->displays_text_p
10418 && row->height >= FRAME_LINE_HEIGHT (f))
10419 change_height_p = 1;
10420
10421 /* If row displays tool-bar items, but is partially visible,
10422 change the tool-bar's height. */
10423 if (row->displays_text_p
10424 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y
10425 && MATRIX_ROW_BOTTOM_Y (row) < max_tool_bar_height)
10426 change_height_p = 1;
10427
10428 /* Resize windows as needed by changing the `tool-bar-lines'
10429 frame parameter. */
10430 if (change_height_p)
10431 {
10432 extern Lisp_Object Qtool_bar_lines;
10433 Lisp_Object frame;
10434 int old_height = WINDOW_TOTAL_LINES (w);
10435 int nrows;
10436 int nlines = tool_bar_lines_needed (f, &nrows);
10437
10438 change_height_p = ((EQ (Vauto_resize_tool_bars, Qgrow_only)
10439 && !f->minimize_tool_bar_window_p)
10440 ? (nlines > old_height)
10441 : (nlines != old_height));
10442 f->minimize_tool_bar_window_p = 0;
10443
10444 if (change_height_p)
10445 {
10446 XSETFRAME (frame, f);
10447 Fmodify_frame_parameters (frame,
10448 Fcons (Fcons (Qtool_bar_lines,
10449 make_number (nlines)),
10450 Qnil));
10451 if (WINDOW_TOTAL_LINES (w) != old_height)
10452 {
10453 clear_glyph_matrix (w->desired_matrix);
10454 f->n_tool_bar_rows = nrows;
10455 fonts_changed_p = 1;
10456 return 1;
10457 }
10458 }
10459 }
10460 }
10461
10462 f->minimize_tool_bar_window_p = 0;
10463 return 0;
10464 }
10465
10466
10467 /* Get information about the tool-bar item which is displayed in GLYPH
10468 on frame F. Return in *PROP_IDX the index where tool-bar item
10469 properties start in F->tool_bar_items. Value is zero if
10470 GLYPH doesn't display a tool-bar item. */
10471
10472 static int
10473 tool_bar_item_info (f, glyph, prop_idx)
10474 struct frame *f;
10475 struct glyph *glyph;
10476 int *prop_idx;
10477 {
10478 Lisp_Object prop;
10479 int success_p;
10480 int charpos;
10481
10482 /* This function can be called asynchronously, which means we must
10483 exclude any possibility that Fget_text_property signals an
10484 error. */
10485 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
10486 charpos = max (0, charpos);
10487
10488 /* Get the text property `menu-item' at pos. The value of that
10489 property is the start index of this item's properties in
10490 F->tool_bar_items. */
10491 prop = Fget_text_property (make_number (charpos),
10492 Qmenu_item, f->current_tool_bar_string);
10493 if (INTEGERP (prop))
10494 {
10495 *prop_idx = XINT (prop);
10496 success_p = 1;
10497 }
10498 else
10499 success_p = 0;
10500
10501 return success_p;
10502 }
10503
10504 \f
10505 /* Get information about the tool-bar item at position X/Y on frame F.
10506 Return in *GLYPH a pointer to the glyph of the tool-bar item in
10507 the current matrix of the tool-bar window of F, or NULL if not
10508 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
10509 item in F->tool_bar_items. Value is
10510
10511 -1 if X/Y is not on a tool-bar item
10512 0 if X/Y is on the same item that was highlighted before.
10513 1 otherwise. */
10514
10515 static int
10516 get_tool_bar_item (f, x, y, glyph, hpos, vpos, prop_idx)
10517 struct frame *f;
10518 int x, y;
10519 struct glyph **glyph;
10520 int *hpos, *vpos, *prop_idx;
10521 {
10522 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10523 struct window *w = XWINDOW (f->tool_bar_window);
10524 int area;
10525
10526 /* Find the glyph under X/Y. */
10527 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
10528 if (*glyph == NULL)
10529 return -1;
10530
10531 /* Get the start of this tool-bar item's properties in
10532 f->tool_bar_items. */
10533 if (!tool_bar_item_info (f, *glyph, prop_idx))
10534 return -1;
10535
10536 /* Is mouse on the highlighted item? */
10537 if (EQ (f->tool_bar_window, dpyinfo->mouse_face_window)
10538 && *vpos >= dpyinfo->mouse_face_beg_row
10539 && *vpos <= dpyinfo->mouse_face_end_row
10540 && (*vpos > dpyinfo->mouse_face_beg_row
10541 || *hpos >= dpyinfo->mouse_face_beg_col)
10542 && (*vpos < dpyinfo->mouse_face_end_row
10543 || *hpos < dpyinfo->mouse_face_end_col
10544 || dpyinfo->mouse_face_past_end))
10545 return 0;
10546
10547 return 1;
10548 }
10549
10550
10551 /* EXPORT:
10552 Handle mouse button event on the tool-bar of frame F, at
10553 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
10554 0 for button release. MODIFIERS is event modifiers for button
10555 release. */
10556
10557 void
10558 handle_tool_bar_click (f, x, y, down_p, modifiers)
10559 struct frame *f;
10560 int x, y, down_p;
10561 unsigned int modifiers;
10562 {
10563 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10564 struct window *w = XWINDOW (f->tool_bar_window);
10565 int hpos, vpos, prop_idx;
10566 struct glyph *glyph;
10567 Lisp_Object enabled_p;
10568
10569 /* If not on the highlighted tool-bar item, return. */
10570 frame_to_window_pixel_xy (w, &x, &y);
10571 if (get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
10572 return;
10573
10574 /* If item is disabled, do nothing. */
10575 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
10576 if (NILP (enabled_p))
10577 return;
10578
10579 if (down_p)
10580 {
10581 /* Show item in pressed state. */
10582 show_mouse_face (dpyinfo, DRAW_IMAGE_SUNKEN);
10583 dpyinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
10584 last_tool_bar_item = prop_idx;
10585 }
10586 else
10587 {
10588 Lisp_Object key, frame;
10589 struct input_event event;
10590 EVENT_INIT (event);
10591
10592 /* Show item in released state. */
10593 show_mouse_face (dpyinfo, DRAW_IMAGE_RAISED);
10594 dpyinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
10595
10596 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
10597
10598 XSETFRAME (frame, f);
10599 event.kind = TOOL_BAR_EVENT;
10600 event.frame_or_window = frame;
10601 event.arg = frame;
10602 kbd_buffer_store_event (&event);
10603
10604 event.kind = TOOL_BAR_EVENT;
10605 event.frame_or_window = frame;
10606 event.arg = key;
10607 event.modifiers = modifiers;
10608 kbd_buffer_store_event (&event);
10609 last_tool_bar_item = -1;
10610 }
10611 }
10612
10613
10614 /* Possibly highlight a tool-bar item on frame F when mouse moves to
10615 tool-bar window-relative coordinates X/Y. Called from
10616 note_mouse_highlight. */
10617
10618 static void
10619 note_tool_bar_highlight (f, x, y)
10620 struct frame *f;
10621 int x, y;
10622 {
10623 Lisp_Object window = f->tool_bar_window;
10624 struct window *w = XWINDOW (window);
10625 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10626 int hpos, vpos;
10627 struct glyph *glyph;
10628 struct glyph_row *row;
10629 int i;
10630 Lisp_Object enabled_p;
10631 int prop_idx;
10632 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
10633 int mouse_down_p, rc;
10634
10635 /* Function note_mouse_highlight is called with negative x(y
10636 values when mouse moves outside of the frame. */
10637 if (x <= 0 || y <= 0)
10638 {
10639 clear_mouse_face (dpyinfo);
10640 return;
10641 }
10642
10643 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
10644 if (rc < 0)
10645 {
10646 /* Not on tool-bar item. */
10647 clear_mouse_face (dpyinfo);
10648 return;
10649 }
10650 else if (rc == 0)
10651 /* On same tool-bar item as before. */
10652 goto set_help_echo;
10653
10654 clear_mouse_face (dpyinfo);
10655
10656 /* Mouse is down, but on different tool-bar item? */
10657 mouse_down_p = (dpyinfo->grabbed
10658 && f == last_mouse_frame
10659 && FRAME_LIVE_P (f));
10660 if (mouse_down_p
10661 && last_tool_bar_item != prop_idx)
10662 return;
10663
10664 dpyinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
10665 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
10666
10667 /* If tool-bar item is not enabled, don't highlight it. */
10668 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
10669 if (!NILP (enabled_p))
10670 {
10671 /* Compute the x-position of the glyph. In front and past the
10672 image is a space. We include this in the highlighted area. */
10673 row = MATRIX_ROW (w->current_matrix, vpos);
10674 for (i = x = 0; i < hpos; ++i)
10675 x += row->glyphs[TEXT_AREA][i].pixel_width;
10676
10677 /* Record this as the current active region. */
10678 dpyinfo->mouse_face_beg_col = hpos;
10679 dpyinfo->mouse_face_beg_row = vpos;
10680 dpyinfo->mouse_face_beg_x = x;
10681 dpyinfo->mouse_face_beg_y = row->y;
10682 dpyinfo->mouse_face_past_end = 0;
10683
10684 dpyinfo->mouse_face_end_col = hpos + 1;
10685 dpyinfo->mouse_face_end_row = vpos;
10686 dpyinfo->mouse_face_end_x = x + glyph->pixel_width;
10687 dpyinfo->mouse_face_end_y = row->y;
10688 dpyinfo->mouse_face_window = window;
10689 dpyinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
10690
10691 /* Display it as active. */
10692 show_mouse_face (dpyinfo, draw);
10693 dpyinfo->mouse_face_image_state = draw;
10694 }
10695
10696 set_help_echo:
10697
10698 /* Set help_echo_string to a help string to display for this tool-bar item.
10699 XTread_socket does the rest. */
10700 help_echo_object = help_echo_window = Qnil;
10701 help_echo_pos = -1;
10702 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
10703 if (NILP (help_echo_string))
10704 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
10705 }
10706
10707 #endif /* HAVE_WINDOW_SYSTEM */
10708
10709
10710 \f
10711 /************************************************************************
10712 Horizontal scrolling
10713 ************************************************************************/
10714
10715 static int hscroll_window_tree P_ ((Lisp_Object));
10716 static int hscroll_windows P_ ((Lisp_Object));
10717
10718 /* For all leaf windows in the window tree rooted at WINDOW, set their
10719 hscroll value so that PT is (i) visible in the window, and (ii) so
10720 that it is not within a certain margin at the window's left and
10721 right border. Value is non-zero if any window's hscroll has been
10722 changed. */
10723
10724 static int
10725 hscroll_window_tree (window)
10726 Lisp_Object window;
10727 {
10728 int hscrolled_p = 0;
10729 int hscroll_relative_p = FLOATP (Vhscroll_step);
10730 int hscroll_step_abs = 0;
10731 double hscroll_step_rel = 0;
10732
10733 if (hscroll_relative_p)
10734 {
10735 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
10736 if (hscroll_step_rel < 0)
10737 {
10738 hscroll_relative_p = 0;
10739 hscroll_step_abs = 0;
10740 }
10741 }
10742 else if (INTEGERP (Vhscroll_step))
10743 {
10744 hscroll_step_abs = XINT (Vhscroll_step);
10745 if (hscroll_step_abs < 0)
10746 hscroll_step_abs = 0;
10747 }
10748 else
10749 hscroll_step_abs = 0;
10750
10751 while (WINDOWP (window))
10752 {
10753 struct window *w = XWINDOW (window);
10754
10755 if (WINDOWP (w->hchild))
10756 hscrolled_p |= hscroll_window_tree (w->hchild);
10757 else if (WINDOWP (w->vchild))
10758 hscrolled_p |= hscroll_window_tree (w->vchild);
10759 else if (w->cursor.vpos >= 0)
10760 {
10761 int h_margin;
10762 int text_area_width;
10763 struct glyph_row *current_cursor_row
10764 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
10765 struct glyph_row *desired_cursor_row
10766 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
10767 struct glyph_row *cursor_row
10768 = (desired_cursor_row->enabled_p
10769 ? desired_cursor_row
10770 : current_cursor_row);
10771
10772 text_area_width = window_box_width (w, TEXT_AREA);
10773
10774 /* Scroll when cursor is inside this scroll margin. */
10775 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
10776
10777 if (!NILP (Fbuffer_local_value (Qauto_hscroll_mode, w->buffer))
10778 && ((XFASTINT (w->hscroll)
10779 && w->cursor.x <= h_margin)
10780 || (cursor_row->enabled_p
10781 && cursor_row->truncated_on_right_p
10782 && (w->cursor.x >= text_area_width - h_margin))))
10783 {
10784 struct it it;
10785 int hscroll;
10786 struct buffer *saved_current_buffer;
10787 int pt;
10788 int wanted_x;
10789
10790 /* Find point in a display of infinite width. */
10791 saved_current_buffer = current_buffer;
10792 current_buffer = XBUFFER (w->buffer);
10793
10794 if (w == XWINDOW (selected_window))
10795 pt = BUF_PT (current_buffer);
10796 else
10797 {
10798 pt = marker_position (w->pointm);
10799 pt = max (BEGV, pt);
10800 pt = min (ZV, pt);
10801 }
10802
10803 /* Move iterator to pt starting at cursor_row->start in
10804 a line with infinite width. */
10805 init_to_row_start (&it, w, cursor_row);
10806 it.last_visible_x = INFINITY;
10807 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
10808 current_buffer = saved_current_buffer;
10809
10810 /* Position cursor in window. */
10811 if (!hscroll_relative_p && hscroll_step_abs == 0)
10812 hscroll = max (0, (it.current_x
10813 - (ITERATOR_AT_END_OF_LINE_P (&it)
10814 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
10815 : (text_area_width / 2))))
10816 / FRAME_COLUMN_WIDTH (it.f);
10817 else if (w->cursor.x >= text_area_width - h_margin)
10818 {
10819 if (hscroll_relative_p)
10820 wanted_x = text_area_width * (1 - hscroll_step_rel)
10821 - h_margin;
10822 else
10823 wanted_x = text_area_width
10824 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
10825 - h_margin;
10826 hscroll
10827 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
10828 }
10829 else
10830 {
10831 if (hscroll_relative_p)
10832 wanted_x = text_area_width * hscroll_step_rel
10833 + h_margin;
10834 else
10835 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
10836 + h_margin;
10837 hscroll
10838 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
10839 }
10840 hscroll = max (hscroll, XFASTINT (w->min_hscroll));
10841
10842 /* Don't call Fset_window_hscroll if value hasn't
10843 changed because it will prevent redisplay
10844 optimizations. */
10845 if (XFASTINT (w->hscroll) != hscroll)
10846 {
10847 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
10848 w->hscroll = make_number (hscroll);
10849 hscrolled_p = 1;
10850 }
10851 }
10852 }
10853
10854 window = w->next;
10855 }
10856
10857 /* Value is non-zero if hscroll of any leaf window has been changed. */
10858 return hscrolled_p;
10859 }
10860
10861
10862 /* Set hscroll so that cursor is visible and not inside horizontal
10863 scroll margins for all windows in the tree rooted at WINDOW. See
10864 also hscroll_window_tree above. Value is non-zero if any window's
10865 hscroll has been changed. If it has, desired matrices on the frame
10866 of WINDOW are cleared. */
10867
10868 static int
10869 hscroll_windows (window)
10870 Lisp_Object window;
10871 {
10872 int hscrolled_p = hscroll_window_tree (window);
10873 if (hscrolled_p)
10874 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
10875 return hscrolled_p;
10876 }
10877
10878
10879 \f
10880 /************************************************************************
10881 Redisplay
10882 ************************************************************************/
10883
10884 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
10885 to a non-zero value. This is sometimes handy to have in a debugger
10886 session. */
10887
10888 #if GLYPH_DEBUG
10889
10890 /* First and last unchanged row for try_window_id. */
10891
10892 int debug_first_unchanged_at_end_vpos;
10893 int debug_last_unchanged_at_beg_vpos;
10894
10895 /* Delta vpos and y. */
10896
10897 int debug_dvpos, debug_dy;
10898
10899 /* Delta in characters and bytes for try_window_id. */
10900
10901 int debug_delta, debug_delta_bytes;
10902
10903 /* Values of window_end_pos and window_end_vpos at the end of
10904 try_window_id. */
10905
10906 EMACS_INT debug_end_pos, debug_end_vpos;
10907
10908 /* Append a string to W->desired_matrix->method. FMT is a printf
10909 format string. A1...A9 are a supplement for a variable-length
10910 argument list. If trace_redisplay_p is non-zero also printf the
10911 resulting string to stderr. */
10912
10913 static void
10914 debug_method_add (w, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9)
10915 struct window *w;
10916 char *fmt;
10917 int a1, a2, a3, a4, a5, a6, a7, a8, a9;
10918 {
10919 char buffer[512];
10920 char *method = w->desired_matrix->method;
10921 int len = strlen (method);
10922 int size = sizeof w->desired_matrix->method;
10923 int remaining = size - len - 1;
10924
10925 sprintf (buffer, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9);
10926 if (len && remaining)
10927 {
10928 method[len] = '|';
10929 --remaining, ++len;
10930 }
10931
10932 strncpy (method + len, buffer, remaining);
10933
10934 if (trace_redisplay_p)
10935 fprintf (stderr, "%p (%s): %s\n",
10936 w,
10937 ((BUFFERP (w->buffer)
10938 && STRINGP (XBUFFER (w->buffer)->name))
10939 ? (char *) SDATA (XBUFFER (w->buffer)->name)
10940 : "no buffer"),
10941 buffer);
10942 }
10943
10944 #endif /* GLYPH_DEBUG */
10945
10946
10947 /* Value is non-zero if all changes in window W, which displays
10948 current_buffer, are in the text between START and END. START is a
10949 buffer position, END is given as a distance from Z. Used in
10950 redisplay_internal for display optimization. */
10951
10952 static INLINE int
10953 text_outside_line_unchanged_p (w, start, end)
10954 struct window *w;
10955 int start, end;
10956 {
10957 int unchanged_p = 1;
10958
10959 /* If text or overlays have changed, see where. */
10960 if (XFASTINT (w->last_modified) < MODIFF
10961 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
10962 {
10963 /* Gap in the line? */
10964 if (GPT < start || Z - GPT < end)
10965 unchanged_p = 0;
10966
10967 /* Changes start in front of the line, or end after it? */
10968 if (unchanged_p
10969 && (BEG_UNCHANGED < start - 1
10970 || END_UNCHANGED < end))
10971 unchanged_p = 0;
10972
10973 /* If selective display, can't optimize if changes start at the
10974 beginning of the line. */
10975 if (unchanged_p
10976 && INTEGERP (current_buffer->selective_display)
10977 && XINT (current_buffer->selective_display) > 0
10978 && (BEG_UNCHANGED < start || GPT <= start))
10979 unchanged_p = 0;
10980
10981 /* If there are overlays at the start or end of the line, these
10982 may have overlay strings with newlines in them. A change at
10983 START, for instance, may actually concern the display of such
10984 overlay strings as well, and they are displayed on different
10985 lines. So, quickly rule out this case. (For the future, it
10986 might be desirable to implement something more telling than
10987 just BEG/END_UNCHANGED.) */
10988 if (unchanged_p)
10989 {
10990 if (BEG + BEG_UNCHANGED == start
10991 && overlay_touches_p (start))
10992 unchanged_p = 0;
10993 if (END_UNCHANGED == end
10994 && overlay_touches_p (Z - end))
10995 unchanged_p = 0;
10996 }
10997 }
10998
10999 return unchanged_p;
11000 }
11001
11002
11003 /* Do a frame update, taking possible shortcuts into account. This is
11004 the main external entry point for redisplay.
11005
11006 If the last redisplay displayed an echo area message and that message
11007 is no longer requested, we clear the echo area or bring back the
11008 mini-buffer if that is in use. */
11009
11010 void
11011 redisplay ()
11012 {
11013 redisplay_internal (0);
11014 }
11015
11016
11017 static Lisp_Object
11018 overlay_arrow_string_or_property (var)
11019 Lisp_Object var;
11020 {
11021 Lisp_Object val;
11022
11023 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
11024 return val;
11025
11026 return Voverlay_arrow_string;
11027 }
11028
11029 /* Return 1 if there are any overlay-arrows in current_buffer. */
11030 static int
11031 overlay_arrow_in_current_buffer_p ()
11032 {
11033 Lisp_Object vlist;
11034
11035 for (vlist = Voverlay_arrow_variable_list;
11036 CONSP (vlist);
11037 vlist = XCDR (vlist))
11038 {
11039 Lisp_Object var = XCAR (vlist);
11040 Lisp_Object val;
11041
11042 if (!SYMBOLP (var))
11043 continue;
11044 val = find_symbol_value (var);
11045 if (MARKERP (val)
11046 && current_buffer == XMARKER (val)->buffer)
11047 return 1;
11048 }
11049 return 0;
11050 }
11051
11052
11053 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
11054 has changed. */
11055
11056 static int
11057 overlay_arrows_changed_p ()
11058 {
11059 Lisp_Object vlist;
11060
11061 for (vlist = Voverlay_arrow_variable_list;
11062 CONSP (vlist);
11063 vlist = XCDR (vlist))
11064 {
11065 Lisp_Object var = XCAR (vlist);
11066 Lisp_Object val, pstr;
11067
11068 if (!SYMBOLP (var))
11069 continue;
11070 val = find_symbol_value (var);
11071 if (!MARKERP (val))
11072 continue;
11073 if (! EQ (COERCE_MARKER (val),
11074 Fget (var, Qlast_arrow_position))
11075 || ! (pstr = overlay_arrow_string_or_property (var),
11076 EQ (pstr, Fget (var, Qlast_arrow_string))))
11077 return 1;
11078 }
11079 return 0;
11080 }
11081
11082 /* Mark overlay arrows to be updated on next redisplay. */
11083
11084 static void
11085 update_overlay_arrows (up_to_date)
11086 int up_to_date;
11087 {
11088 Lisp_Object vlist;
11089
11090 for (vlist = Voverlay_arrow_variable_list;
11091 CONSP (vlist);
11092 vlist = XCDR (vlist))
11093 {
11094 Lisp_Object var = XCAR (vlist);
11095
11096 if (!SYMBOLP (var))
11097 continue;
11098
11099 if (up_to_date > 0)
11100 {
11101 Lisp_Object val = find_symbol_value (var);
11102 Fput (var, Qlast_arrow_position,
11103 COERCE_MARKER (val));
11104 Fput (var, Qlast_arrow_string,
11105 overlay_arrow_string_or_property (var));
11106 }
11107 else if (up_to_date < 0
11108 || !NILP (Fget (var, Qlast_arrow_position)))
11109 {
11110 Fput (var, Qlast_arrow_position, Qt);
11111 Fput (var, Qlast_arrow_string, Qt);
11112 }
11113 }
11114 }
11115
11116
11117 /* Return overlay arrow string to display at row.
11118 Return integer (bitmap number) for arrow bitmap in left fringe.
11119 Return nil if no overlay arrow. */
11120
11121 static Lisp_Object
11122 overlay_arrow_at_row (it, row)
11123 struct it *it;
11124 struct glyph_row *row;
11125 {
11126 Lisp_Object vlist;
11127
11128 for (vlist = Voverlay_arrow_variable_list;
11129 CONSP (vlist);
11130 vlist = XCDR (vlist))
11131 {
11132 Lisp_Object var = XCAR (vlist);
11133 Lisp_Object val;
11134
11135 if (!SYMBOLP (var))
11136 continue;
11137
11138 val = find_symbol_value (var);
11139
11140 if (MARKERP (val)
11141 && current_buffer == XMARKER (val)->buffer
11142 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
11143 {
11144 if (FRAME_WINDOW_P (it->f)
11145 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
11146 {
11147 #ifdef HAVE_WINDOW_SYSTEM
11148 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
11149 {
11150 int fringe_bitmap;
11151 if ((fringe_bitmap = lookup_fringe_bitmap (val)) != 0)
11152 return make_number (fringe_bitmap);
11153 }
11154 #endif
11155 return make_number (-1); /* Use default arrow bitmap */
11156 }
11157 return overlay_arrow_string_or_property (var);
11158 }
11159 }
11160
11161 return Qnil;
11162 }
11163
11164 /* Return 1 if point moved out of or into a composition. Otherwise
11165 return 0. PREV_BUF and PREV_PT are the last point buffer and
11166 position. BUF and PT are the current point buffer and position. */
11167
11168 int
11169 check_point_in_composition (prev_buf, prev_pt, buf, pt)
11170 struct buffer *prev_buf, *buf;
11171 int prev_pt, pt;
11172 {
11173 EMACS_INT start, end;
11174 Lisp_Object prop;
11175 Lisp_Object buffer;
11176
11177 XSETBUFFER (buffer, buf);
11178 /* Check a composition at the last point if point moved within the
11179 same buffer. */
11180 if (prev_buf == buf)
11181 {
11182 if (prev_pt == pt)
11183 /* Point didn't move. */
11184 return 0;
11185
11186 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
11187 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
11188 && COMPOSITION_VALID_P (start, end, prop)
11189 && start < prev_pt && end > prev_pt)
11190 /* The last point was within the composition. Return 1 iff
11191 point moved out of the composition. */
11192 return (pt <= start || pt >= end);
11193 }
11194
11195 /* Check a composition at the current point. */
11196 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
11197 && find_composition (pt, -1, &start, &end, &prop, buffer)
11198 && COMPOSITION_VALID_P (start, end, prop)
11199 && start < pt && end > pt);
11200 }
11201
11202
11203 /* Reconsider the setting of B->clip_changed which is displayed
11204 in window W. */
11205
11206 static INLINE void
11207 reconsider_clip_changes (w, b)
11208 struct window *w;
11209 struct buffer *b;
11210 {
11211 if (b->clip_changed
11212 && !NILP (w->window_end_valid)
11213 && w->current_matrix->buffer == b
11214 && w->current_matrix->zv == BUF_ZV (b)
11215 && w->current_matrix->begv == BUF_BEGV (b))
11216 b->clip_changed = 0;
11217
11218 /* If display wasn't paused, and W is not a tool bar window, see if
11219 point has been moved into or out of a composition. In that case,
11220 we set b->clip_changed to 1 to force updating the screen. If
11221 b->clip_changed has already been set to 1, we can skip this
11222 check. */
11223 if (!b->clip_changed
11224 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
11225 {
11226 int pt;
11227
11228 if (w == XWINDOW (selected_window))
11229 pt = BUF_PT (current_buffer);
11230 else
11231 pt = marker_position (w->pointm);
11232
11233 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
11234 || pt != XINT (w->last_point))
11235 && check_point_in_composition (w->current_matrix->buffer,
11236 XINT (w->last_point),
11237 XBUFFER (w->buffer), pt))
11238 b->clip_changed = 1;
11239 }
11240 }
11241 \f
11242
11243 /* Select FRAME to forward the values of frame-local variables into C
11244 variables so that the redisplay routines can access those values
11245 directly. */
11246
11247 static void
11248 select_frame_for_redisplay (frame)
11249 Lisp_Object frame;
11250 {
11251 Lisp_Object tail, symbol, val;
11252 Lisp_Object old = selected_frame;
11253 struct Lisp_Symbol *sym;
11254
11255 xassert (FRAMEP (frame) && FRAME_LIVE_P (XFRAME (frame)));
11256
11257 selected_frame = frame;
11258
11259 do
11260 {
11261 for (tail = XFRAME (frame)->param_alist; CONSP (tail); tail = XCDR (tail))
11262 if (CONSP (XCAR (tail))
11263 && (symbol = XCAR (XCAR (tail)),
11264 SYMBOLP (symbol))
11265 && (sym = indirect_variable (XSYMBOL (symbol)),
11266 val = sym->value,
11267 (BUFFER_LOCAL_VALUEP (val)))
11268 && XBUFFER_LOCAL_VALUE (val)->check_frame)
11269 /* Use find_symbol_value rather than Fsymbol_value
11270 to avoid an error if it is void. */
11271 find_symbol_value (symbol);
11272 } while (!EQ (frame, old) && (frame = old, 1));
11273 }
11274
11275
11276 #define STOP_POLLING \
11277 do { if (! polling_stopped_here) stop_polling (); \
11278 polling_stopped_here = 1; } while (0)
11279
11280 #define RESUME_POLLING \
11281 do { if (polling_stopped_here) start_polling (); \
11282 polling_stopped_here = 0; } while (0)
11283
11284
11285 /* If PRESERVE_ECHO_AREA is nonzero, it means this redisplay is not in
11286 response to any user action; therefore, we should preserve the echo
11287 area. (Actually, our caller does that job.) Perhaps in the future
11288 avoid recentering windows if it is not necessary; currently that
11289 causes some problems. */
11290
11291 static void
11292 redisplay_internal (preserve_echo_area)
11293 int preserve_echo_area;
11294 {
11295 struct window *w = XWINDOW (selected_window);
11296 struct frame *f;
11297 int pause;
11298 int must_finish = 0;
11299 struct text_pos tlbufpos, tlendpos;
11300 int number_of_visible_frames;
11301 int count, count1;
11302 struct frame *sf;
11303 int polling_stopped_here = 0;
11304 Lisp_Object old_frame = selected_frame;
11305
11306 /* Non-zero means redisplay has to consider all windows on all
11307 frames. Zero means, only selected_window is considered. */
11308 int consider_all_windows_p;
11309
11310 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
11311
11312 /* No redisplay if running in batch mode or frame is not yet fully
11313 initialized, or redisplay is explicitly turned off by setting
11314 Vinhibit_redisplay. */
11315 if (noninteractive
11316 || !NILP (Vinhibit_redisplay))
11317 return;
11318
11319 /* Don't examine these until after testing Vinhibit_redisplay.
11320 When Emacs is shutting down, perhaps because its connection to
11321 X has dropped, we should not look at them at all. */
11322 f = XFRAME (w->frame);
11323 sf = SELECTED_FRAME ();
11324
11325 if (!f->glyphs_initialized_p)
11326 return;
11327
11328 /* The flag redisplay_performed_directly_p is set by
11329 direct_output_for_insert when it already did the whole screen
11330 update necessary. */
11331 if (redisplay_performed_directly_p)
11332 {
11333 redisplay_performed_directly_p = 0;
11334 if (!hscroll_windows (selected_window))
11335 return;
11336 }
11337
11338 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS)
11339 if (popup_activated ())
11340 return;
11341 #endif
11342
11343 /* I don't think this happens but let's be paranoid. */
11344 if (redisplaying_p)
11345 return;
11346
11347 /* Record a function that resets redisplaying_p to its old value
11348 when we leave this function. */
11349 count = SPECPDL_INDEX ();
11350 record_unwind_protect (unwind_redisplay,
11351 Fcons (make_number (redisplaying_p), selected_frame));
11352 ++redisplaying_p;
11353 specbind (Qinhibit_free_realized_faces, Qnil);
11354
11355 {
11356 Lisp_Object tail, frame;
11357
11358 FOR_EACH_FRAME (tail, frame)
11359 {
11360 struct frame *f = XFRAME (frame);
11361 f->already_hscrolled_p = 0;
11362 }
11363 }
11364
11365 retry:
11366 if (!EQ (old_frame, selected_frame)
11367 && FRAME_LIVE_P (XFRAME (old_frame)))
11368 /* When running redisplay, we play a bit fast-and-loose and allow e.g.
11369 selected_frame and selected_window to be temporarily out-of-sync so
11370 when we come back here via `goto retry', we need to resync because we
11371 may need to run Elisp code (via prepare_menu_bars). */
11372 select_frame_for_redisplay (old_frame);
11373
11374 pause = 0;
11375 reconsider_clip_changes (w, current_buffer);
11376 last_escape_glyph_frame = NULL;
11377 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
11378
11379 /* If new fonts have been loaded that make a glyph matrix adjustment
11380 necessary, do it. */
11381 if (fonts_changed_p)
11382 {
11383 adjust_glyphs (NULL);
11384 ++windows_or_buffers_changed;
11385 fonts_changed_p = 0;
11386 }
11387
11388 /* If face_change_count is non-zero, init_iterator will free all
11389 realized faces, which includes the faces referenced from current
11390 matrices. So, we can't reuse current matrices in this case. */
11391 if (face_change_count)
11392 ++windows_or_buffers_changed;
11393
11394 if ((FRAME_TERMCAP_P (sf) || FRAME_MSDOS_P (sf))
11395 && FRAME_TTY (sf)->previous_frame != sf)
11396 {
11397 /* Since frames on a single ASCII terminal share the same
11398 display area, displaying a different frame means redisplay
11399 the whole thing. */
11400 windows_or_buffers_changed++;
11401 SET_FRAME_GARBAGED (sf);
11402 #ifndef DOS_NT
11403 set_tty_color_mode (FRAME_TTY (sf), sf);
11404 #endif
11405 FRAME_TTY (sf)->previous_frame = sf;
11406 }
11407
11408 /* Set the visible flags for all frames. Do this before checking
11409 for resized or garbaged frames; they want to know if their frames
11410 are visible. See the comment in frame.h for
11411 FRAME_SAMPLE_VISIBILITY. */
11412 {
11413 Lisp_Object tail, frame;
11414
11415 number_of_visible_frames = 0;
11416
11417 FOR_EACH_FRAME (tail, frame)
11418 {
11419 struct frame *f = XFRAME (frame);
11420
11421 FRAME_SAMPLE_VISIBILITY (f);
11422 if (FRAME_VISIBLE_P (f))
11423 ++number_of_visible_frames;
11424 clear_desired_matrices (f);
11425 }
11426 }
11427
11428
11429 /* Notice any pending interrupt request to change frame size. */
11430 do_pending_window_change (1);
11431
11432 /* Clear frames marked as garbaged. */
11433 if (frame_garbaged)
11434 clear_garbaged_frames ();
11435
11436 /* Build menubar and tool-bar items. */
11437 if (NILP (Vmemory_full))
11438 prepare_menu_bars ();
11439
11440 if (windows_or_buffers_changed)
11441 update_mode_lines++;
11442
11443 /* Detect case that we need to write or remove a star in the mode line. */
11444 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
11445 {
11446 w->update_mode_line = Qt;
11447 if (buffer_shared > 1)
11448 update_mode_lines++;
11449 }
11450
11451 /* Avoid invocation of point motion hooks by `current_column' below. */
11452 count1 = SPECPDL_INDEX ();
11453 specbind (Qinhibit_point_motion_hooks, Qt);
11454
11455 /* If %c is in the mode line, update it if needed. */
11456 if (!NILP (w->column_number_displayed)
11457 /* This alternative quickly identifies a common case
11458 where no change is needed. */
11459 && !(PT == XFASTINT (w->last_point)
11460 && XFASTINT (w->last_modified) >= MODIFF
11461 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
11462 && (XFASTINT (w->column_number_displayed)
11463 != (int) current_column ())) /* iftc */
11464 w->update_mode_line = Qt;
11465
11466 unbind_to (count1, Qnil);
11467
11468 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
11469
11470 /* The variable buffer_shared is set in redisplay_window and
11471 indicates that we redisplay a buffer in different windows. See
11472 there. */
11473 consider_all_windows_p = (update_mode_lines || buffer_shared > 1
11474 || cursor_type_changed);
11475
11476 /* If specs for an arrow have changed, do thorough redisplay
11477 to ensure we remove any arrow that should no longer exist. */
11478 if (overlay_arrows_changed_p ())
11479 consider_all_windows_p = windows_or_buffers_changed = 1;
11480
11481 /* Normally the message* functions will have already displayed and
11482 updated the echo area, but the frame may have been trashed, or
11483 the update may have been preempted, so display the echo area
11484 again here. Checking message_cleared_p captures the case that
11485 the echo area should be cleared. */
11486 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
11487 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
11488 || (message_cleared_p
11489 && minibuf_level == 0
11490 /* If the mini-window is currently selected, this means the
11491 echo-area doesn't show through. */
11492 && !MINI_WINDOW_P (XWINDOW (selected_window))))
11493 {
11494 int window_height_changed_p = echo_area_display (0);
11495 must_finish = 1;
11496
11497 /* If we don't display the current message, don't clear the
11498 message_cleared_p flag, because, if we did, we wouldn't clear
11499 the echo area in the next redisplay which doesn't preserve
11500 the echo area. */
11501 if (!display_last_displayed_message_p)
11502 message_cleared_p = 0;
11503
11504 if (fonts_changed_p)
11505 goto retry;
11506 else if (window_height_changed_p)
11507 {
11508 consider_all_windows_p = 1;
11509 ++update_mode_lines;
11510 ++windows_or_buffers_changed;
11511
11512 /* If window configuration was changed, frames may have been
11513 marked garbaged. Clear them or we will experience
11514 surprises wrt scrolling. */
11515 if (frame_garbaged)
11516 clear_garbaged_frames ();
11517 }
11518 }
11519 else if (EQ (selected_window, minibuf_window)
11520 && (current_buffer->clip_changed
11521 || XFASTINT (w->last_modified) < MODIFF
11522 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
11523 && resize_mini_window (w, 0))
11524 {
11525 /* Resized active mini-window to fit the size of what it is
11526 showing if its contents might have changed. */
11527 must_finish = 1;
11528 /* FIXME: this causes all frames to be updated, which seems unnecessary
11529 since only the current frame needs to be considered. This function needs
11530 to be rewritten with two variables, consider_all_windows and
11531 consider_all_frames. */
11532 consider_all_windows_p = 1;
11533 ++windows_or_buffers_changed;
11534 ++update_mode_lines;
11535
11536 /* If window configuration was changed, frames may have been
11537 marked garbaged. Clear them or we will experience
11538 surprises wrt scrolling. */
11539 if (frame_garbaged)
11540 clear_garbaged_frames ();
11541 }
11542
11543
11544 /* If showing the region, and mark has changed, we must redisplay
11545 the whole window. The assignment to this_line_start_pos prevents
11546 the optimization directly below this if-statement. */
11547 if (((!NILP (Vtransient_mark_mode)
11548 && !NILP (XBUFFER (w->buffer)->mark_active))
11549 != !NILP (w->region_showing))
11550 || (!NILP (w->region_showing)
11551 && !EQ (w->region_showing,
11552 Fmarker_position (XBUFFER (w->buffer)->mark))))
11553 CHARPOS (this_line_start_pos) = 0;
11554
11555 /* Optimize the case that only the line containing the cursor in the
11556 selected window has changed. Variables starting with this_ are
11557 set in display_line and record information about the line
11558 containing the cursor. */
11559 tlbufpos = this_line_start_pos;
11560 tlendpos = this_line_end_pos;
11561 if (!consider_all_windows_p
11562 && CHARPOS (tlbufpos) > 0
11563 && NILP (w->update_mode_line)
11564 && !current_buffer->clip_changed
11565 && !current_buffer->prevent_redisplay_optimizations_p
11566 && FRAME_VISIBLE_P (XFRAME (w->frame))
11567 && !FRAME_OBSCURED_P (XFRAME (w->frame))
11568 /* Make sure recorded data applies to current buffer, etc. */
11569 && this_line_buffer == current_buffer
11570 && current_buffer == XBUFFER (w->buffer)
11571 && NILP (w->force_start)
11572 && NILP (w->optional_new_start)
11573 /* Point must be on the line that we have info recorded about. */
11574 && PT >= CHARPOS (tlbufpos)
11575 && PT <= Z - CHARPOS (tlendpos)
11576 /* All text outside that line, including its final newline,
11577 must be unchanged */
11578 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
11579 CHARPOS (tlendpos)))
11580 {
11581 if (CHARPOS (tlbufpos) > BEGV
11582 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
11583 && (CHARPOS (tlbufpos) == ZV
11584 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
11585 /* Former continuation line has disappeared by becoming empty */
11586 goto cancel;
11587 else if (XFASTINT (w->last_modified) < MODIFF
11588 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
11589 || MINI_WINDOW_P (w))
11590 {
11591 /* We have to handle the case of continuation around a
11592 wide-column character (See the comment in indent.c around
11593 line 885).
11594
11595 For instance, in the following case:
11596
11597 -------- Insert --------
11598 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
11599 J_I_ ==> J_I_ `^^' are cursors.
11600 ^^ ^^
11601 -------- --------
11602
11603 As we have to redraw the line above, we should goto cancel. */
11604
11605 struct it it;
11606 int line_height_before = this_line_pixel_height;
11607
11608 /* Note that start_display will handle the case that the
11609 line starting at tlbufpos is a continuation lines. */
11610 start_display (&it, w, tlbufpos);
11611
11612 /* Implementation note: It this still necessary? */
11613 if (it.current_x != this_line_start_x)
11614 goto cancel;
11615
11616 TRACE ((stderr, "trying display optimization 1\n"));
11617 w->cursor.vpos = -1;
11618 overlay_arrow_seen = 0;
11619 it.vpos = this_line_vpos;
11620 it.current_y = this_line_y;
11621 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
11622 display_line (&it);
11623
11624 /* If line contains point, is not continued,
11625 and ends at same distance from eob as before, we win */
11626 if (w->cursor.vpos >= 0
11627 /* Line is not continued, otherwise this_line_start_pos
11628 would have been set to 0 in display_line. */
11629 && CHARPOS (this_line_start_pos)
11630 /* Line ends as before. */
11631 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
11632 /* Line has same height as before. Otherwise other lines
11633 would have to be shifted up or down. */
11634 && this_line_pixel_height == line_height_before)
11635 {
11636 /* If this is not the window's last line, we must adjust
11637 the charstarts of the lines below. */
11638 if (it.current_y < it.last_visible_y)
11639 {
11640 struct glyph_row *row
11641 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
11642 int delta, delta_bytes;
11643
11644 if (Z - CHARPOS (tlendpos) == ZV)
11645 {
11646 /* This line ends at end of (accessible part of)
11647 buffer. There is no newline to count. */
11648 delta = (Z
11649 - CHARPOS (tlendpos)
11650 - MATRIX_ROW_START_CHARPOS (row));
11651 delta_bytes = (Z_BYTE
11652 - BYTEPOS (tlendpos)
11653 - MATRIX_ROW_START_BYTEPOS (row));
11654 }
11655 else
11656 {
11657 /* This line ends in a newline. Must take
11658 account of the newline and the rest of the
11659 text that follows. */
11660 delta = (Z
11661 - CHARPOS (tlendpos)
11662 - MATRIX_ROW_START_CHARPOS (row));
11663 delta_bytes = (Z_BYTE
11664 - BYTEPOS (tlendpos)
11665 - MATRIX_ROW_START_BYTEPOS (row));
11666 }
11667
11668 increment_matrix_positions (w->current_matrix,
11669 this_line_vpos + 1,
11670 w->current_matrix->nrows,
11671 delta, delta_bytes);
11672 }
11673
11674 /* If this row displays text now but previously didn't,
11675 or vice versa, w->window_end_vpos may have to be
11676 adjusted. */
11677 if ((it.glyph_row - 1)->displays_text_p)
11678 {
11679 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
11680 XSETINT (w->window_end_vpos, this_line_vpos);
11681 }
11682 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
11683 && this_line_vpos > 0)
11684 XSETINT (w->window_end_vpos, this_line_vpos - 1);
11685 w->window_end_valid = Qnil;
11686
11687 /* Update hint: No need to try to scroll in update_window. */
11688 w->desired_matrix->no_scrolling_p = 1;
11689
11690 #if GLYPH_DEBUG
11691 *w->desired_matrix->method = 0;
11692 debug_method_add (w, "optimization 1");
11693 #endif
11694 #ifdef HAVE_WINDOW_SYSTEM
11695 update_window_fringes (w, 0);
11696 #endif
11697 goto update;
11698 }
11699 else
11700 goto cancel;
11701 }
11702 else if (/* Cursor position hasn't changed. */
11703 PT == XFASTINT (w->last_point)
11704 /* Make sure the cursor was last displayed
11705 in this window. Otherwise we have to reposition it. */
11706 && 0 <= w->cursor.vpos
11707 && WINDOW_TOTAL_LINES (w) > w->cursor.vpos)
11708 {
11709 if (!must_finish)
11710 {
11711 do_pending_window_change (1);
11712
11713 /* We used to always goto end_of_redisplay here, but this
11714 isn't enough if we have a blinking cursor. */
11715 if (w->cursor_off_p == w->last_cursor_off_p)
11716 goto end_of_redisplay;
11717 }
11718 goto update;
11719 }
11720 /* If highlighting the region, or if the cursor is in the echo area,
11721 then we can't just move the cursor. */
11722 else if (! (!NILP (Vtransient_mark_mode)
11723 && !NILP (current_buffer->mark_active))
11724 && (EQ (selected_window, current_buffer->last_selected_window)
11725 || highlight_nonselected_windows)
11726 && NILP (w->region_showing)
11727 && NILP (Vshow_trailing_whitespace)
11728 && !cursor_in_echo_area)
11729 {
11730 struct it it;
11731 struct glyph_row *row;
11732
11733 /* Skip from tlbufpos to PT and see where it is. Note that
11734 PT may be in invisible text. If so, we will end at the
11735 next visible position. */
11736 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
11737 NULL, DEFAULT_FACE_ID);
11738 it.current_x = this_line_start_x;
11739 it.current_y = this_line_y;
11740 it.vpos = this_line_vpos;
11741
11742 /* The call to move_it_to stops in front of PT, but
11743 moves over before-strings. */
11744 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
11745
11746 if (it.vpos == this_line_vpos
11747 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
11748 row->enabled_p))
11749 {
11750 xassert (this_line_vpos == it.vpos);
11751 xassert (this_line_y == it.current_y);
11752 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
11753 #if GLYPH_DEBUG
11754 *w->desired_matrix->method = 0;
11755 debug_method_add (w, "optimization 3");
11756 #endif
11757 goto update;
11758 }
11759 else
11760 goto cancel;
11761 }
11762
11763 cancel:
11764 /* Text changed drastically or point moved off of line. */
11765 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
11766 }
11767
11768 CHARPOS (this_line_start_pos) = 0;
11769 consider_all_windows_p |= buffer_shared > 1;
11770 ++clear_face_cache_count;
11771 #ifdef HAVE_WINDOW_SYSTEM
11772 ++clear_image_cache_count;
11773 #endif
11774
11775 /* Build desired matrices, and update the display. If
11776 consider_all_windows_p is non-zero, do it for all windows on all
11777 frames. Otherwise do it for selected_window, only. */
11778
11779 if (consider_all_windows_p)
11780 {
11781 Lisp_Object tail, frame;
11782
11783 FOR_EACH_FRAME (tail, frame)
11784 XFRAME (frame)->updated_p = 0;
11785
11786 /* Recompute # windows showing selected buffer. This will be
11787 incremented each time such a window is displayed. */
11788 buffer_shared = 0;
11789
11790 FOR_EACH_FRAME (tail, frame)
11791 {
11792 struct frame *f = XFRAME (frame);
11793
11794 if (FRAME_WINDOW_P (f) || FRAME_TERMCAP_P (f) || f == sf)
11795 {
11796 if (! EQ (frame, selected_frame))
11797 /* Select the frame, for the sake of frame-local
11798 variables. */
11799 select_frame_for_redisplay (frame);
11800
11801 /* Mark all the scroll bars to be removed; we'll redeem
11802 the ones we want when we redisplay their windows. */
11803 if (FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
11804 FRAME_TERMINAL (f)->condemn_scroll_bars_hook (f);
11805
11806 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
11807 redisplay_windows (FRAME_ROOT_WINDOW (f));
11808
11809 /* Any scroll bars which redisplay_windows should have
11810 nuked should now go away. */
11811 if (FRAME_TERMINAL (f)->judge_scroll_bars_hook)
11812 FRAME_TERMINAL (f)->judge_scroll_bars_hook (f);
11813
11814 /* If fonts changed, display again. */
11815 /* ??? rms: I suspect it is a mistake to jump all the way
11816 back to retry here. It should just retry this frame. */
11817 if (fonts_changed_p)
11818 goto retry;
11819
11820 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
11821 {
11822 /* See if we have to hscroll. */
11823 if (!f->already_hscrolled_p)
11824 {
11825 f->already_hscrolled_p = 1;
11826 if (hscroll_windows (f->root_window))
11827 goto retry;
11828 }
11829
11830 /* Prevent various kinds of signals during display
11831 update. stdio is not robust about handling
11832 signals, which can cause an apparent I/O
11833 error. */
11834 if (interrupt_input)
11835 unrequest_sigio ();
11836 STOP_POLLING;
11837
11838 /* Update the display. */
11839 set_window_update_flags (XWINDOW (f->root_window), 1);
11840 pause |= update_frame (f, 0, 0);
11841 f->updated_p = 1;
11842 }
11843 }
11844 }
11845
11846 if (!EQ (old_frame, selected_frame)
11847 && FRAME_LIVE_P (XFRAME (old_frame)))
11848 /* We played a bit fast-and-loose above and allowed selected_frame
11849 and selected_window to be temporarily out-of-sync but let's make
11850 sure this stays contained. */
11851 select_frame_for_redisplay (old_frame);
11852 eassert (EQ (XFRAME (selected_frame)->selected_window, selected_window));
11853
11854 if (!pause)
11855 {
11856 /* Do the mark_window_display_accurate after all windows have
11857 been redisplayed because this call resets flags in buffers
11858 which are needed for proper redisplay. */
11859 FOR_EACH_FRAME (tail, frame)
11860 {
11861 struct frame *f = XFRAME (frame);
11862 if (f->updated_p)
11863 {
11864 mark_window_display_accurate (f->root_window, 1);
11865 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
11866 FRAME_TERMINAL (f)->frame_up_to_date_hook (f);
11867 }
11868 }
11869 }
11870 }
11871 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
11872 {
11873 Lisp_Object mini_window;
11874 struct frame *mini_frame;
11875
11876 displayed_buffer = XBUFFER (XWINDOW (selected_window)->buffer);
11877 /* Use list_of_error, not Qerror, so that
11878 we catch only errors and don't run the debugger. */
11879 internal_condition_case_1 (redisplay_window_1, selected_window,
11880 list_of_error,
11881 redisplay_window_error);
11882
11883 /* Compare desired and current matrices, perform output. */
11884
11885 update:
11886 /* If fonts changed, display again. */
11887 if (fonts_changed_p)
11888 goto retry;
11889
11890 /* Prevent various kinds of signals during display update.
11891 stdio is not robust about handling signals,
11892 which can cause an apparent I/O error. */
11893 if (interrupt_input)
11894 unrequest_sigio ();
11895 STOP_POLLING;
11896
11897 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
11898 {
11899 if (hscroll_windows (selected_window))
11900 goto retry;
11901
11902 XWINDOW (selected_window)->must_be_updated_p = 1;
11903 pause = update_frame (sf, 0, 0);
11904 }
11905
11906 /* We may have called echo_area_display at the top of this
11907 function. If the echo area is on another frame, that may
11908 have put text on a frame other than the selected one, so the
11909 above call to update_frame would not have caught it. Catch
11910 it here. */
11911 mini_window = FRAME_MINIBUF_WINDOW (sf);
11912 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
11913
11914 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
11915 {
11916 XWINDOW (mini_window)->must_be_updated_p = 1;
11917 pause |= update_frame (mini_frame, 0, 0);
11918 if (!pause && hscroll_windows (mini_window))
11919 goto retry;
11920 }
11921 }
11922
11923 /* If display was paused because of pending input, make sure we do a
11924 thorough update the next time. */
11925 if (pause)
11926 {
11927 /* Prevent the optimization at the beginning of
11928 redisplay_internal that tries a single-line update of the
11929 line containing the cursor in the selected window. */
11930 CHARPOS (this_line_start_pos) = 0;
11931
11932 /* Let the overlay arrow be updated the next time. */
11933 update_overlay_arrows (0);
11934
11935 /* If we pause after scrolling, some rows in the current
11936 matrices of some windows are not valid. */
11937 if (!WINDOW_FULL_WIDTH_P (w)
11938 && !FRAME_WINDOW_P (XFRAME (w->frame)))
11939 update_mode_lines = 1;
11940 }
11941 else
11942 {
11943 if (!consider_all_windows_p)
11944 {
11945 /* This has already been done above if
11946 consider_all_windows_p is set. */
11947 mark_window_display_accurate_1 (w, 1);
11948
11949 /* Say overlay arrows are up to date. */
11950 update_overlay_arrows (1);
11951
11952 if (FRAME_TERMINAL (sf)->frame_up_to_date_hook != 0)
11953 FRAME_TERMINAL (sf)->frame_up_to_date_hook (sf);
11954 }
11955
11956 update_mode_lines = 0;
11957 windows_or_buffers_changed = 0;
11958 cursor_type_changed = 0;
11959 }
11960
11961 /* Start SIGIO interrupts coming again. Having them off during the
11962 code above makes it less likely one will discard output, but not
11963 impossible, since there might be stuff in the system buffer here.
11964 But it is much hairier to try to do anything about that. */
11965 if (interrupt_input)
11966 request_sigio ();
11967 RESUME_POLLING;
11968
11969 /* If a frame has become visible which was not before, redisplay
11970 again, so that we display it. Expose events for such a frame
11971 (which it gets when becoming visible) don't call the parts of
11972 redisplay constructing glyphs, so simply exposing a frame won't
11973 display anything in this case. So, we have to display these
11974 frames here explicitly. */
11975 if (!pause)
11976 {
11977 Lisp_Object tail, frame;
11978 int new_count = 0;
11979
11980 FOR_EACH_FRAME (tail, frame)
11981 {
11982 int this_is_visible = 0;
11983
11984 if (XFRAME (frame)->visible)
11985 this_is_visible = 1;
11986 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
11987 if (XFRAME (frame)->visible)
11988 this_is_visible = 1;
11989
11990 if (this_is_visible)
11991 new_count++;
11992 }
11993
11994 if (new_count != number_of_visible_frames)
11995 windows_or_buffers_changed++;
11996 }
11997
11998 /* Change frame size now if a change is pending. */
11999 do_pending_window_change (1);
12000
12001 /* If we just did a pending size change, or have additional
12002 visible frames, redisplay again. */
12003 if (windows_or_buffers_changed && !pause)
12004 goto retry;
12005
12006 /* Clear the face cache eventually. */
12007 if (consider_all_windows_p)
12008 {
12009 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
12010 {
12011 clear_face_cache (0);
12012 clear_face_cache_count = 0;
12013 }
12014 #ifdef HAVE_WINDOW_SYSTEM
12015 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
12016 {
12017 clear_image_caches (Qnil);
12018 clear_image_cache_count = 0;
12019 }
12020 #endif /* HAVE_WINDOW_SYSTEM */
12021 }
12022
12023 end_of_redisplay:
12024 unbind_to (count, Qnil);
12025 RESUME_POLLING;
12026 }
12027
12028
12029 /* Redisplay, but leave alone any recent echo area message unless
12030 another message has been requested in its place.
12031
12032 This is useful in situations where you need to redisplay but no
12033 user action has occurred, making it inappropriate for the message
12034 area to be cleared. See tracking_off and
12035 wait_reading_process_output for examples of these situations.
12036
12037 FROM_WHERE is an integer saying from where this function was
12038 called. This is useful for debugging. */
12039
12040 void
12041 redisplay_preserve_echo_area (from_where)
12042 int from_where;
12043 {
12044 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
12045
12046 if (!NILP (echo_area_buffer[1]))
12047 {
12048 /* We have a previously displayed message, but no current
12049 message. Redisplay the previous message. */
12050 display_last_displayed_message_p = 1;
12051 redisplay_internal (1);
12052 display_last_displayed_message_p = 0;
12053 }
12054 else
12055 redisplay_internal (1);
12056
12057 if (FRAME_RIF (SELECTED_FRAME ()) != NULL
12058 && FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
12059 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (NULL);
12060 }
12061
12062
12063 /* Function registered with record_unwind_protect in
12064 redisplay_internal. Reset redisplaying_p to the value it had
12065 before redisplay_internal was called, and clear
12066 prevent_freeing_realized_faces_p. It also selects the previously
12067 selected frame, unless it has been deleted (by an X connection
12068 failure during redisplay, for example). */
12069
12070 static Lisp_Object
12071 unwind_redisplay (val)
12072 Lisp_Object val;
12073 {
12074 Lisp_Object old_redisplaying_p, old_frame;
12075
12076 old_redisplaying_p = XCAR (val);
12077 redisplaying_p = XFASTINT (old_redisplaying_p);
12078 old_frame = XCDR (val);
12079 if (! EQ (old_frame, selected_frame)
12080 && FRAME_LIVE_P (XFRAME (old_frame)))
12081 select_frame_for_redisplay (old_frame);
12082 return Qnil;
12083 }
12084
12085
12086 /* Mark the display of window W as accurate or inaccurate. If
12087 ACCURATE_P is non-zero mark display of W as accurate. If
12088 ACCURATE_P is zero, arrange for W to be redisplayed the next time
12089 redisplay_internal is called. */
12090
12091 static void
12092 mark_window_display_accurate_1 (w, accurate_p)
12093 struct window *w;
12094 int accurate_p;
12095 {
12096 if (BUFFERP (w->buffer))
12097 {
12098 struct buffer *b = XBUFFER (w->buffer);
12099
12100 w->last_modified
12101 = make_number (accurate_p ? BUF_MODIFF (b) : 0);
12102 w->last_overlay_modified
12103 = make_number (accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
12104 w->last_had_star
12105 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b) ? Qt : Qnil;
12106
12107 if (accurate_p)
12108 {
12109 b->clip_changed = 0;
12110 b->prevent_redisplay_optimizations_p = 0;
12111
12112 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
12113 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
12114 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
12115 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
12116
12117 w->current_matrix->buffer = b;
12118 w->current_matrix->begv = BUF_BEGV (b);
12119 w->current_matrix->zv = BUF_ZV (b);
12120
12121 w->last_cursor = w->cursor;
12122 w->last_cursor_off_p = w->cursor_off_p;
12123
12124 if (w == XWINDOW (selected_window))
12125 w->last_point = make_number (BUF_PT (b));
12126 else
12127 w->last_point = make_number (XMARKER (w->pointm)->charpos);
12128 }
12129 }
12130
12131 if (accurate_p)
12132 {
12133 w->window_end_valid = w->buffer;
12134 w->update_mode_line = Qnil;
12135 }
12136 }
12137
12138
12139 /* Mark the display of windows in the window tree rooted at WINDOW as
12140 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
12141 windows as accurate. If ACCURATE_P is zero, arrange for windows to
12142 be redisplayed the next time redisplay_internal is called. */
12143
12144 void
12145 mark_window_display_accurate (window, accurate_p)
12146 Lisp_Object window;
12147 int accurate_p;
12148 {
12149 struct window *w;
12150
12151 for (; !NILP (window); window = w->next)
12152 {
12153 w = XWINDOW (window);
12154 mark_window_display_accurate_1 (w, accurate_p);
12155
12156 if (!NILP (w->vchild))
12157 mark_window_display_accurate (w->vchild, accurate_p);
12158 if (!NILP (w->hchild))
12159 mark_window_display_accurate (w->hchild, accurate_p);
12160 }
12161
12162 if (accurate_p)
12163 {
12164 update_overlay_arrows (1);
12165 }
12166 else
12167 {
12168 /* Force a thorough redisplay the next time by setting
12169 last_arrow_position and last_arrow_string to t, which is
12170 unequal to any useful value of Voverlay_arrow_... */
12171 update_overlay_arrows (-1);
12172 }
12173 }
12174
12175
12176 /* Return value in display table DP (Lisp_Char_Table *) for character
12177 C. Since a display table doesn't have any parent, we don't have to
12178 follow parent. Do not call this function directly but use the
12179 macro DISP_CHAR_VECTOR. */
12180
12181 Lisp_Object
12182 disp_char_vector (dp, c)
12183 struct Lisp_Char_Table *dp;
12184 int c;
12185 {
12186 Lisp_Object val;
12187
12188 if (ASCII_CHAR_P (c))
12189 {
12190 val = dp->ascii;
12191 if (SUB_CHAR_TABLE_P (val))
12192 val = XSUB_CHAR_TABLE (val)->contents[c];
12193 }
12194 else
12195 {
12196 Lisp_Object table;
12197
12198 XSETCHAR_TABLE (table, dp);
12199 val = char_table_ref (table, c);
12200 }
12201 if (NILP (val))
12202 val = dp->defalt;
12203 return val;
12204 }
12205
12206
12207 \f
12208 /***********************************************************************
12209 Window Redisplay
12210 ***********************************************************************/
12211
12212 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
12213
12214 static void
12215 redisplay_windows (window)
12216 Lisp_Object window;
12217 {
12218 while (!NILP (window))
12219 {
12220 struct window *w = XWINDOW (window);
12221
12222 if (!NILP (w->hchild))
12223 redisplay_windows (w->hchild);
12224 else if (!NILP (w->vchild))
12225 redisplay_windows (w->vchild);
12226 else
12227 {
12228 displayed_buffer = XBUFFER (w->buffer);
12229 /* Use list_of_error, not Qerror, so that
12230 we catch only errors and don't run the debugger. */
12231 internal_condition_case_1 (redisplay_window_0, window,
12232 list_of_error,
12233 redisplay_window_error);
12234 }
12235
12236 window = w->next;
12237 }
12238 }
12239
12240 static Lisp_Object
12241 redisplay_window_error ()
12242 {
12243 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
12244 return Qnil;
12245 }
12246
12247 static Lisp_Object
12248 redisplay_window_0 (window)
12249 Lisp_Object window;
12250 {
12251 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
12252 redisplay_window (window, 0);
12253 return Qnil;
12254 }
12255
12256 static Lisp_Object
12257 redisplay_window_1 (window)
12258 Lisp_Object window;
12259 {
12260 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
12261 redisplay_window (window, 1);
12262 return Qnil;
12263 }
12264 \f
12265
12266 /* Increment GLYPH until it reaches END or CONDITION fails while
12267 adding (GLYPH)->pixel_width to X. */
12268
12269 #define SKIP_GLYPHS(glyph, end, x, condition) \
12270 do \
12271 { \
12272 (x) += (glyph)->pixel_width; \
12273 ++(glyph); \
12274 } \
12275 while ((glyph) < (end) && (condition))
12276
12277
12278 /* Set cursor position of W. PT is assumed to be displayed in ROW.
12279 DELTA is the number of bytes by which positions recorded in ROW
12280 differ from current buffer positions.
12281
12282 Return 0 if cursor is not on this row. 1 otherwise. */
12283
12284 int
12285 set_cursor_from_row (w, row, matrix, delta, delta_bytes, dy, dvpos)
12286 struct window *w;
12287 struct glyph_row *row;
12288 struct glyph_matrix *matrix;
12289 int delta, delta_bytes, dy, dvpos;
12290 {
12291 struct glyph *glyph = row->glyphs[TEXT_AREA];
12292 struct glyph *end = glyph + row->used[TEXT_AREA];
12293 struct glyph *cursor = NULL;
12294 /* The first glyph that starts a sequence of glyphs from string. */
12295 struct glyph *string_start;
12296 /* The X coordinate of string_start. */
12297 int string_start_x;
12298 /* The last known character position. */
12299 int last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
12300 /* The last known character position before string_start. */
12301 int string_before_pos;
12302 int x = row->x;
12303 int cursor_x = x;
12304 int cursor_from_overlay_pos = 0;
12305 int pt_old = PT - delta;
12306
12307 /* Skip over glyphs not having an object at the start of the row.
12308 These are special glyphs like truncation marks on terminal
12309 frames. */
12310 if (row->displays_text_p)
12311 while (glyph < end
12312 && INTEGERP (glyph->object)
12313 && glyph->charpos < 0)
12314 {
12315 x += glyph->pixel_width;
12316 ++glyph;
12317 }
12318
12319 string_start = NULL;
12320 while (glyph < end
12321 && !INTEGERP (glyph->object)
12322 && (!BUFFERP (glyph->object)
12323 || (last_pos = glyph->charpos) < pt_old
12324 || glyph->avoid_cursor_p))
12325 {
12326 if (! STRINGP (glyph->object))
12327 {
12328 string_start = NULL;
12329 x += glyph->pixel_width;
12330 ++glyph;
12331 if (cursor_from_overlay_pos
12332 && last_pos >= cursor_from_overlay_pos)
12333 {
12334 cursor_from_overlay_pos = 0;
12335 cursor = 0;
12336 }
12337 }
12338 else
12339 {
12340 if (string_start == NULL)
12341 {
12342 string_before_pos = last_pos;
12343 string_start = glyph;
12344 string_start_x = x;
12345 }
12346 /* Skip all glyphs from string. */
12347 do
12348 {
12349 Lisp_Object cprop;
12350 int pos;
12351 if ((cursor == NULL || glyph > cursor)
12352 && (cprop = Fget_char_property (make_number ((glyph)->charpos),
12353 Qcursor, (glyph)->object),
12354 !NILP (cprop))
12355 && (pos = string_buffer_position (w, glyph->object,
12356 string_before_pos),
12357 (pos == 0 /* From overlay */
12358 || pos == pt_old)))
12359 {
12360 /* Estimate overlay buffer position from the buffer
12361 positions of the glyphs before and after the overlay.
12362 Add 1 to last_pos so that if point corresponds to the
12363 glyph right after the overlay, we still use a 'cursor'
12364 property found in that overlay. */
12365 cursor_from_overlay_pos = (pos ? 0 : last_pos
12366 + (INTEGERP (cprop) ? XINT (cprop) : 0));
12367 cursor = glyph;
12368 cursor_x = x;
12369 }
12370 x += glyph->pixel_width;
12371 ++glyph;
12372 }
12373 while (glyph < end && EQ (glyph->object, string_start->object));
12374 }
12375 }
12376
12377 if (cursor != NULL)
12378 {
12379 glyph = cursor;
12380 x = cursor_x;
12381 }
12382 else if (row->ends_in_ellipsis_p && glyph == end)
12383 {
12384 /* Scan back over the ellipsis glyphs, decrementing positions. */
12385 while (glyph > row->glyphs[TEXT_AREA]
12386 && (glyph - 1)->charpos == last_pos)
12387 glyph--, x -= glyph->pixel_width;
12388 /* That loop always goes one position too far,
12389 including the glyph before the ellipsis.
12390 So scan forward over that one. */
12391 x += glyph->pixel_width;
12392 glyph++;
12393 }
12394 else if (string_start
12395 && (glyph == end || !BUFFERP (glyph->object) || last_pos > pt_old))
12396 {
12397 /* We may have skipped over point because the previous glyphs
12398 are from string. As there's no easy way to know the
12399 character position of the current glyph, find the correct
12400 glyph on point by scanning from string_start again. */
12401 Lisp_Object limit;
12402 Lisp_Object string;
12403 struct glyph *stop = glyph;
12404 int pos;
12405
12406 limit = make_number (pt_old + 1);
12407 glyph = string_start;
12408 x = string_start_x;
12409 string = glyph->object;
12410 pos = string_buffer_position (w, string, string_before_pos);
12411 /* If STRING is from overlay, LAST_POS == 0. We skip such glyphs
12412 because we always put cursor after overlay strings. */
12413 while (pos == 0 && glyph < stop)
12414 {
12415 string = glyph->object;
12416 SKIP_GLYPHS (glyph, stop, x, EQ (glyph->object, string));
12417 if (glyph < stop)
12418 pos = string_buffer_position (w, glyph->object, string_before_pos);
12419 }
12420
12421 while (glyph < stop)
12422 {
12423 pos = XINT (Fnext_single_char_property_change
12424 (make_number (pos), Qdisplay, Qnil, limit));
12425 if (pos > pt_old)
12426 break;
12427 /* Skip glyphs from the same string. */
12428 string = glyph->object;
12429 SKIP_GLYPHS (glyph, stop, x, EQ (glyph->object, string));
12430 /* Skip glyphs from an overlay. */
12431 while (glyph < stop
12432 && ! string_buffer_position (w, glyph->object, pos))
12433 {
12434 string = glyph->object;
12435 SKIP_GLYPHS (glyph, stop, x, EQ (glyph->object, string));
12436 }
12437 }
12438
12439 /* If we reached the end of the line, and end was from a string,
12440 cursor is not on this line. */
12441 if (glyph == end && row->continued_p)
12442 return 0;
12443 }
12444
12445 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
12446 w->cursor.x = x;
12447 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
12448 w->cursor.y = row->y + dy;
12449
12450 if (w == XWINDOW (selected_window))
12451 {
12452 if (!row->continued_p
12453 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
12454 && row->x == 0)
12455 {
12456 this_line_buffer = XBUFFER (w->buffer);
12457
12458 CHARPOS (this_line_start_pos)
12459 = MATRIX_ROW_START_CHARPOS (row) + delta;
12460 BYTEPOS (this_line_start_pos)
12461 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
12462
12463 CHARPOS (this_line_end_pos)
12464 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
12465 BYTEPOS (this_line_end_pos)
12466 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
12467
12468 this_line_y = w->cursor.y;
12469 this_line_pixel_height = row->height;
12470 this_line_vpos = w->cursor.vpos;
12471 this_line_start_x = row->x;
12472 }
12473 else
12474 CHARPOS (this_line_start_pos) = 0;
12475 }
12476
12477 return 1;
12478 }
12479
12480
12481 /* Run window scroll functions, if any, for WINDOW with new window
12482 start STARTP. Sets the window start of WINDOW to that position.
12483
12484 We assume that the window's buffer is really current. */
12485
12486 static INLINE struct text_pos
12487 run_window_scroll_functions (window, startp)
12488 Lisp_Object window;
12489 struct text_pos startp;
12490 {
12491 struct window *w = XWINDOW (window);
12492 SET_MARKER_FROM_TEXT_POS (w->start, startp);
12493
12494 if (current_buffer != XBUFFER (w->buffer))
12495 abort ();
12496
12497 if (!NILP (Vwindow_scroll_functions))
12498 {
12499 run_hook_with_args_2 (Qwindow_scroll_functions, window,
12500 make_number (CHARPOS (startp)));
12501 SET_TEXT_POS_FROM_MARKER (startp, w->start);
12502 /* In case the hook functions switch buffers. */
12503 if (current_buffer != XBUFFER (w->buffer))
12504 set_buffer_internal_1 (XBUFFER (w->buffer));
12505 }
12506
12507 return startp;
12508 }
12509
12510
12511 /* Make sure the line containing the cursor is fully visible.
12512 A value of 1 means there is nothing to be done.
12513 (Either the line is fully visible, or it cannot be made so,
12514 or we cannot tell.)
12515
12516 If FORCE_P is non-zero, return 0 even if partial visible cursor row
12517 is higher than window.
12518
12519 A value of 0 means the caller should do scrolling
12520 as if point had gone off the screen. */
12521
12522 static int
12523 cursor_row_fully_visible_p (w, force_p, current_matrix_p)
12524 struct window *w;
12525 int force_p;
12526 int current_matrix_p;
12527 {
12528 struct glyph_matrix *matrix;
12529 struct glyph_row *row;
12530 int window_height;
12531
12532 if (!make_cursor_line_fully_visible_p)
12533 return 1;
12534
12535 /* It's not always possible to find the cursor, e.g, when a window
12536 is full of overlay strings. Don't do anything in that case. */
12537 if (w->cursor.vpos < 0)
12538 return 1;
12539
12540 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
12541 row = MATRIX_ROW (matrix, w->cursor.vpos);
12542
12543 /* If the cursor row is not partially visible, there's nothing to do. */
12544 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
12545 return 1;
12546
12547 /* If the row the cursor is in is taller than the window's height,
12548 it's not clear what to do, so do nothing. */
12549 window_height = window_box_height (w);
12550 if (row->height >= window_height)
12551 {
12552 if (!force_p || MINI_WINDOW_P (w)
12553 || w->vscroll || w->cursor.vpos == 0)
12554 return 1;
12555 }
12556 return 0;
12557
12558 #if 0
12559 /* This code used to try to scroll the window just enough to make
12560 the line visible. It returned 0 to say that the caller should
12561 allocate larger glyph matrices. */
12562
12563 if (MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (w, row))
12564 {
12565 int dy = row->height - row->visible_height;
12566 w->vscroll = 0;
12567 w->cursor.y += dy;
12568 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
12569 }
12570 else /* MATRIX_ROW_PARTIALLY_VISIBLE_AT_BOTTOM_P (w, row)) */
12571 {
12572 int dy = - (row->height - row->visible_height);
12573 w->vscroll = dy;
12574 w->cursor.y += dy;
12575 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
12576 }
12577
12578 /* When we change the cursor y-position of the selected window,
12579 change this_line_y as well so that the display optimization for
12580 the cursor line of the selected window in redisplay_internal uses
12581 the correct y-position. */
12582 if (w == XWINDOW (selected_window))
12583 this_line_y = w->cursor.y;
12584
12585 /* If vscrolling requires a larger glyph matrix, arrange for a fresh
12586 redisplay with larger matrices. */
12587 if (matrix->nrows < required_matrix_height (w))
12588 {
12589 fonts_changed_p = 1;
12590 return 0;
12591 }
12592
12593 return 1;
12594 #endif /* 0 */
12595 }
12596
12597
12598 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
12599 non-zero means only WINDOW is redisplayed in redisplay_internal.
12600 TEMP_SCROLL_STEP has the same meaning as scroll_step, and is used
12601 in redisplay_window to bring a partially visible line into view in
12602 the case that only the cursor has moved.
12603
12604 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
12605 last screen line's vertical height extends past the end of the screen.
12606
12607 Value is
12608
12609 1 if scrolling succeeded
12610
12611 0 if scrolling didn't find point.
12612
12613 -1 if new fonts have been loaded so that we must interrupt
12614 redisplay, adjust glyph matrices, and try again. */
12615
12616 enum
12617 {
12618 SCROLLING_SUCCESS,
12619 SCROLLING_FAILED,
12620 SCROLLING_NEED_LARGER_MATRICES
12621 };
12622
12623 static int
12624 try_scrolling (window, just_this_one_p, scroll_conservatively,
12625 scroll_step, temp_scroll_step, last_line_misfit)
12626 Lisp_Object window;
12627 int just_this_one_p;
12628 EMACS_INT scroll_conservatively, scroll_step;
12629 int temp_scroll_step;
12630 int last_line_misfit;
12631 {
12632 struct window *w = XWINDOW (window);
12633 struct frame *f = XFRAME (w->frame);
12634 struct text_pos pos, startp;
12635 struct it it;
12636 int this_scroll_margin, scroll_max, rc, height;
12637 int dy = 0, amount_to_scroll = 0, scroll_down_p = 0;
12638 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
12639 Lisp_Object aggressive;
12640 int scroll_limit = INT_MAX / FRAME_LINE_HEIGHT (f);
12641
12642 #if GLYPH_DEBUG
12643 debug_method_add (w, "try_scrolling");
12644 #endif
12645
12646 SET_TEXT_POS_FROM_MARKER (startp, w->start);
12647
12648 /* Compute scroll margin height in pixels. We scroll when point is
12649 within this distance from the top or bottom of the window. */
12650 if (scroll_margin > 0)
12651 {
12652 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
12653 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
12654 }
12655 else
12656 this_scroll_margin = 0;
12657
12658 /* Force scroll_conservatively to have a reasonable value, to avoid
12659 overflow while computing how much to scroll. Note that it's
12660 fairly common for users to supply scroll-conservatively equal to
12661 `most-positive-fixnum', which can be larger than INT_MAX. */
12662 if (scroll_conservatively > scroll_limit)
12663 {
12664 scroll_conservatively = scroll_limit;
12665 scroll_max = INT_MAX;
12666 }
12667 else if (scroll_step || scroll_conservatively || temp_scroll_step)
12668 /* Compute how much we should try to scroll maximally to bring
12669 point into view. */
12670 scroll_max = (max (scroll_step,
12671 max (scroll_conservatively, temp_scroll_step))
12672 * FRAME_LINE_HEIGHT (f));
12673 else if (NUMBERP (current_buffer->scroll_down_aggressively)
12674 || NUMBERP (current_buffer->scroll_up_aggressively))
12675 /* We're trying to scroll because of aggressive scrolling but no
12676 scroll_step is set. Choose an arbitrary one. */
12677 scroll_max = 10 * FRAME_LINE_HEIGHT (f);
12678 else
12679 scroll_max = 0;
12680
12681 too_near_end:
12682
12683 /* Decide whether we have to scroll down. */
12684 if (PT > CHARPOS (startp))
12685 {
12686 int scroll_margin_y;
12687
12688 /* Compute the pixel ypos of the scroll margin, then move it to
12689 either that ypos or PT, whichever comes first. */
12690 start_display (&it, w, startp);
12691 scroll_margin_y = it.last_visible_y - this_scroll_margin
12692 - FRAME_LINE_HEIGHT (f) * extra_scroll_margin_lines;
12693 move_it_to (&it, PT, -1, scroll_margin_y - 1, -1,
12694 (MOVE_TO_POS | MOVE_TO_Y));
12695
12696 if (PT > CHARPOS (it.current.pos))
12697 {
12698 /* Point is in the scroll margin at the bottom of the
12699 window, or below. Compute the distance from the scroll
12700 margin to PT, and give up if the distance is greater than
12701 scroll_max. */
12702 move_it_to (&it, PT, -1, it.last_visible_y - 1, -1,
12703 MOVE_TO_POS | MOVE_TO_Y);
12704
12705 /* To make point visible, we must move the window start down
12706 so that the cursor line is visible, which means we have
12707 to add in the height of the cursor line. */
12708 dy = line_bottom_y (&it) - scroll_margin_y;
12709
12710 if (dy > scroll_max)
12711 return SCROLLING_FAILED;
12712
12713 scroll_down_p = 1;
12714 }
12715 }
12716
12717 if (scroll_down_p)
12718 {
12719 /* Move the window start down. If scrolling conservatively,
12720 move it just enough down to make point visible. If
12721 scroll_step is set, move it down by scroll_step. */
12722 start_display (&it, w, startp);
12723
12724 if (scroll_conservatively)
12725 /* Set AMOUNT_TO_SCROLL to at least one line,
12726 and at most scroll_conservatively lines. */
12727 amount_to_scroll
12728 = min (max (dy, FRAME_LINE_HEIGHT (f)),
12729 FRAME_LINE_HEIGHT (f) * scroll_conservatively);
12730 else if (scroll_step || temp_scroll_step)
12731 amount_to_scroll = scroll_max;
12732 else
12733 {
12734 aggressive = current_buffer->scroll_up_aggressively;
12735 height = WINDOW_BOX_TEXT_HEIGHT (w);
12736 if (NUMBERP (aggressive))
12737 {
12738 double float_amount = XFLOATINT (aggressive) * height;
12739 amount_to_scroll = float_amount;
12740 if (amount_to_scroll == 0 && float_amount > 0)
12741 amount_to_scroll = 1;
12742 }
12743 }
12744
12745 if (amount_to_scroll <= 0)
12746 return SCROLLING_FAILED;
12747
12748 /* If moving by amount_to_scroll leaves STARTP unchanged,
12749 move it down one screen line. */
12750
12751 move_it_vertically (&it, amount_to_scroll);
12752 if (CHARPOS (it.current.pos) == CHARPOS (startp))
12753 move_it_by_lines (&it, 1, 1);
12754 startp = it.current.pos;
12755 }
12756 else
12757 {
12758 struct text_pos scroll_margin_pos = startp;
12759
12760 /* See if point is inside the scroll margin at the top of the
12761 window. */
12762 if (this_scroll_margin)
12763 {
12764 start_display (&it, w, startp);
12765 move_it_vertically (&it, this_scroll_margin);
12766 scroll_margin_pos = it.current.pos;
12767 }
12768
12769 if (PT < CHARPOS (scroll_margin_pos))
12770 {
12771 /* Point is in the scroll margin at the top of the window or
12772 above what is displayed in the window. */
12773 int y0;
12774
12775 /* Compute the vertical distance from PT to the scroll
12776 margin position. Give up if distance is greater than
12777 scroll_max. */
12778 SET_TEXT_POS (pos, PT, PT_BYTE);
12779 start_display (&it, w, pos);
12780 y0 = it.current_y;
12781 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
12782 it.last_visible_y, -1,
12783 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
12784 dy = it.current_y - y0;
12785 if (dy > scroll_max)
12786 return SCROLLING_FAILED;
12787
12788 /* Compute new window start. */
12789 start_display (&it, w, startp);
12790
12791 if (scroll_conservatively)
12792 amount_to_scroll
12793 = max (dy, FRAME_LINE_HEIGHT (f) * max (scroll_step, temp_scroll_step));
12794 else if (scroll_step || temp_scroll_step)
12795 amount_to_scroll = scroll_max;
12796 else
12797 {
12798 aggressive = current_buffer->scroll_down_aggressively;
12799 height = WINDOW_BOX_TEXT_HEIGHT (w);
12800 if (NUMBERP (aggressive))
12801 {
12802 double float_amount = XFLOATINT (aggressive) * height;
12803 amount_to_scroll = float_amount;
12804 if (amount_to_scroll == 0 && float_amount > 0)
12805 amount_to_scroll = 1;
12806 }
12807 }
12808
12809 if (amount_to_scroll <= 0)
12810 return SCROLLING_FAILED;
12811
12812 move_it_vertically_backward (&it, amount_to_scroll);
12813 startp = it.current.pos;
12814 }
12815 }
12816
12817 /* Run window scroll functions. */
12818 startp = run_window_scroll_functions (window, startp);
12819
12820 /* Display the window. Give up if new fonts are loaded, or if point
12821 doesn't appear. */
12822 if (!try_window (window, startp, 0))
12823 rc = SCROLLING_NEED_LARGER_MATRICES;
12824 else if (w->cursor.vpos < 0)
12825 {
12826 clear_glyph_matrix (w->desired_matrix);
12827 rc = SCROLLING_FAILED;
12828 }
12829 else
12830 {
12831 /* Maybe forget recorded base line for line number display. */
12832 if (!just_this_one_p
12833 || current_buffer->clip_changed
12834 || BEG_UNCHANGED < CHARPOS (startp))
12835 w->base_line_number = Qnil;
12836
12837 /* If cursor ends up on a partially visible line,
12838 treat that as being off the bottom of the screen. */
12839 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1, 0))
12840 {
12841 clear_glyph_matrix (w->desired_matrix);
12842 ++extra_scroll_margin_lines;
12843 goto too_near_end;
12844 }
12845 rc = SCROLLING_SUCCESS;
12846 }
12847
12848 return rc;
12849 }
12850
12851
12852 /* Compute a suitable window start for window W if display of W starts
12853 on a continuation line. Value is non-zero if a new window start
12854 was computed.
12855
12856 The new window start will be computed, based on W's width, starting
12857 from the start of the continued line. It is the start of the
12858 screen line with the minimum distance from the old start W->start. */
12859
12860 static int
12861 compute_window_start_on_continuation_line (w)
12862 struct window *w;
12863 {
12864 struct text_pos pos, start_pos;
12865 int window_start_changed_p = 0;
12866
12867 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
12868
12869 /* If window start is on a continuation line... Window start may be
12870 < BEGV in case there's invisible text at the start of the
12871 buffer (M-x rmail, for example). */
12872 if (CHARPOS (start_pos) > BEGV
12873 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
12874 {
12875 struct it it;
12876 struct glyph_row *row;
12877
12878 /* Handle the case that the window start is out of range. */
12879 if (CHARPOS (start_pos) < BEGV)
12880 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
12881 else if (CHARPOS (start_pos) > ZV)
12882 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
12883
12884 /* Find the start of the continued line. This should be fast
12885 because scan_buffer is fast (newline cache). */
12886 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
12887 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
12888 row, DEFAULT_FACE_ID);
12889 reseat_at_previous_visible_line_start (&it);
12890
12891 /* If the line start is "too far" away from the window start,
12892 say it takes too much time to compute a new window start. */
12893 if (CHARPOS (start_pos) - IT_CHARPOS (it)
12894 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
12895 {
12896 int min_distance, distance;
12897
12898 /* Move forward by display lines to find the new window
12899 start. If window width was enlarged, the new start can
12900 be expected to be > the old start. If window width was
12901 decreased, the new window start will be < the old start.
12902 So, we're looking for the display line start with the
12903 minimum distance from the old window start. */
12904 pos = it.current.pos;
12905 min_distance = INFINITY;
12906 while ((distance = eabs (CHARPOS (start_pos) - IT_CHARPOS (it))),
12907 distance < min_distance)
12908 {
12909 min_distance = distance;
12910 pos = it.current.pos;
12911 move_it_by_lines (&it, 1, 0);
12912 }
12913
12914 /* Set the window start there. */
12915 SET_MARKER_FROM_TEXT_POS (w->start, pos);
12916 window_start_changed_p = 1;
12917 }
12918 }
12919
12920 return window_start_changed_p;
12921 }
12922
12923
12924 /* Try cursor movement in case text has not changed in window WINDOW,
12925 with window start STARTP. Value is
12926
12927 CURSOR_MOVEMENT_SUCCESS if successful
12928
12929 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
12930
12931 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
12932 display. *SCROLL_STEP is set to 1, under certain circumstances, if
12933 we want to scroll as if scroll-step were set to 1. See the code.
12934
12935 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
12936 which case we have to abort this redisplay, and adjust matrices
12937 first. */
12938
12939 enum
12940 {
12941 CURSOR_MOVEMENT_SUCCESS,
12942 CURSOR_MOVEMENT_CANNOT_BE_USED,
12943 CURSOR_MOVEMENT_MUST_SCROLL,
12944 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
12945 };
12946
12947 static int
12948 try_cursor_movement (window, startp, scroll_step)
12949 Lisp_Object window;
12950 struct text_pos startp;
12951 int *scroll_step;
12952 {
12953 struct window *w = XWINDOW (window);
12954 struct frame *f = XFRAME (w->frame);
12955 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
12956
12957 #if GLYPH_DEBUG
12958 if (inhibit_try_cursor_movement)
12959 return rc;
12960 #endif
12961
12962 /* Handle case where text has not changed, only point, and it has
12963 not moved off the frame. */
12964 if (/* Point may be in this window. */
12965 PT >= CHARPOS (startp)
12966 /* Selective display hasn't changed. */
12967 && !current_buffer->clip_changed
12968 /* Function force-mode-line-update is used to force a thorough
12969 redisplay. It sets either windows_or_buffers_changed or
12970 update_mode_lines. So don't take a shortcut here for these
12971 cases. */
12972 && !update_mode_lines
12973 && !windows_or_buffers_changed
12974 && !cursor_type_changed
12975 /* Can't use this case if highlighting a region. When a
12976 region exists, cursor movement has to do more than just
12977 set the cursor. */
12978 && !(!NILP (Vtransient_mark_mode)
12979 && !NILP (current_buffer->mark_active))
12980 && NILP (w->region_showing)
12981 && NILP (Vshow_trailing_whitespace)
12982 /* Right after splitting windows, last_point may be nil. */
12983 && INTEGERP (w->last_point)
12984 /* This code is not used for mini-buffer for the sake of the case
12985 of redisplaying to replace an echo area message; since in
12986 that case the mini-buffer contents per se are usually
12987 unchanged. This code is of no real use in the mini-buffer
12988 since the handling of this_line_start_pos, etc., in redisplay
12989 handles the same cases. */
12990 && !EQ (window, minibuf_window)
12991 /* When splitting windows or for new windows, it happens that
12992 redisplay is called with a nil window_end_vpos or one being
12993 larger than the window. This should really be fixed in
12994 window.c. I don't have this on my list, now, so we do
12995 approximately the same as the old redisplay code. --gerd. */
12996 && INTEGERP (w->window_end_vpos)
12997 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
12998 && (FRAME_WINDOW_P (f)
12999 || !overlay_arrow_in_current_buffer_p ()))
13000 {
13001 int this_scroll_margin, top_scroll_margin;
13002 struct glyph_row *row = NULL;
13003
13004 #if GLYPH_DEBUG
13005 debug_method_add (w, "cursor movement");
13006 #endif
13007
13008 /* Scroll if point within this distance from the top or bottom
13009 of the window. This is a pixel value. */
13010 if (scroll_margin > 0)
13011 {
13012 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
13013 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
13014 }
13015 else
13016 this_scroll_margin = 0;
13017
13018 top_scroll_margin = this_scroll_margin;
13019 if (WINDOW_WANTS_HEADER_LINE_P (w))
13020 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
13021
13022 /* Start with the row the cursor was displayed during the last
13023 not paused redisplay. Give up if that row is not valid. */
13024 if (w->last_cursor.vpos < 0
13025 || w->last_cursor.vpos >= w->current_matrix->nrows)
13026 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13027 else
13028 {
13029 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
13030 if (row->mode_line_p)
13031 ++row;
13032 if (!row->enabled_p)
13033 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13034 }
13035
13036 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
13037 {
13038 int scroll_p = 0;
13039 int last_y = window_text_bottom_y (w) - this_scroll_margin;
13040
13041 if (PT > XFASTINT (w->last_point))
13042 {
13043 /* Point has moved forward. */
13044 while (MATRIX_ROW_END_CHARPOS (row) < PT
13045 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
13046 {
13047 xassert (row->enabled_p);
13048 ++row;
13049 }
13050
13051 /* The end position of a row equals the start position
13052 of the next row. If PT is there, we would rather
13053 display it in the next line. */
13054 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
13055 && MATRIX_ROW_END_CHARPOS (row) == PT
13056 && !cursor_row_p (w, row))
13057 ++row;
13058
13059 /* If within the scroll margin, scroll. Note that
13060 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
13061 the next line would be drawn, and that
13062 this_scroll_margin can be zero. */
13063 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
13064 || PT > MATRIX_ROW_END_CHARPOS (row)
13065 /* Line is completely visible last line in window
13066 and PT is to be set in the next line. */
13067 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
13068 && PT == MATRIX_ROW_END_CHARPOS (row)
13069 && !row->ends_at_zv_p
13070 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
13071 scroll_p = 1;
13072 }
13073 else if (PT < XFASTINT (w->last_point))
13074 {
13075 /* Cursor has to be moved backward. Note that PT >=
13076 CHARPOS (startp) because of the outer if-statement. */
13077 while (!row->mode_line_p
13078 && (MATRIX_ROW_START_CHARPOS (row) > PT
13079 || (MATRIX_ROW_START_CHARPOS (row) == PT
13080 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
13081 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
13082 row > w->current_matrix->rows
13083 && (row-1)->ends_in_newline_from_string_p))))
13084 && (row->y > top_scroll_margin
13085 || CHARPOS (startp) == BEGV))
13086 {
13087 xassert (row->enabled_p);
13088 --row;
13089 }
13090
13091 /* Consider the following case: Window starts at BEGV,
13092 there is invisible, intangible text at BEGV, so that
13093 display starts at some point START > BEGV. It can
13094 happen that we are called with PT somewhere between
13095 BEGV and START. Try to handle that case. */
13096 if (row < w->current_matrix->rows
13097 || row->mode_line_p)
13098 {
13099 row = w->current_matrix->rows;
13100 if (row->mode_line_p)
13101 ++row;
13102 }
13103
13104 /* Due to newlines in overlay strings, we may have to
13105 skip forward over overlay strings. */
13106 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
13107 && MATRIX_ROW_END_CHARPOS (row) == PT
13108 && !cursor_row_p (w, row))
13109 ++row;
13110
13111 /* If within the scroll margin, scroll. */
13112 if (row->y < top_scroll_margin
13113 && CHARPOS (startp) != BEGV)
13114 scroll_p = 1;
13115 }
13116 else
13117 {
13118 /* Cursor did not move. So don't scroll even if cursor line
13119 is partially visible, as it was so before. */
13120 rc = CURSOR_MOVEMENT_SUCCESS;
13121 }
13122
13123 if (PT < MATRIX_ROW_START_CHARPOS (row)
13124 || PT > MATRIX_ROW_END_CHARPOS (row))
13125 {
13126 /* if PT is not in the glyph row, give up. */
13127 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13128 }
13129 else if (rc != CURSOR_MOVEMENT_SUCCESS
13130 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
13131 && make_cursor_line_fully_visible_p)
13132 {
13133 if (PT == MATRIX_ROW_END_CHARPOS (row)
13134 && !row->ends_at_zv_p
13135 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
13136 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13137 else if (row->height > window_box_height (w))
13138 {
13139 /* If we end up in a partially visible line, let's
13140 make it fully visible, except when it's taller
13141 than the window, in which case we can't do much
13142 about it. */
13143 *scroll_step = 1;
13144 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13145 }
13146 else
13147 {
13148 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13149 if (!cursor_row_fully_visible_p (w, 0, 1))
13150 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13151 else
13152 rc = CURSOR_MOVEMENT_SUCCESS;
13153 }
13154 }
13155 else if (scroll_p)
13156 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13157 else
13158 {
13159 do
13160 {
13161 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
13162 {
13163 rc = CURSOR_MOVEMENT_SUCCESS;
13164 break;
13165 }
13166 ++row;
13167 }
13168 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
13169 && MATRIX_ROW_START_CHARPOS (row) == PT
13170 && cursor_row_p (w, row));
13171 }
13172 }
13173 }
13174
13175 return rc;
13176 }
13177
13178 void
13179 set_vertical_scroll_bar (w)
13180 struct window *w;
13181 {
13182 int start, end, whole;
13183
13184 /* Calculate the start and end positions for the current window.
13185 At some point, it would be nice to choose between scrollbars
13186 which reflect the whole buffer size, with special markers
13187 indicating narrowing, and scrollbars which reflect only the
13188 visible region.
13189
13190 Note that mini-buffers sometimes aren't displaying any text. */
13191 if (!MINI_WINDOW_P (w)
13192 || (w == XWINDOW (minibuf_window)
13193 && NILP (echo_area_buffer[0])))
13194 {
13195 struct buffer *buf = XBUFFER (w->buffer);
13196 whole = BUF_ZV (buf) - BUF_BEGV (buf);
13197 start = marker_position (w->start) - BUF_BEGV (buf);
13198 /* I don't think this is guaranteed to be right. For the
13199 moment, we'll pretend it is. */
13200 end = BUF_Z (buf) - XFASTINT (w->window_end_pos) - BUF_BEGV (buf);
13201
13202 if (end < start)
13203 end = start;
13204 if (whole < (end - start))
13205 whole = end - start;
13206 }
13207 else
13208 start = end = whole = 0;
13209
13210 /* Indicate what this scroll bar ought to be displaying now. */
13211 if (FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
13212 (*FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
13213 (w, end - start, whole, start);
13214 }
13215
13216
13217 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
13218 selected_window is redisplayed.
13219
13220 We can return without actually redisplaying the window if
13221 fonts_changed_p is nonzero. In that case, redisplay_internal will
13222 retry. */
13223
13224 static void
13225 redisplay_window (window, just_this_one_p)
13226 Lisp_Object window;
13227 int just_this_one_p;
13228 {
13229 struct window *w = XWINDOW (window);
13230 struct frame *f = XFRAME (w->frame);
13231 struct buffer *buffer = XBUFFER (w->buffer);
13232 struct buffer *old = current_buffer;
13233 struct text_pos lpoint, opoint, startp;
13234 int update_mode_line;
13235 int tem;
13236 struct it it;
13237 /* Record it now because it's overwritten. */
13238 int current_matrix_up_to_date_p = 0;
13239 int used_current_matrix_p = 0;
13240 /* This is less strict than current_matrix_up_to_date_p.
13241 It indictes that the buffer contents and narrowing are unchanged. */
13242 int buffer_unchanged_p = 0;
13243 int temp_scroll_step = 0;
13244 int count = SPECPDL_INDEX ();
13245 int rc;
13246 int centering_position = -1;
13247 int last_line_misfit = 0;
13248 int beg_unchanged, end_unchanged;
13249
13250 SET_TEXT_POS (lpoint, PT, PT_BYTE);
13251 opoint = lpoint;
13252
13253 /* W must be a leaf window here. */
13254 xassert (!NILP (w->buffer));
13255 #if GLYPH_DEBUG
13256 *w->desired_matrix->method = 0;
13257 #endif
13258
13259 restart:
13260 reconsider_clip_changes (w, buffer);
13261
13262 /* Has the mode line to be updated? */
13263 update_mode_line = (!NILP (w->update_mode_line)
13264 || update_mode_lines
13265 || buffer->clip_changed
13266 || buffer->prevent_redisplay_optimizations_p);
13267
13268 if (MINI_WINDOW_P (w))
13269 {
13270 if (w == XWINDOW (echo_area_window)
13271 && !NILP (echo_area_buffer[0]))
13272 {
13273 if (update_mode_line)
13274 /* We may have to update a tty frame's menu bar or a
13275 tool-bar. Example `M-x C-h C-h C-g'. */
13276 goto finish_menu_bars;
13277 else
13278 /* We've already displayed the echo area glyphs in this window. */
13279 goto finish_scroll_bars;
13280 }
13281 else if ((w != XWINDOW (minibuf_window)
13282 || minibuf_level == 0)
13283 /* When buffer is nonempty, redisplay window normally. */
13284 && BUF_Z (XBUFFER (w->buffer)) == BUF_BEG (XBUFFER (w->buffer))
13285 /* Quail displays non-mini buffers in minibuffer window.
13286 In that case, redisplay the window normally. */
13287 && !NILP (Fmemq (w->buffer, Vminibuffer_list)))
13288 {
13289 /* W is a mini-buffer window, but it's not active, so clear
13290 it. */
13291 int yb = window_text_bottom_y (w);
13292 struct glyph_row *row;
13293 int y;
13294
13295 for (y = 0, row = w->desired_matrix->rows;
13296 y < yb;
13297 y += row->height, ++row)
13298 blank_row (w, row, y);
13299 goto finish_scroll_bars;
13300 }
13301
13302 clear_glyph_matrix (w->desired_matrix);
13303 }
13304
13305 /* Otherwise set up data on this window; select its buffer and point
13306 value. */
13307 /* Really select the buffer, for the sake of buffer-local
13308 variables. */
13309 set_buffer_internal_1 (XBUFFER (w->buffer));
13310
13311 current_matrix_up_to_date_p
13312 = (!NILP (w->window_end_valid)
13313 && !current_buffer->clip_changed
13314 && !current_buffer->prevent_redisplay_optimizations_p
13315 && XFASTINT (w->last_modified) >= MODIFF
13316 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
13317
13318 /* Run the window-bottom-change-functions
13319 if it is possible that the text on the screen has changed
13320 (either due to modification of the text, or any other reason). */
13321 if (!current_matrix_up_to_date_p
13322 && !NILP (Vwindow_text_change_functions))
13323 {
13324 safe_run_hooks (Qwindow_text_change_functions);
13325 goto restart;
13326 }
13327
13328 beg_unchanged = BEG_UNCHANGED;
13329 end_unchanged = END_UNCHANGED;
13330
13331 SET_TEXT_POS (opoint, PT, PT_BYTE);
13332
13333 specbind (Qinhibit_point_motion_hooks, Qt);
13334
13335 buffer_unchanged_p
13336 = (!NILP (w->window_end_valid)
13337 && !current_buffer->clip_changed
13338 && XFASTINT (w->last_modified) >= MODIFF
13339 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
13340
13341 /* When windows_or_buffers_changed is non-zero, we can't rely on
13342 the window end being valid, so set it to nil there. */
13343 if (windows_or_buffers_changed)
13344 {
13345 /* If window starts on a continuation line, maybe adjust the
13346 window start in case the window's width changed. */
13347 if (XMARKER (w->start)->buffer == current_buffer)
13348 compute_window_start_on_continuation_line (w);
13349
13350 w->window_end_valid = Qnil;
13351 }
13352
13353 /* Some sanity checks. */
13354 CHECK_WINDOW_END (w);
13355 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
13356 abort ();
13357 if (BYTEPOS (opoint) < CHARPOS (opoint))
13358 abort ();
13359
13360 /* If %c is in mode line, update it if needed. */
13361 if (!NILP (w->column_number_displayed)
13362 /* This alternative quickly identifies a common case
13363 where no change is needed. */
13364 && !(PT == XFASTINT (w->last_point)
13365 && XFASTINT (w->last_modified) >= MODIFF
13366 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
13367 && (XFASTINT (w->column_number_displayed)
13368 != (int) current_column ())) /* iftc */
13369 update_mode_line = 1;
13370
13371 /* Count number of windows showing the selected buffer. An indirect
13372 buffer counts as its base buffer. */
13373 if (!just_this_one_p)
13374 {
13375 struct buffer *current_base, *window_base;
13376 current_base = current_buffer;
13377 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
13378 if (current_base->base_buffer)
13379 current_base = current_base->base_buffer;
13380 if (window_base->base_buffer)
13381 window_base = window_base->base_buffer;
13382 if (current_base == window_base)
13383 buffer_shared++;
13384 }
13385
13386 /* Point refers normally to the selected window. For any other
13387 window, set up appropriate value. */
13388 if (!EQ (window, selected_window))
13389 {
13390 int new_pt = XMARKER (w->pointm)->charpos;
13391 int new_pt_byte = marker_byte_position (w->pointm);
13392 if (new_pt < BEGV)
13393 {
13394 new_pt = BEGV;
13395 new_pt_byte = BEGV_BYTE;
13396 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
13397 }
13398 else if (new_pt > (ZV - 1))
13399 {
13400 new_pt = ZV;
13401 new_pt_byte = ZV_BYTE;
13402 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
13403 }
13404
13405 /* We don't use SET_PT so that the point-motion hooks don't run. */
13406 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
13407 }
13408
13409 /* If any of the character widths specified in the display table
13410 have changed, invalidate the width run cache. It's true that
13411 this may be a bit late to catch such changes, but the rest of
13412 redisplay goes (non-fatally) haywire when the display table is
13413 changed, so why should we worry about doing any better? */
13414 if (current_buffer->width_run_cache)
13415 {
13416 struct Lisp_Char_Table *disptab = buffer_display_table ();
13417
13418 if (! disptab_matches_widthtab (disptab,
13419 XVECTOR (current_buffer->width_table)))
13420 {
13421 invalidate_region_cache (current_buffer,
13422 current_buffer->width_run_cache,
13423 BEG, Z);
13424 recompute_width_table (current_buffer, disptab);
13425 }
13426 }
13427
13428 /* If window-start is screwed up, choose a new one. */
13429 if (XMARKER (w->start)->buffer != current_buffer)
13430 goto recenter;
13431
13432 SET_TEXT_POS_FROM_MARKER (startp, w->start);
13433
13434 /* If someone specified a new starting point but did not insist,
13435 check whether it can be used. */
13436 if (!NILP (w->optional_new_start)
13437 && CHARPOS (startp) >= BEGV
13438 && CHARPOS (startp) <= ZV)
13439 {
13440 w->optional_new_start = Qnil;
13441 start_display (&it, w, startp);
13442 move_it_to (&it, PT, 0, it.last_visible_y, -1,
13443 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
13444 if (IT_CHARPOS (it) == PT)
13445 w->force_start = Qt;
13446 /* IT may overshoot PT if text at PT is invisible. */
13447 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
13448 w->force_start = Qt;
13449 }
13450
13451 force_start:
13452
13453 /* Handle case where place to start displaying has been specified,
13454 unless the specified location is outside the accessible range. */
13455 if (!NILP (w->force_start)
13456 || w->frozen_window_start_p)
13457 {
13458 /* We set this later on if we have to adjust point. */
13459 int new_vpos = -1;
13460
13461 w->force_start = Qnil;
13462 w->vscroll = 0;
13463 w->window_end_valid = Qnil;
13464
13465 /* Forget any recorded base line for line number display. */
13466 if (!buffer_unchanged_p)
13467 w->base_line_number = Qnil;
13468
13469 /* Redisplay the mode line. Select the buffer properly for that.
13470 Also, run the hook window-scroll-functions
13471 because we have scrolled. */
13472 /* Note, we do this after clearing force_start because
13473 if there's an error, it is better to forget about force_start
13474 than to get into an infinite loop calling the hook functions
13475 and having them get more errors. */
13476 if (!update_mode_line
13477 || ! NILP (Vwindow_scroll_functions))
13478 {
13479 update_mode_line = 1;
13480 w->update_mode_line = Qt;
13481 startp = run_window_scroll_functions (window, startp);
13482 }
13483
13484 w->last_modified = make_number (0);
13485 w->last_overlay_modified = make_number (0);
13486 if (CHARPOS (startp) < BEGV)
13487 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
13488 else if (CHARPOS (startp) > ZV)
13489 SET_TEXT_POS (startp, ZV, ZV_BYTE);
13490
13491 /* Redisplay, then check if cursor has been set during the
13492 redisplay. Give up if new fonts were loaded. */
13493 /* We used to issue a CHECK_MARGINS argument to try_window here,
13494 but this causes scrolling to fail when point begins inside
13495 the scroll margin (bug#148) -- cyd */
13496 if (!try_window (window, startp, 0))
13497 {
13498 w->force_start = Qt;
13499 clear_glyph_matrix (w->desired_matrix);
13500 goto need_larger_matrices;
13501 }
13502
13503 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
13504 {
13505 /* If point does not appear, try to move point so it does
13506 appear. The desired matrix has been built above, so we
13507 can use it here. */
13508 new_vpos = window_box_height (w) / 2;
13509 }
13510
13511 if (!cursor_row_fully_visible_p (w, 0, 0))
13512 {
13513 /* Point does appear, but on a line partly visible at end of window.
13514 Move it back to a fully-visible line. */
13515 new_vpos = window_box_height (w);
13516 }
13517
13518 /* If we need to move point for either of the above reasons,
13519 now actually do it. */
13520 if (new_vpos >= 0)
13521 {
13522 struct glyph_row *row;
13523
13524 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
13525 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
13526 ++row;
13527
13528 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
13529 MATRIX_ROW_START_BYTEPOS (row));
13530
13531 if (w != XWINDOW (selected_window))
13532 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
13533 else if (current_buffer == old)
13534 SET_TEXT_POS (lpoint, PT, PT_BYTE);
13535
13536 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
13537
13538 /* If we are highlighting the region, then we just changed
13539 the region, so redisplay to show it. */
13540 if (!NILP (Vtransient_mark_mode)
13541 && !NILP (current_buffer->mark_active))
13542 {
13543 clear_glyph_matrix (w->desired_matrix);
13544 if (!try_window (window, startp, 0))
13545 goto need_larger_matrices;
13546 }
13547 }
13548
13549 #if GLYPH_DEBUG
13550 debug_method_add (w, "forced window start");
13551 #endif
13552 goto done;
13553 }
13554
13555 /* Handle case where text has not changed, only point, and it has
13556 not moved off the frame, and we are not retrying after hscroll.
13557 (current_matrix_up_to_date_p is nonzero when retrying.) */
13558 if (current_matrix_up_to_date_p
13559 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
13560 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
13561 {
13562 switch (rc)
13563 {
13564 case CURSOR_MOVEMENT_SUCCESS:
13565 used_current_matrix_p = 1;
13566 goto done;
13567
13568 #if 0 /* try_cursor_movement never returns this value. */
13569 case CURSOR_MOVEMENT_NEED_LARGER_MATRICES:
13570 goto need_larger_matrices;
13571 #endif
13572
13573 case CURSOR_MOVEMENT_MUST_SCROLL:
13574 goto try_to_scroll;
13575
13576 default:
13577 abort ();
13578 }
13579 }
13580 /* If current starting point was originally the beginning of a line
13581 but no longer is, find a new starting point. */
13582 else if (!NILP (w->start_at_line_beg)
13583 && !(CHARPOS (startp) <= BEGV
13584 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
13585 {
13586 #if GLYPH_DEBUG
13587 debug_method_add (w, "recenter 1");
13588 #endif
13589 goto recenter;
13590 }
13591
13592 /* Try scrolling with try_window_id. Value is > 0 if update has
13593 been done, it is -1 if we know that the same window start will
13594 not work. It is 0 if unsuccessful for some other reason. */
13595 else if ((tem = try_window_id (w)) != 0)
13596 {
13597 #if GLYPH_DEBUG
13598 debug_method_add (w, "try_window_id %d", tem);
13599 #endif
13600
13601 if (fonts_changed_p)
13602 goto need_larger_matrices;
13603 if (tem > 0)
13604 goto done;
13605
13606 /* Otherwise try_window_id has returned -1 which means that we
13607 don't want the alternative below this comment to execute. */
13608 }
13609 else if (CHARPOS (startp) >= BEGV
13610 && CHARPOS (startp) <= ZV
13611 && PT >= CHARPOS (startp)
13612 && (CHARPOS (startp) < ZV
13613 /* Avoid starting at end of buffer. */
13614 || CHARPOS (startp) == BEGV
13615 || (XFASTINT (w->last_modified) >= MODIFF
13616 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
13617 {
13618
13619 /* If first window line is a continuation line, and window start
13620 is inside the modified region, but the first change is before
13621 current window start, we must select a new window start.
13622
13623 However, if this is the result of a down-mouse event (e.g. by
13624 extending the mouse-drag-overlay), we don't want to select a
13625 new window start, since that would change the position under
13626 the mouse, resulting in an unwanted mouse-movement rather
13627 than a simple mouse-click. */
13628 if (NILP (w->start_at_line_beg)
13629 && NILP (do_mouse_tracking)
13630 && CHARPOS (startp) > BEGV
13631 && CHARPOS (startp) > BEG + beg_unchanged
13632 && CHARPOS (startp) <= Z - end_unchanged
13633 /* Even if w->start_at_line_beg is nil, a new window may
13634 start at a line_beg, since that's how set_buffer_window
13635 sets it. So, we need to check the return value of
13636 compute_window_start_on_continuation_line. (See also
13637 bug#197). */
13638 && XMARKER (w->start)->buffer == current_buffer
13639 && compute_window_start_on_continuation_line (w))
13640 {
13641 w->force_start = Qt;
13642 SET_TEXT_POS_FROM_MARKER (startp, w->start);
13643 goto force_start;
13644 }
13645
13646 #if GLYPH_DEBUG
13647 debug_method_add (w, "same window start");
13648 #endif
13649
13650 /* Try to redisplay starting at same place as before.
13651 If point has not moved off frame, accept the results. */
13652 if (!current_matrix_up_to_date_p
13653 /* Don't use try_window_reusing_current_matrix in this case
13654 because a window scroll function can have changed the
13655 buffer. */
13656 || !NILP (Vwindow_scroll_functions)
13657 || MINI_WINDOW_P (w)
13658 || !(used_current_matrix_p
13659 = try_window_reusing_current_matrix (w)))
13660 {
13661 IF_DEBUG (debug_method_add (w, "1"));
13662 if (try_window (window, startp, 1) < 0)
13663 /* -1 means we need to scroll.
13664 0 means we need new matrices, but fonts_changed_p
13665 is set in that case, so we will detect it below. */
13666 goto try_to_scroll;
13667 }
13668
13669 if (fonts_changed_p)
13670 goto need_larger_matrices;
13671
13672 if (w->cursor.vpos >= 0)
13673 {
13674 if (!just_this_one_p
13675 || current_buffer->clip_changed
13676 || BEG_UNCHANGED < CHARPOS (startp))
13677 /* Forget any recorded base line for line number display. */
13678 w->base_line_number = Qnil;
13679
13680 if (!cursor_row_fully_visible_p (w, 1, 0))
13681 {
13682 clear_glyph_matrix (w->desired_matrix);
13683 last_line_misfit = 1;
13684 }
13685 /* Drop through and scroll. */
13686 else
13687 goto done;
13688 }
13689 else
13690 clear_glyph_matrix (w->desired_matrix);
13691 }
13692
13693 try_to_scroll:
13694
13695 w->last_modified = make_number (0);
13696 w->last_overlay_modified = make_number (0);
13697
13698 /* Redisplay the mode line. Select the buffer properly for that. */
13699 if (!update_mode_line)
13700 {
13701 update_mode_line = 1;
13702 w->update_mode_line = Qt;
13703 }
13704
13705 /* Try to scroll by specified few lines. */
13706 if ((scroll_conservatively
13707 || scroll_step
13708 || temp_scroll_step
13709 || NUMBERP (current_buffer->scroll_up_aggressively)
13710 || NUMBERP (current_buffer->scroll_down_aggressively))
13711 && !current_buffer->clip_changed
13712 && CHARPOS (startp) >= BEGV
13713 && CHARPOS (startp) <= ZV)
13714 {
13715 /* The function returns -1 if new fonts were loaded, 1 if
13716 successful, 0 if not successful. */
13717 int rc = try_scrolling (window, just_this_one_p,
13718 scroll_conservatively,
13719 scroll_step,
13720 temp_scroll_step, last_line_misfit);
13721 switch (rc)
13722 {
13723 case SCROLLING_SUCCESS:
13724 goto done;
13725
13726 case SCROLLING_NEED_LARGER_MATRICES:
13727 goto need_larger_matrices;
13728
13729 case SCROLLING_FAILED:
13730 break;
13731
13732 default:
13733 abort ();
13734 }
13735 }
13736
13737 /* Finally, just choose place to start which centers point */
13738
13739 recenter:
13740 if (centering_position < 0)
13741 centering_position = window_box_height (w) / 2;
13742
13743 #if GLYPH_DEBUG
13744 debug_method_add (w, "recenter");
13745 #endif
13746
13747 /* w->vscroll = 0; */
13748
13749 /* Forget any previously recorded base line for line number display. */
13750 if (!buffer_unchanged_p)
13751 w->base_line_number = Qnil;
13752
13753 /* Move backward half the height of the window. */
13754 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
13755 it.current_y = it.last_visible_y;
13756 move_it_vertically_backward (&it, centering_position);
13757 xassert (IT_CHARPOS (it) >= BEGV);
13758
13759 /* The function move_it_vertically_backward may move over more
13760 than the specified y-distance. If it->w is small, e.g. a
13761 mini-buffer window, we may end up in front of the window's
13762 display area. Start displaying at the start of the line
13763 containing PT in this case. */
13764 if (it.current_y <= 0)
13765 {
13766 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
13767 move_it_vertically_backward (&it, 0);
13768 it.current_y = 0;
13769 }
13770
13771 it.current_x = it.hpos = 0;
13772
13773 /* Set startp here explicitly in case that helps avoid an infinite loop
13774 in case the window-scroll-functions functions get errors. */
13775 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
13776
13777 /* Run scroll hooks. */
13778 startp = run_window_scroll_functions (window, it.current.pos);
13779
13780 /* Redisplay the window. */
13781 if (!current_matrix_up_to_date_p
13782 || windows_or_buffers_changed
13783 || cursor_type_changed
13784 /* Don't use try_window_reusing_current_matrix in this case
13785 because it can have changed the buffer. */
13786 || !NILP (Vwindow_scroll_functions)
13787 || !just_this_one_p
13788 || MINI_WINDOW_P (w)
13789 || !(used_current_matrix_p
13790 = try_window_reusing_current_matrix (w)))
13791 try_window (window, startp, 0);
13792
13793 /* If new fonts have been loaded (due to fontsets), give up. We
13794 have to start a new redisplay since we need to re-adjust glyph
13795 matrices. */
13796 if (fonts_changed_p)
13797 goto need_larger_matrices;
13798
13799 /* If cursor did not appear assume that the middle of the window is
13800 in the first line of the window. Do it again with the next line.
13801 (Imagine a window of height 100, displaying two lines of height
13802 60. Moving back 50 from it->last_visible_y will end in the first
13803 line.) */
13804 if (w->cursor.vpos < 0)
13805 {
13806 if (!NILP (w->window_end_valid)
13807 && PT >= Z - XFASTINT (w->window_end_pos))
13808 {
13809 clear_glyph_matrix (w->desired_matrix);
13810 move_it_by_lines (&it, 1, 0);
13811 try_window (window, it.current.pos, 0);
13812 }
13813 else if (PT < IT_CHARPOS (it))
13814 {
13815 clear_glyph_matrix (w->desired_matrix);
13816 move_it_by_lines (&it, -1, 0);
13817 try_window (window, it.current.pos, 0);
13818 }
13819 else
13820 {
13821 /* Not much we can do about it. */
13822 }
13823 }
13824
13825 /* Consider the following case: Window starts at BEGV, there is
13826 invisible, intangible text at BEGV, so that display starts at
13827 some point START > BEGV. It can happen that we are called with
13828 PT somewhere between BEGV and START. Try to handle that case. */
13829 if (w->cursor.vpos < 0)
13830 {
13831 struct glyph_row *row = w->current_matrix->rows;
13832 if (row->mode_line_p)
13833 ++row;
13834 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13835 }
13836
13837 if (!cursor_row_fully_visible_p (w, 0, 0))
13838 {
13839 /* If vscroll is enabled, disable it and try again. */
13840 if (w->vscroll)
13841 {
13842 w->vscroll = 0;
13843 clear_glyph_matrix (w->desired_matrix);
13844 goto recenter;
13845 }
13846
13847 /* If centering point failed to make the whole line visible,
13848 put point at the top instead. That has to make the whole line
13849 visible, if it can be done. */
13850 if (centering_position == 0)
13851 goto done;
13852
13853 clear_glyph_matrix (w->desired_matrix);
13854 centering_position = 0;
13855 goto recenter;
13856 }
13857
13858 done:
13859
13860 SET_TEXT_POS_FROM_MARKER (startp, w->start);
13861 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
13862 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
13863 ? Qt : Qnil);
13864
13865 /* Display the mode line, if we must. */
13866 if ((update_mode_line
13867 /* If window not full width, must redo its mode line
13868 if (a) the window to its side is being redone and
13869 (b) we do a frame-based redisplay. This is a consequence
13870 of how inverted lines are drawn in frame-based redisplay. */
13871 || (!just_this_one_p
13872 && !FRAME_WINDOW_P (f)
13873 && !WINDOW_FULL_WIDTH_P (w))
13874 /* Line number to display. */
13875 || INTEGERP (w->base_line_pos)
13876 /* Column number is displayed and different from the one displayed. */
13877 || (!NILP (w->column_number_displayed)
13878 && (XFASTINT (w->column_number_displayed)
13879 != (int) current_column ()))) /* iftc */
13880 /* This means that the window has a mode line. */
13881 && (WINDOW_WANTS_MODELINE_P (w)
13882 || WINDOW_WANTS_HEADER_LINE_P (w)))
13883 {
13884 display_mode_lines (w);
13885
13886 /* If mode line height has changed, arrange for a thorough
13887 immediate redisplay using the correct mode line height. */
13888 if (WINDOW_WANTS_MODELINE_P (w)
13889 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
13890 {
13891 fonts_changed_p = 1;
13892 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
13893 = DESIRED_MODE_LINE_HEIGHT (w);
13894 }
13895
13896 /* If top line height has changed, arrange for a thorough
13897 immediate redisplay using the correct mode line height. */
13898 if (WINDOW_WANTS_HEADER_LINE_P (w)
13899 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
13900 {
13901 fonts_changed_p = 1;
13902 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
13903 = DESIRED_HEADER_LINE_HEIGHT (w);
13904 }
13905
13906 if (fonts_changed_p)
13907 goto need_larger_matrices;
13908 }
13909
13910 if (!line_number_displayed
13911 && !BUFFERP (w->base_line_pos))
13912 {
13913 w->base_line_pos = Qnil;
13914 w->base_line_number = Qnil;
13915 }
13916
13917 finish_menu_bars:
13918
13919 /* When we reach a frame's selected window, redo the frame's menu bar. */
13920 if (update_mode_line
13921 && EQ (FRAME_SELECTED_WINDOW (f), window))
13922 {
13923 int redisplay_menu_p = 0;
13924 int redisplay_tool_bar_p = 0;
13925
13926 if (FRAME_WINDOW_P (f))
13927 {
13928 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
13929 || defined (HAVE_NS) || defined (USE_GTK)
13930 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
13931 #else
13932 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
13933 #endif
13934 }
13935 else
13936 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
13937
13938 if (redisplay_menu_p)
13939 display_menu_bar (w);
13940
13941 #ifdef HAVE_WINDOW_SYSTEM
13942 if (FRAME_WINDOW_P (f))
13943 {
13944 #if defined (USE_GTK) || defined (HAVE_NS) || USE_MAC_TOOLBAR
13945 redisplay_tool_bar_p = FRAME_EXTERNAL_TOOL_BAR (f);
13946 #else
13947 redisplay_tool_bar_p = WINDOWP (f->tool_bar_window)
13948 && (FRAME_TOOL_BAR_LINES (f) > 0
13949 || !NILP (Vauto_resize_tool_bars));
13950 #endif
13951
13952 if (redisplay_tool_bar_p && redisplay_tool_bar (f))
13953 {
13954 extern int ignore_mouse_drag_p;
13955 ignore_mouse_drag_p = 1;
13956 }
13957 }
13958 #endif
13959 }
13960
13961 #ifdef HAVE_WINDOW_SYSTEM
13962 if (FRAME_WINDOW_P (f)
13963 && update_window_fringes (w, (just_this_one_p
13964 || (!used_current_matrix_p && !overlay_arrow_seen)
13965 || w->pseudo_window_p)))
13966 {
13967 update_begin (f);
13968 BLOCK_INPUT;
13969 if (draw_window_fringes (w, 1))
13970 x_draw_vertical_border (w);
13971 UNBLOCK_INPUT;
13972 update_end (f);
13973 }
13974 #endif /* HAVE_WINDOW_SYSTEM */
13975
13976 /* We go to this label, with fonts_changed_p nonzero,
13977 if it is necessary to try again using larger glyph matrices.
13978 We have to redeem the scroll bar even in this case,
13979 because the loop in redisplay_internal expects that. */
13980 need_larger_matrices:
13981 ;
13982 finish_scroll_bars:
13983
13984 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
13985 {
13986 /* Set the thumb's position and size. */
13987 set_vertical_scroll_bar (w);
13988
13989 /* Note that we actually used the scroll bar attached to this
13990 window, so it shouldn't be deleted at the end of redisplay. */
13991 if (FRAME_TERMINAL (f)->redeem_scroll_bar_hook)
13992 (*FRAME_TERMINAL (f)->redeem_scroll_bar_hook) (w);
13993 }
13994
13995 /* Restore current_buffer and value of point in it. */
13996 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
13997 set_buffer_internal_1 (old);
13998 /* Avoid an abort in TEMP_SET_PT_BOTH if the buffer has become
13999 shorter. This can be caused by log truncation in *Messages*. */
14000 if (CHARPOS (lpoint) <= ZV)
14001 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
14002
14003 unbind_to (count, Qnil);
14004 }
14005
14006
14007 /* Build the complete desired matrix of WINDOW with a window start
14008 buffer position POS.
14009
14010 Value is 1 if successful. It is zero if fonts were loaded during
14011 redisplay which makes re-adjusting glyph matrices necessary, and -1
14012 if point would appear in the scroll margins.
14013 (We check that only if CHECK_MARGINS is nonzero. */
14014
14015 int
14016 try_window (window, pos, check_margins)
14017 Lisp_Object window;
14018 struct text_pos pos;
14019 int check_margins;
14020 {
14021 struct window *w = XWINDOW (window);
14022 struct it it;
14023 struct glyph_row *last_text_row = NULL;
14024 struct frame *f = XFRAME (w->frame);
14025
14026 /* Make POS the new window start. */
14027 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
14028
14029 /* Mark cursor position as unknown. No overlay arrow seen. */
14030 w->cursor.vpos = -1;
14031 overlay_arrow_seen = 0;
14032
14033 /* Initialize iterator and info to start at POS. */
14034 start_display (&it, w, pos);
14035
14036 /* Display all lines of W. */
14037 while (it.current_y < it.last_visible_y)
14038 {
14039 if (display_line (&it))
14040 last_text_row = it.glyph_row - 1;
14041 if (fonts_changed_p)
14042 return 0;
14043 }
14044
14045 /* Don't let the cursor end in the scroll margins. */
14046 if (check_margins
14047 && !MINI_WINDOW_P (w))
14048 {
14049 int this_scroll_margin;
14050
14051 if (scroll_margin > 0)
14052 {
14053 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
14054 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
14055 }
14056 else
14057 this_scroll_margin = 0;
14058
14059 if ((w->cursor.y >= 0 /* not vscrolled */
14060 && w->cursor.y < this_scroll_margin
14061 && CHARPOS (pos) > BEGV
14062 && IT_CHARPOS (it) < ZV)
14063 /* rms: considering make_cursor_line_fully_visible_p here
14064 seems to give wrong results. We don't want to recenter
14065 when the last line is partly visible, we want to allow
14066 that case to be handled in the usual way. */
14067 || w->cursor.y > it.last_visible_y - this_scroll_margin - 1)
14068 {
14069 w->cursor.vpos = -1;
14070 clear_glyph_matrix (w->desired_matrix);
14071 return -1;
14072 }
14073 }
14074
14075 /* If bottom moved off end of frame, change mode line percentage. */
14076 if (XFASTINT (w->window_end_pos) <= 0
14077 && Z != IT_CHARPOS (it))
14078 w->update_mode_line = Qt;
14079
14080 /* Set window_end_pos to the offset of the last character displayed
14081 on the window from the end of current_buffer. Set
14082 window_end_vpos to its row number. */
14083 if (last_text_row)
14084 {
14085 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
14086 w->window_end_bytepos
14087 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
14088 w->window_end_pos
14089 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
14090 w->window_end_vpos
14091 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
14092 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
14093 ->displays_text_p);
14094 }
14095 else
14096 {
14097 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
14098 w->window_end_pos = make_number (Z - ZV);
14099 w->window_end_vpos = make_number (0);
14100 }
14101
14102 /* But that is not valid info until redisplay finishes. */
14103 w->window_end_valid = Qnil;
14104 return 1;
14105 }
14106
14107
14108 \f
14109 /************************************************************************
14110 Window redisplay reusing current matrix when buffer has not changed
14111 ************************************************************************/
14112
14113 /* Try redisplay of window W showing an unchanged buffer with a
14114 different window start than the last time it was displayed by
14115 reusing its current matrix. Value is non-zero if successful.
14116 W->start is the new window start. */
14117
14118 static int
14119 try_window_reusing_current_matrix (w)
14120 struct window *w;
14121 {
14122 struct frame *f = XFRAME (w->frame);
14123 struct glyph_row *row, *bottom_row;
14124 struct it it;
14125 struct run run;
14126 struct text_pos start, new_start;
14127 int nrows_scrolled, i;
14128 struct glyph_row *last_text_row;
14129 struct glyph_row *last_reused_text_row;
14130 struct glyph_row *start_row;
14131 int start_vpos, min_y, max_y;
14132
14133 #if GLYPH_DEBUG
14134 if (inhibit_try_window_reusing)
14135 return 0;
14136 #endif
14137
14138 if (/* This function doesn't handle terminal frames. */
14139 !FRAME_WINDOW_P (f)
14140 /* Don't try to reuse the display if windows have been split
14141 or such. */
14142 || windows_or_buffers_changed
14143 || cursor_type_changed)
14144 return 0;
14145
14146 /* Can't do this if region may have changed. */
14147 if ((!NILP (Vtransient_mark_mode)
14148 && !NILP (current_buffer->mark_active))
14149 || !NILP (w->region_showing)
14150 || !NILP (Vshow_trailing_whitespace))
14151 return 0;
14152
14153 /* If top-line visibility has changed, give up. */
14154 if (WINDOW_WANTS_HEADER_LINE_P (w)
14155 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
14156 return 0;
14157
14158 /* Give up if old or new display is scrolled vertically. We could
14159 make this function handle this, but right now it doesn't. */
14160 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
14161 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
14162 return 0;
14163
14164 /* The variable new_start now holds the new window start. The old
14165 start `start' can be determined from the current matrix. */
14166 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
14167 start = start_row->start.pos;
14168 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
14169
14170 /* Clear the desired matrix for the display below. */
14171 clear_glyph_matrix (w->desired_matrix);
14172
14173 if (CHARPOS (new_start) <= CHARPOS (start))
14174 {
14175 int first_row_y;
14176
14177 /* Don't use this method if the display starts with an ellipsis
14178 displayed for invisible text. It's not easy to handle that case
14179 below, and it's certainly not worth the effort since this is
14180 not a frequent case. */
14181 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
14182 return 0;
14183
14184 IF_DEBUG (debug_method_add (w, "twu1"));
14185
14186 /* Display up to a row that can be reused. The variable
14187 last_text_row is set to the last row displayed that displays
14188 text. Note that it.vpos == 0 if or if not there is a
14189 header-line; it's not the same as the MATRIX_ROW_VPOS! */
14190 start_display (&it, w, new_start);
14191 first_row_y = it.current_y;
14192 w->cursor.vpos = -1;
14193 last_text_row = last_reused_text_row = NULL;
14194
14195 while (it.current_y < it.last_visible_y
14196 && !fonts_changed_p)
14197 {
14198 /* If we have reached into the characters in the START row,
14199 that means the line boundaries have changed. So we
14200 can't start copying with the row START. Maybe it will
14201 work to start copying with the following row. */
14202 while (IT_CHARPOS (it) > CHARPOS (start))
14203 {
14204 /* Advance to the next row as the "start". */
14205 start_row++;
14206 start = start_row->start.pos;
14207 /* If there are no more rows to try, or just one, give up. */
14208 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
14209 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
14210 || CHARPOS (start) == ZV)
14211 {
14212 clear_glyph_matrix (w->desired_matrix);
14213 return 0;
14214 }
14215
14216 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
14217 }
14218 /* If we have reached alignment,
14219 we can copy the rest of the rows. */
14220 if (IT_CHARPOS (it) == CHARPOS (start))
14221 break;
14222
14223 if (display_line (&it))
14224 last_text_row = it.glyph_row - 1;
14225 }
14226
14227 /* A value of current_y < last_visible_y means that we stopped
14228 at the previous window start, which in turn means that we
14229 have at least one reusable row. */
14230 if (it.current_y < it.last_visible_y)
14231 {
14232 /* IT.vpos always starts from 0; it counts text lines. */
14233 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
14234
14235 /* Find PT if not already found in the lines displayed. */
14236 if (w->cursor.vpos < 0)
14237 {
14238 int dy = it.current_y - start_row->y;
14239
14240 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
14241 row = row_containing_pos (w, PT, row, NULL, dy);
14242 if (row)
14243 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
14244 dy, nrows_scrolled);
14245 else
14246 {
14247 clear_glyph_matrix (w->desired_matrix);
14248 return 0;
14249 }
14250 }
14251
14252 /* Scroll the display. Do it before the current matrix is
14253 changed. The problem here is that update has not yet
14254 run, i.e. part of the current matrix is not up to date.
14255 scroll_run_hook will clear the cursor, and use the
14256 current matrix to get the height of the row the cursor is
14257 in. */
14258 run.current_y = start_row->y;
14259 run.desired_y = it.current_y;
14260 run.height = it.last_visible_y - it.current_y;
14261
14262 if (run.height > 0 && run.current_y != run.desired_y)
14263 {
14264 update_begin (f);
14265 FRAME_RIF (f)->update_window_begin_hook (w);
14266 FRAME_RIF (f)->clear_window_mouse_face (w);
14267 FRAME_RIF (f)->scroll_run_hook (w, &run);
14268 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
14269 update_end (f);
14270 }
14271
14272 /* Shift current matrix down by nrows_scrolled lines. */
14273 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
14274 rotate_matrix (w->current_matrix,
14275 start_vpos,
14276 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
14277 nrows_scrolled);
14278
14279 /* Disable lines that must be updated. */
14280 for (i = 0; i < nrows_scrolled; ++i)
14281 (start_row + i)->enabled_p = 0;
14282
14283 /* Re-compute Y positions. */
14284 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
14285 max_y = it.last_visible_y;
14286 for (row = start_row + nrows_scrolled;
14287 row < bottom_row;
14288 ++row)
14289 {
14290 row->y = it.current_y;
14291 row->visible_height = row->height;
14292
14293 if (row->y < min_y)
14294 row->visible_height -= min_y - row->y;
14295 if (row->y + row->height > max_y)
14296 row->visible_height -= row->y + row->height - max_y;
14297 row->redraw_fringe_bitmaps_p = 1;
14298
14299 it.current_y += row->height;
14300
14301 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
14302 last_reused_text_row = row;
14303 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
14304 break;
14305 }
14306
14307 /* Disable lines in the current matrix which are now
14308 below the window. */
14309 for (++row; row < bottom_row; ++row)
14310 row->enabled_p = row->mode_line_p = 0;
14311 }
14312
14313 /* Update window_end_pos etc.; last_reused_text_row is the last
14314 reused row from the current matrix containing text, if any.
14315 The value of last_text_row is the last displayed line
14316 containing text. */
14317 if (last_reused_text_row)
14318 {
14319 w->window_end_bytepos
14320 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
14321 w->window_end_pos
14322 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
14323 w->window_end_vpos
14324 = make_number (MATRIX_ROW_VPOS (last_reused_text_row,
14325 w->current_matrix));
14326 }
14327 else if (last_text_row)
14328 {
14329 w->window_end_bytepos
14330 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
14331 w->window_end_pos
14332 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
14333 w->window_end_vpos
14334 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
14335 }
14336 else
14337 {
14338 /* This window must be completely empty. */
14339 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
14340 w->window_end_pos = make_number (Z - ZV);
14341 w->window_end_vpos = make_number (0);
14342 }
14343 w->window_end_valid = Qnil;
14344
14345 /* Update hint: don't try scrolling again in update_window. */
14346 w->desired_matrix->no_scrolling_p = 1;
14347
14348 #if GLYPH_DEBUG
14349 debug_method_add (w, "try_window_reusing_current_matrix 1");
14350 #endif
14351 return 1;
14352 }
14353 else if (CHARPOS (new_start) > CHARPOS (start))
14354 {
14355 struct glyph_row *pt_row, *row;
14356 struct glyph_row *first_reusable_row;
14357 struct glyph_row *first_row_to_display;
14358 int dy;
14359 int yb = window_text_bottom_y (w);
14360
14361 /* Find the row starting at new_start, if there is one. Don't
14362 reuse a partially visible line at the end. */
14363 first_reusable_row = start_row;
14364 while (first_reusable_row->enabled_p
14365 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
14366 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
14367 < CHARPOS (new_start)))
14368 ++first_reusable_row;
14369
14370 /* Give up if there is no row to reuse. */
14371 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
14372 || !first_reusable_row->enabled_p
14373 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
14374 != CHARPOS (new_start)))
14375 return 0;
14376
14377 /* We can reuse fully visible rows beginning with
14378 first_reusable_row to the end of the window. Set
14379 first_row_to_display to the first row that cannot be reused.
14380 Set pt_row to the row containing point, if there is any. */
14381 pt_row = NULL;
14382 for (first_row_to_display = first_reusable_row;
14383 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
14384 ++first_row_to_display)
14385 {
14386 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
14387 && PT < MATRIX_ROW_END_CHARPOS (first_row_to_display))
14388 pt_row = first_row_to_display;
14389 }
14390
14391 /* Start displaying at the start of first_row_to_display. */
14392 xassert (first_row_to_display->y < yb);
14393 init_to_row_start (&it, w, first_row_to_display);
14394
14395 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
14396 - start_vpos);
14397 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
14398 - nrows_scrolled);
14399 it.current_y = (first_row_to_display->y - first_reusable_row->y
14400 + WINDOW_HEADER_LINE_HEIGHT (w));
14401
14402 /* Display lines beginning with first_row_to_display in the
14403 desired matrix. Set last_text_row to the last row displayed
14404 that displays text. */
14405 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
14406 if (pt_row == NULL)
14407 w->cursor.vpos = -1;
14408 last_text_row = NULL;
14409 while (it.current_y < it.last_visible_y && !fonts_changed_p)
14410 if (display_line (&it))
14411 last_text_row = it.glyph_row - 1;
14412
14413 /* Give up If point isn't in a row displayed or reused. */
14414 if (w->cursor.vpos < 0)
14415 {
14416 clear_glyph_matrix (w->desired_matrix);
14417 return 0;
14418 }
14419
14420 /* If point is in a reused row, adjust y and vpos of the cursor
14421 position. */
14422 if (pt_row)
14423 {
14424 w->cursor.vpos -= nrows_scrolled;
14425 w->cursor.y -= first_reusable_row->y - start_row->y;
14426 }
14427
14428 /* Scroll the display. */
14429 run.current_y = first_reusable_row->y;
14430 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
14431 run.height = it.last_visible_y - run.current_y;
14432 dy = run.current_y - run.desired_y;
14433
14434 if (run.height)
14435 {
14436 update_begin (f);
14437 FRAME_RIF (f)->update_window_begin_hook (w);
14438 FRAME_RIF (f)->clear_window_mouse_face (w);
14439 FRAME_RIF (f)->scroll_run_hook (w, &run);
14440 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
14441 update_end (f);
14442 }
14443
14444 /* Adjust Y positions of reused rows. */
14445 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
14446 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
14447 max_y = it.last_visible_y;
14448 for (row = first_reusable_row; row < first_row_to_display; ++row)
14449 {
14450 row->y -= dy;
14451 row->visible_height = row->height;
14452 if (row->y < min_y)
14453 row->visible_height -= min_y - row->y;
14454 if (row->y + row->height > max_y)
14455 row->visible_height -= row->y + row->height - max_y;
14456 row->redraw_fringe_bitmaps_p = 1;
14457 }
14458
14459 /* Scroll the current matrix. */
14460 xassert (nrows_scrolled > 0);
14461 rotate_matrix (w->current_matrix,
14462 start_vpos,
14463 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
14464 -nrows_scrolled);
14465
14466 /* Disable rows not reused. */
14467 for (row -= nrows_scrolled; row < bottom_row; ++row)
14468 row->enabled_p = 0;
14469
14470 /* Point may have moved to a different line, so we cannot assume that
14471 the previous cursor position is valid; locate the correct row. */
14472 if (pt_row)
14473 {
14474 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
14475 row < bottom_row && PT >= MATRIX_ROW_END_CHARPOS (row);
14476 row++)
14477 {
14478 w->cursor.vpos++;
14479 w->cursor.y = row->y;
14480 }
14481 if (row < bottom_row)
14482 {
14483 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
14484 while (glyph->charpos < PT)
14485 {
14486 w->cursor.hpos++;
14487 w->cursor.x += glyph->pixel_width;
14488 glyph++;
14489 }
14490 }
14491 }
14492
14493 /* Adjust window end. A null value of last_text_row means that
14494 the window end is in reused rows which in turn means that
14495 only its vpos can have changed. */
14496 if (last_text_row)
14497 {
14498 w->window_end_bytepos
14499 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
14500 w->window_end_pos
14501 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
14502 w->window_end_vpos
14503 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
14504 }
14505 else
14506 {
14507 w->window_end_vpos
14508 = make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled);
14509 }
14510
14511 w->window_end_valid = Qnil;
14512 w->desired_matrix->no_scrolling_p = 1;
14513
14514 #if GLYPH_DEBUG
14515 debug_method_add (w, "try_window_reusing_current_matrix 2");
14516 #endif
14517 return 1;
14518 }
14519
14520 return 0;
14521 }
14522
14523
14524 \f
14525 /************************************************************************
14526 Window redisplay reusing current matrix when buffer has changed
14527 ************************************************************************/
14528
14529 static struct glyph_row *find_last_unchanged_at_beg_row P_ ((struct window *));
14530 static struct glyph_row *find_first_unchanged_at_end_row P_ ((struct window *,
14531 int *, int *));
14532 static struct glyph_row *
14533 find_last_row_displaying_text P_ ((struct glyph_matrix *, struct it *,
14534 struct glyph_row *));
14535
14536
14537 /* Return the last row in MATRIX displaying text. If row START is
14538 non-null, start searching with that row. IT gives the dimensions
14539 of the display. Value is null if matrix is empty; otherwise it is
14540 a pointer to the row found. */
14541
14542 static struct glyph_row *
14543 find_last_row_displaying_text (matrix, it, start)
14544 struct glyph_matrix *matrix;
14545 struct it *it;
14546 struct glyph_row *start;
14547 {
14548 struct glyph_row *row, *row_found;
14549
14550 /* Set row_found to the last row in IT->w's current matrix
14551 displaying text. The loop looks funny but think of partially
14552 visible lines. */
14553 row_found = NULL;
14554 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
14555 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
14556 {
14557 xassert (row->enabled_p);
14558 row_found = row;
14559 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
14560 break;
14561 ++row;
14562 }
14563
14564 return row_found;
14565 }
14566
14567
14568 /* Return the last row in the current matrix of W that is not affected
14569 by changes at the start of current_buffer that occurred since W's
14570 current matrix was built. Value is null if no such row exists.
14571
14572 BEG_UNCHANGED us the number of characters unchanged at the start of
14573 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
14574 first changed character in current_buffer. Characters at positions <
14575 BEG + BEG_UNCHANGED are at the same buffer positions as they were
14576 when the current matrix was built. */
14577
14578 static struct glyph_row *
14579 find_last_unchanged_at_beg_row (w)
14580 struct window *w;
14581 {
14582 int first_changed_pos = BEG + BEG_UNCHANGED;
14583 struct glyph_row *row;
14584 struct glyph_row *row_found = NULL;
14585 int yb = window_text_bottom_y (w);
14586
14587 /* Find the last row displaying unchanged text. */
14588 for (row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
14589 MATRIX_ROW_DISPLAYS_TEXT_P (row)
14590 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos;
14591 ++row)
14592 {
14593 if (/* If row ends before first_changed_pos, it is unchanged,
14594 except in some case. */
14595 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
14596 /* When row ends in ZV and we write at ZV it is not
14597 unchanged. */
14598 && !row->ends_at_zv_p
14599 /* When first_changed_pos is the end of a continued line,
14600 row is not unchanged because it may be no longer
14601 continued. */
14602 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
14603 && (row->continued_p
14604 || row->exact_window_width_line_p)))
14605 row_found = row;
14606
14607 /* Stop if last visible row. */
14608 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
14609 break;
14610 }
14611
14612 return row_found;
14613 }
14614
14615
14616 /* Find the first glyph row in the current matrix of W that is not
14617 affected by changes at the end of current_buffer since the
14618 time W's current matrix was built.
14619
14620 Return in *DELTA the number of chars by which buffer positions in
14621 unchanged text at the end of current_buffer must be adjusted.
14622
14623 Return in *DELTA_BYTES the corresponding number of bytes.
14624
14625 Value is null if no such row exists, i.e. all rows are affected by
14626 changes. */
14627
14628 static struct glyph_row *
14629 find_first_unchanged_at_end_row (w, delta, delta_bytes)
14630 struct window *w;
14631 int *delta, *delta_bytes;
14632 {
14633 struct glyph_row *row;
14634 struct glyph_row *row_found = NULL;
14635
14636 *delta = *delta_bytes = 0;
14637
14638 /* Display must not have been paused, otherwise the current matrix
14639 is not up to date. */
14640 eassert (!NILP (w->window_end_valid));
14641
14642 /* A value of window_end_pos >= END_UNCHANGED means that the window
14643 end is in the range of changed text. If so, there is no
14644 unchanged row at the end of W's current matrix. */
14645 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
14646 return NULL;
14647
14648 /* Set row to the last row in W's current matrix displaying text. */
14649 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
14650
14651 /* If matrix is entirely empty, no unchanged row exists. */
14652 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
14653 {
14654 /* The value of row is the last glyph row in the matrix having a
14655 meaningful buffer position in it. The end position of row
14656 corresponds to window_end_pos. This allows us to translate
14657 buffer positions in the current matrix to current buffer
14658 positions for characters not in changed text. */
14659 int Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
14660 int Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
14661 int last_unchanged_pos, last_unchanged_pos_old;
14662 struct glyph_row *first_text_row
14663 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
14664
14665 *delta = Z - Z_old;
14666 *delta_bytes = Z_BYTE - Z_BYTE_old;
14667
14668 /* Set last_unchanged_pos to the buffer position of the last
14669 character in the buffer that has not been changed. Z is the
14670 index + 1 of the last character in current_buffer, i.e. by
14671 subtracting END_UNCHANGED we get the index of the last
14672 unchanged character, and we have to add BEG to get its buffer
14673 position. */
14674 last_unchanged_pos = Z - END_UNCHANGED + BEG;
14675 last_unchanged_pos_old = last_unchanged_pos - *delta;
14676
14677 /* Search backward from ROW for a row displaying a line that
14678 starts at a minimum position >= last_unchanged_pos_old. */
14679 for (; row > first_text_row; --row)
14680 {
14681 /* This used to abort, but it can happen.
14682 It is ok to just stop the search instead here. KFS. */
14683 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
14684 break;
14685
14686 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
14687 row_found = row;
14688 }
14689 }
14690
14691 eassert (!row_found || MATRIX_ROW_DISPLAYS_TEXT_P (row_found));
14692
14693 return row_found;
14694 }
14695
14696
14697 /* Make sure that glyph rows in the current matrix of window W
14698 reference the same glyph memory as corresponding rows in the
14699 frame's frame matrix. This function is called after scrolling W's
14700 current matrix on a terminal frame in try_window_id and
14701 try_window_reusing_current_matrix. */
14702
14703 static void
14704 sync_frame_with_window_matrix_rows (w)
14705 struct window *w;
14706 {
14707 struct frame *f = XFRAME (w->frame);
14708 struct glyph_row *window_row, *window_row_end, *frame_row;
14709
14710 /* Preconditions: W must be a leaf window and full-width. Its frame
14711 must have a frame matrix. */
14712 xassert (NILP (w->hchild) && NILP (w->vchild));
14713 xassert (WINDOW_FULL_WIDTH_P (w));
14714 xassert (!FRAME_WINDOW_P (f));
14715
14716 /* If W is a full-width window, glyph pointers in W's current matrix
14717 have, by definition, to be the same as glyph pointers in the
14718 corresponding frame matrix. Note that frame matrices have no
14719 marginal areas (see build_frame_matrix). */
14720 window_row = w->current_matrix->rows;
14721 window_row_end = window_row + w->current_matrix->nrows;
14722 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
14723 while (window_row < window_row_end)
14724 {
14725 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
14726 struct glyph *end = window_row->glyphs[LAST_AREA];
14727
14728 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
14729 frame_row->glyphs[TEXT_AREA] = start;
14730 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
14731 frame_row->glyphs[LAST_AREA] = end;
14732
14733 /* Disable frame rows whose corresponding window rows have
14734 been disabled in try_window_id. */
14735 if (!window_row->enabled_p)
14736 frame_row->enabled_p = 0;
14737
14738 ++window_row, ++frame_row;
14739 }
14740 }
14741
14742
14743 /* Find the glyph row in window W containing CHARPOS. Consider all
14744 rows between START and END (not inclusive). END null means search
14745 all rows to the end of the display area of W. Value is the row
14746 containing CHARPOS or null. */
14747
14748 struct glyph_row *
14749 row_containing_pos (w, charpos, start, end, dy)
14750 struct window *w;
14751 int charpos;
14752 struct glyph_row *start, *end;
14753 int dy;
14754 {
14755 struct glyph_row *row = start;
14756 int last_y;
14757
14758 /* If we happen to start on a header-line, skip that. */
14759 if (row->mode_line_p)
14760 ++row;
14761
14762 if ((end && row >= end) || !row->enabled_p)
14763 return NULL;
14764
14765 last_y = window_text_bottom_y (w) - dy;
14766
14767 while (1)
14768 {
14769 /* Give up if we have gone too far. */
14770 if (end && row >= end)
14771 return NULL;
14772 /* This formerly returned if they were equal.
14773 I think that both quantities are of a "last plus one" type;
14774 if so, when they are equal, the row is within the screen. -- rms. */
14775 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
14776 return NULL;
14777
14778 /* If it is in this row, return this row. */
14779 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
14780 || (MATRIX_ROW_END_CHARPOS (row) == charpos
14781 /* The end position of a row equals the start
14782 position of the next row. If CHARPOS is there, we
14783 would rather display it in the next line, except
14784 when this line ends in ZV. */
14785 && !row->ends_at_zv_p
14786 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
14787 && charpos >= MATRIX_ROW_START_CHARPOS (row))
14788 return row;
14789 ++row;
14790 }
14791 }
14792
14793
14794 /* Try to redisplay window W by reusing its existing display. W's
14795 current matrix must be up to date when this function is called,
14796 i.e. window_end_valid must not be nil.
14797
14798 Value is
14799
14800 1 if display has been updated
14801 0 if otherwise unsuccessful
14802 -1 if redisplay with same window start is known not to succeed
14803
14804 The following steps are performed:
14805
14806 1. Find the last row in the current matrix of W that is not
14807 affected by changes at the start of current_buffer. If no such row
14808 is found, give up.
14809
14810 2. Find the first row in W's current matrix that is not affected by
14811 changes at the end of current_buffer. Maybe there is no such row.
14812
14813 3. Display lines beginning with the row + 1 found in step 1 to the
14814 row found in step 2 or, if step 2 didn't find a row, to the end of
14815 the window.
14816
14817 4. If cursor is not known to appear on the window, give up.
14818
14819 5. If display stopped at the row found in step 2, scroll the
14820 display and current matrix as needed.
14821
14822 6. Maybe display some lines at the end of W, if we must. This can
14823 happen under various circumstances, like a partially visible line
14824 becoming fully visible, or because newly displayed lines are displayed
14825 in smaller font sizes.
14826
14827 7. Update W's window end information. */
14828
14829 static int
14830 try_window_id (w)
14831 struct window *w;
14832 {
14833 struct frame *f = XFRAME (w->frame);
14834 struct glyph_matrix *current_matrix = w->current_matrix;
14835 struct glyph_matrix *desired_matrix = w->desired_matrix;
14836 struct glyph_row *last_unchanged_at_beg_row;
14837 struct glyph_row *first_unchanged_at_end_row;
14838 struct glyph_row *row;
14839 struct glyph_row *bottom_row;
14840 int bottom_vpos;
14841 struct it it;
14842 int delta = 0, delta_bytes = 0, stop_pos, dvpos, dy;
14843 struct text_pos start_pos;
14844 struct run run;
14845 int first_unchanged_at_end_vpos = 0;
14846 struct glyph_row *last_text_row, *last_text_row_at_end;
14847 struct text_pos start;
14848 int first_changed_charpos, last_changed_charpos;
14849
14850 #if GLYPH_DEBUG
14851 if (inhibit_try_window_id)
14852 return 0;
14853 #endif
14854
14855 /* This is handy for debugging. */
14856 #if 0
14857 #define GIVE_UP(X) \
14858 do { \
14859 fprintf (stderr, "try_window_id give up %d\n", (X)); \
14860 return 0; \
14861 } while (0)
14862 #else
14863 #define GIVE_UP(X) return 0
14864 #endif
14865
14866 SET_TEXT_POS_FROM_MARKER (start, w->start);
14867
14868 /* Don't use this for mini-windows because these can show
14869 messages and mini-buffers, and we don't handle that here. */
14870 if (MINI_WINDOW_P (w))
14871 GIVE_UP (1);
14872
14873 /* This flag is used to prevent redisplay optimizations. */
14874 if (windows_or_buffers_changed || cursor_type_changed)
14875 GIVE_UP (2);
14876
14877 /* Verify that narrowing has not changed.
14878 Also verify that we were not told to prevent redisplay optimizations.
14879 It would be nice to further
14880 reduce the number of cases where this prevents try_window_id. */
14881 if (current_buffer->clip_changed
14882 || current_buffer->prevent_redisplay_optimizations_p)
14883 GIVE_UP (3);
14884
14885 /* Window must either use window-based redisplay or be full width. */
14886 if (!FRAME_WINDOW_P (f)
14887 && (!FRAME_LINE_INS_DEL_OK (f)
14888 || !WINDOW_FULL_WIDTH_P (w)))
14889 GIVE_UP (4);
14890
14891 /* Give up if point is not known NOT to appear in W. */
14892 if (PT < CHARPOS (start))
14893 GIVE_UP (5);
14894
14895 /* Another way to prevent redisplay optimizations. */
14896 if (XFASTINT (w->last_modified) == 0)
14897 GIVE_UP (6);
14898
14899 /* Verify that window is not hscrolled. */
14900 if (XFASTINT (w->hscroll) != 0)
14901 GIVE_UP (7);
14902
14903 /* Verify that display wasn't paused. */
14904 if (NILP (w->window_end_valid))
14905 GIVE_UP (8);
14906
14907 /* Can't use this if highlighting a region because a cursor movement
14908 will do more than just set the cursor. */
14909 if (!NILP (Vtransient_mark_mode)
14910 && !NILP (current_buffer->mark_active))
14911 GIVE_UP (9);
14912
14913 /* Likewise if highlighting trailing whitespace. */
14914 if (!NILP (Vshow_trailing_whitespace))
14915 GIVE_UP (11);
14916
14917 /* Likewise if showing a region. */
14918 if (!NILP (w->region_showing))
14919 GIVE_UP (10);
14920
14921 /* Can use this if overlay arrow position and or string have changed. */
14922 if (overlay_arrows_changed_p ())
14923 GIVE_UP (12);
14924
14925 /* When word-wrap is on, adding a space to the first word of a
14926 wrapped line can change the wrap position, altering the line
14927 above it. It might be worthwhile to handle this more
14928 intelligently, but for now just redisplay from scratch. */
14929 if (!NILP (XBUFFER (w->buffer)->word_wrap))
14930 GIVE_UP (21);
14931
14932 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
14933 only if buffer has really changed. The reason is that the gap is
14934 initially at Z for freshly visited files. The code below would
14935 set end_unchanged to 0 in that case. */
14936 if (MODIFF > SAVE_MODIFF
14937 /* This seems to happen sometimes after saving a buffer. */
14938 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
14939 {
14940 if (GPT - BEG < BEG_UNCHANGED)
14941 BEG_UNCHANGED = GPT - BEG;
14942 if (Z - GPT < END_UNCHANGED)
14943 END_UNCHANGED = Z - GPT;
14944 }
14945
14946 /* The position of the first and last character that has been changed. */
14947 first_changed_charpos = BEG + BEG_UNCHANGED;
14948 last_changed_charpos = Z - END_UNCHANGED;
14949
14950 /* If window starts after a line end, and the last change is in
14951 front of that newline, then changes don't affect the display.
14952 This case happens with stealth-fontification. Note that although
14953 the display is unchanged, glyph positions in the matrix have to
14954 be adjusted, of course. */
14955 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
14956 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
14957 && ((last_changed_charpos < CHARPOS (start)
14958 && CHARPOS (start) == BEGV)
14959 || (last_changed_charpos < CHARPOS (start) - 1
14960 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
14961 {
14962 int Z_old, delta, Z_BYTE_old, delta_bytes;
14963 struct glyph_row *r0;
14964
14965 /* Compute how many chars/bytes have been added to or removed
14966 from the buffer. */
14967 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
14968 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
14969 delta = Z - Z_old;
14970 delta_bytes = Z_BYTE - Z_BYTE_old;
14971
14972 /* Give up if PT is not in the window. Note that it already has
14973 been checked at the start of try_window_id that PT is not in
14974 front of the window start. */
14975 if (PT >= MATRIX_ROW_END_CHARPOS (row) + delta)
14976 GIVE_UP (13);
14977
14978 /* If window start is unchanged, we can reuse the whole matrix
14979 as is, after adjusting glyph positions. No need to compute
14980 the window end again, since its offset from Z hasn't changed. */
14981 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
14982 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + delta
14983 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + delta_bytes
14984 /* PT must not be in a partially visible line. */
14985 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + delta
14986 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
14987 {
14988 /* Adjust positions in the glyph matrix. */
14989 if (delta || delta_bytes)
14990 {
14991 struct glyph_row *r1
14992 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
14993 increment_matrix_positions (w->current_matrix,
14994 MATRIX_ROW_VPOS (r0, current_matrix),
14995 MATRIX_ROW_VPOS (r1, current_matrix),
14996 delta, delta_bytes);
14997 }
14998
14999 /* Set the cursor. */
15000 row = row_containing_pos (w, PT, r0, NULL, 0);
15001 if (row)
15002 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
15003 else
15004 abort ();
15005 return 1;
15006 }
15007 }
15008
15009 /* Handle the case that changes are all below what is displayed in
15010 the window, and that PT is in the window. This shortcut cannot
15011 be taken if ZV is visible in the window, and text has been added
15012 there that is visible in the window. */
15013 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
15014 /* ZV is not visible in the window, or there are no
15015 changes at ZV, actually. */
15016 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
15017 || first_changed_charpos == last_changed_charpos))
15018 {
15019 struct glyph_row *r0;
15020
15021 /* Give up if PT is not in the window. Note that it already has
15022 been checked at the start of try_window_id that PT is not in
15023 front of the window start. */
15024 if (PT >= MATRIX_ROW_END_CHARPOS (row))
15025 GIVE_UP (14);
15026
15027 /* If window start is unchanged, we can reuse the whole matrix
15028 as is, without changing glyph positions since no text has
15029 been added/removed in front of the window end. */
15030 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
15031 if (TEXT_POS_EQUAL_P (start, r0->start.pos)
15032 /* PT must not be in a partially visible line. */
15033 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
15034 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
15035 {
15036 /* We have to compute the window end anew since text
15037 can have been added/removed after it. */
15038 w->window_end_pos
15039 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
15040 w->window_end_bytepos
15041 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
15042
15043 /* Set the cursor. */
15044 row = row_containing_pos (w, PT, r0, NULL, 0);
15045 if (row)
15046 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
15047 else
15048 abort ();
15049 return 2;
15050 }
15051 }
15052
15053 /* Give up if window start is in the changed area.
15054
15055 The condition used to read
15056
15057 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
15058
15059 but why that was tested escapes me at the moment. */
15060 if (CHARPOS (start) >= first_changed_charpos
15061 && CHARPOS (start) <= last_changed_charpos)
15062 GIVE_UP (15);
15063
15064 /* Check that window start agrees with the start of the first glyph
15065 row in its current matrix. Check this after we know the window
15066 start is not in changed text, otherwise positions would not be
15067 comparable. */
15068 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
15069 if (!TEXT_POS_EQUAL_P (start, row->start.pos))
15070 GIVE_UP (16);
15071
15072 /* Give up if the window ends in strings. Overlay strings
15073 at the end are difficult to handle, so don't try. */
15074 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
15075 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
15076 GIVE_UP (20);
15077
15078 /* Compute the position at which we have to start displaying new
15079 lines. Some of the lines at the top of the window might be
15080 reusable because they are not displaying changed text. Find the
15081 last row in W's current matrix not affected by changes at the
15082 start of current_buffer. Value is null if changes start in the
15083 first line of window. */
15084 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
15085 if (last_unchanged_at_beg_row)
15086 {
15087 /* Avoid starting to display in the moddle of a character, a TAB
15088 for instance. This is easier than to set up the iterator
15089 exactly, and it's not a frequent case, so the additional
15090 effort wouldn't really pay off. */
15091 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
15092 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
15093 && last_unchanged_at_beg_row > w->current_matrix->rows)
15094 --last_unchanged_at_beg_row;
15095
15096 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
15097 GIVE_UP (17);
15098
15099 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
15100 GIVE_UP (18);
15101 start_pos = it.current.pos;
15102
15103 /* Start displaying new lines in the desired matrix at the same
15104 vpos we would use in the current matrix, i.e. below
15105 last_unchanged_at_beg_row. */
15106 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
15107 current_matrix);
15108 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
15109 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
15110
15111 xassert (it.hpos == 0 && it.current_x == 0);
15112 }
15113 else
15114 {
15115 /* There are no reusable lines at the start of the window.
15116 Start displaying in the first text line. */
15117 start_display (&it, w, start);
15118 it.vpos = it.first_vpos;
15119 start_pos = it.current.pos;
15120 }
15121
15122 /* Find the first row that is not affected by changes at the end of
15123 the buffer. Value will be null if there is no unchanged row, in
15124 which case we must redisplay to the end of the window. delta
15125 will be set to the value by which buffer positions beginning with
15126 first_unchanged_at_end_row have to be adjusted due to text
15127 changes. */
15128 first_unchanged_at_end_row
15129 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
15130 IF_DEBUG (debug_delta = delta);
15131 IF_DEBUG (debug_delta_bytes = delta_bytes);
15132
15133 /* Set stop_pos to the buffer position up to which we will have to
15134 display new lines. If first_unchanged_at_end_row != NULL, this
15135 is the buffer position of the start of the line displayed in that
15136 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
15137 that we don't stop at a buffer position. */
15138 stop_pos = 0;
15139 if (first_unchanged_at_end_row)
15140 {
15141 xassert (last_unchanged_at_beg_row == NULL
15142 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
15143
15144 /* If this is a continuation line, move forward to the next one
15145 that isn't. Changes in lines above affect this line.
15146 Caution: this may move first_unchanged_at_end_row to a row
15147 not displaying text. */
15148 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
15149 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
15150 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
15151 < it.last_visible_y))
15152 ++first_unchanged_at_end_row;
15153
15154 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
15155 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
15156 >= it.last_visible_y))
15157 first_unchanged_at_end_row = NULL;
15158 else
15159 {
15160 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
15161 + delta);
15162 first_unchanged_at_end_vpos
15163 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
15164 xassert (stop_pos >= Z - END_UNCHANGED);
15165 }
15166 }
15167 else if (last_unchanged_at_beg_row == NULL)
15168 GIVE_UP (19);
15169
15170
15171 #if GLYPH_DEBUG
15172
15173 /* Either there is no unchanged row at the end, or the one we have
15174 now displays text. This is a necessary condition for the window
15175 end pos calculation at the end of this function. */
15176 xassert (first_unchanged_at_end_row == NULL
15177 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
15178
15179 debug_last_unchanged_at_beg_vpos
15180 = (last_unchanged_at_beg_row
15181 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
15182 : -1);
15183 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
15184
15185 #endif /* GLYPH_DEBUG != 0 */
15186
15187
15188 /* Display new lines. Set last_text_row to the last new line
15189 displayed which has text on it, i.e. might end up as being the
15190 line where the window_end_vpos is. */
15191 w->cursor.vpos = -1;
15192 last_text_row = NULL;
15193 overlay_arrow_seen = 0;
15194 while (it.current_y < it.last_visible_y
15195 && !fonts_changed_p
15196 && (first_unchanged_at_end_row == NULL
15197 || IT_CHARPOS (it) < stop_pos))
15198 {
15199 if (display_line (&it))
15200 last_text_row = it.glyph_row - 1;
15201 }
15202
15203 if (fonts_changed_p)
15204 return -1;
15205
15206
15207 /* Compute differences in buffer positions, y-positions etc. for
15208 lines reused at the bottom of the window. Compute what we can
15209 scroll. */
15210 if (first_unchanged_at_end_row
15211 /* No lines reused because we displayed everything up to the
15212 bottom of the window. */
15213 && it.current_y < it.last_visible_y)
15214 {
15215 dvpos = (it.vpos
15216 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
15217 current_matrix));
15218 dy = it.current_y - first_unchanged_at_end_row->y;
15219 run.current_y = first_unchanged_at_end_row->y;
15220 run.desired_y = run.current_y + dy;
15221 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
15222 }
15223 else
15224 {
15225 delta = delta_bytes = dvpos = dy
15226 = run.current_y = run.desired_y = run.height = 0;
15227 first_unchanged_at_end_row = NULL;
15228 }
15229 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
15230
15231
15232 /* Find the cursor if not already found. We have to decide whether
15233 PT will appear on this window (it sometimes doesn't, but this is
15234 not a very frequent case.) This decision has to be made before
15235 the current matrix is altered. A value of cursor.vpos < 0 means
15236 that PT is either in one of the lines beginning at
15237 first_unchanged_at_end_row or below the window. Don't care for
15238 lines that might be displayed later at the window end; as
15239 mentioned, this is not a frequent case. */
15240 if (w->cursor.vpos < 0)
15241 {
15242 /* Cursor in unchanged rows at the top? */
15243 if (PT < CHARPOS (start_pos)
15244 && last_unchanged_at_beg_row)
15245 {
15246 row = row_containing_pos (w, PT,
15247 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
15248 last_unchanged_at_beg_row + 1, 0);
15249 if (row)
15250 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
15251 }
15252
15253 /* Start from first_unchanged_at_end_row looking for PT. */
15254 else if (first_unchanged_at_end_row)
15255 {
15256 row = row_containing_pos (w, PT - delta,
15257 first_unchanged_at_end_row, NULL, 0);
15258 if (row)
15259 set_cursor_from_row (w, row, w->current_matrix, delta,
15260 delta_bytes, dy, dvpos);
15261 }
15262
15263 /* Give up if cursor was not found. */
15264 if (w->cursor.vpos < 0)
15265 {
15266 clear_glyph_matrix (w->desired_matrix);
15267 return -1;
15268 }
15269 }
15270
15271 /* Don't let the cursor end in the scroll margins. */
15272 {
15273 int this_scroll_margin, cursor_height;
15274
15275 this_scroll_margin = max (0, scroll_margin);
15276 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
15277 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
15278 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
15279
15280 if ((w->cursor.y < this_scroll_margin
15281 && CHARPOS (start) > BEGV)
15282 /* Old redisplay didn't take scroll margin into account at the bottom,
15283 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
15284 || (w->cursor.y + (make_cursor_line_fully_visible_p
15285 ? cursor_height + this_scroll_margin
15286 : 1)) > it.last_visible_y)
15287 {
15288 w->cursor.vpos = -1;
15289 clear_glyph_matrix (w->desired_matrix);
15290 return -1;
15291 }
15292 }
15293
15294 /* Scroll the display. Do it before changing the current matrix so
15295 that xterm.c doesn't get confused about where the cursor glyph is
15296 found. */
15297 if (dy && run.height)
15298 {
15299 update_begin (f);
15300
15301 if (FRAME_WINDOW_P (f))
15302 {
15303 FRAME_RIF (f)->update_window_begin_hook (w);
15304 FRAME_RIF (f)->clear_window_mouse_face (w);
15305 FRAME_RIF (f)->scroll_run_hook (w, &run);
15306 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
15307 }
15308 else
15309 {
15310 /* Terminal frame. In this case, dvpos gives the number of
15311 lines to scroll by; dvpos < 0 means scroll up. */
15312 int first_unchanged_at_end_vpos
15313 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
15314 int from = WINDOW_TOP_EDGE_LINE (w) + first_unchanged_at_end_vpos;
15315 int end = (WINDOW_TOP_EDGE_LINE (w)
15316 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
15317 + window_internal_height (w));
15318
15319 /* Perform the operation on the screen. */
15320 if (dvpos > 0)
15321 {
15322 /* Scroll last_unchanged_at_beg_row to the end of the
15323 window down dvpos lines. */
15324 set_terminal_window (f, end);
15325
15326 /* On dumb terminals delete dvpos lines at the end
15327 before inserting dvpos empty lines. */
15328 if (!FRAME_SCROLL_REGION_OK (f))
15329 ins_del_lines (f, end - dvpos, -dvpos);
15330
15331 /* Insert dvpos empty lines in front of
15332 last_unchanged_at_beg_row. */
15333 ins_del_lines (f, from, dvpos);
15334 }
15335 else if (dvpos < 0)
15336 {
15337 /* Scroll up last_unchanged_at_beg_vpos to the end of
15338 the window to last_unchanged_at_beg_vpos - |dvpos|. */
15339 set_terminal_window (f, end);
15340
15341 /* Delete dvpos lines in front of
15342 last_unchanged_at_beg_vpos. ins_del_lines will set
15343 the cursor to the given vpos and emit |dvpos| delete
15344 line sequences. */
15345 ins_del_lines (f, from + dvpos, dvpos);
15346
15347 /* On a dumb terminal insert dvpos empty lines at the
15348 end. */
15349 if (!FRAME_SCROLL_REGION_OK (f))
15350 ins_del_lines (f, end + dvpos, -dvpos);
15351 }
15352
15353 set_terminal_window (f, 0);
15354 }
15355
15356 update_end (f);
15357 }
15358
15359 /* Shift reused rows of the current matrix to the right position.
15360 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
15361 text. */
15362 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
15363 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
15364 if (dvpos < 0)
15365 {
15366 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
15367 bottom_vpos, dvpos);
15368 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
15369 bottom_vpos, 0);
15370 }
15371 else if (dvpos > 0)
15372 {
15373 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
15374 bottom_vpos, dvpos);
15375 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
15376 first_unchanged_at_end_vpos + dvpos, 0);
15377 }
15378
15379 /* For frame-based redisplay, make sure that current frame and window
15380 matrix are in sync with respect to glyph memory. */
15381 if (!FRAME_WINDOW_P (f))
15382 sync_frame_with_window_matrix_rows (w);
15383
15384 /* Adjust buffer positions in reused rows. */
15385 if (delta || delta_bytes)
15386 increment_matrix_positions (current_matrix,
15387 first_unchanged_at_end_vpos + dvpos,
15388 bottom_vpos, delta, delta_bytes);
15389
15390 /* Adjust Y positions. */
15391 if (dy)
15392 shift_glyph_matrix (w, current_matrix,
15393 first_unchanged_at_end_vpos + dvpos,
15394 bottom_vpos, dy);
15395
15396 if (first_unchanged_at_end_row)
15397 {
15398 first_unchanged_at_end_row += dvpos;
15399 if (first_unchanged_at_end_row->y >= it.last_visible_y
15400 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
15401 first_unchanged_at_end_row = NULL;
15402 }
15403
15404 /* If scrolling up, there may be some lines to display at the end of
15405 the window. */
15406 last_text_row_at_end = NULL;
15407 if (dy < 0)
15408 {
15409 /* Scrolling up can leave for example a partially visible line
15410 at the end of the window to be redisplayed. */
15411 /* Set last_row to the glyph row in the current matrix where the
15412 window end line is found. It has been moved up or down in
15413 the matrix by dvpos. */
15414 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
15415 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
15416
15417 /* If last_row is the window end line, it should display text. */
15418 xassert (last_row->displays_text_p);
15419
15420 /* If window end line was partially visible before, begin
15421 displaying at that line. Otherwise begin displaying with the
15422 line following it. */
15423 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
15424 {
15425 init_to_row_start (&it, w, last_row);
15426 it.vpos = last_vpos;
15427 it.current_y = last_row->y;
15428 }
15429 else
15430 {
15431 init_to_row_end (&it, w, last_row);
15432 it.vpos = 1 + last_vpos;
15433 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
15434 ++last_row;
15435 }
15436
15437 /* We may start in a continuation line. If so, we have to
15438 get the right continuation_lines_width and current_x. */
15439 it.continuation_lines_width = last_row->continuation_lines_width;
15440 it.hpos = it.current_x = 0;
15441
15442 /* Display the rest of the lines at the window end. */
15443 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
15444 while (it.current_y < it.last_visible_y
15445 && !fonts_changed_p)
15446 {
15447 /* Is it always sure that the display agrees with lines in
15448 the current matrix? I don't think so, so we mark rows
15449 displayed invalid in the current matrix by setting their
15450 enabled_p flag to zero. */
15451 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
15452 if (display_line (&it))
15453 last_text_row_at_end = it.glyph_row - 1;
15454 }
15455 }
15456
15457 /* Update window_end_pos and window_end_vpos. */
15458 if (first_unchanged_at_end_row
15459 && !last_text_row_at_end)
15460 {
15461 /* Window end line if one of the preserved rows from the current
15462 matrix. Set row to the last row displaying text in current
15463 matrix starting at first_unchanged_at_end_row, after
15464 scrolling. */
15465 xassert (first_unchanged_at_end_row->displays_text_p);
15466 row = find_last_row_displaying_text (w->current_matrix, &it,
15467 first_unchanged_at_end_row);
15468 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
15469
15470 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
15471 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
15472 w->window_end_vpos
15473 = make_number (MATRIX_ROW_VPOS (row, w->current_matrix));
15474 xassert (w->window_end_bytepos >= 0);
15475 IF_DEBUG (debug_method_add (w, "A"));
15476 }
15477 else if (last_text_row_at_end)
15478 {
15479 w->window_end_pos
15480 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
15481 w->window_end_bytepos
15482 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
15483 w->window_end_vpos
15484 = make_number (MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
15485 xassert (w->window_end_bytepos >= 0);
15486 IF_DEBUG (debug_method_add (w, "B"));
15487 }
15488 else if (last_text_row)
15489 {
15490 /* We have displayed either to the end of the window or at the
15491 end of the window, i.e. the last row with text is to be found
15492 in the desired matrix. */
15493 w->window_end_pos
15494 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
15495 w->window_end_bytepos
15496 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
15497 w->window_end_vpos
15498 = make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix));
15499 xassert (w->window_end_bytepos >= 0);
15500 }
15501 else if (first_unchanged_at_end_row == NULL
15502 && last_text_row == NULL
15503 && last_text_row_at_end == NULL)
15504 {
15505 /* Displayed to end of window, but no line containing text was
15506 displayed. Lines were deleted at the end of the window. */
15507 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
15508 int vpos = XFASTINT (w->window_end_vpos);
15509 struct glyph_row *current_row = current_matrix->rows + vpos;
15510 struct glyph_row *desired_row = desired_matrix->rows + vpos;
15511
15512 for (row = NULL;
15513 row == NULL && vpos >= first_vpos;
15514 --vpos, --current_row, --desired_row)
15515 {
15516 if (desired_row->enabled_p)
15517 {
15518 if (desired_row->displays_text_p)
15519 row = desired_row;
15520 }
15521 else if (current_row->displays_text_p)
15522 row = current_row;
15523 }
15524
15525 xassert (row != NULL);
15526 w->window_end_vpos = make_number (vpos + 1);
15527 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
15528 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
15529 xassert (w->window_end_bytepos >= 0);
15530 IF_DEBUG (debug_method_add (w, "C"));
15531 }
15532 else
15533 abort ();
15534
15535 #if 0 /* This leads to problems, for instance when the cursor is
15536 at ZV, and the cursor line displays no text. */
15537 /* Disable rows below what's displayed in the window. This makes
15538 debugging easier. */
15539 enable_glyph_matrix_rows (current_matrix,
15540 XFASTINT (w->window_end_vpos) + 1,
15541 bottom_vpos, 0);
15542 #endif
15543
15544 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
15545 debug_end_vpos = XFASTINT (w->window_end_vpos));
15546
15547 /* Record that display has not been completed. */
15548 w->window_end_valid = Qnil;
15549 w->desired_matrix->no_scrolling_p = 1;
15550 return 3;
15551
15552 #undef GIVE_UP
15553 }
15554
15555
15556 \f
15557 /***********************************************************************
15558 More debugging support
15559 ***********************************************************************/
15560
15561 #if GLYPH_DEBUG
15562
15563 void dump_glyph_row P_ ((struct glyph_row *, int, int));
15564 void dump_glyph_matrix P_ ((struct glyph_matrix *, int));
15565 void dump_glyph P_ ((struct glyph_row *, struct glyph *, int));
15566
15567
15568 /* Dump the contents of glyph matrix MATRIX on stderr.
15569
15570 GLYPHS 0 means don't show glyph contents.
15571 GLYPHS 1 means show glyphs in short form
15572 GLYPHS > 1 means show glyphs in long form. */
15573
15574 void
15575 dump_glyph_matrix (matrix, glyphs)
15576 struct glyph_matrix *matrix;
15577 int glyphs;
15578 {
15579 int i;
15580 for (i = 0; i < matrix->nrows; ++i)
15581 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
15582 }
15583
15584
15585 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
15586 the glyph row and area where the glyph comes from. */
15587
15588 void
15589 dump_glyph (row, glyph, area)
15590 struct glyph_row *row;
15591 struct glyph *glyph;
15592 int area;
15593 {
15594 if (glyph->type == CHAR_GLYPH)
15595 {
15596 fprintf (stderr,
15597 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
15598 glyph - row->glyphs[TEXT_AREA],
15599 'C',
15600 glyph->charpos,
15601 (BUFFERP (glyph->object)
15602 ? 'B'
15603 : (STRINGP (glyph->object)
15604 ? 'S'
15605 : '-')),
15606 glyph->pixel_width,
15607 glyph->u.ch,
15608 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
15609 ? glyph->u.ch
15610 : '.'),
15611 glyph->face_id,
15612 glyph->left_box_line_p,
15613 glyph->right_box_line_p);
15614 }
15615 else if (glyph->type == STRETCH_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 'S',
15621 glyph->charpos,
15622 (BUFFERP (glyph->object)
15623 ? 'B'
15624 : (STRINGP (glyph->object)
15625 ? 'S'
15626 : '-')),
15627 glyph->pixel_width,
15628 0,
15629 '.',
15630 glyph->face_id,
15631 glyph->left_box_line_p,
15632 glyph->right_box_line_p);
15633 }
15634 else if (glyph->type == IMAGE_GLYPH)
15635 {
15636 fprintf (stderr,
15637 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
15638 glyph - row->glyphs[TEXT_AREA],
15639 'I',
15640 glyph->charpos,
15641 (BUFFERP (glyph->object)
15642 ? 'B'
15643 : (STRINGP (glyph->object)
15644 ? 'S'
15645 : '-')),
15646 glyph->pixel_width,
15647 glyph->u.img_id,
15648 '.',
15649 glyph->face_id,
15650 glyph->left_box_line_p,
15651 glyph->right_box_line_p);
15652 }
15653 else if (glyph->type == COMPOSITE_GLYPH)
15654 {
15655 fprintf (stderr,
15656 " %5d %4c %6d %c %3d 0x%05x",
15657 glyph - row->glyphs[TEXT_AREA],
15658 '+',
15659 glyph->charpos,
15660 (BUFFERP (glyph->object)
15661 ? 'B'
15662 : (STRINGP (glyph->object)
15663 ? 'S'
15664 : '-')),
15665 glyph->pixel_width,
15666 glyph->u.cmp.id);
15667 if (glyph->u.cmp.automatic)
15668 fprintf (stderr,
15669 "[%d-%d]",
15670 glyph->u.cmp.from, glyph->u.cmp.to);
15671 fprintf (stderr, " . %4d %1.1d%1.1d\n"
15672 glyph->face_id,
15673 glyph->left_box_line_p,
15674 glyph->right_box_line_p);
15675 }
15676 }
15677
15678
15679 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
15680 GLYPHS 0 means don't show glyph contents.
15681 GLYPHS 1 means show glyphs in short form
15682 GLYPHS > 1 means show glyphs in long form. */
15683
15684 void
15685 dump_glyph_row (row, vpos, glyphs)
15686 struct glyph_row *row;
15687 int vpos, glyphs;
15688 {
15689 if (glyphs != 1)
15690 {
15691 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
15692 fprintf (stderr, "======================================================================\n");
15693
15694 fprintf (stderr, "%3d %5d %5d %4d %1.1d%1.1d%1.1d%1.1d\
15695 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
15696 vpos,
15697 MATRIX_ROW_START_CHARPOS (row),
15698 MATRIX_ROW_END_CHARPOS (row),
15699 row->used[TEXT_AREA],
15700 row->contains_overlapping_glyphs_p,
15701 row->enabled_p,
15702 row->truncated_on_left_p,
15703 row->truncated_on_right_p,
15704 row->continued_p,
15705 MATRIX_ROW_CONTINUATION_LINE_P (row),
15706 row->displays_text_p,
15707 row->ends_at_zv_p,
15708 row->fill_line_p,
15709 row->ends_in_middle_of_char_p,
15710 row->starts_in_middle_of_char_p,
15711 row->mouse_face_p,
15712 row->x,
15713 row->y,
15714 row->pixel_width,
15715 row->height,
15716 row->visible_height,
15717 row->ascent,
15718 row->phys_ascent);
15719 fprintf (stderr, "%9d %5d\t%5d\n", row->start.overlay_string_index,
15720 row->end.overlay_string_index,
15721 row->continuation_lines_width);
15722 fprintf (stderr, "%9d %5d\n",
15723 CHARPOS (row->start.string_pos),
15724 CHARPOS (row->end.string_pos));
15725 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
15726 row->end.dpvec_index);
15727 }
15728
15729 if (glyphs > 1)
15730 {
15731 int area;
15732
15733 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
15734 {
15735 struct glyph *glyph = row->glyphs[area];
15736 struct glyph *glyph_end = glyph + row->used[area];
15737
15738 /* Glyph for a line end in text. */
15739 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
15740 ++glyph_end;
15741
15742 if (glyph < glyph_end)
15743 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
15744
15745 for (; glyph < glyph_end; ++glyph)
15746 dump_glyph (row, glyph, area);
15747 }
15748 }
15749 else if (glyphs == 1)
15750 {
15751 int area;
15752
15753 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
15754 {
15755 char *s = (char *) alloca (row->used[area] + 1);
15756 int i;
15757
15758 for (i = 0; i < row->used[area]; ++i)
15759 {
15760 struct glyph *glyph = row->glyphs[area] + i;
15761 if (glyph->type == CHAR_GLYPH
15762 && glyph->u.ch < 0x80
15763 && glyph->u.ch >= ' ')
15764 s[i] = glyph->u.ch;
15765 else
15766 s[i] = '.';
15767 }
15768
15769 s[i] = '\0';
15770 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
15771 }
15772 }
15773 }
15774
15775
15776 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
15777 Sdump_glyph_matrix, 0, 1, "p",
15778 doc: /* Dump the current matrix of the selected window to stderr.
15779 Shows contents of glyph row structures. With non-nil
15780 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
15781 glyphs in short form, otherwise show glyphs in long form. */)
15782 (glyphs)
15783 Lisp_Object glyphs;
15784 {
15785 struct window *w = XWINDOW (selected_window);
15786 struct buffer *buffer = XBUFFER (w->buffer);
15787
15788 fprintf (stderr, "PT = %d, BEGV = %d. ZV = %d\n",
15789 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
15790 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
15791 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
15792 fprintf (stderr, "=============================================\n");
15793 dump_glyph_matrix (w->current_matrix,
15794 NILP (glyphs) ? 0 : XINT (glyphs));
15795 return Qnil;
15796 }
15797
15798
15799 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
15800 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
15801 ()
15802 {
15803 struct frame *f = XFRAME (selected_frame);
15804 dump_glyph_matrix (f->current_matrix, 1);
15805 return Qnil;
15806 }
15807
15808
15809 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
15810 doc: /* Dump glyph row ROW to stderr.
15811 GLYPH 0 means don't dump glyphs.
15812 GLYPH 1 means dump glyphs in short form.
15813 GLYPH > 1 or omitted means dump glyphs in long form. */)
15814 (row, glyphs)
15815 Lisp_Object row, glyphs;
15816 {
15817 struct glyph_matrix *matrix;
15818 int vpos;
15819
15820 CHECK_NUMBER (row);
15821 matrix = XWINDOW (selected_window)->current_matrix;
15822 vpos = XINT (row);
15823 if (vpos >= 0 && vpos < matrix->nrows)
15824 dump_glyph_row (MATRIX_ROW (matrix, vpos),
15825 vpos,
15826 INTEGERP (glyphs) ? XINT (glyphs) : 2);
15827 return Qnil;
15828 }
15829
15830
15831 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
15832 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
15833 GLYPH 0 means don't dump glyphs.
15834 GLYPH 1 means dump glyphs in short form.
15835 GLYPH > 1 or omitted means dump glyphs in long form. */)
15836 (row, glyphs)
15837 Lisp_Object row, glyphs;
15838 {
15839 struct frame *sf = SELECTED_FRAME ();
15840 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
15841 int vpos;
15842
15843 CHECK_NUMBER (row);
15844 vpos = XINT (row);
15845 if (vpos >= 0 && vpos < m->nrows)
15846 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
15847 INTEGERP (glyphs) ? XINT (glyphs) : 2);
15848 return Qnil;
15849 }
15850
15851
15852 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
15853 doc: /* Toggle tracing of redisplay.
15854 With ARG, turn tracing on if and only if ARG is positive. */)
15855 (arg)
15856 Lisp_Object arg;
15857 {
15858 if (NILP (arg))
15859 trace_redisplay_p = !trace_redisplay_p;
15860 else
15861 {
15862 arg = Fprefix_numeric_value (arg);
15863 trace_redisplay_p = XINT (arg) > 0;
15864 }
15865
15866 return Qnil;
15867 }
15868
15869
15870 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
15871 doc: /* Like `format', but print result to stderr.
15872 usage: (trace-to-stderr STRING &rest OBJECTS) */)
15873 (nargs, args)
15874 int nargs;
15875 Lisp_Object *args;
15876 {
15877 Lisp_Object s = Fformat (nargs, args);
15878 fprintf (stderr, "%s", SDATA (s));
15879 return Qnil;
15880 }
15881
15882 #endif /* GLYPH_DEBUG */
15883
15884
15885 \f
15886 /***********************************************************************
15887 Building Desired Matrix Rows
15888 ***********************************************************************/
15889
15890 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
15891 Used for non-window-redisplay windows, and for windows w/o left fringe. */
15892
15893 static struct glyph_row *
15894 get_overlay_arrow_glyph_row (w, overlay_arrow_string)
15895 struct window *w;
15896 Lisp_Object overlay_arrow_string;
15897 {
15898 struct frame *f = XFRAME (WINDOW_FRAME (w));
15899 struct buffer *buffer = XBUFFER (w->buffer);
15900 struct buffer *old = current_buffer;
15901 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
15902 int arrow_len = SCHARS (overlay_arrow_string);
15903 const unsigned char *arrow_end = arrow_string + arrow_len;
15904 const unsigned char *p;
15905 struct it it;
15906 int multibyte_p;
15907 int n_glyphs_before;
15908
15909 set_buffer_temp (buffer);
15910 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
15911 it.glyph_row->used[TEXT_AREA] = 0;
15912 SET_TEXT_POS (it.position, 0, 0);
15913
15914 multibyte_p = !NILP (buffer->enable_multibyte_characters);
15915 p = arrow_string;
15916 while (p < arrow_end)
15917 {
15918 Lisp_Object face, ilisp;
15919
15920 /* Get the next character. */
15921 if (multibyte_p)
15922 it.c = string_char_and_length (p, arrow_len, &it.len);
15923 else
15924 it.c = *p, it.len = 1;
15925 p += it.len;
15926
15927 /* Get its face. */
15928 ilisp = make_number (p - arrow_string);
15929 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
15930 it.face_id = compute_char_face (f, it.c, face);
15931
15932 /* Compute its width, get its glyphs. */
15933 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
15934 SET_TEXT_POS (it.position, -1, -1);
15935 PRODUCE_GLYPHS (&it);
15936
15937 /* If this character doesn't fit any more in the line, we have
15938 to remove some glyphs. */
15939 if (it.current_x > it.last_visible_x)
15940 {
15941 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
15942 break;
15943 }
15944 }
15945
15946 set_buffer_temp (old);
15947 return it.glyph_row;
15948 }
15949
15950
15951 /* Insert truncation glyphs at the start of IT->glyph_row. Truncation
15952 glyphs are only inserted for terminal frames since we can't really
15953 win with truncation glyphs when partially visible glyphs are
15954 involved. Which glyphs to insert is determined by
15955 produce_special_glyphs. */
15956
15957 static void
15958 insert_left_trunc_glyphs (it)
15959 struct it *it;
15960 {
15961 struct it truncate_it;
15962 struct glyph *from, *end, *to, *toend;
15963
15964 xassert (!FRAME_WINDOW_P (it->f));
15965
15966 /* Get the truncation glyphs. */
15967 truncate_it = *it;
15968 truncate_it.current_x = 0;
15969 truncate_it.face_id = DEFAULT_FACE_ID;
15970 truncate_it.glyph_row = &scratch_glyph_row;
15971 truncate_it.glyph_row->used[TEXT_AREA] = 0;
15972 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
15973 truncate_it.object = make_number (0);
15974 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
15975
15976 /* Overwrite glyphs from IT with truncation glyphs. */
15977 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
15978 end = from + truncate_it.glyph_row->used[TEXT_AREA];
15979 to = it->glyph_row->glyphs[TEXT_AREA];
15980 toend = to + it->glyph_row->used[TEXT_AREA];
15981
15982 while (from < end)
15983 *to++ = *from++;
15984
15985 /* There may be padding glyphs left over. Overwrite them too. */
15986 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
15987 {
15988 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
15989 while (from < end)
15990 *to++ = *from++;
15991 }
15992
15993 if (to > toend)
15994 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
15995 }
15996
15997
15998 /* Compute the pixel height and width of IT->glyph_row.
15999
16000 Most of the time, ascent and height of a display line will be equal
16001 to the max_ascent and max_height values of the display iterator
16002 structure. This is not the case if
16003
16004 1. We hit ZV without displaying anything. In this case, max_ascent
16005 and max_height will be zero.
16006
16007 2. We have some glyphs that don't contribute to the line height.
16008 (The glyph row flag contributes_to_line_height_p is for future
16009 pixmap extensions).
16010
16011 The first case is easily covered by using default values because in
16012 these cases, the line height does not really matter, except that it
16013 must not be zero. */
16014
16015 static void
16016 compute_line_metrics (it)
16017 struct it *it;
16018 {
16019 struct glyph_row *row = it->glyph_row;
16020 int area, i;
16021
16022 if (FRAME_WINDOW_P (it->f))
16023 {
16024 int i, min_y, max_y;
16025
16026 /* The line may consist of one space only, that was added to
16027 place the cursor on it. If so, the row's height hasn't been
16028 computed yet. */
16029 if (row->height == 0)
16030 {
16031 if (it->max_ascent + it->max_descent == 0)
16032 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
16033 row->ascent = it->max_ascent;
16034 row->height = it->max_ascent + it->max_descent;
16035 row->phys_ascent = it->max_phys_ascent;
16036 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
16037 row->extra_line_spacing = it->max_extra_line_spacing;
16038 }
16039
16040 /* Compute the width of this line. */
16041 row->pixel_width = row->x;
16042 for (i = 0; i < row->used[TEXT_AREA]; ++i)
16043 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
16044
16045 xassert (row->pixel_width >= 0);
16046 xassert (row->ascent >= 0 && row->height > 0);
16047
16048 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
16049 || MATRIX_ROW_OVERLAPS_PRED_P (row));
16050
16051 /* If first line's physical ascent is larger than its logical
16052 ascent, use the physical ascent, and make the row taller.
16053 This makes accented characters fully visible. */
16054 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
16055 && row->phys_ascent > row->ascent)
16056 {
16057 row->height += row->phys_ascent - row->ascent;
16058 row->ascent = row->phys_ascent;
16059 }
16060
16061 /* Compute how much of the line is visible. */
16062 row->visible_height = row->height;
16063
16064 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
16065 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
16066
16067 if (row->y < min_y)
16068 row->visible_height -= min_y - row->y;
16069 if (row->y + row->height > max_y)
16070 row->visible_height -= row->y + row->height - max_y;
16071 }
16072 else
16073 {
16074 row->pixel_width = row->used[TEXT_AREA];
16075 if (row->continued_p)
16076 row->pixel_width -= it->continuation_pixel_width;
16077 else if (row->truncated_on_right_p)
16078 row->pixel_width -= it->truncation_pixel_width;
16079 row->ascent = row->phys_ascent = 0;
16080 row->height = row->phys_height = row->visible_height = 1;
16081 row->extra_line_spacing = 0;
16082 }
16083
16084 /* Compute a hash code for this row. */
16085 row->hash = 0;
16086 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
16087 for (i = 0; i < row->used[area]; ++i)
16088 row->hash = ((((row->hash << 4) + (row->hash >> 24)) & 0x0fffffff)
16089 + row->glyphs[area][i].u.val
16090 + row->glyphs[area][i].face_id
16091 + row->glyphs[area][i].padding_p
16092 + (row->glyphs[area][i].type << 2));
16093
16094 it->max_ascent = it->max_descent = 0;
16095 it->max_phys_ascent = it->max_phys_descent = 0;
16096 }
16097
16098
16099 /* Append one space to the glyph row of iterator IT if doing a
16100 window-based redisplay. The space has the same face as
16101 IT->face_id. Value is non-zero if a space was added.
16102
16103 This function is called to make sure that there is always one glyph
16104 at the end of a glyph row that the cursor can be set on under
16105 window-systems. (If there weren't such a glyph we would not know
16106 how wide and tall a box cursor should be displayed).
16107
16108 At the same time this space let's a nicely handle clearing to the
16109 end of the line if the row ends in italic text. */
16110
16111 static int
16112 append_space_for_newline (it, default_face_p)
16113 struct it *it;
16114 int default_face_p;
16115 {
16116 if (FRAME_WINDOW_P (it->f))
16117 {
16118 int n = it->glyph_row->used[TEXT_AREA];
16119
16120 if (it->glyph_row->glyphs[TEXT_AREA] + n
16121 < it->glyph_row->glyphs[1 + TEXT_AREA])
16122 {
16123 /* Save some values that must not be changed.
16124 Must save IT->c and IT->len because otherwise
16125 ITERATOR_AT_END_P wouldn't work anymore after
16126 append_space_for_newline has been called. */
16127 enum display_element_type saved_what = it->what;
16128 int saved_c = it->c, saved_len = it->len;
16129 int saved_x = it->current_x;
16130 int saved_face_id = it->face_id;
16131 struct text_pos saved_pos;
16132 Lisp_Object saved_object;
16133 struct face *face;
16134
16135 saved_object = it->object;
16136 saved_pos = it->position;
16137
16138 it->what = IT_CHARACTER;
16139 bzero (&it->position, sizeof it->position);
16140 it->object = make_number (0);
16141 it->c = ' ';
16142 it->len = 1;
16143
16144 if (default_face_p)
16145 it->face_id = DEFAULT_FACE_ID;
16146 else if (it->face_before_selective_p)
16147 it->face_id = it->saved_face_id;
16148 face = FACE_FROM_ID (it->f, it->face_id);
16149 it->face_id = FACE_FOR_CHAR (it->f, face, 0, -1, Qnil);
16150
16151 PRODUCE_GLYPHS (it);
16152
16153 it->override_ascent = -1;
16154 it->constrain_row_ascent_descent_p = 0;
16155 it->current_x = saved_x;
16156 it->object = saved_object;
16157 it->position = saved_pos;
16158 it->what = saved_what;
16159 it->face_id = saved_face_id;
16160 it->len = saved_len;
16161 it->c = saved_c;
16162 return 1;
16163 }
16164 }
16165
16166 return 0;
16167 }
16168
16169
16170 /* Extend the face of the last glyph in the text area of IT->glyph_row
16171 to the end of the display line. Called from display_line.
16172 If the glyph row is empty, add a space glyph to it so that we
16173 know the face to draw. Set the glyph row flag fill_line_p. */
16174
16175 static void
16176 extend_face_to_end_of_line (it)
16177 struct it *it;
16178 {
16179 struct face *face;
16180 struct frame *f = it->f;
16181
16182 /* If line is already filled, do nothing. */
16183 if (it->current_x >= it->last_visible_x)
16184 return;
16185
16186 /* Face extension extends the background and box of IT->face_id
16187 to the end of the line. If the background equals the background
16188 of the frame, we don't have to do anything. */
16189 if (it->face_before_selective_p)
16190 face = FACE_FROM_ID (it->f, it->saved_face_id);
16191 else
16192 face = FACE_FROM_ID (f, it->face_id);
16193
16194 if (FRAME_WINDOW_P (f)
16195 && it->glyph_row->displays_text_p
16196 && face->box == FACE_NO_BOX
16197 && face->background == FRAME_BACKGROUND_PIXEL (f)
16198 && !face->stipple)
16199 return;
16200
16201 /* Set the glyph row flag indicating that the face of the last glyph
16202 in the text area has to be drawn to the end of the text area. */
16203 it->glyph_row->fill_line_p = 1;
16204
16205 /* If current character of IT is not ASCII, make sure we have the
16206 ASCII face. This will be automatically undone the next time
16207 get_next_display_element returns a multibyte character. Note
16208 that the character will always be single byte in unibyte text. */
16209 if (!ASCII_CHAR_P (it->c))
16210 {
16211 it->face_id = FACE_FOR_CHAR (f, face, 0, -1, Qnil);
16212 }
16213
16214 if (FRAME_WINDOW_P (f))
16215 {
16216 /* If the row is empty, add a space with the current face of IT,
16217 so that we know which face to draw. */
16218 if (it->glyph_row->used[TEXT_AREA] == 0)
16219 {
16220 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
16221 it->glyph_row->glyphs[TEXT_AREA][0].face_id = it->face_id;
16222 it->glyph_row->used[TEXT_AREA] = 1;
16223 }
16224 }
16225 else
16226 {
16227 /* Save some values that must not be changed. */
16228 int saved_x = it->current_x;
16229 struct text_pos saved_pos;
16230 Lisp_Object saved_object;
16231 enum display_element_type saved_what = it->what;
16232 int saved_face_id = it->face_id;
16233
16234 saved_object = it->object;
16235 saved_pos = it->position;
16236
16237 it->what = IT_CHARACTER;
16238 bzero (&it->position, sizeof it->position);
16239 it->object = make_number (0);
16240 it->c = ' ';
16241 it->len = 1;
16242 it->face_id = face->id;
16243
16244 PRODUCE_GLYPHS (it);
16245
16246 while (it->current_x <= it->last_visible_x)
16247 PRODUCE_GLYPHS (it);
16248
16249 /* Don't count these blanks really. It would let us insert a left
16250 truncation glyph below and make us set the cursor on them, maybe. */
16251 it->current_x = saved_x;
16252 it->object = saved_object;
16253 it->position = saved_pos;
16254 it->what = saved_what;
16255 it->face_id = saved_face_id;
16256 }
16257 }
16258
16259
16260 /* Value is non-zero if text starting at CHARPOS in current_buffer is
16261 trailing whitespace. */
16262
16263 static int
16264 trailing_whitespace_p (charpos)
16265 int charpos;
16266 {
16267 int bytepos = CHAR_TO_BYTE (charpos);
16268 int c = 0;
16269
16270 while (bytepos < ZV_BYTE
16271 && (c = FETCH_CHAR (bytepos),
16272 c == ' ' || c == '\t'))
16273 ++bytepos;
16274
16275 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
16276 {
16277 if (bytepos != PT_BYTE)
16278 return 1;
16279 }
16280 return 0;
16281 }
16282
16283
16284 /* Highlight trailing whitespace, if any, in ROW. */
16285
16286 void
16287 highlight_trailing_whitespace (f, row)
16288 struct frame *f;
16289 struct glyph_row *row;
16290 {
16291 int used = row->used[TEXT_AREA];
16292
16293 if (used)
16294 {
16295 struct glyph *start = row->glyphs[TEXT_AREA];
16296 struct glyph *glyph = start + used - 1;
16297
16298 /* Skip over glyphs inserted to display the cursor at the
16299 end of a line, for extending the face of the last glyph
16300 to the end of the line on terminals, and for truncation
16301 and continuation glyphs. */
16302 while (glyph >= start
16303 && glyph->type == CHAR_GLYPH
16304 && INTEGERP (glyph->object))
16305 --glyph;
16306
16307 /* If last glyph is a space or stretch, and it's trailing
16308 whitespace, set the face of all trailing whitespace glyphs in
16309 IT->glyph_row to `trailing-whitespace'. */
16310 if (glyph >= start
16311 && BUFFERP (glyph->object)
16312 && (glyph->type == STRETCH_GLYPH
16313 || (glyph->type == CHAR_GLYPH
16314 && glyph->u.ch == ' '))
16315 && trailing_whitespace_p (glyph->charpos))
16316 {
16317 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
16318 if (face_id < 0)
16319 return;
16320
16321 while (glyph >= start
16322 && BUFFERP (glyph->object)
16323 && (glyph->type == STRETCH_GLYPH
16324 || (glyph->type == CHAR_GLYPH
16325 && glyph->u.ch == ' ')))
16326 (glyph--)->face_id = face_id;
16327 }
16328 }
16329 }
16330
16331
16332 /* Value is non-zero if glyph row ROW in window W should be
16333 used to hold the cursor. */
16334
16335 static int
16336 cursor_row_p (w, row)
16337 struct window *w;
16338 struct glyph_row *row;
16339 {
16340 int cursor_row_p = 1;
16341
16342 if (PT == MATRIX_ROW_END_CHARPOS (row))
16343 {
16344 /* Suppose the row ends on a string.
16345 Unless the row is continued, that means it ends on a newline
16346 in the string. If it's anything other than a display string
16347 (e.g. a before-string from an overlay), we don't want the
16348 cursor there. (This heuristic seems to give the optimal
16349 behavior for the various types of multi-line strings.) */
16350 if (CHARPOS (row->end.string_pos) >= 0)
16351 {
16352 if (row->continued_p)
16353 cursor_row_p = 1;
16354 else
16355 {
16356 /* Check for `display' property. */
16357 struct glyph *beg = row->glyphs[TEXT_AREA];
16358 struct glyph *end = beg + row->used[TEXT_AREA] - 1;
16359 struct glyph *glyph;
16360
16361 cursor_row_p = 0;
16362 for (glyph = end; glyph >= beg; --glyph)
16363 if (STRINGP (glyph->object))
16364 {
16365 Lisp_Object prop
16366 = Fget_char_property (make_number (PT),
16367 Qdisplay, Qnil);
16368 cursor_row_p =
16369 (!NILP (prop)
16370 && display_prop_string_p (prop, glyph->object));
16371 break;
16372 }
16373 }
16374 }
16375 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
16376 {
16377 /* If the row ends in middle of a real character,
16378 and the line is continued, we want the cursor here.
16379 That's because MATRIX_ROW_END_CHARPOS would equal
16380 PT if PT is before the character. */
16381 if (!row->ends_in_ellipsis_p)
16382 cursor_row_p = row->continued_p;
16383 else
16384 /* If the row ends in an ellipsis, then
16385 MATRIX_ROW_END_CHARPOS will equal point after the invisible text.
16386 We want that position to be displayed after the ellipsis. */
16387 cursor_row_p = 0;
16388 }
16389 /* If the row ends at ZV, display the cursor at the end of that
16390 row instead of at the start of the row below. */
16391 else if (row->ends_at_zv_p)
16392 cursor_row_p = 1;
16393 else
16394 cursor_row_p = 0;
16395 }
16396
16397 return cursor_row_p;
16398 }
16399
16400 \f
16401
16402 /* Push the display property PROP so that it will be rendered at the
16403 current position in IT. */
16404
16405 static void
16406 push_display_prop (struct it *it, Lisp_Object prop)
16407 {
16408 push_it (it);
16409
16410 /* Never display a cursor on the prefix. */
16411 it->avoid_cursor_p = 1;
16412
16413 if (STRINGP (prop))
16414 {
16415 if (SCHARS (prop) == 0)
16416 {
16417 pop_it (it);
16418 return;
16419 }
16420
16421 it->string = prop;
16422 it->multibyte_p = STRING_MULTIBYTE (it->string);
16423 it->current.overlay_string_index = -1;
16424 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
16425 it->end_charpos = it->string_nchars = SCHARS (it->string);
16426 it->method = GET_FROM_STRING;
16427 it->stop_charpos = 0;
16428 }
16429 else if (CONSP (prop) && EQ (XCAR (prop), Qspace))
16430 {
16431 it->method = GET_FROM_STRETCH;
16432 it->object = prop;
16433 }
16434 #ifdef HAVE_WINDOW_SYSTEM
16435 else if (IMAGEP (prop))
16436 {
16437 it->what = IT_IMAGE;
16438 it->image_id = lookup_image (it->f, prop);
16439 it->method = GET_FROM_IMAGE;
16440 }
16441 #endif /* HAVE_WINDOW_SYSTEM */
16442 else
16443 {
16444 pop_it (it); /* bogus display property, give up */
16445 return;
16446 }
16447 }
16448
16449 /* Return the character-property PROP at the current position in IT. */
16450
16451 static Lisp_Object
16452 get_it_property (it, prop)
16453 struct it *it;
16454 Lisp_Object prop;
16455 {
16456 Lisp_Object position;
16457
16458 if (STRINGP (it->object))
16459 position = make_number (IT_STRING_CHARPOS (*it));
16460 else if (BUFFERP (it->object))
16461 position = make_number (IT_CHARPOS (*it));
16462 else
16463 return Qnil;
16464
16465 return Fget_char_property (position, prop, it->object);
16466 }
16467
16468 /* See if there's a line- or wrap-prefix, and if so, push it on IT. */
16469
16470 static void
16471 handle_line_prefix (struct it *it)
16472 {
16473 Lisp_Object prefix;
16474 if (it->continuation_lines_width > 0)
16475 {
16476 prefix = get_it_property (it, Qwrap_prefix);
16477 if (NILP (prefix))
16478 prefix = Vwrap_prefix;
16479 }
16480 else
16481 {
16482 prefix = get_it_property (it, Qline_prefix);
16483 if (NILP (prefix))
16484 prefix = Vline_prefix;
16485 }
16486 if (! NILP (prefix))
16487 push_display_prop (it, prefix);
16488 }
16489
16490 \f
16491
16492 /* Construct the glyph row IT->glyph_row in the desired matrix of
16493 IT->w from text at the current position of IT. See dispextern.h
16494 for an overview of struct it. Value is non-zero if
16495 IT->glyph_row displays text, as opposed to a line displaying ZV
16496 only. */
16497
16498 static int
16499 display_line (it)
16500 struct it *it;
16501 {
16502 struct glyph_row *row = it->glyph_row;
16503 Lisp_Object overlay_arrow_string;
16504 struct it wrap_it;
16505 int may_wrap = 0, wrap_x;
16506 int wrap_row_used = -1, wrap_row_ascent, wrap_row_height;
16507 int wrap_row_phys_ascent, wrap_row_phys_height;
16508 int wrap_row_extra_line_spacing;
16509
16510 /* We always start displaying at hpos zero even if hscrolled. */
16511 xassert (it->hpos == 0 && it->current_x == 0);
16512
16513 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
16514 >= it->w->desired_matrix->nrows)
16515 {
16516 it->w->nrows_scale_factor++;
16517 fonts_changed_p = 1;
16518 return 0;
16519 }
16520
16521 /* Is IT->w showing the region? */
16522 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
16523
16524 /* Clear the result glyph row and enable it. */
16525 prepare_desired_row (row);
16526
16527 row->y = it->current_y;
16528 row->start = it->start;
16529 row->continuation_lines_width = it->continuation_lines_width;
16530 row->displays_text_p = 1;
16531 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
16532 it->starts_in_middle_of_char_p = 0;
16533
16534 /* Arrange the overlays nicely for our purposes. Usually, we call
16535 display_line on only one line at a time, in which case this
16536 can't really hurt too much, or we call it on lines which appear
16537 one after another in the buffer, in which case all calls to
16538 recenter_overlay_lists but the first will be pretty cheap. */
16539 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
16540
16541 /* Move over display elements that are not visible because we are
16542 hscrolled. This may stop at an x-position < IT->first_visible_x
16543 if the first glyph is partially visible or if we hit a line end. */
16544 if (it->current_x < it->first_visible_x)
16545 {
16546 move_it_in_display_line_to (it, ZV, it->first_visible_x,
16547 MOVE_TO_POS | MOVE_TO_X);
16548 }
16549 else
16550 {
16551 /* We only do this when not calling `move_it_in_display_line_to'
16552 above, because move_it_in_display_line_to calls
16553 handle_line_prefix itself. */
16554 handle_line_prefix (it);
16555 }
16556
16557 /* Get the initial row height. This is either the height of the
16558 text hscrolled, if there is any, or zero. */
16559 row->ascent = it->max_ascent;
16560 row->height = it->max_ascent + it->max_descent;
16561 row->phys_ascent = it->max_phys_ascent;
16562 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
16563 row->extra_line_spacing = it->max_extra_line_spacing;
16564
16565 /* Loop generating characters. The loop is left with IT on the next
16566 character to display. */
16567 while (1)
16568 {
16569 int n_glyphs_before, hpos_before, x_before;
16570 int x, i, nglyphs;
16571 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
16572
16573 /* Retrieve the next thing to display. Value is zero if end of
16574 buffer reached. */
16575 if (!get_next_display_element (it))
16576 {
16577 /* Maybe add a space at the end of this line that is used to
16578 display the cursor there under X. Set the charpos of the
16579 first glyph of blank lines not corresponding to any text
16580 to -1. */
16581 #ifdef HAVE_WINDOW_SYSTEM
16582 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
16583 row->exact_window_width_line_p = 1;
16584 else
16585 #endif /* HAVE_WINDOW_SYSTEM */
16586 if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
16587 || row->used[TEXT_AREA] == 0)
16588 {
16589 row->glyphs[TEXT_AREA]->charpos = -1;
16590 row->displays_text_p = 0;
16591
16592 if (!NILP (XBUFFER (it->w->buffer)->indicate_empty_lines)
16593 && (!MINI_WINDOW_P (it->w)
16594 || (minibuf_level && EQ (it->window, minibuf_window))))
16595 row->indicate_empty_line_p = 1;
16596 }
16597
16598 it->continuation_lines_width = 0;
16599 row->ends_at_zv_p = 1;
16600 break;
16601 }
16602
16603 /* Now, get the metrics of what we want to display. This also
16604 generates glyphs in `row' (which is IT->glyph_row). */
16605 n_glyphs_before = row->used[TEXT_AREA];
16606 x = it->current_x;
16607
16608 /* Remember the line height so far in case the next element doesn't
16609 fit on the line. */
16610 if (it->line_wrap != TRUNCATE)
16611 {
16612 ascent = it->max_ascent;
16613 descent = it->max_descent;
16614 phys_ascent = it->max_phys_ascent;
16615 phys_descent = it->max_phys_descent;
16616
16617 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
16618 {
16619 if (IT_DISPLAYING_WHITESPACE (it))
16620 may_wrap = 1;
16621 else if (may_wrap)
16622 {
16623 wrap_it = *it;
16624 wrap_x = x;
16625 wrap_row_used = row->used[TEXT_AREA];
16626 wrap_row_ascent = row->ascent;
16627 wrap_row_height = row->height;
16628 wrap_row_phys_ascent = row->phys_ascent;
16629 wrap_row_phys_height = row->phys_height;
16630 wrap_row_extra_line_spacing = row->extra_line_spacing;
16631 may_wrap = 0;
16632 }
16633 }
16634 }
16635
16636 PRODUCE_GLYPHS (it);
16637
16638 /* If this display element was in marginal areas, continue with
16639 the next one. */
16640 if (it->area != TEXT_AREA)
16641 {
16642 row->ascent = max (row->ascent, it->max_ascent);
16643 row->height = max (row->height, it->max_ascent + it->max_descent);
16644 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
16645 row->phys_height = max (row->phys_height,
16646 it->max_phys_ascent + it->max_phys_descent);
16647 row->extra_line_spacing = max (row->extra_line_spacing,
16648 it->max_extra_line_spacing);
16649 set_iterator_to_next (it, 1);
16650 continue;
16651 }
16652
16653 /* Does the display element fit on the line? If we truncate
16654 lines, we should draw past the right edge of the window. If
16655 we don't truncate, we want to stop so that we can display the
16656 continuation glyph before the right margin. If lines are
16657 continued, there are two possible strategies for characters
16658 resulting in more than 1 glyph (e.g. tabs): Display as many
16659 glyphs as possible in this line and leave the rest for the
16660 continuation line, or display the whole element in the next
16661 line. Original redisplay did the former, so we do it also. */
16662 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
16663 hpos_before = it->hpos;
16664 x_before = x;
16665
16666 if (/* Not a newline. */
16667 nglyphs > 0
16668 /* Glyphs produced fit entirely in the line. */
16669 && it->current_x < it->last_visible_x)
16670 {
16671 it->hpos += nglyphs;
16672 row->ascent = max (row->ascent, it->max_ascent);
16673 row->height = max (row->height, it->max_ascent + it->max_descent);
16674 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
16675 row->phys_height = max (row->phys_height,
16676 it->max_phys_ascent + it->max_phys_descent);
16677 row->extra_line_spacing = max (row->extra_line_spacing,
16678 it->max_extra_line_spacing);
16679 if (it->current_x - it->pixel_width < it->first_visible_x)
16680 row->x = x - it->first_visible_x;
16681 }
16682 else
16683 {
16684 int new_x;
16685 struct glyph *glyph;
16686
16687 for (i = 0; i < nglyphs; ++i, x = new_x)
16688 {
16689 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
16690 new_x = x + glyph->pixel_width;
16691
16692 if (/* Lines are continued. */
16693 it->line_wrap != TRUNCATE
16694 && (/* Glyph doesn't fit on the line. */
16695 new_x > it->last_visible_x
16696 /* Or it fits exactly on a window system frame. */
16697 || (new_x == it->last_visible_x
16698 && FRAME_WINDOW_P (it->f))))
16699 {
16700 /* End of a continued line. */
16701
16702 if (it->hpos == 0
16703 || (new_x == it->last_visible_x
16704 && FRAME_WINDOW_P (it->f)))
16705 {
16706 /* Current glyph is the only one on the line or
16707 fits exactly on the line. We must continue
16708 the line because we can't draw the cursor
16709 after the glyph. */
16710 row->continued_p = 1;
16711 it->current_x = new_x;
16712 it->continuation_lines_width += new_x;
16713 ++it->hpos;
16714 if (i == nglyphs - 1)
16715 {
16716 /* If line-wrap is on, check if a previous
16717 wrap point was found. */
16718 if (wrap_row_used > 0
16719 /* Even if there is a previous wrap
16720 point, continue the line here as
16721 usual, if (i) the previous character
16722 was a space or tab AND (ii) the
16723 current character is not. */
16724 && (!may_wrap
16725 || IT_DISPLAYING_WHITESPACE (it)))
16726 goto back_to_wrap;
16727
16728 set_iterator_to_next (it, 1);
16729 #ifdef HAVE_WINDOW_SYSTEM
16730 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
16731 {
16732 if (!get_next_display_element (it))
16733 {
16734 row->exact_window_width_line_p = 1;
16735 it->continuation_lines_width = 0;
16736 row->continued_p = 0;
16737 row->ends_at_zv_p = 1;
16738 }
16739 else if (ITERATOR_AT_END_OF_LINE_P (it))
16740 {
16741 row->continued_p = 0;
16742 row->exact_window_width_line_p = 1;
16743 }
16744 }
16745 #endif /* HAVE_WINDOW_SYSTEM */
16746 }
16747 }
16748 else if (CHAR_GLYPH_PADDING_P (*glyph)
16749 && !FRAME_WINDOW_P (it->f))
16750 {
16751 /* A padding glyph that doesn't fit on this line.
16752 This means the whole character doesn't fit
16753 on the line. */
16754 row->used[TEXT_AREA] = n_glyphs_before;
16755
16756 /* Fill the rest of the row with continuation
16757 glyphs like in 20.x. */
16758 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
16759 < row->glyphs[1 + TEXT_AREA])
16760 produce_special_glyphs (it, IT_CONTINUATION);
16761
16762 row->continued_p = 1;
16763 it->current_x = x_before;
16764 it->continuation_lines_width += x_before;
16765
16766 /* Restore the height to what it was before the
16767 element not fitting on the line. */
16768 it->max_ascent = ascent;
16769 it->max_descent = descent;
16770 it->max_phys_ascent = phys_ascent;
16771 it->max_phys_descent = phys_descent;
16772 }
16773 else if (wrap_row_used > 0)
16774 {
16775 back_to_wrap:
16776 *it = wrap_it;
16777 it->continuation_lines_width += wrap_x;
16778 row->used[TEXT_AREA] = wrap_row_used;
16779 row->ascent = wrap_row_ascent;
16780 row->height = wrap_row_height;
16781 row->phys_ascent = wrap_row_phys_ascent;
16782 row->phys_height = wrap_row_phys_height;
16783 row->extra_line_spacing = wrap_row_extra_line_spacing;
16784 row->continued_p = 1;
16785 row->ends_at_zv_p = 0;
16786 row->exact_window_width_line_p = 0;
16787 it->continuation_lines_width += x;
16788
16789 /* Make sure that a non-default face is extended
16790 up to the right margin of the window. */
16791 extend_face_to_end_of_line (it);
16792 }
16793 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
16794 {
16795 /* A TAB that extends past the right edge of the
16796 window. This produces a single glyph on
16797 window system frames. We leave the glyph in
16798 this row and let it fill the row, but don't
16799 consume the TAB. */
16800 it->continuation_lines_width += it->last_visible_x;
16801 row->ends_in_middle_of_char_p = 1;
16802 row->continued_p = 1;
16803 glyph->pixel_width = it->last_visible_x - x;
16804 it->starts_in_middle_of_char_p = 1;
16805 }
16806 else
16807 {
16808 /* Something other than a TAB that draws past
16809 the right edge of the window. Restore
16810 positions to values before the element. */
16811 row->used[TEXT_AREA] = n_glyphs_before + i;
16812
16813 /* Display continuation glyphs. */
16814 if (!FRAME_WINDOW_P (it->f))
16815 produce_special_glyphs (it, IT_CONTINUATION);
16816 row->continued_p = 1;
16817
16818 it->current_x = x_before;
16819 it->continuation_lines_width += x;
16820 extend_face_to_end_of_line (it);
16821
16822 if (nglyphs > 1 && i > 0)
16823 {
16824 row->ends_in_middle_of_char_p = 1;
16825 it->starts_in_middle_of_char_p = 1;
16826 }
16827
16828 /* Restore the height to what it was before the
16829 element not fitting on the line. */
16830 it->max_ascent = ascent;
16831 it->max_descent = descent;
16832 it->max_phys_ascent = phys_ascent;
16833 it->max_phys_descent = phys_descent;
16834 }
16835
16836 break;
16837 }
16838 else if (new_x > it->first_visible_x)
16839 {
16840 /* Increment number of glyphs actually displayed. */
16841 ++it->hpos;
16842
16843 if (x < it->first_visible_x)
16844 /* Glyph is partially visible, i.e. row starts at
16845 negative X position. */
16846 row->x = x - it->first_visible_x;
16847 }
16848 else
16849 {
16850 /* Glyph is completely off the left margin of the
16851 window. This should not happen because of the
16852 move_it_in_display_line at the start of this
16853 function, unless the text display area of the
16854 window is empty. */
16855 xassert (it->first_visible_x <= it->last_visible_x);
16856 }
16857 }
16858
16859 row->ascent = max (row->ascent, it->max_ascent);
16860 row->height = max (row->height, it->max_ascent + it->max_descent);
16861 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
16862 row->phys_height = max (row->phys_height,
16863 it->max_phys_ascent + it->max_phys_descent);
16864 row->extra_line_spacing = max (row->extra_line_spacing,
16865 it->max_extra_line_spacing);
16866
16867 /* End of this display line if row is continued. */
16868 if (row->continued_p || row->ends_at_zv_p)
16869 break;
16870 }
16871
16872 at_end_of_line:
16873 /* Is this a line end? If yes, we're also done, after making
16874 sure that a non-default face is extended up to the right
16875 margin of the window. */
16876 if (ITERATOR_AT_END_OF_LINE_P (it))
16877 {
16878 int used_before = row->used[TEXT_AREA];
16879
16880 row->ends_in_newline_from_string_p = STRINGP (it->object);
16881
16882 #ifdef HAVE_WINDOW_SYSTEM
16883 /* Add a space at the end of the line that is used to
16884 display the cursor there. */
16885 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
16886 append_space_for_newline (it, 0);
16887 #endif /* HAVE_WINDOW_SYSTEM */
16888
16889 /* Extend the face to the end of the line. */
16890 extend_face_to_end_of_line (it);
16891
16892 /* Make sure we have the position. */
16893 if (used_before == 0)
16894 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
16895
16896 /* Consume the line end. This skips over invisible lines. */
16897 set_iterator_to_next (it, 1);
16898 it->continuation_lines_width = 0;
16899 break;
16900 }
16901
16902 /* Proceed with next display element. Note that this skips
16903 over lines invisible because of selective display. */
16904 set_iterator_to_next (it, 1);
16905
16906 /* If we truncate lines, we are done when the last displayed
16907 glyphs reach past the right margin of the window. */
16908 if (it->line_wrap == TRUNCATE
16909 && (FRAME_WINDOW_P (it->f)
16910 ? (it->current_x >= it->last_visible_x)
16911 : (it->current_x > it->last_visible_x)))
16912 {
16913 /* Maybe add truncation glyphs. */
16914 if (!FRAME_WINDOW_P (it->f))
16915 {
16916 int i, n;
16917
16918 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
16919 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
16920 break;
16921
16922 for (n = row->used[TEXT_AREA]; i < n; ++i)
16923 {
16924 row->used[TEXT_AREA] = i;
16925 produce_special_glyphs (it, IT_TRUNCATION);
16926 }
16927 }
16928 #ifdef HAVE_WINDOW_SYSTEM
16929 else
16930 {
16931 /* Don't truncate if we can overflow newline into fringe. */
16932 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
16933 {
16934 if (!get_next_display_element (it))
16935 {
16936 it->continuation_lines_width = 0;
16937 row->ends_at_zv_p = 1;
16938 row->exact_window_width_line_p = 1;
16939 break;
16940 }
16941 if (ITERATOR_AT_END_OF_LINE_P (it))
16942 {
16943 row->exact_window_width_line_p = 1;
16944 goto at_end_of_line;
16945 }
16946 }
16947 }
16948 #endif /* HAVE_WINDOW_SYSTEM */
16949
16950 row->truncated_on_right_p = 1;
16951 it->continuation_lines_width = 0;
16952 reseat_at_next_visible_line_start (it, 0);
16953 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
16954 it->hpos = hpos_before;
16955 it->current_x = x_before;
16956 break;
16957 }
16958 }
16959
16960 /* If line is not empty and hscrolled, maybe insert truncation glyphs
16961 at the left window margin. */
16962 if (it->first_visible_x
16963 && IT_CHARPOS (*it) != MATRIX_ROW_START_CHARPOS (row))
16964 {
16965 if (!FRAME_WINDOW_P (it->f))
16966 insert_left_trunc_glyphs (it);
16967 row->truncated_on_left_p = 1;
16968 }
16969
16970 /* If the start of this line is the overlay arrow-position, then
16971 mark this glyph row as the one containing the overlay arrow.
16972 This is clearly a mess with variable size fonts. It would be
16973 better to let it be displayed like cursors under X. */
16974 if ((row->displays_text_p || !overlay_arrow_seen)
16975 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
16976 !NILP (overlay_arrow_string)))
16977 {
16978 /* Overlay arrow in window redisplay is a fringe bitmap. */
16979 if (STRINGP (overlay_arrow_string))
16980 {
16981 struct glyph_row *arrow_row
16982 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
16983 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
16984 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
16985 struct glyph *p = row->glyphs[TEXT_AREA];
16986 struct glyph *p2, *end;
16987
16988 /* Copy the arrow glyphs. */
16989 while (glyph < arrow_end)
16990 *p++ = *glyph++;
16991
16992 /* Throw away padding glyphs. */
16993 p2 = p;
16994 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
16995 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
16996 ++p2;
16997 if (p2 > p)
16998 {
16999 while (p2 < end)
17000 *p++ = *p2++;
17001 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
17002 }
17003 }
17004 else
17005 {
17006 xassert (INTEGERP (overlay_arrow_string));
17007 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
17008 }
17009 overlay_arrow_seen = 1;
17010 }
17011
17012 /* Compute pixel dimensions of this line. */
17013 compute_line_metrics (it);
17014
17015 /* Remember the position at which this line ends. */
17016 row->end = it->current;
17017
17018 /* Record whether this row ends inside an ellipsis. */
17019 row->ends_in_ellipsis_p
17020 = (it->method == GET_FROM_DISPLAY_VECTOR
17021 && it->ellipsis_p);
17022
17023 /* Save fringe bitmaps in this row. */
17024 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
17025 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
17026 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
17027 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
17028
17029 it->left_user_fringe_bitmap = 0;
17030 it->left_user_fringe_face_id = 0;
17031 it->right_user_fringe_bitmap = 0;
17032 it->right_user_fringe_face_id = 0;
17033
17034 /* Maybe set the cursor. */
17035 if (it->w->cursor.vpos < 0
17036 && PT >= MATRIX_ROW_START_CHARPOS (row)
17037 && PT <= MATRIX_ROW_END_CHARPOS (row)
17038 && cursor_row_p (it->w, row))
17039 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
17040
17041 /* Highlight trailing whitespace. */
17042 if (!NILP (Vshow_trailing_whitespace))
17043 highlight_trailing_whitespace (it->f, it->glyph_row);
17044
17045 /* Prepare for the next line. This line starts horizontally at (X
17046 HPOS) = (0 0). Vertical positions are incremented. As a
17047 convenience for the caller, IT->glyph_row is set to the next
17048 row to be used. */
17049 it->current_x = it->hpos = 0;
17050 it->current_y += row->height;
17051 ++it->vpos;
17052 ++it->glyph_row;
17053 it->start = it->current;
17054 return row->displays_text_p;
17055 }
17056
17057
17058 \f
17059 /***********************************************************************
17060 Menu Bar
17061 ***********************************************************************/
17062
17063 /* Redisplay the menu bar in the frame for window W.
17064
17065 The menu bar of X frames that don't have X toolkit support is
17066 displayed in a special window W->frame->menu_bar_window.
17067
17068 The menu bar of terminal frames is treated specially as far as
17069 glyph matrices are concerned. Menu bar lines are not part of
17070 windows, so the update is done directly on the frame matrix rows
17071 for the menu bar. */
17072
17073 static void
17074 display_menu_bar (w)
17075 struct window *w;
17076 {
17077 struct frame *f = XFRAME (WINDOW_FRAME (w));
17078 struct it it;
17079 Lisp_Object items;
17080 int i;
17081
17082 /* Don't do all this for graphical frames. */
17083 #ifdef HAVE_NTGUI
17084 if (FRAME_W32_P (f))
17085 return;
17086 #endif
17087 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
17088 if (FRAME_X_P (f))
17089 return;
17090 #endif
17091
17092 #ifdef HAVE_NS
17093 if (FRAME_NS_P (f))
17094 return;
17095 #endif /* HAVE_NS */
17096
17097 #ifdef USE_X_TOOLKIT
17098 xassert (!FRAME_WINDOW_P (f));
17099 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
17100 it.first_visible_x = 0;
17101 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
17102 #else /* not USE_X_TOOLKIT */
17103 if (FRAME_WINDOW_P (f))
17104 {
17105 /* Menu bar lines are displayed in the desired matrix of the
17106 dummy window menu_bar_window. */
17107 struct window *menu_w;
17108 xassert (WINDOWP (f->menu_bar_window));
17109 menu_w = XWINDOW (f->menu_bar_window);
17110 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
17111 MENU_FACE_ID);
17112 it.first_visible_x = 0;
17113 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
17114 }
17115 else
17116 {
17117 /* This is a TTY frame, i.e. character hpos/vpos are used as
17118 pixel x/y. */
17119 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
17120 MENU_FACE_ID);
17121 it.first_visible_x = 0;
17122 it.last_visible_x = FRAME_COLS (f);
17123 }
17124 #endif /* not USE_X_TOOLKIT */
17125
17126 if (! mode_line_inverse_video)
17127 /* Force the menu-bar to be displayed in the default face. */
17128 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
17129
17130 /* Clear all rows of the menu bar. */
17131 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
17132 {
17133 struct glyph_row *row = it.glyph_row + i;
17134 clear_glyph_row (row);
17135 row->enabled_p = 1;
17136 row->full_width_p = 1;
17137 }
17138
17139 /* Display all items of the menu bar. */
17140 items = FRAME_MENU_BAR_ITEMS (it.f);
17141 for (i = 0; i < XVECTOR (items)->size; i += 4)
17142 {
17143 Lisp_Object string;
17144
17145 /* Stop at nil string. */
17146 string = AREF (items, i + 1);
17147 if (NILP (string))
17148 break;
17149
17150 /* Remember where item was displayed. */
17151 ASET (items, i + 3, make_number (it.hpos));
17152
17153 /* Display the item, pad with one space. */
17154 if (it.current_x < it.last_visible_x)
17155 display_string (NULL, string, Qnil, 0, 0, &it,
17156 SCHARS (string) + 1, 0, 0, -1);
17157 }
17158
17159 /* Fill out the line with spaces. */
17160 if (it.current_x < it.last_visible_x)
17161 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
17162
17163 /* Compute the total height of the lines. */
17164 compute_line_metrics (&it);
17165 }
17166
17167
17168 \f
17169 /***********************************************************************
17170 Mode Line
17171 ***********************************************************************/
17172
17173 /* Redisplay mode lines in the window tree whose root is WINDOW. If
17174 FORCE is non-zero, redisplay mode lines unconditionally.
17175 Otherwise, redisplay only mode lines that are garbaged. Value is
17176 the number of windows whose mode lines were redisplayed. */
17177
17178 static int
17179 redisplay_mode_lines (window, force)
17180 Lisp_Object window;
17181 int force;
17182 {
17183 int nwindows = 0;
17184
17185 while (!NILP (window))
17186 {
17187 struct window *w = XWINDOW (window);
17188
17189 if (WINDOWP (w->hchild))
17190 nwindows += redisplay_mode_lines (w->hchild, force);
17191 else if (WINDOWP (w->vchild))
17192 nwindows += redisplay_mode_lines (w->vchild, force);
17193 else if (force
17194 || FRAME_GARBAGED_P (XFRAME (w->frame))
17195 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
17196 {
17197 struct text_pos lpoint;
17198 struct buffer *old = current_buffer;
17199
17200 /* Set the window's buffer for the mode line display. */
17201 SET_TEXT_POS (lpoint, PT, PT_BYTE);
17202 set_buffer_internal_1 (XBUFFER (w->buffer));
17203
17204 /* Point refers normally to the selected window. For any
17205 other window, set up appropriate value. */
17206 if (!EQ (window, selected_window))
17207 {
17208 struct text_pos pt;
17209
17210 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
17211 if (CHARPOS (pt) < BEGV)
17212 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
17213 else if (CHARPOS (pt) > (ZV - 1))
17214 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
17215 else
17216 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
17217 }
17218
17219 /* Display mode lines. */
17220 clear_glyph_matrix (w->desired_matrix);
17221 if (display_mode_lines (w))
17222 {
17223 ++nwindows;
17224 w->must_be_updated_p = 1;
17225 }
17226
17227 /* Restore old settings. */
17228 set_buffer_internal_1 (old);
17229 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
17230 }
17231
17232 window = w->next;
17233 }
17234
17235 return nwindows;
17236 }
17237
17238
17239 /* Display the mode and/or top line of window W. Value is the number
17240 of mode lines displayed. */
17241
17242 static int
17243 display_mode_lines (w)
17244 struct window *w;
17245 {
17246 Lisp_Object old_selected_window, old_selected_frame;
17247 int n = 0;
17248
17249 old_selected_frame = selected_frame;
17250 selected_frame = w->frame;
17251 old_selected_window = selected_window;
17252 XSETWINDOW (selected_window, w);
17253
17254 /* These will be set while the mode line specs are processed. */
17255 line_number_displayed = 0;
17256 w->column_number_displayed = Qnil;
17257
17258 if (WINDOW_WANTS_MODELINE_P (w))
17259 {
17260 struct window *sel_w = XWINDOW (old_selected_window);
17261
17262 /* Select mode line face based on the real selected window. */
17263 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
17264 current_buffer->mode_line_format);
17265 ++n;
17266 }
17267
17268 if (WINDOW_WANTS_HEADER_LINE_P (w))
17269 {
17270 display_mode_line (w, HEADER_LINE_FACE_ID,
17271 current_buffer->header_line_format);
17272 ++n;
17273 }
17274
17275 selected_frame = old_selected_frame;
17276 selected_window = old_selected_window;
17277 return n;
17278 }
17279
17280
17281 /* Display mode or top line of window W. FACE_ID specifies which line
17282 to display; it is either MODE_LINE_FACE_ID or HEADER_LINE_FACE_ID.
17283 FORMAT is the mode line format to display. Value is the pixel
17284 height of the mode line displayed. */
17285
17286 static int
17287 display_mode_line (w, face_id, format)
17288 struct window *w;
17289 enum face_id face_id;
17290 Lisp_Object format;
17291 {
17292 struct it it;
17293 struct face *face;
17294 int count = SPECPDL_INDEX ();
17295
17296 init_iterator (&it, w, -1, -1, NULL, face_id);
17297 /* Don't extend on a previously drawn mode-line.
17298 This may happen if called from pos_visible_p. */
17299 it.glyph_row->enabled_p = 0;
17300 prepare_desired_row (it.glyph_row);
17301
17302 it.glyph_row->mode_line_p = 1;
17303
17304 if (! mode_line_inverse_video)
17305 /* Force the mode-line to be displayed in the default face. */
17306 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
17307
17308 record_unwind_protect (unwind_format_mode_line,
17309 format_mode_line_unwind_data (NULL, Qnil, 0));
17310
17311 mode_line_target = MODE_LINE_DISPLAY;
17312
17313 /* Temporarily make frame's keyboard the current kboard so that
17314 kboard-local variables in the mode_line_format will get the right
17315 values. */
17316 push_kboard (FRAME_KBOARD (it.f));
17317 record_unwind_save_match_data ();
17318 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
17319 pop_kboard ();
17320
17321 unbind_to (count, Qnil);
17322
17323 /* Fill up with spaces. */
17324 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
17325
17326 compute_line_metrics (&it);
17327 it.glyph_row->full_width_p = 1;
17328 it.glyph_row->continued_p = 0;
17329 it.glyph_row->truncated_on_left_p = 0;
17330 it.glyph_row->truncated_on_right_p = 0;
17331
17332 /* Make a 3D mode-line have a shadow at its right end. */
17333 face = FACE_FROM_ID (it.f, face_id);
17334 extend_face_to_end_of_line (&it);
17335 if (face->box != FACE_NO_BOX)
17336 {
17337 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
17338 + it.glyph_row->used[TEXT_AREA] - 1);
17339 last->right_box_line_p = 1;
17340 }
17341
17342 return it.glyph_row->height;
17343 }
17344
17345 /* Move element ELT in LIST to the front of LIST.
17346 Return the updated list. */
17347
17348 static Lisp_Object
17349 move_elt_to_front (elt, list)
17350 Lisp_Object elt, list;
17351 {
17352 register Lisp_Object tail, prev;
17353 register Lisp_Object tem;
17354
17355 tail = list;
17356 prev = Qnil;
17357 while (CONSP (tail))
17358 {
17359 tem = XCAR (tail);
17360
17361 if (EQ (elt, tem))
17362 {
17363 /* Splice out the link TAIL. */
17364 if (NILP (prev))
17365 list = XCDR (tail);
17366 else
17367 Fsetcdr (prev, XCDR (tail));
17368
17369 /* Now make it the first. */
17370 Fsetcdr (tail, list);
17371 return tail;
17372 }
17373 else
17374 prev = tail;
17375 tail = XCDR (tail);
17376 QUIT;
17377 }
17378
17379 /* Not found--return unchanged LIST. */
17380 return list;
17381 }
17382
17383 /* Contribute ELT to the mode line for window IT->w. How it
17384 translates into text depends on its data type.
17385
17386 IT describes the display environment in which we display, as usual.
17387
17388 DEPTH is the depth in recursion. It is used to prevent
17389 infinite recursion here.
17390
17391 FIELD_WIDTH is the number of characters the display of ELT should
17392 occupy in the mode line, and PRECISION is the maximum number of
17393 characters to display from ELT's representation. See
17394 display_string for details.
17395
17396 Returns the hpos of the end of the text generated by ELT.
17397
17398 PROPS is a property list to add to any string we encounter.
17399
17400 If RISKY is nonzero, remove (disregard) any properties in any string
17401 we encounter, and ignore :eval and :propertize.
17402
17403 The global variable `mode_line_target' determines whether the
17404 output is passed to `store_mode_line_noprop',
17405 `store_mode_line_string', or `display_string'. */
17406
17407 static int
17408 display_mode_element (it, depth, field_width, precision, elt, props, risky)
17409 struct it *it;
17410 int depth;
17411 int field_width, precision;
17412 Lisp_Object elt, props;
17413 int risky;
17414 {
17415 int n = 0, field, prec;
17416 int literal = 0;
17417
17418 tail_recurse:
17419 if (depth > 100)
17420 elt = build_string ("*too-deep*");
17421
17422 depth++;
17423
17424 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
17425 {
17426 case Lisp_String:
17427 {
17428 /* A string: output it and check for %-constructs within it. */
17429 unsigned char c;
17430 int offset = 0;
17431
17432 if (SCHARS (elt) > 0
17433 && (!NILP (props) || risky))
17434 {
17435 Lisp_Object oprops, aelt;
17436 oprops = Ftext_properties_at (make_number (0), elt);
17437
17438 /* If the starting string's properties are not what
17439 we want, translate the string. Also, if the string
17440 is risky, do that anyway. */
17441
17442 if (NILP (Fequal (props, oprops)) || risky)
17443 {
17444 /* If the starting string has properties,
17445 merge the specified ones onto the existing ones. */
17446 if (! NILP (oprops) && !risky)
17447 {
17448 Lisp_Object tem;
17449
17450 oprops = Fcopy_sequence (oprops);
17451 tem = props;
17452 while (CONSP (tem))
17453 {
17454 oprops = Fplist_put (oprops, XCAR (tem),
17455 XCAR (XCDR (tem)));
17456 tem = XCDR (XCDR (tem));
17457 }
17458 props = oprops;
17459 }
17460
17461 aelt = Fassoc (elt, mode_line_proptrans_alist);
17462 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
17463 {
17464 /* AELT is what we want. Move it to the front
17465 without consing. */
17466 elt = XCAR (aelt);
17467 mode_line_proptrans_alist
17468 = move_elt_to_front (aelt, mode_line_proptrans_alist);
17469 }
17470 else
17471 {
17472 Lisp_Object tem;
17473
17474 /* If AELT has the wrong props, it is useless.
17475 so get rid of it. */
17476 if (! NILP (aelt))
17477 mode_line_proptrans_alist
17478 = Fdelq (aelt, mode_line_proptrans_alist);
17479
17480 elt = Fcopy_sequence (elt);
17481 Fset_text_properties (make_number (0), Flength (elt),
17482 props, elt);
17483 /* Add this item to mode_line_proptrans_alist. */
17484 mode_line_proptrans_alist
17485 = Fcons (Fcons (elt, props),
17486 mode_line_proptrans_alist);
17487 /* Truncate mode_line_proptrans_alist
17488 to at most 50 elements. */
17489 tem = Fnthcdr (make_number (50),
17490 mode_line_proptrans_alist);
17491 if (! NILP (tem))
17492 XSETCDR (tem, Qnil);
17493 }
17494 }
17495 }
17496
17497 offset = 0;
17498
17499 if (literal)
17500 {
17501 prec = precision - n;
17502 switch (mode_line_target)
17503 {
17504 case MODE_LINE_NOPROP:
17505 case MODE_LINE_TITLE:
17506 n += store_mode_line_noprop (SDATA (elt), -1, prec);
17507 break;
17508 case MODE_LINE_STRING:
17509 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
17510 break;
17511 case MODE_LINE_DISPLAY:
17512 n += display_string (NULL, elt, Qnil, 0, 0, it,
17513 0, prec, 0, STRING_MULTIBYTE (elt));
17514 break;
17515 }
17516
17517 break;
17518 }
17519
17520 /* Handle the non-literal case. */
17521
17522 while ((precision <= 0 || n < precision)
17523 && SREF (elt, offset) != 0
17524 && (mode_line_target != MODE_LINE_DISPLAY
17525 || it->current_x < it->last_visible_x))
17526 {
17527 int last_offset = offset;
17528
17529 /* Advance to end of string or next format specifier. */
17530 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
17531 ;
17532
17533 if (offset - 1 != last_offset)
17534 {
17535 int nchars, nbytes;
17536
17537 /* Output to end of string or up to '%'. Field width
17538 is length of string. Don't output more than
17539 PRECISION allows us. */
17540 offset--;
17541
17542 prec = c_string_width (SDATA (elt) + last_offset,
17543 offset - last_offset, precision - n,
17544 &nchars, &nbytes);
17545
17546 switch (mode_line_target)
17547 {
17548 case MODE_LINE_NOPROP:
17549 case MODE_LINE_TITLE:
17550 n += store_mode_line_noprop (SDATA (elt) + last_offset, 0, prec);
17551 break;
17552 case MODE_LINE_STRING:
17553 {
17554 int bytepos = last_offset;
17555 int charpos = string_byte_to_char (elt, bytepos);
17556 int endpos = (precision <= 0
17557 ? string_byte_to_char (elt, offset)
17558 : charpos + nchars);
17559
17560 n += store_mode_line_string (NULL,
17561 Fsubstring (elt, make_number (charpos),
17562 make_number (endpos)),
17563 0, 0, 0, Qnil);
17564 }
17565 break;
17566 case MODE_LINE_DISPLAY:
17567 {
17568 int bytepos = last_offset;
17569 int charpos = string_byte_to_char (elt, bytepos);
17570
17571 if (precision <= 0)
17572 nchars = string_byte_to_char (elt, offset) - charpos;
17573 n += display_string (NULL, elt, Qnil, 0, charpos,
17574 it, 0, nchars, 0,
17575 STRING_MULTIBYTE (elt));
17576 }
17577 break;
17578 }
17579 }
17580 else /* c == '%' */
17581 {
17582 int percent_position = offset;
17583
17584 /* Get the specified minimum width. Zero means
17585 don't pad. */
17586 field = 0;
17587 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
17588 field = field * 10 + c - '0';
17589
17590 /* Don't pad beyond the total padding allowed. */
17591 if (field_width - n > 0 && field > field_width - n)
17592 field = field_width - n;
17593
17594 /* Note that either PRECISION <= 0 or N < PRECISION. */
17595 prec = precision - n;
17596
17597 if (c == 'M')
17598 n += display_mode_element (it, depth, field, prec,
17599 Vglobal_mode_string, props,
17600 risky);
17601 else if (c != 0)
17602 {
17603 int multibyte;
17604 int bytepos, charpos;
17605 unsigned char *spec;
17606
17607 bytepos = percent_position;
17608 charpos = (STRING_MULTIBYTE (elt)
17609 ? string_byte_to_char (elt, bytepos)
17610 : bytepos);
17611 spec
17612 = decode_mode_spec (it->w, c, field, prec, &multibyte);
17613
17614 switch (mode_line_target)
17615 {
17616 case MODE_LINE_NOPROP:
17617 case MODE_LINE_TITLE:
17618 n += store_mode_line_noprop (spec, field, prec);
17619 break;
17620 case MODE_LINE_STRING:
17621 {
17622 int len = strlen (spec);
17623 Lisp_Object tem = make_string (spec, len);
17624 props = Ftext_properties_at (make_number (charpos), elt);
17625 /* Should only keep face property in props */
17626 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
17627 }
17628 break;
17629 case MODE_LINE_DISPLAY:
17630 {
17631 int nglyphs_before, nwritten;
17632
17633 nglyphs_before = it->glyph_row->used[TEXT_AREA];
17634 nwritten = display_string (spec, Qnil, elt,
17635 charpos, 0, it,
17636 field, prec, 0,
17637 multibyte);
17638
17639 /* Assign to the glyphs written above the
17640 string where the `%x' came from, position
17641 of the `%'. */
17642 if (nwritten > 0)
17643 {
17644 struct glyph *glyph
17645 = (it->glyph_row->glyphs[TEXT_AREA]
17646 + nglyphs_before);
17647 int i;
17648
17649 for (i = 0; i < nwritten; ++i)
17650 {
17651 glyph[i].object = elt;
17652 glyph[i].charpos = charpos;
17653 }
17654
17655 n += nwritten;
17656 }
17657 }
17658 break;
17659 }
17660 }
17661 else /* c == 0 */
17662 break;
17663 }
17664 }
17665 }
17666 break;
17667
17668 case Lisp_Symbol:
17669 /* A symbol: process the value of the symbol recursively
17670 as if it appeared here directly. Avoid error if symbol void.
17671 Special case: if value of symbol is a string, output the string
17672 literally. */
17673 {
17674 register Lisp_Object tem;
17675
17676 /* If the variable is not marked as risky to set
17677 then its contents are risky to use. */
17678 if (NILP (Fget (elt, Qrisky_local_variable)))
17679 risky = 1;
17680
17681 tem = Fboundp (elt);
17682 if (!NILP (tem))
17683 {
17684 tem = Fsymbol_value (elt);
17685 /* If value is a string, output that string literally:
17686 don't check for % within it. */
17687 if (STRINGP (tem))
17688 literal = 1;
17689
17690 if (!EQ (tem, elt))
17691 {
17692 /* Give up right away for nil or t. */
17693 elt = tem;
17694 goto tail_recurse;
17695 }
17696 }
17697 }
17698 break;
17699
17700 case Lisp_Cons:
17701 {
17702 register Lisp_Object car, tem;
17703
17704 /* A cons cell: five distinct cases.
17705 If first element is :eval or :propertize, do something special.
17706 If first element is a string or a cons, process all the elements
17707 and effectively concatenate them.
17708 If first element is a negative number, truncate displaying cdr to
17709 at most that many characters. If positive, pad (with spaces)
17710 to at least that many characters.
17711 If first element is a symbol, process the cadr or caddr recursively
17712 according to whether the symbol's value is non-nil or nil. */
17713 car = XCAR (elt);
17714 if (EQ (car, QCeval))
17715 {
17716 /* An element of the form (:eval FORM) means evaluate FORM
17717 and use the result as mode line elements. */
17718
17719 if (risky)
17720 break;
17721
17722 if (CONSP (XCDR (elt)))
17723 {
17724 Lisp_Object spec;
17725 spec = safe_eval (XCAR (XCDR (elt)));
17726 n += display_mode_element (it, depth, field_width - n,
17727 precision - n, spec, props,
17728 risky);
17729 }
17730 }
17731 else if (EQ (car, QCpropertize))
17732 {
17733 /* An element of the form (:propertize ELT PROPS...)
17734 means display ELT but applying properties PROPS. */
17735
17736 if (risky)
17737 break;
17738
17739 if (CONSP (XCDR (elt)))
17740 n += display_mode_element (it, depth, field_width - n,
17741 precision - n, XCAR (XCDR (elt)),
17742 XCDR (XCDR (elt)), risky);
17743 }
17744 else if (SYMBOLP (car))
17745 {
17746 tem = Fboundp (car);
17747 elt = XCDR (elt);
17748 if (!CONSP (elt))
17749 goto invalid;
17750 /* elt is now the cdr, and we know it is a cons cell.
17751 Use its car if CAR has a non-nil value. */
17752 if (!NILP (tem))
17753 {
17754 tem = Fsymbol_value (car);
17755 if (!NILP (tem))
17756 {
17757 elt = XCAR (elt);
17758 goto tail_recurse;
17759 }
17760 }
17761 /* Symbol's value is nil (or symbol is unbound)
17762 Get the cddr of the original list
17763 and if possible find the caddr and use that. */
17764 elt = XCDR (elt);
17765 if (NILP (elt))
17766 break;
17767 else if (!CONSP (elt))
17768 goto invalid;
17769 elt = XCAR (elt);
17770 goto tail_recurse;
17771 }
17772 else if (INTEGERP (car))
17773 {
17774 register int lim = XINT (car);
17775 elt = XCDR (elt);
17776 if (lim < 0)
17777 {
17778 /* Negative int means reduce maximum width. */
17779 if (precision <= 0)
17780 precision = -lim;
17781 else
17782 precision = min (precision, -lim);
17783 }
17784 else if (lim > 0)
17785 {
17786 /* Padding specified. Don't let it be more than
17787 current maximum. */
17788 if (precision > 0)
17789 lim = min (precision, lim);
17790
17791 /* If that's more padding than already wanted, queue it.
17792 But don't reduce padding already specified even if
17793 that is beyond the current truncation point. */
17794 field_width = max (lim, field_width);
17795 }
17796 goto tail_recurse;
17797 }
17798 else if (STRINGP (car) || CONSP (car))
17799 {
17800 register int limit = 50;
17801 /* Limit is to protect against circular lists. */
17802 while (CONSP (elt)
17803 && --limit > 0
17804 && (precision <= 0 || n < precision))
17805 {
17806 n += display_mode_element (it, depth,
17807 /* Do padding only after the last
17808 element in the list. */
17809 (! CONSP (XCDR (elt))
17810 ? field_width - n
17811 : 0),
17812 precision - n, XCAR (elt),
17813 props, risky);
17814 elt = XCDR (elt);
17815 }
17816 }
17817 }
17818 break;
17819
17820 default:
17821 invalid:
17822 elt = build_string ("*invalid*");
17823 goto tail_recurse;
17824 }
17825
17826 /* Pad to FIELD_WIDTH. */
17827 if (field_width > 0 && n < field_width)
17828 {
17829 switch (mode_line_target)
17830 {
17831 case MODE_LINE_NOPROP:
17832 case MODE_LINE_TITLE:
17833 n += store_mode_line_noprop ("", field_width - n, 0);
17834 break;
17835 case MODE_LINE_STRING:
17836 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
17837 break;
17838 case MODE_LINE_DISPLAY:
17839 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
17840 0, 0, 0);
17841 break;
17842 }
17843 }
17844
17845 return n;
17846 }
17847
17848 /* Store a mode-line string element in mode_line_string_list.
17849
17850 If STRING is non-null, display that C string. Otherwise, the Lisp
17851 string LISP_STRING is displayed.
17852
17853 FIELD_WIDTH is the minimum number of output glyphs to produce.
17854 If STRING has fewer characters than FIELD_WIDTH, pad to the right
17855 with spaces. FIELD_WIDTH <= 0 means don't pad.
17856
17857 PRECISION is the maximum number of characters to output from
17858 STRING. PRECISION <= 0 means don't truncate the string.
17859
17860 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
17861 properties to the string.
17862
17863 PROPS are the properties to add to the string.
17864 The mode_line_string_face face property is always added to the string.
17865 */
17866
17867 static int
17868 store_mode_line_string (string, lisp_string, copy_string, field_width, precision, props)
17869 char *string;
17870 Lisp_Object lisp_string;
17871 int copy_string;
17872 int field_width;
17873 int precision;
17874 Lisp_Object props;
17875 {
17876 int len;
17877 int n = 0;
17878
17879 if (string != NULL)
17880 {
17881 len = strlen (string);
17882 if (precision > 0 && len > precision)
17883 len = precision;
17884 lisp_string = make_string (string, len);
17885 if (NILP (props))
17886 props = mode_line_string_face_prop;
17887 else if (!NILP (mode_line_string_face))
17888 {
17889 Lisp_Object face = Fplist_get (props, Qface);
17890 props = Fcopy_sequence (props);
17891 if (NILP (face))
17892 face = mode_line_string_face;
17893 else
17894 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
17895 props = Fplist_put (props, Qface, face);
17896 }
17897 Fadd_text_properties (make_number (0), make_number (len),
17898 props, lisp_string);
17899 }
17900 else
17901 {
17902 len = XFASTINT (Flength (lisp_string));
17903 if (precision > 0 && len > precision)
17904 {
17905 len = precision;
17906 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
17907 precision = -1;
17908 }
17909 if (!NILP (mode_line_string_face))
17910 {
17911 Lisp_Object face;
17912 if (NILP (props))
17913 props = Ftext_properties_at (make_number (0), lisp_string);
17914 face = Fplist_get (props, Qface);
17915 if (NILP (face))
17916 face = mode_line_string_face;
17917 else
17918 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
17919 props = Fcons (Qface, Fcons (face, Qnil));
17920 if (copy_string)
17921 lisp_string = Fcopy_sequence (lisp_string);
17922 }
17923 if (!NILP (props))
17924 Fadd_text_properties (make_number (0), make_number (len),
17925 props, lisp_string);
17926 }
17927
17928 if (len > 0)
17929 {
17930 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
17931 n += len;
17932 }
17933
17934 if (field_width > len)
17935 {
17936 field_width -= len;
17937 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
17938 if (!NILP (props))
17939 Fadd_text_properties (make_number (0), make_number (field_width),
17940 props, lisp_string);
17941 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
17942 n += field_width;
17943 }
17944
17945 return n;
17946 }
17947
17948
17949 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
17950 1, 4, 0,
17951 doc: /* Format a string out of a mode line format specification.
17952 First arg FORMAT specifies the mode line format (see `mode-line-format'
17953 for details) to use.
17954
17955 Optional second arg FACE specifies the face property to put
17956 on all characters for which no face is specified.
17957 The value t means whatever face the window's mode line currently uses
17958 \(either `mode-line' or `mode-line-inactive', depending).
17959 A value of nil means the default is no face property.
17960 If FACE is an integer, the value string has no text properties.
17961
17962 Optional third and fourth args WINDOW and BUFFER specify the window
17963 and buffer to use as the context for the formatting (defaults
17964 are the selected window and the window's buffer). */)
17965 (format, face, window, buffer)
17966 Lisp_Object format, face, window, buffer;
17967 {
17968 struct it it;
17969 int len;
17970 struct window *w;
17971 struct buffer *old_buffer = NULL;
17972 int face_id = -1;
17973 int no_props = INTEGERP (face);
17974 int count = SPECPDL_INDEX ();
17975 Lisp_Object str;
17976 int string_start = 0;
17977
17978 if (NILP (window))
17979 window = selected_window;
17980 CHECK_WINDOW (window);
17981 w = XWINDOW (window);
17982
17983 if (NILP (buffer))
17984 buffer = w->buffer;
17985 CHECK_BUFFER (buffer);
17986
17987 /* Make formatting the modeline a non-op when noninteractive, otherwise
17988 there will be problems later caused by a partially initialized frame. */
17989 if (NILP (format) || noninteractive)
17990 return empty_unibyte_string;
17991
17992 if (no_props)
17993 face = Qnil;
17994
17995 if (!NILP (face))
17996 {
17997 if (EQ (face, Qt))
17998 face = (EQ (window, selected_window) ? Qmode_line : Qmode_line_inactive);
17999 face_id = lookup_named_face (XFRAME (WINDOW_FRAME (w)), face, 0);
18000 }
18001
18002 if (face_id < 0)
18003 face_id = DEFAULT_FACE_ID;
18004
18005 if (XBUFFER (buffer) != current_buffer)
18006 old_buffer = current_buffer;
18007
18008 /* Save things including mode_line_proptrans_alist,
18009 and set that to nil so that we don't alter the outer value. */
18010 record_unwind_protect (unwind_format_mode_line,
18011 format_mode_line_unwind_data
18012 (old_buffer, selected_window, 1));
18013 mode_line_proptrans_alist = Qnil;
18014
18015 Fselect_window (window, Qt);
18016 if (old_buffer)
18017 set_buffer_internal_1 (XBUFFER (buffer));
18018
18019 init_iterator (&it, w, -1, -1, NULL, face_id);
18020
18021 if (no_props)
18022 {
18023 mode_line_target = MODE_LINE_NOPROP;
18024 mode_line_string_face_prop = Qnil;
18025 mode_line_string_list = Qnil;
18026 string_start = MODE_LINE_NOPROP_LEN (0);
18027 }
18028 else
18029 {
18030 mode_line_target = MODE_LINE_STRING;
18031 mode_line_string_list = Qnil;
18032 mode_line_string_face = face;
18033 mode_line_string_face_prop
18034 = (NILP (face) ? Qnil : Fcons (Qface, Fcons (face, Qnil)));
18035 }
18036
18037 push_kboard (FRAME_KBOARD (it.f));
18038 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
18039 pop_kboard ();
18040
18041 if (no_props)
18042 {
18043 len = MODE_LINE_NOPROP_LEN (string_start);
18044 str = make_string (mode_line_noprop_buf + string_start, len);
18045 }
18046 else
18047 {
18048 mode_line_string_list = Fnreverse (mode_line_string_list);
18049 str = Fmapconcat (intern ("identity"), mode_line_string_list,
18050 empty_unibyte_string);
18051 }
18052
18053 unbind_to (count, Qnil);
18054 return str;
18055 }
18056
18057 /* Write a null-terminated, right justified decimal representation of
18058 the positive integer D to BUF using a minimal field width WIDTH. */
18059
18060 static void
18061 pint2str (buf, width, d)
18062 register char *buf;
18063 register int width;
18064 register int d;
18065 {
18066 register char *p = buf;
18067
18068 if (d <= 0)
18069 *p++ = '0';
18070 else
18071 {
18072 while (d > 0)
18073 {
18074 *p++ = d % 10 + '0';
18075 d /= 10;
18076 }
18077 }
18078
18079 for (width -= (int) (p - buf); width > 0; --width)
18080 *p++ = ' ';
18081 *p-- = '\0';
18082 while (p > buf)
18083 {
18084 d = *buf;
18085 *buf++ = *p;
18086 *p-- = d;
18087 }
18088 }
18089
18090 /* Write a null-terminated, right justified decimal and "human
18091 readable" representation of the nonnegative integer D to BUF using
18092 a minimal field width WIDTH. D should be smaller than 999.5e24. */
18093
18094 static const char power_letter[] =
18095 {
18096 0, /* not used */
18097 'k', /* kilo */
18098 'M', /* mega */
18099 'G', /* giga */
18100 'T', /* tera */
18101 'P', /* peta */
18102 'E', /* exa */
18103 'Z', /* zetta */
18104 'Y' /* yotta */
18105 };
18106
18107 static void
18108 pint2hrstr (buf, width, d)
18109 char *buf;
18110 int width;
18111 int d;
18112 {
18113 /* We aim to represent the nonnegative integer D as
18114 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
18115 int quotient = d;
18116 int remainder = 0;
18117 /* -1 means: do not use TENTHS. */
18118 int tenths = -1;
18119 int exponent = 0;
18120
18121 /* Length of QUOTIENT.TENTHS as a string. */
18122 int length;
18123
18124 char * psuffix;
18125 char * p;
18126
18127 if (1000 <= quotient)
18128 {
18129 /* Scale to the appropriate EXPONENT. */
18130 do
18131 {
18132 remainder = quotient % 1000;
18133 quotient /= 1000;
18134 exponent++;
18135 }
18136 while (1000 <= quotient);
18137
18138 /* Round to nearest and decide whether to use TENTHS or not. */
18139 if (quotient <= 9)
18140 {
18141 tenths = remainder / 100;
18142 if (50 <= remainder % 100)
18143 {
18144 if (tenths < 9)
18145 tenths++;
18146 else
18147 {
18148 quotient++;
18149 if (quotient == 10)
18150 tenths = -1;
18151 else
18152 tenths = 0;
18153 }
18154 }
18155 }
18156 else
18157 if (500 <= remainder)
18158 {
18159 if (quotient < 999)
18160 quotient++;
18161 else
18162 {
18163 quotient = 1;
18164 exponent++;
18165 tenths = 0;
18166 }
18167 }
18168 }
18169
18170 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
18171 if (tenths == -1 && quotient <= 99)
18172 if (quotient <= 9)
18173 length = 1;
18174 else
18175 length = 2;
18176 else
18177 length = 3;
18178 p = psuffix = buf + max (width, length);
18179
18180 /* Print EXPONENT. */
18181 if (exponent)
18182 *psuffix++ = power_letter[exponent];
18183 *psuffix = '\0';
18184
18185 /* Print TENTHS. */
18186 if (tenths >= 0)
18187 {
18188 *--p = '0' + tenths;
18189 *--p = '.';
18190 }
18191
18192 /* Print QUOTIENT. */
18193 do
18194 {
18195 int digit = quotient % 10;
18196 *--p = '0' + digit;
18197 }
18198 while ((quotient /= 10) != 0);
18199
18200 /* Print leading spaces. */
18201 while (buf < p)
18202 *--p = ' ';
18203 }
18204
18205 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
18206 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
18207 type of CODING_SYSTEM. Return updated pointer into BUF. */
18208
18209 static unsigned char invalid_eol_type[] = "(*invalid*)";
18210
18211 static char *
18212 decode_mode_spec_coding (coding_system, buf, eol_flag)
18213 Lisp_Object coding_system;
18214 register char *buf;
18215 int eol_flag;
18216 {
18217 Lisp_Object val;
18218 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
18219 const unsigned char *eol_str;
18220 int eol_str_len;
18221 /* The EOL conversion we are using. */
18222 Lisp_Object eoltype;
18223
18224 val = CODING_SYSTEM_SPEC (coding_system);
18225 eoltype = Qnil;
18226
18227 if (!VECTORP (val)) /* Not yet decided. */
18228 {
18229 if (multibyte)
18230 *buf++ = '-';
18231 if (eol_flag)
18232 eoltype = eol_mnemonic_undecided;
18233 /* Don't mention EOL conversion if it isn't decided. */
18234 }
18235 else
18236 {
18237 Lisp_Object attrs;
18238 Lisp_Object eolvalue;
18239
18240 attrs = AREF (val, 0);
18241 eolvalue = AREF (val, 2);
18242
18243 if (multibyte)
18244 *buf++ = XFASTINT (CODING_ATTR_MNEMONIC (attrs));
18245
18246 if (eol_flag)
18247 {
18248 /* The EOL conversion that is normal on this system. */
18249
18250 if (NILP (eolvalue)) /* Not yet decided. */
18251 eoltype = eol_mnemonic_undecided;
18252 else if (VECTORP (eolvalue)) /* Not yet decided. */
18253 eoltype = eol_mnemonic_undecided;
18254 else /* eolvalue is Qunix, Qdos, or Qmac. */
18255 eoltype = (EQ (eolvalue, Qunix)
18256 ? eol_mnemonic_unix
18257 : (EQ (eolvalue, Qdos) == 1
18258 ? eol_mnemonic_dos : eol_mnemonic_mac));
18259 }
18260 }
18261
18262 if (eol_flag)
18263 {
18264 /* Mention the EOL conversion if it is not the usual one. */
18265 if (STRINGP (eoltype))
18266 {
18267 eol_str = SDATA (eoltype);
18268 eol_str_len = SBYTES (eoltype);
18269 }
18270 else if (CHARACTERP (eoltype))
18271 {
18272 unsigned char *tmp = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
18273 eol_str_len = CHAR_STRING (XINT (eoltype), tmp);
18274 eol_str = tmp;
18275 }
18276 else
18277 {
18278 eol_str = invalid_eol_type;
18279 eol_str_len = sizeof (invalid_eol_type) - 1;
18280 }
18281 bcopy (eol_str, buf, eol_str_len);
18282 buf += eol_str_len;
18283 }
18284
18285 return buf;
18286 }
18287
18288 /* Return a string for the output of a mode line %-spec for window W,
18289 generated by character C. PRECISION >= 0 means don't return a
18290 string longer than that value. FIELD_WIDTH > 0 means pad the
18291 string returned with spaces to that value. Return 1 in *MULTIBYTE
18292 if the result is multibyte text.
18293
18294 Note we operate on the current buffer for most purposes,
18295 the exception being w->base_line_pos. */
18296
18297 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
18298
18299 static char *
18300 decode_mode_spec (w, c, field_width, precision, multibyte)
18301 struct window *w;
18302 register int c;
18303 int field_width, precision;
18304 int *multibyte;
18305 {
18306 Lisp_Object obj;
18307 struct frame *f = XFRAME (WINDOW_FRAME (w));
18308 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
18309 struct buffer *b = current_buffer;
18310
18311 obj = Qnil;
18312 *multibyte = 0;
18313
18314 switch (c)
18315 {
18316 case '*':
18317 if (!NILP (b->read_only))
18318 return "%";
18319 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
18320 return "*";
18321 return "-";
18322
18323 case '+':
18324 /* This differs from %* only for a modified read-only buffer. */
18325 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
18326 return "*";
18327 if (!NILP (b->read_only))
18328 return "%";
18329 return "-";
18330
18331 case '&':
18332 /* This differs from %* in ignoring read-only-ness. */
18333 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
18334 return "*";
18335 return "-";
18336
18337 case '%':
18338 return "%";
18339
18340 case '[':
18341 {
18342 int i;
18343 char *p;
18344
18345 if (command_loop_level > 5)
18346 return "[[[... ";
18347 p = decode_mode_spec_buf;
18348 for (i = 0; i < command_loop_level; i++)
18349 *p++ = '[';
18350 *p = 0;
18351 return decode_mode_spec_buf;
18352 }
18353
18354 case ']':
18355 {
18356 int i;
18357 char *p;
18358
18359 if (command_loop_level > 5)
18360 return " ...]]]";
18361 p = decode_mode_spec_buf;
18362 for (i = 0; i < command_loop_level; i++)
18363 *p++ = ']';
18364 *p = 0;
18365 return decode_mode_spec_buf;
18366 }
18367
18368 case '-':
18369 {
18370 register int i;
18371
18372 /* Let lots_of_dashes be a string of infinite length. */
18373 if (mode_line_target == MODE_LINE_NOPROP ||
18374 mode_line_target == MODE_LINE_STRING)
18375 return "--";
18376 if (field_width <= 0
18377 || field_width > sizeof (lots_of_dashes))
18378 {
18379 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
18380 decode_mode_spec_buf[i] = '-';
18381 decode_mode_spec_buf[i] = '\0';
18382 return decode_mode_spec_buf;
18383 }
18384 else
18385 return lots_of_dashes;
18386 }
18387
18388 case 'b':
18389 obj = b->name;
18390 break;
18391
18392 case 'c':
18393 /* %c and %l are ignored in `frame-title-format'.
18394 (In redisplay_internal, the frame title is drawn _before_ the
18395 windows are updated, so the stuff which depends on actual
18396 window contents (such as %l) may fail to render properly, or
18397 even crash emacs.) */
18398 if (mode_line_target == MODE_LINE_TITLE)
18399 return "";
18400 else
18401 {
18402 int col = (int) current_column (); /* iftc */
18403 w->column_number_displayed = make_number (col);
18404 pint2str (decode_mode_spec_buf, field_width, col);
18405 return decode_mode_spec_buf;
18406 }
18407
18408 case 'e':
18409 #ifndef SYSTEM_MALLOC
18410 {
18411 if (NILP (Vmemory_full))
18412 return "";
18413 else
18414 return "!MEM FULL! ";
18415 }
18416 #else
18417 return "";
18418 #endif
18419
18420 case 'F':
18421 /* %F displays the frame name. */
18422 if (!NILP (f->title))
18423 return (char *) SDATA (f->title);
18424 if (f->explicit_name || ! FRAME_WINDOW_P (f))
18425 return (char *) SDATA (f->name);
18426 return "Emacs";
18427
18428 case 'f':
18429 obj = b->filename;
18430 break;
18431
18432 case 'i':
18433 {
18434 int size = ZV - BEGV;
18435 pint2str (decode_mode_spec_buf, field_width, size);
18436 return decode_mode_spec_buf;
18437 }
18438
18439 case 'I':
18440 {
18441 int size = ZV - BEGV;
18442 pint2hrstr (decode_mode_spec_buf, field_width, size);
18443 return decode_mode_spec_buf;
18444 }
18445
18446 case 'l':
18447 {
18448 int startpos, startpos_byte, line, linepos, linepos_byte;
18449 int topline, nlines, junk, height;
18450
18451 /* %c and %l are ignored in `frame-title-format'. */
18452 if (mode_line_target == MODE_LINE_TITLE)
18453 return "";
18454
18455 startpos = XMARKER (w->start)->charpos;
18456 startpos_byte = marker_byte_position (w->start);
18457 height = WINDOW_TOTAL_LINES (w);
18458
18459 /* If we decided that this buffer isn't suitable for line numbers,
18460 don't forget that too fast. */
18461 if (EQ (w->base_line_pos, w->buffer))
18462 goto no_value;
18463 /* But do forget it, if the window shows a different buffer now. */
18464 else if (BUFFERP (w->base_line_pos))
18465 w->base_line_pos = Qnil;
18466
18467 /* If the buffer is very big, don't waste time. */
18468 if (INTEGERP (Vline_number_display_limit)
18469 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
18470 {
18471 w->base_line_pos = Qnil;
18472 w->base_line_number = Qnil;
18473 goto no_value;
18474 }
18475
18476 if (INTEGERP (w->base_line_number)
18477 && INTEGERP (w->base_line_pos)
18478 && XFASTINT (w->base_line_pos) <= startpos)
18479 {
18480 line = XFASTINT (w->base_line_number);
18481 linepos = XFASTINT (w->base_line_pos);
18482 linepos_byte = buf_charpos_to_bytepos (b, linepos);
18483 }
18484 else
18485 {
18486 line = 1;
18487 linepos = BUF_BEGV (b);
18488 linepos_byte = BUF_BEGV_BYTE (b);
18489 }
18490
18491 /* Count lines from base line to window start position. */
18492 nlines = display_count_lines (linepos, linepos_byte,
18493 startpos_byte,
18494 startpos, &junk);
18495
18496 topline = nlines + line;
18497
18498 /* Determine a new base line, if the old one is too close
18499 or too far away, or if we did not have one.
18500 "Too close" means it's plausible a scroll-down would
18501 go back past it. */
18502 if (startpos == BUF_BEGV (b))
18503 {
18504 w->base_line_number = make_number (topline);
18505 w->base_line_pos = make_number (BUF_BEGV (b));
18506 }
18507 else if (nlines < height + 25 || nlines > height * 3 + 50
18508 || linepos == BUF_BEGV (b))
18509 {
18510 int limit = BUF_BEGV (b);
18511 int limit_byte = BUF_BEGV_BYTE (b);
18512 int position;
18513 int distance = (height * 2 + 30) * line_number_display_limit_width;
18514
18515 if (startpos - distance > limit)
18516 {
18517 limit = startpos - distance;
18518 limit_byte = CHAR_TO_BYTE (limit);
18519 }
18520
18521 nlines = display_count_lines (startpos, startpos_byte,
18522 limit_byte,
18523 - (height * 2 + 30),
18524 &position);
18525 /* If we couldn't find the lines we wanted within
18526 line_number_display_limit_width chars per line,
18527 give up on line numbers for this window. */
18528 if (position == limit_byte && limit == startpos - distance)
18529 {
18530 w->base_line_pos = w->buffer;
18531 w->base_line_number = Qnil;
18532 goto no_value;
18533 }
18534
18535 w->base_line_number = make_number (topline - nlines);
18536 w->base_line_pos = make_number (BYTE_TO_CHAR (position));
18537 }
18538
18539 /* Now count lines from the start pos to point. */
18540 nlines = display_count_lines (startpos, startpos_byte,
18541 PT_BYTE, PT, &junk);
18542
18543 /* Record that we did display the line number. */
18544 line_number_displayed = 1;
18545
18546 /* Make the string to show. */
18547 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
18548 return decode_mode_spec_buf;
18549 no_value:
18550 {
18551 char* p = decode_mode_spec_buf;
18552 int pad = field_width - 2;
18553 while (pad-- > 0)
18554 *p++ = ' ';
18555 *p++ = '?';
18556 *p++ = '?';
18557 *p = '\0';
18558 return decode_mode_spec_buf;
18559 }
18560 }
18561 break;
18562
18563 case 'm':
18564 obj = b->mode_name;
18565 break;
18566
18567 case 'n':
18568 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
18569 return " Narrow";
18570 break;
18571
18572 case 'p':
18573 {
18574 int pos = marker_position (w->start);
18575 int total = BUF_ZV (b) - BUF_BEGV (b);
18576
18577 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
18578 {
18579 if (pos <= BUF_BEGV (b))
18580 return "All";
18581 else
18582 return "Bottom";
18583 }
18584 else if (pos <= BUF_BEGV (b))
18585 return "Top";
18586 else
18587 {
18588 if (total > 1000000)
18589 /* Do it differently for a large value, to avoid overflow. */
18590 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
18591 else
18592 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
18593 /* We can't normally display a 3-digit number,
18594 so get us a 2-digit number that is close. */
18595 if (total == 100)
18596 total = 99;
18597 sprintf (decode_mode_spec_buf, "%2d%%", total);
18598 return decode_mode_spec_buf;
18599 }
18600 }
18601
18602 /* Display percentage of size above the bottom of the screen. */
18603 case 'P':
18604 {
18605 int toppos = marker_position (w->start);
18606 int botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
18607 int total = BUF_ZV (b) - BUF_BEGV (b);
18608
18609 if (botpos >= BUF_ZV (b))
18610 {
18611 if (toppos <= BUF_BEGV (b))
18612 return "All";
18613 else
18614 return "Bottom";
18615 }
18616 else
18617 {
18618 if (total > 1000000)
18619 /* Do it differently for a large value, to avoid overflow. */
18620 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
18621 else
18622 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
18623 /* We can't normally display a 3-digit number,
18624 so get us a 2-digit number that is close. */
18625 if (total == 100)
18626 total = 99;
18627 if (toppos <= BUF_BEGV (b))
18628 sprintf (decode_mode_spec_buf, "Top%2d%%", total);
18629 else
18630 sprintf (decode_mode_spec_buf, "%2d%%", total);
18631 return decode_mode_spec_buf;
18632 }
18633 }
18634
18635 case 's':
18636 /* status of process */
18637 obj = Fget_buffer_process (Fcurrent_buffer ());
18638 if (NILP (obj))
18639 return "no process";
18640 #ifdef subprocesses
18641 obj = Fsymbol_name (Fprocess_status (obj));
18642 #endif
18643 break;
18644
18645 case '@':
18646 {
18647 Lisp_Object val;
18648 val = call1 (intern ("file-remote-p"), current_buffer->directory);
18649 if (NILP (val))
18650 return "-";
18651 else
18652 return "@";
18653 }
18654
18655 case 't': /* indicate TEXT or BINARY */
18656 #ifdef MODE_LINE_BINARY_TEXT
18657 return MODE_LINE_BINARY_TEXT (b);
18658 #else
18659 return "T";
18660 #endif
18661
18662 case 'z':
18663 /* coding-system (not including end-of-line format) */
18664 case 'Z':
18665 /* coding-system (including end-of-line type) */
18666 {
18667 int eol_flag = (c == 'Z');
18668 char *p = decode_mode_spec_buf;
18669
18670 if (! FRAME_WINDOW_P (f))
18671 {
18672 /* No need to mention EOL here--the terminal never needs
18673 to do EOL conversion. */
18674 p = decode_mode_spec_coding (CODING_ID_NAME
18675 (FRAME_KEYBOARD_CODING (f)->id),
18676 p, 0);
18677 p = decode_mode_spec_coding (CODING_ID_NAME
18678 (FRAME_TERMINAL_CODING (f)->id),
18679 p, 0);
18680 }
18681 p = decode_mode_spec_coding (b->buffer_file_coding_system,
18682 p, eol_flag);
18683
18684 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
18685 #ifdef subprocesses
18686 obj = Fget_buffer_process (Fcurrent_buffer ());
18687 if (PROCESSP (obj))
18688 {
18689 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
18690 p, eol_flag);
18691 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
18692 p, eol_flag);
18693 }
18694 #endif /* subprocesses */
18695 #endif /* 0 */
18696 *p = 0;
18697 return decode_mode_spec_buf;
18698 }
18699 }
18700
18701 if (STRINGP (obj))
18702 {
18703 *multibyte = STRING_MULTIBYTE (obj);
18704 return (char *) SDATA (obj);
18705 }
18706 else
18707 return "";
18708 }
18709
18710
18711 /* Count up to COUNT lines starting from START / START_BYTE.
18712 But don't go beyond LIMIT_BYTE.
18713 Return the number of lines thus found (always nonnegative).
18714
18715 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
18716
18717 static int
18718 display_count_lines (start, start_byte, limit_byte, count, byte_pos_ptr)
18719 int start, start_byte, limit_byte, count;
18720 int *byte_pos_ptr;
18721 {
18722 register unsigned char *cursor;
18723 unsigned char *base;
18724
18725 register int ceiling;
18726 register unsigned char *ceiling_addr;
18727 int orig_count = count;
18728
18729 /* If we are not in selective display mode,
18730 check only for newlines. */
18731 int selective_display = (!NILP (current_buffer->selective_display)
18732 && !INTEGERP (current_buffer->selective_display));
18733
18734 if (count > 0)
18735 {
18736 while (start_byte < limit_byte)
18737 {
18738 ceiling = BUFFER_CEILING_OF (start_byte);
18739 ceiling = min (limit_byte - 1, ceiling);
18740 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
18741 base = (cursor = BYTE_POS_ADDR (start_byte));
18742 while (1)
18743 {
18744 if (selective_display)
18745 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
18746 ;
18747 else
18748 while (*cursor != '\n' && ++cursor != ceiling_addr)
18749 ;
18750
18751 if (cursor != ceiling_addr)
18752 {
18753 if (--count == 0)
18754 {
18755 start_byte += cursor - base + 1;
18756 *byte_pos_ptr = start_byte;
18757 return orig_count;
18758 }
18759 else
18760 if (++cursor == ceiling_addr)
18761 break;
18762 }
18763 else
18764 break;
18765 }
18766 start_byte += cursor - base;
18767 }
18768 }
18769 else
18770 {
18771 while (start_byte > limit_byte)
18772 {
18773 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
18774 ceiling = max (limit_byte, ceiling);
18775 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
18776 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
18777 while (1)
18778 {
18779 if (selective_display)
18780 while (--cursor != ceiling_addr
18781 && *cursor != '\n' && *cursor != 015)
18782 ;
18783 else
18784 while (--cursor != ceiling_addr && *cursor != '\n')
18785 ;
18786
18787 if (cursor != ceiling_addr)
18788 {
18789 if (++count == 0)
18790 {
18791 start_byte += cursor - base + 1;
18792 *byte_pos_ptr = start_byte;
18793 /* When scanning backwards, we should
18794 not count the newline posterior to which we stop. */
18795 return - orig_count - 1;
18796 }
18797 }
18798 else
18799 break;
18800 }
18801 /* Here we add 1 to compensate for the last decrement
18802 of CURSOR, which took it past the valid range. */
18803 start_byte += cursor - base + 1;
18804 }
18805 }
18806
18807 *byte_pos_ptr = limit_byte;
18808
18809 if (count < 0)
18810 return - orig_count + count;
18811 return orig_count - count;
18812
18813 }
18814
18815
18816 \f
18817 /***********************************************************************
18818 Displaying strings
18819 ***********************************************************************/
18820
18821 /* Display a NUL-terminated string, starting with index START.
18822
18823 If STRING is non-null, display that C string. Otherwise, the Lisp
18824 string LISP_STRING is displayed.
18825
18826 If FACE_STRING is not nil, FACE_STRING_POS is a position in
18827 FACE_STRING. Display STRING or LISP_STRING with the face at
18828 FACE_STRING_POS in FACE_STRING:
18829
18830 Display the string in the environment given by IT, but use the
18831 standard display table, temporarily.
18832
18833 FIELD_WIDTH is the minimum number of output glyphs to produce.
18834 If STRING has fewer characters than FIELD_WIDTH, pad to the right
18835 with spaces. If STRING has more characters, more than FIELD_WIDTH
18836 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
18837
18838 PRECISION is the maximum number of characters to output from
18839 STRING. PRECISION < 0 means don't truncate the string.
18840
18841 This is roughly equivalent to printf format specifiers:
18842
18843 FIELD_WIDTH PRECISION PRINTF
18844 ----------------------------------------
18845 -1 -1 %s
18846 -1 10 %.10s
18847 10 -1 %10s
18848 20 10 %20.10s
18849
18850 MULTIBYTE zero means do not display multibyte chars, > 0 means do
18851 display them, and < 0 means obey the current buffer's value of
18852 enable_multibyte_characters.
18853
18854 Value is the number of columns displayed. */
18855
18856 static int
18857 display_string (string, lisp_string, face_string, face_string_pos,
18858 start, it, field_width, precision, max_x, multibyte)
18859 unsigned char *string;
18860 Lisp_Object lisp_string;
18861 Lisp_Object face_string;
18862 EMACS_INT face_string_pos;
18863 EMACS_INT start;
18864 struct it *it;
18865 int field_width, precision, max_x;
18866 int multibyte;
18867 {
18868 int hpos_at_start = it->hpos;
18869 int saved_face_id = it->face_id;
18870 struct glyph_row *row = it->glyph_row;
18871
18872 /* Initialize the iterator IT for iteration over STRING beginning
18873 with index START. */
18874 reseat_to_string (it, string, lisp_string, start,
18875 precision, field_width, multibyte);
18876
18877 /* If displaying STRING, set up the face of the iterator
18878 from LISP_STRING, if that's given. */
18879 if (STRINGP (face_string))
18880 {
18881 EMACS_INT endptr;
18882 struct face *face;
18883
18884 it->face_id
18885 = face_at_string_position (it->w, face_string, face_string_pos,
18886 0, it->region_beg_charpos,
18887 it->region_end_charpos,
18888 &endptr, it->base_face_id, 0);
18889 face = FACE_FROM_ID (it->f, it->face_id);
18890 it->face_box_p = face->box != FACE_NO_BOX;
18891 }
18892
18893 /* Set max_x to the maximum allowed X position. Don't let it go
18894 beyond the right edge of the window. */
18895 if (max_x <= 0)
18896 max_x = it->last_visible_x;
18897 else
18898 max_x = min (max_x, it->last_visible_x);
18899
18900 /* Skip over display elements that are not visible. because IT->w is
18901 hscrolled. */
18902 if (it->current_x < it->first_visible_x)
18903 move_it_in_display_line_to (it, 100000, it->first_visible_x,
18904 MOVE_TO_POS | MOVE_TO_X);
18905
18906 row->ascent = it->max_ascent;
18907 row->height = it->max_ascent + it->max_descent;
18908 row->phys_ascent = it->max_phys_ascent;
18909 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
18910 row->extra_line_spacing = it->max_extra_line_spacing;
18911
18912 /* This condition is for the case that we are called with current_x
18913 past last_visible_x. */
18914 while (it->current_x < max_x)
18915 {
18916 int x_before, x, n_glyphs_before, i, nglyphs;
18917
18918 /* Get the next display element. */
18919 if (!get_next_display_element (it))
18920 break;
18921
18922 /* Produce glyphs. */
18923 x_before = it->current_x;
18924 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
18925 PRODUCE_GLYPHS (it);
18926
18927 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
18928 i = 0;
18929 x = x_before;
18930 while (i < nglyphs)
18931 {
18932 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
18933
18934 if (it->line_wrap != TRUNCATE
18935 && x + glyph->pixel_width > max_x)
18936 {
18937 /* End of continued line or max_x reached. */
18938 if (CHAR_GLYPH_PADDING_P (*glyph))
18939 {
18940 /* A wide character is unbreakable. */
18941 it->glyph_row->used[TEXT_AREA] = n_glyphs_before;
18942 it->current_x = x_before;
18943 }
18944 else
18945 {
18946 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
18947 it->current_x = x;
18948 }
18949 break;
18950 }
18951 else if (x + glyph->pixel_width >= it->first_visible_x)
18952 {
18953 /* Glyph is at least partially visible. */
18954 ++it->hpos;
18955 if (x < it->first_visible_x)
18956 it->glyph_row->x = x - it->first_visible_x;
18957 }
18958 else
18959 {
18960 /* Glyph is off the left margin of the display area.
18961 Should not happen. */
18962 abort ();
18963 }
18964
18965 row->ascent = max (row->ascent, it->max_ascent);
18966 row->height = max (row->height, it->max_ascent + it->max_descent);
18967 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
18968 row->phys_height = max (row->phys_height,
18969 it->max_phys_ascent + it->max_phys_descent);
18970 row->extra_line_spacing = max (row->extra_line_spacing,
18971 it->max_extra_line_spacing);
18972 x += glyph->pixel_width;
18973 ++i;
18974 }
18975
18976 /* Stop if max_x reached. */
18977 if (i < nglyphs)
18978 break;
18979
18980 /* Stop at line ends. */
18981 if (ITERATOR_AT_END_OF_LINE_P (it))
18982 {
18983 it->continuation_lines_width = 0;
18984 break;
18985 }
18986
18987 set_iterator_to_next (it, 1);
18988
18989 /* Stop if truncating at the right edge. */
18990 if (it->line_wrap == TRUNCATE
18991 && it->current_x >= it->last_visible_x)
18992 {
18993 /* Add truncation mark, but don't do it if the line is
18994 truncated at a padding space. */
18995 if (IT_CHARPOS (*it) < it->string_nchars)
18996 {
18997 if (!FRAME_WINDOW_P (it->f))
18998 {
18999 int i, n;
19000
19001 if (it->current_x > it->last_visible_x)
19002 {
19003 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
19004 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
19005 break;
19006 for (n = row->used[TEXT_AREA]; i < n; ++i)
19007 {
19008 row->used[TEXT_AREA] = i;
19009 produce_special_glyphs (it, IT_TRUNCATION);
19010 }
19011 }
19012 produce_special_glyphs (it, IT_TRUNCATION);
19013 }
19014 it->glyph_row->truncated_on_right_p = 1;
19015 }
19016 break;
19017 }
19018 }
19019
19020 /* Maybe insert a truncation at the left. */
19021 if (it->first_visible_x
19022 && IT_CHARPOS (*it) > 0)
19023 {
19024 if (!FRAME_WINDOW_P (it->f))
19025 insert_left_trunc_glyphs (it);
19026 it->glyph_row->truncated_on_left_p = 1;
19027 }
19028
19029 it->face_id = saved_face_id;
19030
19031 /* Value is number of columns displayed. */
19032 return it->hpos - hpos_at_start;
19033 }
19034
19035
19036 \f
19037 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
19038 appears as an element of LIST or as the car of an element of LIST.
19039 If PROPVAL is a list, compare each element against LIST in that
19040 way, and return 1/2 if any element of PROPVAL is found in LIST.
19041 Otherwise return 0. This function cannot quit.
19042 The return value is 2 if the text is invisible but with an ellipsis
19043 and 1 if it's invisible and without an ellipsis. */
19044
19045 int
19046 invisible_p (propval, list)
19047 register Lisp_Object propval;
19048 Lisp_Object list;
19049 {
19050 register Lisp_Object tail, proptail;
19051
19052 for (tail = list; CONSP (tail); tail = XCDR (tail))
19053 {
19054 register Lisp_Object tem;
19055 tem = XCAR (tail);
19056 if (EQ (propval, tem))
19057 return 1;
19058 if (CONSP (tem) && EQ (propval, XCAR (tem)))
19059 return NILP (XCDR (tem)) ? 1 : 2;
19060 }
19061
19062 if (CONSP (propval))
19063 {
19064 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
19065 {
19066 Lisp_Object propelt;
19067 propelt = XCAR (proptail);
19068 for (tail = list; CONSP (tail); tail = XCDR (tail))
19069 {
19070 register Lisp_Object tem;
19071 tem = XCAR (tail);
19072 if (EQ (propelt, tem))
19073 return 1;
19074 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
19075 return NILP (XCDR (tem)) ? 1 : 2;
19076 }
19077 }
19078 }
19079
19080 return 0;
19081 }
19082
19083 DEFUN ("invisible-p", Finvisible_p, Sinvisible_p, 1, 1, 0,
19084 doc: /* Non-nil if the property makes the text invisible.
19085 POS-OR-PROP can be a marker or number, in which case it is taken to be
19086 a position in the current buffer and the value of the `invisible' property
19087 is checked; or it can be some other value, which is then presumed to be the
19088 value of the `invisible' property of the text of interest.
19089 The non-nil value returned can be t for truly invisible text or something
19090 else if the text is replaced by an ellipsis. */)
19091 (pos_or_prop)
19092 Lisp_Object pos_or_prop;
19093 {
19094 Lisp_Object prop
19095 = (NATNUMP (pos_or_prop) || MARKERP (pos_or_prop)
19096 ? Fget_char_property (pos_or_prop, Qinvisible, Qnil)
19097 : pos_or_prop);
19098 int invis = TEXT_PROP_MEANS_INVISIBLE (prop);
19099 return (invis == 0 ? Qnil
19100 : invis == 1 ? Qt
19101 : make_number (invis));
19102 }
19103
19104 /* Calculate a width or height in pixels from a specification using
19105 the following elements:
19106
19107 SPEC ::=
19108 NUM - a (fractional) multiple of the default font width/height
19109 (NUM) - specifies exactly NUM pixels
19110 UNIT - a fixed number of pixels, see below.
19111 ELEMENT - size of a display element in pixels, see below.
19112 (NUM . SPEC) - equals NUM * SPEC
19113 (+ SPEC SPEC ...) - add pixel values
19114 (- SPEC SPEC ...) - subtract pixel values
19115 (- SPEC) - negate pixel value
19116
19117 NUM ::=
19118 INT or FLOAT - a number constant
19119 SYMBOL - use symbol's (buffer local) variable binding.
19120
19121 UNIT ::=
19122 in - pixels per inch *)
19123 mm - pixels per 1/1000 meter *)
19124 cm - pixels per 1/100 meter *)
19125 width - width of current font in pixels.
19126 height - height of current font in pixels.
19127
19128 *) using the ratio(s) defined in display-pixels-per-inch.
19129
19130 ELEMENT ::=
19131
19132 left-fringe - left fringe width in pixels
19133 right-fringe - right fringe width in pixels
19134
19135 left-margin - left margin width in pixels
19136 right-margin - right margin width in pixels
19137
19138 scroll-bar - scroll-bar area width in pixels
19139
19140 Examples:
19141
19142 Pixels corresponding to 5 inches:
19143 (5 . in)
19144
19145 Total width of non-text areas on left side of window (if scroll-bar is on left):
19146 '(space :width (+ left-fringe left-margin scroll-bar))
19147
19148 Align to first text column (in header line):
19149 '(space :align-to 0)
19150
19151 Align to middle of text area minus half the width of variable `my-image'
19152 containing a loaded image:
19153 '(space :align-to (0.5 . (- text my-image)))
19154
19155 Width of left margin minus width of 1 character in the default font:
19156 '(space :width (- left-margin 1))
19157
19158 Width of left margin minus width of 2 characters in the current font:
19159 '(space :width (- left-margin (2 . width)))
19160
19161 Center 1 character over left-margin (in header line):
19162 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
19163
19164 Different ways to express width of left fringe plus left margin minus one pixel:
19165 '(space :width (- (+ left-fringe left-margin) (1)))
19166 '(space :width (+ left-fringe left-margin (- (1))))
19167 '(space :width (+ left-fringe left-margin (-1)))
19168
19169 */
19170
19171 #define NUMVAL(X) \
19172 ((INTEGERP (X) || FLOATP (X)) \
19173 ? XFLOATINT (X) \
19174 : - 1)
19175
19176 int
19177 calc_pixel_width_or_height (res, it, prop, font, width_p, align_to)
19178 double *res;
19179 struct it *it;
19180 Lisp_Object prop;
19181 struct font *font;
19182 int width_p, *align_to;
19183 {
19184 double pixels;
19185
19186 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
19187 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
19188
19189 if (NILP (prop))
19190 return OK_PIXELS (0);
19191
19192 xassert (FRAME_LIVE_P (it->f));
19193
19194 if (SYMBOLP (prop))
19195 {
19196 if (SCHARS (SYMBOL_NAME (prop)) == 2)
19197 {
19198 char *unit = SDATA (SYMBOL_NAME (prop));
19199
19200 if (unit[0] == 'i' && unit[1] == 'n')
19201 pixels = 1.0;
19202 else if (unit[0] == 'm' && unit[1] == 'm')
19203 pixels = 25.4;
19204 else if (unit[0] == 'c' && unit[1] == 'm')
19205 pixels = 2.54;
19206 else
19207 pixels = 0;
19208 if (pixels > 0)
19209 {
19210 double ppi;
19211 #ifdef HAVE_WINDOW_SYSTEM
19212 if (FRAME_WINDOW_P (it->f)
19213 && (ppi = (width_p
19214 ? FRAME_X_DISPLAY_INFO (it->f)->resx
19215 : FRAME_X_DISPLAY_INFO (it->f)->resy),
19216 ppi > 0))
19217 return OK_PIXELS (ppi / pixels);
19218 #endif
19219
19220 if ((ppi = NUMVAL (Vdisplay_pixels_per_inch), ppi > 0)
19221 || (CONSP (Vdisplay_pixels_per_inch)
19222 && (ppi = (width_p
19223 ? NUMVAL (XCAR (Vdisplay_pixels_per_inch))
19224 : NUMVAL (XCDR (Vdisplay_pixels_per_inch))),
19225 ppi > 0)))
19226 return OK_PIXELS (ppi / pixels);
19227
19228 return 0;
19229 }
19230 }
19231
19232 #ifdef HAVE_WINDOW_SYSTEM
19233 if (EQ (prop, Qheight))
19234 return OK_PIXELS (font ? FONT_HEIGHT (font) : FRAME_LINE_HEIGHT (it->f));
19235 if (EQ (prop, Qwidth))
19236 return OK_PIXELS (font ? FONT_WIDTH (font) : FRAME_COLUMN_WIDTH (it->f));
19237 #else
19238 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
19239 return OK_PIXELS (1);
19240 #endif
19241
19242 if (EQ (prop, Qtext))
19243 return OK_PIXELS (width_p
19244 ? window_box_width (it->w, TEXT_AREA)
19245 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
19246
19247 if (align_to && *align_to < 0)
19248 {
19249 *res = 0;
19250 if (EQ (prop, Qleft))
19251 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
19252 if (EQ (prop, Qright))
19253 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
19254 if (EQ (prop, Qcenter))
19255 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
19256 + window_box_width (it->w, TEXT_AREA) / 2);
19257 if (EQ (prop, Qleft_fringe))
19258 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
19259 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
19260 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
19261 if (EQ (prop, Qright_fringe))
19262 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
19263 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
19264 : window_box_right_offset (it->w, TEXT_AREA));
19265 if (EQ (prop, Qleft_margin))
19266 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
19267 if (EQ (prop, Qright_margin))
19268 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
19269 if (EQ (prop, Qscroll_bar))
19270 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
19271 ? 0
19272 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
19273 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
19274 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
19275 : 0)));
19276 }
19277 else
19278 {
19279 if (EQ (prop, Qleft_fringe))
19280 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
19281 if (EQ (prop, Qright_fringe))
19282 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
19283 if (EQ (prop, Qleft_margin))
19284 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
19285 if (EQ (prop, Qright_margin))
19286 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
19287 if (EQ (prop, Qscroll_bar))
19288 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
19289 }
19290
19291 prop = Fbuffer_local_value (prop, it->w->buffer);
19292 }
19293
19294 if (INTEGERP (prop) || FLOATP (prop))
19295 {
19296 int base_unit = (width_p
19297 ? FRAME_COLUMN_WIDTH (it->f)
19298 : FRAME_LINE_HEIGHT (it->f));
19299 return OK_PIXELS (XFLOATINT (prop) * base_unit);
19300 }
19301
19302 if (CONSP (prop))
19303 {
19304 Lisp_Object car = XCAR (prop);
19305 Lisp_Object cdr = XCDR (prop);
19306
19307 if (SYMBOLP (car))
19308 {
19309 #ifdef HAVE_WINDOW_SYSTEM
19310 if (FRAME_WINDOW_P (it->f)
19311 && valid_image_p (prop))
19312 {
19313 int id = lookup_image (it->f, prop);
19314 struct image *img = IMAGE_FROM_ID (it->f, id);
19315
19316 return OK_PIXELS (width_p ? img->width : img->height);
19317 }
19318 #endif
19319 if (EQ (car, Qplus) || EQ (car, Qminus))
19320 {
19321 int first = 1;
19322 double px;
19323
19324 pixels = 0;
19325 while (CONSP (cdr))
19326 {
19327 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
19328 font, width_p, align_to))
19329 return 0;
19330 if (first)
19331 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
19332 else
19333 pixels += px;
19334 cdr = XCDR (cdr);
19335 }
19336 if (EQ (car, Qminus))
19337 pixels = -pixels;
19338 return OK_PIXELS (pixels);
19339 }
19340
19341 car = Fbuffer_local_value (car, it->w->buffer);
19342 }
19343
19344 if (INTEGERP (car) || FLOATP (car))
19345 {
19346 double fact;
19347 pixels = XFLOATINT (car);
19348 if (NILP (cdr))
19349 return OK_PIXELS (pixels);
19350 if (calc_pixel_width_or_height (&fact, it, cdr,
19351 font, width_p, align_to))
19352 return OK_PIXELS (pixels * fact);
19353 return 0;
19354 }
19355
19356 return 0;
19357 }
19358
19359 return 0;
19360 }
19361
19362 \f
19363 /***********************************************************************
19364 Glyph Display
19365 ***********************************************************************/
19366
19367 #ifdef HAVE_WINDOW_SYSTEM
19368
19369 #if GLYPH_DEBUG
19370
19371 void
19372 dump_glyph_string (s)
19373 struct glyph_string *s;
19374 {
19375 fprintf (stderr, "glyph string\n");
19376 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
19377 s->x, s->y, s->width, s->height);
19378 fprintf (stderr, " ybase = %d\n", s->ybase);
19379 fprintf (stderr, " hl = %d\n", s->hl);
19380 fprintf (stderr, " left overhang = %d, right = %d\n",
19381 s->left_overhang, s->right_overhang);
19382 fprintf (stderr, " nchars = %d\n", s->nchars);
19383 fprintf (stderr, " extends to end of line = %d\n",
19384 s->extends_to_end_of_line_p);
19385 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
19386 fprintf (stderr, " bg width = %d\n", s->background_width);
19387 }
19388
19389 #endif /* GLYPH_DEBUG */
19390
19391 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
19392 of XChar2b structures for S; it can't be allocated in
19393 init_glyph_string because it must be allocated via `alloca'. W
19394 is the window on which S is drawn. ROW and AREA are the glyph row
19395 and area within the row from which S is constructed. START is the
19396 index of the first glyph structure covered by S. HL is a
19397 face-override for drawing S. */
19398
19399 #ifdef HAVE_NTGUI
19400 #define OPTIONAL_HDC(hdc) hdc,
19401 #define DECLARE_HDC(hdc) HDC hdc;
19402 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
19403 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
19404 #endif
19405
19406 #ifndef OPTIONAL_HDC
19407 #define OPTIONAL_HDC(hdc)
19408 #define DECLARE_HDC(hdc)
19409 #define ALLOCATE_HDC(hdc, f)
19410 #define RELEASE_HDC(hdc, f)
19411 #endif
19412
19413 static void
19414 init_glyph_string (s, OPTIONAL_HDC (hdc) char2b, w, row, area, start, hl)
19415 struct glyph_string *s;
19416 DECLARE_HDC (hdc)
19417 XChar2b *char2b;
19418 struct window *w;
19419 struct glyph_row *row;
19420 enum glyph_row_area area;
19421 int start;
19422 enum draw_glyphs_face hl;
19423 {
19424 bzero (s, sizeof *s);
19425 s->w = w;
19426 s->f = XFRAME (w->frame);
19427 #ifdef HAVE_NTGUI
19428 s->hdc = hdc;
19429 #endif
19430 s->display = FRAME_X_DISPLAY (s->f);
19431 s->window = FRAME_X_WINDOW (s->f);
19432 s->char2b = char2b;
19433 s->hl = hl;
19434 s->row = row;
19435 s->area = area;
19436 s->first_glyph = row->glyphs[area] + start;
19437 s->height = row->height;
19438 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
19439
19440 /* Display the internal border below the tool-bar window. */
19441 if (WINDOWP (s->f->tool_bar_window)
19442 && s->w == XWINDOW (s->f->tool_bar_window))
19443 s->y -= FRAME_INTERNAL_BORDER_WIDTH (s->f);
19444
19445 s->ybase = s->y + row->ascent;
19446 }
19447
19448
19449 /* Append the list of glyph strings with head H and tail T to the list
19450 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
19451
19452 static INLINE void
19453 append_glyph_string_lists (head, tail, h, t)
19454 struct glyph_string **head, **tail;
19455 struct glyph_string *h, *t;
19456 {
19457 if (h)
19458 {
19459 if (*head)
19460 (*tail)->next = h;
19461 else
19462 *head = h;
19463 h->prev = *tail;
19464 *tail = t;
19465 }
19466 }
19467
19468
19469 /* Prepend the list of glyph strings with head H and tail T to the
19470 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
19471 result. */
19472
19473 static INLINE void
19474 prepend_glyph_string_lists (head, tail, h, t)
19475 struct glyph_string **head, **tail;
19476 struct glyph_string *h, *t;
19477 {
19478 if (h)
19479 {
19480 if (*head)
19481 (*head)->prev = t;
19482 else
19483 *tail = t;
19484 t->next = *head;
19485 *head = h;
19486 }
19487 }
19488
19489
19490 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
19491 Set *HEAD and *TAIL to the resulting list. */
19492
19493 static INLINE void
19494 append_glyph_string (head, tail, s)
19495 struct glyph_string **head, **tail;
19496 struct glyph_string *s;
19497 {
19498 s->next = s->prev = NULL;
19499 append_glyph_string_lists (head, tail, s, s);
19500 }
19501
19502
19503 /* Get face and two-byte form of character C in face FACE_ID on frame
19504 F. The encoding of C is returned in *CHAR2B. MULTIBYTE_P non-zero
19505 means we want to display multibyte text. DISPLAY_P non-zero means
19506 make sure that X resources for the face returned are allocated.
19507 Value is a pointer to a realized face that is ready for display if
19508 DISPLAY_P is non-zero. */
19509
19510 static INLINE struct face *
19511 get_char_face_and_encoding (f, c, face_id, char2b, multibyte_p, display_p)
19512 struct frame *f;
19513 int c, face_id;
19514 XChar2b *char2b;
19515 int multibyte_p, display_p;
19516 {
19517 struct face *face = FACE_FROM_ID (f, face_id);
19518
19519 if (face->font)
19520 {
19521 unsigned code = face->font->driver->encode_char (face->font, c);
19522
19523 if (code != FONT_INVALID_CODE)
19524 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
19525 else
19526 STORE_XCHAR2B (char2b, 0, 0);
19527 }
19528
19529 /* Make sure X resources of the face are allocated. */
19530 #ifdef HAVE_X_WINDOWS
19531 if (display_p)
19532 #endif
19533 {
19534 xassert (face != NULL);
19535 PREPARE_FACE_FOR_DISPLAY (f, face);
19536 }
19537
19538 return face;
19539 }
19540
19541
19542 /* Get face and two-byte form of character glyph GLYPH on frame F.
19543 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
19544 a pointer to a realized face that is ready for display. */
19545
19546 static INLINE struct face *
19547 get_glyph_face_and_encoding (f, glyph, char2b, two_byte_p)
19548 struct frame *f;
19549 struct glyph *glyph;
19550 XChar2b *char2b;
19551 int *two_byte_p;
19552 {
19553 struct face *face;
19554
19555 xassert (glyph->type == CHAR_GLYPH);
19556 face = FACE_FROM_ID (f, glyph->face_id);
19557
19558 if (two_byte_p)
19559 *two_byte_p = 0;
19560
19561 if (face->font)
19562 {
19563 unsigned code = face->font->driver->encode_char (face->font, glyph->u.ch);
19564
19565 if (code != FONT_INVALID_CODE)
19566 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
19567 else
19568 STORE_XCHAR2B (char2b, 0, 0);
19569 }
19570
19571 /* Make sure X resources of the face are allocated. */
19572 xassert (face != NULL);
19573 PREPARE_FACE_FOR_DISPLAY (f, face);
19574 return face;
19575 }
19576
19577
19578 /* Fill glyph string S with composition components specified by S->cmp.
19579
19580 BASE_FACE is the base face of the composition.
19581 S->cmp_from is the index of the first component for S.
19582
19583 OVERLAPS non-zero means S should draw the foreground only, and use
19584 its physical height for clipping. See also draw_glyphs.
19585
19586 Value is the index of a component not in S. */
19587
19588 static int
19589 fill_composite_glyph_string (s, base_face, overlaps)
19590 struct glyph_string *s;
19591 struct face *base_face;
19592 int overlaps;
19593 {
19594 int i;
19595 /* For all glyphs of this composition, starting at the offset
19596 S->cmp_from, until we reach the end of the definition or encounter a
19597 glyph that requires the different face, add it to S. */
19598 struct face *face;
19599
19600 xassert (s);
19601
19602 s->for_overlaps = overlaps;
19603 s->face = NULL;
19604 s->font = NULL;
19605 for (i = s->cmp_from; i < s->cmp->glyph_len; i++)
19606 {
19607 int c = COMPOSITION_GLYPH (s->cmp, i);
19608
19609 if (c != '\t')
19610 {
19611 int face_id = FACE_FOR_CHAR (s->f, base_face->ascii_face, c,
19612 -1, Qnil);
19613
19614 face = get_char_face_and_encoding (s->f, c, face_id,
19615 s->char2b + i, 1, 1);
19616 if (face)
19617 {
19618 if (! s->face)
19619 {
19620 s->face = face;
19621 s->font = s->face->font;
19622 }
19623 else if (s->face != face)
19624 break;
19625 }
19626 }
19627 ++s->nchars;
19628 }
19629 s->cmp_to = i;
19630
19631 /* All glyph strings for the same composition has the same width,
19632 i.e. the width set for the first component of the composition. */
19633 s->width = s->first_glyph->pixel_width;
19634
19635 /* If the specified font could not be loaded, use the frame's
19636 default font, but record the fact that we couldn't load it in
19637 the glyph string so that we can draw rectangles for the
19638 characters of the glyph string. */
19639 if (s->font == NULL)
19640 {
19641 s->font_not_found_p = 1;
19642 s->font = FRAME_FONT (s->f);
19643 }
19644
19645 /* Adjust base line for subscript/superscript text. */
19646 s->ybase += s->first_glyph->voffset;
19647
19648 /* This glyph string must always be drawn with 16-bit functions. */
19649 s->two_byte_p = 1;
19650
19651 return s->cmp_to;
19652 }
19653
19654 static int
19655 fill_gstring_glyph_string (s, face_id, start, end, overlaps)
19656 struct glyph_string *s;
19657 int face_id;
19658 int start, end, overlaps;
19659 {
19660 struct glyph *glyph, *last;
19661 Lisp_Object lgstring;
19662 int i;
19663
19664 s->for_overlaps = overlaps;
19665 glyph = s->row->glyphs[s->area] + start;
19666 last = s->row->glyphs[s->area] + end;
19667 s->cmp_id = glyph->u.cmp.id;
19668 s->cmp_from = glyph->u.cmp.from;
19669 s->cmp_to = glyph->u.cmp.to;
19670 s->face = FACE_FROM_ID (s->f, face_id);
19671 lgstring = composition_gstring_from_id (s->cmp_id);
19672 s->font = XFONT_OBJECT (LGSTRING_FONT (lgstring));
19673 glyph++;
19674 while (glyph < last
19675 && glyph->u.cmp.automatic
19676 && glyph->u.cmp.id == s->cmp_id)
19677 s->cmp_to = (glyph++)->u.cmp.to;
19678
19679 for (i = s->cmp_from; i < s->cmp_to; i++)
19680 {
19681 Lisp_Object lglyph = LGSTRING_GLYPH (lgstring, i);
19682 unsigned code = LGLYPH_CODE (lglyph);
19683
19684 STORE_XCHAR2B ((s->char2b + i), code >> 8, code & 0xFF);
19685 }
19686 s->width = composition_gstring_width (lgstring, s->cmp_from, s->cmp_to, NULL);
19687 return glyph - s->row->glyphs[s->area];
19688 }
19689
19690
19691 /* Fill glyph string S from a sequence of character glyphs.
19692
19693 FACE_ID is the face id of the string. START is the index of the
19694 first glyph to consider, END is the index of the last + 1.
19695 OVERLAPS non-zero means S should draw the foreground only, and use
19696 its physical height for clipping. See also draw_glyphs.
19697
19698 Value is the index of the first glyph not in S. */
19699
19700 static int
19701 fill_glyph_string (s, face_id, start, end, overlaps)
19702 struct glyph_string *s;
19703 int face_id;
19704 int start, end, overlaps;
19705 {
19706 struct glyph *glyph, *last;
19707 int voffset;
19708 int glyph_not_available_p;
19709
19710 xassert (s->f == XFRAME (s->w->frame));
19711 xassert (s->nchars == 0);
19712 xassert (start >= 0 && end > start);
19713
19714 s->for_overlaps = overlaps;
19715 glyph = s->row->glyphs[s->area] + start;
19716 last = s->row->glyphs[s->area] + end;
19717 voffset = glyph->voffset;
19718 s->padding_p = glyph->padding_p;
19719 glyph_not_available_p = glyph->glyph_not_available_p;
19720
19721 while (glyph < last
19722 && glyph->type == CHAR_GLYPH
19723 && glyph->voffset == voffset
19724 /* Same face id implies same font, nowadays. */
19725 && glyph->face_id == face_id
19726 && glyph->glyph_not_available_p == glyph_not_available_p)
19727 {
19728 int two_byte_p;
19729
19730 s->face = get_glyph_face_and_encoding (s->f, glyph,
19731 s->char2b + s->nchars,
19732 &two_byte_p);
19733 s->two_byte_p = two_byte_p;
19734 ++s->nchars;
19735 xassert (s->nchars <= end - start);
19736 s->width += glyph->pixel_width;
19737 if (glyph++->padding_p != s->padding_p)
19738 break;
19739 }
19740
19741 s->font = s->face->font;
19742
19743 /* If the specified font could not be loaded, use the frame's font,
19744 but record the fact that we couldn't load it in
19745 S->font_not_found_p so that we can draw rectangles for the
19746 characters of the glyph string. */
19747 if (s->font == NULL || glyph_not_available_p)
19748 {
19749 s->font_not_found_p = 1;
19750 s->font = FRAME_FONT (s->f);
19751 }
19752
19753 /* Adjust base line for subscript/superscript text. */
19754 s->ybase += voffset;
19755
19756 xassert (s->face && s->face->gc);
19757 return glyph - s->row->glyphs[s->area];
19758 }
19759
19760
19761 /* Fill glyph string S from image glyph S->first_glyph. */
19762
19763 static void
19764 fill_image_glyph_string (s)
19765 struct glyph_string *s;
19766 {
19767 xassert (s->first_glyph->type == IMAGE_GLYPH);
19768 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
19769 xassert (s->img);
19770 s->slice = s->first_glyph->slice;
19771 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
19772 s->font = s->face->font;
19773 s->width = s->first_glyph->pixel_width;
19774
19775 /* Adjust base line for subscript/superscript text. */
19776 s->ybase += s->first_glyph->voffset;
19777 }
19778
19779
19780 /* Fill glyph string S from a sequence of stretch glyphs.
19781
19782 ROW is the glyph row in which the glyphs are found, AREA is the
19783 area within the row. START is the index of the first glyph to
19784 consider, END is the index of the last + 1.
19785
19786 Value is the index of the first glyph not in S. */
19787
19788 static int
19789 fill_stretch_glyph_string (s, row, area, start, end)
19790 struct glyph_string *s;
19791 struct glyph_row *row;
19792 enum glyph_row_area area;
19793 int start, end;
19794 {
19795 struct glyph *glyph, *last;
19796 int voffset, face_id;
19797
19798 xassert (s->first_glyph->type == STRETCH_GLYPH);
19799
19800 glyph = s->row->glyphs[s->area] + start;
19801 last = s->row->glyphs[s->area] + end;
19802 face_id = glyph->face_id;
19803 s->face = FACE_FROM_ID (s->f, face_id);
19804 s->font = s->face->font;
19805 s->width = glyph->pixel_width;
19806 s->nchars = 1;
19807 voffset = glyph->voffset;
19808
19809 for (++glyph;
19810 (glyph < last
19811 && glyph->type == STRETCH_GLYPH
19812 && glyph->voffset == voffset
19813 && glyph->face_id == face_id);
19814 ++glyph)
19815 s->width += glyph->pixel_width;
19816
19817 /* Adjust base line for subscript/superscript text. */
19818 s->ybase += voffset;
19819
19820 /* The case that face->gc == 0 is handled when drawing the glyph
19821 string by calling PREPARE_FACE_FOR_DISPLAY. */
19822 xassert (s->face);
19823 return glyph - s->row->glyphs[s->area];
19824 }
19825
19826 static struct font_metrics *
19827 get_per_char_metric (f, font, char2b)
19828 struct frame *f;
19829 struct font *font;
19830 XChar2b *char2b;
19831 {
19832 static struct font_metrics metrics;
19833 unsigned code = (XCHAR2B_BYTE1 (char2b) << 8) | XCHAR2B_BYTE2 (char2b);
19834
19835 if (! font || code == FONT_INVALID_CODE)
19836 return NULL;
19837 font->driver->text_extents (font, &code, 1, &metrics);
19838 return &metrics;
19839 }
19840
19841 /* EXPORT for RIF:
19842 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
19843 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
19844 assumed to be zero. */
19845
19846 void
19847 x_get_glyph_overhangs (glyph, f, left, right)
19848 struct glyph *glyph;
19849 struct frame *f;
19850 int *left, *right;
19851 {
19852 *left = *right = 0;
19853
19854 if (glyph->type == CHAR_GLYPH)
19855 {
19856 struct face *face;
19857 XChar2b char2b;
19858 struct font_metrics *pcm;
19859
19860 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
19861 if (face->font && (pcm = get_per_char_metric (f, face->font, &char2b)))
19862 {
19863 if (pcm->rbearing > pcm->width)
19864 *right = pcm->rbearing - pcm->width;
19865 if (pcm->lbearing < 0)
19866 *left = -pcm->lbearing;
19867 }
19868 }
19869 else if (glyph->type == COMPOSITE_GLYPH)
19870 {
19871 if (! glyph->u.cmp.automatic)
19872 {
19873 struct composition *cmp = composition_table[glyph->u.cmp.id];
19874
19875 if (cmp->rbearing - cmp->pixel_width)
19876 *right = cmp->rbearing - cmp->pixel_width;
19877 if (cmp->lbearing < 0);
19878 *left = - cmp->lbearing;
19879 }
19880 else
19881 {
19882 Lisp_Object gstring = composition_gstring_from_id (glyph->u.cmp.id);
19883 struct font_metrics metrics;
19884
19885 composition_gstring_width (gstring, glyph->u.cmp.from,
19886 glyph->u.cmp.to, &metrics);
19887 if (metrics.rbearing > metrics.width)
19888 *right = metrics.rbearing;
19889 if (metrics.lbearing < 0)
19890 *left = - metrics.lbearing;
19891 }
19892 }
19893 }
19894
19895
19896 /* Return the index of the first glyph preceding glyph string S that
19897 is overwritten by S because of S's left overhang. Value is -1
19898 if no glyphs are overwritten. */
19899
19900 static int
19901 left_overwritten (s)
19902 struct glyph_string *s;
19903 {
19904 int k;
19905
19906 if (s->left_overhang)
19907 {
19908 int x = 0, i;
19909 struct glyph *glyphs = s->row->glyphs[s->area];
19910 int first = s->first_glyph - glyphs;
19911
19912 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
19913 x -= glyphs[i].pixel_width;
19914
19915 k = i + 1;
19916 }
19917 else
19918 k = -1;
19919
19920 return k;
19921 }
19922
19923
19924 /* Return the index of the first glyph preceding glyph string S that
19925 is overwriting S because of its right overhang. Value is -1 if no
19926 glyph in front of S overwrites S. */
19927
19928 static int
19929 left_overwriting (s)
19930 struct glyph_string *s;
19931 {
19932 int i, k, x;
19933 struct glyph *glyphs = s->row->glyphs[s->area];
19934 int first = s->first_glyph - glyphs;
19935
19936 k = -1;
19937 x = 0;
19938 for (i = first - 1; i >= 0; --i)
19939 {
19940 int left, right;
19941 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
19942 if (x + right > 0)
19943 k = i;
19944 x -= glyphs[i].pixel_width;
19945 }
19946
19947 return k;
19948 }
19949
19950
19951 /* Return the index of the last glyph following glyph string S that is
19952 overwritten by S because of S's right overhang. Value is -1 if
19953 no such glyph is found. */
19954
19955 static int
19956 right_overwritten (s)
19957 struct glyph_string *s;
19958 {
19959 int k = -1;
19960
19961 if (s->right_overhang)
19962 {
19963 int x = 0, i;
19964 struct glyph *glyphs = s->row->glyphs[s->area];
19965 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
19966 int end = s->row->used[s->area];
19967
19968 for (i = first; i < end && s->right_overhang > x; ++i)
19969 x += glyphs[i].pixel_width;
19970
19971 k = i;
19972 }
19973
19974 return k;
19975 }
19976
19977
19978 /* Return the index of the last glyph following glyph string S that
19979 overwrites S because of its left overhang. Value is negative
19980 if no such glyph is found. */
19981
19982 static int
19983 right_overwriting (s)
19984 struct glyph_string *s;
19985 {
19986 int i, k, x;
19987 int end = s->row->used[s->area];
19988 struct glyph *glyphs = s->row->glyphs[s->area];
19989 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
19990
19991 k = -1;
19992 x = 0;
19993 for (i = first; i < end; ++i)
19994 {
19995 int left, right;
19996 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
19997 if (x - left < 0)
19998 k = i;
19999 x += glyphs[i].pixel_width;
20000 }
20001
20002 return k;
20003 }
20004
20005
20006 /* Set background width of glyph string S. START is the index of the
20007 first glyph following S. LAST_X is the right-most x-position + 1
20008 in the drawing area. */
20009
20010 static INLINE void
20011 set_glyph_string_background_width (s, start, last_x)
20012 struct glyph_string *s;
20013 int start;
20014 int last_x;
20015 {
20016 /* If the face of this glyph string has to be drawn to the end of
20017 the drawing area, set S->extends_to_end_of_line_p. */
20018
20019 if (start == s->row->used[s->area]
20020 && s->area == TEXT_AREA
20021 && ((s->row->fill_line_p
20022 && (s->hl == DRAW_NORMAL_TEXT
20023 || s->hl == DRAW_IMAGE_RAISED
20024 || s->hl == DRAW_IMAGE_SUNKEN))
20025 || s->hl == DRAW_MOUSE_FACE))
20026 s->extends_to_end_of_line_p = 1;
20027
20028 /* If S extends its face to the end of the line, set its
20029 background_width to the distance to the right edge of the drawing
20030 area. */
20031 if (s->extends_to_end_of_line_p)
20032 s->background_width = last_x - s->x + 1;
20033 else
20034 s->background_width = s->width;
20035 }
20036
20037
20038 /* Compute overhangs and x-positions for glyph string S and its
20039 predecessors, or successors. X is the starting x-position for S.
20040 BACKWARD_P non-zero means process predecessors. */
20041
20042 static void
20043 compute_overhangs_and_x (s, x, backward_p)
20044 struct glyph_string *s;
20045 int x;
20046 int backward_p;
20047 {
20048 if (backward_p)
20049 {
20050 while (s)
20051 {
20052 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
20053 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
20054 x -= s->width;
20055 s->x = x;
20056 s = s->prev;
20057 }
20058 }
20059 else
20060 {
20061 while (s)
20062 {
20063 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
20064 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
20065 s->x = x;
20066 x += s->width;
20067 s = s->next;
20068 }
20069 }
20070 }
20071
20072
20073
20074 /* The following macros are only called from draw_glyphs below.
20075 They reference the following parameters of that function directly:
20076 `w', `row', `area', and `overlap_p'
20077 as well as the following local variables:
20078 `s', `f', and `hdc' (in W32) */
20079
20080 #ifdef HAVE_NTGUI
20081 /* On W32, silently add local `hdc' variable to argument list of
20082 init_glyph_string. */
20083 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
20084 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
20085 #else
20086 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
20087 init_glyph_string (s, char2b, w, row, area, start, hl)
20088 #endif
20089
20090 /* Add a glyph string for a stretch glyph to the list of strings
20091 between HEAD and TAIL. START is the index of the stretch glyph in
20092 row area AREA of glyph row ROW. END is the index of the last glyph
20093 in that glyph row area. X is the current output position assigned
20094 to the new glyph string constructed. HL overrides that face of the
20095 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
20096 is the right-most x-position of the drawing area. */
20097
20098 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
20099 and below -- keep them on one line. */
20100 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
20101 do \
20102 { \
20103 s = (struct glyph_string *) alloca (sizeof *s); \
20104 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
20105 START = fill_stretch_glyph_string (s, row, area, START, END); \
20106 append_glyph_string (&HEAD, &TAIL, s); \
20107 s->x = (X); \
20108 } \
20109 while (0)
20110
20111
20112 /* Add a glyph string for an image glyph to the list of strings
20113 between HEAD and TAIL. START is the index of the image glyph in
20114 row area AREA of glyph row ROW. END is the index of the last glyph
20115 in that glyph row area. X is the current output position assigned
20116 to the new glyph string constructed. HL overrides that face of the
20117 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
20118 is the right-most x-position of the drawing area. */
20119
20120 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
20121 do \
20122 { \
20123 s = (struct glyph_string *) alloca (sizeof *s); \
20124 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
20125 fill_image_glyph_string (s); \
20126 append_glyph_string (&HEAD, &TAIL, s); \
20127 ++START; \
20128 s->x = (X); \
20129 } \
20130 while (0)
20131
20132
20133 /* Add a glyph string for a sequence of character glyphs to the list
20134 of strings between HEAD and TAIL. START is the index of the first
20135 glyph in row area AREA of glyph row ROW that is part of the new
20136 glyph string. END is the index of the last glyph in that glyph row
20137 area. X is the current output position assigned to the new glyph
20138 string constructed. HL overrides that face of the glyph; e.g. it
20139 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
20140 right-most x-position of the drawing area. */
20141
20142 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
20143 do \
20144 { \
20145 int face_id; \
20146 XChar2b *char2b; \
20147 \
20148 face_id = (row)->glyphs[area][START].face_id; \
20149 \
20150 s = (struct glyph_string *) alloca (sizeof *s); \
20151 char2b = (XChar2b *) alloca ((END - START) * sizeof *char2b); \
20152 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
20153 append_glyph_string (&HEAD, &TAIL, s); \
20154 s->x = (X); \
20155 START = fill_glyph_string (s, face_id, START, END, overlaps); \
20156 } \
20157 while (0)
20158
20159
20160 /* Add a glyph string for a composite sequence to the list of strings
20161 between HEAD and TAIL. START is the index of the first glyph in
20162 row area AREA of glyph row ROW that is part of the new glyph
20163 string. END is the index of the last glyph in that glyph row area.
20164 X is the current output position assigned to the new glyph string
20165 constructed. HL overrides that face of the glyph; e.g. it is
20166 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
20167 x-position of the drawing area. */
20168
20169 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
20170 do { \
20171 int face_id = (row)->glyphs[area][START].face_id; \
20172 struct face *base_face = FACE_FROM_ID (f, face_id); \
20173 int cmp_id = (row)->glyphs[area][START].u.cmp.id; \
20174 struct composition *cmp = composition_table[cmp_id]; \
20175 XChar2b *char2b; \
20176 struct glyph_string *first_s; \
20177 int n; \
20178 \
20179 char2b = (XChar2b *) alloca ((sizeof *char2b) * cmp->glyph_len); \
20180 \
20181 /* Make glyph_strings for each glyph sequence that is drawable by \
20182 the same face, and append them to HEAD/TAIL. */ \
20183 for (n = 0; n < cmp->glyph_len;) \
20184 { \
20185 s = (struct glyph_string *) alloca (sizeof *s); \
20186 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
20187 append_glyph_string (&(HEAD), &(TAIL), s); \
20188 s->cmp = cmp; \
20189 s->cmp_from = n; \
20190 s->x = (X); \
20191 if (n == 0) \
20192 first_s = s; \
20193 n = fill_composite_glyph_string (s, base_face, overlaps); \
20194 } \
20195 \
20196 ++START; \
20197 s = first_s; \
20198 } while (0)
20199
20200
20201 /* Add a glyph string for a glyph-string sequence to the list of strings
20202 between HEAD and TAIL. */
20203
20204 #define BUILD_GSTRING_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
20205 do { \
20206 int face_id; \
20207 XChar2b *char2b; \
20208 Lisp_Object gstring; \
20209 \
20210 face_id = (row)->glyphs[area][START].face_id; \
20211 gstring = (composition_gstring_from_id \
20212 ((row)->glyphs[area][START].u.cmp.id)); \
20213 s = (struct glyph_string *) alloca (sizeof *s); \
20214 char2b = (XChar2b *) alloca ((sizeof *char2b) \
20215 * LGSTRING_GLYPH_LEN (gstring)); \
20216 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
20217 append_glyph_string (&(HEAD), &(TAIL), s); \
20218 s->x = (X); \
20219 START = fill_gstring_glyph_string (s, face_id, START, END, overlaps); \
20220 } while (0)
20221
20222
20223 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
20224 of AREA of glyph row ROW on window W between indices START and END.
20225 HL overrides the face for drawing glyph strings, e.g. it is
20226 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
20227 x-positions of the drawing area.
20228
20229 This is an ugly monster macro construct because we must use alloca
20230 to allocate glyph strings (because draw_glyphs can be called
20231 asynchronously). */
20232
20233 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
20234 do \
20235 { \
20236 HEAD = TAIL = NULL; \
20237 while (START < END) \
20238 { \
20239 struct glyph *first_glyph = (row)->glyphs[area] + START; \
20240 switch (first_glyph->type) \
20241 { \
20242 case CHAR_GLYPH: \
20243 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
20244 HL, X, LAST_X); \
20245 break; \
20246 \
20247 case COMPOSITE_GLYPH: \
20248 if (first_glyph->u.cmp.automatic) \
20249 BUILD_GSTRING_GLYPH_STRING (START, END, HEAD, TAIL, \
20250 HL, X, LAST_X); \
20251 else \
20252 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
20253 HL, X, LAST_X); \
20254 break; \
20255 \
20256 case STRETCH_GLYPH: \
20257 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
20258 HL, X, LAST_X); \
20259 break; \
20260 \
20261 case IMAGE_GLYPH: \
20262 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
20263 HL, X, LAST_X); \
20264 break; \
20265 \
20266 default: \
20267 abort (); \
20268 } \
20269 \
20270 if (s) \
20271 { \
20272 set_glyph_string_background_width (s, START, LAST_X); \
20273 (X) += s->width; \
20274 } \
20275 } \
20276 } while (0)
20277
20278
20279 /* Draw glyphs between START and END in AREA of ROW on window W,
20280 starting at x-position X. X is relative to AREA in W. HL is a
20281 face-override with the following meaning:
20282
20283 DRAW_NORMAL_TEXT draw normally
20284 DRAW_CURSOR draw in cursor face
20285 DRAW_MOUSE_FACE draw in mouse face.
20286 DRAW_INVERSE_VIDEO draw in mode line face
20287 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
20288 DRAW_IMAGE_RAISED draw an image with a raised relief around it
20289
20290 If OVERLAPS is non-zero, draw only the foreground of characters and
20291 clip to the physical height of ROW. Non-zero value also defines
20292 the overlapping part to be drawn:
20293
20294 OVERLAPS_PRED overlap with preceding rows
20295 OVERLAPS_SUCC overlap with succeeding rows
20296 OVERLAPS_BOTH overlap with both preceding/succeeding rows
20297 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
20298
20299 Value is the x-position reached, relative to AREA of W. */
20300
20301 static int
20302 draw_glyphs (w, x, row, area, start, end, hl, overlaps)
20303 struct window *w;
20304 int x;
20305 struct glyph_row *row;
20306 enum glyph_row_area area;
20307 EMACS_INT start, end;
20308 enum draw_glyphs_face hl;
20309 int overlaps;
20310 {
20311 struct glyph_string *head, *tail;
20312 struct glyph_string *s;
20313 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
20314 int i, j, x_reached, last_x, area_left = 0;
20315 struct frame *f = XFRAME (WINDOW_FRAME (w));
20316 DECLARE_HDC (hdc);
20317
20318 ALLOCATE_HDC (hdc, f);
20319
20320 /* Let's rather be paranoid than getting a SEGV. */
20321 end = min (end, row->used[area]);
20322 start = max (0, start);
20323 start = min (end, start);
20324
20325 /* Translate X to frame coordinates. Set last_x to the right
20326 end of the drawing area. */
20327 if (row->full_width_p)
20328 {
20329 /* X is relative to the left edge of W, without scroll bars
20330 or fringes. */
20331 area_left = WINDOW_LEFT_EDGE_X (w);
20332 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
20333 }
20334 else
20335 {
20336 area_left = window_box_left (w, area);
20337 last_x = area_left + window_box_width (w, area);
20338 }
20339 x += area_left;
20340
20341 /* Build a doubly-linked list of glyph_string structures between
20342 head and tail from what we have to draw. Note that the macro
20343 BUILD_GLYPH_STRINGS will modify its start parameter. That's
20344 the reason we use a separate variable `i'. */
20345 i = start;
20346 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
20347 if (tail)
20348 x_reached = tail->x + tail->background_width;
20349 else
20350 x_reached = x;
20351
20352 /* If there are any glyphs with lbearing < 0 or rbearing > width in
20353 the row, redraw some glyphs in front or following the glyph
20354 strings built above. */
20355 if (head && !overlaps && row->contains_overlapping_glyphs_p)
20356 {
20357 struct glyph_string *h, *t;
20358 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
20359 int mouse_beg_col, mouse_end_col, check_mouse_face = 0;
20360 int dummy_x = 0;
20361
20362 /* If mouse highlighting is on, we may need to draw adjacent
20363 glyphs using mouse-face highlighting. */
20364 if (area == TEXT_AREA && row->mouse_face_p)
20365 {
20366 struct glyph_row *mouse_beg_row, *mouse_end_row;
20367
20368 mouse_beg_row = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_beg_row);
20369 mouse_end_row = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_end_row);
20370
20371 if (row >= mouse_beg_row && row <= mouse_end_row)
20372 {
20373 check_mouse_face = 1;
20374 mouse_beg_col = (row == mouse_beg_row)
20375 ? dpyinfo->mouse_face_beg_col : 0;
20376 mouse_end_col = (row == mouse_end_row)
20377 ? dpyinfo->mouse_face_end_col
20378 : row->used[TEXT_AREA];
20379 }
20380 }
20381
20382 /* Compute overhangs for all glyph strings. */
20383 if (FRAME_RIF (f)->compute_glyph_string_overhangs)
20384 for (s = head; s; s = s->next)
20385 FRAME_RIF (f)->compute_glyph_string_overhangs (s);
20386
20387 /* Prepend glyph strings for glyphs in front of the first glyph
20388 string that are overwritten because of the first glyph
20389 string's left overhang. The background of all strings
20390 prepended must be drawn because the first glyph string
20391 draws over it. */
20392 i = left_overwritten (head);
20393 if (i >= 0)
20394 {
20395 enum draw_glyphs_face overlap_hl;
20396
20397 /* If this row contains mouse highlighting, attempt to draw
20398 the overlapped glyphs with the correct highlight. This
20399 code fails if the overlap encompasses more than one glyph
20400 and mouse-highlight spans only some of these glyphs.
20401 However, making it work perfectly involves a lot more
20402 code, and I don't know if the pathological case occurs in
20403 practice, so we'll stick to this for now. --- cyd */
20404 if (check_mouse_face
20405 && mouse_beg_col < start && mouse_end_col > i)
20406 overlap_hl = DRAW_MOUSE_FACE;
20407 else
20408 overlap_hl = DRAW_NORMAL_TEXT;
20409
20410 j = i;
20411 BUILD_GLYPH_STRINGS (j, start, h, t,
20412 overlap_hl, dummy_x, last_x);
20413 compute_overhangs_and_x (t, head->x, 1);
20414 prepend_glyph_string_lists (&head, &tail, h, t);
20415 clip_head = head;
20416 }
20417
20418 /* Prepend glyph strings for glyphs in front of the first glyph
20419 string that overwrite that glyph string because of their
20420 right overhang. For these strings, only the foreground must
20421 be drawn, because it draws over the glyph string at `head'.
20422 The background must not be drawn because this would overwrite
20423 right overhangs of preceding glyphs for which no glyph
20424 strings exist. */
20425 i = left_overwriting (head);
20426 if (i >= 0)
20427 {
20428 enum draw_glyphs_face overlap_hl;
20429
20430 if (check_mouse_face
20431 && mouse_beg_col < start && mouse_end_col > i)
20432 overlap_hl = DRAW_MOUSE_FACE;
20433 else
20434 overlap_hl = DRAW_NORMAL_TEXT;
20435
20436 clip_head = head;
20437 BUILD_GLYPH_STRINGS (i, start, h, t,
20438 overlap_hl, dummy_x, last_x);
20439 for (s = h; s; s = s->next)
20440 s->background_filled_p = 1;
20441 compute_overhangs_and_x (t, head->x, 1);
20442 prepend_glyph_string_lists (&head, &tail, h, t);
20443 }
20444
20445 /* Append glyphs strings for glyphs following the last glyph
20446 string tail that are overwritten by tail. The background of
20447 these strings has to be drawn because tail's foreground draws
20448 over it. */
20449 i = right_overwritten (tail);
20450 if (i >= 0)
20451 {
20452 enum draw_glyphs_face overlap_hl;
20453
20454 if (check_mouse_face
20455 && mouse_beg_col < i && mouse_end_col > end)
20456 overlap_hl = DRAW_MOUSE_FACE;
20457 else
20458 overlap_hl = DRAW_NORMAL_TEXT;
20459
20460 BUILD_GLYPH_STRINGS (end, i, h, t,
20461 overlap_hl, x, last_x);
20462 compute_overhangs_and_x (h, tail->x + tail->width, 0);
20463 append_glyph_string_lists (&head, &tail, h, t);
20464 clip_tail = tail;
20465 }
20466
20467 /* Append glyph strings for glyphs following the last glyph
20468 string tail that overwrite tail. The foreground of such
20469 glyphs has to be drawn because it writes into the background
20470 of tail. The background must not be drawn because it could
20471 paint over the foreground of following glyphs. */
20472 i = right_overwriting (tail);
20473 if (i >= 0)
20474 {
20475 enum draw_glyphs_face overlap_hl;
20476 if (check_mouse_face
20477 && mouse_beg_col < i && mouse_end_col > end)
20478 overlap_hl = DRAW_MOUSE_FACE;
20479 else
20480 overlap_hl = DRAW_NORMAL_TEXT;
20481
20482 clip_tail = tail;
20483 i++; /* We must include the Ith glyph. */
20484 BUILD_GLYPH_STRINGS (end, i, h, t,
20485 overlap_hl, x, last_x);
20486 for (s = h; s; s = s->next)
20487 s->background_filled_p = 1;
20488 compute_overhangs_and_x (h, tail->x + tail->width, 0);
20489 append_glyph_string_lists (&head, &tail, h, t);
20490 }
20491 if (clip_head || clip_tail)
20492 for (s = head; s; s = s->next)
20493 {
20494 s->clip_head = clip_head;
20495 s->clip_tail = clip_tail;
20496 }
20497 }
20498
20499 /* Draw all strings. */
20500 for (s = head; s; s = s->next)
20501 FRAME_RIF (f)->draw_glyph_string (s);
20502
20503 #ifndef HAVE_NS
20504 /* When focus a sole frame and move horizontally, this sets on_p to 0
20505 causing a failure to erase prev cursor position. */
20506 if (area == TEXT_AREA
20507 && !row->full_width_p
20508 /* When drawing overlapping rows, only the glyph strings'
20509 foreground is drawn, which doesn't erase a cursor
20510 completely. */
20511 && !overlaps)
20512 {
20513 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
20514 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
20515 : (tail ? tail->x + tail->background_width : x));
20516 x0 -= area_left;
20517 x1 -= area_left;
20518
20519 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
20520 row->y, MATRIX_ROW_BOTTOM_Y (row));
20521 }
20522 #endif
20523
20524 /* Value is the x-position up to which drawn, relative to AREA of W.
20525 This doesn't include parts drawn because of overhangs. */
20526 if (row->full_width_p)
20527 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
20528 else
20529 x_reached -= area_left;
20530
20531 RELEASE_HDC (hdc, f);
20532
20533 return x_reached;
20534 }
20535
20536 /* Expand row matrix if too narrow. Don't expand if area
20537 is not present. */
20538
20539 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
20540 { \
20541 if (!fonts_changed_p \
20542 && (it->glyph_row->glyphs[area] \
20543 < it->glyph_row->glyphs[area + 1])) \
20544 { \
20545 it->w->ncols_scale_factor++; \
20546 fonts_changed_p = 1; \
20547 } \
20548 }
20549
20550 /* Store one glyph for IT->char_to_display in IT->glyph_row.
20551 Called from x_produce_glyphs when IT->glyph_row is non-null. */
20552
20553 static INLINE void
20554 append_glyph (it)
20555 struct it *it;
20556 {
20557 struct glyph *glyph;
20558 enum glyph_row_area area = it->area;
20559
20560 xassert (it->glyph_row);
20561 xassert (it->char_to_display != '\n' && it->char_to_display != '\t');
20562
20563 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
20564 if (glyph < it->glyph_row->glyphs[area + 1])
20565 {
20566 glyph->charpos = CHARPOS (it->position);
20567 glyph->object = it->object;
20568 if (it->pixel_width > 0)
20569 {
20570 glyph->pixel_width = it->pixel_width;
20571 glyph->padding_p = 0;
20572 }
20573 else
20574 {
20575 /* Assure at least 1-pixel width. Otherwise, cursor can't
20576 be displayed correctly. */
20577 glyph->pixel_width = 1;
20578 glyph->padding_p = 1;
20579 }
20580 glyph->ascent = it->ascent;
20581 glyph->descent = it->descent;
20582 glyph->voffset = it->voffset;
20583 glyph->type = CHAR_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->glyph_not_available_p = it->glyph_not_available_p;
20591 glyph->face_id = it->face_id;
20592 glyph->u.ch = it->char_to_display;
20593 glyph->slice = null_glyph_slice;
20594 glyph->font_type = FONT_TYPE_UNKNOWN;
20595 ++it->glyph_row->used[area];
20596 }
20597 else
20598 IT_EXPAND_MATRIX_WIDTH (it, area);
20599 }
20600
20601 /* Store one glyph for the composition IT->cmp_it.id in
20602 IT->glyph_row. Called from x_produce_glyphs when IT->glyph_row is
20603 non-null. */
20604
20605 static INLINE void
20606 append_composite_glyph (it)
20607 struct it *it;
20608 {
20609 struct glyph *glyph;
20610 enum glyph_row_area area = it->area;
20611
20612 xassert (it->glyph_row);
20613
20614 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
20615 if (glyph < it->glyph_row->glyphs[area + 1])
20616 {
20617 glyph->charpos = CHARPOS (it->position);
20618 glyph->object = it->object;
20619 glyph->pixel_width = it->pixel_width;
20620 glyph->ascent = it->ascent;
20621 glyph->descent = it->descent;
20622 glyph->voffset = it->voffset;
20623 glyph->type = COMPOSITE_GLYPH;
20624 if (it->cmp_it.ch < 0)
20625 {
20626 glyph->u.cmp.automatic = 0;
20627 glyph->u.cmp.id = it->cmp_it.id;
20628 }
20629 else
20630 {
20631 glyph->u.cmp.automatic = 1;
20632 glyph->u.cmp.id = it->cmp_it.id;
20633 glyph->u.cmp.from = it->cmp_it.from;
20634 glyph->u.cmp.to = it->cmp_it.to;
20635 }
20636 glyph->avoid_cursor_p = it->avoid_cursor_p;
20637 glyph->multibyte_p = it->multibyte_p;
20638 glyph->left_box_line_p = it->start_of_box_run_p;
20639 glyph->right_box_line_p = it->end_of_box_run_p;
20640 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
20641 || it->phys_descent > it->descent);
20642 glyph->padding_p = 0;
20643 glyph->glyph_not_available_p = 0;
20644 glyph->face_id = it->face_id;
20645 glyph->slice = null_glyph_slice;
20646 glyph->font_type = FONT_TYPE_UNKNOWN;
20647 ++it->glyph_row->used[area];
20648 }
20649 else
20650 IT_EXPAND_MATRIX_WIDTH (it, area);
20651 }
20652
20653
20654 /* Change IT->ascent and IT->height according to the setting of
20655 IT->voffset. */
20656
20657 static INLINE void
20658 take_vertical_position_into_account (it)
20659 struct it *it;
20660 {
20661 if (it->voffset)
20662 {
20663 if (it->voffset < 0)
20664 /* Increase the ascent so that we can display the text higher
20665 in the line. */
20666 it->ascent -= it->voffset;
20667 else
20668 /* Increase the descent so that we can display the text lower
20669 in the line. */
20670 it->descent += it->voffset;
20671 }
20672 }
20673
20674
20675 /* Produce glyphs/get display metrics for the image IT is loaded with.
20676 See the description of struct display_iterator in dispextern.h for
20677 an overview of struct display_iterator. */
20678
20679 static void
20680 produce_image_glyph (it)
20681 struct it *it;
20682 {
20683 struct image *img;
20684 struct face *face;
20685 int glyph_ascent, crop;
20686 struct glyph_slice slice;
20687
20688 xassert (it->what == IT_IMAGE);
20689
20690 face = FACE_FROM_ID (it->f, it->face_id);
20691 xassert (face);
20692 /* Make sure X resources of the face is loaded. */
20693 PREPARE_FACE_FOR_DISPLAY (it->f, face);
20694
20695 if (it->image_id < 0)
20696 {
20697 /* Fringe bitmap. */
20698 it->ascent = it->phys_ascent = 0;
20699 it->descent = it->phys_descent = 0;
20700 it->pixel_width = 0;
20701 it->nglyphs = 0;
20702 return;
20703 }
20704
20705 img = IMAGE_FROM_ID (it->f, it->image_id);
20706 xassert (img);
20707 /* Make sure X resources of the image is loaded. */
20708 prepare_image_for_display (it->f, img);
20709
20710 slice.x = slice.y = 0;
20711 slice.width = img->width;
20712 slice.height = img->height;
20713
20714 if (INTEGERP (it->slice.x))
20715 slice.x = XINT (it->slice.x);
20716 else if (FLOATP (it->slice.x))
20717 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
20718
20719 if (INTEGERP (it->slice.y))
20720 slice.y = XINT (it->slice.y);
20721 else if (FLOATP (it->slice.y))
20722 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
20723
20724 if (INTEGERP (it->slice.width))
20725 slice.width = XINT (it->slice.width);
20726 else if (FLOATP (it->slice.width))
20727 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
20728
20729 if (INTEGERP (it->slice.height))
20730 slice.height = XINT (it->slice.height);
20731 else if (FLOATP (it->slice.height))
20732 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
20733
20734 if (slice.x >= img->width)
20735 slice.x = img->width;
20736 if (slice.y >= img->height)
20737 slice.y = img->height;
20738 if (slice.x + slice.width >= img->width)
20739 slice.width = img->width - slice.x;
20740 if (slice.y + slice.height > img->height)
20741 slice.height = img->height - slice.y;
20742
20743 if (slice.width == 0 || slice.height == 0)
20744 return;
20745
20746 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
20747
20748 it->descent = slice.height - glyph_ascent;
20749 if (slice.y == 0)
20750 it->descent += img->vmargin;
20751 if (slice.y + slice.height == img->height)
20752 it->descent += img->vmargin;
20753 it->phys_descent = it->descent;
20754
20755 it->pixel_width = slice.width;
20756 if (slice.x == 0)
20757 it->pixel_width += img->hmargin;
20758 if (slice.x + slice.width == img->width)
20759 it->pixel_width += img->hmargin;
20760
20761 /* It's quite possible for images to have an ascent greater than
20762 their height, so don't get confused in that case. */
20763 if (it->descent < 0)
20764 it->descent = 0;
20765
20766 #if 0 /* this breaks image tiling */
20767 /* If this glyph is alone on the last line, adjust it.ascent to minimum row ascent. */
20768 int face_ascent = face->font ? FONT_BASE (face->font) : FRAME_BASELINE_OFFSET (it->f);
20769 if (face_ascent > it->ascent)
20770 it->ascent = it->phys_ascent = face_ascent;
20771 #endif
20772
20773 it->nglyphs = 1;
20774
20775 if (face->box != FACE_NO_BOX)
20776 {
20777 if (face->box_line_width > 0)
20778 {
20779 if (slice.y == 0)
20780 it->ascent += face->box_line_width;
20781 if (slice.y + slice.height == img->height)
20782 it->descent += face->box_line_width;
20783 }
20784
20785 if (it->start_of_box_run_p && slice.x == 0)
20786 it->pixel_width += eabs (face->box_line_width);
20787 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
20788 it->pixel_width += eabs (face->box_line_width);
20789 }
20790
20791 take_vertical_position_into_account (it);
20792
20793 /* Automatically crop wide image glyphs at right edge so we can
20794 draw the cursor on same display row. */
20795 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
20796 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
20797 {
20798 it->pixel_width -= crop;
20799 slice.width -= crop;
20800 }
20801
20802 if (it->glyph_row)
20803 {
20804 struct glyph *glyph;
20805 enum glyph_row_area area = it->area;
20806
20807 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
20808 if (glyph < it->glyph_row->glyphs[area + 1])
20809 {
20810 glyph->charpos = CHARPOS (it->position);
20811 glyph->object = it->object;
20812 glyph->pixel_width = it->pixel_width;
20813 glyph->ascent = glyph_ascent;
20814 glyph->descent = it->descent;
20815 glyph->voffset = it->voffset;
20816 glyph->type = IMAGE_GLYPH;
20817 glyph->avoid_cursor_p = it->avoid_cursor_p;
20818 glyph->multibyte_p = it->multibyte_p;
20819 glyph->left_box_line_p = it->start_of_box_run_p;
20820 glyph->right_box_line_p = it->end_of_box_run_p;
20821 glyph->overlaps_vertically_p = 0;
20822 glyph->padding_p = 0;
20823 glyph->glyph_not_available_p = 0;
20824 glyph->face_id = it->face_id;
20825 glyph->u.img_id = img->id;
20826 glyph->slice = slice;
20827 glyph->font_type = FONT_TYPE_UNKNOWN;
20828 ++it->glyph_row->used[area];
20829 }
20830 else
20831 IT_EXPAND_MATRIX_WIDTH (it, area);
20832 }
20833 }
20834
20835
20836 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
20837 of the glyph, WIDTH and HEIGHT are the width and height of the
20838 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
20839
20840 static void
20841 append_stretch_glyph (it, object, width, height, ascent)
20842 struct it *it;
20843 Lisp_Object object;
20844 int width, height;
20845 int ascent;
20846 {
20847 struct glyph *glyph;
20848 enum glyph_row_area area = it->area;
20849
20850 xassert (ascent >= 0 && ascent <= height);
20851
20852 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
20853 if (glyph < it->glyph_row->glyphs[area + 1])
20854 {
20855 glyph->charpos = CHARPOS (it->position);
20856 glyph->object = object;
20857 glyph->pixel_width = width;
20858 glyph->ascent = ascent;
20859 glyph->descent = height - ascent;
20860 glyph->voffset = it->voffset;
20861 glyph->type = STRETCH_GLYPH;
20862 glyph->avoid_cursor_p = it->avoid_cursor_p;
20863 glyph->multibyte_p = it->multibyte_p;
20864 glyph->left_box_line_p = it->start_of_box_run_p;
20865 glyph->right_box_line_p = it->end_of_box_run_p;
20866 glyph->overlaps_vertically_p = 0;
20867 glyph->padding_p = 0;
20868 glyph->glyph_not_available_p = 0;
20869 glyph->face_id = it->face_id;
20870 glyph->u.stretch.ascent = ascent;
20871 glyph->u.stretch.height = height;
20872 glyph->slice = null_glyph_slice;
20873 glyph->font_type = FONT_TYPE_UNKNOWN;
20874 ++it->glyph_row->used[area];
20875 }
20876 else
20877 IT_EXPAND_MATRIX_WIDTH (it, area);
20878 }
20879
20880
20881 /* Produce a stretch glyph for iterator IT. IT->object is the value
20882 of the glyph property displayed. The value must be a list
20883 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
20884 being recognized:
20885
20886 1. `:width WIDTH' specifies that the space should be WIDTH *
20887 canonical char width wide. WIDTH may be an integer or floating
20888 point number.
20889
20890 2. `:relative-width FACTOR' specifies that the width of the stretch
20891 should be computed from the width of the first character having the
20892 `glyph' property, and should be FACTOR times that width.
20893
20894 3. `:align-to HPOS' specifies that the space should be wide enough
20895 to reach HPOS, a value in canonical character units.
20896
20897 Exactly one of the above pairs must be present.
20898
20899 4. `:height HEIGHT' specifies that the height of the stretch produced
20900 should be HEIGHT, measured in canonical character units.
20901
20902 5. `:relative-height FACTOR' specifies that the height of the
20903 stretch should be FACTOR times the height of the characters having
20904 the glyph property.
20905
20906 Either none or exactly one of 4 or 5 must be present.
20907
20908 6. `:ascent ASCENT' specifies that ASCENT percent of the height
20909 of the stretch should be used for the ascent of the stretch.
20910 ASCENT must be in the range 0 <= ASCENT <= 100. */
20911
20912 static void
20913 produce_stretch_glyph (it)
20914 struct it *it;
20915 {
20916 /* (space :width WIDTH :height HEIGHT ...) */
20917 Lisp_Object prop, plist;
20918 int width = 0, height = 0, align_to = -1;
20919 int zero_width_ok_p = 0, zero_height_ok_p = 0;
20920 int ascent = 0;
20921 double tem;
20922 struct face *face = FACE_FROM_ID (it->f, it->face_id);
20923 struct font *font = face->font ? face->font : FRAME_FONT (it->f);
20924
20925 PREPARE_FACE_FOR_DISPLAY (it->f, face);
20926
20927 /* List should start with `space'. */
20928 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
20929 plist = XCDR (it->object);
20930
20931 /* Compute the width of the stretch. */
20932 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
20933 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
20934 {
20935 /* Absolute width `:width WIDTH' specified and valid. */
20936 zero_width_ok_p = 1;
20937 width = (int)tem;
20938 }
20939 else if (prop = Fplist_get (plist, QCrelative_width),
20940 NUMVAL (prop) > 0)
20941 {
20942 /* Relative width `:relative-width FACTOR' specified and valid.
20943 Compute the width of the characters having the `glyph'
20944 property. */
20945 struct it it2;
20946 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
20947
20948 it2 = *it;
20949 if (it->multibyte_p)
20950 {
20951 int maxlen = ((IT_BYTEPOS (*it) >= GPT ? ZV : GPT)
20952 - IT_BYTEPOS (*it));
20953 it2.c = STRING_CHAR_AND_LENGTH (p, maxlen, it2.len);
20954 }
20955 else
20956 it2.c = *p, it2.len = 1;
20957
20958 it2.glyph_row = NULL;
20959 it2.what = IT_CHARACTER;
20960 x_produce_glyphs (&it2);
20961 width = NUMVAL (prop) * it2.pixel_width;
20962 }
20963 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
20964 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
20965 {
20966 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
20967 align_to = (align_to < 0
20968 ? 0
20969 : align_to - window_box_left_offset (it->w, TEXT_AREA));
20970 else if (align_to < 0)
20971 align_to = window_box_left_offset (it->w, TEXT_AREA);
20972 width = max (0, (int)tem + align_to - it->current_x);
20973 zero_width_ok_p = 1;
20974 }
20975 else
20976 /* Nothing specified -> width defaults to canonical char width. */
20977 width = FRAME_COLUMN_WIDTH (it->f);
20978
20979 if (width <= 0 && (width < 0 || !zero_width_ok_p))
20980 width = 1;
20981
20982 /* Compute height. */
20983 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
20984 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
20985 {
20986 height = (int)tem;
20987 zero_height_ok_p = 1;
20988 }
20989 else if (prop = Fplist_get (plist, QCrelative_height),
20990 NUMVAL (prop) > 0)
20991 height = FONT_HEIGHT (font) * NUMVAL (prop);
20992 else
20993 height = FONT_HEIGHT (font);
20994
20995 if (height <= 0 && (height < 0 || !zero_height_ok_p))
20996 height = 1;
20997
20998 /* Compute percentage of height used for ascent. If
20999 `:ascent ASCENT' is present and valid, use that. Otherwise,
21000 derive the ascent from the font in use. */
21001 if (prop = Fplist_get (plist, QCascent),
21002 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
21003 ascent = height * NUMVAL (prop) / 100.0;
21004 else if (!NILP (prop)
21005 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
21006 ascent = min (max (0, (int)tem), height);
21007 else
21008 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
21009
21010 if (width > 0 && it->line_wrap != TRUNCATE
21011 && it->current_x + width > it->last_visible_x)
21012 width = it->last_visible_x - it->current_x - 1;
21013
21014 if (width > 0 && height > 0 && it->glyph_row)
21015 {
21016 Lisp_Object object = it->stack[it->sp - 1].string;
21017 if (!STRINGP (object))
21018 object = it->w->buffer;
21019 append_stretch_glyph (it, object, width, height, ascent);
21020 }
21021
21022 it->pixel_width = width;
21023 it->ascent = it->phys_ascent = ascent;
21024 it->descent = it->phys_descent = height - it->ascent;
21025 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
21026
21027 take_vertical_position_into_account (it);
21028 }
21029
21030 /* Calculate line-height and line-spacing properties.
21031 An integer value specifies explicit pixel value.
21032 A float value specifies relative value to current face height.
21033 A cons (float . face-name) specifies relative value to
21034 height of specified face font.
21035
21036 Returns height in pixels, or nil. */
21037
21038
21039 static Lisp_Object
21040 calc_line_height_property (it, val, font, boff, override)
21041 struct it *it;
21042 Lisp_Object val;
21043 struct font *font;
21044 int boff, override;
21045 {
21046 Lisp_Object face_name = Qnil;
21047 int ascent, descent, height;
21048
21049 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
21050 return val;
21051
21052 if (CONSP (val))
21053 {
21054 face_name = XCAR (val);
21055 val = XCDR (val);
21056 if (!NUMBERP (val))
21057 val = make_number (1);
21058 if (NILP (face_name))
21059 {
21060 height = it->ascent + it->descent;
21061 goto scale;
21062 }
21063 }
21064
21065 if (NILP (face_name))
21066 {
21067 font = FRAME_FONT (it->f);
21068 boff = FRAME_BASELINE_OFFSET (it->f);
21069 }
21070 else if (EQ (face_name, Qt))
21071 {
21072 override = 0;
21073 }
21074 else
21075 {
21076 int face_id;
21077 struct face *face;
21078
21079 face_id = lookup_named_face (it->f, face_name, 0);
21080 if (face_id < 0)
21081 return make_number (-1);
21082
21083 face = FACE_FROM_ID (it->f, face_id);
21084 font = face->font;
21085 if (font == NULL)
21086 return make_number (-1);
21087 boff = font->baseline_offset;
21088 if (font->vertical_centering)
21089 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
21090 }
21091
21092 ascent = FONT_BASE (font) + boff;
21093 descent = FONT_DESCENT (font) - boff;
21094
21095 if (override)
21096 {
21097 it->override_ascent = ascent;
21098 it->override_descent = descent;
21099 it->override_boff = boff;
21100 }
21101
21102 height = ascent + descent;
21103
21104 scale:
21105 if (FLOATP (val))
21106 height = (int)(XFLOAT_DATA (val) * height);
21107 else if (INTEGERP (val))
21108 height *= XINT (val);
21109
21110 return make_number (height);
21111 }
21112
21113
21114 /* RIF:
21115 Produce glyphs/get display metrics for the display element IT is
21116 loaded with. See the description of struct it in dispextern.h
21117 for an overview of struct it. */
21118
21119 void
21120 x_produce_glyphs (it)
21121 struct it *it;
21122 {
21123 int extra_line_spacing = it->extra_line_spacing;
21124
21125 it->glyph_not_available_p = 0;
21126
21127 if (it->what == IT_CHARACTER)
21128 {
21129 XChar2b char2b;
21130 struct font *font;
21131 struct face *face = FACE_FROM_ID (it->f, it->face_id);
21132 struct font_metrics *pcm;
21133 int font_not_found_p;
21134 int boff; /* baseline offset */
21135 /* We may change it->multibyte_p upon unibyte<->multibyte
21136 conversion. So, save the current value now and restore it
21137 later.
21138
21139 Note: It seems that we don't have to record multibyte_p in
21140 struct glyph because the character code itself tells if or
21141 not the character is multibyte. Thus, in the future, we must
21142 consider eliminating the field `multibyte_p' in the struct
21143 glyph. */
21144 int saved_multibyte_p = it->multibyte_p;
21145
21146 /* Maybe translate single-byte characters to multibyte, or the
21147 other way. */
21148 it->char_to_display = it->c;
21149 if (!ASCII_BYTE_P (it->c)
21150 && ! it->multibyte_p)
21151 {
21152 if (SINGLE_BYTE_CHAR_P (it->c)
21153 && unibyte_display_via_language_environment)
21154 it->char_to_display = unibyte_char_to_multibyte (it->c);
21155 if (! SINGLE_BYTE_CHAR_P (it->char_to_display))
21156 {
21157 it->multibyte_p = 1;
21158 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display,
21159 -1, Qnil);
21160 face = FACE_FROM_ID (it->f, it->face_id);
21161 }
21162 }
21163
21164 /* Get font to use. Encode IT->char_to_display. */
21165 get_char_face_and_encoding (it->f, it->char_to_display, it->face_id,
21166 &char2b, it->multibyte_p, 0);
21167 font = face->font;
21168
21169 /* When no suitable font found, use the default font. */
21170 font_not_found_p = font == NULL;
21171 if (font_not_found_p)
21172 {
21173 font = FRAME_FONT (it->f);
21174 boff = FRAME_BASELINE_OFFSET (it->f);
21175 }
21176 else
21177 {
21178 boff = font->baseline_offset;
21179 if (font->vertical_centering)
21180 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
21181 }
21182
21183 if (it->char_to_display >= ' '
21184 && (!it->multibyte_p || it->char_to_display < 128))
21185 {
21186 /* Either unibyte or ASCII. */
21187 int stretched_p;
21188
21189 it->nglyphs = 1;
21190
21191 pcm = get_per_char_metric (it->f, font, &char2b);
21192
21193 if (it->override_ascent >= 0)
21194 {
21195 it->ascent = it->override_ascent;
21196 it->descent = it->override_descent;
21197 boff = it->override_boff;
21198 }
21199 else
21200 {
21201 it->ascent = FONT_BASE (font) + boff;
21202 it->descent = FONT_DESCENT (font) - boff;
21203 }
21204
21205 if (pcm)
21206 {
21207 it->phys_ascent = pcm->ascent + boff;
21208 it->phys_descent = pcm->descent - boff;
21209 it->pixel_width = pcm->width;
21210 }
21211 else
21212 {
21213 it->glyph_not_available_p = 1;
21214 it->phys_ascent = it->ascent;
21215 it->phys_descent = it->descent;
21216 it->pixel_width = FONT_WIDTH (font);
21217 }
21218
21219 if (it->constrain_row_ascent_descent_p)
21220 {
21221 if (it->descent > it->max_descent)
21222 {
21223 it->ascent += it->descent - it->max_descent;
21224 it->descent = it->max_descent;
21225 }
21226 if (it->ascent > it->max_ascent)
21227 {
21228 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
21229 it->ascent = it->max_ascent;
21230 }
21231 it->phys_ascent = min (it->phys_ascent, it->ascent);
21232 it->phys_descent = min (it->phys_descent, it->descent);
21233 extra_line_spacing = 0;
21234 }
21235
21236 /* If this is a space inside a region of text with
21237 `space-width' property, change its width. */
21238 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
21239 if (stretched_p)
21240 it->pixel_width *= XFLOATINT (it->space_width);
21241
21242 /* If face has a box, add the box thickness to the character
21243 height. If character has a box line to the left and/or
21244 right, add the box line width to the character's width. */
21245 if (face->box != FACE_NO_BOX)
21246 {
21247 int thick = face->box_line_width;
21248
21249 if (thick > 0)
21250 {
21251 it->ascent += thick;
21252 it->descent += thick;
21253 }
21254 else
21255 thick = -thick;
21256
21257 if (it->start_of_box_run_p)
21258 it->pixel_width += thick;
21259 if (it->end_of_box_run_p)
21260 it->pixel_width += thick;
21261 }
21262
21263 /* If face has an overline, add the height of the overline
21264 (1 pixel) and a 1 pixel margin to the character height. */
21265 if (face->overline_p)
21266 it->ascent += overline_margin;
21267
21268 if (it->constrain_row_ascent_descent_p)
21269 {
21270 if (it->ascent > it->max_ascent)
21271 it->ascent = it->max_ascent;
21272 if (it->descent > it->max_descent)
21273 it->descent = it->max_descent;
21274 }
21275
21276 take_vertical_position_into_account (it);
21277
21278 /* If we have to actually produce glyphs, do it. */
21279 if (it->glyph_row)
21280 {
21281 if (stretched_p)
21282 {
21283 /* Translate a space with a `space-width' property
21284 into a stretch glyph. */
21285 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
21286 / FONT_HEIGHT (font));
21287 append_stretch_glyph (it, it->object, it->pixel_width,
21288 it->ascent + it->descent, ascent);
21289 }
21290 else
21291 append_glyph (it);
21292
21293 /* If characters with lbearing or rbearing are displayed
21294 in this line, record that fact in a flag of the
21295 glyph row. This is used to optimize X output code. */
21296 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
21297 it->glyph_row->contains_overlapping_glyphs_p = 1;
21298 }
21299 if (! stretched_p && it->pixel_width == 0)
21300 /* We assure that all visible glyphs have at least 1-pixel
21301 width. */
21302 it->pixel_width = 1;
21303 }
21304 else if (it->char_to_display == '\n')
21305 {
21306 /* A newline has no width but we need the height of the line.
21307 But if previous part of the line set a height, don't
21308 increase that height */
21309
21310 Lisp_Object height;
21311 Lisp_Object total_height = Qnil;
21312
21313 it->override_ascent = -1;
21314 it->pixel_width = 0;
21315 it->nglyphs = 0;
21316
21317 height = get_it_property(it, Qline_height);
21318 /* Split (line-height total-height) list */
21319 if (CONSP (height)
21320 && CONSP (XCDR (height))
21321 && NILP (XCDR (XCDR (height))))
21322 {
21323 total_height = XCAR (XCDR (height));
21324 height = XCAR (height);
21325 }
21326 height = calc_line_height_property(it, height, font, boff, 1);
21327
21328 if (it->override_ascent >= 0)
21329 {
21330 it->ascent = it->override_ascent;
21331 it->descent = it->override_descent;
21332 boff = it->override_boff;
21333 }
21334 else
21335 {
21336 it->ascent = FONT_BASE (font) + boff;
21337 it->descent = FONT_DESCENT (font) - boff;
21338 }
21339
21340 if (EQ (height, Qt))
21341 {
21342 if (it->descent > it->max_descent)
21343 {
21344 it->ascent += it->descent - it->max_descent;
21345 it->descent = it->max_descent;
21346 }
21347 if (it->ascent > it->max_ascent)
21348 {
21349 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
21350 it->ascent = it->max_ascent;
21351 }
21352 it->phys_ascent = min (it->phys_ascent, it->ascent);
21353 it->phys_descent = min (it->phys_descent, it->descent);
21354 it->constrain_row_ascent_descent_p = 1;
21355 extra_line_spacing = 0;
21356 }
21357 else
21358 {
21359 Lisp_Object spacing;
21360
21361 it->phys_ascent = it->ascent;
21362 it->phys_descent = it->descent;
21363
21364 if ((it->max_ascent > 0 || it->max_descent > 0)
21365 && face->box != FACE_NO_BOX
21366 && face->box_line_width > 0)
21367 {
21368 it->ascent += face->box_line_width;
21369 it->descent += face->box_line_width;
21370 }
21371 if (!NILP (height)
21372 && XINT (height) > it->ascent + it->descent)
21373 it->ascent = XINT (height) - it->descent;
21374
21375 if (!NILP (total_height))
21376 spacing = calc_line_height_property(it, total_height, font, boff, 0);
21377 else
21378 {
21379 spacing = get_it_property(it, Qline_spacing);
21380 spacing = calc_line_height_property(it, spacing, font, boff, 0);
21381 }
21382 if (INTEGERP (spacing))
21383 {
21384 extra_line_spacing = XINT (spacing);
21385 if (!NILP (total_height))
21386 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
21387 }
21388 }
21389 }
21390 else if (it->char_to_display == '\t')
21391 {
21392 if (font->space_width > 0)
21393 {
21394 int tab_width = it->tab_width * font->space_width;
21395 int x = it->current_x + it->continuation_lines_width;
21396 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
21397
21398 /* If the distance from the current position to the next tab
21399 stop is less than a space character width, use the
21400 tab stop after that. */
21401 if (next_tab_x - x < font->space_width)
21402 next_tab_x += tab_width;
21403
21404 it->pixel_width = next_tab_x - x;
21405 it->nglyphs = 1;
21406 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
21407 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
21408
21409 if (it->glyph_row)
21410 {
21411 append_stretch_glyph (it, it->object, it->pixel_width,
21412 it->ascent + it->descent, it->ascent);
21413 }
21414 }
21415 else
21416 {
21417 it->pixel_width = 0;
21418 it->nglyphs = 1;
21419 }
21420 }
21421 else
21422 {
21423 /* A multi-byte character. Assume that the display width of the
21424 character is the width of the character multiplied by the
21425 width of the font. */
21426
21427 /* If we found a font, this font should give us the right
21428 metrics. If we didn't find a font, use the frame's
21429 default font and calculate the width of the character by
21430 multiplying the width of font by the width of the
21431 character. */
21432
21433 pcm = get_per_char_metric (it->f, font, &char2b);
21434
21435 if (font_not_found_p || !pcm)
21436 {
21437 int char_width = CHAR_WIDTH (it->char_to_display);
21438
21439 if (char_width == 0)
21440 /* This is a non spacing character. But, as we are
21441 going to display an empty box, the box must occupy
21442 at least one column. */
21443 char_width = 1;
21444 it->glyph_not_available_p = 1;
21445 it->pixel_width = FRAME_COLUMN_WIDTH (it->f) * char_width;
21446 it->phys_ascent = FONT_BASE (font) + boff;
21447 it->phys_descent = FONT_DESCENT (font) - boff;
21448 }
21449 else
21450 {
21451 it->pixel_width = pcm->width;
21452 it->phys_ascent = pcm->ascent + boff;
21453 it->phys_descent = pcm->descent - boff;
21454 if (it->glyph_row
21455 && (pcm->lbearing < 0
21456 || pcm->rbearing > pcm->width))
21457 it->glyph_row->contains_overlapping_glyphs_p = 1;
21458 }
21459 it->nglyphs = 1;
21460 it->ascent = FONT_BASE (font) + boff;
21461 it->descent = FONT_DESCENT (font) - boff;
21462 if (face->box != FACE_NO_BOX)
21463 {
21464 int thick = face->box_line_width;
21465
21466 if (thick > 0)
21467 {
21468 it->ascent += thick;
21469 it->descent += thick;
21470 }
21471 else
21472 thick = - thick;
21473
21474 if (it->start_of_box_run_p)
21475 it->pixel_width += thick;
21476 if (it->end_of_box_run_p)
21477 it->pixel_width += thick;
21478 }
21479
21480 /* If face has an overline, add the height of the overline
21481 (1 pixel) and a 1 pixel margin to the character height. */
21482 if (face->overline_p)
21483 it->ascent += overline_margin;
21484
21485 take_vertical_position_into_account (it);
21486
21487 if (it->ascent < 0)
21488 it->ascent = 0;
21489 if (it->descent < 0)
21490 it->descent = 0;
21491
21492 if (it->glyph_row)
21493 append_glyph (it);
21494 if (it->pixel_width == 0)
21495 /* We assure that all visible glyphs have at least 1-pixel
21496 width. */
21497 it->pixel_width = 1;
21498 }
21499 it->multibyte_p = saved_multibyte_p;
21500 }
21501 else if (it->what == IT_COMPOSITION && it->cmp_it.ch < 0)
21502 {
21503 /* A static compositoin.
21504
21505 Note: A composition is represented as one glyph in the
21506 glyph matrix. There are no padding glyphs.
21507
21508 Important is that pixel_width, ascent, and descent are the
21509 values of what is drawn by draw_glyphs (i.e. the values of
21510 the overall glyphs composed). */
21511 struct face *face = FACE_FROM_ID (it->f, it->face_id);
21512 int boff; /* baseline offset */
21513 struct composition *cmp = composition_table[it->cmp_it.id];
21514 int glyph_len = cmp->glyph_len;
21515 struct font *font = face->font;
21516
21517 it->nglyphs = 1;
21518
21519 /* If we have not yet calculated pixel size data of glyphs of
21520 the composition for the current face font, calculate them
21521 now. Theoretically, we have to check all fonts for the
21522 glyphs, but that requires much time and memory space. So,
21523 here we check only the font of the first glyph. This leads
21524 to incorrect display, but it's very rare, and C-l (recenter)
21525 can correct the display anyway. */
21526 if (! cmp->font || cmp->font != font)
21527 {
21528 /* Ascent and descent of the font of the first character
21529 of this composition (adjusted by baseline offset).
21530 Ascent and descent of overall glyphs should not be less
21531 than them respectively. */
21532 int font_ascent, font_descent, font_height;
21533 /* Bounding box of the overall glyphs. */
21534 int leftmost, rightmost, lowest, highest;
21535 int lbearing, rbearing;
21536 int i, width, ascent, descent;
21537 int left_padded = 0, right_padded = 0;
21538 int c;
21539 XChar2b char2b;
21540 struct font_metrics *pcm;
21541 int font_not_found_p;
21542 int pos;
21543
21544 for (glyph_len = cmp->glyph_len; glyph_len > 0; glyph_len--)
21545 if ((c = COMPOSITION_GLYPH (cmp, glyph_len - 1)) != '\t')
21546 break;
21547 if (glyph_len < cmp->glyph_len)
21548 right_padded = 1;
21549 for (i = 0; i < glyph_len; i++)
21550 {
21551 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
21552 break;
21553 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
21554 }
21555 if (i > 0)
21556 left_padded = 1;
21557
21558 pos = (STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
21559 : IT_CHARPOS (*it));
21560 /* When no suitable font found, use the default font. */
21561 font_not_found_p = font == NULL;
21562 if (font_not_found_p)
21563 {
21564 face = face->ascii_face;
21565 font = face->font;
21566 }
21567 boff = font->baseline_offset;
21568 if (font->vertical_centering)
21569 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
21570 font_ascent = FONT_BASE (font) + boff;
21571 font_descent = FONT_DESCENT (font) - boff;
21572 font_height = FONT_HEIGHT (font);
21573
21574 cmp->font = (void *) font;
21575
21576 pcm = NULL;
21577 if (! font_not_found_p)
21578 {
21579 get_char_face_and_encoding (it->f, c, it->face_id,
21580 &char2b, it->multibyte_p, 0);
21581 pcm = get_per_char_metric (it->f, font, &char2b);
21582 }
21583
21584 /* Initialize the bounding box. */
21585 if (pcm)
21586 {
21587 width = pcm->width;
21588 ascent = pcm->ascent;
21589 descent = pcm->descent;
21590 lbearing = pcm->lbearing;
21591 rbearing = pcm->rbearing;
21592 }
21593 else
21594 {
21595 width = FONT_WIDTH (font);
21596 ascent = FONT_BASE (font);
21597 descent = FONT_DESCENT (font);
21598 lbearing = 0;
21599 rbearing = width;
21600 }
21601
21602 rightmost = width;
21603 leftmost = 0;
21604 lowest = - descent + boff;
21605 highest = ascent + boff;
21606
21607 if (! font_not_found_p
21608 && font->default_ascent
21609 && CHAR_TABLE_P (Vuse_default_ascent)
21610 && !NILP (Faref (Vuse_default_ascent,
21611 make_number (it->char_to_display))))
21612 highest = font->default_ascent + boff;
21613
21614 /* Draw the first glyph at the normal position. It may be
21615 shifted to right later if some other glyphs are drawn
21616 at the left. */
21617 cmp->offsets[i * 2] = 0;
21618 cmp->offsets[i * 2 + 1] = boff;
21619 cmp->lbearing = lbearing;
21620 cmp->rbearing = rbearing;
21621
21622 /* Set cmp->offsets for the remaining glyphs. */
21623 for (i++; i < glyph_len; i++)
21624 {
21625 int left, right, btm, top;
21626 int ch = COMPOSITION_GLYPH (cmp, i);
21627 int face_id;
21628 struct face *this_face;
21629 int this_boff;
21630
21631 if (ch == '\t')
21632 ch = ' ';
21633 face_id = FACE_FOR_CHAR (it->f, face, ch, pos, it->string);
21634 this_face = FACE_FROM_ID (it->f, face_id);
21635 font = this_face->font;
21636
21637 if (font == NULL)
21638 pcm = NULL;
21639 else
21640 {
21641 this_boff = font->baseline_offset;
21642 if (font->vertical_centering)
21643 this_boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
21644 get_char_face_and_encoding (it->f, ch, face_id,
21645 &char2b, it->multibyte_p, 0);
21646 pcm = get_per_char_metric (it->f, font, &char2b);
21647 }
21648 if (! pcm)
21649 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
21650 else
21651 {
21652 width = pcm->width;
21653 ascent = pcm->ascent;
21654 descent = pcm->descent;
21655 lbearing = pcm->lbearing;
21656 rbearing = pcm->rbearing;
21657 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
21658 {
21659 /* Relative composition with or without
21660 alternate chars. */
21661 left = (leftmost + rightmost - width) / 2;
21662 btm = - descent + boff;
21663 if (font->relative_compose
21664 && (! CHAR_TABLE_P (Vignore_relative_composition)
21665 || NILP (Faref (Vignore_relative_composition,
21666 make_number (ch)))))
21667 {
21668
21669 if (- descent >= font->relative_compose)
21670 /* One extra pixel between two glyphs. */
21671 btm = highest + 1;
21672 else if (ascent <= 0)
21673 /* One extra pixel between two glyphs. */
21674 btm = lowest - 1 - ascent - descent;
21675 }
21676 }
21677 else
21678 {
21679 /* A composition rule is specified by an integer
21680 value that encodes global and new reference
21681 points (GREF and NREF). GREF and NREF are
21682 specified by numbers as below:
21683
21684 0---1---2 -- ascent
21685 | |
21686 | |
21687 | |
21688 9--10--11 -- center
21689 | |
21690 ---3---4---5--- baseline
21691 | |
21692 6---7---8 -- descent
21693 */
21694 int rule = COMPOSITION_RULE (cmp, i);
21695 int gref, nref, grefx, grefy, nrefx, nrefy, xoff, yoff;
21696
21697 COMPOSITION_DECODE_RULE (rule, gref, nref, xoff, yoff);
21698 grefx = gref % 3, nrefx = nref % 3;
21699 grefy = gref / 3, nrefy = nref / 3;
21700 if (xoff)
21701 xoff = font_height * (xoff - 128) / 256;
21702 if (yoff)
21703 yoff = font_height * (yoff - 128) / 256;
21704
21705 left = (leftmost
21706 + grefx * (rightmost - leftmost) / 2
21707 - nrefx * width / 2
21708 + xoff);
21709
21710 btm = ((grefy == 0 ? highest
21711 : grefy == 1 ? 0
21712 : grefy == 2 ? lowest
21713 : (highest + lowest) / 2)
21714 - (nrefy == 0 ? ascent + descent
21715 : nrefy == 1 ? descent - boff
21716 : nrefy == 2 ? 0
21717 : (ascent + descent) / 2)
21718 + yoff);
21719 }
21720
21721 cmp->offsets[i * 2] = left;
21722 cmp->offsets[i * 2 + 1] = btm + descent;
21723
21724 /* Update the bounding box of the overall glyphs. */
21725 if (width > 0)
21726 {
21727 right = left + width;
21728 if (left < leftmost)
21729 leftmost = left;
21730 if (right > rightmost)
21731 rightmost = right;
21732 }
21733 top = btm + descent + ascent;
21734 if (top > highest)
21735 highest = top;
21736 if (btm < lowest)
21737 lowest = btm;
21738
21739 if (cmp->lbearing > left + lbearing)
21740 cmp->lbearing = left + lbearing;
21741 if (cmp->rbearing < left + rbearing)
21742 cmp->rbearing = left + rbearing;
21743 }
21744 }
21745
21746 /* If there are glyphs whose x-offsets are negative,
21747 shift all glyphs to the right and make all x-offsets
21748 non-negative. */
21749 if (leftmost < 0)
21750 {
21751 for (i = 0; i < cmp->glyph_len; i++)
21752 cmp->offsets[i * 2] -= leftmost;
21753 rightmost -= leftmost;
21754 cmp->lbearing -= leftmost;
21755 cmp->rbearing -= leftmost;
21756 }
21757
21758 if (left_padded && cmp->lbearing < 0)
21759 {
21760 for (i = 0; i < cmp->glyph_len; i++)
21761 cmp->offsets[i * 2] -= cmp->lbearing;
21762 rightmost -= cmp->lbearing;
21763 cmp->rbearing -= cmp->lbearing;
21764 cmp->lbearing = 0;
21765 }
21766 if (right_padded && rightmost < cmp->rbearing)
21767 {
21768 rightmost = cmp->rbearing;
21769 }
21770
21771 cmp->pixel_width = rightmost;
21772 cmp->ascent = highest;
21773 cmp->descent = - lowest;
21774 if (cmp->ascent < font_ascent)
21775 cmp->ascent = font_ascent;
21776 if (cmp->descent < font_descent)
21777 cmp->descent = font_descent;
21778 }
21779
21780 if (it->glyph_row
21781 && (cmp->lbearing < 0
21782 || cmp->rbearing > cmp->pixel_width))
21783 it->glyph_row->contains_overlapping_glyphs_p = 1;
21784
21785 it->pixel_width = cmp->pixel_width;
21786 it->ascent = it->phys_ascent = cmp->ascent;
21787 it->descent = it->phys_descent = cmp->descent;
21788 if (face->box != FACE_NO_BOX)
21789 {
21790 int thick = face->box_line_width;
21791
21792 if (thick > 0)
21793 {
21794 it->ascent += thick;
21795 it->descent += thick;
21796 }
21797 else
21798 thick = - thick;
21799
21800 if (it->start_of_box_run_p)
21801 it->pixel_width += thick;
21802 if (it->end_of_box_run_p)
21803 it->pixel_width += thick;
21804 }
21805
21806 /* If face has an overline, add the height of the overline
21807 (1 pixel) and a 1 pixel margin to the character height. */
21808 if (face->overline_p)
21809 it->ascent += overline_margin;
21810
21811 take_vertical_position_into_account (it);
21812 if (it->ascent < 0)
21813 it->ascent = 0;
21814 if (it->descent < 0)
21815 it->descent = 0;
21816
21817 if (it->glyph_row)
21818 append_composite_glyph (it);
21819 }
21820 else if (it->what == IT_COMPOSITION)
21821 {
21822 /* A dynamic (automatic) composition. */
21823 struct face *face = FACE_FROM_ID (it->f, it->face_id);
21824 Lisp_Object gstring;
21825 struct font_metrics metrics;
21826
21827 gstring = composition_gstring_from_id (it->cmp_it.id);
21828 it->pixel_width
21829 = composition_gstring_width (gstring, it->cmp_it.from, it->cmp_it.to,
21830 &metrics);
21831 if (it->glyph_row
21832 && (metrics.lbearing < 0 || metrics.rbearing > metrics.width))
21833 it->glyph_row->contains_overlapping_glyphs_p = 1;
21834 it->ascent = it->phys_ascent = metrics.ascent;
21835 it->descent = it->phys_descent = metrics.descent;
21836 if (face->box != FACE_NO_BOX)
21837 {
21838 int thick = face->box_line_width;
21839
21840 if (thick > 0)
21841 {
21842 it->ascent += thick;
21843 it->descent += thick;
21844 }
21845 else
21846 thick = - thick;
21847
21848 if (it->start_of_box_run_p)
21849 it->pixel_width += thick;
21850 if (it->end_of_box_run_p)
21851 it->pixel_width += thick;
21852 }
21853 /* If face has an overline, add the height of the overline
21854 (1 pixel) and a 1 pixel margin to the character height. */
21855 if (face->overline_p)
21856 it->ascent += overline_margin;
21857 take_vertical_position_into_account (it);
21858 if (it->ascent < 0)
21859 it->ascent = 0;
21860 if (it->descent < 0)
21861 it->descent = 0;
21862
21863 if (it->glyph_row)
21864 append_composite_glyph (it);
21865 }
21866 else if (it->what == IT_IMAGE)
21867 produce_image_glyph (it);
21868 else if (it->what == IT_STRETCH)
21869 produce_stretch_glyph (it);
21870
21871 /* Accumulate dimensions. Note: can't assume that it->descent > 0
21872 because this isn't true for images with `:ascent 100'. */
21873 xassert (it->ascent >= 0 && it->descent >= 0);
21874 if (it->area == TEXT_AREA)
21875 it->current_x += it->pixel_width;
21876
21877 if (extra_line_spacing > 0)
21878 {
21879 it->descent += extra_line_spacing;
21880 if (extra_line_spacing > it->max_extra_line_spacing)
21881 it->max_extra_line_spacing = extra_line_spacing;
21882 }
21883
21884 it->max_ascent = max (it->max_ascent, it->ascent);
21885 it->max_descent = max (it->max_descent, it->descent);
21886 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
21887 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
21888 }
21889
21890 /* EXPORT for RIF:
21891 Output LEN glyphs starting at START at the nominal cursor position.
21892 Advance the nominal cursor over the text. The global variable
21893 updated_window contains the window being updated, updated_row is
21894 the glyph row being updated, and updated_area is the area of that
21895 row being updated. */
21896
21897 void
21898 x_write_glyphs (start, len)
21899 struct glyph *start;
21900 int len;
21901 {
21902 int x, hpos;
21903
21904 xassert (updated_window && updated_row);
21905 BLOCK_INPUT;
21906
21907 /* Write glyphs. */
21908
21909 hpos = start - updated_row->glyphs[updated_area];
21910 x = draw_glyphs (updated_window, output_cursor.x,
21911 updated_row, updated_area,
21912 hpos, hpos + len,
21913 DRAW_NORMAL_TEXT, 0);
21914
21915 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
21916 if (updated_area == TEXT_AREA
21917 && updated_window->phys_cursor_on_p
21918 && updated_window->phys_cursor.vpos == output_cursor.vpos
21919 && updated_window->phys_cursor.hpos >= hpos
21920 && updated_window->phys_cursor.hpos < hpos + len)
21921 updated_window->phys_cursor_on_p = 0;
21922
21923 UNBLOCK_INPUT;
21924
21925 /* Advance the output cursor. */
21926 output_cursor.hpos += len;
21927 output_cursor.x = x;
21928 }
21929
21930
21931 /* EXPORT for RIF:
21932 Insert LEN glyphs from START at the nominal cursor position. */
21933
21934 void
21935 x_insert_glyphs (start, len)
21936 struct glyph *start;
21937 int len;
21938 {
21939 struct frame *f;
21940 struct window *w;
21941 int line_height, shift_by_width, shifted_region_width;
21942 struct glyph_row *row;
21943 struct glyph *glyph;
21944 int frame_x, frame_y;
21945 EMACS_INT hpos;
21946
21947 xassert (updated_window && updated_row);
21948 BLOCK_INPUT;
21949 w = updated_window;
21950 f = XFRAME (WINDOW_FRAME (w));
21951
21952 /* Get the height of the line we are in. */
21953 row = updated_row;
21954 line_height = row->height;
21955
21956 /* Get the width of the glyphs to insert. */
21957 shift_by_width = 0;
21958 for (glyph = start; glyph < start + len; ++glyph)
21959 shift_by_width += glyph->pixel_width;
21960
21961 /* Get the width of the region to shift right. */
21962 shifted_region_width = (window_box_width (w, updated_area)
21963 - output_cursor.x
21964 - shift_by_width);
21965
21966 /* Shift right. */
21967 frame_x = window_box_left (w, updated_area) + output_cursor.x;
21968 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
21969
21970 FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
21971 line_height, shift_by_width);
21972
21973 /* Write the glyphs. */
21974 hpos = start - row->glyphs[updated_area];
21975 draw_glyphs (w, output_cursor.x, row, updated_area,
21976 hpos, hpos + len,
21977 DRAW_NORMAL_TEXT, 0);
21978
21979 /* Advance the output cursor. */
21980 output_cursor.hpos += len;
21981 output_cursor.x += shift_by_width;
21982 UNBLOCK_INPUT;
21983 }
21984
21985
21986 /* EXPORT for RIF:
21987 Erase the current text line from the nominal cursor position
21988 (inclusive) to pixel column TO_X (exclusive). The idea is that
21989 everything from TO_X onward is already erased.
21990
21991 TO_X is a pixel position relative to updated_area of
21992 updated_window. TO_X == -1 means clear to the end of this area. */
21993
21994 void
21995 x_clear_end_of_line (to_x)
21996 int to_x;
21997 {
21998 struct frame *f;
21999 struct window *w = updated_window;
22000 int max_x, min_y, max_y;
22001 int from_x, from_y, to_y;
22002
22003 xassert (updated_window && updated_row);
22004 f = XFRAME (w->frame);
22005
22006 if (updated_row->full_width_p)
22007 max_x = WINDOW_TOTAL_WIDTH (w);
22008 else
22009 max_x = window_box_width (w, updated_area);
22010 max_y = window_text_bottom_y (w);
22011
22012 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
22013 of window. For TO_X > 0, truncate to end of drawing area. */
22014 if (to_x == 0)
22015 return;
22016 else if (to_x < 0)
22017 to_x = max_x;
22018 else
22019 to_x = min (to_x, max_x);
22020
22021 to_y = min (max_y, output_cursor.y + updated_row->height);
22022
22023 /* Notice if the cursor will be cleared by this operation. */
22024 if (!updated_row->full_width_p)
22025 notice_overwritten_cursor (w, updated_area,
22026 output_cursor.x, -1,
22027 updated_row->y,
22028 MATRIX_ROW_BOTTOM_Y (updated_row));
22029
22030 from_x = output_cursor.x;
22031
22032 /* Translate to frame coordinates. */
22033 if (updated_row->full_width_p)
22034 {
22035 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
22036 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
22037 }
22038 else
22039 {
22040 int area_left = window_box_left (w, updated_area);
22041 from_x += area_left;
22042 to_x += area_left;
22043 }
22044
22045 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
22046 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
22047 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
22048
22049 /* Prevent inadvertently clearing to end of the X window. */
22050 if (to_x > from_x && to_y > from_y)
22051 {
22052 BLOCK_INPUT;
22053 FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
22054 to_x - from_x, to_y - from_y);
22055 UNBLOCK_INPUT;
22056 }
22057 }
22058
22059 #endif /* HAVE_WINDOW_SYSTEM */
22060
22061
22062 \f
22063 /***********************************************************************
22064 Cursor types
22065 ***********************************************************************/
22066
22067 /* Value is the internal representation of the specified cursor type
22068 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
22069 of the bar cursor. */
22070
22071 static enum text_cursor_kinds
22072 get_specified_cursor_type (arg, width)
22073 Lisp_Object arg;
22074 int *width;
22075 {
22076 enum text_cursor_kinds type;
22077
22078 if (NILP (arg))
22079 return NO_CURSOR;
22080
22081 if (EQ (arg, Qbox))
22082 return FILLED_BOX_CURSOR;
22083
22084 if (EQ (arg, Qhollow))
22085 return HOLLOW_BOX_CURSOR;
22086
22087 if (EQ (arg, Qbar))
22088 {
22089 *width = 2;
22090 return BAR_CURSOR;
22091 }
22092
22093 if (CONSP (arg)
22094 && EQ (XCAR (arg), Qbar)
22095 && INTEGERP (XCDR (arg))
22096 && XINT (XCDR (arg)) >= 0)
22097 {
22098 *width = XINT (XCDR (arg));
22099 return BAR_CURSOR;
22100 }
22101
22102 if (EQ (arg, Qhbar))
22103 {
22104 *width = 2;
22105 return HBAR_CURSOR;
22106 }
22107
22108 if (CONSP (arg)
22109 && EQ (XCAR (arg), Qhbar)
22110 && INTEGERP (XCDR (arg))
22111 && XINT (XCDR (arg)) >= 0)
22112 {
22113 *width = XINT (XCDR (arg));
22114 return HBAR_CURSOR;
22115 }
22116
22117 /* Treat anything unknown as "hollow box cursor".
22118 It was bad to signal an error; people have trouble fixing
22119 .Xdefaults with Emacs, when it has something bad in it. */
22120 type = HOLLOW_BOX_CURSOR;
22121
22122 return type;
22123 }
22124
22125 /* Set the default cursor types for specified frame. */
22126 void
22127 set_frame_cursor_types (f, arg)
22128 struct frame *f;
22129 Lisp_Object arg;
22130 {
22131 int width;
22132 Lisp_Object tem;
22133
22134 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
22135 FRAME_CURSOR_WIDTH (f) = width;
22136
22137 /* By default, set up the blink-off state depending on the on-state. */
22138
22139 tem = Fassoc (arg, Vblink_cursor_alist);
22140 if (!NILP (tem))
22141 {
22142 FRAME_BLINK_OFF_CURSOR (f)
22143 = get_specified_cursor_type (XCDR (tem), &width);
22144 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
22145 }
22146 else
22147 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
22148 }
22149
22150
22151 /* Return the cursor we want to be displayed in window W. Return
22152 width of bar/hbar cursor through WIDTH arg. Return with
22153 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
22154 (i.e. if the `system caret' should track this cursor).
22155
22156 In a mini-buffer window, we want the cursor only to appear if we
22157 are reading input from this window. For the selected window, we
22158 want the cursor type given by the frame parameter or buffer local
22159 setting of cursor-type. If explicitly marked off, draw no cursor.
22160 In all other cases, we want a hollow box cursor. */
22161
22162 static enum text_cursor_kinds
22163 get_window_cursor_type (w, glyph, width, active_cursor)
22164 struct window *w;
22165 struct glyph *glyph;
22166 int *width;
22167 int *active_cursor;
22168 {
22169 struct frame *f = XFRAME (w->frame);
22170 struct buffer *b = XBUFFER (w->buffer);
22171 int cursor_type = DEFAULT_CURSOR;
22172 Lisp_Object alt_cursor;
22173 int non_selected = 0;
22174
22175 *active_cursor = 1;
22176
22177 /* Echo area */
22178 if (cursor_in_echo_area
22179 && FRAME_HAS_MINIBUF_P (f)
22180 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
22181 {
22182 if (w == XWINDOW (echo_area_window))
22183 {
22184 if (EQ (b->cursor_type, Qt) || NILP (b->cursor_type))
22185 {
22186 *width = FRAME_CURSOR_WIDTH (f);
22187 return FRAME_DESIRED_CURSOR (f);
22188 }
22189 else
22190 return get_specified_cursor_type (b->cursor_type, width);
22191 }
22192
22193 *active_cursor = 0;
22194 non_selected = 1;
22195 }
22196
22197 /* Detect a nonselected window or nonselected frame. */
22198 else if (w != XWINDOW (f->selected_window)
22199 #ifdef HAVE_WINDOW_SYSTEM
22200 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame
22201 #endif
22202 )
22203 {
22204 *active_cursor = 0;
22205
22206 if (MINI_WINDOW_P (w) && minibuf_level == 0)
22207 return NO_CURSOR;
22208
22209 non_selected = 1;
22210 }
22211
22212 /* Never display a cursor in a window in which cursor-type is nil. */
22213 if (NILP (b->cursor_type))
22214 return NO_CURSOR;
22215
22216 /* Get the normal cursor type for this window. */
22217 if (EQ (b->cursor_type, Qt))
22218 {
22219 cursor_type = FRAME_DESIRED_CURSOR (f);
22220 *width = FRAME_CURSOR_WIDTH (f);
22221 }
22222 else
22223 cursor_type = get_specified_cursor_type (b->cursor_type, width);
22224
22225 /* Use cursor-in-non-selected-windows instead
22226 for non-selected window or frame. */
22227 if (non_selected)
22228 {
22229 alt_cursor = b->cursor_in_non_selected_windows;
22230 if (!EQ (Qt, alt_cursor))
22231 return get_specified_cursor_type (alt_cursor, width);
22232 /* t means modify the normal cursor type. */
22233 if (cursor_type == FILLED_BOX_CURSOR)
22234 cursor_type = HOLLOW_BOX_CURSOR;
22235 else if (cursor_type == BAR_CURSOR && *width > 1)
22236 --*width;
22237 return cursor_type;
22238 }
22239
22240 /* Use normal cursor if not blinked off. */
22241 if (!w->cursor_off_p)
22242 {
22243 #ifdef HAVE_WINDOW_SYSTEM
22244 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
22245 {
22246 if (cursor_type == FILLED_BOX_CURSOR)
22247 {
22248 /* Using a block cursor on large images can be very annoying.
22249 So use a hollow cursor for "large" images.
22250 If image is not transparent (no mask), also use hollow cursor. */
22251 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
22252 if (img != NULL && IMAGEP (img->spec))
22253 {
22254 /* Arbitrarily, interpret "Large" as >32x32 and >NxN
22255 where N = size of default frame font size.
22256 This should cover most of the "tiny" icons people may use. */
22257 if (!img->mask
22258 || img->width > max (32, WINDOW_FRAME_COLUMN_WIDTH (w))
22259 || img->height > max (32, WINDOW_FRAME_LINE_HEIGHT (w)))
22260 cursor_type = HOLLOW_BOX_CURSOR;
22261 }
22262 }
22263 else if (cursor_type != NO_CURSOR)
22264 {
22265 /* Display current only supports BOX and HOLLOW cursors for images.
22266 So for now, unconditionally use a HOLLOW cursor when cursor is
22267 not a solid box cursor. */
22268 cursor_type = HOLLOW_BOX_CURSOR;
22269 }
22270 }
22271 #endif
22272 return cursor_type;
22273 }
22274
22275 /* Cursor is blinked off, so determine how to "toggle" it. */
22276
22277 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
22278 if ((alt_cursor = Fassoc (b->cursor_type, Vblink_cursor_alist), !NILP (alt_cursor)))
22279 return get_specified_cursor_type (XCDR (alt_cursor), width);
22280
22281 /* Then see if frame has specified a specific blink off cursor type. */
22282 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
22283 {
22284 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
22285 return FRAME_BLINK_OFF_CURSOR (f);
22286 }
22287
22288 #if 0
22289 /* Some people liked having a permanently visible blinking cursor,
22290 while others had very strong opinions against it. So it was
22291 decided to remove it. KFS 2003-09-03 */
22292
22293 /* Finally perform built-in cursor blinking:
22294 filled box <-> hollow box
22295 wide [h]bar <-> narrow [h]bar
22296 narrow [h]bar <-> no cursor
22297 other type <-> no cursor */
22298
22299 if (cursor_type == FILLED_BOX_CURSOR)
22300 return HOLLOW_BOX_CURSOR;
22301
22302 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
22303 {
22304 *width = 1;
22305 return cursor_type;
22306 }
22307 #endif
22308
22309 return NO_CURSOR;
22310 }
22311
22312
22313 #ifdef HAVE_WINDOW_SYSTEM
22314
22315 /* Notice when the text cursor of window W has been completely
22316 overwritten by a drawing operation that outputs glyphs in AREA
22317 starting at X0 and ending at X1 in the line starting at Y0 and
22318 ending at Y1. X coordinates are area-relative. X1 < 0 means all
22319 the rest of the line after X0 has been written. Y coordinates
22320 are window-relative. */
22321
22322 static void
22323 notice_overwritten_cursor (w, area, x0, x1, y0, y1)
22324 struct window *w;
22325 enum glyph_row_area area;
22326 int x0, y0, x1, y1;
22327 {
22328 int cx0, cx1, cy0, cy1;
22329 struct glyph_row *row;
22330
22331 if (!w->phys_cursor_on_p)
22332 return;
22333 if (area != TEXT_AREA)
22334 return;
22335
22336 if (w->phys_cursor.vpos < 0
22337 || w->phys_cursor.vpos >= w->current_matrix->nrows
22338 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
22339 !(row->enabled_p && row->displays_text_p)))
22340 return;
22341
22342 if (row->cursor_in_fringe_p)
22343 {
22344 row->cursor_in_fringe_p = 0;
22345 draw_fringe_bitmap (w, row, 0);
22346 w->phys_cursor_on_p = 0;
22347 return;
22348 }
22349
22350 cx0 = w->phys_cursor.x;
22351 cx1 = cx0 + w->phys_cursor_width;
22352 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
22353 return;
22354
22355 /* The cursor image will be completely removed from the
22356 screen if the output area intersects the cursor area in
22357 y-direction. When we draw in [y0 y1[, and some part of
22358 the cursor is at y < y0, that part must have been drawn
22359 before. When scrolling, the cursor is erased before
22360 actually scrolling, so we don't come here. When not
22361 scrolling, the rows above the old cursor row must have
22362 changed, and in this case these rows must have written
22363 over the cursor image.
22364
22365 Likewise if part of the cursor is below y1, with the
22366 exception of the cursor being in the first blank row at
22367 the buffer and window end because update_text_area
22368 doesn't draw that row. (Except when it does, but
22369 that's handled in update_text_area.) */
22370
22371 cy0 = w->phys_cursor.y;
22372 cy1 = cy0 + w->phys_cursor_height;
22373 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
22374 return;
22375
22376 w->phys_cursor_on_p = 0;
22377 }
22378
22379 #endif /* HAVE_WINDOW_SYSTEM */
22380
22381 \f
22382 /************************************************************************
22383 Mouse Face
22384 ************************************************************************/
22385
22386 #ifdef HAVE_WINDOW_SYSTEM
22387
22388 /* EXPORT for RIF:
22389 Fix the display of area AREA of overlapping row ROW in window W
22390 with respect to the overlapping part OVERLAPS. */
22391
22392 void
22393 x_fix_overlapping_area (w, row, area, overlaps)
22394 struct window *w;
22395 struct glyph_row *row;
22396 enum glyph_row_area area;
22397 int overlaps;
22398 {
22399 int i, x;
22400
22401 BLOCK_INPUT;
22402
22403 x = 0;
22404 for (i = 0; i < row->used[area];)
22405 {
22406 if (row->glyphs[area][i].overlaps_vertically_p)
22407 {
22408 int start = i, start_x = x;
22409
22410 do
22411 {
22412 x += row->glyphs[area][i].pixel_width;
22413 ++i;
22414 }
22415 while (i < row->used[area]
22416 && row->glyphs[area][i].overlaps_vertically_p);
22417
22418 draw_glyphs (w, start_x, row, area,
22419 start, i,
22420 DRAW_NORMAL_TEXT, overlaps);
22421 }
22422 else
22423 {
22424 x += row->glyphs[area][i].pixel_width;
22425 ++i;
22426 }
22427 }
22428
22429 UNBLOCK_INPUT;
22430 }
22431
22432
22433 /* EXPORT:
22434 Draw the cursor glyph of window W in glyph row ROW. See the
22435 comment of draw_glyphs for the meaning of HL. */
22436
22437 void
22438 draw_phys_cursor_glyph (w, row, hl)
22439 struct window *w;
22440 struct glyph_row *row;
22441 enum draw_glyphs_face hl;
22442 {
22443 /* If cursor hpos is out of bounds, don't draw garbage. This can
22444 happen in mini-buffer windows when switching between echo area
22445 glyphs and mini-buffer. */
22446 if (w->phys_cursor.hpos < row->used[TEXT_AREA])
22447 {
22448 int on_p = w->phys_cursor_on_p;
22449 int x1;
22450 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA,
22451 w->phys_cursor.hpos, w->phys_cursor.hpos + 1,
22452 hl, 0);
22453 w->phys_cursor_on_p = on_p;
22454
22455 if (hl == DRAW_CURSOR)
22456 w->phys_cursor_width = x1 - w->phys_cursor.x;
22457 /* When we erase the cursor, and ROW is overlapped by other
22458 rows, make sure that these overlapping parts of other rows
22459 are redrawn. */
22460 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
22461 {
22462 w->phys_cursor_width = x1 - w->phys_cursor.x;
22463
22464 if (row > w->current_matrix->rows
22465 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
22466 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
22467 OVERLAPS_ERASED_CURSOR);
22468
22469 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
22470 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
22471 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
22472 OVERLAPS_ERASED_CURSOR);
22473 }
22474 }
22475 }
22476
22477
22478 /* EXPORT:
22479 Erase the image of a cursor of window W from the screen. */
22480
22481 void
22482 erase_phys_cursor (w)
22483 struct window *w;
22484 {
22485 struct frame *f = XFRAME (w->frame);
22486 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
22487 int hpos = w->phys_cursor.hpos;
22488 int vpos = w->phys_cursor.vpos;
22489 int mouse_face_here_p = 0;
22490 struct glyph_matrix *active_glyphs = w->current_matrix;
22491 struct glyph_row *cursor_row;
22492 struct glyph *cursor_glyph;
22493 enum draw_glyphs_face hl;
22494
22495 /* No cursor displayed or row invalidated => nothing to do on the
22496 screen. */
22497 if (w->phys_cursor_type == NO_CURSOR)
22498 goto mark_cursor_off;
22499
22500 /* VPOS >= active_glyphs->nrows means that window has been resized.
22501 Don't bother to erase the cursor. */
22502 if (vpos >= active_glyphs->nrows)
22503 goto mark_cursor_off;
22504
22505 /* If row containing cursor is marked invalid, there is nothing we
22506 can do. */
22507 cursor_row = MATRIX_ROW (active_glyphs, vpos);
22508 if (!cursor_row->enabled_p)
22509 goto mark_cursor_off;
22510
22511 /* If line spacing is > 0, old cursor may only be partially visible in
22512 window after split-window. So adjust visible height. */
22513 cursor_row->visible_height = min (cursor_row->visible_height,
22514 window_text_bottom_y (w) - cursor_row->y);
22515
22516 /* If row is completely invisible, don't attempt to delete a cursor which
22517 isn't there. This can happen if cursor is at top of a window, and
22518 we switch to a buffer with a header line in that window. */
22519 if (cursor_row->visible_height <= 0)
22520 goto mark_cursor_off;
22521
22522 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
22523 if (cursor_row->cursor_in_fringe_p)
22524 {
22525 cursor_row->cursor_in_fringe_p = 0;
22526 draw_fringe_bitmap (w, cursor_row, 0);
22527 goto mark_cursor_off;
22528 }
22529
22530 /* This can happen when the new row is shorter than the old one.
22531 In this case, either draw_glyphs or clear_end_of_line
22532 should have cleared the cursor. Note that we wouldn't be
22533 able to erase the cursor in this case because we don't have a
22534 cursor glyph at hand. */
22535 if (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])
22536 goto mark_cursor_off;
22537
22538 /* If the cursor is in the mouse face area, redisplay that when
22539 we clear the cursor. */
22540 if (! NILP (dpyinfo->mouse_face_window)
22541 && w == XWINDOW (dpyinfo->mouse_face_window)
22542 && (vpos > dpyinfo->mouse_face_beg_row
22543 || (vpos == dpyinfo->mouse_face_beg_row
22544 && hpos >= dpyinfo->mouse_face_beg_col))
22545 && (vpos < dpyinfo->mouse_face_end_row
22546 || (vpos == dpyinfo->mouse_face_end_row
22547 && hpos < dpyinfo->mouse_face_end_col))
22548 /* Don't redraw the cursor's spot in mouse face if it is at the
22549 end of a line (on a newline). The cursor appears there, but
22550 mouse highlighting does not. */
22551 && cursor_row->used[TEXT_AREA] > hpos)
22552 mouse_face_here_p = 1;
22553
22554 /* Maybe clear the display under the cursor. */
22555 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
22556 {
22557 int x, y, left_x;
22558 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
22559 int width;
22560
22561 cursor_glyph = get_phys_cursor_glyph (w);
22562 if (cursor_glyph == NULL)
22563 goto mark_cursor_off;
22564
22565 width = cursor_glyph->pixel_width;
22566 left_x = window_box_left_offset (w, TEXT_AREA);
22567 x = w->phys_cursor.x;
22568 if (x < left_x)
22569 width -= left_x - x;
22570 width = min (width, window_box_width (w, TEXT_AREA) - x);
22571 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
22572 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, max (x, left_x));
22573
22574 if (width > 0)
22575 FRAME_RIF (f)->clear_frame_area (f, x, y, width, cursor_row->visible_height);
22576 }
22577
22578 /* Erase the cursor by redrawing the character underneath it. */
22579 if (mouse_face_here_p)
22580 hl = DRAW_MOUSE_FACE;
22581 else
22582 hl = DRAW_NORMAL_TEXT;
22583 draw_phys_cursor_glyph (w, cursor_row, hl);
22584
22585 mark_cursor_off:
22586 w->phys_cursor_on_p = 0;
22587 w->phys_cursor_type = NO_CURSOR;
22588 }
22589
22590
22591 /* EXPORT:
22592 Display or clear cursor of window W. If ON is zero, clear the
22593 cursor. If it is non-zero, display the cursor. If ON is nonzero,
22594 where to put the cursor is specified by HPOS, VPOS, X and Y. */
22595
22596 void
22597 display_and_set_cursor (w, on, hpos, vpos, x, y)
22598 struct window *w;
22599 int on, hpos, vpos, x, y;
22600 {
22601 struct frame *f = XFRAME (w->frame);
22602 int new_cursor_type;
22603 int new_cursor_width;
22604 int active_cursor;
22605 struct glyph_row *glyph_row;
22606 struct glyph *glyph;
22607
22608 /* This is pointless on invisible frames, and dangerous on garbaged
22609 windows and frames; in the latter case, the frame or window may
22610 be in the midst of changing its size, and x and y may be off the
22611 window. */
22612 if (! FRAME_VISIBLE_P (f)
22613 || FRAME_GARBAGED_P (f)
22614 || vpos >= w->current_matrix->nrows
22615 || hpos >= w->current_matrix->matrix_w)
22616 return;
22617
22618 /* If cursor is off and we want it off, return quickly. */
22619 if (!on && !w->phys_cursor_on_p)
22620 return;
22621
22622 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
22623 /* If cursor row is not enabled, we don't really know where to
22624 display the cursor. */
22625 if (!glyph_row->enabled_p)
22626 {
22627 w->phys_cursor_on_p = 0;
22628 return;
22629 }
22630
22631 glyph = NULL;
22632 if (!glyph_row->exact_window_width_line_p
22633 || hpos < glyph_row->used[TEXT_AREA])
22634 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
22635
22636 xassert (interrupt_input_blocked);
22637
22638 /* Set new_cursor_type to the cursor we want to be displayed. */
22639 new_cursor_type = get_window_cursor_type (w, glyph,
22640 &new_cursor_width, &active_cursor);
22641
22642 /* If cursor is currently being shown and we don't want it to be or
22643 it is in the wrong place, or the cursor type is not what we want,
22644 erase it. */
22645 if (w->phys_cursor_on_p
22646 && (!on
22647 || w->phys_cursor.x != x
22648 || w->phys_cursor.y != y
22649 || new_cursor_type != w->phys_cursor_type
22650 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
22651 && new_cursor_width != w->phys_cursor_width)))
22652 erase_phys_cursor (w);
22653
22654 /* Don't check phys_cursor_on_p here because that flag is only set
22655 to zero in some cases where we know that the cursor has been
22656 completely erased, to avoid the extra work of erasing the cursor
22657 twice. In other words, phys_cursor_on_p can be 1 and the cursor
22658 still not be visible, or it has only been partly erased. */
22659 if (on)
22660 {
22661 w->phys_cursor_ascent = glyph_row->ascent;
22662 w->phys_cursor_height = glyph_row->height;
22663
22664 /* Set phys_cursor_.* before x_draw_.* is called because some
22665 of them may need the information. */
22666 w->phys_cursor.x = x;
22667 w->phys_cursor.y = glyph_row->y;
22668 w->phys_cursor.hpos = hpos;
22669 w->phys_cursor.vpos = vpos;
22670 }
22671
22672 FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
22673 new_cursor_type, new_cursor_width,
22674 on, active_cursor);
22675 }
22676
22677
22678 /* Switch the display of W's cursor on or off, according to the value
22679 of ON. */
22680
22681 #ifndef HAVE_NS
22682 static
22683 #endif
22684 void
22685 update_window_cursor (w, on)
22686 struct window *w;
22687 int on;
22688 {
22689 /* Don't update cursor in windows whose frame is in the process
22690 of being deleted. */
22691 if (w->current_matrix)
22692 {
22693 BLOCK_INPUT;
22694 display_and_set_cursor (w, on, w->phys_cursor.hpos, w->phys_cursor.vpos,
22695 w->phys_cursor.x, w->phys_cursor.y);
22696 UNBLOCK_INPUT;
22697 }
22698 }
22699
22700
22701 /* Call update_window_cursor with parameter ON_P on all leaf windows
22702 in the window tree rooted at W. */
22703
22704 static void
22705 update_cursor_in_window_tree (w, on_p)
22706 struct window *w;
22707 int on_p;
22708 {
22709 while (w)
22710 {
22711 if (!NILP (w->hchild))
22712 update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
22713 else if (!NILP (w->vchild))
22714 update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
22715 else
22716 update_window_cursor (w, on_p);
22717
22718 w = NILP (w->next) ? 0 : XWINDOW (w->next);
22719 }
22720 }
22721
22722
22723 /* EXPORT:
22724 Display the cursor on window W, or clear it, according to ON_P.
22725 Don't change the cursor's position. */
22726
22727 void
22728 x_update_cursor (f, on_p)
22729 struct frame *f;
22730 int on_p;
22731 {
22732 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
22733 }
22734
22735
22736 /* EXPORT:
22737 Clear the cursor of window W to background color, and mark the
22738 cursor as not shown. This is used when the text where the cursor
22739 is is about to be rewritten. */
22740
22741 void
22742 x_clear_cursor (w)
22743 struct window *w;
22744 {
22745 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
22746 update_window_cursor (w, 0);
22747 }
22748
22749
22750 /* EXPORT:
22751 Display the active region described by mouse_face_* according to DRAW. */
22752
22753 void
22754 show_mouse_face (dpyinfo, draw)
22755 Display_Info *dpyinfo;
22756 enum draw_glyphs_face draw;
22757 {
22758 struct window *w = XWINDOW (dpyinfo->mouse_face_window);
22759 struct frame *f = XFRAME (WINDOW_FRAME (w));
22760
22761 if (/* If window is in the process of being destroyed, don't bother
22762 to do anything. */
22763 w->current_matrix != NULL
22764 /* Don't update mouse highlight if hidden */
22765 && (draw != DRAW_MOUSE_FACE || !dpyinfo->mouse_face_hidden)
22766 /* Recognize when we are called to operate on rows that don't exist
22767 anymore. This can happen when a window is split. */
22768 && dpyinfo->mouse_face_end_row < w->current_matrix->nrows)
22769 {
22770 int phys_cursor_on_p = w->phys_cursor_on_p;
22771 struct glyph_row *row, *first, *last;
22772
22773 first = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_beg_row);
22774 last = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_end_row);
22775
22776 for (row = first; row <= last && row->enabled_p; ++row)
22777 {
22778 int start_hpos, end_hpos, start_x;
22779
22780 /* For all but the first row, the highlight starts at column 0. */
22781 if (row == first)
22782 {
22783 start_hpos = dpyinfo->mouse_face_beg_col;
22784 start_x = dpyinfo->mouse_face_beg_x;
22785 }
22786 else
22787 {
22788 start_hpos = 0;
22789 start_x = 0;
22790 }
22791
22792 if (row == last)
22793 end_hpos = dpyinfo->mouse_face_end_col;
22794 else
22795 {
22796 end_hpos = row->used[TEXT_AREA];
22797 if (draw == DRAW_NORMAL_TEXT)
22798 row->fill_line_p = 1; /* Clear to end of line */
22799 }
22800
22801 if (end_hpos > start_hpos)
22802 {
22803 draw_glyphs (w, start_x, row, TEXT_AREA,
22804 start_hpos, end_hpos,
22805 draw, 0);
22806
22807 row->mouse_face_p
22808 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
22809 }
22810 }
22811
22812 /* When we've written over the cursor, arrange for it to
22813 be displayed again. */
22814 if (phys_cursor_on_p && !w->phys_cursor_on_p)
22815 {
22816 BLOCK_INPUT;
22817 display_and_set_cursor (w, 1,
22818 w->phys_cursor.hpos, w->phys_cursor.vpos,
22819 w->phys_cursor.x, w->phys_cursor.y);
22820 UNBLOCK_INPUT;
22821 }
22822 }
22823
22824 /* Change the mouse cursor. */
22825 if (draw == DRAW_NORMAL_TEXT && !EQ (dpyinfo->mouse_face_window, f->tool_bar_window))
22826 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
22827 else if (draw == DRAW_MOUSE_FACE)
22828 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
22829 else
22830 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
22831 }
22832
22833 /* EXPORT:
22834 Clear out the mouse-highlighted active region.
22835 Redraw it un-highlighted first. Value is non-zero if mouse
22836 face was actually drawn unhighlighted. */
22837
22838 int
22839 clear_mouse_face (dpyinfo)
22840 Display_Info *dpyinfo;
22841 {
22842 int cleared = 0;
22843
22844 if (!dpyinfo->mouse_face_hidden && !NILP (dpyinfo->mouse_face_window))
22845 {
22846 show_mouse_face (dpyinfo, DRAW_NORMAL_TEXT);
22847 cleared = 1;
22848 }
22849
22850 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
22851 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
22852 dpyinfo->mouse_face_window = Qnil;
22853 dpyinfo->mouse_face_overlay = Qnil;
22854 return cleared;
22855 }
22856
22857
22858 /* EXPORT:
22859 Non-zero if physical cursor of window W is within mouse face. */
22860
22861 int
22862 cursor_in_mouse_face_p (w)
22863 struct window *w;
22864 {
22865 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
22866 int in_mouse_face = 0;
22867
22868 if (WINDOWP (dpyinfo->mouse_face_window)
22869 && XWINDOW (dpyinfo->mouse_face_window) == w)
22870 {
22871 int hpos = w->phys_cursor.hpos;
22872 int vpos = w->phys_cursor.vpos;
22873
22874 if (vpos >= dpyinfo->mouse_face_beg_row
22875 && vpos <= dpyinfo->mouse_face_end_row
22876 && (vpos > dpyinfo->mouse_face_beg_row
22877 || hpos >= dpyinfo->mouse_face_beg_col)
22878 && (vpos < dpyinfo->mouse_face_end_row
22879 || hpos < dpyinfo->mouse_face_end_col
22880 || dpyinfo->mouse_face_past_end))
22881 in_mouse_face = 1;
22882 }
22883
22884 return in_mouse_face;
22885 }
22886
22887
22888
22889 \f
22890 /* Find the glyph matrix position of buffer position CHARPOS in window
22891 *W. HPOS, *VPOS, *X, and *Y are set to the positions found. W's
22892 current glyphs must be up to date. If CHARPOS is above window
22893 start return (0, 0, 0, 0). If CHARPOS is after end of W, return end
22894 of last line in W. In the row containing CHARPOS, stop before glyphs
22895 having STOP as object. */
22896
22897 #if 1 /* This is a version of fast_find_position that's more correct
22898 in the presence of hscrolling, for example. I didn't install
22899 it right away because the problem fixed is minor, it failed
22900 in 20.x as well, and I think it's too risky to install
22901 so near the release of 21.1. 2001-09-25 gerd. */
22902
22903 static
22904 int
22905 fast_find_position (w, charpos, hpos, vpos, x, y, stop)
22906 struct window *w;
22907 EMACS_INT charpos;
22908 int *hpos, *vpos, *x, *y;
22909 Lisp_Object stop;
22910 {
22911 struct glyph_row *row, *first;
22912 struct glyph *glyph, *end;
22913 int past_end = 0;
22914
22915 first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
22916 if (charpos < MATRIX_ROW_START_CHARPOS (first))
22917 {
22918 *x = first->x;
22919 *y = first->y;
22920 *hpos = 0;
22921 *vpos = MATRIX_ROW_VPOS (first, w->current_matrix);
22922 return 1;
22923 }
22924
22925 row = row_containing_pos (w, charpos, first, NULL, 0);
22926 if (row == NULL)
22927 {
22928 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
22929 past_end = 1;
22930 }
22931
22932 /* If whole rows or last part of a row came from a display overlay,
22933 row_containing_pos will skip over such rows because their end pos
22934 equals the start pos of the overlay or interval.
22935
22936 Move back if we have a STOP object and previous row's
22937 end glyph came from STOP. */
22938 if (!NILP (stop))
22939 {
22940 struct glyph_row *prev;
22941 while ((prev = row - 1, prev >= first)
22942 && MATRIX_ROW_END_CHARPOS (prev) == charpos
22943 && prev->used[TEXT_AREA] > 0)
22944 {
22945 struct glyph *beg = prev->glyphs[TEXT_AREA];
22946 glyph = beg + prev->used[TEXT_AREA];
22947 while (--glyph >= beg
22948 && INTEGERP (glyph->object));
22949 if (glyph < beg
22950 || !EQ (stop, glyph->object))
22951 break;
22952 row = prev;
22953 }
22954 }
22955
22956 *x = row->x;
22957 *y = row->y;
22958 *vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
22959
22960 glyph = row->glyphs[TEXT_AREA];
22961 end = glyph + row->used[TEXT_AREA];
22962
22963 /* Skip over glyphs not having an object at the start of the row.
22964 These are special glyphs like truncation marks on terminal
22965 frames. */
22966 if (row->displays_text_p)
22967 while (glyph < end
22968 && INTEGERP (glyph->object)
22969 && !EQ (stop, glyph->object)
22970 && glyph->charpos < 0)
22971 {
22972 *x += glyph->pixel_width;
22973 ++glyph;
22974 }
22975
22976 while (glyph < end
22977 && !INTEGERP (glyph->object)
22978 && !EQ (stop, glyph->object)
22979 && (!BUFFERP (glyph->object)
22980 || glyph->charpos < charpos))
22981 {
22982 *x += glyph->pixel_width;
22983 ++glyph;
22984 }
22985
22986 *hpos = glyph - row->glyphs[TEXT_AREA];
22987 return !past_end;
22988 }
22989
22990 #else /* not 1 */
22991
22992 static int
22993 fast_find_position (w, pos, hpos, vpos, x, y, stop)
22994 struct window *w;
22995 EMACS_INT pos;
22996 int *hpos, *vpos, *x, *y;
22997 Lisp_Object stop;
22998 {
22999 int i;
23000 int lastcol;
23001 int maybe_next_line_p = 0;
23002 int line_start_position;
23003 int yb = window_text_bottom_y (w);
23004 struct glyph_row *row, *best_row;
23005 int row_vpos, best_row_vpos;
23006 int current_x;
23007
23008 row = best_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
23009 row_vpos = best_row_vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
23010
23011 while (row->y < yb)
23012 {
23013 if (row->used[TEXT_AREA])
23014 line_start_position = row->glyphs[TEXT_AREA]->charpos;
23015 else
23016 line_start_position = 0;
23017
23018 if (line_start_position > pos)
23019 break;
23020 /* If the position sought is the end of the buffer,
23021 don't include the blank lines at the bottom of the window. */
23022 else if (line_start_position == pos
23023 && pos == BUF_ZV (XBUFFER (w->buffer)))
23024 {
23025 maybe_next_line_p = 1;
23026 break;
23027 }
23028 else if (line_start_position > 0)
23029 {
23030 best_row = row;
23031 best_row_vpos = row_vpos;
23032 }
23033
23034 if (row->y + row->height >= yb)
23035 break;
23036
23037 ++row;
23038 ++row_vpos;
23039 }
23040
23041 /* Find the right column within BEST_ROW. */
23042 lastcol = 0;
23043 current_x = best_row->x;
23044 for (i = 0; i < best_row->used[TEXT_AREA]; i++)
23045 {
23046 struct glyph *glyph = best_row->glyphs[TEXT_AREA] + i;
23047 int charpos = glyph->charpos;
23048
23049 if (BUFFERP (glyph->object))
23050 {
23051 if (charpos == pos)
23052 {
23053 *hpos = i;
23054 *vpos = best_row_vpos;
23055 *x = current_x;
23056 *y = best_row->y;
23057 return 1;
23058 }
23059 else if (charpos > pos)
23060 break;
23061 }
23062 else if (EQ (glyph->object, stop))
23063 break;
23064
23065 if (charpos > 0)
23066 lastcol = i;
23067 current_x += glyph->pixel_width;
23068 }
23069
23070 /* If we're looking for the end of the buffer,
23071 and we didn't find it in the line we scanned,
23072 use the start of the following line. */
23073 if (maybe_next_line_p)
23074 {
23075 ++best_row;
23076 ++best_row_vpos;
23077 lastcol = 0;
23078 current_x = best_row->x;
23079 }
23080
23081 *vpos = best_row_vpos;
23082 *hpos = lastcol + 1;
23083 *x = current_x;
23084 *y = best_row->y;
23085 return 0;
23086 }
23087
23088 #endif /* not 1 */
23089
23090
23091 /* Find the position of the glyph for position POS in OBJECT in
23092 window W's current matrix, and return in *X, *Y the pixel
23093 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
23094
23095 RIGHT_P non-zero means return the position of the right edge of the
23096 glyph, RIGHT_P zero means return the left edge position.
23097
23098 If no glyph for POS exists in the matrix, return the position of
23099 the glyph with the next smaller position that is in the matrix, if
23100 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
23101 exists in the matrix, return the position of the glyph with the
23102 next larger position in OBJECT.
23103
23104 Value is non-zero if a glyph was found. */
23105
23106 static int
23107 fast_find_string_pos (w, pos, object, hpos, vpos, x, y, right_p)
23108 struct window *w;
23109 EMACS_INT pos;
23110 Lisp_Object object;
23111 int *hpos, *vpos, *x, *y;
23112 int right_p;
23113 {
23114 int yb = window_text_bottom_y (w);
23115 struct glyph_row *r;
23116 struct glyph *best_glyph = NULL;
23117 struct glyph_row *best_row = NULL;
23118 int best_x = 0;
23119
23120 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
23121 r->enabled_p && r->y < yb;
23122 ++r)
23123 {
23124 struct glyph *g = r->glyphs[TEXT_AREA];
23125 struct glyph *e = g + r->used[TEXT_AREA];
23126 int gx;
23127
23128 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
23129 if (EQ (g->object, object))
23130 {
23131 if (g->charpos == pos)
23132 {
23133 best_glyph = g;
23134 best_x = gx;
23135 best_row = r;
23136 goto found;
23137 }
23138 else if (best_glyph == NULL
23139 || ((eabs (g->charpos - pos)
23140 < eabs (best_glyph->charpos - pos))
23141 && (right_p
23142 ? g->charpos < pos
23143 : g->charpos > pos)))
23144 {
23145 best_glyph = g;
23146 best_x = gx;
23147 best_row = r;
23148 }
23149 }
23150 }
23151
23152 found:
23153
23154 if (best_glyph)
23155 {
23156 *x = best_x;
23157 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
23158
23159 if (right_p)
23160 {
23161 *x += best_glyph->pixel_width;
23162 ++*hpos;
23163 }
23164
23165 *y = best_row->y;
23166 *vpos = best_row - w->current_matrix->rows;
23167 }
23168
23169 return best_glyph != NULL;
23170 }
23171
23172
23173 /* See if position X, Y is within a hot-spot of an image. */
23174
23175 static int
23176 on_hot_spot_p (hot_spot, x, y)
23177 Lisp_Object hot_spot;
23178 int x, y;
23179 {
23180 if (!CONSP (hot_spot))
23181 return 0;
23182
23183 if (EQ (XCAR (hot_spot), Qrect))
23184 {
23185 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
23186 Lisp_Object rect = XCDR (hot_spot);
23187 Lisp_Object tem;
23188 if (!CONSP (rect))
23189 return 0;
23190 if (!CONSP (XCAR (rect)))
23191 return 0;
23192 if (!CONSP (XCDR (rect)))
23193 return 0;
23194 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
23195 return 0;
23196 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
23197 return 0;
23198 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
23199 return 0;
23200 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
23201 return 0;
23202 return 1;
23203 }
23204 else if (EQ (XCAR (hot_spot), Qcircle))
23205 {
23206 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
23207 Lisp_Object circ = XCDR (hot_spot);
23208 Lisp_Object lr, lx0, ly0;
23209 if (CONSP (circ)
23210 && CONSP (XCAR (circ))
23211 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
23212 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
23213 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
23214 {
23215 double r = XFLOATINT (lr);
23216 double dx = XINT (lx0) - x;
23217 double dy = XINT (ly0) - y;
23218 return (dx * dx + dy * dy <= r * r);
23219 }
23220 }
23221 else if (EQ (XCAR (hot_spot), Qpoly))
23222 {
23223 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
23224 if (VECTORP (XCDR (hot_spot)))
23225 {
23226 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
23227 Lisp_Object *poly = v->contents;
23228 int n = v->size;
23229 int i;
23230 int inside = 0;
23231 Lisp_Object lx, ly;
23232 int x0, y0;
23233
23234 /* Need an even number of coordinates, and at least 3 edges. */
23235 if (n < 6 || n & 1)
23236 return 0;
23237
23238 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
23239 If count is odd, we are inside polygon. Pixels on edges
23240 may or may not be included depending on actual geometry of the
23241 polygon. */
23242 if ((lx = poly[n-2], !INTEGERP (lx))
23243 || (ly = poly[n-1], !INTEGERP (lx)))
23244 return 0;
23245 x0 = XINT (lx), y0 = XINT (ly);
23246 for (i = 0; i < n; i += 2)
23247 {
23248 int x1 = x0, y1 = y0;
23249 if ((lx = poly[i], !INTEGERP (lx))
23250 || (ly = poly[i+1], !INTEGERP (ly)))
23251 return 0;
23252 x0 = XINT (lx), y0 = XINT (ly);
23253
23254 /* Does this segment cross the X line? */
23255 if (x0 >= x)
23256 {
23257 if (x1 >= x)
23258 continue;
23259 }
23260 else if (x1 < x)
23261 continue;
23262 if (y > y0 && y > y1)
23263 continue;
23264 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
23265 inside = !inside;
23266 }
23267 return inside;
23268 }
23269 }
23270 return 0;
23271 }
23272
23273 Lisp_Object
23274 find_hot_spot (map, x, y)
23275 Lisp_Object map;
23276 int x, y;
23277 {
23278 while (CONSP (map))
23279 {
23280 if (CONSP (XCAR (map))
23281 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
23282 return XCAR (map);
23283 map = XCDR (map);
23284 }
23285
23286 return Qnil;
23287 }
23288
23289 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
23290 3, 3, 0,
23291 doc: /* Lookup in image map MAP coordinates X and Y.
23292 An image map is an alist where each element has the format (AREA ID PLIST).
23293 An AREA is specified as either a rectangle, a circle, or a polygon:
23294 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
23295 pixel coordinates of the upper left and bottom right corners.
23296 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
23297 and the radius of the circle; r may be a float or integer.
23298 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
23299 vector describes one corner in the polygon.
23300 Returns the alist element for the first matching AREA in MAP. */)
23301 (map, x, y)
23302 Lisp_Object map;
23303 Lisp_Object x, y;
23304 {
23305 if (NILP (map))
23306 return Qnil;
23307
23308 CHECK_NUMBER (x);
23309 CHECK_NUMBER (y);
23310
23311 return find_hot_spot (map, XINT (x), XINT (y));
23312 }
23313
23314
23315 /* Display frame CURSOR, optionally using shape defined by POINTER. */
23316 static void
23317 define_frame_cursor1 (f, cursor, pointer)
23318 struct frame *f;
23319 Cursor cursor;
23320 Lisp_Object pointer;
23321 {
23322 /* Do not change cursor shape while dragging mouse. */
23323 if (!NILP (do_mouse_tracking))
23324 return;
23325
23326 if (!NILP (pointer))
23327 {
23328 if (EQ (pointer, Qarrow))
23329 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
23330 else if (EQ (pointer, Qhand))
23331 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
23332 else if (EQ (pointer, Qtext))
23333 cursor = FRAME_X_OUTPUT (f)->text_cursor;
23334 else if (EQ (pointer, intern ("hdrag")))
23335 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
23336 #ifdef HAVE_X_WINDOWS
23337 else if (EQ (pointer, intern ("vdrag")))
23338 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
23339 #endif
23340 else if (EQ (pointer, intern ("hourglass")))
23341 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
23342 else if (EQ (pointer, Qmodeline))
23343 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
23344 else
23345 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
23346 }
23347
23348 if (cursor != No_Cursor)
23349 FRAME_RIF (f)->define_frame_cursor (f, cursor);
23350 }
23351
23352 /* Take proper action when mouse has moved to the mode or header line
23353 or marginal area AREA of window W, x-position X and y-position Y.
23354 X is relative to the start of the text display area of W, so the
23355 width of bitmap areas and scroll bars must be subtracted to get a
23356 position relative to the start of the mode line. */
23357
23358 static void
23359 note_mode_line_or_margin_highlight (window, x, y, area)
23360 Lisp_Object window;
23361 int x, y;
23362 enum window_part area;
23363 {
23364 struct window *w = XWINDOW (window);
23365 struct frame *f = XFRAME (w->frame);
23366 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
23367 Cursor cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
23368 Lisp_Object pointer = Qnil;
23369 int charpos, dx, dy, width, height;
23370 Lisp_Object string, object = Qnil;
23371 Lisp_Object pos, help;
23372
23373 Lisp_Object mouse_face;
23374 int original_x_pixel = x;
23375 struct glyph * glyph = NULL, * row_start_glyph = NULL;
23376 struct glyph_row *row;
23377
23378 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
23379 {
23380 int x0;
23381 struct glyph *end;
23382
23383 string = mode_line_string (w, area, &x, &y, &charpos,
23384 &object, &dx, &dy, &width, &height);
23385
23386 row = (area == ON_MODE_LINE
23387 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
23388 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
23389
23390 /* Find glyph */
23391 if (row->mode_line_p && row->enabled_p)
23392 {
23393 glyph = row_start_glyph = row->glyphs[TEXT_AREA];
23394 end = glyph + row->used[TEXT_AREA];
23395
23396 for (x0 = original_x_pixel;
23397 glyph < end && x0 >= glyph->pixel_width;
23398 ++glyph)
23399 x0 -= glyph->pixel_width;
23400
23401 if (glyph >= end)
23402 glyph = NULL;
23403 }
23404 }
23405 else
23406 {
23407 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
23408 string = marginal_area_string (w, area, &x, &y, &charpos,
23409 &object, &dx, &dy, &width, &height);
23410 }
23411
23412 help = Qnil;
23413
23414 if (IMAGEP (object))
23415 {
23416 Lisp_Object image_map, hotspot;
23417 if ((image_map = Fplist_get (XCDR (object), QCmap),
23418 !NILP (image_map))
23419 && (hotspot = find_hot_spot (image_map, dx, dy),
23420 CONSP (hotspot))
23421 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
23422 {
23423 Lisp_Object area_id, plist;
23424
23425 area_id = XCAR (hotspot);
23426 /* Could check AREA_ID to see if we enter/leave this hot-spot.
23427 If so, we could look for mouse-enter, mouse-leave
23428 properties in PLIST (and do something...). */
23429 hotspot = XCDR (hotspot);
23430 if (CONSP (hotspot)
23431 && (plist = XCAR (hotspot), CONSP (plist)))
23432 {
23433 pointer = Fplist_get (plist, Qpointer);
23434 if (NILP (pointer))
23435 pointer = Qhand;
23436 help = Fplist_get (plist, Qhelp_echo);
23437 if (!NILP (help))
23438 {
23439 help_echo_string = help;
23440 /* Is this correct? ++kfs */
23441 XSETWINDOW (help_echo_window, w);
23442 help_echo_object = w->buffer;
23443 help_echo_pos = charpos;
23444 }
23445 }
23446 }
23447 if (NILP (pointer))
23448 pointer = Fplist_get (XCDR (object), QCpointer);
23449 }
23450
23451 if (STRINGP (string))
23452 {
23453 pos = make_number (charpos);
23454 /* If we're on a string with `help-echo' text property, arrange
23455 for the help to be displayed. This is done by setting the
23456 global variable help_echo_string to the help string. */
23457 if (NILP (help))
23458 {
23459 help = Fget_text_property (pos, Qhelp_echo, string);
23460 if (!NILP (help))
23461 {
23462 help_echo_string = help;
23463 XSETWINDOW (help_echo_window, w);
23464 help_echo_object = string;
23465 help_echo_pos = charpos;
23466 }
23467 }
23468
23469 if (NILP (pointer))
23470 pointer = Fget_text_property (pos, Qpointer, string);
23471
23472 /* Change the mouse pointer according to what is under X/Y. */
23473 if (NILP (pointer) && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
23474 {
23475 Lisp_Object map;
23476 map = Fget_text_property (pos, Qlocal_map, string);
23477 if (!KEYMAPP (map))
23478 map = Fget_text_property (pos, Qkeymap, string);
23479 if (!KEYMAPP (map))
23480 cursor = dpyinfo->vertical_scroll_bar_cursor;
23481 }
23482
23483 /* Change the mouse face according to what is under X/Y. */
23484 mouse_face = Fget_text_property (pos, Qmouse_face, string);
23485 if (!NILP (mouse_face)
23486 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
23487 && glyph)
23488 {
23489 Lisp_Object b, e;
23490
23491 struct glyph * tmp_glyph;
23492
23493 int gpos;
23494 int gseq_length;
23495 int total_pixel_width;
23496 EMACS_INT ignore;
23497
23498 int vpos, hpos;
23499
23500 b = Fprevious_single_property_change (make_number (charpos + 1),
23501 Qmouse_face, string, Qnil);
23502 if (NILP (b))
23503 b = make_number (0);
23504
23505 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
23506 if (NILP (e))
23507 e = make_number (SCHARS (string));
23508
23509 /* Calculate the position(glyph position: GPOS) of GLYPH in
23510 displayed string. GPOS is different from CHARPOS.
23511
23512 CHARPOS is the position of glyph in internal string
23513 object. A mode line string format has structures which
23514 is converted to a flatten by emacs lisp interpreter.
23515 The internal string is an element of the structures.
23516 The displayed string is the flatten string. */
23517 gpos = 0;
23518 if (glyph > row_start_glyph)
23519 {
23520 tmp_glyph = glyph - 1;
23521 while (tmp_glyph >= row_start_glyph
23522 && tmp_glyph->charpos >= XINT (b)
23523 && EQ (tmp_glyph->object, glyph->object))
23524 {
23525 tmp_glyph--;
23526 gpos++;
23527 }
23528 }
23529
23530 /* Calculate the lenght(glyph sequence length: GSEQ_LENGTH) of
23531 displayed string holding GLYPH.
23532
23533 GSEQ_LENGTH is different from SCHARS (STRING).
23534 SCHARS (STRING) returns the length of the internal string. */
23535 for (tmp_glyph = glyph, gseq_length = gpos;
23536 tmp_glyph->charpos < XINT (e);
23537 tmp_glyph++, gseq_length++)
23538 {
23539 if (!EQ (tmp_glyph->object, glyph->object))
23540 break;
23541 }
23542
23543 total_pixel_width = 0;
23544 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
23545 total_pixel_width += tmp_glyph->pixel_width;
23546
23547 /* Pre calculation of re-rendering position */
23548 vpos = (x - gpos);
23549 hpos = (area == ON_MODE_LINE
23550 ? (w->current_matrix)->nrows - 1
23551 : 0);
23552
23553 /* If the re-rendering position is included in the last
23554 re-rendering area, we should do nothing. */
23555 if ( EQ (window, dpyinfo->mouse_face_window)
23556 && dpyinfo->mouse_face_beg_col <= vpos
23557 && vpos < dpyinfo->mouse_face_end_col
23558 && dpyinfo->mouse_face_beg_row == hpos )
23559 return;
23560
23561 if (clear_mouse_face (dpyinfo))
23562 cursor = No_Cursor;
23563
23564 dpyinfo->mouse_face_beg_col = vpos;
23565 dpyinfo->mouse_face_beg_row = hpos;
23566
23567 dpyinfo->mouse_face_beg_x = original_x_pixel - (total_pixel_width + dx);
23568 dpyinfo->mouse_face_beg_y = 0;
23569
23570 dpyinfo->mouse_face_end_col = vpos + gseq_length;
23571 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_beg_row;
23572
23573 dpyinfo->mouse_face_end_x = 0;
23574 dpyinfo->mouse_face_end_y = 0;
23575
23576 dpyinfo->mouse_face_past_end = 0;
23577 dpyinfo->mouse_face_window = window;
23578
23579 dpyinfo->mouse_face_face_id = face_at_string_position (w, string,
23580 charpos,
23581 0, 0, 0, &ignore,
23582 glyph->face_id, 1);
23583 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
23584
23585 if (NILP (pointer))
23586 pointer = Qhand;
23587 }
23588 else if ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
23589 clear_mouse_face (dpyinfo);
23590 }
23591 define_frame_cursor1 (f, cursor, pointer);
23592 }
23593
23594
23595 /* EXPORT:
23596 Take proper action when the mouse has moved to position X, Y on
23597 frame F as regards highlighting characters that have mouse-face
23598 properties. Also de-highlighting chars where the mouse was before.
23599 X and Y can be negative or out of range. */
23600
23601 void
23602 note_mouse_highlight (f, x, y)
23603 struct frame *f;
23604 int x, y;
23605 {
23606 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
23607 enum window_part part;
23608 Lisp_Object window;
23609 struct window *w;
23610 Cursor cursor = No_Cursor;
23611 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
23612 struct buffer *b;
23613
23614 /* When a menu is active, don't highlight because this looks odd. */
23615 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS)
23616 if (popup_activated ())
23617 return;
23618 #endif
23619
23620 if (NILP (Vmouse_highlight)
23621 || !f->glyphs_initialized_p)
23622 return;
23623
23624 dpyinfo->mouse_face_mouse_x = x;
23625 dpyinfo->mouse_face_mouse_y = y;
23626 dpyinfo->mouse_face_mouse_frame = f;
23627
23628 if (dpyinfo->mouse_face_defer)
23629 return;
23630
23631 if (gc_in_progress)
23632 {
23633 dpyinfo->mouse_face_deferred_gc = 1;
23634 return;
23635 }
23636
23637 /* Which window is that in? */
23638 window = window_from_coordinates (f, x, y, &part, 0, 0, 1);
23639
23640 /* If we were displaying active text in another window, clear that.
23641 Also clear if we move out of text area in same window. */
23642 if (! EQ (window, dpyinfo->mouse_face_window)
23643 || (part != ON_TEXT && part != ON_MODE_LINE && part != ON_HEADER_LINE
23644 && !NILP (dpyinfo->mouse_face_window)))
23645 clear_mouse_face (dpyinfo);
23646
23647 /* Not on a window -> return. */
23648 if (!WINDOWP (window))
23649 return;
23650
23651 /* Reset help_echo_string. It will get recomputed below. */
23652 help_echo_string = Qnil;
23653
23654 /* Convert to window-relative pixel coordinates. */
23655 w = XWINDOW (window);
23656 frame_to_window_pixel_xy (w, &x, &y);
23657
23658 /* Handle tool-bar window differently since it doesn't display a
23659 buffer. */
23660 if (EQ (window, f->tool_bar_window))
23661 {
23662 note_tool_bar_highlight (f, x, y);
23663 return;
23664 }
23665
23666 /* Mouse is on the mode, header line or margin? */
23667 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
23668 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
23669 {
23670 note_mode_line_or_margin_highlight (window, x, y, part);
23671 return;
23672 }
23673
23674 if (part == ON_VERTICAL_BORDER)
23675 {
23676 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
23677 help_echo_string = build_string ("drag-mouse-1: resize");
23678 }
23679 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
23680 || part == ON_SCROLL_BAR)
23681 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
23682 else
23683 cursor = FRAME_X_OUTPUT (f)->text_cursor;
23684
23685 /* Are we in a window whose display is up to date?
23686 And verify the buffer's text has not changed. */
23687 b = XBUFFER (w->buffer);
23688 if (part == ON_TEXT
23689 && EQ (w->window_end_valid, w->buffer)
23690 && XFASTINT (w->last_modified) == BUF_MODIFF (b)
23691 && XFASTINT (w->last_overlay_modified) == BUF_OVERLAY_MODIFF (b))
23692 {
23693 int hpos, vpos, pos, i, dx, dy, area;
23694 struct glyph *glyph;
23695 Lisp_Object object;
23696 Lisp_Object mouse_face = Qnil, overlay = Qnil, position;
23697 Lisp_Object *overlay_vec = NULL;
23698 int noverlays;
23699 struct buffer *obuf;
23700 int obegv, ozv, same_region;
23701
23702 /* Find the glyph under X/Y. */
23703 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
23704
23705 /* Look for :pointer property on image. */
23706 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
23707 {
23708 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
23709 if (img != NULL && IMAGEP (img->spec))
23710 {
23711 Lisp_Object image_map, hotspot;
23712 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
23713 !NILP (image_map))
23714 && (hotspot = find_hot_spot (image_map,
23715 glyph->slice.x + dx,
23716 glyph->slice.y + dy),
23717 CONSP (hotspot))
23718 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
23719 {
23720 Lisp_Object area_id, plist;
23721
23722 area_id = XCAR (hotspot);
23723 /* Could check AREA_ID to see if we enter/leave this hot-spot.
23724 If so, we could look for mouse-enter, mouse-leave
23725 properties in PLIST (and do something...). */
23726 hotspot = XCDR (hotspot);
23727 if (CONSP (hotspot)
23728 && (plist = XCAR (hotspot), CONSP (plist)))
23729 {
23730 pointer = Fplist_get (plist, Qpointer);
23731 if (NILP (pointer))
23732 pointer = Qhand;
23733 help_echo_string = Fplist_get (plist, Qhelp_echo);
23734 if (!NILP (help_echo_string))
23735 {
23736 help_echo_window = window;
23737 help_echo_object = glyph->object;
23738 help_echo_pos = glyph->charpos;
23739 }
23740 }
23741 }
23742 if (NILP (pointer))
23743 pointer = Fplist_get (XCDR (img->spec), QCpointer);
23744 }
23745 }
23746
23747 /* Clear mouse face if X/Y not over text. */
23748 if (glyph == NULL
23749 || area != TEXT_AREA
23750 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p)
23751 {
23752 if (clear_mouse_face (dpyinfo))
23753 cursor = No_Cursor;
23754 if (NILP (pointer))
23755 {
23756 if (area != TEXT_AREA)
23757 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
23758 else
23759 pointer = Vvoid_text_area_pointer;
23760 }
23761 goto set_cursor;
23762 }
23763
23764 pos = glyph->charpos;
23765 object = glyph->object;
23766 if (!STRINGP (object) && !BUFFERP (object))
23767 goto set_cursor;
23768
23769 /* If we get an out-of-range value, return now; avoid an error. */
23770 if (BUFFERP (object) && pos > BUF_Z (b))
23771 goto set_cursor;
23772
23773 /* Make the window's buffer temporarily current for
23774 overlays_at and compute_char_face. */
23775 obuf = current_buffer;
23776 current_buffer = b;
23777 obegv = BEGV;
23778 ozv = ZV;
23779 BEGV = BEG;
23780 ZV = Z;
23781
23782 /* Is this char mouse-active or does it have help-echo? */
23783 position = make_number (pos);
23784
23785 if (BUFFERP (object))
23786 {
23787 /* Put all the overlays we want in a vector in overlay_vec. */
23788 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
23789 /* Sort overlays into increasing priority order. */
23790 noverlays = sort_overlays (overlay_vec, noverlays, w);
23791 }
23792 else
23793 noverlays = 0;
23794
23795 same_region = (EQ (window, dpyinfo->mouse_face_window)
23796 && vpos >= dpyinfo->mouse_face_beg_row
23797 && vpos <= dpyinfo->mouse_face_end_row
23798 && (vpos > dpyinfo->mouse_face_beg_row
23799 || hpos >= dpyinfo->mouse_face_beg_col)
23800 && (vpos < dpyinfo->mouse_face_end_row
23801 || hpos < dpyinfo->mouse_face_end_col
23802 || dpyinfo->mouse_face_past_end));
23803
23804 if (same_region)
23805 cursor = No_Cursor;
23806
23807 /* Check mouse-face highlighting. */
23808 if (! same_region
23809 /* If there exists an overlay with mouse-face overlapping
23810 the one we are currently highlighting, we have to
23811 check if we enter the overlapping overlay, and then
23812 highlight only that. */
23813 || (OVERLAYP (dpyinfo->mouse_face_overlay)
23814 && mouse_face_overlay_overlaps (dpyinfo->mouse_face_overlay)))
23815 {
23816 /* Find the highest priority overlay that has a mouse-face
23817 property. */
23818 overlay = Qnil;
23819 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
23820 {
23821 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
23822 if (!NILP (mouse_face))
23823 overlay = overlay_vec[i];
23824 }
23825
23826 /* If we're actually highlighting the same overlay as
23827 before, there's no need to do that again. */
23828 if (!NILP (overlay)
23829 && EQ (overlay, dpyinfo->mouse_face_overlay))
23830 goto check_help_echo;
23831
23832 dpyinfo->mouse_face_overlay = overlay;
23833
23834 /* Clear the display of the old active region, if any. */
23835 if (clear_mouse_face (dpyinfo))
23836 cursor = No_Cursor;
23837
23838 /* If no overlay applies, get a text property. */
23839 if (NILP (overlay))
23840 mouse_face = Fget_text_property (position, Qmouse_face, object);
23841
23842 /* Handle the overlay case. */
23843 if (!NILP (overlay))
23844 {
23845 /* Find the range of text around this char that
23846 should be active. */
23847 Lisp_Object before, after;
23848 EMACS_INT ignore;
23849
23850 before = Foverlay_start (overlay);
23851 after = Foverlay_end (overlay);
23852 /* Record this as the current active region. */
23853 fast_find_position (w, XFASTINT (before),
23854 &dpyinfo->mouse_face_beg_col,
23855 &dpyinfo->mouse_face_beg_row,
23856 &dpyinfo->mouse_face_beg_x,
23857 &dpyinfo->mouse_face_beg_y, Qnil);
23858
23859 dpyinfo->mouse_face_past_end
23860 = !fast_find_position (w, XFASTINT (after),
23861 &dpyinfo->mouse_face_end_col,
23862 &dpyinfo->mouse_face_end_row,
23863 &dpyinfo->mouse_face_end_x,
23864 &dpyinfo->mouse_face_end_y, Qnil);
23865 dpyinfo->mouse_face_window = window;
23866
23867 dpyinfo->mouse_face_face_id
23868 = face_at_buffer_position (w, pos, 0, 0,
23869 &ignore, pos + 1,
23870 !dpyinfo->mouse_face_hidden);
23871
23872 /* Display it as active. */
23873 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
23874 cursor = No_Cursor;
23875 }
23876 /* Handle the text property case. */
23877 else if (!NILP (mouse_face) && BUFFERP (object))
23878 {
23879 /* Find the range of text around this char that
23880 should be active. */
23881 Lisp_Object before, after, beginning, end;
23882 EMACS_INT ignore;
23883
23884 beginning = Fmarker_position (w->start);
23885 end = make_number (BUF_Z (XBUFFER (object))
23886 - XFASTINT (w->window_end_pos));
23887 before
23888 = Fprevious_single_property_change (make_number (pos + 1),
23889 Qmouse_face,
23890 object, beginning);
23891 after
23892 = Fnext_single_property_change (position, Qmouse_face,
23893 object, end);
23894
23895 /* Record this as the current active region. */
23896 fast_find_position (w, XFASTINT (before),
23897 &dpyinfo->mouse_face_beg_col,
23898 &dpyinfo->mouse_face_beg_row,
23899 &dpyinfo->mouse_face_beg_x,
23900 &dpyinfo->mouse_face_beg_y, Qnil);
23901 dpyinfo->mouse_face_past_end
23902 = !fast_find_position (w, XFASTINT (after),
23903 &dpyinfo->mouse_face_end_col,
23904 &dpyinfo->mouse_face_end_row,
23905 &dpyinfo->mouse_face_end_x,
23906 &dpyinfo->mouse_face_end_y, Qnil);
23907 dpyinfo->mouse_face_window = window;
23908
23909 if (BUFFERP (object))
23910 dpyinfo->mouse_face_face_id
23911 = face_at_buffer_position (w, pos, 0, 0,
23912 &ignore, pos + 1,
23913 !dpyinfo->mouse_face_hidden);
23914
23915 /* Display it as active. */
23916 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
23917 cursor = No_Cursor;
23918 }
23919 else if (!NILP (mouse_face) && STRINGP (object))
23920 {
23921 Lisp_Object b, e;
23922 EMACS_INT ignore;
23923
23924 b = Fprevious_single_property_change (make_number (pos + 1),
23925 Qmouse_face,
23926 object, Qnil);
23927 e = Fnext_single_property_change (position, Qmouse_face,
23928 object, Qnil);
23929 if (NILP (b))
23930 b = make_number (0);
23931 if (NILP (e))
23932 e = make_number (SCHARS (object) - 1);
23933
23934 fast_find_string_pos (w, XINT (b), object,
23935 &dpyinfo->mouse_face_beg_col,
23936 &dpyinfo->mouse_face_beg_row,
23937 &dpyinfo->mouse_face_beg_x,
23938 &dpyinfo->mouse_face_beg_y, 0);
23939 fast_find_string_pos (w, XINT (e), object,
23940 &dpyinfo->mouse_face_end_col,
23941 &dpyinfo->mouse_face_end_row,
23942 &dpyinfo->mouse_face_end_x,
23943 &dpyinfo->mouse_face_end_y, 1);
23944 dpyinfo->mouse_face_past_end = 0;
23945 dpyinfo->mouse_face_window = window;
23946 dpyinfo->mouse_face_face_id
23947 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
23948 glyph->face_id, 1);
23949 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
23950 cursor = No_Cursor;
23951 }
23952 else if (STRINGP (object) && NILP (mouse_face))
23953 {
23954 /* A string which doesn't have mouse-face, but
23955 the text ``under'' it might have. */
23956 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
23957 int start = MATRIX_ROW_START_CHARPOS (r);
23958
23959 pos = string_buffer_position (w, object, start);
23960 if (pos > 0)
23961 mouse_face = get_char_property_and_overlay (make_number (pos),
23962 Qmouse_face,
23963 w->buffer,
23964 &overlay);
23965 if (!NILP (mouse_face) && !NILP (overlay))
23966 {
23967 Lisp_Object before = Foverlay_start (overlay);
23968 Lisp_Object after = Foverlay_end (overlay);
23969 EMACS_INT ignore;
23970
23971 /* Note that we might not be able to find position
23972 BEFORE in the glyph matrix if the overlay is
23973 entirely covered by a `display' property. In
23974 this case, we overshoot. So let's stop in
23975 the glyph matrix before glyphs for OBJECT. */
23976 fast_find_position (w, XFASTINT (before),
23977 &dpyinfo->mouse_face_beg_col,
23978 &dpyinfo->mouse_face_beg_row,
23979 &dpyinfo->mouse_face_beg_x,
23980 &dpyinfo->mouse_face_beg_y,
23981 object);
23982
23983 dpyinfo->mouse_face_past_end
23984 = !fast_find_position (w, XFASTINT (after),
23985 &dpyinfo->mouse_face_end_col,
23986 &dpyinfo->mouse_face_end_row,
23987 &dpyinfo->mouse_face_end_x,
23988 &dpyinfo->mouse_face_end_y,
23989 Qnil);
23990 dpyinfo->mouse_face_window = window;
23991 dpyinfo->mouse_face_face_id
23992 = face_at_buffer_position (w, pos, 0, 0,
23993 &ignore, pos + 1,
23994 !dpyinfo->mouse_face_hidden);
23995
23996 /* Display it as active. */
23997 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
23998 cursor = No_Cursor;
23999 }
24000 }
24001 }
24002
24003 check_help_echo:
24004
24005 /* Look for a `help-echo' property. */
24006 if (NILP (help_echo_string)) {
24007 Lisp_Object help, overlay;
24008
24009 /* Check overlays first. */
24010 help = overlay = Qnil;
24011 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
24012 {
24013 overlay = overlay_vec[i];
24014 help = Foverlay_get (overlay, Qhelp_echo);
24015 }
24016
24017 if (!NILP (help))
24018 {
24019 help_echo_string = help;
24020 help_echo_window = window;
24021 help_echo_object = overlay;
24022 help_echo_pos = pos;
24023 }
24024 else
24025 {
24026 Lisp_Object object = glyph->object;
24027 int charpos = glyph->charpos;
24028
24029 /* Try text properties. */
24030 if (STRINGP (object)
24031 && charpos >= 0
24032 && charpos < SCHARS (object))
24033 {
24034 help = Fget_text_property (make_number (charpos),
24035 Qhelp_echo, object);
24036 if (NILP (help))
24037 {
24038 /* If the string itself doesn't specify a help-echo,
24039 see if the buffer text ``under'' it does. */
24040 struct glyph_row *r
24041 = MATRIX_ROW (w->current_matrix, vpos);
24042 int start = MATRIX_ROW_START_CHARPOS (r);
24043 int pos = string_buffer_position (w, object, start);
24044 if (pos > 0)
24045 {
24046 help = Fget_char_property (make_number (pos),
24047 Qhelp_echo, w->buffer);
24048 if (!NILP (help))
24049 {
24050 charpos = pos;
24051 object = w->buffer;
24052 }
24053 }
24054 }
24055 }
24056 else if (BUFFERP (object)
24057 && charpos >= BEGV
24058 && charpos < ZV)
24059 help = Fget_text_property (make_number (charpos), Qhelp_echo,
24060 object);
24061
24062 if (!NILP (help))
24063 {
24064 help_echo_string = help;
24065 help_echo_window = window;
24066 help_echo_object = object;
24067 help_echo_pos = charpos;
24068 }
24069 }
24070 }
24071
24072 /* Look for a `pointer' property. */
24073 if (NILP (pointer))
24074 {
24075 /* Check overlays first. */
24076 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
24077 pointer = Foverlay_get (overlay_vec[i], Qpointer);
24078
24079 if (NILP (pointer))
24080 {
24081 Lisp_Object object = glyph->object;
24082 int charpos = glyph->charpos;
24083
24084 /* Try text properties. */
24085 if (STRINGP (object)
24086 && charpos >= 0
24087 && charpos < SCHARS (object))
24088 {
24089 pointer = Fget_text_property (make_number (charpos),
24090 Qpointer, object);
24091 if (NILP (pointer))
24092 {
24093 /* If the string itself doesn't specify a pointer,
24094 see if the buffer text ``under'' it does. */
24095 struct glyph_row *r
24096 = MATRIX_ROW (w->current_matrix, vpos);
24097 int start = MATRIX_ROW_START_CHARPOS (r);
24098 int pos = string_buffer_position (w, object, start);
24099 if (pos > 0)
24100 pointer = Fget_char_property (make_number (pos),
24101 Qpointer, w->buffer);
24102 }
24103 }
24104 else if (BUFFERP (object)
24105 && charpos >= BEGV
24106 && charpos < ZV)
24107 pointer = Fget_text_property (make_number (charpos),
24108 Qpointer, object);
24109 }
24110 }
24111
24112 BEGV = obegv;
24113 ZV = ozv;
24114 current_buffer = obuf;
24115 }
24116
24117 set_cursor:
24118
24119 define_frame_cursor1 (f, cursor, pointer);
24120 }
24121
24122
24123 /* EXPORT for RIF:
24124 Clear any mouse-face on window W. This function is part of the
24125 redisplay interface, and is called from try_window_id and similar
24126 functions to ensure the mouse-highlight is off. */
24127
24128 void
24129 x_clear_window_mouse_face (w)
24130 struct window *w;
24131 {
24132 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
24133 Lisp_Object window;
24134
24135 BLOCK_INPUT;
24136 XSETWINDOW (window, w);
24137 if (EQ (window, dpyinfo->mouse_face_window))
24138 clear_mouse_face (dpyinfo);
24139 UNBLOCK_INPUT;
24140 }
24141
24142
24143 /* EXPORT:
24144 Just discard the mouse face information for frame F, if any.
24145 This is used when the size of F is changed. */
24146
24147 void
24148 cancel_mouse_face (f)
24149 struct frame *f;
24150 {
24151 Lisp_Object window;
24152 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
24153
24154 window = dpyinfo->mouse_face_window;
24155 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
24156 {
24157 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
24158 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
24159 dpyinfo->mouse_face_window = Qnil;
24160 }
24161 }
24162
24163
24164 #endif /* HAVE_WINDOW_SYSTEM */
24165
24166 \f
24167 /***********************************************************************
24168 Exposure Events
24169 ***********************************************************************/
24170
24171 #ifdef HAVE_WINDOW_SYSTEM
24172
24173 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
24174 which intersects rectangle R. R is in window-relative coordinates. */
24175
24176 static void
24177 expose_area (w, row, r, area)
24178 struct window *w;
24179 struct glyph_row *row;
24180 XRectangle *r;
24181 enum glyph_row_area area;
24182 {
24183 struct glyph *first = row->glyphs[area];
24184 struct glyph *end = row->glyphs[area] + row->used[area];
24185 struct glyph *last;
24186 int first_x, start_x, x;
24187
24188 if (area == TEXT_AREA && row->fill_line_p)
24189 /* If row extends face to end of line write the whole line. */
24190 draw_glyphs (w, 0, row, area,
24191 0, row->used[area],
24192 DRAW_NORMAL_TEXT, 0);
24193 else
24194 {
24195 /* Set START_X to the window-relative start position for drawing glyphs of
24196 AREA. The first glyph of the text area can be partially visible.
24197 The first glyphs of other areas cannot. */
24198 start_x = window_box_left_offset (w, area);
24199 x = start_x;
24200 if (area == TEXT_AREA)
24201 x += row->x;
24202
24203 /* Find the first glyph that must be redrawn. */
24204 while (first < end
24205 && x + first->pixel_width < r->x)
24206 {
24207 x += first->pixel_width;
24208 ++first;
24209 }
24210
24211 /* Find the last one. */
24212 last = first;
24213 first_x = x;
24214 while (last < end
24215 && x < r->x + r->width)
24216 {
24217 x += last->pixel_width;
24218 ++last;
24219 }
24220
24221 /* Repaint. */
24222 if (last > first)
24223 draw_glyphs (w, first_x - start_x, row, area,
24224 first - row->glyphs[area], last - row->glyphs[area],
24225 DRAW_NORMAL_TEXT, 0);
24226 }
24227 }
24228
24229
24230 /* Redraw the parts of the glyph row ROW on window W intersecting
24231 rectangle R. R is in window-relative coordinates. Value is
24232 non-zero if mouse-face was overwritten. */
24233
24234 static int
24235 expose_line (w, row, r)
24236 struct window *w;
24237 struct glyph_row *row;
24238 XRectangle *r;
24239 {
24240 xassert (row->enabled_p);
24241
24242 if (row->mode_line_p || w->pseudo_window_p)
24243 draw_glyphs (w, 0, row, TEXT_AREA,
24244 0, row->used[TEXT_AREA],
24245 DRAW_NORMAL_TEXT, 0);
24246 else
24247 {
24248 if (row->used[LEFT_MARGIN_AREA])
24249 expose_area (w, row, r, LEFT_MARGIN_AREA);
24250 if (row->used[TEXT_AREA])
24251 expose_area (w, row, r, TEXT_AREA);
24252 if (row->used[RIGHT_MARGIN_AREA])
24253 expose_area (w, row, r, RIGHT_MARGIN_AREA);
24254 draw_row_fringe_bitmaps (w, row);
24255 }
24256
24257 return row->mouse_face_p;
24258 }
24259
24260
24261 /* Redraw those parts of glyphs rows during expose event handling that
24262 overlap other rows. Redrawing of an exposed line writes over parts
24263 of lines overlapping that exposed line; this function fixes that.
24264
24265 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
24266 row in W's current matrix that is exposed and overlaps other rows.
24267 LAST_OVERLAPPING_ROW is the last such row. */
24268
24269 static void
24270 expose_overlaps (w, first_overlapping_row, last_overlapping_row, r)
24271 struct window *w;
24272 struct glyph_row *first_overlapping_row;
24273 struct glyph_row *last_overlapping_row;
24274 XRectangle *r;
24275 {
24276 struct glyph_row *row;
24277
24278 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
24279 if (row->overlapping_p)
24280 {
24281 xassert (row->enabled_p && !row->mode_line_p);
24282
24283 row->clip = r;
24284 if (row->used[LEFT_MARGIN_AREA])
24285 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
24286
24287 if (row->used[TEXT_AREA])
24288 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
24289
24290 if (row->used[RIGHT_MARGIN_AREA])
24291 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
24292 row->clip = NULL;
24293 }
24294 }
24295
24296
24297 /* Return non-zero if W's cursor intersects rectangle R. */
24298
24299 static int
24300 phys_cursor_in_rect_p (w, r)
24301 struct window *w;
24302 XRectangle *r;
24303 {
24304 XRectangle cr, result;
24305 struct glyph *cursor_glyph;
24306 struct glyph_row *row;
24307
24308 if (w->phys_cursor.vpos >= 0
24309 && w->phys_cursor.vpos < w->current_matrix->nrows
24310 && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
24311 row->enabled_p)
24312 && row->cursor_in_fringe_p)
24313 {
24314 /* Cursor is in the fringe. */
24315 cr.x = window_box_right_offset (w,
24316 (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
24317 ? RIGHT_MARGIN_AREA
24318 : TEXT_AREA));
24319 cr.y = row->y;
24320 cr.width = WINDOW_RIGHT_FRINGE_WIDTH (w);
24321 cr.height = row->height;
24322 return x_intersect_rectangles (&cr, r, &result);
24323 }
24324
24325 cursor_glyph = get_phys_cursor_glyph (w);
24326 if (cursor_glyph)
24327 {
24328 /* r is relative to W's box, but w->phys_cursor.x is relative
24329 to left edge of W's TEXT area. Adjust it. */
24330 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
24331 cr.y = w->phys_cursor.y;
24332 cr.width = cursor_glyph->pixel_width;
24333 cr.height = w->phys_cursor_height;
24334 /* ++KFS: W32 version used W32-specific IntersectRect here, but
24335 I assume the effect is the same -- and this is portable. */
24336 return x_intersect_rectangles (&cr, r, &result);
24337 }
24338 /* If we don't understand the format, pretend we're not in the hot-spot. */
24339 return 0;
24340 }
24341
24342
24343 /* EXPORT:
24344 Draw a vertical window border to the right of window W if W doesn't
24345 have vertical scroll bars. */
24346
24347 void
24348 x_draw_vertical_border (w)
24349 struct window *w;
24350 {
24351 struct frame *f = XFRAME (WINDOW_FRAME (w));
24352
24353 /* We could do better, if we knew what type of scroll-bar the adjacent
24354 windows (on either side) have... But we don't :-(
24355 However, I think this works ok. ++KFS 2003-04-25 */
24356
24357 /* Redraw borders between horizontally adjacent windows. Don't
24358 do it for frames with vertical scroll bars because either the
24359 right scroll bar of a window, or the left scroll bar of its
24360 neighbor will suffice as a border. */
24361 if (FRAME_HAS_VERTICAL_SCROLL_BARS (XFRAME (w->frame)))
24362 return;
24363
24364 if (!WINDOW_RIGHTMOST_P (w)
24365 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
24366 {
24367 int x0, x1, y0, y1;
24368
24369 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
24370 y1 -= 1;
24371
24372 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
24373 x1 -= 1;
24374
24375 FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
24376 }
24377 else if (!WINDOW_LEFTMOST_P (w)
24378 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
24379 {
24380 int x0, x1, y0, y1;
24381
24382 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
24383 y1 -= 1;
24384
24385 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
24386 x0 -= 1;
24387
24388 FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
24389 }
24390 }
24391
24392
24393 /* Redraw the part of window W intersection rectangle FR. Pixel
24394 coordinates in FR are frame-relative. Call this function with
24395 input blocked. Value is non-zero if the exposure overwrites
24396 mouse-face. */
24397
24398 static int
24399 expose_window (w, fr)
24400 struct window *w;
24401 XRectangle *fr;
24402 {
24403 struct frame *f = XFRAME (w->frame);
24404 XRectangle wr, r;
24405 int mouse_face_overwritten_p = 0;
24406
24407 /* If window is not yet fully initialized, do nothing. This can
24408 happen when toolkit scroll bars are used and a window is split.
24409 Reconfiguring the scroll bar will generate an expose for a newly
24410 created window. */
24411 if (w->current_matrix == NULL)
24412 return 0;
24413
24414 /* When we're currently updating the window, display and current
24415 matrix usually don't agree. Arrange for a thorough display
24416 later. */
24417 if (w == updated_window)
24418 {
24419 SET_FRAME_GARBAGED (f);
24420 return 0;
24421 }
24422
24423 /* Frame-relative pixel rectangle of W. */
24424 wr.x = WINDOW_LEFT_EDGE_X (w);
24425 wr.y = WINDOW_TOP_EDGE_Y (w);
24426 wr.width = WINDOW_TOTAL_WIDTH (w);
24427 wr.height = WINDOW_TOTAL_HEIGHT (w);
24428
24429 if (x_intersect_rectangles (fr, &wr, &r))
24430 {
24431 int yb = window_text_bottom_y (w);
24432 struct glyph_row *row;
24433 int cursor_cleared_p;
24434 struct glyph_row *first_overlapping_row, *last_overlapping_row;
24435
24436 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
24437 r.x, r.y, r.width, r.height));
24438
24439 /* Convert to window coordinates. */
24440 r.x -= WINDOW_LEFT_EDGE_X (w);
24441 r.y -= WINDOW_TOP_EDGE_Y (w);
24442
24443 /* Turn off the cursor. */
24444 if (!w->pseudo_window_p
24445 && phys_cursor_in_rect_p (w, &r))
24446 {
24447 x_clear_cursor (w);
24448 cursor_cleared_p = 1;
24449 }
24450 else
24451 cursor_cleared_p = 0;
24452
24453 /* Update lines intersecting rectangle R. */
24454 first_overlapping_row = last_overlapping_row = NULL;
24455 for (row = w->current_matrix->rows;
24456 row->enabled_p;
24457 ++row)
24458 {
24459 int y0 = row->y;
24460 int y1 = MATRIX_ROW_BOTTOM_Y (row);
24461
24462 if ((y0 >= r.y && y0 < r.y + r.height)
24463 || (y1 > r.y && y1 < r.y + r.height)
24464 || (r.y >= y0 && r.y < y1)
24465 || (r.y + r.height > y0 && r.y + r.height < y1))
24466 {
24467 /* A header line may be overlapping, but there is no need
24468 to fix overlapping areas for them. KFS 2005-02-12 */
24469 if (row->overlapping_p && !row->mode_line_p)
24470 {
24471 if (first_overlapping_row == NULL)
24472 first_overlapping_row = row;
24473 last_overlapping_row = row;
24474 }
24475
24476 row->clip = fr;
24477 if (expose_line (w, row, &r))
24478 mouse_face_overwritten_p = 1;
24479 row->clip = NULL;
24480 }
24481 else if (row->overlapping_p)
24482 {
24483 /* We must redraw a row overlapping the exposed area. */
24484 if (y0 < r.y
24485 ? y0 + row->phys_height > r.y
24486 : y0 + row->ascent - row->phys_ascent < r.y +r.height)
24487 {
24488 if (first_overlapping_row == NULL)
24489 first_overlapping_row = row;
24490 last_overlapping_row = row;
24491 }
24492 }
24493
24494 if (y1 >= yb)
24495 break;
24496 }
24497
24498 /* Display the mode line if there is one. */
24499 if (WINDOW_WANTS_MODELINE_P (w)
24500 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
24501 row->enabled_p)
24502 && row->y < r.y + r.height)
24503 {
24504 if (expose_line (w, row, &r))
24505 mouse_face_overwritten_p = 1;
24506 }
24507
24508 if (!w->pseudo_window_p)
24509 {
24510 /* Fix the display of overlapping rows. */
24511 if (first_overlapping_row)
24512 expose_overlaps (w, first_overlapping_row, last_overlapping_row,
24513 fr);
24514
24515 /* Draw border between windows. */
24516 x_draw_vertical_border (w);
24517
24518 /* Turn the cursor on again. */
24519 if (cursor_cleared_p)
24520 update_window_cursor (w, 1);
24521 }
24522 }
24523
24524 return mouse_face_overwritten_p;
24525 }
24526
24527
24528
24529 /* Redraw (parts) of all windows in the window tree rooted at W that
24530 intersect R. R contains frame pixel coordinates. Value is
24531 non-zero if the exposure overwrites mouse-face. */
24532
24533 static int
24534 expose_window_tree (w, r)
24535 struct window *w;
24536 XRectangle *r;
24537 {
24538 struct frame *f = XFRAME (w->frame);
24539 int mouse_face_overwritten_p = 0;
24540
24541 while (w && !FRAME_GARBAGED_P (f))
24542 {
24543 if (!NILP (w->hchild))
24544 mouse_face_overwritten_p
24545 |= expose_window_tree (XWINDOW (w->hchild), r);
24546 else if (!NILP (w->vchild))
24547 mouse_face_overwritten_p
24548 |= expose_window_tree (XWINDOW (w->vchild), r);
24549 else
24550 mouse_face_overwritten_p |= expose_window (w, r);
24551
24552 w = NILP (w->next) ? NULL : XWINDOW (w->next);
24553 }
24554
24555 return mouse_face_overwritten_p;
24556 }
24557
24558
24559 /* EXPORT:
24560 Redisplay an exposed area of frame F. X and Y are the upper-left
24561 corner of the exposed rectangle. W and H are width and height of
24562 the exposed area. All are pixel values. W or H zero means redraw
24563 the entire frame. */
24564
24565 void
24566 expose_frame (f, x, y, w, h)
24567 struct frame *f;
24568 int x, y, w, h;
24569 {
24570 XRectangle r;
24571 int mouse_face_overwritten_p = 0;
24572
24573 TRACE ((stderr, "expose_frame "));
24574
24575 /* No need to redraw if frame will be redrawn soon. */
24576 if (FRAME_GARBAGED_P (f))
24577 {
24578 TRACE ((stderr, " garbaged\n"));
24579 return;
24580 }
24581
24582 /* If basic faces haven't been realized yet, there is no point in
24583 trying to redraw anything. This can happen when we get an expose
24584 event while Emacs is starting, e.g. by moving another window. */
24585 if (FRAME_FACE_CACHE (f) == NULL
24586 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
24587 {
24588 TRACE ((stderr, " no faces\n"));
24589 return;
24590 }
24591
24592 if (w == 0 || h == 0)
24593 {
24594 r.x = r.y = 0;
24595 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
24596 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
24597 }
24598 else
24599 {
24600 r.x = x;
24601 r.y = y;
24602 r.width = w;
24603 r.height = h;
24604 }
24605
24606 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
24607 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
24608
24609 if (WINDOWP (f->tool_bar_window))
24610 mouse_face_overwritten_p
24611 |= expose_window (XWINDOW (f->tool_bar_window), &r);
24612
24613 #ifdef HAVE_X_WINDOWS
24614 #ifndef MSDOS
24615 #ifndef USE_X_TOOLKIT
24616 if (WINDOWP (f->menu_bar_window))
24617 mouse_face_overwritten_p
24618 |= expose_window (XWINDOW (f->menu_bar_window), &r);
24619 #endif /* not USE_X_TOOLKIT */
24620 #endif
24621 #endif
24622
24623 /* Some window managers support a focus-follows-mouse style with
24624 delayed raising of frames. Imagine a partially obscured frame,
24625 and moving the mouse into partially obscured mouse-face on that
24626 frame. The visible part of the mouse-face will be highlighted,
24627 then the WM raises the obscured frame. With at least one WM, KDE
24628 2.1, Emacs is not getting any event for the raising of the frame
24629 (even tried with SubstructureRedirectMask), only Expose events.
24630 These expose events will draw text normally, i.e. not
24631 highlighted. Which means we must redo the highlight here.
24632 Subsume it under ``we love X''. --gerd 2001-08-15 */
24633 /* Included in Windows version because Windows most likely does not
24634 do the right thing if any third party tool offers
24635 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
24636 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
24637 {
24638 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
24639 if (f == dpyinfo->mouse_face_mouse_frame)
24640 {
24641 int x = dpyinfo->mouse_face_mouse_x;
24642 int y = dpyinfo->mouse_face_mouse_y;
24643 clear_mouse_face (dpyinfo);
24644 note_mouse_highlight (f, x, y);
24645 }
24646 }
24647 }
24648
24649
24650 /* EXPORT:
24651 Determine the intersection of two rectangles R1 and R2. Return
24652 the intersection in *RESULT. Value is non-zero if RESULT is not
24653 empty. */
24654
24655 int
24656 x_intersect_rectangles (r1, r2, result)
24657 XRectangle *r1, *r2, *result;
24658 {
24659 XRectangle *left, *right;
24660 XRectangle *upper, *lower;
24661 int intersection_p = 0;
24662
24663 /* Rearrange so that R1 is the left-most rectangle. */
24664 if (r1->x < r2->x)
24665 left = r1, right = r2;
24666 else
24667 left = r2, right = r1;
24668
24669 /* X0 of the intersection is right.x0, if this is inside R1,
24670 otherwise there is no intersection. */
24671 if (right->x <= left->x + left->width)
24672 {
24673 result->x = right->x;
24674
24675 /* The right end of the intersection is the minimum of the
24676 the right ends of left and right. */
24677 result->width = (min (left->x + left->width, right->x + right->width)
24678 - result->x);
24679
24680 /* Same game for Y. */
24681 if (r1->y < r2->y)
24682 upper = r1, lower = r2;
24683 else
24684 upper = r2, lower = r1;
24685
24686 /* The upper end of the intersection is lower.y0, if this is inside
24687 of upper. Otherwise, there is no intersection. */
24688 if (lower->y <= upper->y + upper->height)
24689 {
24690 result->y = lower->y;
24691
24692 /* The lower end of the intersection is the minimum of the lower
24693 ends of upper and lower. */
24694 result->height = (min (lower->y + lower->height,
24695 upper->y + upper->height)
24696 - result->y);
24697 intersection_p = 1;
24698 }
24699 }
24700
24701 return intersection_p;
24702 }
24703
24704 #endif /* HAVE_WINDOW_SYSTEM */
24705
24706 \f
24707 /***********************************************************************
24708 Initialization
24709 ***********************************************************************/
24710
24711 void
24712 syms_of_xdisp ()
24713 {
24714 Vwith_echo_area_save_vector = Qnil;
24715 staticpro (&Vwith_echo_area_save_vector);
24716
24717 Vmessage_stack = Qnil;
24718 staticpro (&Vmessage_stack);
24719
24720 Qinhibit_redisplay = intern ("inhibit-redisplay");
24721 staticpro (&Qinhibit_redisplay);
24722
24723 message_dolog_marker1 = Fmake_marker ();
24724 staticpro (&message_dolog_marker1);
24725 message_dolog_marker2 = Fmake_marker ();
24726 staticpro (&message_dolog_marker2);
24727 message_dolog_marker3 = Fmake_marker ();
24728 staticpro (&message_dolog_marker3);
24729
24730 #if GLYPH_DEBUG
24731 defsubr (&Sdump_frame_glyph_matrix);
24732 defsubr (&Sdump_glyph_matrix);
24733 defsubr (&Sdump_glyph_row);
24734 defsubr (&Sdump_tool_bar_row);
24735 defsubr (&Strace_redisplay);
24736 defsubr (&Strace_to_stderr);
24737 #endif
24738 #ifdef HAVE_WINDOW_SYSTEM
24739 defsubr (&Stool_bar_lines_needed);
24740 defsubr (&Slookup_image_map);
24741 #endif
24742 defsubr (&Sformat_mode_line);
24743 defsubr (&Sinvisible_p);
24744
24745 staticpro (&Qmenu_bar_update_hook);
24746 Qmenu_bar_update_hook = intern ("menu-bar-update-hook");
24747
24748 staticpro (&Qoverriding_terminal_local_map);
24749 Qoverriding_terminal_local_map = intern ("overriding-terminal-local-map");
24750
24751 staticpro (&Qoverriding_local_map);
24752 Qoverriding_local_map = intern ("overriding-local-map");
24753
24754 staticpro (&Qwindow_scroll_functions);
24755 Qwindow_scroll_functions = intern ("window-scroll-functions");
24756
24757 staticpro (&Qwindow_text_change_functions);
24758 Qwindow_text_change_functions = intern ("window-text-change-functions");
24759
24760 staticpro (&Qredisplay_end_trigger_functions);
24761 Qredisplay_end_trigger_functions = intern ("redisplay-end-trigger-functions");
24762
24763 staticpro (&Qinhibit_point_motion_hooks);
24764 Qinhibit_point_motion_hooks = intern ("inhibit-point-motion-hooks");
24765
24766 Qeval = intern ("eval");
24767 staticpro (&Qeval);
24768
24769 QCdata = intern (":data");
24770 staticpro (&QCdata);
24771 Qdisplay = intern ("display");
24772 staticpro (&Qdisplay);
24773 Qspace_width = intern ("space-width");
24774 staticpro (&Qspace_width);
24775 Qraise = intern ("raise");
24776 staticpro (&Qraise);
24777 Qslice = intern ("slice");
24778 staticpro (&Qslice);
24779 Qspace = intern ("space");
24780 staticpro (&Qspace);
24781 Qmargin = intern ("margin");
24782 staticpro (&Qmargin);
24783 Qpointer = intern ("pointer");
24784 staticpro (&Qpointer);
24785 Qleft_margin = intern ("left-margin");
24786 staticpro (&Qleft_margin);
24787 Qright_margin = intern ("right-margin");
24788 staticpro (&Qright_margin);
24789 Qcenter = intern ("center");
24790 staticpro (&Qcenter);
24791 Qline_height = intern ("line-height");
24792 staticpro (&Qline_height);
24793 QCalign_to = intern (":align-to");
24794 staticpro (&QCalign_to);
24795 QCrelative_width = intern (":relative-width");
24796 staticpro (&QCrelative_width);
24797 QCrelative_height = intern (":relative-height");
24798 staticpro (&QCrelative_height);
24799 QCeval = intern (":eval");
24800 staticpro (&QCeval);
24801 QCpropertize = intern (":propertize");
24802 staticpro (&QCpropertize);
24803 QCfile = intern (":file");
24804 staticpro (&QCfile);
24805 Qfontified = intern ("fontified");
24806 staticpro (&Qfontified);
24807 Qfontification_functions = intern ("fontification-functions");
24808 staticpro (&Qfontification_functions);
24809 Qtrailing_whitespace = intern ("trailing-whitespace");
24810 staticpro (&Qtrailing_whitespace);
24811 Qescape_glyph = intern ("escape-glyph");
24812 staticpro (&Qescape_glyph);
24813 Qnobreak_space = intern ("nobreak-space");
24814 staticpro (&Qnobreak_space);
24815 Qimage = intern ("image");
24816 staticpro (&Qimage);
24817 QCmap = intern (":map");
24818 staticpro (&QCmap);
24819 QCpointer = intern (":pointer");
24820 staticpro (&QCpointer);
24821 Qrect = intern ("rect");
24822 staticpro (&Qrect);
24823 Qcircle = intern ("circle");
24824 staticpro (&Qcircle);
24825 Qpoly = intern ("poly");
24826 staticpro (&Qpoly);
24827 Qmessage_truncate_lines = intern ("message-truncate-lines");
24828 staticpro (&Qmessage_truncate_lines);
24829 Qgrow_only = intern ("grow-only");
24830 staticpro (&Qgrow_only);
24831 Qinhibit_menubar_update = intern ("inhibit-menubar-update");
24832 staticpro (&Qinhibit_menubar_update);
24833 Qinhibit_eval_during_redisplay = intern ("inhibit-eval-during-redisplay");
24834 staticpro (&Qinhibit_eval_during_redisplay);
24835 Qposition = intern ("position");
24836 staticpro (&Qposition);
24837 Qbuffer_position = intern ("buffer-position");
24838 staticpro (&Qbuffer_position);
24839 Qobject = intern ("object");
24840 staticpro (&Qobject);
24841 Qbar = intern ("bar");
24842 staticpro (&Qbar);
24843 Qhbar = intern ("hbar");
24844 staticpro (&Qhbar);
24845 Qbox = intern ("box");
24846 staticpro (&Qbox);
24847 Qhollow = intern ("hollow");
24848 staticpro (&Qhollow);
24849 Qhand = intern ("hand");
24850 staticpro (&Qhand);
24851 Qarrow = intern ("arrow");
24852 staticpro (&Qarrow);
24853 Qtext = intern ("text");
24854 staticpro (&Qtext);
24855 Qrisky_local_variable = intern ("risky-local-variable");
24856 staticpro (&Qrisky_local_variable);
24857 Qinhibit_free_realized_faces = intern ("inhibit-free-realized-faces");
24858 staticpro (&Qinhibit_free_realized_faces);
24859
24860 list_of_error = Fcons (Fcons (intern ("error"),
24861 Fcons (intern ("void-variable"), Qnil)),
24862 Qnil);
24863 staticpro (&list_of_error);
24864
24865 Qlast_arrow_position = intern ("last-arrow-position");
24866 staticpro (&Qlast_arrow_position);
24867 Qlast_arrow_string = intern ("last-arrow-string");
24868 staticpro (&Qlast_arrow_string);
24869
24870 Qoverlay_arrow_string = intern ("overlay-arrow-string");
24871 staticpro (&Qoverlay_arrow_string);
24872 Qoverlay_arrow_bitmap = intern ("overlay-arrow-bitmap");
24873 staticpro (&Qoverlay_arrow_bitmap);
24874
24875 echo_buffer[0] = echo_buffer[1] = Qnil;
24876 staticpro (&echo_buffer[0]);
24877 staticpro (&echo_buffer[1]);
24878
24879 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
24880 staticpro (&echo_area_buffer[0]);
24881 staticpro (&echo_area_buffer[1]);
24882
24883 Vmessages_buffer_name = build_string ("*Messages*");
24884 staticpro (&Vmessages_buffer_name);
24885
24886 mode_line_proptrans_alist = Qnil;
24887 staticpro (&mode_line_proptrans_alist);
24888 mode_line_string_list = Qnil;
24889 staticpro (&mode_line_string_list);
24890 mode_line_string_face = Qnil;
24891 staticpro (&mode_line_string_face);
24892 mode_line_string_face_prop = Qnil;
24893 staticpro (&mode_line_string_face_prop);
24894 Vmode_line_unwind_vector = Qnil;
24895 staticpro (&Vmode_line_unwind_vector);
24896
24897 help_echo_string = Qnil;
24898 staticpro (&help_echo_string);
24899 help_echo_object = Qnil;
24900 staticpro (&help_echo_object);
24901 help_echo_window = Qnil;
24902 staticpro (&help_echo_window);
24903 previous_help_echo_string = Qnil;
24904 staticpro (&previous_help_echo_string);
24905 help_echo_pos = -1;
24906
24907 #ifdef HAVE_WINDOW_SYSTEM
24908 DEFVAR_BOOL ("x-stretch-cursor", &x_stretch_cursor_p,
24909 doc: /* *Non-nil means draw block cursor as wide as the glyph under it.
24910 For example, if a block cursor is over a tab, it will be drawn as
24911 wide as that tab on the display. */);
24912 x_stretch_cursor_p = 0;
24913 #endif
24914
24915 DEFVAR_LISP ("show-trailing-whitespace", &Vshow_trailing_whitespace,
24916 doc: /* *Non-nil means highlight trailing whitespace.
24917 The face used for trailing whitespace is `trailing-whitespace'. */);
24918 Vshow_trailing_whitespace = Qnil;
24919
24920 DEFVAR_LISP ("nobreak-char-display", &Vnobreak_char_display,
24921 doc: /* *Control highlighting of nobreak space and soft hyphen.
24922 A value of t means highlight the character itself (for nobreak space,
24923 use face `nobreak-space').
24924 A value of nil means no highlighting.
24925 Other values mean display the escape glyph followed by an ordinary
24926 space or ordinary hyphen. */);
24927 Vnobreak_char_display = Qt;
24928
24929 DEFVAR_LISP ("void-text-area-pointer", &Vvoid_text_area_pointer,
24930 doc: /* *The pointer shape to show in void text areas.
24931 A value of nil means to show the text pointer. Other options are `arrow',
24932 `text', `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
24933 Vvoid_text_area_pointer = Qarrow;
24934
24935 DEFVAR_LISP ("inhibit-redisplay", &Vinhibit_redisplay,
24936 doc: /* Non-nil means don't actually do any redisplay.
24937 This is used for internal purposes. */);
24938 Vinhibit_redisplay = Qnil;
24939
24940 DEFVAR_LISP ("global-mode-string", &Vglobal_mode_string,
24941 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
24942 Vglobal_mode_string = Qnil;
24943
24944 DEFVAR_LISP ("overlay-arrow-position", &Voverlay_arrow_position,
24945 doc: /* Marker for where to display an arrow on top of the buffer text.
24946 This must be the beginning of a line in order to work.
24947 See also `overlay-arrow-string'. */);
24948 Voverlay_arrow_position = Qnil;
24949
24950 DEFVAR_LISP ("overlay-arrow-string", &Voverlay_arrow_string,
24951 doc: /* String to display as an arrow in non-window frames.
24952 See also `overlay-arrow-position'. */);
24953 Voverlay_arrow_string = build_string ("=>");
24954
24955 DEFVAR_LISP ("overlay-arrow-variable-list", &Voverlay_arrow_variable_list,
24956 doc: /* List of variables (symbols) which hold markers for overlay arrows.
24957 The symbols on this list are examined during redisplay to determine
24958 where to display overlay arrows. */);
24959 Voverlay_arrow_variable_list
24960 = Fcons (intern ("overlay-arrow-position"), Qnil);
24961
24962 DEFVAR_INT ("scroll-step", &scroll_step,
24963 doc: /* *The number of lines to try scrolling a window by when point moves out.
24964 If that fails to bring point back on frame, point is centered instead.
24965 If this is zero, point is always centered after it moves off frame.
24966 If you want scrolling to always be a line at a time, you should set
24967 `scroll-conservatively' to a large value rather than set this to 1. */);
24968
24969 DEFVAR_INT ("scroll-conservatively", &scroll_conservatively,
24970 doc: /* *Scroll up to this many lines, to bring point back on screen.
24971 If point moves off-screen, redisplay will scroll by up to
24972 `scroll-conservatively' lines in order to bring point just barely
24973 onto the screen again. If that cannot be done, then redisplay
24974 recenters point as usual.
24975
24976 A value of zero means always recenter point if it moves off screen. */);
24977 scroll_conservatively = 0;
24978
24979 DEFVAR_INT ("scroll-margin", &scroll_margin,
24980 doc: /* *Number of lines of margin at the top and bottom of a window.
24981 Recenter the window whenever point gets within this many lines
24982 of the top or bottom of the window. */);
24983 scroll_margin = 0;
24984
24985 DEFVAR_LISP ("display-pixels-per-inch", &Vdisplay_pixels_per_inch,
24986 doc: /* Pixels per inch value for non-window system displays.
24987 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
24988 Vdisplay_pixels_per_inch = make_float (72.0);
24989
24990 #if GLYPH_DEBUG
24991 DEFVAR_INT ("debug-end-pos", &debug_end_pos, doc: /* Don't ask. */);
24992 #endif
24993
24994 DEFVAR_LISP ("truncate-partial-width-windows",
24995 &Vtruncate_partial_width_windows,
24996 doc: /* Non-nil means truncate lines in windows with less than the frame width.
24997 For an integer value, truncate lines in each window with less than the
24998 full frame width, provided the window width is less than that integer;
24999 otherwise, respect the value of `truncate-lines'.
25000
25001 For any other non-nil value, truncate lines in all windows with
25002 less than the full frame width.
25003
25004 A value of nil means to respect the value of `truncate-lines'.
25005
25006 If `word-wrap' is enabled, you might want to reduce this. */);
25007 Vtruncate_partial_width_windows = make_number (50);
25008
25009 DEFVAR_BOOL ("mode-line-inverse-video", &mode_line_inverse_video,
25010 doc: /* When nil, display the mode-line/header-line/menu-bar in the default face.
25011 Any other value means to use the appropriate face, `mode-line',
25012 `header-line', or `menu' respectively. */);
25013 mode_line_inverse_video = 1;
25014
25015 DEFVAR_LISP ("line-number-display-limit", &Vline_number_display_limit,
25016 doc: /* *Maximum buffer size for which line number should be displayed.
25017 If the buffer is bigger than this, the line number does not appear
25018 in the mode line. A value of nil means no limit. */);
25019 Vline_number_display_limit = Qnil;
25020
25021 DEFVAR_INT ("line-number-display-limit-width",
25022 &line_number_display_limit_width,
25023 doc: /* *Maximum line width (in characters) for line number display.
25024 If the average length of the lines near point is bigger than this, then the
25025 line number may be omitted from the mode line. */);
25026 line_number_display_limit_width = 200;
25027
25028 DEFVAR_BOOL ("highlight-nonselected-windows", &highlight_nonselected_windows,
25029 doc: /* *Non-nil means highlight region even in nonselected windows. */);
25030 highlight_nonselected_windows = 0;
25031
25032 DEFVAR_BOOL ("multiple-frames", &multiple_frames,
25033 doc: /* Non-nil if more than one frame is visible on this display.
25034 Minibuffer-only frames don't count, but iconified frames do.
25035 This variable is not guaranteed to be accurate except while processing
25036 `frame-title-format' and `icon-title-format'. */);
25037
25038 DEFVAR_LISP ("frame-title-format", &Vframe_title_format,
25039 doc: /* Template for displaying the title bar of visible frames.
25040 \(Assuming the window manager supports this feature.)
25041
25042 This variable has the same structure as `mode-line-format', except that
25043 the %c and %l constructs are ignored. It is used only on frames for
25044 which no explicit name has been set \(see `modify-frame-parameters'). */);
25045
25046 DEFVAR_LISP ("icon-title-format", &Vicon_title_format,
25047 doc: /* Template for displaying the title bar of an iconified frame.
25048 \(Assuming the window manager supports this feature.)
25049 This variable has the same structure as `mode-line-format' (which see),
25050 and is used only on frames for which no explicit name has been set
25051 \(see `modify-frame-parameters'). */);
25052 Vicon_title_format
25053 = Vframe_title_format
25054 = Fcons (intern ("multiple-frames"),
25055 Fcons (build_string ("%b"),
25056 Fcons (Fcons (empty_unibyte_string,
25057 Fcons (intern ("invocation-name"),
25058 Fcons (build_string ("@"),
25059 Fcons (intern ("system-name"),
25060 Qnil)))),
25061 Qnil)));
25062
25063 DEFVAR_LISP ("message-log-max", &Vmessage_log_max,
25064 doc: /* Maximum number of lines to keep in the message log buffer.
25065 If nil, disable message logging. If t, log messages but don't truncate
25066 the buffer when it becomes large. */);
25067 Vmessage_log_max = make_number (100);
25068
25069 DEFVAR_LISP ("window-size-change-functions", &Vwindow_size_change_functions,
25070 doc: /* Functions called before redisplay, if window sizes have changed.
25071 The value should be a list of functions that take one argument.
25072 Just before redisplay, for each frame, if any of its windows have changed
25073 size since the last redisplay, or have been split or deleted,
25074 all the functions in the list are called, with the frame as argument. */);
25075 Vwindow_size_change_functions = Qnil;
25076
25077 DEFVAR_LISP ("window-scroll-functions", &Vwindow_scroll_functions,
25078 doc: /* List of functions to call before redisplaying a window with scrolling.
25079 Each function is called with two arguments, the window
25080 and its new display-start position. Note that the value of `window-end'
25081 is not valid when these functions are called. */);
25082 Vwindow_scroll_functions = Qnil;
25083
25084 DEFVAR_LISP ("window-text-change-functions",
25085 &Vwindow_text_change_functions,
25086 doc: /* Functions to call in redisplay when text in the window might change. */);
25087 Vwindow_text_change_functions = Qnil;
25088
25089 DEFVAR_LISP ("redisplay-end-trigger-functions", &Vredisplay_end_trigger_functions,
25090 doc: /* Functions called when redisplay of a window reaches the end trigger.
25091 Each function is called with two arguments, the window and the end trigger value.
25092 See `set-window-redisplay-end-trigger'. */);
25093 Vredisplay_end_trigger_functions = Qnil;
25094
25095 DEFVAR_LISP ("mouse-autoselect-window", &Vmouse_autoselect_window,
25096 doc: /* *Non-nil means autoselect window with mouse pointer.
25097 If nil, do not autoselect windows.
25098 A positive number means delay autoselection by that many seconds: a
25099 window is autoselected only after the mouse has remained in that
25100 window for the duration of the delay.
25101 A negative number has a similar effect, but causes windows to be
25102 autoselected only after the mouse has stopped moving. \(Because of
25103 the way Emacs compares mouse events, you will occasionally wait twice
25104 that time before the window gets selected.\)
25105 Any other value means to autoselect window instantaneously when the
25106 mouse pointer enters it.
25107
25108 Autoselection selects the minibuffer only if it is active, and never
25109 unselects the minibuffer if it is active.
25110
25111 When customizing this variable make sure that the actual value of
25112 `focus-follows-mouse' matches the behavior of your window manager. */);
25113 Vmouse_autoselect_window = Qnil;
25114
25115 DEFVAR_LISP ("auto-resize-tool-bars", &Vauto_resize_tool_bars,
25116 doc: /* *Non-nil means automatically resize tool-bars.
25117 This dynamically changes the tool-bar's height to the minimum height
25118 that is needed to make all tool-bar items visible.
25119 If value is `grow-only', the tool-bar's height is only increased
25120 automatically; to decrease the tool-bar height, use \\[recenter]. */);
25121 Vauto_resize_tool_bars = Qt;
25122
25123 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", &auto_raise_tool_bar_buttons_p,
25124 doc: /* *Non-nil means raise tool-bar buttons when the mouse moves over them. */);
25125 auto_raise_tool_bar_buttons_p = 1;
25126
25127 DEFVAR_BOOL ("make-cursor-line-fully-visible", &make_cursor_line_fully_visible_p,
25128 doc: /* *Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
25129 make_cursor_line_fully_visible_p = 1;
25130
25131 DEFVAR_LISP ("tool-bar-border", &Vtool_bar_border,
25132 doc: /* *Border below tool-bar in pixels.
25133 If an integer, use it as the height of the border.
25134 If it is one of `internal-border-width' or `border-width', use the
25135 value of the corresponding frame parameter.
25136 Otherwise, no border is added below the tool-bar. */);
25137 Vtool_bar_border = Qinternal_border_width;
25138
25139 DEFVAR_LISP ("tool-bar-button-margin", &Vtool_bar_button_margin,
25140 doc: /* *Margin around tool-bar buttons in pixels.
25141 If an integer, use that for both horizontal and vertical margins.
25142 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
25143 HORZ specifying the horizontal margin, and VERT specifying the
25144 vertical margin. */);
25145 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
25146
25147 DEFVAR_INT ("tool-bar-button-relief", &tool_bar_button_relief,
25148 doc: /* *Relief thickness of tool-bar buttons. */);
25149 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
25150
25151 DEFVAR_LISP ("fontification-functions", &Vfontification_functions,
25152 doc: /* List of functions to call to fontify regions of text.
25153 Each function is called with one argument POS. Functions must
25154 fontify a region starting at POS in the current buffer, and give
25155 fontified regions the property `fontified'. */);
25156 Vfontification_functions = Qnil;
25157 Fmake_variable_buffer_local (Qfontification_functions);
25158
25159 DEFVAR_BOOL ("unibyte-display-via-language-environment",
25160 &unibyte_display_via_language_environment,
25161 doc: /* *Non-nil means display unibyte text according to language environment.
25162 Specifically this means that unibyte non-ASCII characters
25163 are displayed by converting them to the equivalent multibyte characters
25164 according to the current language environment. As a result, they are
25165 displayed according to the current fontset. */);
25166 unibyte_display_via_language_environment = 0;
25167
25168 DEFVAR_LISP ("max-mini-window-height", &Vmax_mini_window_height,
25169 doc: /* *Maximum height for resizing mini-windows.
25170 If a float, it specifies a fraction of the mini-window frame's height.
25171 If an integer, it specifies a number of lines. */);
25172 Vmax_mini_window_height = make_float (0.25);
25173
25174 DEFVAR_LISP ("resize-mini-windows", &Vresize_mini_windows,
25175 doc: /* *How to resize mini-windows.
25176 A value of nil means don't automatically resize mini-windows.
25177 A value of t means resize them to fit the text displayed in them.
25178 A value of `grow-only', the default, means let mini-windows grow
25179 only, until their display becomes empty, at which point the windows
25180 go back to their normal size. */);
25181 Vresize_mini_windows = Qgrow_only;
25182
25183 DEFVAR_LISP ("blink-cursor-alist", &Vblink_cursor_alist,
25184 doc: /* Alist specifying how to blink the cursor off.
25185 Each element has the form (ON-STATE . OFF-STATE). Whenever the
25186 `cursor-type' frame-parameter or variable equals ON-STATE,
25187 comparing using `equal', Emacs uses OFF-STATE to specify
25188 how to blink it off. ON-STATE and OFF-STATE are values for
25189 the `cursor-type' frame parameter.
25190
25191 If a frame's ON-STATE has no entry in this list,
25192 the frame's other specifications determine how to blink the cursor off. */);
25193 Vblink_cursor_alist = Qnil;
25194
25195 DEFVAR_BOOL ("auto-hscroll-mode", &automatic_hscrolling_p,
25196 doc: /* *Non-nil means scroll the display automatically to make point visible. */);
25197 automatic_hscrolling_p = 1;
25198 Qauto_hscroll_mode = intern ("auto-hscroll-mode");
25199 staticpro (&Qauto_hscroll_mode);
25200
25201 DEFVAR_INT ("hscroll-margin", &hscroll_margin,
25202 doc: /* *How many columns away from the window edge point is allowed to get
25203 before automatic hscrolling will horizontally scroll the window. */);
25204 hscroll_margin = 5;
25205
25206 DEFVAR_LISP ("hscroll-step", &Vhscroll_step,
25207 doc: /* *How many columns to scroll the window when point gets too close to the edge.
25208 When point is less than `hscroll-margin' columns from the window
25209 edge, automatic hscrolling will scroll the window by the amount of columns
25210 determined by this variable. If its value is a positive integer, scroll that
25211 many columns. If it's a positive floating-point number, it specifies the
25212 fraction of the window's width to scroll. If it's nil or zero, point will be
25213 centered horizontally after the scroll. Any other value, including negative
25214 numbers, are treated as if the value were zero.
25215
25216 Automatic hscrolling always moves point outside the scroll margin, so if
25217 point was more than scroll step columns inside the margin, the window will
25218 scroll more than the value given by the scroll step.
25219
25220 Note that the lower bound for automatic hscrolling specified by `scroll-left'
25221 and `scroll-right' overrides this variable's effect. */);
25222 Vhscroll_step = make_number (0);
25223
25224 DEFVAR_BOOL ("message-truncate-lines", &message_truncate_lines,
25225 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
25226 Bind this around calls to `message' to let it take effect. */);
25227 message_truncate_lines = 0;
25228
25229 DEFVAR_LISP ("menu-bar-update-hook", &Vmenu_bar_update_hook,
25230 doc: /* Normal hook run to update the menu bar definitions.
25231 Redisplay runs this hook before it redisplays the menu bar.
25232 This is used to update submenus such as Buffers,
25233 whose contents depend on various data. */);
25234 Vmenu_bar_update_hook = Qnil;
25235
25236 DEFVAR_LISP ("menu-updating-frame", &Vmenu_updating_frame,
25237 doc: /* Frame for which we are updating a menu.
25238 The enable predicate for a menu binding should check this variable. */);
25239 Vmenu_updating_frame = Qnil;
25240
25241 DEFVAR_BOOL ("inhibit-menubar-update", &inhibit_menubar_update,
25242 doc: /* Non-nil means don't update menu bars. Internal use only. */);
25243 inhibit_menubar_update = 0;
25244
25245 DEFVAR_LISP ("wrap-prefix", &Vwrap_prefix,
25246 doc: /* Prefix added to the beginning of all continuation lines at display-time.
25247 May be a string, an image, or a stretch-glyph such as used by the
25248 `display' text-property.
25249
25250 This variable is overridden by any `wrap-prefix' text-property.
25251
25252 To add a prefix to non-continuation lines, use the `line-prefix' variable. */);
25253 Vwrap_prefix = Qnil;
25254 staticpro (&Qwrap_prefix);
25255 Qwrap_prefix = intern ("wrap-prefix");
25256 Fmake_variable_buffer_local (Qwrap_prefix);
25257
25258 DEFVAR_LISP ("line-prefix", &Vline_prefix,
25259 doc: /* Prefix added to the beginning of all non-continuation lines at display-time.
25260 May be a string, an image, or a stretch-glyph such as used by the
25261 `display' text-property.
25262
25263 This variable is overridden by any `line-prefix' text-property.
25264
25265 To add a prefix to continuation lines, use the `wrap-prefix' variable. */);
25266 Vline_prefix = Qnil;
25267 staticpro (&Qline_prefix);
25268 Qline_prefix = intern ("line-prefix");
25269 Fmake_variable_buffer_local (Qline_prefix);
25270
25271 DEFVAR_BOOL ("inhibit-eval-during-redisplay", &inhibit_eval_during_redisplay,
25272 doc: /* Non-nil means don't eval Lisp during redisplay. */);
25273 inhibit_eval_during_redisplay = 0;
25274
25275 DEFVAR_BOOL ("inhibit-free-realized-faces", &inhibit_free_realized_faces,
25276 doc: /* Non-nil means don't free realized faces. Internal use only. */);
25277 inhibit_free_realized_faces = 0;
25278
25279 #if GLYPH_DEBUG
25280 DEFVAR_BOOL ("inhibit-try-window-id", &inhibit_try_window_id,
25281 doc: /* Inhibit try_window_id display optimization. */);
25282 inhibit_try_window_id = 0;
25283
25284 DEFVAR_BOOL ("inhibit-try-window-reusing", &inhibit_try_window_reusing,
25285 doc: /* Inhibit try_window_reusing display optimization. */);
25286 inhibit_try_window_reusing = 0;
25287
25288 DEFVAR_BOOL ("inhibit-try-cursor-movement", &inhibit_try_cursor_movement,
25289 doc: /* Inhibit try_cursor_movement display optimization. */);
25290 inhibit_try_cursor_movement = 0;
25291 #endif /* GLYPH_DEBUG */
25292
25293 DEFVAR_INT ("overline-margin", &overline_margin,
25294 doc: /* *Space between overline and text, in pixels.
25295 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
25296 margin to the caracter height. */);
25297 overline_margin = 2;
25298
25299 DEFVAR_INT ("underline-minimum-offset",
25300 &underline_minimum_offset,
25301 doc: /* Minimum distance between baseline and underline.
25302 This can improve legibility of underlined text at small font sizes,
25303 particularly when using variable `x-use-underline-position-properties'
25304 with fonts that specify an UNDERLINE_POSITION relatively close to the
25305 baseline. The default value is 1. */);
25306 underline_minimum_offset = 1;
25307
25308 DEFVAR_BOOL ("display-hourglass", &display_hourglass_p,
25309 doc: /* Non-zero means Emacs displays an hourglass pointer on window systems. */);
25310 display_hourglass_p = 1;
25311
25312 DEFVAR_LISP ("hourglass-delay", &Vhourglass_delay,
25313 doc: /* *Seconds to wait before displaying an hourglass pointer.
25314 Value must be an integer or float. */);
25315 Vhourglass_delay = make_number (DEFAULT_HOURGLASS_DELAY);
25316
25317 hourglass_atimer = NULL;
25318 hourglass_shown_p = 0;
25319 }
25320
25321
25322 /* Initialize this module when Emacs starts. */
25323
25324 void
25325 init_xdisp ()
25326 {
25327 Lisp_Object root_window;
25328 struct window *mini_w;
25329
25330 current_header_line_height = current_mode_line_height = -1;
25331
25332 CHARPOS (this_line_start_pos) = 0;
25333
25334 mini_w = XWINDOW (minibuf_window);
25335 root_window = FRAME_ROOT_WINDOW (XFRAME (WINDOW_FRAME (mini_w)));
25336
25337 if (!noninteractive)
25338 {
25339 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (root_window)));
25340 int i;
25341
25342 XWINDOW (root_window)->top_line = make_number (FRAME_TOP_MARGIN (f));
25343 set_window_height (root_window,
25344 FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f),
25345 0);
25346 mini_w->top_line = make_number (FRAME_LINES (f) - 1);
25347 set_window_height (minibuf_window, 1, 0);
25348
25349 XWINDOW (root_window)->total_cols = make_number (FRAME_COLS (f));
25350 mini_w->total_cols = make_number (FRAME_COLS (f));
25351
25352 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
25353 scratch_glyph_row.glyphs[TEXT_AREA + 1]
25354 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
25355
25356 /* The default ellipsis glyphs `...'. */
25357 for (i = 0; i < 3; ++i)
25358 default_invis_vector[i] = make_number ('.');
25359 }
25360
25361 {
25362 /* Allocate the buffer for frame titles.
25363 Also used for `format-mode-line'. */
25364 int size = 100;
25365 mode_line_noprop_buf = (char *) xmalloc (size);
25366 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
25367 mode_line_noprop_ptr = mode_line_noprop_buf;
25368 mode_line_target = MODE_LINE_DISPLAY;
25369 }
25370
25371 help_echo_showing_p = 0;
25372 }
25373
25374 /* Since w32 does not support atimers, it defines its own implementation of
25375 the following three functions in w32fns.c. */
25376 #ifndef WINDOWSNT
25377
25378 /* Platform-independent portion of hourglass implementation. */
25379
25380 /* Return non-zero if houglass timer has been started or hourglass is shown. */
25381 int
25382 hourglass_started ()
25383 {
25384 return hourglass_shown_p || hourglass_atimer != NULL;
25385 }
25386
25387 /* Cancel a currently active hourglass timer, and start a new one. */
25388 void
25389 start_hourglass ()
25390 {
25391 #if defined (HAVE_WINDOW_SYSTEM)
25392 EMACS_TIME delay;
25393 int secs, usecs = 0;
25394
25395 cancel_hourglass ();
25396
25397 if (INTEGERP (Vhourglass_delay)
25398 && XINT (Vhourglass_delay) > 0)
25399 secs = XFASTINT (Vhourglass_delay);
25400 else if (FLOATP (Vhourglass_delay)
25401 && XFLOAT_DATA (Vhourglass_delay) > 0)
25402 {
25403 Lisp_Object tem;
25404 tem = Ftruncate (Vhourglass_delay, Qnil);
25405 secs = XFASTINT (tem);
25406 usecs = (XFLOAT_DATA (Vhourglass_delay) - secs) * 1000000;
25407 }
25408 else
25409 secs = DEFAULT_HOURGLASS_DELAY;
25410
25411 EMACS_SET_SECS_USECS (delay, secs, usecs);
25412 hourglass_atimer = start_atimer (ATIMER_RELATIVE, delay,
25413 show_hourglass, NULL);
25414 #endif
25415 }
25416
25417
25418 /* Cancel the hourglass cursor timer if active, hide a busy cursor if
25419 shown. */
25420 void
25421 cancel_hourglass ()
25422 {
25423 #if defined (HAVE_WINDOW_SYSTEM)
25424 if (hourglass_atimer)
25425 {
25426 cancel_atimer (hourglass_atimer);
25427 hourglass_atimer = NULL;
25428 }
25429
25430 if (hourglass_shown_p)
25431 hide_hourglass ();
25432 #endif
25433 }
25434 #endif /* ! WINDOWSNT */
25435
25436 /* arch-tag: eacc864d-bb6a-4b74-894a-1a4399a1358b
25437 (do not change this comment) */